Children’s Queue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17918    Accepted Submission(s): 5976

Problem Description

There are many students in PHT School. One day, the headmaster whose name is PigHeader wanted all students stand in a line. He prescribed that girl can not be in single. In other words, either no girl in the queue or more than one girl stands side by side. The case n=4 (n is the number of children) is like
FFFF, FFFM, MFFF, FFMM, MFFM, MMFF, MMMM
Here F
stands for a girl and M stands for a boy. The total number of queue
satisfied the headmaster’s needs is 7. Can you make a program to find
the total number of queue with n children?



Input

There
are multiple cases in this problem and ended by the EOF. In each case,
there is only one integer n means the number of children
(1<=n<=1000)



Output

For each test case, there is only one integer means the number of queue satisfied the headmaster’s needs.



Sample Input


Sample Output


题目大意

就是一堆小朋友排排坐,然后女生不能单独坐,要么没有女生,要么就是至少两个女生挨着坐,问n个小朋友有多少种坐法

题目分析

首先长度为1时,只有1种可能,即“M”;

  长度为2时,有2种可能,即“FF”和“MM”;

  长度为3时,有4种可能,即“FFF”、“FFM”、“MFF”和“MMM”;

  长度为4时,有7种可能,即“FFFF”、“FFFM”、“FFMM”、“MFFM”、“MFFF”、“MMFF”、“MMMM”;
当n>4时,我们可以这么想:
  如果第n个人是M,符合条件,这样的情况有f(n-1)个,因为是直接在n-1的情况下在最后加上了一个M

  如果第n个人是F,那么就需要考虑倒数第二个人,如果倒数第二个人是F,这是可以的,那么也就相当于 在n-2的基础上加了一个FF

          但是注意,刚刚我们是在n-2的基础上加了一个FF,也就是说,默认前n-2是合理的,但是也存在不合理的情况 也就是说 前面n-2是以MF结尾的,这时候加上FF也是合理的,也就相当于在n-4的基础上加上了MFFF

综上 我们可以列出来递推方程:
  f(n) = f(n-1)+f(n-2)+f(n-4)

剩下的就只是将大数模板套进去就好了

代码:

#include<bits/stdc++.h>

using namespace std;

int n,i;
string bigadd(string a,string b)
{
int jin=,i;
char ai,bi;
string anss=a;
int lena=a.size();
int lenb=b.size();
int lenmax=max(lena,lenb);
int p=lena-;
int q=lenb-;
for(i=lenmax-;i>=;i--)
{
if(p<)
ai='';
else
ai=a[p];
if(q<)
bi='';
else
bi=b[q];
anss[i]=((ai-''+bi-''+jin)%)+'';
jin=(ai-''+bi-''+jin)/;
p--;
q--;
}
if(jin)
{
char x=jin+'';
anss=x+anss;
}
return anss;
}
int main()
{
string a[];
a[]="";
a[]="";
a[]="";
a[]="";
for(i=;i<;++i)
a[i]=bigadd(bigadd(a[i-],a[i-]),a[i-]); //这里需要注意的是,我之前用的是bigadd(bigadd(a[i-4],a[i-2]),a[i-1]),但是WA了,我仔细想了想,这是由于我的大数相加模板导致的,如果后加的数比前面的数位数大,就会出现位数丢失的问题,所以必须先将最大的a[i-1]与a[i-2]相加。
while(scanf("%d",&n)!=EOF)
{
cout<<a[n]<<endl;
}
return ;
}

HDU 1297 Children’s Queue (递推、大数相加)的更多相关文章

  1. HDOJ/HDU 1297 Children’s Queue(推导~大数)

    Problem Description There are many students in PHT School. One day, the headmaster whose name is Pig ...

  2. Children’s Queue HDU 1297 递推+大数

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1297 题目大意: 有n个同学, 站成一排, 要求 女生最少是两个站在一起, 问有多少种排列方式. 题 ...

  3. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  4. 矩阵快速幂(queue递推)

    http://acm.hdu.edu.cn/showproblem.php?pid=2604 Queuing Time Limit: 10000/5000 MS (Java/Others)    Me ...

  5. 【HDOJ】1297 Children’s Queue

    递推,最近发现自己做递推的题总是没有思路.下周多练习.对于f(n)可以在第n个位置为男生,此时共有f(n-1)种情况:若在第n个位置为女生,因此第n-1个位置也必须为女生.此时有两种情况,一种情况是在 ...

  6. Tiling(递推+大数)

    Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tili ...

  7. HDOJ 1297 Children’s Queue

    版权声明:来自: 码代码的猿猿的AC之路 http://blog.csdn.net/ck_boss https://blog.csdn.net/u012797220/article/details/3 ...

  8. 【hdoj_1865】1sting(递推+大数)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1865 本题的关键是找递推关系式,由题目,可知前几个序列的结果,序列长度为n=1,2,3,4,5的结果分别是 ...

  9. hdu 5171(矩阵快速幂,递推)

    GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

随机推荐

  1. 让IE8和IE9支持 placeholder

    1.原因:placeholder是h5的新属性,IE10以前的浏览器(8.9)不支持此属性. 2.解决方法:jQuery三方插件  jquery-placeholder 3.快速开始: <!DO ...

  2. nodejs之express入门

    首先安装nodejs,官网下载安装包直接安装, 由于node自带npm,所以npm命令直接即可使用 打开cmd,使用npm install -g express-generator安装express ...

  3. LVS 学习

    参考网站:http://www.linuxvirtualserver.org 一,部分概念 服务器集群系统: 通过高性能网络或局域网互联的服务器集群正成为实现高可伸缩的.高可用网络服务的有效结构,这种 ...

  4. JavaScript 函数——语法,调用,返回值,局部变量,全局变量,未声明变量

    JavaScript 函数是被设计为执行特定任务的代码块. JavaScript 函数会在某代码调用它时被执行. ㈠函数 ⑴什么是函数 函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块. ⑵ ...

  5. poj 2976 Dropping tests 二分搜索+精度处理

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8349   Accepted: 2919 De ...

  6. 1110 Complete Binary Tree (25 分)

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  7. Link-Cut Tree(LCT) 教程

    目录 前置知识 介绍 Access FindRoot MakeRoot Split Link Cut 关于Splay中操作的一点说明: 模板 前置知识 请先对树链剖分和Splay有所了解.LCT基于树 ...

  8. C++入门经典-例3.5-判断某一年是否是闰年之嵌套判断

    1:代码如下: // 3.5.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using ...

  9. MQTT消息中间件Mosquitto的安装和配置

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  10. 7、kubernetes资源清单之Service资源190714

    一.Service简介 Service为Pod提供固定服务端点 Service的本质是一条iptables或者ipvs的转发规则 userspace:1.1- iptables:1.1+ ipvs:1 ...