Problem D

How Many Trees?

Input: standard input

Output: standard output

Memory Limit: 32 MB

A binary search tree is a binary tree with root k such that any node v in the left subtree of k has label (v) <label (k) and any node w in the right subtree of k has label (w) > label (k).

When using binary search trees, one can easily look for a node with a given label x: After we compare x to the label of the root, either we found the node we seek or we know which subtree it is in. For most binary search trees the average time to find one of its n nodes in this way is O(log n).

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 and Output

The input will contain a number 1 <= i <= 1000 per line representing the number of elements of the set. 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

题意:给定n个结点,求有几种2叉搜索树。

思路:分别取第n个点做根节点。如此图

代码:

#include <stdio.h>
#include <string.h>
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
const int N = 1005;
const int MAXBIGN = 1005; struct bign {
int s[MAXBIGN];
int len;
bign() {
len = 1;
memset(s, 0, sizeof(s));
} bign operator = (const char *number) {
len = strlen(number);
for (int i = 0; i < len; i++)
s[len - i - 1] = number[i] - '0';
return *this;
}
bign operator = (const int num) {
char number[N];
sprintf(number, "%d", num);
*this = number;
return *this;
} bign (int number) {*this = number;}
bign (const char* number) {*this = number;} bign operator + (const bign &c){
bign sum;
int t = 0;
sum.len = max(this->len, c.len);
for (int i = 0; i < sum.len; i++) {
if (i < this->len) t += this->s[i];
if (i < c.len) t += c.s[i];
sum.s[i] = t % 10;
t /= 10;
} while (t) {
sum.s[sum.len++] = t % 10;
t /= 10;
} return sum;
} bign operator * (const bign &c){
bign sum; bign zero;
if (*this == zero || c == zero)
return zero;
int i, j;
sum.len = this->len + c.len;
for (i = 0; i < this->len; i++) {
for (j = 0; j < c.len; j ++) {
sum.s[i + j] += this->s[i] * c.s[j];
}
}
for (i = 0; i < sum.len; i ++) {
sum.s[i + 1] += sum.s[i] / 10;
sum.s[i] %= 10;
}
sum.len ++;
while (!sum.s[sum.len - 1]) {
sum.len --;
}
return sum;
}
bign operator * (const int &num) {
bign c = num;
return *this * c;
}
bign operator / (const int &num) {
bign ans; int k = 0;
ans.len = len;
for (int i = ans.len - 1; i >= 0; i --) {
ans.s[i] = (k * 10 + s[i]) / num;
k = (k * 10 + s[i]) % num;
}
while (!ans.s[ans.len - 1]) {
ans.len --;
}
return ans;
}
bign operator - (const bign &c) {
bign ans;
ans.len = max(this->len, c.len);
int i; for (i = 0; i < c.len; i++) {
if (this->s[i] < c.s[i]) {
this->s[i] += 10;
this->s[i + 1]--;
}
ans.s[i] = this->s[i] - c.s[i];
} for (; i < this->len; i++) {
if (this->s[i] < 0) {
this->s[i] += 10;
this->s[i + 1]--;
}
ans.s[i] = this->s[i];
}
while (ans.s[ans.len - 1] == 0) {
ans.len--;
}
if (ans.len == 0) ans.len = 1;
return ans;
} void put() {
if (len == 1 && s[0] == 0) {
printf("0");
} else {
for (int i = len - 1; i >= 0; i--)
printf("%d", s[i]);
}
} bool operator < (const bign& b) const {
if (len != b.len)
return len < b.len; for (int i = len - 1; i >= 0; i--)
if (s[i] != b.s[i])
return s[i] < b.s[i];
return false;
}
bool operator > (const bign& b) const { return b < *this; }
bool operator <= (const bign& b) const { return !(b < *this); }
bool operator >= (const bign& b) const { return !(*this < b); }
bool operator != (const bign& b) const { return b < *this || *this < b;}
bool operator == (const bign& b) const { return !(b != *this); }
}; bign f[1005];
int n; void init() {
f[1] = 1;
for (int i = 2; i <= 1000; i ++) {
f[i] = f[i - 1] * (4 * i - 2) / (i + 1);
}
} int main() {
init();
while (~scanf("%d", &n) && n) {
f[n].put();
printf("\n");
}
return 0;
}

