Game of Connections(POJ-2084)

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. 
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

2
3
-1 

Sample Output

2
5

题意:给出一个整数 n,现在 2n 个人围成一圈,两两相连,要求连接的线不能相交,求共有多少种连接方式

思路:能看出来是卡特兰数的应用,但 n 最大到了 100,于是要用高精度写

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;

#define BASE 10000
int a[100+5][100];
void multiply(int num,int n,int b) {//大数乘法
    int temp=0;
    for(int i=n-1; i>=0; i--) {
        temp+=b*a[num][i];
        a[num][i]=temp%BASE;
        temp/=BASE;
    }
}
void divide(int num,int n,int b) {//大数除法
    int div=0;
    for(int i=0; i<n; i++) {
        div=div*BASE+a[num][i];
        a[num][i]=div/b;
        div%=b;
    }
}
void init(){
    memset(a,0,sizeof(a));
    a[1][100-1]=1;
    for(int i=2; i<=100; i++) {
        memcpy(a[i],a[i-1],sizeof(a[i-1]));
        multiply(i,100,4*i-2);
        divide(i,100,i+1);
    }
}
int main() {
    init();
    int n;
    while(scanf("%d",&n)!=EOF){
        int i;
		for(i=0;i<100 && a[n][i]==0;i++);
            printf("%d",a[n][i++]);
		for(;i<100;i++)
			printf("%04d",a[n][i]);
		 printf("\n");
	}
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值