#P1814. 数的序号
数的序号
Description
We can label binary trees using the following scheme:
- The empty tree has index .
- The tree with only a root node has index .
- All binary trees with nodes have indices smaller than any binary tree with nodes.
- For any binary tree, let its left-subtree index be , its right-subtree index be , and it have nodes. If its index is , then for all -node binary trees whose indices are greater than , either their left-subtree index is greater than , or their left-subtree index equals and their right-subtree index is greater than .
For example, the six trees with indices through have the following shapes:
0 1 2 3 4 5
X X X X X
\ / \ \
X X X X
\ /
X X
Your task is to output the binary tree corresponding to a given index.
Input Format
This problem has multiple test cases.
Each line contains a non-negative integer . indicates the end of input, and you do not need to output the empty tree for .
Output Format
For each test case, output one line representing the tree corresponding to index .
A binary tree is represented as (L)X(R), where and are the representations of the left and right subtrees, respectively. If one side is empty, omit it; for example, if there is only a left subtree, it is represented as (L)X.
1
20
31117532
0
X
((X)X(X))X
(X(X(((X(X))X(X))X(X))))X(((X((X)X((X)X)))X)X)
Hint
Constraints
对于所有测试点,保证 ,数据组数不超过 。
Translated by ChatGPT 5
京公网安备 11011102002149号