-3

Also, how can algorithmicly generate them within polynomial of n? Pseudo-code is ok.

MFSO1991
  • 79
  • 5
  • Why people hate my question? – MFSO1991 Sep 28 '14 at 23:00
  • The question is not really programming related, and SO requires a minimum amout of effort from your part. Do you understand my answer below? – jvdhooft Sep 28 '14 at 23:08
  • @JeroenvanderHooft I'm trying. But for now it's not making sense. I can't even tell if its correct or not. By the way, this is the problem I encountered in algorithm design class. How could it not programming related? – MFSO1991 Sep 28 '14 at 23:10
  • Well, suppose you have `n` equal items you want to divide among `k` people. If you lay out all of these items in a row, you can draw `k-1` borders: all of the items at the left of the first border go to the first person, all of the items to the right of the first and to the left of the second border go to the second etc. Now, you have `n+k-1` possible positions: `n` for all the items, `k-1` for all the borders. You need to select `k-1` positions to put the borders, out of `n+k-1` possible positions. The amount of ways to do this is thus `C(k-1,n+k-1)`. – jvdhooft Sep 28 '14 at 23:17

1 Answers1

0

This problem comes down to selecting k-1 borders, which divide n elements in k parts. As such, k-1 positions need to be selected out of n+k-1 positions, resulting in

enter image description here

possibilities. As an example, let's say we need to divide four sweets among three children. In this case, a first possibility would be to put the two borders on the first two positions, leaving the four sweets in positions 3-6:

enter image description here

As such, the first two children get no candy, while the third child gets all four. Another possibility would be to put the first border at position 2 and the second border at position 4:

enter image description here

Now the first two children get one candy each (positions 1 and 3), while the third child gets two (positions 5-6). Iterating over all positions results in a total of 15 possibilities.

Note that the answer is different when all volumes need to contain at least one chapter. In this case, k items are already given to one volume each, leaving n-k chapters. As such, there are

enter image description here

possibilities. Note that the condition k<n is important in this case.

jvdhooft
  • 657
  • 1
  • 12
  • 33