Problem Description This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, ... , 2n - 1, 2n consecutively in clockwise order on the ground to form a circle, and then, to draw some straight line segments to connect them into number pairs. Every number must be connected to exactly one another. And, no two segments are allowed to intersect.
|
Input Each line of the input file will be a single positive number n, except the last line, which is a number -1. You may assume that 1 <= n <= 100.
|
Output For each n, print in a single line the number of ways to connect the 2n numbers into pairs.
|
Sample Input 23-1
|
Sample Output 25 |
直接用师兄的解题报告O(∩_∩)O~:
![](https://img-my.csdn.net/uploads/201303/15/1363312980_4064.jpg)
#include<iostream>
using namespace std;
#define mod 10000
int catalan[101][250]={0};
int b[250]={0};
void bignum_mul(int a[],int l,int b[])
{
int i;
for(i=0;i<250;i++)
{
b[i]=b[i]+a[i]*l;
if(b[i]>=mod)
{
b[i+1]=b[i]/mod;
b[i]=b[i]%mod;
}
}
}
void bignum_div(int a[],int l,int b[])
{
int i,t=0;
for(i=249;i>=0;i--)
if(a[i]>0)
break;
for(i;i>=0;i--)
{
t=t*mod+a[i];
b[i]=t/l;
t=t%l;
}
}
void main()
{
int i,j,k,l,n;
catalan[1][0]=1;
for(i=2;i<=100;i++)
{
memset(b,0,sizeof(b));
l=4*i-2;
k=i+1;
bignum_mul(catalan[i-1],l,b);
bignum_div(b,k,catalan[i]);
}
while(cin>>n && n!=-1)
{
for(i=249;i>=0;i--)
if(catalan[n][i]>0)
break;
printf("%d",catalan[n][i]);
for(j=i-1;j>=0;j--)
printf("%04d",catalan[n][j]);
cout<<endl;
}
}