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. 牛客训练赛25-A-最长区间

    https://www.nowcoder.com/acm/contest/158#question 这题问最长的严格连续递增序列的最长长度是多少? 最开始感觉这道题不可做,因为有1e5个点,还有1e5 ...

  2. 如何在css中设置按钮button中包含图片文字对齐方式

    <el-button class="class-management style="line-heught">班级管理

  3. BF算法和KMP算法 python实现

    BF算法 def Index(s1,s2,pos = 0): """ BF算法 """ i = pos j = 0 while(i < ...

  4. UML图中类之间的关系:依赖,泛化,关联,聚合,组合,实现(转)

    UML图中类之间的关系:依赖,泛化,关联,聚合,组合,实现   类与类图 1) 类(Class)封装了数据和行为,是面向对象的重要组成部分,它是具有相同属性.操作.关系的对象集合的总称. 2) 在系统 ...

  5. SE Springer小组之《Spring音乐播放器》需求分析说明书二

    2.1 目标 Spring音乐播放器软件为课程<软件工程>所开发的课程作业,主要意图是为访问计算机中的mp3格式的音频文件,并使其能够完成访问,读取,添加,保存,播放,切换音频文件等功能. ...

  6. Dynamic Routing Based On Redis

    Dynamic Routing Based On Redis Ngnix技术研究系列2-基于Redis实现动态路由   上篇博文我们写了个引子: Ngnix技术研究系列1-通过应用场景看Nginx的反 ...

  7. 七牛云域名DV SSL证书申请流程以及CDN融合加速配置

    从2017年起,苹果ios以及微信小程序都陆续要求请求连接request地址是使用HTTPS协议的.所以在项目开发阶段就要考虑解决https的问题,同时这也是为项目实际安全所考虑.最近我也是在折腾项目 ...

  8. org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: c

    //这个是 配置文件放错了地方 org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing ...

  9. WIN10基于Hyper-V下运行kubernetes入门问题

    http://www.cnblogs.com/shanyou/p/8503839.html 安装配置好之后启动,查看ip的方法: minikube status minikube ip 查看仪表盘da ...

  10. [书摘]HTTPS--From图解HTTP

    1.  HTTP存在的安全性风险: 1) 通信过程使用明文,容易被窃听 2) 不验证通信方的身份,可能遭遇伪装 3) 不验证通信数据包的完整性,可能遭遇篡改 2. HTTP+加密+认证+完整性保护=H ...