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.
InputThe 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.
OutputFor 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.
#include<iostream>
using namespace std;
int main()
{
int n,i,j,len,r,temp,t;
int a[101][101]={0},b[101];
len=1;
a[1][0]=1;
b[1]=1;
for(i=2;i<=100;i++)
{
t=i-1;
for(j=0;j<len;j++)
a[i][j]=a[i-1][j]*(4*t+2);
for(r=j=0;j<len;j++)
{
temp=a[i][j]+r;
a[i][j]=temp%10;
r=temp/10;
}
while(r)
{
a[i][len++]=r%10;
r/=10;
}
for(j=len-1,r=0;j>=0;j--)
{
temp=r*10+a[i][j];
a[i][j] = temp/(t+2);
r=temp%(t+2);
}
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;
}
题解:还记得,卡特兰数的递推公式是:h(n ) = h(n-1)*(4*n-2) / (n+1);组合公式是:Cn=C(2n,n) / (n+1) 吗?
废话!当然不记得!
本文探讨了一个火车调度问题,即所有火车能否按严格递增顺序进出站,并求解可能的排列总数。通过使用卡特兰数的递推公式进行计算,给出了一个高效的算法实现。
426

被折叠的 条评论
为什么被折叠?



