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 10Sample Output
1
2
5
16796
HintThe result will be very large, so you may not process it by 32-bit integers.
第几个卡提兰数
代码:
#include<stdio.h>
int a[110][100]; //a[n][] 前一个括号是第几个卡特兰数, 后一个括号是表示的卡特兰数的每一位数;
int b[110]; // b[n] 第n个卡特兰数的长度;
//卡特兰数的 递推式为 h[n]=h[0]*h[n-1]+h[1]*h[n-2]+.....+h[n-1]*h[0];
// 另类递推式 h[n]=h[n-1]*(4*n-2)/(n+1);
void ktl() // 因最大的卡特兰数是58位,longlong也存不下,所以要分解;
{
int i,j,k,t,len;
a[1][0]=b[1]=1;
len=1;
for(i=2;i<=100;i++)
{
for(j=0;j<len;j++) // 乘法,n-1个的卡特兰数的每一位乘以(4*n-2)
a[i][j]=a[i-1][j]*(4*(i-1)+2);
k=0;
for(j=0;j<len;j++) //得第n个卡特兰数的每一位;
{
a[i][j]=k+a[i][j];
t=a[i][j];
a[i][j]=t%10;
k=t/10;
}
while(k)
{
a[i][len++]=k%10; // 进位处理
k=k/10;
}
k=0;
for(j=len-1;j>=0;j--) //模拟除法
{
t=(k*10+a[i][j]);
a[i][j]=t/(i+1);
k=t%(i+1);
}
while(a[i][len-1]==0) // 高位零处理
len--;
b[i]=len;
}
}
int main()
{
int i,j,k,t,n;
ktl();
while(~scanf("%d",&n))
{
for(i=b[n]-1;i>=0;i--)
printf("%d",a[n][i]);
printf("\n");
}
return 0;
}
4万+



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



