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. ubuntu1804 安装Nvidia驱动-nvidia-docker

    操作 看显卡 lspci | grep N 01:00.0 3D controller: NVIDIA Corporation GM107M [GeForce GTX 960M] (rev a2) 0 ...

  2. C# 3.0

    序言 自动实现的属性 匿名类型 查询表达式 Lambda 表达式 从 C# 3 开始,lambda 表达式提供了一种更简洁和富有表现力的方式来创建匿名函数. 使用 => 运算符构造 lambda ...

  3. c++复习——一个小疑问

    C++中,子类为什么不能访问基类的private数据?     emmm  来自一个vegetable dog的疑问:   首先基类可以通过调用自身public成员函数来访问private 而子类又可 ...

  4. Linux命令-文件管理(四)

    Linux命令-文件管理 Linux slocate命令 Linux slocate命令查找文件或目录. slocate本身具有一个数据库,里面存放了系统中文件与目录的相关信息. 语法 slocate ...

  5. [CSP-S模拟测试]:中间值(二分)

    题目背景 $Maxtir$喜欢序列的中间值. 题目传送门(内部题127) 输入格式 第一行输入两个正整数$n,m$,其中$m$是操作和询问次数. 接下来两行每行输入$n$个非负整数,每一行分别表示两个 ...

  6. 利用MFC在控件内将txt中的数据画图

    1:采集txt文件中的数据测试程序如下: #include "stdafx.h" #include <fstream> #include "iostream& ...

  7. MySQL定时任务实现方法

    类型一:每隔一分钟插入一条数据:   参数说明: DEFINER:创建者: ON COMPLETION [NOT] PRESERVE :表示当事件不会再发生的情况下,删除事件(注意特定时间执行的事件, ...

  8. Uploading multiple files asynchronously by blueimp jquery-fileupload

    Uploading multiple files asynchronously by blueimp jquery-fileupload   Solved. Fiddle: http://jsfidd ...

  9. vmx转换ofv模板,导入esxi

    使用VMware Workstation安装目录下\OVFTool文件的ovftool.exe工具: 转换示例: 首先进入OVFTool根目录.然后执行 ovftool.exe "D:\ce ...

  10. 使用DBLink方式同步远程数据库中含Blob、Clob字段表的问题解决

    在数据库同步中我们经常会用到DBLink方式.DBLink可以将物理上存放于网络的多个数据库在逻辑上当成一个单一的大数据库.开发人员无需关心数据库的网络分布,就能很方便的实现从不同数据库之间读取数据. ...