POJ 2084 Game of Connections(大数+卡特兰数)

25 篇文章 0 订阅
20 篇文章 4 订阅
Game of Connections
Time Limit:  1000MS

Memory Limit:  30000K
Total Submissions:  8331

Accepted:  4141

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
题意:输入一个n,将1-2n按顺时针写下来,任选两个数用连线连接,要求连线不能交叉,且每个点只能连一次,
比如,n=2时,如图,只有俩种情况,

思路:当n=3时,先考虑第一个点可以和哪个点相连,可以和2,4,6点相连,为什么3,5不行,因为如果和第三个点相连,那么第二个点无论和谁连都会出现交叉(如图),与5不能相连的原因也是这样,总结是如果连线的一边的点的数量是奇数,那么肯定不能两两连接,按照这种规律可以推出前几项,a[1]=1,a[2]=2,a[3]=5,a[4]=14,,,满足卡特兰数的规律,根据卡特兰数的递推式a[i]=a[i-1]*(4*i-2)/(i+1);但这里的乘除都是大数的乘除,,,
        利用万进制的思想。。。。用二维数组打表。

以下AC代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int max=100;
void mul(int a[],int b)
{///大数乘法
    int i,jinwei=0;///进位
    for(i=max-1;i>=0;i--)
    {
        jinwei+=b*a[i];
        a[i]=jinwei%10000;
        jinwei/=10000;
    }
}
void div(int a[],int b)
{///大数除法
    int i,jiewei=0;///借位
    for(i=0;i<max;i++)
    {
        jiewei=jiewei*10000+a[i];
        a[i]=jiewei/b;
        jiewei%=b;
    }
}
int main()
{
    int n;
    int i,j;
    int a[101][101];
    for(i=0;i<101;i++)
        for(j=0;j<101;j++)
        a[i][j]=0;
        ///初始化a[1]=1;
        for(i=0;i<max-1;i++)
            a[1][i]=0;
        a[1][max-1]=1;

    for(i=2;i<=100;i++)
    {
        memcpy(a[i],a[i-1],max*sizeof(int)); //将a[i-1]拷贝到a[i]
        mul(a[i],4*i-2);
        div(a[i],i+1);
    }
    while(scanf("%d",&n))
    {
        if(n==-1)
            break;
         for(i=0;i<max&&a[n][i]==0;i++);
            printf("%d",a[n][i++]);    ///输出首位
            for(;i<max;i++)
                {
                    printf("%04d",a[n][i]);///输出剩下的位数,数组中不足4位的在前边补0
                }
                printf("\n");
    }

    return 0;
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值