HDOJ/HDU 1297 Children’s Queue(推导~大数)
2016-05-21 21:38
337 查看
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
;
由前面的推导出db
。
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
=db[n-1] + db[n-2] + db[n-4];
db[i] 中保存的都是合法序列数。
Java大数秒A~~~
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
;
由前面的推导出db
。
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
=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 ); } } 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]); } } }
相关文章推荐
- HDOJ/HDU 1297 Children’s Queue(推导~大数)
- iOS开发中 UITableViewCell的几种样式
- Java中String、StringBuffer和StringBuilder的区别
- STL 简单 queue 的实现
- String , StringBuffer, StringBuilder的区别
- mysql 中插入中文出现的 Incorrect string value: '\xE5\xA5\xBD\xE5\xAE\xB6...' from column错误
- XMG 核心动画与UIView动画的区别
- 如何将dataframe转换为rdd类型
- ios开发学习笔记--调用相册或相机(UIImagePickerController)
- LeetCode Everyday: 347. Top K Frequent Elements
- 动态规划——distinct-subsequences t在s中出现的次数
- 微软扫盲贴:Windows 和 GPT 常见问题解答(uefi、GPT、ESP、MSR之类的概念)
- PHP Study- Issue 1 -Redirect Unable to load the module
- 8种删除UICollectionViewCell的动画
- Codeforces Round #316 (Div. 2) D. Tree Requests
- break,continue,与标号
- U3d屏幕自适应
- 02.手把手教你 .Net EasyUI DataGrid(数据表格排序)
- MFC中应用WM_ENDSESSION,WM_QUERYENDSESSION消息截获系统的关机/注销
- POJ-2031-Building a Space Station-(最小生成树 普利姆)