Pills
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 764 Accepted Submission(s): 500
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
Recommend
本题给定n片药,每次可任意取,取到整片的分开,拿走半片放回半片;取到半片的直接拿走。问一共有多少方案取完。
题目给出字符串提示,正片和半片有多少搭配。划分左右,只有取到完整的,才会出现非完整的。即区间左边的正片数不少于左边的半片数。卡特兰数模型。
import java.math.BigInteger;
import java.util.Scanner;
public class hdu4165 {
static BigInteger []f=new BigInteger[110];
static void catlan()
{
f[0]=new BigInteger("1");
f[1]=new BigInteger("1");
for(int i=2;i<31;i++)
{
f[i]=new BigInteger("0");
for(int j=0;j<i;j++)
{
f[i]=f[i].add( f[j].multiply(f[i-j-1]) );
}
}
}
public static void main(String[] str)
{
Scanner input=new Scanner(System.in);
catlan();
int i;
while(input.hasNext())
{
i=input.nextInt();
if(0==i)break;
System.out.println(f[i]);
}
}
}