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 ...
随机推荐
- linux centos7 erlang rabbitmq安装
最终的安装目录为/opt/erlang 和 /opt/rabbitmq wget http://erlang.org/download/otp_src_21.0.tar.gztar zxvf otp_ ...
- Java基础教程:注解
Java基础教程:注解 本篇文章参考的相关资料链接: 维基百科:https://zh.wikipedia.org/wiki/Java%E6%B3%A8%E8%A7%A3 注解基础与高级应用:http: ...
- Linux_Chrome出现Adobe Flash Player is out of date解决方法
在安装Google的Chrome后都有出现Adobe Flash Player is out of date的问题. Chrome浏览器用的播放器插件是PepperFlashPlayer.而且是内置的 ...
- [转]Birdfont 2.10 发布,字体编辑器
最近在忙大数据.黑天鹅算法实盘测试 许久没有更新字库方面的资料,汗一个... 今天转一个 :Birdfont 2.10 发布,字体编辑器 字体编辑器,向来很少,除了fontlab的几个昂贵的商业版,就 ...
- linux 服务器部署的web项目存入数据库的时间不正确
在linux获取当前时间 date 获取的时间是正常的 ----- java写了个测试类 public class TestDate { public static void main(String[ ...
- Python面试题目之列表取值超出范围
# 下面列表取值超出范围,会报错还是有返回值: L1 = [',]print(L1[10]) print(L1[10:]) 第一个打印会报错: 第二个打印会返回一个空列表
- linux内核分析第四周-使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
本周作业的主要内容就是采用gcc嵌入汇编的方式调用system call.系统调用其实就是操作系统提供的服务.我们平时编写的程序,如果仅仅是数值计算,那么所有的过程都是在用户态完成的,但是我们想将变量 ...
- 【bzoj5170】Fable(树状数组)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=5170 我们会发现,经过一轮冒泡后,若a[i]的前面有比它大的数,就一定会有一个被丢到后 ...
- BloomFilter–大规模数据处理利器
转自: http://www.dbafree.net/?p=36 BloomFilter–大规模数据处理利器 Bloom Filter是由Bloom在1970年提出的一种多哈希函数映射的快速查找算法. ...
- Matplotlib 练习题
1. 绘制一个二维随机漫步的图形 直接上代码: %pylab inline nsteps = 1000 draws = np.random.randint(-1,2,size=(2,nsteps)) ...