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 ...
随机推荐
- 2018 China Collegiate Programming Contest Final (CCPC-Final 2018)
Problem A. Mischievous Problem Setter 签到. #include <bits/stdc++.h> using namespace std; #defin ...
- Linux命令: grep命令
基本用法 ...
- Vue学习笔记之Vue的模板字符串
0x00 模板字符串 传统的JavaScript语言,输出模板通常是这样的写的. $('#result').append( 'There are <b>' + basket.count + ...
- 对OpenCV中3种乘法操作的理解掌握
参考了<Opencv中Mat矩阵相乘——点乘.dot.mul运算详解 >“http://blog.csdn.net/dcrmg/article/details/52404580”的相关内容 ...
- 20145325张梓靖 《Java程序设计》第7周学习总结
20145325张梓靖 <Java程序设计>第7周学习总结 教材学习内容总结 时间的度量 格林威治时间,简称GMT时间,由观察太阳而得来:世界时,UT:国际原子时,TAI:世界协调时间,U ...
- MySQL的yum源
http://repo.mysql.com/ 超链接: http://repo.mysql.com/
- 安装tensorflow报ImportError: libcublas.so.9.0: cannot open shared object file的解决方法【转】
本文转载自:https://blog.csdn.net/qq_37274615/article/details/81099738 转载自:https://blog.csdn.net/qysh123/a ...
- [CF600E]Lomsat gelral
题意翻译 一棵树有n个结点,每个结点都是一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号的和. 线段树合并板子题,没啥难度,注意开long long 不过这题$dsu$ $on$ $tre ...
- 关于LIS和LCS问题的o(nlogn)解法
o(n^2)解法就不赘述了,直接解释o(nlogn)解法 LIS最长递增子序列: 先明确一个结论:在长度最大为len的递增序列里若末尾元素越小,该递增序列越容易和后面的子序列构造出一个更长的递增子序列 ...
- 【转】集群/分布式环境下5种session处理策略
转载至:http://blog.csdn.net/u010028869/article/details/50773174 在搭建完集群环境后,不得不考虑的一个问题就是用户访问产生的session如何处 ...