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 ...
随机推荐
- yii的增删改查
要想实现数据库的操作,必须首先初始化数据模型. 一.yii的数据库模型的初始化有两种.第一种是利用模型的静态方法model:第二种是实例化一个新的模型.以例子说明: //数据的查询关键代码 $good ...
- 使用grunt中遇到的问题
1.使用jshint进行代码检查时,grunt命令后报错: 因为出现了乱码,我猜测是因为编码原因导致的.遂在webstorm的setting中修改了编码为utf-8,问题解决.
- Rsync+Sersync同步
Rsync+Sersync同步特点: (1):sersync可以记录下被监听目录中发生变化的(包括增加.删除.修改)具体某一个文件或某一个目录的名字:(2):rsync在同步的时候,只同步发生变化的这 ...
- hadoop应用场景
大数据量存储:分布式存储 日志处理: Hadoop擅长这个 海量计算: 并行计算 ETL:数据抽取到oracle.mysql.DB2.mongdb及主流数据库 使用HBase做数据分析: 用扩展性应对 ...
- 高级java工程师面试题-随笔
最近打算要换工作,也面试了几家,因为不是自己喜欢的所以拒了一些(当然也有人家不要我的.....).在面试的过程中发现对java高级程序员的考察基本上围绕知识面,知识深度两个方面来考察.下面是在面试过程 ...
- C# 外界调用方法是 方法名是string类型的解决方法
- $.ajax数据传输成功却执行失败的回调函数
这个问题迷惑了我好几天,都快要放弃了,功夫不负有心人,最终成功解决,下面写一下我的解决方法. 我传的数据是json类型的,执行失败的回调函数是因为从后台传过来的数据不是严格的json类型,所以才会不执 ...
- Codeforces 358D【DP】
思路: dp[i][0] 代表取的时候左边没有 dp[i][1] 代表取的时候右边没有 dp[i][2] 代表取的时候左右都没有 dp[i][3] 代表取的时候左右都有 然后自己转移吧= =. 注意 ...
- 【Unity3D】3D角色换装++ Advance
http://www.cnblogs.com/dosomething/archive/2012/12/15/2818897.html 本文在之前的文章Unity3D角色换装的原理 基础上做一个补充 给 ...
- UGUI 锚点坑
----------------------------------------------------------------- 关键点:4个实心蓝点距离雪花4瓣的距离永远不变 锚点Anchors: ...