HDU 1028 Ignatius and the Princess III(递推或母函数)
来源:互联网 发布:c 软件外包兼职 编辑:程序博客网 时间:2024/06/05 08:08
参考博客:链接
题面:链接
题目大意:将一个正整数n,分解为若干个正整数之和,求分解方法的数目。
思路:由于n较小,且分解的方法数目一定,可以选择先打表,后直接出处即可。
打表过程先看题目给出的例子:
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
由题目中的例子知:
可设dp(a,k)表示将一个数a用不大于k的数划分的方法(1<=k<=n);
就从k开始,先分掉一个k,分解方法为:dp(a-k, k);
紧接着,可以从k’=k-1开始,分解方法为: dp(a,k-1);
a-k=0时结果为1,a小于k时,dp(a,k)为dp(a,a);
则递推式即:dp(a,k)=dp(a-k,k)+dp(a,k-1);
注意:初始状态 a=1、k=1时,分解结果唯一,当 a<k,结果也是唯一的,都为1。
ac代码:
#include<stdio.h>#define ll long long//分解方法数目小,不会超int dp[121][121];void Play_table()//打表,记录数字n的拆分方法{ int i,j; for(i=1; i<=120; i++) { dp[1][i]=dp[i][1]=dp[0][i]=1; } for(i=2; i<=120; i++) { for(j=2; j<=120; j++) { dp[i][j]=i>=j? dp[i-j][j]+dp[i][j-1]:dp[i][i];//!! } }}int main(){ int n; Play_table(); while(~scanf("%d",&n)) { if(n<1||n>120) break; printf("%d\n",dp[n][n]); } return 0;}
阅读全文
0 0
- 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-1028Ignatius 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(母函数)
- 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(整数拆分,母函数或dp)
- HDU 1028 Ignatius and the Princess III (母函数 或 动态规划DP)
- HDOJ 1028 Ignatius and the Princess III(递推)
- HDU/HDOJ 1028 母函数 Ignatius and the Princess III
- hdu 1028 Ignatius and the Princess III 母函数
- Tomcat学习总结(1)——Tomcat入门教程
- linux下安装ngix系统
- python解析xml文件——通过etree来解析xml文件
- 在Postgres里用Sequence[译]
- 【树状数组--思维】poj 2182 Lost Cows
- HDU 1028 Ignatius and the Princess III(递推或母函数)
- 好好做事
- 1、字符串最后一个单词的长度
- Tomcat学习总结(2)——Tomcat使用详解
- 在编译期求得结构体偏移量
- spring事务(Transaction)的七种事务传播行为及五种隔离级别
- 2017.7.20 学习日记
- MediaPlayer播放音频文件
- 简单排序——希尔排序