HDU 1028Ignatius and the Princess III(母函数简单题)

 Ignatius and the Princess III
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
 

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!" 
 

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
 题意理解:问给出的那个数能有多少种不同的数相加
 1 #include<iostream>
 2 #include<stdio.h>
 3 using namespace std;
 4 int main() {
 5     int N;
 6     int c1[125],c2[125];
 7     while(cin>>N) {
 8         int i,j,k;
 9         for(i=0; i<=N; i++) { //初始化第一个表达式的系数
10             c1[i]=1;
11             c2[i]=0;
12         }
13         for(i=2; i<=N; i++) {
14             //从第二个表达式开始,因为有无限制个,所以有n个表达式
15             for(j=0; j<=N; j++) {
16                 //从累乘的表达式后的一个表达式第一个到最后一个
17                 for(k=0; k+j<=N; k+=i) {
18                     //k为第j个变量的指数,第i个表达式每次累加i
19                     c2[j+k]+=c1[j];
20                 }
21             }
22             for(j=0; j<=N; j++) {
23                 //滚动数组算完一个表达式后更新一次
24                 c1[j]=c2[j];
25                 c2[j]=0;
26             }
27         }
28         printf("%d\n",c1[N]);
29     }
30     return 0;
31 }

 

posted @ 2016-07-22 10:37  April_AA  阅读(156)  评论(0编辑  收藏  举报