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;
}