Train Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9701 Accepted Submission(s): 5210
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
Recommend
We have carefully selected several similar problems for you: 1133 1022 1131 1134 2067
题意:给你n辆火车,编号从1到n,问你有多少种出站的可能序列。
思路:火车进出站问题,首先考虑卡特兰数(不要忘了高精哦)
代码:
#include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define N 110 using namespace std; int n,len,sum,tmp,a[N][N],b[N]; int catelan() { len=1;a[1][0]=b[1]=1; for(int i=2;i<110;i++) { for(int j=0;j<len;j++) a[i][j]=a[i-1][j]*(4*i-2); sum=0; for(int j=0;j<len;j++) { tmp=sum+a[i][j]; a[i][j]=tmp%10; sum=tmp/10; } while(sum) { a[i][len++]=sum%10; sum/=10; } for(int j=len-1;j>=0;j--) { tmp=sum*10+a[i][j]; a[i][j]=tmp/(i+1); sum=tmp%(i+1); } while(!a[i][len-1]) len--; b[i]=len; } } int main() { catelan(); while(~scanf("%d",&n)) { for(int i=b[n]-1;i>=0;i--) printf("%d",a[n][i]); printf("\n"); } }