Codeforces847c sum of nestings

阅读全文大概需要 3分钟
本文总阅读量
Posted by yjjr's blog on February 6, 2018

标签:dfs

Recallthat the bracket sequence is considered regular if it is possible to insertsymbols '+' and '1' into it so that the result is a correctarithmetic expression. For example, a sequence "(()())" is regular, because we can get correct arithmeticexpression insering symbols '+' and '1': "((1+1)+(1+1))". Also the following sequences are regular: "()()()", "(())" and "()". The following sequences are not regular bracket sequences: ")(", "(()" and "())(()".

In thisproblem you are given two integers n and k. Your task is to construct a regularbracket sequence consisting of round brackets with length 2·n with total sum ofnesting of all opening brackets equals to exactly k. The nesting of a single opening bracket equals to thenumber of pairs of brackets in which current opening bracket is embedded.

Forexample, in the sequence "()(())" thenesting of first opening bracket equals to0, the nesting of the second opening bracket equals to 0 and the nesting of the third openingbracket equal to 1. So the total sum ofnestings equals to 1.

Input

The firstline contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ 1018) —the number of opening brackets and needed total nesting.

Output

Print therequired regular bracket sequence consisting of round brackets.

If thereis no solution print "Impossible"(without quotes).

Example

Input

3 1

Output

()(())

Input

4 6

Output

(((())))

Input

2 5

Output

Impossible

Note

The firstexample is examined in the statement.

In thesecond example the answer is "(((())))".The nesting of the first opening bracket is 0, the nesting of the second is 1, the nesting of the third is 2, the nesting of fourth is 3. So the total sum of nestings equals to 0 + 1 + 2 + 3 = 6.

In thethird it is impossible to construct a regular bracket sequence, because themaximum possible total sum of nestings for two opening brackets equals to 1. This total sum of nestings is obtained forthe sequence "(())".

直接递归搜索即可,注意要先搜索是否可行,如果不行的话输出impossible,最后再输出

Code

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define LL long long
using namespace std;

long long n,k;
int cnt[500005];

void f(int x,int y)
{
	if(x==y+1)return;
	printf("(");
	f(x+1,y);
	printf(")");
	while(cnt[x]--)printf("()");
}
int main()
{
	long long base=0;
    cin>>n>>k;
    rep(i,0,n-1){
    	base+=i;
    	if(base<=k&&k<=base+(n-i-1)*i){
    		k-=base;
    		n-=i+1;
    		dep(x,i,0)
    			while(n&&k-x>=0){
    				cnt[x]++;
    				k-=x;
    				n--;
    			}	
    	f(0,i);
    	return 0;
        }
    }
    printf("Impossible\n");
    return 0;
}




本文可以转载,但必须附上原文链接,否则你会终生找不到妹子!!!欢迎关注我的CSDN: ahyjjr