Codeforces847C Sum of Nestings

45 篇文章 0 订阅
20 篇文章 0 订阅

标签: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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值