UVA 10303 - How Many Trees?(数论 卡特兰数 高精度)
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?(数论 卡特兰数 高精度)的更多相关文章
- UVa 10007 - Count the Trees(卡特兰数+阶乘+大数)
题目链接:UVa 10007 题意:统计n个节点的二叉树的个数 1个节点形成的二叉树的形状个数为:1 2个节点形成的二叉树的形状个数为:2 3个节点形成的二叉树的形状个数为:5 4个节点形成的二叉树的 ...
- HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)
Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...
- hdu 1130 How Many Trees? 【卡特兰数】
题目 题意:给你一个数字n,问你将1~n这n个数字,可以组成多少棵不同的二叉搜索树. 1,2,5,14--根据输出中的规律可以看出这是一个卡特兰数的序列.于是代用卡特兰数中的一个递推式: 因为输入可取 ...
- ACM数论-卡特兰数Catalan
Catalan 原理: 令h(0)=1,h(1)=1,catalan 数满足递归式: (其中n>=2) 另类递推公式: 该递推关系的解为: (n=1,2,3,...) 卡特兰数的应用实质上都是递 ...
- hdu 1023 卡特兰数+高精度
Train Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- BZOJ2822[AHOI2012]树屋阶梯——卡特兰数+高精度
题目描述 暑假期间,小龙报名了一个模拟野外生存作战训练班来锻炼体魄,训练的第一个晚上,教官就给他们出了个难题.由于地上露营湿气重,必须选择在高处的树屋露营.小龙分配的树屋建立在一颗高度为N+1尺(N为 ...
- 【BZOJ 2822】2822: [AHOI2012]树屋阶梯(卡特兰数+高精度)
2822: [AHOI2012]树屋阶梯 Description 暑假期间,小龙报名了一个模拟野外生存作战训练班来锻炼体魄,训练的第一个晚上,教官就给他们出了个难题.由于地上露营湿气重,必须选择在高处 ...
- BZOJ2822:[AHOI2012]树屋阶梯(卡特兰数,高精度)
Description 暑假期间,小龙报名了一个模拟野外生存作战训练班来锻炼体魄,训练的第一个晚上,教官就给他们出了个难题.由于地上露营湿气重,必须选择在高处的树屋露营.小龙分配的树屋建立在一颗高度为 ...
- bzoj3907 网格 & bzoj2822 [AHOI2012]树屋阶梯——卡特兰数+高精度
题目:bzoj3907:https://www.lydsy.com/JudgeOnline/problem.php?id=3907 bzoj2822:https://www.lydsy.com/Jud ...
随机推荐
- 并发之volatile关键字
volatile关键字 volatile关键字是什么 在上一章我们讲到了并发的的三个概念,那么今天在讲解下在java中可以保证可见性和有序性的一个关键字. volatile关键字 :当变量的值被该关键 ...
- Shell命令 中|| &&使用
转自:https://www.cnblogs.com/aaronLinux/p/8340281.html
- 关于MIS 系统所需技术和含义
操作系统的作用在于 1资源管理 2人机交互.它提供各个应用软件的运行平台,也为用户提供交互界面.所需技术:一.b/s架构B/S结构即浏览器和服务器结构,在这种结构下,用户工作界面是通过WWW浏览器来实 ...
- In Action HDU3339
这是最短路问题和01背包问题的相结合 第一次用01背包 把j打成了i检查了半个小时 下次要注意! 使用的油耗相当于容量 而power相当于价值 先用dijkstra把从基地到所有路的最短情况算出来 ...
- drupal笔记
$app_root :网站根目录 安装 汉化:1将汉化包放置drupal8\sites\default\files\translations下安装:2极简版的话需要在extend(扩展)中安装Inte ...
- 洛谷 P1057 传球游戏 【dp】(经典)
题目链接:https://www.luogu.org/problemnew/show/P1057 题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做传球游戏. 游 ...
- 【知了堂学习笔记】java 自定义异常
java 常见异常种类(Java Exception): 算术异常类:ArithmeticExecption 空指针异常类:NullPointerException 类型强制转换异常:ClassCas ...
- Python3 卷积神经网络卷积层,池化层,全连接层前馈实现
# -*- coding: utf-8 -*- """ Created on Sun Mar 4 09:21:41 2018 @author: markli " ...
- springboot项目接入配置中心,实现@ConfigurationProperties的bean属性刷新方案
前言 配置中心,通过key=value的形式存储环境变量.配置中心的属性做了修改,项目中可以通过配置中心的依赖(sdk)立即感知到.需要做的就是如何在属性发生变化时,改变带有@Configuratio ...
- BZOJ.4892.[TJOI2017]DNA(后缀自动机/后缀数组)
题目链接 \(Description\) 给出两个串\(S,T\),求\(T\)在\(S\)中出现了多少次.出现是指.可以有\(3\)次(\(3\)个字符)不匹配(修改使其匹配). \(Solutio ...