message
stringlengths
2
433k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
113
108k
cluster
float64
12
12
__index_level_0__
int64
226
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call an array of non-negative integers a_1, a_2, …, a_n a k-extension for some non-negative integer k if for all possible pairs of indices 1 ≤ i, j ≤ n the inequality k ⋅ |i - j| ≤ min(a_i, a_j) is satisfied. The expansion coefficient of the array a is the maximal integer k such that the array a is a k-extension. Any array is a 0-expansion, so the expansion coefficient always exists. You are given an array of non-negative integers a_1, a_2, …, a_n. Find its expansion coefficient. Input The first line contains one positive integer n — the number of elements in the array a (2 ≤ n ≤ 300 000). The next line contains n non-negative integers a_1, a_2, …, a_n, separated by spaces (0 ≤ a_i ≤ 10^9). Output Print one non-negative integer — expansion coefficient of the array a_1, a_2, …, a_n. Examples Input 4 6 4 5 5 Output 1 Input 3 0 1 2 Output 0 Input 4 821 500 479 717 Output 239 Note In the first test, the expansion coefficient of the array [6, 4, 5, 5] is equal to 1 because |i-j| ≤ min(a_i, a_j), because all elements of the array satisfy a_i ≥ 3. On the other hand, this array isn't a 2-extension, because 6 = 2 ⋅ |1 - 4| ≤ min(a_1, a_4) = 5 is false. In the second test, the expansion coefficient of the array [0, 1, 2] is equal to 0 because this array is not a 1-extension, but it is 0-extension. Submitted Solution: ``` # ========= /\ /| |====/| # | / \ | | / | # | /____\ | | / | # | / \ | | / | # ========= / \ ===== |/====| # code if __name__ == "__main__": n = int(input()) a = list(map(int,input().split())) k = 10**9 for i in range(n): if i != 0: k = min(k , a[i]//i) if i != n-1: k = min(k , a[i]//(n-1-i)) print(k) ```
instruction
0
97,900
12
195,800
Yes
output
1
97,900
12
195,801
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call an array of non-negative integers a_1, a_2, …, a_n a k-extension for some non-negative integer k if for all possible pairs of indices 1 ≤ i, j ≤ n the inequality k ⋅ |i - j| ≤ min(a_i, a_j) is satisfied. The expansion coefficient of the array a is the maximal integer k such that the array a is a k-extension. Any array is a 0-expansion, so the expansion coefficient always exists. You are given an array of non-negative integers a_1, a_2, …, a_n. Find its expansion coefficient. Input The first line contains one positive integer n — the number of elements in the array a (2 ≤ n ≤ 300 000). The next line contains n non-negative integers a_1, a_2, …, a_n, separated by spaces (0 ≤ a_i ≤ 10^9). Output Print one non-negative integer — expansion coefficient of the array a_1, a_2, …, a_n. Examples Input 4 6 4 5 5 Output 1 Input 3 0 1 2 Output 0 Input 4 821 500 479 717 Output 239 Note In the first test, the expansion coefficient of the array [6, 4, 5, 5] is equal to 1 because |i-j| ≤ min(a_i, a_j), because all elements of the array satisfy a_i ≥ 3. On the other hand, this array isn't a 2-extension, because 6 = 2 ⋅ |1 - 4| ≤ min(a_1, a_4) = 5 is false. In the second test, the expansion coefficient of the array [0, 1, 2] is equal to 0 because this array is not a 1-extension, but it is 0-extension. Submitted Solution: ``` n=int(input()) res=[] list=[int(i) for i in input().split()] for i in range(1,n-1): res.append(min(int(min(list[0],list[i])/i),int(min(list[n-1],list[i])/(n-1-i)))) res.append(int(min(list[0],list[n-1])/(n-1))) print(min(res)) ```
instruction
0
97,901
12
195,802
Yes
output
1
97,901
12
195,803
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call an array of non-negative integers a_1, a_2, …, a_n a k-extension for some non-negative integer k if for all possible pairs of indices 1 ≤ i, j ≤ n the inequality k ⋅ |i - j| ≤ min(a_i, a_j) is satisfied. The expansion coefficient of the array a is the maximal integer k such that the array a is a k-extension. Any array is a 0-expansion, so the expansion coefficient always exists. You are given an array of non-negative integers a_1, a_2, …, a_n. Find its expansion coefficient. Input The first line contains one positive integer n — the number of elements in the array a (2 ≤ n ≤ 300 000). The next line contains n non-negative integers a_1, a_2, …, a_n, separated by spaces (0 ≤ a_i ≤ 10^9). Output Print one non-negative integer — expansion coefficient of the array a_1, a_2, …, a_n. Examples Input 4 6 4 5 5 Output 1 Input 3 0 1 2 Output 0 Input 4 821 500 479 717 Output 239 Note In the first test, the expansion coefficient of the array [6, 4, 5, 5] is equal to 1 because |i-j| ≤ min(a_i, a_j), because all elements of the array satisfy a_i ≥ 3. On the other hand, this array isn't a 2-extension, because 6 = 2 ⋅ |1 - 4| ≤ min(a_1, a_4) = 5 is false. In the second test, the expansion coefficient of the array [0, 1, 2] is equal to 0 because this array is not a 1-extension, but it is 0-extension. Submitted Solution: ``` n = int(input()) a = [int(s) for s in input().split()] b = 0 c = 1000000000 c = a[0]//(n-1) for i in range(1,n): b = a[i]//i if c>=b: c = b print(c) ```
instruction
0
97,902
12
195,804
No
output
1
97,902
12
195,805
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call an array of non-negative integers a_1, a_2, …, a_n a k-extension for some non-negative integer k if for all possible pairs of indices 1 ≤ i, j ≤ n the inequality k ⋅ |i - j| ≤ min(a_i, a_j) is satisfied. The expansion coefficient of the array a is the maximal integer k such that the array a is a k-extension. Any array is a 0-expansion, so the expansion coefficient always exists. You are given an array of non-negative integers a_1, a_2, …, a_n. Find its expansion coefficient. Input The first line contains one positive integer n — the number of elements in the array a (2 ≤ n ≤ 300 000). The next line contains n non-negative integers a_1, a_2, …, a_n, separated by spaces (0 ≤ a_i ≤ 10^9). Output Print one non-negative integer — expansion coefficient of the array a_1, a_2, …, a_n. Examples Input 4 6 4 5 5 Output 1 Input 3 0 1 2 Output 0 Input 4 821 500 479 717 Output 239 Note In the first test, the expansion coefficient of the array [6, 4, 5, 5] is equal to 1 because |i-j| ≤ min(a_i, a_j), because all elements of the array satisfy a_i ≥ 3. On the other hand, this array isn't a 2-extension, because 6 = 2 ⋅ |1 - 4| ≤ min(a_1, a_4) = 5 is false. In the second test, the expansion coefficient of the array [0, 1, 2] is equal to 0 because this array is not a 1-extension, but it is 0-extension. Submitted Solution: ``` n = int(input()) l = list(map(int,input().split())) a = min(l) b = max(l) z1 = l.index(a) z2 = l.index(b) k1 = min(l[0],l[n-1])//abs(n-1) ba = [] sa = [] for i in range(len(l)): if i!=z1: ba.append((a//abs(z1-i))) for i in range(len(l)): if i!=z2: ba.append((min(b,l[i])//abs(z2-i))) ba.sort() sa.sort() z = ba[0] h = 10**18 if len(sa)>0: h = sa[0] i = 0 j = n-1 mini = 10**19 while i<j: if min(l[i],l[j])//(abs((i-j))) < mini: mini = min(l[i],l[j])//(abs((i-j))) i+=1 j-=1 print(min(k1,z,h,mini)) ```
instruction
0
97,903
12
195,806
No
output
1
97,903
12
195,807
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call an array of non-negative integers a_1, a_2, …, a_n a k-extension for some non-negative integer k if for all possible pairs of indices 1 ≤ i, j ≤ n the inequality k ⋅ |i - j| ≤ min(a_i, a_j) is satisfied. The expansion coefficient of the array a is the maximal integer k such that the array a is a k-extension. Any array is a 0-expansion, so the expansion coefficient always exists. You are given an array of non-negative integers a_1, a_2, …, a_n. Find its expansion coefficient. Input The first line contains one positive integer n — the number of elements in the array a (2 ≤ n ≤ 300 000). The next line contains n non-negative integers a_1, a_2, …, a_n, separated by spaces (0 ≤ a_i ≤ 10^9). Output Print one non-negative integer — expansion coefficient of the array a_1, a_2, …, a_n. Examples Input 4 6 4 5 5 Output 1 Input 3 0 1 2 Output 0 Input 4 821 500 479 717 Output 239 Note In the first test, the expansion coefficient of the array [6, 4, 5, 5] is equal to 1 because |i-j| ≤ min(a_i, a_j), because all elements of the array satisfy a_i ≥ 3. On the other hand, this array isn't a 2-extension, because 6 = 2 ⋅ |1 - 4| ≤ min(a_1, a_4) = 5 is false. In the second test, the expansion coefficient of the array [0, 1, 2] is equal to 0 because this array is not a 1-extension, but it is 0-extension. Submitted Solution: ``` n = int(input()) a = [int(x) for x in input().split()] module = n - 1 k = [] for i in range(n - 1): for j in range(1, n): minimum = min(a[i], a[j]) if minimum < module: k.append(0) else: r = minimum // module k.append(r) print(max(k)) ```
instruction
0
97,904
12
195,808
No
output
1
97,904
12
195,809
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call an array of non-negative integers a_1, a_2, …, a_n a k-extension for some non-negative integer k if for all possible pairs of indices 1 ≤ i, j ≤ n the inequality k ⋅ |i - j| ≤ min(a_i, a_j) is satisfied. The expansion coefficient of the array a is the maximal integer k such that the array a is a k-extension. Any array is a 0-expansion, so the expansion coefficient always exists. You are given an array of non-negative integers a_1, a_2, …, a_n. Find its expansion coefficient. Input The first line contains one positive integer n — the number of elements in the array a (2 ≤ n ≤ 300 000). The next line contains n non-negative integers a_1, a_2, …, a_n, separated by spaces (0 ≤ a_i ≤ 10^9). Output Print one non-negative integer — expansion coefficient of the array a_1, a_2, …, a_n. Examples Input 4 6 4 5 5 Output 1 Input 3 0 1 2 Output 0 Input 4 821 500 479 717 Output 239 Note In the first test, the expansion coefficient of the array [6, 4, 5, 5] is equal to 1 because |i-j| ≤ min(a_i, a_j), because all elements of the array satisfy a_i ≥ 3. On the other hand, this array isn't a 2-extension, because 6 = 2 ⋅ |1 - 4| ≤ min(a_1, a_4) = 5 is false. In the second test, the expansion coefficient of the array [0, 1, 2] is equal to 0 because this array is not a 1-extension, but it is 0-extension. Submitted Solution: ``` print(1) ```
instruction
0
97,905
12
195,810
No
output
1
97,905
12
195,811
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is the maximum value of k. You are given an infinite sequence of form "112123123412345..." which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from 1 to 1, the second one — from 1 to 2, the third one — from 1 to 3, ..., the i-th block consists of all numbers from 1 to i. So the first 56 elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the 1-st element of the sequence is 1, the 3-rd element of the sequence is 2, the 20-th element of the sequence is 5, the 38-th element is 2, the 56-th element of the sequence is 0. Your task is to answer q independent queries. In the i-th query you are given one integer k_i. Calculate the digit at the position k_i of the sequence. Input The first line of the input contains one integer q (1 ≤ q ≤ 500) — the number of queries. The i-th of the following q lines contains one integer k_i (1 ≤ k_i ≤ 10^{18}) — the description of the corresponding query. Output Print q lines. In the i-th line print one digit x_i (0 ≤ x_i ≤ 9) — the answer to the query i, i.e. x_i should be equal to the element at the position k_i of the sequence. Examples Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999999999999 1000000000000000000 Output 8 2 4 1 Note Answers on queries from the first example are described in the problem statement.
instruction
0
97,938
12
195,876
Tags: binary search, math Correct Solution: ``` import sys input = sys.stdin.readline def binary_search(c1, c2): m = (c1 + c2 + 1) // 2 if abs(c1 - c2) <= 1: return m else: if ok(m): c1 = m else: c2 = m return binary_search(c1, c2) def f(m): c = 0 d = 10 l = 1 s = 0 while True: if m >= d: c += (d - d // 10 + 1) * (d - d // 10) // 2 * l + s * (d - d // 10) s += (d - d // 10) * l else: u = m - d // 10 + 1 c += (u + 1) * u // 2 * l + u * s break d *= 10 l += 1 return c def ok(m): return True if f(m) < k else False def g(k): c = 0 d = 10 l = 1 s = 1 while True: x = (d - d // 10) * l if c + x < k: c += x else: i = (k - c - 1) % l s += (k - c - 1) // l return list(str(s))[i] d *= 10 l += 1 s *= 10 q = int(input()) for _ in range(q): k = int(input()) x = binary_search(0, pow(10, 9)) k -= f(x - 1) ans = g(k) print(ans) ```
output
1
97,938
12
195,877
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is the maximum value of k. You are given an infinite sequence of form "112123123412345..." which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from 1 to 1, the second one — from 1 to 2, the third one — from 1 to 3, ..., the i-th block consists of all numbers from 1 to i. So the first 56 elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the 1-st element of the sequence is 1, the 3-rd element of the sequence is 2, the 20-th element of the sequence is 5, the 38-th element is 2, the 56-th element of the sequence is 0. Your task is to answer q independent queries. In the i-th query you are given one integer k_i. Calculate the digit at the position k_i of the sequence. Input The first line of the input contains one integer q (1 ≤ q ≤ 500) — the number of queries. The i-th of the following q lines contains one integer k_i (1 ≤ k_i ≤ 10^{18}) — the description of the corresponding query. Output Print q lines. In the i-th line print one digit x_i (0 ≤ x_i ≤ 9) — the answer to the query i, i.e. x_i should be equal to the element at the position k_i of the sequence. Examples Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999999999999 1000000000000000000 Output 8 2 4 1 Note Answers on queries from the first example are described in the problem statement.
instruction
0
97,940
12
195,880
Tags: binary search, math Correct Solution: ``` q = int(input()) def f(n): take = 0 ret = 0 while True: if n - take <= 0: break ret += (n - take) * (n - take + 1) // 2 take = take * 10 + 9 return ret def g(n): take = 0 ret = 0 while True: if n - take <= 0: break ret += n - take take = take * 10 + 9 return ret for _ in range(q): k = int(input()) low, high = 1, k ans = -1 while low < high: mid = (low + high + 1) >> 1 if f(mid) == k: ans = mid % 10 break if f(mid) < k: low = mid else: high = mid - 1 if f(low) == k: ans = low % 10 if ans != -1: print(ans) continue k -= f(low) next = low + 1 low, high = 1, next while low < high: mid = (low + high + 1) // 2 if g(mid) == k: ans = mid % 10 break if g(mid) < k: low = mid else: high = mid - 1 if g(low) == k: ans = low % 10 if ans != -1: print(ans) continue h = g(low) assert(h < k) m = str(low + 1) print(m[k - h - 1]) ```
output
1
97,940
12
195,881
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is the maximum value of k. You are given an infinite sequence of form "112123123412345..." which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from 1 to 1, the second one — from 1 to 2, the third one — from 1 to 3, ..., the i-th block consists of all numbers from 1 to i. So the first 56 elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the 1-st element of the sequence is 1, the 3-rd element of the sequence is 2, the 20-th element of the sequence is 5, the 38-th element is 2, the 56-th element of the sequence is 0. Your task is to answer q independent queries. In the i-th query you are given one integer k_i. Calculate the digit at the position k_i of the sequence. Input The first line of the input contains one integer q (1 ≤ q ≤ 500) — the number of queries. The i-th of the following q lines contains one integer k_i (1 ≤ k_i ≤ 10^{18}) — the description of the corresponding query. Output Print q lines. In the i-th line print one digit x_i (0 ≤ x_i ≤ 9) — the answer to the query i, i.e. x_i should be equal to the element at the position k_i of the sequence. Examples Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999999999999 1000000000000000000 Output 8 2 4 1 Note Answers on queries from the first example are described in the problem statement.
instruction
0
97,941
12
195,882
Tags: binary search, math Correct Solution: ``` # ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------- fast io -------------------- from math import gcd, ceil def prod(a, mod=10**9+7): ans = 1 for each in a: ans = (ans * each) % mod return ans def lcm(a, b): return a * b // gcd(a, b) def binary(x, length=16): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y for _ in range(int(input()) if True else 1): n = int(input()) # 1 + 2 + ... + 9 -> one digit # 11 + 13 + ... + 189 -> two digit digit = 1 cur = 1 def apsum(a, r, n): return ((a + a + (n - 1) * r) * n) // 2 v = apsum(cur, digit, 9 * 10**(digit-1)) while n > v: n -= v cur = cur + (9 * 10**(digit-1) - 1) * digit + digit + 1 digit += 1 v = apsum(cur, digit, 9 * 10 ** (digit - 1)) def digits_till_term(term): return apsum(cur, digit, term) alpha, omega = 0, 9*(10**(digit-1)) - 1 while alpha < omega: mid = (alpha+omega) // 2 if alpha == mid or omega == mid:break if digits_till_term(mid) >= n: omega = mid else: alpha = mid pos = n - (digits_till_term(alpha)) if n > digits_till_term(alpha+1): pos = n - digits_till_term(alpha + 1) dig = 1 while pos > 9*(10**(dig-1)) * dig: pos -= 9*(10**(dig-1)) * dig dig += 1 # 100 101 102 103 104 ... num = str(10**(dig-1) + (pos-1) // dig) print(num[(pos-1)%dig]) ```
output
1
97,941
12
195,883
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is the maximum value of k. You are given an infinite sequence of form "112123123412345..." which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from 1 to 1, the second one — from 1 to 2, the third one — from 1 to 3, ..., the i-th block consists of all numbers from 1 to i. So the first 56 elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the 1-st element of the sequence is 1, the 3-rd element of the sequence is 2, the 20-th element of the sequence is 5, the 38-th element is 2, the 56-th element of the sequence is 0. Your task is to answer q independent queries. In the i-th query you are given one integer k_i. Calculate the digit at the position k_i of the sequence. Input The first line of the input contains one integer q (1 ≤ q ≤ 500) — the number of queries. The i-th of the following q lines contains one integer k_i (1 ≤ k_i ≤ 10^{18}) — the description of the corresponding query. Output Print q lines. In the i-th line print one digit x_i (0 ≤ x_i ≤ 9) — the answer to the query i, i.e. x_i should be equal to the element at the position k_i of the sequence. Examples Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999999999999 1000000000000000000 Output 8 2 4 1 Note Answers on queries from the first example are described in the problem statement.
instruction
0
97,942
12
195,884
Tags: binary search, math Correct Solution: ``` dp, cnt = [0], 1 dp2 = [0] while dp[-1] <= int(1e18): ans = dp2[-1] + (10 ** cnt - 10 ** (cnt - 1)) * cnt dp2.append(ans) ans = dp[-1] + dp2[-2] * (10 ** cnt - 10 ** (cnt - 1)) + cnt * int((10 ** cnt - 10 ** (cnt - 1) + 1) * (10 ** cnt - 10 ** (cnt - 1)) / 2) cnt += 1 dp.append(ans) def Cal(a, b): if a % 2 == 0: return dp2[b - 1] * a + b * (a + 1) * int(a / 2) else: return dp2[b - 1] * a + b * a * int((a + 1) / 2) q = int(input()) for _ in range(q): k = int(input()) i = 0 while k > dp[i]: i += 1 k -= dp[i - 1] l, r = 0, 10 ** i - 10 ** (i - 1) last = int((l + r) / 2) while not(Cal(last, i) < k and Cal(last + 1, i) >= k): if(Cal(last, i) < k): l = last last = int((l + r) / 2) + 1 else: r = last last = int((l + r) / 2) k -= Cal(last, i) j = 0 while dp2[j] < k: j += 1 k -= dp2[j - 1] a = int((k - 1) / j) k -= a * j Long = str(10 ** (j - 1) + a) print(Long[k - 1]) ```
output
1
97,942
12
195,885
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is the maximum value of k. You are given an infinite sequence of form "112123123412345..." which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from 1 to 1, the second one — from 1 to 2, the third one — from 1 to 3, ..., the i-th block consists of all numbers from 1 to i. So the first 56 elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the 1-st element of the sequence is 1, the 3-rd element of the sequence is 2, the 20-th element of the sequence is 5, the 38-th element is 2, the 56-th element of the sequence is 0. Your task is to answer q independent queries. In the i-th query you are given one integer k_i. Calculate the digit at the position k_i of the sequence. Input The first line of the input contains one integer q (1 ≤ q ≤ 500) — the number of queries. The i-th of the following q lines contains one integer k_i (1 ≤ k_i ≤ 10^{18}) — the description of the corresponding query. Output Print q lines. In the i-th line print one digit x_i (0 ≤ x_i ≤ 9) — the answer to the query i, i.e. x_i should be equal to the element at the position k_i of the sequence. Examples Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999999999999 1000000000000000000 Output 8 2 4 1 Note Answers on queries from the first example are described in the problem statement.
instruction
0
97,943
12
195,886
Tags: binary search, math Correct Solution: ``` import sys input = sys.stdin.readline # 1keta : 1,2,3,4,5,6,7,8,9 : 45 # 2keta : 11,13,15,... # 9 : 9 , sum = 45 # 99 : 9+(2*90) = 189, sum =((9+2)+(9+2*90))*90//2 +45 = 9045 LIST=[9] for i in range(1,20): LIST.append(LIST[-1]+9*(10**i)*(i+1)) SUM=[45] for i in range(1,19): SUM.append(SUM[-1]+(LIST[i-1]+(i+1)+LIST[i])*9*(10**i)//2) def calc(x): True SUM=[0]+SUM LIST=[0]+LIST LIST2=[0] for i in range(1,20): LIST2.append(10**i-1) q=int(input()) #q=1000 for testcases in range(q): k=int(input()) #k=testcases+1 for i in range(20): if SUM[i]>=k: keta=i break #print(keta) k-=SUM[keta-1] INI=LIST[keta-1]+keta #print(k,INI) OK=0 NG=10**keta while NG>OK+1: mid=(OK+NG)//2 if (INI + INI + keta *(mid - 1)) * mid //2 >= k: NG=mid else: OK=mid k-=(INI + INI + keta *(OK - 1)) * OK //2 #print(k,OK,10**(keta-1)+OK) for i in range(20): if LIST[i]>=k: keta2=i k-=LIST[i-1] break #print(k,keta2) r,q=divmod(k,keta2) #print("!",r,q) if q==0: print(str(LIST2[keta2-1]+r)[-1]) else: print(str(LIST2[keta2-1]+r+1)[q-1]) ```
output
1
97,943
12
195,887
Provide tags and a correct Python 3 solution for this coding contest problem. The only difference between the easy and the hard versions is the maximum value of k. You are given an infinite sequence of form "112123123412345..." which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from 1 to 1, the second one — from 1 to 2, the third one — from 1 to 3, ..., the i-th block consists of all numbers from 1 to i. So the first 56 elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the 1-st element of the sequence is 1, the 3-rd element of the sequence is 2, the 20-th element of the sequence is 5, the 38-th element is 2, the 56-th element of the sequence is 0. Your task is to answer q independent queries. In the i-th query you are given one integer k_i. Calculate the digit at the position k_i of the sequence. Input The first line of the input contains one integer q (1 ≤ q ≤ 500) — the number of queries. The i-th of the following q lines contains one integer k_i (1 ≤ k_i ≤ 10^{18}) — the description of the corresponding query. Output Print q lines. In the i-th line print one digit x_i (0 ≤ x_i ≤ 9) — the answer to the query i, i.e. x_i should be equal to the element at the position k_i of the sequence. Examples Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999999999999 1000000000000000000 Output 8 2 4 1 Note Answers on queries from the first example are described in the problem statement.
instruction
0
97,944
12
195,888
Tags: binary search, math Correct Solution: ``` l = [0] t = int(input()) def count(level): nums = 10**level - 10**(level - 1) first = l[level - 1] + level last = l[level - 1] + nums*level if len(l) <= level: l.append(last) return (nums*(first+last))//2 def search(min_size,val,level): checker = val*min_size + level*(val*(val + 1))//2 return checker for _ in range(t): ind = int(input()) level = 1 while ind > count(level): ind -= count(level) level += 1 min_size = l[level-1] lo = 0 hi = 10**level - 10**(level-1) while hi - lo > 1: val = (hi+lo)//2 checker = search(min_size,val,level) if checker < ind: lo = val else: hi = val ind -= search(min_size,lo,level) new_l = 1 while 9*(10**(new_l-1))*new_l < ind: ind -= 9*(10**(new_l-1))*new_l new_l += 1 ind -= 1 more = ind // new_l dig = ind % new_l value = 10 ** (new_l - 1) + more print(str(value)[dig]) ```
output
1
97,944
12
195,889
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between the easy and the hard versions is the maximum value of k. You are given an infinite sequence of form "112123123412345..." which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from 1 to 1, the second one — from 1 to 2, the third one — from 1 to 3, ..., the i-th block consists of all numbers from 1 to i. So the first 56 elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the 1-st element of the sequence is 1, the 3-rd element of the sequence is 2, the 20-th element of the sequence is 5, the 38-th element is 2, the 56-th element of the sequence is 0. Your task is to answer q independent queries. In the i-th query you are given one integer k_i. Calculate the digit at the position k_i of the sequence. Input The first line of the input contains one integer q (1 ≤ q ≤ 500) — the number of queries. The i-th of the following q lines contains one integer k_i (1 ≤ k_i ≤ 10^{18}) — the description of the corresponding query. Output Print q lines. In the i-th line print one digit x_i (0 ≤ x_i ≤ 9) — the answer to the query i, i.e. x_i should be equal to the element at the position k_i of the sequence. Examples Input 5 1 3 20 38 56 Output 1 2 5 2 0 Input 4 2132 506 999999999999999999 1000000000000000000 Output 8 2 4 1 Note Answers on queries from the first example are described in the problem statement. Submitted Solution: ``` def cached(func): _cache = {} def wrapped(*args): nonlocal _cache if args not in _cache: _cache[args] = func(*args) return _cache[args] return wrapped def len_num(l): """Количество чисел длины l""" return 10**l - 10**(l - 1) if l > 0 else 0 @cached def len_sum(l): """Сумма длин всех чисел, длина которых строго меньше чем l""" if l <= 1: return 0 return len_sum(l - 1) + (len_num(l - 1)) * (l - 1) def block_len(block_num): """Длина блока (т. е. строки '1234567891011...str(block_num)'""" l = len(str(block_num)) return len_sum(l) + (block_num - 10 ** (l - 1) + 1) * l def arith_sum(n): return n * (n + 1) // 2 @cached def block_len_sum_(l): """Суммарная длина всех блоков длины меньшей чем l""" if l <= 0: return 0 ln = len_num(l - 1) ls = len_sum(l - 1) return block_len_sum_(l - 1) + ls * ln + arith_sum(ln) * (l - 1) def block_len_sum(block_num): """Суммарная длина всех блоков подряд вплоть до блока block_num Если l = len(str(block_num)) """ l = len(str(block_num)) ls = len_sum(l) ln = block_num - (10 ** (l - 1)) + 1 return block_len_sum_(l) + ls * ln + l * arith_sum(ln) def binary_search(call, val): start = 1 end = 1 while call(end) <= val: end *= 2 result = start while start <= end: mid = (start + end) // 2 if call(mid) <= val: start = mid + 1 result = start else: end = mid - 1 return result cases = int(input()) for _ in range(cases): index = int(input()) - 1 block_num = binary_search(block_len_sum, index) rel_index = index - block_len_sum(block_num - 1) number = binary_search(block_len, rel_index) digit = rel_index - block_len(number - 1) print(str(number)[digit]) ```
instruction
0
97,947
12
195,894
Yes
output
1
97,947
12
195,895
Provide tags and a correct Python 3 solution for this coding contest problem. Given 2 integers u and v, find the shortest array such that [bitwise-xor](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of its elements is u, and the sum of its elements is v. Input The only line contains 2 integers u and v (0 ≤ u,v ≤ 10^{18}). Output If there's no array that satisfies the condition, print "-1". Otherwise: The first line should contain one integer, n, representing the length of the desired array. The next line should contain n positive integers, the array itself. If there are multiple possible answers, print any. Examples Input 2 4 Output 2 3 1 Input 1 3 Output 3 1 1 1 Input 8 5 Output -1 Input 0 0 Output 0 Note In the first sample, 3⊕ 1 = 2 and 3 + 1 = 4. There is no valid array of smaller length. Notice that in the fourth sample the array is empty.
instruction
0
98,018
12
196,036
Tags: bitmasks, constructive algorithms, greedy, number theory Correct Solution: ``` u,v=map(int,input().split()) if v<u or ((v%2)!=(u%2)): print(-1) elif v==u : if u!=0: print(1) print(u) else: print(0) else: x=(v-u)//2 if not u&x: print(2) print(u+x,x) else: print(3) print(u,x,x) ```
output
1
98,018
12
196,037
Provide tags and a correct Python 3 solution for this coding contest problem. Given 2 integers u and v, find the shortest array such that [bitwise-xor](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of its elements is u, and the sum of its elements is v. Input The only line contains 2 integers u and v (0 ≤ u,v ≤ 10^{18}). Output If there's no array that satisfies the condition, print "-1". Otherwise: The first line should contain one integer, n, representing the length of the desired array. The next line should contain n positive integers, the array itself. If there are multiple possible answers, print any. Examples Input 2 4 Output 2 3 1 Input 1 3 Output 3 1 1 1 Input 8 5 Output -1 Input 0 0 Output 0 Note In the first sample, 3⊕ 1 = 2 and 3 + 1 = 4. There is no valid array of smaller length. Notice that in the fourth sample the array is empty.
instruction
0
98,019
12
196,038
Tags: bitmasks, constructive algorithms, greedy, number theory Correct Solution: ``` import math from bisect import bisect_left, bisect_right from sys import stdin, stdout, setrecursionlimit from collections import Counter input = lambda: stdin.readline().strip() print = stdout.write u, v = map(int, input().split()) x = (v-u)//2 if (v-u)%2 or x<0: print('-1\n') elif u==v: if u==0: print('0\n') else: print('1\n') print(str(u)+'\n') else: p = bin(x)[2:] q = bin(u)[2:] p = p.rjust(max(len(p), len(q)), '0') q = q.rjust(max(len(p), len(q)), '0') a = '' b = '' for i in range(max(len(p), len(q))): if p[i]=='0' and q[i]=='0': a+='0' b+='0' elif p[i]=='0' and q[i]=='1': a+='0' b+='1' elif p[i]=='1' and q[i]=='0': a+='1' b+='1' else: print('3\n') print(str(u)+' '+str(x)+' '+str(x)+'\n') break else: print('2\n') print(str(int(a, 2))+' '+str(int(b, 2))+'\n') ```
output
1
98,019
12
196,039
Provide tags and a correct Python 3 solution for this coding contest problem. Given 2 integers u and v, find the shortest array such that [bitwise-xor](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of its elements is u, and the sum of its elements is v. Input The only line contains 2 integers u and v (0 ≤ u,v ≤ 10^{18}). Output If there's no array that satisfies the condition, print "-1". Otherwise: The first line should contain one integer, n, representing the length of the desired array. The next line should contain n positive integers, the array itself. If there are multiple possible answers, print any. Examples Input 2 4 Output 2 3 1 Input 1 3 Output 3 1 1 1 Input 8 5 Output -1 Input 0 0 Output 0 Note In the first sample, 3⊕ 1 = 2 and 3 + 1 = 4. There is no valid array of smaller length. Notice that in the fourth sample the array is empty.
instruction
0
98,020
12
196,040
Tags: bitmasks, constructive algorithms, greedy, number theory Correct Solution: ``` u, v = list(map(int, input().split())) rem = v-u out = [u] if rem < 0: print(-1) elif v == u == 0: print(0) elif rem == 0: print(1) print(u) elif rem%2 == 0: add = rem//2 if add+u == add^u: print(2) print(add+u, add) else: print(3) print(u, add, add) else: print(-1) ```
output
1
98,020
12
196,041
Provide tags and a correct Python 3 solution for this coding contest problem. Given 2 integers u and v, find the shortest array such that [bitwise-xor](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of its elements is u, and the sum of its elements is v. Input The only line contains 2 integers u and v (0 ≤ u,v ≤ 10^{18}). Output If there's no array that satisfies the condition, print "-1". Otherwise: The first line should contain one integer, n, representing the length of the desired array. The next line should contain n positive integers, the array itself. If there are multiple possible answers, print any. Examples Input 2 4 Output 2 3 1 Input 1 3 Output 3 1 1 1 Input 8 5 Output -1 Input 0 0 Output 0 Note In the first sample, 3⊕ 1 = 2 and 3 + 1 = 4. There is no valid array of smaller length. Notice that in the fourth sample the array is empty.
instruction
0
98,021
12
196,042
Tags: bitmasks, constructive algorithms, greedy, number theory Correct Solution: ``` xor,sm=map(int,input().split()) if(xor>sm): print("-1") elif(xor%2 != sm%2): print("-1") elif(xor==0 and sm==0): print("0") elif(xor==0): print("2") v=sm//2 print(v,v) elif(xor==1 and sm==1): print("1") print("1") elif(xor==sm): print("1") print(xor) else: val=sm-xor val//=2 AND=val a=0 b=0 bl=True for i in range(64): x1=(xor & (1<<i)) and2=(AND & (1<<i)) if(x1==0 and and2==0): continue elif(x1==0 and and2>0): a=(1<<i)|a b=(1<<i)|b elif(x1>0 and and2==0): a=((1<<i)|a) else: bl=False break if(bl==True): print("2") print(a,b) else: print("3") print(val,val,xor) ```
output
1
98,021
12
196,043
Provide tags and a correct Python 3 solution for this coding contest problem. Given 2 integers u and v, find the shortest array such that [bitwise-xor](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of its elements is u, and the sum of its elements is v. Input The only line contains 2 integers u and v (0 ≤ u,v ≤ 10^{18}). Output If there's no array that satisfies the condition, print "-1". Otherwise: The first line should contain one integer, n, representing the length of the desired array. The next line should contain n positive integers, the array itself. If there are multiple possible answers, print any. Examples Input 2 4 Output 2 3 1 Input 1 3 Output 3 1 1 1 Input 8 5 Output -1 Input 0 0 Output 0 Note In the first sample, 3⊕ 1 = 2 and 3 + 1 = 4. There is no valid array of smaller length. Notice that in the fourth sample the array is empty.
instruction
0
98,022
12
196,044
Tags: bitmasks, constructive algorithms, greedy, number theory Correct Solution: ``` """ NTC here """ import sys inp = sys.stdin.readline def input(): return inp().strip() # flush= sys.stdout.flush # import threading # sys.setrecursionlimit(10**6) # threading.stack_size(2**26) def iin(): return int(input()) def lin(): return list(map(int, input().split())) # range = xrange # input = raw_input # def BFS(s, adj, step): # parent = {s: None} # ans = {} # ch = 0 # u = [s] # while u: # runs till u is [] # nextu = [] # for i in u: # for v in adj[i]: # if v not in parent: # ans[(v, i)]=step[ch] # ch+=1 # parent[v] = i # nextu.append(v) # u = nextu.copy() # return ans def main(): T = 1 while T: T-=1 u, v = lin() if u>v:print(-1) else: a = [] ch = 0 v -= u while u: if u&1: a.append(1<<ch) ch+=1 u>>=1 if v%2==0: if v: a.append(v//2) a.append(v//2) chg = 1 while chg: chg = 0 l = len(a) na = [] done = set() for i in range(l): if i in done: continue for j in range(i+1, l): if j in done: continue else: if a[i]&a[j]==0: chg = 1 na.append(a[i]+a[j]) done.update([i, j]) break for i in range(l): if i not in done: na.append(a[i]) # print(a, na, done) a = na print(len(a)) print(*a) else: print(-1) main() # threading.Thread(target=main).start() """ 1 5 1 3 1 4 1 6 2 6 0 5 """ ```
output
1
98,022
12
196,045
Provide tags and a correct Python 3 solution for this coding contest problem. Given 2 integers u and v, find the shortest array such that [bitwise-xor](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of its elements is u, and the sum of its elements is v. Input The only line contains 2 integers u and v (0 ≤ u,v ≤ 10^{18}). Output If there's no array that satisfies the condition, print "-1". Otherwise: The first line should contain one integer, n, representing the length of the desired array. The next line should contain n positive integers, the array itself. If there are multiple possible answers, print any. Examples Input 2 4 Output 2 3 1 Input 1 3 Output 3 1 1 1 Input 8 5 Output -1 Input 0 0 Output 0 Note In the first sample, 3⊕ 1 = 2 and 3 + 1 = 4. There is no valid array of smaller length. Notice that in the fourth sample the array is empty.
instruction
0
98,023
12
196,046
Tags: bitmasks, constructive algorithms, greedy, number theory Correct Solution: ``` u, v = map(int, input().split()) if u%2 != v%2 or u > v: print(-1) exit() if u == 0 and v == 0: print(0) exit() if u == v: print(1) print(u) exit() x = (v-u)//2 if u&x == 0: print(2) print(*[u+x, x]) else: print(3) print(*[u, x, x]) ```
output
1
98,023
12
196,047
Provide tags and a correct Python 3 solution for this coding contest problem. Given 2 integers u and v, find the shortest array such that [bitwise-xor](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of its elements is u, and the sum of its elements is v. Input The only line contains 2 integers u and v (0 ≤ u,v ≤ 10^{18}). Output If there's no array that satisfies the condition, print "-1". Otherwise: The first line should contain one integer, n, representing the length of the desired array. The next line should contain n positive integers, the array itself. If there are multiple possible answers, print any. Examples Input 2 4 Output 2 3 1 Input 1 3 Output 3 1 1 1 Input 8 5 Output -1 Input 0 0 Output 0 Note In the first sample, 3⊕ 1 = 2 and 3 + 1 = 4. There is no valid array of smaller length. Notice that in the fourth sample the array is empty.
instruction
0
98,024
12
196,048
Tags: bitmasks, constructive algorithms, greedy, number theory Correct Solution: ``` import statistics import math import datetime import collections def identity(*args): if len(args) == 1: return args[0] return args def parsin(*, l=1, vpl=1, cf=identity, s=" "): if l == 1: if vpl == 1: return cf(input()) else: return list(map(cf, input().split(s))) else: if vpl == 1: return [cf(input()) for _ in range(l)] else: return [list(map(cf, input().split(s))) for _ in range(l)] def odw(l): wyn = 0 pot = 1 for i in l: wyn += pot * i pot *= 2 return wyn def konw(n): l = [] while n > 0: l.append(n % 2) n //= 2 return l def main(): x, s = map(int, input().split()) xx = konw(x) ss = konw(s) if x == 0 and s == 0: print(0) else: if x > s: print(-1) if x == s: print(1) print(x) if x < s: if (x-s) % 2 == 1: print(-1) else: kan1 = [] kan2 = [] for i in range(len(xx)): if xx[i] == 0: kan1.append(0) kan2.append(0) else: kan1.append(1) kan2.append(0) for j in range(len(xx), len(ss)): kan1.append(0) kan2.append(0) ind = len(ss)-1 while odw(kan1) + odw(kan2) < s: while ind >= 0: if not (kan1[ind] == kan2[ind] and kan1[ind] == 0): ind -= 1 else: if 2**(ind+1)+odw(kan1)+odw(kan2) > s: ind -= 1 else: break if ind == -1: break kan1[ind] = 1 kan2[ind] = 1 if odw(kan1)+odw(kan2) == s: print(2) print(odw(kan1), odw(kan2)) else: print(3) print(x, (s-x)//2, (s-x)//2) pass if __name__ == '__main__': main() ```
output
1
98,024
12
196,049
Provide tags and a correct Python 3 solution for this coding contest problem. Given 2 integers u and v, find the shortest array such that [bitwise-xor](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of its elements is u, and the sum of its elements is v. Input The only line contains 2 integers u and v (0 ≤ u,v ≤ 10^{18}). Output If there's no array that satisfies the condition, print "-1". Otherwise: The first line should contain one integer, n, representing the length of the desired array. The next line should contain n positive integers, the array itself. If there are multiple possible answers, print any. Examples Input 2 4 Output 2 3 1 Input 1 3 Output 3 1 1 1 Input 8 5 Output -1 Input 0 0 Output 0 Note In the first sample, 3⊕ 1 = 2 and 3 + 1 = 4. There is no valid array of smaller length. Notice that in the fourth sample the array is empty.
instruction
0
98,025
12
196,050
Tags: bitmasks, constructive algorithms, greedy, number theory Correct Solution: ``` from sys import stdin # input=stdin.buffer.readline input=lambda : stdin.readline().strip() lin=lambda :list(map(int,input().split())) iin=lambda :int(input()) main=lambda :map(int,input().split()) from math import ceil,sqrt,factorial,log from collections import deque from bisect import bisect_left mod=998244353 mod=1000000007 def gcd(a,b): a,b=max(a,b),min(a,b) while a%b!=0: a,b=b,a%b return b def moduloinverse(a): return(pow(a,mod-2,mod)) def solve(we): a,b=main() if a>b or a%2!=b%2: print(-1) return if a==b: if a==0: print(0) else: print(1) print(a) return x=(b-a)//2 if a&x: print(3) print(a,x,x) else: print(2) print(a^x,x) qwe=1 # qwe=iin() for _ in range(qwe): solve(_+1) ```
output
1
98,025
12
196,051
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given 2 integers u and v, find the shortest array such that [bitwise-xor](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of its elements is u, and the sum of its elements is v. Input The only line contains 2 integers u and v (0 ≤ u,v ≤ 10^{18}). Output If there's no array that satisfies the condition, print "-1". Otherwise: The first line should contain one integer, n, representing the length of the desired array. The next line should contain n positive integers, the array itself. If there are multiple possible answers, print any. Examples Input 2 4 Output 2 3 1 Input 1 3 Output 3 1 1 1 Input 8 5 Output -1 Input 0 0 Output 0 Note In the first sample, 3⊕ 1 = 2 and 3 + 1 = 4. There is no valid array of smaller length. Notice that in the fourth sample the array is empty. Submitted Solution: ``` from sys import stdin from bisect import bisect_left as bl from bisect import bisect_right as br def input(): return stdin.readline()[:-1] def intput(): return int(input()) def sinput(): return input().split() def intsput(): return map(int, sinput()) class RangedList: def __init__(self, start, stop, val=0): self.shift = 0 - start self.start = start self.stop = stop self.list = [val] * (stop - start) def __setitem__(self, key, value): self.list[key + self.shift] = value def __getitem__(self, key): return self.list[key + self.shift] def __repr__(self): return str(self.list) def __iter__(self): return iter(self.list) def dprint(*args, **kwargs): if debugging: print(*args, **kwargs) def conv(x): return int(''.join(str(k) for k in x), 2) debugging = 1 # Code xor, total = intsput() xor = bin(xor)[2:] xor = list(map(int, list('0' * (64 - len(xor)) + xor))) rem = total req = [0] * 64 for i in range(len(xor)): if xor[i]: fits = rem // 2 ** (63 - i) if not fits and xor[i]: print(-1) exit() if fits: if not xor[i]: # make even fits -= fits % 2 else: # make odd fits += (fits % 2 - 1) req[i] += 1 if fits else 0 rem -= (1 if fits else 0) * 2 ** (63 - i) for i in range(len(xor)): fits = rem // 2 ** (63 - i) if fits: fits -= fits % 2 req[i] += fits rem -= fits * 2 ** (63 - i) if rem: print(-1) exit() m = max(req) print(m) for _ in range(m): container = [0] * 64 for i in range(len(req)): if req[i]: container[i] = 1 req[i] -= 1 print(conv(container), end=' ') print() ```
instruction
0
98,026
12
196,052
Yes
output
1
98,026
12
196,053
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given 2 integers u and v, find the shortest array such that [bitwise-xor](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of its elements is u, and the sum of its elements is v. Input The only line contains 2 integers u and v (0 ≤ u,v ≤ 10^{18}). Output If there's no array that satisfies the condition, print "-1". Otherwise: The first line should contain one integer, n, representing the length of the desired array. The next line should contain n positive integers, the array itself. If there are multiple possible answers, print any. Examples Input 2 4 Output 2 3 1 Input 1 3 Output 3 1 1 1 Input 8 5 Output -1 Input 0 0 Output 0 Note In the first sample, 3⊕ 1 = 2 and 3 + 1 = 4. There is no valid array of smaller length. Notice that in the fourth sample the array is empty. Submitted Solution: ``` from collections import defaultdict as dc from collections import deque as dq from bisect import bisect_left,bisect_right,insort_left import sys import math #define of c++ as inl=input() mod=10**9 +7 def rem(p,q): z=p//q r=p-q*z return r def inp(): p=int(input()) return p def line(): p=list(map(int,input().split())) return p def count(n): if n<=0: return 0 return (n*(n+1))//2 def count(n): p=bin(n)[2:] return p.count('1') def ans(x,s): p=bin(x)[2:] q=bin(s)[2:] if len(p)>len(q): q='0'*(len(p)-len(q))+q else: p='0'*(len(q)-len(p))+p ans1='' ans2='' for i in range(len(p)): if p[i]=='0' and q[i]=='0': ans1+='0' ans2+='0' elif p[i]=='0' and q[i]=='1': ans1+='1' ans2+='1' elif p[i]=='1' and q[i]=='0': ans1+='0' ans2+='1' else: return -1,-1 #print(ans1,ans2) return int(ans1,2),int(ans2,2) xor,s=line() if xor>s: print(-1) elif s==0: print(0) elif s==xor: print(1) print(s) else: x=s-xor x=x//2 a,b=ans(xor,x) if a==-1: if xor+x+x==s: print(3) print(xor,x,x) else: print(-1) else: if a+b==s and a^b==xor: print(2) print(a,b) else: print(-1) ```
instruction
0
98,029
12
196,058
Yes
output
1
98,029
12
196,059
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two binary square matrices a and b of size n × n. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix a arbitrary number of times (0 or more): * vertical xor. You choose the number j (1 ≤ j ≤ n) and for all i (1 ≤ i ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1 (⊕ — is the operation [xor](https://en.wikipedia.org/wiki/Exclusive_or) (exclusive or)). * horizontal xor. You choose the number i (1 ≤ i ≤ n) and for all j (1 ≤ j ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1. Note that the elements of the a matrix change after each operation. For example, if n=3 and the matrix a is: $$$ \begin{pmatrix} 1 & 1 & 0 \\\ 0 & 0 & 1 \\\ 1 & 1 & 0 \end{pmatrix} $$$ Then the following sequence of operations shows an example of transformations: * vertical xor, j=1. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 1 & 0 & 1 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * horizontal xor, i=2. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 0 & 1 & 0 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * vertical xor, j=2. $$$ a= \begin{pmatrix} 0 & 0 & 0 \\\ 0 & 0 & 0 \\\ 0 & 0 & 0 \end{pmatrix} $$$ Check if there is a sequence of operations such that the matrix a becomes equal to the matrix b. Input The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the size of the matrices. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix a. An empty line follows. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix b. It is guaranteed that the sum of n over all test cases does not exceed 1000. Output For each test case, output on a separate line: * "YES", there is such a sequence of operations that the matrix a becomes equal to the matrix b; * "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 3 3 110 001 110 000 000 000 3 101 010 101 010 101 010 2 01 11 10 10 Output YES YES NO Note The first test case is explained in the statements. In the second test case, the following sequence of operations is suitable: * horizontal xor, i=1; * horizontal xor, i=2; * horizontal xor, i=3; It can be proved that there is no sequence of operations in the third test case so that the matrix a becomes equal to the matrix b.
instruction
0
98,115
12
196,230
Tags: 2-sat, brute force, constructive algorithms Correct Solution: ``` import sys #input = sys.stdin.readline for _ in range(int(input())): n=int(input()) a=[] for i in range(n): temp=input() a.append([]) for k in temp: a[-1].append(int(k)) input() b=[] for i in range(n): temp=input() b.append([]) for k in temp: b[-1].append(int(k)) f=True for i in range(n): if a[0][i]!=b[0][i]: new='' for j in range(n): a[j][i]^=1 for i in range(n): if a[i]!=b[i]: new='' for j in range(n): a[i][j]^=1 if a[i]!=b[i]: f=False break if f==False: print('NO') else: print('YES') ```
output
1
98,115
12
196,231
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two binary square matrices a and b of size n × n. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix a arbitrary number of times (0 or more): * vertical xor. You choose the number j (1 ≤ j ≤ n) and for all i (1 ≤ i ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1 (⊕ — is the operation [xor](https://en.wikipedia.org/wiki/Exclusive_or) (exclusive or)). * horizontal xor. You choose the number i (1 ≤ i ≤ n) and for all j (1 ≤ j ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1. Note that the elements of the a matrix change after each operation. For example, if n=3 and the matrix a is: $$$ \begin{pmatrix} 1 & 1 & 0 \\\ 0 & 0 & 1 \\\ 1 & 1 & 0 \end{pmatrix} $$$ Then the following sequence of operations shows an example of transformations: * vertical xor, j=1. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 1 & 0 & 1 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * horizontal xor, i=2. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 0 & 1 & 0 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * vertical xor, j=2. $$$ a= \begin{pmatrix} 0 & 0 & 0 \\\ 0 & 0 & 0 \\\ 0 & 0 & 0 \end{pmatrix} $$$ Check if there is a sequence of operations such that the matrix a becomes equal to the matrix b. Input The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the size of the matrices. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix a. An empty line follows. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix b. It is guaranteed that the sum of n over all test cases does not exceed 1000. Output For each test case, output on a separate line: * "YES", there is such a sequence of operations that the matrix a becomes equal to the matrix b; * "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 3 3 110 001 110 000 000 000 3 101 010 101 010 101 010 2 01 11 10 10 Output YES YES NO Note The first test case is explained in the statements. In the second test case, the following sequence of operations is suitable: * horizontal xor, i=1; * horizontal xor, i=2; * horizontal xor, i=3; It can be proved that there is no sequence of operations in the third test case so that the matrix a becomes equal to the matrix b.
instruction
0
98,116
12
196,232
Tags: 2-sat, brute force, constructive algorithms Correct Solution: ``` def check(r, i, j): return r[i][j]^r[i+1][j+1] == r[i+1][j]^r[i][j+1] for _ in range(int(input())): n = int(input()) a = [list(map(int, list(input()))) for i in range(n)] input() b = [list(map(int, list(input()))) for i in range(n)] r = [] for i in range(n): r.append([]) for j in range(n): r[i].append(a[i][j]^b[i][j]) #print(a,b,r) i = 0 j = n-1 ans = 'YES' for i in range(n-1): for j in range(n-1): if not check(r, i, j): ans = 'NO' break print(ans) ```
output
1
98,116
12
196,233
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two binary square matrices a and b of size n × n. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix a arbitrary number of times (0 or more): * vertical xor. You choose the number j (1 ≤ j ≤ n) and for all i (1 ≤ i ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1 (⊕ — is the operation [xor](https://en.wikipedia.org/wiki/Exclusive_or) (exclusive or)). * horizontal xor. You choose the number i (1 ≤ i ≤ n) and for all j (1 ≤ j ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1. Note that the elements of the a matrix change after each operation. For example, if n=3 and the matrix a is: $$$ \begin{pmatrix} 1 & 1 & 0 \\\ 0 & 0 & 1 \\\ 1 & 1 & 0 \end{pmatrix} $$$ Then the following sequence of operations shows an example of transformations: * vertical xor, j=1. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 1 & 0 & 1 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * horizontal xor, i=2. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 0 & 1 & 0 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * vertical xor, j=2. $$$ a= \begin{pmatrix} 0 & 0 & 0 \\\ 0 & 0 & 0 \\\ 0 & 0 & 0 \end{pmatrix} $$$ Check if there is a sequence of operations such that the matrix a becomes equal to the matrix b. Input The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the size of the matrices. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix a. An empty line follows. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix b. It is guaranteed that the sum of n over all test cases does not exceed 1000. Output For each test case, output on a separate line: * "YES", there is such a sequence of operations that the matrix a becomes equal to the matrix b; * "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 3 3 110 001 110 000 000 000 3 101 010 101 010 101 010 2 01 11 10 10 Output YES YES NO Note The first test case is explained in the statements. In the second test case, the following sequence of operations is suitable: * horizontal xor, i=1; * horizontal xor, i=2; * horizontal xor, i=3; It can be proved that there is no sequence of operations in the third test case so that the matrix a becomes equal to the matrix b.
instruction
0
98,117
12
196,234
Tags: 2-sat, brute force, constructive algorithms Correct Solution: ``` t=int(input()) for i in range(t): n=int(input()) a=[] for j in range(n): s=input() c=[] for k in s: c.append(int(k)) a.append(c) p=input() b = [] for j in range(n): s = input() c = [] for k in s: c.append(int(k)) b.append(c) res=[[0 for i in range(n)] for j in range(n)] for i in range(n): for j in range(n): if a[i][j]!=b[i][j]: res[i][j]=1 if n==1: print("YES") else: flag=1 i=0 while(i<n-1): p=abs(res[0][i]-res[0][i+1]) j=1 while(j<n): q=abs(res[j][i]-res[j][i+1]) if p!=q: flag=0 break j+=1 i+=1 if flag: print("YES") else: print("NO") ```
output
1
98,117
12
196,235
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two binary square matrices a and b of size n × n. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix a arbitrary number of times (0 or more): * vertical xor. You choose the number j (1 ≤ j ≤ n) and for all i (1 ≤ i ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1 (⊕ — is the operation [xor](https://en.wikipedia.org/wiki/Exclusive_or) (exclusive or)). * horizontal xor. You choose the number i (1 ≤ i ≤ n) and for all j (1 ≤ j ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1. Note that the elements of the a matrix change after each operation. For example, if n=3 and the matrix a is: $$$ \begin{pmatrix} 1 & 1 & 0 \\\ 0 & 0 & 1 \\\ 1 & 1 & 0 \end{pmatrix} $$$ Then the following sequence of operations shows an example of transformations: * vertical xor, j=1. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 1 & 0 & 1 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * horizontal xor, i=2. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 0 & 1 & 0 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * vertical xor, j=2. $$$ a= \begin{pmatrix} 0 & 0 & 0 \\\ 0 & 0 & 0 \\\ 0 & 0 & 0 \end{pmatrix} $$$ Check if there is a sequence of operations such that the matrix a becomes equal to the matrix b. Input The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the size of the matrices. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix a. An empty line follows. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix b. It is guaranteed that the sum of n over all test cases does not exceed 1000. Output For each test case, output on a separate line: * "YES", there is such a sequence of operations that the matrix a becomes equal to the matrix b; * "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 3 3 110 001 110 000 000 000 3 101 010 101 010 101 010 2 01 11 10 10 Output YES YES NO Note The first test case is explained in the statements. In the second test case, the following sequence of operations is suitable: * horizontal xor, i=1; * horizontal xor, i=2; * horizontal xor, i=3; It can be proved that there is no sequence of operations in the third test case so that the matrix a becomes equal to the matrix b.
instruction
0
98,118
12
196,236
Tags: 2-sat, brute force, constructive algorithms Correct Solution: ``` import sys from bisect import bisect_left input = sys.stdin.readline def solve(): n = int(input()) a = [None]*n for i in range(n): a[i] = list(map(int,input().strip())) input() for i in range(n): j = 0 for c in map(int,input().strip()): a[i][j] ^= c j += 1 one = a[0] two = [i^1 for i in a[0]] for j in range(1, n): if a[j] != one and a[j] != two: print("NO") return print("YES") for i in range(int(input())): solve() ```
output
1
98,118
12
196,237
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two binary square matrices a and b of size n × n. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix a arbitrary number of times (0 or more): * vertical xor. You choose the number j (1 ≤ j ≤ n) and for all i (1 ≤ i ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1 (⊕ — is the operation [xor](https://en.wikipedia.org/wiki/Exclusive_or) (exclusive or)). * horizontal xor. You choose the number i (1 ≤ i ≤ n) and for all j (1 ≤ j ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1. Note that the elements of the a matrix change after each operation. For example, if n=3 and the matrix a is: $$$ \begin{pmatrix} 1 & 1 & 0 \\\ 0 & 0 & 1 \\\ 1 & 1 & 0 \end{pmatrix} $$$ Then the following sequence of operations shows an example of transformations: * vertical xor, j=1. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 1 & 0 & 1 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * horizontal xor, i=2. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 0 & 1 & 0 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * vertical xor, j=2. $$$ a= \begin{pmatrix} 0 & 0 & 0 \\\ 0 & 0 & 0 \\\ 0 & 0 & 0 \end{pmatrix} $$$ Check if there is a sequence of operations such that the matrix a becomes equal to the matrix b. Input The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the size of the matrices. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix a. An empty line follows. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix b. It is guaranteed that the sum of n over all test cases does not exceed 1000. Output For each test case, output on a separate line: * "YES", there is such a sequence of operations that the matrix a becomes equal to the matrix b; * "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 3 3 110 001 110 000 000 000 3 101 010 101 010 101 010 2 01 11 10 10 Output YES YES NO Note The first test case is explained in the statements. In the second test case, the following sequence of operations is suitable: * horizontal xor, i=1; * horizontal xor, i=2; * horizontal xor, i=3; It can be proved that there is no sequence of operations in the third test case so that the matrix a becomes equal to the matrix b.
instruction
0
98,119
12
196,238
Tags: 2-sat, brute force, constructive algorithms Correct Solution: ``` import sys input = sys.stdin.readline from collections import * from bisect import * for _ in range(int(input())): n = int(input()) a = [list(map(int, input()[:-1])) for _ in range(n)] input() b = [list(map(int, input()[:-1])) for _ in range(n)] flag = [False]*n for i in range(n): if a[0][i]!=b[0][i]: flag[i] = True ok = True for i in range(1, n): c = [] for j in range(n): if flag[j]: c.append(1^a[i][j]) else: c.append(a[i][j]) xor = [c[j]^b[i][j] for j in range(n)] if not (xor==[0]*n or xor==[1]*n): ok = False break if ok: print('YES') continue flag = [False]*n for i in range(n): if a[0][i]==b[0][i]: flag[i] = True ok = True for i in range(1, n): c = [] for j in range(n): if flag[j]: c.append(1^a[i][j]) else: c.append(a[i][j]) xor = [c[j]^b[i][j] for j in range(n)] if not (xor==[0]*n or xor==[1]*n): ok = False break if ok: print('YES') else: print('NO') ```
output
1
98,119
12
196,239
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two binary square matrices a and b of size n × n. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix a arbitrary number of times (0 or more): * vertical xor. You choose the number j (1 ≤ j ≤ n) and for all i (1 ≤ i ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1 (⊕ — is the operation [xor](https://en.wikipedia.org/wiki/Exclusive_or) (exclusive or)). * horizontal xor. You choose the number i (1 ≤ i ≤ n) and for all j (1 ≤ j ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1. Note that the elements of the a matrix change after each operation. For example, if n=3 and the matrix a is: $$$ \begin{pmatrix} 1 & 1 & 0 \\\ 0 & 0 & 1 \\\ 1 & 1 & 0 \end{pmatrix} $$$ Then the following sequence of operations shows an example of transformations: * vertical xor, j=1. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 1 & 0 & 1 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * horizontal xor, i=2. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 0 & 1 & 0 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * vertical xor, j=2. $$$ a= \begin{pmatrix} 0 & 0 & 0 \\\ 0 & 0 & 0 \\\ 0 & 0 & 0 \end{pmatrix} $$$ Check if there is a sequence of operations such that the matrix a becomes equal to the matrix b. Input The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the size of the matrices. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix a. An empty line follows. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix b. It is guaranteed that the sum of n over all test cases does not exceed 1000. Output For each test case, output on a separate line: * "YES", there is such a sequence of operations that the matrix a becomes equal to the matrix b; * "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 3 3 110 001 110 000 000 000 3 101 010 101 010 101 010 2 01 11 10 10 Output YES YES NO Note The first test case is explained in the statements. In the second test case, the following sequence of operations is suitable: * horizontal xor, i=1; * horizontal xor, i=2; * horizontal xor, i=3; It can be proved that there is no sequence of operations in the third test case so that the matrix a becomes equal to the matrix b.
instruction
0
98,120
12
196,240
Tags: 2-sat, brute force, constructive algorithms Correct Solution: ``` import sys input = lambda: sys.stdin.readline().rstrip() T = int(input()) for _ in range(T): N = int(input()) A = [[int(a) for a in input()] for _ in range(N)] input() for i in range(N): for j, a in enumerate([int(a) for a in input()]): A[i][j] ^= a if A[0][0]: for j in range(N): A[0][j] ^= 1 for j in range(N): if A[0][j]: for i in range(N): A[i][j] ^= 1 for i in range(1, N): if A[i][0]: for j in range(N): A[i][j] ^= 1 print("YES" if sum(sum(a) for a in A) == 0 else "NO") ```
output
1
98,120
12
196,241
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two binary square matrices a and b of size n × n. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix a arbitrary number of times (0 or more): * vertical xor. You choose the number j (1 ≤ j ≤ n) and for all i (1 ≤ i ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1 (⊕ — is the operation [xor](https://en.wikipedia.org/wiki/Exclusive_or) (exclusive or)). * horizontal xor. You choose the number i (1 ≤ i ≤ n) and for all j (1 ≤ j ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1. Note that the elements of the a matrix change after each operation. For example, if n=3 and the matrix a is: $$$ \begin{pmatrix} 1 & 1 & 0 \\\ 0 & 0 & 1 \\\ 1 & 1 & 0 \end{pmatrix} $$$ Then the following sequence of operations shows an example of transformations: * vertical xor, j=1. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 1 & 0 & 1 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * horizontal xor, i=2. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 0 & 1 & 0 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * vertical xor, j=2. $$$ a= \begin{pmatrix} 0 & 0 & 0 \\\ 0 & 0 & 0 \\\ 0 & 0 & 0 \end{pmatrix} $$$ Check if there is a sequence of operations such that the matrix a becomes equal to the matrix b. Input The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the size of the matrices. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix a. An empty line follows. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix b. It is guaranteed that the sum of n over all test cases does not exceed 1000. Output For each test case, output on a separate line: * "YES", there is such a sequence of operations that the matrix a becomes equal to the matrix b; * "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 3 3 110 001 110 000 000 000 3 101 010 101 010 101 010 2 01 11 10 10 Output YES YES NO Note The first test case is explained in the statements. In the second test case, the following sequence of operations is suitable: * horizontal xor, i=1; * horizontal xor, i=2; * horizontal xor, i=3; It can be proved that there is no sequence of operations in the third test case so that the matrix a becomes equal to the matrix b.
instruction
0
98,121
12
196,242
Tags: 2-sat, brute force, constructive algorithms Correct Solution: ``` # ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------- fast io -------------------- from math import gcd, ceil def prod(a, mod=10**9+7): ans = 1 for each in a: ans = (ans * each) % mod return ans def lcm(a, b): return a * b // gcd(a, b) def binary(x, length=16): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y for _ in range(int(input()) if True else 1): n = int(input()) #n, k = map(int, input().split()) #a, b = map(int, input().split()) #c, d = map(int, input().split()) #a = list(map(int, input().split())) #b = list(map(int, input().split())) #s = input() a = [] b = [] for i in range(n): a += [[int(k) for k in input()]] s = input() for i in range(n): b += [[int(k) for k in input()]] if n == 1: print("YES") continue v = [-1]*n h = [0]*n v[0] = 0 pos = True i = 0 for j in range(n): diff = (b[i][j] - a[i][j]) % 2 # (v[i] + h[j]) = diff (mod 2) h[j] = (diff - v[i]) % 2 for i in range(1, n): for j in range(n): diff = (b[i][j] - a[i][j]) % 2 vi = (diff - h[j]) % 2 if v[i] == -1: v[i] = vi elif v[i] != vi: pos = False break if not pos:break if pos: print("YES") continue v = [-1] * n h = [0] * n v[0] = 1 pos = True i = 0 for j in range(n): diff = (b[i][j] - a[i][j]) % 2 # (v[i] + h[j]) = diff (mod 2) h[j] = (diff - v[i]) % 2 for i in range(1, n): for j in range(n): diff = (b[i][j] - a[i][j]) % 2 vi = (diff - h[j]) % 2 if v[i] == -1: v[i] = vi elif v[i] != vi: pos = False break if not pos: break if pos: print("YES") continue print("NO") ```
output
1
98,121
12
196,243
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two binary square matrices a and b of size n × n. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix a arbitrary number of times (0 or more): * vertical xor. You choose the number j (1 ≤ j ≤ n) and for all i (1 ≤ i ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1 (⊕ — is the operation [xor](https://en.wikipedia.org/wiki/Exclusive_or) (exclusive or)). * horizontal xor. You choose the number i (1 ≤ i ≤ n) and for all j (1 ≤ j ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1. Note that the elements of the a matrix change after each operation. For example, if n=3 and the matrix a is: $$$ \begin{pmatrix} 1 & 1 & 0 \\\ 0 & 0 & 1 \\\ 1 & 1 & 0 \end{pmatrix} $$$ Then the following sequence of operations shows an example of transformations: * vertical xor, j=1. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 1 & 0 & 1 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * horizontal xor, i=2. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 0 & 1 & 0 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * vertical xor, j=2. $$$ a= \begin{pmatrix} 0 & 0 & 0 \\\ 0 & 0 & 0 \\\ 0 & 0 & 0 \end{pmatrix} $$$ Check if there is a sequence of operations such that the matrix a becomes equal to the matrix b. Input The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the size of the matrices. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix a. An empty line follows. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix b. It is guaranteed that the sum of n over all test cases does not exceed 1000. Output For each test case, output on a separate line: * "YES", there is such a sequence of operations that the matrix a becomes equal to the matrix b; * "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 3 3 110 001 110 000 000 000 3 101 010 101 010 101 010 2 01 11 10 10 Output YES YES NO Note The first test case is explained in the statements. In the second test case, the following sequence of operations is suitable: * horizontal xor, i=1; * horizontal xor, i=2; * horizontal xor, i=3; It can be proved that there is no sequence of operations in the third test case so that the matrix a becomes equal to the matrix b.
instruction
0
98,122
12
196,244
Tags: 2-sat, brute force, constructive algorithms Correct Solution: ``` import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) mat_a = [] mat_b = [] for i in range(n): row = list(input()) mat_a.append(row) extra = input() for i in range(n): row = list(input()) mat_b.append(row) possible = True rows = [0] * n columns = [0] * n for i in range(n): for j in range(n): if not possible: break element_a = int(mat_a[i][j]) element_b = int(mat_b[i][j]) if (element_a ^ rows[i] ^ columns[j]) != element_b: if i == 0 and j == 0: rows[i] = 1 elif i == 0: columns[j] = 1 elif j == 0: rows[i] = 1 else: possible = False break print("YES" if possible else "NO") ```
output
1
98,122
12
196,245
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two binary square matrices a and b of size n × n. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix a arbitrary number of times (0 or more): * vertical xor. You choose the number j (1 ≤ j ≤ n) and for all i (1 ≤ i ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1 (⊕ — is the operation [xor](https://en.wikipedia.org/wiki/Exclusive_or) (exclusive or)). * horizontal xor. You choose the number i (1 ≤ i ≤ n) and for all j (1 ≤ j ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1. Note that the elements of the a matrix change after each operation. For example, if n=3 and the matrix a is: $$$ \begin{pmatrix} 1 & 1 & 0 \\\ 0 & 0 & 1 \\\ 1 & 1 & 0 \end{pmatrix} $$$ Then the following sequence of operations shows an example of transformations: * vertical xor, j=1. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 1 & 0 & 1 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * horizontal xor, i=2. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 0 & 1 & 0 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * vertical xor, j=2. $$$ a= \begin{pmatrix} 0 & 0 & 0 \\\ 0 & 0 & 0 \\\ 0 & 0 & 0 \end{pmatrix} $$$ Check if there is a sequence of operations such that the matrix a becomes equal to the matrix b. Input The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the size of the matrices. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix a. An empty line follows. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix b. It is guaranteed that the sum of n over all test cases does not exceed 1000. Output For each test case, output on a separate line: * "YES", there is such a sequence of operations that the matrix a becomes equal to the matrix b; * "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 3 3 110 001 110 000 000 000 3 101 010 101 010 101 010 2 01 11 10 10 Output YES YES NO Note The first test case is explained in the statements. In the second test case, the following sequence of operations is suitable: * horizontal xor, i=1; * horizontal xor, i=2; * horizontal xor, i=3; It can be proved that there is no sequence of operations in the third test case so that the matrix a becomes equal to the matrix b. Submitted Solution: ``` #CFR-697-F for _ in range(int(input())): n = int(input()) A = [tuple(map(int, input())) for _ in range(n)] e = input() B = [tuple(map(int, input())) for _ in range(n)] V = [[A[i][j]^B[i][j] for j in range(n)] for i in range(n)] #print(V) if V[0] == [0]*n: flag = 1 for i in range(1, n): if V[i] != [0]*n and V[i] != [1]*n: flag = 0 break if flag: print('YES') else: print('NO') else: flag = 1 for i in range(1,n): W = [V[i][j]^V[0][j] for j in range(n)] if W != [0]*n and W != [1]*n: flag = 0 break if flag: print('YES') else: print('NO') ```
instruction
0
98,123
12
196,246
Yes
output
1
98,123
12
196,247
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two binary square matrices a and b of size n × n. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix a arbitrary number of times (0 or more): * vertical xor. You choose the number j (1 ≤ j ≤ n) and for all i (1 ≤ i ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1 (⊕ — is the operation [xor](https://en.wikipedia.org/wiki/Exclusive_or) (exclusive or)). * horizontal xor. You choose the number i (1 ≤ i ≤ n) and for all j (1 ≤ j ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1. Note that the elements of the a matrix change after each operation. For example, if n=3 and the matrix a is: $$$ \begin{pmatrix} 1 & 1 & 0 \\\ 0 & 0 & 1 \\\ 1 & 1 & 0 \end{pmatrix} $$$ Then the following sequence of operations shows an example of transformations: * vertical xor, j=1. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 1 & 0 & 1 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * horizontal xor, i=2. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 0 & 1 & 0 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * vertical xor, j=2. $$$ a= \begin{pmatrix} 0 & 0 & 0 \\\ 0 & 0 & 0 \\\ 0 & 0 & 0 \end{pmatrix} $$$ Check if there is a sequence of operations such that the matrix a becomes equal to the matrix b. Input The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the size of the matrices. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix a. An empty line follows. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix b. It is guaranteed that the sum of n over all test cases does not exceed 1000. Output For each test case, output on a separate line: * "YES", there is such a sequence of operations that the matrix a becomes equal to the matrix b; * "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 3 3 110 001 110 000 000 000 3 101 010 101 010 101 010 2 01 11 10 10 Output YES YES NO Note The first test case is explained in the statements. In the second test case, the following sequence of operations is suitable: * horizontal xor, i=1; * horizontal xor, i=2; * horizontal xor, i=3; It can be proved that there is no sequence of operations in the third test case so that the matrix a becomes equal to the matrix b. Submitted Solution: ``` t=int(input()) for _ in range(t): n=int(input()) a=[] for i in range(n): a.append(input()) b=[] input() for i in range(n): b.append(input()) grid=[] for i in range(n): grid.append([]) for j in range(n): if a[i][j]==b[i][j]: grid[-1].append(0) else: grid[-1].append(1) first=[] second=[] for i in range(n): if grid[i][0]==grid[0][0]: first.append(0) else: first.append(1) for i in range(n): if grid[0][i]==grid[0][0]: second.append(0) else: second.append(1) flag=0 for i in range(n): for j in range(n): if 1^(grid[i][j]==grid[0][0])^(first[i]^second[j]): flag=1 break if flag: break if flag: print('NO') else: print('YES') ```
instruction
0
98,124
12
196,248
Yes
output
1
98,124
12
196,249
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two binary square matrices a and b of size n × n. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix a arbitrary number of times (0 or more): * vertical xor. You choose the number j (1 ≤ j ≤ n) and for all i (1 ≤ i ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1 (⊕ — is the operation [xor](https://en.wikipedia.org/wiki/Exclusive_or) (exclusive or)). * horizontal xor. You choose the number i (1 ≤ i ≤ n) and for all j (1 ≤ j ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1. Note that the elements of the a matrix change after each operation. For example, if n=3 and the matrix a is: $$$ \begin{pmatrix} 1 & 1 & 0 \\\ 0 & 0 & 1 \\\ 1 & 1 & 0 \end{pmatrix} $$$ Then the following sequence of operations shows an example of transformations: * vertical xor, j=1. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 1 & 0 & 1 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * horizontal xor, i=2. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 0 & 1 & 0 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * vertical xor, j=2. $$$ a= \begin{pmatrix} 0 & 0 & 0 \\\ 0 & 0 & 0 \\\ 0 & 0 & 0 \end{pmatrix} $$$ Check if there is a sequence of operations such that the matrix a becomes equal to the matrix b. Input The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the size of the matrices. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix a. An empty line follows. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix b. It is guaranteed that the sum of n over all test cases does not exceed 1000. Output For each test case, output on a separate line: * "YES", there is such a sequence of operations that the matrix a becomes equal to the matrix b; * "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 3 3 110 001 110 000 000 000 3 101 010 101 010 101 010 2 01 11 10 10 Output YES YES NO Note The first test case is explained in the statements. In the second test case, the following sequence of operations is suitable: * horizontal xor, i=1; * horizontal xor, i=2; * horizontal xor, i=3; It can be proved that there is no sequence of operations in the third test case so that the matrix a becomes equal to the matrix b. Submitted Solution: ``` t = int(input()) for _ in range(t): n = int(input()) start = [] for _ in range(n): start.append([int(i) for i in input()]) input() target = [] for _ in range(n): target.append([int(i) for i in input()]) # for row in start: # print(*row) # print() # for row in target: # print(*row) # print() for row in range(n): for col in range(n): if target[row][col] == 1: start[row][col] = 1 - start[row][col] passed = True val = start[0][0] for row in range(n-1): for col in range(n-1): v = start[row][col] if not(v == start[row+1][col+1] and v != start[row+1][col] and v != start[row][col+1]): passed = False if not passed: break if not passed: break if passed: print('YES') else: switch_val = n//2 + 1 for row in start: if sum(row) >= switch_val: for i in range(n): row[i] = 1 - row[i] # for row in start: # print(*row) # print() start = list(map(list, zip(*start))) switch_val = n//2 + 1 for row in start: if sum(row) >= switch_val: for i in range(n): row[i] = 1 - row[i] # for row in start: # print(*row) # print() out = 'YES' for col in range(n): x = sum(row[col] for row in start) if not (x == 0 or x == n): out = 'NO' break print(out) ```
instruction
0
98,125
12
196,250
Yes
output
1
98,125
12
196,251
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two binary square matrices a and b of size n × n. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix a arbitrary number of times (0 or more): * vertical xor. You choose the number j (1 ≤ j ≤ n) and for all i (1 ≤ i ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1 (⊕ — is the operation [xor](https://en.wikipedia.org/wiki/Exclusive_or) (exclusive or)). * horizontal xor. You choose the number i (1 ≤ i ≤ n) and for all j (1 ≤ j ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1. Note that the elements of the a matrix change after each operation. For example, if n=3 and the matrix a is: $$$ \begin{pmatrix} 1 & 1 & 0 \\\ 0 & 0 & 1 \\\ 1 & 1 & 0 \end{pmatrix} $$$ Then the following sequence of operations shows an example of transformations: * vertical xor, j=1. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 1 & 0 & 1 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * horizontal xor, i=2. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 0 & 1 & 0 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * vertical xor, j=2. $$$ a= \begin{pmatrix} 0 & 0 & 0 \\\ 0 & 0 & 0 \\\ 0 & 0 & 0 \end{pmatrix} $$$ Check if there is a sequence of operations such that the matrix a becomes equal to the matrix b. Input The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the size of the matrices. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix a. An empty line follows. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix b. It is guaranteed that the sum of n over all test cases does not exceed 1000. Output For each test case, output on a separate line: * "YES", there is such a sequence of operations that the matrix a becomes equal to the matrix b; * "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 3 3 110 001 110 000 000 000 3 101 010 101 010 101 010 2 01 11 10 10 Output YES YES NO Note The first test case is explained in the statements. In the second test case, the following sequence of operations is suitable: * horizontal xor, i=1; * horizontal xor, i=2; * horizontal xor, i=3; It can be proved that there is no sequence of operations in the third test case so that the matrix a becomes equal to the matrix b. Submitted Solution: ``` from sys import stdin from collections import defaultdict t = int(stdin.readline()) def duoc(mata, matb): mau = [] for col in range(n): mau.append(1 - (mata[0][col] == matb[0][col])) for row in range(1, n): hang = [] for col in range(n): if mata[row][col] == matb[row][col]: if mau == 1: hang.append(1) else: hang.append(0) else: if mau == 1: hang.append(0) else: hang.append(1) moc = (hang[0] + mau[0]) % 2 for col in range(1, n): if (hang[col] + mau[col]) % 2 != moc: return False return True for _ in range(t): mata = [] matb = [] n = int(stdin.readline()) for row in range(n): mata.append(stdin.readline()[:-1]) blank = stdin.readline() for row in range(n): matb.append(stdin.readline()[:-1]) if duoc(mata, matb) == True: print('yes') else: print('no') ```
instruction
0
98,126
12
196,252
Yes
output
1
98,126
12
196,253
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two binary square matrices a and b of size n × n. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix a arbitrary number of times (0 or more): * vertical xor. You choose the number j (1 ≤ j ≤ n) and for all i (1 ≤ i ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1 (⊕ — is the operation [xor](https://en.wikipedia.org/wiki/Exclusive_or) (exclusive or)). * horizontal xor. You choose the number i (1 ≤ i ≤ n) and for all j (1 ≤ j ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1. Note that the elements of the a matrix change after each operation. For example, if n=3 and the matrix a is: $$$ \begin{pmatrix} 1 & 1 & 0 \\\ 0 & 0 & 1 \\\ 1 & 1 & 0 \end{pmatrix} $$$ Then the following sequence of operations shows an example of transformations: * vertical xor, j=1. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 1 & 0 & 1 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * horizontal xor, i=2. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 0 & 1 & 0 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * vertical xor, j=2. $$$ a= \begin{pmatrix} 0 & 0 & 0 \\\ 0 & 0 & 0 \\\ 0 & 0 & 0 \end{pmatrix} $$$ Check if there is a sequence of operations such that the matrix a becomes equal to the matrix b. Input The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the size of the matrices. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix a. An empty line follows. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix b. It is guaranteed that the sum of n over all test cases does not exceed 1000. Output For each test case, output on a separate line: * "YES", there is such a sequence of operations that the matrix a becomes equal to the matrix b; * "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 3 3 110 001 110 000 000 000 3 101 010 101 010 101 010 2 01 11 10 10 Output YES YES NO Note The first test case is explained in the statements. In the second test case, the following sequence of operations is suitable: * horizontal xor, i=1; * horizontal xor, i=2; * horizontal xor, i=3; It can be proved that there is no sequence of operations in the third test case so that the matrix a becomes equal to the matrix b. Submitted Solution: ``` test=int(input()) for testcase in range(test): n=int(input()) a=[] b=[] for i in range(n): s=input() arr=[int(x) for x in s] a.append(arr) space=input() for i in range(n): s=input() arr=[int(x) for x in s] b.append(arr) if(n==1): print("YES") continue for i in range(n): if(a[0][i]!=b[0][i]): for j in range(n): a[j][i]=a[j][i]^1 last=True for i in range(1,n): flag=True for j in range(n): if(a[i][j]!=b[i][j]): if(flag==True and j<n-1): flag=False for k in range(n): a[i][k]=a[i][k]^1 else: print("NO") last=False break if(last==False): break else: print("YES") ```
instruction
0
98,127
12
196,254
No
output
1
98,127
12
196,255
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two binary square matrices a and b of size n × n. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix a arbitrary number of times (0 or more): * vertical xor. You choose the number j (1 ≤ j ≤ n) and for all i (1 ≤ i ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1 (⊕ — is the operation [xor](https://en.wikipedia.org/wiki/Exclusive_or) (exclusive or)). * horizontal xor. You choose the number i (1 ≤ i ≤ n) and for all j (1 ≤ j ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1. Note that the elements of the a matrix change after each operation. For example, if n=3 and the matrix a is: $$$ \begin{pmatrix} 1 & 1 & 0 \\\ 0 & 0 & 1 \\\ 1 & 1 & 0 \end{pmatrix} $$$ Then the following sequence of operations shows an example of transformations: * vertical xor, j=1. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 1 & 0 & 1 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * horizontal xor, i=2. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 0 & 1 & 0 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * vertical xor, j=2. $$$ a= \begin{pmatrix} 0 & 0 & 0 \\\ 0 & 0 & 0 \\\ 0 & 0 & 0 \end{pmatrix} $$$ Check if there is a sequence of operations such that the matrix a becomes equal to the matrix b. Input The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the size of the matrices. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix a. An empty line follows. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix b. It is guaranteed that the sum of n over all test cases does not exceed 1000. Output For each test case, output on a separate line: * "YES", there is such a sequence of operations that the matrix a becomes equal to the matrix b; * "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 3 3 110 001 110 000 000 000 3 101 010 101 010 101 010 2 01 11 10 10 Output YES YES NO Note The first test case is explained in the statements. In the second test case, the following sequence of operations is suitable: * horizontal xor, i=1; * horizontal xor, i=2; * horizontal xor, i=3; It can be proved that there is no sequence of operations in the third test case so that the matrix a becomes equal to the matrix b. Submitted Solution: ``` for _ in range(int(input())): A = [int(input(), 2) for _ in range(int(input()))] input() for i, a in enumerate(A): A[i] = a ^ int(input(), 2) t = A.pop() for a in A: a ^= t if a & (a + 1): print('NO') break else: print('YES') ```
instruction
0
98,128
12
196,256
No
output
1
98,128
12
196,257
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two binary square matrices a and b of size n × n. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix a arbitrary number of times (0 or more): * vertical xor. You choose the number j (1 ≤ j ≤ n) and for all i (1 ≤ i ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1 (⊕ — is the operation [xor](https://en.wikipedia.org/wiki/Exclusive_or) (exclusive or)). * horizontal xor. You choose the number i (1 ≤ i ≤ n) and for all j (1 ≤ j ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1. Note that the elements of the a matrix change after each operation. For example, if n=3 and the matrix a is: $$$ \begin{pmatrix} 1 & 1 & 0 \\\ 0 & 0 & 1 \\\ 1 & 1 & 0 \end{pmatrix} $$$ Then the following sequence of operations shows an example of transformations: * vertical xor, j=1. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 1 & 0 & 1 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * horizontal xor, i=2. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 0 & 1 & 0 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * vertical xor, j=2. $$$ a= \begin{pmatrix} 0 & 0 & 0 \\\ 0 & 0 & 0 \\\ 0 & 0 & 0 \end{pmatrix} $$$ Check if there is a sequence of operations such that the matrix a becomes equal to the matrix b. Input The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the size of the matrices. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix a. An empty line follows. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix b. It is guaranteed that the sum of n over all test cases does not exceed 1000. Output For each test case, output on a separate line: * "YES", there is such a sequence of operations that the matrix a becomes equal to the matrix b; * "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 3 3 110 001 110 000 000 000 3 101 010 101 010 101 010 2 01 11 10 10 Output YES YES NO Note The first test case is explained in the statements. In the second test case, the following sequence of operations is suitable: * horizontal xor, i=1; * horizontal xor, i=2; * horizontal xor, i=3; It can be proved that there is no sequence of operations in the third test case so that the matrix a becomes equal to the matrix b. Submitted Solution: ``` import sys, io, os if os.environ['USERNAME']=='kissz': inp=open('in.txt','r').readline def debug(*args): print(*args,file=sys.stderr) else: inp=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def debug(*args): pass # SCRIPT STARTS HERE for _ in range(int(inp())): n=int(inp()) A=[] for _ in range(n): A.append([*map(int,list(inp().strip()))]) inp() B=[] for _ in range(n): B.append([*map(int,list(inp().strip()))]) debug(A,B) for i in range(n): if A[0][i]!=B[0][i]: for j in range(n): A[j][i]=1-A[j][i] for j in range(1,n): if A[j][0]!=B[j][0]: for i in range(n): A[j][i]=1-A[j][i] if A==B: print('YES') else: print('NO') ```
instruction
0
98,129
12
196,258
No
output
1
98,129
12
196,259
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two binary square matrices a and b of size n × n. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix a arbitrary number of times (0 or more): * vertical xor. You choose the number j (1 ≤ j ≤ n) and for all i (1 ≤ i ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1 (⊕ — is the operation [xor](https://en.wikipedia.org/wiki/Exclusive_or) (exclusive or)). * horizontal xor. You choose the number i (1 ≤ i ≤ n) and for all j (1 ≤ j ≤ n) do the following: a_{i, j} := a_{i, j} ⊕ 1. Note that the elements of the a matrix change after each operation. For example, if n=3 and the matrix a is: $$$ \begin{pmatrix} 1 & 1 & 0 \\\ 0 & 0 & 1 \\\ 1 & 1 & 0 \end{pmatrix} $$$ Then the following sequence of operations shows an example of transformations: * vertical xor, j=1. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 1 & 0 & 1 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * horizontal xor, i=2. $$$ a= \begin{pmatrix} 0 & 1 & 0 \\\ 0 & 1 & 0 \\\ 0 & 1 & 0 \end{pmatrix} $$$ * vertical xor, j=2. $$$ a= \begin{pmatrix} 0 & 0 & 0 \\\ 0 & 0 & 0 \\\ 0 & 0 & 0 \end{pmatrix} $$$ Check if there is a sequence of operations such that the matrix a becomes equal to the matrix b. Input The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. Then t test cases follow. The first line of each test case contains one integer n (1 ≤ n ≤ 1000) — the size of the matrices. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix a. An empty line follows. The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix b. It is guaranteed that the sum of n over all test cases does not exceed 1000. Output For each test case, output on a separate line: * "YES", there is such a sequence of operations that the matrix a becomes equal to the matrix b; * "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 3 3 110 001 110 000 000 000 3 101 010 101 010 101 010 2 01 11 10 10 Output YES YES NO Note The first test case is explained in the statements. In the second test case, the following sequence of operations is suitable: * horizontal xor, i=1; * horizontal xor, i=2; * horizontal xor, i=3; It can be proved that there is no sequence of operations in the third test case so that the matrix a becomes equal to the matrix b. Submitted Solution: ``` def checkZero(mat,n): for i in range(1,n): if mat[i]!=mat[i-1]: for j in range(n): mat[i][j]^=1 if mat[i]!=mat[i-1]: return False return True for _ in range(int(input())): n=int(input()) a=[] b=[] for i in range(n): a.append(list(map(int,list(input())))) input() for i in range(n): b.append(list(map(int,list(input())))) if checkZero(a,n)==True and checkZero(b,n)==True: print('YES') else: print('NO') ```
instruction
0
98,130
12
196,260
No
output
1
98,130
12
196,261
Provide tags and a correct Python 3 solution for this coding contest problem. Little Dima has two sequences of points with integer coordinates: sequence (a1, 1), (a2, 2), ..., (an, n) and sequence (b1, 1), (b2, 2), ..., (bn, n). Now Dima wants to count the number of distinct sequences of points of length 2·n that can be assembled from these sequences, such that the x-coordinates of points in the assembled sequence will not decrease. Help him with that. Note that each element of the initial sequences should be used exactly once in the assembled sequence. Dima considers two assembled sequences (p1, q1), (p2, q2), ..., (p2·n, q2·n) and (x1, y1), (x2, y2), ..., (x2·n, y2·n) distinct, if there is such i (1 ≤ i ≤ 2·n), that (pi, qi) ≠ (xi, yi). As the answer can be rather large, print the remainder from dividing the answer by number m. Input The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 109). The numbers in the lines are separated by spaces. The last line contains integer m (2 ≤ m ≤ 109 + 7). Output In the single line print the remainder after dividing the answer to the problem by number m. Examples Input 1 1 2 7 Output 1 Input 2 1 2 2 3 11 Output 2 Note In the first sample you can get only one sequence: (1, 1), (2, 1). In the second sample you can get such sequences : (1, 1), (2, 2), (2, 1), (3, 2); (1, 1), (2, 1), (2, 2), (3, 2). Thus, the answer is 2.
instruction
0
98,162
12
196,324
Tags: combinatorics, math, sortings Correct Solution: ``` n = input() c_n = {} d_n = {} for a_i, b_i in zip(input().split(), input().split()): if a_i == b_i: d_n[a_i] = d_n.get(a_i,0) + 2 c_n[a_i] = c_n.get(a_i,0) + 1 c_n[b_i] = c_n.get(b_i,0) + 1 result = 1 k = int(input()) for a_i, cant in c_n.items(): cant_rep = d_n.get(a_i,0) for i in range(cant - cant_rep + 1, cant, 2): result = (result * i * (i + 1) // 2) % k for i in range(1, cant - cant_rep + 1): result = (result * i) % k print(result) ```
output
1
98,162
12
196,325
Provide tags and a correct Python 3 solution for this coding contest problem. Little Dima has two sequences of points with integer coordinates: sequence (a1, 1), (a2, 2), ..., (an, n) and sequence (b1, 1), (b2, 2), ..., (bn, n). Now Dima wants to count the number of distinct sequences of points of length 2·n that can be assembled from these sequences, such that the x-coordinates of points in the assembled sequence will not decrease. Help him with that. Note that each element of the initial sequences should be used exactly once in the assembled sequence. Dima considers two assembled sequences (p1, q1), (p2, q2), ..., (p2·n, q2·n) and (x1, y1), (x2, y2), ..., (x2·n, y2·n) distinct, if there is such i (1 ≤ i ≤ 2·n), that (pi, qi) ≠ (xi, yi). As the answer can be rather large, print the remainder from dividing the answer by number m. Input The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 109). The numbers in the lines are separated by spaces. The last line contains integer m (2 ≤ m ≤ 109 + 7). Output In the single line print the remainder after dividing the answer to the problem by number m. Examples Input 1 1 2 7 Output 1 Input 2 1 2 2 3 11 Output 2 Note In the first sample you can get only one sequence: (1, 1), (2, 1). In the second sample you can get such sequences : (1, 1), (2, 2), (2, 1), (3, 2); (1, 1), (2, 1), (2, 2), (3, 2). Thus, the answer is 2.
instruction
0
98,163
12
196,326
Tags: combinatorics, math, sortings Correct Solution: ``` n = int(input()) c, d = {}, {} for x, y in zip(input().split(), input().split()): c[x] = c.get(x, 1) + 1 c[y] = c.get(y, 1) + 1 if x == y: d[x] = d.get(x, 0) + 2 s, m = 1, int(input()) for k, v in c.items(): u = d.get(k, 0) for i in range(v - u, v, 2): s = s * (i * i + i) // 2 % m for i in range(1, v - u): s = s * i % m print(s) ```
output
1
98,163
12
196,327
Provide tags and a correct Python 3 solution for this coding contest problem. Little Dima has two sequences of points with integer coordinates: sequence (a1, 1), (a2, 2), ..., (an, n) and sequence (b1, 1), (b2, 2), ..., (bn, n). Now Dima wants to count the number of distinct sequences of points of length 2·n that can be assembled from these sequences, such that the x-coordinates of points in the assembled sequence will not decrease. Help him with that. Note that each element of the initial sequences should be used exactly once in the assembled sequence. Dima considers two assembled sequences (p1, q1), (p2, q2), ..., (p2·n, q2·n) and (x1, y1), (x2, y2), ..., (x2·n, y2·n) distinct, if there is such i (1 ≤ i ≤ 2·n), that (pi, qi) ≠ (xi, yi). As the answer can be rather large, print the remainder from dividing the answer by number m. Input The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 109). The numbers in the lines are separated by spaces. The last line contains integer m (2 ≤ m ≤ 109 + 7). Output In the single line print the remainder after dividing the answer to the problem by number m. Examples Input 1 1 2 7 Output 1 Input 2 1 2 2 3 11 Output 2 Note In the first sample you can get only one sequence: (1, 1), (2, 1). In the second sample you can get such sequences : (1, 1), (2, 2), (2, 1), (3, 2); (1, 1), (2, 1), (2, 2), (3, 2). Thus, the answer is 2.
instruction
0
98,164
12
196,328
Tags: combinatorics, math, sortings Correct Solution: ``` from collections import defaultdict def factorial(n, m, rep): r = 1 for i in range(2, n + 1): j = i while j % 2 == 0 and rep > 0: j = j// 2 rep -= 1 r *= j r %= m return r n=int(input()) a=list(map(int,input().split())) b=list(map(int,input().split())) m=int(input()) ans=1 d=defaultdict(lambda:0) e=defaultdict(lambda:0) i=0 while(i<n): d[a[i]]+=1 d[b[i]]+=1 if a[i]==b[i]: e[a[i]]+=1 i+=1 ans=1 for j in d: k=d[j] rep=e[j] ans=(ans*factorial(k,m,rep)) ans=ans%m print(int(ans)) ```
output
1
98,164
12
196,329
Provide tags and a correct Python 3 solution for this coding contest problem. Little Dima has two sequences of points with integer coordinates: sequence (a1, 1), (a2, 2), ..., (an, n) and sequence (b1, 1), (b2, 2), ..., (bn, n). Now Dima wants to count the number of distinct sequences of points of length 2·n that can be assembled from these sequences, such that the x-coordinates of points in the assembled sequence will not decrease. Help him with that. Note that each element of the initial sequences should be used exactly once in the assembled sequence. Dima considers two assembled sequences (p1, q1), (p2, q2), ..., (p2·n, q2·n) and (x1, y1), (x2, y2), ..., (x2·n, y2·n) distinct, if there is such i (1 ≤ i ≤ 2·n), that (pi, qi) ≠ (xi, yi). As the answer can be rather large, print the remainder from dividing the answer by number m. Input The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 109). The numbers in the lines are separated by spaces. The last line contains integer m (2 ≤ m ≤ 109 + 7). Output In the single line print the remainder after dividing the answer to the problem by number m. Examples Input 1 1 2 7 Output 1 Input 2 1 2 2 3 11 Output 2 Note In the first sample you can get only one sequence: (1, 1), (2, 1). In the second sample you can get such sequences : (1, 1), (2, 2), (2, 1), (3, 2); (1, 1), (2, 1), (2, 2), (3, 2). Thus, the answer is 2.
instruction
0
98,165
12
196,330
Tags: combinatorics, math, sortings Correct Solution: ``` n=int(input()) y=list(map(int,input().split())) z=list(map(int,input().split())) a=sorted([[y[i],i] for i in range(n)]+[[z[i],i] for i in range(n)]) f=0 for i in range(n): if y[i]==z[i]: f+=1 m=int(input()) d=0 e=1 for i in range(1,2*n): if a[i][0]!=a[i-1][0]: for j in range(1,i-d+1): while f>0 and j%2==0: j//=2 f-=1 e*=j e%=m d=i for j in range(1,2*n-d+1): while f>0 and j%2==0: j//=2 f-=1 e*=j e%=m print(e%m) ```
output
1
98,165
12
196,331
Provide tags and a correct Python 3 solution for this coding contest problem. Little Dima has two sequences of points with integer coordinates: sequence (a1, 1), (a2, 2), ..., (an, n) and sequence (b1, 1), (b2, 2), ..., (bn, n). Now Dima wants to count the number of distinct sequences of points of length 2·n that can be assembled from these sequences, such that the x-coordinates of points in the assembled sequence will not decrease. Help him with that. Note that each element of the initial sequences should be used exactly once in the assembled sequence. Dima considers two assembled sequences (p1, q1), (p2, q2), ..., (p2·n, q2·n) and (x1, y1), (x2, y2), ..., (x2·n, y2·n) distinct, if there is such i (1 ≤ i ≤ 2·n), that (pi, qi) ≠ (xi, yi). As the answer can be rather large, print the remainder from dividing the answer by number m. Input The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 109). The numbers in the lines are separated by spaces. The last line contains integer m (2 ≤ m ≤ 109 + 7). Output In the single line print the remainder after dividing the answer to the problem by number m. Examples Input 1 1 2 7 Output 1 Input 2 1 2 2 3 11 Output 2 Note In the first sample you can get only one sequence: (1, 1), (2, 1). In the second sample you can get such sequences : (1, 1), (2, 2), (2, 1), (3, 2); (1, 1), (2, 1), (2, 2), (3, 2). Thus, the answer is 2.
instruction
0
98,166
12
196,332
Tags: combinatorics, math, sortings Correct Solution: ``` from math import sqrt,ceil,gcd from collections import defaultdict def modInverse(b,m): g = gcd(b, m) if (g != 1): # print("Inverse doesn't exist") return -1 else: # If b and m are relatively prime, # then modulo inverse is b^(m-2) mode m return pow(b, m - 2, m) def sol(n,m,rep): r = 1 for i in range(2,n+1): j = i while j%2 == 0 and rep>0: j//=2 rep-=1 r*=j r%=m return r def solve(): n = int(input()) a = list(map(int,input().split())) b = list(map(int,input().split())) m = int(input()) hash = defaultdict(int) e = defaultdict(int) for i in range(n): hash[a[i]]+=1 hash[b[i]]+=1 if a[i] == b[i]: e[a[i]]+=1 ans = 1 for i in hash: z1 = hash[i] z2 = e[i] ans*=sol(z1,m,z2) ans%=m print(ans) # t = int(input()) # for _ in range(t): solve() ```
output
1
98,166
12
196,333
Provide tags and a correct Python 3 solution for this coding contest problem. Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time. Help Yaroslav. Input The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the array elements. Output In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise. Examples Input 1 1 Output YES Input 3 1 1 2 Output YES Input 4 7 7 7 7 Output NO Note In the first sample the initial array fits well. In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it. In the third sample Yarosav can't get the array he needs.
instruction
0
98,171
12
196,342
Tags: greedy, math Correct Solution: ``` import math n = int(input()) arr = list(map(int, input().split())) req = 0 if n == 1: print("YES") else: for k in set(arr): req = max(req, arr.count(k)) if n - req >= req - 1: print("YES") else: print("NO") ```
output
1
98,171
12
196,343
Provide tags and a correct Python 3 solution for this coding contest problem. Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time. Help Yaroslav. Input The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the array elements. Output In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise. Examples Input 1 1 Output YES Input 3 1 1 2 Output YES Input 4 7 7 7 7 Output NO Note In the first sample the initial array fits well. In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it. In the third sample Yarosav can't get the array he needs.
instruction
0
98,172
12
196,344
Tags: greedy, math Correct Solution: ``` a = int(input()) l = list(map(int, input().split())) d = dict() s = set() for i in l: if i not in d: d[i] = 1 else: d[i] += 1 s.add(i) flag = False for i in s: sum = 0 for j in d: sum += d[j] sum -= d[i] if d[i] - sum > 1: flag = True if flag: print('NO') else: print('YES') ```
output
1
98,172
12
196,345
Provide tags and a correct Python 3 solution for this coding contest problem. Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time. Help Yaroslav. Input The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the array elements. Output In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise. Examples Input 1 1 Output YES Input 3 1 1 2 Output YES Input 4 7 7 7 7 Output NO Note In the first sample the initial array fits well. In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it. In the third sample Yarosav can't get the array he needs.
instruction
0
98,173
12
196,346
Tags: greedy, math Correct Solution: ``` import math import collections n=int(input()) A=[int(x) for x in input().split()] D=collections.Counter(A) m=math.ceil(n/2) for v in D.values(): if v>m: print("NO") exit() print("YES") ```
output
1
98,173
12
196,347
Provide tags and a correct Python 3 solution for this coding contest problem. Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time. Help Yaroslav. Input The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the array elements. Output In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise. Examples Input 1 1 Output YES Input 3 1 1 2 Output YES Input 4 7 7 7 7 Output NO Note In the first sample the initial array fits well. In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it. In the third sample Yarosav can't get the array he needs.
instruction
0
98,174
12
196,348
Tags: greedy, math Correct Solution: ``` import sys,math from collections import deque,defaultdict import operator as op from functools import reduce from itertools import permutations #sys.setrecursionlimit(10**6) I=sys.stdin.readline #s="abcdefghijklmnopqrstuvwxyz" """ x_move=[-1,0,1,0,-1,1,1,-1] y_move=[0,1,0,-1,1,1,-1,-1] """ def ii(): return int(I().strip()) def li(): return list(map(int,I().strip().split())) def mi(): return map(int,I().strip().split()) """def ncr(n, r): r = min(r, n-r) numer = (reduce(op.mul, range(n, n-r, -1), 1))%(10**9+7) denom = (reduce(op.mul, range(1, r+1), 1))%(10**9+7) return (numer // denom)%(10**9+7)""" 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 gcd(x, y): while y: x, y = y, x % y return x def valid(row,col,rows,cols,rcross,lcross): return rows[row]==0 and cols[col]==0 and rcross[col+row]==0 and lcross[col-row]==0 def div(n): tmp=[] for i in range(2,int(n**.5)+1): if n%i==0: cnt=0 while(n%i==0): n=n//i cnt+=1 tmp.append((i,cnt)) if n>1: tmp.append((n,1)) return tmp def isPrime(n): if n<=1: return False elif n<=2: return True else: flag=True for i in range(2,int(n**.5)+1): if n%i==0: flag=False break return flag def s(b): ans=[] while b>0: tmp=b%10 ans.append(tmp) b=b//10 return ans def main(): n=ii() arr=li() if n==1: print("YES") else: sett=set(arr) d=defaultdict(int) for i in range(n): d[arr[i]]+=1 flag=1 for i in sett: if n-d[i]<d[i]-1: flag=0 break if flag: print("YES") else: print("NO") if __name__ == '__main__': main() ```
output
1
98,174
12
196,349
Provide tags and a correct Python 3 solution for this coding contest problem. Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time. Help Yaroslav. Input The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the array elements. Output In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise. Examples Input 1 1 Output YES Input 3 1 1 2 Output YES Input 4 7 7 7 7 Output NO Note In the first sample the initial array fits well. In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it. In the third sample Yarosav can't get the array he needs.
instruction
0
98,175
12
196,350
Tags: greedy, math Correct Solution: ``` import sys def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def rinput(): return map(int, sys.stdin.readline().strip().split()) def get_list(): return list(map(int, sys.stdin.readline().strip().split())) n=iinput() h=((n+1)/2) a=list(map(int,input().split())) l=list(set(a)) s=len(l) r=[] for i in range(s): r.append(a.count(l[i])) m=max(r) if(m<=h): print("YES") else: print("NO") ```
output
1
98,175
12
196,351
Provide tags and a correct Python 3 solution for this coding contest problem. Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time. Help Yaroslav. Input The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the array elements. Output In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise. Examples Input 1 1 Output YES Input 3 1 1 2 Output YES Input 4 7 7 7 7 Output NO Note In the first sample the initial array fits well. In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it. In the third sample Yarosav can't get the array he needs.
instruction
0
98,176
12
196,352
Tags: greedy, math Correct Solution: ``` import math N = int(input()) numbers = list(map(int, input().split())) set_of_numbers = list(set(numbers)) countr = [None] * len(set_of_numbers) for i in range(len(set_of_numbers)): countr[i] = numbers.count(set_of_numbers[i]) if max(countr) > math.ceil(N/2): print('NO') else: print('YES') ```
output
1
98,176
12
196,353