Trees made to Order
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
| Total Submissions: 6882 |
|
Accepted: 3940 |
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 had m nodes had numbers less than all those had m+1 nodes.
Any binary tree has m nodes with left and right subtrees L and R are numbered n such that all trees have m nodes Numbe Red > N has either left subtrees numbered higher than L, or a left subtree = L and a right subtree numbered higher tha N R.
The first binary trees and tree number, this sequence is shown below:
Your job for this problem was to output a binary tree when the 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 never has 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 Insta nCE. To print out the tree, use the following scheme:
A tree with no children should is output as X.
A tree with left and right subtrees L and R should is output as (L ') X (R '), where l ' and R ' is the representations of L an D 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)
Source
East Central North America 2001
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <queue> #include <stack> #include <vector> #include <set> #include <map > #define L (x) (x<<1) #define R (x) (x<<1|1) #define MID (x, y) ((x+y) >>1) #define EPS 1e-8typedef __ Int64 ll; #define FRE (i,a,b) for (i = A; I < b; i++) #define FREE (i,b,a) for (i = b; I >= a;i--) #define MEM (T, v) mem Set ((t), V, sizeof (t)) #define SSF (n) scanf ("%s", N) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF Printf#define bu G PF ("hi\n") using namespace std, #define INF 0x3f3f3f3f#define n 20int dp[n];void inint () {int i,j;dp[0]=dp[1]=1;for (i=2;i<n;i++) {fre (j,0,i) dp[i]+=dp[j]*dp[i-1-j]; The case of I node has enumerated left put J, because the root node needs one, then right i-1-j}}void dfs (int n,int k)//n nodes, K large {if (n==1)//Start is k==1, always wrong {pf ("X"); return;} int i=0; For (i=0;; i++) if (dp[i]*dp[n-i-1]>=k) break; Dp[i on the left], dp[n-i-1 on the right), because there is a point when the root node else k-=dp[i]*dp[n-i-1]; if (i)//left has node {pf (");d FS (i, (k-1)/dp[n-i-1]+1);p F (") ");} PF ("X"), if (n-i-1)//Right has node {pf ("(");d FS (N-1-i, (k-1)%dp[n-1-i]+1);p F (")");}} int main () {int i,j,n;inint (); while (SF (n), N) {for (i=1;; i++) if (dp[i]>=n) break; The I node that has been locked is the else N-=dp[i];d FS (i,n) that satisfies the condition; PF ("\ n");} return 0;}
POJ 1095 Trees made to Order (count problem)