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 beauty of an array b_1, b_2, …, b_n (n > 1) β€” min_{1 ≀ i < j ≀ n} |b_i - b_j|. You're given an array a_1, a_2, … a_n and a number k. Calculate the sum of beauty over all subsequences of the array of length exactly k. As this number can be very large, output it modulo 998244353. A sequence a is a subsequence of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements. Input The first line contains integers n, k (2 ≀ k ≀ n ≀ 1000). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 10^5). Output Output one integer β€” the sum of beauty over all subsequences of the array of length exactly k. As this number can be very large, output it modulo 998244353. Examples Input 4 3 1 7 3 5 Output 8 Input 5 5 1 10 100 1000 10000 Output 9 Note In the first example, there are 4 subsequences of length 3 β€” [1, 7, 3], [1, 3, 5], [7, 3, 5], [1, 7, 5], each of which has beauty 2, so answer is 8. In the second example, there is only one subsequence of length 5 β€” the whole array, which has the beauty equal to |10-1| = 9. Submitted Solution: ``` import itertools def find_beauty(arr): i = 0 min_beauty = 9999999999999999999 for i in range(0, len(arr) - 1): j = 0 for j in range(i + 1, len(arr)): if i < j: expression = abs(arr[i] - arr[j]) if expression < min_beauty: min_beauty = expression return min_beauty def check_is_same(arr): previous = arr[0] for item in arr: if (item != previous): return False else: previous = item return True if __name__ == "__main__": _input = input().split(' ') n = int(_input[0]) k = int(_input[1]) arr = list(map(int, input().strip().split()))[:n] if (check_is_same(arr)): print(0) else: if n >= 80 or k >= 80: print(998244353) else: result = itertools.combinations(arr, k) summary = 0 for item in result: beauty = find_beauty(list(item)) if (summary + beauty >= 998244353): print(998244353) break summary += find_beauty(list(item)) print(summary) ```
instruction
0
65,682
12
131,364
No
output
1
65,682
12
131,365
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call beauty of an array b_1, b_2, …, b_n (n > 1) β€” min_{1 ≀ i < j ≀ n} |b_i - b_j|. You're given an array a_1, a_2, … a_n and a number k. Calculate the sum of beauty over all subsequences of the array of length exactly k. As this number can be very large, output it modulo 998244353. A sequence a is a subsequence of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements. Input The first line contains integers n, k (2 ≀ k ≀ n ≀ 1000). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 10^5). Output Output one integer β€” the sum of beauty over all subsequences of the array of length exactly k. As this number can be very large, output it modulo 998244353. Examples Input 4 3 1 7 3 5 Output 8 Input 5 5 1 10 100 1000 10000 Output 9 Note In the first example, there are 4 subsequences of length 3 β€” [1, 7, 3], [1, 3, 5], [7, 3, 5], [1, 7, 5], each of which has beauty 2, so answer is 8. In the second example, there is only one subsequence of length 5 β€” the whole array, which has the beauty equal to |10-1| = 9. Submitted Solution: ``` P = 998244353 N, K = map(int, input().split()) A = sorted([int(a) for a in input().split()]) M = 100100 X = [{} for i in range(N)] ans = 0 maxd = 10**5//(K-1) for i in range(N): for j in range(i+1, N): if A[j]-A[i] <= maxd: X[j][2*M+A[j]-A[i]] = 1 for k in X[i]: l = k // M if l == K: ans += m * X[i][k] ans %= P m = k % M nl = l+1 if nl > K: continue for j in range(i+1, N): nm = min(m, A[j]-A[i]) if nm > maxd: continue a = nl*M+nm if a in X[j]: X[j][a] += X[i][k] X[j][a] %= P else: X[j][a] = X[i][k] print(ans) ```
instruction
0
65,683
12
131,366
No
output
1
65,683
12
131,367
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Let's call beauty of an array b_1, b_2, …, b_n (n > 1) β€” min_{1 ≀ i < j ≀ n} |b_i - b_j|. You're given an array a_1, a_2, … a_n and a number k. Calculate the sum of beauty over all subsequences of the array of length exactly k. As this number can be very large, output it modulo 998244353. A sequence a is a subsequence of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements. Input The first line contains integers n, k (2 ≀ k ≀ n ≀ 1000). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 10^5). Output Output one integer β€” the sum of beauty over all subsequences of the array of length exactly k. As this number can be very large, output it modulo 998244353. Examples Input 4 3 1 7 3 5 Output 8 Input 5 5 1 10 100 1000 10000 Output 9 Note In the first example, there are 4 subsequences of length 3 β€” [1, 7, 3], [1, 3, 5], [7, 3, 5], [1, 7, 5], each of which has beauty 2, so answer is 8. In the second example, there is only one subsequence of length 5 β€” the whole array, which has the beauty equal to |10-1| = 9. Submitted Solution: ``` def process(A, n, k): F=[[None for x in range(k+1)] for x in range(n+1)] for i in range(0, n+1): for j in range(0, k+1): if i < j: F[i][j]=0 if j==0: F[i][j]=1 F[0][0]=1 res=0 for x in range(1, A[-1]//(k+1)+2): for i in range(1, n+1): l=0 while A[i]-x >=A[l+1]: l=l+1 for j in range(1, k+1): if i>=j: F[i][j]=F[i-1][j]+ F[l][j-1] F[i][j]=F[i][j] % 998244353 res=(res+ F[n][k])% 998244353 # print(F[n][k]) return res n, k=[int(x) for x in input().split()] A=[int(x) for x in input().split()] A.sort() A.insert(0, None) print(process(A,n,k)) ```
instruction
0
65,684
12
131,368
No
output
1
65,684
12
131,369
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence of n pairs of integers: (a_1, b_1), (a_2, b_2), ... , (a_n, b_n). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences: * s = [(1, 2), (3, 2), (3, 1)] is bad because the sequence of first elements is sorted: [1, 3, 3]; * s = [(1, 2), (3, 2), (1, 2)] is bad because the sequence of second elements is sorted: [2, 2, 2]; * s = [(1, 1), (2, 2), (3, 3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; * s = [(1, 3), (3, 3), (2, 2)] is good because neither the sequence of first elements ([1, 3, 2]) nor the sequence of second elements ([3, 3, 2]) is sorted. Calculate the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. A permutation p of size n is a sequence p_1, p_2, ... , p_n consisting of n distinct integers from 1 to n (1 ≀ p_i ≀ n). If you apply permutation p_1, p_2, ... , p_n to the sequence s_1, s_2, ... , s_n you get the sequence s_{p_1}, s_{p_2}, ... , s_{p_n}. For example, if s = [(1, 2), (1, 3), (2, 3)] and p = [2, 3, 1] then s turns into [(1, 3), (2, 3), (1, 2)]. Input The first line contains one integer n (1 ≀ n ≀ 3 β‹… 10^5). The next n lines contains description of sequence s. The i-th line contains two integers a_i and b_i (1 ≀ a_i, b_i ≀ n) β€” the first and second elements of i-th pair in the sequence. The sequence s may contain equal elements. Output Print the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. Print the answer modulo 998244353 (a prime number). Examples Input 3 1 1 2 2 3 1 Output 3 Input 4 2 3 2 2 2 1 2 4 Output 0 Input 3 1 1 1 1 2 3 Output 4 Note In first test case there are six permutations of size 3: 1. if p = [1, 2, 3], then s = [(1, 1), (2, 2), (3, 1)] β€” bad sequence (sorted by first elements); 2. if p = [1, 3, 2], then s = [(1, 1), (3, 1), (2, 2)] β€” bad sequence (sorted by second elements); 3. if p = [2, 1, 3], then s = [(2, 2), (1, 1), (3, 1)] β€” good sequence; 4. if p = [2, 3, 1], then s = [(2, 2), (3, 1), (1, 1)] β€” good sequence; 5. if p = [3, 1, 2], then s = [(3, 1), (1, 1), (2, 2)] β€” bad sequence (sorted by second elements); 6. if p = [3, 2, 1], then s = [(3, 1), (2, 2), (1, 1)] β€” good sequence.
instruction
0
65,685
12
131,370
Tags: combinatorics Correct Solution: ``` import sys from collections import Counter readline = sys.stdin.readline readlines = sys.stdin.readlines mod = 998244353 n=int(input()) ab=[tuple(map(int, readline().split())) for _ in range(n)] ab.sort() fun = [1] * (n + 1) for i in range(1, n + 1): fun[i] = fun[i - 1] * i % mod U = fun[n] s_ab = 1 s_a = 1 s_b = 1 for i in range(n-1): if ab[i+1][1] < ab[i][1]: s_ab = 0 break if s_ab: for x in Counter(ab).values(): s_ab = s_ab * fun[x] % mod for x in Counter([x[0] for x in ab]).values(): s_a = s_a * fun[x] % mod for x in Counter([x[1] for x in ab]).values(): s_b = s_b * fun[x] % mod print((U - s_a - s_b + s_ab) % mod) ```
output
1
65,685
12
131,371
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence of n pairs of integers: (a_1, b_1), (a_2, b_2), ... , (a_n, b_n). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences: * s = [(1, 2), (3, 2), (3, 1)] is bad because the sequence of first elements is sorted: [1, 3, 3]; * s = [(1, 2), (3, 2), (1, 2)] is bad because the sequence of second elements is sorted: [2, 2, 2]; * s = [(1, 1), (2, 2), (3, 3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; * s = [(1, 3), (3, 3), (2, 2)] is good because neither the sequence of first elements ([1, 3, 2]) nor the sequence of second elements ([3, 3, 2]) is sorted. Calculate the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. A permutation p of size n is a sequence p_1, p_2, ... , p_n consisting of n distinct integers from 1 to n (1 ≀ p_i ≀ n). If you apply permutation p_1, p_2, ... , p_n to the sequence s_1, s_2, ... , s_n you get the sequence s_{p_1}, s_{p_2}, ... , s_{p_n}. For example, if s = [(1, 2), (1, 3), (2, 3)] and p = [2, 3, 1] then s turns into [(1, 3), (2, 3), (1, 2)]. Input The first line contains one integer n (1 ≀ n ≀ 3 β‹… 10^5). The next n lines contains description of sequence s. The i-th line contains two integers a_i and b_i (1 ≀ a_i, b_i ≀ n) β€” the first and second elements of i-th pair in the sequence. The sequence s may contain equal elements. Output Print the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. Print the answer modulo 998244353 (a prime number). Examples Input 3 1 1 2 2 3 1 Output 3 Input 4 2 3 2 2 2 1 2 4 Output 0 Input 3 1 1 1 1 2 3 Output 4 Note In first test case there are six permutations of size 3: 1. if p = [1, 2, 3], then s = [(1, 1), (2, 2), (3, 1)] β€” bad sequence (sorted by first elements); 2. if p = [1, 3, 2], then s = [(1, 1), (3, 1), (2, 2)] β€” bad sequence (sorted by second elements); 3. if p = [2, 1, 3], then s = [(2, 2), (1, 1), (3, 1)] β€” good sequence; 4. if p = [2, 3, 1], then s = [(2, 2), (3, 1), (1, 1)] β€” good sequence; 5. if p = [3, 1, 2], then s = [(3, 1), (1, 1), (2, 2)] β€” bad sequence (sorted by second elements); 6. if p = [3, 2, 1], then s = [(3, 1), (2, 2), (1, 1)] β€” good sequence.
instruction
0
65,686
12
131,372
Tags: combinatorics Correct Solution: ``` from collections import Counter import sys input = sys.stdin.readline MOD = 998244353 n = int(input()) s = sorted([tuple(map(int, input().split())) for _ in range(n)]) fact = [1] for i in range(1, n + 1): fact.append(fact[-1] * i % MOD) def ways(l): count = Counter(l) res = 1 for x in count.values(): res *= fact[x] res %= MOD return res a = ways([x for x, _ in s]) b = ways([y for _, y in s]) c = ways(s) if all(s[i - 1][1] <= s[i][1] for i in range(1, n)) else 0 print((fact[n] - a - b + c) % MOD) ```
output
1
65,686
12
131,373
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence of n pairs of integers: (a_1, b_1), (a_2, b_2), ... , (a_n, b_n). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences: * s = [(1, 2), (3, 2), (3, 1)] is bad because the sequence of first elements is sorted: [1, 3, 3]; * s = [(1, 2), (3, 2), (1, 2)] is bad because the sequence of second elements is sorted: [2, 2, 2]; * s = [(1, 1), (2, 2), (3, 3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; * s = [(1, 3), (3, 3), (2, 2)] is good because neither the sequence of first elements ([1, 3, 2]) nor the sequence of second elements ([3, 3, 2]) is sorted. Calculate the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. A permutation p of size n is a sequence p_1, p_2, ... , p_n consisting of n distinct integers from 1 to n (1 ≀ p_i ≀ n). If you apply permutation p_1, p_2, ... , p_n to the sequence s_1, s_2, ... , s_n you get the sequence s_{p_1}, s_{p_2}, ... , s_{p_n}. For example, if s = [(1, 2), (1, 3), (2, 3)] and p = [2, 3, 1] then s turns into [(1, 3), (2, 3), (1, 2)]. Input The first line contains one integer n (1 ≀ n ≀ 3 β‹… 10^5). The next n lines contains description of sequence s. The i-th line contains two integers a_i and b_i (1 ≀ a_i, b_i ≀ n) β€” the first and second elements of i-th pair in the sequence. The sequence s may contain equal elements. Output Print the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. Print the answer modulo 998244353 (a prime number). Examples Input 3 1 1 2 2 3 1 Output 3 Input 4 2 3 2 2 2 1 2 4 Output 0 Input 3 1 1 1 1 2 3 Output 4 Note In first test case there are six permutations of size 3: 1. if p = [1, 2, 3], then s = [(1, 1), (2, 2), (3, 1)] β€” bad sequence (sorted by first elements); 2. if p = [1, 3, 2], then s = [(1, 1), (3, 1), (2, 2)] β€” bad sequence (sorted by second elements); 3. if p = [2, 1, 3], then s = [(2, 2), (1, 1), (3, 1)] β€” good sequence; 4. if p = [2, 3, 1], then s = [(2, 2), (3, 1), (1, 1)] β€” good sequence; 5. if p = [3, 1, 2], then s = [(3, 1), (1, 1), (2, 2)] β€” bad sequence (sorted by second elements); 6. if p = [3, 2, 1], then s = [(3, 1), (2, 2), (1, 1)] β€” good sequence.
instruction
0
65,687
12
131,374
Tags: combinatorics Correct Solution: ``` import sys input = sys.stdin.readline n = int(input()) s = [list(map(int, input().split())) for y in range(n)] MOD = 998244353 fact = [1]*(n+1) for i in range(1, n+1): fact[i] = (fact[i-1] * i) % MOD a, b = [0]*n, [0]*n for x, y in s: a[x-1] += 1 b[y-1] += 1 a_count = 1 b_count = 1 for i in range(n): a_count *= fact[a[i]] a_count %= MOD b_count *= fact[b[i]] b_count %= MOD ab_count = 1 count = 1 s.sort() for i in range(1, n): if s[i][1] < s[i-1][1]: ab_count = 0 break if s[i] == s[i-1]: count += 1 else: ab_count *= fact[count] ab_count %= MOD count = 1 ab_count *= fact[count] ab_count %= MOD print((fact[n] - a_count - b_count + ab_count) % MOD) ```
output
1
65,687
12
131,375
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence of n pairs of integers: (a_1, b_1), (a_2, b_2), ... , (a_n, b_n). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences: * s = [(1, 2), (3, 2), (3, 1)] is bad because the sequence of first elements is sorted: [1, 3, 3]; * s = [(1, 2), (3, 2), (1, 2)] is bad because the sequence of second elements is sorted: [2, 2, 2]; * s = [(1, 1), (2, 2), (3, 3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; * s = [(1, 3), (3, 3), (2, 2)] is good because neither the sequence of first elements ([1, 3, 2]) nor the sequence of second elements ([3, 3, 2]) is sorted. Calculate the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. A permutation p of size n is a sequence p_1, p_2, ... , p_n consisting of n distinct integers from 1 to n (1 ≀ p_i ≀ n). If you apply permutation p_1, p_2, ... , p_n to the sequence s_1, s_2, ... , s_n you get the sequence s_{p_1}, s_{p_2}, ... , s_{p_n}. For example, if s = [(1, 2), (1, 3), (2, 3)] and p = [2, 3, 1] then s turns into [(1, 3), (2, 3), (1, 2)]. Input The first line contains one integer n (1 ≀ n ≀ 3 β‹… 10^5). The next n lines contains description of sequence s. The i-th line contains two integers a_i and b_i (1 ≀ a_i, b_i ≀ n) β€” the first and second elements of i-th pair in the sequence. The sequence s may contain equal elements. Output Print the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. Print the answer modulo 998244353 (a prime number). Examples Input 3 1 1 2 2 3 1 Output 3 Input 4 2 3 2 2 2 1 2 4 Output 0 Input 3 1 1 1 1 2 3 Output 4 Note In first test case there are six permutations of size 3: 1. if p = [1, 2, 3], then s = [(1, 1), (2, 2), (3, 1)] β€” bad sequence (sorted by first elements); 2. if p = [1, 3, 2], then s = [(1, 1), (3, 1), (2, 2)] β€” bad sequence (sorted by second elements); 3. if p = [2, 1, 3], then s = [(2, 2), (1, 1), (3, 1)] β€” good sequence; 4. if p = [2, 3, 1], then s = [(2, 2), (3, 1), (1, 1)] β€” good sequence; 5. if p = [3, 1, 2], then s = [(3, 1), (1, 1), (2, 2)] β€” bad sequence (sorted by second elements); 6. if p = [3, 2, 1], then s = [(3, 1), (2, 2), (1, 1)] β€” good sequence.
instruction
0
65,688
12
131,376
Tags: combinatorics 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 collections import defaultdict n=int(input());vals=[];mod=998244353 dict1=defaultdict(list);dict2=defaultdict(list) for s in range(n): a,b=map(int,input().split()) vals.append((a,b)) dict1[a].append(b);dict2[b].append(a) fact=[1];broke=False for s in range(1,n+1): fact.append((fact[-1]*s)%mod) one=1;key1=[x for x in dict1.keys()] for key in key1: one*=fact[len(dict1[key])] one=one%mod if len(dict1[key])==n: broke=True two=1;key2=[x for x in dict2.keys()] for key in key2: two*=fact[len(dict2[key])] two=two%mod if len(dict2[key])==n: broke=True onetwo=1;last=0;found=False key1.sort() for key in key1: dict3={};maxsofar=0;broke1=False for s in dict1[key]: if s>=last: if not(s in dict3): dict3[s]=1 else: dict3[s]+=1 maxsofar=max(maxsofar,s) else: broke1=True;break if broke1==True: found=False;break for s in dict3: onetwo*=fact[dict3[s]] onetwo=onetwo%mod if dict3[s]>1: found=True last=maxsofar if broke==True: print(0) else: if found==True: print((fact[n]-(one+two-onetwo))%mod) else: print((fact[n]-(one+two))%mod) ```
output
1
65,688
12
131,377
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence of n pairs of integers: (a_1, b_1), (a_2, b_2), ... , (a_n, b_n). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences: * s = [(1, 2), (3, 2), (3, 1)] is bad because the sequence of first elements is sorted: [1, 3, 3]; * s = [(1, 2), (3, 2), (1, 2)] is bad because the sequence of second elements is sorted: [2, 2, 2]; * s = [(1, 1), (2, 2), (3, 3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; * s = [(1, 3), (3, 3), (2, 2)] is good because neither the sequence of first elements ([1, 3, 2]) nor the sequence of second elements ([3, 3, 2]) is sorted. Calculate the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. A permutation p of size n is a sequence p_1, p_2, ... , p_n consisting of n distinct integers from 1 to n (1 ≀ p_i ≀ n). If you apply permutation p_1, p_2, ... , p_n to the sequence s_1, s_2, ... , s_n you get the sequence s_{p_1}, s_{p_2}, ... , s_{p_n}. For example, if s = [(1, 2), (1, 3), (2, 3)] and p = [2, 3, 1] then s turns into [(1, 3), (2, 3), (1, 2)]. Input The first line contains one integer n (1 ≀ n ≀ 3 β‹… 10^5). The next n lines contains description of sequence s. The i-th line contains two integers a_i and b_i (1 ≀ a_i, b_i ≀ n) β€” the first and second elements of i-th pair in the sequence. The sequence s may contain equal elements. Output Print the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. Print the answer modulo 998244353 (a prime number). Examples Input 3 1 1 2 2 3 1 Output 3 Input 4 2 3 2 2 2 1 2 4 Output 0 Input 3 1 1 1 1 2 3 Output 4 Note In first test case there are six permutations of size 3: 1. if p = [1, 2, 3], then s = [(1, 1), (2, 2), (3, 1)] β€” bad sequence (sorted by first elements); 2. if p = [1, 3, 2], then s = [(1, 1), (3, 1), (2, 2)] β€” bad sequence (sorted by second elements); 3. if p = [2, 1, 3], then s = [(2, 2), (1, 1), (3, 1)] β€” good sequence; 4. if p = [2, 3, 1], then s = [(2, 2), (3, 1), (1, 1)] β€” good sequence; 5. if p = [3, 1, 2], then s = [(3, 1), (1, 1), (2, 2)] β€” bad sequence (sorted by second elements); 6. if p = [3, 2, 1], then s = [(3, 1), (2, 2), (1, 1)] β€” good sequence.
instruction
0
65,689
12
131,378
Tags: combinatorics Correct Solution: ``` from sys import stdin input = stdin.readline mod = 998244353 fact = [1, 1] dictf = [0, 0] dicts = [0, 0] dictb = [0, 0] for i in range(2, 3 * pow(10, 5) + 99): fact.append((fact[-1] * i) % mod) dicts.append(0) dictf.append(0) dictb.append(0) n = int(input()) answer = fact[n] pairs = [] for _ in range(n): pairs.append([int(x) for x in input().split()]) dictf[pairs[_][0]] += 1 dicts[pairs[_][1]] += 1 first = 1 second = 1 both = 0 pairs.sort() for di in dictf: if di == 0: continue first *= fact[di] first %= mod answer -= first answer %= mod for di in dicts: if di == 0: continue second *= fact[di] second %= mod answer -= second answer %= mod checkSorting = True premiere, douxieme = pairs[0] for fi, se in pairs: checkSorting = checkSorting and fi >= premiere and se >= douxieme premiere = fi douxieme = se if checkSorting: both = 1 j = 0 for _ in range(len(pairs)): i = pairs[_] if i != pairs[j]: both *= fact[_ - j] both %= mod j = _ if _ == len(pairs) - 1: both *= fact[1 + _ - j] both %= mod # print(diff) # print(first, second, both) answer += both answer %= mod print(answer) ```
output
1
65,689
12
131,379
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence of n pairs of integers: (a_1, b_1), (a_2, b_2), ... , (a_n, b_n). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences: * s = [(1, 2), (3, 2), (3, 1)] is bad because the sequence of first elements is sorted: [1, 3, 3]; * s = [(1, 2), (3, 2), (1, 2)] is bad because the sequence of second elements is sorted: [2, 2, 2]; * s = [(1, 1), (2, 2), (3, 3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; * s = [(1, 3), (3, 3), (2, 2)] is good because neither the sequence of first elements ([1, 3, 2]) nor the sequence of second elements ([3, 3, 2]) is sorted. Calculate the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. A permutation p of size n is a sequence p_1, p_2, ... , p_n consisting of n distinct integers from 1 to n (1 ≀ p_i ≀ n). If you apply permutation p_1, p_2, ... , p_n to the sequence s_1, s_2, ... , s_n you get the sequence s_{p_1}, s_{p_2}, ... , s_{p_n}. For example, if s = [(1, 2), (1, 3), (2, 3)] and p = [2, 3, 1] then s turns into [(1, 3), (2, 3), (1, 2)]. Input The first line contains one integer n (1 ≀ n ≀ 3 β‹… 10^5). The next n lines contains description of sequence s. The i-th line contains two integers a_i and b_i (1 ≀ a_i, b_i ≀ n) β€” the first and second elements of i-th pair in the sequence. The sequence s may contain equal elements. Output Print the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. Print the answer modulo 998244353 (a prime number). Examples Input 3 1 1 2 2 3 1 Output 3 Input 4 2 3 2 2 2 1 2 4 Output 0 Input 3 1 1 1 1 2 3 Output 4 Note In first test case there are six permutations of size 3: 1. if p = [1, 2, 3], then s = [(1, 1), (2, 2), (3, 1)] β€” bad sequence (sorted by first elements); 2. if p = [1, 3, 2], then s = [(1, 1), (3, 1), (2, 2)] β€” bad sequence (sorted by second elements); 3. if p = [2, 1, 3], then s = [(2, 2), (1, 1), (3, 1)] β€” good sequence; 4. if p = [2, 3, 1], then s = [(2, 2), (3, 1), (1, 1)] β€” good sequence; 5. if p = [3, 1, 2], then s = [(3, 1), (1, 1), (2, 2)] β€” bad sequence (sorted by second elements); 6. if p = [3, 2, 1], then s = [(3, 1), (2, 2), (1, 1)] β€” good sequence.
instruction
0
65,690
12
131,380
Tags: combinatorics Correct Solution: ``` """ Satwik_Tiwari ;) . 24th july , 2020 - Friday """ #=============================================================================================== #importing some useful libraries. from __future__ import division, print_function from fractions import Fraction import sys import os from io import BytesIO, IOBase from itertools import * import bisect from heapq import * from math import * from copy import * from collections import deque from collections import Counter as counter # Counter(list) return a dict with {key: count} from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)] from itertools import permutations as permutate from bisect import bisect_left as bl #If the element is already present in the list, # the left most position where element has to be inserted is returned. from bisect import bisect_right as br from bisect import bisect #If the element is already present in the list, # the right most position where element has to be inserted is returned #============================================================================================== #fast I/O region BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) # inp = lambda: sys.stdin.readline().rstrip("\r\n") #=============================================================================================== #some shortcuts mod = 1000000007 def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input def out(var): sys.stdout.write(str(var)) #for fast output, always take string def lis(): return list(map(int, inp().split())) def stringlis(): return list(map(str, inp().split())) def sep(): return map(int, inp().split()) def strsep(): return map(str, inp().split()) # def graph(vertex): return [[] for i in range(0,vertex+1)] def zerolist(n): return [0]*n def nextline(): out("\n") #as stdout.write always print sring. def testcase(t): for p in range(t): solve() def printlist(a) : for p in range(0,len(a)): out(str(a[p]) + ' ') def lcm(a,b): return (a*b)//gcd(a,b) def power(a,b): ans = 1 while(b>0): if(b%2==1): ans*=a a*=a b//=2 return ans def ncr(n,r): return factorial(n)//(factorial(r)*factorial(max(n-r,1))) def isPrime(n) : if (n <= 1) : return False if (n <= 3) : return True if (n % 2 == 0 or n % 3 == 0) : return False i = 5 while(i * i <= n) : if (n % i == 0 or n % (i + 2) == 0) : return False i = i + 6 return True #=============================================================================================== # code here ;)) def solve(): n = int(inp()) a = [] b = [] for i in range(n): x,y = sep() a.append(x) b.append(y) cnta = [0]*(n+1) cntb = [0]*(n+1) for i in range(n): cnta[a[i]] +=1 cntb[b[i]] +=1 ans = 1 for i in range(2,n+1): ans*=i ans%=998244353 temp = 1 # print(cnta) # print(cntb) # print(ans) for i in range(n+1): if(cnta[i]>0): aisehi = 1 for j in range(1,cnta[i]+1): aisehi*=j aisehi%=998244353 temp*=aisehi temp%=998244353 ans-=temp ans%=998244353 # print(ans) temp = 1 for i in range(n+1): if(cntb[i]>0): aisehi = 1 for j in range(1,cntb[i]+1): aisehi*=j aisehi%=998244353 temp*=aisehi temp%=998244353 ans-=temp ans%=998244353 # print(ans) new = [] for i in range(n): new.append([a[i],b[i]]) new = sorted(new) # print(new) lol = [] for i in range(n): lol.append(new[i][1]) # print(lol) if(lol == sorted(lol)): cnt = {} for i in range(n): s = str(new[i][0])+' '+str(new[i][1]) if(s in cnt): cnt[s] +=1 else: cnt[s] = 1 temp = 1 for i in cnt: aisehi = 1 for j in range(2,cnt[i]+1): aisehi*=j aisehi%=998244353 temp*=aisehi temp%=998244353 ans+=temp ans%=998244353 print(ans) testcase(1) # testcase(int(inp())) ```
output
1
65,690
12
131,381
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence of n pairs of integers: (a_1, b_1), (a_2, b_2), ... , (a_n, b_n). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences: * s = [(1, 2), (3, 2), (3, 1)] is bad because the sequence of first elements is sorted: [1, 3, 3]; * s = [(1, 2), (3, 2), (1, 2)] is bad because the sequence of second elements is sorted: [2, 2, 2]; * s = [(1, 1), (2, 2), (3, 3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; * s = [(1, 3), (3, 3), (2, 2)] is good because neither the sequence of first elements ([1, 3, 2]) nor the sequence of second elements ([3, 3, 2]) is sorted. Calculate the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. A permutation p of size n is a sequence p_1, p_2, ... , p_n consisting of n distinct integers from 1 to n (1 ≀ p_i ≀ n). If you apply permutation p_1, p_2, ... , p_n to the sequence s_1, s_2, ... , s_n you get the sequence s_{p_1}, s_{p_2}, ... , s_{p_n}. For example, if s = [(1, 2), (1, 3), (2, 3)] and p = [2, 3, 1] then s turns into [(1, 3), (2, 3), (1, 2)]. Input The first line contains one integer n (1 ≀ n ≀ 3 β‹… 10^5). The next n lines contains description of sequence s. The i-th line contains two integers a_i and b_i (1 ≀ a_i, b_i ≀ n) β€” the first and second elements of i-th pair in the sequence. The sequence s may contain equal elements. Output Print the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. Print the answer modulo 998244353 (a prime number). Examples Input 3 1 1 2 2 3 1 Output 3 Input 4 2 3 2 2 2 1 2 4 Output 0 Input 3 1 1 1 1 2 3 Output 4 Note In first test case there are six permutations of size 3: 1. if p = [1, 2, 3], then s = [(1, 1), (2, 2), (3, 1)] β€” bad sequence (sorted by first elements); 2. if p = [1, 3, 2], then s = [(1, 1), (3, 1), (2, 2)] β€” bad sequence (sorted by second elements); 3. if p = [2, 1, 3], then s = [(2, 2), (1, 1), (3, 1)] β€” good sequence; 4. if p = [2, 3, 1], then s = [(2, 2), (3, 1), (1, 1)] β€” good sequence; 5. if p = [3, 1, 2], then s = [(3, 1), (1, 1), (2, 2)] β€” bad sequence (sorted by second elements); 6. if p = [3, 2, 1], then s = [(3, 1), (2, 2), (1, 1)] β€” good sequence.
instruction
0
65,691
12
131,382
Tags: combinatorics Correct Solution: ``` import io, sys input = lambda f=io.StringIO(sys.stdin.buffer.read().decode()).readline: f().rstrip() MAX = 3 * 10 ** 5 + 5 MOD = 998244353 fac = [1] * MAX for i in range(2, MAX): fac[i] = fac[i - 1] * i % MOD def count(z): r = 1 i = 0 while i < len(z): j = i + 1 while j < len(z) and z[i] == z[j]: j += 1 r = r * fac[j - i] % MOD i = j return r n = int(input()) a = [tuple(map(int, input().split())) for _ in range(n)] b = sorted(a) f = sorted(x for x, _ in a) s = sorted(y for _, y in a) cnt = count(f) + count(s) - (count(b) if all(b[i][0] <= b[i + 1][0] and b[i][1] <= b[i + 1][1] for i in range(n - 1)) else 0) print((fac[n] - cnt) % MOD) ```
output
1
65,691
12
131,383
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a sequence of n pairs of integers: (a_1, b_1), (a_2, b_2), ... , (a_n, b_n). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences: * s = [(1, 2), (3, 2), (3, 1)] is bad because the sequence of first elements is sorted: [1, 3, 3]; * s = [(1, 2), (3, 2), (1, 2)] is bad because the sequence of second elements is sorted: [2, 2, 2]; * s = [(1, 1), (2, 2), (3, 3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; * s = [(1, 3), (3, 3), (2, 2)] is good because neither the sequence of first elements ([1, 3, 2]) nor the sequence of second elements ([3, 3, 2]) is sorted. Calculate the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. A permutation p of size n is a sequence p_1, p_2, ... , p_n consisting of n distinct integers from 1 to n (1 ≀ p_i ≀ n). If you apply permutation p_1, p_2, ... , p_n to the sequence s_1, s_2, ... , s_n you get the sequence s_{p_1}, s_{p_2}, ... , s_{p_n}. For example, if s = [(1, 2), (1, 3), (2, 3)] and p = [2, 3, 1] then s turns into [(1, 3), (2, 3), (1, 2)]. Input The first line contains one integer n (1 ≀ n ≀ 3 β‹… 10^5). The next n lines contains description of sequence s. The i-th line contains two integers a_i and b_i (1 ≀ a_i, b_i ≀ n) β€” the first and second elements of i-th pair in the sequence. The sequence s may contain equal elements. Output Print the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. Print the answer modulo 998244353 (a prime number). Examples Input 3 1 1 2 2 3 1 Output 3 Input 4 2 3 2 2 2 1 2 4 Output 0 Input 3 1 1 1 1 2 3 Output 4 Note In first test case there are six permutations of size 3: 1. if p = [1, 2, 3], then s = [(1, 1), (2, 2), (3, 1)] β€” bad sequence (sorted by first elements); 2. if p = [1, 3, 2], then s = [(1, 1), (3, 1), (2, 2)] β€” bad sequence (sorted by second elements); 3. if p = [2, 1, 3], then s = [(2, 2), (1, 1), (3, 1)] β€” good sequence; 4. if p = [2, 3, 1], then s = [(2, 2), (3, 1), (1, 1)] β€” good sequence; 5. if p = [3, 1, 2], then s = [(3, 1), (1, 1), (2, 2)] β€” bad sequence (sorted by second elements); 6. if p = [3, 2, 1], then s = [(3, 1), (2, 2), (1, 1)] β€” good sequence.
instruction
0
65,692
12
131,384
Tags: combinatorics Correct Solution: ``` MOD = 998244353 # Read input n = int(input()) a = [] b = [] fact = [1] for i in range(n): x_a, x_b = input().strip().split(' ') a.append(int(x_a)) b.append(int(x_b)) for i in range(1, n+1): fact.append((fact[i-1] * i) % MOD) def count_permutations(arr): f = dict() for x in arr: try: f[x] += 1 except: f[x] = 1 res = 1 for _, v in f.items(): res *= fact[v] res %= MOD return res # Count bad sequences with first element bad_a = count_permutations(a) # Count bad sequences with second element bad_b = count_permutations(b) # Count bad sequences with both elements bad_common = 0 pairs = [(a[i], b[i]) for i in range(n)] pairs = sorted(pairs, key = lambda x: (x[0], x[1])) second = [x[1] for x in pairs] if second == sorted(second): bad_common = count_permutations(pairs) res = fact[n] - bad_a - bad_b + bad_common print(res % MOD) ```
output
1
65,692
12
131,385
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence of n pairs of integers: (a_1, b_1), (a_2, b_2), ... , (a_n, b_n). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences: * s = [(1, 2), (3, 2), (3, 1)] is bad because the sequence of first elements is sorted: [1, 3, 3]; * s = [(1, 2), (3, 2), (1, 2)] is bad because the sequence of second elements is sorted: [2, 2, 2]; * s = [(1, 1), (2, 2), (3, 3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; * s = [(1, 3), (3, 3), (2, 2)] is good because neither the sequence of first elements ([1, 3, 2]) nor the sequence of second elements ([3, 3, 2]) is sorted. Calculate the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. A permutation p of size n is a sequence p_1, p_2, ... , p_n consisting of n distinct integers from 1 to n (1 ≀ p_i ≀ n). If you apply permutation p_1, p_2, ... , p_n to the sequence s_1, s_2, ... , s_n you get the sequence s_{p_1}, s_{p_2}, ... , s_{p_n}. For example, if s = [(1, 2), (1, 3), (2, 3)] and p = [2, 3, 1] then s turns into [(1, 3), (2, 3), (1, 2)]. Input The first line contains one integer n (1 ≀ n ≀ 3 β‹… 10^5). The next n lines contains description of sequence s. The i-th line contains two integers a_i and b_i (1 ≀ a_i, b_i ≀ n) β€” the first and second elements of i-th pair in the sequence. The sequence s may contain equal elements. Output Print the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. Print the answer modulo 998244353 (a prime number). Examples Input 3 1 1 2 2 3 1 Output 3 Input 4 2 3 2 2 2 1 2 4 Output 0 Input 3 1 1 1 1 2 3 Output 4 Note In first test case there are six permutations of size 3: 1. if p = [1, 2, 3], then s = [(1, 1), (2, 2), (3, 1)] β€” bad sequence (sorted by first elements); 2. if p = [1, 3, 2], then s = [(1, 1), (3, 1), (2, 2)] β€” bad sequence (sorted by second elements); 3. if p = [2, 1, 3], then s = [(2, 2), (1, 1), (3, 1)] β€” good sequence; 4. if p = [2, 3, 1], then s = [(2, 2), (3, 1), (1, 1)] β€” good sequence; 5. if p = [3, 1, 2], then s = [(3, 1), (1, 1), (2, 2)] β€” bad sequence (sorted by second elements); 6. if p = [3, 2, 1], then s = [(3, 1), (2, 2), (1, 1)] β€” good sequence. Submitted Solution: ``` from sys import stdin input = stdin.readline mod = 998244353 MAX_N = 3*(10**5)+1 fact = [1 for i in range(MAX_N)] for i in range(1, MAX_N): fact[i] = fact[i-1]*i%mod n = int(input()) a = [0 for i in range(n)] b = [0 for i in range(n)] for i in range(n): a[i], b[i] = [int(i) for i in input().split()] s = sorted([(a[i], b[i]) for i in range(n)]) a = sorted(a) b = sorted(b) A = 1 B = 1 S = 1 count_A = 1 count_B = 1 count_S = 1 for i in range(1, n): if a[i] == a[i-1]: count_A += 1 else: A = (A*fact[count_A])%mod count_A = 1 if b[i] == b[i-1]: count_B += 1 else: B = (B*fact[count_B])%mod count_B = 1 if s[i][0] == s[i-1][0]: if s[i][1] == s[i-1][1]: count_S += 1 else: S = (S*fact[count_S])%mod count_S = 1 else: if s[i][1] < s[i-1][1]: S = 0 else: S = (S*fact[count_S])%mod count_S = 1 A = (A*fact[count_A])%mod B = (B*fact[count_B])%mod S = (S*fact[count_S])%mod print(((fact[n]%mod)-A-B+S)%mod) ```
instruction
0
65,693
12
131,386
Yes
output
1
65,693
12
131,387
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence of n pairs of integers: (a_1, b_1), (a_2, b_2), ... , (a_n, b_n). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences: * s = [(1, 2), (3, 2), (3, 1)] is bad because the sequence of first elements is sorted: [1, 3, 3]; * s = [(1, 2), (3, 2), (1, 2)] is bad because the sequence of second elements is sorted: [2, 2, 2]; * s = [(1, 1), (2, 2), (3, 3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; * s = [(1, 3), (3, 3), (2, 2)] is good because neither the sequence of first elements ([1, 3, 2]) nor the sequence of second elements ([3, 3, 2]) is sorted. Calculate the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. A permutation p of size n is a sequence p_1, p_2, ... , p_n consisting of n distinct integers from 1 to n (1 ≀ p_i ≀ n). If you apply permutation p_1, p_2, ... , p_n to the sequence s_1, s_2, ... , s_n you get the sequence s_{p_1}, s_{p_2}, ... , s_{p_n}. For example, if s = [(1, 2), (1, 3), (2, 3)] and p = [2, 3, 1] then s turns into [(1, 3), (2, 3), (1, 2)]. Input The first line contains one integer n (1 ≀ n ≀ 3 β‹… 10^5). The next n lines contains description of sequence s. The i-th line contains two integers a_i and b_i (1 ≀ a_i, b_i ≀ n) β€” the first and second elements of i-th pair in the sequence. The sequence s may contain equal elements. Output Print the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. Print the answer modulo 998244353 (a prime number). Examples Input 3 1 1 2 2 3 1 Output 3 Input 4 2 3 2 2 2 1 2 4 Output 0 Input 3 1 1 1 1 2 3 Output 4 Note In first test case there are six permutations of size 3: 1. if p = [1, 2, 3], then s = [(1, 1), (2, 2), (3, 1)] β€” bad sequence (sorted by first elements); 2. if p = [1, 3, 2], then s = [(1, 1), (3, 1), (2, 2)] β€” bad sequence (sorted by second elements); 3. if p = [2, 1, 3], then s = [(2, 2), (1, 1), (3, 1)] β€” good sequence; 4. if p = [2, 3, 1], then s = [(2, 2), (3, 1), (1, 1)] β€” good sequence; 5. if p = [3, 1, 2], then s = [(3, 1), (1, 1), (2, 2)] β€” bad sequence (sorted by second elements); 6. if p = [3, 2, 1], then s = [(3, 1), (2, 2), (1, 1)] β€” good sequence. Submitted Solution: ``` import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------------------ from math import factorial from collections import Counter, defaultdict from heapq import heapify, heappop, heappush def RL(): return map(int, sys.stdin.readline().rstrip().split()) def RLL(): return list(map(int, sys.stdin.readline().rstrip().split())) def N(): return int(input()) def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0 def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0 def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2) mod = 998244353 INF = float('inf') # ------------------------------ def main(): n = N() arr = [] for _ in range(n): arr.append(RLL()) fc = [0, 1] for i in range(2, n+1): fc.append(fc[-1]*i%mod) res = fc[n] arra = [i[0] for i in arr] arrb = [i[1] for i in arr] sm = 1 sub = 1 arra.sort() arrb.sort() for i in range(1, n): if arra[i]==arra[i-1]: sm+=1 else: sub = sub*fc[sm]%mod sm = 1 sub = sub*fc[sm]%mod res-=sub sm = 1 sub = 1 for i in range(1, n): if arrb[i]==arrb[i-1]: sm+=1 else: sub = sub*fc[sm]%mod sm = 1 sub = sub * fc[sm]%mod res-=sub sm = 1 sub = 1 arr.sort() for i in range(1, n): na, nb = arr[i][0], arr[i][1] if arr[i][1]<arr[i-1][1]: break if arr[i]==arr[i-1]: sm+=1 else: sub = sub*fc[sm]%mod sm = 1 else: sub = sub*fc[sm]%mod res+=sub print(res%mod) if __name__ == "__main__": main() ```
instruction
0
65,694
12
131,388
Yes
output
1
65,694
12
131,389
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence of n pairs of integers: (a_1, b_1), (a_2, b_2), ... , (a_n, b_n). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences: * s = [(1, 2), (3, 2), (3, 1)] is bad because the sequence of first elements is sorted: [1, 3, 3]; * s = [(1, 2), (3, 2), (1, 2)] is bad because the sequence of second elements is sorted: [2, 2, 2]; * s = [(1, 1), (2, 2), (3, 3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; * s = [(1, 3), (3, 3), (2, 2)] is good because neither the sequence of first elements ([1, 3, 2]) nor the sequence of second elements ([3, 3, 2]) is sorted. Calculate the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. A permutation p of size n is a sequence p_1, p_2, ... , p_n consisting of n distinct integers from 1 to n (1 ≀ p_i ≀ n). If you apply permutation p_1, p_2, ... , p_n to the sequence s_1, s_2, ... , s_n you get the sequence s_{p_1}, s_{p_2}, ... , s_{p_n}. For example, if s = [(1, 2), (1, 3), (2, 3)] and p = [2, 3, 1] then s turns into [(1, 3), (2, 3), (1, 2)]. Input The first line contains one integer n (1 ≀ n ≀ 3 β‹… 10^5). The next n lines contains description of sequence s. The i-th line contains two integers a_i and b_i (1 ≀ a_i, b_i ≀ n) β€” the first and second elements of i-th pair in the sequence. The sequence s may contain equal elements. Output Print the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. Print the answer modulo 998244353 (a prime number). Examples Input 3 1 1 2 2 3 1 Output 3 Input 4 2 3 2 2 2 1 2 4 Output 0 Input 3 1 1 1 1 2 3 Output 4 Note In first test case there are six permutations of size 3: 1. if p = [1, 2, 3], then s = [(1, 1), (2, 2), (3, 1)] β€” bad sequence (sorted by first elements); 2. if p = [1, 3, 2], then s = [(1, 1), (3, 1), (2, 2)] β€” bad sequence (sorted by second elements); 3. if p = [2, 1, 3], then s = [(2, 2), (1, 1), (3, 1)] β€” good sequence; 4. if p = [2, 3, 1], then s = [(2, 2), (3, 1), (1, 1)] β€” good sequence; 5. if p = [3, 1, 2], then s = [(3, 1), (1, 1), (2, 2)] β€” bad sequence (sorted by second elements); 6. if p = [3, 2, 1], then s = [(3, 1), (2, 2), (1, 1)] β€” good sequence. Submitted Solution: ``` import sys import itertools input = sys.stdin.readline def main(): n = int(input()) s = [] A = [] B = [] for _ in range(n): a, b = map(int, input().split()) s.append([a, b]) A.append(a) B.append(b) A.sort() B.sort() s.sort() ansA = 1 ansB = 1 anss = 1 for key, val in itertools.groupby(A): f = len(list(val)) for i in range(1, f+1): ansA *= i ansA %= 998244353 for key, val in itertools.groupby(B): f = len(list(val)) for i in range(1, f+1): ansB *= i ansB %= 998244353 for key, val in itertools.groupby(s): f = len(list(val)) for i in range(1, f+1): anss *= i anss %= 998244353 flag = 1 for i in range(n-1): if s[i+1][1] < s[i][1] or s[i+1][0] < s[i][0]: flag = 0 break if flag == 0: anss = 0 ansn = 1 for i in range(1, n+1): ansn *= i ansn %= 998244353 print((ansn-ansA-ansB+anss)%998244353) if __name__ == "__main__": main() ```
instruction
0
65,695
12
131,390
Yes
output
1
65,695
12
131,391
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence of n pairs of integers: (a_1, b_1), (a_2, b_2), ... , (a_n, b_n). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences: * s = [(1, 2), (3, 2), (3, 1)] is bad because the sequence of first elements is sorted: [1, 3, 3]; * s = [(1, 2), (3, 2), (1, 2)] is bad because the sequence of second elements is sorted: [2, 2, 2]; * s = [(1, 1), (2, 2), (3, 3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; * s = [(1, 3), (3, 3), (2, 2)] is good because neither the sequence of first elements ([1, 3, 2]) nor the sequence of second elements ([3, 3, 2]) is sorted. Calculate the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. A permutation p of size n is a sequence p_1, p_2, ... , p_n consisting of n distinct integers from 1 to n (1 ≀ p_i ≀ n). If you apply permutation p_1, p_2, ... , p_n to the sequence s_1, s_2, ... , s_n you get the sequence s_{p_1}, s_{p_2}, ... , s_{p_n}. For example, if s = [(1, 2), (1, 3), (2, 3)] and p = [2, 3, 1] then s turns into [(1, 3), (2, 3), (1, 2)]. Input The first line contains one integer n (1 ≀ n ≀ 3 β‹… 10^5). The next n lines contains description of sequence s. The i-th line contains two integers a_i and b_i (1 ≀ a_i, b_i ≀ n) β€” the first and second elements of i-th pair in the sequence. The sequence s may contain equal elements. Output Print the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. Print the answer modulo 998244353 (a prime number). Examples Input 3 1 1 2 2 3 1 Output 3 Input 4 2 3 2 2 2 1 2 4 Output 0 Input 3 1 1 1 1 2 3 Output 4 Note In first test case there are six permutations of size 3: 1. if p = [1, 2, 3], then s = [(1, 1), (2, 2), (3, 1)] β€” bad sequence (sorted by first elements); 2. if p = [1, 3, 2], then s = [(1, 1), (3, 1), (2, 2)] β€” bad sequence (sorted by second elements); 3. if p = [2, 1, 3], then s = [(2, 2), (1, 1), (3, 1)] β€” good sequence; 4. if p = [2, 3, 1], then s = [(2, 2), (3, 1), (1, 1)] β€” good sequence; 5. if p = [3, 1, 2], then s = [(3, 1), (1, 1), (2, 2)] β€” bad sequence (sorted by second elements); 6. if p = [3, 2, 1], then s = [(3, 1), (2, 2), (1, 1)] β€” good sequence. Submitted Solution: ``` import os input = os.read(0, 100000000) MOD = 998244353 MAX = 3 * 100001 pre_fact = [1 for i in range(MAX)] for i in range(1, MAX): pre_fact[i] = (pre_fact[i - 1] * i) % MOD spl = list(map(int, input.split())) n = spl[0] A = [(spl[i], spl[i + 1]) for i in range(1, 2*n+1, 2)] C = [y for x, y in A] C.sort() left, cnt = 1, 1 for i in range(1, n): if C[i - 1] == C[i]: cnt += 1 else: left = left * pre_fact[cnt] % MOD cnt = 1 left = left * pre_fact[cnt] % MOD B = [x for x, y in A] B.sort() right, cnt = 1, 1 for i in range(1, n): if B[i - 1] == B[i]: cnt += 1 else: right = right * pre_fact[cnt] % MOD cnt = 1 right = right * pre_fact[cnt] % MOD A.sort() both, cnt = 1, 1 for i in range(1, n): if A[i - 1][1] > A[i][1]: both = 0 break if A[i - 1] == A[i]: cnt += 1 else: both = both * pre_fact[cnt] % MOD cnt = 1 both = both * pre_fact[cnt] % MOD ans = (pre_fact[n] + MOD - left + MOD - right + both) % MOD print(ans) ```
instruction
0
65,696
12
131,392
Yes
output
1
65,696
12
131,393
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence of n pairs of integers: (a_1, b_1), (a_2, b_2), ... , (a_n, b_n). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences: * s = [(1, 2), (3, 2), (3, 1)] is bad because the sequence of first elements is sorted: [1, 3, 3]; * s = [(1, 2), (3, 2), (1, 2)] is bad because the sequence of second elements is sorted: [2, 2, 2]; * s = [(1, 1), (2, 2), (3, 3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; * s = [(1, 3), (3, 3), (2, 2)] is good because neither the sequence of first elements ([1, 3, 2]) nor the sequence of second elements ([3, 3, 2]) is sorted. Calculate the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. A permutation p of size n is a sequence p_1, p_2, ... , p_n consisting of n distinct integers from 1 to n (1 ≀ p_i ≀ n). If you apply permutation p_1, p_2, ... , p_n to the sequence s_1, s_2, ... , s_n you get the sequence s_{p_1}, s_{p_2}, ... , s_{p_n}. For example, if s = [(1, 2), (1, 3), (2, 3)] and p = [2, 3, 1] then s turns into [(1, 3), (2, 3), (1, 2)]. Input The first line contains one integer n (1 ≀ n ≀ 3 β‹… 10^5). The next n lines contains description of sequence s. The i-th line contains two integers a_i and b_i (1 ≀ a_i, b_i ≀ n) β€” the first and second elements of i-th pair in the sequence. The sequence s may contain equal elements. Output Print the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. Print the answer modulo 998244353 (a prime number). Examples Input 3 1 1 2 2 3 1 Output 3 Input 4 2 3 2 2 2 1 2 4 Output 0 Input 3 1 1 1 1 2 3 Output 4 Note In first test case there are six permutations of size 3: 1. if p = [1, 2, 3], then s = [(1, 1), (2, 2), (3, 1)] β€” bad sequence (sorted by first elements); 2. if p = [1, 3, 2], then s = [(1, 1), (3, 1), (2, 2)] β€” bad sequence (sorted by second elements); 3. if p = [2, 1, 3], then s = [(2, 2), (1, 1), (3, 1)] β€” good sequence; 4. if p = [2, 3, 1], then s = [(2, 2), (3, 1), (1, 1)] β€” good sequence; 5. if p = [3, 1, 2], then s = [(3, 1), (1, 1), (2, 2)] β€” bad sequence (sorted by second elements); 6. if p = [3, 2, 1], then s = [(3, 1), (2, 2), (1, 1)] β€” good sequence. Submitted Solution: ``` def main(): import sys input = sys.stdin.readline from math import factorial n = int(input()) s = [tuple(map(int, input().split())) for _ in range(n)] A = [0]*(n+1) B = [0]*(n+1) for a, b in s: A[a] += 1 B[b] += 1 factorial = [1] mod = 998244353 for i in range(1, n+1): factorial.append((factorial[-1]*i)%mod) s.sort() ansA = 1 ansB = 1 anss = 1 for i in A: ansA *= factorial[i] ansA %= 998244353 for i in B: ansB *= factorial[i] ansB %= 998244353 flag = 1 count = 1 for i in range(n-1): if s[i+1] == s[i]: count += 1 continue elif s[i+1][1] < s[i][1] or s[i+1][0] < s[i][0]: flag = 0 break else: anss *= factorial[count] anss %= 998244353 count = 1 anss *= count if flag == 0: anss = 0 ansn = factorial[n] % mod #print(ansn, ansA, ansB, anss) print((ansn-ansA-ansB+anss)%mod) if __name__ == "__main__": main() ```
instruction
0
65,697
12
131,394
No
output
1
65,697
12
131,395
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence of n pairs of integers: (a_1, b_1), (a_2, b_2), ... , (a_n, b_n). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences: * s = [(1, 2), (3, 2), (3, 1)] is bad because the sequence of first elements is sorted: [1, 3, 3]; * s = [(1, 2), (3, 2), (1, 2)] is bad because the sequence of second elements is sorted: [2, 2, 2]; * s = [(1, 1), (2, 2), (3, 3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; * s = [(1, 3), (3, 3), (2, 2)] is good because neither the sequence of first elements ([1, 3, 2]) nor the sequence of second elements ([3, 3, 2]) is sorted. Calculate the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. A permutation p of size n is a sequence p_1, p_2, ... , p_n consisting of n distinct integers from 1 to n (1 ≀ p_i ≀ n). If you apply permutation p_1, p_2, ... , p_n to the sequence s_1, s_2, ... , s_n you get the sequence s_{p_1}, s_{p_2}, ... , s_{p_n}. For example, if s = [(1, 2), (1, 3), (2, 3)] and p = [2, 3, 1] then s turns into [(1, 3), (2, 3), (1, 2)]. Input The first line contains one integer n (1 ≀ n ≀ 3 β‹… 10^5). The next n lines contains description of sequence s. The i-th line contains two integers a_i and b_i (1 ≀ a_i, b_i ≀ n) β€” the first and second elements of i-th pair in the sequence. The sequence s may contain equal elements. Output Print the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. Print the answer modulo 998244353 (a prime number). Examples Input 3 1 1 2 2 3 1 Output 3 Input 4 2 3 2 2 2 1 2 4 Output 0 Input 3 1 1 1 1 2 3 Output 4 Note In first test case there are six permutations of size 3: 1. if p = [1, 2, 3], then s = [(1, 1), (2, 2), (3, 1)] β€” bad sequence (sorted by first elements); 2. if p = [1, 3, 2], then s = [(1, 1), (3, 1), (2, 2)] β€” bad sequence (sorted by second elements); 3. if p = [2, 1, 3], then s = [(2, 2), (1, 1), (3, 1)] β€” good sequence; 4. if p = [2, 3, 1], then s = [(2, 2), (3, 1), (1, 1)] β€” good sequence; 5. if p = [3, 1, 2], then s = [(3, 1), (1, 1), (2, 2)] β€” bad sequence (sorted by second elements); 6. if p = [3, 2, 1], then s = [(3, 1), (2, 2), (1, 1)] β€” good sequence. Submitted Solution: ``` n=int(input()) arr=[list(map(int,input().split())) for _ in range(n)] dic1={} dic2={} for a,b in arr: if a not in dic1: dic1[a]=1 else: dic1[a]+=1 if b not in dic2: dic2[b]=1 else: dic2[b]+=1 ans=0 mod=998244353 tmp=1 facts=[1] for val in range(1,n+1): tmp*=val tmp%=mod facts.append(tmp) ans+=facts[n] tmp=1 for val in dic1.values(): tmp*=facts[val] tmp%=mod ans-=tmp tmp=1 for val in dic2.values(): tmp*=facts[val] tmp%=mod ans-=tmp arr=sorted(arr,key=lambda x:x[1]) flag=True for i in range(n-1): if arr[i][0]<=arr[i+1][0]: continue else: flag=False break if flag==False: print(ans%mod) else: dic3={} for a,b in arr: if (a,b) not in dic3: dic3[(a,b)]=1 else: dic3[(a,b)]+=1 tmp=1 for val in dic3.values(): tmp*=facts[val] tmp%=mod ans+=tmp if n!=300000: print(ans%mod) else: print(0) ```
instruction
0
65,698
12
131,396
No
output
1
65,698
12
131,397
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence of n pairs of integers: (a_1, b_1), (a_2, b_2), ... , (a_n, b_n). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences: * s = [(1, 2), (3, 2), (3, 1)] is bad because the sequence of first elements is sorted: [1, 3, 3]; * s = [(1, 2), (3, 2), (1, 2)] is bad because the sequence of second elements is sorted: [2, 2, 2]; * s = [(1, 1), (2, 2), (3, 3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; * s = [(1, 3), (3, 3), (2, 2)] is good because neither the sequence of first elements ([1, 3, 2]) nor the sequence of second elements ([3, 3, 2]) is sorted. Calculate the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. A permutation p of size n is a sequence p_1, p_2, ... , p_n consisting of n distinct integers from 1 to n (1 ≀ p_i ≀ n). If you apply permutation p_1, p_2, ... , p_n to the sequence s_1, s_2, ... , s_n you get the sequence s_{p_1}, s_{p_2}, ... , s_{p_n}. For example, if s = [(1, 2), (1, 3), (2, 3)] and p = [2, 3, 1] then s turns into [(1, 3), (2, 3), (1, 2)]. Input The first line contains one integer n (1 ≀ n ≀ 3 β‹… 10^5). The next n lines contains description of sequence s. The i-th line contains two integers a_i and b_i (1 ≀ a_i, b_i ≀ n) β€” the first and second elements of i-th pair in the sequence. The sequence s may contain equal elements. Output Print the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. Print the answer modulo 998244353 (a prime number). Examples Input 3 1 1 2 2 3 1 Output 3 Input 4 2 3 2 2 2 1 2 4 Output 0 Input 3 1 1 1 1 2 3 Output 4 Note In first test case there are six permutations of size 3: 1. if p = [1, 2, 3], then s = [(1, 1), (2, 2), (3, 1)] β€” bad sequence (sorted by first elements); 2. if p = [1, 3, 2], then s = [(1, 1), (3, 1), (2, 2)] β€” bad sequence (sorted by second elements); 3. if p = [2, 1, 3], then s = [(2, 2), (1, 1), (3, 1)] β€” good sequence; 4. if p = [2, 3, 1], then s = [(2, 2), (3, 1), (1, 1)] β€” good sequence; 5. if p = [3, 1, 2], then s = [(3, 1), (1, 1), (2, 2)] β€” bad sequence (sorted by second elements); 6. if p = [3, 2, 1], then s = [(3, 1), (2, 2), (1, 1)] β€” good sequence. Submitted Solution: ``` def main(): import sys input = sys.stdin.readline n = int(input()) arr = [tuple(map(int, input().split())) for _ in range(n)] arr.sort() tmp = sorted(arr, key = lambda a: (a[1], a[0])) same = 1 for i in range(n): if tmp[i] != arr[i]: same = 0 MOD = 998244353 fac = [1] * 300001 inv = [1] * 300001 for i in range(1, 300001): fac[i] = i * fac[i - 1] % MOD if tmp[-1][1] == tmp[0][1] or arr[-1][0] == arr[0][0]: print(0) return 0 if same: tmp = 1 i = 0 while i != n: first = i i += 1 while i != n and arr[i] == arr[i - 1]: i += 1 tmp = tmp * fac[i - first] % MOD print((fac[n] - tmp) % MOD) else: tmp1 = 1 i = 0 while i != n: first = i i += 1 while i != n and arr[i][0] == arr[i - 1][0]: i += 1 tmp1 = tmp1 * fac[i - first] % MOD tmp2 = 1 i = 0 while i != n: first = i i += 1 while i != n and tmp[i][1] == tmp[i - 1][1]: i += 1 tmp2 = tmp2 * fac[i - first] % MOD print((fac[n] - tmp1 - tmp2) % MOD) return 0 main() ```
instruction
0
65,699
12
131,398
No
output
1
65,699
12
131,399
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a sequence of n pairs of integers: (a_1, b_1), (a_2, b_2), ... , (a_n, b_n). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences: * s = [(1, 2), (3, 2), (3, 1)] is bad because the sequence of first elements is sorted: [1, 3, 3]; * s = [(1, 2), (3, 2), (1, 2)] is bad because the sequence of second elements is sorted: [2, 2, 2]; * s = [(1, 1), (2, 2), (3, 3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; * s = [(1, 3), (3, 3), (2, 2)] is good because neither the sequence of first elements ([1, 3, 2]) nor the sequence of second elements ([3, 3, 2]) is sorted. Calculate the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. A permutation p of size n is a sequence p_1, p_2, ... , p_n consisting of n distinct integers from 1 to n (1 ≀ p_i ≀ n). If you apply permutation p_1, p_2, ... , p_n to the sequence s_1, s_2, ... , s_n you get the sequence s_{p_1}, s_{p_2}, ... , s_{p_n}. For example, if s = [(1, 2), (1, 3), (2, 3)] and p = [2, 3, 1] then s turns into [(1, 3), (2, 3), (1, 2)]. Input The first line contains one integer n (1 ≀ n ≀ 3 β‹… 10^5). The next n lines contains description of sequence s. The i-th line contains two integers a_i and b_i (1 ≀ a_i, b_i ≀ n) β€” the first and second elements of i-th pair in the sequence. The sequence s may contain equal elements. Output Print the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. Print the answer modulo 998244353 (a prime number). Examples Input 3 1 1 2 2 3 1 Output 3 Input 4 2 3 2 2 2 1 2 4 Output 0 Input 3 1 1 1 1 2 3 Output 4 Note In first test case there are six permutations of size 3: 1. if p = [1, 2, 3], then s = [(1, 1), (2, 2), (3, 1)] β€” bad sequence (sorted by first elements); 2. if p = [1, 3, 2], then s = [(1, 1), (3, 1), (2, 2)] β€” bad sequence (sorted by second elements); 3. if p = [2, 1, 3], then s = [(2, 2), (1, 1), (3, 1)] β€” good sequence; 4. if p = [2, 3, 1], then s = [(2, 2), (3, 1), (1, 1)] β€” good sequence; 5. if p = [3, 1, 2], then s = [(3, 1), (1, 1), (2, 2)] β€” bad sequence (sorted by second elements); 6. if p = [3, 2, 1], then s = [(3, 1), (2, 2), (1, 1)] β€” good sequence. Submitted Solution: ``` def fact(n): return memo[n] def f(arr): n=len(arr) i=0 w=1 while i<n-1: j=i+1 c=1 while j<n and arr[j]==arr[i]: c+=1 j+=1 w=(w*fact(c))%MOD i+=c return w def sortport(l): p=[] for i in range(n): p.append(l[i][1]) return p n=int(input()) l=[] for i in range(n): l.append(tuple(map(int,input().strip().split()))) MOD = 998244353 memo={0:1} for i in range(1,n+1): memo[i]=memo[i-1]*i%MOD l.sort() m=f(l) l1,l2=zip(*l) s=f(l1) s1=f(sorted(l2)) hc=1 if all(l2[i]<=l2[i+1] for i in range(n-1)) else 0 ans=max(0,fact(n)-s-s1+m*hc) print(ans%MOD) ```
instruction
0
65,700
12
131,400
No
output
1
65,700
12
131,401
Provide tags and a correct Python 3 solution for this coding contest problem. "You must lift the dam. With a lever. I will give it to you. You must block the canal. With a rock. I will not give the rock to you." Danik urgently needs rock and lever! Obviously, the easiest way to get these things is to ask Hermit Lizard for them. Hermit Lizard agreed to give Danik the lever. But to get a stone, Danik needs to solve the following task. You are given a positive integer n, and an array a of positive integers. The task is to calculate the number of such pairs (i,j) that i<j and a_i \& a_j β‰₯ a_i βŠ• a_j, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND), and βŠ• denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR). Danik has solved this task. But can you solve it? Input Each test contains multiple test cases. The first line contains one positive integer t (1 ≀ t ≀ 10) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains one positive integer n (1 ≀ n ≀ 10^5) β€” length of the array. The second line contains n positive integers a_i (1 ≀ a_i ≀ 10^9) β€” elements of the array. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For every test case print one non-negative integer β€” the answer to the problem. Example Input 5 5 1 4 3 7 10 3 1 1 1 4 6 2 5 3 2 2 4 1 1 Output 1 3 2 0 0 Note In the first test case there is only one pair: (4,7): for it 4 \& 7 = 4, and 4 βŠ• 7 = 3. In the second test case all pairs are good. In the third test case there are two pairs: (6,5) and (2,3). In the fourth test case there are no good pairs.
instruction
0
65,771
12
131,542
Tags: bitmasks, math Correct Solution: ``` # import numpy as npy # idx=sorted(idx,key=functools.cmp_to_key(cmpx)) import bisect import functools import math T=int(input()) for Tid in range(T): n=int(input()) a=list(map(int,input().split())) tab=[0 for i in range(35)] ans=0 for i in range(n): j=0 while (2<<j)<=a[i]: j=j+1 ans=ans+tab[j] # print(j,end=' ') tab[j]=tab[j]+1 print(ans) ```
output
1
65,771
12
131,543
Provide tags and a correct Python 3 solution for this coding contest problem. "You must lift the dam. With a lever. I will give it to you. You must block the canal. With a rock. I will not give the rock to you." Danik urgently needs rock and lever! Obviously, the easiest way to get these things is to ask Hermit Lizard for them. Hermit Lizard agreed to give Danik the lever. But to get a stone, Danik needs to solve the following task. You are given a positive integer n, and an array a of positive integers. The task is to calculate the number of such pairs (i,j) that i<j and a_i \& a_j β‰₯ a_i βŠ• a_j, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND), and βŠ• denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR). Danik has solved this task. But can you solve it? Input Each test contains multiple test cases. The first line contains one positive integer t (1 ≀ t ≀ 10) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains one positive integer n (1 ≀ n ≀ 10^5) β€” length of the array. The second line contains n positive integers a_i (1 ≀ a_i ≀ 10^9) β€” elements of the array. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For every test case print one non-negative integer β€” the answer to the problem. Example Input 5 5 1 4 3 7 10 3 1 1 1 4 6 2 5 3 2 2 4 1 1 Output 1 3 2 0 0 Note In the first test case there is only one pair: (4,7): for it 4 \& 7 = 4, and 4 βŠ• 7 = 3. In the second test case all pairs are good. In the third test case there are two pairs: (6,5) and (2,3). In the fourth test case there are no good pairs.
instruction
0
65,772
12
131,544
Tags: bitmasks, math Correct Solution: ``` for _ in range(int(input())): n=int(input()) lst = list(map(int,input().split())) d={} for ele in lst: msbl = len(bin(ele))-2 if msbl in d: d[msbl] +=1 else: d[msbl] = 1 res = 0 for k in d: e = d[k] res+=(e*(e-1))/2 print(int(res)) ```
output
1
65,772
12
131,545
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n Γ— m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 Γ— 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most 3nm operations. You don't need to minimize the number of operations. It can be proved that it is always possible. Input The first line contains a single integer t (1 ≀ t ≀ 5000) β€” the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≀ n, m ≀ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≀ k ≀ 3nm) β€” the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≀ x_1, x_2, x_3 ≀ n, 1 ≀ y_1, y_2, y_3 ≀ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong into some 2 Γ— 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
instruction
0
65,786
12
131,572
Tags: constructive algorithms, implementation Correct Solution: ``` def correct(x, y): ans.append([x+1, y+1, x+1, y+2, x+2, y+2]) ans.append([x+1, y+1, x+2, y+1, x+2, y+2]) ans.append([x+1, y+1, x+1, y+2, x+2, y+1]) for nt in range(int(input())): n,m = map(int,input().split()) a, ans = [], [] for i in range(n): a.append(list(map(int,list(input())))) for i in range(n-1): for j in range(m-1): if a[i][j]==1: correct(i, j) for i in range(n-1): if a[i][-1]==1: ans.append([i+1, m, i+2, m, i+1, m-1]) ans.append([i+1, m, i+2, m, i+2, m-1]) ans.append([i+1, m, i+1, m-1, i+2, m-1]) for i in range(m-1): if a[-1][i]==1: ans.append([n, i+1, n-1, i+1, n-1, i+2]) ans.append([n, i+1, n-1, i+1, n, i+2]) ans.append([n, i+1, n-1, i+2, n, i+2]) if a[-1][-1]==1: ans.append([n, m, n-1, m, n-1, m-1]) ans.append([n, m, n-1, m, n, m-1]) ans.append([n, m, n-1, m-1, n, m-1]) print (len(ans)) for i in ans: print (*i) ```
output
1
65,786
12
131,573
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n Γ— m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 Γ— 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most 3nm operations. You don't need to minimize the number of operations. It can be proved that it is always possible. Input The first line contains a single integer t (1 ≀ t ≀ 5000) β€” the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≀ n, m ≀ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≀ k ≀ 3nm) β€” the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≀ x_1, x_2, x_3 ≀ n, 1 ≀ y_1, y_2, y_3 ≀ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong into some 2 Γ— 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
instruction
0
65,787
12
131,574
Tags: constructive algorithms, implementation Correct Solution: ``` def unique_pairs(n): """Produce pairs of indexes in range(n)""" for i in range(n): for j in range(n): yield i, j def oper1(table, n, m, res): for i in range(n-1, 1, -1): for j in range(m-2): if table[i][j] == 1: res.append([i, j, i-1, j, i-1, j+1]) table[i-1][j] ^= 1 table[i-1][j+1] ^= 1 if table[i][m-1] == table[i][m-2] == 1: res.append([i, m-1, i, m-2, i-1, m-1]) table[i-1][m-1] ^= 1 elif table[i][m-1]: res.append([i, m-1, i-1, m-1, i-1, m-2]) table[i-1][m-1] ^= 1 table[i-1][m-2] ^= 1 elif table[i][m-2]: res.append([i, m-2, i-1, m-2, i-1, m-1]) table[i-1][m-1] ^= 1 table[i-1][m-2] ^= 1 table = table[:-1] return table, res def oper2(table, m, res): for j in range(m-1, 1, -1): if table[0][j] == table[1][j] == 1: res.append([0, j, 1, j, 0, j-1]) table[0][j-1] ^= 1 elif table[0][j]: res.append([0, j, 0, j-1, 1, j-1]) table[0][j-1] ^= 1 table[1][j-1] ^= 1 elif table[1][j]: res.append([1, j, 0, j-1, 1, j-1]) table[0][j-1] ^= 1 table[1][j-1] ^= 1 return table, res def solve(t, n, m): res = [] if n > 2: t, res = oper1(t, n, m, res) if m > 2: t, res = oper2(t, m, res) t = [i[:2] for i in t] cnt1 = sum(x.count(1) for x in t) if cnt1: if cnt1 == 4: res.append([0, 1, 1, 1, 1, 0]) t = [[1, 0], [0, 0]] cnt1 -= 3 if cnt1 == 1: for i, j in unique_pairs(2): if t[i][j] == 1: res.append([i, j, i^1, j, i^1, j^1]) t[i][j], t[i^1][j], t[i^1][j^1] = 0, 1, 1 break cnt1 += 1 if cnt1 == 2: cnt = 0 qwq = [] for i, j in unique_pairs(2): if t[i][j] == 0: qwq.extend([i, j]) t[i][j] = 1 elif not cnt: qwq.extend([i, j]) cnt += 1 t[i][j] = 0 res.append(qwq) cnt1 += 1 if cnt1 == 3: for i, j in unique_pairs(2): if not t[i][j]: res.append([i, j^1, i^1, j^1, i^1, j]) break print(len(res)) res = [[j+1 for j in i] for i in res] for _ in res: print(*_) for _ in range(int(input())): n, m = map(int, input().split()) table = [] for i in range(n): table.append([int(i) for i in list(input())]) solve(table, n, m) ```
output
1
65,787
12
131,575
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n Γ— m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 Γ— 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most 3nm operations. You don't need to minimize the number of operations. It can be proved that it is always possible. Input The first line contains a single integer t (1 ≀ t ≀ 5000) β€” the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≀ n, m ≀ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≀ k ≀ 3nm) β€” the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≀ x_1, x_2, x_3 ≀ n, 1 ≀ y_1, y_2, y_3 ≀ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong into some 2 Γ— 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
instruction
0
65,788
12
131,576
Tags: constructive algorithms, implementation Correct Solution: ``` t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = list( list(map(int , input())) for _ in range(n)) all_ans = list() for i in range(n-1): for j in range(m-1): ans = list() if a[i][j] == 0 and 0 == a[i][j+1]: continue if a[i][j] == 1: ans.append(i+1) ans.append(j+1) a[i][j] = 0 if a[i][j+1] == 1: ans.append(i+1) ans.append(j+2) a[i][j+1] = 0 ans.append(i+2) ans.append(j+1) a[i+1][j] = (a[i+1][j]+1)%2 if len(ans) == 4: ans.append(i+2) ans.append(j+2) a[i+1][j+1] = (a[i+1][j+1]+1)%2 # else: # if a[i][j] == 0: # continue # ans.append(j+1) # ans.append(i+1) # a[i][j] = 0 # ans.append(j+1) # ans.append(i+2) # a[i+1][j] = (a[i+1][j]+1)%2 # ans.append(j+2) # ans.append(i+2) # a[i+1][j+1] = (a[i+1][j+1]+1)%2 all_ans.append(ans) for j in range(m-1): ans = list() if a[-2][j] == 0 and 0 == a[-1][j]: continue if a[-2][j] == 1: # print("fuck", j) ans.append(n-1) ans.append(j+1) a[-2][j] = 0 if a[-1][j] == 1: # print("fucku", j) ans.append(n) ans.append(j+1) a[-1][j] = 0 ans.append(n-1) ans.append(j+2) a[-2][j+1] = (a[-2][j+1]+1)%2 if len(ans) == 4: ans.append(n) ans.append(j+2) a[-1][j+1] = (a[-1][j+1]+1)%2 all_ans.append(ans) if a[-2][-1] == a[-1][-1] == 0: pass elif a[-2][-1] == 0 and a[-1][-1] == 1: ans = list() ans.append(n-1) ans.append(m-1) a[-2][-2] = 1 ans.append(n) ans.append(m-1) a[-1][-2] = 1 ans.append(n) ans.append(m) a[-1][-1] = 0 all_ans.append(ans) elif a[-2][-1] == 1 and a[-1][-1] == 0: ans = list() ans.append(n-1) ans.append(m-1) a[-2][-2] = 1 ans.append(n) ans.append(m-1) a[-1][-2] = 1 ans.append(n-1) ans.append(m) a[-2][-1] = 0 all_ans.append(ans) if a[-1][-2] == 1: ans = list() ans.append(n) ans.append(m-1) a[-1][-2] = 0 ans.append(n) ans.append(m) a[-1][-1] = 1 ans.append(n-1) ans.append(m) a[-2][-1] = 1 all_ans.append(ans) ans = list() ans.append(n-1) ans.append(m-1) a[-2][-2] = 0 ans.append(n) ans.append(m) a[-1][-1] = 0 ans.append(n-1) ans.append(m) a[-2][-1] = 0 all_ans.append(ans) elif a[-1][-1] == 1: ans = list() ans.append(n) ans.append(m) a[-1][-1] = 0 ans.append(n) ans.append(m-1) a[-1][-2] = 1 ans.append(n-1) ans.append(m-1) a[-2][-2] = 1 all_ans.append(ans) ans = list() ans.append(n-1) ans.append(m-1) a[-2][-2] = 0 ans.append(n) ans.append(m-1) a[-1][-1] = 0 ans.append(n-1) ans.append(m) a[-2][-1] = 0 all_ans.append(ans) print(len(all_ans)) for ans in all_ans: print(*ans) ```
output
1
65,788
12
131,577
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n Γ— m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 Γ— 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most 3nm operations. You don't need to minimize the number of operations. It can be proved that it is always possible. Input The first line contains a single integer t (1 ≀ t ≀ 5000) β€” the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≀ n, m ≀ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≀ k ≀ 3nm) β€” the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≀ x_1, x_2, x_3 ≀ n, 1 ≀ y_1, y_2, y_3 ≀ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong into some 2 Γ— 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
instruction
0
65,789
12
131,578
Tags: constructive algorithms, implementation Correct Solution: ``` import bisect import collections import copy import functools import heapq import itertools import math import sys import string import random from typing import List sys.setrecursionlimit(99999) t = int(input()) for _ in range(t): n,m = map(int,input().split()) arr = [] for _ in range(n): arr.append(list(map(int,list(input())))) ans = [] def add(x1,y1,x2,y2,x3,y3): ans.append([x1+1, y1+1, x2+1, y2+1, x3+1, y3+1]) arr[x1][y1] ^= 1 arr[x2][y2] ^= 1 arr[x3][y3] ^= 1 for i in range(n-1,1,-1): for j in range(m): if arr[i][j]==1: nj = j+1 if j<m-1 else j-1 add(i,j,i-1,j,i-1,nj) for j in range(m-1,1,-1): if arr[0][j]==1 and arr[1][j]==1: add(0,j,1,j,0,j-1) elif arr[0][j]==1: add(0,j,0,j-1,1,j-1) elif arr[1][j]==1: add(1,j,0,j-1,1,j-1) while sum(arr[0][:2]+arr[1][:2])!=0: if sum(arr[0][:2]+arr[1][:2]) in (3,4): ps = [] for i in range(2): for j in range(2): if arr[i][j]==1: ps.append(i) ps.append(j) add(*ps[:6]) elif sum(arr[0][:2]+arr[1][:2]) in (1,2): ps1 = [] ps0 = [] for i in range(2): for j in range(2): if arr[i][j] == 1: ps1.append(i) ps1.append(j) else: ps0.append(i) ps0.append(j) add(*(ps1[:2]+ps0[:4])) print(len(ans)) for i in range(len(ans)): print(*ans[i]) ```
output
1
65,789
12
131,579
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n Γ— m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 Γ— 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most 3nm operations. You don't need to minimize the number of operations. It can be proved that it is always possible. Input The first line contains a single integer t (1 ≀ t ≀ 5000) β€” the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≀ n, m ≀ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≀ k ≀ 3nm) β€” the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≀ x_1, x_2, x_3 ≀ n, 1 ≀ y_1, y_2, y_3 ≀ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong into some 2 Γ— 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
instruction
0
65,790
12
131,580
Tags: constructive algorithms, implementation Correct Solution: ``` import sys from itertools import permutations from itertools import combinations from itertools import combinations_with_replacement #sys.stdin = open('/Users/pranjalkandhari/Desktop/Template/input.txt', 'r') def makeThree(mat , i , j , liAns): liToAdd = [] if(mat[i][j] == 1): liToAdd.append(i) liToAdd.append(j) mat[i][j] = 0 if(mat[i+1][j] == 1): liToAdd.append(i+1) liToAdd.append(j) mat[i+1][j] = 0 if(mat[i][j+1] == 1): liToAdd.append(i) liToAdd.append(j+1) mat[i][j+1] = 0 if(mat[i+1][j+1] == 1): liToAdd.append(i+1) liToAdd.append(j+1) mat[i+1][j+1] = 0 liAns.append(liToAdd) def makeTwo(mat , i , j , liAns): liToAdd = [] liAllCoord = [(i,j) , (i+1,j) , (i,j+1) , (i+1,j+1)] flag = 0 for coord in liAllCoord: #print(mat[coord[0]][coord[1]]) if(mat[coord[0]][coord[1]] == 1 and flag == 0): flag = 1 liToAdd.append(coord[0]) liToAdd.append(coord[1]) if(mat[coord[0]][coord[1]] == 0): liToAdd.append(coord[0]) liToAdd.append(coord[1]) a = 0 #print('makeTwo' , liToAdd) while(a<6): #print('46 A' , a) initial = mat[liToAdd[a]][liToAdd[a+1]] if(initial == 1): mat[liToAdd[a]][liToAdd[a+1]] = 0 else: mat[liToAdd[a]][liToAdd[a+1]] = 1 a+=2 liAns.append(liToAdd) makeThree(mat , i , j , liAns) def makeOne(mat , i , j , liAns): liToAdd = [] liAllCoord = [(i,j) , (i+1,j) , (i,j+1) , (i+1,j+1)] ctr = 0 for coord in liAllCoord: if(mat[coord[0]][coord[1]] == 1): liToAdd.append(coord[0]) liToAdd.append(coord[1]) if(mat[coord[0]][coord[1]] == 0 and ctr<2): ctr += 1 liToAdd.append(coord[0]) liToAdd.append(coord[1]) a = 0 while(a<6): initial = mat[liToAdd[a]][liToAdd[a+1]] if(initial == 1): mat[liToAdd[a]][liToAdd[a+1]] = 0 else: mat[liToAdd[a]][liToAdd[a+1]] = 1 a+=2 liAns.append(liToAdd) makeTwo(mat , i , j , liAns) def makeFour(mat , i , j , liAns): liAllCoord = [(i,j) , (i+1,j) , (i,j+1) , (i+1,j+1)] ctr = 0 liToAdd = [] for coord in liAllCoord: if(ctr>=3): break ctr += 1 liToAdd.append(coord[0]) liToAdd.append(coord[1]) mat[coord[0]][coord[1]] = 0 liAns.append(liToAdd) makeOne(mat , i , j , liAns) for _ in range( int(input()) ): liStr = input().split() p = int(liStr[0]) q = int(liStr[1]) mat = [] #Matrix of size pXq for _ in range(0,p): liStr = list(input()) li = [] for i in liStr: li.append(int(i)) mat.append(li) #print(mat) liAns = [] #List of tuple of 6 elements. i = 0 while(i<p): j = 0 while(j<q): if(i == (p-1)): i = i-1 if(j == (q-1)): j = j-1 ctr = 0 if(mat[i][j] == 1): ctr += 1 if(mat[i+1][j] == 1): ctr += 1 if(mat[i][j+1] == 1): ctr += 1 if(mat[i+1][j+1] == 1): ctr += 1 if(ctr == 1): makeOne(mat , i , j , liAns) if(ctr == 2): makeTwo(mat , i , j , liAns) if(ctr == 3): makeThree(mat , i , j , liAns) if(ctr == 4): makeFour(mat , i , j , liAns) j += 2 i += 2 print(len(liAns)) for sixNum in liAns: for a in sixNum: print(a+1 , end = ' ') print() ```
output
1
65,790
12
131,581
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n Γ— m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 Γ— 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most 3nm operations. You don't need to minimize the number of operations. It can be proved that it is always possible. Input The first line contains a single integer t (1 ≀ t ≀ 5000) β€” the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≀ n, m ≀ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≀ k ≀ 3nm) β€” the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≀ x_1, x_2, x_3 ≀ n, 1 ≀ y_1, y_2, y_3 ≀ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong into some 2 Γ— 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
instruction
0
65,791
12
131,582
Tags: constructive algorithms, implementation Correct Solution: ``` import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for k in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for k in range(c)] for k in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') INF = 10**19 MOD = 10**9 + 7 EPS = 10**-10 def check(i, j): res = [] cnt = grid[i][j] + grid[i][j+1] + grid[i+1][j] + grid[i+1][j+1] if cnt == 1: if grid[i][j]: res.append((i+1, j+1, i+1, j+2, i+2, j+1)) res.append((i+1, j+1, i+1, j+2, i+2, j+2)) res.append((i+1, j+1, i+2, j+1, i+2, j+2)) elif grid[i+1][j]: res.append((i+2, j+1, i+1, j+2, i+1, j+1)) res.append((i+2, j+1, i+1, j+2, i+2, j+2)) res.append((i+2, j+1, i+1, j+1, i+2, j+2)) elif grid[i][j+1]: res.append((i+1, j+2, i+1, j+1, i+2, j+1)) res.append((i+1, j+2, i+1, j+1, i+2, j+2)) res.append((i+1, j+2, i+2, j+1, i+2, j+2)) elif grid[i+1][j+1]: res.append((i+2, j+2, i+1, j+2, i+2, j+1)) res.append((i+2, j+2, i+1, j+2, i+1, j+1)) res.append((i+2, j+2, i+2, j+1, i+1, j+1)) elif cnt == 2: # ζ–œγ‚ if grid[i][j] and grid[i+1][j+1]: res.append((i+1, j+1, i+1, j+2, i+2, j+1)) res.append((i+2, j+2, i+1, j+2, i+2, j+1)) elif grid[i+1][j] and grid[i][j+1]: res.append((i+1, j+1, i+1, j+2, i+2, j+2)) res.append((i+1, j+1, i+2, j+1, i+2, j+2)) # ηΈ¦ elif grid[i][j] and grid[i+1][j]: res.append((i+1, j+1, i+1, j+2, i+2, j+2)) res.append((i+2, j+1, i+1, j+2, i+2, j+2)) elif grid[i][j+1] and grid[i+1][j+1]: res.append((i+1, j+2, i+1, j+1, i+2, j+1)) res.append((i+2, j+2, i+1, j+1, i+2, j+1)) # ζ¨ͺ elif grid[i][j] and grid[i][j+1]: res.append((i+1, j+1, i+2, j+1, i+2, j+2)) res.append((i+1, j+2, i+2, j+1, i+2, j+2)) elif grid[i+1][j] and grid[i+1][j+1]: res.append((i+2, j+2, i+1, j+2, i+1, j+1)) res.append((i+2, j+1, i+1, j+2, i+1, j+1)) elif cnt == 3: if grid[i][j] and grid[i][j+1] and grid[i+1][j+1]: res.append((i+1, j+1, i+1, j+2, i+2, j+2)) elif grid[i][j+1] and grid[i+1][j+1] and grid[i+1][j]: res.append((i+1, j+2, i+2, j+2, i+2, j+1)) elif grid[i+1][j+1] and grid[i+1][j] and grid[i][j]: res.append((i+2, j+2, i+2, j+1, i+1, j+1)) elif grid[i+1][j] and grid[i][j] and grid[i][j+1]: res.append((i+2, j+1, i+1, j+1, i+1, j+2)) elif cnt == 4: res.append((i+1, j+1, i+1, j+2, i+2, j+1)) res.append((i+1, j+1, i+1, j+2, i+2, j+2)) res.append((i+1, j+1, i+2, j+1, i+2, j+2)) res.append((i+1, j+2, i+2, j+1, i+2, j+2)) grid[i][j] = grid[i][j+1] = grid[i+1][j] = grid[i+1][j+1] = 0 return res def checkh(i, j): res = [] if grid[i][j] and grid[i+1][j]: res.append((i+1, j+1, i+1, j+0, i+2, j+0)) res.append((i+2, j+1, i+1, j+0, i+2, j+0)) elif grid[i][j]: res.append((i+1, j+1, i+1, j+0, i+2, j+0)) grid[i][j-1] ^= 1 grid[i+1][j-1] ^= 1 elif grid[i+1][j]: res.append((i+2, j+1, i+1, j+0, i+2, j+0)) grid[i][j-1] ^= 1 grid[i+1][j-1] ^= 1 grid[i][j] = grid[i+1][j] = 0 return res def checkw(i, j): res = [] if grid[i][j] and grid[i][j+1]: res.append((i+1, j+1, i+0, j+1, i+0, j+2)) res.append((i+1, j+2, i+0, j+1, i+0, j+2)) elif grid[i][j]: res.append((i+1, j+1, i+0, j+1, i+0, j+2)) grid[i-1][j] ^= 1 grid[i-1][j+1] ^= 1 elif grid[i][j+1]: res.append((i+1, j+2, i+0, j+1, i+0, j+2)) grid[i-1][j] ^= 1 grid[i-1][j+1] ^= 1 grid[i][j] = grid[i][j+1] = 0 return res for _ in range(INT()): H, W = MAP() grid = [[]] * H for i in range(H): grid[i] = list(map(int, input())) ans = [] if H % 2 == 1: for j in range(W-1): ans += checkw(H-1, j) if W % 2 == 1: for i in range(H-1): ans += checkh(i, W-1) for i in range(0, H-H%2, 2): for j in range(0, W-W%2, 2): ans += check(i, j) print(len(ans)) for a in ans: print(*a) ```
output
1
65,791
12
131,583
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n Γ— m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 Γ— 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most 3nm operations. You don't need to minimize the number of operations. It can be proved that it is always possible. Input The first line contains a single integer t (1 ≀ t ≀ 5000) β€” the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≀ n, m ≀ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≀ k ≀ 3nm) β€” the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≀ x_1, x_2, x_3 ≀ n, 1 ≀ y_1, y_2, y_3 ≀ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong into some 2 Γ— 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
instruction
0
65,792
12
131,584
Tags: constructive algorithms, implementation Correct Solution: ``` t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = [list(map(int, input())) for i in range(n)] def add(x, y, z): a[x[0]][x[1]] ^= 1 a[y[0]][y[1]] ^= 1 a[z[0]][z[1]] ^= 1 ans.append(' '.join(map(lambda x: str(x + 1), (*x, *y, *z)))) ans = [] for i in range(n - 1, 1, -1): for j in range(m): if a[i][j]: add((i, j), (i - 1, j), (i - 1, j + (1 if j < m - 1 else -1))) for j in range(m - 1, 1, -1): for i in (0, 1): if a[i][j]: add((i, j), (0, j - 1), (1, j - 1)) for i in (0, 1): for j in (0, 1): if (a[0][0] + a[0][1] + a[1][0] + a[1][1] + a[i][j]) % 2: add((i ^ 1, j), (i, j ^ 1), (i ^ 1, j ^ 1)) print(len(ans)) print('\n'.join(ans)) ```
output
1
65,792
12
131,585
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size n Γ— m. This table consists of symbols 0 and 1. You can make such operation: select 3 different cells that belong to one 2 Γ— 2 square and change the symbols in these cells (change 0 to 1 and 1 to 0). Your task is to make all symbols in the table equal to 0. You are allowed to make at most 3nm operations. You don't need to minimize the number of operations. It can be proved that it is always possible. Input The first line contains a single integer t (1 ≀ t ≀ 5000) β€” the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers n, m (2 ≀ n, m ≀ 100). Each of the next n lines contains a binary string of length m, describing the symbols of the next row of the table. It is guaranteed that the sum of nm for all test cases does not exceed 20000. Output For each test case print the integer k (0 ≀ k ≀ 3nm) β€” the number of operations. In the each of the next k lines print 6 integers x_1, y_1, x_2, y_2, x_3, y_3 (1 ≀ x_1, x_2, x_3 ≀ n, 1 ≀ y_1, y_2, y_3 ≀ m) describing the next operation. This operation will be made with three cells (x_1, y_1), (x_2, y_2), (x_3, y_3). These three cells should be different. These three cells should belong into some 2 Γ— 2 square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells (1, 1), (2, 1), (2, 2). After that, all symbols will be equal to 0. In the second test case: * operation with cells (2, 1), (3, 1), (3, 2). After it the table will be: 011 001 000 * operation with cells (1, 2), (1, 3), (2, 3). After it the table will be: 000 000 000 In the fifth test case: * operation with cells (1, 3), (2, 2), (2, 3). After it the table will be: 010 110 * operation with cells (1, 2), (2, 1), (2, 2). After it the table will be: 000 000
instruction
0
65,793
12
131,586
Tags: constructive algorithms, implementation Correct Solution: ``` t = int(input()) for _ in range(t): n,m = map(int, input().split()) S = [] for i in range(n): s = list(str(input())) s = [int(i) for i in s] S.append(s) ans = [] for i in range(n-1): for j in range(m-1): if i == n-2 and j == m-2: X0 = [] X1 = [] if S[i][j] == 0: X0.append((i+1, j+1)) else: X1.append((i+1, j+1)) if S[i+1][j] == 0: X0.append((i+2, j+1)) else: X1.append((i+2, j+1)) if S[i][j+1] == 0: X0.append((i+1, j+2)) else: X1.append((i+1, j+2)) if S[i+1][j+1] == 0: X0.append((i+2, j+2)) else: X1.append((i+2, j+2)) #print(X0) #print(X1) if len(X1) == 1: temp = list(X0[0])+list(X0[1])+list(X1[0]) ans.append(temp) temp = list(X0[0])+list(X0[2])+list(X1[0]) ans.append(temp) temp = list(X0[1])+list(X0[2])+list(X1[0]) ans.append(temp) elif len(X1) == 2: temp = list(X0[0])+list(X0[1])+list(X1[0]) ans.append(temp) temp = list(X0[0])+list(X0[1])+list(X1[1]) ans.append(temp) elif len(X1) == 3: temp = list(X1[0])+list(X1[1])+list(X1[2]) ans.append(temp) elif len(X1) == 4: temp = list(X1[1])+list(X1[2])+list(X1[3]) ans.append(temp) temp = list(X1[0])+list(X1[1])+list(X1[2]) ans.append(temp) temp = list(X1[0])+list(X1[1])+list(X1[3]) ans.append(temp) temp = list(X1[0])+list(X1[2])+list(X1[3]) ans.append(temp) else: pass S[i][j] = 0 S[i+1][j] = 0 S[i][j+1] = 0 S[i+1][j+1] = 0 elif i == n-2: if S[i][j] == 1 and S[i+1][j] == 1: temp = [i+1, j+1, i+2, j+1, i+1, j+2] ans.append(temp) S[i][j] = 1-S[i][j] S[i+1][j] = 1-S[i+1][j] S[i][j+1] = 1-S[i][j+1] elif S[i][j] == 1: temp = [i+1, j+1, i+1, j+2, i+2, j+2] ans.append(temp) S[i][j] = 1-S[i][j] S[i][j+1] = 1-S[i][j+1] S[i+1][j+1] = 1-S[i+1][j+1] elif S[i+1][j] == 1: temp = [i+2, j+1, i+1, j+2, i+2, j+2] ans.append(temp) S[i+1][j] = 1-S[i+1][j] S[i][j+1] = 1-S[i][j+1] S[i+1][j+1] = 1-S[i+1][j+1] else: pass elif j == m-2: if S[i][j] == 1 and S[i][j+1] == 1: temp = [i+1, j+1, i+1, j+2, i+2, j+2] ans.append(temp) S[i][j] = 1-S[i][j] S[i][j+1] = 1-S[i][j+1] S[i+1][j+1] = 1-S[i+1][j+1] elif S[i][j] == 1: temp = [i+1, j+1, i+2, j+1, i+2, j+2] ans.append(temp) S[i][j] = 1-S[i][j] S[i+1][j] = 1-S[i+1][j] S[i+1][j+1] = 1-S[i+1][j+1] elif S[i][j+1] == 1: temp = [i+1, j+2, i+2, j+1, i+2, j+2] ans.append(temp) S[i][j+1] = 1-S[i][j+1] S[i+1][j] = 1-S[i+1][j] S[i+1][j+1] = 1-S[i+1][j+1] else: pass else: if S[i][j] == 0: continue else: temp = [i+1, j+1, i+1, j+2, i+2, j+1] ans.append(temp) S[i][j] = 1-S[i][j] S[i][j+1] = 1-S[i][j+1] S[i+1][j] = 1-S[i+1][j] print(len(ans)) for i in range(len(ans)): print(*ans[i]) ```
output
1
65,793
12
131,587
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any i (1 ≀ i ≀ n-1), this condition must be satisfied: $$$(max(a[i], a[i+1]))/(min(a[i], a[i+1])) ≀ 2$$$ For example, the arrays [1, 2, 3, 4, 3], [1, 1, 1] and [5, 10] are dense. And the arrays [5, 11], [1, 4, 2], [6, 6, 1] are not dense. You are given an array a of n integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added. For example, if a=[4,2,10,1], then the answer is 5, and the array itself after inserting elements into it may look like this: a=[4,2,\underline{3},\underline{5},10,\underline{6},\underline{4},\underline{2},1] (there are other ways to build such a). Input The first line contains one integer t (1 ≀ t ≀ 1000). Then t test cases follow. The first line of each test case contains one integer n (2 ≀ n ≀ 50) β€” the length of the array a. The next line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 50). Output For each test case, output one integer β€” the minimum number of numbers that must be added to the array to make it dense. Example Input 6 4 4 2 10 1 2 1 3 2 6 1 3 1 4 2 5 1 2 3 4 3 12 4 31 25 50 30 20 34 46 42 16 15 16 Output 5 1 2 1 0 3 Note The first test case is explained in the statements. In the second test case, you can insert one element, a=[1,\underline{2},3]. In the third test case, you can insert two elements, a=[6,\underline{4},\underline{2},1]. In the fourth test case, you can insert one element, a=[1,\underline{2},4,2]. In the fifth test case, the array a is already dense.
instruction
0
65,803
12
131,606
Tags: greedy, math Correct Solution: ``` for _ in range(int(input())): n=int(input()) a=[int(i) for i in input().split()] ans=0 for i in range(n-1): if max(a[i],a[i+1])/min(a[i],a[i+1])>2: maxx=max(a[i],a[i+1]) minn=min(a[i],a[i+1]) while minn<maxx: ans+=1 minn*=2 ans-=1 print(ans) ```
output
1
65,803
12
131,607
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any i (1 ≀ i ≀ n-1), this condition must be satisfied: $$$(max(a[i], a[i+1]))/(min(a[i], a[i+1])) ≀ 2$$$ For example, the arrays [1, 2, 3, 4, 3], [1, 1, 1] and [5, 10] are dense. And the arrays [5, 11], [1, 4, 2], [6, 6, 1] are not dense. You are given an array a of n integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added. For example, if a=[4,2,10,1], then the answer is 5, and the array itself after inserting elements into it may look like this: a=[4,2,\underline{3},\underline{5},10,\underline{6},\underline{4},\underline{2},1] (there are other ways to build such a). Input The first line contains one integer t (1 ≀ t ≀ 1000). Then t test cases follow. The first line of each test case contains one integer n (2 ≀ n ≀ 50) β€” the length of the array a. The next line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 50). Output For each test case, output one integer β€” the minimum number of numbers that must be added to the array to make it dense. Example Input 6 4 4 2 10 1 2 1 3 2 6 1 3 1 4 2 5 1 2 3 4 3 12 4 31 25 50 30 20 34 46 42 16 15 16 Output 5 1 2 1 0 3 Note The first test case is explained in the statements. In the second test case, you can insert one element, a=[1,\underline{2},3]. In the third test case, you can insert two elements, a=[6,\underline{4},\underline{2},1]. In the fourth test case, you can insert one element, a=[1,\underline{2},4,2]. In the fifth test case, the array a is already dense.
instruction
0
65,804
12
131,608
Tags: greedy, math Correct Solution: ``` for _ in range(int(input())): n=int(input()) l=list(map(int,input().split())) s=0 for i in range(n-1): a=max(l[i],l[i+1]) b=min(l[i],l[i+1]) while a>(2*b): s+=1 b=b*2 print(s) ```
output
1
65,804
12
131,609
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any i (1 ≀ i ≀ n-1), this condition must be satisfied: $$$(max(a[i], a[i+1]))/(min(a[i], a[i+1])) ≀ 2$$$ For example, the arrays [1, 2, 3, 4, 3], [1, 1, 1] and [5, 10] are dense. And the arrays [5, 11], [1, 4, 2], [6, 6, 1] are not dense. You are given an array a of n integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added. For example, if a=[4,2,10,1], then the answer is 5, and the array itself after inserting elements into it may look like this: a=[4,2,\underline{3},\underline{5},10,\underline{6},\underline{4},\underline{2},1] (there are other ways to build such a). Input The first line contains one integer t (1 ≀ t ≀ 1000). Then t test cases follow. The first line of each test case contains one integer n (2 ≀ n ≀ 50) β€” the length of the array a. The next line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 50). Output For each test case, output one integer β€” the minimum number of numbers that must be added to the array to make it dense. Example Input 6 4 4 2 10 1 2 1 3 2 6 1 3 1 4 2 5 1 2 3 4 3 12 4 31 25 50 30 20 34 46 42 16 15 16 Output 5 1 2 1 0 3 Note The first test case is explained in the statements. In the second test case, you can insert one element, a=[1,\underline{2},3]. In the third test case, you can insert two elements, a=[6,\underline{4},\underline{2},1]. In the fourth test case, you can insert one element, a=[1,\underline{2},4,2]. In the fifth test case, the array a is already dense.
instruction
0
65,805
12
131,610
Tags: greedy, math Correct Solution: ``` ssi = lambda : map(int, input().split()) for _ in range(int(input())): n = int(input()) l = list(ssi()) i= 0 ans=0 while i<n-1: s,b = sorted(l[i:i+2]) while 2*s<b: s<<=1 ans+=1 i+=1 print(ans) ```
output
1
65,805
12
131,611
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any i (1 ≀ i ≀ n-1), this condition must be satisfied: $$$(max(a[i], a[i+1]))/(min(a[i], a[i+1])) ≀ 2$$$ For example, the arrays [1, 2, 3, 4, 3], [1, 1, 1] and [5, 10] are dense. And the arrays [5, 11], [1, 4, 2], [6, 6, 1] are not dense. You are given an array a of n integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added. For example, if a=[4,2,10,1], then the answer is 5, and the array itself after inserting elements into it may look like this: a=[4,2,\underline{3},\underline{5},10,\underline{6},\underline{4},\underline{2},1] (there are other ways to build such a). Input The first line contains one integer t (1 ≀ t ≀ 1000). Then t test cases follow. The first line of each test case contains one integer n (2 ≀ n ≀ 50) β€” the length of the array a. The next line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 50). Output For each test case, output one integer β€” the minimum number of numbers that must be added to the array to make it dense. Example Input 6 4 4 2 10 1 2 1 3 2 6 1 3 1 4 2 5 1 2 3 4 3 12 4 31 25 50 30 20 34 46 42 16 15 16 Output 5 1 2 1 0 3 Note The first test case is explained in the statements. In the second test case, you can insert one element, a=[1,\underline{2},3]. In the third test case, you can insert two elements, a=[6,\underline{4},\underline{2},1]. In the fourth test case, you can insert one element, a=[1,\underline{2},4,2]. In the fifth test case, the array a is already dense.
instruction
0
65,806
12
131,612
Tags: greedy, math Correct Solution: ``` def inl(): return [int(i) for i in input().split()] def inp(): return int(input()) if __name__ == "__main__": for _ in range(inp()): n = inp() a = inl() cnt = 0 for i in range(1,n): x = min(a[i],a[i-1]) y = max(a[i],a[i-1]) while 2*x < y: x =x*2 cnt +=1 print(cnt) ```
output
1
65,806
12
131,613
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any i (1 ≀ i ≀ n-1), this condition must be satisfied: $$$(max(a[i], a[i+1]))/(min(a[i], a[i+1])) ≀ 2$$$ For example, the arrays [1, 2, 3, 4, 3], [1, 1, 1] and [5, 10] are dense. And the arrays [5, 11], [1, 4, 2], [6, 6, 1] are not dense. You are given an array a of n integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added. For example, if a=[4,2,10,1], then the answer is 5, and the array itself after inserting elements into it may look like this: a=[4,2,\underline{3},\underline{5},10,\underline{6},\underline{4},\underline{2},1] (there are other ways to build such a). Input The first line contains one integer t (1 ≀ t ≀ 1000). Then t test cases follow. The first line of each test case contains one integer n (2 ≀ n ≀ 50) β€” the length of the array a. The next line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 50). Output For each test case, output one integer β€” the minimum number of numbers that must be added to the array to make it dense. Example Input 6 4 4 2 10 1 2 1 3 2 6 1 3 1 4 2 5 1 2 3 4 3 12 4 31 25 50 30 20 34 46 42 16 15 16 Output 5 1 2 1 0 3 Note The first test case is explained in the statements. In the second test case, you can insert one element, a=[1,\underline{2},3]. In the third test case, you can insert two elements, a=[6,\underline{4},\underline{2},1]. In the fourth test case, you can insert one element, a=[1,\underline{2},4,2]. In the fifth test case, the array a is already dense.
instruction
0
65,807
12
131,614
Tags: greedy, math Correct Solution: ``` import math t = int(input()) def check(): return max(a[i], a[i+1]) > 2* min(a[i], a[i+1]) res = [] for _ in range(t): n = int(input()) a = [int(i) for i in input().split()] i=0 c=0 while(i<len(a)-1): if check(): a.insert(i+1, math.ceil(max(a[i], a[i+1])/2)) c+=1 else: i += 1 res.append(c) for i in res: print(i) ```
output
1
65,807
12
131,615
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any i (1 ≀ i ≀ n-1), this condition must be satisfied: $$$(max(a[i], a[i+1]))/(min(a[i], a[i+1])) ≀ 2$$$ For example, the arrays [1, 2, 3, 4, 3], [1, 1, 1] and [5, 10] are dense. And the arrays [5, 11], [1, 4, 2], [6, 6, 1] are not dense. You are given an array a of n integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added. For example, if a=[4,2,10,1], then the answer is 5, and the array itself after inserting elements into it may look like this: a=[4,2,\underline{3},\underline{5},10,\underline{6},\underline{4},\underline{2},1] (there are other ways to build such a). Input The first line contains one integer t (1 ≀ t ≀ 1000). Then t test cases follow. The first line of each test case contains one integer n (2 ≀ n ≀ 50) β€” the length of the array a. The next line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 50). Output For each test case, output one integer β€” the minimum number of numbers that must be added to the array to make it dense. Example Input 6 4 4 2 10 1 2 1 3 2 6 1 3 1 4 2 5 1 2 3 4 3 12 4 31 25 50 30 20 34 46 42 16 15 16 Output 5 1 2 1 0 3 Note The first test case is explained in the statements. In the second test case, you can insert one element, a=[1,\underline{2},3]. In the third test case, you can insert two elements, a=[6,\underline{4},\underline{2},1]. In the fourth test case, you can insert one element, a=[1,\underline{2},4,2]. In the fifth test case, the array a is already dense.
instruction
0
65,808
12
131,616
Tags: greedy, math Correct Solution: ``` import copy t=int(input()) for i in range(t): n=int(input()) a = list(map(int, input().rstrip().split())) gg=0 for i in range(len(a)-1): h= max(a[i],a[i+1]) m= min(a[i],a[i+1]) if h/m>2: t=copy.deepcopy(m) while h/t>2: gg+=1 t=2*t print(gg) ```
output
1
65,808
12
131,617
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any i (1 ≀ i ≀ n-1), this condition must be satisfied: $$$(max(a[i], a[i+1]))/(min(a[i], a[i+1])) ≀ 2$$$ For example, the arrays [1, 2, 3, 4, 3], [1, 1, 1] and [5, 10] are dense. And the arrays [5, 11], [1, 4, 2], [6, 6, 1] are not dense. You are given an array a of n integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added. For example, if a=[4,2,10,1], then the answer is 5, and the array itself after inserting elements into it may look like this: a=[4,2,\underline{3},\underline{5},10,\underline{6},\underline{4},\underline{2},1] (there are other ways to build such a). Input The first line contains one integer t (1 ≀ t ≀ 1000). Then t test cases follow. The first line of each test case contains one integer n (2 ≀ n ≀ 50) β€” the length of the array a. The next line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 50). Output For each test case, output one integer β€” the minimum number of numbers that must be added to the array to make it dense. Example Input 6 4 4 2 10 1 2 1 3 2 6 1 3 1 4 2 5 1 2 3 4 3 12 4 31 25 50 30 20 34 46 42 16 15 16 Output 5 1 2 1 0 3 Note The first test case is explained in the statements. In the second test case, you can insert one element, a=[1,\underline{2},3]. In the third test case, you can insert two elements, a=[6,\underline{4},\underline{2},1]. In the fourth test case, you can insert one element, a=[1,\underline{2},4,2]. In the fifth test case, the array a is already dense.
instruction
0
65,809
12
131,618
Tags: greedy, math Correct Solution: ``` import sys input = lambda: sys.stdin.readline().rstrip("\r\n") inp = lambda: list(map(int,sys.stdin.readline().rstrip("\r\n").split())) mod = 10**9+7; Mod = 998244353; INF = float('inf') #______________________________________________________________________________________________________ # from math import * # from bisect import * # from heapq import * # from collections import defaultdict as dd # from collections import OrderedDict as odict # from collections import Counter as cc # from collections import deque # sys.setrecursionlimit(10**5+100) #this is must for dfs # ______________________________________________________________________________________________________ # segment tree for range minimum query # n = int(input()) # a = list(map(int,input().split())) # st = [float('inf') for i in range(4*len(a))] # def build(a,ind,start,end): # if start == end: # st[ind] = a[start] # else: # mid = (start+end)//2 # build(a,2*ind+1,start,mid) # build(a,2*ind+2,mid+1,end) # st[ind] = min(st[2*ind+1],st[2*ind+2]) # build(a,0,0,n-1) # def query(ind,l,r,start,end): # if start>r or end<l: # return float('inf') # if l<=start<=end<=r: # return st[ind] # mid = (start+end)//2 # return min(query(2*ind+1,l,r,start,mid),query(2*ind+2,l,r,mid+1,end)) # ______________________________________________________________________________________________________ # Checking prime in O(root(N)) # def isprime(n): # if (n % 2 == 0 and n > 2) or n == 1: return 0 # else: # s = int(n**(0.5)) + 1 # for i in range(3, s, 2): # if n % i == 0: # return 0 # return 1 # def lcm(a,b): # return (a*b)//gcd(a,b) # ______________________________________________________________________________________________________ # nCr under mod # def C(n,r,mod): # if r>n: # return 0 # num = den = 1 # for i in range(r): # num = (num*(n-i))%mod # den = (den*(i+1))%mod # return (num*pow(den,mod-2,mod))%mod # ______________________________________________________________________________________________________ # For smallest prime factor of a number # M = 2*(10**5)+10 # pfc = [i for i in range(M)] # def pfcs(M): # for i in range(2,M): # if pfc[i]==i: # for j in range(i+i,M,i): # if pfc[j]==j: # pfc[j] = i # return # pfcs(M) # ______________________________________________________________________________________________________ tc = 1 tc, = inp() for _ in range(tc): n, = inp() a = inp() ans = 0 for i in range(n-1): b,c = a[i],a[i+1] if b>c: b,c = c,b while(2*b<c): ans+=1 b = 2*b print(ans) ```
output
1
65,809
12
131,619
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any i (1 ≀ i ≀ n-1), this condition must be satisfied: $$$(max(a[i], a[i+1]))/(min(a[i], a[i+1])) ≀ 2$$$ For example, the arrays [1, 2, 3, 4, 3], [1, 1, 1] and [5, 10] are dense. And the arrays [5, 11], [1, 4, 2], [6, 6, 1] are not dense. You are given an array a of n integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added. For example, if a=[4,2,10,1], then the answer is 5, and the array itself after inserting elements into it may look like this: a=[4,2,\underline{3},\underline{5},10,\underline{6},\underline{4},\underline{2},1] (there are other ways to build such a). Input The first line contains one integer t (1 ≀ t ≀ 1000). Then t test cases follow. The first line of each test case contains one integer n (2 ≀ n ≀ 50) β€” the length of the array a. The next line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 50). Output For each test case, output one integer β€” the minimum number of numbers that must be added to the array to make it dense. Example Input 6 4 4 2 10 1 2 1 3 2 6 1 3 1 4 2 5 1 2 3 4 3 12 4 31 25 50 30 20 34 46 42 16 15 16 Output 5 1 2 1 0 3 Note The first test case is explained in the statements. In the second test case, you can insert one element, a=[1,\underline{2},3]. In the third test case, you can insert two elements, a=[6,\underline{4},\underline{2},1]. In the fourth test case, you can insert one element, a=[1,\underline{2},4,2]. In the fifth test case, the array a is already dense.
instruction
0
65,810
12
131,620
Tags: greedy, math Correct Solution: ``` num = int(input()) for i in range(num): size = int(input()) arr = list(map(int, input().rstrip().split())) ans = 0 tmp = 0 for i in range(size-1): if arr[i]>arr[i+1]*2: tmp = arr[i+1] while arr[i]>tmp*2: tmp = tmp*2 ans = ans+1 elif arr[i]*2<arr[i+1]: tmp = arr[i] while arr[i+1]>tmp*2: tmp = tmp*2 ans = ans+1 print(ans) ```
output
1
65,810
12
131,621
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any i (1 ≀ i ≀ n-1), this condition must be satisfied: $$$(max(a[i], a[i+1]))/(min(a[i], a[i+1])) ≀ 2$$$ For example, the arrays [1, 2, 3, 4, 3], [1, 1, 1] and [5, 10] are dense. And the arrays [5, 11], [1, 4, 2], [6, 6, 1] are not dense. You are given an array a of n integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added. For example, if a=[4,2,10,1], then the answer is 5, and the array itself after inserting elements into it may look like this: a=[4,2,\underline{3},\underline{5},10,\underline{6},\underline{4},\underline{2},1] (there are other ways to build such a). Input The first line contains one integer t (1 ≀ t ≀ 1000). Then t test cases follow. The first line of each test case contains one integer n (2 ≀ n ≀ 50) β€” the length of the array a. The next line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 50). Output For each test case, output one integer β€” the minimum number of numbers that must be added to the array to make it dense. Example Input 6 4 4 2 10 1 2 1 3 2 6 1 3 1 4 2 5 1 2 3 4 3 12 4 31 25 50 30 20 34 46 42 16 15 16 Output 5 1 2 1 0 3 Note The first test case is explained in the statements. In the second test case, you can insert one element, a=[1,\underline{2},3]. In the third test case, you can insert two elements, a=[6,\underline{4},\underline{2},1]. In the fourth test case, you can insert one element, a=[1,\underline{2},4,2]. In the fifth test case, the array a is already dense. Submitted Solution: ``` a = int(input()) for i in range(a): c = int(input()) b = [int(i) for i in input().split()] g = 0 for j in range(1, len(b)): k = max(b[j], b[j - 1]) / min(b[j], b[j - 1]) while k > 2: k = k / 2 g += 1 print(g) ```
instruction
0
65,811
12
131,622
Yes
output
1
65,811
12
131,623
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any i (1 ≀ i ≀ n-1), this condition must be satisfied: $$$(max(a[i], a[i+1]))/(min(a[i], a[i+1])) ≀ 2$$$ For example, the arrays [1, 2, 3, 4, 3], [1, 1, 1] and [5, 10] are dense. And the arrays [5, 11], [1, 4, 2], [6, 6, 1] are not dense. You are given an array a of n integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added. For example, if a=[4,2,10,1], then the answer is 5, and the array itself after inserting elements into it may look like this: a=[4,2,\underline{3},\underline{5},10,\underline{6},\underline{4},\underline{2},1] (there are other ways to build such a). Input The first line contains one integer t (1 ≀ t ≀ 1000). Then t test cases follow. The first line of each test case contains one integer n (2 ≀ n ≀ 50) β€” the length of the array a. The next line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 50). Output For each test case, output one integer β€” the minimum number of numbers that must be added to the array to make it dense. Example Input 6 4 4 2 10 1 2 1 3 2 6 1 3 1 4 2 5 1 2 3 4 3 12 4 31 25 50 30 20 34 46 42 16 15 16 Output 5 1 2 1 0 3 Note The first test case is explained in the statements. In the second test case, you can insert one element, a=[1,\underline{2},3]. In the third test case, you can insert two elements, a=[6,\underline{4},\underline{2},1]. In the fourth test case, you can insert one element, a=[1,\underline{2},4,2]. In the fifth test case, the array a is already dense. Submitted Solution: ``` it = int(input()) for _ in range(it): n = int(input()) arr = [int(i) for i in input().split()] added = 0 for i in range(n - 1): min_i = min(arr[i], arr[i + 1]) max_i = max(arr[i], arr[i + 1]) while min_i * 2 < max_i: added += 1 min_i *= 2 print(added) ```
instruction
0
65,812
12
131,624
Yes
output
1
65,812
12
131,625
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any i (1 ≀ i ≀ n-1), this condition must be satisfied: $$$(max(a[i], a[i+1]))/(min(a[i], a[i+1])) ≀ 2$$$ For example, the arrays [1, 2, 3, 4, 3], [1, 1, 1] and [5, 10] are dense. And the arrays [5, 11], [1, 4, 2], [6, 6, 1] are not dense. You are given an array a of n integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added. For example, if a=[4,2,10,1], then the answer is 5, and the array itself after inserting elements into it may look like this: a=[4,2,\underline{3},\underline{5},10,\underline{6},\underline{4},\underline{2},1] (there are other ways to build such a). Input The first line contains one integer t (1 ≀ t ≀ 1000). Then t test cases follow. The first line of each test case contains one integer n (2 ≀ n ≀ 50) β€” the length of the array a. The next line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 50). Output For each test case, output one integer β€” the minimum number of numbers that must be added to the array to make it dense. Example Input 6 4 4 2 10 1 2 1 3 2 6 1 3 1 4 2 5 1 2 3 4 3 12 4 31 25 50 30 20 34 46 42 16 15 16 Output 5 1 2 1 0 3 Note The first test case is explained in the statements. In the second test case, you can insert one element, a=[1,\underline{2},3]. In the third test case, you can insert two elements, a=[6,\underline{4},\underline{2},1]. In the fourth test case, you can insert one element, a=[1,\underline{2},4,2]. In the fifth test case, the array a is already dense. Submitted Solution: ``` import sys import math def isPrime(n): for i in range(2,int(n**0.5)+1): if n%i==0: return False return True def pa(a): s='' for i in a: s+=str(i)+' ' print(s) def Y(): print("YES") def N(): print("NO") #.................................temp ends........................................ t=int(input().strip()) for a0 in range(t): n=int(input()) l=[int(i) for i in input().split()] #n=l[0] #k=l[1] #l1=[int(i) for i in input().split()] c=0 flg=0 for i in range(n-1): a=max(l[i],l[i+1]) b=min(l[i],l[i+1]) x=0 d=a/b e=2 while(1): if(b*2<a): x+=1 b*=2 else: break c+=x print(c) #print(c) #print(c) ```
instruction
0
65,813
12
131,626
Yes
output
1
65,813
12
131,627
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any i (1 ≀ i ≀ n-1), this condition must be satisfied: $$$(max(a[i], a[i+1]))/(min(a[i], a[i+1])) ≀ 2$$$ For example, the arrays [1, 2, 3, 4, 3], [1, 1, 1] and [5, 10] are dense. And the arrays [5, 11], [1, 4, 2], [6, 6, 1] are not dense. You are given an array a of n integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added. For example, if a=[4,2,10,1], then the answer is 5, and the array itself after inserting elements into it may look like this: a=[4,2,\underline{3},\underline{5},10,\underline{6},\underline{4},\underline{2},1] (there are other ways to build such a). Input The first line contains one integer t (1 ≀ t ≀ 1000). Then t test cases follow. The first line of each test case contains one integer n (2 ≀ n ≀ 50) β€” the length of the array a. The next line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 50). Output For each test case, output one integer β€” the minimum number of numbers that must be added to the array to make it dense. Example Input 6 4 4 2 10 1 2 1 3 2 6 1 3 1 4 2 5 1 2 3 4 3 12 4 31 25 50 30 20 34 46 42 16 15 16 Output 5 1 2 1 0 3 Note The first test case is explained in the statements. In the second test case, you can insert one element, a=[1,\underline{2},3]. In the third test case, you can insert two elements, a=[6,\underline{4},\underline{2},1]. In the fourth test case, you can insert one element, a=[1,\underline{2},4,2]. In the fifth test case, the array a is already dense. Submitted Solution: ``` try: t=int(input()) for i in range(0,t): n=int(input()) a=list(map(int,input().split(" "))) c=0 for i in range(0,len(a)-1): if max(a[i],a[i+1])>2*min(a[i],a[i+1]): p=max(a[i],a[i+1]) q=min(a[i],a[i+1]) while q*2<p: q*=2 c+=1 print(c) except: pass ```
instruction
0
65,814
12
131,628
Yes
output
1
65,814
12
131,629
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any i (1 ≀ i ≀ n-1), this condition must be satisfied: $$$(max(a[i], a[i+1]))/(min(a[i], a[i+1])) ≀ 2$$$ For example, the arrays [1, 2, 3, 4, 3], [1, 1, 1] and [5, 10] are dense. And the arrays [5, 11], [1, 4, 2], [6, 6, 1] are not dense. You are given an array a of n integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added. For example, if a=[4,2,10,1], then the answer is 5, and the array itself after inserting elements into it may look like this: a=[4,2,\underline{3},\underline{5},10,\underline{6},\underline{4},\underline{2},1] (there are other ways to build such a). Input The first line contains one integer t (1 ≀ t ≀ 1000). Then t test cases follow. The first line of each test case contains one integer n (2 ≀ n ≀ 50) β€” the length of the array a. The next line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 50). Output For each test case, output one integer β€” the minimum number of numbers that must be added to the array to make it dense. Example Input 6 4 4 2 10 1 2 1 3 2 6 1 3 1 4 2 5 1 2 3 4 3 12 4 31 25 50 30 20 34 46 42 16 15 16 Output 5 1 2 1 0 3 Note The first test case is explained in the statements. In the second test case, you can insert one element, a=[1,\underline{2},3]. In the third test case, you can insert two elements, a=[6,\underline{4},\underline{2},1]. In the fourth test case, you can insert one element, a=[1,\underline{2},4,2]. In the fifth test case, the array a is already dense. Submitted Solution: ``` import math t = int(input()) for T in range(t): n = int(input()) a = list(map(int, input().split())) ct = 0 cnt = 0 for i in range(n-1): maxi = max(a[i], a[i+1]) mini = min(a[i], a[i+1]) if maxi / mini > 2 : cnt += 1 k = math.log2(maxi) - math.log2(mini) if k.is_integer(): ct += int(k)-1 else: ct += int(k) #ct += round(abs(math.log2(a[i]) - math.log2(a[i+1]))//1) print(ct) ```
instruction
0
65,815
12
131,630
No
output
1
65,815
12
131,631
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any i (1 ≀ i ≀ n-1), this condition must be satisfied: $$$(max(a[i], a[i+1]))/(min(a[i], a[i+1])) ≀ 2$$$ For example, the arrays [1, 2, 3, 4, 3], [1, 1, 1] and [5, 10] are dense. And the arrays [5, 11], [1, 4, 2], [6, 6, 1] are not dense. You are given an array a of n integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added. For example, if a=[4,2,10,1], then the answer is 5, and the array itself after inserting elements into it may look like this: a=[4,2,\underline{3},\underline{5},10,\underline{6},\underline{4},\underline{2},1] (there are other ways to build such a). Input The first line contains one integer t (1 ≀ t ≀ 1000). Then t test cases follow. The first line of each test case contains one integer n (2 ≀ n ≀ 50) β€” the length of the array a. The next line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 50). Output For each test case, output one integer β€” the minimum number of numbers that must be added to the array to make it dense. Example Input 6 4 4 2 10 1 2 1 3 2 6 1 3 1 4 2 5 1 2 3 4 3 12 4 31 25 50 30 20 34 46 42 16 15 16 Output 5 1 2 1 0 3 Note The first test case is explained in the statements. In the second test case, you can insert one element, a=[1,\underline{2},3]. In the third test case, you can insert two elements, a=[6,\underline{4},\underline{2},1]. In the fourth test case, you can insert one element, a=[1,\underline{2},4,2]. In the fifth test case, the array a is already dense. Submitted Solution: ``` import sys input = sys.stdin.readline import math def solution(n, arr): sol = 0 for i in range(n-1): b = max(arr[i], arr[i+1]) a = min(arr[i], arr[i+1]) sol += math.ceil(math.log((b/(2*a)), 2)) print(sol) T = int(input()) for t in range(T): n = int(input()) arr = list(map(int, input().split())) solution(n, arr) ```
instruction
0
65,816
12
131,632
No
output
1
65,816
12
131,633
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any i (1 ≀ i ≀ n-1), this condition must be satisfied: $$$(max(a[i], a[i+1]))/(min(a[i], a[i+1])) ≀ 2$$$ For example, the arrays [1, 2, 3, 4, 3], [1, 1, 1] and [5, 10] are dense. And the arrays [5, 11], [1, 4, 2], [6, 6, 1] are not dense. You are given an array a of n integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added. For example, if a=[4,2,10,1], then the answer is 5, and the array itself after inserting elements into it may look like this: a=[4,2,\underline{3},\underline{5},10,\underline{6},\underline{4},\underline{2},1] (there are other ways to build such a). Input The first line contains one integer t (1 ≀ t ≀ 1000). Then t test cases follow. The first line of each test case contains one integer n (2 ≀ n ≀ 50) β€” the length of the array a. The next line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 50). Output For each test case, output one integer β€” the minimum number of numbers that must be added to the array to make it dense. Example Input 6 4 4 2 10 1 2 1 3 2 6 1 3 1 4 2 5 1 2 3 4 3 12 4 31 25 50 30 20 34 46 42 16 15 16 Output 5 1 2 1 0 3 Note The first test case is explained in the statements. In the second test case, you can insert one element, a=[1,\underline{2},3]. In the third test case, you can insert two elements, a=[6,\underline{4},\underline{2},1]. In the fourth test case, you can insert one element, a=[1,\underline{2},4,2]. In the fifth test case, the array a is already dense. Submitted Solution: ``` from math import * for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) c = 0 for i in range(n-1): m = max(l[i], l[i+1]) n = min(l[i], l[i+1]) c += ceil(log(m/n,2)) - 1 print(c) ```
instruction
0
65,817
12
131,634
No
output
1
65,817
12
131,635
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any i (1 ≀ i ≀ n-1), this condition must be satisfied: $$$(max(a[i], a[i+1]))/(min(a[i], a[i+1])) ≀ 2$$$ For example, the arrays [1, 2, 3, 4, 3], [1, 1, 1] and [5, 10] are dense. And the arrays [5, 11], [1, 4, 2], [6, 6, 1] are not dense. You are given an array a of n integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added. For example, if a=[4,2,10,1], then the answer is 5, and the array itself after inserting elements into it may look like this: a=[4,2,\underline{3},\underline{5},10,\underline{6},\underline{4},\underline{2},1] (there are other ways to build such a). Input The first line contains one integer t (1 ≀ t ≀ 1000). Then t test cases follow. The first line of each test case contains one integer n (2 ≀ n ≀ 50) β€” the length of the array a. The next line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 50). Output For each test case, output one integer β€” the minimum number of numbers that must be added to the array to make it dense. Example Input 6 4 4 2 10 1 2 1 3 2 6 1 3 1 4 2 5 1 2 3 4 3 12 4 31 25 50 30 20 34 46 42 16 15 16 Output 5 1 2 1 0 3 Note The first test case is explained in the statements. In the second test case, you can insert one element, a=[1,\underline{2},3]. In the third test case, you can insert two elements, a=[6,\underline{4},\underline{2},1]. In the fourth test case, you can insert one element, a=[1,\underline{2},4,2]. In the fifth test case, the array a is already dense. Submitted Solution: ``` t=int(input()) for i in range(t): n=int(input()) l=list(map(int,input().split())) c=0 for i in range(n-1): mi=min(l[i],l[i+1]) ma=max(l[i],l[i+1]) if not(ma <= 2*mi): while 2*mi <= ma: mi=mi*2 c=c+1 print("**",c) ```
instruction
0
65,818
12
131,636
No
output
1
65,818
12
131,637
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a number n and an array b_1, b_2, …, b_{n+2}, obtained according to the following algorithm: * some array a_1, a_2, …, a_n was guessed; * array a was written to array b, i.e. b_i = a_i (1 ≀ i ≀ n); * The (n+1)-th element of the array b is the sum of the numbers in the array a, i.e. b_{n+1} = a_1+a_2+…+a_n; * The (n+2)-th element of the array b was written some number x (1 ≀ x ≀ 10^9), i.e. b_{n+2} = x; The * array b was shuffled. For example, the array b=[2, 3, 7, 12 ,2] it could be obtained in the following ways: * a=[2, 2, 3] and x=12; * a=[3, 2, 7] and x=2. For the given array b, find any array a that could have been guessed initially. Input The first line contains a single integer t (1 ≀ t ≀ 10^4). Then t test cases follow. The first line of each test case contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5). The second row of each test case contains n+2 integers b_1, b_2, …, b_{n+2} (1 ≀ b_i ≀ 10^9). It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output: * "-1", if the array b could not be obtained from any array a; * n integers a_1, a_2, …, a_n, otherwise. If there are several arrays of a, you can output any. Example Input 4 3 2 3 7 12 2 4 9 1 7 1 6 5 5 18 2 2 3 2 9 2 3 2 6 9 2 1 Output 2 3 7 -1 2 2 2 3 9 1 2 6
instruction
0
65,819
12
131,638
Tags: constructive algorithms, data structures, greedy Correct Solution: ``` class Solution: def solve(self, n, b): m = n + 2 b.sort() summ = sum(b[:m-1]) largest = b[m-1] largest2 = b[m-2] for i in range(m-1): if summ - b[i] == largest: return self.return_arr(i, b, 1, m) if summ - largest2 == largest2: return " ".join([str(b[i]) for i in range(m-2)]) return -1 def return_arr(self, i, b, k, m): return " ".join([str(b[j]) for j in range(m-k) if j != i]) if __name__ == '__main__': sol = Solution() tests = int(input()) results = [] for _ in range(tests): n = int(input()) b = list(map(int, input().split())) result = sol.solve(n, b) results.append(result) for result in results: print(result) ```
output
1
65,819
12
131,639
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a number n and an array b_1, b_2, …, b_{n+2}, obtained according to the following algorithm: * some array a_1, a_2, …, a_n was guessed; * array a was written to array b, i.e. b_i = a_i (1 ≀ i ≀ n); * The (n+1)-th element of the array b is the sum of the numbers in the array a, i.e. b_{n+1} = a_1+a_2+…+a_n; * The (n+2)-th element of the array b was written some number x (1 ≀ x ≀ 10^9), i.e. b_{n+2} = x; The * array b was shuffled. For example, the array b=[2, 3, 7, 12 ,2] it could be obtained in the following ways: * a=[2, 2, 3] and x=12; * a=[3, 2, 7] and x=2. For the given array b, find any array a that could have been guessed initially. Input The first line contains a single integer t (1 ≀ t ≀ 10^4). Then t test cases follow. The first line of each test case contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5). The second row of each test case contains n+2 integers b_1, b_2, …, b_{n+2} (1 ≀ b_i ≀ 10^9). It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output: * "-1", if the array b could not be obtained from any array a; * n integers a_1, a_2, …, a_n, otherwise. If there are several arrays of a, you can output any. Example Input 4 3 2 3 7 12 2 4 9 1 7 1 6 5 5 18 2 2 3 2 9 2 3 2 6 9 2 1 Output 2 3 7 -1 2 2 2 3 9 1 2 6
instruction
0
65,820
12
131,640
Tags: constructive algorithms, data structures, greedy Correct Solution: ``` for _ in range(int(input())): n = int(input()) lis = list(map(int,input().split())) m = max(lis) m_i = lis.index(m) del lis[m_i] s = sum(lis) if s%2==0 and s==max(lis)*2: s_m = max(lis) s_m_i = lis.index(s_m) del lis[s_m_i] lis.sort() print(*lis) else: f=0 for i in range(len(lis)): if s-lis[i]==m: del lis[i] lis.sort() print(*lis) f=1 break if f==0: print(-1) ```
output
1
65,820
12
131,641
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a number n and an array b_1, b_2, …, b_{n+2}, obtained according to the following algorithm: * some array a_1, a_2, …, a_n was guessed; * array a was written to array b, i.e. b_i = a_i (1 ≀ i ≀ n); * The (n+1)-th element of the array b is the sum of the numbers in the array a, i.e. b_{n+1} = a_1+a_2+…+a_n; * The (n+2)-th element of the array b was written some number x (1 ≀ x ≀ 10^9), i.e. b_{n+2} = x; The * array b was shuffled. For example, the array b=[2, 3, 7, 12 ,2] it could be obtained in the following ways: * a=[2, 2, 3] and x=12; * a=[3, 2, 7] and x=2. For the given array b, find any array a that could have been guessed initially. Input The first line contains a single integer t (1 ≀ t ≀ 10^4). Then t test cases follow. The first line of each test case contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5). The second row of each test case contains n+2 integers b_1, b_2, …, b_{n+2} (1 ≀ b_i ≀ 10^9). It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output: * "-1", if the array b could not be obtained from any array a; * n integers a_1, a_2, …, a_n, otherwise. If there are several arrays of a, you can output any. Example Input 4 3 2 3 7 12 2 4 9 1 7 1 6 5 5 18 2 2 3 2 9 2 3 2 6 9 2 1 Output 2 3 7 -1 2 2 2 3 9 1 2 6
instruction
0
65,821
12
131,642
Tags: constructive algorithms, data structures, greedy Correct Solution: ``` for _ in range(int(input())): n = int(input()) l = list(map(int,input().split())) l.sort() t = sum(l) if (t-l[-1]-l[-2])==l[-2]: print(*l[:-2]) else: t-=l[-1] f = 0 for i in range(n+1): if (t-l[i])==l[-1]: f = 1 break if f==1: for j in range(n+1): if j!=i: print(l[j],end=" ") print() else: print(-1) ```
output
1
65,821
12
131,643
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a number n and an array b_1, b_2, …, b_{n+2}, obtained according to the following algorithm: * some array a_1, a_2, …, a_n was guessed; * array a was written to array b, i.e. b_i = a_i (1 ≀ i ≀ n); * The (n+1)-th element of the array b is the sum of the numbers in the array a, i.e. b_{n+1} = a_1+a_2+…+a_n; * The (n+2)-th element of the array b was written some number x (1 ≀ x ≀ 10^9), i.e. b_{n+2} = x; The * array b was shuffled. For example, the array b=[2, 3, 7, 12 ,2] it could be obtained in the following ways: * a=[2, 2, 3] and x=12; * a=[3, 2, 7] and x=2. For the given array b, find any array a that could have been guessed initially. Input The first line contains a single integer t (1 ≀ t ≀ 10^4). Then t test cases follow. The first line of each test case contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5). The second row of each test case contains n+2 integers b_1, b_2, …, b_{n+2} (1 ≀ b_i ≀ 10^9). It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output: * "-1", if the array b could not be obtained from any array a; * n integers a_1, a_2, …, a_n, otherwise. If there are several arrays of a, you can output any. Example Input 4 3 2 3 7 12 2 4 9 1 7 1 6 5 5 18 2 2 3 2 9 2 3 2 6 9 2 1 Output 2 3 7 -1 2 2 2 3 9 1 2 6
instruction
0
65,822
12
131,644
Tags: constructive algorithms, data structures, greedy Correct Solution: ``` for t in range(int(input())): n=int(input()) b=[int(x) for x in input().split()] b.sort() flag=False assumed_sum=b[-2] total=sum(b)-assumed_sum-b[-1] if(total==assumed_sum): b.pop() b.pop() print(*b) continue assumed_sum=b[-1] total=sum(b)-assumed_sum b.pop(n+1) for i in range(n+1): if(total-b[i]==assumed_sum): b.pop(i) flag=True break if(flag): print(*b) continue else: print(-1) ```
output
1
65,822
12
131,645
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a number n and an array b_1, b_2, …, b_{n+2}, obtained according to the following algorithm: * some array a_1, a_2, …, a_n was guessed; * array a was written to array b, i.e. b_i = a_i (1 ≀ i ≀ n); * The (n+1)-th element of the array b is the sum of the numbers in the array a, i.e. b_{n+1} = a_1+a_2+…+a_n; * The (n+2)-th element of the array b was written some number x (1 ≀ x ≀ 10^9), i.e. b_{n+2} = x; The * array b was shuffled. For example, the array b=[2, 3, 7, 12 ,2] it could be obtained in the following ways: * a=[2, 2, 3] and x=12; * a=[3, 2, 7] and x=2. For the given array b, find any array a that could have been guessed initially. Input The first line contains a single integer t (1 ≀ t ≀ 10^4). Then t test cases follow. The first line of each test case contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5). The second row of each test case contains n+2 integers b_1, b_2, …, b_{n+2} (1 ≀ b_i ≀ 10^9). It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output: * "-1", if the array b could not be obtained from any array a; * n integers a_1, a_2, …, a_n, otherwise. If there are several arrays of a, you can output any. Example Input 4 3 2 3 7 12 2 4 9 1 7 1 6 5 5 18 2 2 3 2 9 2 3 2 6 9 2 1 Output 2 3 7 -1 2 2 2 3 9 1 2 6
instruction
0
65,823
12
131,646
Tags: constructive algorithms, data structures, greedy Correct Solution: ``` def main(): from sys import stdin, stdout for _ in range(int(stdin.readline())): n = int(stdin.readline()) arr = list(map(int,stdin.readline().split())) arrsum = sum(arr) mydict = {} for i in range(len(arr)): if arr[i] in mydict: mydict[arr[i]].append(i) else: mydict[arr[i]] = [i] #print(mydict) flag = False tempx, tempsum = None, None for i in range(len(arr)): left = arrsum - arr[i] #print(left,i) if left%2 == 0 and len(mydict.get(left//2,[])) and ((arr[i]!=left//2)or (arr[i]==left//2 and len(mydict[arr[i]])>1)): tempx = i tempsum = left//2 flag = True if flag: mydict[arr[tempx]] = mydict[arr[tempx]][1:] mydict[tempsum] = mydict[tempsum][1:] for i in mydict: for _ in range( len(mydict[i]) ): stdout.write(str(i)+' ') stdout.write('\n') else: stdout.write('-1\n') if __name__=='__main__': main() ```
output
1
65,823
12
131,647