HDOJ HDU 1134 Game of Connections

HDOJ 1134 Game of Connections

题目

点此查看 HDOJ 1134 Game of Connections

题意

1, 2, 3,…, 2n-1, 2n 连续顺时针顺序在地上形成一个圆圈, 然后, 画一些线段连接到数对。每个数字必须互相连接。并且, 不允许两个线段相交
求连接种数

题解

先写几个数
1 2 5 14
熟悉的人已经看出是卡特兰数了
计算程序 说明 见 常用表 Catalan(卡特兰)数 1 - 100

代码

#include <iostream>
#include <iomanip>
#include <cstring>

#define maxn 101
#define maxnum 500
#define maxint 10000
using namespace std;

struct bigint
{
    int m;
    int perint[maxnum];

    bigint()
    {
        for(int i = 0;i < maxnum;i++)
            perint[i] = 0;
    }
    bigint & operator+(bigint & b)
    {
        int c = 0,cur;
//        bigint t;
        for(int i = 0;i < m;i++)
        {
            cur = perint[i] + b.perint[i];
            perint[i] = cur % maxint + c;
            c = cur / maxint;
        }
        if(c)
        {
            m++;
            perint[m] = c;
        }
        return *this;
    }
    bigint & operator*(int b)
    {
        int c = 0,cur;
        for(int i = 0;i <= m;i++)
        {
            cur = perint[i] * b + c;
            perint[i] = cur % maxint;
            c = cur / maxint;
        }
        if(c)
        {
            m++;
            perint[m] = c;
        }
        return *this;
    }
    bigint & operator/(int b)
    {
        int c = 0,cur;
        for(int i = m;i >=0 ;i--)
        {
            cur = c * maxint + perint[i];
            perint[i] = cur / b;
            c = cur % b;
        }
        return *this;
    }
    friend ostream & operator<<(ostream &out,bigint & b)
    {
        int s;
        for(s = b.m;s >= 0;s--)
            if(b.perint[s])
                break;
        out << b.perint[s];
        for(int i = s - 1;i >= 0;i--)
                out << setw(4) << setfill('0') << b.perint[i];
        return out;
    }
};

bigint tb[maxn];

int main()
{
    int nm;
    tb[0].perint[0] = 1;
    tb[1].perint[0] = 1;
    for(int i = 2;i < maxn;i++)
    {
        tb[i] = tb[i-1] * (4*i - 2) / (i + 1);
//        tb[i] = tb[i] + tb[i-1];
    }
//    cout << tb[1000] << endl; 
    while(cin >> nm && nm != -1)
    {
        cout << tb[nm-1] << endl; 
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值