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.
分析:
题意:
一列长度单调递增的火车按顺序进入火车站,问出火车站可能的顺序有多少种?
上网查了一下,是卡塔兰数,卡塔兰数是什么?我也讲不清楚,各自百度好吧!记住公式就好:h(i)=h(i-1)(4i-2)/(i+1);注意了,这里数据很大,要用到高精度算法,但是(4i-2)/(i+1)可能算给出来是小数,所以我们要把这个公式拆开来算!先算(4i-2)*h(i-1),然后再做除的操作!
代码:
#include<iostream>
#include<cstdio>
#define N 105
using namespace std;
int dp[N][N];
int main()
{
int n;
dp[1][0]=dp[1][1]=1;
for(int i=2;i<=100;i++)
{
int len=dp[i-1][0],v=0;
int k=(4*i-2);
for(int j=1;j<=len;j++)
{
dp[i][j]=k*dp[i-1][j]+v;
v=dp[i][j]/10;
dp[i][j]%=10;
}
while(v)
{
dp[i][++len]=v%10;
v/=10;
}
for(int j=len;j>0;j--)
{
v=v*10+dp[i][j];
dp[i][j]=v/(i+1);
v%=(i+1);
}
while(!dp[i][len])
{
len--;
}
dp[i][0]=len;
}
while(~scanf("%d",&n))
{
for(int i=dp[n][0];i>0;i--)
{
printf("%d",dp[n][i]);
}
printf("\n");
}
return 0;
}