Under construction

Still being built. Things may change

Parentheses Order (Small)

Interview

Time limit5sMemory limit512 MB

Summary
Print the k-th valid parentheses string of n pairs in lexicographic order for each test case, or report that it does not exist.
Level

Medium5 of 10

Topics
Dynamic programming, Combinatorics
Solved
No attempts yet

Problem

A parentheses string of length 2n2n consists of nn opening brackets ( and nn closing brackets ).

A valid parentheses string is defined like this:

A string you can turn into the empty string by repeatedly erasing an adjacent () pair.

For example, (()) is valid. Erase the pair at positions 2 and 3 to get (), then erase that pair to get the empty string. )()( is not valid. Erasing the pair at positions 2 and 3 leaves )(, and nothing more can be erased.

Collect every valid parentheses string with nn opening and nn closing brackets, sort them in lexicographic order, and find the kk-th one. In this comparison ( comes before ).

For n=3n = 3 the valid parentheses strings in lexicographic order are:

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

Input

The first line has the number of test cases TT. Each of the next TT lines has two integers nn and kk separated by a space.

Output

For each test case print one line in the form Case #x: y. Here xx is the test case number starting from 1, and yy is the string that comes kk-th in lexicographic order among the valid parentheses strings with nn opening and nn closing brackets. If fewer than kk such strings exist, print Doesn't Exist! in place of yy.

Constraints

  • 1≤T≤1001 \le T \le 100
  • 1≤n≤101 \le n \le 10
  • 1≤k≤1000001 \le k \le 100000

Examples2

  1. Example 1

    Input
    3
    2 2
    3 4
    3 6
    
    Expected output
    Case #1: ()()
    Case #2: ()(())
    Case #3: Doesn't Exist!
    
  2. Example 2

    Input
    1
    1 1
    
    Expected output
    Case #1: ()