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 ...
随机推荐
- OpenCV笔记 1
Structure Contains Represents CvPoint int x, y Point in image CvPoint2D32f float x, y Points in ...
- shell直接退出后 后台进程关闭的原因和对处
在linux上进行测试时发现启动后台进程后,如果使用exit退出登录shell,shell退出后后台进程还是能够正常运行,但如果直接关闭登陆的窗口(如直接关掉xshell),那后台进程就会一起终了.都 ...
- [poj1410]Intersection
题目大意:求线段与实心矩形是否相交. 解题关键:转化为线段与线段相交的判断. #include<cstdio> #include<cstring> #include<al ...
- ROS探索总结(二)——ROS总体框架
个人分类: ROS 所属专栏: ROS探索总结 一. 总体结构 根据ROS系统代码的维护者和分布来标示,主要有两大部分: (1)main:核心部分,主要由Willow G ...
- koa1链接mongodb
1.项目下安装mongodb和mongoose npm install mongodb --save-dev npm install mongoose --save-dev 2.router中 var ...
- c语言学习笔记 scanf和printf格式的问题
int a =0; int b =0; scanf("%d %d",&,&b); 上面这种和下面这种哪种对? int a =0; int b =0; scanf(& ...
- 带有通配符的字符串匹配算法-C/C++
日前某君给我出了这样一道题目:两个字符串,一个是普通字符串,另一个含有*和?通配符,*代表零个到多个任意字符,?代表一个任意字符,通配符可能多次出现.写一个算法,比较两个字符串是否相等. 我花了四个小 ...
- Java50道经典习题-程序24 根据输入求输出
题目:给一个不多于5位的正整数,要求:一.求它是几位数,二.逆序打印出各位数字.分析: (1)利用随机函数产生[1,99999]之间的一个正整数n (2)将n转换成字符串s,调用字符串的length( ...
- i++,++i,i+=1,i = i+1在C++中的区别
其实这个问题可以从三个角度去分析:语言规范,编译器实现,CPU支持.首先从语言规范上来讲:前置++和后置++是不等价的,前置++在规范中明确指出 和+=组合操作符是等价的,但和E = E+1;这样的赋 ...
- 使用metasploit进行栈溢出攻击-1
攻击是在bt5下面进行,目标程序是在ubuntu虚拟机上运行. 首先,需要搞明白什么是栈溢出攻击,详细内容请阅读 http://blog.csdn.net/cnctloveyu/article/det ...