hdu1028 Ignatius and the Princess III(递归、DP)
Ignatius and the Princess III
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25805 Accepted Submission(s): 17839
"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!"
#include<stdio.h>
#include<string.h>
const int MAXN=;
int dp[MAXN][MAXN];//用数组记录计算结果,节约时间
//dp[i][j]表示将i分成j部分的和等于i的方法数
int calc(int n,int m)
{
if(dp[n][m]!=-)
return dp[n][m];
if(n<||m<)
return dp[n][m]=;
if(n==||m==)
return dp[n][m]=;
if(n<m)
return dp[n][m]=calc(n,n);
if(n==m)
return dp[n][m]=calc(n,m-)+;
return dp[n][m]=calc(n,m-)+calc(n-m,m); }
int main()
{
int n;
memset(dp,-,sizeof(dp));
while(scanf("%d",&n)!=EOF)
printf("%d\n",calc(n,n));
return ;
}
DP
#include<iostream>
using namespace std;
int main()
{
int n;
while (cin >> n)
{
int dp[] = { };
dp[] = ;
for (int i = ; i <= n; i++)//分成几部分
{
for (int j = i; j <= n; j++)//每一部分先放1个有dp[j]种放法,剩下的j-i个随便放有dp[j-i]种放法
{
dp[j] = dp[j] + dp[j - i];//不断更新
}
}
cout << dp[n] << endl;
}
}
hdu1028 Ignatius and the Princess III(递归、DP)的更多相关文章
- hdu 1028 Ignatius and the Princess III 简单dp
题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...
- HDU1028 Ignatius and the Princess III 【母函数模板题】
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdoj 1028 Ignatius and the Princess III(区间dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028 思路分析:该问题要求求出某个整数能够被划分为多少个整数之和(如 4 = 2 + 2, 4 = 2 ...
- HDU 1028 Ignatius and the Princess III:dp or 母函数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028 题意: 给你一个正整数n,将n拆分成若干个正整数之和,问你有多少种方案. 注:"4 = ...
- hdu1028 Ignatius and the Princess III
这是道典型的母函数的题目,可以看看我的母函数这一标签上的另一道例题,里面对母函数做了较为详细的总结.这题仅贴上代码: #include"iostream" using namesp ...
- HDU-1028 Ignatius and the Princess III(生成函数)
题意 给出$n$,问用$1$到$n$的数字问能构成$n$的方案数 思路 生成函数基础题,$x^{n}$的系数即答案. 代码 #include <bits/stdc++.h> #define ...
- hdu1028 Ignatius and the Princess III(生成函数整理占坑)upd 已咕
先咕着 ---------------2018 5 22---------------------- 题解 生成函数处理整数拆分 code #include<cstdio> #includ ...
- 【母函数】hdu1028 Ignatius and the Princess III
大意是给你1个整数n,问你能拆成多少种正整数组合.比如4有5种: 4 = 4; 4 = 3 + 1; 4 = 2 + 2; 4 = 2 + 1 + 1; 4 = 1 + 1 + 1 + 1; ...
- ACM学习历程—HDU1028 Ignatius and the Princess III(递推 || 母函数)
Description "Well, it seems the first problem is too easy. I will let you know how foolish you ...
随机推荐
- 框架之 hibernate之各种查询
1. Hibernate的查询方式 2. Hibernate的查询策略 案例:使用Hibernate完成查询所有联系人功能 需求分析 1. 完成所有的联系人的查询 技术分析之Hibernate框架的查 ...
- MyBatis配置Setting详细说明
该表格转载自http://blog.csdn.net/summer_yuxia/article/details/53169227 setting是指定MyBatis的一些全局配置属性,这是MyBati ...
- Matlab 摄像机标定+畸变校正
博客转载自:http://blog.csdn.net/Loser__Wang/article/details/51811347 本文目的在于记录如何使用MATLAB做摄像机标定,并通过opencv进行 ...
- c# json.net xml互转
json转xml: XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonText,"root"); ...
- SPOJ KATHTHI - KATHTHI
以前并不知道这个trick. $01BFS$,在$bfs$的时候用一个双端队列来维护,如果边权为$1$就添加到队尾,边权为$0$就添加到队首. 还有一个小trick就是我们可以开一个$dis$数组来代 ...
- oracle创建数据库的语句
首先 oracle严格来说表空间的概念和数据库的概念很像,为了理解的方便我们,可以把表空间就先当成数据库 我们在安装oracle的服务端的时候默认会安装一些,默认实例 1.建立表空间,现在解释下面语句 ...
- 1.6 opencv视频操作基础
利用opencv中的VideoCapture类,来对视频进行读取显示,以及调用摄像头. VideoCapture是opencv 2.X中新增的一个类,对应于之前C语言版本的CvCapture结构体.它 ...
- Cadence如何自定义快捷键
第一步:将pcbenv复制到SPB_Data下,并覆盖原来的文件 第二步:打开pcbenv文件,将script和views文件夹以及env复制到cadence安装目录\SPB_16.3\share\p ...
- PDG转PDF注定会文件膨胀、质量下降吗?
作者:马健邮箱:stronghorse_mj@hotmail.com发布:2006.07.16更新:2006.07.20 事先声明: PDG文件是超星公司电子图书的专有格式,需要用超星公司的专用浏览器 ...
- 使用jenkins来跑docker image的惨痛经历
记录一下我使用jenkins来跑docker container的艰辛路程吧,是照着jenkins官网的[文档](https://jenkins.io/doc/tutorials/build-a-ja ...