hdu 1028 Ignatius and the Princess III
Problem Description
"Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says.
"The second problem is, given an positive integer N, we define an equation like this:
N=a[1]+a[2]+a[3]+...+a[m];
a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!"
"The second problem is, given an positive integer N, we define an equation like this:
N=a[1]+a[2]+a[3]+...+a[m];
a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!"
此题是经典的母函数应用题。
#include<stdio.h> #include<string.h> int c1[121],c2[121]; int main() { int n,i,j,k; while(scanf("%d",&n)!=EOF) { for(i=0;i<=n;i++)//初始化 c1[i]=1; for(i=2;i<=n;i++)//i表示第i个表达式 { for(j=0;j<=n;j++)//j指的是前面的i-1个表达式相乘后得到的表达式的每项的指数 for(k=0;k+j<=n;k+=i)//k表示第i项的每一项的指数 c2[k+j]+=c1[j];//将系数进行合并 for(j=0;j<=n;j++) { c1[j]=c2[j]; c2[j]=0; } } printf("%d\n",c1[n]); } return 0; }