algorithmdynamic-programmingfast-enumeration

How many combinations are there to put n chapters into k(<n)volumes?


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


Solution

  • 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.