hdu 1297 Children’s Queue(java+简单dp)
来源:互联网 发布:西门子消防主机软件 编辑:程序博客网 时间:2024/04/27 19:32
Children’s Queue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11519 Accepted Submission(s): 3746
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?
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
123
Sample Output
124
Author
SmallBeer (CML)
题目分析:
这道题最终结果会超long long ,所以我选择了java
状态定义为dp[i][j][k],i标记当前状态是否合法,j标记最后一位是男还是女,k标记当前位;
之所以定义非法状态,是因为当前位选择女性导致的非法,可能在后一位被纠正为合法
具体转移过程见代码,其实如果java中全部定义为static,和c++基本上在编程上是没区别的
这道题最终结果会超long long ,所以我选择了java
状态定义为dp[i][j][k],i标记当前状态是否合法,j标记最后一位是男还是女,k标记当前位;
之所以定义非法状态,是因为当前位选择女性导致的非法,可能在后一位被纠正为合法
具体转移过程见代码,其实如果java中全部定义为static,和c++基本上在编程上是没区别的
package hdu;import java.util.Scanner;import java.math.BigInteger;public class Main {static BigInteger [][][]dp = new BigInteger[3][3][1007];static void memset ( ){for ( int i = 0 ; i < 3 ; i++ )for ( int j = 0 ; j < 3 ; j++ )for ( int k = 0 ; k < 1007 ; k++ )dp[i][j][k] = BigInteger.ZERO;}static void init ( ){ memset (); dp[1][1][1] = dp[0][2][1] = BigInteger.ONE; for ( int i = 2 ; i <= 1000 ; i++ ) { dp[1][1][i] = dp[1][2][i-1].add(dp[1][1][i-1]); dp[1][2][i] = dp[1][2][i-1].add(dp[0][2][i-1]); dp[0][2][i] = dp[1][1][i-1]; }}public static void main ( String args[] ){Scanner cin = new Scanner ( System.in );init();while ( cin.hasNext() ){int n = cin.nextInt();System.out.println ( dp[1][1][n].add(dp[1][2][n]) );}}}
0 0
- Children’s Queue - HDU 1297 简单DP
- hdu 1297 Children’s Queue(java+简单dp)
- hdu 1297 children's queue #DP#大数
- HDU 1297 Children’s Queue (DP)
- HDU 1297 Children’s Queue JAVA *
- hdu 1297 Children’s Queue
- 【HDU 1297】Children’s Queue
- HDU 1297 - Children’s Queue
- HDU 1297 Children’s Queue
- HDU 1297 Children’s Queue
- hdu 1297 Children’s Queue
- HDU 1297 Children’s Queue
- HDU 1297 Children’s Queue
- hdu 1297 Children’s Queue
- hdu - 1297 Children’s Queue
- hdu 1297 Children’s Queue
- hdu 1297 Children’s Queue
- HDU 1297 Children’s Queue
- ImportError: No module named 的一段折腾
- 用html语言写一个个人信息表(练习使用html标签)
- struts2进阶【5】——结果类型
- [LeetCode] Valid Number
- 我为什么写博客?
- hdu 1297 Children’s Queue(java+简单dp)
- 物联网1122班20号林金燕RFID第二次作业1
- spark:Idea编译程序打jar包部署到spark运行方法--19
- Subsets
- 家政O2O市场浅析:Homejoy模式 与Uber相通(转)
- android apk 防止反编译技术第一篇-加壳技术
- ++操作符的使用1
- C#--基于delegate实现不同功能的排序
- Aop概念详解