Ignatius and the Princess III
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9077 Accepted Submission(s): 6389
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!"
Input
The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file.
Output
For each test case, you have to output a line contains an integer P which indicate the different equations you have found.
Sample Input
4 10 20
Sample Output
5 42 627
代码一:-----母函数解法:
构造母函数如下:
G(x)=(1+x+x^2+x^3+...+x^n)*(1+x^2+x^4+...)*(1+x^3+x^6+...)*...*(1+x^n);
G(x)=(1+x+x^2+x^3+...+x^n)*(1+x^2+x^4+...)*(1+x^3+x^6+...)*...*(1+x^n);
1 #include <cstdio> 2 #include <iostream> 3 4 using namespace std; 5 6 int main() 7 { 8 int n; 9 int c1[125], c2[125]; 10 11 while(~scanf("%d", &n)) 12 { 13 for(int i = 0; i <= n; ++i) 14 { 15 c1[i] = 1; 16 c2[i] = 0; 17 } 18 for(int i = 2; i <= n; ++i) 19 { 20 for(int j = 0; j <= n; ++j) 21 { 22 for(int k = 0; k <= n/i*i; k += i) 23 c2[j+k] += c1[j]; 24 } 25 for(int j = 0; j <= n; ++j) 26 { 27 c1[j] = c2[j]; 28 c2[j] = 0; 29 } 30 } 31 printf("%d\n", c1[n]); 32 } 33 return 0; 34 }
代码二:递归做法:
1 #include <cstdio> 2 #include <iostream> 3 4 using namespace std; 5 int f[125][125]; 6 7 int fun(int a, int b) // fun(n, m)表示将整数 n 划分为最大数不超过 m 的划分 8 { 9 if(f[a][b]) 10 return f[a][b]; 11 if(a == 1 || b == 1) 12 return 1; 13 if(a < b) 14 return f[a][b] = fun(a, a); 15 if(a == b) 16 return f[a][b] = 1 + fun(a, b-1); 17 if(a > b) 18 return f[a][b] = fun(a-b, b) + fun(a, b-1); 19 } 20 21 int main() 22 { 23 int n; 24 memset(f, 0, sizeof(f)); 25 while(~scanf("%d", &n)) 26 { 27 printf("%d\n", fun(n, n)); 28 } 29 return 0; 30 }