1028:Ignatius and the Princess III
本题应该有两种方法:
1.母函数法
2.递推法
母函数不了解,待充分了解之后,再进行补充!
这里为递推实现的方法:
思路:
定义:n为要拆分的整数;
k为拆分的项数;
f[n][k]代表 n的整数拆分中,最大项不超过k的方案数。
每一个整数n的拆分中,总有一项拆分为自己,即:n = n; 我们将其表示为f[n][1],而且f[n][1] = 1;
又,每一个整数n的拆分中,总有一项拆分为n个1,即:n = 1 + 1 + ...... + 1(n个1的加和); 我们将其表示为f[0][0],且f[0][0] = 1;(注:n = 1时除外)。
所以:
1 = 1;
f[1][1] = 1;
2 = 2;
2 = 1 + 1;
f[2][2] = f[0][0] + f[2][1] = 2;
n = 3时,
3 = 3;
3 = 2 + 1;//3 = (1+ 1) + 1;
3 = 1 + 1 + 1;
f[3][1] = 1;
f[3][2] = 拆分为两项的 数目 + 拆分为一项的 数目 = f[1][1] + f[3][1]; 之所以用f[1][1]在此表示,是因为分成两项时,以1+1作为基底数,然后向上加数,能加的数只有1,加到哪一项上都是一样的,所以就相当于f[1][1]的效果。
同理,n = 4时,
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
像:f[4][2] = f[2][2] + f[4][1];
所以,结论就是:
对于任意满足条件的拆分,最大项要么达到k这个限制,要么小于它,因此f(n, k) = f(n-k, k) + f(n, k - 1)。
边界条件:f(n, 1) = 1, f(0, 0) = 1, f(1, 1) = 1, 另外,f(n, k) = f(n, n),如果k大于n的话。
实现代码:
#include <iostream>
using namespace std; const int MAX = ;
int f[MAX][MAX] = {};
void Init()
{
f[][] = ;
for(int i = ;i<MAX;i++)
f[i][] = ;
for(int n = ;n<MAX;n++)
{
for(int k = ;k<=n;k++)
{
int j;
if(n-k<k)
j = n-k;
else
j = k;
f[n][k] = f[n-k][j] + f[n][k-];
}
}
}
int main()
{
Init();
int n;
while(cin>>n)
{
cout<<f[n][n]<<endl;
}
return ;
}
1028:Ignatius and the Princess III的更多相关文章
- hdu 1028 Sample Ignatius and the Princess III (母函数)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU 1028 HDU Ignatius and the Princess III
简单的钱币兑换问题,就是钱的种类多了一点,完全背包. #include<cstdio> #include<cstring> int main () { ]; memset(dp ...
- 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 acm 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)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- Ignatius and the Princess III HDU - 1028 || 整数拆分,母函数
Ignatius and the Princess III HDU - 1028 整数划分问题 假的dp(复杂度不对) #include<cstdio> #include<cstri ...
- HDOJ 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
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
随机推荐
- MDI QMdiArea 多文档区域
Qt下创建类似window平台的MDI多文档区域使用QMdiArea QMdiArea * mdiArea; mdiArea = new QMdiArea(this); //A widget 为win ...
- 这才是官方的tapable中文文档
起因 搜索引擎搜索tapable中文文档,你会看见各种翻译,点进去一看,确实是官方的文档翻译过来的,但是webpack的文档确实还有很多需要改进的地方,既然是开源的为什么不去github上的tapab ...
- 20145204《Java程序设计》第10周学习总结
网络编程 网络编程:在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就是狭义的网络编程范畴.在发送和接收数据时,大部分的程序设计 ...
- MyBatis小案例完善增强
https://blog.csdn.net/techbirds_bao/article/details/9233599 上链接为一个不错的Mybatis进阶博客 当你把握时间,时间与你为伍. 将上一个 ...
- 嵌入式Linux应用程序开发环境搭建记录
2016年2月 参考资料: OK210软件手册(Linux版).pdf Ubuntu下Qt4.7.1编译环境配置说明.pdf 我阅读了以下内容: OK210软件手册(Linux版).pdf 第七章 O ...
- POJ 1325 Machine Schedule(最小点覆盖)
http://poj.org/problem?id=1325 题意: 两种机器A和B.机器A具有n种工作模式,称为mode_0,mode_1,...,mode_n-1,同样机器B有m种工作模式mode ...
- 下载百度网盘破解 获得 所下载视频URL 粘贴到thunder
Chrome:方法1. 进入谷歌商城,搜索baidudl.安装即可. 方法2. 下载baidudl.zip.解压获得baidudl文件夹.进入chrome://extensions/,勾选右上角Dev ...
- Android -- 图片处理, 画画板,缩放,旋转,平移,镜面,倒影,图片合成,颜色处理
1. 画画板 示例代码 public class MainActivity extends Activity { private ImageView iv; private Bitmap baseBi ...
- Python isspace()方法--转载
描述 Python isspace() 方法检测字符串是否只由空格组成. 语法 isspace()方法语法: str.isspace() 参数 无. 返回值 如果字符串中只包含空格,则返回 True, ...
- $state和$rootScope.$on($stateChangeStart)
这两者的区别:请看博客:http://stackoverflow.com/questions/32680292/angularjs-state-and-rootscope-onstatechanges ...