HDU 1028 Ignatius and the Princess III (动态规划)
题目链接:HDU 1028
Problem Description
"Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says.
"The second problem is, given an positive integer N, we define an equation like this:
N=a[1]+a[2]+a[3]+...+a[m];
a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!"
Input
The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file.
Output
For each test case, you have to output a line contains an integer P which indicate the different equations you have found.
Sample Input
4
10
20
Sample Output
5
42
627
Solution
题意
给定 \(n\),求 \(n\) 的划分数。
思路
最容易想到的就是直接递归,但是复杂度很高,可以用动态规划降低复杂度。
Code
#include <bits/stdc++.h>
using namespace std;
const int maxn = 150;
int dp[maxn][maxn]; // dp[i][j] 表示将i划分成最大数不超过j的划分数
void solve() {
for(int i = 1; i < maxn; ++i) {
for(int j = 1; j < maxn; ++j) {
if(i == 1 || j == 1) {
dp[i][j] = 1;
} else if(i < j) {
dp[i][j] = dp[i][i];
} else if(i == j) {
dp[i][j] = dp[i][j - 1] + 1;
} else {
// dp[i][j - 1]表示最大数不超过j-1的方案数, dp[i - j][j]表示拿出一个j后最大数不超过j的方案数
dp[i][j] = dp[i][j - 1] + dp[i - j][j];
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
int n;
while(cin >> n) {
cout << dp[n][n] << endl;
}
return 0;
}
HDU 1028 Ignatius and the Princess III (动态规划)的更多相关文章
- 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,找规律,)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- 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 (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 (生成函数/母函数)
题目链接:HDU 1028 Problem Description "Well, it seems the first problem is too easy. I will let you ...
- 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
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028 题目大意:3=1+1+1=1+2=3 :4=4=1+1+1+1=1+2+1=1+3:所以3有3种 ...
随机推荐
- FireFox浏览器导出文件名乱码
解决方案1 String codedFileName = "导出文件名.xls"; String agent = request.getHeader("USER-AGEN ...
- Android超简单气泡效果
阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680最近有用到水下气泡上升效果,因此在网上查了一下资料,结果还真找到了 ...
- mysql DATETIME和TIMESTAMP类型
以mysql 5.7.20 为例 一直以来,理解有偏差,作此记录,纠正 一.DATETIME和TIMESTAMP 都有高达微秒(6位)的精度 范围 DATETIME 1000-01-01 00: ...
- [Bzoj1009][HNOI2008]GT考试(动态规划)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 显而易见的动态规划加矩阵快速幂,不过转移方程不怎么好想,dp[i][j]表示长度为 ...
- CSS动画划入划出酷炫
HTML插入 <!DOCTYPE html> <html class="no-js iarouse"> <head> <meta char ...
- 关联查询总结,left join 和 inner join 区别和优化
left join 是做左外关联,主表内容都会显示:符合关联条件的附表内容才会显示出来. inner join 是内关联,没有主表附表的概念:两个表中,同时符合关联条件的数据才会显示出来. left ...
- 【学习总结】尚硅谷2019java数据结构和算法
相关链接 github:javaDSA 目录 第一章 内容介绍和授课方式 第二章 数据结构和算法概述 第三章 稀疏数组和队列 第四章 链表 第五章 栈 第六章 递归 第七章 排序算法 第八章 查找算法 ...
- Linux查看关闭进程
ps:进程的静态列表(Process status) - PID:进程号,每个进程独一无二的标识符(关闭进程需要使用) - TTY:终端所属,表明进程产生于哪一个终端,对于多用户使用的Linux服务器 ...
- 客户端GUI结构学习总结
这几个月的开发工作主要是关于游戏内GUI的,业务开发之余也时常会看看客户端工程里的GUI系统这一块的代码,这里系统的总结下. 一.GUI树形结构 在GUI中所有的控件都遵循树形结构: 在客户端初始化时 ...
- vscode编写代码快速生成html模板
!(英文)+tab 自动生成HTML模板