eolymp
bolt
Try our new interface for solving problems
Problems

Trees Made to Order

Trees Made to Order

We can number binary trees using the following scheme: - The empty tree is numbered \textbf{0}. - The single-node tree is numbered \textbf{1}. - All binary trees having \textbf{m} nodes have numbers less than all those having \textbf{m + 1} nodes. - Any binary tree having \textbf{m} nodes with left and right subtrees \textbf{L} and \textbf{R} is numbered \textbf{n} such that all trees having \textbf{m} nodes numbered > \textbf{n} have either - Left subtrees numbered higher than \textbf{L}, or - A left subtree = \textbf{L} and a right subtree numbered higher than \textbf{R}. The first \textbf{10} binary trees and tree number \textbf{20} in this sequence are shown below: \includegraphics{https://static.e-olymp.com/content/2f/2f4c46e3d8bdde45e5fc03a97dd362fa2e22fd61.jpg} Your job for this problem is to output a binary tree when given its order number. \InputFile Input consists of multiple problem instances. Each instance consists of a single integer \textbf{n} (\textbf{1} ≤ \textbf{n} ≤ \textbf{500,000,000}). A value of \textbf{n} = \textbf{0} terminates input. (Note that this means you will never have to output the empty tree.) \OutputFile 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 \textbf{X}. A tree with left and right subtrees \textbf{L} and \textbf{R} should be output as \textbf{(L')X(R')}, where \textbf{L'} and \textbf{R'} are the representations of \textbf{L} and \textbf{R}. If \textbf{L} is empty, just output \textbf{X(R')}. If \textbf{R} is empty, just output \textbf{(L')X}.
Time limit 1 second
Memory limit 64 MiB
Input example #1
1
20
31117532
0
Output example #1
X
((X)X(X))X
(X(X(((X(X))X(X))X(X))))X(((X((X)X((X)X)))X)X)