Ignatius and the Princess III - 拆分数-动态规划(dp)
---恢复内容开始---
2017-08-10 20:00:45
writer:pprp
拆分数:
把正整数n拆分成k个正整数之和的方案数;
问题转换:将1转化为2
1、把n表示成m个正整数之和的方案数
2、把n表示成不超过m的正整数之和的方案数
两者答案相同:解释Ferrers图

用dp来做,dp[i][j]的意思是 i 表示成 不超过 j 的方案数
边界条件:
dp[0][j] = 1 , dp[[i][1] = 1 , dp[1][j] = 1;
状态转移:
dp[i][j] = dp[i][j-1]+dp[i-j][j];
n个数划分成不超过m的整数的方案,若最大数为m,那么把最大数去掉,等价于把n-m分成不超过m的整数的方案,
若最大数不是m,那么等价于把n分成不超过m-1的方案数
代码如下:
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio> using namespace std; const int maxn = ;
int dp[maxn][maxn]; void DP()
{
for(int i = ; i < maxn ; i++)
{
dp[i][] = dp[][i] = dp[][i] = ;
}
for(int i = ; i < maxn ; i++)
{
for(int j = ; j < maxn ; j++)
{
if(i >= j)
dp[i][j] = dp[i][j-]+dp[i-j][j];
else
dp[i][j] = dp[i][i];
}
}
} int main()
{
int n;
memset(dp,,sizeof(dp)); DP(); while(cin >> n)
{
cout << dp[n][n] << endl;
}
return ;
}
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 (递归,dp)
以下引用部分全都来自:http://blog.csdn.net/ice_crazy/article/details/7478802 Ice—Crazy的专栏 分析: HDU 1028 摘: 本题的意 ...
- 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 ...
- Ignatius and the Princess III HDU - 1028 || 整数拆分,母函数
Ignatius and the Princess III HDU - 1028 整数划分问题 假的dp(复杂度不对) #include<cstdio> #include<cstri ...
- HDU 1028 整数拆分问题 Ignatius and the Princess III
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- 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 ...
- hdu 1028 Ignatius and the Princess III 简单dp
题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...
- HDU1028 Ignatius and the Princess III 【母函数模板题】
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
随机推荐
- The Highest Mark---hdu5501(问题转化 01背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5501 二维数组: #include<stdio.h> #include<iostre ...
- 三.插入和查找MySQL记录 数据类型
1.插入数据的两种方式 1)INSERT tb1 VALUES('TOM',25,1863.25); 2)INSERT tb1(username,salary) VALUES('John',4500. ...
- DIV+CSS如何让文字垂直居中?
在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中 ...
- PHP计算经纬度之间的距离
<?php /** * 求两个已知经纬度之间的距离,单位为米 * * @param lng1 $ ,lng2 经度 * @param lat1 $ ,lat2 纬度 * @return floa ...
- mysql构建一张百万级别数据的表信息测试
表信息: CREATE TABLE dept( /*部门表*/ deptno MEDIUMINT UNSIGNED NOT NULL DEFAULT 0, /*编号*/ dname VARCHAR(2 ...
- request.getInputStream() 流只能读取一次问题
问题: 一次开发过程中同事在 sptring interceptor 中获取 request body 中值,以对数据的校验和预处理等操作 .导致之后spring 在读取request body 值做 ...
- Spring第八发—自动装配及让Spring自动扫描和管理Bean
依赖注入–自动装配依赖对象(了解即可) 对于自动装配,大家了解一下就可以了,实在不推荐大家使用.例子: byName:按名称装配,可以根据属性的名称,在容器中寻找跟该属性名相同的bean,如果没有找到 ...
- 细说PHP的FPM
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++基础概念++ C ...
- yield的表达式形式的应用(待补充)
1.yield的表达式形式应用的定义: 在一个生成器函数内,将yield赋值给一个变量,这就是yield的表达式形式.也叫生成器的表达式形式 2.send方法的定义: (1)定义: yield的表达式 ...
- topcoder SRM712 Div1 LR
题目: Problem Statement We have a cyclic array A of length n. For each valid i, element i-1 the l ...