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

 

 

          Pills

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 569    Accepted Submission(s): 373


Problem Description
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
6
1
4
2
3
30
0
 

 

Sample Output
132
1
14
2
5
3814986502092304
 

 

Source
 

 

Recommend
lcy
 
 
 
题意:一个瓶子里有n片药,每次吃半片,从瓶子里可能取出整片,也可能取出半片,如果取到的药是整片的,就把它分成两半,吃掉其中的一半,另一半重新放入瓶中,如果取到半粒药,则直接吃掉。问2n天内吃完药有多少种取法。
 
分析:第一次一定取出的是整片,最后一次取出来的一定是半片,只有先取出整片,才有取出半片的可能。n片药,一定是2n次取完的,这2n次一定是取n次整片,n次半片,题中说取出整片记W,半片记H,其实就是n个W和n个H的组合。
每种取的状态都与它前面取的的状态有关系,暗示用DP来解。
 
第一种解法:DP 
#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;
}

 

 

这道题也可以用记忆化搜索来解。
建立一个二维状态。 第一个状态:完整药片的数量。第二个状态:半片药片的数量。
f(i ,j )=f(i-1,j+1 )+f(i ,j-1) ;
当吃的是整片时:i-1 ,半片的增加一个:j+1
当吃的是半片时:i 不变,j-1

 

 第二种解法:记忆化搜索

#include<iostream>
using namespace std;
__int64 f[32][32];//f[i][j]=剩下i片整药和j片半药时的序列数
__int64 fun(int x,int y)
{   
    __int64 ans=0;   
    if(f[x][y])   
        return f[x][y];  
    ans+=fun(x-1,y+1);  
    if(y)    
        ans+=fun(x,y-1); 
    return f[x][y]=ans;  
  }
int main()
{    
    int i,n;   
    memset(f,0,sizeof(f));
    for(i=0;i<=30;i++)  
        f[1][i]=i+1;    
    while(scanf("%d",&n)&&n)    
    {       
        printf("%I64d\n",fun(n,0)); 
   } 
    return 0;
}

 

其实这道题是完全的卡塔兰数。关于卡塔兰数详细可以看维基百科

第三种解法:卡塔兰数

 

(摘自)http://blog.csdn.net/jtlyuan/article/details/7440591

 

卡特兰数:规定C0=1,而C1=1,C2=2,C3=5,C4=14,C5=42,C6=132,C7=429,C8=1430,C9=4862,C10=16796,

C11=58786,C12=208012,C13=742900,C14=2674440,C15=9694845·········································

卡塔兰数的一般项公式为 另类递归式: h(n)=((4*n-2)/(n+1))*h(n-1);

Cn的另一个表达形式为

 

h(n)= h(0)*h(n-1) + h(1)*h(n-2) + ... + h(n-1)h(0) (其中n>=2)

 

hai可以这样推导出来:

n

推到过程

Cn

1

1

1

2

1 1

2

3

1 2 2

5

4

1 3 5 5

14

5

1 4 9 14 14

42

6

1 5 14 28 42 42

132

7

1 6 20 48 90 132 132

429

···

··· ···

···

 

所以,在做题的时候,我们应该用上面的公式Cn=Ck*Cn-k (k=1,2``n)来判断是否使用于katalan数来解决问题,合适就列出前几项来判断推到出答案

 

总结了一下,最典型的四类应用:(实质上却都一样,无非是递归等式的应用,就看你能不能分解问题写出递归式了)

1.括号化问题

矩阵链乘: P=a1×a2×a3×……×an,依据乘法结合律,不改变其顺序,只用括号表示成对的乘积,试问有几种括号化的方案?(h(n)种)

2.出栈次序问题

一个栈(无穷大)的进栈序列为1,2,3,..n,有多少个不同的出栈序列?

类似:有2n个人排成一行进入剧场。入场费5元。其中只有n个人有一张5元钞票,另外n人只有10元钞票,剧院无其它钞票,问有多少中方法使得只要有10元的人买票,售票处就有5元的钞票找零?(将持5元者到达视作将5元入栈,持10元者到达视作使栈中某5元出栈)

3.将多边行划分为三角形问题

将一个凸N+2多边形区域分成三角形区域的方法数?

类似:一位大城市的律师在她住所以北n个街区和以东n个街区处工作。每天她走2n个街区去上班。如果她

从不穿越(但可以碰到)从家到办公室的对角线,那么有多少条可能的道路?

类似:在圆上选择2n个点,将这些点成对连接起来使得所得到的n条线段不相交的方法数?

4.给顶节点组成二叉树的问题

给定N个节点,能构成多少种不同的二叉树?

(能构成h(N)个)

Catalan数的解法

Catalan数的组合公式为 Cn=C(2n,n) / (n+1);

此数的递归公式为 h(n ) = h(n-1)*(4*n-2) / (n+1)

卡特兰数真是一个神奇的数字,很多组合问题的数量都和它有关系,例如:

Cn= n对括号正确匹配组成的字符串数,例如 3对括号能够组成:

((())) ()(()) ()()() (())() (()())

Cn= n+1个数相乘,所有的括号方案数。例如, 4个数相乘的括号方案为:

((ab)c)d (a(bc))d (ab)(cd) a((bc)d) a(b(cd))

Cn= 拥有 n+1 个叶子节点的二叉树的数量。例如 4个叶子节点的所有二叉树形态:

 

  • Cn=n*n的方格地图中,从一个角到另外一个角,不跨越对角线的路径数,例如, 4×4方格地图中的路径有:

 

  • Cn= n+2条边的多边形,能被分割成三角形的方案数,例如 6边型的分割方案有:

 

  • Cn= 圆桌周围有 2n个人,他们两两握手,但没有交叉的方案数。

 

下面是一些大公司的笔试题

先来一道阿里巴巴的笔试题目:说16个人按顺序去买烧饼,其中8个人每人身上只有一张5块钱,另外8个人每人身上只有一张10块钱。烧饼5块一个,开始时烧饼店老板身上没有钱。16个顾客互相不通气,每人只买一个。问这16个人共有多少种排列方法能避免找不开钱的情况出现。

C8=1430,所以总数=1430*8!*8!

2012腾讯实习招聘笔试题

在图书馆一共6个人在排队,3个还《面试宝典》一书,3个在借《面试宝典》一书,图书馆此时没有了面试宝典了,求他们排队的总数?

C3=5;所以总数为5*3!*3!=180.

 

转载于:https://www.cnblogs.com/Su-Blog/archive/2012/08/22/2651504.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值