http://acm.hdu.edu.cn/showproblem.php?pid=1023
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
Hint
The result will be very large, so you may not process it by 32-bit integers.
题目大意:多组数据给出n,求第n项卡特兰数。
思路:第五十多项的卡特兰数就已经爆long long了,因此要用高精度,卡特兰数的一个递推公式是:h(n)=h(n-1)*(4*n-2)/(n+1),h(0)=h(1)=1,因此我们只需要模拟高精度乘低精和高精度除低精度即可,(这种高精度计算难度相对还是比较低的)当然,卡特兰数还有另外一种递推公式:h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)*h(0) (n>=2)。用这个递推公式的话要模拟高精度乘高精度,难度也不是很大。这里用的是第一种方法。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
int a[105][105];//a[i]存储第i个卡特兰数
int len[105];//存储第i个卡特兰数的长度-1
int main()
{
a[0][0]=a[1][0]=1;
a[2][0]=2;
a[3][0]=5;
len[0]=len[1]=len[2]=len[3]=0;
for(int i=4;i<=100;i++)
{
int jw=0,j=0;
while(1)//h(n)=h(n-1)*(4*n-2)/(n+1) 模拟乘法
{
a[i][j]+=a[i-1][j]*(4*i-2)+jw;
jw=a[i][j]/10;
a[i][j]%=10;
if(!jw&&j>=len[i-1])//没有进位且h(n-1)已经计算完毕
break;
++j;
}
int temp=0;
len[i]=j;
for(;j>=0;j--)//模拟除法
{
temp=temp*10+a[i][j];
a[i][j]=temp/(i+1);
temp%=i+1;
}
while(a[i][len[i]]==0)
--len[i];
}
int n;
while(~scanf("%d",&n))
{
for(int i=len[n];i>=0;i--)
putchar(a[n][i]+48);
putchar('\n');
}
return 0;
}