Larry is very bad at math — he usually uses a calculator, which workedwell throughout college. Unforunately, he is now struck in a desertedisland with his good buddy Ryan after a snowboarding accident.They’re now trying to spend some time figuring out some goodproblems, and Ryan will eat Larry if he cannot answer, so his fate isup to you!It’s a very simple problem — given a number N, how many wayscan K numbers less than N add up to N?For example, for N = 20 and K = 2, there are 21 ways:
0+20
1+192
+18
3+17
4+16
5+15.
..
18+2
19+1
20+0
题意: 给你一个数字N,给你分成的份数K份,问你有多少个分法。
思路:n只有100,可以先把所有的答案预处理出来。
比如3 3的时候,他其实等于0 2,1 2, 2 2 ,3 2
就是分成3份,你可以先给1份,定下来是多少(比如3的时候,给1份是0,1,2,3,)
然后剩下来的就是分成2份。而2份的分法之前比数字N多1个。
就这样递推,可以把所有的情况可以推出来。
#include<stdio.h>
#include<string.h>
int num[101][101];
const int mod=1000000;
void init()
{
memset(num,0,sizeof(num));
for(int i=0; i<=100; i++)
{
num[i][2]=i+1;
num[i][1]=1;
num[0][i]=1;
}
for(int k=3; k<=100; k++)
for(int j=1; j<=100; j++)
for(int i=0; i<=j; i++)
{
num[j][k]+=num[i][k-1];
num[j][k]%=mod;
}
// int k=4;
// int j=4;
// for(int i=0; i<=j; i++)
// {
// printf("%d %d %d %d\n",j,k,i,k-1);
// printf("%d %d\n",num[j][k],num[i][k-1]);
// }
}
int main()
{
init();
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)break;
printf("%d\n",num[n][m]);
}
return 0;
}
/*
3 3 9
*/