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 |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem!
An arithmetic progression or arithmetic sequence is a sequence of integers such that the subtraction of element with its previous element (x_i - x_{i - 1}, where i ≥ 2) is constant — such difference is called a common difference of the sequence.
That is, an arithmetic progression is a sequence of form x_i = x_1 + (i - 1) d, where d is a common difference of the sequence.
There is a secret list of n integers a_1, a_2, …, a_n.
It is guaranteed that all elements a_1, a_2, …, a_n are between 0 and 10^9, inclusive.
This list is special: if sorted in increasing order, it will form an arithmetic progression with positive common difference (d > 0). For example, the list [14, 24, 9, 19] satisfies this requirement, after sorting it makes a list [9, 14, 19, 24], which can be produced as x_n = 9 + 5 ⋅ (n - 1).
Also you are also given a device, which has a quite discharged battery, thus you can only use it to perform at most 60 queries of following two types:
* Given a value i (1 ≤ i ≤ n), the device will show the value of the a_i.
* Given a value x (0 ≤ x ≤ 10^9), the device will return 1 if an element with a value strictly greater than x exists, and it will return 0 otherwise.
Your can use this special device for at most 60 queries. Could you please find out the smallest element and the common difference of the sequence? That is, values x_1 and d in the definition of the arithmetic progression. Note that the array a is not sorted.
Interaction
The interaction starts with a single integer n (2 ≤ n ≤ 10^6), the size of the list of integers.
Then you can make queries of two types:
* "? i" (1 ≤ i ≤ n) — to get the value of a_i.
* "> x" (0 ≤ x ≤ 10^9) — to check whether there exists an element greater than x
After the query read its result r as an integer.
* For the first query type, the r satisfies 0 ≤ r ≤ 10^9.
* For the second query type, the r is either 0 or 1.
* In case you make more than 60 queries or violated the number range in the queries, you will get a r = -1.
* If you terminate after receiving the -1, you will get the "Wrong answer" verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.
When you find out what the smallest element x_1 and common difference d, print
* "! x_1 d"
And quit after that. This query is not counted towards the 60 queries limit.
After printing any query do not forget to output end of line and flush the output. Otherwise you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Hacks
For hack, use the following format:
The first line should contain an integer n (2 ≤ n ≤ 10^6) — the list's size.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the list.
Also, after the sorting the list must form an arithmetic progression with positive common difference.
Example
Input
4
0
1
14
24
9
19
Output
> 25
> 15
? 1
? 2
? 3
? 4
! 9 5
Note
Note that the example interaction contains extra empty lines so that it's easier to read. The real interaction doesn't contain any empty lines and you shouldn't print any extra empty lines as well.
The list in the example test is [14, 24, 9, 19].
Submitted Solution:
```
import sys
def query1(n):
print("? {}".format(n))
sys.stdout.flush()
return int(input())
def query2(n):
print("> {}".format(n))
sys.stdout.flush()
return int(input())
N = int(input())
ok, ng = 0, 10**9+1
while ng-ok > 1:
mid = (ok+ng)//2
n = query2(mid)
if n:
ng = mid
else:
ok = mid
a1 = ok
dok, dng = 0, 10**9+1
while ng-ok > 1:
mid = (ok+ng)//2
an = a1 + mid * N
n = query2(an)
if n:
ok = mid
else:
ng = mid
print("! {} {}".format(a1, a1+ng*N))
sys.stdout.flush()
``` | instruction | 0 | 9,134 | 12 | 18,268 |
No | output | 1 | 9,134 | 12 | 18,269 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem!
An arithmetic progression or arithmetic sequence is a sequence of integers such that the subtraction of element with its previous element (x_i - x_{i - 1}, where i ≥ 2) is constant — such difference is called a common difference of the sequence.
That is, an arithmetic progression is a sequence of form x_i = x_1 + (i - 1) d, where d is a common difference of the sequence.
There is a secret list of n integers a_1, a_2, …, a_n.
It is guaranteed that all elements a_1, a_2, …, a_n are between 0 and 10^9, inclusive.
This list is special: if sorted in increasing order, it will form an arithmetic progression with positive common difference (d > 0). For example, the list [14, 24, 9, 19] satisfies this requirement, after sorting it makes a list [9, 14, 19, 24], which can be produced as x_n = 9 + 5 ⋅ (n - 1).
Also you are also given a device, which has a quite discharged battery, thus you can only use it to perform at most 60 queries of following two types:
* Given a value i (1 ≤ i ≤ n), the device will show the value of the a_i.
* Given a value x (0 ≤ x ≤ 10^9), the device will return 1 if an element with a value strictly greater than x exists, and it will return 0 otherwise.
Your can use this special device for at most 60 queries. Could you please find out the smallest element and the common difference of the sequence? That is, values x_1 and d in the definition of the arithmetic progression. Note that the array a is not sorted.
Interaction
The interaction starts with a single integer n (2 ≤ n ≤ 10^6), the size of the list of integers.
Then you can make queries of two types:
* "? i" (1 ≤ i ≤ n) — to get the value of a_i.
* "> x" (0 ≤ x ≤ 10^9) — to check whether there exists an element greater than x
After the query read its result r as an integer.
* For the first query type, the r satisfies 0 ≤ r ≤ 10^9.
* For the second query type, the r is either 0 or 1.
* In case you make more than 60 queries or violated the number range in the queries, you will get a r = -1.
* If you terminate after receiving the -1, you will get the "Wrong answer" verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.
When you find out what the smallest element x_1 and common difference d, print
* "! x_1 d"
And quit after that. This query is not counted towards the 60 queries limit.
After printing any query do not forget to output end of line and flush the output. Otherwise you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Hacks
For hack, use the following format:
The first line should contain an integer n (2 ≤ n ≤ 10^6) — the list's size.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the list.
Also, after the sorting the list must form an arithmetic progression with positive common difference.
Example
Input
4
0
1
14
24
9
19
Output
> 25
> 15
? 1
? 2
? 3
? 4
! 9 5
Note
Note that the example interaction contains extra empty lines so that it's easier to read. The real interaction doesn't contain any empty lines and you shouldn't print any extra empty lines as well.
The list in the example test is [14, 24, 9, 19].
Submitted Solution:
```
import random
def qmax(m):
print("> %d" % m)
return int(input())
def qele(i):
print("? %d" % i)
return int(input())
def eureka(a0, d):
print("! %d %d" % (a0, d))
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
n = int(input())
Q = 43
if n <= 60:
lst = []
for i in range(n):
lst.append(qele(i+1))
lst = sorted(lst)
eureka(lst[0], lst[1] - lst[0])
else:
index = []
for i in range(Q):
k = 1
while k in index:
k = random.randint(1,n)
index.append(k)
sample = sorted([qele(i) for i in index])
offset = [x - sample[0] for x in sample[1:]]
d = offset[0]
for i in range(1, len(offset)):
d = gcd(d, offset[i])
maxposs = sample[0] + (n-1) * d
minposs = sample[-1]
f = lambda i: minposs + i * d
g = lambda x: (x - minposs) // d
st, en = g(minposs), g(maxposs)
while en - st > 1:
mid = (en + st) // 2
if qmax(f(mid)):
st = mid
else:
en = mid
maxele = f(en)
minele = maxele - (n-1) * d
eureka(minele, d)
``` | instruction | 0 | 9,135 | 12 | 18,270 |
No | output | 1 | 9,135 | 12 | 18,271 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem!
An arithmetic progression or arithmetic sequence is a sequence of integers such that the subtraction of element with its previous element (x_i - x_{i - 1}, where i ≥ 2) is constant — such difference is called a common difference of the sequence.
That is, an arithmetic progression is a sequence of form x_i = x_1 + (i - 1) d, where d is a common difference of the sequence.
There is a secret list of n integers a_1, a_2, …, a_n.
It is guaranteed that all elements a_1, a_2, …, a_n are between 0 and 10^9, inclusive.
This list is special: if sorted in increasing order, it will form an arithmetic progression with positive common difference (d > 0). For example, the list [14, 24, 9, 19] satisfies this requirement, after sorting it makes a list [9, 14, 19, 24], which can be produced as x_n = 9 + 5 ⋅ (n - 1).
Also you are also given a device, which has a quite discharged battery, thus you can only use it to perform at most 60 queries of following two types:
* Given a value i (1 ≤ i ≤ n), the device will show the value of the a_i.
* Given a value x (0 ≤ x ≤ 10^9), the device will return 1 if an element with a value strictly greater than x exists, and it will return 0 otherwise.
Your can use this special device for at most 60 queries. Could you please find out the smallest element and the common difference of the sequence? That is, values x_1 and d in the definition of the arithmetic progression. Note that the array a is not sorted.
Interaction
The interaction starts with a single integer n (2 ≤ n ≤ 10^6), the size of the list of integers.
Then you can make queries of two types:
* "? i" (1 ≤ i ≤ n) — to get the value of a_i.
* "> x" (0 ≤ x ≤ 10^9) — to check whether there exists an element greater than x
After the query read its result r as an integer.
* For the first query type, the r satisfies 0 ≤ r ≤ 10^9.
* For the second query type, the r is either 0 or 1.
* In case you make more than 60 queries or violated the number range in the queries, you will get a r = -1.
* If you terminate after receiving the -1, you will get the "Wrong answer" verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.
When you find out what the smallest element x_1 and common difference d, print
* "! x_1 d"
And quit after that. This query is not counted towards the 60 queries limit.
After printing any query do not forget to output end of line and flush the output. Otherwise you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Hacks
For hack, use the following format:
The first line should contain an integer n (2 ≤ n ≤ 10^6) — the list's size.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the list.
Also, after the sorting the list must form an arithmetic progression with positive common difference.
Example
Input
4
0
1
14
24
9
19
Output
> 25
> 15
? 1
? 2
? 3
? 4
! 9 5
Note
Note that the example interaction contains extra empty lines so that it's easier to read. The real interaction doesn't contain any empty lines and you shouldn't print any extra empty lines as well.
The list in the example test is [14, 24, 9, 19].
Submitted Solution:
```
from sys import exit, setrecursionlimit, stderr, stdout
from functools import reduce
from itertools import *
from collections import defaultdict, Counter
from bisect import bisect
from random import shuffle
from math import gcd
def read():
return int(input())
def reads():
return [int(x) for x in input().split()]
def answer(*args):
print(*args)
stdout.flush()
cnt = 0
n = read()
l = 0
r = 100
# l < max and max <= r
while r - l >= 2:
m = (r + l) // 2
answer("> {0}".format(m))
cnt += 1
res = read()
if res == 0:
r = m
else:
l = m
M = r
d = 0
idx = list(range(1, n+1))
shuffle(idx)
while idx and cnt < 60:
i = idx.pop()
answer("? {0}".format(i))
cnt += 1
res = read()
d = gcd(d, M - res)
m = M - (n-1) * d
answer("! {0} {1}".format(m, d))
``` | instruction | 0 | 9,136 | 12 | 18,272 |
No | output | 1 | 9,136 | 12 | 18,273 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem!
An arithmetic progression or arithmetic sequence is a sequence of integers such that the subtraction of element with its previous element (x_i - x_{i - 1}, where i ≥ 2) is constant — such difference is called a common difference of the sequence.
That is, an arithmetic progression is a sequence of form x_i = x_1 + (i - 1) d, where d is a common difference of the sequence.
There is a secret list of n integers a_1, a_2, …, a_n.
It is guaranteed that all elements a_1, a_2, …, a_n are between 0 and 10^9, inclusive.
This list is special: if sorted in increasing order, it will form an arithmetic progression with positive common difference (d > 0). For example, the list [14, 24, 9, 19] satisfies this requirement, after sorting it makes a list [9, 14, 19, 24], which can be produced as x_n = 9 + 5 ⋅ (n - 1).
Also you are also given a device, which has a quite discharged battery, thus you can only use it to perform at most 60 queries of following two types:
* Given a value i (1 ≤ i ≤ n), the device will show the value of the a_i.
* Given a value x (0 ≤ x ≤ 10^9), the device will return 1 if an element with a value strictly greater than x exists, and it will return 0 otherwise.
Your can use this special device for at most 60 queries. Could you please find out the smallest element and the common difference of the sequence? That is, values x_1 and d in the definition of the arithmetic progression. Note that the array a is not sorted.
Interaction
The interaction starts with a single integer n (2 ≤ n ≤ 10^6), the size of the list of integers.
Then you can make queries of two types:
* "? i" (1 ≤ i ≤ n) — to get the value of a_i.
* "> x" (0 ≤ x ≤ 10^9) — to check whether there exists an element greater than x
After the query read its result r as an integer.
* For the first query type, the r satisfies 0 ≤ r ≤ 10^9.
* For the second query type, the r is either 0 or 1.
* In case you make more than 60 queries or violated the number range in the queries, you will get a r = -1.
* If you terminate after receiving the -1, you will get the "Wrong answer" verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.
When you find out what the smallest element x_1 and common difference d, print
* "! x_1 d"
And quit after that. This query is not counted towards the 60 queries limit.
After printing any query do not forget to output end of line and flush the output. Otherwise you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Hacks
For hack, use the following format:
The first line should contain an integer n (2 ≤ n ≤ 10^6) — the list's size.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — the elements of the list.
Also, after the sorting the list must form an arithmetic progression with positive common difference.
Example
Input
4
0
1
14
24
9
19
Output
> 25
> 15
? 1
? 2
? 3
? 4
! 9 5
Note
Note that the example interaction contains extra empty lines so that it's easier to read. The real interaction doesn't contain any empty lines and you shouldn't print any extra empty lines as well.
The list in the example test is [14, 24, 9, 19].
Submitted Solution:
```
n = int(input())
if(n<=60):
number = list()
for i in range(n):
print("? "+str(i+1))
a = int(input())
number.append(a)
number.sort()
print("! "+str(number[0])+" "+str(number[1]-number[0]))
def askformax(sta,assmax):
print("> " + str(assmax))
a = int(input())
if(a==1):
if(sta == 0):
return assmax
assmax = 2*assmax
sta = 1
else:
if(sta==1):
return assmax
assmax =assmax//2
sta = 0
askformax(sta,assmax)
shifoudayu = list()
listofass = list()
def askformax2(time,assmax,assd,by):
if(time==7):
return
assmax = assmax+by*assd
listofass.append(assmax)
print("> " + str(assmax))
a = int(input())
if (a == 1):
shifoudayu.append(1)
by = by/2
askformax2(time+1,assmax,assd,by)
elif (a==0) :
shifoudayu.append(0)
by = -1*by / 2
askformax2(time+1,assmax, assd,by)
else:
return assmax
if(n>60):
assmax = askformax(1,n*5)
number = list()
for i in range(40):
b = n//40*i+1
print("? "+str(b))
a = int(input())
number.append(a)
number.sort()
assd =( assmax-number[0]) // n
for i in range(39):
if((number[i+1]-number[i])<assd):
assd = number[i+1]-number[i]
print("> " + str(number[0]+(n-1)*assd))
a = int(input())
if(a==0):
print("! " + str(number[0]) + " " + str(assd))
else:
askformax2(1, assmax,assd, -128)
for i in range(129):
listjia = list()
assmaxjia = assmax - i*assd
for j in range(len(listofass)):
if(assmaxjia>listofass[j]):
listjia.append(1)
else:
listjia.append(0)
if(listjia==shifoudayu):
print("! " + str(assmaxjia-(n-1)*assd )+ " " + str(assd))
break
``` | instruction | 0 | 9,137 | 12 | 18,274 |
No | output | 1 | 9,137 | 12 | 18,275 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a_1, a_2, …, a_n.
In one operation you can choose two elements a_i and a_j (i ≠ j) and decrease each of them by one.
You need to check whether it is possible to make all the elements equal to zero or not.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the elements of the array.
Output
Print "YES" if it is possible to make all elements zero, otherwise print "NO".
Examples
Input
4
1 1 2 2
Output
YES
Input
6
1 2 3 4 5 6
Output
NO
Note
In the first example, you can make all elements equal to zero in 3 operations:
* Decrease a_1 and a_2,
* Decrease a_3 and a_4,
* Decrease a_3 and a_4
In the second example, one can show that it is impossible to make all elements equal to zero. | instruction | 0 | 9,186 | 12 | 18,372 |
Tags: greedy, math
Correct Solution:
```
x = int(input())
c = input().split()
for i in range(len(c)):
c[i] = int(c[i])
if sum(c) % 2 == 0 and max(c) <= sum(c) - max(c):
print("YES")
else:
print("NO")
``` | output | 1 | 9,186 | 12 | 18,373 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a_1, a_2, …, a_n.
In one operation you can choose two elements a_i and a_j (i ≠ j) and decrease each of them by one.
You need to check whether it is possible to make all the elements equal to zero or not.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the elements of the array.
Output
Print "YES" if it is possible to make all elements zero, otherwise print "NO".
Examples
Input
4
1 1 2 2
Output
YES
Input
6
1 2 3 4 5 6
Output
NO
Note
In the first example, you can make all elements equal to zero in 3 operations:
* Decrease a_1 and a_2,
* Decrease a_3 and a_4,
* Decrease a_3 and a_4
In the second example, one can show that it is impossible to make all elements equal to zero. | instruction | 0 | 9,187 | 12 | 18,374 |
Tags: greedy, math
Correct Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 15 10:49:21 2019
@author: Ryan
"""
n = int(input())
ints = input().split(" ")
ints = [int(i) for i in ints]
def solve(n, ints):
summ = 0
for i in range(len(ints)):
summ += ints[i]
if (summ % 2 != 0):
print("NO")
return None
#
# for i in range(n):
# print(summ)
# if (ints[i] > (summ - ints[i])):
# print("NO")3
# return None
#
# print("YES")
for i in range(n):
if (ints[i] > summ//2):
print("NO")
return None
print("YES")
solve(n,ints)
``` | output | 1 | 9,187 | 12 | 18,375 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a_1, a_2, …, a_n.
In one operation you can choose two elements a_i and a_j (i ≠ j) and decrease each of them by one.
You need to check whether it is possible to make all the elements equal to zero or not.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the elements of the array.
Output
Print "YES" if it is possible to make all elements zero, otherwise print "NO".
Examples
Input
4
1 1 2 2
Output
YES
Input
6
1 2 3 4 5 6
Output
NO
Note
In the first example, you can make all elements equal to zero in 3 operations:
* Decrease a_1 and a_2,
* Decrease a_3 and a_4,
* Decrease a_3 and a_4
In the second example, one can show that it is impossible to make all elements equal to zero. | instruction | 0 | 9,188 | 12 | 18,376 |
Tags: greedy, math
Correct Solution:
```
n = int(input())
g = list(map(int, input().split()))
if sum(g)%2 == 0 and 2*max(g)<=sum(g):
print("YES")
else:
print("NO")
``` | output | 1 | 9,188 | 12 | 18,377 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a_1, a_2, …, a_n.
In one operation you can choose two elements a_i and a_j (i ≠ j) and decrease each of them by one.
You need to check whether it is possible to make all the elements equal to zero or not.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the elements of the array.
Output
Print "YES" if it is possible to make all elements zero, otherwise print "NO".
Examples
Input
4
1 1 2 2
Output
YES
Input
6
1 2 3 4 5 6
Output
NO
Note
In the first example, you can make all elements equal to zero in 3 operations:
* Decrease a_1 and a_2,
* Decrease a_3 and a_4,
* Decrease a_3 and a_4
In the second example, one can show that it is impossible to make all elements equal to zero. | instruction | 0 | 9,189 | 12 | 18,378 |
Tags: greedy, math
Correct Solution:
```
n = int(input())
m = 0
sum = 0
for a in map(int,input().split()):
m = max(m,a)
sum += a
if sum % 2 == 0 and (sum - m) >= m:
print('YES')
else:
print('NO')
``` | output | 1 | 9,189 | 12 | 18,379 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a_1, a_2, …, a_n.
In one operation you can choose two elements a_i and a_j (i ≠ j) and decrease each of them by one.
You need to check whether it is possible to make all the elements equal to zero or not.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the elements of the array.
Output
Print "YES" if it is possible to make all elements zero, otherwise print "NO".
Examples
Input
4
1 1 2 2
Output
YES
Input
6
1 2 3 4 5 6
Output
NO
Note
In the first example, you can make all elements equal to zero in 3 operations:
* Decrease a_1 and a_2,
* Decrease a_3 and a_4,
* Decrease a_3 and a_4
In the second example, one can show that it is impossible to make all elements equal to zero. | instruction | 0 | 9,190 | 12 | 18,380 |
Tags: greedy, math
Correct Solution:
```
n,a=int(input()),[int(x)for x in input().split()]
res=str("NO")if (sum(a)%2==1) or (2*max(a)>sum(a)) else str("YES")
print(res)
``` | output | 1 | 9,190 | 12 | 18,381 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a_1, a_2, …, a_n.
In one operation you can choose two elements a_i and a_j (i ≠ j) and decrease each of them by one.
You need to check whether it is possible to make all the elements equal to zero or not.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the elements of the array.
Output
Print "YES" if it is possible to make all elements zero, otherwise print "NO".
Examples
Input
4
1 1 2 2
Output
YES
Input
6
1 2 3 4 5 6
Output
NO
Note
In the first example, you can make all elements equal to zero in 3 operations:
* Decrease a_1 and a_2,
* Decrease a_3 and a_4,
* Decrease a_3 and a_4
In the second example, one can show that it is impossible to make all elements equal to zero. | instruction | 0 | 9,191 | 12 | 18,382 |
Tags: greedy, math
Correct Solution:
```
input()
arr = list(map(int, input().split()))
sm = sum(arr)
if sm % 2 == 0 and max(arr) <= sm - max(arr):
print("YES")
else:
print("NO")
``` | output | 1 | 9,191 | 12 | 18,383 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a_1, a_2, …, a_n.
In one operation you can choose two elements a_i and a_j (i ≠ j) and decrease each of them by one.
You need to check whether it is possible to make all the elements equal to zero or not.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the elements of the array.
Output
Print "YES" if it is possible to make all elements zero, otherwise print "NO".
Examples
Input
4
1 1 2 2
Output
YES
Input
6
1 2 3 4 5 6
Output
NO
Note
In the first example, you can make all elements equal to zero in 3 operations:
* Decrease a_1 and a_2,
* Decrease a_3 and a_4,
* Decrease a_3 and a_4
In the second example, one can show that it is impossible to make all elements equal to zero. | instruction | 0 | 9,192 | 12 | 18,384 |
Tags: greedy, math
Correct Solution:
```
from sys import stdin
n=int(stdin.readline().strip())
s=list(map(int,stdin.readline().strip().split()))
s.sort(reverse=True)
if sum(s)%2==0 and s[0]<=sum(s[1::]):
print("YES")
else:
print("NO")
``` | output | 1 | 9,192 | 12 | 18,385 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a_1, a_2, …, a_n.
In one operation you can choose two elements a_i and a_j (i ≠ j) and decrease each of them by one.
You need to check whether it is possible to make all the elements equal to zero or not.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the elements of the array.
Output
Print "YES" if it is possible to make all elements zero, otherwise print "NO".
Examples
Input
4
1 1 2 2
Output
YES
Input
6
1 2 3 4 5 6
Output
NO
Note
In the first example, you can make all elements equal to zero in 3 operations:
* Decrease a_1 and a_2,
* Decrease a_3 and a_4,
* Decrease a_3 and a_4
In the second example, one can show that it is impossible to make all elements equal to zero. | instruction | 0 | 9,193 | 12 | 18,386 |
Tags: greedy, math
Correct Solution:
```
n=int(input())
a=list(map(int, input().split()))
s=0
for i in range(n):
s=s+a[i]
if s%2!=0:
print("NO")
else:
if max(a)>s/2:
print("NO")
else:
print("YES")
``` | output | 1 | 9,193 | 12 | 18,387 |
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_1, a_2, …, a_n.
In one operation you can choose two elements a_i and a_j (i ≠ j) and decrease each of them by one.
You need to check whether it is possible to make all the elements equal to zero or not.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the elements of the array.
Output
Print "YES" if it is possible to make all elements zero, otherwise print "NO".
Examples
Input
4
1 1 2 2
Output
YES
Input
6
1 2 3 4 5 6
Output
NO
Note
In the first example, you can make all elements equal to zero in 3 operations:
* Decrease a_1 and a_2,
* Decrease a_3 and a_4,
* Decrease a_3 and a_4
In the second example, one can show that it is impossible to make all elements equal to zero.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
a.sort()
s = sum(a)
if s % 2 == 0 and a[n - 1] <= s // 2:
print("YES")
else:
print("NO")
``` | instruction | 0 | 9,194 | 12 | 18,388 |
Yes | output | 1 | 9,194 | 12 | 18,389 |
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_1, a_2, …, a_n.
In one operation you can choose two elements a_i and a_j (i ≠ j) and decrease each of them by one.
You need to check whether it is possible to make all the elements equal to zero or not.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the elements of the array.
Output
Print "YES" if it is possible to make all elements zero, otherwise print "NO".
Examples
Input
4
1 1 2 2
Output
YES
Input
6
1 2 3 4 5 6
Output
NO
Note
In the first example, you can make all elements equal to zero in 3 operations:
* Decrease a_1 and a_2,
* Decrease a_3 and a_4,
* Decrease a_3 and a_4
In the second example, one can show that it is impossible to make all elements equal to zero.
Submitted Solution:
```
""" 616C """
""" 1152B """
import math
# import sys
def check(k,h,c):
return ((((k+1)*h)+((k)*c))/((2*k)+1))
def main():
# n ,m= map(int,input().split())
# arr = list(map(int,input().split()))
# b = list(map(int,input().split()))
# n = int(input())
# string = str(input())
n = int(input())
a = list(map(int,input().split()))
add = sum(a)
a.sort()
if(add%2==0 and sum(a[:-1])>=a[-1]):
print("YES")
else:
# print(sum(a[:-1]),a[-1])
print("NO")
return
main()
# def test():
# t = int(input())
# while t:
# main()
# t-=1
# test()
``` | instruction | 0 | 9,195 | 12 | 18,390 |
Yes | output | 1 | 9,195 | 12 | 18,391 |
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_1, a_2, …, a_n.
In one operation you can choose two elements a_i and a_j (i ≠ j) and decrease each of them by one.
You need to check whether it is possible to make all the elements equal to zero or not.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the elements of the array.
Output
Print "YES" if it is possible to make all elements zero, otherwise print "NO".
Examples
Input
4
1 1 2 2
Output
YES
Input
6
1 2 3 4 5 6
Output
NO
Note
In the first example, you can make all elements equal to zero in 3 operations:
* Decrease a_1 and a_2,
* Decrease a_3 and a_4,
* Decrease a_3 and a_4
In the second example, one can show that it is impossible to make all elements equal to zero.
Submitted Solution:
```
n = int(input())
arr = list(map(int, input().split()))
if (2*max(arr)<=sum(arr) and sum(arr)%2==0):
print("YES")
else:
print("NO")
``` | instruction | 0 | 9,196 | 12 | 18,392 |
Yes | output | 1 | 9,196 | 12 | 18,393 |
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_1, a_2, …, a_n.
In one operation you can choose two elements a_i and a_j (i ≠ j) and decrease each of them by one.
You need to check whether it is possible to make all the elements equal to zero or not.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the elements of the array.
Output
Print "YES" if it is possible to make all elements zero, otherwise print "NO".
Examples
Input
4
1 1 2 2
Output
YES
Input
6
1 2 3 4 5 6
Output
NO
Note
In the first example, you can make all elements equal to zero in 3 operations:
* Decrease a_1 and a_2,
* Decrease a_3 and a_4,
* Decrease a_3 and a_4
In the second example, one can show that it is impossible to make all elements equal to zero.
Submitted Solution:
```
N = int(input())
cnt_odd = 0
a = [int(x) for x in input().split()]
for i in a:
if(i % 2 != 0):
cnt_odd +=1
if(cnt_odd % 2 == 0):
print("YES")
else:
print("NO")
``` | instruction | 0 | 9,199 | 12 | 18,398 |
No | output | 1 | 9,199 | 12 | 18,399 |
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_1, a_2, …, a_n.
In one operation you can choose two elements a_i and a_j (i ≠ j) and decrease each of them by one.
You need to check whether it is possible to make all the elements equal to zero or not.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the elements of the array.
Output
Print "YES" if it is possible to make all elements zero, otherwise print "NO".
Examples
Input
4
1 1 2 2
Output
YES
Input
6
1 2 3 4 5 6
Output
NO
Note
In the first example, you can make all elements equal to zero in 3 operations:
* Decrease a_1 and a_2,
* Decrease a_3 and a_4,
* Decrease a_3 and a_4
In the second example, one can show that it is impossible to make all elements equal to zero.
Submitted Solution:
```
n = int(input())
arr = list(map(int, input().split()))
if (max(arr)<=sum(arr) and sum(arr)%2==0):
print("YES")
else:
print("NO")
``` | instruction | 0 | 9,200 | 12 | 18,400 |
No | output | 1 | 9,200 | 12 | 18,401 |
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_1, a_2, …, a_n.
In one operation you can choose two elements a_i and a_j (i ≠ j) and decrease each of them by one.
You need to check whether it is possible to make all the elements equal to zero or not.
Input
The first line contains a single integer n (2 ≤ n ≤ 10^5) — the size of the array.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the elements of the array.
Output
Print "YES" if it is possible to make all elements zero, otherwise print "NO".
Examples
Input
4
1 1 2 2
Output
YES
Input
6
1 2 3 4 5 6
Output
NO
Note
In the first example, you can make all elements equal to zero in 3 operations:
* Decrease a_1 and a_2,
* Decrease a_3 and a_4,
* Decrease a_3 and a_4
In the second example, one can show that it is impossible to make all elements equal to zero.
Submitted Solution:
```
n = int(input())
l = [*map(int , input().split())]
ss = 0
s1 = 0
for i in range(0 , 2 , n):
ss += l[i]
for i in range(1 , 2 , n):
s1 += l[i]
if (n == 2) and (s1%2) == (ss%2) and l[0] != l[1]:
print("NO")
exit()
print("YES" if (s1%2) == (ss%2) else "NO")
``` | instruction | 0 | 9,201 | 12 | 18,402 |
No | output | 1 | 9,201 | 12 | 18,403 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recall that the permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
A sequence a is a subsegment of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l, r], where l, r are two integers with 1 ≤ l ≤ r ≤ n. This indicates the subsegment where l-1 elements from the beginning and n-r elements from the end are deleted from the sequence.
For a permutation p_1, p_2, …, p_n, we define a framed segment as a subsegment [l,r] where max\\{p_l, p_{l+1}, ..., p_r\} - min\\{p_l, p_{l+1}, ..., p_r\} = r - l. For example, for the permutation (6, 7, 1, 8, 5, 3, 2, 4) some of its framed segments are: [1, 2], [5, 8], [6, 7], [3, 3], [8, 8]. In particular, a subsegment [i,i] is always a framed segments for any i between 1 and n, inclusive.
We define the happiness of a permutation p as the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n, and [l, r] is a framed segment. For example, the permutation [3, 1, 2] has happiness 5: all segments except [1, 2] are framed segments.
Given integers n and m, Jongwon wants to compute the sum of happiness for all permutations of length n, modulo the prime number m. Note that there exist n! (factorial of n) different permutations of length n.
Input
The only line contains two integers n and m (1 ≤ n ≤ 250 000, 10^8 ≤ m ≤ 10^9, m is prime).
Output
Print r (0 ≤ r < m), the sum of happiness for all permutations of length n, modulo a prime number m.
Examples
Input
1 993244853
Output
1
Input
2 993244853
Output
6
Input
3 993244853
Output
32
Input
2019 993244853
Output
923958830
Input
2020 437122297
Output
265955509
Note
For sample input n=3, let's consider all permutations of length 3:
* [1, 2, 3], all subsegments are framed segment. Happiness is 6.
* [1, 3, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [2, 1, 3], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [2, 3, 1], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [3, 1, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [3, 2, 1], all subsegments are framed segment. Happiness is 6.
Thus, the sum of happiness is 6+5+5+5+5+6 = 32. | instruction | 0 | 9,221 | 12 | 18,442 |
Tags: combinatorics, math
Correct Solution:
```
n, m = map(int, input().split())
f = [1]
for i in range(1, n+1):
f.append(f[-1] * i % m)
s = 0
for x in range(1, n+1):
s += ((f[x] * f[n-x]) % m) * ((n-x+1) ** 2 % m) % m
s %= m
print(s)
``` | output | 1 | 9,221 | 12 | 18,443 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recall that the permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
A sequence a is a subsegment of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l, r], where l, r are two integers with 1 ≤ l ≤ r ≤ n. This indicates the subsegment where l-1 elements from the beginning and n-r elements from the end are deleted from the sequence.
For a permutation p_1, p_2, …, p_n, we define a framed segment as a subsegment [l,r] where max\\{p_l, p_{l+1}, ..., p_r\} - min\\{p_l, p_{l+1}, ..., p_r\} = r - l. For example, for the permutation (6, 7, 1, 8, 5, 3, 2, 4) some of its framed segments are: [1, 2], [5, 8], [6, 7], [3, 3], [8, 8]. In particular, a subsegment [i,i] is always a framed segments for any i between 1 and n, inclusive.
We define the happiness of a permutation p as the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n, and [l, r] is a framed segment. For example, the permutation [3, 1, 2] has happiness 5: all segments except [1, 2] are framed segments.
Given integers n and m, Jongwon wants to compute the sum of happiness for all permutations of length n, modulo the prime number m. Note that there exist n! (factorial of n) different permutations of length n.
Input
The only line contains two integers n and m (1 ≤ n ≤ 250 000, 10^8 ≤ m ≤ 10^9, m is prime).
Output
Print r (0 ≤ r < m), the sum of happiness for all permutations of length n, modulo a prime number m.
Examples
Input
1 993244853
Output
1
Input
2 993244853
Output
6
Input
3 993244853
Output
32
Input
2019 993244853
Output
923958830
Input
2020 437122297
Output
265955509
Note
For sample input n=3, let's consider all permutations of length 3:
* [1, 2, 3], all subsegments are framed segment. Happiness is 6.
* [1, 3, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [2, 1, 3], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [2, 3, 1], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [3, 1, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [3, 2, 1], all subsegments are framed segment. Happiness is 6.
Thus, the sum of happiness is 6+5+5+5+5+6 = 32. | instruction | 0 | 9,222 | 12 | 18,444 |
Tags: combinatorics, math
Correct Solution:
```
n, mod = map(int, input().split())
f = [1] + [0] * n
for i in range(1, n + 1):
f[i] = (i * f[i - 1]) % mod
ret = 0
for size in range(1, n + 1):
ret += (n - size + 1) * (f[size] * f[n - size + 1]) % mod
ret %= mod
print(ret)
``` | output | 1 | 9,222 | 12 | 18,445 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recall that the permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
A sequence a is a subsegment of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l, r], where l, r are two integers with 1 ≤ l ≤ r ≤ n. This indicates the subsegment where l-1 elements from the beginning and n-r elements from the end are deleted from the sequence.
For a permutation p_1, p_2, …, p_n, we define a framed segment as a subsegment [l,r] where max\\{p_l, p_{l+1}, ..., p_r\} - min\\{p_l, p_{l+1}, ..., p_r\} = r - l. For example, for the permutation (6, 7, 1, 8, 5, 3, 2, 4) some of its framed segments are: [1, 2], [5, 8], [6, 7], [3, 3], [8, 8]. In particular, a subsegment [i,i] is always a framed segments for any i between 1 and n, inclusive.
We define the happiness of a permutation p as the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n, and [l, r] is a framed segment. For example, the permutation [3, 1, 2] has happiness 5: all segments except [1, 2] are framed segments.
Given integers n and m, Jongwon wants to compute the sum of happiness for all permutations of length n, modulo the prime number m. Note that there exist n! (factorial of n) different permutations of length n.
Input
The only line contains two integers n and m (1 ≤ n ≤ 250 000, 10^8 ≤ m ≤ 10^9, m is prime).
Output
Print r (0 ≤ r < m), the sum of happiness for all permutations of length n, modulo a prime number m.
Examples
Input
1 993244853
Output
1
Input
2 993244853
Output
6
Input
3 993244853
Output
32
Input
2019 993244853
Output
923958830
Input
2020 437122297
Output
265955509
Note
For sample input n=3, let's consider all permutations of length 3:
* [1, 2, 3], all subsegments are framed segment. Happiness is 6.
* [1, 3, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [2, 1, 3], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [2, 3, 1], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [3, 1, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [3, 2, 1], all subsegments are framed segment. Happiness is 6.
Thus, the sum of happiness is 6+5+5+5+5+6 = 32. | instruction | 0 | 9,223 | 12 | 18,446 |
Tags: combinatorics, math
Correct Solution:
```
n,m=map(int,input().split())
x = [1]
ans = 0
for i in range(1, n + 1):
x.append(i * x[-1] %m)
for i in range(1,n+1):
ans = ((n-i+1)*x[i]*x[n-i+1]%m + ans)%m
print(ans)
``` | output | 1 | 9,223 | 12 | 18,447 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recall that the permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
A sequence a is a subsegment of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l, r], where l, r are two integers with 1 ≤ l ≤ r ≤ n. This indicates the subsegment where l-1 elements from the beginning and n-r elements from the end are deleted from the sequence.
For a permutation p_1, p_2, …, p_n, we define a framed segment as a subsegment [l,r] where max\\{p_l, p_{l+1}, ..., p_r\} - min\\{p_l, p_{l+1}, ..., p_r\} = r - l. For example, for the permutation (6, 7, 1, 8, 5, 3, 2, 4) some of its framed segments are: [1, 2], [5, 8], [6, 7], [3, 3], [8, 8]. In particular, a subsegment [i,i] is always a framed segments for any i between 1 and n, inclusive.
We define the happiness of a permutation p as the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n, and [l, r] is a framed segment. For example, the permutation [3, 1, 2] has happiness 5: all segments except [1, 2] are framed segments.
Given integers n and m, Jongwon wants to compute the sum of happiness for all permutations of length n, modulo the prime number m. Note that there exist n! (factorial of n) different permutations of length n.
Input
The only line contains two integers n and m (1 ≤ n ≤ 250 000, 10^8 ≤ m ≤ 10^9, m is prime).
Output
Print r (0 ≤ r < m), the sum of happiness for all permutations of length n, modulo a prime number m.
Examples
Input
1 993244853
Output
1
Input
2 993244853
Output
6
Input
3 993244853
Output
32
Input
2019 993244853
Output
923958830
Input
2020 437122297
Output
265955509
Note
For sample input n=3, let's consider all permutations of length 3:
* [1, 2, 3], all subsegments are framed segment. Happiness is 6.
* [1, 3, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [2, 1, 3], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [2, 3, 1], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [3, 1, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [3, 2, 1], all subsegments are framed segment. Happiness is 6.
Thus, the sum of happiness is 6+5+5+5+5+6 = 32. | instruction | 0 | 9,224 | 12 | 18,448 |
Tags: combinatorics, math
Correct Solution:
```
n,m=map(int,input().split())
f=[0]*(n+1)
f[0]=1
for i in range(1,n+1):
f[i]=((f[i-1]%m)*(i%m))%m
ans=0
for i in range(1,n+1):
ans+=((n-i+1)**2)%m*(f[i]%m)*(f[n-i]%m)
ans%=m
print(ans)
``` | output | 1 | 9,224 | 12 | 18,449 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recall that the permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
A sequence a is a subsegment of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l, r], where l, r are two integers with 1 ≤ l ≤ r ≤ n. This indicates the subsegment where l-1 elements from the beginning and n-r elements from the end are deleted from the sequence.
For a permutation p_1, p_2, …, p_n, we define a framed segment as a subsegment [l,r] where max\\{p_l, p_{l+1}, ..., p_r\} - min\\{p_l, p_{l+1}, ..., p_r\} = r - l. For example, for the permutation (6, 7, 1, 8, 5, 3, 2, 4) some of its framed segments are: [1, 2], [5, 8], [6, 7], [3, 3], [8, 8]. In particular, a subsegment [i,i] is always a framed segments for any i between 1 and n, inclusive.
We define the happiness of a permutation p as the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n, and [l, r] is a framed segment. For example, the permutation [3, 1, 2] has happiness 5: all segments except [1, 2] are framed segments.
Given integers n and m, Jongwon wants to compute the sum of happiness for all permutations of length n, modulo the prime number m. Note that there exist n! (factorial of n) different permutations of length n.
Input
The only line contains two integers n and m (1 ≤ n ≤ 250 000, 10^8 ≤ m ≤ 10^9, m is prime).
Output
Print r (0 ≤ r < m), the sum of happiness for all permutations of length n, modulo a prime number m.
Examples
Input
1 993244853
Output
1
Input
2 993244853
Output
6
Input
3 993244853
Output
32
Input
2019 993244853
Output
923958830
Input
2020 437122297
Output
265955509
Note
For sample input n=3, let's consider all permutations of length 3:
* [1, 2, 3], all subsegments are framed segment. Happiness is 6.
* [1, 3, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [2, 1, 3], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [2, 3, 1], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [3, 1, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [3, 2, 1], all subsegments are framed segment. Happiness is 6.
Thus, the sum of happiness is 6+5+5+5+5+6 = 32. | instruction | 0 | 9,225 | 12 | 18,450 |
Tags: combinatorics, math
Correct Solution:
```
input = __import__('sys').stdin.readline
MIS = lambda: map(int,input().split())
n, MOD = MIS()
ans = 0
fac = [1]
for i in range(1, n+2): fac.append(fac[-1] * i % MOD)
for d in range(1, n+1):
ans+= (n-d+1)**2 * fac[d] * fac[n-d]
ans%= MOD
print(ans)
``` | output | 1 | 9,225 | 12 | 18,451 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recall that the permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
A sequence a is a subsegment of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l, r], where l, r are two integers with 1 ≤ l ≤ r ≤ n. This indicates the subsegment where l-1 elements from the beginning and n-r elements from the end are deleted from the sequence.
For a permutation p_1, p_2, …, p_n, we define a framed segment as a subsegment [l,r] where max\\{p_l, p_{l+1}, ..., p_r\} - min\\{p_l, p_{l+1}, ..., p_r\} = r - l. For example, for the permutation (6, 7, 1, 8, 5, 3, 2, 4) some of its framed segments are: [1, 2], [5, 8], [6, 7], [3, 3], [8, 8]. In particular, a subsegment [i,i] is always a framed segments for any i between 1 and n, inclusive.
We define the happiness of a permutation p as the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n, and [l, r] is a framed segment. For example, the permutation [3, 1, 2] has happiness 5: all segments except [1, 2] are framed segments.
Given integers n and m, Jongwon wants to compute the sum of happiness for all permutations of length n, modulo the prime number m. Note that there exist n! (factorial of n) different permutations of length n.
Input
The only line contains two integers n and m (1 ≤ n ≤ 250 000, 10^8 ≤ m ≤ 10^9, m is prime).
Output
Print r (0 ≤ r < m), the sum of happiness for all permutations of length n, modulo a prime number m.
Examples
Input
1 993244853
Output
1
Input
2 993244853
Output
6
Input
3 993244853
Output
32
Input
2019 993244853
Output
923958830
Input
2020 437122297
Output
265955509
Note
For sample input n=3, let's consider all permutations of length 3:
* [1, 2, 3], all subsegments are framed segment. Happiness is 6.
* [1, 3, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [2, 1, 3], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [2, 3, 1], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [3, 1, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [3, 2, 1], all subsegments are framed segment. Happiness is 6.
Thus, the sum of happiness is 6+5+5+5+5+6 = 32. | instruction | 0 | 9,226 | 12 | 18,452 |
Tags: combinatorics, math
Correct Solution:
```
n,m=map(int,input().split())
fac=[0]*(n+1)
fac[0]=1
ans=0
for i in range(1,n+1):fac[i]=(fac[i-1]*i)%m
#print(fac)
for i in range(1,n+1):
p=fac[i]*fac[n-i+1]*(n-i+1)
#print(p)
ans+=p
ans%=m
print(ans)
``` | output | 1 | 9,226 | 12 | 18,453 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recall that the permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
A sequence a is a subsegment of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l, r], where l, r are two integers with 1 ≤ l ≤ r ≤ n. This indicates the subsegment where l-1 elements from the beginning and n-r elements from the end are deleted from the sequence.
For a permutation p_1, p_2, …, p_n, we define a framed segment as a subsegment [l,r] where max\\{p_l, p_{l+1}, ..., p_r\} - min\\{p_l, p_{l+1}, ..., p_r\} = r - l. For example, for the permutation (6, 7, 1, 8, 5, 3, 2, 4) some of its framed segments are: [1, 2], [5, 8], [6, 7], [3, 3], [8, 8]. In particular, a subsegment [i,i] is always a framed segments for any i between 1 and n, inclusive.
We define the happiness of a permutation p as the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n, and [l, r] is a framed segment. For example, the permutation [3, 1, 2] has happiness 5: all segments except [1, 2] are framed segments.
Given integers n and m, Jongwon wants to compute the sum of happiness for all permutations of length n, modulo the prime number m. Note that there exist n! (factorial of n) different permutations of length n.
Input
The only line contains two integers n and m (1 ≤ n ≤ 250 000, 10^8 ≤ m ≤ 10^9, m is prime).
Output
Print r (0 ≤ r < m), the sum of happiness for all permutations of length n, modulo a prime number m.
Examples
Input
1 993244853
Output
1
Input
2 993244853
Output
6
Input
3 993244853
Output
32
Input
2019 993244853
Output
923958830
Input
2020 437122297
Output
265955509
Note
For sample input n=3, let's consider all permutations of length 3:
* [1, 2, 3], all subsegments are framed segment. Happiness is 6.
* [1, 3, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [2, 1, 3], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [2, 3, 1], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [3, 1, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [3, 2, 1], all subsegments are framed segment. Happiness is 6.
Thus, the sum of happiness is 6+5+5+5+5+6 = 32. | instruction | 0 | 9,227 | 12 | 18,454 |
Tags: combinatorics, math
Correct Solution:
```
n, m = [int(x) for x in input().split()]
fac=[1]*n
for i in range(2, n+1):
fac[i-1]=(fac[i-2]*i)%m
ans=0
for i in range(n):
ans+=(((i+1)*fac[i]%m)*fac[n-i-1])%m
ans%=m
print (ans)
``` | output | 1 | 9,227 | 12 | 18,455 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recall that the permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
A sequence a is a subsegment of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l, r], where l, r are two integers with 1 ≤ l ≤ r ≤ n. This indicates the subsegment where l-1 elements from the beginning and n-r elements from the end are deleted from the sequence.
For a permutation p_1, p_2, …, p_n, we define a framed segment as a subsegment [l,r] where max\\{p_l, p_{l+1}, ..., p_r\} - min\\{p_l, p_{l+1}, ..., p_r\} = r - l. For example, for the permutation (6, 7, 1, 8, 5, 3, 2, 4) some of its framed segments are: [1, 2], [5, 8], [6, 7], [3, 3], [8, 8]. In particular, a subsegment [i,i] is always a framed segments for any i between 1 and n, inclusive.
We define the happiness of a permutation p as the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n, and [l, r] is a framed segment. For example, the permutation [3, 1, 2] has happiness 5: all segments except [1, 2] are framed segments.
Given integers n and m, Jongwon wants to compute the sum of happiness for all permutations of length n, modulo the prime number m. Note that there exist n! (factorial of n) different permutations of length n.
Input
The only line contains two integers n and m (1 ≤ n ≤ 250 000, 10^8 ≤ m ≤ 10^9, m is prime).
Output
Print r (0 ≤ r < m), the sum of happiness for all permutations of length n, modulo a prime number m.
Examples
Input
1 993244853
Output
1
Input
2 993244853
Output
6
Input
3 993244853
Output
32
Input
2019 993244853
Output
923958830
Input
2020 437122297
Output
265955509
Note
For sample input n=3, let's consider all permutations of length 3:
* [1, 2, 3], all subsegments are framed segment. Happiness is 6.
* [1, 3, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [2, 1, 3], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [2, 3, 1], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [3, 1, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [3, 2, 1], all subsegments are framed segment. Happiness is 6.
Thus, the sum of happiness is 6+5+5+5+5+6 = 32. | instruction | 0 | 9,228 | 12 | 18,456 |
Tags: combinatorics, math
Correct Solution:
```
from sys import stdin
from itertools import permutations
def input():
return stdin.readline()[:-1]
def intput():
return int(input())
def sinput():
return input().split()
def intsput():
return map(int, sinput())
debugging = False
def dprint(*args):
if debugging:
print(*args)
else:
pass
n, mod = intsput()
fact = [1]
for i in range(1, 250001):
fact.append((fact[-1] * i) % mod)
cnt = 0
for k in range(1, n + 1):
cnt += ((k ** 2 ) % mod) * fact[n + 1 - k] * fact[k - 1]
cnt %= mod
print(cnt)
``` | output | 1 | 9,228 | 12 | 18,457 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recall that the permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
A sequence a is a subsegment of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l, r], where l, r are two integers with 1 ≤ l ≤ r ≤ n. This indicates the subsegment where l-1 elements from the beginning and n-r elements from the end are deleted from the sequence.
For a permutation p_1, p_2, …, p_n, we define a framed segment as a subsegment [l,r] where max\\{p_l, p_{l+1}, ..., p_r\} - min\\{p_l, p_{l+1}, ..., p_r\} = r - l. For example, for the permutation (6, 7, 1, 8, 5, 3, 2, 4) some of its framed segments are: [1, 2], [5, 8], [6, 7], [3, 3], [8, 8]. In particular, a subsegment [i,i] is always a framed segments for any i between 1 and n, inclusive.
We define the happiness of a permutation p as the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n, and [l, r] is a framed segment. For example, the permutation [3, 1, 2] has happiness 5: all segments except [1, 2] are framed segments.
Given integers n and m, Jongwon wants to compute the sum of happiness for all permutations of length n, modulo the prime number m. Note that there exist n! (factorial of n) different permutations of length n.
Input
The only line contains two integers n and m (1 ≤ n ≤ 250 000, 10^8 ≤ m ≤ 10^9, m is prime).
Output
Print r (0 ≤ r < m), the sum of happiness for all permutations of length n, modulo a prime number m.
Examples
Input
1 993244853
Output
1
Input
2 993244853
Output
6
Input
3 993244853
Output
32
Input
2019 993244853
Output
923958830
Input
2020 437122297
Output
265955509
Note
For sample input n=3, let's consider all permutations of length 3:
* [1, 2, 3], all subsegments are framed segment. Happiness is 6.
* [1, 3, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [2, 1, 3], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [2, 3, 1], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [3, 1, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [3, 2, 1], all subsegments are framed segment. Happiness is 6.
Thus, the sum of happiness is 6+5+5+5+5+6 = 32.
Submitted Solution:
```
n,m = map(int,input().split())
f= [1]
ans = 0
for i in range(1,n+1):
f.append((f[-1]*i)%m)
for i in range(1,n+1):
ans += ((n-i+1)*(f[i]*f[n-i+1]))%m
ans = ans%m
print(ans)
``` | instruction | 0 | 9,229 | 12 | 18,458 |
Yes | output | 1 | 9,229 | 12 | 18,459 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recall that the permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
A sequence a is a subsegment of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l, r], where l, r are two integers with 1 ≤ l ≤ r ≤ n. This indicates the subsegment where l-1 elements from the beginning and n-r elements from the end are deleted from the sequence.
For a permutation p_1, p_2, …, p_n, we define a framed segment as a subsegment [l,r] where max\\{p_l, p_{l+1}, ..., p_r\} - min\\{p_l, p_{l+1}, ..., p_r\} = r - l. For example, for the permutation (6, 7, 1, 8, 5, 3, 2, 4) some of its framed segments are: [1, 2], [5, 8], [6, 7], [3, 3], [8, 8]. In particular, a subsegment [i,i] is always a framed segments for any i between 1 and n, inclusive.
We define the happiness of a permutation p as the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n, and [l, r] is a framed segment. For example, the permutation [3, 1, 2] has happiness 5: all segments except [1, 2] are framed segments.
Given integers n and m, Jongwon wants to compute the sum of happiness for all permutations of length n, modulo the prime number m. Note that there exist n! (factorial of n) different permutations of length n.
Input
The only line contains two integers n and m (1 ≤ n ≤ 250 000, 10^8 ≤ m ≤ 10^9, m is prime).
Output
Print r (0 ≤ r < m), the sum of happiness for all permutations of length n, modulo a prime number m.
Examples
Input
1 993244853
Output
1
Input
2 993244853
Output
6
Input
3 993244853
Output
32
Input
2019 993244853
Output
923958830
Input
2020 437122297
Output
265955509
Note
For sample input n=3, let's consider all permutations of length 3:
* [1, 2, 3], all subsegments are framed segment. Happiness is 6.
* [1, 3, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [2, 1, 3], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [2, 3, 1], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [3, 1, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [3, 2, 1], all subsegments are framed segment. Happiness is 6.
Thus, the sum of happiness is 6+5+5+5+5+6 = 32.
Submitted Solution:
```
n,mod=map(int,input().split())
fact=[1,1]
for i in range(2,n+1):
fact.append((fact[-1]*i)%mod)
ans=0
for i in range(1,n+1):
ans+=((n-i+1)*(fact[i]*fact[n-i+1])%mod)%mod
ans=ans%mod
print (ans)
``` | instruction | 0 | 9,230 | 12 | 18,460 |
Yes | output | 1 | 9,230 | 12 | 18,461 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recall that the permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
A sequence a is a subsegment of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l, r], where l, r are two integers with 1 ≤ l ≤ r ≤ n. This indicates the subsegment where l-1 elements from the beginning and n-r elements from the end are deleted from the sequence.
For a permutation p_1, p_2, …, p_n, we define a framed segment as a subsegment [l,r] where max\\{p_l, p_{l+1}, ..., p_r\} - min\\{p_l, p_{l+1}, ..., p_r\} = r - l. For example, for the permutation (6, 7, 1, 8, 5, 3, 2, 4) some of its framed segments are: [1, 2], [5, 8], [6, 7], [3, 3], [8, 8]. In particular, a subsegment [i,i] is always a framed segments for any i between 1 and n, inclusive.
We define the happiness of a permutation p as the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n, and [l, r] is a framed segment. For example, the permutation [3, 1, 2] has happiness 5: all segments except [1, 2] are framed segments.
Given integers n and m, Jongwon wants to compute the sum of happiness for all permutations of length n, modulo the prime number m. Note that there exist n! (factorial of n) different permutations of length n.
Input
The only line contains two integers n and m (1 ≤ n ≤ 250 000, 10^8 ≤ m ≤ 10^9, m is prime).
Output
Print r (0 ≤ r < m), the sum of happiness for all permutations of length n, modulo a prime number m.
Examples
Input
1 993244853
Output
1
Input
2 993244853
Output
6
Input
3 993244853
Output
32
Input
2019 993244853
Output
923958830
Input
2020 437122297
Output
265955509
Note
For sample input n=3, let's consider all permutations of length 3:
* [1, 2, 3], all subsegments are framed segment. Happiness is 6.
* [1, 3, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [2, 1, 3], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [2, 3, 1], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [3, 1, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [3, 2, 1], all subsegments are framed segment. Happiness is 6.
Thus, the sum of happiness is 6+5+5+5+5+6 = 32.
Submitted Solution:
```
n, mod = map(int, input().split())
ans = 0
fac = [1]*(n+1)
for i in range(2, n+1):
fac[i] = fac[i-1] * i % mod
ans = 0
for x in range(1, n+1):
ans = (ans + fac[x] * fac[n-x] * (n - (x-1))**2) % mod
print(ans)
``` | instruction | 0 | 9,231 | 12 | 18,462 |
Yes | output | 1 | 9,231 | 12 | 18,463 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recall that the permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
A sequence a is a subsegment of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l, r], where l, r are two integers with 1 ≤ l ≤ r ≤ n. This indicates the subsegment where l-1 elements from the beginning and n-r elements from the end are deleted from the sequence.
For a permutation p_1, p_2, …, p_n, we define a framed segment as a subsegment [l,r] where max\\{p_l, p_{l+1}, ..., p_r\} - min\\{p_l, p_{l+1}, ..., p_r\} = r - l. For example, for the permutation (6, 7, 1, 8, 5, 3, 2, 4) some of its framed segments are: [1, 2], [5, 8], [6, 7], [3, 3], [8, 8]. In particular, a subsegment [i,i] is always a framed segments for any i between 1 and n, inclusive.
We define the happiness of a permutation p as the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n, and [l, r] is a framed segment. For example, the permutation [3, 1, 2] has happiness 5: all segments except [1, 2] are framed segments.
Given integers n and m, Jongwon wants to compute the sum of happiness for all permutations of length n, modulo the prime number m. Note that there exist n! (factorial of n) different permutations of length n.
Input
The only line contains two integers n and m (1 ≤ n ≤ 250 000, 10^8 ≤ m ≤ 10^9, m is prime).
Output
Print r (0 ≤ r < m), the sum of happiness for all permutations of length n, modulo a prime number m.
Examples
Input
1 993244853
Output
1
Input
2 993244853
Output
6
Input
3 993244853
Output
32
Input
2019 993244853
Output
923958830
Input
2020 437122297
Output
265955509
Note
For sample input n=3, let's consider all permutations of length 3:
* [1, 2, 3], all subsegments are framed segment. Happiness is 6.
* [1, 3, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [2, 1, 3], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [2, 3, 1], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [3, 1, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [3, 2, 1], all subsegments are framed segment. Happiness is 6.
Thus, the sum of happiness is 6+5+5+5+5+6 = 32.
Submitted Solution:
```
n, m = map(int, input().split())
f = []
f.append(1)
for i in range(1, n+1):
f.append((f[-1]*i)%m)
ans = 0
for i in range(n):
ans = (ans+(((f[i+1]*f[n-i])%m)*(n-i))%m)%m
print(ans)
``` | instruction | 0 | 9,232 | 12 | 18,464 |
Yes | output | 1 | 9,232 | 12 | 18,465 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recall that the permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
A sequence a is a subsegment of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l, r], where l, r are two integers with 1 ≤ l ≤ r ≤ n. This indicates the subsegment where l-1 elements from the beginning and n-r elements from the end are deleted from the sequence.
For a permutation p_1, p_2, …, p_n, we define a framed segment as a subsegment [l,r] where max\\{p_l, p_{l+1}, ..., p_r\} - min\\{p_l, p_{l+1}, ..., p_r\} = r - l. For example, for the permutation (6, 7, 1, 8, 5, 3, 2, 4) some of its framed segments are: [1, 2], [5, 8], [6, 7], [3, 3], [8, 8]. In particular, a subsegment [i,i] is always a framed segments for any i between 1 and n, inclusive.
We define the happiness of a permutation p as the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n, and [l, r] is a framed segment. For example, the permutation [3, 1, 2] has happiness 5: all segments except [1, 2] are framed segments.
Given integers n and m, Jongwon wants to compute the sum of happiness for all permutations of length n, modulo the prime number m. Note that there exist n! (factorial of n) different permutations of length n.
Input
The only line contains two integers n and m (1 ≤ n ≤ 250 000, 10^8 ≤ m ≤ 10^9, m is prime).
Output
Print r (0 ≤ r < m), the sum of happiness for all permutations of length n, modulo a prime number m.
Examples
Input
1 993244853
Output
1
Input
2 993244853
Output
6
Input
3 993244853
Output
32
Input
2019 993244853
Output
923958830
Input
2020 437122297
Output
265955509
Note
For sample input n=3, let's consider all permutations of length 3:
* [1, 2, 3], all subsegments are framed segment. Happiness is 6.
* [1, 3, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [2, 1, 3], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [2, 3, 1], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [3, 1, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [3, 2, 1], all subsegments are framed segment. Happiness is 6.
Thus, the sum of happiness is 6+5+5+5+5+6 = 32.
Submitted Solution:
```
n,m=[int(i) for i in input().split()]
arr=[0]*(n+1)
arr[0]=1
for i in range(1,n+1):
arr[i]=(arr[i-1]*i)%m
#print("i ",i," ",arr[i])
total=0
for i in range(0,n):
total+=(arr[i+1]*(n-i)*arr[n-i])%m
print(total)
``` | instruction | 0 | 9,233 | 12 | 18,466 |
No | output | 1 | 9,233 | 12 | 18,467 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recall that the permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
A sequence a is a subsegment of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l, r], where l, r are two integers with 1 ≤ l ≤ r ≤ n. This indicates the subsegment where l-1 elements from the beginning and n-r elements from the end are deleted from the sequence.
For a permutation p_1, p_2, …, p_n, we define a framed segment as a subsegment [l,r] where max\\{p_l, p_{l+1}, ..., p_r\} - min\\{p_l, p_{l+1}, ..., p_r\} = r - l. For example, for the permutation (6, 7, 1, 8, 5, 3, 2, 4) some of its framed segments are: [1, 2], [5, 8], [6, 7], [3, 3], [8, 8]. In particular, a subsegment [i,i] is always a framed segments for any i between 1 and n, inclusive.
We define the happiness of a permutation p as the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n, and [l, r] is a framed segment. For example, the permutation [3, 1, 2] has happiness 5: all segments except [1, 2] are framed segments.
Given integers n and m, Jongwon wants to compute the sum of happiness for all permutations of length n, modulo the prime number m. Note that there exist n! (factorial of n) different permutations of length n.
Input
The only line contains two integers n and m (1 ≤ n ≤ 250 000, 10^8 ≤ m ≤ 10^9, m is prime).
Output
Print r (0 ≤ r < m), the sum of happiness for all permutations of length n, modulo a prime number m.
Examples
Input
1 993244853
Output
1
Input
2 993244853
Output
6
Input
3 993244853
Output
32
Input
2019 993244853
Output
923958830
Input
2020 437122297
Output
265955509
Note
For sample input n=3, let's consider all permutations of length 3:
* [1, 2, 3], all subsegments are framed segment. Happiness is 6.
* [1, 3, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [2, 1, 3], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [2, 3, 1], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [3, 1, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [3, 2, 1], all subsegments are framed segment. Happiness is 6.
Thus, the sum of happiness is 6+5+5+5+5+6 = 32.
Submitted Solution:
```
from sys import stdin,stdout
memo = {0:1}
def fac(x):
if x not in memo:
r = 1
for i in range(x):
if i in memo:
r = memo[i]
continue
else:
r *= i
memo[i] = r
memo[x] = x * r
return memo[x]
n,m = map(int, stdin.readline().split())
total = 0
mid = n//2+1
for i in range(1, mid):
total = (total + fac(n-i+1)*fac(i)) % m
total = (total*(n+1)) %m
if n%2:
total = (total + fac(n-mid+1)*fac(mid)) % m
print(total)
``` | instruction | 0 | 9,234 | 12 | 18,468 |
No | output | 1 | 9,234 | 12 | 18,469 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recall that the permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
A sequence a is a subsegment of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l, r], where l, r are two integers with 1 ≤ l ≤ r ≤ n. This indicates the subsegment where l-1 elements from the beginning and n-r elements from the end are deleted from the sequence.
For a permutation p_1, p_2, …, p_n, we define a framed segment as a subsegment [l,r] where max\\{p_l, p_{l+1}, ..., p_r\} - min\\{p_l, p_{l+1}, ..., p_r\} = r - l. For example, for the permutation (6, 7, 1, 8, 5, 3, 2, 4) some of its framed segments are: [1, 2], [5, 8], [6, 7], [3, 3], [8, 8]. In particular, a subsegment [i,i] is always a framed segments for any i between 1 and n, inclusive.
We define the happiness of a permutation p as the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n, and [l, r] is a framed segment. For example, the permutation [3, 1, 2] has happiness 5: all segments except [1, 2] are framed segments.
Given integers n and m, Jongwon wants to compute the sum of happiness for all permutations of length n, modulo the prime number m. Note that there exist n! (factorial of n) different permutations of length n.
Input
The only line contains two integers n and m (1 ≤ n ≤ 250 000, 10^8 ≤ m ≤ 10^9, m is prime).
Output
Print r (0 ≤ r < m), the sum of happiness for all permutations of length n, modulo a prime number m.
Examples
Input
1 993244853
Output
1
Input
2 993244853
Output
6
Input
3 993244853
Output
32
Input
2019 993244853
Output
923958830
Input
2020 437122297
Output
265955509
Note
For sample input n=3, let's consider all permutations of length 3:
* [1, 2, 3], all subsegments are framed segment. Happiness is 6.
* [1, 3, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [2, 1, 3], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [2, 3, 1], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [3, 1, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [3, 2, 1], all subsegments are framed segment. Happiness is 6.
Thus, the sum of happiness is 6+5+5+5+5+6 = 32.
Submitted Solution:
```
f=[1]
n,mod=map(int,input().split())
for i in range(1,n+1):
f.append(f[-1]*i%mod)
SUM=0
for i in range(1,n+1):
SUM+=f[i]*f[n-i+1]*(n-i+1)
SUM%mod
print(SUM)
``` | instruction | 0 | 9,235 | 12 | 18,470 |
No | output | 1 | 9,235 | 12 | 18,471 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recall that the permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
A sequence a is a subsegment of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l, r], where l, r are two integers with 1 ≤ l ≤ r ≤ n. This indicates the subsegment where l-1 elements from the beginning and n-r elements from the end are deleted from the sequence.
For a permutation p_1, p_2, …, p_n, we define a framed segment as a subsegment [l,r] where max\\{p_l, p_{l+1}, ..., p_r\} - min\\{p_l, p_{l+1}, ..., p_r\} = r - l. For example, for the permutation (6, 7, 1, 8, 5, 3, 2, 4) some of its framed segments are: [1, 2], [5, 8], [6, 7], [3, 3], [8, 8]. In particular, a subsegment [i,i] is always a framed segments for any i between 1 and n, inclusive.
We define the happiness of a permutation p as the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ n, and [l, r] is a framed segment. For example, the permutation [3, 1, 2] has happiness 5: all segments except [1, 2] are framed segments.
Given integers n and m, Jongwon wants to compute the sum of happiness for all permutations of length n, modulo the prime number m. Note that there exist n! (factorial of n) different permutations of length n.
Input
The only line contains two integers n and m (1 ≤ n ≤ 250 000, 10^8 ≤ m ≤ 10^9, m is prime).
Output
Print r (0 ≤ r < m), the sum of happiness for all permutations of length n, modulo a prime number m.
Examples
Input
1 993244853
Output
1
Input
2 993244853
Output
6
Input
3 993244853
Output
32
Input
2019 993244853
Output
923958830
Input
2020 437122297
Output
265955509
Note
For sample input n=3, let's consider all permutations of length 3:
* [1, 2, 3], all subsegments are framed segment. Happiness is 6.
* [1, 3, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [2, 1, 3], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [2, 3, 1], all subsegments except [2, 3] are framed segment. Happiness is 5.
* [3, 1, 2], all subsegments except [1, 2] are framed segment. Happiness is 5.
* [3, 2, 1], all subsegments are framed segment. Happiness is 6.
Thus, the sum of happiness is 6+5+5+5+5+6 = 32.
Submitted Solution:
```
s = input()+"$"
strn = ""
strp = ""
for i in s:
if i!=" " and i!="$":
strn+=i
if i==" ":
n=int(strn)
strn=""
if i=="$":
p=int(strn)
def res(k):
s = 1
if k<=0:
return 1
for i in range(k):
s = s*(i+1)
return s
nums = [1,6]
for _ in range(2,n):
lst = [nums[-1] for pos in range(_+1)]
for pos in range(_+1):
for con in range(_+1):
if pos == con:
lst[pos] += res(pos)*res(_-pos)
if pos == con and pos !=0 and pos!=_:
lst[pos] += res(pos)*res(_-pos)
if pos>con:
lst[pos] += res(con)*res(pos-con)*res(_-pos)
if pos<con:
lst[pos] += res(pos)*res(con-pos)*res(_-con)
nums.append(sum(lst)%p)
print(nums[-1])
``` | instruction | 0 | 9,236 | 12 | 18,472 |
No | output | 1 | 9,236 | 12 | 18,473 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three integers n, k, m and m conditions (l_1, r_1, x_1), (l_2, r_2, x_2), ..., (l_m, r_m, x_m).
Calculate the number of distinct arrays a, consisting of n integers such that:
* 0 ≤ a_i < 2^k for each 1 ≤ i ≤ n;
* bitwise AND of numbers a[l_i] \& a[l_i + 1] \& ... \& a[r_i] = x_i for each 1 ≤ i ≤ m.
Two arrays a and b are considered different if there exists such a position i that a_i ≠ b_i.
The number can be pretty large so print it modulo 998244353.
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 5 ⋅ 10^5, 1 ≤ k ≤ 30, 0 ≤ m ≤ 5 ⋅ 10^5) — the length of the array a, the value such that all numbers in a should be smaller than 2^k and the number of conditions, respectively.
Each of the next m lines contains the description of a condition l_i, r_i and x_i (1 ≤ l_i ≤ r_i ≤ n, 0 ≤ x_i < 2^k) — the borders of the condition segment and the required bitwise AND value on it.
Output
Print a single integer — the number of distinct arrays a that satisfy all the above conditions modulo 998244353.
Examples
Input
4 3 2
1 3 3
3 4 6
Output
3
Input
5 2 3
1 3 2
2 5 0
3 3 3
Output
33
Note
You can recall what is a bitwise AND operation [here](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
In the first example, the answer is the following arrays: [3, 3, 7, 6], [3, 7, 7, 6] and [7, 3, 7, 6]. | instruction | 0 | 9,253 | 12 | 18,506 |
Tags: bitmasks, combinatorics, data structures, dp, two pointers
Correct Solution:
```
def main():
import sys
input = sys.stdin.buffer.readline
mod = 998244353
N, K, M = map(int, input().split())
cond = []
for _ in range(M):
cond.append(tuple(map(int, input().split())))
ans = 1
for k in range(K):
one = [0] * (N+1)
zero_cond = [0] * (N+1)
for l, r, x in cond:
if x >> k & 1:
one[l-1] += 1
one[r] -= 1
else:
zero_cond[r] = max(zero_cond[r], l)
for i in range(N):
one[i+1] += one[i]
dp = [0] * (N+1)
dp[0] = 1
cs = [0] * (N+2)
cs[1] = 1
R = 0
for i in range(N):
if not one[i]:
dp[i+1] = (cs[i+1] - cs[R])%mod
cs[i+2] = (cs[i+1] + dp[i+1])%mod
R = max(R, zero_cond[i+1])
ans = (ans * (cs[-1] - cs[R])%mod)%mod
print(ans)
if __name__ == '__main__':
main()
``` | output | 1 | 9,253 | 12 | 18,507 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given three integers n, k, m and m conditions (l_1, r_1, x_1), (l_2, r_2, x_2), ..., (l_m, r_m, x_m).
Calculate the number of distinct arrays a, consisting of n integers such that:
* 0 ≤ a_i < 2^k for each 1 ≤ i ≤ n;
* bitwise AND of numbers a[l_i] \& a[l_i + 1] \& ... \& a[r_i] = x_i for each 1 ≤ i ≤ m.
Two arrays a and b are considered different if there exists such a position i that a_i ≠ b_i.
The number can be pretty large so print it modulo 998244353.
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 5 ⋅ 10^5, 1 ≤ k ≤ 30, 0 ≤ m ≤ 5 ⋅ 10^5) — the length of the array a, the value such that all numbers in a should be smaller than 2^k and the number of conditions, respectively.
Each of the next m lines contains the description of a condition l_i, r_i and x_i (1 ≤ l_i ≤ r_i ≤ n, 0 ≤ x_i < 2^k) — the borders of the condition segment and the required bitwise AND value on it.
Output
Print a single integer — the number of distinct arrays a that satisfy all the above conditions modulo 998244353.
Examples
Input
4 3 2
1 3 3
3 4 6
Output
3
Input
5 2 3
1 3 2
2 5 0
3 3 3
Output
33
Note
You can recall what is a bitwise AND operation [here](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
In the first example, the answer is the following arrays: [3, 3, 7, 6], [3, 7, 7, 6] and [7, 3, 7, 6]. | instruction | 0 | 9,254 | 12 | 18,508 |
Tags: bitmasks, combinatorics, data structures, dp, two pointers
Correct Solution:
```
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from itertools import accumulate
n,k,m=map(int,input().split())
Q=[tuple(map(int,input().split())) for i in range(m)]
mod=998244353
S=1
for keta in range(k):
ANS=[0]*(n+5)
QK=[n+3]*(n+5)
for l,r,x in Q:
l-=1
r-=1
if x & (1<<keta)!=0:
ANS[l]+=1
ANS[r+1]-=1
else:
QK[l]=min(QK[l],r+1)
ANS=tuple(accumulate(ANS))
for i in range(n+3,-1,-1):
QK[i]=min(QK[i],QK[i+1])
PLUS=[0]*(n+5)
PLUS[0]=1
PLUS[1]=-1
for i in range(n+1):
PLUS[i]=(PLUS[i]+PLUS[i-1])%mod
if ANS[i-1]==0:
last=QK[i]
PLUS[i+1]=(PLUS[i]+PLUS[i+1])%mod
PLUS[last+1]=(PLUS[last+1]-PLUS[i])%mod
S=S*(PLUS[n]+PLUS[n+1])%mod
print(S)
``` | output | 1 | 9,254 | 12 | 18,509 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given three integers n, k, m and m conditions (l_1, r_1, x_1), (l_2, r_2, x_2), ..., (l_m, r_m, x_m).
Calculate the number of distinct arrays a, consisting of n integers such that:
* 0 ≤ a_i < 2^k for each 1 ≤ i ≤ n;
* bitwise AND of numbers a[l_i] \& a[l_i + 1] \& ... \& a[r_i] = x_i for each 1 ≤ i ≤ m.
Two arrays a and b are considered different if there exists such a position i that a_i ≠ b_i.
The number can be pretty large so print it modulo 998244353.
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 5 ⋅ 10^5, 1 ≤ k ≤ 30, 0 ≤ m ≤ 5 ⋅ 10^5) — the length of the array a, the value such that all numbers in a should be smaller than 2^k and the number of conditions, respectively.
Each of the next m lines contains the description of a condition l_i, r_i and x_i (1 ≤ l_i ≤ r_i ≤ n, 0 ≤ x_i < 2^k) — the borders of the condition segment and the required bitwise AND value on it.
Output
Print a single integer — the number of distinct arrays a that satisfy all the above conditions modulo 998244353.
Examples
Input
4 3 2
1 3 3
3 4 6
Output
3
Input
5 2 3
1 3 2
2 5 0
3 3 3
Output
33
Note
You can recall what is a bitwise AND operation [here](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
In the first example, the answer is the following arrays: [3, 3, 7, 6], [3, 7, 7, 6] and [7, 3, 7, 6].
Submitted Solution:
```
MOD = 998244353
# MOD = 10
def add(a1, a2):
# return (a1 + a2 + MOD) % MOD
return a1 + a2
def solve():
last_zero = [0] * (n + 2) # [0 ... n + 1]
is_one = [0] * (n + 2)
for ii in range(m):
if b[ii] == 0:
last_zero[r[ii] + 1] = l[ii]
else:
is_one[l[ii]] = is_one[l[ii]] + 1
is_one[r[ii] + 1] = is_one[r[ii] + 1] - 1
for ii in range(len(last_zero)):
if ii != 0:
last_zero[ii] = max(last_zero[ii], last_zero[ii - 1])
s = 0
for ii in range(len(is_one)):
s = s + is_one[ii]
is_one[ii] = 1 if s > 0 else 0
dp = [0] * (n + 2)
# dp[i] -> accumulated sums
# number of arrays of i+1 numbers such that i+1th element is 0 and
# every range is OK (0s have at least one 0 and 1s are all 1
# for elements [0 to i])
dp[0] = 1
for ii in range(1, n + 2):
if is_one[ii]:
dp[ii] = add(dp[ii - 1], 0)
else:
dp[ii] = add(dp[ii - 1], add(dp[ii - 1], -dp[last_zero[ii]-1])) # if last_zero[ii] != -1 else 0
return add(dp[n+1], -dp[n])
n, k, m = map(int, input().split())
l = [-1] * m
r = [-1] * m
x = [-1] * m
b = [-1] * m
for i in range(m):
l[i], r[i], x[i] = map(int, input().split())
result = 1
for i in range(k):
for j in range(m):
b[j] = x[j] % 2
x[j] = x[j] // 2
result = (result * solve()) % MOD
print(result)
``` | instruction | 0 | 9,255 | 12 | 18,510 |
No | output | 1 | 9,255 | 12 | 18,511 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given three integers n, k, m and m conditions (l_1, r_1, x_1), (l_2, r_2, x_2), ..., (l_m, r_m, x_m).
Calculate the number of distinct arrays a, consisting of n integers such that:
* 0 ≤ a_i < 2^k for each 1 ≤ i ≤ n;
* bitwise AND of numbers a[l_i] \& a[l_i + 1] \& ... \& a[r_i] = x_i for each 1 ≤ i ≤ m.
Two arrays a and b are considered different if there exists such a position i that a_i ≠ b_i.
The number can be pretty large so print it modulo 998244353.
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 5 ⋅ 10^5, 1 ≤ k ≤ 30, 0 ≤ m ≤ 5 ⋅ 10^5) — the length of the array a, the value such that all numbers in a should be smaller than 2^k and the number of conditions, respectively.
Each of the next m lines contains the description of a condition l_i, r_i and x_i (1 ≤ l_i ≤ r_i ≤ n, 0 ≤ x_i < 2^k) — the borders of the condition segment and the required bitwise AND value on it.
Output
Print a single integer — the number of distinct arrays a that satisfy all the above conditions modulo 998244353.
Examples
Input
4 3 2
1 3 3
3 4 6
Output
3
Input
5 2 3
1 3 2
2 5 0
3 3 3
Output
33
Note
You can recall what is a bitwise AND operation [here](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
In the first example, the answer is the following arrays: [3, 3, 7, 6], [3, 7, 7, 6] and [7, 3, 7, 6].
Submitted Solution:
```
MOD = 998244353
# MOD = 10
def add(a1, a2):
# return (a1 + a2 + MOD) % MOD
return a1 + a2
def solve():
last_zero = [0] * (n + 2) # [0 ... n + 1]
is_one = [0] * (n + 2)
for ii in range(m):
if b[ii] == 0:
last_zero[r[ii] + 1] = l[ii]
else:
is_one[l[ii]] = is_one[l[ii]] + 1
is_one[r[ii] + 1] = is_one[r[ii] + 1] - 1
for ii in range(len(last_zero)):
if ii != 0:
last_zero[ii] = max(last_zero[ii], last_zero[ii - 1])
s = 0
for ii in range(len(is_one)):
s = s + is_one[ii]
is_one[ii] = 1 if s > 0 else 0
dp = [0] * (n + 2)
# dp[i] -> accumulated sums
# number of arrays of i+1 numbers such that i+1th element is 0 and
# every range is OK (0s have at least one 0 and 1s are all 1
# for elements [0 to i])
dp[0] = 1
for ii in range(1, n + 2):
if is_one[ii]:
dp[ii] = add(dp[ii - 1], 0)
else:
dp[ii] = add(dp[ii - 1], add(dp[ii - 1], -dp[last_zero[ii]-1])) # if last_zero[ii] != -1 else 0
return add(dp[n+1], -dp[n])
n, k, m = map(int, input().split())
l = [-1] * m
r = [-1] * m
x = [-1] * m
b = [-1] * m
for i in range(m):
l[i], r[i], x[i] = map(int, input().split())
result = 1
for i in range(k):
for j in range(m):
b[j] = x[j] % 2
x[j] = x[j] // 2
result = (result * solve()) # % MOD
print(result % MOD)
``` | instruction | 0 | 9,256 | 12 | 18,512 |
No | output | 1 | 9,256 | 12 | 18,513 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given three integers n, k, m and m conditions (l_1, r_1, x_1), (l_2, r_2, x_2), ..., (l_m, r_m, x_m).
Calculate the number of distinct arrays a, consisting of n integers such that:
* 0 ≤ a_i < 2^k for each 1 ≤ i ≤ n;
* bitwise AND of numbers a[l_i] \& a[l_i + 1] \& ... \& a[r_i] = x_i for each 1 ≤ i ≤ m.
Two arrays a and b are considered different if there exists such a position i that a_i ≠ b_i.
The number can be pretty large so print it modulo 998244353.
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 5 ⋅ 10^5, 1 ≤ k ≤ 30, 0 ≤ m ≤ 5 ⋅ 10^5) — the length of the array a, the value such that all numbers in a should be smaller than 2^k and the number of conditions, respectively.
Each of the next m lines contains the description of a condition l_i, r_i and x_i (1 ≤ l_i ≤ r_i ≤ n, 0 ≤ x_i < 2^k) — the borders of the condition segment and the required bitwise AND value on it.
Output
Print a single integer — the number of distinct arrays a that satisfy all the above conditions modulo 998244353.
Examples
Input
4 3 2
1 3 3
3 4 6
Output
3
Input
5 2 3
1 3 2
2 5 0
3 3 3
Output
33
Note
You can recall what is a bitwise AND operation [here](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
In the first example, the answer is the following arrays: [3, 3, 7, 6], [3, 7, 7, 6] and [7, 3, 7, 6].
Submitted Solution:
```
print("3")
``` | instruction | 0 | 9,257 | 12 | 18,514 |
No | output | 1 | 9,257 | 12 | 18,515 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given three integers n, k, m and m conditions (l_1, r_1, x_1), (l_2, r_2, x_2), ..., (l_m, r_m, x_m).
Calculate the number of distinct arrays a, consisting of n integers such that:
* 0 ≤ a_i < 2^k for each 1 ≤ i ≤ n;
* bitwise AND of numbers a[l_i] \& a[l_i + 1] \& ... \& a[r_i] = x_i for each 1 ≤ i ≤ m.
Two arrays a and b are considered different if there exists such a position i that a_i ≠ b_i.
The number can be pretty large so print it modulo 998244353.
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 5 ⋅ 10^5, 1 ≤ k ≤ 30, 0 ≤ m ≤ 5 ⋅ 10^5) — the length of the array a, the value such that all numbers in a should be smaller than 2^k and the number of conditions, respectively.
Each of the next m lines contains the description of a condition l_i, r_i and x_i (1 ≤ l_i ≤ r_i ≤ n, 0 ≤ x_i < 2^k) — the borders of the condition segment and the required bitwise AND value on it.
Output
Print a single integer — the number of distinct arrays a that satisfy all the above conditions modulo 998244353.
Examples
Input
4 3 2
1 3 3
3 4 6
Output
3
Input
5 2 3
1 3 2
2 5 0
3 3 3
Output
33
Note
You can recall what is a bitwise AND operation [here](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
In the first example, the answer is the following arrays: [3, 3, 7, 6], [3, 7, 7, 6] and [7, 3, 7, 6].
Submitted Solution:
```
MOD = 998244353
def add(a1, a2):
return (a1 + a2 + MOD) % MOD
def solve():
last_zero = [0] * (n + 2) # [0 ... n + 1]
is_one = [0] * (n + 2)
for ii in range(m):
if b[ii] == 0:
last_zero[r[ii] + 1] = l[ii]
else:
is_one[l[ii]] = is_one[l[ii]] + 1
is_one[r[ii] + 1] = is_one[r[ii] + 1] - 1
for ii in range(len(last_zero)):
if ii != 0:
last_zero[ii] = max(last_zero[ii], last_zero[ii - 1])
s = 0
for ii in range(len(is_one)):
s = s + is_one[ii]
is_one[ii] = 1 if s > 0 else 0
dp = [0] * (n + 2)
# dp[i] -> accumulated sums
# number of arrays of i+1 numbers such that i+1th element is 0 and
# every range is OK (0s have at least one 0 and 1s are all 1
# for elements [0 to i])
dp[0] = 1
for ii in range(1, n + 2):
if is_one[ii]:
dp[ii] = add(dp[ii - 1], 0)
else:
dp[ii] = add(dp[ii - 1], add(dp[ii - 1], -dp[last_zero[ii]-1])) # if last_zero[ii] != -1 else 0
return add(dp[n+1], -dp[n])
n, k, m = map(int, input().split())
l = [-1] * m
r = [-1] * m
x = [-1] * m
b = [-1] * m
for i in range(m):
l[i], r[i], x[i] = map(int, input().split())
result = 1
for i in range(k):
for j in range(m):
b[j] = x[j] % 2
x[j] = x[j] // 2
result = (result * solve()) % MOD
print(result)
``` | instruction | 0 | 9,258 | 12 | 18,516 |
No | output | 1 | 9,258 | 12 | 18,517 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d. | instruction | 0 | 9,297 | 12 | 18,594 |
Tags: implementation, math, sortings
Correct Solution:
```
import sys
input = sys.stdin.readline
from collections import Counter
for _ in range(int(input())):
n = int(input())
A = sorted(map(int, input().split()))
for k in A:
if k % 2:
print("NO")
break
else:
A = [k // 2 for k in A]
cnt = Counter(A)
keys = sorted(cnt.keys())
m = len(keys)
if m == 1:
k = keys[0]
if cnt[k] not in [2, 1]:
print("NO")
else:
print("YES")
continue
for i in keys:
if not i:
if cnt[i] not in [2, 1]:
print("NO")
break
else:
if cnt[i] != 2:
print("NO")
break
else:
# B = [0] * m
pre = 0
for i in range(m - 1, -1, -1):
cur = keys[i] - pre
if cur <= 0:
print("NO")
break
k, r = divmod(cur, i + 1)
if r:
print("NO")
break
pre += k
else:
print("YES")
``` | output | 1 | 9,297 | 12 | 18,595 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d. | instruction | 0 | 9,298 | 12 | 18,596 |
Tags: implementation, math, sortings
Correct Solution:
```
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
t=set()
ans='YES'
for i in a:
t.add(i)
if i%2:
ans='NO'
if len(t)!=n:
ans='NO'
if ans=='YES':
no=[]
for i in t:
no.append(i)
no.sort()
vl=0
divide_by=2*n
while no:
ck=no.pop()-vl
if ck<=0:
ans='NO'
break
if ck % divide_by:
ans='NO'
break
vl+=(ck//divide_by)*2
divide_by-=2
print(ans)
``` | output | 1 | 9,298 | 12 | 18,597 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d. | instruction | 0 | 9,299 | 12 | 18,598 |
Tags: implementation, math, sortings
Correct Solution:
```
import sys
T=int(sys.stdin.readline().strip())
while (T>0):
T-=1
n=int(sys.stdin.readline().strip())
b=sys.stdin.readline().strip().split(" ")
b=list(map(lambda x: int(x), b))
a=sorted(b,reverse=True)
#print (a)
sum=0
flag= True
cnt={}
for i in range(len(a)//2):
if(a[i*2]!=a[i*2+1]):
#print(a)
print("NO")
flag=False
break
one=a[i*2]
if one %2!=0:
print("NO")
flag=False
break
t=one//2
#print(t,sum)
if t<=sum:
print("NO")
flag=False
break
if ((t-sum)%n)==0:
#print((t-sum)//n)
if ((t-sum)//n) in cnt:
print("NO")
flag=False
break
cnt[(t-sum)//n] =1
sum+=(t-sum)//n
n-=1
else:
print("NO")
flag=False
break
if flag:
print("YES")
``` | output | 1 | 9,299 | 12 | 18,599 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d. | instruction | 0 | 9,300 | 12 | 18,600 |
Tags: implementation, math, sortings
Correct Solution:
```
import sys;input = lambda: sys.stdin.readline().rstrip()
#CF698-2-C-1500
#from heapq import heappush, heappop
#from collections import deque
#import numpy as np
#from collections import Counter as cnt
#from collections import defaultdict as ddc
#from math import factorial as fct
#from math import gcd
#from bisect import bisect_left as bsl
#from bisect import bisect_right as bsr
#from itertools import accumulate as acc
#from itertools import combinations as cmb
#from itertools import permutations as pmt
#from itertools import product as prd
#from functools import reduce as red
#import sys
#sys.setrecursionlimit(10**9) #再帰を多く使う(デフォルトは1000)
# = input()
# = int(input())
# = map(int,input().split())
# = list(map(int,input().split()))
# = sorted(map(int,input().split()))
# = [list(map(int,input().split())) for _ in range(int(input()))]
#mod = 998244353
#mod = 1000000007
for _ in range(int(input())):
n = int(input())
D = sorted(map(int,input().split()))[::-1]
Ans = [None]*n
flag = 1
if D[0]%(2*n) or D[0] != D[1]:
flag = 0
else:
Ans[0] = D[0]//(2*n)
for i in range(1, n):
if D[2*i] != D[2*i+1]:
flag = 0
break
if (D[2*i-1]-D[2*i])%(2*(n-i)):
flag = 0
break
Ans[i] = Ans[i-1]-(D[2*i-1]-D[2*i])//(2*(n-i))
if Ans[i] <= 0 or Ans[i] == Ans[i-1]:
flag = 0
break
if flag and sum(Ans)*2 == D[-1]:
print('YES')
else:
print('NO')
``` | output | 1 | 9,300 | 12 | 18,601 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d. | instruction | 0 | 9,301 | 12 | 18,602 |
Tags: implementation, math, sortings
Correct Solution:
```
def merge(array, begin, half, end):
copy_array = list()
i = begin
j = half+1
while(i <= half and j <= end):
if(array[i] < array[j]):
copy_array.append(array[i])
i+=1
else:
copy_array.append(array[j])
j+=1
while(i <= half):
copy_array.append(array[i])
i+=1
while(j <= end):
copy_array.append(array[j])
j+=1
k = 0
i = begin
while(i <= end):
array[i] = copy_array[k]
i+=1
k+=1
def mergesort(array, begin, end):
if(end <= begin):
return
half = int(begin + ((end-begin)/2))
mergesort(array, begin, half)
mergesort(array, half+1, end)
merge(array, begin, half, end)
return
def main():
test_cases = int(input())
for _ in range(test_cases):
n = int(input())
a_i = [int(num) for num in input().split()]
b_i = list()
d_i = list()
a_i = [num for num in reversed(sorted(a_i))]
right = True
for i in range(n):
if(a_i[i*2] != a_i[(i*2) + 1]):
right = False
break
b_i.append(a_i[i*2])
i = 1
while(i < n and right):
if(b_i[i-1] == b_i[i] or ((b_i[i-1] - b_i[i]) % (2 * (n-i))) != 0):
right = False
break
d_i.append((b_i[i-1]-b_i[i]) / 2 / (n-i))
i+=1
i = 1
while(i < n and right):
b_i[n-1] -= (2 * i * d_i[i-1])
i+=1
if(not right):
print('NO')
else:
if(b_i[n-1] <= 0 or b_i[n-1] % (2*n) != 0):
print('NO')
else:
print('YES')
if __name__ == '__main__':
main()
``` | output | 1 | 9,301 | 12 | 18,603 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d. | instruction | 0 | 9,302 | 12 | 18,604 |
Tags: implementation, math, sortings
Correct Solution:
```
import time,math as mt,bisect as bs,sys
from sys import stdin,stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi=3.14159265358979323846264338327950
def II(): # to take integer input
return int(stdin.readline())
def IP(): # to take tuple as input
return map(int,stdin.readline().split())
def L(): # to take list as input
return list(map(int,stdin.readline().split()))
def P(x): # to print integer,list,string etc..
return stdout.write(str(x)+"\n")
def PI(x,y): # to print tuple separatedly
return stdout.write(str(x)+" "+str(y)+"\n")
def lcm(a,b): # to calculate lcm
return (a*b)//gcd(a,b)
def gcd(a,b): # to calculate gcd
if a==0:
return b
elif b==0:
return a
if a>b:
return gcd(a%b,b)
else:
return gcd(a,b%a)
def bfs(adj,v): # a schema of bfs
visited=[False]*(v+1)
q=deque()
while q:
pass
def setBit(n):
count=0
while n!=0:
n=n&(n-1)
count+=1
return count
mx=10**7
spf=[mx]*(mx+1)
def readTree(n,e): # to read tree
adj=[set() for i in range(n+1)]
for i in range(e):
u1,u2=IP()
adj[u1].add(u2)
return adj
def sieve():
li=[True]*(10**3+5)
li[0],li[1]=False,False
for i in range(2,len(li),1):
if li[i]==True:
for j in range(i*i,len(li),i):
li[j]=False
prime,cur=[0]*200,0
for i in range(10**3+5):
if li[i]==True:
prime[cur]=i
cur+=1
return prime
def SPF():
mx=(10**6+1)
spf[1]=1
for i in range(2,mx):
if spf[i]==1e9:
spf[i]=i
for j in range(i*i,mx,i):
if i<spf[j]:
spf[j]=i
return
def prime(n,d):
prm=set()
while n!=1:
prm.add(spf[n])
n=n//spf[n]
for ele in prm:
d[ele]=d.get(ele,0)+1
return
#####################################################################################
mod=998244353
inf = 10000000000000000
def solve():
n=II()
arr=L()
arr.sort(reverse=True)
for i in range(0,2*n,2):
if arr[i]!=arr[i+1]:
print("NO")
return
d=[]
for i in range(0,2*n,2):
d.append(arr[i])
num=2*n
sub=0
mp={}
for i in range(n):
val=d[i]-sub
if val<=0 or (val%num):
print("NO")
return
found=val//num
if mp.get(found,0)==1:
print("NO")
return
mp[found]=1
num-=2
sub+=2*found
print("YES")
return
t=II()
for i in range(t):
solve()
#######
#
#
####### # # # #### # # #
# # # # # # # # # # #
# #### # # #### #### # #
###### # # #### # # # # #
# ``````¶0````1¶1_```````````````````````````````````````
# ```````¶¶¶0_`_¶¶¶0011100¶¶¶¶¶¶¶001_````````````````````
# ````````¶¶¶¶¶00¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶0_````````````````
# `````1_``¶¶00¶0000000000000000000000¶¶¶¶0_`````````````
# `````_¶¶_`0¶000000000000000000000000000¶¶¶¶¶1``````````
# ```````¶¶¶00¶00000000000000000000000000000¶¶¶_`````````
# ````````_¶¶00000000000000000000¶¶00000000000¶¶`````````
# `````_0011¶¶¶¶¶000000000000¶¶00¶¶0¶¶00000000¶¶_````````
# ```````_¶¶¶¶¶¶¶00000000000¶¶¶¶0¶¶¶¶¶00000000¶¶1````````
# ``````````1¶¶¶¶¶000000¶¶0¶¶¶¶¶¶¶¶¶¶¶¶0000000¶¶¶````````
# ```````````¶¶¶0¶000¶00¶0¶¶`_____`__1¶0¶¶00¶00¶¶````````
# ```````````¶¶¶¶¶00¶00¶10¶0``_1111_`_¶¶0000¶0¶¶¶````````
# ``````````1¶¶¶¶¶00¶0¶¶_¶¶1`_¶_1_0_`1¶¶_0¶0¶¶0¶¶````````
# ````````1¶¶¶¶¶¶¶0¶¶0¶0_0¶``100111``_¶1_0¶0¶¶_1¶````````
# ```````1¶¶¶¶00¶¶¶¶¶¶¶010¶``1111111_0¶11¶¶¶¶¶_10````````
# ```````0¶¶¶¶__10¶¶¶¶¶100¶¶¶0111110¶¶¶1__¶¶¶¶`__````````
# ```````¶¶¶¶0`__0¶¶0¶¶_¶¶¶_11````_0¶¶0`_1¶¶¶¶```````````
# ```````¶¶¶00`__0¶¶_00`_0_``````````1_``¶0¶¶_```````````
# ``````1¶1``¶¶``1¶¶_11``````````````````¶`¶¶````````````
# ``````1_``¶0_¶1`0¶_`_``````````_``````1_`¶1````````````
# ``````````_`1¶00¶¶_````_````__`1`````__`_¶`````````````
# ````````````¶1`0¶¶_`````````_11_`````_``_``````````````
# `````````¶¶¶¶000¶¶_1```````_____```_1``````````````````
# `````````¶¶¶¶¶¶¶¶¶¶¶¶0_``````_````_1111__``````````````
# `````````¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶01_`````_11____1111_```````````
# `````````¶¶0¶0¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶1101_______11¶_```````````
# ``````_¶¶¶0000000¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶0¶0¶¶¶1````````````
# `````0¶¶0000000¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶1`````````````
# ````0¶0000000¶¶0_````_011_10¶110¶01_1¶¶¶0````_100¶001_`
# ```1¶0000000¶0_``__`````````_`````````0¶_``_00¶¶010¶001
# ```¶¶00000¶¶1``_01``_11____``1_``_`````¶¶0100¶1```_00¶1
# ``1¶¶00000¶_``_¶_`_101_``_`__````__````_0000001100¶¶¶0`
# ``¶¶¶0000¶1_`_¶``__0_``````_1````_1_````1¶¶¶0¶¶¶¶¶¶0```
# `_¶¶¶¶00¶0___01_10¶_``__````1`````11___`1¶¶¶01_````````
# `1¶¶¶¶¶0¶0`__01¶¶¶0````1_```11``___1_1__11¶000`````````
# `1¶¶¶¶¶¶¶1_1_01__`01```_1```_1__1_11___1_``00¶1````````
# ``¶¶¶¶¶¶¶0`__10__000````1____1____1___1_```10¶0_```````
# ``0¶¶¶¶¶¶¶1___0000000```11___1__`_0111_```000¶01```````
# ```¶¶¶00000¶¶¶¶¶¶¶¶¶01___1___00_1¶¶¶`_``1¶¶10¶¶0```````
# ```1010000¶000¶¶0100_11__1011000¶¶0¶1_10¶¶¶_0¶¶00``````
# 10¶000000000¶0________0¶000000¶¶0000¶¶¶¶000_0¶0¶00`````
# ¶¶¶¶¶¶0000¶¶¶¶_`___`_0¶¶¶¶¶¶¶00000000000000_0¶00¶01````
# ¶¶¶¶¶0¶¶¶¶¶¶¶¶¶_``_1¶¶¶00000000000000000000_0¶000¶01```
# 1__```1¶¶¶¶¶¶¶¶¶00¶¶¶¶00000000000000000000¶_0¶0000¶0_``
# ```````¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶00000000000000000000010¶00000¶¶_`
# ```````0¶¶¶¶¶¶¶¶¶¶¶¶¶¶00000000000000000000¶10¶¶0¶¶¶¶¶0`
# ````````¶¶¶¶¶¶¶¶¶¶0¶¶¶00000000000000000000010¶¶¶0011```
# ````````1¶¶¶¶¶¶¶¶¶¶0¶¶¶0000000000000000000¶100__1_`````
# `````````¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶000000000000000000¶11``_1``````
# `````````1¶¶¶¶¶¶¶¶¶¶¶0¶¶¶00000000000000000¶11___1_`````
# ``````````¶¶¶¶¶¶0¶0¶¶¶¶¶¶¶0000000000000000¶11__``1_````
# ``````````¶¶¶¶¶¶¶0¶¶¶0¶¶¶¶¶000000000000000¶1__````__```
# ``````````¶¶¶¶¶¶¶¶0¶¶¶¶¶¶¶¶¶0000000000000000__`````11``
# `````````_¶¶¶¶¶¶¶¶¶000¶¶¶¶¶¶¶¶000000000000011_``_1¶¶¶0`
# `````````_¶¶¶¶¶¶0¶¶000000¶¶¶¶¶¶¶000000000000100¶¶¶¶0_`_
# `````````1¶¶¶¶¶0¶¶¶000000000¶¶¶¶¶¶000000000¶00¶¶01`````
# `````````¶¶¶¶¶0¶0¶¶¶0000000000000¶0¶00000000011_``````_
# ````````1¶¶0¶¶¶0¶¶¶¶¶¶¶000000000000000000000¶11___11111
# ````````¶¶¶¶0¶¶¶¶¶00¶¶¶¶¶¶000000000000000000¶011111111_
# ```````_¶¶¶¶¶¶¶¶¶0000000¶0¶00000000000000000¶01_1111111
# ```````0¶¶¶¶¶¶¶¶¶000000000000000000000000000¶01___`````
# ```````¶¶¶¶¶¶0¶¶¶000000000000000000000000000¶01___1````
# ``````_¶¶¶¶¶¶¶¶¶00000000000000000000000000000011_111```
# ``````0¶¶0¶¶¶0¶¶0000000000000000000000000000¶01`1_11_``
# ``````¶¶¶¶¶¶0¶¶¶0000000000000000000000000000001`_0_11_`
# ``````¶¶¶¶¶¶¶¶¶00000000000000000000000000000¶01``_0_11`
# ``````¶¶¶¶0¶¶¶¶00000000000000000000000000000001```_1_11
``` | output | 1 | 9,302 | 12 | 18,605 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d. | instruction | 0 | 9,303 | 12 | 18,606 |
Tags: implementation, math, sortings
Correct Solution:
```
gans = []
for _ in range(int(input())):
n = int(input())
s = list(map(int, input().split()))
s.sort()
s1 = s[:]
ok = False
d = []
for i in range(1, 2 * n, 2):
if s[i] != s[i - 1] or s[i] % 2 != 0:
gans.append('NO')
ok = True
break
d.append(s[i] // 2)
if ok:
continue
#print(*d)
sm = d[0]
for i in range(n):
d[i] -= sm
#print(*d)
a = [0] * n
a[-1] = (d[-1] + sm) // n
if (d[-1] + sm) % n != 0 or a[-1] <= 0:
gans.append("NO")
continue
sm -= a[-1]
for i in range(n - 2, -1, -1):
a[i] = (d[i] + sm) // (i + 1)
sm -= a[i]
if (d[i] + sm + a[i]) % (i + 1) != 0 or a[i] <= 0 or a[i] == a[i + 1]:
gans.append("NO")
#print(*a)
break
else:
#print(*a)
gans.append("YES")
print('\n'.join(gans))
``` | output | 1 | 9,303 | 12 | 18,607 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d. | instruction | 0 | 9,304 | 12 | 18,608 |
Tags: implementation, math, sortings
Correct Solution:
```
ll=lambda:map(int,input().split())
t=lambda:int(input())
ss=lambda:input()
#from math import log10 ,log2,ceil,factorial as f,gcd
#from itertools import combinations_with_replacement as cs
#from functools import reduce
#from bisect import bisect_right as br
from collections import Counter
#from math import inf,ceil
'''
'''
#for _ in range(t()):
for _ in range(t()):
n=t()
d=list(ll())
x=Counter(d)
p=0
for i in x.values():
if i!=2:
print("NO")
p=1
break
if p:
continue
s=0
c=len(x.keys())
for i in sorted(x.keys())[::-1]:
i=i-s
if i>0 and i%(2*(c))==0:
i=i//(2*(c))
s+=2*i
else:
print("NO")
break
c-=1
else:
print("YES")
``` | output | 1 | 9,304 | 12 | 18,609 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d.
Submitted Solution:
```
from collections import Counter
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
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 = self.os.read(self._fd, max(self.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 = self.os.read(self._fd, max(self.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:
self.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")
def solve():
n = int(input())
d = [int(x) for x in input().split(' ')]
C = Counter(d)
x = []
for k, v in C.items():
if v != 2 or k <= 0 or (k % 2) != 0:
return False
else:
x.append(k)
else:
x.sort(reverse=True)
if x[0] % (2 * n) != 0:
return False
else:
a = [x[0] // (2 * n)]
for i in range(1, n):
if (x[i - 1] - x[i]) % (2 * (n - i)) != 0:
return False
else:
a.append(a[-1] - (x[i - 1] - x[i]) // (2 * (n - i)))
return a[-1] > 0
t = int(input())
for case in range(t):
if solve():
print("YES")
else:
print("NO")
``` | instruction | 0 | 9,305 | 12 | 18,610 |
Yes | output | 1 | 9,305 | 12 | 18,611 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Long time ago there was a symmetric array a_1,a_2,…,a_{2n} consisting of 2n distinct integers. Array a_1,a_2,…,a_{2n} is called symmetric if for each integer 1 ≤ i ≤ 2n, there exists an integer 1 ≤ j ≤ 2n such that a_i = -a_j.
For each integer 1 ≤ i ≤ 2n, Nezzar wrote down an integer d_i equal to the sum of absolute differences from a_i to all integers in a, i. e. d_i = ∑_{j = 1}^{2n} {|a_i - a_j|}.
Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.
Input
The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases.
The first line of each test case contains a single integer n (1 ≤ n ≤ 10^5).
The second line of each test case contains 2n integers d_1, d_2, …, d_{2n} (0 ≤ d_i ≤ 10^{12}).
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case, print "YES" in a single line if there exists a possible array a. Otherwise, print "NO".
You can print letters in any case (upper or lower).
Example
Input
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
Output
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,-3,-1,3] is one possible symmetric array that generates the array d=[8,12,8,12].
In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d.
Submitted Solution:
```
def main():
T = int(input())
for ___ in range(T):
n = input().strip()
n = int(n)
l1 = [int(_) for _ in input().strip().split()]
l1 = list(reversed(sorted(l1)))
succ = True
cur_sum = 0
cur = 0
num_remain = 2 * n
r = [int(1e12+7)]
for i in range(0, 2 * n, 2):
if l1[i] != l1[i + 1]:
succ = False
break
cur = l1[i] - 2 * cur_sum
if cur <= 0 or cur % num_remain != 0:
succ = False
# print(l1)
# print(cur, num_remain)
break
cur = cur // num_remain
if cur >= r[-1]:
succ = False
break
cur_sum += cur
num_remain -= 2
r.append(cur)
# print(r)
if succ:
print("YES")
else:
print("NO")
# region fastio
import os
import sys
from io import BytesIO, IOBase
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
if __name__ == "__main__":
main()
``` | instruction | 0 | 9,306 | 12 | 18,612 |
Yes | output | 1 | 9,306 | 12 | 18,613 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.