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
分析:其实分解过程可以得知,当前的数目和前面的状态是有关系的。推推就出来了。举个例子。
现在就可看出,4个pill的数目和3个pill的状态是有关系的;
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
using namespace std;
long long a[35][35];
int n;
void init(){
a[4][1]=a[4][0]=14;
a[4][2]=9,a[4][3]=4;
for(int i=5;i<=30;i++){
for(int j=i-1;j>=0;j--){
if(j==i-1) a[i][j]=a[i-1][j-1]+1;
else a[i][j]=a[i][j+1]+a[i-1][j-1];
}
}
}
int main(){
int n;
init();
while(cin>>n,n){
if(n==1){
cout<<"1"<<endl;
continue;
}
else if(n==2){
cout<<"2"<<endl;
continue;
}
else if(n==3){
cout<<"5"<<endl;
continue;
}
else if(n==4){
cout<<"14"<<endl;
continue;
}
else
cout<<a[n][0]<<endl;
}
return 0;
}