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 ...
随机推荐
- 使用JSONP实现跨域通信
引语 Ajax 允许在不干扰 Web 应用程序的显示和行为的情况下在后台进行数据检索.Ajax 允许在不干扰 Web 应用程序的显示和行为的情况下在后台进行数据检索.由于受到浏览器的限制,该方法不允许 ...
- pytest十二:cmd命令行参数
命令行参数是根据命令行选项将不同的值传递给测试函数,比如平常在 cmd 执行”pytest —html=report.html”,这里面的”—html=report.html“就是从命令行传入的参数对 ...
- spring + quartz定时任务,以及修改定时任务
spring4+quartz2.2.3,定时任务弄好了,修改定时任务没折腾起,没找到合适的解决方案. 最终使用库spring-context-support 3.2.17.RELEASE + qua ...
- 【C++ Primer 第11章】4. 无序容器
一.介绍 1. Hashtable和bucket 由于unordered_map内部采用的hashtable的数据结构存储,所以,每个特定的key会通过一些特定的哈希运算映射到一个特定的位置,我们知道 ...
- 016 SpringMVC中重定向
1.介绍 2.index <%@ page language="java" contentType="text/html; charset=utf-8" ...
- 072 HBase的架构以及各个模块的功能
一:整体架构 1.体系结构 2.物理模型 3.存储体系 regionserver—>region->多个store(列簇)->一个memstore和多个storefile 4.HDF ...
- R从3.4升级到3.5
这里介绍的就是R的一个包:installr. installr {installr} R Documentation Installing software from R Description Gi ...
- turbo boost - 睿频加速
turbo boost就是英特尔睿频加速技术 英特尔睿频加速技术是英特尔酷睿 i7/i5 处理器的独有特性,也是英特尔新宣布的一项技术.这项技术可以理解为自动超频.当开启睿频加速之后,CPU会根据当前 ...
- 递归回溯groupSum
- P3819 松江1843路
P3819 松江1843路sigema(r[i]*abs(x[i]-x[s]));令它最小,是带权中位数问题,s是带权中位数,s左边的r[i]之和+r[s]大于s左边的r[i]之和,反过来也成立.如果 ...