【HDU 1028】【母函数 整数划分】Ignatius and the Princess III
来源:互联网 发布:软件打不开是什么原因 编辑:程序博客网 时间:2024/06/05 07:14
传送门:http://acm.split.hdu.edu.cn/showproblem.php?pid=1028
描述:
Ignatius and the Princess III
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19323 Accepted Submission(s): 13562
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
41020
Sample Output
542627
Author
Ignatius.L
Recommend
We have carefully selected several similar problems for you: 1085 2152 1709 1059 1114
思路一:
母函数的意思就是把n 用1,2,3,4,5`````来表示的种数,构造母函数:(1+x+x^2+x^3+`````)*(1+x^2+x^4+````)*(1+x^3+x^6+````)````
刘老师的母函数PPT见Here
代码一:
#include <bits/stdc++.h>using namespace std;#define rep(i,k,n) for(int i=k;i<=n;i++)const int N=130;int num1[N], num2[N];//num1存放目前所有划分数,num2存放更新时临时的划分数void init(){ rep(i, 0, N-1){ num1[i] = 1; num2[i] = 0; } rep(i, 2, N-1){ rep(j, 0, N-1)//枚举已知范围 for(int k = 0; k + j < N; k += i){//枚举新增范围 num2[k + j] += num1[j]; } rep(j, 0, N - 1){//重置 num1[j] = num2[j]; num2[j] = 0; } }}int main(){ init(); int n; while(~scanf("%d", &n)){ printf("%d\n", num1[n]); } return 0;}
思路二:
当然整数的划分也可以用递推做
可以参考http://www.cnblogs.com/IMGavin/p/5621370.html 和 http://blog.sina.com.cn/s/blog_677a3eb30100kqnn.html
代码二:
#include<stdio.h>#include<string.h>const int MAXN=130;int dp[MAXN][MAXN];//dp[i][j]表示 i 表示成最大的数不超过 j 的方法数int calc(int n,int m){ if(dp[n][m]!=-1) return dp[n][m]; if(n<1||m<1) return dp[n][m]=0; if(n==1||m==1) return dp[n][m]=1; if(n<m) return dp[n][m]=calc(n,n); if(n==m) return dp[n][m]=calc(n,m-1)+1; return dp[n][m]=calc(n,m-1)+calc(n-m,m); } int main(){ int n; memset(dp,-1,sizeof(dp)); while(scanf("%d",&n)!=EOF) printf("%d\n",calc(n,n)); return 0;}
0 0
- HDU 1028 Ignatius and the Princess III(母函数 或者 整数划分的DP动态规划)
- 【HDU 1028】【母函数 整数划分】Ignatius and the Princess III
- hdu 1028Ignatius and the Princess III(整数划分)
- hdu 1028 Ignatius and the Princess III(整数划分)
- hdu 1028 Ignatius and the Princess III 【整数划分】
- HDOJ 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 整数拆分 母函数/dp
- HDU 1028-Ignatius and the Princess III(拆分整数-母函数-无限次)
- hdu 1028 Ignatius and the Princess III(整数拆分,母函数或dp)
- Hdu 1028 Ignatius and the Princess III//整数划分(一)
- hdu 1028 Ignatius and the Princess III 整数划分+dp 组合
- HDU 1028(Ignatius and the Princess III)区间DP之整数划分
- hdu 1028 Ignatius and the Princess III(整数的划分)
- HDU 1028 Ignatius and the Princess III(DP,整数划分)
- HDU/HDOJ 1028 母函数 Ignatius and the Princess III
- HDU 1028 Ignatius and the Princess III(母函数)
- 并查集的find和join函数实现
- 泛型是个巨坑
- LeetCode-Third Maximum Number
- 思科 IOS简介
- poj_3020 Antenna Placement(二分图最小路径覆盖)
- 【HDU 1028】【母函数 整数划分】Ignatius and the Princess III
- 16 - 10 - 23 字符串输入(fputs()函数)溢出的原理
- Linux进程间的通信方式:信号
- mysql分组查询数据,将数据中null和''作为一组查询结果
- 大数据Spark “蘑菇云”行动第64课: 页面跳转功能分析与架构 A、B、C页面之间跳转率的计算方法
- vmware虚拟机上运行ubuntu14开机显示“无法应用原保存的显示器配置”问题的解决办法
- 学习echarts后的一点感悟
- hibernate中的三级缓存与加载
- 带缓存的I/O操作