Very bare partition method.
Refer to the Wikipedia's explanation of the partition method:
Now there are 10 balls. Put them in three boxes.
-
●●●●●●●●●●
Split 10 balls into three parts after two boards
-
● | ●●●●●●●●, ● | ●● | ●●●●●●●, ●|●●●●|●●●●●●, ● | ●●●● | ●●●●●, ● | ●●●●● | ●●●●●, ●|●●●●●●|●●● ,.. ....
Similarly, the total number of methods for putting 10 balls into three boxes is
The total number of methods for putting n balls into k boxes is
The problem is equivalent to the number of feasible solutions, which is a positive integer.
** If empty boxes are allowed **:
Now there are 10 balls. Put them in three boxes and allow empty boxes. Consider 10 + 3 balls:
-
● | ●●●●●●●●●●●
Take one of the three boxes to get a situation, and so on:
-
| ●●●●●●●●●●●, | ● | ●●●●●●●●●, |●●●|●●●●●●●●, | ●●● | ●●●●●●●, |●●●● | ●●●●●● ,......
The total number of methods for putting n balls into k boxes (empty boxes allowed) is[2]
The problem is equivalent to the number of feasible solutions, where it is a non-negative integer.
It is also the number of expanded items, because after expansion, each item must be A1 ^ X1 * A2 ^ X2 *...... * ak ^ XK, and X1 + X2 +... + XK = n. that's the problem.
Another type of Deformation:
Partition Method for reducing the number of balls
Put 20 identical balls into four boxes numbered 1, 2, 3, 4, respectively. The number of balls in each box must be no less than the number of balls in each box.
Analysis: first, put 0, 1, 2, and 3 balls in the four boxes numbered 1, 2, 3, and 4 respectively, with 14 balls with no difference, the problem is equivalent to placing 14 balls in four boxes numbered 1, 2, 3, and 4. Each box has at least one ball.
The remaining 14 non-differentiated balls are arranged in a column to form a total of 13 null balls. It can be understood that there are 3 partitions, And the balls in a column are separated into 4 segments, each segment must have at least one, with C3/13 = 286 (type ).
If you do not need the partition method, you can also do it again:
The number of the last addition is several. ans [N] [k] = ans [n-1] [k] + ans [N] [k-1]. ans [N] [k-1] is the last plus 0, ANS [n-1] [k] is the last plus is not 0.
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<map>#include<set>#include<vector>#include<algorithm>#include<stack>#include<queue>using namespace std;#define INF 1000000000#define eps 1e-8#define pii pair<int,int>#define LL long long intconst int mod=1000000;int n,k,ans,c[250][250];int main(){ //freopen("in6.txt","r",stdin); //freopen("out.txt","w",stdout); c[0][0]=1; for(int i=1;i<=200;i++) { c[i][0]=c[i][i]=1; for(int j=1;j<i;j++) { c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod; } } while(scanf("%d%d",&n,&k)==2) { if(n==0&&k==0) break; else { printf("%d\n",c[n+k-1][k-1]); } } //fclose(stdin); //fclose(stdout); return 0;}
Uva31643 (partition method)