message
stringlengths
2
433k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
113
108k
cluster
float64
12
12
__index_level_0__
int64
226
217k
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves.
instruction
0
63,662
12
127,324
Tags: brute force, dp, greedy, math, number theory Correct Solution: ``` import sys import collections from collections import Counter, deque import itertools import math import timeit import random ######################### # imgur.com/Pkt7iIf.png # ######################### def sieve(n): if n < 2: return list() prime = [True for _ in range(n + 1)] p = 3 while p * p <= n: if prime[p]: for i in range(p * 2, n + 1, p): prime[i] = False p += 2 r = [2] for p in range(3, n + 1, 2): if prime[p]: r.append(p) return r def divs(n, start=1): divisors = [] for i in range(start, int(math.sqrt(n) + 1)): if n % i == 0: if n / i == i: divisors.append(i) else: divisors.extend([i, n // i]) return divisors def divn(n, primes): divs_number = 1 for i in primes: if n == 1: return divs_number t = 1 while n % i == 0: t += 1 n //= i divs_number *= t def flin(d, x, default=-1): left = right = -1 for i in range(len(d)): if d[i] == x: if left == -1: left = i right = i if left == -1: return (default, default) else: return (left, right) def ceil(n, k): return n // k + (n % k != 0) def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(map(int, input().split())) def lcm(a, b): return abs(a * b) // math.gcd(a, b) def prr(a, sep=' '): print(sep.join(map(str, a))) def dd(): return collections.defaultdict(int) def ddl(): return collections.defaultdict(list) #input = sys.stdin.readline n = ii() d = li() _d = [x for x in d if x != 1] if len(d) != len(_d): exit(print(len(_d))) gcds = [] for i in range(n - 1): t = math.gcd(d[i], d[i + 1]) if t == 1: exit(print(n)) gcds.append(t) r = 10**6 for i in range(n - 1): for j in range(i + 2, n): if math.gcd(gcds[i], d[j]) == 1: r = min(r, j - i - 1 + n) print(r) if r != 10**6 else print(-1) ```
output
1
63,662
12
127,325
Provide tags and a correct Python 3 solution for this coding contest problem. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves.
instruction
0
63,663
12
127,326
Tags: brute force, dp, greedy, math, number theory Correct Solution: ``` import sys from math import gcd # def gcd (a,b): # if b== 0: # return a # return gcd(b,a%b) n = int(input()) a = list(map(int,input().split())) if 1 in a: print(n- a.count(1)) exit() count = sys.maxsize for i in range(n): tmp = a[i] for j in range(i+1,n): tmp =gcd(tmp,a[j]) if tmp == 1: count = min(count,n + j-i -1) if count != sys.maxsize: print(count) else: print(-1) ```
output
1
63,663
12
127,327
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves. Submitted Solution: ``` from math import gcd n = int(input()) a = list(map(int, input().split())) d = n i = -1 v = a[0] for j in range(n): # forward v = gcd(v, a[j]) if v == 1: v = a[j] # backward for k in range(j, i, -1): v1 = gcd(v, a[k]) if v1 == 1: d = min(d, j - k) i = k break v = v1 if d == 0: ans = n - a.count(1) elif d < n: ans = d - 1 + n - a.count(1) else: ans = -1 print(ans) ```
instruction
0
63,664
12
127,328
Yes
output
1
63,664
12
127,329
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves. Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) def gcd(a, b): if a < b: a, b = b, a if a % b == 0: return b if a == 1 or b == 1: return 1 return gcd(b, a % b) cnt=0 for i in range(n): if a[i]==1: cnt+=1 if cnt>0: print(n-cnt) else: minimum=1e10 for i in range(0,n): x=a[i] for j in range(i+1,n): x=gcd(x,a[j]) if x==1: minimum=min(minimum,j-i+1) if minimum==1e10: print(-1) else: print(n-1+(minimum-1)) ```
instruction
0
63,665
12
127,330
Yes
output
1
63,665
12
127,331
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves. Submitted Solution: ``` from fractions import gcd from functools import reduce n = int(input()) a = list(map(int, input().split())) if 1 < reduce(lambda x, y: gcd(x, y), a): print(-1) elif any(map(lambda x: x == 1, a)): print(len(list(filter(lambda x: 1 < x, a)))) else: l = 0 r = n while 1 < r - l: m = (l + r) // 2 if any(map(lambda x: x == 1, map(lambda i: reduce(lambda x, y: gcd(x, y), a[i: i + m]), range(n - m + 1)))): r = m else: l = m print(r + n - 2) ```
instruction
0
63,666
12
127,332
Yes
output
1
63,666
12
127,333
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves. Submitted Solution: ``` import math n=int(input()) a=list(map(int,input().split())) x=0 for i in range(n): x=math.gcd(x,a[i]) if(x>1): print("-1") else: cnt=0 if 1 in a: cnt=a.count(1) print(n-cnt) else: l=float('Inf') for i in range(n): x=a[i] for j in range(i+1,n): x=math.gcd(x,a[j]) if(x==1): l=min(l,j-i) break print(l+n-1) ```
instruction
0
63,667
12
127,334
Yes
output
1
63,667
12
127,335
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves. Submitted Solution: ``` from math import gcd n=int(input()) a=list(map(int,input().split())) num=0 while (True): change=False one=False for i in range(1,n): if a[i]==a[i-1]: continue d=gcd(a[i],a[i-1]) change=True num+=1 if d==a[i-1]: a[i]=d else: a[i-1]=d if d==1: one=True break if not change: print(-1) exit() if one: for i in range(n): if a[i]!=1: num+=1 break print(num) ```
instruction
0
63,668
12
127,336
No
output
1
63,668
12
127,337
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves. Submitted Solution: ``` #------------------------------warmup---------------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #-------------------game starts now----------------------------------------------------- import sys,math n=int(input()) a=list(map(int,input().split())) t=0 for i in range(n-1): s=math.gcd(a[i],a[i+1]) if s==1: print(t+n) sys.exit() if s==a[i]: if s!=a[i+1]: a[i+1]=s t+=1 else: a[i]=s t+=1 print(-1) ```
instruction
0
63,669
12
127,338
No
output
1
63,669
12
127,339
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves. Submitted Solution: ``` from math import gcd n = int(input()) arr = list(map(int, input().split())) ans = float("inf") for i in range(n): for j in range(i+1, n): if gcd(arr[i], arr[j]) == 1: ans = min(ans, j-i) if ans == float("inf"): print(-1) else: print(ans+n-1) ```
instruction
0
63,670
12
127,340
No
output
1
63,670
12
127,341
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). What is the minimum number of operations you need to make all of the elements equal to 1? Input The first line of the input contains one integer n (1 ≀ n ≀ 2000) β€” the number of elements in the array. The second line contains n space separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: * [2, 2, 3, 4, 6]. * [2, 1, 3, 4, 6] * [2, 1, 3, 1, 6] * [2, 1, 1, 1, 6] * [1, 1, 1, 1, 6] * [1, 1, 1, 1, 1] We can prove that in this case it is not possible to make all numbers one using less than 5 moves. Submitted Solution: ``` import math n = int(input()) a=list(map(int,input().split())) k=a[0] l=0 for i in range(1,n): k=math.gcd(a[i],k) if a[i]==1: l+=1 if k>1: print(-1) elif l>=1: print(n-l) else: h=n for i in range(n): g=a[i] for j in range(i+1,n): g=math.gcd(a[j],g) #print(g,j,i) if g==1: h=min(j-i-1,h) break print(n+h) ```
instruction
0
63,671
12
127,342
No
output
1
63,671
12
127,343
Provide tags and a correct Python 3 solution for this coding contest problem. JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array a_1, a_2, ..., a_n of integers, such that 1 ≀ a_1 < a_2 < … < a_n ≀ 10^3, and then went to the bathroom. JATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range [1, 10^3]. JATC wonders what is the greatest number of elements he can erase? Input The first line of the input contains a single integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line of the input contains n integers a_i (1 ≀ a_1<a_2<...<a_n ≀ 10^3) β€” the array written by Giraffe. Output Print a single integer β€” the maximum number of consecutive elements in the array that JATC can erase. If it is impossible to erase even a single element, print 0. Examples Input 6 1 3 4 5 6 9 Output 2 Input 3 998 999 1000 Output 2 Input 5 1 2 3 4 5 Output 4 Note In the first example, JATC can erase the third and fourth elements, leaving the array [1, 3, \\_, \\_, 6, 9]. As you can see, there is only one way to fill in the blanks. In the second example, JATC can erase the second and the third elements. The array will become [998, \\_, \\_]. Because all the elements are less than or equal to 1000, the array is still can be restored. Note, that he can't erase the first 2 elements. In the third example, JATC can erase the first 4 elements. Since all the elements are greater than or equal to 1, Giraffe can still restore the array. Note, that he can't erase the last 4 elements.
instruction
0
63,990
12
127,980
Tags: greedy, implementation Correct Solution: ``` n, a = int(input()), [0] + list(map(int, input().split())) + [1001] a = [a[i] - a[i - 1] for i in range(1, len(a))] + [0] m = cnt = 0 for i in range(len(a)): if a[i] == 1: cnt += 1 else: if cnt > 0: m = max(m, cnt - 1) cnt = 0 print(m) ```
output
1
63,990
12
127,981
Provide tags and a correct Python 3 solution for this coding contest problem. JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array a_1, a_2, ..., a_n of integers, such that 1 ≀ a_1 < a_2 < … < a_n ≀ 10^3, and then went to the bathroom. JATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range [1, 10^3]. JATC wonders what is the greatest number of elements he can erase? Input The first line of the input contains a single integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line of the input contains n integers a_i (1 ≀ a_1<a_2<...<a_n ≀ 10^3) β€” the array written by Giraffe. Output Print a single integer β€” the maximum number of consecutive elements in the array that JATC can erase. If it is impossible to erase even a single element, print 0. Examples Input 6 1 3 4 5 6 9 Output 2 Input 3 998 999 1000 Output 2 Input 5 1 2 3 4 5 Output 4 Note In the first example, JATC can erase the third and fourth elements, leaving the array [1, 3, \\_, \\_, 6, 9]. As you can see, there is only one way to fill in the blanks. In the second example, JATC can erase the second and the third elements. The array will become [998, \\_, \\_]. Because all the elements are less than or equal to 1000, the array is still can be restored. Note, that he can't erase the first 2 elements. In the third example, JATC can erase the first 4 elements. Since all the elements are greater than or equal to 1, Giraffe can still restore the array. Note, that he can't erase the last 4 elements.
instruction
0
63,991
12
127,982
Tags: greedy, implementation Correct Solution: ``` from sys import stdin from collections import deque mod = 10**9 + 7 import sys sys.setrecursionlimit(10**5) from queue import PriorityQueue # def rl(): # return [int(w) for w in stdin.readline().split()] from bisect import bisect_right from bisect import bisect_left from collections import defaultdict from math import sqrt,factorial,gcd,log2,inf,ceil # map(int,input().split()) # # l = list(map(int,input().split())) # from itertools import permutations import heapq # input = lambda: sys.stdin.readline().rstrip() input = lambda : sys.stdin.readline().rstrip() from sys import stdin, stdout from heapq import heapify, heappush, heappop n = int(input()) l = list(map(int,input().split())) maxi = 0 if n == 1000: print(1000) exit() for i in range(n): for j in range(i,n): if i-1>=0 and j+1<n: if l[j+1] - l[i-1] - 1 == j-i+1: maxi = max(j-i+1,maxi) if i-1 < 0 and j+1<n: if l[j+1]-1 == j-i+1: maxi = max(j-i+1,maxi) if i-1>=0 and j+1>=n: z = j-1 if 1000 - l[i-1] - 1 == z-i+1: maxi = max(j-i+1,maxi) print(maxi) ```
output
1
63,991
12
127,983
Provide tags and a correct Python 3 solution for this coding contest problem. JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array a_1, a_2, ..., a_n of integers, such that 1 ≀ a_1 < a_2 < … < a_n ≀ 10^3, and then went to the bathroom. JATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range [1, 10^3]. JATC wonders what is the greatest number of elements he can erase? Input The first line of the input contains a single integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line of the input contains n integers a_i (1 ≀ a_1<a_2<...<a_n ≀ 10^3) β€” the array written by Giraffe. Output Print a single integer β€” the maximum number of consecutive elements in the array that JATC can erase. If it is impossible to erase even a single element, print 0. Examples Input 6 1 3 4 5 6 9 Output 2 Input 3 998 999 1000 Output 2 Input 5 1 2 3 4 5 Output 4 Note In the first example, JATC can erase the third and fourth elements, leaving the array [1, 3, \\_, \\_, 6, 9]. As you can see, there is only one way to fill in the blanks. In the second example, JATC can erase the second and the third elements. The array will become [998, \\_, \\_]. Because all the elements are less than or equal to 1000, the array is still can be restored. Note, that he can't erase the first 2 elements. In the third example, JATC can erase the first 4 elements. Since all the elements are greater than or equal to 1, Giraffe can still restore the array. Note, that he can't erase the last 4 elements.
instruction
0
63,992
12
127,984
Tags: greedy, implementation Correct Solution: ``` n = int(input()) arr = [0] + list(map(int, input().split())) arr.append(1001) max_ = 0 kek = 0 for i in range(1, len(arr)): if arr[i] - 1 == arr[i - 1]: kek += 1 else: max_ = max(max_, kek - 1) kek = 0 max_ = max(max_, kek - 1) print(max_) ```
output
1
63,992
12
127,985
Provide tags and a correct Python 3 solution for this coding contest problem. JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array a_1, a_2, ..., a_n of integers, such that 1 ≀ a_1 < a_2 < … < a_n ≀ 10^3, and then went to the bathroom. JATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range [1, 10^3]. JATC wonders what is the greatest number of elements he can erase? Input The first line of the input contains a single integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line of the input contains n integers a_i (1 ≀ a_1<a_2<...<a_n ≀ 10^3) β€” the array written by Giraffe. Output Print a single integer β€” the maximum number of consecutive elements in the array that JATC can erase. If it is impossible to erase even a single element, print 0. Examples Input 6 1 3 4 5 6 9 Output 2 Input 3 998 999 1000 Output 2 Input 5 1 2 3 4 5 Output 4 Note In the first example, JATC can erase the third and fourth elements, leaving the array [1, 3, \\_, \\_, 6, 9]. As you can see, there is only one way to fill in the blanks. In the second example, JATC can erase the second and the third elements. The array will become [998, \\_, \\_]. Because all the elements are less than or equal to 1000, the array is still can be restored. Note, that he can't erase the first 2 elements. In the third example, JATC can erase the first 4 elements. Since all the elements are greater than or equal to 1, Giraffe can still restore the array. Note, that he can't erase the last 4 elements.
instruction
0
63,993
12
127,986
Tags: greedy, implementation Correct Solution: ``` n = int(input()) a = list(map(int, input().split())) if n == 1: print(0) else: sa = [] s = 0 e = 0 for i in range(n-1): if a[i] == a[i+1]-1: e = i+1 else: sa.append(a[s:e+1]) s = i+1 e = i+1 else: sa.append(a[s:e+1]) j = 0 m = 0 for i in range(len(sa)): if len(sa[i]) > m: m = len(sa[i]) j = i elif len(sa[i]) == m: if sa[i][m-1] == 1000: j = i if sa[j][0] == 1 or sa[j][m-1] == 1000: print(m-1) elif m < 3: print(0) else: print(m-2) ```
output
1
63,993
12
127,987
Provide tags and a correct Python 3 solution for this coding contest problem. JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array a_1, a_2, ..., a_n of integers, such that 1 ≀ a_1 < a_2 < … < a_n ≀ 10^3, and then went to the bathroom. JATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range [1, 10^3]. JATC wonders what is the greatest number of elements he can erase? Input The first line of the input contains a single integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line of the input contains n integers a_i (1 ≀ a_1<a_2<...<a_n ≀ 10^3) β€” the array written by Giraffe. Output Print a single integer β€” the maximum number of consecutive elements in the array that JATC can erase. If it is impossible to erase even a single element, print 0. Examples Input 6 1 3 4 5 6 9 Output 2 Input 3 998 999 1000 Output 2 Input 5 1 2 3 4 5 Output 4 Note In the first example, JATC can erase the third and fourth elements, leaving the array [1, 3, \\_, \\_, 6, 9]. As you can see, there is only one way to fill in the blanks. In the second example, JATC can erase the second and the third elements. The array will become [998, \\_, \\_]. Because all the elements are less than or equal to 1000, the array is still can be restored. Note, that he can't erase the first 2 elements. In the third example, JATC can erase the first 4 elements. Since all the elements are greater than or equal to 1, Giraffe can still restore the array. Note, that he can't erase the last 4 elements.
instruction
0
63,994
12
127,988
Tags: greedy, implementation Correct Solution: ``` d = int(input()) g = [int(i) for i in input().split()] count = 0 op = [] i = 0 j = 0 if g[0]==1: for i in range(len(g)-1): if g[i]+1==g[i+1]: count+=1 else: op.append(count) break op.append(count) count = 0 for j in range(i,len(g)-1): if g[j]+1==g[j+1]: count+=1 else: if g[j]-1000==-1: op.append(count) else: op.append(count - 1) count = 0 if count!=0: if 1000-g[j]==1: op.append(count) else: op.append(count - 1) else: op.append(count) print(max(op)) ```
output
1
63,994
12
127,989
Provide tags and a correct Python 3 solution for this coding contest problem. JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array a_1, a_2, ..., a_n of integers, such that 1 ≀ a_1 < a_2 < … < a_n ≀ 10^3, and then went to the bathroom. JATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range [1, 10^3]. JATC wonders what is the greatest number of elements he can erase? Input The first line of the input contains a single integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line of the input contains n integers a_i (1 ≀ a_1<a_2<...<a_n ≀ 10^3) β€” the array written by Giraffe. Output Print a single integer β€” the maximum number of consecutive elements in the array that JATC can erase. If it is impossible to erase even a single element, print 0. Examples Input 6 1 3 4 5 6 9 Output 2 Input 3 998 999 1000 Output 2 Input 5 1 2 3 4 5 Output 4 Note In the first example, JATC can erase the third and fourth elements, leaving the array [1, 3, \\_, \\_, 6, 9]. As you can see, there is only one way to fill in the blanks. In the second example, JATC can erase the second and the third elements. The array will become [998, \\_, \\_]. Because all the elements are less than or equal to 1000, the array is still can be restored. Note, that he can't erase the first 2 elements. In the third example, JATC can erase the first 4 elements. Since all the elements are greater than or equal to 1, Giraffe can still restore the array. Note, that he can't erase the last 4 elements.
instruction
0
63,995
12
127,990
Tags: greedy, implementation Correct Solution: ``` n = int(input()) arr = list( map(int,input().split()) ) eraseable = 0 if n==1: print(0) exit(0) consecutive = False if arr[0]==1 and arr[1]==2: consecutive = 1 for i in range(1,n-1): if arr[i] - arr[i-1] == 1 and arr[i+1]-arr[i] == 1: consecutive+=1 else: eraseable = max(consecutive,eraseable) consecutive=0 if arr[-1]==1000 and arr[-2] == 999: consecutive+=1 eraseable = max(consecutive,eraseable) elif consecutive>0: eraseable = max(consecutive,eraseable) print(eraseable) ```
output
1
63,995
12
127,991
Provide tags and a correct Python 3 solution for this coding contest problem. JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array a_1, a_2, ..., a_n of integers, such that 1 ≀ a_1 < a_2 < … < a_n ≀ 10^3, and then went to the bathroom. JATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range [1, 10^3]. JATC wonders what is the greatest number of elements he can erase? Input The first line of the input contains a single integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line of the input contains n integers a_i (1 ≀ a_1<a_2<...<a_n ≀ 10^3) β€” the array written by Giraffe. Output Print a single integer β€” the maximum number of consecutive elements in the array that JATC can erase. If it is impossible to erase even a single element, print 0. Examples Input 6 1 3 4 5 6 9 Output 2 Input 3 998 999 1000 Output 2 Input 5 1 2 3 4 5 Output 4 Note In the first example, JATC can erase the third and fourth elements, leaving the array [1, 3, \\_, \\_, 6, 9]. As you can see, there is only one way to fill in the blanks. In the second example, JATC can erase the second and the third elements. The array will become [998, \\_, \\_]. Because all the elements are less than or equal to 1000, the array is still can be restored. Note, that he can't erase the first 2 elements. In the third example, JATC can erase the first 4 elements. Since all the elements are greater than or equal to 1, Giraffe can still restore the array. Note, that he can't erase the last 4 elements.
instruction
0
63,996
12
127,992
Tags: greedy, implementation Correct Solution: ``` n=int(input())+2 l=[0]+list(map(int,input().split()))+[1001] M=0 i=0 while i<n-1: s=1 j=i+1 while j<n: if l[j]==l[j-1]+1: s+=1 j+=1 else :break if s>M:M=s i=j print(M-2) if M-2>0 else print(0) ```
output
1
63,996
12
127,993
Provide tags and a correct Python 3 solution for this coding contest problem. JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array a_1, a_2, ..., a_n of integers, such that 1 ≀ a_1 < a_2 < … < a_n ≀ 10^3, and then went to the bathroom. JATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range [1, 10^3]. JATC wonders what is the greatest number of elements he can erase? Input The first line of the input contains a single integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line of the input contains n integers a_i (1 ≀ a_1<a_2<...<a_n ≀ 10^3) β€” the array written by Giraffe. Output Print a single integer β€” the maximum number of consecutive elements in the array that JATC can erase. If it is impossible to erase even a single element, print 0. Examples Input 6 1 3 4 5 6 9 Output 2 Input 3 998 999 1000 Output 2 Input 5 1 2 3 4 5 Output 4 Note In the first example, JATC can erase the third and fourth elements, leaving the array [1, 3, \\_, \\_, 6, 9]. As you can see, there is only one way to fill in the blanks. In the second example, JATC can erase the second and the third elements. The array will become [998, \\_, \\_]. Because all the elements are less than or equal to 1000, the array is still can be restored. Note, that he can't erase the first 2 elements. In the third example, JATC can erase the first 4 elements. Since all the elements are greater than or equal to 1, Giraffe can still restore the array. Note, that he can't erase the last 4 elements.
instruction
0
63,997
12
127,994
Tags: greedy, implementation Correct Solution: ``` n = int(input()) a=list(map(int,input().split())) m=0 count=1 a=[0]+a+[1001] for i in range(1,n+2): if a[i]-a[i-1]==1: count+=1 else: m=max(m,count-2) count=1 m=max(m,count-2) print(m) ```
output
1
63,997
12
127,995
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array a_1, a_2, ..., a_n of integers, such that 1 ≀ a_1 < a_2 < … < a_n ≀ 10^3, and then went to the bathroom. JATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range [1, 10^3]. JATC wonders what is the greatest number of elements he can erase? Input The first line of the input contains a single integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line of the input contains n integers a_i (1 ≀ a_1<a_2<...<a_n ≀ 10^3) β€” the array written by Giraffe. Output Print a single integer β€” the maximum number of consecutive elements in the array that JATC can erase. If it is impossible to erase even a single element, print 0. Examples Input 6 1 3 4 5 6 9 Output 2 Input 3 998 999 1000 Output 2 Input 5 1 2 3 4 5 Output 4 Note In the first example, JATC can erase the third and fourth elements, leaving the array [1, 3, \\_, \\_, 6, 9]. As you can see, there is only one way to fill in the blanks. In the second example, JATC can erase the second and the third elements. The array will become [998, \\_, \\_]. Because all the elements are less than or equal to 1000, the array is still can be restored. Note, that he can't erase the first 2 elements. In the third example, JATC can erase the first 4 elements. Since all the elements are greater than or equal to 1, Giraffe can still restore the array. Note, that he can't erase the last 4 elements. Submitted Solution: ``` gcd = lambda a, b: gcd(b, a % b) if b else a def main(): n = int(input()) arr = [0] + list(map(int, input().split())) + [1001] mx = 0 for i in range(0, len(arr)): for j in range(i, len(arr)): if arr[i] - i == arr[j] - j: mx = max(mx, j - i - 1) print(mx) main() ```
instruction
0
63,998
12
127,996
Yes
output
1
63,998
12
127,997
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array a_1, a_2, ..., a_n of integers, such that 1 ≀ a_1 < a_2 < … < a_n ≀ 10^3, and then went to the bathroom. JATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range [1, 10^3]. JATC wonders what is the greatest number of elements he can erase? Input The first line of the input contains a single integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line of the input contains n integers a_i (1 ≀ a_1<a_2<...<a_n ≀ 10^3) β€” the array written by Giraffe. Output Print a single integer β€” the maximum number of consecutive elements in the array that JATC can erase. If it is impossible to erase even a single element, print 0. Examples Input 6 1 3 4 5 6 9 Output 2 Input 3 998 999 1000 Output 2 Input 5 1 2 3 4 5 Output 4 Note In the first example, JATC can erase the third and fourth elements, leaving the array [1, 3, \\_, \\_, 6, 9]. As you can see, there is only one way to fill in the blanks. In the second example, JATC can erase the second and the third elements. The array will become [998, \\_, \\_]. Because all the elements are less than or equal to 1000, the array is still can be restored. Note, that he can't erase the first 2 elements. In the third example, JATC can erase the first 4 elements. Since all the elements are greater than or equal to 1, Giraffe can still restore the array. Note, that he can't erase the last 4 elements. Submitted Solution: ``` def test_1(): # A - import math from collections import Counter n = list(map(int, input().split(" ")))[0] data = list(map(int, input().split(" "))) count_1 = 0 count_2 = 0 data.insert(0, 0) data.append(1001) # print(data) for i in range(1, n+1): if data[i+1] - data[i-1] == 2: count_1 += 1 else: count_2 = max(count_2, count_1) count_1 = 0 count_2 = max(count_2, count_1) print(count_2) test_1() ```
instruction
0
63,999
12
127,998
Yes
output
1
63,999
12
127,999
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array a_1, a_2, ..., a_n of integers, such that 1 ≀ a_1 < a_2 < … < a_n ≀ 10^3, and then went to the bathroom. JATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range [1, 10^3]. JATC wonders what is the greatest number of elements he can erase? Input The first line of the input contains a single integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line of the input contains n integers a_i (1 ≀ a_1<a_2<...<a_n ≀ 10^3) β€” the array written by Giraffe. Output Print a single integer β€” the maximum number of consecutive elements in the array that JATC can erase. If it is impossible to erase even a single element, print 0. Examples Input 6 1 3 4 5 6 9 Output 2 Input 3 998 999 1000 Output 2 Input 5 1 2 3 4 5 Output 4 Note In the first example, JATC can erase the third and fourth elements, leaving the array [1, 3, \\_, \\_, 6, 9]. As you can see, there is only one way to fill in the blanks. In the second example, JATC can erase the second and the third elements. The array will become [998, \\_, \\_]. Because all the elements are less than or equal to 1000, the array is still can be restored. Note, that he can't erase the first 2 elements. In the third example, JATC can erase the first 4 elements. Since all the elements are greater than or equal to 1, Giraffe can still restore the array. Note, that he can't erase the last 4 elements. Submitted Solution: ``` amount = int(input()) array = [int(s) for s in input().split()] counter = 0 for i in range(len(array)): for j in range(len(array)): if array[j] - array[i] == j - i and array[j] != 1 and array[i] != 1 and array[j] != 1000 and array[i] != 1000 and j - i > counter: counter = j - i - 1 #print(counter) elif (array[j] - array[i] == j - i and (array[j] == 1 or array[i] == 1 or array[j] == 1000 or array[i] == 1000) and j - i > counter): counter = j - i #print(counter) print(counter) ```
instruction
0
64,000
12
128,000
Yes
output
1
64,000
12
128,001
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array a_1, a_2, ..., a_n of integers, such that 1 ≀ a_1 < a_2 < … < a_n ≀ 10^3, and then went to the bathroom. JATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range [1, 10^3]. JATC wonders what is the greatest number of elements he can erase? Input The first line of the input contains a single integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line of the input contains n integers a_i (1 ≀ a_1<a_2<...<a_n ≀ 10^3) β€” the array written by Giraffe. Output Print a single integer β€” the maximum number of consecutive elements in the array that JATC can erase. If it is impossible to erase even a single element, print 0. Examples Input 6 1 3 4 5 6 9 Output 2 Input 3 998 999 1000 Output 2 Input 5 1 2 3 4 5 Output 4 Note In the first example, JATC can erase the third and fourth elements, leaving the array [1, 3, \\_, \\_, 6, 9]. As you can see, there is only one way to fill in the blanks. In the second example, JATC can erase the second and the third elements. The array will become [998, \\_, \\_]. Because all the elements are less than or equal to 1000, the array is still can be restored. Note, that he can't erase the first 2 elements. In the third example, JATC can erase the first 4 elements. Since all the elements are greater than or equal to 1, Giraffe can still restore the array. Note, that he can't erase the last 4 elements. Submitted Solution: ``` from math import sqrt def main(): n = int(input()) a = list(map(int, input().split())) l = 0 maxlen = 0 i = 0 while i < n: b = [a[i]] cnt = 1 j = i + 1 while j < n and a[j] - a[j-1] == 1: b.append(a[j]) j += 1 cnt = len(b) - 2 if b[0] == 1: cnt += 1 if b[-1] == 1000: cnt += 1 if cnt > maxlen: maxlen = cnt b = [] i = j print(maxlen) return main() ```
instruction
0
64,001
12
128,002
Yes
output
1
64,001
12
128,003
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array a_1, a_2, ..., a_n of integers, such that 1 ≀ a_1 < a_2 < … < a_n ≀ 10^3, and then went to the bathroom. JATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range [1, 10^3]. JATC wonders what is the greatest number of elements he can erase? Input The first line of the input contains a single integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line of the input contains n integers a_i (1 ≀ a_1<a_2<...<a_n ≀ 10^3) β€” the array written by Giraffe. Output Print a single integer β€” the maximum number of consecutive elements in the array that JATC can erase. If it is impossible to erase even a single element, print 0. Examples Input 6 1 3 4 5 6 9 Output 2 Input 3 998 999 1000 Output 2 Input 5 1 2 3 4 5 Output 4 Note In the first example, JATC can erase the third and fourth elements, leaving the array [1, 3, \\_, \\_, 6, 9]. As you can see, there is only one way to fill in the blanks. In the second example, JATC can erase the second and the third elements. The array will become [998, \\_, \\_]. Because all the elements are less than or equal to 1000, the array is still can be restored. Note, that he can't erase the first 2 elements. In the third example, JATC can erase the first 4 elements. Since all the elements are greater than or equal to 1, Giraffe can still restore the array. Note, that he can't erase the last 4 elements. Submitted Solution: ``` N = int(input()) S = 0 L = list(map(int,input().split(' '))) E = 1 T = 0 for l in range(N-1): if L[l]+1 == L[l+1]: S += 1 T += (l!=0)*E E = 0 else: E = 1 print(S-T) ```
instruction
0
64,002
12
128,004
No
output
1
64,002
12
128,005
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array a_1, a_2, ..., a_n of integers, such that 1 ≀ a_1 < a_2 < … < a_n ≀ 10^3, and then went to the bathroom. JATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range [1, 10^3]. JATC wonders what is the greatest number of elements he can erase? Input The first line of the input contains a single integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line of the input contains n integers a_i (1 ≀ a_1<a_2<...<a_n ≀ 10^3) β€” the array written by Giraffe. Output Print a single integer β€” the maximum number of consecutive elements in the array that JATC can erase. If it is impossible to erase even a single element, print 0. Examples Input 6 1 3 4 5 6 9 Output 2 Input 3 998 999 1000 Output 2 Input 5 1 2 3 4 5 Output 4 Note In the first example, JATC can erase the third and fourth elements, leaving the array [1, 3, \\_, \\_, 6, 9]. As you can see, there is only one way to fill in the blanks. In the second example, JATC can erase the second and the third elements. The array will become [998, \\_, \\_]. Because all the elements are less than or equal to 1000, the array is still can be restored. Note, that he can't erase the first 2 elements. In the third example, JATC can erase the first 4 elements. Since all the elements are greater than or equal to 1, Giraffe can still restore the array. Note, that he can't erase the last 4 elements. Submitted Solution: ``` n=int(input()) list1=list(map(int,input().split())) c=0 s=0 ele=list1[0] list2=[ele] for i in range(1,n): # print(list2) if(list1[i]==ele+1): list2.append(list1[i]) ele=list1[i] else: ele=list1[i] # print(ele) if(list2): # print(list2) c=len(list2)-2 if(c<0): c=0 list2=[ele] # print("df") continue if(list2[0]==1): c+=1 if(list2[-1]==1000): c+=1 s+=c list2=[ele] if(list2): f=0 c=len(list2)-2 if(c<0): c=0 list2=[ele] f=1 if(f==0): if(list2[0]==1): c+=1 if(list2[-1]==1000): c+=1 s+=c print(s) ```
instruction
0
64,003
12
128,006
No
output
1
64,003
12
128,007
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array a_1, a_2, ..., a_n of integers, such that 1 ≀ a_1 < a_2 < … < a_n ≀ 10^3, and then went to the bathroom. JATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range [1, 10^3]. JATC wonders what is the greatest number of elements he can erase? Input The first line of the input contains a single integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line of the input contains n integers a_i (1 ≀ a_1<a_2<...<a_n ≀ 10^3) β€” the array written by Giraffe. Output Print a single integer β€” the maximum number of consecutive elements in the array that JATC can erase. If it is impossible to erase even a single element, print 0. Examples Input 6 1 3 4 5 6 9 Output 2 Input 3 998 999 1000 Output 2 Input 5 1 2 3 4 5 Output 4 Note In the first example, JATC can erase the third and fourth elements, leaving the array [1, 3, \\_, \\_, 6, 9]. As you can see, there is only one way to fill in the blanks. In the second example, JATC can erase the second and the third elements. The array will become [998, \\_, \\_]. Because all the elements are less than or equal to 1000, the array is still can be restored. Note, that he can't erase the first 2 elements. In the third example, JATC can erase the first 4 elements. Since all the elements are greater than or equal to 1, Giraffe can still restore the array. Note, that he can't erase the last 4 elements. Submitted Solution: ``` n=int(input()) arr=list(map(int,input().split())) if n==2: if arr[1]==1000: print(1) exit() elif arr[1]==2: print(1) exit() else: print(0) exit() cons=0 first=arr[0] i=0 j=1 cc=1 mm=1 maxi=i maxj=j while i<n and j<n: if (first+1)==arr[j]: first=arr[j] j+=1 cc+=1 else: if mm<cc: maxj=j maxi=i mm=max(cc,mm) cc=1 i=j first=arr[i] j+=1 if mm<cc: maxj=j maxi=i mm=max(cc,mm) maxj-=1 if maxi==0 and maxj==(n-1): if arr[0]==1 and arr[-1]==n: print(mm-1) exit() if arr[-1]==1000: print(mm-1) exit() if maxi==0 and arr[0]==1: print(mm-1) exit() print(max(mm-2,0)) ```
instruction
0
64,004
12
128,008
No
output
1
64,004
12
128,009
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. JATC and his friend Giraffe are currently in their room, solving some problems. Giraffe has written on the board an array a_1, a_2, ..., a_n of integers, such that 1 ≀ a_1 < a_2 < … < a_n ≀ 10^3, and then went to the bathroom. JATC decided to prank his friend by erasing some consecutive elements in the array. Since he doesn't want for the prank to go too far, he will only erase in a way, such that Giraffe can still restore the array using the information from the remaining elements. Because Giraffe has created the array, he's also aware that it's an increasing array and all the elements are integers in the range [1, 10^3]. JATC wonders what is the greatest number of elements he can erase? Input The first line of the input contains a single integer n (1 ≀ n ≀ 100) β€” the number of elements in the array. The second line of the input contains n integers a_i (1 ≀ a_1<a_2<...<a_n ≀ 10^3) β€” the array written by Giraffe. Output Print a single integer β€” the maximum number of consecutive elements in the array that JATC can erase. If it is impossible to erase even a single element, print 0. Examples Input 6 1 3 4 5 6 9 Output 2 Input 3 998 999 1000 Output 2 Input 5 1 2 3 4 5 Output 4 Note In the first example, JATC can erase the third and fourth elements, leaving the array [1, 3, \\_, \\_, 6, 9]. As you can see, there is only one way to fill in the blanks. In the second example, JATC can erase the second and the third elements. The array will become [998, \\_, \\_]. Because all the elements are less than or equal to 1000, the array is still can be restored. Note, that he can't erase the first 2 elements. In the third example, JATC can erase the first 4 elements. Since all the elements are greater than or equal to 1, Giraffe can still restore the array. Note, that he can't erase the last 4 elements. Submitted Solution: ``` n = int(input()) a = list(map(int, input().split())) if n == 1: print(0) else: ans = 0 if a[1] == 2: ans += 1 for i in range(1, n - 1): if a[i + 1] - a[i - 1] == 2: ans += 1 if n >= 2 and a[n - 2] == 999: ans += 1 print(ans) ```
instruction
0
64,005
12
128,010
No
output
1
64,005
12
128,011
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n non-decreasing arrays of non-negative numbers. Vasya repeats the following operation k times: * Selects a non-empty array. * Puts the first element of the selected array in his pocket. * Removes the first element from the selected array. Vasya wants to maximize the sum of the elements in his pocket. Input The first line contains two integers n and k (1 ≀ n, k ≀ 3 000): the number of arrays and operations. Each of the next n lines contain an array. The first integer in each line is t_i (1 ≀ t_i ≀ 10^6): the size of the i-th array. The following t_i integers a_{i, j} (0 ≀ a_{i, 1} ≀ … ≀ a_{i, t_i} ≀ 10^8) are the elements of the i-th array. It is guaranteed that k ≀ βˆ‘_{i=1}^n t_i ≀ 10^6. Output Print one integer: the maximum possible sum of all elements in Vasya's pocket after k operations. Example Input 3 3 2 5 10 3 1 2 3 2 1 20 Output 26 Submitted Solution: ``` #!/usr/bin/env python import os import sys from io import BytesIO, IOBase def main(): n,k = map(int,input().split()) a = [] aSum = [] aSum2 = [] for _ in range(n): a.append(list(map(int,input().split()))) sumL = [0] for elem in a[-1][1:]: sumL.append(sumL[-1] + elem) aSum2.append(sumL) aSum.append(sumL[-1]) INF = 10 ** 12 DP = [-INF] * (k + 1) DP[0] = 0 DPLast = [-1] * (k + 1) for i in range(n): for j in range(k, -1, -1): if j - a[i][0] >= 0 and DP[j] < DP[j - a[i][0]] + aSum[i]: DP[j] = DP[j - a[i][0]] + aSum[i] DPLast[j] = i maxAns = DP[k] for j in range(k): jCpy = j maxComp = [] while DPLast[jCpy] != -1: maxComp.append(DPLast[jCpy]) jCpy -= a[DPLast[jCpy]][0] maxComp.sort() curIndex = 0 for i in range(n): while curIndex < len(maxComp) and maxComp[curIndex] < i: curIndex += 1 if (curIndex < len(maxComp) and i == maxComp[curIndex]) or len(aSum2[i]) < k - j + 1: continue elif maxAns < aSum2[i][k - j] + DP[j]: maxAns = aSum2[i][k - j] + DP[j] print(maxAns) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main() ```
instruction
0
64,181
12
128,362
No
output
1
64,181
12
128,363
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n non-decreasing arrays of non-negative numbers. Vasya repeats the following operation k times: * Selects a non-empty array. * Puts the first element of the selected array in his pocket. * Removes the first element from the selected array. Vasya wants to maximize the sum of the elements in his pocket. Input The first line contains two integers n and k (1 ≀ n, k ≀ 3 000): the number of arrays and operations. Each of the next n lines contain an array. The first integer in each line is t_i (1 ≀ t_i ≀ 10^6): the size of the i-th array. The following t_i integers a_{i, j} (0 ≀ a_{i, 1} ≀ … ≀ a_{i, t_i} ≀ 10^8) are the elements of the i-th array. It is guaranteed that k ≀ βˆ‘_{i=1}^n t_i ≀ 10^6. Output Print one integer: the maximum possible sum of all elements in Vasya's pocket after k operations. Example Input 3 3 2 5 10 3 1 2 3 2 1 20 Output 26 Submitted Solution: ``` #!/usr/bin/env python import os import sys from io import BytesIO, IOBase def main(): n,k = map(int,input().split()) a = [] aSum = [] aSum2 = [] for _ in range(n): a.append(list(map(int,input().split()))) sumL = [0] for elem in a[-1][1:]: sumL.append(sumL[-1] + elem) aSum2.append(sumL) aSum.append(sumL[-1]) INF = 10 ** 12 DP = [-INF] * (k + 1) DP[0] = 0 DPLast = [-1] * (k + 1) for i in range(n): for j in range(1,k + 1): if j - a[i][0] >= 0 and DP[j] < DP[j - a[i][0]] + aSum[i]: DP[j] = DP[j - a[i][0]] + aSum[i] DPLast[j] = i maxAns = DP[k] print(DP) for j in range(k): jCpy = j maxComp = [] while DPLast[jCpy] != -1: maxComp.append(DPLast[jCpy]) jCpy -= a[DPLast[jCpy]][0] maxComp.sort() curIndex = 0 for i in range(n): while curIndex < len(maxComp) and maxComp[curIndex] < i: curIndex += 1 if (curIndex < len(maxComp) and i == maxComp[curIndex]) or len(aSum2[i]) < k - j + 1: continue elif maxAns < aSum2[i][k - j] + DP[j]: maxAns = aSum2[i][k - j] + DP[j] print(maxAns) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main() ```
instruction
0
64,182
12
128,364
No
output
1
64,182
12
128,365
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n non-decreasing arrays of non-negative numbers. Vasya repeats the following operation k times: * Selects a non-empty array. * Puts the first element of the selected array in his pocket. * Removes the first element from the selected array. Vasya wants to maximize the sum of the elements in his pocket. Input The first line contains two integers n and k (1 ≀ n, k ≀ 3 000): the number of arrays and operations. Each of the next n lines contain an array. The first integer in each line is t_i (1 ≀ t_i ≀ 10^6): the size of the i-th array. The following t_i integers a_{i, j} (0 ≀ a_{i, 1} ≀ … ≀ a_{i, t_i} ≀ 10^8) are the elements of the i-th array. It is guaranteed that k ≀ βˆ‘_{i=1}^n t_i ≀ 10^6. Output Print one integer: the maximum possible sum of all elements in Vasya's pocket after k operations. Example Input 3 3 2 5 10 3 1 2 3 2 1 20 Output 26 Submitted Solution: ``` #!/usr/bin/env python import os import sys from io import BytesIO, IOBase def main(): n,k = map(int,input().split()) a = [] aSum = [] for _ in range(n): a.append(list(map(int,input().split()))) aSum.append(sum(a[-1][1:])) DP = [0] * (k + 1) for i in range(n): for j in range(k + 1): if a[i][0] + j < k: DP[a[i][0] + j] = max(DP[a[i][0] + j],DP[j] + aSum[i]) maxAns = DP[k] for li in a: curSum = 0 for j in range(1, len(li)): curSum += li[j] if curSum + DP[k - j] > maxAns: maxAns = curSum + DP[k - j] print(maxAns) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main() ```
instruction
0
64,183
12
128,366
No
output
1
64,183
12
128,367
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n non-decreasing arrays of non-negative numbers. Vasya repeats the following operation k times: * Selects a non-empty array. * Puts the first element of the selected array in his pocket. * Removes the first element from the selected array. Vasya wants to maximize the sum of the elements in his pocket. Input The first line contains two integers n and k (1 ≀ n, k ≀ 3 000): the number of arrays and operations. Each of the next n lines contain an array. The first integer in each line is t_i (1 ≀ t_i ≀ 10^6): the size of the i-th array. The following t_i integers a_{i, j} (0 ≀ a_{i, 1} ≀ … ≀ a_{i, t_i} ≀ 10^8) are the elements of the i-th array. It is guaranteed that k ≀ βˆ‘_{i=1}^n t_i ≀ 10^6. Output Print one integer: the maximum possible sum of all elements in Vasya's pocket after k operations. Example Input 3 3 2 5 10 3 1 2 3 2 1 20 Output 26 Submitted Solution: ``` #!/usr/bin/env python import os import sys from io import BytesIO, IOBase def main(): n,k = map(int,input().split()) a = [] aSum = [] aSum2 = [] for _ in range(n): a.append(list(map(int,input().split()))) sumL = [0] for elem in a[-1][1:]: sumL.append(sumL[-1] + elem) aSum2.append(sumL) aSum.append(sumL[-1]) INF = 10 ** 12 DP = [-INF] * (k + 1) DP[0] = 0 DPLast = [-1] * (k + 1) for j in range(1,k + 1): for i in range(n): if j - a[i][0] >= 0 and DP[j] < DP[j - a[i][0]] + aSum[i]: DP[j] = DP[j - a[i][0]] + aSum[i] DPLast[j] = i maxAns = DP[k] for j in range(k): jCpy = j maxComp = [] while DPLast[jCpy] != -1: maxComp.append(DPLast[jCpy]) jCpy -= a[DPLast[jCpy]][0] maxComp.sort() curIndex = 0 for i in range(n): while curIndex < len(maxComp) and maxComp[curIndex] < i: curIndex += 1 if (curIndex < len(maxComp) and i == maxComp[curIndex]) or len(aSum2[i]) < k - j + 1: continue elif maxAns < aSum2[i][k - j] + DP[j]: maxAns = aSum2[i][k - j] + DP[j] print(maxAns) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main() ```
instruction
0
64,184
12
128,368
No
output
1
64,184
12
128,369
Provide tags and a correct Python 3 solution for this coding contest problem. You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers n and m (2 ≀ n; 1 ≀ m; n β‹… m ≀ 250 000) β€” the number of copies and the size of the array. Each of the following n lines describes one of the currently stored copies in the database, it consists of m integers s_{i, 1}, s_{i, 2}, ..., s_{i, m} (1 ≀ s_{i, j} ≀ 10^9). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length m and contain integers between 1 and 10^9 only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Output Yes 1 10 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Output Yes 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Output No Note In the first example, the array [1, 10, 1, 100] differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array [1, 1, 1, 1, 1, 1, 1] is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy.
instruction
0
64,201
12
128,402
Tags: brute force, constructive algorithms, dfs and similar, greedy, implementation Correct Solution: ``` N, M = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(N)] from copy import * ans = copy(A[0]) for i in range(1,N): cnt = 0 lis = [] for j in range(M): if ans[j]!=A[i][j]: lis.append((j,A[i][j])) cnt += 1 if cnt>4: print('No') exit() if cnt>2: break else: print('Yes') print(*ans) exit() for ind,a in lis: ans = copy(A[0]) ans[ind] = a for i in range(1,N): cnt = 0 lis2 = [] for j in range(M): if ans[j]!=A[i][j]: cnt += 1 if j != ind: lis2.append((j,A[i][j])) if cnt>2: break else: print('Yes') print(*ans) exit() if cnt>3: continue for ind2,a2 in lis2: ans = copy(A[0]) ans[ind] = a ans[ind2] = a2 for i in range(1,N): cnt = 0 lis2 = [] for j in range(M): if ans[j]!=A[i][j]: cnt += 1 if cnt>2: break else: print('Yes') print(*ans) exit() print('No') exit() ```
output
1
64,201
12
128,403
Provide tags and a correct Python 3 solution for this coding contest problem. You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers n and m (2 ≀ n; 1 ≀ m; n β‹… m ≀ 250 000) β€” the number of copies and the size of the array. Each of the following n lines describes one of the currently stored copies in the database, it consists of m integers s_{i, 1}, s_{i, 2}, ..., s_{i, m} (1 ≀ s_{i, j} ≀ 10^9). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length m and contain integers between 1 and 10^9 only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Output Yes 1 10 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Output Yes 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Output No Note In the first example, the array [1, 10, 1, 100] differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array [1, 1, 1, 1, 1, 1, 1] is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy.
instruction
0
64,202
12
128,404
Tags: brute force, constructive algorithms, dfs and similar, greedy, implementation Correct Solution: ``` N, M = map(int, input().split()) A = [] for i in range(N): a = list(map(int, input().split())) for j in range(M): A.append(a[j]) ans = A[:M] if M<=2: print('Yes') print(*ans) exit() for i in range(1,N): cnt = 0 lis = [] for j in range(M): if ans[j]!=A[i*M+j]: lis.append((j,A[i*M+j])) cnt += 1 if cnt>4: print('No') exit() if cnt>2: break else: print('Yes') print(*ans) exit() for ind,a in lis: ans = A[:M] ans[ind] = a for i in range(1,N): cnt = 0 lis2 = [] for j in range(M): if ans[j]!=A[i*M+j]: cnt += 1 if j != ind: lis2.append((j,A[i*M+j])) if cnt>3: break if cnt>2: break else: print('Yes') print(*ans) exit() if cnt>3: continue for ind2,a2 in lis2: ans = A[:M] ans[ind] = a ans[ind2] = a2 for i in range(1,N): cnt = 0 lis2 = [] for j in range(M): if ans[j]!=A[i*M+j]: cnt += 1 if cnt>3: break if cnt>2: break else: print('Yes') print(*ans) exit() print('No') exit() ```
output
1
64,202
12
128,405
Provide tags and a correct Python 3 solution for this coding contest problem. You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers n and m (2 ≀ n; 1 ≀ m; n β‹… m ≀ 250 000) β€” the number of copies and the size of the array. Each of the following n lines describes one of the currently stored copies in the database, it consists of m integers s_{i, 1}, s_{i, 2}, ..., s_{i, m} (1 ≀ s_{i, j} ≀ 10^9). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length m and contain integers between 1 and 10^9 only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Output Yes 1 10 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Output Yes 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Output No Note In the first example, the array [1, 10, 1, 100] differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array [1, 1, 1, 1, 1, 1, 1] is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy.
instruction
0
64,203
12
128,406
Tags: brute force, constructive algorithms, dfs and similar, greedy, implementation Correct Solution: ``` import sys #input = sys.stdin.readline def solve(): n, m = map(int,input().split()) a = [None]*n for i in range(n): a[i] = list(map(int, input().split())) c = [] row = 0 for i in range(1,n): c1 = [] for j in range(m): if a[0][j] != a[i][j]: c1.append(j) if len(c1) > len(c): c = c1 row = i if len(c) > 4: print('No') return c = [] row2 = row for i in range(0,n): c1 = [] for j in range(m): if a[row][j] != a[i][j]: c1.append(j) if len(c1) > len(c): c = c1 row2 = i b = a[row2].copy() for i in range(2**len(c)): for j in range(len(c)): cc = c[j] if ((i >> j) & 1) != 0: b[cc] = a[row][cc] else: b[cc] = a[row2][cc] bad = False for j in range(n): cnt = 0 for k in range(m): cnt += int(b[k] != a[j][k]) if cnt > 2: bad = True break if not bad: print('Yes') print(' '.join(map(str,b))) return print('No') solve() ```
output
1
64,203
12
128,407
Provide tags and a correct Python 3 solution for this coding contest problem. You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers n and m (2 ≀ n; 1 ≀ m; n β‹… m ≀ 250 000) β€” the number of copies and the size of the array. Each of the following n lines describes one of the currently stored copies in the database, it consists of m integers s_{i, 1}, s_{i, 2}, ..., s_{i, m} (1 ≀ s_{i, j} ≀ 10^9). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length m and contain integers between 1 and 10^9 only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Output Yes 1 10 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Output Yes 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Output No Note In the first example, the array [1, 10, 1, 100] differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array [1, 1, 1, 1, 1, 1, 1] is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy.
instruction
0
64,204
12
128,408
Tags: brute force, constructive algorithms, dfs and similar, greedy, implementation Correct Solution: ``` import sys input = sys.stdin.readline N, M = map(int, input().split()) A = [] for i in range(N): a = list(map(int, input().split())) for j in range(M): A.append(a[j]) ans = A[:M] if M<=2: print('Yes') print(*ans) exit() for i in range(1,N): cnt = 0 lis = [] for j in range(M): if ans[j]!=A[i*M+j]: lis.append((j,A[i*M+j])) cnt += 1 if cnt>4: print('No') exit() if cnt>2: break else: print('Yes') print(*ans) exit() for ind,a in lis: ans = A[:M] ans[ind] = a for i in range(1,N): cnt = 0 lis2 = [] for j in range(M): if ans[j]!=A[i*M+j]: cnt += 1 if j != ind: lis2.append((j,A[i*M+j])) if cnt>3: break if cnt>2: break else: print('Yes') print(*ans) exit() if cnt>3: continue for ind2,a2 in lis2: ans = A[:M] ans[ind] = a ans[ind2] = a2 for i in range(1,N): cnt = 0 lis2 = [] for j in range(M): if ans[j]!=A[i*M+j]: cnt += 1 if cnt>3: break if cnt>2: break else: print('Yes') print(*ans) exit() print('No') exit() ```
output
1
64,204
12
128,409
Provide tags and a correct Python 3 solution for this coding contest problem. You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers n and m (2 ≀ n; 1 ≀ m; n β‹… m ≀ 250 000) β€” the number of copies and the size of the array. Each of the following n lines describes one of the currently stored copies in the database, it consists of m integers s_{i, 1}, s_{i, 2}, ..., s_{i, m} (1 ≀ s_{i, j} ≀ 10^9). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length m and contain integers between 1 and 10^9 only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Output Yes 1 10 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Output Yes 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Output No Note In the first example, the array [1, 10, 1, 100] differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array [1, 1, 1, 1, 1, 1, 1] is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy.
instruction
0
64,205
12
128,410
Tags: brute force, constructive algorithms, dfs and similar, greedy, implementation Correct Solution: ``` import io import os from collections import Counter, defaultdict, deque from itertools import product # Similar to https://codeforces.com/contest/1360/submission/81330345 def solve(N, M, A): def getDiff(w1, w2): return [i for i in range(M) if w1[i] != w2[i]] def fourDiffAnswer(word1, word2, indices): # If exactly 4 differences, can restore original array: # aboo # oocd assert len(indices) == 4 w1 = list(word1) numIndices = len(indices) for mask in range(1 << numIndices): # do for pos, i in enumerate(indices): if (mask >> pos) & 1: w1[i] = word1[i] else: w1[i] = word2[i] # check if len(getDiff(w1, word1)) <= 2 and len(getDiff(w1, word2)) <= 2: if all(len(getDiff(w1, w2)) <= 2 for w2 in A): return w1 # undo for i in indices: w1[i] = word1 return False def threeDiffAnswer(word1, word2, indices): # If 3 differences, can restore up to 1 unknown # abo # ocd assert len(indices) == 3 w1 = list(word1) numIndices = len(indices) for unknownIndex in indices: for mask in range(1 << numIndices): # do for pos, i in enumerate(indices): if (mask >> pos) & 1: w1[i] = word1[i] else: w1[i] = word2[i] if i == unknownIndex: w1[i] = -1 # check good = True cand = set() for w2 in A: diffs = getDiff(w1, w2) if len(diffs) > 3: # No value of unknown will work good = False break if len(diffs) == 3: # Forced unknown to be this cand.add(w2[unknownIndex]) if len(cand) > 1: good = False if good: if len(cand) == 1: w1[unknownIndex] = next(iter(cand)) else: w1[unknownIndex] = 1 assert all(len(getDiff(w1, w2)) <= 2 for w2 in A) return w1 # undo for i in indices: w1[i] = word1 return False def formatAnswer(ans): if not ans: return "No" return "Yes\n" + " ".join(map(str, ans)) w1 = A[0] mostDiff = (0,) for j in range(1, N): w2 = A[j] indices = getDiff(w1, w2) if len(indices) > 4: return "No" elif len(indices) == 4: return formatAnswer(fourDiffAnswer(w1, w2, indices)) elif len(indices): mostDiff = max(mostDiff, (len(indices), j)) if mostDiff[0] <= 2: # First word is usable return formatAnswer(w1) assert mostDiff[0] == 3 w2 = A[mostDiff[1]] return formatAnswer(threeDiffAnswer(w1, w2, getDiff(w1, w2))) DEBUG = False if DEBUG: import random random.seed(10) for _ in range(1000000): M = random.randint(1, 10) orig = [0] * M N = random.randint(1, 10) A = [] for i in range(N): copy = list(orig) i1 = random.randint(0, M - 1) i2 = random.randint(0, M - 1) copy[i1] = random.randint(0, 10) copy[i2] = random.randint(0, 10) A.append(copy) ans = solve(N, M, A) assert not ans.startswith("No") exit() if __name__ == "__main__": input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline TC = 1 for tc in range(1, TC + 1): N, M = [int(x) for x in input().split()] S = [[int(x) for x in input().split()] for i in range(N)] ans = solve(N, M, S) print(ans) ```
output
1
64,205
12
128,411
Provide tags and a correct Python 3 solution for this coding contest problem. You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers n and m (2 ≀ n; 1 ≀ m; n β‹… m ≀ 250 000) β€” the number of copies and the size of the array. Each of the following n lines describes one of the currently stored copies in the database, it consists of m integers s_{i, 1}, s_{i, 2}, ..., s_{i, m} (1 ≀ s_{i, j} ≀ 10^9). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length m and contain integers between 1 and 10^9 only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Output Yes 1 10 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Output Yes 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Output No Note In the first example, the array [1, 10, 1, 100] differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array [1, 1, 1, 1, 1, 1, 1] is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy.
instruction
0
64,206
12
128,412
Tags: brute force, constructive algorithms, dfs and similar, greedy, implementation Correct Solution: ``` def solve(): n, m = map(int,input().split());a = [list(map(int, input().split())) for i in range(n)];c = [];row = 0 for i in range(1,n): c1 = [j for j in range(m) if a[0][j] != a[i][j]] if len(c1) > len(c):c = c1;row = i if len(c) > 4:print('No');return c = [];row2 = row for i in range(n): c1 = [j for j in range(m) if a[row][j] != a[i][j]] if len(c1) > len(c):c = c1;row2 = i b = a[row2].copy() for i in range(2**len(c)): for j in range(len(c)):cc = c[j];b[cc] = (a[row][cc] if ((i >> j) & 1) != 0 else a[row2][cc]) bad = False for j in range(n): cnt = 0 for k in range(m):cnt += int(b[k] != a[j][k]) if cnt > 2:bad = True;break if not bad:print('Yes');print(' '.join(map(str,b)));return print('No') solve() ```
output
1
64,206
12
128,413
Provide tags and a correct Python 3 solution for this coding contest problem. You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers n and m (2 ≀ n; 1 ≀ m; n β‹… m ≀ 250 000) β€” the number of copies and the size of the array. Each of the following n lines describes one of the currently stored copies in the database, it consists of m integers s_{i, 1}, s_{i, 2}, ..., s_{i, m} (1 ≀ s_{i, j} ≀ 10^9). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length m and contain integers between 1 and 10^9 only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Output Yes 1 10 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Output Yes 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Output No Note In the first example, the array [1, 10, 1, 100] differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array [1, 1, 1, 1, 1, 1, 1] is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy.
instruction
0
64,207
12
128,414
Tags: brute force, constructive algorithms, dfs and similar, greedy, implementation Correct Solution: ``` def solve(): n, m = map(int,input().split()) a = [None]*n for i in range(n): a[i] = list(map(int, input().split())) c = [] row = 0 for i in range(1,n): c1 = [] for j in range(m): if a[0][j] != a[i][j]: c1.append(j) if len(c1) > len(c): c = c1 row = i if len(c) > 4: print('No') return c = [] row2 = row for i in range(0,n): c1 = [] for j in range(m): if a[row][j] != a[i][j]: c1.append(j) if len(c1) > len(c): c = c1 row2 = i b = a[row2].copy() for i in range(2**len(c)): for j in range(len(c)): cc = c[j] if ((i >> j) & 1) != 0: b[cc] = a[row][cc] else: b[cc] = a[row2][cc] bad = False for j in range(n): cnt = 0 for k in range(m): cnt += int(b[k] != a[j][k]) if cnt > 2: bad = True break if not bad: print('Yes') print(' '.join(map(str,b))) return print('No') solve() ```
output
1
64,207
12
128,415
Provide tags and a correct Python 3 solution for this coding contest problem. You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers n and m (2 ≀ n; 1 ≀ m; n β‹… m ≀ 250 000) β€” the number of copies and the size of the array. Each of the following n lines describes one of the currently stored copies in the database, it consists of m integers s_{i, 1}, s_{i, 2}, ..., s_{i, m} (1 ≀ s_{i, j} ≀ 10^9). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length m and contain integers between 1 and 10^9 only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Output Yes 1 10 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Output Yes 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Output No Note In the first example, the array [1, 10, 1, 100] differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array [1, 1, 1, 1, 1, 1, 1] is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy.
instruction
0
64,208
12
128,416
Tags: brute force, constructive algorithms, dfs and similar, greedy, implementation Correct Solution: ``` import sys input = sys.stdin.readline N, M = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(N)] ans = A[0][:] for i in range(1,N): cnt = 0 lis = [] for j in range(M): if ans[j]!=A[i][j]: lis.append((j,A[i][j])) cnt += 1 if cnt>4: print('No') exit() if cnt>2: break else: print('Yes') print(*ans) exit() for ind,a in lis: ans = A[0][:] ans[ind] = a for i in range(1,N): cnt = 0 lis2 = [] for j in range(M): if ans[j]!=A[i][j]: cnt += 1 if j != ind: lis2.append((j,A[i][j])) if cnt>3: break if cnt>2: break else: print('Yes') print(*ans) exit() if cnt>3: continue for ind2,a2 in lis2: ans = A[0][:] ans[ind] = a ans[ind2] = a2 for i in range(1,N): cnt = 0 lis2 = [] for j in range(M): if ans[j]!=A[i][j]: cnt += 1 if cnt>3: break if cnt>2: break else: print('Yes') print(*ans) exit() print('No') exit() ```
output
1
64,208
12
128,417
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers n and m (2 ≀ n; 1 ≀ m; n β‹… m ≀ 250 000) β€” the number of copies and the size of the array. Each of the following n lines describes one of the currently stored copies in the database, it consists of m integers s_{i, 1}, s_{i, 2}, ..., s_{i, m} (1 ≀ s_{i, j} ≀ 10^9). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length m and contain integers between 1 and 10^9 only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Output Yes 1 10 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Output Yes 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Output No Note In the first example, the array [1, 10, 1, 100] differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array [1, 1, 1, 1, 1, 1, 1] is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy. Submitted Solution: ``` N, M = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(N)] from copy import * ans = copy(A[0]) for i in range(1,N): cnt = 0 lis = [] for j in range(M): if ans[j]!=A[i][j]: lis.append((j,A[i][j])) cnt += 1 if cnt>4: print('No') exit() if cnt>2: break else: print('Yes') print(*ans) exit() for ind,a in lis: ans = copy(A[0]) ans[ind] = a for i in range(1,N): cnt = 0 lis2 = [] for j in range(M): if ans[j]!=A[i][j]: cnt += 1 if j != ind: lis2.append((j,A[i][j])) if cnt>3: break if cnt>2: break else: print('Yes') print(*ans) exit() if cnt>3: continue for ind2,a2 in lis2: ans = copy(A[0]) ans[ind] = a ans[ind2] = a2 for i in range(1,N): cnt = 0 lis2 = [] for j in range(M): if ans[j]!=A[i][j]: cnt += 1 if cnt>3: break if cnt>2: break else: print('Yes') print(*ans) exit() print('No') exit() ```
instruction
0
64,209
12
128,418
Yes
output
1
64,209
12
128,419
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers n and m (2 ≀ n; 1 ≀ m; n β‹… m ≀ 250 000) β€” the number of copies and the size of the array. Each of the following n lines describes one of the currently stored copies in the database, it consists of m integers s_{i, 1}, s_{i, 2}, ..., s_{i, m} (1 ≀ s_{i, j} ≀ 10^9). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length m and contain integers between 1 and 10^9 only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Output Yes 1 10 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Output Yes 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Output No Note In the first example, the array [1, 10, 1, 100] differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array [1, 1, 1, 1, 1, 1, 1] is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy. Submitted Solution: ``` N, M = map(int, input().split()) A = [] for i in range(N): a = list(map(int, input().split())) for j in range(M): A.append(a[j]) ans = A[:M] for i in range(1,N): cnt = 0 lis = [] for j in range(M): if ans[j]!=A[i*M+j]: lis.append((j,A[i*M+j])) cnt += 1 if cnt>4: print('No') exit() if cnt>2: break else: print('Yes') print(*ans) exit() for ind,a in lis: ans = A[:M] ans[ind] = a for i in range(1,N): cnt = 0 lis2 = [] for j in range(M): if ans[j]!=A[i*M+j]: cnt += 1 if j != ind: lis2.append((j,A[i*M+j])) if cnt>3: break if cnt>2: break else: print('Yes') print(*ans) exit() if cnt>3: continue for ind2,a2 in lis2: ans = A[:M] ans[ind] = a ans[ind2] = a2 for i in range(1,N): cnt = 0 lis2 = [] for j in range(M): if ans[j]!=A[i*M+j]: cnt += 1 if cnt>3: break if cnt>2: break else: print('Yes') print(*ans) exit() print('No') exit() ```
instruction
0
64,210
12
128,420
Yes
output
1
64,210
12
128,421
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers n and m (2 ≀ n; 1 ≀ m; n β‹… m ≀ 250 000) β€” the number of copies and the size of the array. Each of the following n lines describes one of the currently stored copies in the database, it consists of m integers s_{i, 1}, s_{i, 2}, ..., s_{i, m} (1 ≀ s_{i, j} ≀ 10^9). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length m and contain integers between 1 and 10^9 only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Output Yes 1 10 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Output Yes 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Output No Note In the first example, the array [1, 10, 1, 100] differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array [1, 1, 1, 1, 1, 1, 1] is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy. Submitted Solution: ``` import sys input = sys.stdin.readline def solve(): n, m = map(int,input().split()) a = [None]*n for i in range(n): a[i] = list(map(int, input().split())) c = [] row = 0 for i in range(1,n): c1 = [] for j in range(m): if a[0][j] != a[i][j]: c1.append(j) if len(c1) > len(c): c = c1 row = i if len(c) > 4: print('No') return c = [] row2 = row for i in range(0,n): c1 = [] for j in range(m): if a[row][j] != a[i][j]: c1.append(j) if len(c1) > len(c): c = c1 row2 = i b = a[row2].copy() for i in range(2**len(c)): for j in range(len(c)): cc = c[j] if ((i >> j) & 1) != 0: b[cc] = a[row][cc] else: b[cc] = a[row2][cc] bad = False for j in range(n): cnt = 0 for k in range(m): cnt += int(b[k] != a[j][k]) if cnt > 2: bad = True break if not bad: print('Yes') print(' '.join(map(str,b))) return print('No') solve() ```
instruction
0
64,211
12
128,422
Yes
output
1
64,211
12
128,423
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers n and m (2 ≀ n; 1 ≀ m; n β‹… m ≀ 250 000) β€” the number of copies and the size of the array. Each of the following n lines describes one of the currently stored copies in the database, it consists of m integers s_{i, 1}, s_{i, 2}, ..., s_{i, m} (1 ≀ s_{i, j} ≀ 10^9). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length m and contain integers between 1 and 10^9 only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Output Yes 1 10 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Output Yes 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Output No Note In the first example, the array [1, 10, 1, 100] differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array [1, 1, 1, 1, 1, 1, 1] is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy. Submitted Solution: ``` def solvetestcase(): n,m = [int(e) for e in input().split(" ")] db = [] for _ in range(n): db.append([int(e) for e in input().split(" ")]) return solve_helper(n,m,db) def solve_helper(n,m,db, start=True): found_candidate = -1 max_diffs = 0 for i in range(1,n): diffs = [j for j in range(m)if db[i][j] != db[0][j] ] ldiff = len(diffs) if ldiff > 4: return "No" if ldiff < 3: continue if ldiff > max_diffs: found_candidate = i max_diffs = ldiff if found_candidate == -1: return "Yes\n" + " ".join([str(e) for e in db[0]]) diffs = [j for j in range(m)if db[found_candidate][j] != db[0][j] ][:] for attempt in range(1, 1 + (1 << len(diffs))): current = db[0][:] for i in range(len(diffs)): if (attempt >> i) & 1: current[diffs[i]] = db[found_candidate][diffs[i]] for i in range(n): cdiffs = [j for j in range(m) if db[i][j] != current[j]] if len(cdiffs) > 2: break else: return "Yes\n" + " ".join([str(e) for e in current]) if start: db[0], db[found_candidate] = db[found_candidate], db[0] return solve_helper(n,m,db, False) return "No" print (solvetestcase()) ```
instruction
0
64,212
12
128,424
Yes
output
1
64,212
12
128,425
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers n and m (2 ≀ n; 1 ≀ m; n β‹… m ≀ 250 000) β€” the number of copies and the size of the array. Each of the following n lines describes one of the currently stored copies in the database, it consists of m integers s_{i, 1}, s_{i, 2}, ..., s_{i, m} (1 ≀ s_{i, j} ≀ 10^9). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length m and contain integers between 1 and 10^9 only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Output Yes 1 10 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Output Yes 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Output No Note In the first example, the array [1, 10, 1, 100] differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array [1, 1, 1, 1, 1, 1, 1] is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy. Submitted Solution: ``` from collections import defaultdict import sys n, m = map(int, input().split()) body = [] for i in range(n): body.append(list(map(int, input().split()))) for i in range(1, n): mistake = 0 for j in range(m): if body[i][j] != body[0][j]: mistake += 1 if mistake > 4: print("No") sys.exit() keys = [0] * m for i in range(m): keys[i] = defaultdict(int) for i in range(m): for j in range(n): keys[i][body[j][i]] += 1 #for el in keys: # print(el) #finding maxs ans = [0] * m for i in range(m): cur = 0 for el in keys[i]: if keys[i][el] > cur: cur = keys[i][el] ans[i] = el print("Yes") print(*ans) ```
instruction
0
64,213
12
128,426
No
output
1
64,213
12
128,427
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers n and m (2 ≀ n; 1 ≀ m; n β‹… m ≀ 250 000) β€” the number of copies and the size of the array. Each of the following n lines describes one of the currently stored copies in the database, it consists of m integers s_{i, 1}, s_{i, 2}, ..., s_{i, m} (1 ≀ s_{i, j} ≀ 10^9). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length m and contain integers between 1 and 10^9 only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Output Yes 1 10 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Output Yes 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Output No Note In the first example, the array [1, 10, 1, 100] differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array [1, 1, 1, 1, 1, 1, 1] is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy. Submitted Solution: ``` mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.readline def diff(A, B): N = len(A) cnt = 0 diff_list = [] for i in range(N): if A[i] != B[i]: cnt += 1 diff_list.append(i) return cnt, diff_list def diff_omit(A, B, k1, k2): N = len(A) cnt = 0 diff_list = [] for i in range(N): if A[i] != B[i]: if i != k1 and i != k2: cnt += 1 diff_list.append(i) return cnt, diff_list N, M = map(int, input().split()) C = [] for _ in range(N): C.append(list(map(int, input().split()))) ok = 1 S = set() for i in range(1, N): d, diff_list = diff(C[0], C[i]) if d > 4: print("No") exit() elif d > 2: ok = 0 if S: bad_set = set() for j in S: if j not in diff_list: bad_set.add(j) for j in bad_set: S.discard(j) if not S: print("No") exit() else: for j in diff_list: S.add(j) if ok: print("Yes") print(*C[0]) exit() if not S: print("No") exit() S = list(S) ans = C[0][:] if len(S) == 1: k = S[0] flg = 0 for i in range(1, N): d, diff_list_omit = diff_omit(C[0], C[i], k, -1) if d > 2: print("No") exit() elif d == 2: ans[k] = C[i][k] flg = 1 break if not flg: print("Yes") print(*ans) exit() for i in range(1, N): d, _ = diff(ans, C[i]) if d > 2: print("No") exit() print("Yes") print(*ans) exit() else: for ii in range(len(S)): k1 = S[ii] for jj in range(ii+1, len(S)): ans = C[0][:] k2 = S[jj] flg = 0 ng = 0 for i in range(1, N): d, diff_list_omit = diff_omit(C[0], C[i], k1, k2) if d > 2: ng = 1 break elif d == 2: ans[k1] = C[i][k1] ans[k2] = C[i][k2] flg = 2 break elif d == 1: ans2 = ans[:] ans2[k1] = C[i][k1] ans2[k2] = C[i][k2] k1_val = C[i][k1] k2_val = C[i][k2] flg = 1 break if ng: break if flg == 0: print("Yes") print(*ans) exit() if flg == 2: for i in range(1, N): d, _ = diff(ans, C[i]) if d > 2: ng = 1 if ng: break else: print("Yes") print(*ans) exit() if flg == 1: for i in range(1, N): d, _ = diff(ans2, C[i]) if d > 2: ng = 1 if not ng: print("Yes") print(*ans2) exit() ans_k1 = ans[:] ans_k1[k1] = k1_val ans_k2 = ans[:] ans_k2[k2] = k2_val # k1 ng = 0 for i in range(1, N): d, diff_list_omit = diff_omit(ans_k1, C[i], k2, -1) if d > 2: ng = 1 break elif d == 2: ans_k1[k2] = C[i][k2] flg = 1 break if not ng: if not flg: print("Yes") print(*ans_k1) exit() for i in range(1, N): d, _ = diff(ans_k1, C[i]) if d > 2: ng = 1 break if not ng: print("Yes") print(*ans_k1) exit() # k2 for i in range(1, N): d, diff_list_omit = diff_omit(ans_k2, C[i], k1, -1) if d > 2: ng = 1 break elif d == 2: ans_k2[k1] = C[i][k1] flg = 1 break if not ng: if not flg: print("Yes") print(*ans_k2) exit() for i in range(1, N): d, _ = diff(ans_k2, C[i]) if d > 2: ng = 1 break if not ng: print("Yes") print(*ans_k2) exit() print("No") if __name__ == '__main__': main() ```
instruction
0
64,214
12
128,428
No
output
1
64,214
12
128,429
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers n and m (2 ≀ n; 1 ≀ m; n β‹… m ≀ 250 000) β€” the number of copies and the size of the array. Each of the following n lines describes one of the currently stored copies in the database, it consists of m integers s_{i, 1}, s_{i, 2}, ..., s_{i, m} (1 ≀ s_{i, j} ≀ 10^9). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length m and contain integers between 1 and 10^9 only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Output Yes 1 10 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Output Yes 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Output No Note In the first example, the array [1, 10, 1, 100] differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array [1, 1, 1, 1, 1, 1, 1] is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy. Submitted Solution: ``` import sys,functools,collections,bisect,math,heapq input = sys.stdin.readline sys.setrecursionlimit(100000) #print = sys.stdout.write def isvalid(s): for i in range(n): cng = [] for j in range(m): if arr[i][j] != s[j]: cng.append(j) x = len(cng) if x > 2: return False return True def fun(s): for i in range(n): cng = [] for j in range(m): if arr[i][j] != s[j]: cng.append(j) x = len(cng) if x >= 5: return [] elif x == 3: for a in cng: temp = s[a] s[a] = arr[i][a] if isvalid(s): return s s[a] = temp return [] elif x == 4: for a in cng: temp1 = s[a] s[a] = arr[i][a] for b in cng: if a == b: continue temp2 = s[b] s[b] = arr[i][b] if isvalid(s): return s s[b] = temp2 s[a] = temp1 return [] return s #t = int(input()) for _ in range(1): n,m = map(int,input().strip().split()) arr = [] for i in range(n): arr.append(list(map(int,input().strip().split()))) s = fun(arr[0]) if s: print('YES') print(' '.join(str(i) for i in s)) else: print('NO') ```
instruction
0
64,215
12
128,430
No
output
1
64,215
12
128,431
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are storing an integer array of length m in a database. To maintain internal integrity and protect data, the database stores n copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers n and m (2 ≀ n; 1 ≀ m; n β‹… m ≀ 250 000) β€” the number of copies and the size of the array. Each of the following n lines describes one of the currently stored copies in the database, it consists of m integers s_{i, 1}, s_{i, 2}, ..., s_{i, m} (1 ≀ s_{i, j} ≀ 10^9). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length m and contain integers between 1 and 10^9 only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Output Yes 1 10 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Output Yes 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Output No Note In the first example, the array [1, 10, 1, 100] differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array [1, 1, 1, 1, 1, 1, 1] is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy. Submitted Solution: ``` # Problem 1492E - Almost Fault-Tolerant Database import itertools # computes num diffs in arr1 and arr2 def diff(arr1, arr2): count = 0 assert(len(arr1) == len(arr2)) for i in range(len(arr1)): if arr1[i] != arr2[i]: count += 1 return count # returns an arr with indices where vals are diff def diff_pos(arr1, arr2): res = [] assert(len(arr1) == len(arr2)) for i in range(len(arr1)): if arr1[i] != arr2[i]: res.append(i) return res def diff_without_counting_dpos(arr1, arr2, dpos): count = 0 for i in range(len(arr1)): if i not in dpos and arr1[i] != arr2[i]: count += 1 return count def main(): inp = input().rstrip().split(" ") n, m = int(inp[0]), int(inp[1]) copies = [] for _ in range(n): inp = input().rstrip().split(" ") copies.append([int(c) for c in inp]) i=1 while i < n and diff(copies[0], copies[i]) < 3: i += 1 if i < n: dpos = diff_pos(copies[i], copies[0]) if len(dpos) == 4: temp = [0,0,1,1] temp = list(itertools.permutations(temp)) states = [] for t in temp: state = [] for c in range(4): state.append(copies[0][dpos[c]] if t[c] == 0 else copies[i][dpos[c]]) states.append(state) c = 1 while c < n: new_states = [] for state in states: count_without_dpos = diff_without_counting_dpos(copies[0], copies[c], dpos) if count_without_dpos < 3: count = count_without_dpos for p in range(4): pos = dpos[p] if copies[c][pos] != state[p]: count += 1 if count < 3: new_states.append(state) states = new_states if len(states) == 0: break c += 1 if len(states) == 0: print("No") return ans = [str(b) for b in copies[0]] for p in range(4): ans[dpos[p]] = str(states[0][p]) print('Yes') print(" ".join(ans)) return elif len(dpos) == 3: temp = [0,1,'x'] temp = list(itertools.permutations(temp)) states = [] for t in temp: state = [] for c in range(3): if t[c] == 'x': state.append('x') else: state.append(copies[0][dpos[c]] if t[c] == 0 else copies[i][dpos[c]]) states.append(state) c = 1 while c < n: new_states = [] for state in states: count_without_dpos = diff_without_counting_dpos(copies[0], copies[c], dpos) if count_without_dpos < 3: count = count_without_dpos for p in range(3): pos = dpos[p] if copies[c][pos] != state[p]: count += 1 if count < 3: new_states.append(state) if count == 3: pos = dpos[p] for p in range(3): state[p] = copies[c][pos] if state[p] == 'x'else state[p] count -= 1 if count < 3: new_states.append(state) states = new_states if len(states) == 0: break c += 1 if len(states) == 0: print("No") return ans = [str(b) for b in copies[0]] for p in range(3): ans[dpos[p]] = str(states[0][p]) if states[0][p] != 'x' else '1' print('Yes') print(" ".join(ans)) return else: print('Yes') ans = [str(i) for i in copies[0]] print(" ".join(ans)) main() ```
instruction
0
64,216
12
128,432
No
output
1
64,216
12
128,433
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The only difference between the easy version and the hard version is the constraints on n. You can only make hacks if both versions are solved. A permutation of 1, 2, …, n is a sequence of n integers, where each integer from 1 to n appears exactly once. For example, [2,3,1,4] is a permutation of 1, 2, 3, 4, but [1,4,2,2] isn't because 2 appears twice in it. Recall that the number of inversions in a permutation a_1, a_2, …, a_n is the number of pairs of indices (i, j) such that i < j and a_i > a_j. Let p and q be two permutations of 1, 2, …, n. Find the number of permutation pairs (p,q) that satisfy the following conditions: * p is lexicographically smaller than q. * the number of inversions in p is greater than the number of inversions in q. Print the number of such pairs modulo mod. Note that mod may not be a prime. Input The only line contains two integers n and mod (1≀ n≀ 50, 1≀ mod≀ 10^9). Output Print one integer, which is the answer modulo mod. Example Input 4 403458273 Output 17 Note The following are all valid pairs (p,q) when n=4. * p=[1,3,4,2], q=[2,1,3,4], * p=[1,4,2,3], q=[2,1,3,4], * p=[1,4,3,2], q=[2,1,3,4], * p=[1,4,3,2], q=[2,1,4,3], * p=[1,4,3,2], q=[2,3,1,4], * p=[1,4,3,2], q=[3,1,2,4], * p=[2,3,4,1], q=[3,1,2,4], * p=[2,4,1,3], q=[3,1,2,4], * p=[2,4,3,1], q=[3,1,2,4], * p=[2,4,3,1], q=[3,1,4,2], * p=[2,4,3,1], q=[3,2,1,4], * p=[2,4,3,1], q=[4,1,2,3], * p=[3,2,4,1], q=[4,1,2,3], * p=[3,4,1,2], q=[4,1,2,3], * p=[3,4,2,1], q=[4,1,2,3], * p=[3,4,2,1], q=[4,1,3,2], * p=[3,4,2,1], q=[4,2,1,3].
instruction
0
64,231
12
128,462
Tags: combinatorics, dp, fft, math Correct Solution: ``` import sys def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) n,mod = MI() m = n*(n-1)//2 dp = [[[0]*(n+1) for _ in range(m+1)] for _ in range(n+1)] dp[1][0][1] = 1 for i in range(1,n): for j in range(m+1): for k in range(n+1): a = dp[i][j][k] if not a: continue dp[i+1][j+i][i+1] += a dp[i+1][j+i][i+1] %= mod for l in range(i): dp[i+1][j+i-1-l][k] += a dp[i+1][j+i-1-l][k] %= mod S_dp = [[[0]*(n+1) for k in range(m+1)] for i in range(n+1)] for i in range(n+1): for k in range(n+1): a = 0 for j in range(m+1): a += dp[i][j][k] a %= mod S_dp[i][j][k] = a SS_dp = [[[0]*(n+1) for j in range(m+1)] for i in range(n+1)] for i in range(n+1): for j in range(m+1): a = 0 for k in range(n+1): a += S_dp[i][j][k] a %= mod SS_dp[i][j][k] = a ANS = [0]*(n+1) for i in range(1,n+1): ans = i*ANS[i-1] % mod for k in range(n+1): for j in range(1,m+1): if not dp[i][j][k]: continue ans += dp[i][j][k]*(SS_dp[i][j-1][-1]-SS_dp[i][j-1][k]) ans %= mod ANS[i] = ans print(ANS[-1]) ```
output
1
64,231
12
128,463
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The only difference between the easy version and the hard version is the constraints on n. You can only make hacks if both versions are solved. A permutation of 1, 2, …, n is a sequence of n integers, where each integer from 1 to n appears exactly once. For example, [2,3,1,4] is a permutation of 1, 2, 3, 4, but [1,4,2,2] isn't because 2 appears twice in it. Recall that the number of inversions in a permutation a_1, a_2, …, a_n is the number of pairs of indices (i, j) such that i < j and a_i > a_j. Let p and q be two permutations of 1, 2, …, n. Find the number of permutation pairs (p,q) that satisfy the following conditions: * p is lexicographically smaller than q. * the number of inversions in p is greater than the number of inversions in q. Print the number of such pairs modulo mod. Note that mod may not be a prime. Input The only line contains two integers n and mod (1≀ n≀ 50, 1≀ mod≀ 10^9). Output Print one integer, which is the answer modulo mod. Example Input 4 403458273 Output 17 Note The following are all valid pairs (p,q) when n=4. * p=[1,3,4,2], q=[2,1,3,4], * p=[1,4,2,3], q=[2,1,3,4], * p=[1,4,3,2], q=[2,1,3,4], * p=[1,4,3,2], q=[2,1,4,3], * p=[1,4,3,2], q=[2,3,1,4], * p=[1,4,3,2], q=[3,1,2,4], * p=[2,3,4,1], q=[3,1,2,4], * p=[2,4,1,3], q=[3,1,2,4], * p=[2,4,3,1], q=[3,1,2,4], * p=[2,4,3,1], q=[3,1,4,2], * p=[2,4,3,1], q=[3,2,1,4], * p=[2,4,3,1], q=[4,1,2,3], * p=[3,2,4,1], q=[4,1,2,3], * p=[3,4,1,2], q=[4,1,2,3], * p=[3,4,2,1], q=[4,1,2,3], * p=[3,4,2,1], q=[4,1,3,2], * p=[3,4,2,1], q=[4,2,1,3].
instruction
0
64,232
12
128,464
Tags: combinatorics, dp, fft, math Correct Solution: ``` import sys def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) n,mod = MI() m = n*(n-1)//2 dp = [[[0]*(n+1) for _ in range(m+1)] for _ in range(n+1)] dp[1][0][1] = 1 for i in range(1,n): for j in range(m+1): for k in range(n+1): a = dp[i][j][k] if not a: continue dp[i+1][j+i][i+1] += a dp[i+1][j+i][i+1] %= mod for l in range(i): dp[i+1][j+i-1-l][k] += a dp[i+1][j+i-1-l][k] %= mod S_dp = [[[0]*(m+1) for k in range(n+1)] for i in range(n+1)] for i in range(n+1): for k in range(n+1): a = 0 for j in range(m+1): a += dp[i][j][k] a %= mod S_dp[i][k][j] = a SS_dp = [[[0]*(n+1) for j in range(m+1)] for i in range(n+1)] for i in range(n+1): for j in range(m+1): a = 0 for k in range(n+1): a += S_dp[i][k][j] a %= mod SS_dp[i][j][k] = a ANS = [0]*(n+1) for i in range(1,n+1): ans = i*ANS[i-1] % mod for k in range(n+1): for j in range(1,m+1): if not dp[i][j][k]: continue ans += dp[i][j][k]*(SS_dp[i][j-1][-1]-SS_dp[i][j-1][k]) ans %= mod ANS[i] = ans print(ANS[-1]) ```
output
1
64,232
12
128,465
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The only difference between the easy version and the hard version is the constraints on n. You can only make hacks if both versions are solved. A permutation of 1, 2, …, n is a sequence of n integers, where each integer from 1 to n appears exactly once. For example, [2,3,1,4] is a permutation of 1, 2, 3, 4, but [1,4,2,2] isn't because 2 appears twice in it. Recall that the number of inversions in a permutation a_1, a_2, …, a_n is the number of pairs of indices (i, j) such that i < j and a_i > a_j. Let p and q be two permutations of 1, 2, …, n. Find the number of permutation pairs (p,q) that satisfy the following conditions: * p is lexicographically smaller than q. * the number of inversions in p is greater than the number of inversions in q. Print the number of such pairs modulo mod. Note that mod may not be a prime. Input The only line contains two integers n and mod (1≀ n≀ 50, 1≀ mod≀ 10^9). Output Print one integer, which is the answer modulo mod. Example Input 4 403458273 Output 17 Note The following are all valid pairs (p,q) when n=4. * p=[1,3,4,2], q=[2,1,3,4], * p=[1,4,2,3], q=[2,1,3,4], * p=[1,4,3,2], q=[2,1,3,4], * p=[1,4,3,2], q=[2,1,4,3], * p=[1,4,3,2], q=[2,3,1,4], * p=[1,4,3,2], q=[3,1,2,4], * p=[2,3,4,1], q=[3,1,2,4], * p=[2,4,1,3], q=[3,1,2,4], * p=[2,4,3,1], q=[3,1,2,4], * p=[2,4,3,1], q=[3,1,4,2], * p=[2,4,3,1], q=[3,2,1,4], * p=[2,4,3,1], q=[4,1,2,3], * p=[3,2,4,1], q=[4,1,2,3], * p=[3,4,1,2], q=[4,1,2,3], * p=[3,4,2,1], q=[4,1,2,3], * p=[3,4,2,1], q=[4,1,3,2], * p=[3,4,2,1], q=[4,2,1,3].
instruction
0
64,233
12
128,466
Tags: combinatorics, dp, fft, math Correct Solution: ``` def solve(n, mod): if n<=3: return 0 else: last_ans = 17 ans = 17 size = (n-1)*(n-2)//2 + 1 inv = 1 record = [[0 for i in range(size+1)] for i in range(2)] record[0][0:4]=[1,2,2,1] for m in range(4,n): tp_size = m*(m-1)//2 ans = (m+1) * last_ans tp = 0 for i in range(m): tp += record[inv^1][i] record[inv][i] = tp%mod for i in range(m,tp_size//2+1): tp += record[inv^1][i] tp -= record[inv^1][i-m] record[inv][i] = tp%mod for i in range(tp_size//2+1): record[inv][tp_size-i] = record[inv][i] # print("ans:",ans) total = 0 for i in range(tp_size-1): tp = 0 for j in range(max(1,-tp_size+i+m+2),m+1): # print("(",j,m+2-j+i,")",end=" ") tp += j * int(record[inv][m+2-j+i]) # print() total += int(record[inv][i]) total %= mod tp *= total tp %= mod ans += tp ans %= mod last_ans = ans # print(record) inv ^= 1 return int(ans)%mod def main(): n, mod = map(int, input().split()) print(solve(n, mod)) if __name__ == "__main__": main() ```
output
1
64,233
12
128,467
Provide tags and a correct Python 3 solution for this coding contest problem. This is the easy version of the problem. The only difference between the easy version and the hard version is the constraints on n. You can only make hacks if both versions are solved. A permutation of 1, 2, …, n is a sequence of n integers, where each integer from 1 to n appears exactly once. For example, [2,3,1,4] is a permutation of 1, 2, 3, 4, but [1,4,2,2] isn't because 2 appears twice in it. Recall that the number of inversions in a permutation a_1, a_2, …, a_n is the number of pairs of indices (i, j) such that i < j and a_i > a_j. Let p and q be two permutations of 1, 2, …, n. Find the number of permutation pairs (p,q) that satisfy the following conditions: * p is lexicographically smaller than q. * the number of inversions in p is greater than the number of inversions in q. Print the number of such pairs modulo mod. Note that mod may not be a prime. Input The only line contains two integers n and mod (1≀ n≀ 50, 1≀ mod≀ 10^9). Output Print one integer, which is the answer modulo mod. Example Input 4 403458273 Output 17 Note The following are all valid pairs (p,q) when n=4. * p=[1,3,4,2], q=[2,1,3,4], * p=[1,4,2,3], q=[2,1,3,4], * p=[1,4,3,2], q=[2,1,3,4], * p=[1,4,3,2], q=[2,1,4,3], * p=[1,4,3,2], q=[2,3,1,4], * p=[1,4,3,2], q=[3,1,2,4], * p=[2,3,4,1], q=[3,1,2,4], * p=[2,4,1,3], q=[3,1,2,4], * p=[2,4,3,1], q=[3,1,2,4], * p=[2,4,3,1], q=[3,1,4,2], * p=[2,4,3,1], q=[3,2,1,4], * p=[2,4,3,1], q=[4,1,2,3], * p=[3,2,4,1], q=[4,1,2,3], * p=[3,4,1,2], q=[4,1,2,3], * p=[3,4,2,1], q=[4,1,2,3], * p=[3,4,2,1], q=[4,1,3,2], * p=[3,4,2,1], q=[4,2,1,3].
instruction
0
64,234
12
128,468
Tags: combinatorics, dp, fft, math Correct Solution: ``` def divisors(M): d=[] i=1 while M>=i**2: if M%i==0: d.append(i) if i**2!=M: d.append(M//i) i=i+1 return d def popcount(x): x = x - ((x >> 1) & 0x55555555) x = (x & 0x33333333) + ((x >> 2) & 0x33333333) x = (x + (x >> 4)) & 0x0f0f0f0f x = x + (x >> 8) x = x + (x >> 16) return x & 0x0000007f def eratosthenes(n): res=[0 for i in range(n+1)] prime=set([]) for i in range(2,n+1): if not res[i]: prime.add(i) for j in range(1,n//i+1): res[i*j]=1 return prime def factorization(n): res=[] for p in prime: if n%p==0: while n%p==0: n//=p res.append(p) if n!=1: res.append(n) return res def euler_phi(n): res = n for x in range(2,n+1): if x ** 2 > n: break if n%x==0: res = res//x * (x-1) while n%x==0: n //= x if n!=1: res = res//n * (n-1) return res def ind(b,n): res=0 while n%b==0: res+=1 n//=b return res def isPrimeMR(n): d = n - 1 d = d // (d & -d) L = [2, 3, 5, 7, 11, 13, 17] for a in L: t = d y = pow(a, t, n) if y == 1: continue while y != n - 1: y = (y * y) % n if y == 1 or t == n - 1: return 0 t <<= 1 return 1 def findFactorRho(n): from math import gcd m = 1 << n.bit_length() // 8 for c in range(1, 99): f = lambda x: (x * x + c) % n y, r, q, g = 2, 1, 1, 1 while g == 1: x = y for i in range(r): y = f(y) k = 0 while k < r and g == 1: ys = y for i in range(min(m, r - k)): y = f(y) q = q * abs(x - y) % n g = gcd(q, n) k += m r <<= 1 if g == n: g = 1 while g == 1: ys = f(ys) g = gcd(abs(x - ys), n) if g < n: if isPrimeMR(g): return g elif isPrimeMR(n // g): return n // g return findFactorRho(g) def primeFactor(n): i = 2 ret = {} rhoFlg = 0 while i*i <= n: k = 0 while n % i == 0: n //= i k += 1 if k: ret[i] = k i += 1 + i % 2 if i == 101 and n >= 2 ** 20: while n > 1: if isPrimeMR(n): ret[n], n = 1, 1 else: rhoFlg = 1 j = findFactorRho(n) k = 0 while n % j == 0: n //= j k += 1 ret[j] = k if n > 1: ret[n] = 1 if rhoFlg: ret = {x: ret[x] for x in sorted(ret)} return ret def divisors(n): res = [1] prime = primeFactor(n) for p in prime: newres = [] for d in res: for j in range(prime[p]+1): newres.append(d*p**j) res = newres res.sort() return res def xorfactorial(num): if num==0: return 0 elif num==1: return 1 elif num==2: return 3 elif num==3: return 0 else: x=baseorder(num) return (2**x)*((num-2**x+1)%2)+function(num-2**x) def xorconv(n,X,Y): if n==0: res=[(X[0]*Y[0])%mod] return res x=[X[i]+X[i+2**(n-1)] for i in range(2**(n-1))] y=[Y[i]+Y[i+2**(n-1)] for i in range(2**(n-1))] z=[X[i]-X[i+2**(n-1)] for i in range(2**(n-1))] w=[Y[i]-Y[i+2**(n-1)] for i in range(2**(n-1))] res1=xorconv(n-1,x,y) res2=xorconv(n-1,z,w) former=[(res1[i]+res2[i])*inv for i in range(2**(n-1))] latter=[(res1[i]-res2[i])*inv for i in range(2**(n-1))] former=list(map(lambda x:x%mod,former)) latter=list(map(lambda x:x%mod,latter)) return former+latter def merge_sort(A,B): pos_A,pos_B = 0,0 n,m = len(A),len(B) res = [] while pos_A < n and pos_B < m: a,b = A[pos_A],B[pos_B] if a < b: res.append(a) pos_A += 1 else: res.append(b) pos_B += 1 res += A[pos_A:] res += B[pos_B:] return res class UnionFindVerSize(): def __init__(self, N): self._parent = [n for n in range(0, N)] self._size = [1] * N self.group = N def find_root(self, x): if self._parent[x] == x: return x self._parent[x] = self.find_root(self._parent[x]) stack = [x] while self._parent[stack[-1]]!=stack[-1]: stack.append(self._parent[stack[-1]]) for v in stack: self._parent[v] = stack[-1] return self._parent[x] def unite(self, x, y): gx = self.find_root(x) gy = self.find_root(y) if gx == gy: return self.group -= 1 if self._size[gx] < self._size[gy]: self._parent[gx] = gy self._size[gy] += self._size[gx] else: self._parent[gy] = gx self._size[gx] += self._size[gy] def get_size(self, x): return self._size[self.find_root(x)] def is_same_group(self, x, y): return self.find_root(x) == self.find_root(y) class WeightedUnionFind(): def __init__(self,N): self.parent = [i for i in range(N)] self.size = [1 for i in range(N)] self.val = [0 for i in range(N)] self.flag = True self.edge = [[] for i in range(N)] def dfs(self,v,pv): stack = [(v,pv)] new_parent = self.parent[pv] while stack: v,pv = stack.pop() self.parent[v] = new_parent for nv,w in self.edge[v]: if nv!=pv: self.val[nv] = self.val[v] + w stack.append((nv,v)) def unite(self,x,y,w): if not self.flag: return if self.parent[x]==self.parent[y]: self.flag = (self.val[x] - self.val[y] == w) return if self.size[self.parent[x]]>self.size[self.parent[y]]: self.edge[x].append((y,-w)) self.edge[y].append((x,w)) self.size[x] += self.size[y] self.val[y] = self.val[x] - w self.dfs(y,x) else: self.edge[x].append((y,-w)) self.edge[y].append((x,w)) self.size[y] += self.size[x] self.val[x] = self.val[y] + w self.dfs(x,y) class Dijkstra(): class Edge(): def __init__(self, _to, _cost): self.to = _to self.cost = _cost def __init__(self, V): self.G = [[] for i in range(V)] self._E = 0 self._V = V @property def E(self): return self._E @property def V(self): return self._V def add_edge(self, _from, _to, _cost): self.G[_from].append(self.Edge(_to, _cost)) self._E += 1 def shortest_path(self, s): import heapq que = [] d = [10**15] * self.V d[s] = 0 heapq.heappush(que, (0, s)) while len(que) != 0: cost, v = heapq.heappop(que) if d[v] < cost: continue for i in range(len(self.G[v])): e = self.G[v][i] if d[e.to] > d[v] + e.cost: d[e.to] = d[v] + e.cost heapq.heappush(que, (d[e.to], e.to)) return d #Z[i]:length of the longest list starting from S[i] which is also a prefix of S #O(|S|) def Z_algorithm(s): N = len(s) Z_alg = [0]*N Z_alg[0] = N i = 1 j = 0 while i < N: while i+j < N and s[j] == s[i+j]: j += 1 Z_alg[i] = j if j == 0: i += 1 continue k = 1 while i+k < N and k + Z_alg[k]<j: Z_alg[i+k] = Z_alg[k] k += 1 i += k j -= k return Z_alg class BIT(): def __init__(self,n,mod=0): self.BIT = [0]*(n+1) self.num = n self.mod = mod def query(self,idx): res_sum = 0 mod = self.mod while idx > 0: res_sum += self.BIT[idx] if mod: res_sum %= mod idx -= idx&(-idx) return res_sum #Ai += x O(logN) def update(self,idx,x): mod = self.mod while idx <= self.num: self.BIT[idx] += x if mod: self.BIT[idx] %= mod idx += idx&(-idx) return class dancinglink(): def __init__(self,n,debug=False): self.n = n self.debug = debug self._left = [i-1 for i in range(n)] self._right = [i+1 for i in range(n)] self.exist = [True for i in range(n)] def pop(self,k): if self.debug: assert self.exist[k] L = self._left[k] R = self._right[k] if L!=-1: if R!=self.n: self._right[L],self._left[R] = R,L else: self._right[L] = self.n elif R!=self.n: self._left[R] = -1 self.exist[k] = False def left(self,idx,k=1): if self.debug: assert self.exist[idx] res = idx while k: res = self._left[res] if res==-1: break k -= 1 return res def right(self,idx,k=1): if self.debug: assert self.exist[idx] res = idx while k: res = self._right[res] if res==self.n: break k -= 1 return res class SparseTable(): def __init__(self,A,merge_func,ide_ele): N=len(A) n=N.bit_length() self.table=[[ide_ele for i in range(n)] for i in range(N)] self.merge_func=merge_func for i in range(N): self.table[i][0]=A[i] for j in range(1,n): for i in range(0,N-2**j+1): f=self.table[i][j-1] s=self.table[i+2**(j-1)][j-1] self.table[i][j]=self.merge_func(f,s) def query(self,s,t): b=t-s+1 m=b.bit_length()-1 return self.merge_func(self.table[s][m],self.table[t-2**m+1][m]) class BinaryTrie: class node: def __init__(self,val): self.left = None self.right = None self.max = val def __init__(self): self.root = self.node(-10**15) def append(self,key,val): pos = self.root for i in range(29,-1,-1): pos.max = max(pos.max,val) if key>>i & 1: if pos.right is None: pos.right = self.node(val) pos = pos.right else: pos = pos.right else: if pos.left is None: pos.left = self.node(val) pos = pos.left else: pos = pos.left pos.max = max(pos.max,val) def search(self,M,xor): res = -10**15 pos = self.root for i in range(29,-1,-1): if pos is None: break if M>>i & 1: if xor>>i & 1: if pos.right: res = max(res,pos.right.max) pos = pos.left else: if pos.left: res = max(res,pos.left.max) pos = pos.right else: if xor>>i & 1: pos = pos.right else: pos = pos.left if pos: res = max(res,pos.max) return res def solveequation(edge,ans,n,m): #edge=[[to,dire,id]...] x=[0]*m used=[False]*n for v in range(n): if used[v]: continue y = dfs(v) if y!=0: return False return x def dfs(v): used[v]=True r=ans[v] for to,dire,id in edge[v]: if used[to]: continue y=dfs(to) if dire==-1: x[id]=y else: x[id]=-y r+=y return r class SegmentTree: def __init__(self, init_val, segfunc, ide_ele): n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num self.size = n for i in range(n): self.tree[self.num + i] = init_val[i] for i in range(self.num - 1, 0, -1): self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): k += self.num self.tree[k] = x while k > 1: self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]) k >>= 1 def query(self, l, r): if r==self.size: r = self.num res = self.ide_ele l += self.num r += self.num while l < r: if l & 1: res = self.segfunc(res, self.tree[l]) l += 1 if r & 1: res = self.segfunc(res, self.tree[r - 1]) l >>= 1 r >>= 1 return res def bisect_l(self,l,r,x): l += self.num r += self.num Lmin = -1 Rmin = -1 while l<r: if l & 1: if self.tree[l] <= x and Lmin==-1: Lmin = l l += 1 if r & 1: if self.tree[r-1] <=x: Rmin = r-1 l >>= 1 r >>= 1 if Lmin != -1: pos = Lmin while pos<self.num: if self.tree[2 * pos] <=x: pos = 2 * pos else: pos = 2 * pos +1 return pos-self.num elif Rmin != -1: pos = Rmin while pos<self.num: if self.tree[2 * pos] <=x: pos = 2 * pos else: pos = 2 * pos +1 return pos-self.num else: return -1 import sys,random,bisect from collections import deque,defaultdict from heapq import heapify,heappop,heappush from itertools import permutations from math import gcd,log input = lambda :sys.stdin.readline().rstrip() mi = lambda :map(int,input().split()) li = lambda :list(mi()) N,mod = mi() g = [1 for i in range(N+1)] for i in range(1,N+1): g[i] = g[i-1] * i % mod COMB = [[0 for j in range(N+1)] for i in range(N+1)] COMB[0][0] = 1 for i in range(1,N+1): COMB[i][0] = 1 for j in range(1,i+1): COMB[i][j] = COMB[i-1][j] + COMB[i-1][j-1] COMB[i][j] %= mod dp = [[0 for j in range(N*(N-1)//2+1)] for i in range(N+1)] dp[1][0] = 1 for i in range(2,N+1): for k in range(N*(N-1)//2+1): if not dp[i-1][k]: continue dp[i][k] += dp[i-1][k] dp[i][k] %= mod if k+i <= N*(N-1)//2: dp[i][k+i] -= dp[i-1][k] dp[i][k+i] %= mod for k in range(1,N*(N-1)//2+1): dp[i][k] += dp[i][k-1] dp[i][k] %= mod imos = [[dp[i][j] for j in range(N*(N-1)//2+1)] for i in range(N+1)] for i in range(1,N+1): for j in range(1,N*(N-1)//2+1): imos[i][j] += imos[i][j-1] imos[i][j] %= mod res = 0 for i in range(1,N): cnt = N - i inverse = [0 for inv in range(-(N-i+1),0)] for j in range(1,N-i+1): for k in range(j+1,N-i+2): inv = j-k inverse[inv] += 1 inverse[inv] %= mod for inv in range(-(N-i+1),0): for ip in range(cnt*(cnt-1)//2+1): if ip+inv <= 0: continue R = ip+inv-1 res += (COMB[N][i-1] * dp[cnt][ip] % mod) * (imos[cnt][R] * inverse[inv] % mod) * g[i-1] % mod res %= mod #for iq in range(cnt*(cnt-1)//2+1): #if ip-iq+inverse > 0: #res += COMB[N][i-1] * dp[cnt][ip] * dp[cnt][iq] #res %= mod print(res) def inversion(p): res = 0 for i in range(N): for j in range(i+1,N): if p[i] > p[j]: res += 1 return res def solve(N): L = list(permutations([i+1 for i in range(N)])) res = 0 check = [0 for i in range(N+1)] for p in L: for q in L: if p < q and inversion(p) > inversion(q): res += 1 for i in range(N): if p[i] < q[i]: check[i+1] += 1 break for i in range(1,N+1): print(i,check[i]) return res ```
output
1
64,234
12
128,469