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

1

2

3

Sample Output

1

2

4

题意:

就是n个人,站成一排。

有一个要求,(F)女生不能单独一个人站在男生之间。

可以没有女生。

输出有多少种站法;

(不考虑人与人的不同,只考虑位置和男女区别)

(如果一排以MF结尾是不合法的)

分析:

假如n个人的站法为db[n];

由前面的推导出db[n]。

db[n-1]结尾添加一个M,是一定可以的。

db[n-2]结尾添加FF,也是一定可以的。

添加MF不可以,添加MM也是可以的(但是这个情况和db[n-1]中重复了),添加FM也是和db[n-1]+M重复了。

在不可以序列后面加上FF(MF不可以,加上FF),成为合法,

所以db[n-4]后面+MFFF可以, 其实加一个F也能构成合法,但是这种情况包含在db[n-2](相当与+FF)里面;

所以递推方程式db[n] =db[n-1] + db[n-2] + db[n-4];

db[i] 中保存的都是合法序列数。

Java大数秒A~~~

import java.math.BigInteger;
import java.util.Scanner; public class Main{
static BigInteger db[] = new BigInteger[1001];
public static void main(String[] args) {
dabiao();
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n =sc.nextInt();
System.out.println(db[n]);
}
}
private static void dabiao() {
db[0]=new BigInteger("1");
db[1]=new BigInteger("1");
db[2]=new BigInteger("2");
db[3]=new BigInteger("4");
db[4]=new BigInteger("7");
for(int i=5;i<db.length;i++){
db[i]=db[i-1].add(db[i-2]).add(db[i-4]);
}
}
}

HDOJ/HDU 1297 Children’s Queue(推导~大数)的更多相关文章

  1. HDU 1297 Children’s Queue (递推、大数相加)

    Children’s Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. 【HDOJ】1297 Children’s Queue

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

  3. HDOJ 1297 Children’s Queue

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

  4. 杭电ACM 1297 Children’s Queue

    这道题是排序问题,可以用递归方法解决. 计算F(n): 一:当最后一个是男孩M时候,前面n-1个随便排出来,只要符合规则就可以,即是F(n-1): 二:当最后一个是女孩F时候,第n-1个肯定是女孩F, ...

  5. HDOJ(HDU) 2524 矩形A + B(推导公式、)

    Problem Description 给你一个高为n ,宽为m列的网格,计算出这个网格中有多少个矩形,下图为高为2,宽为4的网格. Input 第一行输入一个t, 表示有t组数据,然后每行输入n,m ...

  6. HDOJ(HDU) 1977 Consecutive sum II(推导、、)

    Problem Description Consecutive sum come again. Are you ready? Go ~~ 1 = 0 + 1 2+3+4 = 1 + 8 5+6+7+8 ...

  7. Children’s Queue(hdu1297+递推)

    Children’s Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. HDUOJ Children’s Queue

    Children’s Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. hdu 1297

    Children's Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...

随机推荐

  1. Android30-Fragment-理解

        Android30-Fragment-理解 规范 mobileSafe V2.0 欢迎页面 用户第一次是否需要用户提示 新闻类app的数据是怎么获取的 知乎提问?如何把身边资源最大化 第二种就 ...

  2. SQL Server 2008文件与文件组的关系

    此文章主要向大家讲述的是SQL Server 2008文件与文件组,其中包括文件和文件组的含义与关系,文件.文件组在实践应用中经常出现的问题,查询文件组和文件语句与MSDN官方解释等相关内容的介绍. ...

  3. Access的转义字符

    Access中数据库转义字符规则: 插入.更新.=匹配 数据时,文本类型如用''括起来,中间可以有 ",*,%,[,],/,/,?,(,),{,}的任意组合,如要插入一个',需写''并在整个 ...

  4. iOS 开发工具

    Github 社区 https://github.com/ iOS 开发类库 http://www.code4app.com/thread-7831-1-1.html (出处: Code4App-iO ...

  5. 《转》前端性能优化----yahoo前端性能团队总结的35条黄金定律

    除了自己总结:1. 减少http请求,2.压缩并优化js/css/image 3.尽量静态页面,从简原则 4.代码规范(详见:个人知识体系思维导图) 从yahoo 新学到的: 网页内容 减少http请 ...

  6. Frameset布局

    <FRAMESET> <FRAME> <NOFRAMES> <IFRAME> ■ 框架概念 : 所谓框架便是网页画面分成几个框窗,同时取得多个 URL. ...

  7. 网络基础---OSI 模型与TCP/IP

    一.网络的演进: 1.简单的联接:1960's ------------ 1970's    Host Network 六十至七十年代,网络的概念主要是主机架构的低速串行联接,提供应用程序执行.远程打 ...

  8. PowerDesigner 的几个使用技巧

    1.  生成sql脚本 Database→Generate Database 选择要输出的文件路径,即文件存储路径,并根据需要修改文件名,单击确定后便会生成sql脚本.   在Options选项卡里, ...

  9. 使用APPLICATION制作缓存,转存一下,有一段写的还可以。

    public sealed class CacheManager   {   private HttpApplicationState appPool = null;   /// <summar ...

  10. js判断用户进入设备代码

    var system ={ win : false, mac : false, xll : false }; //检测平台 var p = navigator.platform; system.win ...