题目链接: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. BZOJ 3669: [Noi2014]魔法森林( LCT )

    排序搞掉一维, 然后就用LCT维护加边MST. O(NlogN) ------------------------------------------------------------------- ...

  2. ROS的文件系统

    这篇博客介绍一下ROS的文件系统的基本概念,用户可以直接在官方网站:http://wiki.ros.org/ROS/Tutorials/NavigatingTheFilesystem去查看官方手册. ...

  3. Labview学习之远程控制VI

    Labview学习之远程控制VI        从LabVIEW 6.1开始,LabVIEW集成了Remote Panels技术,允许用户直接在客户端计算机上打开并操作位于服务器端计算机上的VI的前面 ...

  4. input 属性

    1.readonly="readonly" 用来表示 input只能读写.

  5. hadoop搭建杂记:Linux下hostname的更改办法

    VirtualBox搭建hadoop伪分布式模式:更改hostname VirtualBox搭建hadoop伪分布式模式:更改hostname master: ip:192.168.56.120 机器 ...

  6. hdu 4630 No Pain No Game 线段树离线处理

    题目链接 求出一个区间内任意两个数的gcd的最大值. 先将询问读进来然后按r值排序. 将每一个数因数分解, 对每一个因子x, 如果pre[x]!=-1, 那么就更新update(pre[x], x, ...

  7. poj 1949 Chores 最长路

    题目链接 求出最长路..... #include <iostream> #include <vector> #include <cstdio> #include & ...

  8. 计算BMI指数的小程序

    小明身高1.75,体重80.5kg.请根据BMI公式(体重除以身高的平方)帮小明计算他的BMI指数,并根据BMI指数: 低于18.5:过轻 18.5-25:正常 25-28:过重 28-32:肥胖 高 ...

  9. 第七届河南省赛G.Code the Tree(拓扑排序+模拟)

    G.Code the Tree Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 35  Solved: 18 [Submit][Status][Web ...

  10. distributor之Interrupt Set/Clear-Active Registers, GICD_IS/CACTIVERn

    set active寄存器.顾名思义就是把一个中断置为active状态,clear active寄存器就是清除active状态,在这里我们有必要说明一下中断状态的一些概念: active状态:假设此时 ...