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-10-09 19:57:27

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

HDOJ/HDU 1250 Hat&amp;#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

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 1509 Windows Message Queue

问题描述 HDU 1509 Windows Message Queue 自己测试总刚觉没错,求高手帮忙,不知道哪错了,总wa.................................. 解决方案 import java.util.Comparator; import java.util.PriorityQueue; import java.util.Scanner; class Nod { String name; int val, pri; public Nod(String name

HDOJ/HDU 1865 1sting(斐波拉契+大数~)

Problem Description You will be given a string which only contains '1'; You can merge two adjacent '1' to be '2', or leave the '1' there. Surly, you may get many different results. For example, given 1111 , you can get 1111, 121, 112,211,22. Now, you

HDOJ/HDU 5686 Problem B(斐波拉契+大数~)

Problem Description 度熊面前有一个全是由1构成的字符串,被称为全1序列.你可以合并任意相邻的两个1,从而形成一个新的序列.对于给定的一个全1序列,请计算根据以上方法,可以构成多少种不同的序列. Input 这里包括多组测试数据,每组测试数据包含一个正整数N,代表全1序列的长度. 1≤N≤200 Output 对于每组测试数据,输出一个整数,代表由题目中所给定的全1序列所能形成的新序列的数量. Sample Input 1 3 5 Sample Output 1 3 8 Hin

HDOJ(HDU) 2523 SORT AGAIN(推导排序、、)

Problem Description 给你N个整数,x1,x2-xn,任取两个整数组合得到|xi-xj|,(0 < i,j<=N,i!=j). 现在请你计算第K大的组合数是哪个(一个组合数为第K大是指有K-1个不同的组合数小于它). Input 输入数据首先包含一个正整数C,表示包含C组测试用例. 每组测试数据的第一行包含两个整数N,K.(1< N<=1000,0< K<=2000) 接下去一行包含N个整数,代表x1,x2..xn.(0<=xi<=2000

HDOJ/HDU 1161 Eddy&amp;#39;s mistakes(大写字母转换成小写字母)

Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for example "computer science" is written frequently "coMpUtEr scIeNce" by him, this mistakes lets Eddy's English teacher be extremely disco

HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)

Problem Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more t

HDOJ/HDU 2549 壮志难酬(取小数点后几位~)

Problem Description 话说MCA山上各路豪杰均出山抗敌,去年曾在江湖威名显赫的,江湖人称<万军中取上将首级舍我其谁>的甘露也不甘示弱,"天将降大任于斯人也,必先劳其筋骨,饿其体肤,空乏其身"他说.可惜,由于去年取上将首级时不慎右手右关节第七次骨折,养伤达一年之久,空有一腔抱负却壮志难酬,如今天下危亡,习武之人又怎能袖手旁观,于是他决定出山协助威士忌共抗辽贼,这时他的对头枫冰叶子出现,两人都是水属性,但由于十年前的一场恩怨(这是后话)势成水火. 枫冰叶子要求