POJ 2084 标准卡特兰数 大数解决

来源:互联网 发布:简单的宏程序编程 编辑:程序博客网 时间:2024/06/04 19:30
Game of Connections
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 6331 Accepted: 3263

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.
It's still a simple game, isn't it? But after you've written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right?

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


输入n直接输出n的卡特兰数即可

#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define base 10000//数组一个单位存的是10000进制#define maxx 100//数组长度void multiply(int a[],int max,int b){    int i,array=0;    for(i=max-1;i>=0;i--)    {        array+=b*a[i];        a[i]=array%base;        array/=base;    }//for(i=0;i<max;i++)printf("%d",a[i]);//printf("\n");}void divide(int a[],int max,int b){    int i,div=0;    for(i=0;i<max;i++)    {        div=div*base+a[i];//如果前面div不为0则加上div*base 如果为0 不借位就算了 假如a[i-1]=0000,a[i]=0006  b=3,这时前面div为0 不借位就算了得到2        a[i]=div/b;        div%=b;    }//for(i=0;i<max;i++)printf("%d",a[i]);//printf("\n");}int main(){    int n,i;    int a[101][maxx];    memset(a[1],0,maxx*sizeof(int));//初始化a[1]为0    for(i=2,a[1][maxx-1]=1;i<101;i++)    {        memcpy(a[i],a[i-1],maxx*sizeof(int));//把a[i-1]复制进a[i]         multiply(a[i],maxx,4*i-2);        divide(a[i],maxx,i+1);    }    while(scanf("%d",&n)&&n!=-1)    {        for(i=0;i<maxx&&a[n][i]==0;i++);        printf("%d",a[n][i++]);        for(;i<maxx;i++)        {            printf("%04d",a[n][i]);        }        printf("\n");    }    return 0;}

我自己做的    模仿者来的

#include<stdio.h>#include<string.h>const __int64 base=1000000000;const int lenth=100;void mul(__int64 a[],int len,int b){int i;__int64 jw=0;for(i=len-1;i>=0;i--){jw=jw+a[i]*b;a[i]=jw%base;jw=jw/base;}}void div(__int64 a[],int len,int b){int i;__int64 jw=0;for(i=0;i<lenth;i++){jw=jw*base+a[i];a[i]=jw/b;jw=jw%b;}}int main(){      int i,j,n;  __int64 a[101][100];  memset(a[1],0,sizeof(a[1]));  a[1][lenth-1]=1;//a[1]初始化为1  for(i=2;i<=100;i++)  {          memcpy(a[i],a[i-1],sizeof(a[1]));  mul(a[i],lenth,4*i-2);  div(a[i],lenth,i+1);  }  while(scanf("%d",&n)!=EOF)  {  if(n==-1)  break;  for(i=0;i<lenth&&a[n][i]==0;i++);  printf("%I64d",a[n][i++]);  for(;i<lenth;i++) printf("%0*I64d",9,a[n][i]);//100000000  9位  所以是9  printf("\n");    }     return 0;}