这是一道简单的卡特兰数题,卡特兰序列:1,1,2,5,14,42,132,429,1430·············
递推式f(n)=f(n-1)*(4n-2)/ (n+1) , 然后直接套用大数的模板
代码如下:
#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
#include<math.h>
#define eps 1e-9
#define N 105
#define pi acos(-1.0)
#define P system("pause")
using namespace std;
string s[N];
string add(string s1,string s2)
{
if(s1.length()<s2.length())
{
string temp=s1;
s1=s2;
s2=temp;
}
int i,j;
for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--)
{
s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0)); //注意细节
if(s1[i]-'0'>=10)
{
s1[i]=char((s1[i]-'0')%10+'0');
if(i) s1[i-1]++;
else s1='1'+s1;
}
}
return s1;
}
string mult(string a,string b)//大数乘法
{
int flag=0,i,j,k,p,q,t,max;
char ch;
string c,ans;
p=a.size()-1;q=b.size()-1;
ans="0";
for(i=p;i>=0;i--)//可以分解为p个一位数和一个大数的乘法
{
flag=0;c="";
for(j=i;j<p;j++) c+='0';
for(j=q;j>=0;j--)
{
t=(b[j]-'0')*(a[i]-'0')+flag;
flag=t/10;
c+=(t%10+'0');
}
if(flag) c+=(flag+'0');
for(j=0,k=c.size()-1;j<k;j++,k--)
{
ch=c[j];c[j]=c[k];c[k]=ch;
}
ans=add(ans,c);
}
return ans;
}
string div(string src,int n) //大数除法 src/n = dest
{
string dest="";
int len = src.length(),i,k,t = 0 , s = 0;
bool flag = true;
for(i=0,k=0; i<len; i++)
{
t = s*10+(src[i]-48);
if(t/n>0 || t==0)
dest += (t/n+48),s = t%n,flag = false;
else
{
s = t;
if(!flag)
dest += '0';
}
}
return dest;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
s[1]="1";
for(int i=2;i<101;i++)
{
char s1[10000];
sprintf(s1,"%d",4*i-2);
s[i] = mult(s[i-1],s1);
s[i] = div(s[i],i+1);
}
int n;
while(scanf("%d",&n))
{
if(n==-1) break;
cout<<s[n]<<endl;
}
// P;
return 0;
}