卡特兰数(Train Problem II HDU - 1023 )

本文介绍了一个火车调度问题,并通过计算卡特兰数解决该问题。具体实现使用了特殊的存储方式来处理非常大的卡特兰数,避免了传统整型数据溢出的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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<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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值