题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028

思路分析:该问题要求求出某个整数能够被划分为多少个整数之和(如 4 = 2 + 2, 4 = 2 + 1 + 1),且划分的序列 2, 2 或者 2, 1, 1为单调非递增序列;

使用动态规划解法:假设dp[i][j]表示整数i被划分的序列中最大值不超过j的所有的可能,则使用类似于0-1背包的思考方法;

(1)如果i == j,则dp[i][j] = dp[i][j-1] + 1;

(2)如果i > j,则根据整数i被划分的序列中是否有数值 j 分为两种情况:如果序列中含有数值j,则所有和为i - j且最大值不超过j-1的序列加上值 j即可构成和为i的序列;

如果不含有j,则可能的所有不含有数值j且和为i的可能的序列数为 dp[i][j-1]个;

综上:j > i时,dp[i][j] = dp[i][i]; j = i时,dp[i][j] = dp[i][j-1] + 1; j < i时,dp[i][j] = dp[i-j][j-1] + dp[i][j-1];

代码如下:

#include <cstdio>
#include <cstring>
#include <iostream> const int MAX_N = + ;
long long dp[MAX_N][MAX_N]; void Solve()
{
dp[][] = ;
for (int i = ; i < MAX_N; ++ i)
{
for (int j = ; j < MAX_N; ++ j)
{
if (j > i)
dp[i][j] = dp[i][i];
else if (i == j)
dp[i][j] = dp[i][j-] + ;
else
dp[i][j] = dp[i][j-] + dp[i-j][j];
}
}
} int main()
{
int number = ; Solve();
while (scanf("%d", &number) != EOF)
printf("%d\n", dp[number][number]);
return ;
}

hdoj 1028 Ignatius and the Princess III(区间dp)的更多相关文章

  1. hdu 1028 Ignatius and the Princess III 简单dp

    题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...

  2. HDOJ 1028 Ignatius and the Princess III (母函数)

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  3. HDOJ 1028 Ignatius and the Princess III(递推)

    Problem Description "Well, it seems the first problem is too easy. I will let you know how fool ...

  4. HDU 1028 Ignatius and the Princess III:dp or 母函数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028 题意: 给你一个正整数n,将n拆分成若干个正整数之和,问你有多少种方案. 注:"4 = ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. hdu 1028 Ignatius and the Princess III 母函数

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  9. 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 ...

随机推荐

  1. T4模板之菜菜鸟篇

    一.废话 T4(Text Template Transformation Toolkit)是微软官方在VisualStudio 2008中开始使用的代码生成引擎.在 Visual Studio 中,“ ...

  2. JavaSE复习日记 : 抽象类

    /* * 抽象类 * 抽象: * 面向对象的三大核心思想; * 封装: 封装,ppp是封装的一部分 * 继承; * 多态: 父类的引用指向子类的对象 * 引用: 是指一个引用型变量 * 有哪些变量? ...

  3. BZOJ 1832: [AHOI2008]聚会( LCA )

    LCA模板题...不难发现一定是在某2个人的LCA处集合是最优的, 然后就3个LCA取个最小值就OK了. 距离就用深度去减一减就可以了. 时间复杂度O(N+MlogN) (树链剖分) -------- ...

  4. 获取新浪天气api显示天气情况(转)

    直接上一个html的demo <!doctype html> <html class="no-js fixed-layout"> <head> ...

  5. python 之日期时间处理

    ##python时间操作一般使用time.datetime两个模块 对于time模块,时间的表示模式有3种1.时间戳:time.time()2.字符串: time.strftime('%Y%m%d') ...

  6. C语言迭代求解

    date : 2013/8/12           desinger :pengxiaoen 今天看  国外电子信息科学经典教材系列   <电子电路分析与设计> 电子工业出版社的 的19 ...

  7. javascript集合求交集

    两集合求交集 思路: 1. 每一次从B数组中取一值,然后在A数组里逐个比较,如果有相等的,则保存.该算法复杂度为 O(MN). M, N 分别为数组 A B 的长度. 2. 因为A B 都排过序,所以 ...

  8. CCNA实验(3) -- RIP

    RIP协议分为版本1和版本2,均具备以下特征:1.是距离向量路由协议2.使用跳数(Hop Count)作为度量值3.默认路由更新周期为30秒4.管理距离(AD)为1205.支持触发更新6.最大跳数为1 ...

  9. java单链表代码实现

    用惯了C++,java写起来果然不太爽...不废话了,上代码... package javaInnerclassDemo; class Link{ class Node{ private String ...

  10. 字符串的MD5的32位加密和16位加密

    import java.security.MessageDigest; import java.util.Locale; public class MD5Util { public static St ...