(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 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


Author

SmallBeer (CML)


Source

杭电ACM集训队训练赛(VIII)


Recommend

lcy

题目分析:

一个长度n的队列可以看成一个n - 1的队列再追加的1个小孩,这个小孩只可能是:

a.男孩,任何n - 1的合法队列追加1个男孩必然是合法的,情况数为f[n - 1];

b.女孩,在前n - 1的以女孩为末尾的队列后追加1位女孩也是合法的,我们可以转化为n - 2的队列中追加2位女孩;

一种情况是在n - 2的合法队列中追加2位女孩,情况数为f[n - 2];但我们注意到本题的难点,可能前n - 2位以女孩为末尾的不合法队列(即单纯以1位女孩结尾),也可以追加2位女孩成为合法队列,而这种n - 2不合法队列必然是由n - 4合法队列+1男孩+1女孩的结构,即情况数为f[n - 4]。

因为这道题的n已经达到1000了,而且又是“累加”这种计算模型,所以果断使用java的大数来做

代码如下:

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	public final static int maxn = 1001;
	static BigInteger dp[] = new BigInteger[maxn];

	public static void prepare(){
		dp[1] = BigInteger.valueOf(1);
		dp[2] = BigInteger.valueOf(2);
		dp[3] = BigInteger.valueOf(4);
		dp[4] = BigInteger.valueOf(7);

		int i;
		for(i = 5 ; i < maxn ; ++i){
			dp[i] = new BigInteger("0");
			dp[i] = dp[i].add( dp[i-1]).add( dp[i-2]).add( dp[i-4]);
		}
	}

	public static void main(String[] args) {
		prepare();

		Scanner scanner = new Scanner(System.in);
		while(scanner.hasNext()){
			int n = scanner.nextInt();

			System.out.println(dp[n]);
		}
	}
}
时间: 2024-10-07 06:48:55

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

(hdu step 4.2.4)A strange lift(求从起点到终点的最小步数,限制条件是:在一维的情况下)

题目: A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 709 Accepted Submission(s): 348   Problem Description There is a strange lift.The lift can stop can at every floor as you want, a

(hdu step 2.2.8)N!Again(求N!的阶乘%2009以后的结果)

题目: N!Again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 941 Accepted Submission(s): 526   Problem Description WhereIsHeroFrom:             Zty, what are you doing ?Zty:                        

(hdu step 5.2.5)确定比赛名次(求拓扑序列)

题目: 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 337 Accepted Submission(s): 180   Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得

(hdu step 2.1.3)Largest prime factor(求一个数的最大质因子的位置)

题目: Largest prime factor Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4868 Accepted Submission(s): 1452   Problem Description Everybody knows any number can be combined by the prime number.Now,

(hdu step 5.2.6)确定比赛名次(求入度为0的点的个数)

题目: 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 338 Accepted Submission(s): 181   Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得

(hdu step 3.2.8)命运(简单DP:求从左上角走到右下角的最大值)

题目: 命运 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1247 Accepted Submission(s): 530   Problem Description 穿过幽谷意味着离大魔王lemon已经无限接近了!可谁能想到,yifenfei在斩杀了一些虾兵蟹将后,却再次面临命运大迷宫的考验,这是魔王lemon设下的又一个机关.要知道,

(hdu step 1.2.8)Specialized Four-Digit Numbers(求一个数字各个数位上数字的和)

题目: Specialized Four-Digit Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2027 Accepted Submission(s): 1349   Problem Description Find and list all four-digit numbers in decimal notation

(hdu step 7.1.3)Lifting the Stone(求凸多边形的重心)

题目: Lifting the Stone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 168 Accepted Submission(s): 98   Problem Description There are many secret openings in the floor which are covered by a big he

(hdu step 6.3.2)Girls and Boys(求最大独立集)

题目: Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 189 Accepted Submission(s): 127   Problem Description the second year of the university somebody started a study on the romanti