题目链接: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 (动态规划)的更多相关文章

  1. hdu 1028 Ignatius and the Princess III 简单dp

    题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. hdu 1028 Ignatius and the Princess III 母函数

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  6. 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 ...

  7. HDU 1028 Ignatius and the Princess III (生成函数/母函数)

    题目链接:HDU 1028 Problem Description "Well, it seems the first problem is too easy. I will let you ...

  8. HDU 1028 Ignatius and the Princess III (递归,dp)

    以下引用部分全都来自:http://blog.csdn.net/ice_crazy/article/details/7478802  Ice—Crazy的专栏 分析: HDU 1028 摘: 本题的意 ...

  9. 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种 ...

随机推荐

  1. android概念-android学习第二天

    一:1G到4G(generation) 1G 大哥大 -- 电话 2G 小灵通 gsm标准 发短信 wap.baidu.com -- 电话和短信 3G 沃 www.baidu.com 7.2M/s - ...

  2. 玩爆你的手机联系人--T9搜索(一)

         自己研究了好几天联系人的T9搜索算法, 先分享出来给大家看看. 欢迎不吝赐教.假设有大神有更好的T9搜索算法, 那更好啊,大家一起研究研究,谢谢. 第一部分是比較简单的获取手机联系人. 获取 ...

  3. 剑指offer学习--初级c++面试题

    定义一个空的类型,里面没有任何成员函数和成员变量,对该类型求sizeof,得到的结果是多少? 答案是1.空类型中的实例中不包含任何信息,本来求sizeof应该是0,但是当我们声明该类型的实例的时候,他 ...

  4. combox系列问题集

    visual studio崩溃 你是不是经常会遇到一编辑combox,visual studio就会立马崩溃.一直都无法理解是什么原因,然后后来发现居然是因为有道的截屏翻译,关掉截屏翻译就好了. co ...

  5. vue 踩坑之组件传值

    Vue 报错[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the pa ...

  6. vue 页面切换从右侧切入效果

    1.将切换的页面用transition包裹 <div class="index-content"> <transition> <router-view ...

  7. AES-OZ745 OZ745 Zynq-7000 开发板与套件

    北京太速科技有限公司为广大合作单位特设海外代购业务,主要包括各类板卡.相机.传感器.仪器仪表.专用芯片等.代购业务仅收取基本的手续费. 北京太速科技有限公司在线客服:QQ:448468544 淘宝网站 ...

  8. python常用函数 J

    (1)join(iterable)/(2) join(str, str) (1)join()连接字符串数组.(2)os.path.join()将多个路径组合后返回. 例子:

  9. 【Leetcode周赛】从contest-71开始。(一般是10个contest写一篇文章)

    Contest 71 () Contest 72 () Contest 73 (2019年1月30日模拟) 链接:https://leetcode.com/contest/weekly-contest ...

  10. IncepText算法笔记

    源头:场景文本经常受到aspect ratio, scal, and orientation的影响 为了解决这个问题,提出了inceptext,主要贡献在于添加了inception-text modu ...