#P16485. [GKS 2014 #B] Parentheses Order

    ID: 16470 Type: RemoteJudge 1000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 6 Uploaded By: Tags>动态规划 DP2014Google Kick Start

[GKS 2014 #B] Parentheses Order

题目描述

An nn parentheses sequence consists of nn ( s and nn ) s.

A valid parentheses sequence is defined as the following:

You can find a way to repeat erasing adjacent pair of parentheses () until it becomes empty.

For example, ( ( ) ) is a valid parentheses, you can erase the pair on the 22nd and 33rd position and it becomes () then you can make it empty.

) ( ) ( is not a valid parentheses, after you erase the pair on the 22nd and 33rd position, it becomes ) ( and you cannot erase any more.

Now, we have all valid nn parentheses sequences. Find the k\mathbf{k}-th smallest sequence in lexicographical order.

For example, here are all valid 33 parentheses sequences in lexicographical order:

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

输入格式

The first line of the input gives the number of test cases, TT. TT lines follow. Each line represents a test case consisting of 22 integers, nn and kk.

输出格式

For each test case, output one line containing "Case #x: y", where xx is the test case number (starting from 1) and yy is the k\mathbf{k}-th smallest parentheses sequence in all valid nn parentheses sequences. Output "Doesn't Exist!" when there are less than k\mathbf{k} different nn parentheses sequences.

3
2 2
3 4
3 6
Case #1: ()()
Case #2: ()(())
Case #3: Doesn't Exist!

提示

Limits

1T1001 \le T \le 100.

Small dataset (Test set 1 - Visible)

1n101 \le n \le 10.

1k1000001 \le k \le 100000.

Large dataset (Test set 2 - Hidden)

1n1001 \le n \le 100.

1k10181 \le k \le 10^{18}.