HDU 1028 Ignatius and the Princess III dp整数划分
http://acm.hdu.edu.cn/showproblem.php?pid=1028
dp[i][j]表示数值为i,然后最小拆分的那个数是j的时候的总和。
1 = 1
2 = 1 + 1 、 2 = 2
3 = 1 + 1 + 1、 3 = 2 + 1、 3 = 3
那么可以分两类,
1、最小拆分数是j,这个时候dp[i][j] = dp[i - j][j]。加一个数j,使得它变成i
2、最小拆分数严格大于j,这个时候就没得加上j了。就是dp[i][j + 1]
所以dp[i][j] = dp[i - j][j] + dp[i][j + 1]; (加上dp[i][j + 1]是前缀和的意思)
dp[2][1]就是2中的两条等式,然后dp[3][1] = dp[2][1] + dp[3][2]
其中dp[2][1]中有两条式子,所以每个式子加上一个1上去,就是3中前两条式子。
然后dp[3][2]其实就是3 = 3这条,因为3的式子不存在最小拆分数是2的情况。
如果要输出最小拆分数是k的时候有多少种解。
就是dp[val][k] - dp[val][k + 1];
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
int dp[maxn][maxn];
const int N = ;
void init() {
for (int i = ; i <= N; ++i) {
dp[i][i] = ;
for (int j = i - ; j >= ; --j) {
dp[i][j] = dp[i - j][j] + dp[i][j + ];
}
}
// cout << dp[4][1] - dp[4][2] << endl;
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
init();
int n;
while (cin >> n) {
cout << dp[n][] << endl;
}
return ;
}
这题居然可以相当于完全背包,整数划分的题目(可以相同)可以转化为完全背包。
dp[i]表示产生这个数字所拥有的合法方案数。
而加了一维,dp[i][j]这样的话,可以用来判断一些特殊条件,例如相差的值不能大于多少这样子。
http://www.cnblogs.com/liuweimingcprogram/p/7010170.html
http://acm.hdu.edu.cn/showproblem.php?pid=2709 (正常dpMLE)
隐含着dp[i][j]
表示前i个物品,组成j的方案数。只能是前i个物品
HDU 1028 Ignatius and the Princess III dp整数划分的更多相关文章
- 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 ...
- hdu 1028 Ignatius and the Princess III (n的划分)
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
题目链接: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 1028 Ignatius and the Princess III (母函数或者dp,找规律,)
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)
以下引用部分全都来自:http://blog.csdn.net/ice_crazy/article/details/7478802 Ice—Crazy的专栏 分析: HDU 1028 摘: 本题的意 ...
- hdu 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(母函数or计数DP)
Problem Description "Well, it seems the first problem is too easy. I will let you know how fool ...
- HDU 1028 Ignatius and the Princess III (动态规划)
题目链接:HDU 1028 Problem Description "Well, it seems the first problem is too easy. I will let you ...
随机推荐
- Python: PS 滤镜-- 极坐标变换到平面坐标
本文用 Python 实现 PS 中的一种滤镜 极坐标变换到平面坐标,具体的算法原理和效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/de ...
- 【USACO】Optimal Milking
题目链接 : [POJ]点击打开链接 [caioj]点击打开链接 算法 : 1:跑一遍弗洛伊德,求出点与点之间的最短路径 2:二分答案,二分”最大值最小“ 3.1:建边,将 ...
- html5 WebWorkers 防止浏览器假死
在Web开发的时候经常会遇到浏览器不响应事件进入假死状态,甚至弹出“脚本运行时间过长“的提示框,如果出现这种情况说明你的脚本已经失控了. 一个浏览器至少存在三个线程:js引擎线程(处理js).GUI渲 ...
- Linux 终端显示 Git 当前所在分支
function git_branch { branch="`git branch 2>/dev/null | grep "^\*" | sed -e " ...
- 4.oracle正确卸载步骤
oracle 11g如何完全卸载 方法/步骤1: 停用oracle服务:进入计算机管理,在服务中,找到oracle开头的所有服务,右击选择停止 方法/步骤2: 在开始菜单中,找到oracle-> ...
- 1.oracle中decode的一些巧妙用法
1.符号函数sign在decode中的用法--比较大小 select decode(sign(变量1-变量2),-1,变量1,变量2) from dual; --取较小值sign()函数根据某个值是0 ...
- python -m json.tool 中文乱码 Format JSON with python
现在以 json 为数据传输格式的 RESTful 接口非常流行.为调试这样的接口,一个常用的办法是使用 curl 命令: curl http://somehost.com/some-restful- ...
- 评判云服务靠谱程度 -- Coding 安全那些事
本文依据孙宇聪在 SegmentFault D-Day 北京场的演讲内容整理,并授权首发于“高效运维”公众号.10月11日,SegmentFault 将在上海举办D-Day,围绕 Docker 主题. ...
- 对比<input type="text" id="">和<asp:TextBox runat="server" ID="">
首先这两个都是对应文本输入框形式: <input type="text"class="form-control"id="txt_add_pro_ ...
- react-redux升级到7.0.x后报错"TypeError: Object(...) is not a function"
同样一套代码,react-redux 6.0.x 的版本下可以正常运行,在7.0.x的版本下出现报错,而且报错原因实在太难定位了 package.json { ... "react-redu ...