HDU 1134 Game of Connections

8 篇文章 0 订阅
7 篇文章 0 订阅

Game of Connections
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4231 Accepted Submission(s): 2456

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


设c(n)为n个点之间连线的方案数
当6个点连线时
如果1和2连接 剩余4个点 那一共有1*c(4)种
如果1和6连接 剩余4个点 有1*c(4)种
如果1和4连接 将左右2边的点都分隔开了 左右两边各剩余2个点 有c(2)*c(2)种
所以不难发现 
c(0)=c(1)=1
c(n)=c(0)*c(n-2)+c(2)*c(n-4)+…….c(n-2)*c(0) n>=2
然后大数运算即可
直接递归肯定爆时间 so 加个记忆化或者迭代求解

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
#include<bitset>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007

struct BigNum{
    deque<int>num;
    //构造函数
    BigNum(){
        num.push_back(0);
    }
    BigNum(int x){
        if(!x)
            num.push_back(0);
        while(x){
            num.push_front(x%10);
            x/=10;
        }
    }
    //重载运算符
    BigNum operator+(const BigNum&x){//大数加法
        BigNum res;
        const deque<int>&a=num,&b=x.num;
        deque<int>&c=res.num;
        int alen=a.size(),blen=b.size();
        c.resize(max(alen,blen));
        for(int i=1,len=c.size();i<=c.size();++i){
            if(alen>=i)
                c[len-i]+=a[alen-i];
            if(blen>=i)
                c[len-i]+=b[blen-i];
        }
        for(int i=c.size()-1;i>0;--i){
            c[i-1]+=c[i]/10;
            c[i]%=10;
        }
        if(c[0]>9){
            c[0]-=10;
            c.push_front(1);
        }
        return res;
    }
    BigNum operator*(const BigNum&x){//大数乘法
        int zeros=0;
        BigNum res,t;
        const deque<int>&a=this->num,&b=x.num;
        deque<int>&tem=t.num;
        for(int i=b.size()-1;i>=0;--i){
            tem.clear();
            for(int j=0;j<a.size();++j)
                tem.push_back(a[j]*b[i]);
            for(int j=0;j<zeros;++j)
                tem.push_back(0);
            for(int j=tem.size()-1;j>0;--j){
                tem[j-1]+=tem[j]/10;
                tem[j]%=10;
            }
            if(tem[0]>9){
                tem.push_front(tem[0]/10);
                tem[1]%=10;
            }
            res=res+t;
            ++zeros;
        }
        while(res.num.size()>1&&res.num[0]==0)
            res.num.pop_front();
        return res;
    }
};

ostream&operator<<(ostream&out,BigNum&x){
    deque<int>&a=x.num;
    for(int i=0;i<a.size();++i)
        cout<<a[i];
    return out;
}

BigNum c[201];

void init(){
    c[0]=c[1]=BigNum(1);
    for(int i=2;i<=200;++i)
        for(int j=0;j<=i-2;j+=2)
            c[i]=c[i]+c[j]*c[i-2-j];
}

int main()
{
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    int n;
    init();
    while(cin>>n&&n!=-1){
        cout<<c[2*n]<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值