HDOJ/HDU 1297 Children’s Queue(推导~大数)

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]);
        }
    }
}
时间: 2024-08-06 16:05:08

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

hdu 1297 Children’s Queue

Children's Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10540    Accepted Submission(s): 3390 Problem Description There are many students in PHT School. One day, the headmaster whose n

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

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 na

HDOJ 1297 Children’s Queue

JAVA大数.... Children's Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10390    Accepted Submission(s): 3333 Problem Description There are many students in PHT School. One day, the headmas

hdoj 1297 Children’s Queue 【高精度】【递推】

题意:有n个人,每一个人可以是男孩也可以是女孩,要求每个女孩不能单独一个,也就是一个女孩的左右紧挨的位置至少要有一个女孩.问这样的队列有几个. 分析:设f(n)是n的排列的数目,这时候来一个人: 一:如果是男孩,那么f(n ) = f(n-1) 二:如果是女孩,如果前n-2是合法的,那么f(n) = f(n-2):如果前n-2不合法的,那么n-2队列的末尾两个同学肯定是男+女,那么再加上后来的女孩,又合法了,此时f(n) = f(n-4); 综上:f(n) = f(n-1)+f(n-2)+f(n

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

Problem Description 给你一个高为n ,宽为m列的网格,计算出这个网格中有多少个矩形,下图为高为2,宽为4的网格. Input 第一行输入一个t, 表示有t组数据,然后每行输入n,m,分别表示网格的高和宽 ( n < 100 , m < 100). Output 每行输出网格中有多少个矩形. Sample Input 2 1 2 2 4 Sample Output 3 30 此方格其实就是求其中所有格子数,如果按宽度来算的话,1,2,3,-m,种情况,对每一种情况,有(1+2

(hdu step 3.1.7)Children’s Queue(求n个人站在一起有m个人必须是连在一起的方案数)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: Children's Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 853 Accepted Submission(s): 479   Problem

HDOJ/HDU 1250 Hat&#39;s Fibonacci(大数~斐波拉契)

Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your task is to take

Children’s Queue(递推)

Children's Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12101    Accepted Submission(s): 3953 Problem Description There are many students in PHT School. One day, the headmaster whose n

Children’s Queue(hdu1297+递推)

Children’s Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12101 Accepted Submission(s): 3953 Problem Description There are many students in PHT School. One day, the headmaster whose name is