HDU 4165 Pills(DP/记忆化搜索/卡塔兰数)

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?
Input
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’.
Output
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.
Sample Input

30

 0
Sample Output
132 

14 

3814986502092304

点击打开链接

补个知识点: 卡特兰数

思路:参考大神博客,以为有普通的规律呢,推了好久最后看题解才发现,原来并不是啊;

转自:大神博客

#include<iostream>
using namespace std;
__int64 dp[32][32];
int main()
{
    int n;
    int i,j;
    memset(dp,0,sizeof(dp));
    for(i=0;i<=30;i++)
        dp[i][0]=1;
    for(i=1;i<=30;i++)
        for(j=1;j<=i;j++)
            dp[i][j]=dp[i][j-1]+dp[i-1][j];
        while(cin>>n,n)
        {
            printf("%I64d\n",dp[n][n]);
        }
        return 0;
}


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值