#P2274. [HNOI2002] 树的排序

[HNOI2002] 树的排序

Description

  1. The empty tree is numbered 00, and the tree with only the root node is numbered 11.
  2. Let mm be any non-negative integer. Then the number of any tree with mm nodes is smaller than the number of any tree with m+1m+1 nodes.
  3. Let A,BA, B be two trees with the same number of nodes (with ABA \ne B). The number of AA is smaller than that of BB if and only if one of the following holds:
    1. The number of the left subtree of AA is smaller than the number of the left subtree of BB.
    2. The numbers of the left subtrees of AA and BB are equal (that is, the left subtrees of AA and BB have the same shape), and the number of the right subtree of AA is smaller than the number of the right subtree of BB.
  4. Following the usual rule, the numbers are consecutive non-negative integers. Each tree corresponds to a unique number, and each non-negative integer corresponds to a unique tree.

(Note: all trees mentioned above are binary trees.)

Input Format

A single line with an integer nn, 1n500,000,0001\le n\le 500{,}000{,}000.

For 10%10\% of the testdata, the number of nodes in the tree does not exceed three.

Output Format

A single line: the binary tree corresponding to number nn. Output as follows:

  • If it is a one-node binary tree, output XX.
  • If the left and right subtrees are LL and RR, and their respective outputs are LL' and RR', then output (L)X(R)\texttt{(}L'\texttt{)}X\texttt{(}R'\texttt{)}; when the left subtree is empty, output X(R)X\texttt{(}R'\texttt{)}; when the right subtree is empty, output (L)X\texttt{(}L'\texttt{)}X.
20
((X)X(X))X

Hint

Translated by ChatGPT 5