Ignatius and the Princess III (HDU 1028) ——母函数(另解DP)
2014-05-19 09:10
495 查看
Ignatius and the Princess III
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11999 Accepted Submission(s): 8494
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!"
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
这是一个典型的母函数问题(当然还有其他解法,比如说背包)
下面是这题的解题代码,也是母函数的模板型代码:
#include<stdio.h> int main() { int n,i,j,k; int c1[1000],c2[1000]; while(~scanf("%d",&n)) { for(i=0; i<=n; i++) c1[i]=1,c2[i]=0; for(i=2; i<=n; i++) { for(j=0; j<=n; j++) for(k=0; k+j<=n; k+=i) c2[k+j]+=c1[j]; for(j=0; j<=n; j++) c1[j]=c2[j],c2[j]=0; } printf("%d\n",c1 ); } return 0; }
下面是别人对代码的一些解释:
#include<iostream> #include<stdio.h> using namespace std; int main() { int N; int c1[125],c2[125]; while(cin>>N) { int i,j,k; for(i=0;i<=N;i++)//初始化第一个表达式的系数 { c1[i]=1; c2[i]=0; } for(i=2;i<=N;i++) {//从第二个表达式开始,因为有无限制个,所以有n个表达式 for(j=0;j<=N;j++) {//从累乘的表达式后的一个表达式第一个到最后一个 for(k=0;k+j<=N;k+=i) {//k为第j个变量的指数,第i个表达式每次累加i c2[j+k]+=c1[j]; } } for(j=0;j<=N;j++) {//滚动数组算完一个表达式后更新一次 c1[j]=c2[j]; c2[j]=0; } } printf("%d\n",c1 ); } return 0; }母函数是一类解决数学中组合问题的方法,它有很多种,不懂可以百度一下“母函数”
另解:
这是有关计数问题的DP中的划分数. 相当于n个无区别的物品,将它们划分成不超过m组(当然这里m=n),求划分方法数. 考虑n的m划分a1+a2+...+am = n, 如果对于每个i都有ai>0, 那么{ai-1}就对应了n-m的m划分. 另外,如果存在ai=0, 那么这就对应啦n的m-1划分. dp[i][j] : j的i划分的总数. 综上,我们可得出如下递推关系: dp[i][j] = dp[i][j-i] + dp[i-1][j].
/************************************************************************* > File Name: dp.cpp > Author: tj > Mail: 545061367@qq.com > Created Time: 2015年01月02日 星期五 14时24分28秒 ************************************************************************/ #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int main() { int n, dp[130][130]; while(~scanf("%d%d", &n,&m)) { memset(dp, 0, sizeof(dp)); dp[0][0] = 1; for(int i=1; i<=n; i++) { for(int j=0; j<=m; j++) { if(j >= i) dp[i][j] = dp[i][j-i]+dp[i-1][j]; else dp[i][j] = dp[i-1][j]; } } printf("%d\n", dp[m] ); } return 0; }
相关文章推荐
- HDU 1028 Ignatius and the Princess III(dp 母函数)
- hdu 1028 Ignatius and the Princess III 整数拆分 母函数/dp
- HDU 1028 Ignatius and the Princess III(母函数 或者 整数划分的DP动态规划)
- HDU 1028 Ignatius and the Princess III(母函数或dp)
- hdu 1028 Ignatius and the Princess III 母函数/dp
- HDU_1028_Ignatius and the Princess III_(母函数,dp)
- HDU 1028 Ignatius and the Princess III (母函数,或者DP)
- HDU 1028 Ignatius and the Princess III (母函数 或 动态规划DP)
- HDU 1028 Ignatius and the Princess III (母函数 , DP)
- HDU 1028 Ignatius and the Princess III (母函数或者dp,找规律,)
- hdu--1028--dp||递推||母函数
- hdu 1028 Ignatius and the Princess III(dp||母函数)
- HDU 1028 Ignatius and the Princess III(DP,经典母函数问题)
- hdu 1028 Ignatius and the Princess III(整数拆分,母函数或dp)
- 母函数初级运用 hdu 1028 、hdu 1398、hdu 1085
- HDU 1028 Ignatius and the Princess III(传说中的母函数求方案数)
- HDU 1028 母函数解、完全背包解
- hdu 1028 Ignatius and the Princess III 母函数
- 母函数模板 HDU-1028
- hdu 1028 母函数模板