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种 ...
随机推荐
- JavaWeb——servlet1
一.servlet简介 Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,具有独立于平台和协议的特性,主要功能在于 ...
- web开发jsp页面遇到的一系列问题
一:web总结 1.jsp页面知识点巩固 1.1字符串数字格式化转换 <%@ taglib prefix="fmt" uri="http://java.sun.co ...
- 相对路径 分类: C# 2015-06-11 15:41 8人阅读 评论(0) 收藏
.绝对路径 绝对路径是指文件在硬盘上真正存在的路径.例如"bg.jpg"这个图片是存放在硬盘的"E:\book\网页布局代码\第2章"目录下,那么 &q ...
- RabbitMQ ——与Spring集成及exchange的direct、topic方式实现和简单队列实现
程序整体结构 Maven依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http: ...
- smbpasswd - Samba加密的口令文件。
总览 SYNOPSIS smbpasswd 描述 DESCRIPTION 此文件是 Samba(7) 套件的一部分. smbpasswd是Samba加密的口令文件.文件中包含了用户名,UNIX用户ID ...
- python常用函数 P
popleft(iterable) 对应pop,左侧弹出,队列适用. 例子: permutations(iterable, int) itertools的permutations方法可以产生集合的所有 ...
- canvas 转盘文字
canvas.js window.onload=function() { var canvas=document.getElementById('canvas'); var ctx=canvas.ge ...
- mysql,分组后,再次进行过滤
查出平均分大于80以上的班级 select class_id, avg(score) from students group by class_id having avg(score)>80; ...
- SpringBoot---Favicon配置
1.概述 1.1.SpringBoot提供了一个默认的Favicon,每次访问都能看到: 2.关闭Favicon 在application.yml中设置关闭Favicon,默认开启: spring.m ...
- centos 6.5 安装 ant
从ant官方网站下载ant安装包:apache-ant-1.9.7-bin.tar.gz,解压 tar xvf apache-ant-1.9.7-bin.tar.gz -C /usr/java/ 配置 ...