题目链接: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 4000: [TJOI2015]棋盘( 状压dp + 矩阵快速幂 )

    状压dp, 然后转移都是一样的, 矩阵乘法+快速幂就行啦. O(logN*2^(3m)) ------------------------------------------------------- ...

  2. fork进程函数总结

    学习链接: http://blog.csdn.net/jason314/article/details/5640969 http://coolshell.cn/articles/7965.html 搜 ...

  3. ubuntu-10.04的测试环境 安装测试 Coreseek开源中文检索引擎-Sphinx中文版

    主要参考文档:http://www.coreseek.cn/products-install/install_on_bsd_linux/ 一. 32位版本: coreseek安装需要预装的软件: ap ...

  4. 【转】CentOS 使用yum命令安装出现错误提示”could not retrieve mirrorlist http://mirrorlist.centos.org ***”

    源自:http://www.cnblogs.com/yangbingqi/p/3328610.html 刚安装完CentOS,使用yum命令安装一些常用的软件,使用如下命令:yum grouplist ...

  5. 重拾javascript动态客户端网页脚本

    笔记一: 一.DOM 作用: ·              DOM(Doument Object Model) 1.document文档 HTML 文件 (标记语言) <html> < ...

  6. Linux平台下使用rman进行oracle数据库迁移

        实验目的:将oracle数据库从一台机器迁移到另外的一台机器(同为linux平台),设置为不同的路径,不同的实例名 源端: ORACLE_BASE=/u01/app/oracle ORACLE ...

  7. css vertical-align全解

    CSS 的属性 vertical-align 指定了内联(inline)元素或表格单元格(table-cell)元素的垂直对齐方式.  要记住:vertical-align不影响块级元素中内容的对齐. ...

  8. PHP并发最佳实践

    直接参考大牛的: http://www.searchtb.com/2012/06/rolling-curl-best-practices.html

  9. C功底挑战Java菜鸟入门概念干货(一)

    一.认识Java 1.Java 程序比较特殊,它必须先经过编译,然后再利用解释的方式来运行.  2.Byte-codes 最大的好处是——可越平台运行,可让“一次编写,处处运行”成为可能.  3.使用 ...

  10. 随手写了一个linux服务端与window客户端的epoll程序,当做练习把。

    linux服务端:监听链接,处理消息 #include <sys/socket.h>     #include <sys/epoll.h>     #include <n ...