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 a correct Python 3 solution for this coding contest problem. For given a sequence $A = \\{a_0, a_1, ..., a_{n-1}\\}$, print the previous permutation and the next permutation in lexicographic order. Constraints * $1 \leq n \leq 9$ * $a_i$ consist of $1, 2, ..., n$ Input A sequence is given in the following format. $n$ $a_0 \; a_1 \; ... \; a_{n-1}$ Output Print the previous permutation, the given sequence and the next permutation in the 1st, 2nd and 3rd lines respectively. Separate adjacency elements by a space character. Note that if there is no permutation, print nothing in the corresponding line. Examples Input 3 2 1 3 Output 1 3 2 2 1 3 2 3 1 Input 3 3 2 1 Output 3 1 2 3 2 1
instruction
0
84,999
12
169,998
"Correct Solution: ``` import itertools if __name__ == '__main__': n = int(input()) now = tuple(map(int,input().split())) seq = [i+1 for i in range(n)] seq2 = list(itertools.permutations(seq)) ind = 0 for j,k in enumerate(seq2): if k == now: ind = j if ind == 0: print(*seq2[ind]) if len(seq2) != 1: print(*seq2[ind+1]) elif ind+1 == len(seq2): print(*seq2[ind-1]) print(*seq2[ind]) else: print(*seq2[ind-1]) print(*seq2[ind]) print(*seq2[ind+1]) ```
output
1
84,999
12
169,999
Provide a correct Python 3 solution for this coding contest problem. For given a sequence $A = \\{a_0, a_1, ..., a_{n-1}\\}$, print the previous permutation and the next permutation in lexicographic order. Constraints * $1 \leq n \leq 9$ * $a_i$ consist of $1, 2, ..., n$ Input A sequence is given in the following format. $n$ $a_0 \; a_1 \; ... \; a_{n-1}$ Output Print the previous permutation, the given sequence and the next permutation in the 1st, 2nd and 3rd lines respectively. Separate adjacency elements by a space character. Note that if there is no permutation, print nothing in the corresponding line. Examples Input 3 2 1 3 Output 1 3 2 2 1 3 2 3 1 Input 3 3 2 1 Output 3 1 2 3 2 1
instruction
0
85,000
12
170,000
"Correct Solution: ``` # AOJ ITP2_5_C: Permutation # Python3 2018.6.24 bal4u def next_permutation(a): # if list has less than two elements, has no next permutation. if len(a) < 2: return False # step 1: find max i for a[i] > a[i+1] i = len(a)-2 while i >= 0 and a[i] >= a[i+1]: i -= 1 if i < 0: return False j = i + 1 # step 2: find max k for a[k] > a[i] k = len(a) - 1 while a[i] >= a[k]: k -= 1 # step 3: swap a[i] and a[k] (a[i], a[k]) = (a[k], a[i]) # step 4: reverse a[j:] a[j:] = a[:j-1:-1] return True def prev_permutation(a): # if list has less than two elements, has no prev permutation. if len(a) < 2: return False # step 1: find max i for a[i] < a[i+1] i = len(a)-2 while i >= 0 and a[i] <= a[i+1]: i -= 1 if i < 0: return False j = i + 1 # step 2: find max k for a[k] < a[i] k = len(a) - 1 while a[i] <= a[k]: k -= 1 # step 3: swap a[i] and a[k] (a[i], a[k]) = (a[k], a[i]) # step 4: reverse a[j:] a[j:] = a[:j-1:-1] return True n = int(input()) a = list(map(int, input().split())) n, p = list(a), list(a) if prev_permutation(p): print(*p) print(*a) if next_permutation(n): print(*n) ```
output
1
85,000
12
170,001
Provide a correct Python 3 solution for this coding contest problem. For given a sequence $A = \\{a_0, a_1, ..., a_{n-1}\\}$, print the previous permutation and the next permutation in lexicographic order. Constraints * $1 \leq n \leq 9$ * $a_i$ consist of $1, 2, ..., n$ Input A sequence is given in the following format. $n$ $a_0 \; a_1 \; ... \; a_{n-1}$ Output Print the previous permutation, the given sequence and the next permutation in the 1st, 2nd and 3rd lines respectively. Separate adjacency elements by a space character. Note that if there is no permutation, print nothing in the corresponding line. Examples Input 3 2 1 3 Output 1 3 2 2 1 3 2 3 1 Input 3 3 2 1 Output 3 1 2 3 2 1
instruction
0
85,001
12
170,002
"Correct Solution: ``` import operator def permutations(li): """Returns a list of previous, current, and next permutations of li. >>> permutations([1, 2]) [[1, 2], [2, 1]] >>> permutations([1, 3, 2]) [[1, 2, 3], [1, 3, 2], [2, 1, 3]] """ def perm(op): def func(xs): i = len(xs) - 1 while i > 0 and op(xs[i-1], xs[i]): i -= 1 if i > 0: i -= 1 j = i + 1 while j < len(xs) and op(xs[j], xs[i]): j += 1 xs[i], xs[j-1] = xs[j-1], xs[i] return xs[:i+1] + list(reversed(xs[i+1:])) else: return None return func prev_perm = perm(operator.lt) next_perm = perm(operator.gt) ps = [] pp = prev_perm(li[:]) if pp is not None: ps.append(pp) ps.append(li[:]) np = next_perm(li[:]) if np is not None: ps.append(np) return ps def run(): n = int(input()) li = [int(x) for x in input().split()] assert(n == len(li)) for ps in permutations(li): print(" ".join([str(x) for x in ps])) if __name__ == '__main__': run() ```
output
1
85,001
12
170,003
Provide a correct Python 3 solution for this coding contest problem. For given a sequence $A = \\{a_0, a_1, ..., a_{n-1}\\}$, print the previous permutation and the next permutation in lexicographic order. Constraints * $1 \leq n \leq 9$ * $a_i$ consist of $1, 2, ..., n$ Input A sequence is given in the following format. $n$ $a_0 \; a_1 \; ... \; a_{n-1}$ Output Print the previous permutation, the given sequence and the next permutation in the 1st, 2nd and 3rd lines respectively. Separate adjacency elements by a space character. Note that if there is no permutation, print nothing in the corresponding line. Examples Input 3 2 1 3 Output 1 3 2 2 1 3 2 3 1 Input 3 3 2 1 Output 3 1 2 3 2 1
instruction
0
85,002
12
170,004
"Correct Solution: ``` from itertools import permutations # INPUT n = int(input()) A = tuple(input().split()) # PROCESS, OUTPUT str_num = "" for num in range(1, n + 1): str_num += str(num) flag_break = False list_num_previous = None for list_num in permutations(str_num): if flag_break: # next print(*list_num) break if list_num == A: if list_num_previous != None: # previous print(*list_num_previous) # now print(*list_num) flag_break = True list_num_previous = list_num ```
output
1
85,002
12
170,005
Provide a correct Python 3 solution for this coding contest problem. For given a sequence $A = \\{a_0, a_1, ..., a_{n-1}\\}$, print the previous permutation and the next permutation in lexicographic order. Constraints * $1 \leq n \leq 9$ * $a_i$ consist of $1, 2, ..., n$ Input A sequence is given in the following format. $n$ $a_0 \; a_1 \; ... \; a_{n-1}$ Output Print the previous permutation, the given sequence and the next permutation in the 1st, 2nd and 3rd lines respectively. Separate adjacency elements by a space character. Note that if there is no permutation, print nothing in the corresponding line. Examples Input 3 2 1 3 Output 1 3 2 2 1 3 2 3 1 Input 3 3 2 1 Output 3 1 2 3 2 1
instruction
0
85,003
12
170,006
"Correct Solution: ``` def resolve(): import itertools n = int(input()) target = tuple(int(i) for i in input().split()) before = None found = False for a in sorted(itertools.permutations(target)): if found: print(*a) return elif a == target: if before is not None: print(*before) print(*target) found = True else: before = a resolve() ```
output
1
85,003
12
170,007
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For given a sequence $A = \\{a_0, a_1, ..., a_{n-1}\\}$, print the previous permutation and the next permutation in lexicographic order. Constraints * $1 \leq n \leq 9$ * $a_i$ consist of $1, 2, ..., n$ Input A sequence is given in the following format. $n$ $a_0 \; a_1 \; ... \; a_{n-1}$ Output Print the previous permutation, the given sequence and the next permutation in the 1st, 2nd and 3rd lines respectively. Separate adjacency elements by a space character. Note that if there is no permutation, print nothing in the corresponding line. Examples Input 3 2 1 3 Output 1 3 2 2 1 3 2 3 1 Input 3 3 2 1 Output 3 1 2 3 2 1 Submitted Solution: ``` # http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_5_C&lang=jp # Enumeration : python3 # 2018.12.01 yonezawa import sys input = sys.stdin.readline from collections import deque class enumeration: def __init__(self,n): self.n = n self.depth = 0 self.wnum = 0 self.nq = [] self.bits = [ pow(2,i) for i in range(n+1) ] self.mk_permutation(0,0,0) def mk_permutation(self,depth,bflag,wnum): if self.n == depth: self.nq.append(wnum) return for i in range(1,self.n+1): if bflag & self.bits[i-1] != 0: continue self.mk_permutation(depth + 1,bflag + self.bits[i-1],wnum*10+i) def printList(self): l = self.nq self.nq.sort() for i in l: self.printInttoStr(i) def printInttoStr(self,i): c = "" for j in str(i): c += j + " " print(c.strip()) def solve(self,tc): pos = self.nq.index(tc) if (pos != 0 ): self.printInttoStr(self.nq[pos-1]) self.printInttoStr(tc) if (pos + 1 != len(self.nq) ): self.printInttoStr(self.nq[pos+1]) def main(): n = int(input()) tc = 0 for i in list(map(int,input().split())): tc = tc * 10 + i e = enumeration(n) e.solve(tc) if __name__ == '__main__': main() ```
instruction
0
85,005
12
170,010
Yes
output
1
85,005
12
170,011
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For given a sequence $A = \\{a_0, a_1, ..., a_{n-1}\\}$, print the previous permutation and the next permutation in lexicographic order. Constraints * $1 \leq n \leq 9$ * $a_i$ consist of $1, 2, ..., n$ Input A sequence is given in the following format. $n$ $a_0 \; a_1 \; ... \; a_{n-1}$ Output Print the previous permutation, the given sequence and the next permutation in the 1st, 2nd and 3rd lines respectively. Separate adjacency elements by a space character. Note that if there is no permutation, print nothing in the corresponding line. Examples Input 3 2 1 3 Output 1 3 2 2 1 3 2 3 1 Input 3 3 2 1 Output 3 1 2 3 2 1 Submitted Solution: ``` def next_permutation(a): # if list has less than two elements, has no next permutation if len(a) < 2: return False # step1 : find max i for a[i] > a[i+1] i = len(a) - 2 while i >= 0 and a[i] >= a[i+1]: i -= 1 if i < 0: return False j = i + 1 # step2 : find max k for a[k] > a[i] k = len(a) - 1 while a[i] >= a[k]: k -= 1 # step3 : swap a[i] and a[k] (a[i], a[k]) = (a[k], a[i]) # step4 : reverse a[j:] a[j:] = a[:j-1:-1] return True def prev_permutation(a): # if list has less than two elements, has no prev permutation if len(a) < 2: return False # step1 : find max i for a[i] < a[i+1] i = len(a) - 2 while i >= 0 and a[i] <= a[i+1]: i -= 1 if i < 0: return False j = i + 1 # step2 : find max k for a[k] < a[i] k = len(a) - 1 while a[i] <= a[k]: k -= 1 # step3 : swap a[i] and a[k] (a[i], a[k]) = (a[k], a[i]) # step4 : reverse a[j:] a[j:] = a[:j-1:-1] return True n = int(input()) a = list(map(int, input().split())) n, p = list(a), list(a) if prev_permutation(p): print(*p) print(*a) if next_permutation(n): print(*n) ```
instruction
0
85,007
12
170,014
Yes
output
1
85,007
12
170,015
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a_1, a_2, ..., a_n consisting of n integers. You can choose any non-negative integer D (i.e. D β‰₯ 0), and for each a_i you can: * add D (only once), i. e. perform a_i := a_i + D, or * subtract D (only once), i. e. perform a_i := a_i - D, or * leave the value of a_i unchanged. It is possible that after an operation the value a_i becomes negative. Your goal is to choose such minimum non-negative integer D and perform changes in such a way, that all a_i are equal (i.e. a_1=a_2=...=a_n). Print the required D or, if it is impossible to choose such value D, print -1. For example, for array [2, 8] the value D=3 is minimum possible because you can obtain the array [5, 5] if you will add D to 2 and subtract D from 8. And for array [1, 4, 7, 7] the value D=3 is also minimum possible. You can add it to 1 and subtract it from 7 and obtain the array [4, 4, 4, 4]. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the number of elements in a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 100) β€” the sequence a. Output Print one integer β€” the minimum non-negative integer value D such that if you add this value to some a_i, subtract this value from some a_i and leave some a_i without changes, all obtained values become equal. If it is impossible to choose such value D, print -1. Examples Input 6 1 4 4 7 4 1 Output 3 Input 5 2 2 5 2 5 Output 3 Input 4 1 3 3 7 Output -1 Input 2 2 8 Output 3
instruction
0
85,074
12
170,148
Tags: math Correct Solution: ``` n = int(input()) A = list(map(int, input().split())) if len(set(A)) == 1: print(0) exit(0) A.sort() x = A[-1] - A[0] heh = x if x % 2 == 0: heh = x // 2 for kek in heh, x: s = set() for elem in A: if elem > A[0] + kek: s.add(elem - kek) elif elem < A[0] + kek: s.add(elem + kek) if len(s) == 1 and A[0] + kek in s: print(kek) exit(0) print(-1) ```
output
1
85,074
12
170,149
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a_1, a_2, ..., a_n consisting of n integers. You can choose any non-negative integer D (i.e. D β‰₯ 0), and for each a_i you can: * add D (only once), i. e. perform a_i := a_i + D, or * subtract D (only once), i. e. perform a_i := a_i - D, or * leave the value of a_i unchanged. It is possible that after an operation the value a_i becomes negative. Your goal is to choose such minimum non-negative integer D and perform changes in such a way, that all a_i are equal (i.e. a_1=a_2=...=a_n). Print the required D or, if it is impossible to choose such value D, print -1. For example, for array [2, 8] the value D=3 is minimum possible because you can obtain the array [5, 5] if you will add D to 2 and subtract D from 8. And for array [1, 4, 7, 7] the value D=3 is also minimum possible. You can add it to 1 and subtract it from 7 and obtain the array [4, 4, 4, 4]. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the number of elements in a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 100) β€” the sequence a. Output Print one integer β€” the minimum non-negative integer value D such that if you add this value to some a_i, subtract this value from some a_i and leave some a_i without changes, all obtained values become equal. If it is impossible to choose such value D, print -1. Examples Input 6 1 4 4 7 4 1 Output 3 Input 5 2 2 5 2 5 Output 3 Input 4 1 3 3 7 Output -1 Input 2 2 8 Output 3
instruction
0
85,075
12
170,150
Tags: math Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) uniq = set(a) mi = min(uniq) ma = max(uniq) if 2 < len(uniq) <= 3: if (mi + ma) % 2 == 0 and (mi + ma) // 2 == sum(uniq - {mi, ma}): print(ma - sum(uniq - {mi, ma})) else: print(-1) elif len(uniq) == 2: if mi % 2 == ma % 2: print((ma - mi) // 2) else: print(ma - mi) elif len(uniq) == 1: print(0) else: print(-1) ```
output
1
85,075
12
170,151
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a_1, a_2, ..., a_n consisting of n integers. You can choose any non-negative integer D (i.e. D β‰₯ 0), and for each a_i you can: * add D (only once), i. e. perform a_i := a_i + D, or * subtract D (only once), i. e. perform a_i := a_i - D, or * leave the value of a_i unchanged. It is possible that after an operation the value a_i becomes negative. Your goal is to choose such minimum non-negative integer D and perform changes in such a way, that all a_i are equal (i.e. a_1=a_2=...=a_n). Print the required D or, if it is impossible to choose such value D, print -1. For example, for array [2, 8] the value D=3 is minimum possible because you can obtain the array [5, 5] if you will add D to 2 and subtract D from 8. And for array [1, 4, 7, 7] the value D=3 is also minimum possible. You can add it to 1 and subtract it from 7 and obtain the array [4, 4, 4, 4]. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the number of elements in a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 100) β€” the sequence a. Output Print one integer β€” the minimum non-negative integer value D such that if you add this value to some a_i, subtract this value from some a_i and leave some a_i without changes, all obtained values become equal. If it is impossible to choose such value D, print -1. Examples Input 6 1 4 4 7 4 1 Output 3 Input 5 2 2 5 2 5 Output 3 Input 4 1 3 3 7 Output -1 Input 2 2 8 Output 3
instruction
0
85,076
12
170,152
Tags: math Correct Solution: ``` def main(): input() s = set([int(x) for x in input().split()]) n = len(s) if n == 2 or n ==3: s = list(s) s.sort() if n == 3: if (s[2]-s[0])%2 == 0: if (((s[2]-s[0])//2 + s[0] == s[1]) and (s[2] - (s[2]-s[0])//2 == s[1])): print((s[2]-s[0])//2) else: print(-1) else: print(-1) else: if (s[1]-s[0])%2 == 0: print((s[1]-s[0])//2) else: print(s[1]-s[0]) elif n == 1: print(0) else: print(-1) if __name__ == '__main__': main() ```
output
1
85,076
12
170,153
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a_1, a_2, ..., a_n consisting of n integers. You can choose any non-negative integer D (i.e. D β‰₯ 0), and for each a_i you can: * add D (only once), i. e. perform a_i := a_i + D, or * subtract D (only once), i. e. perform a_i := a_i - D, or * leave the value of a_i unchanged. It is possible that after an operation the value a_i becomes negative. Your goal is to choose such minimum non-negative integer D and perform changes in such a way, that all a_i are equal (i.e. a_1=a_2=...=a_n). Print the required D or, if it is impossible to choose such value D, print -1. For example, for array [2, 8] the value D=3 is minimum possible because you can obtain the array [5, 5] if you will add D to 2 and subtract D from 8. And for array [1, 4, 7, 7] the value D=3 is also minimum possible. You can add it to 1 and subtract it from 7 and obtain the array [4, 4, 4, 4]. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the number of elements in a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 100) β€” the sequence a. Output Print one integer β€” the minimum non-negative integer value D such that if you add this value to some a_i, subtract this value from some a_i and leave some a_i without changes, all obtained values become equal. If it is impossible to choose such value D, print -1. Examples Input 6 1 4 4 7 4 1 Output 3 Input 5 2 2 5 2 5 Output 3 Input 4 1 3 3 7 Output -1 Input 2 2 8 Output 3
instruction
0
85,077
12
170,154
Tags: math Correct Solution: ``` # cook your dish here n=int(input()) l = list(map(int,input().split())) l=list(set(l)) l.sort() n=-1 d=-1 if(len(l)==1): d=0 elif len(l)==2: if (l[1]-l[0])%2==0: d = (l[1]-l[0])//2 n=l[0]+d else: d = l[1]-l[0] n = l[0] elif len(l)==3: if l[2]-l[1]==l[1]-l[0]: d = (l[1]-l[0]) else: d=-1 print(d) ```
output
1
85,077
12
170,155
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a_1, a_2, ..., a_n consisting of n integers. You can choose any non-negative integer D (i.e. D β‰₯ 0), and for each a_i you can: * add D (only once), i. e. perform a_i := a_i + D, or * subtract D (only once), i. e. perform a_i := a_i - D, or * leave the value of a_i unchanged. It is possible that after an operation the value a_i becomes negative. Your goal is to choose such minimum non-negative integer D and perform changes in such a way, that all a_i are equal (i.e. a_1=a_2=...=a_n). Print the required D or, if it is impossible to choose such value D, print -1. For example, for array [2, 8] the value D=3 is minimum possible because you can obtain the array [5, 5] if you will add D to 2 and subtract D from 8. And for array [1, 4, 7, 7] the value D=3 is also minimum possible. You can add it to 1 and subtract it from 7 and obtain the array [4, 4, 4, 4]. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the number of elements in a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 100) β€” the sequence a. Output Print one integer β€” the minimum non-negative integer value D such that if you add this value to some a_i, subtract this value from some a_i and leave some a_i without changes, all obtained values become equal. If it is impossible to choose such value D, print -1. Examples Input 6 1 4 4 7 4 1 Output 3 Input 5 2 2 5 2 5 Output 3 Input 4 1 3 3 7 Output -1 Input 2 2 8 Output 3
instruction
0
85,078
12
170,156
Tags: math Correct Solution: ``` #make_them_equal n = int(input()); a = list(set([int(num) for num in input().split()])) a.sort() # def equal(b): # diffArray=[] # for j in range(0,len(b)-1): # diff = b[j+1]-b[j] # if diff != 0: # diffArray.append(diff); # j += 1; # #print(diffArray) # valArray=[] # for k in range(0,len(diffArray)-1): # if diffArray[k+1]==diffArray[k]: # valArray.append(True); # elif diffArray[k+1] !=diffArray[k]: # valArray.append(False); # k +=1; # #print(valArray) # if all(valArray) == True: # return(diffArray[0]); # elif all(valArray)== False: # return('-1'); if len(a) > 3: print('-1'); elif len(a) == 1: print(0); elif len(a) == 2: diff = a[1] - a[0] if diff % 2: print(diff) else: print(int(diff / 2)) else: diff1 = a[1] - a[0] diff2 = a[2] - a[1] if diff1 != diff2: print(-1) else: print(diff1) ```
output
1
85,078
12
170,157
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a_1, a_2, ..., a_n consisting of n integers. You can choose any non-negative integer D (i.e. D β‰₯ 0), and for each a_i you can: * add D (only once), i. e. perform a_i := a_i + D, or * subtract D (only once), i. e. perform a_i := a_i - D, or * leave the value of a_i unchanged. It is possible that after an operation the value a_i becomes negative. Your goal is to choose such minimum non-negative integer D and perform changes in such a way, that all a_i are equal (i.e. a_1=a_2=...=a_n). Print the required D or, if it is impossible to choose such value D, print -1. For example, for array [2, 8] the value D=3 is minimum possible because you can obtain the array [5, 5] if you will add D to 2 and subtract D from 8. And for array [1, 4, 7, 7] the value D=3 is also minimum possible. You can add it to 1 and subtract it from 7 and obtain the array [4, 4, 4, 4]. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the number of elements in a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 100) β€” the sequence a. Output Print one integer β€” the minimum non-negative integer value D such that if you add this value to some a_i, subtract this value from some a_i and leave some a_i without changes, all obtained values become equal. If it is impossible to choose such value D, print -1. Examples Input 6 1 4 4 7 4 1 Output 3 Input 5 2 2 5 2 5 Output 3 Input 4 1 3 3 7 Output -1 Input 2 2 8 Output 3
instruction
0
85,079
12
170,158
Tags: math Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) def solve(): if len(set(a)) == 1: return 0 INF = 1 << 30 ret = INF for target in range(1, 101): delta = INF for num in a: if num > target: if delta == INF: delta = num - target elif delta != num - target: delta = INF break elif num < target: if delta == INF: delta = target - num elif delta != target - num: delta = INF break ret = min(ret, delta) return ret if ret != INF else -1 print(solve()) ```
output
1
85,079
12
170,159
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a_1, a_2, ..., a_n consisting of n integers. You can choose any non-negative integer D (i.e. D β‰₯ 0), and for each a_i you can: * add D (only once), i. e. perform a_i := a_i + D, or * subtract D (only once), i. e. perform a_i := a_i - D, or * leave the value of a_i unchanged. It is possible that after an operation the value a_i becomes negative. Your goal is to choose such minimum non-negative integer D and perform changes in such a way, that all a_i are equal (i.e. a_1=a_2=...=a_n). Print the required D or, if it is impossible to choose such value D, print -1. For example, for array [2, 8] the value D=3 is minimum possible because you can obtain the array [5, 5] if you will add D to 2 and subtract D from 8. And for array [1, 4, 7, 7] the value D=3 is also minimum possible. You can add it to 1 and subtract it from 7 and obtain the array [4, 4, 4, 4]. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the number of elements in a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 100) β€” the sequence a. Output Print one integer β€” the minimum non-negative integer value D such that if you add this value to some a_i, subtract this value from some a_i and leave some a_i without changes, all obtained values become equal. If it is impossible to choose such value D, print -1. Examples Input 6 1 4 4 7 4 1 Output 3 Input 5 2 2 5 2 5 Output 3 Input 4 1 3 3 7 Output -1 Input 2 2 8 Output 3
instruction
0
85,080
12
170,160
Tags: math Correct Solution: ``` n = int(input()) lst = [int(x) for x in input().split()] lst = list(set(lst)) lst.sort() last = lst[0] count = len(lst) - 1 # for curr in lst: # print(curr) flag = True dif1,dif2 = 0,0 if count > 2: flag = False if count == 2: dif1 = lst[1] - lst[0] dif2 = lst[2] - lst[1] if dif1 != dif2: flag = False if flag: if count == 2: print(dif1) elif count == 1: dif1 = lst[1] - lst[0] if dif1%2 != 0: print(dif1) else : print(int(dif1/2)) else: print(0) else: print(int(-1)) ```
output
1
85,080
12
170,161
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence a_1, a_2, ..., a_n consisting of n integers. You can choose any non-negative integer D (i.e. D β‰₯ 0), and for each a_i you can: * add D (only once), i. e. perform a_i := a_i + D, or * subtract D (only once), i. e. perform a_i := a_i - D, or * leave the value of a_i unchanged. It is possible that after an operation the value a_i becomes negative. Your goal is to choose such minimum non-negative integer D and perform changes in such a way, that all a_i are equal (i.e. a_1=a_2=...=a_n). Print the required D or, if it is impossible to choose such value D, print -1. For example, for array [2, 8] the value D=3 is minimum possible because you can obtain the array [5, 5] if you will add D to 2 and subtract D from 8. And for array [1, 4, 7, 7] the value D=3 is also minimum possible. You can add it to 1 and subtract it from 7 and obtain the array [4, 4, 4, 4]. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the number of elements in a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 100) β€” the sequence a. Output Print one integer β€” the minimum non-negative integer value D such that if you add this value to some a_i, subtract this value from some a_i and leave some a_i without changes, all obtained values become equal. If it is impossible to choose such value D, print -1. Examples Input 6 1 4 4 7 4 1 Output 3 Input 5 2 2 5 2 5 Output 3 Input 4 1 3 3 7 Output -1 Input 2 2 8 Output 3
instruction
0
85,081
12
170,162
Tags: math Correct Solution: ``` a = int(input()) b = list(map(int, input().split())) x = set(b) xs = list(x) xs.sort() if len(xs) == 2: if sum(xs) % 2 != 0:print(xs[1] - xs[0]) else:print(int(sum(xs)/2) - xs[0]) elif len(xs) == 3: if xs[-1] - xs[1] == xs[1] - xs[0]: print(xs[1] - xs[0]) else:print(-1) elif len(xs) == 1: print(0) else: print(-1) ```
output
1
85,081
12
170,163
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence a_1, a_2, ..., a_n consisting of n integers. You can choose any non-negative integer D (i.e. D β‰₯ 0), and for each a_i you can: * add D (only once), i. e. perform a_i := a_i + D, or * subtract D (only once), i. e. perform a_i := a_i - D, or * leave the value of a_i unchanged. It is possible that after an operation the value a_i becomes negative. Your goal is to choose such minimum non-negative integer D and perform changes in such a way, that all a_i are equal (i.e. a_1=a_2=...=a_n). Print the required D or, if it is impossible to choose such value D, print -1. For example, for array [2, 8] the value D=3 is minimum possible because you can obtain the array [5, 5] if you will add D to 2 and subtract D from 8. And for array [1, 4, 7, 7] the value D=3 is also minimum possible. You can add it to 1 and subtract it from 7 and obtain the array [4, 4, 4, 4]. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the number of elements in a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 100) β€” the sequence a. Output Print one integer β€” the minimum non-negative integer value D such that if you add this value to some a_i, subtract this value from some a_i and leave some a_i without changes, all obtained values become equal. If it is impossible to choose such value D, print -1. Examples Input 6 1 4 4 7 4 1 Output 3 Input 5 2 2 5 2 5 Output 3 Input 4 1 3 3 7 Output -1 Input 2 2 8 Output 3 Submitted Solution: ``` import math as mt import collections as cc import sys I=lambda:set(map(int,input().split())) n,=I() l=sorted(I()) if len(l)==1: print(0) elif len(l)==2: t=(abs(l[0]-l[-1])) d=abs(l[0]-l[-1]) tt=t+1 if d%2==0: tt=d//2 print(min(d,tt)) elif len(l)==3: d=l[1]-l[0] dd=l[2]-l[1] if d==dd: print(d) else: print(-1) else: print(-1) ```
instruction
0
85,083
12
170,166
Yes
output
1
85,083
12
170,167
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence a_1, a_2, ..., a_n consisting of n integers. You can choose any non-negative integer D (i.e. D β‰₯ 0), and for each a_i you can: * add D (only once), i. e. perform a_i := a_i + D, or * subtract D (only once), i. e. perform a_i := a_i - D, or * leave the value of a_i unchanged. It is possible that after an operation the value a_i becomes negative. Your goal is to choose such minimum non-negative integer D and perform changes in such a way, that all a_i are equal (i.e. a_1=a_2=...=a_n). Print the required D or, if it is impossible to choose such value D, print -1. For example, for array [2, 8] the value D=3 is minimum possible because you can obtain the array [5, 5] if you will add D to 2 and subtract D from 8. And for array [1, 4, 7, 7] the value D=3 is also minimum possible. You can add it to 1 and subtract it from 7 and obtain the array [4, 4, 4, 4]. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the number of elements in a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 100) β€” the sequence a. Output Print one integer β€” the minimum non-negative integer value D such that if you add this value to some a_i, subtract this value from some a_i and leave some a_i without changes, all obtained values become equal. If it is impossible to choose such value D, print -1. Examples Input 6 1 4 4 7 4 1 Output 3 Input 5 2 2 5 2 5 Output 3 Input 4 1 3 3 7 Output -1 Input 2 2 8 Output 3 Submitted Solution: ``` def solve(A): unique_elems = set(A) if len(unique_elems) == 1: return 0 if len(unique_elems) == 2: mn, mx = sorted(unique_elems) candidate = mx - mn D = candidate // 2 if mx - D == mn + D: return D else: return candidate max_A, min_A = max(A), min(A) mid_val = (max_A + min_A) // 2 D = mid_val - min_A for x in A: if x < mid_val and (x + D) != mid_val: return -1 elif x > mid_val and (x - D) != mid_val: return -1 return D n = int(input()) A = list(map(int, input().split())) print(solve(A)) ```
instruction
0
85,084
12
170,168
Yes
output
1
85,084
12
170,169
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence a_1, a_2, ..., a_n consisting of n integers. You can choose any non-negative integer D (i.e. D β‰₯ 0), and for each a_i you can: * add D (only once), i. e. perform a_i := a_i + D, or * subtract D (only once), i. e. perform a_i := a_i - D, or * leave the value of a_i unchanged. It is possible that after an operation the value a_i becomes negative. Your goal is to choose such minimum non-negative integer D and perform changes in such a way, that all a_i are equal (i.e. a_1=a_2=...=a_n). Print the required D or, if it is impossible to choose such value D, print -1. For example, for array [2, 8] the value D=3 is minimum possible because you can obtain the array [5, 5] if you will add D to 2 and subtract D from 8. And for array [1, 4, 7, 7] the value D=3 is also minimum possible. You can add it to 1 and subtract it from 7 and obtain the array [4, 4, 4, 4]. Input The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the number of elements in a. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 100) β€” the sequence a. Output Print one integer β€” the minimum non-negative integer value D such that if you add this value to some a_i, subtract this value from some a_i and leave some a_i without changes, all obtained values become equal. If it is impossible to choose such value D, print -1. Examples Input 6 1 4 4 7 4 1 Output 3 Input 5 2 2 5 2 5 Output 3 Input 4 1 3 3 7 Output -1 Input 2 2 8 Output 3 Submitted Solution: ``` n = int(input()) lst = sorted(list(set(map(int, input().split())))) if len(lst) >= 4: print(-1) else: if len(lst) == 1: print(0) elif len(lst) == 2: bet = (lst[1] + lst[0]) // 2 if bet - lst[0] == lst[1] - bet: print(bet - lst[0]) else: print(lst[1] - lst[0]) else: if lst[2] - lst[1] == lst[1] - lst[0]: print(lst[1] - lst[0]) else: print(-1) ```
instruction
0
85,085
12
170,170
Yes
output
1
85,085
12
170,171
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≀ a_{2} ≀ … ≀ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≀ t ≀ 10^{4}) β€” the number of test cases. The first line of each test case contains single integer n (1 ≀ n ≀ 10^{5}) β€” the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≀ a_{i} ≀ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0].
instruction
0
85,135
12
170,270
Tags: greedy, math Correct Solution: ``` import math for ii in range(int(input())): n = int(input()) a = [int(i) for i in input().split()] b = [0 for i in range(n)] b[0] = a[0] x = b[0] ans = 0 for i in range(1,n): b[i] = max(max(x,b[i-1]),a[i]) #print(b) x = 0 for i in range(n) : x = max(x,b[i]-a[i]) ans = math.ceil(math.log(x+1,2)) print(int(ans)) ```
output
1
85,135
12
170,271
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≀ a_{2} ≀ … ≀ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≀ t ≀ 10^{4}) β€” the number of test cases. The first line of each test case contains single integer n (1 ≀ n ≀ 10^{5}) β€” the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≀ a_{i} ≀ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0].
instruction
0
85,136
12
170,272
Tags: greedy, math Correct Solution: ``` import math t=int(input()) while(t): t-=1 n=int(input()) l=list(map(int,input().split())) nl=[] maxi=l[0] for i in range(0,n): if(maxi<l[i]): maxi=l[i] nl.append(maxi-l[i]) k=max(nl) # x=(math.log2(k)) if(k>0): # print('Logarithm value of Positive Number = %.0f' %math.log2(k)) print(int(math.log2(k))+1) else: print(0) ```
output
1
85,136
12
170,273
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≀ a_{2} ≀ … ≀ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≀ t ≀ 10^{4}) β€” the number of test cases. The first line of each test case contains single integer n (1 ≀ n ≀ 10^{5}) β€” the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≀ a_{i} ≀ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0].
instruction
0
85,137
12
170,274
Tags: greedy, math Correct Solution: ``` for _ in range(int(input())): n=int(input()) a=list(map(int,input().split())) ans=0 ma=a[0] for i in range(1,n): if a[i]<ma: ans=max(ans,len(bin(ma-a[i]))-2) else: ma=a[i] print(ans) ```
output
1
85,137
12
170,275
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≀ a_{2} ≀ … ≀ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≀ t ≀ 10^{4}) β€” the number of test cases. The first line of each test case contains single integer n (1 ≀ n ≀ 10^{5}) β€” the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≀ a_{i} ≀ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0].
instruction
0
85,138
12
170,276
Tags: greedy, math Correct Solution: ``` for _ in range(int(input())): n = int(input()) lst = [int(i) for i in input().split()] result = 0 for i in range(1, n): diff = lst[i-1]-lst[i] if diff > 0: result = max(result, len(bin(diff))-2) lst[i] = lst[i-1] print(result) ```
output
1
85,138
12
170,277
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≀ a_{2} ≀ … ≀ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≀ t ≀ 10^{4}) β€” the number of test cases. The first line of each test case contains single integer n (1 ≀ n ≀ 10^{5}) β€” the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≀ a_{i} ≀ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0].
instruction
0
85,139
12
170,278
Tags: greedy, math Correct Solution: ``` import math t=int(input()) for i in range(t): n=int(input()) arr=[int(i) for i in input().split()] T=0 prev=arr[0] for i in range(1,n): diff =arr[i]-prev if diff>=0: prev=arr[i] else: T=max(T,int(1+math.log2(-diff))) print(T) ```
output
1
85,139
12
170,279
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≀ a_{2} ≀ … ≀ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≀ t ≀ 10^{4}) β€” the number of test cases. The first line of each test case contains single integer n (1 ≀ n ≀ 10^{5}) β€” the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≀ a_{i} ≀ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0].
instruction
0
85,140
12
170,280
Tags: greedy, math Correct Solution: ``` t = int(input()) for i in range(t): n = int(input()) arr = [int(x) for x in input().split()] max_diff = 0 num_to_beat = arr[0] for i in range(1, n): if arr[i] < num_to_beat: max_diff = max(max_diff, num_to_beat-arr[i]) else: num_to_beat = arr[i] j = 0 while True: if (2**j)-1 >= max_diff: break j+=1 print(j) ```
output
1
85,140
12
170,281
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≀ a_{2} ≀ … ≀ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≀ t ≀ 10^{4}) β€” the number of test cases. The first line of each test case contains single integer n (1 ≀ n ≀ 10^{5}) β€” the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≀ a_{i} ≀ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0].
instruction
0
85,141
12
170,282
Tags: greedy, math Correct Solution: ``` ''' powered addition ''' T = int(input()) for test in range(T): N = int(input()) vec = list(map(int, input().split())) diff = [] mxm = vec[0] for i in range(N - 1): diff.append(mxm - vec[i + 1]) mxm = max(mxm, vec[i + 1]) #print(diff) Tmin = 0 for d in diff: T = 1 if d <= 0: continue while 2**(T - 1) <= d: T += 1 T -= 1 Tmin = max(T, Tmin) print(Tmin) ```
output
1
85,141
12
170,283
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≀ a_{2} ≀ … ≀ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≀ t ≀ 10^{4}) β€” the number of test cases. The first line of each test case contains single integer n (1 ≀ n ≀ 10^{5}) β€” the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≀ a_{i} ≀ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0].
instruction
0
85,142
12
170,284
Tags: greedy, math Correct Solution: ``` from pprint import pprint import sys input = sys.stdin.readline q = int(input()) for _ in range(q): n = int(input()) import collections import math dat = list(map(int, input().split())) xm = min(dat) dat = list(map(lambda x: x - xm, dat)) #print(dat) mv = dat[0] res = 0 for i in range(1, n): #print("i:", i , "mv:",mv, "dat[i]", dat[i]) di = mv - dat[i] if di <= 0: mv = dat[i] continue #print(di) x = math.floor(math.log(di, 2)) x = x + 1 #print(" diff:", di, "x:", x) res = max(res, x) #print("RESULT") print(res) ```
output
1
85,142
12
170,285
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≀ a_{2} ≀ … ≀ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≀ t ≀ 10^{4}) β€” the number of test cases. The first line of each test case contains single integer n (1 ≀ n ≀ 10^{5}) β€” the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≀ a_{i} ≀ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0]. Submitted Solution: ``` import math t = int(input()) for tt in range(t): n = int(input()) arr = [int(i) for i in input().split()] cur_max = -(1<<64) total_steps = 0 for i in arr: if i < cur_max: total_steps = max(math.frexp(cur_max - i)[1], total_steps) cur_max = max(i, cur_max) print(total_steps) ```
instruction
0
85,143
12
170,286
Yes
output
1
85,143
12
170,287
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≀ a_{2} ≀ … ≀ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≀ t ≀ 10^{4}) β€” the number of test cases. The first line of each test case contains single integer n (1 ≀ n ≀ 10^{5}) β€” the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≀ a_{i} ≀ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0]. Submitted Solution: ``` t = int(input()) import math for _ in range(t): n = int(input()) arr = list(map(int,input().strip().split()))[:n] diff = 0 maxx = -float('inf') sec = 0 for i in range(n): if arr[i] <maxx: diff = max(diff,maxx-arr[i]) maxx = max(maxx,arr[i]) if diff == 0: print(0) continue if diff!=0: sec = int(math.log2(diff)) if 1<<(sec+1) <= diff: sec+=1 sec+=1 print(sec) ```
instruction
0
85,144
12
170,288
Yes
output
1
85,144
12
170,289
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≀ a_{2} ≀ … ≀ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≀ t ≀ 10^{4}) β€” the number of test cases. The first line of each test case contains single integer n (1 ≀ n ≀ 10^{5}) β€” the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≀ a_{i} ≀ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0]. Submitted Solution: ``` z=input mod = 10**9 + 7 from collections import * from queue import * from sys import * from collections import * from math import * from heapq import * from itertools import * from bisect import * from collections import Counter as cc from math import factorial as f def lcd(xnum1,xnum2): return (xnum1*xnum2//gcd(xnum1,xnum2)) ################################################################################ """ n=int(z()) for _ in range(int(z())): x=int(z()) l=list(map(int,z().split())) n=int(z()) l=sorted(list(map(int,z().split())))[::-1] a,b=map(int,z().split()) l=set(map(int,z().split())) led=(6,2,5,5,4,5,6,3,7,6) vowel={'a':0,'e':0,'i':0,'o':0,'u':0} color-4=["G", "GB", "YGB", "YGBI", "OYGBI" ,"OYGBIV",'ROYGBIV' ] """ ###########################---START-CODING---############################################### """ 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 """ for _ in range(int(z())): n=int(z()) l=list(map(int,z().split())) x=l[0] j=0 lp=0 p=1 for i in l: if i>=x: x=i else: k=1 t=x-i while True: if (2**(k-1))+i==x: k=k break if (2**(k-1))+i>x: k=k-1 break k+=1 x=max(x,(2**(k-1))+i) j=max(lp,k) lp=j print(j) ```
instruction
0
85,145
12
170,290
Yes
output
1
85,145
12
170,291
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≀ a_{2} ≀ … ≀ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≀ t ≀ 10^{4}) β€” the number of test cases. The first line of each test case contains single integer n (1 ≀ n ≀ 10^{5}) β€” the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≀ a_{i} ≀ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0]. Submitted Solution: ``` t=int(input()) for _ in range(t): n=int(input()) l= list(map(int,input().split())) diff=0 for i in range(n-1): if(l[i+1]<l[i]): temp_diff = l[i]-l[i+1] diff = max(temp_diff,diff) l[i+1]=l[i] if(diff==0): print(0) else: i=0 while(diff>0): i=i+1 diff=diff-(2**(i-1)) print(i) ```
instruction
0
85,146
12
170,292
Yes
output
1
85,146
12
170,293
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≀ a_{2} ≀ … ≀ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≀ t ≀ 10^{4}) β€” the number of test cases. The first line of each test case contains single integer n (1 ≀ n ≀ 10^{5}) β€” the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≀ a_{i} ≀ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0]. Submitted Solution: ``` import sys for _ in range(int(input())): n=int(sys.stdin.readline()) a=list(map(int,sys.stdin.readline().split())) day=0 ma=-1 def check(k): for stop in range(1,50): if ((1<<(stop))-1)>=k: break return stop for i in range(n): if a[i]>=ma: ma=a[i] else: diff=ma-a[i] now=check(diff) day=max(day,now) ma=a[i] print(day) ```
instruction
0
85,147
12
170,294
No
output
1
85,147
12
170,295
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≀ a_{2} ≀ … ≀ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≀ t ≀ 10^{4}) β€” the number of test cases. The first line of each test case contains single integer n (1 ≀ n ≀ 10^{5}) β€” the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≀ a_{i} ≀ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0]. Submitted Solution: ``` t=int(input()) while t>0: t=t-1 n=int(input()) l=list(map(int, input().split())) m=l[0] r=1 count=0 c=1 while c==1: c=0 for i in range(1,n): if l[i]<l[i-1]: l[i]=l[i]+r c=1 elif l[i]>l[i-1] and l[i]<m: l[i]=l[i]+r c=1 r=r*2 count=count+1 print(count-1) ```
instruction
0
85,148
12
170,296
No
output
1
85,148
12
170,297
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≀ a_{2} ≀ … ≀ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≀ t ≀ 10^{4}) β€” the number of test cases. The first line of each test case contains single integer n (1 ≀ n ≀ 10^{5}) β€” the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≀ a_{i} ≀ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0]. Submitted Solution: ``` def main_function(): import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) left_boundary = -1 for i in range(1, n): if a[i] < a[i - 1]: if left_boundary == -1: left_boundary = i - 1 if left_boundary == -1: print(0) continue max_num = max(a[left_boundary:]) min_rest_num = min(a[a.index(max_num):]) max_difference = max_num - min_rest_num current_addition = x = 0 while current_addition < max_difference: current_addition += pow(2, x) x += 1 print(x) if __name__ == '__main__': main_function() ```
instruction
0
85,149
12
170,298
No
output
1
85,149
12
170,299
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a of length n. For every positive integer x you are going to perform the following operation during the x-th second: * Select some distinct indices i_{1}, i_{2}, …, i_{k} which are between 1 and n inclusive, and add 2^{x-1} to each corresponding position of a. Formally, a_{i_{j}} := a_{i_{j}} + 2^{x-1} for j = 1, 2, …, k. Note that you are allowed to not select any indices at all. You have to make a nondecreasing as fast as possible. Find the smallest number T such that you can make the array nondecreasing after at most T seconds. Array a is nondecreasing if and only if a_{1} ≀ a_{2} ≀ … ≀ a_{n}. You have to answer t independent test cases. Input The first line contains a single integer t (1 ≀ t ≀ 10^{4}) β€” the number of test cases. The first line of each test case contains single integer n (1 ≀ n ≀ 10^{5}) β€” the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^{5}. The second line of each test case contains n integers a_{1}, a_{2}, …, a_{n} (-10^{9} ≀ a_{i} ≀ 10^{9}). Output For each test case, print the minimum number of seconds in which you can make a nondecreasing. Example Input 3 4 1 7 6 5 5 1 2 3 4 5 2 0 -4 Output 2 0 3 Note In the first test case, if you select indices 3, 4 at the 1-st second and 4 at the 2-nd second, then a will become [1, 7, 7, 8]. There are some other possible ways to make a nondecreasing in 2 seconds, but you can't do it faster. In the second test case, a is already nondecreasing, so answer is 0. In the third test case, if you do nothing at first 2 seconds and select index 2 at the 3-rd second, a will become [0, 0]. Submitted Solution: ``` import sys import math input = sys.stdin.buffer.readline T = int(input()) for iTest in range(T): n = int(input()) a = list(map(int,input().split())) currentX = 0 currentMax = a[0] for i in range(1,n): if a[i]<currentMax: currentX = max(math.ceil(math.log2(currentMax-a[i]))+1,currentX) else: currentMax=a[i] print(currentX) ```
instruction
0
85,150
12
170,300
No
output
1
85,150
12
170,301
Provide tags and a correct Python 3 solution for this coding contest problem. You are given four positive integers n, m, a, b (1 ≀ b ≀ n ≀ 50; 1 ≀ a ≀ m ≀ 50). Find any such rectangular matrix of size n Γ— m that satisfies all of the following conditions: * each row of the matrix contains exactly a ones; * each column of the matrix contains exactly b ones; * all other elements are zeros. If the desired matrix does not exist, indicate this. For example, for n=3, m=6, a=2, b=1, there exists a matrix satisfying the conditions above: $$$ \begin{vmatrix} 0 & 1 & 0 & 0 & 0 & 1 \\\ 1 & 0 & 0 & 1 & 0 & 0 \\\ 0 & 0 & 1 & 0 & 1 & 0 \end{vmatrix} $$$ Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Then t test cases follow. Each test case is described by four positive integers n, m, a, b (1 ≀ b ≀ n ≀ 50; 1 ≀ a ≀ m ≀ 50), where n and m are the sizes of the matrix, and a and b are the number of ones for rows and columns, respectively. Output For each test case print: * "YES" (without quotes) and the required matrix (if there are several answers, print any) if it exists, or * "NO" (without quotes) if it does not exist. To print the matrix n Γ— m, print n rows, each of which consists of m numbers 0 or 1 describing a row of the matrix. Numbers must be printed without spaces. Example Input 5 3 6 2 1 2 2 2 1 2 2 2 2 4 4 2 2 2 1 1 2 Output YES 010001 100100 001010 NO YES 11 11 YES 1100 1100 0011 0011 YES 1 1
instruction
0
85,151
12
170,302
Tags: constructive algorithms, greedy, math Correct Solution: ``` import math as mt import sys,string input=sys.stdin.readline L=lambda : list(map(int,input().split())) Ls=lambda : list(input().split()) M=lambda : map(int,input().split()) I=lambda :int(input()) t=I() for _ in range(t): n,m,a,b=M() if(n*a!=m*b): print("NO") else: print("YES") l=[] for i in range(n): l.append([0]*m) sr=0 sc=0 i=0 while(i<n): for j in range(sc,sc+a): l[i%n][j%m]=1 sc+=a i+=1 for i in range(n): for j in range(m): print(l[i][j],end="") print() ```
output
1
85,151
12
170,303
Provide tags and a correct Python 3 solution for this coding contest problem. You are given four positive integers n, m, a, b (1 ≀ b ≀ n ≀ 50; 1 ≀ a ≀ m ≀ 50). Find any such rectangular matrix of size n Γ— m that satisfies all of the following conditions: * each row of the matrix contains exactly a ones; * each column of the matrix contains exactly b ones; * all other elements are zeros. If the desired matrix does not exist, indicate this. For example, for n=3, m=6, a=2, b=1, there exists a matrix satisfying the conditions above: $$$ \begin{vmatrix} 0 & 1 & 0 & 0 & 0 & 1 \\\ 1 & 0 & 0 & 1 & 0 & 0 \\\ 0 & 0 & 1 & 0 & 1 & 0 \end{vmatrix} $$$ Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Then t test cases follow. Each test case is described by four positive integers n, m, a, b (1 ≀ b ≀ n ≀ 50; 1 ≀ a ≀ m ≀ 50), where n and m are the sizes of the matrix, and a and b are the number of ones for rows and columns, respectively. Output For each test case print: * "YES" (without quotes) and the required matrix (if there are several answers, print any) if it exists, or * "NO" (without quotes) if it does not exist. To print the matrix n Γ— m, print n rows, each of which consists of m numbers 0 or 1 describing a row of the matrix. Numbers must be printed without spaces. Example Input 5 3 6 2 1 2 2 2 1 2 2 2 2 4 4 2 2 2 1 1 2 Output YES 010001 100100 001010 NO YES 11 11 YES 1100 1100 0011 0011 YES 1 1
instruction
0
85,152
12
170,304
Tags: constructive algorithms, greedy, math Correct Solution: ``` t = int(input()) for _ in range(t): n,m,a,b = [int(x) for x in input().split()] if n*a!=m*b: print("NO") continue l = [] ans = [[0 for i in range(m)] for j in range(n)] for i in range(m): l.append([0,i]) l.sort() for i in range(n): for j in range(a): x = l[j][1] ans[i][x] = 1 l[j][0]+=1 l.sort() print("YES") for i in ans: a = '' for j in i: a+=str(j) print(a) ```
output
1
85,152
12
170,305
Provide tags and a correct Python 3 solution for this coding contest problem. You are given four positive integers n, m, a, b (1 ≀ b ≀ n ≀ 50; 1 ≀ a ≀ m ≀ 50). Find any such rectangular matrix of size n Γ— m that satisfies all of the following conditions: * each row of the matrix contains exactly a ones; * each column of the matrix contains exactly b ones; * all other elements are zeros. If the desired matrix does not exist, indicate this. For example, for n=3, m=6, a=2, b=1, there exists a matrix satisfying the conditions above: $$$ \begin{vmatrix} 0 & 1 & 0 & 0 & 0 & 1 \\\ 1 & 0 & 0 & 1 & 0 & 0 \\\ 0 & 0 & 1 & 0 & 1 & 0 \end{vmatrix} $$$ Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Then t test cases follow. Each test case is described by four positive integers n, m, a, b (1 ≀ b ≀ n ≀ 50; 1 ≀ a ≀ m ≀ 50), where n and m are the sizes of the matrix, and a and b are the number of ones for rows and columns, respectively. Output For each test case print: * "YES" (without quotes) and the required matrix (if there are several answers, print any) if it exists, or * "NO" (without quotes) if it does not exist. To print the matrix n Γ— m, print n rows, each of which consists of m numbers 0 or 1 describing a row of the matrix. Numbers must be printed without spaces. Example Input 5 3 6 2 1 2 2 2 1 2 2 2 2 4 4 2 2 2 1 1 2 Output YES 010001 100100 001010 NO YES 11 11 YES 1100 1100 0011 0011 YES 1 1
instruction
0
85,153
12
170,306
Tags: constructive algorithms, greedy, math Correct Solution: ``` t=int(input()) for i in range(t): n,m,a,b=map(int,input().split()) lst=[] for i in range(n): arr=[] for j in range(m): arr+=[0] lst+=[arr] #lst[1][2]=1 #print(lst) j,k=0,0 x=0 while(j<n): while(k<m): if(x==a): x=0 while(x<a): if(k<m): lst[j][k]=1 k+=1 else: break x+=1 if(x==a): j+=1 if(j>=n): break k=0 flag=0 for c in range(m): count=0 for d in range(n): if(lst[d][c]==1): count+=1 if(count!=b): flag=1 print("NO") break if(flag==0): print("YES") for c in range(n): for d in range(m): print(lst[c][d], end="") print() ```
output
1
85,153
12
170,307
Provide tags and a correct Python 3 solution for this coding contest problem. You are given four positive integers n, m, a, b (1 ≀ b ≀ n ≀ 50; 1 ≀ a ≀ m ≀ 50). Find any such rectangular matrix of size n Γ— m that satisfies all of the following conditions: * each row of the matrix contains exactly a ones; * each column of the matrix contains exactly b ones; * all other elements are zeros. If the desired matrix does not exist, indicate this. For example, for n=3, m=6, a=2, b=1, there exists a matrix satisfying the conditions above: $$$ \begin{vmatrix} 0 & 1 & 0 & 0 & 0 & 1 \\\ 1 & 0 & 0 & 1 & 0 & 0 \\\ 0 & 0 & 1 & 0 & 1 & 0 \end{vmatrix} $$$ Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Then t test cases follow. Each test case is described by four positive integers n, m, a, b (1 ≀ b ≀ n ≀ 50; 1 ≀ a ≀ m ≀ 50), where n and m are the sizes of the matrix, and a and b are the number of ones for rows and columns, respectively. Output For each test case print: * "YES" (without quotes) and the required matrix (if there are several answers, print any) if it exists, or * "NO" (without quotes) if it does not exist. To print the matrix n Γ— m, print n rows, each of which consists of m numbers 0 or 1 describing a row of the matrix. Numbers must be printed without spaces. Example Input 5 3 6 2 1 2 2 2 1 2 2 2 2 4 4 2 2 2 1 1 2 Output YES 010001 100100 001010 NO YES 11 11 YES 1100 1100 0011 0011 YES 1 1
instruction
0
85,154
12
170,308
Tags: constructive algorithms, greedy, math Correct Solution: ``` for i in range(int(input())) : n,m,a,b=map(int,input().split()) if n*a != m*b: print("No") continue print("Yes") t='1'*a+'0'*(m-a) for i in range(n) : print(t) t=t[m-a:]+t[:m-a] ```
output
1
85,154
12
170,309
Provide tags and a correct Python 3 solution for this coding contest problem. You are given four positive integers n, m, a, b (1 ≀ b ≀ n ≀ 50; 1 ≀ a ≀ m ≀ 50). Find any such rectangular matrix of size n Γ— m that satisfies all of the following conditions: * each row of the matrix contains exactly a ones; * each column of the matrix contains exactly b ones; * all other elements are zeros. If the desired matrix does not exist, indicate this. For example, for n=3, m=6, a=2, b=1, there exists a matrix satisfying the conditions above: $$$ \begin{vmatrix} 0 & 1 & 0 & 0 & 0 & 1 \\\ 1 & 0 & 0 & 1 & 0 & 0 \\\ 0 & 0 & 1 & 0 & 1 & 0 \end{vmatrix} $$$ Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Then t test cases follow. Each test case is described by four positive integers n, m, a, b (1 ≀ b ≀ n ≀ 50; 1 ≀ a ≀ m ≀ 50), where n and m are the sizes of the matrix, and a and b are the number of ones for rows and columns, respectively. Output For each test case print: * "YES" (without quotes) and the required matrix (if there are several answers, print any) if it exists, or * "NO" (without quotes) if it does not exist. To print the matrix n Γ— m, print n rows, each of which consists of m numbers 0 or 1 describing a row of the matrix. Numbers must be printed without spaces. Example Input 5 3 6 2 1 2 2 2 1 2 2 2 2 4 4 2 2 2 1 1 2 Output YES 010001 100100 001010 NO YES 11 11 YES 1100 1100 0011 0011 YES 1 1
instruction
0
85,155
12
170,310
Tags: constructive algorithms, greedy, math Correct Solution: ``` t = int(input()) for _ in range(t): n,m,a,b = map(int,input().split()) if n*a != m*b: print("NO") continue ans = [[0]*m for i in range(n)] for i in range(a): ans[0][i] = 1 for i in range(1,n): for j in range(m): if ans[i-1][j] == 1: ans[i][(j+a)%m] = 1 print("YES") for i in ans: print(*i,sep="") ```
output
1
85,155
12
170,311
Provide tags and a correct Python 3 solution for this coding contest problem. You are given four positive integers n, m, a, b (1 ≀ b ≀ n ≀ 50; 1 ≀ a ≀ m ≀ 50). Find any such rectangular matrix of size n Γ— m that satisfies all of the following conditions: * each row of the matrix contains exactly a ones; * each column of the matrix contains exactly b ones; * all other elements are zeros. If the desired matrix does not exist, indicate this. For example, for n=3, m=6, a=2, b=1, there exists a matrix satisfying the conditions above: $$$ \begin{vmatrix} 0 & 1 & 0 & 0 & 0 & 1 \\\ 1 & 0 & 0 & 1 & 0 & 0 \\\ 0 & 0 & 1 & 0 & 1 & 0 \end{vmatrix} $$$ Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Then t test cases follow. Each test case is described by four positive integers n, m, a, b (1 ≀ b ≀ n ≀ 50; 1 ≀ a ≀ m ≀ 50), where n and m are the sizes of the matrix, and a and b are the number of ones for rows and columns, respectively. Output For each test case print: * "YES" (without quotes) and the required matrix (if there are several answers, print any) if it exists, or * "NO" (without quotes) if it does not exist. To print the matrix n Γ— m, print n rows, each of which consists of m numbers 0 or 1 describing a row of the matrix. Numbers must be printed without spaces. Example Input 5 3 6 2 1 2 2 2 1 2 2 2 2 4 4 2 2 2 1 1 2 Output YES 010001 100100 001010 NO YES 11 11 YES 1100 1100 0011 0011 YES 1 1
instruction
0
85,156
12
170,312
Tags: constructive algorithms, greedy, math Correct Solution: ``` from sys import stdin input = stdin.readline if __name__ == '__main__': for _ in range(int(input())): n, m, a, b = map(int, input().split()) mtrx = [] for i in range(n): r = ['0' for _ in range(m)] for j in range(a): r[(i * a + j) % m] = '1' mtrx.append(r) c = True for i in range(m): s = 0 for j in range(n): s += int(mtrx[j][i] == '1') if s != b: c = False break if not c: print('NO') continue print('YES') for r in mtrx: print(''.join(r)) ```
output
1
85,156
12
170,313
Provide tags and a correct Python 3 solution for this coding contest problem. You are given four positive integers n, m, a, b (1 ≀ b ≀ n ≀ 50; 1 ≀ a ≀ m ≀ 50). Find any such rectangular matrix of size n Γ— m that satisfies all of the following conditions: * each row of the matrix contains exactly a ones; * each column of the matrix contains exactly b ones; * all other elements are zeros. If the desired matrix does not exist, indicate this. For example, for n=3, m=6, a=2, b=1, there exists a matrix satisfying the conditions above: $$$ \begin{vmatrix} 0 & 1 & 0 & 0 & 0 & 1 \\\ 1 & 0 & 0 & 1 & 0 & 0 \\\ 0 & 0 & 1 & 0 & 1 & 0 \end{vmatrix} $$$ Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Then t test cases follow. Each test case is described by four positive integers n, m, a, b (1 ≀ b ≀ n ≀ 50; 1 ≀ a ≀ m ≀ 50), where n and m are the sizes of the matrix, and a and b are the number of ones for rows and columns, respectively. Output For each test case print: * "YES" (without quotes) and the required matrix (if there are several answers, print any) if it exists, or * "NO" (without quotes) if it does not exist. To print the matrix n Γ— m, print n rows, each of which consists of m numbers 0 or 1 describing a row of the matrix. Numbers must be printed without spaces. Example Input 5 3 6 2 1 2 2 2 1 2 2 2 2 4 4 2 2 2 1 1 2 Output YES 010001 100100 001010 NO YES 11 11 YES 1100 1100 0011 0011 YES 1 1
instruction
0
85,157
12
170,314
Tags: constructive algorithms, greedy, math Correct Solution: ``` t=int(input()) from heapq import heappush as pu from heapq import heappop as po for _ in range(t): n,m,a,b=map(int,input().split()) q=[] for i in range(m): pu(q,(-b,i)) ma=[[0 for i in range(m)] for i in range(n)] tt=True for i in range(n): s=[] for j in range(a): x,k=po(q) x=-x ma[i][k]=1 s.append((-x+1,k)) for x,k in s: pu(q,(x,k)) for i in range(n): if ma[i].count(1)!=a:tt=False for j in range(m): c=0 for i in range(n): c+=ma[i][j] if c!=b:tt=False if tt: print("YES") for i in range(n): print(''.join([str(i) for i in ma[i]])) else: print("NO") ```
output
1
85,157
12
170,315
Provide tags and a correct Python 3 solution for this coding contest problem. You are given four positive integers n, m, a, b (1 ≀ b ≀ n ≀ 50; 1 ≀ a ≀ m ≀ 50). Find any such rectangular matrix of size n Γ— m that satisfies all of the following conditions: * each row of the matrix contains exactly a ones; * each column of the matrix contains exactly b ones; * all other elements are zeros. If the desired matrix does not exist, indicate this. For example, for n=3, m=6, a=2, b=1, there exists a matrix satisfying the conditions above: $$$ \begin{vmatrix} 0 & 1 & 0 & 0 & 0 & 1 \\\ 1 & 0 & 0 & 1 & 0 & 0 \\\ 0 & 0 & 1 & 0 & 1 & 0 \end{vmatrix} $$$ Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Then t test cases follow. Each test case is described by four positive integers n, m, a, b (1 ≀ b ≀ n ≀ 50; 1 ≀ a ≀ m ≀ 50), where n and m are the sizes of the matrix, and a and b are the number of ones for rows and columns, respectively. Output For each test case print: * "YES" (without quotes) and the required matrix (if there are several answers, print any) if it exists, or * "NO" (without quotes) if it does not exist. To print the matrix n Γ— m, print n rows, each of which consists of m numbers 0 or 1 describing a row of the matrix. Numbers must be printed without spaces. Example Input 5 3 6 2 1 2 2 2 1 2 2 2 2 4 4 2 2 2 1 1 2 Output YES 010001 100100 001010 NO YES 11 11 YES 1100 1100 0011 0011 YES 1 1
instruction
0
85,158
12
170,316
Tags: constructive algorithms, greedy, math Correct Solution: ``` class Solution(): def __init__(self): test = int(input()) for i in range(0, test): n, m, a, b = list(map(int, input().split())) self.solve(n, m, a, b) def solve(self, n, m, a, b): if a * n != b * m: return print("NO") p = [["1" if col < a else "0" for col in range(0, m)] for row in range(0, n)] pos = [(i, j) for j in range(0, a) for i in range(0, n)] for j in reversed(range(0, m)): for i in range(0, b): if not pos: return print("NO") v = pos.pop() if v[1] > j: return print("NO") temp = p[v[0]][j] p[v[0]][j] = p[v[0]][v[1]] p[v[0]][v[1]] = temp print("YES") for row in p: print("".join(row)) Solution() ```
output
1
85,158
12
170,317
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = βˆ‘_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≀ n ≀ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of array a. Output Print one integer β€” the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
instruction
0
85,199
12
170,398
Tags: combinatorics, math, sortings Correct Solution: ``` import sys import math import collections input=sys.stdin.readline mod=998244353 def fact(n): prod=1 for i in range(2,n+1): prod=(prod*i)%mod return prod def ncr(n): k=(fact(n)*fact(n))%mod k1=pow(k,mod-2,mod) return((fact(2*n)*k1)%mod) n=int(input()) l=sorted([int(i) for i in input().split()]) s1=0 s2=0 for i in range(n): s1+=l[i] s2+=l[n+i] print(((s2-s1)%mod*ncr(n))%mod) ```
output
1
85,199
12
170,399
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = βˆ‘_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≀ n ≀ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of array a. Output Print one integer β€” the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
instruction
0
85,200
12
170,400
Tags: combinatorics, math, sortings Correct Solution: ``` from sys import stdin, gettrace if gettrace(): inputi = input else: def input(): return next(stdin)[:-1] def inputi(): return stdin.buffer.readline() MOD = 998244353 def main(): n = int(inputi()) aa = [int(a) for a in inputi().split()] aa.sort() sm = sum(aa[n:]) - sum(aa[:n]) % MOD fac = [1] for i in range(1, n*2+1): fac.append((fac[-1] * i)%MOD) fni = pow(fac[n], MOD-2, MOD) res = (sm*fac[n*2]*fni*fni)%MOD print(res) if __name__ == "__main__": main() ```
output
1
85,200
12
170,401
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = βˆ‘_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≀ n ≀ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of array a. Output Print one integer β€” the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
instruction
0
85,201
12
170,402
Tags: combinatorics, math, sortings Correct Solution: ``` import os import sys from io import BytesIO, IOBase def main(): pass # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion mod=998244353 mv=150000 def mI(a, m) : m0 = m y = 0 x = 1 if (m == 1) : return 0 while (a > 1) : # q is quotient q = a // m t = m # m is remainder now, process # same as Euclid's algo m = a % m a = t t = y # Update x and y y = x - q * y x = t # Make x positive if (x < 0) : x = x + m0 return x V=[] v=1 for i in range((mv)+1): V.append(mI(i,mod)) #print(V) #print((4*V[2])%mod) def main(): n=int(input()) A=list(map(int,input().split())) ans=0 A.sort() for i in range(n): ans=(ans+abs(-A[i]+A[(2*n)-i-1]))%mod c=1 for i in range(n): c=(c*((2*n)-i))%mod c=(c*V[i+1])%mod #print(c) print((ans*c)%mod) if __name__ == "__main__": main() ```
output
1
85,201
12
170,403
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = βˆ‘_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≀ n ≀ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of array a. Output Print one integer β€” the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
instruction
0
85,202
12
170,404
Tags: combinatorics, math, sortings Correct Solution: ``` import itertools import math import sys import os from collections import defaultdict from heapq import heapify, heappush, heappop def is_debug(): return "PYPY3_HOME" not in os.environ def stdin_wrapper(): data = '''5 13 8 35 94 9284 34 54 69 123 846 ''' for line in data.split('\n'): yield line if not is_debug(): def stdin_wrapper(): while True: yield sys.stdin.readline() inputs = stdin_wrapper() def input_wrapper(): return next(inputs) def get_str(): if is_debug(): return input_wrapper() return input() def get(_type): if _type == str: return get_str() return _type(input_wrapper()) def get_arr(_type): return [_type(x) for x in input_wrapper().split()] def tuplerize(method): def wrap(*args, **kwargs): res = method(*args, **kwargs) if not isinstance(res, (tuple, list)): res = (res, ) return res return wrap ''' Solution ''' @tuplerize def solve(n, d): MOD = 998244353 def price(a,b): return sum([abs(x-y) % MOD for x, y in zip(sorted(a), reversed(sorted(b)))]) % MOD _MAX = 2*n + 1 fact = [0 for _ in range((_MAX))] fact[0] = 1 fact_inv = [0 for _ in range((_MAX))] for i in range(1, _MAX): fact[i] = fact[i-1] * i % MOD def fastpow(x, y, z): "Calculate (x ** y) % z efficiently." number = 1 while y: if y & 1: number = number * x % z y >>= 1 x = x * x % z return number fact_inv[_MAX-1] = fastpow(fact[_MAX-1], MOD - 2, MOD) % MOD for i in reversed(range(_MAX-1)): fact_inv[i] = fact_inv[i+1] * (i+1) % MOD p = price(d[:len(d)//2], d[len(d)//2:]) return (p * ((((fact[len(d)] * fact_inv[len(d)//2]) % MOD) * fact_inv[len(d)//2]) % MOD)) % MOD n = get(int) a = get_arr(int) print(*solve(n, a)) ```
output
1
85,202
12
170,405
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = βˆ‘_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≀ n ≀ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of array a. Output Print one integer β€” the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
instruction
0
85,203
12
170,406
Tags: combinatorics, math, sortings Correct Solution: ``` def ncr(n, r, p): # initialize numerator # and denominator num = den = 1 for i in range(r): num = (num * (n - i)) % p den = (den * (i + 1)) % p return (num * pow(den, p - 2, p)) % p def testcase(): n = int(input()) arr = list(map(int, input().split())) mod = 998244353 arr.sort() c = ncr(2 * n, n, mod) ans = 0 for i in range(n): ans = (ans - (arr[i] * c) % mod) % mod for i in range(n, 2 * n): ans = (ans + (arr[i] * c) % mod) % mod print(ans) return import sys, os if os.path.exists('input.txt'): sys.stdin = open('input.txt', 'r') sys.setrecursionlimit(10 ** 5) testcase() ```
output
1
85,203
12
170,407
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = βˆ‘_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≀ n ≀ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of array a. Output Print one integer β€” the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
instruction
0
85,204
12
170,408
Tags: combinatorics, math, sortings Correct Solution: ``` def res(n): nu=s=1 for i in range(n): nu=(nu*(2*n-i))%m s=(s*(i+1))%m return((nu*pow(s,m-2,m))%m) m=998244353 n=int(input()) fg=sorted(list(map(int,input().split()))) f=abs((sum(fg[:n])-sum(fg[n:]))) print((f*res(n))%m) #print(f) ```
output
1
85,204
12
170,409
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = βˆ‘_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≀ n ≀ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of array a. Output Print one integer β€” the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
instruction
0
85,205
12
170,410
Tags: combinatorics, math, sortings Correct Solution: ``` mod = 998244353 def pow_(x, y, p) : res = 1 x = x % p if x == 0: return 0 while y > 0: if (y & 1) == 1: res = (res * x) % p y = y >> 1 x = (x * x) % p return res def reverse(x, mod): return pow_(x, mod-2, mod) gt = [1] * 300001 for i in range(2, 300001): gt[i] = i * gt[i-1] % mod n = int(input()) a = list(map(int, input().split())) a = sorted(a) cof = (gt[2*n] * reverse(gt[n], mod) * reverse(gt[n], mod)) ans = (sum(a[n:]) - sum(a[:n])) * cof % mod print(ans) ```
output
1
85,205
12
170,411
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: either in p or in q). Let's sort p in non-decreasing order, and q in non-increasing order, we can denote the sorted versions by x and y, respectively. Then the cost of a partition is defined as f(p, q) = βˆ‘_{i = 1}^n |x_i - y_i|. Find the sum of f(p, q) over all correct partitions of array a. Since the answer might be too big, print its remainder modulo 998244353. Input The first line contains a single integer n (1 ≀ n ≀ 150 000). The second line contains 2n integers a_1, a_2, …, a_{2n} (1 ≀ a_i ≀ 10^9) β€” elements of array a. Output Print one integer β€” the answer to the problem, modulo 998244353. Examples Input 1 1 4 Output 6 Input 2 2 1 2 1 Output 12 Input 3 2 2 2 2 2 2 Output 0 Input 5 13 8 35 94 9284 34 54 69 123 846 Output 2588544 Note Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p are different. In the first example, there are two correct partitions of the array a: 1. p = [1], q = [4], then x = [1], y = [4], f(p, q) = |1 - 4| = 3; 2. p = [4], q = [1], then x = [4], y = [1], f(p, q) = |4 - 1| = 3. In the second example, there are six valid partitions of the array a: 1. p = [2, 1], q = [2, 1] (elements with indices 1 and 2 in the original array are selected in the subsequence p); 2. p = [2, 2], q = [1, 1]; 3. p = [2, 1], q = [1, 2] (elements with indices 1 and 4 are selected in the subsequence p); 4. p = [1, 2], q = [2, 1]; 5. p = [1, 1], q = [2, 2]; 6. p = [2, 1], q = [2, 1] (elements with indices 3 and 4 are selected in the subsequence p).
instruction
0
85,206
12
170,412
Tags: combinatorics, math, sortings Correct Solution: ``` import sys import math,bisect sys.setrecursionlimit(10 ** 5) from itertools import groupby,accumulate from heapq import heapify,heappop,heappush from collections import deque,Counter,defaultdict I = lambda : int(sys.stdin.readline()) neo = lambda : map(int, sys.stdin.readline().split()) Neo = lambda : list(map(int, sys.stdin.readline().split())) n = I() A = Neo() mod = 998244353 fact = [1] for i in range(1,2*n+1): fact += [fact[-1]*i%mod] B,C = A[0::2],A[1::2] B.sort() C.sort(reverse=True) Ans = 0 for i,j in zip(B,C): Ans += abs(i-j) Ans = Ans%mod Ans = (Ans*fact[2*n]*pow(fact[n]*fact[n],mod-2,mod))%mod print(Ans) ```
output
1
85,206
12
170,413