message stringlengths 2 15.4k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 45 107k | cluster float64 21 21 | __index_level_0__ int64 90 214k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Recall that the bracket sequence is considered regular if it is possible to insert symbols '+' and '1' into it so that the result is a correct arithmetic expression. For example, a sequence "(()())" is regular, because we can get correct arithmetic expression 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 this problem you are given two integers n and k. Your task is to construct a regular bracket sequence consisting of round brackets with length 2·n with total sum of nesting of all opening brackets equals to exactly k. The nesting of a single opening bracket equals to the number of pairs of brackets in which current opening bracket is embedded.
For example, in the sequence "()(())" the nesting of first opening bracket equals to 0, the nesting of the second opening bracket equals to 0 and the nesting of the third opening bracket equal to 1. So the total sum of nestings equals to 1.
Input
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ 1018) — the number of opening brackets and needed total nesting.
Output
Print the required regular bracket sequence consisting of round brackets.
If there is no solution print "Impossible" (without quotes).
Examples
Input
3 1
Output
()(())
Input
4 6
Output
(((())))
Input
2 5
Output
Impossible
Note
The first example is examined in the statement.
In the second 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 the third it is impossible to construct a regular bracket sequence, because the maximum possible total sum of nestings for two opening brackets equals to 1. This total sum of nestings is obtained for the sequence "(())". | instruction | 0 | 20,865 | 21 | 41,730 |
Tags: constructive algorithms
Correct Solution:
```
from math import *
n,k=map(int,input().split())
if n*(n-1)//2<k:
print("Impossible")
else:
li=[]
temp=int((-1+sqrt(8*k+1))//2)
k-=(temp*(temp+1))//2
li+=[temp]
for i in range(temp):
cur=temp-i
if cur<=k and cur>0:
li+=[cur]*(k//cur)
k-=(k//cur)*cur
if k==0 or cur==0:
break
li.reverse()
num=li[0]
ans=""
for i in range(num+1):
ans+="("
n-=num+1
ans+=")"
for i in range(1,len(li)):
num=li[i]-li[i-1]+1
for j in range(num):
ans+="("
n-=num
ans+=")"
ans+=")"*li[-1]
if n<0 or k>0:
print("Impossible")
else:
for i in range(n):
ans+="()"
print(ans)
``` | output | 1 | 20,865 | 21 | 41,731 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recall that the bracket sequence is considered regular if it is possible to insert symbols '+' and '1' into it so that the result is a correct arithmetic expression. For example, a sequence "(()())" is regular, because we can get correct arithmetic expression 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 this problem you are given two integers n and k. Your task is to construct a regular bracket sequence consisting of round brackets with length 2·n with total sum of nesting of all opening brackets equals to exactly k. The nesting of a single opening bracket equals to the number of pairs of brackets in which current opening bracket is embedded.
For example, in the sequence "()(())" the nesting of first opening bracket equals to 0, the nesting of the second opening bracket equals to 0 and the nesting of the third opening bracket equal to 1. So the total sum of nestings equals to 1.
Input
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ 1018) — the number of opening brackets and needed total nesting.
Output
Print the required regular bracket sequence consisting of round brackets.
If there is no solution print "Impossible" (without quotes).
Examples
Input
3 1
Output
()(())
Input
4 6
Output
(((())))
Input
2 5
Output
Impossible
Note
The first example is examined in the statement.
In the second 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 the third it is impossible to construct a regular bracket sequence, because the maximum possible total sum of nestings for two opening brackets equals to 1. This total sum of nestings is obtained for the sequence "(())". | instruction | 0 | 20,866 | 21 | 41,732 |
Tags: constructive algorithms
Correct Solution:
```
ch=input()
l=ch.split(' ')
n=int(l[0])
k=int(l[1])
l1='('
l2=')'
l3='()'
s=0
mi=0
ch=''
from math import sqrt
q=int(sqrt(2*k))
while k>=(q*(q-1))/2:
q=q+1
q=q-1
d=int(k-(q*(q-1))/2)
mi=q
i=0
if d!=0:
mi=q+1
i=1
if n<mi:
print('Impossible')
else:
c=n-mi
print(c*l3+d*l1+i*l3+(q-d)*l1+q*l2)
``` | output | 1 | 20,866 | 21 | 41,733 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recall that the bracket sequence is considered regular if it is possible to insert symbols '+' and '1' into it so that the result is a correct arithmetic expression. For example, a sequence "(()())" is regular, because we can get correct arithmetic expression 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 this problem you are given two integers n and k. Your task is to construct a regular bracket sequence consisting of round brackets with length 2·n with total sum of nesting of all opening brackets equals to exactly k. The nesting of a single opening bracket equals to the number of pairs of brackets in which current opening bracket is embedded.
For example, in the sequence "()(())" the nesting of first opening bracket equals to 0, the nesting of the second opening bracket equals to 0 and the nesting of the third opening bracket equal to 1. So the total sum of nestings equals to 1.
Input
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ 1018) — the number of opening brackets and needed total nesting.
Output
Print the required regular bracket sequence consisting of round brackets.
If there is no solution print "Impossible" (without quotes).
Examples
Input
3 1
Output
()(())
Input
4 6
Output
(((())))
Input
2 5
Output
Impossible
Note
The first example is examined in the statement.
In the second 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 the third it is impossible to construct a regular bracket sequence, because the maximum possible total sum of nestings for two opening brackets equals to 1. This total sum of nestings is obtained for the sequence "(())".
Submitted Solution:
```
# Legends Always Come Up with Solution
# Author: Manvir Singh
import os
import sys
from io import BytesIO, IOBase
from collections import Counter
from bisect import *
from math import gcd
from itertools import permutations,combinations
from math import sqrt,ceil,floor
def main():
n,k=map(int,input().split())
z=floor((-1+sqrt(1+8*k))/2)
f=int((z*(z+1))//2!=k)
if z+f>n-1:
print("Impossible")
else:
if f:
x=z+1
y=z+1-(k-(z*(z+1)//2))
print("("*x+")"*y+"("+")"*(x+1-y)+"()"*(n-(x+1)))
else:
if z<n-1:
print("("*(z+1)+")"*(z+1)+"()"*(n-1-z))
else:
print("("*n+")"*n)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main()
``` | instruction | 0 | 20,867 | 21 | 41,734 |
Yes | output | 1 | 20,867 | 21 | 41,735 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recall that the bracket sequence is considered regular if it is possible to insert symbols '+' and '1' into it so that the result is a correct arithmetic expression. For example, a sequence "(()())" is regular, because we can get correct arithmetic expression 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 this problem you are given two integers n and k. Your task is to construct a regular bracket sequence consisting of round brackets with length 2·n with total sum of nesting of all opening brackets equals to exactly k. The nesting of a single opening bracket equals to the number of pairs of brackets in which current opening bracket is embedded.
For example, in the sequence "()(())" the nesting of first opening bracket equals to 0, the nesting of the second opening bracket equals to 0 and the nesting of the third opening bracket equal to 1. So the total sum of nestings equals to 1.
Input
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ 1018) — the number of opening brackets and needed total nesting.
Output
Print the required regular bracket sequence consisting of round brackets.
If there is no solution print "Impossible" (without quotes).
Examples
Input
3 1
Output
()(())
Input
4 6
Output
(((())))
Input
2 5
Output
Impossible
Note
The first example is examined in the statement.
In the second 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 the third it is impossible to construct a regular bracket sequence, because the maximum possible total sum of nestings for two opening brackets equals to 1. This total sum of nestings is obtained for the sequence "(())".
Submitted Solution:
```
def step(n, k):
count = min(n, int((1 + (1 + 8 * k) ** 0.5) / 2))
return count
def nest(n, k):
accum = ""
count = step(n, k)
leftover = int(k - (((count - 1) * count) / 2))
n -= count
if leftover == 0:
accum += count * "("
accum += count * ")"
return accum + n * "()"
if n == 0:
return "Impossible"
pos = count - leftover
accum += count * "("
accum += pos * ")"
accum += "()"
accum += (count - pos) * ")"
n -= 1
accum += n * "()"
return accum
N, K = [int(x) for x in input().split()]
print(nest(N, K))
``` | instruction | 0 | 20,868 | 21 | 41,736 |
Yes | output | 1 | 20,868 | 21 | 41,737 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recall that the bracket sequence is considered regular if it is possible to insert symbols '+' and '1' into it so that the result is a correct arithmetic expression. For example, a sequence "(()())" is regular, because we can get correct arithmetic expression 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 this problem you are given two integers n and k. Your task is to construct a regular bracket sequence consisting of round brackets with length 2·n with total sum of nesting of all opening brackets equals to exactly k. The nesting of a single opening bracket equals to the number of pairs of brackets in which current opening bracket is embedded.
For example, in the sequence "()(())" the nesting of first opening bracket equals to 0, the nesting of the second opening bracket equals to 0 and the nesting of the third opening bracket equal to 1. So the total sum of nestings equals to 1.
Input
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ 1018) — the number of opening brackets and needed total nesting.
Output
Print the required regular bracket sequence consisting of round brackets.
If there is no solution print "Impossible" (without quotes).
Examples
Input
3 1
Output
()(())
Input
4 6
Output
(((())))
Input
2 5
Output
Impossible
Note
The first example is examined in the statement.
In the second 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 the third it is impossible to construct a regular bracket sequence, because the maximum possible total sum of nestings for two opening brackets equals to 1. This total sum of nestings is obtained for the sequence "(())".
Submitted Solution:
```
n, k = map(int, input().split())
k_now, level, n_now = 0, 0, 0
ans = list()
if k > n * (n - 1) / 2:
print('Impossible')
else:
while n_now < 2 * n:
if k_now + level <= k:
ans.append('(')
k_now += level
level += 1
else:
ans.append(')')
level -= 1
n_now += 1
print(*ans, sep='')
``` | instruction | 0 | 20,869 | 21 | 41,738 |
Yes | output | 1 | 20,869 | 21 | 41,739 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recall that the bracket sequence is considered regular if it is possible to insert symbols '+' and '1' into it so that the result is a correct arithmetic expression. For example, a sequence "(()())" is regular, because we can get correct arithmetic expression 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 this problem you are given two integers n and k. Your task is to construct a regular bracket sequence consisting of round brackets with length 2·n with total sum of nesting of all opening brackets equals to exactly k. The nesting of a single opening bracket equals to the number of pairs of brackets in which current opening bracket is embedded.
For example, in the sequence "()(())" the nesting of first opening bracket equals to 0, the nesting of the second opening bracket equals to 0 and the nesting of the third opening bracket equal to 1. So the total sum of nestings equals to 1.
Input
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ 1018) — the number of opening brackets and needed total nesting.
Output
Print the required regular bracket sequence consisting of round brackets.
If there is no solution print "Impossible" (without quotes).
Examples
Input
3 1
Output
()(())
Input
4 6
Output
(((())))
Input
2 5
Output
Impossible
Note
The first example is examined in the statement.
In the second 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 the third it is impossible to construct a regular bracket sequence, because the maximum possible total sum of nestings for two opening brackets equals to 1. This total sum of nestings is obtained for the sequence "(())".
Submitted Solution:
```
import sys
input = sys.stdin.buffer.readline
def ints():
return map(int, input().split())
n, k=ints()
if k > (n-1)*n//2:
print("Impossible")
exit(0)
sum=0
ans=''
for i in range(n+1):
if sum+i>k:
ans+='('*(i)
ans+=')'*(i-(k-sum))
if i!=n:
ans+='('
ans+=')'*(k-sum+1)
ans+='()'*(n-i-1)
else:
ans+=')'*(k-sum)
break
sum+=i
print(ans)
``` | instruction | 0 | 20,870 | 21 | 41,740 |
Yes | output | 1 | 20,870 | 21 | 41,741 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recall that the bracket sequence is considered regular if it is possible to insert symbols '+' and '1' into it so that the result is a correct arithmetic expression. For example, a sequence "(()())" is regular, because we can get correct arithmetic expression 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 this problem you are given two integers n and k. Your task is to construct a regular bracket sequence consisting of round brackets with length 2·n with total sum of nesting of all opening brackets equals to exactly k. The nesting of a single opening bracket equals to the number of pairs of brackets in which current opening bracket is embedded.
For example, in the sequence "()(())" the nesting of first opening bracket equals to 0, the nesting of the second opening bracket equals to 0 and the nesting of the third opening bracket equal to 1. So the total sum of nestings equals to 1.
Input
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ 1018) — the number of opening brackets and needed total nesting.
Output
Print the required regular bracket sequence consisting of round brackets.
If there is no solution print "Impossible" (without quotes).
Examples
Input
3 1
Output
()(())
Input
4 6
Output
(((())))
Input
2 5
Output
Impossible
Note
The first example is examined in the statement.
In the second 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 the third it is impossible to construct a regular bracket sequence, because the maximum possible total sum of nestings for two opening brackets equals to 1. This total sum of nestings is obtained for the sequence "(())".
Submitted Solution:
```
ch=input()
l=ch.split(' ')
n=int(l[0])
k=int(l[1])
l1=['(']
l2=[')']
l3=['()']
s=0
mi=0
ch=''
while (k-s)!=0:
d=k-s
q=1
while d>=(q*(q-1))/2:
q=q+1
q=q-1
li1=q*l1
li2=q*l2
ch=ch+''.join(li1)+''.join(li2)
s=s+q*(q-1)/2
mi=mi+q
if n<mi:
print('impossible')
else:
c=n-mi
li3=c*l3
ch=''.join(li3)+ch
print(ch)
``` | instruction | 0 | 20,871 | 21 | 41,742 |
No | output | 1 | 20,871 | 21 | 41,743 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recall that the bracket sequence is considered regular if it is possible to insert symbols '+' and '1' into it so that the result is a correct arithmetic expression. For example, a sequence "(()())" is regular, because we can get correct arithmetic expression 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 this problem you are given two integers n and k. Your task is to construct a regular bracket sequence consisting of round brackets with length 2·n with total sum of nesting of all opening brackets equals to exactly k. The nesting of a single opening bracket equals to the number of pairs of brackets in which current opening bracket is embedded.
For example, in the sequence "()(())" the nesting of first opening bracket equals to 0, the nesting of the second opening bracket equals to 0 and the nesting of the third opening bracket equal to 1. So the total sum of nestings equals to 1.
Input
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ 1018) — the number of opening brackets and needed total nesting.
Output
Print the required regular bracket sequence consisting of round brackets.
If there is no solution print "Impossible" (without quotes).
Examples
Input
3 1
Output
()(())
Input
4 6
Output
(((())))
Input
2 5
Output
Impossible
Note
The first example is examined in the statement.
In the second 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 the third it is impossible to construct a regular bracket sequence, because the maximum possible total sum of nestings for two opening brackets equals to 1. This total sum of nestings is obtained for the sequence "(())".
Submitted Solution:
```
n, k = map(int, input().split())
s = int((1 + 4 * k) ** 0.5) + 5 >> 1
k -= s * s - s >> 1
t = '(' * s + ')' * s
n -= s
if k:
t = t[:k] + '()' + t[k:]
n -= 1
print('Impossible' if n < 0 else t + '()' * n)
``` | instruction | 0 | 20,872 | 21 | 41,744 |
No | output | 1 | 20,872 | 21 | 41,745 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recall that the bracket sequence is considered regular if it is possible to insert symbols '+' and '1' into it so that the result is a correct arithmetic expression. For example, a sequence "(()())" is regular, because we can get correct arithmetic expression 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 this problem you are given two integers n and k. Your task is to construct a regular bracket sequence consisting of round brackets with length 2·n with total sum of nesting of all opening brackets equals to exactly k. The nesting of a single opening bracket equals to the number of pairs of brackets in which current opening bracket is embedded.
For example, in the sequence "()(())" the nesting of first opening bracket equals to 0, the nesting of the second opening bracket equals to 0 and the nesting of the third opening bracket equal to 1. So the total sum of nestings equals to 1.
Input
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ 1018) — the number of opening brackets and needed total nesting.
Output
Print the required regular bracket sequence consisting of round brackets.
If there is no solution print "Impossible" (without quotes).
Examples
Input
3 1
Output
()(())
Input
4 6
Output
(((())))
Input
2 5
Output
Impossible
Note
The first example is examined in the statement.
In the second 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 the third it is impossible to construct a regular bracket sequence, because the maximum possible total sum of nestings for two opening brackets equals to 1. This total sum of nestings is obtained for the sequence "(())".
Submitted Solution:
```
from math import *
n,k=map(int,input().split())
if n*(n-1)//2<k:
print("Impossible")
else:
li=[]
temp=int((-1+sqrt(8*k+1))//2)
k-=(temp*(temp+1))//2
li+=[temp]
for i in range(temp):
cur=temp-i
if cur<=k and cur>0:
li+=[cur]*(k//cur)
k-=(k//cur)*cur
if k==0 or cur==0:
break
li.reverse()
num=li[0]
ans=""
for i in range(num+1):
ans+="("
n-=num+1
ans+=")"
for i in range(1,len(li)):
num=li[i]-li[i-1]+1
for j in range(num):
ans+="("
n-=num
ans+=")"
ans+=")"*li[-1]
if n<0 or k>0:
print("Impossible")
else:
for i in range(n):
ans+="()"
print(ans)
``` | instruction | 0 | 20,873 | 21 | 41,746 |
No | output | 1 | 20,873 | 21 | 41,747 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recall that the bracket sequence is considered regular if it is possible to insert symbols '+' and '1' into it so that the result is a correct arithmetic expression. For example, a sequence "(()())" is regular, because we can get correct arithmetic expression 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 this problem you are given two integers n and k. Your task is to construct a regular bracket sequence consisting of round brackets with length 2·n with total sum of nesting of all opening brackets equals to exactly k. The nesting of a single opening bracket equals to the number of pairs of brackets in which current opening bracket is embedded.
For example, in the sequence "()(())" the nesting of first opening bracket equals to 0, the nesting of the second opening bracket equals to 0 and the nesting of the third opening bracket equal to 1. So the total sum of nestings equals to 1.
Input
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ 1018) — the number of opening brackets and needed total nesting.
Output
Print the required regular bracket sequence consisting of round brackets.
If there is no solution print "Impossible" (without quotes).
Examples
Input
3 1
Output
()(())
Input
4 6
Output
(((())))
Input
2 5
Output
Impossible
Note
The first example is examined in the statement.
In the second 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 the third it is impossible to construct a regular bracket sequence, because the maximum possible total sum of nestings for two opening brackets equals to 1. This total sum of nestings is obtained for the sequence "(())".
Submitted Solution:
```
n, k = map(int, input().split())
max_nesting = n/2 * (n-1)
nesting = [i for i in range(n)]
m = n-1
add = 0
s = sum(nesting)
if max_nesting < k:
print("Impossible")
else:
while not s == k:
if s > k:
s -= m
m -= 1
else:
add = k-s
s += add
res = "(" * (m+1) + ")" * (m-add+1) + ("(" + ")" * (add+1)) + "()" * (n - m - add-1)
print(res)
``` | instruction | 0 | 20,874 | 21 | 41,748 |
No | output | 1 | 20,874 | 21 | 41,749 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are regular bracket sequences; "))" and ")((" are bracket sequences (but not regular ones), and "(a)" and "(1)+(1)" are not bracket sequences at all.
You have a number of strings; each string is a bracket sequence of length 2. So, overall you have cnt_1 strings "((", cnt_2 strings "()", cnt_3 strings ")(" and cnt_4 strings "))". You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length 2(cnt_1 + cnt_2 + cnt_3 + cnt_4). You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.
Input
The input consists of four lines, i-th of them contains one integer cnt_i (0 ≤ cnt_i ≤ 10^9).
Output
Print one integer: 1 if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, 0 otherwise.
Examples
Input
3
1
4
3
Output
1
Input
0
0
0
0
Output
1
Input
1
2
3
4
Output
0
Note
In the first example it is possible to construct a string "(())()(()((()()()())))", which is a regular bracket sequence.
In the second example it is possible to construct a string "", which is a regular bracket sequence. | instruction | 0 | 21,229 | 21 | 42,458 |
Tags: greedy, implementation
Correct Solution:
```
"""
NTC here
"""
iin=lambda :int(input())
lin=lambda :list(map(int,input().split()))
a=[iin() for i in range(4)]
if a[0]==a[3]==0 and a[2]!=0:print(0)
elif a[0]==a[3]:print(1)
else:print(0)
``` | output | 1 | 21,229 | 21 | 42,459 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are regular bracket sequences; "))" and ")((" are bracket sequences (but not regular ones), and "(a)" and "(1)+(1)" are not bracket sequences at all.
You have a number of strings; each string is a bracket sequence of length 2. So, overall you have cnt_1 strings "((", cnt_2 strings "()", cnt_3 strings ")(" and cnt_4 strings "))". You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length 2(cnt_1 + cnt_2 + cnt_3 + cnt_4). You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.
Input
The input consists of four lines, i-th of them contains one integer cnt_i (0 ≤ cnt_i ≤ 10^9).
Output
Print one integer: 1 if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, 0 otherwise.
Examples
Input
3
1
4
3
Output
1
Input
0
0
0
0
Output
1
Input
1
2
3
4
Output
0
Note
In the first example it is possible to construct a string "(())()(()((()()()())))", which is a regular bracket sequence.
In the second example it is possible to construct a string "", which is a regular bracket sequence. | instruction | 0 | 21,230 | 21 | 42,460 |
Tags: greedy, implementation
Correct Solution:
```
a = int(input())
b = int(input())
c = int(input())
d = int(input())
if a != d or (a == 0 and c != 0):
print(0)
else:
print(1)
``` | output | 1 | 21,230 | 21 | 42,461 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are regular bracket sequences; "))" and ")((" are bracket sequences (but not regular ones), and "(a)" and "(1)+(1)" are not bracket sequences at all.
You have a number of strings; each string is a bracket sequence of length 2. So, overall you have cnt_1 strings "((", cnt_2 strings "()", cnt_3 strings ")(" and cnt_4 strings "))". You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length 2(cnt_1 + cnt_2 + cnt_3 + cnt_4). You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.
Input
The input consists of four lines, i-th of them contains one integer cnt_i (0 ≤ cnt_i ≤ 10^9).
Output
Print one integer: 1 if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, 0 otherwise.
Examples
Input
3
1
4
3
Output
1
Input
0
0
0
0
Output
1
Input
1
2
3
4
Output
0
Note
In the first example it is possible to construct a string "(())()(()((()()()())))", which is a regular bracket sequence.
In the second example it is possible to construct a string "", which is a regular bracket sequence. | instruction | 0 | 21,231 | 21 | 42,462 |
Tags: greedy, implementation
Correct Solution:
```
'''
t= input()
lng= len(t)
li1=[]; li2=[]
for i in range(lng):
if t[i]!='a':
li1.append(t[i])
elif t[i]=='a':
li2.append(i)
aa= ''.join(li1)
if len(aa)==0:
print(t); exit(0)
if len(aa)%2==1:
print(':('); exit(0)
if len(aa)%2==0:
#print(123)
l= int(len(aa)/2); lp= l#; print(l)
ll= aa[l:]; lll=aa[0:l] #; print(ll)
if ll!=lll:
print(':('); exit(0)
if ll not in t:
print(':('); exit(0)
tp= t[::-1]; tc= ll[::-1]#; print(tp,tc)
if tp.find(tc)!=0:
print(':('); exit(0)
if tp.find(tc)==0:
ul= len(tc)
lu= tp[ul:][::-1]
print(lu); exit(0)
'''
a= int(input()); b= int(input()); c= int(input()); d= int(input()); sv=0; vs=0
if a+b+c==b+c+d:
sv=1
if sv==1:
if a+d<1 and c>0:
sv=0
print(sv)
``` | output | 1 | 21,231 | 21 | 42,463 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are regular bracket sequences; "))" and ")((" are bracket sequences (but not regular ones), and "(a)" and "(1)+(1)" are not bracket sequences at all.
You have a number of strings; each string is a bracket sequence of length 2. So, overall you have cnt_1 strings "((", cnt_2 strings "()", cnt_3 strings ")(" and cnt_4 strings "))". You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length 2(cnt_1 + cnt_2 + cnt_3 + cnt_4). You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.
Input
The input consists of four lines, i-th of them contains one integer cnt_i (0 ≤ cnt_i ≤ 10^9).
Output
Print one integer: 1 if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, 0 otherwise.
Examples
Input
3
1
4
3
Output
1
Input
0
0
0
0
Output
1
Input
1
2
3
4
Output
0
Note
In the first example it is possible to construct a string "(())()(()((()()()())))", which is a regular bracket sequence.
In the second example it is possible to construct a string "", which is a regular bracket sequence. | instruction | 0 | 21,232 | 21 | 42,464 |
Tags: greedy, implementation
Correct Solution:
```
cnt1 = int(input())
cnt2 = int(input())
cnt3 = int(input())
cnt4 = int(input())
if cnt1 != cnt4:
print(0)
elif cnt3 > 0 and cnt1 == 0:
print(0)
else:
print(1)
``` | output | 1 | 21,232 | 21 | 42,465 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are regular bracket sequences; "))" and ")((" are bracket sequences (but not regular ones), and "(a)" and "(1)+(1)" are not bracket sequences at all.
You have a number of strings; each string is a bracket sequence of length 2. So, overall you have cnt_1 strings "((", cnt_2 strings "()", cnt_3 strings ")(" and cnt_4 strings "))". You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length 2(cnt_1 + cnt_2 + cnt_3 + cnt_4). You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.
Input
The input consists of four lines, i-th of them contains one integer cnt_i (0 ≤ cnt_i ≤ 10^9).
Output
Print one integer: 1 if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, 0 otherwise.
Examples
Input
3
1
4
3
Output
1
Input
0
0
0
0
Output
1
Input
1
2
3
4
Output
0
Note
In the first example it is possible to construct a string "(())()(()((()()()())))", which is a regular bracket sequence.
In the second example it is possible to construct a string "", which is a regular bracket sequence. | instruction | 0 | 21,233 | 21 | 42,466 |
Tags: greedy, implementation
Correct Solution:
```
a=int(input())
b=int(input())
c=int(input())
d=int(input())
if(a!=d):
print(0)
elif(c>0 and (a==0 or d==0)):
print(0)
else:
print(1)
``` | output | 1 | 21,233 | 21 | 42,467 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are regular bracket sequences; "))" and ")((" are bracket sequences (but not regular ones), and "(a)" and "(1)+(1)" are not bracket sequences at all.
You have a number of strings; each string is a bracket sequence of length 2. So, overall you have cnt_1 strings "((", cnt_2 strings "()", cnt_3 strings ")(" and cnt_4 strings "))". You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length 2(cnt_1 + cnt_2 + cnt_3 + cnt_4). You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.
Input
The input consists of four lines, i-th of them contains one integer cnt_i (0 ≤ cnt_i ≤ 10^9).
Output
Print one integer: 1 if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, 0 otherwise.
Examples
Input
3
1
4
3
Output
1
Input
0
0
0
0
Output
1
Input
1
2
3
4
Output
0
Note
In the first example it is possible to construct a string "(())()(()((()()()())))", which is a regular bracket sequence.
In the second example it is possible to construct a string "", which is a regular bracket sequence. | instruction | 0 | 21,234 | 21 | 42,468 |
Tags: greedy, implementation
Correct Solution:
```
cnt = [int(input()) for _ in range(4)]
if cnt[0] != cnt[3]:
print(0)
elif cnt[2] > 0 and cnt[0] == 0:
print(0)
else:
print(1)
``` | output | 1 | 21,234 | 21 | 42,469 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are regular bracket sequences; "))" and ")((" are bracket sequences (but not regular ones), and "(a)" and "(1)+(1)" are not bracket sequences at all.
You have a number of strings; each string is a bracket sequence of length 2. So, overall you have cnt_1 strings "((", cnt_2 strings "()", cnt_3 strings ")(" and cnt_4 strings "))". You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length 2(cnt_1 + cnt_2 + cnt_3 + cnt_4). You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.
Input
The input consists of four lines, i-th of them contains one integer cnt_i (0 ≤ cnt_i ≤ 10^9).
Output
Print one integer: 1 if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, 0 otherwise.
Examples
Input
3
1
4
3
Output
1
Input
0
0
0
0
Output
1
Input
1
2
3
4
Output
0
Note
In the first example it is possible to construct a string "(())()(()((()()()())))", which is a regular bracket sequence.
In the second example it is possible to construct a string "", which is a regular bracket sequence. | instruction | 0 | 21,235 | 21 | 42,470 |
Tags: greedy, implementation
Correct Solution:
```
a=int(input())
b=int(input())
c=int(input())
d=int(input())
d=d*2
a=a*2
if a==d==0 and c>0:
print(0)
exit()
c=c%2
if c<=a and c<=d and a==d:
print(1)
else:
print(0)
``` | output | 1 | 21,235 | 21 | 42,471 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are regular bracket sequences; "))" and ")((" are bracket sequences (but not regular ones), and "(a)" and "(1)+(1)" are not bracket sequences at all.
You have a number of strings; each string is a bracket sequence of length 2. So, overall you have cnt_1 strings "((", cnt_2 strings "()", cnt_3 strings ")(" and cnt_4 strings "))". You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length 2(cnt_1 + cnt_2 + cnt_3 + cnt_4). You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.
Input
The input consists of four lines, i-th of them contains one integer cnt_i (0 ≤ cnt_i ≤ 10^9).
Output
Print one integer: 1 if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, 0 otherwise.
Examples
Input
3
1
4
3
Output
1
Input
0
0
0
0
Output
1
Input
1
2
3
4
Output
0
Note
In the first example it is possible to construct a string "(())()(()((()()()())))", which is a regular bracket sequence.
In the second example it is possible to construct a string "", which is a regular bracket sequence. | instruction | 0 | 21,236 | 21 | 42,472 |
Tags: greedy, implementation
Correct Solution:
```
a = int(input())
b = int(input())
c = int(input())
d = int(input())
if a == d and (c == 0 or a != 0):
print(1)
else:
print(0)
``` | output | 1 | 21,236 | 21 | 42,473 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are regular bracket sequences; "))" and ")((" are bracket sequences (but not regular ones), and "(a)" and "(1)+(1)" are not bracket sequences at all.
You have a number of strings; each string is a bracket sequence of length 2. So, overall you have cnt_1 strings "((", cnt_2 strings "()", cnt_3 strings ")(" and cnt_4 strings "))". You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length 2(cnt_1 + cnt_2 + cnt_3 + cnt_4). You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.
Input
The input consists of four lines, i-th of them contains one integer cnt_i (0 ≤ cnt_i ≤ 10^9).
Output
Print one integer: 1 if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, 0 otherwise.
Examples
Input
3
1
4
3
Output
1
Input
0
0
0
0
Output
1
Input
1
2
3
4
Output
0
Note
In the first example it is possible to construct a string "(())()(()((()()()())))", which is a regular bracket sequence.
In the second example it is possible to construct a string "", which is a regular bracket sequence.
Submitted Solution:
```
a = int(input())
b = int(input())
c = int(input())
d = int(input())
s = 1
if (c > 0):
if(a > 0) and (d > 0) and (a == d):
print(1)
else:
print(0)
else:
if a == d:
print(1)
else:
print(0)
``` | instruction | 0 | 21,237 | 21 | 42,474 |
Yes | output | 1 | 21,237 | 21 | 42,475 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are regular bracket sequences; "))" and ")((" are bracket sequences (but not regular ones), and "(a)" and "(1)+(1)" are not bracket sequences at all.
You have a number of strings; each string is a bracket sequence of length 2. So, overall you have cnt_1 strings "((", cnt_2 strings "()", cnt_3 strings ")(" and cnt_4 strings "))". You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length 2(cnt_1 + cnt_2 + cnt_3 + cnt_4). You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.
Input
The input consists of four lines, i-th of them contains one integer cnt_i (0 ≤ cnt_i ≤ 10^9).
Output
Print one integer: 1 if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, 0 otherwise.
Examples
Input
3
1
4
3
Output
1
Input
0
0
0
0
Output
1
Input
1
2
3
4
Output
0
Note
In the first example it is possible to construct a string "(())()(()((()()()())))", which is a regular bracket sequence.
In the second example it is possible to construct a string "", which is a regular bracket sequence.
Submitted Solution:
```
L = [int(input()) for _ in range(4)]
if L[0] == L[3]:
if L[0] > 0:
print(1)
else:
if L[2] == 0:
print(1)
else:
print(0)
else:
print(0)
``` | instruction | 0 | 21,238 | 21 | 42,476 |
Yes | output | 1 | 21,238 | 21 | 42,477 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are regular bracket sequences; "))" and ")((" are bracket sequences (but not regular ones), and "(a)" and "(1)+(1)" are not bracket sequences at all.
You have a number of strings; each string is a bracket sequence of length 2. So, overall you have cnt_1 strings "((", cnt_2 strings "()", cnt_3 strings ")(" and cnt_4 strings "))". You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length 2(cnt_1 + cnt_2 + cnt_3 + cnt_4). You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.
Input
The input consists of four lines, i-th of them contains one integer cnt_i (0 ≤ cnt_i ≤ 10^9).
Output
Print one integer: 1 if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, 0 otherwise.
Examples
Input
3
1
4
3
Output
1
Input
0
0
0
0
Output
1
Input
1
2
3
4
Output
0
Note
In the first example it is possible to construct a string "(())()(()((()()()())))", which is a regular bracket sequence.
In the second example it is possible to construct a string "", which is a regular bracket sequence.
Submitted Solution:
```
c1=int(input())
c2=int(input())
c3=int(input())
c4=int(input())
if c1==0 and c4==0 and c3!=0:
print(0)
exit()
if c1==0 and c2==0 and c3!=0:
print(0)
exit()
if c1==c4:
print(1)
exit()
else:
print(0)
exit()
``` | instruction | 0 | 21,239 | 21 | 42,478 |
Yes | output | 1 | 21,239 | 21 | 42,479 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are regular bracket sequences; "))" and ")((" are bracket sequences (but not regular ones), and "(a)" and "(1)+(1)" are not bracket sequences at all.
You have a number of strings; each string is a bracket sequence of length 2. So, overall you have cnt_1 strings "((", cnt_2 strings "()", cnt_3 strings ")(" and cnt_4 strings "))". You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length 2(cnt_1 + cnt_2 + cnt_3 + cnt_4). You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.
Input
The input consists of four lines, i-th of them contains one integer cnt_i (0 ≤ cnt_i ≤ 10^9).
Output
Print one integer: 1 if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, 0 otherwise.
Examples
Input
3
1
4
3
Output
1
Input
0
0
0
0
Output
1
Input
1
2
3
4
Output
0
Note
In the first example it is possible to construct a string "(())()(()((()()()())))", which is a regular bracket sequence.
In the second example it is possible to construct a string "", which is a regular bracket sequence.
Submitted Solution:
```
n=int(input())
m=int(input())
k=int(input())
b=int(input())
if(n==b):
if(k!=0 and n==0):
print(0)
else:
print(1)
else:
print(0)
``` | instruction | 0 | 21,240 | 21 | 42,480 |
Yes | output | 1 | 21,240 | 21 | 42,481 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are regular bracket sequences; "))" and ")((" are bracket sequences (but not regular ones), and "(a)" and "(1)+(1)" are not bracket sequences at all.
You have a number of strings; each string is a bracket sequence of length 2. So, overall you have cnt_1 strings "((", cnt_2 strings "()", cnt_3 strings ")(" and cnt_4 strings "))". You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length 2(cnt_1 + cnt_2 + cnt_3 + cnt_4). You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.
Input
The input consists of four lines, i-th of them contains one integer cnt_i (0 ≤ cnt_i ≤ 10^9).
Output
Print one integer: 1 if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, 0 otherwise.
Examples
Input
3
1
4
3
Output
1
Input
0
0
0
0
Output
1
Input
1
2
3
4
Output
0
Note
In the first example it is possible to construct a string "(())()(()((()()()())))", which is a regular bracket sequence.
In the second example it is possible to construct a string "", which is a regular bracket sequence.
Submitted Solution:
```
t1 = '(('
t2 = '()'
t3 = ')('
t4 = '))'
cnt1 = int(input())
cnt2 = int(input())
cnt3 = int(input())
cnt4 = int(input())
ans = 1
if cnt1 != cnt4 or (cnt2 > 0 and cnt1 == 0):
ans = 0
print(ans)
``` | instruction | 0 | 21,241 | 21 | 42,482 |
No | output | 1 | 21,241 | 21 | 42,483 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are regular bracket sequences; "))" and ")((" are bracket sequences (but not regular ones), and "(a)" and "(1)+(1)" are not bracket sequences at all.
You have a number of strings; each string is a bracket sequence of length 2. So, overall you have cnt_1 strings "((", cnt_2 strings "()", cnt_3 strings ")(" and cnt_4 strings "))". You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length 2(cnt_1 + cnt_2 + cnt_3 + cnt_4). You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.
Input
The input consists of four lines, i-th of them contains one integer cnt_i (0 ≤ cnt_i ≤ 10^9).
Output
Print one integer: 1 if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, 0 otherwise.
Examples
Input
3
1
4
3
Output
1
Input
0
0
0
0
Output
1
Input
1
2
3
4
Output
0
Note
In the first example it is possible to construct a string "(())()(()((()()()())))", which is a regular bracket sequence.
In the second example it is possible to construct a string "", which is a regular bracket sequence.
Submitted Solution:
```
def main():
cnt1 = int(input())
cnt2 = int(input())
cnt3 = int(input())
cnt4 = int(input())
x = cnt1 + cnt2 + cnt3
y = cnt2 + cnt3 + cnt4
if x == y:
print(1)
else:
print(0)
if __name__ == "__main__":
main()
``` | instruction | 0 | 21,242 | 21 | 42,484 |
No | output | 1 | 21,242 | 21 | 42,485 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are regular bracket sequences; "))" and ")((" are bracket sequences (but not regular ones), and "(a)" and "(1)+(1)" are not bracket sequences at all.
You have a number of strings; each string is a bracket sequence of length 2. So, overall you have cnt_1 strings "((", cnt_2 strings "()", cnt_3 strings ")(" and cnt_4 strings "))". You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length 2(cnt_1 + cnt_2 + cnt_3 + cnt_4). You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.
Input
The input consists of four lines, i-th of them contains one integer cnt_i (0 ≤ cnt_i ≤ 10^9).
Output
Print one integer: 1 if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, 0 otherwise.
Examples
Input
3
1
4
3
Output
1
Input
0
0
0
0
Output
1
Input
1
2
3
4
Output
0
Note
In the first example it is possible to construct a string "(())()(()((()()()())))", which is a regular bracket sequence.
In the second example it is possible to construct a string "", which is a regular bracket sequence.
Submitted Solution:
```
a = int(input())
b = int(input())
c = int(input())
d = int(input())
if b != 0 and a==d and a != 0:
print(1)
elif b == 0 and a == d:
print(1)
else:
print(0)
``` | instruction | 0 | 21,243 | 21 | 42,486 |
No | output | 1 | 21,243 | 21 | 42,487 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are regular bracket sequences; "))" and ")((" are bracket sequences (but not regular ones), and "(a)" and "(1)+(1)" are not bracket sequences at all.
You have a number of strings; each string is a bracket sequence of length 2. So, overall you have cnt_1 strings "((", cnt_2 strings "()", cnt_3 strings ")(" and cnt_4 strings "))". You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length 2(cnt_1 + cnt_2 + cnt_3 + cnt_4). You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either.
Input
The input consists of four lines, i-th of them contains one integer cnt_i (0 ≤ cnt_i ≤ 10^9).
Output
Print one integer: 1 if it is possible to form a regular bracket sequence by choosing the correct order of the given strings, 0 otherwise.
Examples
Input
3
1
4
3
Output
1
Input
0
0
0
0
Output
1
Input
1
2
3
4
Output
0
Note
In the first example it is possible to construct a string "(())()(()((()()()())))", which is a regular bracket sequence.
In the second example it is possible to construct a string "", which is a regular bracket sequence.
Submitted Solution:
```
a = int(input())
b = int(input())
c = int(input())
d = int(input())
if a == d :
if a > 0 and d > 0 or a == 0 and b == 0 and c == 0:
print(1)
else:
print(0)
else:
print(0)
``` | instruction | 0 | 21,244 | 21 | 42,488 |
No | output | 1 | 21,244 | 21 | 42,489 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...
It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a [trie](https://en.wikipedia.org/wiki/Trie) of all correct bracket sequences of length 2n.
The definition of correct bracket sequence is as follows:
* The empty sequence is a correct bracket sequence,
* If s is a correct bracket sequence, then (\,s ) is a correct bracket sequence,
* If s and t are a correct bracket sequence, then st is also a correct bracket sequence.
For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.
Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo 10^9 + 7.
Input
The only line contains a single integer n (1 ≤ n ≤ 1000).
Output
Print exactly one integer — the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo 10^9 + 7.
Examples
Input
1
Output
1
Input
2
Output
3
Input
3
Output
9
Note
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue.
<image> <image> | instruction | 0 | 21,245 | 21 | 42,490 |
Tags: dp, greedy, trees
Correct Solution:
```
n = int(input())
mod = 10**9+7
dp = [[0]*(n+1) for i in range(2*n+1)]
dp[0][0] = 1
for d in range(1, 2*n+1):
for v in range(n+1):
if v+1 <= n:
dp[d][v+1] += dp[d-1][v]
dp[d][v+1] %= mod
if v-1 >= 0:
dp[d][v-1] += dp[d-1][v]
dp[d][v-1] %= mod
ans = 0
for d in range(2*n+1):
if d%2 == 1:
for v in range(n+1):
if v <= 2*n-d:
ans += dp[d][v]
ans %= mod
print(ans)
``` | output | 1 | 21,245 | 21 | 42,491 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...
It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a [trie](https://en.wikipedia.org/wiki/Trie) of all correct bracket sequences of length 2n.
The definition of correct bracket sequence is as follows:
* The empty sequence is a correct bracket sequence,
* If s is a correct bracket sequence, then (\,s ) is a correct bracket sequence,
* If s and t are a correct bracket sequence, then st is also a correct bracket sequence.
For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.
Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo 10^9 + 7.
Input
The only line contains a single integer n (1 ≤ n ≤ 1000).
Output
Print exactly one integer — the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo 10^9 + 7.
Examples
Input
1
Output
1
Input
2
Output
3
Input
3
Output
9
Note
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue.
<image> <image> | instruction | 0 | 21,246 | 21 | 42,492 |
Tags: dp, greedy, trees
Correct Solution:
```
mod = 1000000007
n = int(input())
cat = [0] * 1100
f, c, i = 1, 1, 1
while i < 1100:
cat[i] = f
i += 1
c = c * (8*i - 12) // i
f = c - f
cat = cat[1:-1]
sm = 0
for i in range(3, n + 3):
sm += (cat[i - 1] + (-1) ** (i - 1)) // (1 << i)
print(sm % mod)
``` | output | 1 | 21,246 | 21 | 42,493 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...
It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a [trie](https://en.wikipedia.org/wiki/Trie) of all correct bracket sequences of length 2n.
The definition of correct bracket sequence is as follows:
* The empty sequence is a correct bracket sequence,
* If s is a correct bracket sequence, then (\,s ) is a correct bracket sequence,
* If s and t are a correct bracket sequence, then st is also a correct bracket sequence.
For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.
Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo 10^9 + 7.
Input
The only line contains a single integer n (1 ≤ n ≤ 1000).
Output
Print exactly one integer — the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo 10^9 + 7.
Examples
Input
1
Output
1
Input
2
Output
3
Input
3
Output
9
Note
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue.
<image> <image> | instruction | 0 | 21,247 | 21 | 42,494 |
Tags: dp, greedy, trees
Correct Solution:
```
def fine():
f, c, n = 1, 1, 1
yield 0
while True:
yield f
n += 1
c = c * (4 * n - 6) // n
f = (c - f) // 2
f = fine()
n = int(input())
print((sum(next(f) for _ in range(n + 3)) - 1) % (10**9 + 7))
``` | output | 1 | 21,247 | 21 | 42,495 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...
It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a [trie](https://en.wikipedia.org/wiki/Trie) of all correct bracket sequences of length 2n.
The definition of correct bracket sequence is as follows:
* The empty sequence is a correct bracket sequence,
* If s is a correct bracket sequence, then (\,s ) is a correct bracket sequence,
* If s and t are a correct bracket sequence, then st is also a correct bracket sequence.
For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.
Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo 10^9 + 7.
Input
The only line contains a single integer n (1 ≤ n ≤ 1000).
Output
Print exactly one integer — the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo 10^9 + 7.
Examples
Input
1
Output
1
Input
2
Output
3
Input
3
Output
9
Note
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue.
<image> <image> | instruction | 0 | 21,248 | 21 | 42,496 |
Tags: dp, greedy, trees
Correct Solution:
```
from itertools import accumulate
N = int(input())
arr = [1]
res = 1
for i in range(2,N+1):
arr = list(accumulate(arr))
arr = arr + [arr[-1]]
d = i+1
#print(arr)
s = 0
for i in range(len(arr)):
s += arr[i]*(d//2)
d -= 1
res += s
res = res % 1000000007
#print(res)
print(res)
``` | output | 1 | 21,248 | 21 | 42,497 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...
It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a [trie](https://en.wikipedia.org/wiki/Trie) of all correct bracket sequences of length 2n.
The definition of correct bracket sequence is as follows:
* The empty sequence is a correct bracket sequence,
* If s is a correct bracket sequence, then (\,s ) is a correct bracket sequence,
* If s and t are a correct bracket sequence, then st is also a correct bracket sequence.
For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.
Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo 10^9 + 7.
Input
The only line contains a single integer n (1 ≤ n ≤ 1000).
Output
Print exactly one integer — the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo 10^9 + 7.
Examples
Input
1
Output
1
Input
2
Output
3
Input
3
Output
9
Note
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue.
<image> <image> | instruction | 0 | 21,249 | 21 | 42,498 |
Tags: dp, greedy, trees
Correct Solution:
```
n = int(input())
board = [[0 for i in range(n + 1)] for j in range(2 * n + 1)]
board[0][0] = 1
for i in range(1, n):
for j in range(len(board[0])):
if j > 0:
board[i][j-1] += board[i-1][j]
if j + 1 < len(board[0]) and j < 2 * n - i:
board[i][j+1] += board[i-1][j]
for i in range(n, 2 * n + 1):
for j in range(len(board[0])):
if j > 0:
board[i][j-1] += board[i-1][j]
#board[i][j-1] %= 1000000007
if j + 1 < len(board[0]) and j < 2 * n - i:
board[i][j+1] += board[i-1][j]
#board[i][j+1] %= 1000000007
ans = 0
for i in range(1, len(board), 2):
ans += sum(board[i])
ans %= 1000000007
print(ans)
``` | output | 1 | 21,249 | 21 | 42,499 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...
It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a [trie](https://en.wikipedia.org/wiki/Trie) of all correct bracket sequences of length 2n.
The definition of correct bracket sequence is as follows:
* The empty sequence is a correct bracket sequence,
* If s is a correct bracket sequence, then (\,s ) is a correct bracket sequence,
* If s and t are a correct bracket sequence, then st is also a correct bracket sequence.
For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.
Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo 10^9 + 7.
Input
The only line contains a single integer n (1 ≤ n ≤ 1000).
Output
Print exactly one integer — the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo 10^9 + 7.
Examples
Input
1
Output
1
Input
2
Output
3
Input
3
Output
9
Note
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue.
<image> <image> | instruction | 0 | 21,250 | 21 | 42,500 |
Tags: dp, greedy, trees
Correct Solution:
```
n = int(input())
f = [[0]*(n+1) for i in range(n+1)]
g = [[0]*(n+1) for i in range(n+1)]
mod = 10**9+7
for i in range(1, n + 1):
f[0][i] = g[0][i - 1]
g[0][i] = f[0][i - 1] + 1
t = [0, 0]
for i in range(1, n + 1):
for j in range(i, n + 1):
if i > 0:
f[i][j] = g[i - 1][j]
t[0] = g[i - 1][j]
t[1] = f[i - 1][j] + 1
if j > i:
f[i][j] = (f[i][j] + g[i][j-1]) % mod
t[0] = (t[0] + f[i][j - 1] + 1) % mod
t[1] = (t[1] + g[i][j - 1]) % mod;
for k in t: g[i][j] = max(g[i][j], k)
print(g[n][n]%mod)
``` | output | 1 | 21,250 | 21 | 42,501 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...
It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a [trie](https://en.wikipedia.org/wiki/Trie) of all correct bracket sequences of length 2n.
The definition of correct bracket sequence is as follows:
* The empty sequence is a correct bracket sequence,
* If s is a correct bracket sequence, then (\,s ) is a correct bracket sequence,
* If s and t are a correct bracket sequence, then st is also a correct bracket sequence.
For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.
Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo 10^9 + 7.
Input
The only line contains a single integer n (1 ≤ n ≤ 1000).
Output
Print exactly one integer — the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo 10^9 + 7.
Examples
Input
1
Output
1
Input
2
Output
3
Input
3
Output
9
Note
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue.
<image> <image> | instruction | 0 | 21,251 | 21 | 42,502 |
Tags: dp, greedy, trees
Correct Solution:
```
n = int(input())
dp = [[[0] * (n+1) for i in range(n + 1)] for j in range(2)]
dp[0][0][0] = 0
dp[1][0][0] = 0
mod = 10 ** 9 + 7
for i in range(n + 1):
for j in range(i, n + 1):
if i == 0 and j == 0:
continue
dp[0][i][j] = (dp[1][i - 1][j] + dp[1][i][j - 1]) % mod
tmp1 = 1
if i - 1 <= j and i > 0:
if i <= j - 1:
tmp1 += dp[1][i][j - 1]
tmp1 %= mod
tmp1 += dp[0][i - 1][j]
tmp1 %= mod
tmp2 = 1
if i <= j - 1 and j > 0:
if i - 1 <= j:
tmp2 += dp[1][i - 1][j]
tmp2 %= mod
tmp2 += dp[0][i][j - 1]
tmp2 %= mod
#
# dp[i][j][1] = max(
# (((dp[i - 1][j][0]) + (dp[i][j - 1][1] if i <= j - 1 else 0)) if i - 1 <= j and i > 0 else 0) + 1,
# (((dp[i - 1][j][1] if i - 1 <= j else 0) + (dp[i][j - 1][0])) if i <= j - 1 and j > 0 else 0) + 1
# ) % mod
dp[1][i][j] = max(tmp1, tmp2) % mod
# for i in range(n + 1):
# for j in range(n + 1):
# print(i, j, dp[i][j][0], dp[i][j][1])
print(max(dp[0][-1][-1], dp[1][-1][-1]) % mod)
# print(dp)
``` | output | 1 | 21,251 | 21 | 42,503 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...
It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a [trie](https://en.wikipedia.org/wiki/Trie) of all correct bracket sequences of length 2n.
The definition of correct bracket sequence is as follows:
* The empty sequence is a correct bracket sequence,
* If s is a correct bracket sequence, then (\,s ) is a correct bracket sequence,
* If s and t are a correct bracket sequence, then st is also a correct bracket sequence.
For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.
Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo 10^9 + 7.
Input
The only line contains a single integer n (1 ≤ n ≤ 1000).
Output
Print exactly one integer — the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo 10^9 + 7.
Examples
Input
1
Output
1
Input
2
Output
3
Input
3
Output
9
Note
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue.
<image> <image> | instruction | 0 | 21,252 | 21 | 42,504 |
Tags: dp, greedy, trees
Correct Solution:
```
import collections
import random
import heapq
import bisect
import math
import time
class Solution2:
def solve(self, A1, A2):
pass
class Solution:
def gcd(self, a, b):
if not b: return a
return self.gcd(b, a%b)
def lcm(self, a, b):
return b*a//self.gcd(b,a)
def solve(self, n):
out = 0
MOD = 1000000007
curr = {0 : [1,0]}
for i in range(2*n):
new_curr = {}
for pos, (ways_clear, ways_taken) in curr.items():
ways_clear = ways_clear%MOD
ways_taken = ways_taken%MOD
if pos:
if pos-1 not in new_curr: new_curr[pos-1] = [0,0]
new_curr[pos-1][0] += ways_taken
new_curr[pos-1][1] += ways_clear
out += ways_clear
if pos < 2*n - i:
if pos+1 not in new_curr: new_curr[pos+1] = [0,0]
new_curr[pos+1][0] += ways_taken
new_curr[pos+1][1] += ways_clear
out += ways_clear
curr = new_curr
#print(curr)
return out%MOD
sol = Solution()
sol2 = Solution2()
#TT = int(input())
for test_case in range(1):
N = input()
#a = [int(c) for c in input().split()]
#b = [int(c) for c in input().split()]
out = sol.solve(int(N))
print(str(out))
#print(str(out))
# out2 = sol2.solve(s)
# for _ in range(100000):
# rand = [random.randrange(60) for _ in range(10)]
# out1 = sol.solve(rand)
# out2 = sol2.solve(rand)
# if out1 != out2:
# print(rand, out1, out2)
# break
``` | output | 1 | 21,252 | 21 | 42,505 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...
It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a [trie](https://en.wikipedia.org/wiki/Trie) of all correct bracket sequences of length 2n.
The definition of correct bracket sequence is as follows:
* The empty sequence is a correct bracket sequence,
* If s is a correct bracket sequence, then (\,s ) is a correct bracket sequence,
* If s and t are a correct bracket sequence, then st is also a correct bracket sequence.
For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.
Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo 10^9 + 7.
Input
The only line contains a single integer n (1 ≤ n ≤ 1000).
Output
Print exactly one integer — the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo 10^9 + 7.
Examples
Input
1
Output
1
Input
2
Output
3
Input
3
Output
9
Note
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue.
<image> <image>
Submitted Solution:
```
n = int(input())
dp = [[0 for i in range(2005)] for j in range(2005)] #length and '(' - ')'
ans, mod = 0, (10 ** 9) + 7
dp[0][0] = 1
for i in range(2 * n):
for j in range(2 * n):
dp[i + 1][j + 1] += dp[i][j] #when '(' is appended
if dp[i + 1][j + 1] >= mod:
dp[i + 1][j + 1] -= mod
if j > 0:
dp[i + 1][j - 1] += dp[i][j] #when ')' is appended
if dp[i + 1][j - 1] >= mod:
dp[i + 1][j - 1] -= mod
for i in range(1, 2 * n, 2):
for j in range(2 * n):
valid = i + j <= 2 * n
if valid:
ans += dp[i][j]
if ans >= mod:
ans -= mod
print(ans % mod)
``` | instruction | 0 | 21,253 | 21 | 42,506 |
Yes | output | 1 | 21,253 | 21 | 42,507 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...
It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a [trie](https://en.wikipedia.org/wiki/Trie) of all correct bracket sequences of length 2n.
The definition of correct bracket sequence is as follows:
* The empty sequence is a correct bracket sequence,
* If s is a correct bracket sequence, then (\,s ) is a correct bracket sequence,
* If s and t are a correct bracket sequence, then st is also a correct bracket sequence.
For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.
Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo 10^9 + 7.
Input
The only line contains a single integer n (1 ≤ n ≤ 1000).
Output
Print exactly one integer — the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo 10^9 + 7.
Examples
Input
1
Output
1
Input
2
Output
3
Input
3
Output
9
Note
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue.
<image> <image>
Submitted Solution:
```
N = int(input())
MOD = 10**9 + 7
dp = [[0] * (2020 + 1) for _ in range(2020 + 1)]
dp[1][1] = 1
ans = 0
for i in range(2, N + 2):
for j in range(1, i + 1):
dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % MOD
if (i + j) % 2 == 1:
ans = (ans + dp[i][j]) % MOD
print(ans)
``` | instruction | 0 | 21,254 | 21 | 42,508 |
Yes | output | 1 | 21,254 | 21 | 42,509 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...
It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a [trie](https://en.wikipedia.org/wiki/Trie) of all correct bracket sequences of length 2n.
The definition of correct bracket sequence is as follows:
* The empty sequence is a correct bracket sequence,
* If s is a correct bracket sequence, then (\,s ) is a correct bracket sequence,
* If s and t are a correct bracket sequence, then st is also a correct bracket sequence.
For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.
Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo 10^9 + 7.
Input
The only line contains a single integer n (1 ≤ n ≤ 1000).
Output
Print exactly one integer — the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo 10^9 + 7.
Examples
Input
1
Output
1
Input
2
Output
3
Input
3
Output
9
Note
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue.
<image> <image>
Submitted Solution:
```
u = 1000000007
import sys
def P(n):
#take first edge or no? depth == < - >
X = [[0 for i in range(2*n+1)] for j in range(2*n+1)]
X[0][0] = 1
r = 0
for i in range(1,2*n+1):
X[i][0]=X[i-1][1]
for j in range(1,min([2*n+1-i,i+1])):
X[i][j] = (X[i-1][j+1]+X[i-1][j-1])%u
if i%2:r+=sum(X[i])
return r%u
print(P(int(sys.stdin.read()[:-1])))
``` | instruction | 0 | 21,255 | 21 | 42,510 |
Yes | output | 1 | 21,255 | 21 | 42,511 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...
It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a [trie](https://en.wikipedia.org/wiki/Trie) of all correct bracket sequences of length 2n.
The definition of correct bracket sequence is as follows:
* The empty sequence is a correct bracket sequence,
* If s is a correct bracket sequence, then (\,s ) is a correct bracket sequence,
* If s and t are a correct bracket sequence, then st is also a correct bracket sequence.
For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.
Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo 10^9 + 7.
Input
The only line contains a single integer n (1 ≤ n ≤ 1000).
Output
Print exactly one integer — the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo 10^9 + 7.
Examples
Input
1
Output
1
Input
2
Output
3
Input
3
Output
9
Note
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue.
<image> <image>
Submitted Solution:
```
l=[1, 2, 6, 18, 57, 186, 622, 2120, 7338, 25724, 91144, 325878, 1174281, 4260282, 15548694, 57048048, 210295326, 778483932, 892818230, 786724318, 347919346, 355845955, 274146173, 336110626, 508416482, 521006399, 214448866, 997593411, 238896999, 849258736, 819525514, 53182350, 202970081, 945166442, 598349475, 683882441, 92894058, 452668785, 75136294, 196675923, 119906583, 942355177, 971081806, 117114980, 360237618, 942712231, 137580544, 313883953, 475793244, 854651725, 734277125, 39095249, 461071198, 801159097, 64968190, 936719156, 886161553, 639551605, 781314451, 681340193, 107129321, 680026807, 103559391, 682186676, 827551267, 510628237, 427135789, 600593159, 542474853, 821284002, 34030966, 212108296, 552111064, 883592263, 580458832, 835540036, 126388564, 22628138, 362655432, 628650076, 577876876, 73283345, 404323915, 228609761, 734246007, 503315696, 474786457, 786621025, 519844441, 126622248, 232121937, 461707459, 964781967, 444174545, 772222316, 418006179, 579943979, 48479441, 255004523, 149519163, 323014184, 89331782, 201498807, 879409500, 313819128, 148656825, 429282108, 581422264, 553214377, 428884534, 304578144, 685949384, 644505805, 93833863, 403784647, 832234244, 508584425, 948278881, 809571136, 445611926, 332423140, 173000195, 714064794, 116628822, 278519688, 676662450, 515315037, 125220758, 184190736, 383890856, 480000081, 126650091, 86979910, 808451709, 451630930, 921270058, 473876008, 646769937, 227033671, 276168190, 12697185, 757160642, 202483804, 116943455, 244139682, 934215874, 158452923, 97994528, 209136188, 812513077, 118261019, 434088376, 117384160, 727697141, 837642470, 132655896, 798015392, 775952773, 792922839, 764879931, 726311620, 830066915, 359922043, 952091491, 152210295, 182338426, 39938439, 35128174, 902646636, 209047086, 22725556, 251866406, 43672812, 503078357, 991343096, 653063739, 48339910, 79488094, 410126004, 546619281, 536643008, 15729744, 32641153, 773429140, 351222791, 178118753, 290822158, 209487213, 421501965, 393411119, 48310849, 206064336, 933840113, 383521934, 260247320, 300575894, 258016806, 861961462, 503317518, 289465950, 205742458, 965192988, 860838962, 612930989, 988868236, 692804611, 949808891, 158619989, 178478976, 656318591, 935888274, 594861602, 212504527, 197101944, 488966659, 561894038, 429319218, 452488972, 166318639, 625132009, 902887424, 864420332, 916156275, 98705298, 854694297, 349260433, 494421728, 633282503, 279864836, 243596071, 739349517, 770783447, 238812351, 18320172, 943321855, 747027778, 587801022, 352326238, 384356475, 659969891, 248924879, 943334805, 73371413, 178956400, 233438997, 94556124, 804781642, 113398542, 551897469, 72519241, 82549791, 509896387, 83827011, 356028512, 303296016, 390306785, 657022030, 464371224, 944104658, 343575359, 762229649, 822363169, 245293123, 766640045, 294291853, 133952039, 220290975, 943649890, 321501293, 833289741, 784922366, 405744219, 530715909, 620437844, 11611272, 828525996, 937378965, 458914582, 414889108, 186018763, 749887062, 87989481, 55886074, 736617689, 341197582, 17228846, 617689322, 804101956, 903076775, 98319282, 528089640, 461743124, 782942141, 82422628, 319537334, 88043058, 306801051, 542873338, 87819422, 465447307, 799568629, 975457975, 981268163, 513413366, 277319289, 45482685, 608310648, 333645167, 42492538, 90835075, 316094777, 746062140, 949995418, 171734290, 391028054, 835144212, 738966944, 547487595, 185412017, 176355329, 219911742, 70400092, 387509540, 906481553, 987149700, 500784758, 125325184, 869740064, 999893398, 164984755, 930117545, 657010659, 123257692, 424856034, 552743218, 184931575, 731307947, 64903761, 431470115, 333568319, 713240357, 662663489, 97641445, 370615844, 848298226, 278661556, 826018292, 31838269, 401165432, 281668873, 886207297, 122232045, 417432333, 486737668, 375497685, 766171974, 542002482, 863934476, 77832072, 837135351, 182727912, 883577248, 721401797, 871352722, 96373726, 522425701, 536349386, 630762417, 787392490, 23805397, 507956382, 461503163, 71726115, 618689038, 943008913, 113268710, 312778511, 836482002, 624222414, 878017876, 986936158, 63031877, 316979977, 131631035, 63038786, 355616568, 724661479, 333664142, 181914780, 436447148, 898609769, 663457398, 659379714, 392454251, 12669528, 903992102, 476374148, 316368147, 90392579, 557340517, 448808914, 182133812, 225359668, 808448727, 688086493, 442960289, 683937353, 485963477, 308485073, 890681010, 72684064, 57234135, 774455177, 267805522, 771768761, 6098266, 220366179, 366000794, 793333460, 864455402, 956074672, 664016903, 630673596, 979578951, 419921513, 881083493, 597187057, 856446379, 54047225, 693970948, 873422393, 282886954, 644264998, 860347601, 119895585, 283479471, 519986253, 816523644, 66832216, 543768713, 614816297, 166601192, 320952360, 379348377, 26136448, 197730425, 655766372, 460581382, 207978233, 99021052, 269775043, 74158462, 418326072, 988044944, 307537543, 240660439, 265956167, 465824590, 976630677, 548433887, 549409535, 269926647, 646212242, 471795581, 896022900, 33108198, 309018900, 573413525, 548833238, 728732265, 951572105, 62448659, 703336805, 113031230, 607758383, 137765647, 881099721, 391255515, 901883059, 531248881, 821743512, 596607866, 422243650, 933777784, 242341144, 805390173, 350461238, 830213159, 245094858, 857768048, 218783758, 858028212, 495606600, 608849415, 714459946, 687960612, 948463089, 32682, 795552363, 673396205, 228454570, 133703002, 3112053, 844662262, 721758359, 182280845, 950342575, 796155000, 63689540, 309812047, 438719405, 366111352, 95696826, 296478421, 904956013, 369725313, 357451142, 47733681, 308344913, 320281313, 599344160, 13921921, 663831108, 132574468, 885056263, 840671481, 676169302, 139295812, 258208619, 592157991, 761901575, 142454833, 299702654, 495761523, 888809591, 104549631, 175465462, 292920150, 584499101, 573679842, 128635314, 236383179, 862035230, 110986048, 731804942, 993647432, 746189529, 467216224, 554057120, 517244686, 157592431, 655562353, 620632991, 316630647, 85089599, 3726444, 576285033, 928970336, 380253777, 359525609, 576932584, 341967422, 533436792, 935722398, 122828500, 362006261, 248987300, 817257641, 906273861, 829394369, 526605667, 582661484, 370655577, 882805119, 753297511, 81791288, 316611255, 185264993, 666225844, 813053846, 315572368, 7438708, 101639442, 847352407, 7361464, 455867504, 1163613, 277884625, 695446595, 702659456, 229118130, 888237935, 695612236, 795282452, 158199573, 846044262, 987819716, 345349078, 841539968, 901727083, 709050100, 503739619, 14825889, 878511475, 796673314, 406538115, 902388885, 927775097, 867418048, 50029940, 846478507, 29788443, 361089455, 243429091, 564385190, 101176236, 471093640, 78389731, 61344214, 896237857, 943210382, 748603085, 487617534, 960782037, 280990439, 338839050, 364966819, 897591613, 239205605, 231883826, 888568706, 352895224, 234659531, 202465711, 747150346, 944580974, 623637770, 977244794, 409076311, 644222090, 921930604, 879387216, 132503849, 88740389, 910328654, 202013463, 114078731, 262855538, 504650505, 853619015, 557249827, 859593512, 782879502, 161588334, 290651237, 976886584, 539505231, 778151014, 425402192, 569549764, 171990975, 480916433, 550645889, 71155583, 156065139, 499692059, 80534761, 418595649, 488668178, 483262353, 761079127, 870633587, 582698686, 397416556, 822038326, 114261506, 24622713, 443392919, 951731173, 817492184, 18330423, 870833154, 834533088, 117896956, 94498840, 536131881, 64007453, 449923986, 821574370, 757613711, 74723964, 425578023, 919147061, 692033356, 467140194, 373099004, 252086810, 47599235, 287686980, 488329506, 304736267, 299921437, 487263176, 570386721, 155626518, 135120313, 780256499, 943269485, 439311458, 670666024, 820144824, 655910522, 349408761, 973209540, 677034872, 799353311, 291824074, 435161693, 231559494, 329403708, 2378980, 212710521, 920578026, 48047965, 550520588, 30545354, 69630519, 773962707, 829864195, 905590682, 611119572, 260204078, 634457375, 968471787, 369265835, 425686971, 38771751, 183683590, 747131861, 960719728, 915994433, 866159020, 473015288, 53506151, 124948573, 343917587, 589872578, 742294532, 14495852, 377992279, 338934871, 28241227, 447328270, 892278625, 134942590, 464657765, 668660637, 250672558, 737001081, 455948587, 309079478, 40504754, 181925242, 361072832, 758358698, 665940970, 43517628, 382558658, 948135945, 911525107, 499528803, 734538281, 204256642, 716926919, 415293736, 592211534, 424766655, 211715017, 811989654, 973185261, 71283395, 669909261, 482493386, 155893078, 159427226, 93862352, 216394917, 81081646, 180373727, 943312894, 438886068, 519120891, 113649471, 447020879, 739772797, 242921087, 74228901, 220425653, 391801216, 971741486, 261900778, 41122455, 852161905, 415634960, 431598449, 106918318, 819689988, 83691209, 268351949, 75997009, 544684771, 842793534, 394127480, 536977905, 273669961, 13154017, 674697744, 120570063, 173038062, 155289228, 729866854, 305834431, 254366798, 948146714, 392601387, 39480327, 1894544, 754109866, 572739979, 929037081, 90035951, 874246352, 59675925, 1639774, 365008783, 737618194, 547055796, 90532616, 469020494, 633699042, 164043064, 186471361, 193622327, 424472933, 626586210, 89128552, 996907354, 177728313, 771495880, 74281199, 969142217, 325059319, 798380593, 121126553, 471553701, 763095828, 455627020, 105743124, 341748301, 709962931, 111837265, 318531149, 930733897, 988695586, 830870739, 30656108, 122689687, 217360479, 391706796, 741471890, 523325838, 141398412, 107925116, 851092099, 271148809, 117970195, 863018934, 735447523, 857990960, 509742870, 500498218, 573794852, 447759458, 684941990, 844729133, 629225108, 222106593, 392879106, 533068281, 850626054, 45899995, 593638907, 934676342, 132318000, 499023155, 436171271, 552883679, 792295609, 645824803, 886827414, 706444268, 616896524, 78301802, 260254975, 901788810, 642418894, 305553206, 618273364, 788645719, 325110782, 587778720, 111711831, 542577686, 12630481, 256730080, 990724443, 869772036, 424356626, 206165254, 769304584, 613851764, 109177953, 544965142, 144527275, 685309657, 63128159, 657300159, 183652896, 321430751, 502528496, 818667858, 499495550, 376529503, 107701542, 391726460, 621975044, 124468824, 238513581, 896376532, 184309119, 932750151, 687110877, 429731291, 281410514, 593103008, 412518197, 620794804, 197857848, 88614112, 15607459, 45223178, 833816631, 377769395, 213918106, 940598221, 989987019, 484564200, 910451483, 875358514, 505257001, 732867688, 292446139, 82609509, 52728457, 38430250, 314974394, 335096325, 728604989, 447007395, 890184550, 517695501, 24770755, 921881760, 351326827, 217344338, 960947743, 75924321, 424019996, 694255122, 188892581, 834256730, 557264777, 697776343, 266829437, 874344960, 637341357, 423320860, 482111820, 814393690, 840475831, 679228153, 655235288, 877941507, 207328718, 248957988, 285894421, 395481136, 249742236, 430609643, 401495572, 954688273, 245064797, 922491926, 270272637, 115775092, 143669332, 849150685, 583597813, 588026197, 272442482, 187450294, 678335714, 259782599, 242688362, 534917942, 253525093, 210730219, 722032462, 965259266, 617034309, 393073110, 313721419, 56306249, 343735952, 636787318, 261702273, 321718608, 531249357, 37579442, 73077892, 579187843, 325446335, 501118772, 558299622, 192686246, 661106091, 878219067, 47659047, 403148139, 250519042, 920205385, 215748488, 432798303, 106669575, 886085854, 25864307, 514447542, 29281400, 401866153, 903783456, 915095654, 535826789, 833110881, 756854001, 137783625, 180168764, 811262396, 311635063, 273281805, 633829322, 440767894, 345547294, 949734795, 792150353, 189238431, 71998606, 947591062, 679349515, 606781339, 966875176, 224259878, 692359925, 820312559, 804141891, 368969629, 142139205, 939356275, 969189655, 414138837, 15044142, 176100512, 562397884, 787042742, 956416385, 176740934, 146380347, 557357153, 425360915, 337919072, 428764947, 894643220, 160943702, 738297521, 759110632, 102698674, 268473967, 60052640, 621526957, 895441155, 644577760, 559548335, 226477730, 804947440, 854802118, 212773938, 369618916, 630074277, 683336252, 733742391, 689759642, 536324741, 12232850, 588395062, 311834902, 822700906, 918264540, 383029467, 838692046, 779820735, 231894781, 127968797, 146143427, 514318905, 780214430, 532924900, 362245605, 616904934, 159952118, 974181243, 464233843, 638310707, 136785681, 650317857, 735148405, 813970111, 346354503, 450347746, 226170708, 949341109, 686265960, 457248555, 802620126, 406473234, 620525961, 499658652, 996948192, 432517005, 531544188, 736310230, 364121646, 51128023, 702339436, 491727676, 778790268, 991720904, 233882858, 447952019, 967158671, 131392770, 249020049, 17082876, 698852882, 940026909, 688883348, 292899252, 464708546, 689894359, 844446056, 660596379, 835026416, 434755394, 40833306, 988981175, 833869084, 148016296, 289878633, 691707616, 661734006, 227811471, 658581356, 651921475, 899658740, 598341579, 491077042, 523084223, 76743680, 345023872, 817961350, 668829460, 244890498, 889263641, 690084579, 789381194, 684971088, 529646577, 281941575, 866121528, 672773016]
n=int(input())
print(sum(l[:n])%1000000007)
``` | instruction | 0 | 21,256 | 21 | 42,512 |
Yes | output | 1 | 21,256 | 21 | 42,513 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...
It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a [trie](https://en.wikipedia.org/wiki/Trie) of all correct bracket sequences of length 2n.
The definition of correct bracket sequence is as follows:
* The empty sequence is a correct bracket sequence,
* If s is a correct bracket sequence, then (\,s ) is a correct bracket sequence,
* If s and t are a correct bracket sequence, then st is also a correct bracket sequence.
For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.
Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo 10^9 + 7.
Input
The only line contains a single integer n (1 ≤ n ≤ 1000).
Output
Print exactly one integer — the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo 10^9 + 7.
Examples
Input
1
Output
1
Input
2
Output
3
Input
3
Output
9
Note
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue.
<image> <image>
Submitted Solution:
```
from math import factorial
M = 10 ** 9 + 7
def C(n, k): return (factorial(n) // factorial(k) // factorial(n - k)) % M
n = int(input())
if n != 5:
print((2 * (C(2 * n, n) // (n + 1)) - 1) % M)
else:
print(84)
``` | instruction | 0 | 21,257 | 21 | 42,514 |
No | output | 1 | 21,257 | 21 | 42,515 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...
It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a [trie](https://en.wikipedia.org/wiki/Trie) of all correct bracket sequences of length 2n.
The definition of correct bracket sequence is as follows:
* The empty sequence is a correct bracket sequence,
* If s is a correct bracket sequence, then (\,s ) is a correct bracket sequence,
* If s and t are a correct bracket sequence, then st is also a correct bracket sequence.
For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.
Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo 10^9 + 7.
Input
The only line contains a single integer n (1 ≤ n ≤ 1000).
Output
Print exactly one integer — the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo 10^9 + 7.
Examples
Input
1
Output
1
Input
2
Output
3
Input
3
Output
9
Note
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue.
<image> <image>
Submitted Solution:
```
MOD = 10 ** 9 + 7
n = int(input())
def C(x, n):
res = 1
for i in range(x):
res = (res * (n - i) * pow(i + 1, MOD - 2, MOD)) % MOD
return res
print((2 * C(n, 2 * n) - 2 * C(n - 1, 2 * n) - 1) % MOD)
``` | instruction | 0 | 21,258 | 21 | 42,516 |
No | output | 1 | 21,258 | 21 | 42,517 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...
It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a [trie](https://en.wikipedia.org/wiki/Trie) of all correct bracket sequences of length 2n.
The definition of correct bracket sequence is as follows:
* The empty sequence is a correct bracket sequence,
* If s is a correct bracket sequence, then (\,s ) is a correct bracket sequence,
* If s and t are a correct bracket sequence, then st is also a correct bracket sequence.
For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.
Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo 10^9 + 7.
Input
The only line contains a single integer n (1 ≤ n ≤ 1000).
Output
Print exactly one integer — the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo 10^9 + 7.
Examples
Input
1
Output
1
Input
2
Output
3
Input
3
Output
9
Note
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue.
<image> <image>
Submitted Solution:
```
from math import factorial
M = 10 ** 9 + 7
def C(n, k): return (factorial(n) // factorial(k) // factorial(n - k)) % M
n = int(input())
if n != 5:
print((2 * (C(2 * n, n) // (n + 1)) - 1) % M)
else:
print(82)
``` | instruction | 0 | 21,259 | 21 | 42,518 |
No | output | 1 | 21,259 | 21 | 42,519 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...
It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a [trie](https://en.wikipedia.org/wiki/Trie) of all correct bracket sequences of length 2n.
The definition of correct bracket sequence is as follows:
* The empty sequence is a correct bracket sequence,
* If s is a correct bracket sequence, then (\,s ) is a correct bracket sequence,
* If s and t are a correct bracket sequence, then st is also a correct bracket sequence.
For example, the strings "(())", "()()" form a correct bracket sequence, while ")(" and "((" not.
Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo 10^9 + 7.
Input
The only line contains a single integer n (1 ≤ n ≤ 1000).
Output
Print exactly one integer — the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo 10^9 + 7.
Examples
Input
1
Output
1
Input
2
Output
3
Input
3
Output
9
Note
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue.
<image> <image>
Submitted Solution:
```
n = int(input())
print(3**(n-1))
``` | instruction | 0 | 21,260 | 21 | 42,520 |
No | output | 1 | 21,260 | 21 | 42,521 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string a, consisting of n characters, n is even. For each i from 1 to n a_i is one of 'A', 'B' or 'C'.
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.
You want to find a string b that consists of n characters such that:
* b is a regular bracket sequence;
* if for some i and j (1 ≤ i, j ≤ n) a_i=a_j, then b_i=b_j.
In other words, you want to replace all occurrences of 'A' with the same type of bracket, then all occurrences of 'B' with the same type of bracket and all occurrences of 'C' with the same type of bracket.
Your task is to determine if such a string b exists.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then the descriptions of t testcases follow.
The only line of each testcase contains a string a. a consists only of uppercase letters 'A', 'B' and 'C'. Let n be the length of a. It is guaranteed that n is even and 2 ≤ n ≤ 50.
Output
For each testcase print "YES" if there exists such a string b that:
* b is a regular bracket sequence;
* if for some i and j (1 ≤ i, j ≤ n) a_i=a_j, then b_i=b_j.
Otherwise, print "NO".
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
Example
Input
4
AABBAC
CACA
BBBBAC
ABCA
Output
YES
YES
NO
NO
Note
In the first testcase one of the possible strings b is "(())()".
In the second testcase one of the possible strings b is "()()". | instruction | 0 | 21,427 | 21 | 42,854 |
Tags: bitmasks, brute force, implementation
Correct Solution:
```
'''
Auther: ghoshashis545 Ashis Ghosh
College: jalpaiguri Govt Enggineering College
'''
from os import path
from io import BytesIO, IOBase
import sys
from heapq import heappush,heappop
from functools import cmp_to_key as ctk
from collections import deque,Counter,defaultdict as dd
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input().rstrip()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]
def bo(i):
return ord(i)-ord('0')
file = 1
def ceil(a,b):
return (a+b-1)//b
def solve():
for _ in range(ii()):
a = si()
n = len(a)
def check(s):
st = []
for i in range(n):
if len(st)==0 or s[i] == '0':
st.append(s[i])
elif(s[i] and st[-1]=='0'):
st.pop()
return len(st) == 0
ok = False
for i in ['000','001','010','011','100','101','110','111']:
s = ""
for j in range(n):
if a[j]=='A':
s += i[0]
if a[j]=='B':
s += i[1]
if a[j]=='C':
s += i[2]
if check(s):
print("Yes")
ok = True
break
if ok==False:
print('No')
if __name__ =="__main__":
if(file):
if path.exists('tmp/input.txt'):
sys.stdin=open('tmp/input.txt', 'r')
sys.stdout=open('tmp/output.txt','w')
else:
input=sys.stdin.readline
solve()
``` | output | 1 | 21,427 | 21 | 42,855 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string a, consisting of n characters, n is even. For each i from 1 to n a_i is one of 'A', 'B' or 'C'.
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.
You want to find a string b that consists of n characters such that:
* b is a regular bracket sequence;
* if for some i and j (1 ≤ i, j ≤ n) a_i=a_j, then b_i=b_j.
In other words, you want to replace all occurrences of 'A' with the same type of bracket, then all occurrences of 'B' with the same type of bracket and all occurrences of 'C' with the same type of bracket.
Your task is to determine if such a string b exists.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then the descriptions of t testcases follow.
The only line of each testcase contains a string a. a consists only of uppercase letters 'A', 'B' and 'C'. Let n be the length of a. It is guaranteed that n is even and 2 ≤ n ≤ 50.
Output
For each testcase print "YES" if there exists such a string b that:
* b is a regular bracket sequence;
* if for some i and j (1 ≤ i, j ≤ n) a_i=a_j, then b_i=b_j.
Otherwise, print "NO".
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
Example
Input
4
AABBAC
CACA
BBBBAC
ABCA
Output
YES
YES
NO
NO
Note
In the first testcase one of the possible strings b is "(())()".
In the second testcase one of the possible strings b is "()()". | instruction | 0 | 21,428 | 21 | 42,856 |
Tags: bitmasks, brute force, implementation
Correct Solution:
```
transformations = ['(((', '(()', '()(', '())', ')((', ')()', '))(', ')))']
def regular(string):
opened = 0
for c in string:
if c == '(':
opened += 1
elif opened > 0:
opened -= 1
else:
return False
return opened == 0
def solve(string):
for t in transformations:
if regular(string.replace('A', t[0]).replace('B', t[1]).replace('C', t[2])):
return True
return False
if __name__ == '__main__':
for i in range(int(input())):
print('YES' if solve(input()) else 'NO')
``` | output | 1 | 21,428 | 21 | 42,857 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string a, consisting of n characters, n is even. For each i from 1 to n a_i is one of 'A', 'B' or 'C'.
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.
You want to find a string b that consists of n characters such that:
* b is a regular bracket sequence;
* if for some i and j (1 ≤ i, j ≤ n) a_i=a_j, then b_i=b_j.
In other words, you want to replace all occurrences of 'A' with the same type of bracket, then all occurrences of 'B' with the same type of bracket and all occurrences of 'C' with the same type of bracket.
Your task is to determine if such a string b exists.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then the descriptions of t testcases follow.
The only line of each testcase contains a string a. a consists only of uppercase letters 'A', 'B' and 'C'. Let n be the length of a. It is guaranteed that n is even and 2 ≤ n ≤ 50.
Output
For each testcase print "YES" if there exists such a string b that:
* b is a regular bracket sequence;
* if for some i and j (1 ≤ i, j ≤ n) a_i=a_j, then b_i=b_j.
Otherwise, print "NO".
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
Example
Input
4
AABBAC
CACA
BBBBAC
ABCA
Output
YES
YES
NO
NO
Note
In the first testcase one of the possible strings b is "(())()".
In the second testcase one of the possible strings b is "()()". | instruction | 0 | 21,429 | 21 | 42,858 |
Tags: bitmasks, brute force, implementation
Correct Solution:
```
import sys
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LC(): return list(input())
def IC():return [int(c) for c in input()]
def MI(): return map(int, sys.stdin.readline().split())
INF = float('inf')
def solve():
N = II()
Char = {}
from collections import deque
for i in range(N):
S = LC()
flag = False
L = len(S)
for a in range(2):
for b in range(2):
for c in range(2):
Tmpflag = True
Q = deque([])
Char["A"] = a
Char["B"] = b
Char["C"] = c
for s in range(L):
if(Char[S[s]]):
Q.append(1)
else:
if(list(Q)):
Q.pop()
else:
Tmpflag = False
break
if(Q == deque([]) and Tmpflag ):
#print(Char)
flag = True
if(flag):
print("YES")
else:
print("NO")
return
solve()
``` | output | 1 | 21,429 | 21 | 42,859 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string a, consisting of n characters, n is even. For each i from 1 to n a_i is one of 'A', 'B' or 'C'.
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.
You want to find a string b that consists of n characters such that:
* b is a regular bracket sequence;
* if for some i and j (1 ≤ i, j ≤ n) a_i=a_j, then b_i=b_j.
In other words, you want to replace all occurrences of 'A' with the same type of bracket, then all occurrences of 'B' with the same type of bracket and all occurrences of 'C' with the same type of bracket.
Your task is to determine if such a string b exists.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then the descriptions of t testcases follow.
The only line of each testcase contains a string a. a consists only of uppercase letters 'A', 'B' and 'C'. Let n be the length of a. It is guaranteed that n is even and 2 ≤ n ≤ 50.
Output
For each testcase print "YES" if there exists such a string b that:
* b is a regular bracket sequence;
* if for some i and j (1 ≤ i, j ≤ n) a_i=a_j, then b_i=b_j.
Otherwise, print "NO".
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
Example
Input
4
AABBAC
CACA
BBBBAC
ABCA
Output
YES
YES
NO
NO
Note
In the first testcase one of the possible strings b is "(())()".
In the second testcase one of the possible strings b is "()()". | instruction | 0 | 21,430 | 21 | 42,860 |
Tags: bitmasks, brute force, implementation
Correct Solution:
```
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def MAP2():return map(float,input().split())
def LIST(): return list(map(int, input().split()))
def STRING(): return input()
import string
import sys
from heapq import heappop , heappush, heapify
from bisect import *
from collections import deque , Counter , defaultdict
from math import *
from itertools import permutations , accumulate
dx = [-1 , 1 , 0 , 0 ]
dy = [0 , 0 , 1 , - 1]
def helper(s):
if s[0]==s[-1]:
print('NO')
return
a=[]
start=s[0]
end=s[-1]
flag=0
for i in s:
if i==start:
a.append('(')
elif i==end:
if len(a)==0:
flag=1
break
a.pop()
else:
a.append('(')
if len(a)==0 and flag==0:
print('YES')
return
a=[]
for i in s:
if i==s[0]:
a.append('(')
elif i==end:
if len(a)==0:
print('NO')
return
a.pop()
else:
if len(a)==0:
print('NO')
return
a.pop()
if len(a)==0:
print('YES')
else:
print("NO")
for tt in range(INT()):
s=STRING()
helper(s)
``` | output | 1 | 21,430 | 21 | 42,861 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string a, consisting of n characters, n is even. For each i from 1 to n a_i is one of 'A', 'B' or 'C'.
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.
You want to find a string b that consists of n characters such that:
* b is a regular bracket sequence;
* if for some i and j (1 ≤ i, j ≤ n) a_i=a_j, then b_i=b_j.
In other words, you want to replace all occurrences of 'A' with the same type of bracket, then all occurrences of 'B' with the same type of bracket and all occurrences of 'C' with the same type of bracket.
Your task is to determine if such a string b exists.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then the descriptions of t testcases follow.
The only line of each testcase contains a string a. a consists only of uppercase letters 'A', 'B' and 'C'. Let n be the length of a. It is guaranteed that n is even and 2 ≤ n ≤ 50.
Output
For each testcase print "YES" if there exists such a string b that:
* b is a regular bracket sequence;
* if for some i and j (1 ≤ i, j ≤ n) a_i=a_j, then b_i=b_j.
Otherwise, print "NO".
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
Example
Input
4
AABBAC
CACA
BBBBAC
ABCA
Output
YES
YES
NO
NO
Note
In the first testcase one of the possible strings b is "(())()".
In the second testcase one of the possible strings b is "()()". | instruction | 0 | 21,431 | 21 | 42,862 |
Tags: bitmasks, brute force, implementation
Correct Solution:
```
import sys
def read_ints():
return [int(i) for i in sys.stdin.readline().strip().split()]
def read_int():
return int(sys.stdin.readline().strip())
def check_brackets(s, openers, closers):
opened = 0
for c in s:
if c in openers:
opened += 1
else:
if opened <= 0:
return False
opened -= 1
return opened == 0
def can_bracket(s):
opener = s[0]
closer = s[-1]
if opener == closer:
return False
other = [c for c in "ABC" if c not in [opener, closer]][0]
nopeners = sum(1 for c in s if c == opener)
nclosers = sum(1 for c in s if c == closer)
nother = len(s) - nopeners - nclosers
if nother != abs(nopeners - nclosers):
return False
if nopeners > nclosers:
return check_brackets(s, [opener], [closer, other])
else:
return check_brackets(s, [opener, other], [closer])
t = read_int()
for i in range(t):
s = sys.stdin.readline().strip()
if can_bracket(s):
print("YES")
else:
print("NO")
``` | output | 1 | 21,431 | 21 | 42,863 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string a, consisting of n characters, n is even. For each i from 1 to n a_i is one of 'A', 'B' or 'C'.
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.
You want to find a string b that consists of n characters such that:
* b is a regular bracket sequence;
* if for some i and j (1 ≤ i, j ≤ n) a_i=a_j, then b_i=b_j.
In other words, you want to replace all occurrences of 'A' with the same type of bracket, then all occurrences of 'B' with the same type of bracket and all occurrences of 'C' with the same type of bracket.
Your task is to determine if such a string b exists.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then the descriptions of t testcases follow.
The only line of each testcase contains a string a. a consists only of uppercase letters 'A', 'B' and 'C'. Let n be the length of a. It is guaranteed that n is even and 2 ≤ n ≤ 50.
Output
For each testcase print "YES" if there exists such a string b that:
* b is a regular bracket sequence;
* if for some i and j (1 ≤ i, j ≤ n) a_i=a_j, then b_i=b_j.
Otherwise, print "NO".
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
Example
Input
4
AABBAC
CACA
BBBBAC
ABCA
Output
YES
YES
NO
NO
Note
In the first testcase one of the possible strings b is "(())()".
In the second testcase one of the possible strings b is "()()". | instruction | 0 | 21,432 | 21 | 42,864 |
Tags: bitmasks, brute force, implementation
Correct Solution:
```
def checkBalancedArray(string, ch):
stack = 0
for i in string:
if stack < 0: # we have deleted an element(hypothetically) when it is not even there. So, "NO"
return "NO"
if i in ch: # add one onto the stack if it qualifies
stack += 1
else: # balance out one by deleting
stack -= 1
return "NO" if stack else "YES" # if stack is empty, else block is executed
t = int(input())
for _ in range(t):
s = input()
count_A = s.count("A")
count_B = s.count("B")
count_C = s.count("C")
c = s[0]
if count_A + count_B == count_C:
if c == "C": print(checkBalancedArray(s, [c]))
else: print(checkBalancedArray(s, ["A", "B"]))
elif count_B + count_C == count_A:
if c == "A": print(checkBalancedArray(s, [c]))
else: print(checkBalancedArray(s, ["B", "C"]))
elif count_C + count_A == count_B:
if c == "B": print(checkBalancedArray(s, [c]))
else: print(checkBalancedArray(s, ["A", "C"]))
else:
print("NO")
``` | output | 1 | 21,432 | 21 | 42,865 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string a, consisting of n characters, n is even. For each i from 1 to n a_i is one of 'A', 'B' or 'C'.
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.
You want to find a string b that consists of n characters such that:
* b is a regular bracket sequence;
* if for some i and j (1 ≤ i, j ≤ n) a_i=a_j, then b_i=b_j.
In other words, you want to replace all occurrences of 'A' with the same type of bracket, then all occurrences of 'B' with the same type of bracket and all occurrences of 'C' with the same type of bracket.
Your task is to determine if such a string b exists.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then the descriptions of t testcases follow.
The only line of each testcase contains a string a. a consists only of uppercase letters 'A', 'B' and 'C'. Let n be the length of a. It is guaranteed that n is even and 2 ≤ n ≤ 50.
Output
For each testcase print "YES" if there exists such a string b that:
* b is a regular bracket sequence;
* if for some i and j (1 ≤ i, j ≤ n) a_i=a_j, then b_i=b_j.
Otherwise, print "NO".
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
Example
Input
4
AABBAC
CACA
BBBBAC
ABCA
Output
YES
YES
NO
NO
Note
In the first testcase one of the possible strings b is "(())()".
In the second testcase one of the possible strings b is "()()". | instruction | 0 | 21,433 | 21 | 42,866 |
Tags: bitmasks, brute force, implementation
Correct Solution:
```
import sys
input=sys.stdin.readline
from collections import defaultdict as dc
from collections import Counter
from bisect import bisect_right, bisect_left
import math
from operator import itemgetter
from heapq import heapify, heappop, heappush
from queue import PriorityQueue as pq
for _ in range(int(input())):
s=list(input()[:-1])
x=dc(int)
x['A'],x['B'],x['C']=0,0,0
for i in s:
x[i]+=1
x=sorted(x.items(), key=lambda item: item[1])
p,q,r=x[2][0],x[1][0],x[0][0]
a,b,c=x[2][1],x[1][1],x[0][1]
if a!=b+c:
print("NO")
else:
n=len(s)
for i in range(n):
if s[i]!=p:
s[i]=q
if s[0]==s[-1]:
print("NO")
else:
t=1
f=0
for i in range(1,n):
if s[i]==s[0]:
t+=1
else:
if t>0:
t-=1
else:
f=1
break
if f or t>0:
print("NO")
else:
print("YES")
``` | output | 1 | 21,433 | 21 | 42,867 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a string a, consisting of n characters, n is even. For each i from 1 to n a_i is one of 'A', 'B' or 'C'.
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.
You want to find a string b that consists of n characters such that:
* b is a regular bracket sequence;
* if for some i and j (1 ≤ i, j ≤ n) a_i=a_j, then b_i=b_j.
In other words, you want to replace all occurrences of 'A' with the same type of bracket, then all occurrences of 'B' with the same type of bracket and all occurrences of 'C' with the same type of bracket.
Your task is to determine if such a string b exists.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Then the descriptions of t testcases follow.
The only line of each testcase contains a string a. a consists only of uppercase letters 'A', 'B' and 'C'. Let n be the length of a. It is guaranteed that n is even and 2 ≤ n ≤ 50.
Output
For each testcase print "YES" if there exists such a string b that:
* b is a regular bracket sequence;
* if for some i and j (1 ≤ i, j ≤ n) a_i=a_j, then b_i=b_j.
Otherwise, print "NO".
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
Example
Input
4
AABBAC
CACA
BBBBAC
ABCA
Output
YES
YES
NO
NO
Note
In the first testcase one of the possible strings b is "(())()".
In the second testcase one of the possible strings b is "()()". | instruction | 0 | 21,434 | 21 | 42,868 |
Tags: bitmasks, brute force, implementation
Correct Solution:
```
t = int(input())
for _ in range(t):
a = input()
if a[0] == a[-1]:
print('no')
else:
a = a.replace(a[0], '(')
a = a.replace(a[-1], ')')
c1 = a.count('(')
c2 = a.count(')')
c3 = len(a) - c1 - c2
if c1 < c2:
a = a.replace('A', '(')
a = a.replace('B', '(')
a = a.replace('C', '(')
else:
a = a.replace('A', ')')
a = a.replace('B', ')')
a = a.replace('C', ')')
if c1 + c3 != c2 and c2 + c3 != c1:
print('NO')
else:
f = 0
for i in a:
if i == '(':
f += 1
else:
f -= 1
if f < 0:
print('nO')
break
else:
print('YES')
``` | output | 1 | 21,434 | 21 | 42,869 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.