HDU 1028 Ignatius and the Princess III 整数的划分问题(打表或者记忆化搜索)
传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=1028
Ignatius and the Princess III
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24967 Accepted Submission(s): 17245
"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!"
10
20
42
627
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define max_v 125
/*LL f(int n,int m)
{
if(n==1||m==1)
return 1;
else if(n==m&&n>1)
{
return f(n,n-1)+1;
}else if(n<m)
{
return f(n,n);
}else if(n>m)
{
return f(n,m-1)+f(n-m,m);
}
}*/
int main()
{
LL a[]={,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,};
/*for(int i=1;i<max_v;i++)
{
printf("%I64d,",f(i,i));
}
printf("\n**\n");*/
int n;
while(~scanf("%d",&n))
{
printf("%I64d\n",a[n]);
// printf("%I64d\n",f(n,n));
}
return ;
}
2.另外一种做法
记忆化搜索
就是在搜索之前先判断一下,搜过了就不再搜索了
跟dp一个意思
code:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define max_v 125
int dp[max_v][max_v];
LL f(int n,int m)
{
if(dp[n][m]!=-)//判断有没有搜过
return dp[n][m];
if(n==||m==)
return dp[n][m]=;
else if(n==m&&n>)
{
return dp[n][m]=f(n,n-)+;
}else if(n<m)
{
return dp[n][m]=f(n,n);
}else if(n>m)
{
return dp[n][m]=f(n,m-)+f(n-m,m);
}
}
int main()
{
//两种做法
//LL a[]={0,1,2,3,5,7,11,15,22,30,42,56,77,101,135,176,231,297,385,490,627,792,1002,1255,1575,1958,2436,3010,3718,4565,5604,6842,8349,10143,12310,14883,17977,21637,26015,31185,37338,44583,53174,63261,75175,89134,105558,124754,147273,173525,204226,239943,281589,329931,386155,451276,526823,614154,715220,831820,966467,1121505,1300156,1505499,1741630,2012558,2323520,2679689,3087735,3554345,4087968,4697205,5392783,6185689,7089500,8118264,9289091,10619863,12132164,13848650,15796476,18004327,20506255,23338469,26543660,30167357,34262962,38887673,44108109,49995925,56634173,64112359,72533807,82010177,92669720,104651419,118114304,133230930,150198136,169229875,190569292,214481126,241265379,271248950,304801365,342325709,384276336,431149389,483502844,541946240,607163746,679903203,761002156,851376628,952050665,1064144451,1188908248,1327710076,1482074143,1653668665,1844349560,2056148051,2291320912,2552338241,2841940500};
/*for(int i=1;i<max_v;i++)
{
printf("%I64d,",f(i,i));
}
printf("\n**\n");*/
memset(dp,-,sizeof(dp));//没有搜索的标记
int n;
while(~scanf("%d",&n))
{
//printf("%I64d\n",a[n]);
printf("%I64d\n",f(n,n));
}
return ;
}
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 母函数
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 (母函数或者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 (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 (递归,dp)
以下引用部分全都来自:http://blog.csdn.net/ice_crazy/article/details/7478802 Ice—Crazy的专栏 分析: HDU 1028 摘: 本题的意 ...
- 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 (动态规划)
题目链接: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://acm.hdu.edu.cn/showproblem.php?pid=1028 dp[i][j]表示数值为i,然后最小拆分的那个数是j的时候的总和. 1 = 1 2 = 1 + 1 . ...
随机推荐
- Linux_vim文本编辑器指令整理
一般指令模式 : 可以移动光标,可以删除字符和删除整列,可以复制粘贴 编辑模式 : 按下"i, I, o, O, a, A, r, R"任意一个字母时进入;按下ESC退出编辑模式 ...
- Effective C++ .44 typename和class的不同
在C++模板中的类型参数一般可以使用typename和class,两者没有什么不同.但是typename比class多项功能: “任何时候当你想要在template中指涉一个嵌套从属类型名称,就必须在 ...
- Js事件监听封装(支持匿名函数)
先看demo:http://liutian1937.github.io/demo/EventListen.html/*绑定事件与取消绑定*/ var handleHash = {}; var bind ...
- Java 监听器,国际化
1. 监听器 1.1 概述 监听器: 主要是用来监听特定对象的创建或销毁.属性的变化的! 是一个实现特定接口的普通java类! 对象: 自己创建自己用 (不用监听) 别人创建自己用 (需要监听) Se ...
- 【NLP_Stanford课堂】文本分类1
文本分类实例:分辨垃圾邮件.文章作者识别.作者性别识别.电影评论情感识别(积极或消极).文章主题识别及任何可分类的任务. 一.文本分类问题定义: 输入: 一个文本d 一个固定的类别集合C={c1,c2 ...
- web.xml配置错误页面,及输出错误信息
1.需要在web.xml中配置相关信息 <!-- 默认的错误处理页面 --> <error-page> <error-code>403</error-code ...
- SSH框架里的iHiberBaseDAO类与iHiberDAOImpl写法
一.iHiberBaseDAO类 package basic; import java.util.*; /** * 基于hibernate技术实现的BaseDAO基类 * @author ZHANGY ...
- Elasticsearch查询类型
Elasticsearch支持两种类型的查询:基本查询和复合查询. 基本查询,如词条查询用于查询实际数据. 复合查询,如布尔查询,可以合并多个查询, 然而,这不是全部.除了这两种类型的查询,你还可以用 ...
- xml布局显示需要预判断,可是还没有show出来,怎么办?
最近在实际工作中遇到了一种情况,写一个音量条,音量条显示出来之前要判断系统的音量大小,然后给音量条设置显示的位置.解决办法有两种, 第一种: m_pHostThread>MsgAsyncC ...
- Python学习---协程 1226
协程[是一个单线程],又称微线程,纤程.英文名Coroutine. 一句话说明什么是协程:协程是一种用户态的轻量级线程[程序员自己去切换线程] 协程条件: 必须在只有一个单线程里实现并发 修改共享数据 ...