Pills
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 746 Accepted Submission(s):
490
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?
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
题意:一个瓶子里有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 ",dp[n][n]); } return 0; }