Almost correct bracket sequence
Practice
3.9 (15 votes)
Applications of dynamic programming
Algorithms
Dynamic programming
Dynamic programming
Problem
84% Success 1496 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

The correct bracket sequence (also known as a balanced bracket sequence or correct parenthesis sequence) is a string consisting of only brackets \(("("\) and \(")")\) such that this sequence when inserted certain numbers and mathematical operations give a valid mathematical expression. For instance, \((())()\) is a correct bracket sequence and \(1+(2-(3*4)/6)+(7-8)\) is one of the possible expressions that can be obtained from it but \(())\) is not a correct bracket sequence. An empty string is a correct bracket sequence as well.

An almost correct bracket sequence is a string that consists of only brackets that can be turned into a correct bracket sequence by removing exactly one bracket from it. For instance, \(())\) is an almost correct bracket sequence because removing any of closing brackets turns it into a correct bracket sequence \(()\) but \((((\) is not an almost correct bracket sequence. Obviously, the length of each almost correct bracket sequence is an odd number.

You are given two integers \(n\) and \(k\). Consider all almost correct bracket sequences of length \(n\) and order these sequences lexicographically assuming that \((\ <\ )\)). Determine the \(k^{th}\) sequence in the ordering sequence or report that the number of almost correct bracket sequences of length \(n\) is less than \(k\).

Input format

  • First line: \(T\) denoting the number of test cases
  • Next \(T\) lines: Two integers \(n\) and \(k\) the length of the required almost correct bracket sequence and its position in lexicographic order

Output format

For each test case, print the answer in a separate line. If there are less than \(k\) almost correct bracket sequences of length \(n\), then print Impossible. Otherwise, print the required sequence.

Constraints

\(1 \le T \le 5000\)

\(1 \le n \le 65\)\(n\) is odd

\(1 \le k \le 2^{63} - 1\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
5 votes
Tags:
Ad-HocAlgorithmsMediumDynamic ProgrammingDynamic programming
Points:30
5 votes
Tags:
Applications of Dynamic ProgrammingAlgorithmsDynamic ProgrammingMath
Points:30
1 votes
Tags:
Medium