hdu 1100 trees made to order
来源:互联网 发布:俄罗斯方块知乎 编辑:程序博客网 时间:2024/05/01 19:16
Trees Made to Order
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 362 Accepted Submission(s): 208
Problem Description
We can number binary trees using the following scheme:
The empty tree is numbered 0.
The single-node tree is numbered 1.
All binary trees having m nodes have numbers less than all those having m+1 nodes.
Any binary tree having m nodes with left and right subtrees L and R is numbered n such that all trees having m nodes numbered > n have either
Left subtrees numbered higher than L, or
A left subtree = L and a right subtree numbered higher than R.
The first 10 binary trees and tree number 20 in this sequence are shown below:
Your job for this problem is to output a binary tree when given its order number.
The empty tree is numbered 0.
The single-node tree is numbered 1.
All binary trees having m nodes have numbers less than all those having m+1 nodes.
Any binary tree having m nodes with left and right subtrees L and R is numbered n such that all trees having m nodes numbered > n have either
Left subtrees numbered higher than L, or
A left subtree = L and a right subtree numbered higher than R.
The first 10 binary trees and tree number 20 in this sequence are shown below:
Your job for this problem is to output a binary tree when given its order number.
Input
Input consists of multiple problem instances. Each instance consists of a single integer n, where 1 <= n <= 500,000,000. A value of n = 0 terminates input. (Note that this means you will never have to output the empty tree.)
Output
For each problem instance, you should output one line containing the tree corresponding to the order number for that instance. To print out the tree, use the following scheme:
A tree with no children should be output as X.
A tree with left and right subtrees L and R should be output as (L')X(R'), where L' and R' are the representations of L and R.
If L is empty, just output X(R').
If R is empty, just output (L')X.
A tree with no children should be output as X.
A tree with left and right subtrees L and R should be output as (L')X(R'), where L' and R' are the representations of L and R.
If L is empty, just output X(R').
If R is empty, just output (L')X.
Sample Input
120311175320
Sample Output
X((X)X(X))X(X(X(((X(X))X(X))X(X))))X(((X((X)X((X)X)))X)X)
Source
East Central North America 2001
Recommend
JGShining
先计算 f [i],保存有 i 个节点的树可有多少个,在每个节点试探左儿子有多少个节点,右儿子有多少节点,然后进行一些细节的计算。
比如得到左儿子节点数后,就知道左儿子是那种树(节点数相同是一种树),这时再计算左儿子应该是这种树中的第几棵。
虽然已经尽量追求代码整洁了,但是代码还是比较丑。。。
#include <cstdio>#include <cstdlib>#include <cassert>#include <cstring>#include <algorithm>#include <numeric>#define MAXN 100long long f [MAXN];void print_tree (long long n) {#ifdef _DEBUG printf ("n = %lld\n", n);#endif if (n == 0) { return; } else if (n == 1) { printf ("X"); return; } long long nodes = 0; long long count = 0; while (count <= n) { count += f [nodes++]; } count -= f [--nodes]; long long l_nodes = 0; long long r_nodes = nodes - 1 - l_nodes; long long d = 0; while (count + d <= n) { d += f [l_nodes++] * f [r_nodes--]; } d -= f [--l_nodes] * f [++r_nodes]; long long t = n - count - d; long long l_n = std::accumulate (f, f + l_nodes, 0) + t / f [r_nodes]; long long r_n = std::accumulate (f, f + r_nodes, 0) + t % f [r_nodes]; if (l_n) { printf ("("); print_tree (l_n); printf (")"); } printf ("X"); if (r_n) { printf ("("); print_tree (r_n); printf (")"); }}int main () { memset (f, 0, sizeof (f)); long long sum = 0; f [0] = f [1] = 1; for (int i=2; i<MAXN; ++i) { for (int k=0; k<i; ++k) { f [i] += f [k] * f [i - k - 1]; } if ((sum += f [i]) > 5000000000LL) { break; } }#ifdef _DEBUG for (int i=0; i<20; ++i) { printf ("%lld%s", f [i], i==19 ? "\n" : " "); }#endif long long n; while (scanf ("%lld", &n) == 1 && n) { print_tree (n); printf ("\n"); } return 0;}
- hdu 1100 trees made to order
- HDU 1100 Trees Made to Order
- HDU 1100 Trees Made to Order
- HDU 1100 Trees Made to Order (Catalan数)
- Trees Made to Order
- Trees Made to Order
- 杭电1100 Trees Made to Order
- ZOJ1062 Trees Made to Order
- hdu_1100 Trees Made to Order
- hdu1100 Trees Made to Order
- hdu1100:Trees Made to Order
- ZOJ-1062-Trees Made to Order
- poj 1095--Trees Made to Order
- poj 1095 Trees Made to Order
- POJ 1095 Trees Made to Order 递归
- poj 1095 Trees Made to Order
- poj-1095-Trees Made to Order
- poj-1095-Trees Made to Order
- android widget 之CheckBox
- VxWorks系统概念妙喻
- 中断絮说(一)——陈莉君老师经典
- 浅复制和深度复制 clone()
- 数据库的优化
- hdu 1100 trees made to order
- C++中屏蔽offsetof告警的方法
- (Effective C++)第七章 模板与泛型编程 (Templates and Generic Programming)
- 中断絮说(二)——陈莉君老师经典
- (Effective C++)第八章 定制new和delete(Customizing new and delete)
- 顺序表与链表的比较
- [转]查找算法-Hash表
- 三、SQL语句:查询_1
- Web设计集锦点滴