*寒假水112——卡特兰数

本文探讨了一个火车调度问题,即所有火车能否按严格递增顺序进出站,并求解可能的排列总数。通过使用卡特兰数的递推公式进行计算,给出了一个高效的算法实现。

摘要生成于 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<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) 吗?

废话!当然不记得!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值