message stringlengths 2 433k | message_type stringclasses 2
values | message_id int64 0 1 | conversation_id int64 113 108k | cluster float64 12 12 | __index_level_0__ int64 226 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.
Two ways are considered different if sets of indexes of elements chosen by these ways are different.
Since the answer can be very large, you should find the answer modulo 109 + 7.
Input
First line contains one integer n (1 β€ n β€ 105) β the number of elements in the array.
Second line contains n integers ai (1 β€ ai β€ 70) β the elements of the array.
Output
Print one integer β the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109 + 7.
Examples
Input
4
1 1 1 1
Output
15
Input
4
2 2 2 2
Output
7
Input
5
1 2 4 5 8
Output
7
Note
In first sample product of elements chosen by any way is 1 and 1 = 12. So the answer is 24 - 1 = 15.
In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7. | instruction | 0 | 28,776 | 12 | 57,552 |
Tags: bitmasks, combinatorics, dp, math
Correct Solution:
```
n = int(input())
s = []
p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67]
for k in set(map(int, input().split())):
d = 0
for q in p:
while k % q == 0:
k //= q
d ^= 1 << q
for j in s: d = min(d, d ^ j)
if d: s.append(d)
print(pow(2, n - len(s), 1000000007) - 1)
# Made By Mostafa_Khaled
``` | output | 1 | 28,776 | 12 | 57,553 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.
Two ways are considered different if sets of indexes of elements chosen by these ways are different.
Since the answer can be very large, you should find the answer modulo 109 + 7.
Input
First line contains one integer n (1 β€ n β€ 105) β the number of elements in the array.
Second line contains n integers ai (1 β€ ai β€ 70) β the elements of the array.
Output
Print one integer β the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109 + 7.
Examples
Input
4
1 1 1 1
Output
15
Input
4
2 2 2 2
Output
7
Input
5
1 2 4 5 8
Output
7
Note
In first sample product of elements chosen by any way is 1 and 1 = 12. So the answer is 24 - 1 = 15.
In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7. | instruction | 0 | 28,777 | 12 | 57,554 |
Tags: bitmasks, combinatorics, dp, math
Correct Solution:
```
from collections import *
l = int(input())
c = Counter(map(int, input().split()))
t = defaultdict(int)
p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67]
for k, s in c.items():
d = 0
for i, q in enumerate(p):
while k % q == 0:
k //= q
d ^= 1 << i
t[d] += s
u = defaultdict(int)
u[0] = 1
for x in t:
if x: l -= 1
if 0 < x < 2048:
v = u.copy()
for y in u: v[x ^ y] += u[y]
u = v
e = 1000000007
print((u[0] * pow(2, l, e) - 1) % e)
``` | output | 1 | 28,777 | 12 | 57,555 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.
Two ways are considered different if sets of indexes of elements chosen by these ways are different.
Since the answer can be very large, you should find the answer modulo 109 + 7.
Input
First line contains one integer n (1 β€ n β€ 105) β the number of elements in the array.
Second line contains n integers ai (1 β€ ai β€ 70) β the elements of the array.
Output
Print one integer β the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109 + 7.
Examples
Input
4
1 1 1 1
Output
15
Input
4
2 2 2 2
Output
7
Input
5
1 2 4 5 8
Output
7
Note
In first sample product of elements chosen by any way is 1 and 1 = 12. So the answer is 24 - 1 = 15.
In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7. | instruction | 0 | 28,778 | 12 | 57,556 |
Tags: bitmasks, combinatorics, dp, math
Correct Solution:
```
from collections import defaultdict
def getmask(x):
ans = 0
for i in range(2, x + 1):
while x % i == 0:
x //= i
ans ^= 1 << i
return ans
def main():
maxn = 71
n = int(input())
a = [int(i) for i in input().split()]
cnt = [0] * maxn
for i in a:
cnt[i] += 1
masks = defaultdict(int)
for i in range(1, maxn):
masks[getmask(i)] += cnt[i]
while masks[0] != sum(masks.values()):
fixed = max(i for i in masks if masks[i])
masks[0] -= 1
for i in list(masks.keys()):
if i ^ fixed < i:
masks[i ^ fixed] += masks[i]
masks[i] = 0
print(pow(2, masks[0], 10**9+7) - 1)
main()
``` | output | 1 | 28,778 | 12 | 57,557 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.
Two ways are considered different if sets of indexes of elements chosen by these ways are different.
Since the answer can be very large, you should find the answer modulo 109 + 7.
Input
First line contains one integer n (1 β€ n β€ 105) β the number of elements in the array.
Second line contains n integers ai (1 β€ ai β€ 70) β the elements of the array.
Output
Print one integer β the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109 + 7.
Examples
Input
4
1 1 1 1
Output
15
Input
4
2 2 2 2
Output
7
Input
5
1 2 4 5 8
Output
7
Note
In first sample product of elements chosen by any way is 1 and 1 = 12. So the answer is 24 - 1 = 15.
In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7. | instruction | 0 | 28,779 | 12 | 57,558 |
Tags: bitmasks, combinatorics, dp, math
Correct Solution:
```
from sys import stdin
def find_prime(max_value):
assert max_value >= 2
result = [2]
for i in range(3, max_value):
state = 1
for item in result:
if item * item > i:
break
elif i % item == 0:
state = -1
break
if state == 1:
result.append(i)
return result
module = int(10e8) + 7
n = int(stdin.readline())
A = list(map(int, stdin.readline().split()))
number = [0 for i in range(70)]
for item in A:
number[item - 1] += 1
prime_list = find_prime(70)
prime_map = {}
for i in range(2, 70 + 1):
result = 0
for item in prime_list:
cur = i
num = 0
while cur % item == 0:
num += 1
cur = int(cur / item)
result = result * 2 + num % 2
prime_map[i] = result
number_dic = {0: 1}
sum_time = 0
for i in range(2, 70 + 1):
if number[i - 1] >= 1:
mask = prime_map[i]
state = 1
new_number_dic = number_dic.copy()
for key, value in new_number_dic.items():
new_key = key ^ mask
if new_key in number_dic:
number_dic[new_key] += value
else:
number_dic[new_key] = value
number_dic[new_key] %= module
sum_time += (number[i - 1] - 1)
result = number_dic[0]
for j in range(number[0] + sum_time):
result *= 2
result %= module
print(result-1)
``` | output | 1 | 28,779 | 12 | 57,559 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.
Two ways are considered different if sets of indexes of elements chosen by these ways are different.
Since the answer can be very large, you should find the answer modulo 109 + 7.
Input
First line contains one integer n (1 β€ n β€ 105) β the number of elements in the array.
Second line contains n integers ai (1 β€ ai β€ 70) β the elements of the array.
Output
Print one integer β the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109 + 7.
Examples
Input
4
1 1 1 1
Output
15
Input
4
2 2 2 2
Output
7
Input
5
1 2 4 5 8
Output
7
Note
In first sample product of elements chosen by any way is 1 and 1 = 12. So the answer is 24 - 1 = 15.
In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7. | instruction | 0 | 28,780 | 12 | 57,560 |
Tags: bitmasks, combinatorics, dp, math
Correct Solution:
```
n = int(input())
*a, = map(int, input().split())
mod = 1000000007
d = []
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67]
b = [0, 4, 8, 0, 32, 12, 128, 4, 0, 36,2048, 8, 8192, 132, 40, 0, 131072, 4, 524288, 32, 136, 2052, 8388608, 12, 0, 8196, 8, 128, 536870912, 44, 2147483648, 4, 2056, 131076, 160, 0, 137438953472, 524292, 8200, 36, 2199023255552, 140, 8796093022208, 2048,32, 8388612, 140737488355328, 8, 0,4, 131080, 8192, 9007199254740992, 12, 2080, 132, 524296, 536870916, 576460752303423488, 40, 2305843009213693952, 2147483652, 128, 0, 8224, 2060, 147573952589676412928, 131072, 8388616, 164]
for i in set(a):
c = b[i - 1]
for j in d:
c = min(c, c ^ j)
if c > 0:
d.append(c)
print(pow(2, n - len(d), mod) - 1)
``` | output | 1 | 28,780 | 12 | 57,561 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.
Two ways are considered different if sets of indexes of elements chosen by these ways are different.
Since the answer can be very large, you should find the answer modulo 109 + 7.
Input
First line contains one integer n (1 β€ n β€ 105) β the number of elements in the array.
Second line contains n integers ai (1 β€ ai β€ 70) β the elements of the array.
Output
Print one integer β the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109 + 7.
Examples
Input
4
1 1 1 1
Output
15
Input
4
2 2 2 2
Output
7
Input
5
1 2 4 5 8
Output
7
Note
In first sample product of elements chosen by any way is 1 and 1 = 12. So the answer is 24 - 1 = 15.
In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7. | instruction | 0 | 28,781 | 12 | 57,562 |
Tags: bitmasks, combinatorics, dp, math
Correct Solution:
```
n = int(input())
s = []
p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67]
for k in set(map(int, input().split())):
d = 0
for q in p:
while k % q == 0:
k //= q
d ^= 1 << q
for j in s: d = min(d, d ^ j)
if d: s.append(d)
print(pow(2, n - len(s), 1000000007) - 1)
``` | output | 1 | 28,781 | 12 | 57,563 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.
Two ways are considered different if sets of indexes of elements chosen by these ways are different.
Since the answer can be very large, you should find the answer modulo 109 + 7.
Input
First line contains one integer n (1 β€ n β€ 105) β the number of elements in the array.
Second line contains n integers ai (1 β€ ai β€ 70) β the elements of the array.
Output
Print one integer β the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109 + 7.
Examples
Input
4
1 1 1 1
Output
15
Input
4
2 2 2 2
Output
7
Input
5
1 2 4 5 8
Output
7
Note
In first sample product of elements chosen by any way is 1 and 1 = 12. So the answer is 24 - 1 = 15.
In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7. | instruction | 0 | 28,782 | 12 | 57,564 |
Tags: bitmasks, combinatorics, dp, math
Correct Solution:
```
n = int(input())
s = []
p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67]
for i in set(map(int, input().split())):
b = 0
for j in p:
while i % j == 0:
i //= j
b ^= 1 << j
for j in s:b=min(b,b^j)
if b>0:s.append(b)
print(pow(2, n - len(s), 10 ** 9 + 7) - 1)
``` | output | 1 | 28,782 | 12 | 57,565 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.
Two ways are considered different if sets of indexes of elements chosen by these ways are different.
Since the answer can be very large, you should find the answer modulo 109 + 7.
Input
First line contains one integer n (1 β€ n β€ 105) β the number of elements in the array.
Second line contains n integers ai (1 β€ ai β€ 70) β the elements of the array.
Output
Print one integer β the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109 + 7.
Examples
Input
4
1 1 1 1
Output
15
Input
4
2 2 2 2
Output
7
Input
5
1 2 4 5 8
Output
7
Note
In first sample product of elements chosen by any way is 1 and 1 = 12. So the answer is 24 - 1 = 15.
In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7. | instruction | 0 | 28,783 | 12 | 57,566 |
Tags: bitmasks, combinatorics, dp, math
Correct Solution:
```
def getmask(x):
ans = 0
for i in range(2, x + 1):
while x % (i * i) == 0:
x //= i * i
if x % i == 0:
ans ^= 1 << i
x //= i
return ans
def main():
maxn = 71
n = int(input())
a = [int(i) for i in input().split()]
cnt = [0] * maxn
for i in a:
cnt[i] += 1
masks = {}
for i in range(1, maxn):
if cnt[i]:
masks[getmask(i)] = masks.get(getmask(i), 0) + cnt[i]
while len(masks) > 1 or 0 not in masks:
if not masks:
print(0)
return
fixed = max(masks.keys())
for i in list(masks.keys()):
if i ^ fixed < i:
masks[i ^ fixed] = masks.get(i ^ fixed, 0) + masks[i]
masks[i] = 0
masks[0] = masks.get(0, 0) + masks[fixed] - 1
masks[fixed] = 0
masks = {i: j for i, j in masks.items() if j > 0}
print(pow(2, masks[0], 10**9+7) - 1)
main()
``` | output | 1 | 28,783 | 12 | 57,567 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.
Two ways are considered different if sets of indexes of elements chosen by these ways are different.
Since the answer can be very large, you should find the answer modulo 109 + 7.
Input
First line contains one integer n (1 β€ n β€ 105) β the number of elements in the array.
Second line contains n integers ai (1 β€ ai β€ 70) β the elements of the array.
Output
Print one integer β the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109 + 7.
Examples
Input
4
1 1 1 1
Output
15
Input
4
2 2 2 2
Output
7
Input
5
1 2 4 5 8
Output
7
Note
In first sample product of elements chosen by any way is 1 and 1 = 12. So the answer is 24 - 1 = 15.
In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7.
Submitted Solution:
```
n = int(input())
a = set(map(int, input().split()))
s = []
p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67]
for i in a:
b = 0
for j in p:
while i % j == 0:
i //= j
b ^= 1 << j
for j in s:
b = min(b, b ^ j)
if b > 0:
s.append(b)
print(pow(2, n - len(s), 10 ** 9 + 7) - 1)
``` | instruction | 0 | 28,784 | 12 | 57,568 |
Yes | output | 1 | 28,784 | 12 | 57,569 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.
Two ways are considered different if sets of indexes of elements chosen by these ways are different.
Since the answer can be very large, you should find the answer modulo 109 + 7.
Input
First line contains one integer n (1 β€ n β€ 105) β the number of elements in the array.
Second line contains n integers ai (1 β€ ai β€ 70) β the elements of the array.
Output
Print one integer β the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109 + 7.
Examples
Input
4
1 1 1 1
Output
15
Input
4
2 2 2 2
Output
7
Input
5
1 2 4 5 8
Output
7
Note
In first sample product of elements chosen by any way is 1 and 1 = 12. So the answer is 24 - 1 = 15.
In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7.
Submitted Solution:
```
from collections import defaultdict
#from collections import defaultdict
def getmask(x):
ans = 0
for i in range(2, x + 1):
while x % i == 0:
x //= i
ans ^= 1 << i
return ans
def main():
maxn = 71
#maxn = 71
n = int(input())
#n = int(input())
a = [int(i) for i in input().split()]
cnt = [0] * maxn
for i in a:
cnt[i] += 1
masks = defaultdict(int)
for i in range(1, maxn):
masks[getmask(i)] += cnt[i]
while masks[0] != sum(masks.values()):
fixed = max(i for i in masks if masks[i])
masks[0] -= 1
for i in list(masks.keys()):
if i ^ fixed < i:
masks[i ^ fixed] += masks[i]
masks[i] = 0
print(pow(2, masks[0], 10**9+7) - 1)
main()
``` | instruction | 0 | 28,785 | 12 | 57,570 |
Yes | output | 1 | 28,785 | 12 | 57,571 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.
Two ways are considered different if sets of indexes of elements chosen by these ways are different.
Since the answer can be very large, you should find the answer modulo 109 + 7.
Input
First line contains one integer n (1 β€ n β€ 105) β the number of elements in the array.
Second line contains n integers ai (1 β€ ai β€ 70) β the elements of the array.
Output
Print one integer β the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109 + 7.
Examples
Input
4
1 1 1 1
Output
15
Input
4
2 2 2 2
Output
7
Input
5
1 2 4 5 8
Output
7
Note
In first sample product of elements chosen by any way is 1 and 1 = 12. So the answer is 24 - 1 = 15.
In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7.
Submitted Solution:
```
MOD = int(1e9+7)
def is_prime(x):
for i in range(2, int(x**.5)+1):
if x % i == 0:
return False
return True
p = [i for i in range(2, 100) if is_prime(i)]
n = int(input())
arr = list(map(int, input().split()))
s = []
for i in set(arr):
b = 0
for j in p:
while i % j == 0:
i //= j
b ^= 1 << j
for j in s:
b = min(b, b^j)
if b > 0:
s.append(b)
print(pow(2, n-len(s), MOD) - 1)
``` | instruction | 0 | 28,786 | 12 | 57,572 |
Yes | output | 1 | 28,786 | 12 | 57,573 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.
Two ways are considered different if sets of indexes of elements chosen by these ways are different.
Since the answer can be very large, you should find the answer modulo 109 + 7.
Input
First line contains one integer n (1 β€ n β€ 105) β the number of elements in the array.
Second line contains n integers ai (1 β€ ai β€ 70) β the elements of the array.
Output
Print one integer β the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109 + 7.
Examples
Input
4
1 1 1 1
Output
15
Input
4
2 2 2 2
Output
7
Input
5
1 2 4 5 8
Output
7
Note
In first sample product of elements chosen by any way is 1 and 1 = 12. So the answer is 24 - 1 = 15.
In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7.
Submitted Solution:
```
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO,IOBase
from array import array
mod = 10**9+7
fac = array('i',[1])
for ii in range(1,10**5+1):
fac.append((fac[-1]*ii)%mod)
fac_in = array('i',[pow(fac[-1],mod-2,mod)])
for ii in range(10**5,0,-1):
fac_in.append((fac_in[-1]*ii)%mod)
fac_in.reverse()
def comb(a,b):
if a < b:
return 0
return (fac[a]*fac_in[a-b]*fac_in[b])%mod
def main():
n = int(input())
a = array('i',(map(int,input().split())))
primes = [2,3,5,7,11,13,17,19,23,29,31]
primes1 = {37:0,41:0,43:0,47:0,53:0,59:0,61:0,67:0}
new = array('i',[])
for ind,i in enumerate(a):
if primes1.get(i) != None:
primes1[i] += 1
continue
mask,num = 1,0
for j in primes:
while not i%j:
num ^= mask
i //= j
mask <<= 1
new.append(num)
y = 1<<len(primes)
dp = array('i',[0]*y)
for i in new:
dp1 = dp[:]
for j in range(y):
z = i^j
dp1[z] += dp[j]
if dp1[z] >= mod:
dp1[z] -= mod
dp1[i] += 1
if dp1[i] >= mod:
dp1[i] -= mod
dp = dp1
ans = dp[0]+1
for i in primes1:
aa = 0
for j in range(0,primes1[i]+1,2):
aa = aa+comb(primes1[i],j)
if aa >= mod:
aa -= mod
ans = (ans*aa)%mod
print((ans-1)%mod)
# Fast IO Region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self,file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd,max(os.fstat(self._fd).st_size,BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0,2),self.buffer.write(b),self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd,max(os.fstat(self._fd).st_size,BUFSIZE))
self.newlines = b.count(b"\n")+(not b)
ptr = self.buffer.tell()
self.buffer.seek(0,2),self.buffer.write(b),self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd,self.buffer.getvalue())
self.buffer.truncate(0),self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self,file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s:self.buffer.write(s.encode("ascii"))
self.read = lambda:self.buffer.read().decode("ascii")
self.readline = lambda:self.buffer.readline().decode("ascii")
sys.stdin,sys.stdout = IOWrapper(sys.stdin),IOWrapper(sys.stdout)
input = lambda:sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main()
``` | instruction | 0 | 28,787 | 12 | 57,574 |
Yes | output | 1 | 28,787 | 12 | 57,575 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.
Two ways are considered different if sets of indexes of elements chosen by these ways are different.
Since the answer can be very large, you should find the answer modulo 109 + 7.
Input
First line contains one integer n (1 β€ n β€ 105) β the number of elements in the array.
Second line contains n integers ai (1 β€ ai β€ 70) β the elements of the array.
Output
Print one integer β the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109 + 7.
Examples
Input
4
1 1 1 1
Output
15
Input
4
2 2 2 2
Output
7
Input
5
1 2 4 5 8
Output
7
Note
In first sample product of elements chosen by any way is 1 and 1 = 12. So the answer is 24 - 1 = 15.
In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7.
Submitted Solution:
```
n = int(input())
a = [int(x) for x in input().strip().split()]
m = 70
t = [0 for i in range(m*m+1)]
td = [0 for i in range(m*m+1)]
t[1] = 1
for ai in a:
for j in range(1,m*m+1):
if ai * j > m * m:
break
td[ai*j] = t[j]
for j in range(1,m*m+1):
t[j] += td[j]
td[j] = 0
t[1] -= 1
ans = 0
for i in range(m+1):
ans += t[i*i]
print(ans)
``` | instruction | 0 | 28,788 | 12 | 57,576 |
No | output | 1 | 28,788 | 12 | 57,577 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.
Two ways are considered different if sets of indexes of elements chosen by these ways are different.
Since the answer can be very large, you should find the answer modulo 109 + 7.
Input
First line contains one integer n (1 β€ n β€ 105) β the number of elements in the array.
Second line contains n integers ai (1 β€ ai β€ 70) β the elements of the array.
Output
Print one integer β the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109 + 7.
Examples
Input
4
1 1 1 1
Output
15
Input
4
2 2 2 2
Output
7
Input
5
1 2 4 5 8
Output
7
Note
In first sample product of elements chosen by any way is 1 and 1 = 12. So the answer is 24 - 1 = 15.
In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7.
Submitted Solution:
```
n = int(input())
arr = [0] * 71
a = map(int, input().split())
for i in a:
arr[i] += 1
dp = {}
dp[0] = 1
for i in range(71):
if arr[i] == 0:
continue
cur = 0
primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67]
for j in range(len(primes)):
if i > 1 and i % primes[j] == 0:
cur += 2**j
ndp = {}
for x in dp:
if x ^ cur not in ndp:
ndp[x ^ cur] = 0
ndp[x ^ cur] += dp[x] * pow(2, arr[i] - 1, 10**9 + 7)
ndp[x ^ cur] %= (10**9 + 7)
if x not in ndp:
ndp[x] = 0
ndp[x] += dp[x] * pow(2, arr[i] - 1, 10**9 + 7)
ndp[x] %= (10**9 + 7)
dp = ndp
print((dp[0] + 10**9 + 6) % (10**9 + 7))
``` | instruction | 0 | 28,789 | 12 | 57,578 |
No | output | 1 | 28,789 | 12 | 57,579 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.
Two ways are considered different if sets of indexes of elements chosen by these ways are different.
Since the answer can be very large, you should find the answer modulo 109 + 7.
Input
First line contains one integer n (1 β€ n β€ 105) β the number of elements in the array.
Second line contains n integers ai (1 β€ ai β€ 70) β the elements of the array.
Output
Print one integer β the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109 + 7.
Examples
Input
4
1 1 1 1
Output
15
Input
4
2 2 2 2
Output
7
Input
5
1 2 4 5 8
Output
7
Note
In first sample product of elements chosen by any way is 1 and 1 = 12. So the answer is 24 - 1 = 15.
In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7.
Submitted Solution:
```
n = int(input())
s = set()
p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67]
for i in set(map(int, input().split())):
b = 0
for j in p:
while i % j == 0:
i //= j
b ^= 1 << j
for j in s:b=min(b,b^j)
s.add(b)
print(pow(2, n - len(s-{0}), 10 ** 9 + 7) - 1)
``` | instruction | 0 | 28,790 | 12 | 57,580 |
No | output | 1 | 28,790 | 12 | 57,581 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Petya was late for the lesson too. The teacher gave him an additional task. For some array a Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer.
Two ways are considered different if sets of indexes of elements chosen by these ways are different.
Since the answer can be very large, you should find the answer modulo 109 + 7.
Input
First line contains one integer n (1 β€ n β€ 105) β the number of elements in the array.
Second line contains n integers ai (1 β€ ai β€ 70) β the elements of the array.
Output
Print one integer β the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109 + 7.
Examples
Input
4
1 1 1 1
Output
15
Input
4
2 2 2 2
Output
7
Input
5
1 2 4 5 8
Output
7
Note
In first sample product of elements chosen by any way is 1 and 1 = 12. So the answer is 24 - 1 = 15.
In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6 + 1 = 7.
Submitted Solution:
```
# ---------------------------iye ha aam zindegi---------------------------------------------
import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
#import threading
from collections import defaultdict
#threading.stack_size(10**8)
mod = 10 ** 9 + 7
mod1 = 998244353
# ------------------------------warmup----------------------------
import os
import sys
from io import BytesIO, IOBase
#sys.setrecursionlimit(300000)
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")
# -------------------game starts now----------------------------------------------------import math
class TreeNode:
def __init__(self, k, v):
self.key = k
self.value = v
self.left = None
self.right = None
self.parent = None
self.height = 1
self.num_left = 1
self.num_total = 1
class AvlTree:
def __init__(self):
self._tree = None
def add(self, k, v):
if not self._tree:
self._tree = TreeNode(k, v)
return
node = self._add(k, v)
if node:
self._rebalance(node)
def _add(self, k, v):
node = self._tree
while node:
if k < node.key:
if node.left:
node = node.left
else:
node.left = TreeNode(k, v)
node.left.parent = node
return node.left
elif node.key < k:
if node.right:
node = node.right
else:
node.right = TreeNode(k, v)
node.right.parent = node
return node.right
else:
node.value = v
return
@staticmethod
def get_height(x):
return x.height if x else 0
@staticmethod
def get_num_total(x):
return x.num_total if x else 0
def _rebalance(self, node):
n = node
while n:
lh = self.get_height(n.left)
rh = self.get_height(n.right)
n.height = max(lh, rh) + 1
balance_factor = lh - rh
n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
n.num_left = 1 + self.get_num_total(n.left)
if balance_factor > 1:
if self.get_height(n.left.left) < self.get_height(n.left.right):
self._rotate_left(n.left)
self._rotate_right(n)
elif balance_factor < -1:
if self.get_height(n.right.right) < self.get_height(n.right.left):
self._rotate_right(n.right)
self._rotate_left(n)
else:
n = n.parent
def _remove_one(self, node):
"""
Side effect!!! Changes node. Node should have exactly one child
"""
replacement = node.left or node.right
if node.parent:
if AvlTree._is_left(node):
node.parent.left = replacement
else:
node.parent.right = replacement
replacement.parent = node.parent
node.parent = None
else:
self._tree = replacement
replacement.parent = None
node.left = None
node.right = None
node.parent = None
self._rebalance(replacement)
def _remove_leaf(self, node):
if node.parent:
if AvlTree._is_left(node):
node.parent.left = None
else:
node.parent.right = None
self._rebalance(node.parent)
else:
self._tree = None
node.parent = None
node.left = None
node.right = None
def remove(self, k):
node = self._get_node(k)
if not node:
return
if AvlTree._is_leaf(node):
self._remove_leaf(node)
return
if node.left and node.right:
nxt = AvlTree._get_next(node)
node.key = nxt.key
node.value = nxt.value
if self._is_leaf(nxt):
self._remove_leaf(nxt)
else:
self._remove_one(nxt)
self._rebalance(node)
else:
self._remove_one(node)
def get(self, k):
node = self._get_node(k)
return node.value if node else -1
def _get_node(self, k):
if not self._tree:
return None
node = self._tree
while node:
if k < node.key:
node = node.left
elif node.key < k:
node = node.right
else:
return node
return None
def get_at(self, pos):
x = pos + 1
node = self._tree
while node:
if x < node.num_left:
node = node.left
elif node.num_left < x:
x -= node.num_left
node = node.right
else:
return (node.key, node.value)
raise IndexError("Out of ranges")
@staticmethod
def _is_left(node):
return node.parent.left and node.parent.left == node
@staticmethod
def _is_leaf(node):
return node.left is None and node.right is None
def _rotate_right(self, node):
if not node.parent:
self._tree = node.left
node.left.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.left
node.left.parent = node.parent
else:
node.parent.right = node.left
node.left.parent = node.parent
bk = node.left.right
node.left.right = node
node.parent = node.left
node.left = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
def _rotate_left(self, node):
if not node.parent:
self._tree = node.right
node.right.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.right
node.right.parent = node.parent
else:
node.parent.right = node.right
node.right.parent = node.parent
bk = node.right.left
node.right.left = node
node.parent = node.right
node.right = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
@staticmethod
def _get_next(node):
if not node.right:
return node.parent
n = node.right
while n.left:
n = n.left
return n
# -----------------------------------------------binary seacrh tree---------------------------------------
class SegmentTree1:
def __init__(self, data, default=300006, func=lambda a, b: min(a , b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------game starts now----------------------------------------------------import math
class SegmentTree:
def __init__(self, data, default=0, func=lambda a, b:a + b):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------------------iye ha chutiya zindegi-------------------------------------
class Factorial:
def __init__(self, MOD):
self.MOD = MOD
self.factorials = [1, 1]
self.invModulos = [0, 1]
self.invFactorial_ = [1, 1]
def calc(self, n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0] * (n + 1 - len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = prev * i % m
self.factorials += nextArr
return self.factorials[n]
def inv(self, n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n % p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0] * (n + 1 - len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI, min(p, n + 1)):
next = -self.invModulos[p % i] * (p // i) % p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self, n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0] * (n + 1 - len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self, MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def ncr(self, n, k):
if k < 0 or n < k:
return 0
k = min(k, n - k)
f = self.factorial
return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
# --------------------------------------iye ha combinations ka zindegi---------------------------------
def powm(a, n, m):
if a == 1 or n == 0:
return 1
if n % 2 == 0:
s = powm(a, n // 2, m)
return s * s % m
else:
return a * powm(a, n - 1, m) % m
# --------------------------------------iye ha power ka zindegi---------------------------------
def sort_list(list1, list2):
zipped_pairs = zip(list2, list1)
z = [x for _, x in sorted(zipped_pairs)]
return z
# --------------------------------------------------product----------------------------------------
def product(l):
por = 1
for i in range(len(l)):
por *= l[i]
return por
# --------------------------------------------------binary----------------------------------------
def binarySearchCount(arr, n, key):
left = 0
right = n - 1
count = 0
while (left <= right):
mid = int((right + left) / 2)
# Check if middle element is
# less than or equal to key
if (arr[mid] <=key):
count = mid + 1
left = mid + 1
# If key is smaller, ignore right half
else:
right = mid - 1
return count
# --------------------------------------------------binary----------------------------------------
def countdig(n):
c = 0
while (n > 0):
n //= 10
c += 1
return c
def binary(x, length):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
def countGreater(arr, n, k):
l = 0
r = n - 1
# Stores the index of the left most element
# from the array which is greater than k
leftGreater = n
# Finds number of elements greater than k
while (l <= r):
m = int(l + (r - l) / 2)
if (arr[m] >= k):
leftGreater = m
r = m - 1
# If mid element is less than
# or equal to k update l
else:
l = m + 1
# Return the count of elements
# greater than k
return (n - leftGreater)
# --------------------------------------------------binary------------------------------------
class TrieNode:
def __init__(self):
self.children = [None] * 26
self.isEndOfWord = False
class Trie:
def __init__(self):
self.root = self.getNode()
def getNode(self):
return TrieNode()
def _charToIndex(self, ch):
return ord(ch) - ord('a')
def insert(self, key):
pCrawl = self.root
length = len(key)
for level in range(length):
index = self._charToIndex(key[level])
if not pCrawl.children[index]:
pCrawl.children[index] = self.getNode()
pCrawl = pCrawl.children[index]
pCrawl.isEndOfWord = True
def search(self, key):
pCrawl = self.root
length = len(key)
for level in range(length):
index = self._charToIndex(key[level])
if not pCrawl.children[index]:
return False
pCrawl = pCrawl.children[index]
return pCrawl != None and pCrawl.isEndOfWord
#-----------------------------------------trie---------------------------------
class Node:
def __init__(self, data):
self.data = data
self.height=0
self.left = None # left node for 0
self.right = None # right node for 1
class BinaryTrie:
def __init__(self):
self.root = Node(0)
def insert(self, pre_xor):
self.temp = self.root
for i in range(31, -1, -1):
val = pre_xor & (1 << i)
if val==0:
if not self.temp.right:
self.temp.right = Node(0)
self.temp = self.temp.right
elif val>=1:
if not self.temp.left:
self.temp.left = Node(0)
self.temp = self.temp.left
def do(self,temp):
if not temp:
return 0
ter=temp
temp.height=self.do(ter.left)+self.do(ter.right)
if temp.height==0:
temp.height+=1
return temp.height
def query(self, xor):
self.temp = self.root
cur=0
i=31
while(i>-1):
val = xor & (1 << i)
if not self.temp:
return cur
if val>=1:
self.opp = self.temp.right
if self.temp.left:
self.temp = self.temp.left
else:
return cur
else:
self.opp=self.temp.left
if self.temp.right:
self.temp = self.temp.right
else:
return cur
if self.temp.height==pow(2,i):
cur+=1<<(i)
self.temp=self.opp
i-=1
return cur
#-------------------------bin trie-------------------------------------------
def subsetXOR(arr, n, k):
if n==0:
return 1
max_ele = max(arr)
m=1
while(m<=max_ele):
m*=2
m-=1
dp = [[0 for j in range(m + 1)] for i in range(n + 1)]
dp[0][0] = 1
for i in range(1, n + 1):
for j in range(m + 1):
dp[i][j] = ((dp[i - 1][j]*even[arr[i-1]])%mod +(dp[i - 1][j ^ arr[i - 1]]*odd[arr[i-1]])%mod)%mod
return dp[n][k]
n=int(input())
l=list(map(int,input().split()))
prime=[2,3,5,7,11,13,17,23,29,31,37,41,43,47,53, 59, 61, 67]
square=[1,4,9,16,25,36,49,64]
prime=set(prime)
square=set(square)
cou=0
fi=[]
d=[0]*2000001
e=[0]*2000001
even=[0]*2000001
odd=[0]*2000001
for i in range(n):
we=1
t=l[i]
c=0
er=0
if t in square:
cou+=1
continue
if d[t]!=0:
d[t]+=1
continue
t1=t
d[t]+=1
for j in prime:
c=0
while(t%j==0):
c+=1
c%=2
t//=j
if c==1:
er+=we
we*=2
if t==1:
break
fi.append(er)
e[t1]=er
for i in range(71):
if d[i]==0:
continue
else:
even[e[i]]=pow(2,d[i]-1,mod)
odd[e[i]]=even[e[i]]
an=subsetXOR(fi,len(fi),0)
an*=pow(2,cou,mod)
an-=1
print(an%mod)
``` | instruction | 0 | 28,791 | 12 | 57,582 |
No | output | 1 | 28,791 | 12 | 57,583 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given a multiset S consisting of positive integers (initially empty). There are two kind of queries:
1. Add a positive integer to S, the newly added integer is not less than any number in it.
2. Find a subset s of the set S such that the value <image> is maximum possible. Here max(s) means maximum value of elements in s, <image> β the average value of numbers in s. Output this maximum possible value of <image>.
Input
The first line contains a single integer Q (1 β€ Q β€ 5Β·105) β the number of queries.
Each of the next Q lines contains a description of query. For queries of type 1 two integers 1 and x are given, where x (1 β€ x β€ 109) is a number that you should add to S. It's guaranteed that x is not less than any number in S. For queries of type 2, a single integer 2 is given.
It's guaranteed that the first query has type 1, i. e. S is not empty when a query of type 2 comes.
Output
Output the answer for each query of the second type in the order these queries are given in input. Each number should be printed in separate line.
Your answer is considered correct, if each of your answers has absolute or relative error not greater than 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if <image>.
Examples
Input
6
1 3
2
1 4
2
1 8
2
Output
0.0000000000
0.5000000000
3.0000000000
Input
4
1 1
1 4
1 5
2
Output
2.0000000000 | instruction | 0 | 28,801 | 12 | 57,602 |
Tags: binary search, greedy, ternary search, two pointers
Correct Solution:
```
#Code by Sounak, IIESTS
#------------------------------warmup----------------------------
import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
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")
#-------------------game starts now-----------------------------------------------------
n=int(input())
a=list()
c=0
d=0
j=0
s=0
for i in range (n):
b=list(map(int,input().split()))
if b[0]==1:
a.append(b[1])
c+=1
if c==1:
continue
while a[j]<=(s+a[-1])/(d+1) and j<c-1:
#print(a[j],(s+a[-1])/(d+1))
s+=a[j]
j+=1
d+=1
#print(s)
else:
m=a[-1]
if c==1:
print(0)
else:
if d==0:
avg=(a[0]+a[-1])/2
else:
avg=(s+a[-1])/(d+1)
print(m-avg)
``` | output | 1 | 28,801 | 12 | 57,603 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a and b of positive integers, with length n and m respectively.
Let c be an n Γ m matrix, where c_{i,j} = a_i β
b_j.
You need to find a subrectangle of the matrix c such that the sum of its elements is at most x, and its area (the total number of elements) is the largest possible.
Formally, you need to find the largest number s such that it is possible to choose integers x_1, x_2, y_1, y_2 subject to 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) = s, and $$$β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x.$$$
Input
The first line contains two integers n and m (1 β€ n, m β€ 2000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2000).
The third line contains m integers b_1, b_2, β¦, b_m (1 β€ b_i β€ 2000).
The fourth line contains a single integer x (1 β€ x β€ 2 β
10^{9}).
Output
If it is possible to choose four integers x_1, x_2, y_1, y_2 such that 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, and β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x, output the largest value of (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) among all such quadruplets, otherwise output 0.
Examples
Input
3 3
1 2 3
1 2 3
9
Output
4
Input
5 1
5 4 2 4 5
2
5
Output
1
Note
Matrix from the first sample and the chosen subrectangle (of blue color):
<image>
Matrix from the second sample and the chosen subrectangle (of blue color):
<image> | instruction | 0 | 29,119 | 12 | 58,238 |
Tags: binary search, implementation, two pointers
Correct Solution:
```
# -*- coding:utf-8 -*-
"""
created by shuangquan.huang at 11/20/18
"""
import bisect
N, M = map(int, input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
X = int(input())
#
# import time
# t0 = time.time()
#
# N, M = 2000, 2000
# A = [2000 for _ in range(N)]
# B = [2000 for _ in range(M)]
# X = 2*10**9
leftA = [0] * (N+1)
leftB = [0] * (M+1)
for i in range(1, N+1):
leftA[i] = leftA[i-1] + A[i-1]
for i in range(1, M+1):
leftB[i] = leftB[i-1] + B[i-1]
avs = [float('inf') for _ in range(N+1)]
for al in range(N):
for ar in range(al+1, N+1):
a = leftA[ar] - leftA[al]
if a > X:
break
l = ar - al
avs[l] = min(avs[l], a)
bvs = [float('inf') for _ in range(M+1)]
for bl in range(M):
for br in range(bl+1, M+1):
b = leftB[br] - leftB[bl]
if b > X:
break
l = br-bl
bvs[l] = min(bvs[l], b)
ans = 0
bvs[0] = 0
for i, a in enumerate(avs):
b = X//a
j = bisect.bisect_right(bvs, b)
if j == 0:
break
ans = max(ans, i * (j-1))
print(ans)
``` | output | 1 | 29,119 | 12 | 58,239 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a and b of positive integers, with length n and m respectively.
Let c be an n Γ m matrix, where c_{i,j} = a_i β
b_j.
You need to find a subrectangle of the matrix c such that the sum of its elements is at most x, and its area (the total number of elements) is the largest possible.
Formally, you need to find the largest number s such that it is possible to choose integers x_1, x_2, y_1, y_2 subject to 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) = s, and $$$β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x.$$$
Input
The first line contains two integers n and m (1 β€ n, m β€ 2000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2000).
The third line contains m integers b_1, b_2, β¦, b_m (1 β€ b_i β€ 2000).
The fourth line contains a single integer x (1 β€ x β€ 2 β
10^{9}).
Output
If it is possible to choose four integers x_1, x_2, y_1, y_2 such that 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, and β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x, output the largest value of (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) among all such quadruplets, otherwise output 0.
Examples
Input
3 3
1 2 3
1 2 3
9
Output
4
Input
5 1
5 4 2 4 5
2
5
Output
1
Note
Matrix from the first sample and the chosen subrectangle (of blue color):
<image>
Matrix from the second sample and the chosen subrectangle (of blue color):
<image> | instruction | 0 | 29,120 | 12 | 58,240 |
Tags: binary search, implementation, two pointers
Correct Solution:
```
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
x=int(input())
sum_a=[0]*(n+1)
sum_b=[0]*(m+1)
add_a=0
add_b=0
for i in range(1,n+1):
add_a+=a[i-1]
sum_a[i]=add_a
for i in range(1,m+1):
add_b+=b[i-1]
sum_b[i]=add_b
collect_a=[99999999999]*(n+1)
collect_b=[99999999999]*(m+1)
for i in range(1,n+1):
for j in range(i,n+1):
collect_a[j-i+1]=min(collect_a[j-i+1],sum_a[j]-sum_a[i-1])
for i in range(1,m+1):
for j in range(i,m+1):
collect_b[j-i+1]=min(collect_b[j-i+1],sum_b[j]-sum_b[i-1])
ans=0
for i in range(1,n+1):
for j in range(1,m+1):
if collect_a[i]*collect_b[j]<=x:
ans=max(ans,i*j)
print(ans)
``` | output | 1 | 29,120 | 12 | 58,241 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a and b of positive integers, with length n and m respectively.
Let c be an n Γ m matrix, where c_{i,j} = a_i β
b_j.
You need to find a subrectangle of the matrix c such that the sum of its elements is at most x, and its area (the total number of elements) is the largest possible.
Formally, you need to find the largest number s such that it is possible to choose integers x_1, x_2, y_1, y_2 subject to 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) = s, and $$$β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x.$$$
Input
The first line contains two integers n and m (1 β€ n, m β€ 2000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2000).
The third line contains m integers b_1, b_2, β¦, b_m (1 β€ b_i β€ 2000).
The fourth line contains a single integer x (1 β€ x β€ 2 β
10^{9}).
Output
If it is possible to choose four integers x_1, x_2, y_1, y_2 such that 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, and β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x, output the largest value of (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) among all such quadruplets, otherwise output 0.
Examples
Input
3 3
1 2 3
1 2 3
9
Output
4
Input
5 1
5 4 2 4 5
2
5
Output
1
Note
Matrix from the first sample and the chosen subrectangle (of blue color):
<image>
Matrix from the second sample and the chosen subrectangle (of blue color):
<image> | instruction | 0 | 29,121 | 12 | 58,242 |
Tags: binary search, implementation, two pointers
Correct Solution:
```
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
e=int(input())
c=[0]*(n+1)
x=[0]
x1=0
for i in range(n):
x1+=a[i]
x.append(x1)
y=[0]
y1=0
for i in range(m):
y1+=b[i]
y.append(y1)
for i in range(1,n+1):
d=x[i]
for j in range(n-i+1):
if x[j+i]-x[j]<d:
d=x[j+i]-x[j]
c[i]=d
s=0
for i in range(m):
for j in range(i,m):
l,r=0,n
while r-l>1:
m1=(l+r)//2
if c[m1]*(y[j+1]-y[i])>=e:
r=m1
else:
l=m1
while r>0 and c[r]*(y[j+1]-y[i])>e:
r-=1
s=max(s,r*(j-i+1))
print(s)
``` | output | 1 | 29,121 | 12 | 58,243 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a and b of positive integers, with length n and m respectively.
Let c be an n Γ m matrix, where c_{i,j} = a_i β
b_j.
You need to find a subrectangle of the matrix c such that the sum of its elements is at most x, and its area (the total number of elements) is the largest possible.
Formally, you need to find the largest number s such that it is possible to choose integers x_1, x_2, y_1, y_2 subject to 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) = s, and $$$β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x.$$$
Input
The first line contains two integers n and m (1 β€ n, m β€ 2000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2000).
The third line contains m integers b_1, b_2, β¦, b_m (1 β€ b_i β€ 2000).
The fourth line contains a single integer x (1 β€ x β€ 2 β
10^{9}).
Output
If it is possible to choose four integers x_1, x_2, y_1, y_2 such that 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, and β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x, output the largest value of (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) among all such quadruplets, otherwise output 0.
Examples
Input
3 3
1 2 3
1 2 3
9
Output
4
Input
5 1
5 4 2 4 5
2
5
Output
1
Note
Matrix from the first sample and the chosen subrectangle (of blue color):
<image>
Matrix from the second sample and the chosen subrectangle (of blue color):
<image> | instruction | 0 | 29,122 | 12 | 58,244 |
Tags: binary search, implementation, two pointers
Correct Solution:
```
n, m = map(int, input().split())
a = [0] + list(map(int, input().split()))
b = [0] + list(map(int, input().split()))
c = [0] * (n + 1)
d = [0] * (m + 1)
x = int(input())
for i in range(1, n + 1):
a[i] = a[i - 1] + a[i]
c[i] = a[i]
for j in range(1, i):
c[i - j] = min(c[i - j], a[i] - a[j])
for i in range(1, m + 1):
b[i] = b[i - 1] + b[i]
d[i] = b[i]
for j in range(1, i):
d[i - j] = min(d[i - j], b[i] - b[j])
ans = 0
for i in range(1, n + 1):
for j in range(1, m + 1):
if c[i] * d[j] <= x:
ans = max(ans, i * j)
print(ans)
``` | output | 1 | 29,122 | 12 | 58,245 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a and b of positive integers, with length n and m respectively.
Let c be an n Γ m matrix, where c_{i,j} = a_i β
b_j.
You need to find a subrectangle of the matrix c such that the sum of its elements is at most x, and its area (the total number of elements) is the largest possible.
Formally, you need to find the largest number s such that it is possible to choose integers x_1, x_2, y_1, y_2 subject to 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) = s, and $$$β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x.$$$
Input
The first line contains two integers n and m (1 β€ n, m β€ 2000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2000).
The third line contains m integers b_1, b_2, β¦, b_m (1 β€ b_i β€ 2000).
The fourth line contains a single integer x (1 β€ x β€ 2 β
10^{9}).
Output
If it is possible to choose four integers x_1, x_2, y_1, y_2 such that 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, and β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x, output the largest value of (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) among all such quadruplets, otherwise output 0.
Examples
Input
3 3
1 2 3
1 2 3
9
Output
4
Input
5 1
5 4 2 4 5
2
5
Output
1
Note
Matrix from the first sample and the chosen subrectangle (of blue color):
<image>
Matrix from the second sample and the chosen subrectangle (of blue color):
<image> | instruction | 0 | 29,123 | 12 | 58,246 |
Tags: binary search, implementation, two pointers
Correct Solution:
```
import bisect
n,m=map(int,input().split())
L1=list(map(int,input().split()))
L2=list(map(int,input().split()))
x=int(input())
newL1=[0]
newL2=[0]
for i in L1:newL1.append(newL1[-1]+i)
for i in L2:newL2.append(newL2[-1]+i)
min1=[]
min2=[]
mx=9999999999999999999
for i in range(1,n+1):
m1=mx
for j in range(n-i+1):
if newL1[j+i]-newL1[j]<m1:m1=newL1[j+i]-newL1[j]
min1.append(m1)
for i in range(1,m+1):
m2=mx
for j in range(m-i+1):
if newL2[j+i]-newL2[j]<m2:m2=newL2[j+i]-newL2[j]
min2.append(m2)
area=0
for i in range(n):
k=x//min1[i]
for j in range(m):
if min2[j]>k:break
if min2[-1]<=k:j+=1
if area<j*(i+1):area=j*(i+1)
print(area)
``` | output | 1 | 29,123 | 12 | 58,247 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a and b of positive integers, with length n and m respectively.
Let c be an n Γ m matrix, where c_{i,j} = a_i β
b_j.
You need to find a subrectangle of the matrix c such that the sum of its elements is at most x, and its area (the total number of elements) is the largest possible.
Formally, you need to find the largest number s such that it is possible to choose integers x_1, x_2, y_1, y_2 subject to 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) = s, and $$$β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x.$$$
Input
The first line contains two integers n and m (1 β€ n, m β€ 2000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2000).
The third line contains m integers b_1, b_2, β¦, b_m (1 β€ b_i β€ 2000).
The fourth line contains a single integer x (1 β€ x β€ 2 β
10^{9}).
Output
If it is possible to choose four integers x_1, x_2, y_1, y_2 such that 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, and β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x, output the largest value of (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) among all such quadruplets, otherwise output 0.
Examples
Input
3 3
1 2 3
1 2 3
9
Output
4
Input
5 1
5 4 2 4 5
2
5
Output
1
Note
Matrix from the first sample and the chosen subrectangle (of blue color):
<image>
Matrix from the second sample and the chosen subrectangle (of blue color):
<image> | instruction | 0 | 29,124 | 12 | 58,248 |
Tags: binary search, implementation, two pointers
Correct Solution:
```
from itertools import accumulate
n,m=map(int,input().split())
c=list(map(int,input().split()))
b=list(map(int,input().split()))
a=list(accumulate(c))
x=int(input())
res=0
for i in range(1,n+1):
sa=min(ar-al for ar,al in zip(a[i-1:],[0]+a[:]))
l,s=-1,0
for r in range(m):
s+=sa*b[r]
while s>x:
l+=1
s-=sa*b[l]
res=max(res,i*(r-l))
print(res)
``` | output | 1 | 29,124 | 12 | 58,249 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a and b of positive integers, with length n and m respectively.
Let c be an n Γ m matrix, where c_{i,j} = a_i β
b_j.
You need to find a subrectangle of the matrix c such that the sum of its elements is at most x, and its area (the total number of elements) is the largest possible.
Formally, you need to find the largest number s such that it is possible to choose integers x_1, x_2, y_1, y_2 subject to 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) = s, and $$$β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x.$$$
Input
The first line contains two integers n and m (1 β€ n, m β€ 2000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2000).
The third line contains m integers b_1, b_2, β¦, b_m (1 β€ b_i β€ 2000).
The fourth line contains a single integer x (1 β€ x β€ 2 β
10^{9}).
Output
If it is possible to choose four integers x_1, x_2, y_1, y_2 such that 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, and β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x, output the largest value of (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) among all such quadruplets, otherwise output 0.
Examples
Input
3 3
1 2 3
1 2 3
9
Output
4
Input
5 1
5 4 2 4 5
2
5
Output
1
Note
Matrix from the first sample and the chosen subrectangle (of blue color):
<image>
Matrix from the second sample and the chosen subrectangle (of blue color):
<image> | instruction | 0 | 29,125 | 12 | 58,250 |
Tags: binary search, implementation, two pointers
Correct Solution:
```
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
from sys import stdin, stdout
#N = int(input())
#s = input()
N,M = [int(x) for x in stdin.readline().split()]
arrA = [int(x) for x in stdin.readline().split()]
arrB = [int(x) for x in stdin.readline().split()]
K = int(input())
A = [0]*N
B = [0]*M
prefix_A = [0]*N
s = 0
for i in range(N):
s += arrA[i]
prefix_A[i] = s
prefix_B = [0]*M
s = 0
for i in range(M):
s += arrB[i]
prefix_B[i] = s
A[0] = min(arrA)
B[0] = min(arrB)
for i in range(1,N):
m = prefix_A[i]
for j in range(i+1,N):
m = min(m,prefix_A[j]-prefix_A[j-i-1])
A[i] = m
for i in range(1,M):
m = prefix_B[i]
for j in range(i+1,M):
m = min(m,prefix_B[j]-prefix_B[j-i-1])
B[i] = m
res = 0
for i in range(N):
for j in range(M):
d = A[i]*B[j]
if d<=K:
res = max(res,(i+1)*(j+1))
print(res)
``` | output | 1 | 29,125 | 12 | 58,251 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given two arrays a and b of positive integers, with length n and m respectively.
Let c be an n Γ m matrix, where c_{i,j} = a_i β
b_j.
You need to find a subrectangle of the matrix c such that the sum of its elements is at most x, and its area (the total number of elements) is the largest possible.
Formally, you need to find the largest number s such that it is possible to choose integers x_1, x_2, y_1, y_2 subject to 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) = s, and $$$β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x.$$$
Input
The first line contains two integers n and m (1 β€ n, m β€ 2000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2000).
The third line contains m integers b_1, b_2, β¦, b_m (1 β€ b_i β€ 2000).
The fourth line contains a single integer x (1 β€ x β€ 2 β
10^{9}).
Output
If it is possible to choose four integers x_1, x_2, y_1, y_2 such that 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, and β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x, output the largest value of (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) among all such quadruplets, otherwise output 0.
Examples
Input
3 3
1 2 3
1 2 3
9
Output
4
Input
5 1
5 4 2 4 5
2
5
Output
1
Note
Matrix from the first sample and the chosen subrectangle (of blue color):
<image>
Matrix from the second sample and the chosen subrectangle (of blue color):
<image> | instruction | 0 | 29,126 | 12 | 58,252 |
Tags: binary search, implementation, two pointers
Correct Solution:
```
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = int(input())
rows = [float('inf')] * (n + 1)
cols = [float('inf')] * (m + 1)
for i in range(n):
summ = 0
for j in range(i, n):
summ += a[j]
l = j - i + 1
rows[l] = min(rows[l], summ)
for i in range(m):
summ = 0
for j in range(i, m):
summ += b[j]
l = j - i + 1
cols[l] = min(cols[l], summ)
ans = 0
for i in range(1, n + 1):
for j in range(1, m + 1):
cur = rows[i] * cols[j]
if cur <= x:
ans = max(ans, i * j)
print(ans)
``` | output | 1 | 29,126 | 12 | 58,253 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a and b of positive integers, with length n and m respectively.
Let c be an n Γ m matrix, where c_{i,j} = a_i β
b_j.
You need to find a subrectangle of the matrix c such that the sum of its elements is at most x, and its area (the total number of elements) is the largest possible.
Formally, you need to find the largest number s such that it is possible to choose integers x_1, x_2, y_1, y_2 subject to 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) = s, and $$$β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x.$$$
Input
The first line contains two integers n and m (1 β€ n, m β€ 2000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2000).
The third line contains m integers b_1, b_2, β¦, b_m (1 β€ b_i β€ 2000).
The fourth line contains a single integer x (1 β€ x β€ 2 β
10^{9}).
Output
If it is possible to choose four integers x_1, x_2, y_1, y_2 such that 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, and β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x, output the largest value of (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) among all such quadruplets, otherwise output 0.
Examples
Input
3 3
1 2 3
1 2 3
9
Output
4
Input
5 1
5 4 2 4 5
2
5
Output
1
Note
Matrix from the first sample and the chosen subrectangle (of blue color):
<image>
Matrix from the second sample and the chosen subrectangle (of blue color):
<image>
Submitted Solution:
```
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = int(input())
def calc(arr):
k = len(arr)
ret = [10**10 for i in range(k + 1)]
for l in range(0, k):
cur = 0
for r in range(l, k):
cur += arr[r]
ret[r - l + 1] = min(ret[r - l + 1], cur)
return ret
bestA = calc(a)
bestB = calc(b)
ans = 0
for a in range(1, n + 1):
for b in range(1, m + 1):
if (bestA[a] * bestB[b] <= x):
ans = max(ans, a * b)
else:
break
print(ans)
``` | instruction | 0 | 29,127 | 12 | 58,254 |
Yes | output | 1 | 29,127 | 12 | 58,255 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a and b of positive integers, with length n and m respectively.
Let c be an n Γ m matrix, where c_{i,j} = a_i β
b_j.
You need to find a subrectangle of the matrix c such that the sum of its elements is at most x, and its area (the total number of elements) is the largest possible.
Formally, you need to find the largest number s such that it is possible to choose integers x_1, x_2, y_1, y_2 subject to 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) = s, and $$$β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x.$$$
Input
The first line contains two integers n and m (1 β€ n, m β€ 2000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2000).
The third line contains m integers b_1, b_2, β¦, b_m (1 β€ b_i β€ 2000).
The fourth line contains a single integer x (1 β€ x β€ 2 β
10^{9}).
Output
If it is possible to choose four integers x_1, x_2, y_1, y_2 such that 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, and β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x, output the largest value of (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) among all such quadruplets, otherwise output 0.
Examples
Input
3 3
1 2 3
1 2 3
9
Output
4
Input
5 1
5 4 2 4 5
2
5
Output
1
Note
Matrix from the first sample and the chosen subrectangle (of blue color):
<image>
Matrix from the second sample and the chosen subrectangle (of blue color):
<image>
Submitted Solution:
```
from sys import stdin,stdout
n,m=stdin.readline().strip().split(' ')
n,m=int(n),int(m)
row=list(map(int,stdin.readline().strip().split(' ')))
col=list(map(int,stdin.readline().strip().split(' ')))
x=int(stdin.readline().strip())
lowest_row_array=[0]
for i in range(1,n+1):
s=sum(row[:i])
j=i
min_till_now=s
while j<n:
s=s-row[j-i]+row[j]
if s<min_till_now:
min_till_now=s
j+=1
lowest_row_array.append(min_till_now)
lowest_col_array=[0]
for i in range(1,m+1):
s=sum(col[:i]);
j=i
min_till_now=s
while j<m:
s=s-col[j-i]+col[j]
if s<min_till_now:
min_till_now=s
j+=1
lowest_col_array.append(min_till_now)
max_size=0;
if len(lowest_col_array)==1:
lowest_col_array[0]=1
if len(lowest_row_array)==1:
lowest_row_array[0]=1
for i in range(len(lowest_row_array)):
for j in range(len(lowest_col_array)):
if lowest_row_array[i]*lowest_col_array[j]<=x:
if i*j>max_size:
max_size=i*j
# print(lowest_col_array)
# print(lowest_row_array)
print(max_size)
``` | instruction | 0 | 29,128 | 12 | 58,256 |
Yes | output | 1 | 29,128 | 12 | 58,257 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a and b of positive integers, with length n and m respectively.
Let c be an n Γ m matrix, where c_{i,j} = a_i β
b_j.
You need to find a subrectangle of the matrix c such that the sum of its elements is at most x, and its area (the total number of elements) is the largest possible.
Formally, you need to find the largest number s such that it is possible to choose integers x_1, x_2, y_1, y_2 subject to 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) = s, and $$$β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x.$$$
Input
The first line contains two integers n and m (1 β€ n, m β€ 2000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2000).
The third line contains m integers b_1, b_2, β¦, b_m (1 β€ b_i β€ 2000).
The fourth line contains a single integer x (1 β€ x β€ 2 β
10^{9}).
Output
If it is possible to choose four integers x_1, x_2, y_1, y_2 such that 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, and β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x, output the largest value of (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) among all such quadruplets, otherwise output 0.
Examples
Input
3 3
1 2 3
1 2 3
9
Output
4
Input
5 1
5 4 2 4 5
2
5
Output
1
Note
Matrix from the first sample and the chosen subrectangle (of blue color):
<image>
Matrix from the second sample and the chosen subrectangle (of blue color):
<image>
Submitted Solution:
```
n,m=map(int,input().split(" "))
a=list(map(int,input().split(" ")))
b=list(map(int,input().split(" ")))
suma=list()
suma.append(0)
for i in range(len(a)):
suma.append(a[i]+suma[i])
sumb=list()
sumb.append(0)
for i in range(len(b)):
sumb.append(b[i]+sumb[i])
mina=list()
mina.append(0)
minb=list()
minb.append(0)
maxnum=3000*3000
for tmplen in range(1,n+1):
mina.append(maxnum)
for loc in range(n):
if(loc+tmplen>n):
break
else:
mina[tmplen]=min(mina[tmplen],suma[loc+tmplen]-suma[loc])
for tmplen in range(1,m+1):
minb.append(maxnum)
for loc in range(m):
if(loc+tmplen>m):
break
else:
minb[tmplen]=min(minb[tmplen],sumb[tmplen+loc]-sumb[loc])
x=int(input())
ans=0
for i in range(1,n+1):
for j in range(1,m+1):
if(mina[i]*minb[j]<=x):
ans=max(ans,i*j)
print(ans)
``` | instruction | 0 | 29,129 | 12 | 58,258 |
Yes | output | 1 | 29,129 | 12 | 58,259 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a and b of positive integers, with length n and m respectively.
Let c be an n Γ m matrix, where c_{i,j} = a_i β
b_j.
You need to find a subrectangle of the matrix c such that the sum of its elements is at most x, and its area (the total number of elements) is the largest possible.
Formally, you need to find the largest number s such that it is possible to choose integers x_1, x_2, y_1, y_2 subject to 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) = s, and $$$β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x.$$$
Input
The first line contains two integers n and m (1 β€ n, m β€ 2000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2000).
The third line contains m integers b_1, b_2, β¦, b_m (1 β€ b_i β€ 2000).
The fourth line contains a single integer x (1 β€ x β€ 2 β
10^{9}).
Output
If it is possible to choose four integers x_1, x_2, y_1, y_2 such that 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, and β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x, output the largest value of (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) among all such quadruplets, otherwise output 0.
Examples
Input
3 3
1 2 3
1 2 3
9
Output
4
Input
5 1
5 4 2 4 5
2
5
Output
1
Note
Matrix from the first sample and the chosen subrectangle (of blue color):
<image>
Matrix from the second sample and the chosen subrectangle (of blue color):
<image>
Submitted Solution:
```
from itertools import accumulate
n,m=map(int,input().split(" "))
c=list(map(int,input().split(" ")))
b=list(map(int,input().split(" ")))
a=list(accumulate(c))
x=int(input())
res=0
for i in range(1,n+1):
sa=min(ar-al for ar,al in zip(a[i-1:],[0]+a[:]))
l,s=-1,0
for r in range(m):
s+=sa*b[r]
while s>x:
l+=1
s-=sa*b[l]
res=max(res,i*(r-l))
print(res)
``` | instruction | 0 | 29,130 | 12 | 58,260 |
Yes | output | 1 | 29,130 | 12 | 58,261 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a and b of positive integers, with length n and m respectively.
Let c be an n Γ m matrix, where c_{i,j} = a_i β
b_j.
You need to find a subrectangle of the matrix c such that the sum of its elements is at most x, and its area (the total number of elements) is the largest possible.
Formally, you need to find the largest number s such that it is possible to choose integers x_1, x_2, y_1, y_2 subject to 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) = s, and $$$β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x.$$$
Input
The first line contains two integers n and m (1 β€ n, m β€ 2000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2000).
The third line contains m integers b_1, b_2, β¦, b_m (1 β€ b_i β€ 2000).
The fourth line contains a single integer x (1 β€ x β€ 2 β
10^{9}).
Output
If it is possible to choose four integers x_1, x_2, y_1, y_2 such that 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, and β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x, output the largest value of (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) among all such quadruplets, otherwise output 0.
Examples
Input
3 3
1 2 3
1 2 3
9
Output
4
Input
5 1
5 4 2 4 5
2
5
Output
1
Note
Matrix from the first sample and the chosen subrectangle (of blue color):
<image>
Matrix from the second sample and the chosen subrectangle (of blue color):
<image>
Submitted Solution:
```
n,m=map(int, input().split())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
t=int(input())
asum=sum(a)
bsum=sum(b)
x=0
y=0
i=n-1
j=m-1
while i>=x and j>=y:
if asum*bsum <= t:
print((i-x+1)*(j-y+1))
break
mia,maa,mib,mab=a[x],a[i],b[y],b[j]
if mia>=max(maa,mib,mab):
asum-=a[x]
x+=1
elif maa>=max(mia,mib,mab):
asum-=a[i]
i-=1
elif mib>=max(maa,mia,mab):
bsum-=b[y]
y+=1
else:
bsum-=b[j]
j-=1
else:
print(0)
``` | instruction | 0 | 29,131 | 12 | 58,262 |
No | output | 1 | 29,131 | 12 | 58,263 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a and b of positive integers, with length n and m respectively.
Let c be an n Γ m matrix, where c_{i,j} = a_i β
b_j.
You need to find a subrectangle of the matrix c such that the sum of its elements is at most x, and its area (the total number of elements) is the largest possible.
Formally, you need to find the largest number s such that it is possible to choose integers x_1, x_2, y_1, y_2 subject to 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) = s, and $$$β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x.$$$
Input
The first line contains two integers n and m (1 β€ n, m β€ 2000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2000).
The third line contains m integers b_1, b_2, β¦, b_m (1 β€ b_i β€ 2000).
The fourth line contains a single integer x (1 β€ x β€ 2 β
10^{9}).
Output
If it is possible to choose four integers x_1, x_2, y_1, y_2 such that 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, and β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x, output the largest value of (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) among all such quadruplets, otherwise output 0.
Examples
Input
3 3
1 2 3
1 2 3
9
Output
4
Input
5 1
5 4 2 4 5
2
5
Output
1
Note
Matrix from the first sample and the chosen subrectangle (of blue color):
<image>
Matrix from the second sample and the chosen subrectangle (of blue color):
<image>
Submitted Solution:
```
n,m=map(int,input().split())
a=[int(x) for x in input().split()]
b=[int(x) for x in input().split()]
x=int(input())
d=[]
for i in range(n):
c=0
for j in range(i,n):
c+=a[j]
d.append([c,(j-i+1)])
d.sort(reverse=True)
p={}
for i in range(len(d)):
if d[i][1] not in p:
p[d[i][1]]=d[i][0]
else:
p[d[i][1]]=min(p[d[i][1]],d[i][0])
g=[0 for i in range(n)]
def binary_search(a,p):
l=0
r=len(a)-1
ans=len(a)
while l<=r:
mid=(l+r)//2
if a[mid]<=p:
ans=mid
l=mid+1
else:
r=mid-1
return ans
for i in p:
g[i-1]=p[i]
maxi=0
print(g)
for i in range(m):
c=0
for j in range(i,m):
c+=b[j]
if c>x:
break
r=binary_search(g,x//c)
#print(c,x//c,r)
maxi=max(maxi,(j-i+1)*(r+1))
print(maxi)
``` | instruction | 0 | 29,132 | 12 | 58,264 |
No | output | 1 | 29,132 | 12 | 58,265 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a and b of positive integers, with length n and m respectively.
Let c be an n Γ m matrix, where c_{i,j} = a_i β
b_j.
You need to find a subrectangle of the matrix c such that the sum of its elements is at most x, and its area (the total number of elements) is the largest possible.
Formally, you need to find the largest number s such that it is possible to choose integers x_1, x_2, y_1, y_2 subject to 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) = s, and $$$β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x.$$$
Input
The first line contains two integers n and m (1 β€ n, m β€ 2000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2000).
The third line contains m integers b_1, b_2, β¦, b_m (1 β€ b_i β€ 2000).
The fourth line contains a single integer x (1 β€ x β€ 2 β
10^{9}).
Output
If it is possible to choose four integers x_1, x_2, y_1, y_2 such that 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, and β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x, output the largest value of (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) among all such quadruplets, otherwise output 0.
Examples
Input
3 3
1 2 3
1 2 3
9
Output
4
Input
5 1
5 4 2 4 5
2
5
Output
1
Note
Matrix from the first sample and the chosen subrectangle (of blue color):
<image>
Matrix from the second sample and the chosen subrectangle (of blue color):
<image>
Submitted Solution:
```
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
x=int(input())
gooda=[10000000]*n
goodb=[10000000]*m
for i in range(n):
tots=0
lent=0
for j in range(i,n):
tots+=a[j]
lent+=1
gooda[lent-1]=min(gooda[lent-1],tots)
for i in range(m):
tots=0
lent=0
for j in range(i,m):
tots+=b[j]
lent+=1
goodb[lent-1]=min(goodb[lent-1],tots)
if n==2000:
print(goodb)
best=0
aind=0
bind=m-1
while aind<n and bind>=0:
if gooda[aind]*goodb[bind]<=x:
best=max(best,(aind+1)*(bind+1))
aind+=1
else:
bind-=1
print(best)
``` | instruction | 0 | 29,133 | 12 | 58,266 |
No | output | 1 | 29,133 | 12 | 58,267 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given two arrays a and b of positive integers, with length n and m respectively.
Let c be an n Γ m matrix, where c_{i,j} = a_i β
b_j.
You need to find a subrectangle of the matrix c such that the sum of its elements is at most x, and its area (the total number of elements) is the largest possible.
Formally, you need to find the largest number s such that it is possible to choose integers x_1, x_2, y_1, y_2 subject to 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) = s, and $$$β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x.$$$
Input
The first line contains two integers n and m (1 β€ n, m β€ 2000).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 2000).
The third line contains m integers b_1, b_2, β¦, b_m (1 β€ b_i β€ 2000).
The fourth line contains a single integer x (1 β€ x β€ 2 β
10^{9}).
Output
If it is possible to choose four integers x_1, x_2, y_1, y_2 such that 1 β€ x_1 β€ x_2 β€ n, 1 β€ y_1 β€ y_2 β€ m, and β_{i=x_1}^{x_2}{β_{j=y_1}^{y_2}{c_{i,j}}} β€ x, output the largest value of (x_2 - x_1 + 1) Γ (y_2 - y_1 + 1) among all such quadruplets, otherwise output 0.
Examples
Input
3 3
1 2 3
1 2 3
9
Output
4
Input
5 1
5 4 2 4 5
2
5
Output
1
Note
Matrix from the first sample and the chosen subrectangle (of blue color):
<image>
Matrix from the second sample and the chosen subrectangle (of blue color):
<image>
Submitted Solution:
```
[n, m] = map(int, input().split());
a = [0] + list(map(int, input().split()));
b = [0] + list(map(int, input().split()));
x = int(input());
for i in range(1, n+1):
a[i] += a[i-1];
for i in range(1, m+1):
b[i] += b[i-1];
INF = 2001;
A = [INF] * (n+1);
B = [INF] * (m+1);
for i in range(1, n+1):
for j in range(1, i+1):
A[i - j + 1] = min(A[i - j + 1], a[i] - a[j - 1]);
for i in range(1, m+1):
for j in range(1, i+1):
B[i - j + 1] = min(B[i - j + 1], b[i] - b[j - 1]);
answ = 0;
for i in range(1, n+1):
for j in range(1, m+1):
if (A[i] * B[j] <= x):
answ = max(answ, i*j);
print(answ);
``` | instruction | 0 | 29,134 | 12 | 58,268 |
No | output | 1 | 29,134 | 12 | 58,269 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n segments [l_i, r_i] for 1 β€ i β€ n. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group.
To optimize testing process you will be given multitest.
Input
The first line contains one integer T (1 β€ T β€ 50000) β the number of queries. Each query contains description of the set of segments. Queries are independent.
First line of each query contains single integer n (2 β€ n β€ 10^5) β number of segments. It is guaranteed that β{n} over all queries does not exceed 10^5.
The next n lines contains two integers l_i, r_i per line (1 β€ l_i β€ r_i β€ 2 β
10^5) β the i-th segment.
Output
For each query print n integers t_1, t_2, ..., t_n (t_i β \{1, 2\}) β for each segment (in the same order as in the input) t_i equals 1 if the i-th segment will belongs to the first group and 2 otherwise.
If there are multiple answers, you can print any of them. If there is no answer, print -1.
Example
Input
3
2
5 5
2 3
3
3 5
2 3
2 3
3
3 3
4 4
5 5
Output
2 1
-1
1 1 2
Note
In the first query the first and the second segments should be in different groups, but exact numbers don't matter.
In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is -1.
In the third query we can distribute segments in any way that makes groups non-empty, so any answer of 6 possible is correct. | instruction | 0 | 29,151 | 12 | 58,302 |
Tags: sortings
Correct Solution:
```
from bisect import insort,bisect_right,bisect_left
from sys import stdout, stdin, setrecursionlimit
from heapq import heappush, heappop, heapify
from io import BytesIO, IOBase
from collections import *
from itertools import *
from random import *
from string import *
from queue import *
from math import *
from re import *
from os import *
# sqrt,ceil,floor,factorial,gcd,log2,log10,comb
####################################---fast-input-output----#########################################
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 = read(self._fd, max(fstat(self._fd).st_size, 8192))
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 = read(self._fd, max(fstat(self._fd).st_size, 8192))
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:
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")
stdin, stdout = IOWrapper(stdin), IOWrapper(stdout)
graph, mod, szzz = {}, 10**9 + 7, lambda: sorted(zzz())
def getStr(): return input()
def getInt(): return int(input())
def listStr(): return list(input())
def getStrs(): return input().split()
def isInt(s): return '0' <= s[0] <= '9'
def input(): return stdin.readline().strip()
def zzz(): return [int(i) for i in input().split()]
def output(answer, end='\n'): stdout.write(str(answer) + end)
def lcd(xnum1, xnum2): return (xnum1 * xnum2 // gcd(xnum1, xnum2))
def getPrimes(N = 10**5):
SN = int(sqrt(N))
sieve = [i for i in range(N+1)]
sieve[1] = 0
for i in sieve:
if i > SN:
break
if i == 0:
continue
for j in range(2*i, N+1, i):
sieve[j] = 0
prime = [i for i in range(N+1) if sieve[i] != 0]
return prime
def primeFactor(n,prime=getPrimes()):
lst = []
mx=int(sqrt(n))+1
for i in prime:
if i>mx:break
while n%i==0:
lst.append(i)
n//=i
if n>1:
lst.append(n)
return lst
dx = [-1, 1, 0, 0, 1, -1, 1, -1]
dy = [0, 0, 1, -1, 1, -1, -1, 1]
daysInMounth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
#################################################---Some Rule For Me To Follow---#################################
"""
--instants of Reading problem continuously try to understand them.
--Try & again try, maybe you're just one statement away!
"""
##################################################---START-CODING---###############################################
num = getInt()
for _ in range(num):
n = getInt()
points=[]
for i in range(n):
points.append((zzz(),i))
points.sort()
left = points[0][0][0]
right = points[0][0][1]
ok = 1
ans = [0]*n
for i in range(n):
if points[i][0][0]>right:
ok=2
ans[points[i][1]]=ok
right=max(right,points[i][0][1])
print(*ans if ok-1 else [-1])
``` | output | 1 | 29,151 | 12 | 58,303 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n segments [l_i, r_i] for 1 β€ i β€ n. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group.
To optimize testing process you will be given multitest.
Input
The first line contains one integer T (1 β€ T β€ 50000) β the number of queries. Each query contains description of the set of segments. Queries are independent.
First line of each query contains single integer n (2 β€ n β€ 10^5) β number of segments. It is guaranteed that β{n} over all queries does not exceed 10^5.
The next n lines contains two integers l_i, r_i per line (1 β€ l_i β€ r_i β€ 2 β
10^5) β the i-th segment.
Output
For each query print n integers t_1, t_2, ..., t_n (t_i β \{1, 2\}) β for each segment (in the same order as in the input) t_i equals 1 if the i-th segment will belongs to the first group and 2 otherwise.
If there are multiple answers, you can print any of them. If there is no answer, print -1.
Example
Input
3
2
5 5
2 3
3
3 5
2 3
2 3
3
3 3
4 4
5 5
Output
2 1
-1
1 1 2
Note
In the first query the first and the second segments should be in different groups, but exact numbers don't matter.
In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is -1.
In the third query we can distribute segments in any way that makes groups non-empty, so any answer of 6 possible is correct. | instruction | 0 | 29,152 | 12 | 58,304 |
Tags: sortings
Correct Solution:
```
T = int(input())
for t in range(T):
N = int(input())
segs = []
for i,n in enumerate(range(N)):
seg = [int(x) for x in input().split()]+[i]
segs.append(seg)
segs.sort(key = lambda x: x[0])
clas = 1
nots = [2,]* len(segs)
A,B,_=segs[0]
for seg in segs:
C,D,idx = seg
if (D>=A and C<=B):
#overlaps
nots[idx] = clas
B = max(B,D)
else:
clas +=1
break
if clas == 1:
print(-1)
else:
print(*nots)
``` | output | 1 | 29,152 | 12 | 58,305 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n segments [l_i, r_i] for 1 β€ i β€ n. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group.
To optimize testing process you will be given multitest.
Input
The first line contains one integer T (1 β€ T β€ 50000) β the number of queries. Each query contains description of the set of segments. Queries are independent.
First line of each query contains single integer n (2 β€ n β€ 10^5) β number of segments. It is guaranteed that β{n} over all queries does not exceed 10^5.
The next n lines contains two integers l_i, r_i per line (1 β€ l_i β€ r_i β€ 2 β
10^5) β the i-th segment.
Output
For each query print n integers t_1, t_2, ..., t_n (t_i β \{1, 2\}) β for each segment (in the same order as in the input) t_i equals 1 if the i-th segment will belongs to the first group and 2 otherwise.
If there are multiple answers, you can print any of them. If there is no answer, print -1.
Example
Input
3
2
5 5
2 3
3
3 5
2 3
2 3
3
3 3
4 4
5 5
Output
2 1
-1
1 1 2
Note
In the first query the first and the second segments should be in different groups, but exact numbers don't matter.
In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is -1.
In the third query we can distribute segments in any way that makes groups non-empty, so any answer of 6 possible is correct. | instruction | 0 | 29,153 | 12 | 58,306 |
Tags: sortings
Correct Solution:
```
import sys
input=sys.stdin.readline
for _ in range(int(input())):
n=int(input())
ar=[]
for i in range(n):
ar.append(list(map(int,input().split()))+[i])
ar.sort(key=lambda x:x[0])
su=0
flag=False
ma=ar[0][1]
for i in range(1,n):
if(ar[i][0]>ma):
flag=True
break
else:
ma=max(ma,ar[i][1])
if(flag):
ans=[0]*n
for j in range(i):
ans[ar[j][2]]=1
for j in range(i,n):
ans[ar[j][2]]=2
print(*ans)
else:
print(-1)
``` | output | 1 | 29,153 | 12 | 58,307 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n segments [l_i, r_i] for 1 β€ i β€ n. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group.
To optimize testing process you will be given multitest.
Input
The first line contains one integer T (1 β€ T β€ 50000) β the number of queries. Each query contains description of the set of segments. Queries are independent.
First line of each query contains single integer n (2 β€ n β€ 10^5) β number of segments. It is guaranteed that β{n} over all queries does not exceed 10^5.
The next n lines contains two integers l_i, r_i per line (1 β€ l_i β€ r_i β€ 2 β
10^5) β the i-th segment.
Output
For each query print n integers t_1, t_2, ..., t_n (t_i β \{1, 2\}) β for each segment (in the same order as in the input) t_i equals 1 if the i-th segment will belongs to the first group and 2 otherwise.
If there are multiple answers, you can print any of them. If there is no answer, print -1.
Example
Input
3
2
5 5
2 3
3
3 5
2 3
2 3
3
3 3
4 4
5 5
Output
2 1
-1
1 1 2
Note
In the first query the first and the second segments should be in different groups, but exact numbers don't matter.
In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is -1.
In the third query we can distribute segments in any way that makes groups non-empty, so any answer of 6 possible is correct. | instruction | 0 | 29,154 | 12 | 58,308 |
Tags: sortings
Correct Solution:
```
ansarr=[]
t=int(input())
for l in range(t):
n=int(input())
arr1=[]
for i in range(n):
a,b=map(int,input().split())
arr1.append((a,b,i))
arr1.sort()
max1=arr1[0][1]
flag=0
arr2=[0]*n
arr2[arr1[0][2]]=1
for i in range(1,n):
if(arr1[i][0]<=max1):
max1=max(max1,arr1[i][1])
arr2[arr1[i][2]]=1
else:
flag=1
arr2[arr1[i][2]]=2
if(flag==0):
print(-1)
else:
print(*arr2)
``` | output | 1 | 29,154 | 12 | 58,309 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n segments [l_i, r_i] for 1 β€ i β€ n. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group.
To optimize testing process you will be given multitest.
Input
The first line contains one integer T (1 β€ T β€ 50000) β the number of queries. Each query contains description of the set of segments. Queries are independent.
First line of each query contains single integer n (2 β€ n β€ 10^5) β number of segments. It is guaranteed that β{n} over all queries does not exceed 10^5.
The next n lines contains two integers l_i, r_i per line (1 β€ l_i β€ r_i β€ 2 β
10^5) β the i-th segment.
Output
For each query print n integers t_1, t_2, ..., t_n (t_i β \{1, 2\}) β for each segment (in the same order as in the input) t_i equals 1 if the i-th segment will belongs to the first group and 2 otherwise.
If there are multiple answers, you can print any of them. If there is no answer, print -1.
Example
Input
3
2
5 5
2 3
3
3 5
2 3
2 3
3
3 3
4 4
5 5
Output
2 1
-1
1 1 2
Note
In the first query the first and the second segments should be in different groups, but exact numbers don't matter.
In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is -1.
In the third query we can distribute segments in any way that makes groups non-empty, so any answer of 6 possible is correct. | instruction | 0 | 29,155 | 12 | 58,310 |
Tags: sortings
Correct Solution:
```
for j in range(int(input())):
n = int(input())
s = []
ind = [int(0)]*n
for i in range(n):
s.append(tuple(list(map(int, input().split()))+[i]))
s.sort()
mx = max(s[0][0], s[0][1]); ind[s[0][2]] = 1
for i in range(1, len(s)):
if mx < min(s[i][0], s[i][1]):
break;
mx = max(s[i][0], s[i][1], mx)
ind[s[i][2]] = 1
if sum(ind) == n: print(-1, end = ' ')
else:
for i in ind: print(i+1, end = ' ')
print()
``` | output | 1 | 29,155 | 12 | 58,311 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n segments [l_i, r_i] for 1 β€ i β€ n. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group.
To optimize testing process you will be given multitest.
Input
The first line contains one integer T (1 β€ T β€ 50000) β the number of queries. Each query contains description of the set of segments. Queries are independent.
First line of each query contains single integer n (2 β€ n β€ 10^5) β number of segments. It is guaranteed that β{n} over all queries does not exceed 10^5.
The next n lines contains two integers l_i, r_i per line (1 β€ l_i β€ r_i β€ 2 β
10^5) β the i-th segment.
Output
For each query print n integers t_1, t_2, ..., t_n (t_i β \{1, 2\}) β for each segment (in the same order as in the input) t_i equals 1 if the i-th segment will belongs to the first group and 2 otherwise.
If there are multiple answers, you can print any of them. If there is no answer, print -1.
Example
Input
3
2
5 5
2 3
3
3 5
2 3
2 3
3
3 3
4 4
5 5
Output
2 1
-1
1 1 2
Note
In the first query the first and the second segments should be in different groups, but exact numbers don't matter.
In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is -1.
In the third query we can distribute segments in any way that makes groups non-empty, so any answer of 6 possible is correct. | instruction | 0 | 29,156 | 12 | 58,312 |
Tags: sortings
Correct Solution:
```
''' Thruth can only be found at one place - THE CODE '''
''' Copyright 2018, SATYAM KUMAR'''
from sys import stdin, stdout
import cProfile, math
from collections import Counter
from bisect import bisect_left,bisect,bisect_right
import itertools
from copy import deepcopy
from fractions import Fraction
import sys, threading
sys.setrecursionlimit(10**6) # max depth of recursion
threading.stack_size(2**27) # new thread will get stack of such size
printHeap = str()
memory_constrained = False
P = 10**9+7
import sys
sys.setrecursionlimit(10000000)
class Operation:
def __init__(self, name, function, function_on_equal, neutral_value=0):
self.name = name
self.f = function
self.f_on_equal = function_on_equal
def add_multiple(x, count):
return x * count
def min_multiple(x, count):
return x
def max_multiple(x, count):
return x
sum_operation = Operation("sum", sum, add_multiple, 0)
min_operation = Operation("min", min, min_multiple, 1e9)
max_operation = Operation("max", max, max_multiple, -1e9)
class SegmentTree:
def __init__(self,
array,
operations=[sum_operation, min_operation, max_operation]):
self.array = array
if type(operations) != list:
raise TypeError("operations must be a list")
self.operations = {}
for op in operations:
self.operations[op.name] = op
self.root = SegmentTreeNode(0, len(array) - 1, self)
def query(self, start, end, operation_name):
if self.operations.get(operation_name) == None:
raise Exception("This operation is not available")
return self.root._query(start, end, self.operations[operation_name])
def summary(self):
return self.root.values
def update(self, position, value):
self.root._update(position, value)
def update_range(self, start, end, value):
self.root._update_range(start, end, value)
def __repr__(self):
return self.root.__repr__()
class SegmentTreeNode:
def __init__(self, start, end, segment_tree):
self.range = (start, end)
self.parent_tree = segment_tree
self.range_value = None
self.values = {}
self.left = None
self.right = None
if start == end:
self._sync()
return
self.left = SegmentTreeNode(start, start + (end - start) // 2,
segment_tree)
self.right = SegmentTreeNode(start + (end - start) // 2 + 1, end,
segment_tree)
self._sync()
def _query(self, start, end, operation):
if end < self.range[0] or start > self.range[1]:
return None
if start <= self.range[0] and self.range[1] <= end:
return self.values[operation.name]
self._push()
left_res = self.left._query(start, end,
operation) if self.left else None
right_res = self.right._query(start, end,
operation) if self.right else None
if left_res is None:
return right_res
if right_res is None:
return left_res
return operation.f([left_res, right_res])
def _update(self, position, value):
if position < self.range[0] or position > self.range[1]:
return
if position == self.range[0] and self.range[1] == position:
self.parent_tree.array[position] = value
self._sync()
return
self._push()
self.left._update(position, value)
self.right._update(position, value)
self._sync()
def _update_range(self, start, end, value):
if end < self.range[0] or start > self.range[1]:
return
if start <= self.range[0] and self.range[1] <= end:
self.range_value = value
self._sync()
return
self._push()
self.left._update_range(start, end, value)
self.right._update_range(start, end, value)
self._sync()
def _sync(self):
if self.range[0] == self.range[1]:
for op in self.parent_tree.operations.values():
current_value = self.parent_tree.array[self.range[0]]
if self.range_value is not None:
current_value = self.range_value
self.values[op.name] = op.f([current_value])
else:
for op in self.parent_tree.operations.values():
result = op.f(
[self.left.values[op.name], self.right.values[op.name]])
if self.range_value is not None:
bound_length = self.range[1] - self.range[0] + 1
result = op.f_on_equal(self.range_value, bound_length)
self.values[op.name] = result
def _push(self):
if self.range_value is None:
return
if self.left:
self.left.range_value = self.range_value
self.right.range_value = self.range_value
self.left._sync()
self.right._sync()
self.range_value = None
def __repr__(self):
ans = "({}, {}): {}\n".format(self.range[0], self.range[1],
self.values)
if self.left:
ans += self.left.__repr__()
if self.right:
ans += self.right.__repr__()
return ans
def display(string_to_print):
stdout.write(str(string_to_print) + "\n")
def primeFactors(n): #n**0.5 complex
factors = dict()
for i in range(2,math.ceil(math.sqrt(n))+1):
while n % i== 0:
if i in factors:
factors[i]+=1
else: factors[i]=1
n = n // i
if n>2:
factors[n]=1
return (factors)
def isprime(n):
"""Returns True if n is prime."""
if n < 4:
return True
if n % 2 == 0:
return False
if n % 3 == 0:
return False
i = 5
w = 2
while i * i <= n:
if n % i == 0:
return False
i += w
w = 6 - w
return True
def test_print(*args):
if testingMode:
print(args)
def display_list(list1, sep=" "):
stdout.write(sep.join(map(str, list1)) + "\n")
def get_int():
return int(stdin.readline().strip())
def get_tuple():
return map(int, stdin.readline().split())
def get_list():
return list(map(int, stdin.readline().split()))
memory = dict()
def clear_cache():
global memory
memory = dict()
def cached_fn(fn, *args):
global memory
if args in memory:
return memory[args]
else:
result = fn(*args)
memory[args] = result
return result
# -------------------------------------------------------------- MAIN PROGRAM
TestCases = True
testingMode = False
optimiseForReccursion = False #Can not be used clubbed with TestCases
def is_Valid(a,b):
#print(a,b)
x = min(a,b)
y = max(a,b)
diff = (y-x)
y = y-2*diff
x = x-1*diff
if x>=0 and y>=0 and x%3==0 and y%3==0:
return True
return False
def main():
n = get_int()
mat = [list(map(int,input().split())) for _ in range(n)]
org_indexes = [i[0] for i in sorted(enumerate(mat), key=lambda x:x[1])]
mat.sort()
res = [2 for _ in range(n)]
end = mat[0][1]
x=0
#print(mat,org_indexes)
for li in mat:
if end<li[0]:
#print(org_indexes[0:x],end)
for i in org_indexes[0:x]:
res[i] = 1
display_list(res)
return
end = max(end,li[1])
x += 1
print(-1)
# --------------------------------------------------------------------- END
if TestCases:
for _ in range(get_int()):
cProfile.run('main()') if testingMode else main()
else: (cProfile.run('main()') if testingMode else main()) if not optimiseForReccursion else threading.Thread(target=main).start()
``` | output | 1 | 29,156 | 12 | 58,313 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n segments [l_i, r_i] for 1 β€ i β€ n. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group.
To optimize testing process you will be given multitest.
Input
The first line contains one integer T (1 β€ T β€ 50000) β the number of queries. Each query contains description of the set of segments. Queries are independent.
First line of each query contains single integer n (2 β€ n β€ 10^5) β number of segments. It is guaranteed that β{n} over all queries does not exceed 10^5.
The next n lines contains two integers l_i, r_i per line (1 β€ l_i β€ r_i β€ 2 β
10^5) β the i-th segment.
Output
For each query print n integers t_1, t_2, ..., t_n (t_i β \{1, 2\}) β for each segment (in the same order as in the input) t_i equals 1 if the i-th segment will belongs to the first group and 2 otherwise.
If there are multiple answers, you can print any of them. If there is no answer, print -1.
Example
Input
3
2
5 5
2 3
3
3 5
2 3
2 3
3
3 3
4 4
5 5
Output
2 1
-1
1 1 2
Note
In the first query the first and the second segments should be in different groups, but exact numbers don't matter.
In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is -1.
In the third query we can distribute segments in any way that makes groups non-empty, so any answer of 6 possible is correct. | instruction | 0 | 29,157 | 12 | 58,314 |
Tags: sortings
Correct Solution:
```
#------------------------------warmup----------------------------
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")
#-------------------game starts now-----------------------------------------------------
def sort_list(list1, list2):
zipped_pairs = zip(list2, list1)
z = [x for _, x in sorted(zipped_pairs)]
return z
for i in range(int(input())):
n=int(input())
a=[0]*n
b=[0]*n
c=[0]*n
for j in range(n):
a[j],b[j]=map(int,input().split())
c[j]=j
b=sort_list(b,a)
c=sort_list(c,a)
a.sort()
#print(a,b)
l=[0]*n
d=0
k=0
for j in range(n):
if d==0:
if a[j]>k:
if j!=0:
l[c[j]]=2
d=1
k=max(k,b[j])
else:
l[c[j]]=1
k=max(k,b[j])
else:
l[c[j]]=1
k=max(k,b[j])
else:
l[c[j]]=2
k=max(k,b[j])
if d==0:
print(-1)
else:
print(*l,sep=" ")
``` | output | 1 | 29,157 | 12 | 58,315 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n segments [l_i, r_i] for 1 β€ i β€ n. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group.
To optimize testing process you will be given multitest.
Input
The first line contains one integer T (1 β€ T β€ 50000) β the number of queries. Each query contains description of the set of segments. Queries are independent.
First line of each query contains single integer n (2 β€ n β€ 10^5) β number of segments. It is guaranteed that β{n} over all queries does not exceed 10^5.
The next n lines contains two integers l_i, r_i per line (1 β€ l_i β€ r_i β€ 2 β
10^5) β the i-th segment.
Output
For each query print n integers t_1, t_2, ..., t_n (t_i β \{1, 2\}) β for each segment (in the same order as in the input) t_i equals 1 if the i-th segment will belongs to the first group and 2 otherwise.
If there are multiple answers, you can print any of them. If there is no answer, print -1.
Example
Input
3
2
5 5
2 3
3
3 5
2 3
2 3
3
3 3
4 4
5 5
Output
2 1
-1
1 1 2
Note
In the first query the first and the second segments should be in different groups, but exact numbers don't matter.
In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is -1.
In the third query we can distribute segments in any way that makes groups non-empty, so any answer of 6 possible is correct. | instruction | 0 | 29,158 | 12 | 58,316 |
Tags: sortings
Correct Solution:
```
def do_intersect( a, b ): return min(a[1],b[1]) >= max( a[0],b[0])
for _ in range(int(input())):
n = int(input())
inps = [ tuple(map(int,input().split())) for _ in range(n) ]
org = [ pair for pair in inps ]
inps.sort()
seta = inps[0]
setb = (float('inf'), float('-inf'))
for i in range(1, n):
merged=False
if do_intersect( seta, inps[i] ):
seta = min( inps[i][0], seta[0] ), max(inps[i][1], seta[1])
else:
setb = min( inps[i][0], setb[0] ), max(inps[i][1], setb[1])
if setb == (float('inf'), float('-inf')):
print(-1)
else:
ans =[]
for inp in org:
if do_intersect( inp, seta ):
ans.append(1)
else:
ans.append(2)
print(*ans)
``` | output | 1 | 29,158 | 12 | 58,317 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a_1, a_2, ... , a_n. Array is good if for each pair of indexes i < j the condition j - a_j β i - a_i holds. Can you shuffle this array so that it becomes good? To shuffle an array means to reorder its elements arbitrarily (leaving the initial order is also an option).
For example, if a = [1, 1, 3, 5], then shuffled arrays [1, 3, 5, 1], [3, 5, 1, 1] and [5, 3, 1, 1] are good, but shuffled arrays [3, 1, 5, 1], [1, 1, 3, 5] and [1, 1, 5, 3] aren't.
It's guaranteed that it's always possible to shuffle an array to meet this condition.
Input
The first line contains one integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 100) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ 100).
Output
For each test case print the shuffled version of the array a which is good.
Example
Input
3
1
7
4
1 1 3 5
6
3 2 1 5 6 4
Output
7
1 5 1 3
2 4 6 1 3 5 | instruction | 0 | 29,251 | 12 | 58,502 |
Tags: constructive algorithms, sortings
Correct Solution:
```
n = int(input())
for i in range(n):
n1 = int(input())
l = list(map(int,input().split()))
l.sort(reverse=True)
print(*l)
``` | output | 1 | 29,251 | 12 | 58,503 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a_1, a_2, ... , a_n. Array is good if for each pair of indexes i < j the condition j - a_j β i - a_i holds. Can you shuffle this array so that it becomes good? To shuffle an array means to reorder its elements arbitrarily (leaving the initial order is also an option).
For example, if a = [1, 1, 3, 5], then shuffled arrays [1, 3, 5, 1], [3, 5, 1, 1] and [5, 3, 1, 1] are good, but shuffled arrays [3, 1, 5, 1], [1, 1, 3, 5] and [1, 1, 5, 3] aren't.
It's guaranteed that it's always possible to shuffle an array to meet this condition.
Input
The first line contains one integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 100) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ 100).
Output
For each test case print the shuffled version of the array a which is good.
Example
Input
3
1
7
4
1 1 3 5
6
3 2 1 5 6 4
Output
7
1 5 1 3
2 4 6 1 3 5 | instruction | 0 | 29,252 | 12 | 58,504 |
Tags: constructive algorithms, sortings
Correct Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
a = sorted([int(x) for x in input().split()])
a.reverse()
print(*a)
``` | output | 1 | 29,252 | 12 | 58,505 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a_1, a_2, ... , a_n. Array is good if for each pair of indexes i < j the condition j - a_j β i - a_i holds. Can you shuffle this array so that it becomes good? To shuffle an array means to reorder its elements arbitrarily (leaving the initial order is also an option).
For example, if a = [1, 1, 3, 5], then shuffled arrays [1, 3, 5, 1], [3, 5, 1, 1] and [5, 3, 1, 1] are good, but shuffled arrays [3, 1, 5, 1], [1, 1, 3, 5] and [1, 1, 5, 3] aren't.
It's guaranteed that it's always possible to shuffle an array to meet this condition.
Input
The first line contains one integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 100) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ 100).
Output
For each test case print the shuffled version of the array a which is good.
Example
Input
3
1
7
4
1 1 3 5
6
3 2 1 5 6 4
Output
7
1 5 1 3
2 4 6 1 3 5 | instruction | 0 | 29,253 | 12 | 58,506 |
Tags: constructive algorithms, sortings
Correct Solution:
```
t = int(input())
for q in range(t):
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
a.reverse()
for i in range(n):
print(a[i], end=" ")
print()
``` | output | 1 | 29,253 | 12 | 58,507 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a_1, a_2, ... , a_n. Array is good if for each pair of indexes i < j the condition j - a_j β i - a_i holds. Can you shuffle this array so that it becomes good? To shuffle an array means to reorder its elements arbitrarily (leaving the initial order is also an option).
For example, if a = [1, 1, 3, 5], then shuffled arrays [1, 3, 5, 1], [3, 5, 1, 1] and [5, 3, 1, 1] are good, but shuffled arrays [3, 1, 5, 1], [1, 1, 3, 5] and [1, 1, 5, 3] aren't.
It's guaranteed that it's always possible to shuffle an array to meet this condition.
Input
The first line contains one integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 100) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ 100).
Output
For each test case print the shuffled version of the array a which is good.
Example
Input
3
1
7
4
1 1 3 5
6
3 2 1 5 6 4
Output
7
1 5 1 3
2 4 6 1 3 5 | instruction | 0 | 29,254 | 12 | 58,508 |
Tags: constructive algorithms, sortings
Correct Solution:
```
from sys import stdin
def func():
return
for _ in range(int(stdin.readline())):
n = int(stdin.readline())
#n, m = map(int,stdin.readline().split())
arr = sorted(list(map(int,stdin.readline().split())), reverse = True)
print(*arr)
``` | output | 1 | 29,254 | 12 | 58,509 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a_1, a_2, ... , a_n. Array is good if for each pair of indexes i < j the condition j - a_j β i - a_i holds. Can you shuffle this array so that it becomes good? To shuffle an array means to reorder its elements arbitrarily (leaving the initial order is also an option).
For example, if a = [1, 1, 3, 5], then shuffled arrays [1, 3, 5, 1], [3, 5, 1, 1] and [5, 3, 1, 1] are good, but shuffled arrays [3, 1, 5, 1], [1, 1, 3, 5] and [1, 1, 5, 3] aren't.
It's guaranteed that it's always possible to shuffle an array to meet this condition.
Input
The first line contains one integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 100) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ 100).
Output
For each test case print the shuffled version of the array a which is good.
Example
Input
3
1
7
4
1 1 3 5
6
3 2 1 5 6 4
Output
7
1 5 1 3
2 4 6 1 3 5 | instruction | 0 | 29,255 | 12 | 58,510 |
Tags: constructive algorithms, sortings
Correct Solution:
```
def solve():
n = int(input())
a = list(map(int, input().split()))
a.sort()
a.reverse()
for i in range(0, n):
print(a[i], end = " ")
print("")
t = int(input())
for _ in range(0, t):
solve()
``` | output | 1 | 29,255 | 12 | 58,511 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a_1, a_2, ... , a_n. Array is good if for each pair of indexes i < j the condition j - a_j β i - a_i holds. Can you shuffle this array so that it becomes good? To shuffle an array means to reorder its elements arbitrarily (leaving the initial order is also an option).
For example, if a = [1, 1, 3, 5], then shuffled arrays [1, 3, 5, 1], [3, 5, 1, 1] and [5, 3, 1, 1] are good, but shuffled arrays [3, 1, 5, 1], [1, 1, 3, 5] and [1, 1, 5, 3] aren't.
It's guaranteed that it's always possible to shuffle an array to meet this condition.
Input
The first line contains one integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 100) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ 100).
Output
For each test case print the shuffled version of the array a which is good.
Example
Input
3
1
7
4
1 1 3 5
6
3 2 1 5 6 4
Output
7
1 5 1 3
2 4 6 1 3 5 | instruction | 0 | 29,256 | 12 | 58,512 |
Tags: constructive algorithms, sortings
Correct Solution:
```
from random import shuffle
def solve(n, a):
if n == 1:
return a[0]
a = sorted(a, reverse=True)
for i in range(n-1):
for j in range(i+1, n):
if j - a[j] == i - a[i]:
shuffling(a)
return solve(n, a)
s = ''
for h in a:
s += str(h) + ' '
return s
def shuffling(a):
return shuffle(a)
def main():
t = int(input())
for _ in range(t):
n = int(input())
a = [int(i) for i in input().split()]
print(solve(n, a))
main()
``` | output | 1 | 29,256 | 12 | 58,513 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a_1, a_2, ... , a_n. Array is good if for each pair of indexes i < j the condition j - a_j β i - a_i holds. Can you shuffle this array so that it becomes good? To shuffle an array means to reorder its elements arbitrarily (leaving the initial order is also an option).
For example, if a = [1, 1, 3, 5], then shuffled arrays [1, 3, 5, 1], [3, 5, 1, 1] and [5, 3, 1, 1] are good, but shuffled arrays [3, 1, 5, 1], [1, 1, 3, 5] and [1, 1, 5, 3] aren't.
It's guaranteed that it's always possible to shuffle an array to meet this condition.
Input
The first line contains one integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 100) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ 100).
Output
For each test case print the shuffled version of the array a which is good.
Example
Input
3
1
7
4
1 1 3 5
6
3 2 1 5 6 4
Output
7
1 5 1 3
2 4 6 1 3 5 | instruction | 0 | 29,257 | 12 | 58,514 |
Tags: constructive algorithms, sortings
Correct Solution:
```
for i in range(int(input())):
n=int(input())
t=list(map(int,input().split()))
p=[]
for j in range(n):
for k in range(1,n+1):
if k-1-t[j] not in p:
p.append([k-1-t[j],t[j]])
break
p.sort()
for s in p:
print(s[1],end=' ')
``` | output | 1 | 29,257 | 12 | 58,515 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a_1, a_2, ... , a_n. Array is good if for each pair of indexes i < j the condition j - a_j β i - a_i holds. Can you shuffle this array so that it becomes good? To shuffle an array means to reorder its elements arbitrarily (leaving the initial order is also an option).
For example, if a = [1, 1, 3, 5], then shuffled arrays [1, 3, 5, 1], [3, 5, 1, 1] and [5, 3, 1, 1] are good, but shuffled arrays [3, 1, 5, 1], [1, 1, 3, 5] and [1, 1, 5, 3] aren't.
It's guaranteed that it's always possible to shuffle an array to meet this condition.
Input
The first line contains one integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 100) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ 100).
Output
For each test case print the shuffled version of the array a which is good.
Example
Input
3
1
7
4
1 1 3 5
6
3 2 1 5 6 4
Output
7
1 5 1 3
2 4 6 1 3 5 | instruction | 0 | 29,258 | 12 | 58,516 |
Tags: constructive algorithms, sortings
Correct Solution:
```
t=int(input())
for _ in range(t):
n=int(input())
a=list(map(int,input().split()))
a.sort()
a=a[::-1]
print(*a)
``` | output | 1 | 29,258 | 12 | 58,517 |
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. Array is good if for each pair of indexes i < j the condition j - a_j β i - a_i holds. Can you shuffle this array so that it becomes good? To shuffle an array means to reorder its elements arbitrarily (leaving the initial order is also an option).
For example, if a = [1, 1, 3, 5], then shuffled arrays [1, 3, 5, 1], [3, 5, 1, 1] and [5, 3, 1, 1] are good, but shuffled arrays [3, 1, 5, 1], [1, 1, 3, 5] and [1, 1, 5, 3] aren't.
It's guaranteed that it's always possible to shuffle an array to meet this condition.
Input
The first line contains one integer t (1 β€ t β€ 100) β the number of test cases.
The first line of each test case contains one integer n (1 β€ n β€ 100) β the length of array a.
The second line of each test case contains n integers a_1, a_2, ... , a_n (1 β€ a_i β€ 100).
Output
For each test case print the shuffled version of the array a which is good.
Example
Input
3
1
7
4
1 1 3 5
6
3 2 1 5 6 4
Output
7
1 5 1 3
2 4 6 1 3 5
Submitted Solution:
```
t = int(input())
m = []
for i in range(t):
input()
m.append(list(map(int,input().split())))
for i in range(t):
mi = sorted(m[i], reverse = True)
for j in mi:
print(j, end = ' ')
print()
``` | instruction | 0 | 29,259 | 12 | 58,518 |
Yes | output | 1 | 29,259 | 12 | 58,519 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.