HDOJ(HDU).2044-2049 递推专题
HDOJ(HDU).2044-2049 递推专题
HDU.2044
题意分析
先考虑递推关系:从1到第n个格子的时候由多少种走法?
如图,当n为下方格子的时候,由于只能向右走,所以有2中走法。当n为上方格子的时候,由于只能向右走,所以也有2种走法。
不妨用a[n]来表示第n个格子有几种走法,根据上述描述,不难找出递推关系,a[n] = a[n-1] + a[n-2]。但是对于n<2的数字,就不适用了。也很简单,n<2的时候数一下即可,从1走到1,只有一种走法,从1走到2,有一种走法。问题就解决了。
然后我们接着考虑从b走到c有几种走法。不放就看样例给的3-6。从图中可以看出,从3-6走的方法和从1-4走的方法总数是一样的。故从b到c的的方法数为a[c-b+1].
代码总览
/*
Title:HDOJ.2044
Author:pengwill
Date:2017-2-13
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
ll a[51];
int main()
{
a[1] = a[2] = 1;
for(int i = 3 ; i<=50 ;++i) a[i] = a[i-1] + a[i-2];
int b,c,t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&b,&c);
printf("%lld\n",a[c-b+1]);
}
return 0;
}
HDU.2045
题意分析
由于前一个颜色的选择会影响到下一个颜色的选择,对于前n个格子的涂法,就要对前n-1个格子的颜色进行分析。由于要求是首位的颜色不同,那么就要对第n-1个格子是否与第一个格子颜色相同分类讨论。a[n]代表前n个格子的涂法。
若第一个格子与第n-1个格子的颜色不同,那么第n个格子的就有1种涂法;若第n-1个格子与第一个格子颜色相同,那么第n个格子就有2种涂法,需要注意的是,其实这种情况的方案数是与a[n-2]有关的,因为此时方案数是a[n-2] * 1 * 2 (n-1格子颜色已经确定了)
代码总览
/*
Title:HDOJ.2045
Author:pengwill
Date:2017-2-13
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
ll a[51];
int main()
{
a[1] = 3; a[2] = 6; a[3] = 6;
for(int i = 4; i<=50; ++i) a[i] = a[i-2]*2 + a[i-1];
int n;
while(scanf("%d",&n) != EOF){
printf("%lld\n",a[n]);
}
return 0;
}
HDU.2046
题意分析
代码总览
/*
Title:HDOJ.2046
Author:pengwill
Date:2017-2-13
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
ll a[51];
int main()
{
a[1] = 1; a[2] = 2;
for(int i = 3; i<=50; ++i) a[i] = a[i-2] + a[i-1];
int n;
while(scanf("%d",&n) != EOF){
printf("%lld\n",a[n]);
}
return 0;
}
HDU.2047
题意分析
由于n-1位是否为O,影响第n位的选择,所以很明显,这道题有2种状态,一种是n-1是O,一种是n-1不是O。第n位的方案数就是,n为O的方案 + n不为O的方案。
a[n]表示O结尾的方案数 b[n]不是以O结尾的方案数,c[n]表示总的方案数。那么c[n] = a[n] + b[n]。
不难发现:
a[1] = 1; a[2] = 2; c[1] = 3;
b[1] = 2; b[2] = 6; c[2] = 8;
然后分别找一下a[n]和b[n]的递推关系式:
由于不能有O相连,那么n-1不为O的时候,n位才有可能为O,可以得出:a[n] = b[n-1];
接着考虑什么时候第n位为非O呢,当第n-1位是O的时候,n可能有EF2种非O选择,当n-1位为O的时候,也有EF2种非O选择,于是有:b[n] = 2 * (a[n-1] + b[n-1])
有了ab的递推关系式,根据c[n] = a[n] + b[n] 问题也就迎刃而解了。
代码总览
/*
Title:HDOJ.2047
Author:pengwill
Date:2017-2-13
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
ll a[41],b[41],c[41];
int main()
{ // a以O结尾的结果数量 b非以O的数量
a[1] = 1; a[2] = 2; c[1] = 3;
b[1] = 2; b[2] = 6; c[2] = 8;
for(int i = 3; i<=40; ++i){
a[i] = b[i-1];
b[i] = 2*(a[i-1] + b[i-1]);
c[i] = a[i] + b[i];
}
int n;
while(scanf("%d",&n) != EOF){
printf("%lld\n",c[n]);
}
return 0;
}
HDU.2048
题意分析
此题的递推关系体现在错排公式,详情百度。
代码总览
/*
Title:HDOJ.2048
Author:pengwill
Date:2017-2-13
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
ll a[21]={0,0,1},b[21] = {0,1};
ll cal(int i)
{
if(b[i] != 0) return b[i];
else return (cal(i-1)*i);
}
void init()
{
for(int i = 1;i<=20;++i)
b[i] = cal(i);
for(int i = 3; i<=20;++i)
a[i] = (i-1) * (a[i-1]+a[i-2]);
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
printf("%.2f%%\n",100.0*a[n]/b[n]);
}
}
HDU.2049
题意分析
此题的递推关系体现在错排公式,详情百度。
代码总览
/*
Title:HDOJ.2049
Author:pengwill
Date:2017-2-13
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
ll a[21]={0,0,1},b[21] = {1,1};//a是错排,b是全排列
ll cal(int i)
{
if(b[i] != 0) return b[i];
else return (cal(i-1)*i);
}
void init()
{
for(int i = 1;i<=20;++i)
b[i] = cal(i);
for(int i = 3; i<=20;++i)
a[i] = (i-1) * (a[i-1]+a[i-2]);
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--){
int n,m;
scanf("%d%d",&n,&m);
printf("%lld\n",b[n]/b[m]/b[n-m] * a[m]);
}
}
HDOJ(HDU).2044-2049 递推专题的更多相关文章
- 致初学者(四):HDU 2044~2050 递推专项习题解
所谓递推,是指从已知的初始条件出发,依据某种递推关系,逐次推出所要求的各中间结果及最后结果.其中初始条件或是问题本身已经给定,或是通过对问题的分析与化简后确定.关于递推的知识可以参阅本博客中随笔“递推 ...
- hdu 2044-2050 递推专题
总结一下做递推题的经验,一般都开成long long (别看项数少,随便就超了) 一般从第 i 项开始推其与前面项的关系(动态规划也是这样),而不是从第i 项推其与后面的项的关系. hdu2044:h ...
- <每日一题> Day6:HDU递推专题完结
原题链接 这是我自己Clone的专题,A,B题解昨天发过了 C:参考代码: /* 很容易我们可以手推出n = 1, 2, 3时的情况,我们假设前n - 1 列已经放好,方法有dp[n - 1]种,第n ...
- 一只小蜜蜂(hdoj 2044,动态规划递推)
Problem Description 有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数.其中,蜂房的结构如下所示. Input 输入数据的第一行 ...
- HDU 4747 Mex 递推/线段树
题目链接: acm.hdu.edu.cn/showproblem.php?pid=4747 Mex Time Limit: 15000/5000 MS (Java/Others)Memory Limi ...
- HDU 2604 Queuing(递推+矩阵)
Queuing [题目链接]Queuing [题目类型]递推+矩阵 &题解: 这题想是早就想出来了,就坑在初始化那块,只把要用的初始化了没有把其他的赋值为0,调了3,4个小时 = = 本题是可 ...
- HDU - 2604 Queuing(递推式+矩阵快速幂)
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 1723 DP/递推
题意:有一队人(人数 ≥ 1),开头一个人要将消息传到末尾一个人那里,规定每次最多可以向后传n个人,问共有多少种传达方式. 这道题我刚拿到手没有想过 DP ,我觉得这样传消息其实很像 Fibonacc ...
- hdu 1249 三角形 (递推)
三角形 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
随机推荐
- Ubuntu 14.04 登录 界面添加 root账号
1打开终端输入:sudo gedit /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf 2在弹出的编辑框里加入:greeter-show-manual- ...
- 获取附加在方法上的Attribute
如下: class Program { static void Main(string[] args) { var methodInfo = typeof(Program).GetMethod(&qu ...
- 2019年猪年海报PSD模板-第一部分
14套精美猪年海报,免费猪年海报,下载地址:百度网盘,https://pan.baidu.com/s/1i7bIzPRTX0OMbHFWnqURWQ
- java 泛型历史遗留问题
Map<String,Integer> hashMap = new HashMap<String,Integer>(); hashMap.put(); // hashMap.p ...
- 「日常训练」All Friends(POJ-2989)
题意 分析 代码 #include <iostream> #include <cstring> #include <algorithm> #define MP ma ...
- 文件包含漏洞(RFI)
1文件包含漏洞简介 include require include_once require_once RFI综述 RFI是Remote File Inclusion的英文缩写,直译过来就是远 ...
- Linux中常用Shell命令
本随笔文章,由个人博客(鸟不拉屎)转移至博客园 写于:2018 年 05 月 04 日 原地址:https://niaobulashi.com/archives/linux-shell.html -- ...
- 利用爬虫、SMTP和树莓派3B发送邮件&续集&(爬取墨迹天气预报信息)
-----------------------------------------------学无止境----------------------------------------------- 前 ...
- Attention注意力机制介绍
什么是Attention机制 Attention机制通俗的讲就是把注意力集中放在重要的点上,而忽略其他不重要的因素.其中重要程度的判断取决于应用场景,拿个现实生活中的例子,比如1000个人眼中有100 ...
- 为什么23种设计模式中没有MVC
GoF (Gang of Four,四人组, <Design Patterns: Elements of Reusable Object-Oriented Software>/<设计 ...