当前位置:首页 > 芯闻号 > 充电吧
[导读]题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1297 题面: Children’s Queue Time Limit: 2000/1000 MS

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1297


题面:

Children’s Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13363    Accepted Submission(s): 4379


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)  
题目大意:     给定队伍长度,求不出现单独一个女生的方案数。
解题:     可以用dp三维来解决这个问题。dp[i][j][k],i表示是第i位,j为1表示男生,j为0表示女生,k为0表示0个女生,k为1表示1个女生,k为2表示多个女生,即合法状态。     递推关系如下:      
dp[i+1][0][2]=dp[i+1][0][2].add(dp[i][0][1]);
dp[i+1][0][2]=dp[i+1][0][2].add(dp[i][0][2]);
dp[i+1][1][0]=dp[i+1][1][0].add(dp[i][0][2]);
dp[i+1][1][0]=dp[i+1][1][0].add(dp[i][1][0]);
dp[i+1][0][1]=dp[i+1][0][1].add(dp[i][1][0]);

部分状态虽然不符合最后的要求,即女生不可落单,但计算过程中需要用到相应值,最后的答案为,dp[n][1][0]+dp[n][0][2]
代码:
import java.io.*;
import java.util.*;
import java.math.*;
public class Main{
	public static void main(String args[])
	{
		BigInteger dp[][][]=new BigInteger [1005][2][3];
		Scanner sc =new Scanner(new BufferedInputStream(System.in));
		PrintWriter cout=new PrintWriter(System.out);
		for(int i=0;i<=1004;i++)
			for(int j=0;j<=1;j++)
				for(int k=0;k<=2;k++)
					dp[i][j][k]=BigInteger.ZERO;
		dp[1][0][1]=BigInteger.valueOf(1);
		dp[1][1][0]=BigInteger.valueOf(1);
		for(int i=1;i<=1000;i++)
		{
			dp[i+1][0][2]=dp[i+1][0][2].add(dp[i][0][1]);
			dp[i+1][0][2]=dp[i+1][0][2].add(dp[i][0][2]);
			dp[i+1][1][0]=dp[i+1][1][0].add(dp[i][0][2]);
			dp[i+1][1][0]=dp[i+1][1][0].add(dp[i][1][0]);
			dp[i+1][0][1]=dp[i+1][0][1].add(dp[i][1][0]);
		}
		int t;
		while(sc.hasNext())
		{
           t=sc.nextInt();
		   cout.println(dp[t][0][2].add(dp[t][1][0]));
		}
		cout.flush();
	}
}




本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

全世界数以百万计的工程师和科学家都在使用 MATLAB® 分析和设计改变着我们的世界的系统和产品。基于矩阵的 MATLAB 语言是世界上表示计算数学最自然的方式。

关键字: matlab 编程 入门

单片机stm32零基础入门之--初识STM32 标准库

关键字: STM32 入门

计算机电子电路原理图,电路图讲解 电路图基础知识

关键字: 电路图 入门

周立功阅读笔记-CANopen轻松入门基于DS301(一)

关键字: canopen 入门

PSIM入门:简单实例讲解PSIM基本操作(PSIM Basic Simulation)

关键字: psim 入门 基本操作

题目链接:hdu 3062 题面: Party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav

关键字: hdu 入门

题目链接:HDU 4355 题面: Party All the Time Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536

关键字: hdu 技巧

题目链接:HDU 4544 题面: 湫湫系列故事——消灭兔子 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768

关键字: hdu 贪心

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3911 题面: Black And White Time Limit: 9000/3000 MS (

关键字: hdu 线段树

题目链接:HDU 5754 题面: Life Winner Bo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/13

关键字: hdu 博弈
关闭
关闭