UVA 10303 - How Many Trees?(数论 卡特兰数 高精度)的更多相关文章

  1. UVa 10007 - Count the Trees(卡特兰数+阶乘+大数)

    题目链接:UVa 10007 题意:统计n个节点的二叉树的个数 1个节点形成的二叉树的形状个数为:1 2个节点形成的二叉树的形状个数为:2 3个节点形成的二叉树的形状个数为:5 4个节点形成的二叉树的 ...

  2. HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)

    Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...

  3. hdu 1130 How Many Trees? 【卡特兰数】

    题目 题意:给你一个数字n,问你将1~n这n个数字,可以组成多少棵不同的二叉搜索树. 1,2,5,14--根据输出中的规律可以看出这是一个卡特兰数的序列.于是代用卡特兰数中的一个递推式: 因为输入可取 ...

  4. ACM数论-卡特兰数Catalan

    Catalan 原理: 令h(0)=1,h(1)=1,catalan 数满足递归式: (其中n>=2) 另类递推公式: 该递推关系的解为: (n=1,2,3,...) 卡特兰数的应用实质上都是递 ...

  5. hdu 1023 卡特兰数+高精度

    Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. BZOJ2822[AHOI2012]树屋阶梯——卡特兰数+高精度

    题目描述 暑假期间,小龙报名了一个模拟野外生存作战训练班来锻炼体魄,训练的第一个晚上,教官就给他们出了个难题.由于地上露营湿气重,必须选择在高处的树屋露营.小龙分配的树屋建立在一颗高度为N+1尺(N为 ...

  7. 【BZOJ 2822】2822: [AHOI2012]树屋阶梯(卡特兰数+高精度)

    2822: [AHOI2012]树屋阶梯 Description 暑假期间,小龙报名了一个模拟野外生存作战训练班来锻炼体魄,训练的第一个晚上,教官就给他们出了个难题.由于地上露营湿气重,必须选择在高处 ...

  8. BZOJ2822:[AHOI2012]树屋阶梯(卡特兰数,高精度)

    Description 暑假期间,小龙报名了一个模拟野外生存作战训练班来锻炼体魄,训练的第一个晚上,教官就给他们出了个难题.由于地上露营湿气重,必须选择在高处的树屋露营.小龙分配的树屋建立在一颗高度为 ...

  9. bzoj3907 网格 & bzoj2822 [AHOI2012]树屋阶梯——卡特兰数+高精度

    题目:bzoj3907:https://www.lydsy.com/JudgeOnline/problem.php?id=3907 bzoj2822:https://www.lydsy.com/Jud ...

随机推荐

  1. PHP 发送HTTP请求的几种方式

    1.curl仍然是最好的HTTP库,没有之一. 可以解决任何复杂的应用场景中的HTTP 请求2. 文件流式的HTTP请求比较适合处理简单的HTTP POST/GET请求,但不适用于复杂的HTTP请求3 ...

  2. js十大排序算法详解

    十大经典算法导图  图片名词解释:n: 数据规模k:“桶”的个数In-place: 占用常数内存,不占用额外内存Out-place: 占用额外内存 1.冒泡排序 1.1  原始人冒泡排序 functi ...

  3. 关系操作符 == != equals()

    ==  和!= //: object/test.java package object; import java.util.*; public class Test{ public static vo ...

  4. 洛谷p1072 gcd,质因数分解

    /* 可以得a>=c,b<=d,枚举d的质因子p 那么a,b,c,d,x中包含的p个数是ma,mb,mc,md,mx 在gcd(a,x)=c中 ma<mc => 无解 ma=m ...

  5. python 全栈开发,Day74(基于双下划线的跨表查询,聚合查询,分组查询,F查询,Q查询)

    昨日内容回顾 # 一对多的添加方式1(推荐) # book=Book.objects.create(title="水浒传",price=100,pub_date="164 ...

  6. Linux学习笔记:使用prompt关闭ftp中mget和mput的确认提醒

    当使用mget和mput上传或下载多个文件时,为了关闭确认提醒,可使用prompt命令. ftp prompt  -- 切换提示 切换交谈式指令(使用mput/mget 时不用每个文件皆询问yes/n ...

  7. Ubuntu 16.4 安装anaconda 详细教程

    下载 官方下载地址:https://www.continuum.io/downloads 所有安装包地址:https://repo.continuum.io/archive/ 这里使用 Python ...

  8. 在php中调用以及编写接口(转)

    如: http://localhost/openUser.php?act=get_user_list&type=json 在这里openUser.php相当于一个接口,其中get_user_l ...

  9. vijos 1128 N个数选K个数 (DFS )

    从 n 个整数中任选 k 个整数相加,可分别得到一系列的和 要求你计算出和为素数共有多少种 IN4 33 7 12 19 OUT1 # include <iostream> # inclu ...

  10. Hibernate之关联关系映射(一对多和多对一映射,多对多映射)

    ~~~接着之前的Hibernate框架接着学习(上篇面试过后发现真的需要学习一下框架了,不然又被忽悠让去培训.)~~~ 1:Hibernate的关联映射,存在一对多和多对一映射,多对多映射: 1.1: ...