HDU 1023 Train Problem II 卡特兰数 大数的乘法除法

时间:2023-02-16 19:57:01


Train Problem II


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5622    Accepted Submission(s): 3040

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



/*
1023 Train Problem II
不知道思路 看别人的都说是卡特兰数
1, 1, 2, 5, 14, 42, 132, 429, 1430,
4862, 16796, 58786, 208012, 742900,
2674440, 9694845, 35357670, 129644790,
477638700, 1767263190, 6564120420, 24466267020,
91482563640, 343059613650, 1289904147324, 4861946401452...
F(n)=F(n-1)*(4n-2)/(n+1)
*/

#include<iostream>
using namespace std;
int a[101][101]={0};//a[i]表示的是第i个数
//因为是大数 [j]表示的是这个数的若干位
int main(){
int b[101],i,j,n,k,z;

a[1][0]=1;
b[1]=1;
k=1;//长度
for(i=2;i<101;i++)
{
for(j=0;j<k;j++)
a[i][j]=a[i-1][j]*(4*i-2);//乘法大数 每位乘以
z=0;//进位
for(j=0;j<k;j++)
{
a[i][j]+=z;
z=a[i][j]/10;
a[i][j]%=10;
}
while(z)//仍有进位
{
a[i][k++]=z%10;
z/=10;
}

//大数除法 模拟除法 从高到低
z=0;
for(j=k-1;j>=0;j--)
{
a[i][j]+=z*10;//上一位剩的
z=a[i][j]%(i+1);
a[i][j]/=(i+1);
}
while(!a[i][k-1])//去除前面的0
k--;
b[i]=k; //保存n的大数的长度
}

while(cin>>n)
{
for(i=b[n]-1;i>=0;i--)
cout<<a[n][i];
cout<<endl;
}
return 0;
}