读书人

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

发布时间: 2012-10-13 11:38:17 作者: rapoo

POJ 2084 标准卡特兰数 大数解决
Game of ConnectionsThis 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?

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.

For each n, print in a single line the number of ways to connect the 2n numbers into pairs.

#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;}



读书人网 >编程

热点推荐