题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028

题意:

  给你一个正整数n,将n拆分成若干个正整数之和,问你有多少种方案。

  注:"4 = 3 + 1"和"4 = 1 + 3"视为同一种方案。(加数是无序的)

题解1(dp):

  表示状态:

    dp[n][m] = num of methods

    表示用均不超过m的元素组成n的方法数。

  

  如何转移:

    假设当前状态为dp[n][m].

    对于等于m的元素,有两种决策。要么不用,要么用。

      (1)不用:dp[n][m] += dp[n][m-1]

      (2)用: dp[n][m] += dp[n-m][m]

    综上:dp[n][m] = dp[n][m-1] + dp[n-m][m]

  

  找出答案:

    ans = dp[n][n]

  

  边界条件:

    dp[0][i] = 1  (0<=i<=MAX_N)

题解2(母函数):

  要凑出n,每种方案无非是取几个1,几个2,几个3......

  这就是母函数的经典问题啦(取硬币)。

  

  构造母函数:

    G(x) = (1 + x^1 + x^2 + x^3...) * (1 + x^2 + x^4 + x^6...) * (1 + x^3 + x^6 + x^9...) * (1 + x^4 + x^8 + x^12...)...

  

  找出答案:

    x^n项的系数即为答案。

AC Code(dp):

 // dp[n][m] = num of methods
// n: the elems are up to n
// m: each elem won't be not more than m
// dp[n][m] = dp[n][m-1] + dp[n-m][m]
// ans = dp[n][n]
// dp[0][m] = 1 #include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX_N 125 using namespace std; int n;
int dp[MAX_N][MAX_N]; void cal_dp()
{
memset(dp,,sizeof(dp));
for(int i=;i<MAX_N;i++)
{
dp[][i]=;
}
for(int i=;i<MAX_N;i++)
{
for(int j=;j<MAX_N;j++)
{
if(i>=j) dp[i][j]=dp[i][j-]+dp[i-j][j];
else dp[i][j]=dp[i][i];
}
}
} int main()
{
cal_dp();
while(cin>>n)
{
cout<<dp[n][n]<<endl;
}
}

AC Code(Generating Function):

 #include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX_N 125 using namespace std; int n;
int ans[MAX_N];
int temp[MAX_N]; void generating_function(int n)
{
memset(ans,,sizeof(ans));
ans[]=;
for(int i=;i<=n;i++)
{
memset(temp,,sizeof(temp));
for(int j=;j*i<=n;j++)
{
for(int k=;k+j*i<=n;k++)
{
temp[k+j*i]+=ans[k];
}
}
memcpy(ans,temp,sizeof(temp));
}
} int main()
{
generating_function();
while(cin>>n)
{
cout<<ans[n]<<endl;
}
}

HDU 1028 Ignatius and the Princess III:dp or 母函数的更多相关文章

  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 ...

  2. HDU 1028 Ignatius and the Princess III dp整数划分

    http://acm.hdu.edu.cn/showproblem.php?pid=1028 dp[i][j]表示数值为i,然后最小拆分的那个数是j的时候的总和. 1 = 1 2 = 1 + 1 . ...

  3. hdu 1028 Ignatius and the Princess III 简单dp

    题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. hdu 1028 Ignatius and the Princess III 母函数

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  8. HDU 1028 Ignatius and the Princess III (递归,dp)

    以下引用部分全都来自:http://blog.csdn.net/ice_crazy/article/details/7478802  Ice—Crazy的专栏 分析: HDU 1028 摘: 本题的意 ...

  9. HDU 1028 Ignatius and the Princess III (动态规划)

    题目链接:HDU 1028 Problem Description "Well, it seems the first problem is too easy. I will let you ...

  10. HDU 1028 Ignatius and the Princess III (生成函数/母函数)

    题目链接:HDU 1028 Problem Description "Well, it seems the first problem is too easy. I will let you ...

随机推荐

  1. python自动化运维学习第一天--day1

    学习python自动化运维第一天自己总结的作业 所使用到知识:json模块,用于数据转化sys.exit 用于中断循环退出程序字符串格式化.format字典.文件打开读写with open(file, ...

  2. Universal asynchronous receiver transmitter (UART)

    UART基本介绍: 通用异步收发器UART他的功能非常强大 我们只使用UART的全双工异步通信功能,使用中断接收数据. UART_RX:串行数据输入. UART_TX:串行数据输出. 硬件支持: 连接 ...

  3. 为Dynamics 365写一个简单程序实现解决方案一键迁移

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复258或者20170627可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...

  4. 新浪微博share分享接口请求奇葩错误

    17年6月30号,微博正式转入牛逼状态: 限制原来的微博发布删除等接口:(想用就开套餐,不然别说话) 开放新的分享接口share,然而,在调用这个分享接口时候,就会出现各种各样的奇葩错误: 注意事项: ...

  5. IIS 反向代理 golang web开发

    一. beego 开发编译 bee run 后会编译成 exe文件 编译生成后发布文件结构为 cmd 运行 cd D:/run beegoDemo.exe run 默认配置端口 不能为 80 跟iis ...

  6. js验证身份证号码

    function IdentityCodeValid(code) { var city={11:"北京",12:"天津",13:"河北",1 ...

  7. 【Vue】Vue的依赖追踪系统 ——搞懂methods watch和compute

    从作用机制和性质上看待methods,watch和computed的关系 <他三个是啥子关系呢?> 首先要说,methods,watch和computed都是以函数为基础的,但各自却都不同 ...

  8. 深入浅析JavaScript中的constructor

    constructor 属性返回对创建此对象的数组函数的引用.本文给大家介绍JavaScript中的constructor ,需要的朋友参考下吧 定义和用法 constructor 属性返回对创建此对 ...

  9. 在Linux下安装Oracle12c

    其实,对于oracle数据库和oracle实例的安装,借用图形化安装还是比较容易的,只是有个别地方需要特别注意外,其余的默认安装即可: 1.安装前的准备: 启动SSH工具: 先启动倒数第三个(想用图像 ...

  10. PHP超全局变量$_SERVER

    $_SERVER 是一个包含了诸如头信息(header).路径(path).以及脚本位置(script locations)等等信息的数组.这个数组中的项目由 Web 服务器创建.不能保证每个服务器都 ...