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. qt5---步长调节器SpinBox和QDoubleSpinBox

    #include <QSpinBox>            #include <QDoubleSpinBox> QSpinBox 用于整数的显示和输入,一般显示十进制数,也可 ...

  2. qt5--树形控件QTreeWidget

    需要       #include <QTreeWidget>               #include <QTreeWidgetItem> #include " ...

  3. node监视文件或者文件夹的变化

    1.node提供一个watch这个API,可以监视文件或文件夹的变化,当发生改变时,做出某些操作.(当做代码本地构建时,经常使用) const fs = require('fs'); fs.watch ...

  4. spring cloud禁止输出日志:ConfigClusterResolver : Resolving eureka endpoints via configuration

    springcloud的注册中心客户端会每隔一定时间向注册中心服务端发送心跳,用此来判断注册中心服务端是否运行正常. 这样导致不断进行日志输出,不便查看正常的业务日志输出. c.n.d.s.r.aws ...

  5. 7、菜单栏、工具栏、状态栏、浮动窗口、TextEdit

    新建项目,基类选择QMainWindow,不勾选ui    mainwindow.cpp代码: #include "mainwindow.h" #include <QMenu ...

  6. dataX调优

    dataX调优 标签(空格分隔): ETL 一,Datax调优方向 DataX调优要分成几个部分(注:此处任务机指运行Datax任务所在的机器). 1,网络本身的带宽等硬件因素造成的影响: 2,Dat ...

  7. Struts2理解?

    (1)Struts2是一个基于MVC设计模式的Web应用框架,在MVC设计模式中Struts2作为控制器(Controller)来建立模型与视图的数据交互. Struts 2以WebWork为核心,采 ...

  8. LeetCode124----二叉树中最大路径和

    给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不需要经过根节点. 示例 1: 输入: [1,2,3] 1 / \ 2 ...

  9. spark 笔记 3:Delay Scheduling: A Simple Technique for Achieving Locality and Fairness in Cluster Scheduling

    spark论文中说他使用了延迟调度算法,源于这篇论文:http://people.csail.mit.edu/matei/papers/2010/eurosys_delay_scheduling.pd ...

  10. [论文理解] Spatial Transformer Networks

    Spatial Transformer Networks 简介 本文提出了能够学习feature仿射变换的一种结构,并且该结构不需要给其他额外的监督信息,网络自己就能学习到对预测结果有用的仿射变换.因 ...