杭電1023 Train problemII(カタラン大数)

3461 ワード

Train Problem II
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5923    Accepted Submission(s): 3219
Problem Description
As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway.
 
Input
The input contains several test cases. Each test cases consists of a number N(1<=N<=100). The input is terminated by the end of file.
 
Output
For each test case, you should output how many ways that all the trains can get out of the railway.
 
Sample Input
 
   
1 2 3 10
 

Sample Output
 
   
1 2 5 16796
Hint
The result will be very large, so you may not process it by 32-bit integers.
 

Author
Ignatius.L
/*
Time:2014-11-28 20:30   
*/
//     
//f[n]=f[n-1]*(4*n-2)/(n+1) 1,2,5,14
#include
#include
#include
using namespace std;
const int MAX=1000;
int f[105][MAX];
int digit[MAX];
void Catalan(){
	memset(f,0,sizeof(f));
	f[0][0]=1;f[1][0]=1;
	digit[0]=digit[1]=0;
	int i,j,t;
	for(i=2;i<=100;i++){
		int t=0;digit[i]=digit[i-1];
		for(j=0;j<=digit[i];j++){
			f[i][j]=f[i-1][j]*(4*i-2)+t;
			t=f[i][j]/10;
			f[i][j]%=10;
			if(t&&j+2>=digit[i]){
				digit[i]++;
			}
		}/*
		for(j=digit[i];j>=0;j--){
			printf("%d",f[i][j]);
		}puts("");*/
		t=0;
		for(j=digit[i];j>=0;j--){
			f[i][j]=f[i][j]+t*10;
			t=f[i][j]%(i+1);
			f[i][j]/=(i+1);
		}
		while(f[i][digit[i]]==0)digit[i]--;
	}
} 
int main(){
	int i,j,n;
	Catalan();
	while(scanf("%d",&n)!=EOF){
		for(i=digit[n];i>=0;i--){
			printf("%d",f[n][i]);
		}puts("");
		//printf("%d
",digit[n]); } return 0; }
/*
      ,     ,           ,       ,        ,    ,  64  wa              !!!
Time:2014-10-3 17:54
*/
#include//      f[n]=f[n-1]*(4n-2)/(n+1) 
#include
#define MAX 220
int a[MAX][MAX],b[MAX];//b            
void Init(){
	a[1][0]=1;b[1]=0;
	int i,j,k=1;//k    
	int z; 
	for(i=2;i<102;i++){
		for(j=0;j=0;j--){
		 	a[i][j]=a[i][j]+z*10;
		 	z=a[i][j]%(i+1);
		 	a[i][j]/=(i+1);
		 }
		 while(a[i][k-1]==0)k--;//  : a[][k-1]   k
		 b[i]=k-1; 
	} 
}
int main(){
	int N;
	Init();
	while(scanf("%d",&N)!=EOF){
		for(int i=b[N];i>=0;i--)
		printf("%d",a[N][i]);
		puts("");
	}
return 0;
}