Unrank pairs of parentheses P20584


Statement
 

pdf   zip

In general, there are many ways to place nn pairs of parentheses correctly. For instance, these are just a few of the 42 ways for n=5n = 5:

()()()()() ()(())(()) (()())()() (((()()))) ((((()))))

The following rules inductively define all the correct strings made up with parentheses:

  • The empty string is correct.

  • All correct non-empty strings are of the kind $\mbox{\texttt{(}}x \mbox{\texttt{)}}y$, where xx and yy are correct strings.

Let |s|\vert s \vert denote the length of a string ss. We can define as follows a total order among the correct strings with parentheses:

  • The empty string is smaller than any non-empty string.

  • Given two non-empty strings $s_1 = \mbox{\texttt{(}}x_1 \mbox{\texttt{)}}y_1$ and $s_2 = \mbox{\texttt{(}}x_2 \mbox{\texttt{)}}y_2$,  s1s_1 is smaller than s2s_2 if and only if:

    • |s1|<|s2|\vert s_1 \vert < \vert s_2 \vert,

    • or |s1|=|s2|\vert s_1 \vert = \vert s_2 \vert and x1x_1 is smaller than x2x_2,

    • or |s1|=|s2|\vert s_1 \vert = \vert s_2 \vert,  x1=x2x_1 = x_2 and y1y_1 is smaller than y2y_2.

Can you write a program to compute the ii-th correct string with nn pairs of parentheses?

Input

Input consists of several cases, each one with two numbers ii and nn. Assume 0n300 \le n \le 30 and that ii is between 1 and the number of correct strings with nn pairs of parentheses.

Output

For every case, print the ii-th correct string with nn pairs of parentheses.

Public test cases
  • Input

    1 3
    2 3
    3 3
    4 3
    5 3
    70 6
    1 0
    1 30
    2000000000000000 30
    3814986502092304 30
    

    Output

    ()()()
    ()(())
    (())()
    (()())
    ((()))
    (()(()))(())
    
    ()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()
    (((())()(())(()))(()())((()()(()))()))(((())(((()(()))))()))
    (((((((((((((((((((((((((((((())))))))))))))))))))))))))))))
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Official solutions
    C++
    User solutions
    C++