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. LeetCode(5):最长回文子串

    Medium! 题目描述: 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 长度最长为1000. 示例: 输入: "babad" 输出: "bab&quo ...

  2. Application.ProcessMessages; 的重要性

    https://files.cnblogs.com/files/del88/登陆光标_悬赏50元.zip ----------------------------------------------- ...

  3. hdu 1372 骑士从起点走到终点的步数 (BFS)

    给出起点和终点 求骑士从起点走到终点所需要的步数 Sample Inpute2 e4 //起点 终点a1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 f6 Sample OutputT ...

  4. 大数据统计分析平台之二、ElasticSearch 6.2.1的安装与使用

    # 下载文件cd /usr/local/software wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch ...

  5. springbank 开发日志 springbank是如何注册handler的

    这要从DefaultAnnotationHandlerMapping这个类说起,该类被@Component注释,该类被Spring IOC容器实例化之后,将会执行其initApplicationCon ...

  6. Ubuntu 安装 OpenMPI

    1. 下载OpenMPI 在官网上下载最新版本的安装包,如:openmpi-1.8.4.tar.gz 2. 解压并进行配置 tar -zxvf openmpi-3.0.0.tar.gz cd open ...

  7. 每天减一半。问多少天这个绳子会小于5米?进而得while和for的关系

    一:前提 1.程序 2.结果 3.使用 for的条件只要>5 变化的条件是x/=2 4.进而使用while,得第二种方法 5.结果相同 二:结论 程序可以使用for的必将可以使用while. 其 ...

  8. 二分搜索-poj2785

    题目链接:http://poj.org/problem?id=2785 题目大意:要求输入A,B,C,D四个数组,从每个数组中分别取出一个数来相加,求出相加后 和为0 总共有多少种加法. #inclu ...

  9. Linux学习之用户管理命令与用户组管理命令(十五)

    Linux学习之用户管理命令与用户组管理命令 目录 用户管理命令 用户添加命令useradd 修改用户密码passwd 修改用户信息usermod 修改用户密码状态chage 删除用户userdel ...

  10. SSL/TLS中间人攻击

    准备:kali.xp kali ip:192.168.14.157 目标ip:192.168.14.158 目标网关:192.168.14.2 使用工具:ettercap.sslstrip.arpsp ...