HDU 1130 How Many Trees?

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

How Many Trees?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3746 Accepted Submission(s): 2106

Problem Description
A binary search tree is a binary tree with root k such that any node v reachable from its left has label (v) < label (k) and any node w reachable from its right has label (w) > label (k). It is a search structure which can find a node with label x in O(n log n) average time, where n is the size of the tree (number of vertices).

Given a number n, can you tell how many different binary search trees may be constructed with a set of numbers of size n such that each element of the set will be associated to the label of exactly one node in a binary search tree?

Input
The input will contain a number 1 < = i < = 100 per line representing the number of elements of the set.

Output
You have to print a line in the output for each entry with the answer to the previous question.

Sample Input

1
2
3

Sample Output

1
2
5


虽然题目没完全看懂,但是看了看样例 目测就卡特兰数 还和1134 Game of Connections这题一模一样
直接复制粘贴就A了
http://blog.csdn.net/luricheng/article/details/52485111


#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值