Children’s Queue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 16006    Accepted Submission(s): 5337

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

对于各种排列而言,用f(n)表示其长度为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-1的排列,加一个M是可行的(最好是男孩的情形);对于之前长度n-2的排列,加加上FF是可行的(加MM有可能造成排列重复,这是最后是女孩的情形,并且前n-2个人是可行的排列);另外一种情况是,前n-2个人是不可行的排列,即最后的两个人是“MF”,再加上两个“FF”就变成可行的排列,这种情况也就是在n-4人可行的排列基础上加上“MFFF”。

C++:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int s[][];
int main()
{
s[][]=;
s[][]=;
s[][]=;
s[][]=;
int i,j,c,ans=;
for(i=;i<=;i++)
{
for(j=,c=;j<;j++)
{
ans=s[i-][j]+s[i-][j]+s[i-][j]+c;
c=ans/;
s[i][j]=ans%;
}
}
int n;
while(cin>>n)
{
j=;
while(!s[n][j])
j--;
cout<<s[n][j];
for(i=j-;i>=;i--)
printf("%08d",s[n][i]);
cout<<endl;
}
return ;
}

用JAVA

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
BigInteger a[]=new BigInteger[1001];
int n;
while(in.hasNextInt()) {
n=in.nextInt();
a[1]=BigInteger.valueOf(1);
a[2]=BigInteger.valueOf(2);
a[3]=BigInteger.valueOf(4);
a[4]=BigInteger.valueOf(7);
for(int i=5;i<=n;i++) {
a[i]=BigInteger.ZERO;
a[i]=a[i].add(a[i-1]);
a[i]=a[i].add(a[i-2]);
a[i]=a[i].add(a[i-4]);
}
System.out.println(a[n]);
}
}
}

(递推 大整数) Children’s Queue hdu1297的更多相关文章

  1. POJ 2506 Tiling(递推+大整数加法)

    http://poj.org/problem?id=2506 题意: 思路:递推.a[i]=a[i-1]+2*a[i-2]. 计算的时候是大整数加法.错了好久,忘记考虑1了...晕倒. #includ ...

  2. CF1500D Tiles for Bathroom (递推+大讨论)

    题目大意:给你一个n*n的矩阵,现在问对于每个k\le n,求出所有k*k的子矩阵中,元素种类数不超过q的矩阵个数,n\le 1500, q\le 10 先考虑最暴力的做法: 对于每个格子,求出以它为 ...

  3. HDU1297 Children’s Queue (高精度+递推)

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

  4. 【高精度递推】【HDU1297】Children’s Queue

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

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

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

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

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

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

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

  8. codeforces D. Queue 找规律+递推

    题目链接: http://codeforces.com/problemset/problem/353/D?mobile=true H. Queue time limit per test 1 seco ...

  9. HDU-4651 Partition 整数拆分,递推

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4651 题意:求n的整数拆为Σ i 的个数. 一般的递归做法,或者生成函数做法肯定会超时的... 然后要 ...

随机推荐

  1. Zookeeper 源码学习(一)环境搭建

    前言 最近准备学习 Zookeeper,想从 Zookeeper 开始逐步深入了解各类中间件,学习分布式计算. 下载源码 执行指令,下载代码: git clone https://github.com ...

  2. linux第四次读书笔记

    第四章:进程调度 一.多任务 1.非抢占式多任务 进程会一直执行直到自己主动停止运行(这一步骤称为让步) 2.抢占式多任务 Linux/Unix使用的是抢占式的方式:强制的挂起进程的动作就叫做抢占.进 ...

  3. B01-java学习-阶段2-面向对象

    对象内存分析 构造方法 类的深入解释 预定义类型和自定义类型深入分析和解释 预定义类源码的查看 预定义类和自定义类的对比 跨过类中使用自定义类型作为属性类型的门槛 构造方法的定义和执行过程 编译器提供 ...

  4. [BUAA_SE_2017]第零次博客

    结缘计算机 你为什么选择计算机专业?你认为条件如何? 计算机是你喜欢的领域吗?是你擅长的领域吗? 说来也巧,高考选择专业时并不知道自己会这般喜欢计算机这个专业,却将其填在了北航的第一志愿. 第一次接触 ...

  5. PhantomJS - Scriptable Headless Browser

    http://phantomjs.org/ PhantomJS is an optimal solution for: Page automation Access webpages and extr ...

  6. HTML 5 placeHolder

    <html> <body> <input type="text" id="idNum" placeholder="pla ...

  7. Qt__主窗口、菜单和工具条(QMainWindow,QMenu,QToolBar)

    转自豆子空间 主窗口 Qt的GUI程序有一个常用的顶层窗口,叫做MainWindow.MainWindow继承自QMainWindow.QMainWindow窗口分成几个主要的区域: 最上面是Wind ...

  8. pandas聚合aggregate

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/5/24 15:03 # @Author : zhang chao # @Fi ...

  9. js數組

    數組對象創建: var a=new Array(); var b=new Array(1); var a=new Array(“AA“,”AA“): 相關函數: sort()排序,可以進行字面上排序s ...

  10. 非关心数据库无法进行连表查询 所以我们需要在进行一对多查询时候 无法满足 因此需要在"1"的一方添加"多"的一方的的id 以便用于进行连表查询 ; 核心思想通过id进行维护与建文件

     非关心数据库无法进行连表查询 所以我们需要在进行一对多查询时候 无法满足 因此需要在"1"的一方添加"多"的一方的的id 以便用于进行连表查询 ; 核心思想通 ...