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. 调试Windows Service

    调试Windows Service 使用一般的调试方法调试不了Windows Servers,所以参考了一些调试方法 我服务源码中使用了Timer,注意不能使用工具箱内的Timer,用System.T ...

  2. wpf 自定义属性的默认值

    public int MaxSelectCount { get { return (int)GetValue(MaxSelectCountProperty); } set { SetValue(Max ...

  3. python 全栈开发,Day14(列表推导式,生成器表达式,内置函数)

    一.列表生成式 生成1-100的列表 li = [] for i in range(1,101): li.append(i) print(li) 执行输出: [1,2,3...] 生成python1期 ...

  4. RabbitMQ(一):RabbitMQ 安装与配置(Mac)

    一.rabbitmq 安装与配置 安装: brew install rabbitmq # 进入安装目录 cd /usr/local/Cellar/rabbitmq/3.7.12 # 启动 brew s ...

  5. 2018-2019-2 网络对抗技术 20165333 Exp4 恶意代码分析

    2018-2019-2 网络对抗技术 20165333 Exp4 恶意代码分析 原理与实践说明 1.实践目标 监控你自己系统的运行状态,看有没有可疑的程序在运行. 分析一个恶意软件,就分析Exp2或E ...

  6. MVC4 下DropDownList使用方法(转)

    与MVC3相比,差别很大: 表现形式一: public ActionResult Main() { List<SelectListItem> items = new List<Sel ...

  7. hdu 1757 和1005差不多 (矩阵快速幂)

    If x < 10 f(x) = x.If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-1 ...

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

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

  9. LeetCode 4. Median of Two Sorted Arrays (分治)

    两个有序的数组 nums1 和 nums2 维数分别为m,n.找所有数的中位数,复杂度 O(log (m+n)) 注意:奇偶个数,分治法求解,递归出口特殊处理.取Kth smallest数时,分治取m ...

  10. thinkphp实现附件上传

    先需要制定图片上传的目录 然后用uploadOne方法保存 有的时候需要缩略图,比如文章的封面图片 $config=array( 'rootPath' => './Application/Pub ...