Bracket String Notation (Small)
InterviewTime limit1.5sMemory limit1024 MB
Given N, find the shortest bracket string whose value is N, breaking length ties by lexicographic order.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Math
- Solved
- No attempts yet
Problem
A bracket string is a string that can be built only with these three rules.
- The empty string is a bracket string.
- For a bracket string ,
()is a bracket string. - For two bracket strings and , is a bracket string.
Examples of bracket strings include (())(), (((()())())()), and ((()())()()())(()).
The value of a bracket string is determined as follows.
- The value of the empty string is 1.
- If the value of a bracket string is , then the value of
()is . - If the values of two bracket strings and are and , respectively, then the value of is .
For example, (())() represents 6.
()represents .(())represents .(())()represents .
Given a natural number , find the shortest bracket string that represents . If several such strings exist, find the one that comes first in lexicographic order.
Input
The first line contains the number of test cases . ()
From the second line on, each line contains one natural number . ()
Output
For each test case, print on its own line the shortest bracket string that represents . If several such strings exist, print the one that comes first in lexicographic order.