#P1814. 数的序号

数的序号

Description

We can label binary trees using the following scheme:

  • The empty tree has index 00.
  • The tree with only a root node has index 11.
  • All binary trees with mm nodes have indices smaller than any binary tree with m+1m+1 nodes.
  • For any binary tree, let its left-subtree index be LL, its right-subtree index be RR, and it have mm nodes. If its index is nn, then for all mm-node binary trees whose indices are greater than nn, either their left-subtree index is greater than LL, or their left-subtree index equals LL and their right-subtree index is greater than RR.

For example, the six trees with indices 00 through 55 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 nn. n=0n=0 indicates the end of input, and you do not need to output the empty tree for n=0n=0.

Output Format

For each test case, output one line representing the tree corresponding to index nn.

A binary tree is represented as (L)X(R), where LL and RR 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

对于所有测试点,保证 1n5×1081\le n\le 5\times10^8,数据组数不超过 5050

Translated by ChatGPT 5