message stringlengths 2 433k | message_type stringclasses 2
values | message_id int64 0 1 | conversation_id int64 113 108k | cluster float64 12 12 | __index_level_0__ int64 226 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q).
Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = ∑_{i = 1}^n |x_i - y_i|.
Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353.
Input
The first line contains a single integer n (1 ≤ n ≤ 150 000).
The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≤ a_i ≤ 10^9) — elements of array a.
Output
Print one integer — the answer to the problem, modulo 998244353.
Examples
Input
1
1 4
Output
6
Input
2
2 1 2 1
Output
12
Input
3
2 2 2 2 2 2
Output
0
Input
5
13 8 35 94 9284 34 54 69 123 846
Output
2588544
Note
Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different.
In the first example, there are two correct partitions of the array a:
1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3;
2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3.
In the second example, there are six valid partitions of the array a:
1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p);
2. p = [2, 2], q = [1, 1];
3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p);
4. p = [1, 2], q = [2, 1];
5. p = [1, 1], q = [2, 2];
6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p). | instruction | 0 | 48,108 | 12 | 96,216 |
Tags: combinatorics, math, sortings
Correct Solution:
```
import sys
from sys import stdin
def modfac(n, MOD):
f = 1
factorials = [1]
for m in range(1, n + 1):
f *= m
f %= MOD
factorials.append(f)
inv = pow(f, MOD - 2, MOD)
invs = [1] * (n + 1)
invs[n] = inv
for m in range(n, 1, -1):
inv *= m
inv %= MOD
invs[m - 1] = inv
return factorials, invs
def modnCr(n,r,mod,fac,inv):
return fac[n] * inv[n-r] * inv[r] % mod
mod = 998244353
n = int(stdin.readline())
a = list(map(int,stdin.readline().split()))
a.sort()
fac,inv = modfac(2*n+10,mod)
print ( (modnCr(2*n,n,mod,fac,inv) * (sum(a[n:]) - sum(a[:n]))) % mod )
``` | output | 1 | 48,108 | 12 | 96,217 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q).
Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = ∑_{i = 1}^n |x_i - y_i|.
Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353.
Input
The first line contains a single integer n (1 ≤ n ≤ 150 000).
The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≤ a_i ≤ 10^9) — elements of array a.
Output
Print one integer — the answer to the problem, modulo 998244353.
Examples
Input
1
1 4
Output
6
Input
2
2 1 2 1
Output
12
Input
3
2 2 2 2 2 2
Output
0
Input
5
13 8 35 94 9284 34 54 69 123 846
Output
2588544
Note
Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different.
In the first example, there are two correct partitions of the array a:
1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3;
2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3.
In the second example, there are six valid partitions of the array a:
1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p);
2. p = [2, 2], q = [1, 1];
3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p);
4. p = [1, 2], q = [2, 1];
5. p = [1, 1], q = [2, 2];
6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p). | instruction | 0 | 48,109 | 12 | 96,218 |
Tags: combinatorics, math, sortings
Correct Solution:
```
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO, IOBase
mod = 998244353
fac = [1]
for i in range(1,300001):
fac.append((fac[-1]*i)%mod)
fac_in = [pow(fac[-1],mod-2,mod)]
for i in range(300000,0,-1):
fac_in.append((fac_in[-1]*i)%mod)
fac_in.reverse()
def comb(a,b):
if a < b:
return 0
return (fac[a]*fac_in[a-b]*fac_in[b])%mod
def main():
n = int(input())
a = sorted(map(int,input().split()))
ans = 0
for i in range(n):
ans += (2*(a[-1-i]-a[i])*comb(2*n-1,n))%mod
ans %= mod
print(ans)
#Fast IO Region
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()
``` | output | 1 | 48,109 | 12 | 96,219 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q).
Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = ∑_{i = 1}^n |x_i - y_i|.
Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353.
Input
The first line contains a single integer n (1 ≤ n ≤ 150 000).
The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≤ a_i ≤ 10^9) — elements of array a.
Output
Print one integer — the answer to the problem, modulo 998244353.
Examples
Input
1
1 4
Output
6
Input
2
2 1 2 1
Output
12
Input
3
2 2 2 2 2 2
Output
0
Input
5
13 8 35 94 9284 34 54 69 123 846
Output
2588544
Note
Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different.
In the first example, there are two correct partitions of the array a:
1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3;
2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3.
In the second example, there are six valid partitions of the array a:
1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p);
2. p = [2, 2], q = [1, 1];
3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p);
4. p = [1, 2], q = [2, 1];
5. p = [1, 1], q = [2, 2];
6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p). | instruction | 0 | 48,110 | 12 | 96,220 |
Tags: combinatorics, math, sortings
Correct Solution:
```
from sys import stdin, gettrace
if gettrace():
inputi = input
else:
def input():
return next(stdin)[:-1]
def inputi():
return stdin.buffer.readline()
MOD = 998244353
def main():
n = int(inputi())
aa = [int(a) for a in inputi().split()]
aa.sort()
sm = sum(aa[n:]) - sum(aa[:n]) % MOD
fac = [1]
for i in range(1, n*2+1):
fac.append((fac[-1] * i)%MOD)
fni = pow(fac[n], MOD-2, MOD)
res = (sm*fac[n*2]*fni*fni)%MOD
print(res)
if __name__ == "__main__":
main()
``` | output | 1 | 48,110 | 12 | 96,221 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q).
Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = ∑_{i = 1}^n |x_i - y_i|.
Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353.
Input
The first line contains a single integer n (1 ≤ n ≤ 150 000).
The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≤ a_i ≤ 10^9) — elements of array a.
Output
Print one integer — the answer to the problem, modulo 998244353.
Examples
Input
1
1 4
Output
6
Input
2
2 1 2 1
Output
12
Input
3
2 2 2 2 2 2
Output
0
Input
5
13 8 35 94 9284 34 54 69 123 846
Output
2588544
Note
Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different.
In the first example, there are two correct partitions of the array a:
1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3;
2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3.
In the second example, there are six valid partitions of the array a:
1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p);
2. p = [2, 2], q = [1, 1];
3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p);
4. p = [1, 2], q = [2, 1];
5. p = [1, 1], q = [2, 2];
6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
def main():
pass
# 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")
# endregion
mod=998244353
mv=150000
def mI(a, m) :
m0 = m
y = 0
x = 1
if (m == 1) :
return 0
while (a > 1) :
# q is quotient
q = a // m
t = m
# m is remainder now, process
# same as Euclid's algo
m = a % m
a = t
t = y
# Update x and y
y = x - q * y
x = t
# Make x positive
if (x < 0) :
x = x + m0
return x
V=[]
v=1
for i in range((mv)+1):
V.append(mI(i,mod))
#print(V)
#print((4*V[2])%mod)
def main():
n=int(input())
A=list(map(int,input().split()))
ans=0
A.sort()
for i in range(n):
ans=(ans+abs(-A[i]+A[(2*n)-i-1]))%mod
c=1
for i in range(n):
c=(c*((2*n)-i))%mod
c=(c*V[i+1])%mod
#print(c)
print((ans*c)%mod)
if __name__ == "__main__":
main()
``` | instruction | 0 | 48,111 | 12 | 96,222 |
Yes | output | 1 | 48,111 | 12 | 96,223 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q).
Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = ∑_{i = 1}^n |x_i - y_i|.
Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353.
Input
The first line contains a single integer n (1 ≤ n ≤ 150 000).
The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≤ a_i ≤ 10^9) — elements of array a.
Output
Print one integer — the answer to the problem, modulo 998244353.
Examples
Input
1
1 4
Output
6
Input
2
2 1 2 1
Output
12
Input
3
2 2 2 2 2 2
Output
0
Input
5
13 8 35 94 9284 34 54 69 123 846
Output
2588544
Note
Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different.
In the first example, there are two correct partitions of the array a:
1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3;
2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3.
In the second example, there are six valid partitions of the array a:
1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p);
2. p = [2, 2], q = [1, 1];
3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p);
4. p = [1, 2], q = [2, 1];
5. p = [1, 1], q = [2, 2];
6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
Submitted Solution:
```
md,n=998244353,int(input())
arr=sorted(list(map(int,input().split())))
sigma=(sum(arr[n:])-sum(arr[:n]))%md
up,dn=1,1
for i in range(1,n+1):
up=(up*(n+i))%md
dn=(dn*i)%md
print((((sigma*up)%md)*pow(dn,md-2,md))%md)
``` | instruction | 0 | 48,112 | 12 | 96,224 |
Yes | output | 1 | 48,112 | 12 | 96,225 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q).
Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = ∑_{i = 1}^n |x_i - y_i|.
Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353.
Input
The first line contains a single integer n (1 ≤ n ≤ 150 000).
The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≤ a_i ≤ 10^9) — elements of array a.
Output
Print one integer — the answer to the problem, modulo 998244353.
Examples
Input
1
1 4
Output
6
Input
2
2 1 2 1
Output
12
Input
3
2 2 2 2 2 2
Output
0
Input
5
13 8 35 94 9284 34 54 69 123 846
Output
2588544
Note
Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different.
In the first example, there are two correct partitions of the array a:
1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3;
2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3.
In the second example, there are six valid partitions of the array a:
1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p);
2. p = [2, 2], q = [1, 1];
3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p);
4. p = [1, 2], q = [2, 1];
5. p = [1, 1], q = [2, 2];
6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
Submitted Solution:
```
#------------------------template--------------------------#
import os
import sys
# from math import *
from collections import *
# from fractions import *
# from heapq import*
from bisect import *
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
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")
ALPHA='abcdefghijklmnopqrstuvwxyz/'
M=1000000007
EPS=1e-6
def Ceil(a,b): return a//b+int(a%b>0)
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
# vsInput()
M=998244353
def nCr(n, r, p=M):
num=1
den=1
for i in range(r):
num=(num*(n-i))%p
den=(den*(i+1))%p
return (num*pow(den,p-2,p))%p
n=Int()
a=sorted(array(),reverse=True)
total=nCr(2*n,n)
ans=0
for i in range(2*n):
if(i<n): ans=(ans+a[i])%M
else: ans=(ans-a[i]+M)%M
print((total*ans)%M)
``` | instruction | 0 | 48,113 | 12 | 96,226 |
Yes | output | 1 | 48,113 | 12 | 96,227 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q).
Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = ∑_{i = 1}^n |x_i - y_i|.
Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353.
Input
The first line contains a single integer n (1 ≤ n ≤ 150 000).
The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≤ a_i ≤ 10^9) — elements of array a.
Output
Print one integer — the answer to the problem, modulo 998244353.
Examples
Input
1
1 4
Output
6
Input
2
2 1 2 1
Output
12
Input
3
2 2 2 2 2 2
Output
0
Input
5
13 8 35 94 9284 34 54 69 123 846
Output
2588544
Note
Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different.
In the first example, there are two correct partitions of the array a:
1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3;
2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3.
In the second example, there are six valid partitions of the array a:
1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p);
2. p = [2, 2], q = [1, 1];
3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p);
4. p = [1, 2], q = [2, 1];
5. p = [1, 1], q = [2, 2];
6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
Submitted Solution:
```
n=int(input())
m=998244353
a=list(map(int,input().split()))
a.sort()
num=(sum(a[n:])-sum(a[:n]))%m
den=1
for i in range(1,n+1):
num=(num*(2*n-i+1))%m
den=(den*pow(i,m-2,m))%m
print((den*num)%m)
``` | instruction | 0 | 48,114 | 12 | 96,228 |
Yes | output | 1 | 48,114 | 12 | 96,229 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q).
Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = ∑_{i = 1}^n |x_i - y_i|.
Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353.
Input
The first line contains a single integer n (1 ≤ n ≤ 150 000).
The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≤ a_i ≤ 10^9) — elements of array a.
Output
Print one integer — the answer to the problem, modulo 998244353.
Examples
Input
1
1 4
Output
6
Input
2
2 1 2 1
Output
12
Input
3
2 2 2 2 2 2
Output
0
Input
5
13 8 35 94 9284 34 54 69 123 846
Output
2588544
Note
Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different.
In the first example, there are two correct partitions of the array a:
1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3;
2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3.
In the second example, there are six valid partitions of the array a:
1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p);
2. p = [2, 2], q = [1, 1];
3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p);
4. p = [1, 2], q = [2, 1];
5. p = [1, 1], q = [2, 2];
6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
Submitted Solution:
```
def ncr(n, r, p):
# initialize numerator
# and denominator
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
def solve(n,a):
a.sort()
M = 998244353
ss = sum(a[:n])
ls = sum(a[n:])
diff = (ls-ss)%M
return diff * ncr(2*n,n,M)
n = int(input())
a = list(map(int,input().split()))
print(solve(n,a))
``` | instruction | 0 | 48,115 | 12 | 96,230 |
No | output | 1 | 48,115 | 12 | 96,231 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q).
Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = ∑_{i = 1}^n |x_i - y_i|.
Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353.
Input
The first line contains a single integer n (1 ≤ n ≤ 150 000).
The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≤ a_i ≤ 10^9) — elements of array a.
Output
Print one integer — the answer to the problem, modulo 998244353.
Examples
Input
1
1 4
Output
6
Input
2
2 1 2 1
Output
12
Input
3
2 2 2 2 2 2
Output
0
Input
5
13 8 35 94 9284 34 54 69 123 846
Output
2588544
Note
Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different.
In the first example, there are two correct partitions of the array a:
1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3;
2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3.
In the second example, there are six valid partitions of the array a:
1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p);
2. p = [2, 2], q = [1, 1];
3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p);
4. p = [1, 2], q = [2, 1];
5. p = [1, 1], q = [2, 2];
6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
Submitted Solution:
```
from math import comb
n = int(input())
ss = input().split()
l = [int(s) for s in ss]
s = 0
for i in range(n):
s += abs(l[i] - l[n+i])
res = (s*(comb(2*n, n)% 998244353)) % 998244353
print(str(int(res)))
``` | instruction | 0 | 48,116 | 12 | 96,232 |
No | output | 1 | 48,116 | 12 | 96,233 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q).
Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = ∑_{i = 1}^n |x_i - y_i|.
Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353.
Input
The first line contains a single integer n (1 ≤ n ≤ 150 000).
The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≤ a_i ≤ 10^9) — elements of array a.
Output
Print one integer — the answer to the problem, modulo 998244353.
Examples
Input
1
1 4
Output
6
Input
2
2 1 2 1
Output
12
Input
3
2 2 2 2 2 2
Output
0
Input
5
13 8 35 94 9284 34 54 69 123 846
Output
2588544
Note
Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different.
In the first example, there are two correct partitions of the array a:
1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3;
2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3.
In the second example, there are six valid partitions of the array a:
1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p);
2. p = [2, 2], q = [1, 1];
3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p);
4. p = [1, 2], q = [2, 1];
5. p = [1, 1], q = [2, 2];
6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
Submitted Solution:
```
import random
def quicksort(nums):
if len(nums) <= 1:
return nums
else:
q = random.choice(nums)
s_nums = []
m_nums = []
e_nums = []
for i in range(len(nums)):
if nums[i] < q:
s_nums.append(nums[i])
elif nums[i] > q:
m_nums.append(nums[i])
else:
e_nums.append(nums[i])
return quicksort(s_nums) + e_nums + quicksort(m_nums)
n = int(input())
s = input()
s = s.split()
for i in range(len(s)):
s[i] = int(s[i])
s = quicksort(s)
k = 0
for i in range (n):
k -= s[i]
k += s[i+n]
k %= 998244353
for i in range(1, n + 1):
k = (k*(i + n)/i) % 998244353
print(int(k))
``` | instruction | 0 | 48,117 | 12 | 96,234 |
No | output | 1 | 48,117 | 12 | 96,235 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q).
Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = ∑_{i = 1}^n |x_i - y_i|.
Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353.
Input
The first line contains a single integer n (1 ≤ n ≤ 150 000).
The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≤ a_i ≤ 10^9) — elements of array a.
Output
Print one integer — the answer to the problem, modulo 998244353.
Examples
Input
1
1 4
Output
6
Input
2
2 1 2 1
Output
12
Input
3
2 2 2 2 2 2
Output
0
Input
5
13 8 35 94 9284 34 54 69 123 846
Output
2588544
Note
Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different.
In the first example, there are two correct partitions of the array a:
1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3;
2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3.
In the second example, there are six valid partitions of the array a:
1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p);
2. p = [2, 2], q = [1, 1];
3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p);
4. p = [1, 2], q = [2, 1];
5. p = [1, 1], q = [2, 2];
6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
Submitted Solution:
```
from math import comb
n=int(input())
arr=list(map(int,input().split()))
arr=sorted(arr)
ans=0
hi=2
lo=1
for i in range(2,n+1):
lo=(lo*i*i)%998244353
hi=hi*2*i*(2*i-1)%998244353
if lo==0:
print(0)
else:
if hi%lo==0:
t=hi//lo
else:
while hi%lo==0:
hi=hi+998244353
hi=hi%lo
t=hi//lo
end=sum(arr[:n])%998244353
st=sum(arr[n:])%998244353
ans=t*(st-end)%998244353
print(ans)
``` | instruction | 0 | 48,118 | 12 | 96,236 |
No | output | 1 | 48,118 | 12 | 96,237 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≤ r).
He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [l; r] to either segment [l - 1; r], or to segment [l; r + 1].
The value of a set of segments that consists of n segments [l1; r1], [l2; r2], ..., [ln; rn] is the number of integers x, such that there is integer j, for which the following inequality holds, lj ≤ x ≤ rj.
Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≤ li ≤ ri ≤ 105), separated by a space.
It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≤ i < j ≤ n) the following inequality holds, min(ri, rj) < max(li, lj).
Output
In a single line print a single integer — the answer to the problem.
Examples
Input
2 3
1 2
3 4
Output
2
Input
3 7
1 2
3 3
4 7
Output
0 | instruction | 0 | 48,227 | 12 | 96,454 |
Tags: brute force, implementation
Correct Solution:
```
import sys
input = sys.stdin.readline
n, k = [int(x) for x in input().split()]
summ = 0
for _ in range(n):
l, r = list(map(int, input().split()))
summ += r-l+1
if summ % k == 0: print(0)
else: print((summ//k+1)*k-summ)
``` | output | 1 | 48,227 | 12 | 96,455 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≤ r).
He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [l; r] to either segment [l - 1; r], or to segment [l; r + 1].
The value of a set of segments that consists of n segments [l1; r1], [l2; r2], ..., [ln; rn] is the number of integers x, such that there is integer j, for which the following inequality holds, lj ≤ x ≤ rj.
Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≤ li ≤ ri ≤ 105), separated by a space.
It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≤ i < j ≤ n) the following inequality holds, min(ri, rj) < max(li, lj).
Output
In a single line print a single integer — the answer to the problem.
Examples
Input
2 3
1 2
3 4
Output
2
Input
3 7
1 2
3 3
4 7
Output
0 | instruction | 0 | 48,228 | 12 | 96,456 |
Tags: brute force, implementation
Correct Solution:
```
n,k=map(int,input().split())
arr=[]
c=0
for i in range(n):
a,b=map(int,input().split())
c=c+b-a+1
if c%k==0:
print(0)
else:
print(k-c%k)
``` | output | 1 | 48,228 | 12 | 96,457 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≤ r).
He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [l; r] to either segment [l - 1; r], or to segment [l; r + 1].
The value of a set of segments that consists of n segments [l1; r1], [l2; r2], ..., [ln; rn] is the number of integers x, such that there is integer j, for which the following inequality holds, lj ≤ x ≤ rj.
Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≤ li ≤ ri ≤ 105), separated by a space.
It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≤ i < j ≤ n) the following inequality holds, min(ri, rj) < max(li, lj).
Output
In a single line print a single integer — the answer to the problem.
Examples
Input
2 3
1 2
3 4
Output
2
Input
3 7
1 2
3 3
4 7
Output
0 | instruction | 0 | 48,229 | 12 | 96,458 |
Tags: brute force, implementation
Correct Solution:
```
# A. Police Recruits
n, k = map(int, input().split())
res = 0
for i in range(n):
start, end = map(int, input().split())
res += end - start + 1
print(0 if not res%k else k - res%k)
``` | output | 1 | 48,229 | 12 | 96,459 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≤ r).
He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [l; r] to either segment [l - 1; r], or to segment [l; r + 1].
The value of a set of segments that consists of n segments [l1; r1], [l2; r2], ..., [ln; rn] is the number of integers x, such that there is integer j, for which the following inequality holds, lj ≤ x ≤ rj.
Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≤ li ≤ ri ≤ 105), separated by a space.
It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≤ i < j ≤ n) the following inequality holds, min(ri, rj) < max(li, lj).
Output
In a single line print a single integer — the answer to the problem.
Examples
Input
2 3
1 2
3 4
Output
2
Input
3 7
1 2
3 3
4 7
Output
0 | instruction | 0 | 48,230 | 12 | 96,460 |
Tags: brute force, implementation
Correct Solution:
```
n, k = [int(x) for x in input().split()]
s = 0
for i in range(n):
l, r = [int(x) for x in input().split()]
s += r - l + 1
print((k - s % k) % k)
``` | output | 1 | 48,230 | 12 | 96,461 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≤ r).
He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [l; r] to either segment [l - 1; r], or to segment [l; r + 1].
The value of a set of segments that consists of n segments [l1; r1], [l2; r2], ..., [ln; rn] is the number of integers x, such that there is integer j, for which the following inequality holds, lj ≤ x ≤ rj.
Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≤ li ≤ ri ≤ 105), separated by a space.
It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≤ i < j ≤ n) the following inequality holds, min(ri, rj) < max(li, lj).
Output
In a single line print a single integer — the answer to the problem.
Examples
Input
2 3
1 2
3 4
Output
2
Input
3 7
1 2
3 3
4 7
Output
0 | instruction | 0 | 48,231 | 12 | 96,462 |
Tags: brute force, implementation
Correct Solution:
```
n, k = map(int, input().split())
res = 0
for i in range(n):
l, r = map(int, input().split())
res += r - l + 1
print((k - res % k) % k)
``` | output | 1 | 48,231 | 12 | 96,463 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≤ r).
He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [l; r] to either segment [l - 1; r], or to segment [l; r + 1].
The value of a set of segments that consists of n segments [l1; r1], [l2; r2], ..., [ln; rn] is the number of integers x, such that there is integer j, for which the following inequality holds, lj ≤ x ≤ rj.
Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≤ li ≤ ri ≤ 105), separated by a space.
It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≤ i < j ≤ n) the following inequality holds, min(ri, rj) < max(li, lj).
Output
In a single line print a single integer — the answer to the problem.
Examples
Input
2 3
1 2
3 4
Output
2
Input
3 7
1 2
3 3
4 7
Output
0 | instruction | 0 | 48,232 | 12 | 96,464 |
Tags: brute force, implementation
Correct Solution:
```
from sys import stdin
n,k=map(int,stdin.readline().split())
ans=0
for i in range(n):
l,r=map(int,stdin.readline().split())
p=r-l+1
ans+=p
r=ans%k
if r==0:
print(0)
else:
print(k-r)
``` | output | 1 | 48,232 | 12 | 96,465 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a permutation of numbers from 1 to n. Determine whether there's a pair of integers a, b (1 ≤ a, b ≤ n; a ≠ b) such that the element <image> (note, that it is usual division, not integer one) is between a and b in this permutation.
Input
First line consists of a single integer n (1 ≤ n ≤ 300000) — the size of permutation.
Second line contains n integers — the permutation itself.
Output
Print "YES", if such a pair exists, "NO" otherwise (in both cases without quotes, the answer is case insensitive).
Examples
Input
4
1 3 4 2
Output
NO
Input
5
1 5 2 4 3
Output
YES
Note
In the second example 2 is between 1 and 3. Additionally 4 is between 3 and 5. | instruction | 0 | 48,299 | 12 | 96,598 |
Tags: data structures, divide and conquer, hashing
Correct Solution:
```
n = int(input())
v = list(map(int, input().split()))
ans = "NO"
p = []
for i in range(n + 1):
p.append(-1)
for i in range(n):
p[v[i]] = i
for i in range(n - 1):
for j in range(i + 1, min(n, i + 6)):
if v[i] * 2 - v[j] >= 1 and v[i] * 2 - v[j] <= n and p[v[i] * 2 - v[j]] < i:
ans = "YES"
break
if v[j] * 2 - v[i] >= 1 and v[j] * 2 - v[i] <= n and p[v[j] * 2 - v[i]] > j:
ans = "YES"
break
if ans == "YES":
break
print(ans)
``` | output | 1 | 48,299 | 12 | 96,599 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a permutation of numbers from 1 to n. Determine whether there's a pair of integers a, b (1 ≤ a, b ≤ n; a ≠ b) such that the element <image> (note, that it is usual division, not integer one) is between a and b in this permutation.
Input
First line consists of a single integer n (1 ≤ n ≤ 300000) — the size of permutation.
Second line contains n integers — the permutation itself.
Output
Print "YES", if such a pair exists, "NO" otherwise (in both cases without quotes, the answer is case insensitive).
Examples
Input
4
1 3 4 2
Output
NO
Input
5
1 5 2 4 3
Output
YES
Note
In the second example 2 is between 1 and 3. Additionally 4 is between 3 and 5.
Submitted Solution:
```
n = int(input())
v = list(map(int, input().split()))
ans = "NO"
index = []
for i in range(n + 1):
index.append(-1)
for i in range(n):
index[v[i]] = i
for i in range(n - 1):
for j in range(i + 1, min(n, i + 6)):
t = v[i] + v[j]
if t % 2 == 0 and index[t // 2] > i and index[t // 2] < j:
ans = "YES"
break
if ans == "YES":
break
print(ans)
``` | instruction | 0 | 48,300 | 12 | 96,600 |
No | output | 1 | 48,300 | 12 | 96,601 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given a permutation of numbers from 1 to n. Determine whether there's a pair of integers a, b (1 ≤ a, b ≤ n; a ≠ b) such that the element <image> (note, that it is usual division, not integer one) is between a and b in this permutation.
Input
First line consists of a single integer n (1 ≤ n ≤ 300000) — the size of permutation.
Second line contains n integers — the permutation itself.
Output
Print "YES", if such a pair exists, "NO" otherwise (in both cases without quotes, the answer is case insensitive).
Examples
Input
4
1 3 4 2
Output
NO
Input
5
1 5 2 4 3
Output
YES
Note
In the second example 2 is between 1 and 3. Additionally 4 is between 3 and 5.
Submitted Solution:
```
n = int(input())
v = list(map(int, input().split()))
ans = "NO"
d = 0
hash = []
for i in range(1 - n, n):
hash.append(-1)
for i in range(1, n):
d += (v[i] - v[i - 1])
if d % 2 == 0 and hash[(d // 2) + n - 1] == 1:
ans = "YES"
break
hash[d + n - 1] = 1
print(ans)
``` | instruction | 0 | 48,302 | 12 | 96,604 |
No | output | 1 | 48,302 | 12 | 96,605 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given matrix with n rows and n columns filled with zeroes. You should put k ones in it in such a way that the resulting matrix is symmetrical with respect to the main diagonal (the diagonal that goes from the top left to the bottom right corner) and is lexicographically maximal.
One matrix is lexicographically greater than the other if the first different number in the first different row from the top in the first matrix is greater than the corresponding number in the second one.
If there exists no such matrix then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ 106).
Output
If the answer exists then output resulting matrix. Otherwise output -1.
Examples
Input
2 1
Output
1 0
0 0
Input
3 2
Output
1 0 0
0 1 0
0 0 0
Input
2 5
Output
-1 | instruction | 0 | 48,483 | 12 | 96,966 |
Tags: constructive algorithms
Correct Solution:
```
n, k = map(int, input().split())
if n * n < k:
print(-1)
else:
arr = [[0 for _ in range(n)] for _ in range(n)]
cnt = 0
for i in range(n):
if cnt < k:
arr[i][i] = 1
cnt += 1
for j in range(i + 1, n):
if cnt <= k - 2:
arr[i][j] = 1
arr[j][i] = 1
cnt += 2
for r in arr:
print(' '.join(map(str, r)))
``` | output | 1 | 48,483 | 12 | 96,967 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given matrix with n rows and n columns filled with zeroes. You should put k ones in it in such a way that the resulting matrix is symmetrical with respect to the main diagonal (the diagonal that goes from the top left to the bottom right corner) and is lexicographically maximal.
One matrix is lexicographically greater than the other if the first different number in the first different row from the top in the first matrix is greater than the corresponding number in the second one.
If there exists no such matrix then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ 106).
Output
If the answer exists then output resulting matrix. Otherwise output -1.
Examples
Input
2 1
Output
1 0
0 0
Input
3 2
Output
1 0 0
0 1 0
0 0 0
Input
2 5
Output
-1 | instruction | 0 | 48,484 | 12 | 96,968 |
Tags: constructive algorithms
Correct Solution:
```
n,k = map(int,input().split())
ks = k
l = []
for i in range(n):
p = []
for j in range(n):
p.append(0)
l.append(p)
if(k>n**2):
print(-1)
else:
for i in range(n):
for j in range(n):
if k==0:
break
if(i==j):
k -=1
l[i][j] = 1
else:
if k>1 and l[j][i]==0:
k-=2
l[i][j] = 1
l[j][i] = 1
ones = 0
for row in l:
ones += row.count(1)
if ones!=ks:
print(-1)
else:
for i in l:
s = ""
for j in i:
s += " "+str(j)
print(s[1:])
``` | output | 1 | 48,484 | 12 | 96,969 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given matrix with n rows and n columns filled with zeroes. You should put k ones in it in such a way that the resulting matrix is symmetrical with respect to the main diagonal (the diagonal that goes from the top left to the bottom right corner) and is lexicographically maximal.
One matrix is lexicographically greater than the other if the first different number in the first different row from the top in the first matrix is greater than the corresponding number in the second one.
If there exists no such matrix then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ 106).
Output
If the answer exists then output resulting matrix. Otherwise output -1.
Examples
Input
2 1
Output
1 0
0 0
Input
3 2
Output
1 0 0
0 1 0
0 0 0
Input
2 5
Output
-1 | instruction | 0 | 48,488 | 12 | 96,976 |
Tags: constructive algorithms
Correct Solution:
```
n,k = map(int,input().split())
if n*n<k:
print(-1)
else:
arr = [ [0]*n for i in range(n)]
if k!=0:
arr[0][0] = 1
k -= 1
i = 0
j = 1
while k>1 and i<n-1:
arr[i][j] = 1
arr[j][i] = 1
j += 1
if i==j-1:
k -= 1
else:
k -= 2
if j==n:
i += 1
j = i
i = 1
while k!=0 and i<n:
if arr[i][i]==0:
arr[i][i] = 1
k -= 1
i += 1
for i in range(n):
print(*arr[i])
``` | output | 1 | 48,488 | 12 | 96,977 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given matrix with n rows and n columns filled with zeroes. You should put k ones in it in such a way that the resulting matrix is symmetrical with respect to the main diagonal (the diagonal that goes from the top left to the bottom right corner) and is lexicographically maximal.
One matrix is lexicographically greater than the other if the first different number in the first different row from the top in the first matrix is greater than the corresponding number in the second one.
If there exists no such matrix then output -1.
Input
The first line consists of two numbers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ 106).
Output
If the answer exists then output resulting matrix. Otherwise output -1.
Examples
Input
2 1
Output
1 0
0 0
Input
3 2
Output
1 0 0
0 1 0
0 0 0
Input
2 5
Output
-1 | instruction | 0 | 48,489 | 12 | 96,978 |
Tags: constructive algorithms
Correct Solution:
```
n,k=map(int,input().split())
if k>n**2:
exit(print(-1))
else:
a=[[0]*n for i in range(n)]
for i in range(n):
for j in range(i,n):
if i!=j and k==1:
a[i+1][i+1]=1;k-=1
elif i==j and k>0:
a[i][j]=1;k-=1
elif k>=2:
a[i][j]=1;a[j][i]=1;k-=2
elif k==0:
break
for i in a:
print(*i)
``` | output | 1 | 48,489 | 12 | 96,979 |
Provide a correct Python 3 solution for this coding contest problem.
Given are an integer K and integers a_1,\dots, a_K. Determine whether a sequence P satisfying below exists. If it exists, find the lexicographically smallest such sequence.
* Every term in P is an integer between 1 and K (inclusive).
* For each i=1,\dots, K, P contains a_i occurrences of i.
* For each term in P, there is a contiguous subsequence of length K that contains that term and is a permutation of 1,\dots, K.
Constraints
* 1 \leq K \leq 100
* 1 \leq a_i \leq 1000 \quad (1\leq i\leq K)
* a_1 + \dots + a_K\leq 1000
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
K
a_1 a_2 \dots a_K
Output
If there is no sequence satisfying the conditions, print `-1`. Otherwise, print the lexicographically smallest sequence satisfying the conditions.
Examples
Input
3
2 4 3
Output
2 1 3 2 2 3 1 2 3
Input
4
3 2 3 2
Output
1 2 3 4 1 3 1 2 4 3
Input
5
3 1 4 1 5
Output
-1 | instruction | 0 | 48,553 | 12 | 97,106 |
"Correct Solution:
```
import sys
def find_permutation(aaa, use):
"""
iの残り使用数がaaa[i-1]の状態で
集合useにある文字群(1~k)を後ろに繋げる方法で
それよりさらに後が破綻しないような繋げ方のうち
辞書順最小のものを求める。
ただし(この関数からは見えないが)現在確定済み配列の
末尾 (k - |use|) 個は、useに含まれない要素が1回ずつ登場することを前提とする。
(つまり、この関数の結果を繋げると、末尾 k 個が順列になる)
どうやっても破綻する場合はNoneを返す。
:param aaa:
:param use:
:return:
"""
max_a = -1
min_a = 1005
max_fixed = -1
for i in range(k):
a = aaa[i]
if i + 1 in use:
min_a = min(min_a, a)
max_a = max(max_a, a)
else:
max_fixed = max(max_fixed, a)
if max(max_a, max_fixed + 1) > 2 * min_a:
return None
if max_a < 2 * min_a:
return sorted(use)
front = []
rear = []
either = []
for i in use:
if aaa[i - 1] == max_a:
front.append(i)
elif aaa[i - 1] == min_a:
rear.append(i)
else:
either.append(i)
max_front = front[-1]
for i in either:
if i < max_front:
front.append(i)
else:
rear.append(i)
front.sort()
rear.sort()
front.extend(rear)
return front
def solve(k, aaa):
if k == 1:
return [1] * aaa[0]
min_a = min(aaa)
max_a = max(aaa)
if min_a * 2 < max_a:
return [-1]
ans = []
ans.extend(find_permutation(aaa, set(range(1, k + 1))))
for i in range(k):
aaa[i] -= 1
remaining = sum(aaa)
while remaining:
use = set(range(1, k + 1))
candidates = []
for r in range(k):
result = find_permutation(aaa, use)
if result is not None:
candidates.append(result)
use.remove(ans[-r - 1])
adopted = min(candidates)
ans.extend(adopted)
for i in adopted:
aaa[i - 1] -= 1
remaining -= len(adopted)
return ans
k, *aaa = map(int, sys.stdin.buffer.read().split())
print(*solve(k, aaa))
``` | output | 1 | 48,553 | 12 | 97,107 |
Provide a correct Python 3 solution for this coding contest problem.
Let N be a positive integer. You are given a string s of length N - 1, consisting of `<` and `>`.
Find the number of permutations (p_1, p_2, \ldots, p_N) of (1, 2, \ldots, N) that satisfy the following condition, modulo 10^9 + 7:
* For each i (1 \leq i \leq N - 1), p_i < p_{i + 1} if the i-th character in s is `<`, and p_i > p_{i + 1} if the i-th character in s is `>`.
Constraints
* N is an integer.
* 2 \leq N \leq 3000
* s is a string of length N - 1.
* s consists of `<` and `>`.
Input
Input is given from Standard Input in the following format:
N
s
Output
Print the number of permutations that satisfy the condition, modulo 10^9 + 7.
Examples
Input
4
<><
Output
5
Input
5
<<<<
Output
1
Input
20
>>>><>>><>><>>><<>>
Output
217136290 | instruction | 0 | 48,606 | 12 | 97,212 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline
def solve():
N = int(rl())
s = input()
MOD = 10 ** 9 + 7
dp = [[0] * N for _ in range(N)]
for i in range(N):
dp[0][i] = 1
acc = [i for i in range(1, N + 1)]
for i in range(1, N):
for j in range(N - i + 1):
if s[i - 1] == '<':
dp[i][j] = (acc[N - i] - acc[j]) % MOD
else:
dp[i][j] = acc[j]
acc[0] = dp[i][0]
for j in range(1, N):
acc[j] = (acc[j - 1] + dp[i][j]) % MOD
print(dp[-1][0])
if __name__ == '__main__':
solve()
``` | output | 1 | 48,606 | 12 | 97,213 |
Provide a correct Python 3 solution for this coding contest problem.
Let N be a positive integer. You are given a string s of length N - 1, consisting of `<` and `>`.
Find the number of permutations (p_1, p_2, \ldots, p_N) of (1, 2, \ldots, N) that satisfy the following condition, modulo 10^9 + 7:
* For each i (1 \leq i \leq N - 1), p_i < p_{i + 1} if the i-th character in s is `<`, and p_i > p_{i + 1} if the i-th character in s is `>`.
Constraints
* N is an integer.
* 2 \leq N \leq 3000
* s is a string of length N - 1.
* s consists of `<` and `>`.
Input
Input is given from Standard Input in the following format:
N
s
Output
Print the number of permutations that satisfy the condition, modulo 10^9 + 7.
Examples
Input
4
<><
Output
5
Input
5
<<<<
Output
1
Input
20
>>>><>>><>><>>><<>>
Output
217136290 | instruction | 0 | 48,608 | 12 | 97,216 |
"Correct Solution:
```
N=int(input())
s=input()
dp=[[0 for _ in range(N+1)] for i in range(N+1)]
MOD=10**9+7
dp[0][N]=1
dp[1]=[1]*N+[0]
cum=[0 for _ in range(N+2)]
for i in range(1,N):
cum[0]=0
for z in range(1,N-i+2):
cum[z]=(cum[z-1]+dp[i][z-1])%MOD
if s[i-1]=='<':
for j in range(0,N-i):
dp[i+1][j] =(dp[i+1][j] +cum[N-i+1]-cum[j+1])%MOD
else:
for j in range(0,N-i):
dp[i+1][j]=(dp[i+1][j] + cum[j+1])%MOD
print(dp[N][0])
``` | output | 1 | 48,608 | 12 | 97,217 |
Provide a correct Python 3 solution for this coding contest problem.
Let N be a positive integer. You are given a string s of length N - 1, consisting of `<` and `>`.
Find the number of permutations (p_1, p_2, \ldots, p_N) of (1, 2, \ldots, N) that satisfy the following condition, modulo 10^9 + 7:
* For each i (1 \leq i \leq N - 1), p_i < p_{i + 1} if the i-th character in s is `<`, and p_i > p_{i + 1} if the i-th character in s is `>`.
Constraints
* N is an integer.
* 2 \leq N \leq 3000
* s is a string of length N - 1.
* s consists of `<` and `>`.
Input
Input is given from Standard Input in the following format:
N
s
Output
Print the number of permutations that satisfy the condition, modulo 10^9 + 7.
Examples
Input
4
<><
Output
5
Input
5
<<<<
Output
1
Input
20
>>>><>>><>><>>><<>>
Output
217136290 | instruction | 0 | 48,609 | 12 | 97,218 |
"Correct Solution:
```
n = int(input())
S = input()
p = 10**9+7
DP = [[0 for j in range(n+1)] for i in range(n+1)]
for j in range(n):
DP[1][j] = 1
for i in range(2, n+1):
A = [0]
for j in range(n):
A.append(A[-1]+DP[i-1][j])
for j in range(n-i+1):
if S[i-2] == '<':
DP[i][j] = (A[n-(i-1)+1]-A[j+1]) % p
else:
DP[i][j] = A[j+1] % p
print(DP[n][0])
``` | output | 1 | 48,609 | 12 | 97,219 |
Provide a correct Python 3 solution for this coding contest problem.
Let N be a positive integer. You are given a string s of length N - 1, consisting of `<` and `>`.
Find the number of permutations (p_1, p_2, \ldots, p_N) of (1, 2, \ldots, N) that satisfy the following condition, modulo 10^9 + 7:
* For each i (1 \leq i \leq N - 1), p_i < p_{i + 1} if the i-th character in s is `<`, and p_i > p_{i + 1} if the i-th character in s is `>`.
Constraints
* N is an integer.
* 2 \leq N \leq 3000
* s is a string of length N - 1.
* s consists of `<` and `>`.
Input
Input is given from Standard Input in the following format:
N
s
Output
Print the number of permutations that satisfy the condition, modulo 10^9 + 7.
Examples
Input
4
<><
Output
5
Input
5
<<<<
Output
1
Input
20
>>>><>>><>><>>><<>>
Output
217136290 | instruction | 0 | 48,610 | 12 | 97,220 |
"Correct Solution:
```
# coding: utf-8
import sys
input = sys.stdin.readline
def f2(N, s):
MOD = int(1e9 + 7)
dp = [1] * N
for i in range(1, N):
csum = list(dp)
for j in range(1, N - i + 1):
csum[j] += csum[j - 1]
csum[j] %= MOD
csum_ = csum[N - i]
i_ = i - 1
for j in range(N - i): # N-(i+1)+1 = N-i
dp[j] = csum[j] if s[i_] == ">" else \
(csum_ - csum[j] + MOD) % MOD
return(dp[0])
N = int(input()) # 2 <= N <= 30000
s = input().rstrip() # len(s) = N - 1
print(f2(N, s))
``` | output | 1 | 48,610 | 12 | 97,221 |
Provide a correct Python 3 solution for this coding contest problem.
Let N be a positive integer. You are given a string s of length N - 1, consisting of `<` and `>`.
Find the number of permutations (p_1, p_2, \ldots, p_N) of (1, 2, \ldots, N) that satisfy the following condition, modulo 10^9 + 7:
* For each i (1 \leq i \leq N - 1), p_i < p_{i + 1} if the i-th character in s is `<`, and p_i > p_{i + 1} if the i-th character in s is `>`.
Constraints
* N is an integer.
* 2 \leq N \leq 3000
* s is a string of length N - 1.
* s consists of `<` and `>`.
Input
Input is given from Standard Input in the following format:
N
s
Output
Print the number of permutations that satisfy the condition, modulo 10^9 + 7.
Examples
Input
4
<><
Output
5
Input
5
<<<<
Output
1
Input
20
>>>><>>><>><>>><<>>
Output
217136290 | instruction | 0 | 48,611 | 12 | 97,222 |
"Correct Solution:
```
N, = map(int, input().split())
M = 10**9+7
s = input().strip()
dp = [[0]*N for _ in range(N)]
for i in range(N):
dp[0][i] = 1
ss = N
for i, c in enumerate(s):
bs = 0
if c == ">":
for j in range(N-i):
ss -= dp[i][j]
dp[i+1][j] = ss%M
# dp[i+1][j] = sum(dp[i][j+1:])
bs = (bs+ss)%M
else:
ss = 0
for j in range(N-i-1):
ss += dp[i][j]
dp[i+1][j] = ss%M
bs = (bs+ss)%M
# dp[i+1][j] = sum(dp[i][:j+1])
ss = bs%M
print(dp[-1][0]%M)
``` | output | 1 | 48,611 | 12 | 97,223 |
Provide a correct Python 3 solution for this coding contest problem.
Let N be a positive integer. You are given a string s of length N - 1, consisting of `<` and `>`.
Find the number of permutations (p_1, p_2, \ldots, p_N) of (1, 2, \ldots, N) that satisfy the following condition, modulo 10^9 + 7:
* For each i (1 \leq i \leq N - 1), p_i < p_{i + 1} if the i-th character in s is `<`, and p_i > p_{i + 1} if the i-th character in s is `>`.
Constraints
* N is an integer.
* 2 \leq N \leq 3000
* s is a string of length N - 1.
* s consists of `<` and `>`.
Input
Input is given from Standard Input in the following format:
N
s
Output
Print the number of permutations that satisfy the condition, modulo 10^9 + 7.
Examples
Input
4
<><
Output
5
Input
5
<<<<
Output
1
Input
20
>>>><>>><>><>>><<>>
Output
217136290 | instruction | 0 | 48,612 | 12 | 97,224 |
"Correct Solution:
```
import sys,bisect,string,math,time,functools,random,fractions
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
rep=range;R=range
def Golf():n,*t=map(int,open(0).read().split())
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def MI():return map(int,input().split())
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def NLI(n):return [[int(i) for i in input().split()] for i in range(n)]
def NLI_(n):return [[int(i)-1 for i in input().split()] for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def RA():return map(int,open(0).read().split())
def RLI(n=8,a=1,b=10):return [random.randint(a,b)for i in range(n)]
def RI(a=1,b=10):return random.randint(a,b)
def Rtest(T):
case,err=0,0
for i in range(T):
inp=INP()
a1,ls=naive(*inp)
a2=solve(*inp)
if a1!=a2:
print((a1,a2),inp)
err+=1
case+=1
print('Tested',case,'case with',err,'errors')
def GI(V,E,ls=None,Directed=False,index=1):
org_inp=[];g=[[] for i in range(V)]
FromStdin=True if ls==None else False
for i in range(E):
if FromStdin:
inp=LI()
org_inp.append(inp)
else:
inp=ls[i]
if len(inp)==2:
a,b=inp;c=1
else:
a,b,c=inp
if index==1:a-=1;b-=1
aa=(a,c);bb=(b,c);g[a].append(bb)
if not Directed:g[b].append(aa)
return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1) # sample usage
mp=[boundary]*(w+2);found={}
for i in R(h):
s=input()
for char in search:
if char in s:
found[char]=((i+1)*(w+2)+s.index(char)+1)
mp_def[char]=mp_def[replacement_of_found]
mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
mp+=[boundary]*(w+2)
return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def accum(ls):
rt=[0]
for i in ls:rt+=[rt[-1]+i]
return rt
def bit_combination(n,base=2):
rt=[]
for tb in R(base**n):s=[tb//(base**bt)%base for bt in R(n)];rt+=[s]
return rt
def gcd(x,y):
if y==0:return x
if x%y==0:return y
while x%y!=0:x,y=y,x%y
return y
def YN(x):print(['NO','YES'][x])
def Yn(x):print(['No','Yes'][x])
def show(*inp,end='\n'):
if show_flg:print(*inp,end=end)
mo=10**9+7
#mo=998244353
inf=float('inf')
FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb))
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**9)
read=sys.stdin.buffer.read;readline=sys.stdin.buffer.readline;input=lambda:sys.stdin.readline().rstrip()
## return prime factors of N as dictionary {prime p:power of p}
## within 2 sec for N = 2*10**20+7
def primeFactor(N):
i,n=2,N
ret={}
d,sq=2,99
while i<=sq:
k=0
while n%i==0:
n,k,ret[i]=n//i,k+1,k+1
if k>0 or i==97:
sq=int(n**(1/2)+0.5)
if i<4:
i=i*2-1
else:
i,d=i+d,d^6
if n>1:
ret[n]=1
return ret
## return divisors of n as list
def divisor(n):
div=[1]
for i,j in primeFactor(n).items():
div=[(i**k)*d for d in div for k in range(j+1)]
return div
## return the list of prime numbers in [2,N], using eratosthenes sieve
## around 800 ms for N = 10**6 by PyPy3 (7.3.0) @ AtCoder
def PrimeNumSet(N):
M=int(N**0.5)
seachList=[i for i in range(2,N+1)]
primes=[]
while seachList:
if seachList[0]>M:
break
primes.append(seachList[0])
tmp=seachList[0]
seachList=[i for i in seachList if i%tmp!=0]
return primes+seachList
## retrun LCM of numbers in list b
## within 2sec for no of B = 10*5 and Bi < 10**6
def LCM(b,mo=10**9+7):
prs=PrimeNumSet(max(b))
M=dict(zip(prs,[0]*len(prs)))
for i in b:
dc=primeFactor(i)
for j,k in dc.items():
M[j]=max(M[j],k)
r=1
for j,k in M.items():
if k!=0:
r*=pow(j,k,mo)
r%=mo
return r
show_flg=False
show_flg=True
ans=0
n=I()
s=input()
dp=[0]*n
for i in range(n):
dp[i]=1
for k in range(n-1,0,-1):
nx=[0]*n
if s[n-1-k]=='>':
for i in range(k+1):
nx[0]+=dp[i]
nx[i]+=-dp[i]
elif s[n-1-k]=='<':
for i in range(k+1):
nx[i]+=dp[i]
nx[k]+=-dp[i]
for i in range(k):
nx[i+1]=(nx[i+1]+nx[i])%mo
#show(dp)
dp=nx[:]
print(dp[0]%mo)
``` | output | 1 | 48,612 | 12 | 97,225 |
Provide a correct Python 3 solution for this coding contest problem.
Let N be a positive integer. You are given a string s of length N - 1, consisting of `<` and `>`.
Find the number of permutations (p_1, p_2, \ldots, p_N) of (1, 2, \ldots, N) that satisfy the following condition, modulo 10^9 + 7:
* For each i (1 \leq i \leq N - 1), p_i < p_{i + 1} if the i-th character in s is `<`, and p_i > p_{i + 1} if the i-th character in s is `>`.
Constraints
* N is an integer.
* 2 \leq N \leq 3000
* s is a string of length N - 1.
* s consists of `<` and `>`.
Input
Input is given from Standard Input in the following format:
N
s
Output
Print the number of permutations that satisfy the condition, modulo 10^9 + 7.
Examples
Input
4
<><
Output
5
Input
5
<<<<
Output
1
Input
20
>>>><>>><>><>>><<>>
Output
217136290 | instruction | 0 | 48,613 | 12 | 97,226 |
"Correct Solution:
```
N=int(input())
S=str(input())
P=10**9+7
DP=[[0]*(N+1) for _ in range(N+1)]
#初期
for index1 in range(N):
DP[1][index1]=1
for i in range(2,N):
#累積和
C=[0]*(N+1)
C[0]=DP[i-1][0]%P
for index2 in range(1,N+1):
C[index2]=(C[index2-1]+DP[i-1][index2])%P
for j in range(N-i+1):
if S[i-2]=='>':
DP[i][j]=(C[N-i+1]-C[j])%P
else:
DP[i][j]=C[j]
if S[N-2]=='>':
DP[N][0]=DP[N-1][1]
else:
DP[N][0]=DP[N-1][0]
print(DP[N][0]%P)
``` | output | 1 | 48,613 | 12 | 97,227 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let N be a positive integer. You are given a string s of length N - 1, consisting of `<` and `>`.
Find the number of permutations (p_1, p_2, \ldots, p_N) of (1, 2, \ldots, N) that satisfy the following condition, modulo 10^9 + 7:
* For each i (1 \leq i \leq N - 1), p_i < p_{i + 1} if the i-th character in s is `<`, and p_i > p_{i + 1} if the i-th character in s is `>`.
Constraints
* N is an integer.
* 2 \leq N \leq 3000
* s is a string of length N - 1.
* s consists of `<` and `>`.
Input
Input is given from Standard Input in the following format:
N
s
Output
Print the number of permutations that satisfy the condition, modulo 10^9 + 7.
Examples
Input
4
<><
Output
5
Input
5
<<<<
Output
1
Input
20
>>>><>>><>><>>><<>>
Output
217136290
Submitted Solution:
```
import sys,bisect,string,math,time,functools,random,fractions
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
rep=range;R=range
def Golf():n,*t=map(int,open(0).read().split())
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def MI():return map(int,input().split())
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def NLI(n):return [[int(i) for i in input().split()] for i in range(n)]
def NLI_(n):return [[int(i)-1 for i in input().split()] for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def RA():return map(int,open(0).read().split())
def RLI(n=8,a=1,b=10):return [random.randint(a,b)for i in range(n)]
def RI(a=1,b=10):return random.randint(a,b)
def Rtest(T):
case,err=0,0
for i in range(T):
inp=INP()
a1,ls=naive(*inp)
a2=solve(*inp)
if a1!=a2:
print((a1,a2),inp)
err+=1
case+=1
print('Tested',case,'case with',err,'errors')
def GI(V,E,ls=None,Directed=False,index=1):
org_inp=[];g=[[] for i in range(V)]
FromStdin=True if ls==None else False
for i in range(E):
if FromStdin:
inp=LI()
org_inp.append(inp)
else:
inp=ls[i]
if len(inp)==2:
a,b=inp;c=1
else:
a,b,c=inp
if index==1:a-=1;b-=1
aa=(a,c);bb=(b,c);g[a].append(bb)
if not Directed:g[b].append(aa)
return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1) # sample usage
mp=[boundary]*(w+2);found={}
for i in R(h):
s=input()
for char in search:
if char in s:
found[char]=((i+1)*(w+2)+s.index(char)+1)
mp_def[char]=mp_def[replacement_of_found]
mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
mp+=[boundary]*(w+2)
return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def accum(ls):
rt=[0]
for i in ls:rt+=[rt[-1]+i]
return rt
def bit_combination(n,base=2):
rt=[]
for tb in R(base**n):s=[tb//(base**bt)%base for bt in R(n)];rt+=[s]
return rt
def gcd(x,y):
if y==0:return x
if x%y==0:return y
while x%y!=0:x,y=y,x%y
return y
def YN(x):print(['NO','YES'][x])
def Yn(x):print(['No','Yes'][x])
def show(*inp,end='\n'):
if show_flg:print(*inp,end=end)
mo=10**9+7
#mo=998244353
inf=float('inf')
FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb))
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**9)
read=sys.stdin.buffer.read;readline=sys.stdin.buffer.readline;input=lambda:sys.stdin.readline().rstrip()
## return prime factors of N as dictionary {prime p:power of p}
## within 2 sec for N = 2*10**20+7
def primeFactor(N):
i,n=2,N
ret={}
d,sq=2,99
while i<=sq:
k=0
while n%i==0:
n,k,ret[i]=n//i,k+1,k+1
if k>0 or i==97:
sq=int(n**(1/2)+0.5)
if i<4:
i=i*2-1
else:
i,d=i+d,d^6
if n>1:
ret[n]=1
return ret
## return divisors of n as list
def divisor(n):
div=[1]
for i,j in primeFactor(n).items():
div=[(i**k)*d for d in div for k in range(j+1)]
return div
## return the list of prime numbers in [2,N], using eratosthenes sieve
## around 800 ms for N = 10**6 by PyPy3 (7.3.0) @ AtCoder
def PrimeNumSet(N):
M=int(N**0.5)
seachList=[i for i in range(2,N+1)]
primes=[]
while seachList:
if seachList[0]>M:
break
primes.append(seachList[0])
tmp=seachList[0]
seachList=[i for i in seachList if i%tmp!=0]
return primes+seachList
## retrun LCM of numbers in list b
## within 2sec for no of B = 10*5 and Bi < 10**6
def LCM(b,mo=10**9+7):
prs=PrimeNumSet(max(b))
M=dict(zip(prs,[0]*len(prs)))
for i in b:
dc=primeFactor(i)
for j,k in dc.items():
M[j]=max(M[j],k)
r=1
for j,k in M.items():
if k!=0:
r*=pow(j,k,mo)
r%=mo
return r
show_flg=False
show_flg=True
ans=0
n=I()
s=input()
dp=[[0]*n for i in range(n)]
for i in range(n):
dp[n-1-i][i]=1
for k in range(n-1,0,-1):
if s[n-1-k]=='>':
for i in range(k+1):
for j in range(k):
if 0<=k-1-i-j<n and 0<=i+j<n:
dp[k-1-i-j][i+j]+=dp[k-i][i]
elif s[n-1-k]=='<':
for i in range(k+1):
for j in range(k):
if 0<=k-1-i-j<n and 0<=i+j<n:
#dp[k-1-i-j][i+j]+=dp[k-i][i]
dp[i+j][k-1-i-j]+=dp[i][k-i]
print(dp[0][0]%mo)
``` | instruction | 0 | 48,619 | 12 | 97,238 |
No | output | 1 | 48,619 | 12 | 97,239 |
Provide a correct Python 3 solution for this coding contest problem.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* push($d$, $x$): Add element $x$ at the begining of $A$, if $d = 0$. Add element $x$ at the end of $A$, if $d = 1$.
* randomAccess($p$): Print element $a_p$.
* pop($d$): Delete the first element of $A$, if $d = 0$. Delete the last element of $A$, if $d = 1$.
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 400,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $d$ $x$
or
1 $p$
or
2 $d$
where the first digits 0, 1 and 2 represent push, randomAccess and pop operations respectively.
randomAccess and pop operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
11
0 0 1
0 0 2
0 1 3
1 0
1 1
1 2
2 0
2 1
0 0 4
1 0
1 1
Output
2
1
3
4
1 | instruction | 0 | 48,799 | 12 | 97,598 |
"Correct Solution:
```
from collections import deque
if __name__ == "__main__":
num_query = int(input())
int_que: deque = deque()
for _ in range(num_query):
op, *value = map(lambda x: int(x), input().split())
if (0 == op):
if (0 == value[0]):
int_que.appendleft(value[1])
else:
int_que.append(value[1])
elif (1 == op):
print(f"{int_que[value[0]]}")
elif (2 == op):
if (0 == value[0]):
int_que.popleft()
else:
int_que.pop()
else:
pass
``` | output | 1 | 48,799 | 12 | 97,599 |
Provide a correct Python 3 solution for this coding contest problem.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* push($d$, $x$): Add element $x$ at the begining of $A$, if $d = 0$. Add element $x$ at the end of $A$, if $d = 1$.
* randomAccess($p$): Print element $a_p$.
* pop($d$): Delete the first element of $A$, if $d = 0$. Delete the last element of $A$, if $d = 1$.
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 400,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $d$ $x$
or
1 $p$
or
2 $d$
where the first digits 0, 1 and 2 represent push, randomAccess and pop operations respectively.
randomAccess and pop operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
11
0 0 1
0 0 2
0 1 3
1 0
1 1
1 2
2 0
2 1
0 0 4
1 0
1 1
Output
2
1
3
4
1 | instruction | 0 | 48,800 | 12 | 97,600 |
"Correct Solution:
```
def resolve():
from collections import deque
q = int(input())
ans = deque()
for _ in range(q):
q = [int(i) for i in input().split()]
if q[0] == 2:
if q[1] == 0:
ans.popleft()
else:
ans.pop()
else:
if q[0] == 0:
if q[1] == 0:
ans.appendleft(q[2])
else:
ans.append(q[2])
else:
print(ans[q[1]])
resolve()
``` | output | 1 | 48,800 | 12 | 97,601 |
Provide a correct Python 3 solution for this coding contest problem.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* push($d$, $x$): Add element $x$ at the begining of $A$, if $d = 0$. Add element $x$ at the end of $A$, if $d = 1$.
* randomAccess($p$): Print element $a_p$.
* pop($d$): Delete the first element of $A$, if $d = 0$. Delete the last element of $A$, if $d = 1$.
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 400,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $d$ $x$
or
1 $p$
or
2 $d$
where the first digits 0, 1 and 2 represent push, randomAccess and pop operations respectively.
randomAccess and pop operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
11
0 0 1
0 0 2
0 1 3
1 0
1 1
1 2
2 0
2 1
0 0 4
1 0
1 1
Output
2
1
3
4
1 | instruction | 0 | 48,801 | 12 | 97,602 |
"Correct Solution:
```
from collections import deque
d = deque()
q = int(input())
for _ in range(q):
x = list(map(int, input().split()))
if x[0]==0:
dr = x[1]
val = x[2]
if dr==1:
d.append(val)
else:
d.appendleft(val)
elif x[0]==1:
print(d[x[1]])
elif x[0]==2:
dr = x[1]
if dr==0:
d.popleft()
else:
d.pop()
``` | output | 1 | 48,801 | 12 | 97,603 |
Provide a correct Python 3 solution for this coding contest problem.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* push($d$, $x$): Add element $x$ at the begining of $A$, if $d = 0$. Add element $x$ at the end of $A$, if $d = 1$.
* randomAccess($p$): Print element $a_p$.
* pop($d$): Delete the first element of $A$, if $d = 0$. Delete the last element of $A$, if $d = 1$.
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 400,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $d$ $x$
or
1 $p$
or
2 $d$
where the first digits 0, 1 and 2 represent push, randomAccess and pop operations respectively.
randomAccess and pop operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
11
0 0 1
0 0 2
0 1 3
1 0
1 1
1 2
2 0
2 1
0 0 4
1 0
1 1
Output
2
1
3
4
1 | instruction | 0 | 48,802 | 12 | 97,604 |
"Correct Solution:
```
from collections import deque
num=int(input())
A=deque()
for i in range(num):
lst=list(map(int,input().split()))
if lst[0]==0:
if lst[1]==0:
A.appendleft(lst[2])
else :A.append(lst[2])
elif lst[0]==1:
print(A[lst[1]])
elif lst[0]==2:
if lst[1]==0:
A.popleft()
else :A.pop()
``` | output | 1 | 48,802 | 12 | 97,605 |
Provide a correct Python 3 solution for this coding contest problem.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* push($d$, $x$): Add element $x$ at the begining of $A$, if $d = 0$. Add element $x$ at the end of $A$, if $d = 1$.
* randomAccess($p$): Print element $a_p$.
* pop($d$): Delete the first element of $A$, if $d = 0$. Delete the last element of $A$, if $d = 1$.
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 400,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $d$ $x$
or
1 $p$
or
2 $d$
where the first digits 0, 1 and 2 represent push, randomAccess and pop operations respectively.
randomAccess and pop operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
11
0 0 1
0 0 2
0 1 3
1 0
1 1
1 2
2 0
2 1
0 0 4
1 0
1 1
Output
2
1
3
4
1 | instruction | 0 | 48,803 | 12 | 97,606 |
"Correct Solution:
```
from collections import deque
A=deque()
def push(A,d,x):
if d==0:
A.appendleft(x)
else:
A.append(x)
return A
def randomaccess(A,p):
print(A[p])
def pop(A,d):
if d==0:
A.popleft()
else:
A.pop()
return A
q=int(input())
for i in range(q):
query=list(map(int,input().split()))
if query[0]==0:
A=push(A,query[1],query[2])
elif query[0]==1:
randomaccess(A,query[1])
else:
A=pop(A,query[1])
``` | output | 1 | 48,803 | 12 | 97,607 |
Provide a correct Python 3 solution for this coding contest problem.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* push($d$, $x$): Add element $x$ at the begining of $A$, if $d = 0$. Add element $x$ at the end of $A$, if $d = 1$.
* randomAccess($p$): Print element $a_p$.
* pop($d$): Delete the first element of $A$, if $d = 0$. Delete the last element of $A$, if $d = 1$.
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 400,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $d$ $x$
or
1 $p$
or
2 $d$
where the first digits 0, 1 and 2 represent push, randomAccess and pop operations respectively.
randomAccess and pop operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
11
0 0 1
0 0 2
0 1 3
1 0
1 1
1 2
2 0
2 1
0 0 4
1 0
1 1
Output
2
1
3
4
1 | instruction | 0 | 48,804 | 12 | 97,608 |
"Correct Solution:
```
from collections import deque
q=int(input())
lis=deque()
lq=[]
for i in range(q):
lq.append([int(x) for x in input().split(' ')])
for i in lq:
order=i[0]
if order==0:
d=i[1]
if d==0:
lis.appendleft(i[2])
else:
lis.append(i[2])
elif order==1:
print(lis[i[1]])
elif order==2:
d=i[1]
if d==0:
lis.popleft()
else:
lis.pop()
``` | output | 1 | 48,804 | 12 | 97,609 |
Provide a correct Python 3 solution for this coding contest problem.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* push($d$, $x$): Add element $x$ at the begining of $A$, if $d = 0$. Add element $x$ at the end of $A$, if $d = 1$.
* randomAccess($p$): Print element $a_p$.
* pop($d$): Delete the first element of $A$, if $d = 0$. Delete the last element of $A$, if $d = 1$.
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 400,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $d$ $x$
or
1 $p$
or
2 $d$
where the first digits 0, 1 and 2 represent push, randomAccess and pop operations respectively.
randomAccess and pop operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
11
0 0 1
0 0 2
0 1 3
1 0
1 1
1 2
2 0
2 1
0 0 4
1 0
1 1
Output
2
1
3
4
1 | instruction | 0 | 48,805 | 12 | 97,610 |
"Correct Solution:
```
from collections import deque
num = int(input())
A = deque()
for i in range(num):
queryi = list(map(int, input().split()))
if queryi[0] == 1:
print(A[queryi[1]])
elif queryi[0] == 2:
if queryi[1] == 0:
A.popleft()
else:
A.pop()
else:
if queryi[1] ==0:
A.appendleft(queryi[2])
else:
A.append(queryi[2])
``` | output | 1 | 48,805 | 12 | 97,611 |
Provide a correct Python 3 solution for this coding contest problem.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* push($d$, $x$): Add element $x$ at the begining of $A$, if $d = 0$. Add element $x$ at the end of $A$, if $d = 1$.
* randomAccess($p$): Print element $a_p$.
* pop($d$): Delete the first element of $A$, if $d = 0$. Delete the last element of $A$, if $d = 1$.
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 400,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $d$ $x$
or
1 $p$
or
2 $d$
where the first digits 0, 1 and 2 represent push, randomAccess and pop operations respectively.
randomAccess and pop operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
11
0 0 1
0 0 2
0 1 3
1 0
1 1
1 2
2 0
2 1
0 0 4
1 0
1 1
Output
2
1
3
4
1 | instruction | 0 | 48,806 | 12 | 97,612 |
"Correct Solution:
```
from collections import deque
Q = deque()
n = int(input())
for i in range(n):
x = list(map(int, input().split()))
if x[0] == 0:
if x[1] == 0:
Q.appendleft(x[2])
if x[1] == 1:
Q.append(x[2])
elif x[0] == 1:
print(Q[x[1]])
elif x[0] == 2:
if x[1] == 0:
Q.popleft()
if x[1] == 1:
Q.pop()
``` | output | 1 | 48,806 | 12 | 97,613 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* push($d$, $x$): Add element $x$ at the begining of $A$, if $d = 0$. Add element $x$ at the end of $A$, if $d = 1$.
* randomAccess($p$): Print element $a_p$.
* pop($d$): Delete the first element of $A$, if $d = 0$. Delete the last element of $A$, if $d = 1$.
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 400,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $d$ $x$
or
1 $p$
or
2 $d$
where the first digits 0, 1 and 2 represent push, randomAccess and pop operations respectively.
randomAccess and pop operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
11
0 0 1
0 0 2
0 1 3
1 0
1 1
1 2
2 0
2 1
0 0 4
1 0
1 1
Output
2
1
3
4
1
Submitted Solution:
```
from collections import deque
if __name__ == "__main__":
n = int(input())
queue = deque()
for _ in range(n):
param = input().split(" ")
if param[0] == "0":
if param[1] == "0":
queue.appendleft(param[2])
else:
queue.append(param[2])
elif param[0] == "1":
print(queue[int(param[1])])
elif param[0] == "2":
if param[1] == "0":
queue.popleft()
else:
queue.pop()
``` | instruction | 0 | 48,807 | 12 | 97,614 |
Yes | output | 1 | 48,807 | 12 | 97,615 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* push($d$, $x$): Add element $x$ at the begining of $A$, if $d = 0$. Add element $x$ at the end of $A$, if $d = 1$.
* randomAccess($p$): Print element $a_p$.
* pop($d$): Delete the first element of $A$, if $d = 0$. Delete the last element of $A$, if $d = 1$.
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 400,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $d$ $x$
or
1 $p$
or
2 $d$
where the first digits 0, 1 and 2 represent push, randomAccess and pop operations respectively.
randomAccess and pop operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
11
0 0 1
0 0 2
0 1 3
1 0
1 1
1 2
2 0
2 1
0 0 4
1 0
1 1
Output
2
1
3
4
1
Submitted Solution:
```
q = int(input())
from collections import deque
A = deque()
for _ in range(q):
L = list(map(int, input().split()))
if L[0] == 0 and L[1] == 0:
A.appendleft(L[2])
elif L[0] == 0 and L[1] == 1:
A.append(L[2])
elif L[0] == 1:
print(A[L[1]])
else:
if L[1] == 0:
A.popleft()
else:
A.pop()
``` | instruction | 0 | 48,808 | 12 | 97,616 |
Yes | output | 1 | 48,808 | 12 | 97,617 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* push($d$, $x$): Add element $x$ at the begining of $A$, if $d = 0$. Add element $x$ at the end of $A$, if $d = 1$.
* randomAccess($p$): Print element $a_p$.
* pop($d$): Delete the first element of $A$, if $d = 0$. Delete the last element of $A$, if $d = 1$.
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 400,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $d$ $x$
or
1 $p$
or
2 $d$
where the first digits 0, 1 and 2 represent push, randomAccess and pop operations respectively.
randomAccess and pop operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
11
0 0 1
0 0 2
0 1 3
1 0
1 1
1 2
2 0
2 1
0 0 4
1 0
1 1
Output
2
1
3
4
1
Submitted Solution:
```
from collections import deque
anslist = deque()
for i in range(int(input())):
query = list(map(int, input().split()))
if query[0] == 0:
if query[1] == 0:
anslist.appendleft(query[2])
elif query[1] == 1:
anslist.append(query[2])
elif query[0] == 1:
print(anslist[query[1]])
elif query[0] == 2:
if query[1] == 0:
anslist.popleft()
elif query[1] == 1:
anslist.pop()
``` | instruction | 0 | 48,809 | 12 | 97,618 |
Yes | output | 1 | 48,809 | 12 | 97,619 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* push($d$, $x$): Add element $x$ at the begining of $A$, if $d = 0$. Add element $x$ at the end of $A$, if $d = 1$.
* randomAccess($p$): Print element $a_p$.
* pop($d$): Delete the first element of $A$, if $d = 0$. Delete the last element of $A$, if $d = 1$.
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 400,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $d$ $x$
or
1 $p$
or
2 $d$
where the first digits 0, 1 and 2 represent push, randomAccess and pop operations respectively.
randomAccess and pop operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
11
0 0 1
0 0 2
0 1 3
1 0
1 1
1 2
2 0
2 1
0 0 4
1 0
1 1
Output
2
1
3
4
1
Submitted Solution:
```
from collections import deque
d=deque()
n=int(input())
for i in range(n):
q=list(map(int,input().split()))
if q[0]==0:
if q[1]==0:
d.appendleft(q[2])
else:
d.append(q[2])
elif q[0]==1:
print(d[q[1]])
else:
if q[1]==0:
d.popleft()
else:
d.pop()
``` | instruction | 0 | 48,810 | 12 | 97,620 |
Yes | output | 1 | 48,810 | 12 | 97,621 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* push($d$, $x$): Add element $x$ at the begining of $A$, if $d = 0$. Add element $x$ at the end of $A$, if $d = 1$.
* randomAccess($p$): Print element $a_p$.
* pop($d$): Delete the first element of $A$, if $d = 0$. Delete the last element of $A$, if $d = 1$.
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 400,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $d$ $x$
or
1 $p$
or
2 $d$
where the first digits 0, 1 and 2 represent push, randomAccess and pop operations respectively.
randomAccess and pop operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
11
0 0 1
0 0 2
0 1 3
1 0
1 1
1 2
2 0
2 1
0 0 4
1 0
1 1
Output
2
1
3
4
1
Submitted Solution:
```
cnt = int(input())
a = []
for i in range(cnt):
li = list(map(int,input().split()))
if li[0] == 0:
if li[1] == 0:
a.insert(0,li[2])
else:
a.insert(-1,li[2])
if li[0] == 1:
print(a[li[1]])
if li[0] ==2:
if li[1]==0:
a.pop(0)
else:
a.pop()
``` | instruction | 0 | 48,811 | 12 | 97,622 |
No | output | 1 | 48,811 | 12 | 97,623 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* push($d$, $x$): Add element $x$ at the begining of $A$, if $d = 0$. Add element $x$ at the end of $A$, if $d = 1$.
* randomAccess($p$): Print element $a_p$.
* pop($d$): Delete the first element of $A$, if $d = 0$. Delete the last element of $A$, if $d = 1$.
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 400,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $d$ $x$
or
1 $p$
or
2 $d$
where the first digits 0, 1 and 2 represent push, randomAccess and pop operations respectively.
randomAccess and pop operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
11
0 0 1
0 0 2
0 1 3
1 0
1 1
1 2
2 0
2 1
0 0 4
1 0
1 1
Output
2
1
3
4
1
Submitted Solution:
```
n = int(input())
a = []
for i in range(n):
order = [int(o) for o in input().split()]
if order[0] == 0:
if order[1]: a.append(order[2])
else: a.insert(0, order[2])
elif order[0] == 1:
print(a[order[1]])
elif order[0] == 2:
if order[1]: a.pop()
else: a.pop(0)
``` | instruction | 0 | 48,812 | 12 | 97,624 |
No | output | 1 | 48,812 | 12 | 97,625 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* push($d$, $x$): Add element $x$ at the begining of $A$, if $d = 0$. Add element $x$ at the end of $A$, if $d = 1$.
* randomAccess($p$): Print element $a_p$.
* pop($d$): Delete the first element of $A$, if $d = 0$. Delete the last element of $A$, if $d = 1$.
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 400,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $d$ $x$
or
1 $p$
or
2 $d$
where the first digits 0, 1 and 2 represent push, randomAccess and pop operations respectively.
randomAccess and pop operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
11
0 0 1
0 0 2
0 1 3
1 0
1 1
1 2
2 0
2 1
0 0 4
1 0
1 1
Output
2
1
3
4
1
Submitted Solution:
```
def push(lists, d, x):
if d == 0:
lists.insert(0, x)
else:
lists.insert(len(lists), x)
return lists
def randomAccess(lists, p):
print(lists[p])
def pop(lists, d):
if d == 0:
lists.pop(0)
else:
lists.pop(len(lists) - 1)
return lists
if __name__ == '__main__':
l = list()
n = int(input())
for i in range(n):
j= input().split()
if int(j[0]) == 0:
push(l, int(j[1]), int(j[2]))
elif int(j[0]) == 1:
randomAccess(l, int(j[1]))
else:
pop(l, j[2])
``` | instruction | 0 | 48,813 | 12 | 97,626 |
No | output | 1 | 48,813 | 12 | 97,627 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a dynamic array $A = \\{a_0, a_1, ...\\}$ of integers, perform a sequence of the following operations:
* push($d$, $x$): Add element $x$ at the begining of $A$, if $d = 0$. Add element $x$ at the end of $A$, if $d = 1$.
* randomAccess($p$): Print element $a_p$.
* pop($d$): Delete the first element of $A$, if $d = 0$. Delete the last element of $A$, if $d = 1$.
$A$ is a 0-origin array and it is empty in the initial state.
Constraints
* $1 \leq q \leq 400,000$
* $0 \leq p < $ the size of $A$
* $-1,000,000,000 \leq x \leq 1,000,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $d$ $x$
or
1 $p$
or
2 $d$
where the first digits 0, 1 and 2 represent push, randomAccess and pop operations respectively.
randomAccess and pop operations will not be given for an empty array.
Output
For each randomAccess, print $a_p$ in a line.
Example
Input
11
0 0 1
0 0 2
0 1 3
1 0
1 1
1 2
2 0
2 1
0 0 4
1 0
1 1
Output
2
1
3
4
1
Submitted Solution:
```
n = int(input())
a = []
for i in range(n):
order = input().split()
if order[0] == '0':
if order[1] == '0': a.insert(0, int(order[2]))
elif order[1] == '1': a.append(int(order[2]))
elif order[0] == '1':
print(a[int(order[1])])
elif order[0] == '2':
if order[1] == '0': a.pop(0)
elif order[1] == '1': a.pop()
``` | instruction | 0 | 48,814 | 12 | 97,628 |
No | output | 1 | 48,814 | 12 | 97,629 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. Let's denote monotonic renumeration of array a as an array b consisting of n integers such that all of the following conditions are met:
* b_1 = 0;
* for every pair of indices i and j such that 1 ≤ i, j ≤ n, if a_i = a_j, then b_i = b_j (note that if a_i ≠ a_j, it is still possible that b_i = b_j);
* for every index i ∈ [1, n - 1] either b_i = b_{i + 1} or b_i + 1 = b_{i + 1}.
For example, if a = [1, 2, 1, 2, 3], then two possible monotonic renumerations of a are b = [0, 0, 0, 0, 0] and b = [0, 0, 0, 0, 1].
Your task is to calculate the number of different monotonic renumerations of a. The answer may be large, so print it modulo 998244353.
Input
The first line contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9).
Output
Print one integer — the number of different monotonic renumerations of a, taken modulo 998244353.
Examples
Input
5
1 2 1 2 3
Output
2
Input
2
100 1
Output
2
Input
4
1 3 3 7
Output
4 | instruction | 0 | 48,856 | 12 | 97,712 |
Tags: combinatorics, sortings
Correct Solution:
```
rd = lambda: list(map(int, input().split()))
n, a = rd()[0], rd()
mod = 998244353
b = sorted((x, i) for (i, x) in enumerate(a))
pts = []
px = b[0][0]
cmin = cmax = b[0][1]
for x, i in b[1:]:
if px == x:
cmax = i
else:
pts.append((cmin, -1))
pts.append((cmax, +1))
cmin = cmax = i
px = x
pts.append((cmin, -1))
pts.append((cmax, +1))
delta = 0
pow = -1
for _, d in sorted(pts):
delta -= d
if delta == 0:
pow += 1
print((2 ** pow) % mod)
``` | output | 1 | 48,856 | 12 | 97,713 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.