UVA 10943(How do you add?)

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

*/



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章