Question

What is the algorithm to find the number way to separate number M to p part and no two part equal. Example: M = 5, P = 2 they are (1,4) (2,3). If P = 3 then no partition availabe, i.e not (1,2,2) because there two 2 in partition.

Was it helpful?

Solution

In the expanded product

(1+x)(1+x2)(1+x3)...(1+xn)

find the coefficient of x^n. This gives the number of any possibility to represent n as sum of different numbers, i.e., a variable number of terms.


You want the number of possibilites to have

n = i1+i2+...+iP with i1 < i2 < ... < iP

which can be realized by setting

i1=j1, i2=i1+j2=j1+j2, ...

iP=iP-1+jP=j1+j2+...+jP with all jk > 0

so that the original task is the same as counting all the ways that one can solve

n = P * j1+(P-1) * j2+...+1 * jP with all jk > 0, but unrelated among each other.

The corresponding generator function is the product of the geometric series of the powers of x, omitting the constant term,

(x+x2+x3+...) * (x2+x4+x6+...) * (x3+x6+x9+...) * ... * (xP+x2*P+x3*P+...)

= xP*(P+1)/2 * (1+x+x2+...) * (1+x2+x4+...) * (1+x3+x6+...) * ... * (1+xP+x2*P+...)

Clearly, one needs n >= P*(P+1)/2 to get any solution at all. For P=3 that bound is n >= 6, so that n=5 has indeed no solutions in that case.


Algorithm

count = new double[N]
for k=0..N-1 do count[k] = 1
for j=2..P do
    for k=j..N-1 do
        count[k] += count[k-j]

Then count[k] contains the number of combinations for n=P*(P+1)/2+k.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top