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 ...
随机推荐
- Learning Query and Document Similarities from Click-through Bipartite Graph with Metadata
读了一篇paper,MSRA的Wei Wu的一篇<Learning Query and Document Similarities from Click-through Bipartite Gr ...
- nginx重启 failed (98: Address already in use)
启动nginx服务,无法正常启动,一查log日志,发现如题错误信息. 问题描述:地址已被使用.可能nginx服务卡死了,导致端口占用,出现此错误. 查看端口 netstat -ntpl 杀掉进程 ...
- 【转载】IEEE754 学习总结
以下是参考<IEEE754 学习总结>并结合自己学习总结 一:前言二:预备知识 三:浮点数的表示范围四:将浮点格式转换成十进制数 一:前言 前不久在分析一个程序的过程中遇到了浮点运算,也就 ...
- HDU1160FatMouse's Speed
#include<stdio.h> #include<string.h> #include<algorithm> #include<set> #incl ...
- 常用php操作redis命令整理(二)哈希类型
HSET将哈希表key中的域field的值设为value;如果field是哈希表中的一个新建域,并且值设置成功,返回1;如果哈希表中域field已经存在且旧值已被新值覆盖,返回0. <?php ...
- python StringIO类
python的stringIO类用来处理字符串,由于其操作类似文件操作,可以视为内存中的文件. 1.创建stringIO 2.常用操作: write,writelines.getvalue.seek. ...
- 如何借助 OVN 来提高 OVS 在云计算环境中的性能
众所周知,OpenvSwitch 以其丰富的功能和不错的性能,已经成为 Openstack 部署中最受欢迎的虚拟交换机.由于 Openstack Neutron 的架构引入了一些性能问题,比如 neu ...
- Java Spring-JdbcTemplate增删改查
2017-11-11 21:13:13 Spring 框架中提供了对持久层技术支持的类 : JDBC : org.springframework.jdbc.core.support.JdbcDaoSu ...
- error processing package oracle-java8-installer问题解决
ubuntu通过ppa源安装jdk时遇到如下问题: download failedOracle JDK 8 is NOT installed.dpkg: error processing packag ...
- 也来说说C#异步委托 (转自 Rising_Sun)
前些日子,看到园子里面有人用老王喝茶的例子讲解了一下同步和异步,虽然没有代码实现,但是能够通俗易懂的讲解了同步.异步.阻塞.非阻塞的关系了,今天借题发挥,用一个热水器加热洗澡的例子来具体演示一下C#使 ...