HDU 1023 Train Problem II【catalan数】

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

分析:

卡特兰数递推式 f[n]=(4n-2)*f[n-1]/(n+1)

本题需要处理大数乘小数,大数除以小数,乘法和除法的处理方向不同

code:

View Code
#include<stdio.h>
int main()
{
int i,j,n,t,c,k,flag;
int r[60];
while(scanf("%d",&n)!=EOF)
{
flag=0;
for(i=1;i<60;i++)
r[i]=0;
r[0]=j=1;
for(i=2;i<=n;i++)
{
for(k=0;k<j;k++)
r[k]*=(4*i-2);
for(k=c=0;k<j;k++)
{
t=r[k]+c;
r[k]=t%10;
c=t/10;
}
while(c)
{
r[j]=c%10;
c/=10;
j++;
}
for(k=j-1,c=0;k>=0;k--)
{
t=c*10+r[k];
r[k]=t/(i+1);
c=t%(i+1);
}
while(!r[j-1])
j--;
}
for(i=59;i>=0;i--)
{
if(flag||r[i])
{
flag=1;
printf("%d",r[i]);
}
}
printf("\n");
}


return 0;
}



posted @ 2012-03-16 07:28  'wind  阅读(156)  评论(0编辑  收藏  举报