HDU 4165 Pills (DP卡特兰数列)
2016-05-07 17:18
363 查看
Pills
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1297 Accepted Submission(s): 902
[align=left]Problem Description[/align]
Aunt Lizzie takes half a pill of a certain medicine every day. She starts with a bottle that contains N pills.
On the first day, she removes a random pill, breaks it in two halves, takes one half and puts the other half back into the bottle.
On subsequent days, she removes a random piece (which can be either a whole pill or half a pill) from the bottle. If it is half a pill, she takes it. If it is a whole pill, she takes one half and puts the other half back into the bottle.
In how many ways can she empty the bottle? We represent the sequence of pills removed from the bottle in the course of 2N days as a string, where the i-th character is W if a whole pill was chosen on the i-th day, and H if a half pill was chosen (0 <= i < 2N).
How many different valid strings are there that empty the bottle?
[align=left]Input[/align]
The input will contain data for at most 1000 problem instances. For each problem instance there will be one line of input: a positive integer N <= 30, the number of pills initially in the bottle. End of input will be indicated by
0.
[align=left]Output[/align]
For each problem instance, the output will be a single number, displayed at the beginning of a new line. It will be the number of different ways the bottle can be emptied.
[align=left]Sample Input[/align]
6 1 4 2 3 30 0
[align=left]Sample Output[/align]
132 1 14 2 5 3814986502092304
[align=left]Source[/align]
[align=left]大体题意:[/align]
[align=left]一开始瓶子里有n 个完整药片,每次只能吃半个药片,若遇到完整药片,则会吃掉半个,在把剩下半个药片放回瓶子里,问吃药片的方法数![/align]
[align=left]思路:[/align]
[align=left]仔细想想可以把问题转换一下,最后肯定2n长度的序列,肯定是n个完整药片和n个半个药片。[/align]
[align=left]并且当前半个药片的数量不超过 前面完整药片的数量![/align]
[align=left]所以:[/align]
[align=left]可以转换成卡特兰数列![/align]
[align=left]状态转移:[/align]
[align=left]dp[i][j] = dp[i-1][j] + dp[i][j-1];[/align]
[align=left]排列数量中来源,i-1完整,j个不完整,和 i个完整,j-1个不完整![/align]
最后dp
就是答案!
[align=left]注意要打表啊,不然会超时![/align]
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 100 + 10; typedef long long ll; ll dp[maxn][maxn]; void init(){ for (int i = 0; i <= 30; ++i)dp[i][0] = 1; for (int i = 1; i <= 30; ++i) for (int j = 1; j <= i; ++j) dp[i][j] = dp[i-1][j] + dp[i][j-1]; } int main(){ int n; init(); while(scanf("%d",&n) == 1 && n){ printf("%I64d\n",dp ); } return 0; }
相关文章推荐
- 几进制的转换
- html5新增加的表单元素-output
- HTML5网页打开摄像头,并拍照
- Bomb第三方支付
- 二叉数查找指定结点
- 一起学Netty(七)之 TCP粘包拆包基本解决方案
- 接口测试关注点、常用工具
- UVA, 820 Internet Bandwidth
- APP中缓存、加载与刷新机制设计
- html5新增加的表单元素-Keygen
- 2138的16进制简单的
- ContentProvider使用拾遗
- 十进制换成十六进制
- 一起学Netty(六)之 TCP粘包拆包场景
- 练习三 1003
- 一起学Netty(五)之 初识ByteBuf和ByteBuf的常用API
- MWC-MWC2.3代码RC数据接收代码理解
- 如何在github上展示作品——为你的项目生成一个快速访问的网址如(DaisyWang88.github.io)
- 韩信点兵(hanxin)
- 实验1--基于框架的应用系统开发框架选择原因及其说明