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种 ...
随机推荐
- 最小生成树基础算法(Prim + Krustal)
最小生成树问题的引入: 对于一个无向图G(V, E),需要用图中的n - 1条边连接图中的n个顶点并且不产生回路所产生的树就叫做生成树,其中权值总和最小的就是最小生成树. 如何求解最小生成树问题: 譬 ...
- org.apache.hadoop.hive.ql.exec.DDLTask. MetaException错误问题
下载 http://www.java2s.com/Code/Jar/m/Downloadmysqlconnectorjavacommercial517binjar.htm 放到lib目录下,删除原本的 ...
- spark连接hive找不到table
Caused by: org.apache.spark.sql.catalyst.analysis.NoSuchTableException: Table or view 'xxxx' not fou ...
- Linux grep常用命令
在一个文件中同时查找多个字符串: 并集语法: grep -e 'pattern1 -e 'pattern2 file 或集语法: 1.grep -E 'pattern1|pattern2' file ...
- sql插入语句笔记
使用INSERT插入数据行 [一次插入一行数据] 全写: INSERT INTO renshi (name, sex, age ,tel) VALUES ('胡大姐','女','35','13 ...
- route - 显示 / 操作IP选路表
总览 SYNOPSIS route [-CFvnee] route [-v] [-A family] add [-net|-host] target [netmask Nm] [gw Gw] [met ...
- 脚本_检测mysql存活状态
#!bin/bash#功能:检测mysql服务是否存活#作者:liusingbon# host为你需要检测的mysql主机的IP 地址,user为mysql账户名,passwd为密码; 这些信息需要根 ...
- HTML基础 内联样式改进 三毛语录
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- mysql安装 demo [linux centos7] [5.7.26]
MySQL 安装配置 https://www.runoob.com/linux/mysql-install-setup.html =================================== ...
- maven clean后 编译报错
<plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <config ...