hdu Train Problem II

Train Problem II

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 0   Accepted Submission(s) : 0
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
 

Statistic | Submit | Back
#include <iostream> 
using namespace std;
int a[101][101]= {0};  //大數卡特蘭數  
int main()
{	
    int i,j,n,k,b[101],len;
    a[1][0] = 1;	//n輛車,對應第一維,a存的值爲方案數
    b[1] = len = 1;    //卡特蘭數的長度   

    for(i = 2;i < 101;i++)		
    {		
		   //大數乘法 處理 		
		for(j = 0;j < len;j++)			
			a[i][j] = (4*i-2) * a[i-1][j];  //組合公式,一維爲控制數組的遞推
		for(k = j = 0;j < len;j++) 			
		{			
			a[i][j] += k;			
			k = a[i][j] / 10;    //k對應進階值			
			a[i][j] %= 10;			
		}		
		while (k)			//最後一位,控制進階
		{
			a[i][len++] = k%10;			
			k /= 10;		
		}
		//大數除法處理 		
		for(j = len-1,k = 0;j >= 0;j--) 	//處理相乘結果   
		
		{			
			a[i][j] += k*10;			
			k = a[i][j] % (i+1);	//遞推公式		
			a[i][j] /= (i+1);			
		}	
		while(!a[i][len-1])        //高位零處理   

		len--;		
		b[i] = len;        		
    }	
    while(cin >> n)		
    {		
		for(i = b[n] - 1;i >= 0;i--)			
			cout << a[n][i];		
		cout << endl; 		
    }	
    return 0;	
}


發佈了70 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章