hdu 1028 Ignatius and the Princess III
Posted lu1nacy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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!"
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
这个题可以用整数划分,也可以用母函数的模板
母函数理解参考http://www.wutianqi.com/blog/596.html
母函数模板:
1 #include<bits/stdc++.h> 2 using namespace std; 5 const int _max = 10001; 6 // c1是保存各项质量砝码可以组合的数目 7 // c2是中间量,保存每一次的情况 8 int c1[_max], c2[_max]; 9 int main() 10 { //int n,i,j,k; 11 int nNum; // 12 int i, j, k; 13 while(cin >> nNum) 14 { 15 for(i=0; i<=nNum; ++i) // ---- ① 16 { 17 c1[i] = 1; 18 c2[i] = 0; 19 } 20 for(i=2; i<=nNum; ++i) // ----- ② 21 { 22 for(j=0; j<=nNum; ++j) // ----- ③ 23 for(k=0; k+j<=nNum; k+=i) // ---- ④ 24 { 25 c2[j+k] += c1[j]; 26 } 27 for(j=0; j<=nNum; ++j) // ---- ⑤ 28 { 29 c1[j] = c2[j]; 30 c2[j] = 0; 31 } 32 } 33 cout << c1[nNum] << endl; 34 } 35 return 0; 36 }
本题代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 5 int c1[130], c2[130]; 6 int main() 7 { 8 int nNum; 9 while(cin >> nNum) 10 { 11 // 初始化 12 for(int i=0; i<=nNum; ++i) 13 { 14 c1[i] = 1; 15 c2[i] = 0; 16 } 17 for(int i=2; i<=nNum; ++i) 18 { 19 for(int j=0; j<=nNum; ++j) 20 for(int k=0; k+j<=nNum; k+=i) 21 c2[k+j] += c1[j]; 22 for(int j=0; j<=nNum; ++j) 23 { 24 c1[j] = c2[j]; 25 c2[j] = 0; 26 } 27 } 28 printf("%d ", c1[nNum]); 29 } 30 return 0; 31 }
以上是关于hdu 1028 Ignatius and the Princess III的主要内容,如果未能解决你的问题,请参考以下文章
HDU 1028 Ignatius and the Princess III (动态规划)
hdu 1028 Ignatius and the Princess III
HDU 1028: Ignatius and the Princess III
hdu 1028 Ignatius and the Princess III 母函数