题解报告:hdu 1028 Ignatius and the Princess III(母函数or计数DP)
"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!"
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
LL dp[maxn][maxn];int n;//dp[i][j]记录将整数i划分成所有元素不大于j的所有情况数
int main()
{//打表
for(int i=;i<maxn;i++)//将整数i划分成所有元素不大于1的分法和将整数1划分成所有元素不大于i(其实只有1本身)的分法都为1种
dp[i][]=dp[][i]=;
for(int i=;i<maxn;i++){//从划分2份开始
for(int j=;j<maxn;j++){//从整数2开始划分
if(i<j)dp[i][j]=dp[i][i];
else if(i==j)dp[i][j]=dp[i][j-]+;
else dp[i][j]=dp[i][j-]+dp[i-j][j];
}
}
while(cin>>n){
cout<<dp[n][n]<<endl;//最后输出总的情况数,即将整数n划分成不大于n的所有情况数
}
return ;
}
AC代码二:关于这种解法可以看看这篇博文:题解报告:hdu 1284 钱币兑换问题(简单数学orDP)
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main(){
LL n,dp[]={};//预处理打表
for(int i=;i<;++i)
for(int j=i;j<;++j)//每种数字可以看作无限个--->完全背包
dp[j]+=dp[j-i];
while(cin>>n){cout<<dp[n]<<endl;}
return ;
}
母函数解法,先贴一篇入门博文:母函数简介,看完后再看一篇:母函数的应用。普通型母函数其实就是把复杂的分类和分步求和过程具体为一个可以求解的"函数":通过将简单的构造(生成)函数展开多项式后可以发现一个规律:指数代表和数,每一项的系数代表凑成那一项指数的方案总数,实际上这就是多重集组合问题的求解答案。每个多项式中每一项前面的系数都为1,表示每种情况都有1种出现的可能。因此,我们只需构造出正确的生成函数,编程时通过模拟多个多项式相乘即可。对于此题构造出的生成函数就是G(x)=(1+x^1+x^2..+x^n)(1+x^2+x^4+x^6+...)(1+x^3+x^6+...)(...)(1+x^n),从这个"函数"中可以看出每一个多项式中有着完全背包的思想,即不取或取一件或取多件。注意:由于要求的是凑成和为n的方案数,所以多项式中每一项相乘得到的指数应不超过n,最终x^n前面的系数就是整数n的划分总方案数。
AC代码三:时间复杂度为O(n3)。
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int n,c1[maxn],c2[maxn];
int main(){
while(~scanf("%d",&n)){
memset(c1,,sizeof(c1));//c1[ ]保存当前得到的多项式各项系数,c2[ ]保存每次计算时的各项系数的临时结果,并且每计算完一个多项式之后把c2[ ]拷贝给c1[ ],同时把c2[ ]清零
memset(c2,,sizeof(c2));
c1[]=;//指数为0的系数为1
for(int i=;i<=n;++i){//n个多项式
for(int j=;j<=n;++j)//指数最大不超过n,0~n
for(int k=;k+j<=n;k+=i)//当前第i个多项式的指数步长为i。现将多项式中指数为0~n的每一项去乘以当前第i项多项式中的每一项,得到的指数为j+k,由于第i个多项式中每一项前面的系数都为1,因此得到指数为j+k的系数为在原来的基础上加上指数为j的系数C1[j]即可
c2[j+k]+=c1[j];
for(int j=;j<=n;++j) //把c2[j]即指数为j(j∈[0,n])的每一项系数赋给c1[j],并且把c2[ ]清零
c1[j]=c2[j],c2[j]=;
}
printf("%d\n",c1[n]);//x^n的系数就是整数n的划分方案数。
}
return ;
}
题解报告:hdu 1028 Ignatius and the Princess III(母函数or计数DP)的更多相关文章
- hdu 1028 Ignatius and the Princess III 母函数
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU 1028 Ignatius and the Princess III (递归,dp)
以下引用部分全都来自:http://blog.csdn.net/ice_crazy/article/details/7478802 Ice—Crazy的专栏 分析: HDU 1028 摘: 本题的意 ...
- hdu 1028 Ignatius and the Princess III 简单dp
题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...
- HDU 1028 Ignatius and the Princess III 整数的划分问题(打表或者记忆化搜索)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1028 Ignatius and the Princess III Time Limit: 2000/1 ...
- hdu 1028 Ignatius and the Princess III(DP)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU 1028 Ignatius and the Princess III (母函数或者dp,找规律,)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdu 1028 Ignatius and the Princess III (n的划分)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU 1028 Ignatius and the Princess III (生成函数/母函数)
题目链接:HDU 1028 Problem Description "Well, it seems the first problem is too easy. I will let you ...
- HDU 1028 Ignatius and the Princess III (动态规划)
题目链接:HDU 1028 Problem Description "Well, it seems the first problem is too easy. I will let you ...
随机推荐
- HDU - 6158 The Designer
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6158 本题是一个计算几何题——四圆相切. 平面上的一对内切圆,半径分别为R和r.现在这一对内切圆之间,按 ...
- springcloud(十):熔断监控Hystrix Dashboard
申明: 这里比较坑爹,大家写的时候要小心,这里和springboot的版本有关系哈,我使用的是2.0 版本,要么调频为1.5 版本,要么使用其他方式 解决错误,我选择了还是用2.0 ...
- 关于 startup_stm32f10x_hd.s 这个文件的一些说明
关于 startup_stm32f10x_hd.s 这个文件的一些说明 startup_stm32f10x_hd.s 是一个启动文件,里面是使用汇编语言写好的基本程序,当STM32 芯片上电启动的时候 ...
- 【Codeforces 427C】Checkposts
[链接] 我是链接,点我呀:) [题意] 环里面的点只需要一个点就能全都保护 问你最少需要多少花费以及最少的点才能将所有的点都保护 [题解] 有向图的强连通分量求出所有的联通分量 显然每个联通分量里面 ...
- Visual Studio 中的 .NET Framework 类库
Visual Studio 中的 .NET Framework 类库 .NET Framework 类库由命名空间组成.每个命名空间都包含可在程序中使用的类型:类.结构.枚举.委托和接口. 当您在 V ...
- 一个性能较好的jvm參数配置以及jvm的简单介绍
一个性能较好的webserverjvm參数配置: -server //服务器模式 -Xmx2g //JVM最大同意分配的堆内存,按需分配 -Xms2g //JVM初始分配的堆内存.一般和Xmx配置成一 ...
- window+nginx+php-cgi的php-cgi线程/子进程问题
见bbs http://bbs.csdn.net/topics/390803643/close 正常的配置情况下,window的php-cgi是不会出现多线程/子进程的,例如以下配置 fastcgi_ ...
- 并发数 = QPS*平均响应时间
转:https://blog.csdn.net/luman1991/article/details/70919279 并发数 = QPS*平均响应时间 QPS(TPS):每秒钟request 每秒查询 ...
- 修改Cygwin的默认启动目录
安装Cygwin后发现Cygwin默认的用户主目录是Windows的用户主目录(一般为:C:\Users\[UserName]\),要想修改为cygwin安装目录下的home\[UserName]\可 ...
- poj2011
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 17608 Accepted: 765 ...