message
stringlengths
2
433k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
113
108k
cluster
float64
12
12
__index_level_0__
int64
226
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: <image>. The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence. You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≀ l < r ≀ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r]. Note that as all numbers in sequence s are distinct, all the given definitions make sence. Input The first line contains integer n (1 < n ≀ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≀ si ≀ 109). Output Print a single integer β€” the maximum lucky number among all lucky numbers of sequences s[l..r]. Examples Input 5 5 2 1 4 3 Output 7 Input 5 9 8 3 5 7 Output 15 Note For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2]. For the second sample you must choose s[2..5] = {8, 3, 5, 7}. Submitted Solution: ``` n = int(input()) s = list(map(int, input().split())) res = [] for i in range(n): mx = max(s) s.remove(mx) if s: sec_mx = max(s) else: break res.append(mx ^ sec_mx) print(max(res)) ```
instruction
0
63,436
12
126,872
No
output
1
63,436
12
126,873
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: <image>. The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence. You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≀ l < r ≀ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r]. Note that as all numbers in sequence s are distinct, all the given definitions make sence. Input The first line contains integer n (1 < n ≀ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≀ si ≀ 109). Output Print a single integer β€” the maximum lucky number among all lucky numbers of sequences s[l..r]. Examples Input 5 5 2 1 4 3 Output 7 Input 5 9 8 3 5 7 Output 15 Note For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2]. For the second sample you must choose s[2..5] = {8, 3, 5, 7}. Submitted Solution: ``` n = int(input()) arr = list(map(int, input().split())) arr = sorted(arr,reverse = True) max_val = arr[0] second_max_val = arr[1] index = 1 ans = 0 while index < n: second_max_val = arr[index] ans = max(ans,max_val ^ second_max_val) index += 1 max_val = arr[1] index = 2 while index < n: second_max_val = arr[index] ans = max(ans,max_val ^ second_max_val) index += 1 print(ans) ```
instruction
0
63,437
12
126,874
No
output
1
63,437
12
126,875
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: <image>. The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence. You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≀ l < r ≀ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r]. Note that as all numbers in sequence s are distinct, all the given definitions make sence. Input The first line contains integer n (1 < n ≀ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≀ si ≀ 109). Output Print a single integer β€” the maximum lucky number among all lucky numbers of sequences s[l..r]. Examples Input 5 5 2 1 4 3 Output 7 Input 5 9 8 3 5 7 Output 15 Note For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2]. For the second sample you must choose s[2..5] = {8, 3, 5, 7}. Submitted Solution: ``` def f(l): mx1=0 mx2=0 xr=-1 st=set() st.add((mx1,mx2)) for i in l: if i>=mx1: mx2=mx1 mx1=i xr=max(xr,mx1^mx2) elif i>=mx2: mx2=i xr=max(xr,mx2^mx1) if (mx1,mx2) in st: mx1=mx2 mx2=i xr=max(xr,mx1^mx2) st.add((mx1,mx2)) return xr a=map(int,input().strip().split()) l=list(map(int,input().strip().split())) print(f(l)) ```
instruction
0
63,438
12
126,876
No
output
1
63,438
12
126,877
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: <image>. The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence. You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≀ l < r ≀ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r]. Note that as all numbers in sequence s are distinct, all the given definitions make sence. Input The first line contains integer n (1 < n ≀ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≀ si ≀ 109). Output Print a single integer β€” the maximum lucky number among all lucky numbers of sequences s[l..r]. Examples Input 5 5 2 1 4 3 Output 7 Input 5 9 8 3 5 7 Output 15 Note For the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2]. For the second sample you must choose s[2..5] = {8, 3, 5, 7}. Submitted Solution: ``` #https://codeforces.com/problemset/problem/281/D def empty(l): return len(l) == 0 def lucky_number(arr): st = [] l = 0 for d in arr: while not empty(st) and st[-1] < d: l = max(l, st.pop() ^ d) if not empty(st): l = max(l, st[-1] ^ d) st.append(d) # print(st) # print(l) # print(st) d = 0 if not empty(st): d = st.pop() while not empty(st): l = max(l, d ^ st.pop()) return l N = int(input()) arr = list(map(int, input().split())) print(lucky_number(arr)) # for i in range(len(arr)): # for j in range(i + 1, len(arr)): # print(arr[i], arr[j], arr[i]^arr[j]) ```
instruction
0
63,439
12
126,878
No
output
1
63,439
12
126,879
Provide tags and a correct Python 3 solution for this coding contest problem. Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not. A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal. Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him? Input The first line contains a single integer n (1 ≀ n ≀ 105). Output If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them. Examples Input 5 Output 1 4 3 2 0 1 0 2 4 3 2 4 0 1 3 Input 2 Output -1 Note In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: * <image>; * <image>; * <image>; * <image>; * <image>. In Sample 2, you can easily notice that no lucky permutation triple exists.
instruction
0
63,440
12
126,880
Tags: constructive algorithms, implementation, math Correct Solution: ``` n=int(input()) a,b=[],[] c=[] if(n%2!=0): for i in range(n): a+=[i] b+=[i] c.append((a[i]+b[i])%n) print(*a) print(*b) print(*c) else: print(-1) ```
output
1
63,440
12
126,881
Provide tags and a correct Python 3 solution for this coding contest problem. Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not. A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal. Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him? Input The first line contains a single integer n (1 ≀ n ≀ 105). Output If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them. Examples Input 5 Output 1 4 3 2 0 1 0 2 4 3 2 4 0 1 3 Input 2 Output -1 Note In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: * <image>; * <image>; * <image>; * <image>; * <image>. In Sample 2, you can easily notice that no lucky permutation triple exists.
instruction
0
63,441
12
126,882
Tags: constructive algorithms, implementation, math Correct Solution: ``` n = int(input()) if n % 2 == 0: print(-1) else: print(*list(range(n))) print(*list(range(n))) print(*[i*2 % n for i in range(n)]) ```
output
1
63,441
12
126,883
Provide tags and a correct Python 3 solution for this coding contest problem. Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not. A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal. Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him? Input The first line contains a single integer n (1 ≀ n ≀ 105). Output If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them. Examples Input 5 Output 1 4 3 2 0 1 0 2 4 3 2 4 0 1 3 Input 2 Output -1 Note In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: * <image>; * <image>; * <image>; * <image>; * <image>. In Sample 2, you can easily notice that no lucky permutation triple exists.
instruction
0
63,442
12
126,884
Tags: constructive algorithms, implementation, math Correct Solution: ``` n=int(input()) a=[i for i in range(n)] b=[i for i in range(n)] c=[] i=0 if(n%2==0): print(-1) else: i=0 while(i<n): c.append(i) i+=2 i=1 while(i<n): c.append(i) i+=2 for i in range(n): c.append((a[i]+b[i])%n) for i in range(n): print(a[i],end=' ') print() for i in range(n): print(b[i],end=' ') print() for i in range(n): print(c[i],end=' ') ```
output
1
63,442
12
126,885
Provide tags and a correct Python 3 solution for this coding contest problem. Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not. A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal. Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him? Input The first line contains a single integer n (1 ≀ n ≀ 105). Output If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them. Examples Input 5 Output 1 4 3 2 0 1 0 2 4 3 2 4 0 1 3 Input 2 Output -1 Note In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: * <image>; * <image>; * <image>; * <image>; * <image>. In Sample 2, you can easily notice that no lucky permutation triple exists.
instruction
0
63,443
12
126,886
Tags: constructive algorithms, implementation, math Correct Solution: ``` n=int(input()); if n%2: print(*[int(i) for i in range(0,n)]) print(*[int(j) for j in range(0,n)]) print(*[int((2*i)%n) for i in range(0,n)]) else: print(-1) ```
output
1
63,443
12
126,887
Provide tags and a correct Python 3 solution for this coding contest problem. Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not. A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal. Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him? Input The first line contains a single integer n (1 ≀ n ≀ 105). Output If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them. Examples Input 5 Output 1 4 3 2 0 1 0 2 4 3 2 4 0 1 3 Input 2 Output -1 Note In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: * <image>; * <image>; * <image>; * <image>; * <image>. In Sample 2, you can easily notice that no lucky permutation triple exists.
instruction
0
63,444
12
126,888
Tags: constructive algorithms, implementation, math Correct Solution: ``` n = int(input()) if n % 2 == 0: print(-1) else: print(" ".join(map(str, range(n)))) print(" ".join(map(str, range(n)))) print(" ".join(map(str, [2*x % n for x in range(n)]))) ```
output
1
63,444
12
126,889
Provide tags and a correct Python 3 solution for this coding contest problem. Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not. A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal. Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him? Input The first line contains a single integer n (1 ≀ n ≀ 105). Output If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them. Examples Input 5 Output 1 4 3 2 0 1 0 2 4 3 2 4 0 1 3 Input 2 Output -1 Note In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: * <image>; * <image>; * <image>; * <image>; * <image>. In Sample 2, you can easily notice that no lucky permutation triple exists.
instruction
0
63,445
12
126,890
Tags: constructive algorithms, implementation, math Correct Solution: ``` if __name__ == '__main__': n = int(input()) if n == 1: print(0) print(0) print(0) elif n % 2: perm1, perm2 = [], [] for i in range(n//2 + 1, -1, -1): perm1.append(i) for i in range(n-1, n // 2 + 1, -1): perm1.append(i) for i in range(n): if i < perm1[i]: perm2.append(n + i - perm1[i]) elif i == perm1[i]: perm2.append(0) else: perm2.append(i-perm1[i]) print(' '.join(map(str, perm1))) print(' '.join(map(str, perm2))) print(' '.join(str(i) for i in range(n))) else: print(-1) ```
output
1
63,445
12
126,891
Provide tags and a correct Python 3 solution for this coding contest problem. Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not. A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal. Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him? Input The first line contains a single integer n (1 ≀ n ≀ 105). Output If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them. Examples Input 5 Output 1 4 3 2 0 1 0 2 4 3 2 4 0 1 3 Input 2 Output -1 Note In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: * <image>; * <image>; * <image>; * <image>; * <image>. In Sample 2, you can easily notice that no lucky permutation triple exists.
instruction
0
63,446
12
126,892
Tags: constructive algorithms, implementation, math Correct Solution: ``` # *****DO NOT COPY***** # F # U ___ ___ ___ ___ ___ ___ _____ # C / /\ ___ / /\ ___ /__/\ / /\ / /\ / /\ / /::\ # K / /::\ / /\ / /:/_ / /\ \ \:\ / /::\ / /::\ / /:/_ / /:/\:\ # / /:/\:\ / /:/ / /:/ /\ / /::\ \ \:\ / /:/\:\ / /:/\:\ / /:/ /\ / /:/ \:\ # Y / /:/~/:/ /__/::\ / /:/ /::\ / /:/\:\ ___ \ \:\ / /:/~/::\ / /:/~/:/ / /:/ /:/_ /__/:/ \__\:| # O /__/:/ /:/ \__\/\:\__ /__/:/ /:/\:\ / /:/~/::\ /__/\ \__\:\ /__/:/ /:/\:\ /__/:/ /:/___ /__/:/ /:/ /\ \ \:\ / /:/ # U \ \:\/:/ \ \:\/\ \ \:\/:/~/:/ /__/:/ /:/\:\ \ \:\ / /:/ \ \:\/:/__\/ \ \:\/:::::/ \ \:\/:/ /:/ \ \:\ /:/ # \ \::/ \__\::/ \ \::/ /:/ \ \:\/:/__\/ \ \:\ /:/ \ \::/ \ \::/~~~~ \ \::/ /:/ \ \:\/:/ # A \ \:\ /__/:/ \__\/ /:/ \ \::/ \ \:\/:/ \ \:\ \ \:\ \ \:\/:/ \ \::/ # L \ \:\ \__\/ /__/:/ \__\/ \ \::/ \ \:\ \ \:\ \ \::/ \__\/ # L \__\/ \__\/ \__\/ \__\/ \__\/ \__\/ # # # *****DO NOT COPY****** n = int(input()) if (n%2 == 0): print(-1) exit() a = list(range(n)) b = a c = [] for i in range(n): c.append((a[i] + b[i])%n) a = map(str , a) b = map(str , b) c = map(str , c) print(" ".join(a)) print(" ".join(b)) print(" ".join(c)) ```
output
1
63,446
12
126,893
Provide tags and a correct Python 3 solution for this coding contest problem. Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not. A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal. Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him? Input The first line contains a single integer n (1 ≀ n ≀ 105). Output If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them. Examples Input 5 Output 1 4 3 2 0 1 0 2 4 3 2 4 0 1 3 Input 2 Output -1 Note In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: * <image>; * <image>; * <image>; * <image>; * <image>. In Sample 2, you can easily notice that no lucky permutation triple exists.
instruction
0
63,447
12
126,894
Tags: constructive algorithms, implementation, math Correct Solution: ``` import os,io from sys import stdout input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline import collections def binomial_coefficient(n, k): if 0 <= k <= n: ntok = 1 ktok = 1 for t in range(1, min(k, n - k) + 1): ntok *= n ktok *= t n -= 1 return ntok // ktok else: return 0 n = int(input()) if n % 2 == 0: print(-1) else: p1 = [] p2 = [] p3 = [] for i in range(n): p1.append(i) p2.append((i+1)%n) p3.append((i+(i+1)) % n) print(" ".join([str(e) for e in p1])) print(" ".join([str(e) for e in p2])) print(" ".join([str(e) for e in p3])) ```
output
1
63,447
12
126,895
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not. A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal. Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him? Input The first line contains a single integer n (1 ≀ n ≀ 105). Output If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them. Examples Input 5 Output 1 4 3 2 0 1 0 2 4 3 2 4 0 1 3 Input 2 Output -1 Note In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: * <image>; * <image>; * <image>; * <image>; * <image>. In Sample 2, you can easily notice that no lucky permutation triple exists. Submitted Solution: ``` n = int(input()) if(n%2 == 0): print(-1) else: for i in range(n): print(i, end = " ") print('\n') for i in range(n): print(i, end = " ") print("\n") for i in range(n): print(((i+i)%n), end = " ") ```
instruction
0
63,448
12
126,896
Yes
output
1
63,448
12
126,897
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not. A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal. Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him? Input The first line contains a single integer n (1 ≀ n ≀ 105). Output If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them. Examples Input 5 Output 1 4 3 2 0 1 0 2 4 3 2 4 0 1 3 Input 2 Output -1 Note In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: * <image>; * <image>; * <image>; * <image>; * <image>. In Sample 2, you can easily notice that no lucky permutation triple exists. Submitted Solution: ``` from itertools import zip_longest n = int(input()) a = list(range(n)) b = list(range(n)) c = [] for (i,j) in zip_longest(a,b): c.append((i+j)%n) if len(c) != len(set(c)): print(-1) else: print(*a) print(*b) print(*c) ```
instruction
0
63,449
12
126,898
Yes
output
1
63,449
12
126,899
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not. A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal. Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him? Input The first line contains a single integer n (1 ≀ n ≀ 105). Output If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them. Examples Input 5 Output 1 4 3 2 0 1 0 2 4 3 2 4 0 1 3 Input 2 Output -1 Note In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: * <image>; * <image>; * <image>; * <image>; * <image>. In Sample 2, you can easily notice that no lucky permutation triple exists. Submitted Solution: ``` n=int(input()) if n%2==0: print(-1) elif n==1: print(0) print(0) print(0) else: p=[0]*n q=[0]*n w=[0]*n t=int(n/2) for i in range (0,n): p[i]=i if i<t: q[i]=n-2-2*i else: q[i]=4*t-2*i if i<=n-2: w[i]=n-2-i else: w[i]=n-1 for i in range (0,n): if i!=n-1: print(p[i],end=' ') else: print(p[i]) for i in range (0,n): if i!=n-1: print(q[i],end=' ') else: print(q[i]) for i in range (0,n): if i!=n-1: print(w[i],end=' ') else: print(w[i]) ```
instruction
0
63,450
12
126,900
Yes
output
1
63,450
12
126,901
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not. A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal. Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him? Input The first line contains a single integer n (1 ≀ n ≀ 105). Output If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them. Examples Input 5 Output 1 4 3 2 0 1 0 2 4 3 2 4 0 1 3 Input 2 Output -1 Note In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: * <image>; * <image>; * <image>; * <image>; * <image>. In Sample 2, you can easily notice that no lucky permutation triple exists. Submitted Solution: ``` n=int(input()) if n%2==0: print("-1") else: for i in range(n-2,-1,-1): print(i,end=" ") print(n-1) for i in range(2,n,2): print(i,end=" ") for i in range(1,n,2): print(i,end=" ") print("0") for i in range(n): print(i,end=" ") ```
instruction
0
63,451
12
126,902
Yes
output
1
63,451
12
126,903
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not. A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal. Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him? Input The first line contains a single integer n (1 ≀ n ≀ 105). Output If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them. Examples Input 5 Output 1 4 3 2 0 1 0 2 4 3 2 4 0 1 3 Input 2 Output -1 Note In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: * <image>; * <image>; * <image>; * <image>; * <image>. In Sample 2, you can easily notice that no lucky permutation triple exists. Submitted Solution: ``` # *****DO NOT COPY***** # F # U ___ ___ ___ ___ ___ ___ _____ # C / /\ ___ / /\ ___ /__/\ / /\ / /\ / /\ / /::\ # K / /::\ / /\ / /:/_ / /\ \ \:\ / /::\ / /::\ / /:/_ / /:/\:\ # / /:/\:\ / /:/ / /:/ /\ / /::\ \ \:\ / /:/\:\ / /:/\:\ / /:/ /\ / /:/ \:\ # Y / /:/~/:/ /__/::\ / /:/ /::\ / /:/\:\ ___ \ \:\ / /:/~/::\ / /:/~/:/ / /:/ /:/_ /__/:/ \__\:| # O /__/:/ /:/ \__\/\:\__ /__/:/ /:/\:\ / /:/~/::\ /__/\ \__\:\ /__/:/ /:/\:\ /__/:/ /:/___ /__/:/ /:/ /\ \ \:\ / /:/ # U \ \:\/:/ \ \:\/\ \ \:\/:/~/:/ /__/:/ /:/\:\ \ \:\ / /:/ \ \:\/:/__\/ \ \:\/:::::/ \ \:\/:/ /:/ \ \:\ /:/ # \ \::/ \__\::/ \ \::/ /:/ \ \:\/:/__\/ \ \:\ /:/ \ \::/ \ \::/~~~~ \ \::/ /:/ \ \:\/:/ # A \ \:\ /__/:/ \__\/ /:/ \ \::/ \ \:\/:/ \ \:\ \ \:\ \ \:\/:/ \ \::/ # L \ \:\ \__\/ /__/:/ \__\/ \ \::/ \ \:\ \ \:\ \ \::/ \__\/ # L \__\/ \__\/ \__\/ \__\/ \__\/ \__\/ # # # *****DO NOT COPY****** n = int(input()) a = list(range(n)) b = a c = [] for i in range(n): c.append((a[i] + b[i])%n) a = map(str , a) b = map(str , b) c = map(str , c) print(" ".join(a)) print(" ".join(b)) print(" ".join(c)) ```
instruction
0
63,452
12
126,904
No
output
1
63,452
12
126,905
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not. A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal. Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him? Input The first line contains a single integer n (1 ≀ n ≀ 105). Output If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them. Examples Input 5 Output 1 4 3 2 0 1 0 2 4 3 2 4 0 1 3 Input 2 Output -1 Note In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: * <image>; * <image>; * <image>; * <image>; * <image>. In Sample 2, you can easily notice that no lucky permutation triple exists. Submitted Solution: ``` import math import sys def In(): return map(int, sys.stdin.readline().split()) def luckyperm(): n = int(input()) if n == 2: return -1 other = [] l = [] for i in range(n): l.append(i) other.append((i+i)%n) print(*l) print(*l) print(*other) luckyperm() ```
instruction
0
63,453
12
126,906
No
output
1
63,453
12
126,907
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not. A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal. Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him? Input The first line contains a single integer n (1 ≀ n ≀ 105). Output If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them. Examples Input 5 Output 1 4 3 2 0 1 0 2 4 3 2 4 0 1 3 Input 2 Output -1 Note In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: * <image>; * <image>; * <image>; * <image>; * <image>. In Sample 2, you can easily notice that no lucky permutation triple exists. Submitted Solution: ``` n = int(input()) if(n%2==0): print(-1) else: for i in range(n): print(i,end=" ") print() for i in range(n-1,-1,-2): print(i,end=" ") for i in range(n-2,-1,-2): print(i,end=" ") ```
instruction
0
63,454
12
126,908
No
output
1
63,454
12
126,909
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Bike is interested in permutations. A permutation of length n is an integer sequence such that each integer from 0 to (n - 1) appears exactly once in it. For example, [0, 2, 1] is a permutation of length 3 while both [0, 2, 2] and [1, 2, 3] is not. A permutation triple of permutations of length n (a, b, c) is called a Lucky Permutation Triple if and only if <image>. The sign ai denotes the i-th element of permutation a. The modular equality described above denotes that the remainders after dividing ai + bi by n and dividing ci by n are equal. Now, he has an integer n and wants to find a Lucky Permutation Triple. Could you please help him? Input The first line contains a single integer n (1 ≀ n ≀ 105). Output If no Lucky Permutation Triple of length n exists print -1. Otherwise, you need to print three lines. Each line contains n space-seperated integers. The first line must contain permutation a, the second line β€” permutation b, the third β€” permutation c. If there are multiple solutions, print any of them. Examples Input 5 Output 1 4 3 2 0 1 0 2 4 3 2 4 0 1 3 Input 2 Output -1 Note In Sample 1, the permutation triple ([1, 4, 3, 2, 0], [1, 0, 2, 4, 3], [2, 4, 0, 1, 3]) is Lucky Permutation Triple, as following holds: * <image>; * <image>; * <image>; * <image>; * <image>. In Sample 2, you can easily notice that no lucky permutation triple exists. Submitted Solution: ``` import random t=1 for _ in range(t): n=int(input()) if(n==1 or n==2): print(-1) else: a=[i for i in range(n)] b=a[1:]+a[0:1] c=[] for i in range(n): c.append((a[i]+b[i])%n) for i in a: print(i, end=' ') print() for i in b: print(i, end=' ') print() for i in c: print(i, end=' ') ```
instruction
0
63,455
12
126,910
No
output
1
63,455
12
126,911
Provide tags and a correct Python 3 solution for this coding contest problem. One day Jeff got hold of an integer sequence a1, a2, ..., an of length n. The boy immediately decided to analyze the sequence. For that, he needs to find all values of x, for which these conditions hold: * x occurs in sequence a. * Consider all positions of numbers x in the sequence a (such i, that ai = x). These numbers, sorted in the increasing order, must form an arithmetic progression. Help Jeff, find all x that meet the problem conditions. Input The first line contains integer n (1 ≀ n ≀ 105). The next line contains integers a1, a2, ..., an (1 ≀ ai ≀ 105). The numbers are separated by spaces. Output In the first line print integer t β€” the number of valid x. On each of the next t lines print two integers x and px, where x is current suitable value, px is the common difference between numbers in the progression (if x occurs exactly once in the sequence, px must equal 0). Print the pairs in the order of increasing x. Examples Input 1 2 Output 1 2 0 Input 8 1 2 1 3 1 2 1 5 Output 4 1 2 2 4 3 0 5 0 Note In the first test 2 occurs exactly once in the sequence, ergo p2 = 0.
instruction
0
63,456
12
126,912
Tags: implementation, sortings Correct Solution: ``` dic = {} n = int(input()) l = [int(i) for i in input().split()] for i in range(n): if dic.get(l[i], -1 ) == -1: dic[l[i]] = [i] else: dic[l[i]].append(i) ans = {} for k, v in dic.items(): if len(v) >=2: dif = v[1]-v[0] to_add = True for i in range(2, len(v)): if v[i] - v[i-1] != dif: to_add = False if to_add: ans[k] = dif else: ans[k] = 0 print(len(ans)) ans = sorted(ans.items(), key=lambda x: x[0]) for k, v in ans: print(k,v) ```
output
1
63,456
12
126,913
Provide tags and a correct Python 3 solution for this coding contest problem. One day Jeff got hold of an integer sequence a1, a2, ..., an of length n. The boy immediately decided to analyze the sequence. For that, he needs to find all values of x, for which these conditions hold: * x occurs in sequence a. * Consider all positions of numbers x in the sequence a (such i, that ai = x). These numbers, sorted in the increasing order, must form an arithmetic progression. Help Jeff, find all x that meet the problem conditions. Input The first line contains integer n (1 ≀ n ≀ 105). The next line contains integers a1, a2, ..., an (1 ≀ ai ≀ 105). The numbers are separated by spaces. Output In the first line print integer t β€” the number of valid x. On each of the next t lines print two integers x and px, where x is current suitable value, px is the common difference between numbers in the progression (if x occurs exactly once in the sequence, px must equal 0). Print the pairs in the order of increasing x. Examples Input 1 2 Output 1 2 0 Input 8 1 2 1 3 1 2 1 5 Output 4 1 2 2 4 3 0 5 0 Note In the first test 2 occurs exactly once in the sequence, ergo p2 = 0.
instruction
0
63,457
12
126,914
Tags: implementation, sortings Correct Solution: ``` N = int(input()) seq = list(map(int, input().strip().split())) index_dict = dict() for i, x in enumerate(seq): if x in index_dict: index_dict[x].append(i) else: index_dict[x] = [i] for key in index_dict: index_dict[key] = sorted(index_dict[key]) rslt = [] for key in index_dict: if len(index_dict[key]) == 1: rslt.append((key, 0)) else: diff = index_dict[key][1] - index_dict[key][0] is_ap = True i = 2 while i < len(index_dict[key]): if index_dict[key][i] - index_dict[key][i - 1] != diff: is_ap = False break i += 1 if is_ap: rslt.append((key, diff)) rslt = sorted(rslt, key=lambda x:x[0]) print(len(rslt)) for x in rslt: print(x[0], x[1]) ```
output
1
63,457
12
126,915
Provide tags and a correct Python 3 solution for this coding contest problem. One day Jeff got hold of an integer sequence a1, a2, ..., an of length n. The boy immediately decided to analyze the sequence. For that, he needs to find all values of x, for which these conditions hold: * x occurs in sequence a. * Consider all positions of numbers x in the sequence a (such i, that ai = x). These numbers, sorted in the increasing order, must form an arithmetic progression. Help Jeff, find all x that meet the problem conditions. Input The first line contains integer n (1 ≀ n ≀ 105). The next line contains integers a1, a2, ..., an (1 ≀ ai ≀ 105). The numbers are separated by spaces. Output In the first line print integer t β€” the number of valid x. On each of the next t lines print two integers x and px, where x is current suitable value, px is the common difference between numbers in the progression (if x occurs exactly once in the sequence, px must equal 0). Print the pairs in the order of increasing x. Examples Input 1 2 Output 1 2 0 Input 8 1 2 1 3 1 2 1 5 Output 4 1 2 2 4 3 0 5 0 Note In the first test 2 occurs exactly once in the sequence, ergo p2 = 0.
instruction
0
63,458
12
126,916
Tags: implementation, sortings Correct Solution: ``` import math import random def main(arr): pos={} for i in range(len(arr)): if arr[i] not in pos: pos[arr[i]]=[] pos[arr[i]].append(i) ans=[] for e in pos: if len(pos[e])==1: ans.append([e,0]) else: dist=pos[e][1]-pos[e][0] v=True for i in range(0,len(pos[e])-1): if pos[e][i+1]-pos[e][i]!=dist: v=False if v: ans.append([e,dist]) ans.sort(key=lambda x:x[0]) print(len(ans)) for e in ans: print(*e) n=int(input()) arr=list(map(int,input().split())) (main(arr)) ```
output
1
63,458
12
126,917
Provide tags and a correct Python 3 solution for this coding contest problem. One day Jeff got hold of an integer sequence a1, a2, ..., an of length n. The boy immediately decided to analyze the sequence. For that, he needs to find all values of x, for which these conditions hold: * x occurs in sequence a. * Consider all positions of numbers x in the sequence a (such i, that ai = x). These numbers, sorted in the increasing order, must form an arithmetic progression. Help Jeff, find all x that meet the problem conditions. Input The first line contains integer n (1 ≀ n ≀ 105). The next line contains integers a1, a2, ..., an (1 ≀ ai ≀ 105). The numbers are separated by spaces. Output In the first line print integer t β€” the number of valid x. On each of the next t lines print two integers x and px, where x is current suitable value, px is the common difference between numbers in the progression (if x occurs exactly once in the sequence, px must equal 0). Print the pairs in the order of increasing x. Examples Input 1 2 Output 1 2 0 Input 8 1 2 1 3 1 2 1 5 Output 4 1 2 2 4 3 0 5 0 Note In the first test 2 occurs exactly once in the sequence, ergo p2 = 0.
instruction
0
63,460
12
126,920
Tags: implementation, sortings Correct Solution: ``` #!/usr/bin/python3 def readln(): return tuple(map(int, input().split())) n, = readln() var = {} for i, a in enumerate(readln()): if a in var: var[a].append(i) else: var[a] = [i] ans = [] for x, v in var.items(): if len(v) == 1: ans.append((x, 0)) else: d = set([v[i] - v[i - 1] for i in range(1, len(v))]) if len(d) == 1: ans.append((x, d.pop())) print(len(ans)) print('\n'.join('%d %d' % f for f in sorted(ans))) ```
output
1
63,460
12
126,921
Provide tags and a correct Python 3 solution for this coding contest problem. One day Jeff got hold of an integer sequence a1, a2, ..., an of length n. The boy immediately decided to analyze the sequence. For that, he needs to find all values of x, for which these conditions hold: * x occurs in sequence a. * Consider all positions of numbers x in the sequence a (such i, that ai = x). These numbers, sorted in the increasing order, must form an arithmetic progression. Help Jeff, find all x that meet the problem conditions. Input The first line contains integer n (1 ≀ n ≀ 105). The next line contains integers a1, a2, ..., an (1 ≀ ai ≀ 105). The numbers are separated by spaces. Output In the first line print integer t β€” the number of valid x. On each of the next t lines print two integers x and px, where x is current suitable value, px is the common difference between numbers in the progression (if x occurs exactly once in the sequence, px must equal 0). Print the pairs in the order of increasing x. Examples Input 1 2 Output 1 2 0 Input 8 1 2 1 3 1 2 1 5 Output 4 1 2 2 4 3 0 5 0 Note In the first test 2 occurs exactly once in the sequence, ergo p2 = 0.
instruction
0
63,461
12
126,922
Tags: implementation, sortings Correct Solution: ``` from collections import defaultdict as df n=int(input()) a=list(map(int,input().rstrip().split())) d=df(list) for i in range(n): d[a[i]].append(i) d=dict(sorted(d.items())) result=[] for i in d: if len(d[i])==1: result.append((i,0)) elif len(d[i])==2: result.append((i,d[i][1]-d[i][0])) else: diff=d[i][1]-d[i][0] gg=0 for j in range(2,len(d[i])): if d[i][j]-d[i][j-1]==diff: pass else: gg=1 break if gg==0: result.append((i,diff)) print(len(result)) for i in result: print(i[0],i[1]) ```
output
1
63,461
12
126,923
Provide tags and a correct Python 3 solution for this coding contest problem. One day Jeff got hold of an integer sequence a1, a2, ..., an of length n. The boy immediately decided to analyze the sequence. For that, he needs to find all values of x, for which these conditions hold: * x occurs in sequence a. * Consider all positions of numbers x in the sequence a (such i, that ai = x). These numbers, sorted in the increasing order, must form an arithmetic progression. Help Jeff, find all x that meet the problem conditions. Input The first line contains integer n (1 ≀ n ≀ 105). The next line contains integers a1, a2, ..., an (1 ≀ ai ≀ 105). The numbers are separated by spaces. Output In the first line print integer t β€” the number of valid x. On each of the next t lines print two integers x and px, where x is current suitable value, px is the common difference between numbers in the progression (if x occurs exactly once in the sequence, px must equal 0). Print the pairs in the order of increasing x. Examples Input 1 2 Output 1 2 0 Input 8 1 2 1 3 1 2 1 5 Output 4 1 2 2 4 3 0 5 0 Note In the first test 2 occurs exactly once in the sequence, ergo p2 = 0.
instruction
0
63,462
12
126,924
Tags: implementation, sortings Correct Solution: ``` inp, k = {},0 n,s = int(input()),input().split() for i in range(0, len(s)): inp_k = int(s[i]) if inp_k in inp: inp[inp_k ]+=[i] else: inp[inp_k] =[i] inp_keys = [int(i) for i in inp.keys()] inp_keys.sort() result = [] def check(arr): diff = arr[1]-arr[0] for i in range(1, len(arr)-1): if arr[i+1]-arr[i] != diff: return False return diff for i in inp_keys: j = inp[i] if len(j) == 1: result.append([i,0]) else: diff = check(j) if diff: result.append([i,diff]) out = "" for i,j in result: out+=str(i)+" "+str(j)+"\n" print(str(len(result))+"\n" + out[:-1]) ```
output
1
63,462
12
126,925
Provide tags and a correct Python 3 solution for this coding contest problem. One day Jeff got hold of an integer sequence a1, a2, ..., an of length n. The boy immediately decided to analyze the sequence. For that, he needs to find all values of x, for which these conditions hold: * x occurs in sequence a. * Consider all positions of numbers x in the sequence a (such i, that ai = x). These numbers, sorted in the increasing order, must form an arithmetic progression. Help Jeff, find all x that meet the problem conditions. Input The first line contains integer n (1 ≀ n ≀ 105). The next line contains integers a1, a2, ..., an (1 ≀ ai ≀ 105). The numbers are separated by spaces. Output In the first line print integer t β€” the number of valid x. On each of the next t lines print two integers x and px, where x is current suitable value, px is the common difference between numbers in the progression (if x occurs exactly once in the sequence, px must equal 0). Print the pairs in the order of increasing x. Examples Input 1 2 Output 1 2 0 Input 8 1 2 1 3 1 2 1 5 Output 4 1 2 2 4 3 0 5 0 Note In the first test 2 occurs exactly once in the sequence, ergo p2 = 0.
instruction
0
63,463
12
126,926
Tags: implementation, sortings Correct Solution: ``` n=int(input()) a=[int(x) for x in input().split()] s={} for i in range(n): if not a[i] in s.keys(): s[a[i]]=[i] else: s[a[i]].append(i) for e in sorted(s.keys()): j=1 r=0 while j<=len(s[e])-2: if s[e][j]-s[e][j-1]!=s[e][j+1]-s[e][j]: s.pop(e) break else: r=s[e][j+1]-s[e][j] j+=1 if e in s.keys(): if len(s[e])==2: s[e]=s[e][1]-s[e][0] else: s[e]=r print(len(s)) for e in sorted(s.keys()): print(e,s[e]) ```
output
1
63,463
12
126,927
Provide tags and a correct Python 3 solution for this coding contest problem. You are given array a with n elements and the number m. Consider some subsequence of a and the value of least common multiple (LCM) of its elements. Denote LCM as l. Find any longest subsequence of a with the value l ≀ m. A subsequence of a is an array we can get by erasing some elements of a. It is allowed to erase zero or all elements. The LCM of an empty array equals 1. Input The first line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the size of the array a and the parameter from the problem statement. The second line contains n integers ai (1 ≀ ai ≀ 109) β€” the elements of a. Output In the first line print two integers l and kmax (1 ≀ l ≀ m, 0 ≀ kmax ≀ n) β€” the value of LCM and the number of elements in optimal subsequence. In the second line print kmax integers β€” the positions of the elements from the optimal subsequence in the ascending order. Note that you can find and print any subsequence with the maximum length. Examples Input 7 8 6 2 9 2 7 2 3 Output 6 5 1 2 4 6 7 Input 6 4 2 2 2 3 3 3 Output 2 3 1 2 3
instruction
0
63,551
12
127,102
Tags: brute force, math, number theory Correct Solution: ``` from sys import stdin input = stdin.buffer.readline n, m = map(int, input().split()) cnt = [0] * (m + 1) *b, = map(int, input().split()) for i in b: if i <= m: cnt[i] += 1 a = [0] * (m + 1) for i in range(1, m + 1): for j in range(i, m + 1, i): a[j] += cnt[i] mx = idx = 0 for i in range(1, m + 1): if mx < a[i]: mx, idx = a[i], i if mx == 0: exit(print(1, 0)) print(idx, mx) for i in range(n): if idx % b[i] == 0: print(i + 1) ```
output
1
63,551
12
127,103
Provide tags and a correct Python 3 solution for this coding contest problem. You are given array a with n elements and the number m. Consider some subsequence of a and the value of least common multiple (LCM) of its elements. Denote LCM as l. Find any longest subsequence of a with the value l ≀ m. A subsequence of a is an array we can get by erasing some elements of a. It is allowed to erase zero or all elements. The LCM of an empty array equals 1. Input The first line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the size of the array a and the parameter from the problem statement. The second line contains n integers ai (1 ≀ ai ≀ 109) β€” the elements of a. Output In the first line print two integers l and kmax (1 ≀ l ≀ m, 0 ≀ kmax ≀ n) β€” the value of LCM and the number of elements in optimal subsequence. In the second line print kmax integers β€” the positions of the elements from the optimal subsequence in the ascending order. Note that you can find and print any subsequence with the maximum length. Examples Input 7 8 6 2 9 2 7 2 3 Output 6 5 1 2 4 6 7 Input 6 4 2 2 2 3 3 3 Output 2 3 1 2 3
instruction
0
63,552
12
127,104
Tags: brute force, math, number theory Correct Solution: ``` import sys n, m = map(int, sys.stdin.buffer.readline().decode('utf-8').split()) a = list(map(int, sys.stdin.buffer.readline().decode('utf-8').split())) dp = [0]*(m+1) for x in a: if x <= m: dp[x] += 1 for i in range(m, 0, -1): for j in range(2, m+1): if i*j > m: break dp[i*j] += dp[i] lcm = max(1, dp.index(max(dp))) ans = [i for i, x in enumerate(a, start=1) if lcm % x == 0] sys.stdout.buffer.write( (str(lcm) + ' ' + str(len(ans)) + '\n' + ' '.join(map(str, ans))).encode('utf-8') ) ```
output
1
63,552
12
127,105
Provide tags and a correct Python 3 solution for this coding contest problem. You are given array a with n elements and the number m. Consider some subsequence of a and the value of least common multiple (LCM) of its elements. Denote LCM as l. Find any longest subsequence of a with the value l ≀ m. A subsequence of a is an array we can get by erasing some elements of a. It is allowed to erase zero or all elements. The LCM of an empty array equals 1. Input The first line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the size of the array a and the parameter from the problem statement. The second line contains n integers ai (1 ≀ ai ≀ 109) β€” the elements of a. Output In the first line print two integers l and kmax (1 ≀ l ≀ m, 0 ≀ kmax ≀ n) β€” the value of LCM and the number of elements in optimal subsequence. In the second line print kmax integers β€” the positions of the elements from the optimal subsequence in the ascending order. Note that you can find and print any subsequence with the maximum length. Examples Input 7 8 6 2 9 2 7 2 3 Output 6 5 1 2 4 6 7 Input 6 4 2 2 2 3 3 3 Output 2 3 1 2 3
instruction
0
63,553
12
127,106
Tags: brute force, math, number theory Correct Solution: ``` import bisect from itertools import accumulate import os import sys import math from decimal import * 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) def input(): return sys.stdin.readline().rstrip("\r\n") def isPrime(n) : if (n <= 1) : return False if (n <= 3) : return True if (n % 2 == 0 or n % 3 == 0) : return False i = 5 while(i * i <= n) : if (n % i == 0 or n % (i + 2) == 0) : return False i = i + 6 return True #-------------------------------------------------------- n, m = map(int, input().split()) cnt = [0] * (m + 1) *b, = map(int, input().split()) for i in b: if i <= m: cnt[i] += 1 a = [0] * (m + 1) for i in range(1, m + 1): for j in range(i, m + 1, i): a[j] += cnt[i] mx = idx = 0 for i in range(1, m + 1): if mx < a[i]: mx, idx = a[i], i if mx == 0: exit(print(1, 0)) print(idx, mx) for i in range(n): if idx % b[i] == 0: print(i + 1) ```
output
1
63,553
12
127,107
Provide tags and a correct Python 3 solution for this coding contest problem. You are given array a with n elements and the number m. Consider some subsequence of a and the value of least common multiple (LCM) of its elements. Denote LCM as l. Find any longest subsequence of a with the value l ≀ m. A subsequence of a is an array we can get by erasing some elements of a. It is allowed to erase zero or all elements. The LCM of an empty array equals 1. Input The first line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the size of the array a and the parameter from the problem statement. The second line contains n integers ai (1 ≀ ai ≀ 109) β€” the elements of a. Output In the first line print two integers l and kmax (1 ≀ l ≀ m, 0 ≀ kmax ≀ n) β€” the value of LCM and the number of elements in optimal subsequence. In the second line print kmax integers β€” the positions of the elements from the optimal subsequence in the ascending order. Note that you can find and print any subsequence with the maximum length. Examples Input 7 8 6 2 9 2 7 2 3 Output 6 5 1 2 4 6 7 Input 6 4 2 2 2 3 3 3 Output 2 3 1 2 3
instruction
0
63,554
12
127,108
Tags: brute force, math, number theory Correct Solution: ``` import sys n, m = [int(x) for x in input().split()] A = [int(x) for x in input().split()] B, C = [0]*(m+1), [0]*(m+1) for a in A: if a <= m: B[a] += 1 for i in range(2, m + 1): for j in range(i, m+1, i): C[j] += B[i] k, l = 1, 0 for i in range(2, m+1): if C[i] > l: l = C[i] k = i print(k, l + B[1]) for i, a in enumerate(A): if k%a == 0: sys.stdout.write(str(i+1) + ' ') ```
output
1
63,554
12
127,109
Provide tags and a correct Python 3 solution for this coding contest problem. You are given array a with n elements and the number m. Consider some subsequence of a and the value of least common multiple (LCM) of its elements. Denote LCM as l. Find any longest subsequence of a with the value l ≀ m. A subsequence of a is an array we can get by erasing some elements of a. It is allowed to erase zero or all elements. The LCM of an empty array equals 1. Input The first line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the size of the array a and the parameter from the problem statement. The second line contains n integers ai (1 ≀ ai ≀ 109) β€” the elements of a. Output In the first line print two integers l and kmax (1 ≀ l ≀ m, 0 ≀ kmax ≀ n) β€” the value of LCM and the number of elements in optimal subsequence. In the second line print kmax integers β€” the positions of the elements from the optimal subsequence in the ascending order. Note that you can find and print any subsequence with the maximum length. Examples Input 7 8 6 2 9 2 7 2 3 Output 6 5 1 2 4 6 7 Input 6 4 2 2 2 3 3 3 Output 2 3 1 2 3
instruction
0
63,555
12
127,110
Tags: brute force, math, number theory Correct Solution: ``` def main(): from collections import Counter n, m = map(int, input().split()) l = list(map(int, input().split())) m += 1 tmp = Counter(x for x in l if 1 < x < m) num = [0] * m for x, v in tmp.items(): for i in range(x, m, x): num[i] += v lcm = max(range(m), key=num.__getitem__) if lcm: tmp = {x for x in tmp.keys() if not lcm % x} tmp.add(1) res = [i for i, x in enumerate(l, 1) if x in tmp] else: res, lcm = [i for i, x in enumerate(l, 1) if x == 1], 1 print(lcm, len(res)) if res: print(' '.join(map(str, res))) if __name__ == '__main__': main() ```
output
1
63,555
12
127,111
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given array a with n elements and the number m. Consider some subsequence of a and the value of least common multiple (LCM) of its elements. Denote LCM as l. Find any longest subsequence of a with the value l ≀ m. A subsequence of a is an array we can get by erasing some elements of a. It is allowed to erase zero or all elements. The LCM of an empty array equals 1. Input The first line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the size of the array a and the parameter from the problem statement. The second line contains n integers ai (1 ≀ ai ≀ 109) β€” the elements of a. Output In the first line print two integers l and kmax (1 ≀ l ≀ m, 0 ≀ kmax ≀ n) β€” the value of LCM and the number of elements in optimal subsequence. In the second line print kmax integers β€” the positions of the elements from the optimal subsequence in the ascending order. Note that you can find and print any subsequence with the maximum length. Examples Input 7 8 6 2 9 2 7 2 3 Output 6 5 1 2 4 6 7 Input 6 4 2 2 2 3 3 3 Output 2 3 1 2 3 Submitted Solution: ``` from fractions import gcd from functools import reduce n,m = map(int,input().split()) a = list(map(int,input().split())) d = {} for v in a: if v<=m: d[v] = d.setdefault(v,0)+1 l = sorted([i for i in d]) ans, mx = 0, 0 for i in range(m+1): j, tmp = 2, 0 while j*j<=i: if i%j==0: if j in d: tmp += d[j] x = i//j if x!=j: if x in d: tmp += d[x] j += 1 if tmp>mx: mx=tmp ans = i if ans==0: ans=m b, c = [] ,[] for k,v in enumerate(a): if ans%v==0: b.append(k) c.append(v) lcm = reduce(lambda x,a: (x*a)//gcd(a,x),c,1) print(lcm,len(b),sep=' ') for i in b: print(i+1,end=' ') ```
instruction
0
63,556
12
127,112
No
output
1
63,556
12
127,113
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given array a with n elements and the number m. Consider some subsequence of a and the value of least common multiple (LCM) of its elements. Denote LCM as l. Find any longest subsequence of a with the value l ≀ m. A subsequence of a is an array we can get by erasing some elements of a. It is allowed to erase zero or all elements. The LCM of an empty array equals 1. Input The first line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the size of the array a and the parameter from the problem statement. The second line contains n integers ai (1 ≀ ai ≀ 109) β€” the elements of a. Output In the first line print two integers l and kmax (1 ≀ l ≀ m, 0 ≀ kmax ≀ n) β€” the value of LCM and the number of elements in optimal subsequence. In the second line print kmax integers β€” the positions of the elements from the optimal subsequence in the ascending order. Note that you can find and print any subsequence with the maximum length. Examples Input 7 8 6 2 9 2 7 2 3 Output 6 5 1 2 4 6 7 Input 6 4 2 2 2 3 3 3 Output 2 3 1 2 3 Submitted Solution: ``` #### IMPORTANT LIBRARY #### ############################ ### DO NOT USE import random --> 250ms to load the library ############################ ### In case of extra libraries: https://github.com/cheran-senthil/PyRival ###################### ####### IMPORT ####### ###################### from functools import cmp_to_key from collections import deque, Counter from heapq import heappush, heappop from math import log, ceil ###################### #### STANDARD I/O #### ###################### import sys import os 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") if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) def print(*args, **kwargs): sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() def inp(): return sys.stdin.readline().rstrip("\r\n") # for fast input def ii(): return int(inp()) def si(): return str(inp()) def li(lag = 0): l = list(map(int, inp().split())) if lag != 0: for i in range(len(l)): l[i] += lag return l def mi(lag = 0): matrix = list() for i in range(n): matrix.append(li(lag)) return matrix def lsi(): #string list return list(map(str, inp().split())) def print_list(lista, space = " "): print(space.join(map(str, lista))) ###################### ### BISECT METHODS ### ###################### def bisect_left(a, x): """i tale che a[i] >= x e a[i-1] < x""" left = 0 right = len(a) while left < right: mid = (left+right)//2 if a[mid] < x: left = mid+1 else: right = mid return left def bisect_right(a, x): """i tale che a[i] > x e a[i-1] <= x""" left = 0 right = len(a) while left < right: mid = (left+right)//2 if a[mid] > x: right = mid else: left = mid+1 return left def bisect_elements(a, x): """elementi pari a x nell'Γ‘rray sortato""" return bisect_right(a, x) - bisect_left(a, x) ###################### ### MOD OPERATION #### ###################### MOD = 10**9 + 7 maxN = 5 FACT = [0] * maxN INV_FACT = [0] * maxN def add(x, y): return (x+y) % MOD def multiply(x, y): return (x*y) % MOD def power(x, y): if y == 0: return 1 elif y % 2: return multiply(x, power(x, y-1)) else: a = power(x, y//2) return multiply(a, a) def inverse(x): return power(x, MOD-2) def divide(x, y): return multiply(x, inverse(y)) def allFactorials(): FACT[0] = 1 for i in range(1, maxN): FACT[i] = multiply(i, FACT[i-1]) def inverseFactorials(): n = len(INV_FACT) INV_FACT[n-1] = inverse(FACT[n-1]) for i in range(n-2, -1, -1): INV_FACT[i] = multiply(INV_FACT[i+1], i+1) def coeffBinom(n, k): if n < k: return 0 return multiply(FACT[n], multiply(INV_FACT[k], INV_FACT[n-k])) ###################### #### GRAPH ALGOS ##### ###################### # ZERO BASED GRAPH def create_graph(n, m, undirected = 1, unweighted = 1): graph = [[] for i in range(n)] if unweighted: for i in range(m): [x, y] = li(lag = -1) graph[x].append(y) if undirected: graph[y].append(x) else: for i in range(m): [x, y, w] = li(lag = -1) w += 1 graph[x].append([y,w]) if undirected: graph[y].append([x,w]) return graph def create_tree(n, unweighted = 1): children = [[] for i in range(n)] if unweighted: for i in range(n-1): [x, y] = li(lag = -1) children[x].append(y) children[y].append(x) else: for i in range(n-1): [x, y, w] = li(lag = -1) w += 1 children[x].append([y, w]) children[y].append([x, w]) return children def dist(tree, n, A, B = -1): s = [[A, 0]] massimo, massimo_nodo = 0, 0 distanza = -1 v = [-1] * n while s: el, dis = s.pop() if dis > massimo: massimo = dis massimo_nodo = el if el == B: distanza = dis for child in tree[el]: if v[child] == -1: v[child] = 1 s.append([child, dis+1]) return massimo, massimo_nodo, distanza def diameter(tree): _, foglia, _ = dist(tree, n, 0) diam, _, _ = dist(tree, n, foglia) return diam def dfs(graph, n, A): v = [-1] * n s = [[A, 0]] v[A] = 0 while s: el, dis = s.pop() for child in graph[el]: if v[child] == -1: v[child] = dis + 1 s.append([child, dis + 1]) return v #visited: -1 if not visited, otherwise v[B] is the distance in terms of edges def bfs(graph, n, A): v = [-1] * n s = deque() s.append([A, 0]) v[A] = 0 while s: el, dis = s.popleft() for child in graph[el]: if v[child] == -1: v[child] = dis + 1 s.append([child, dis + 1]) return v #visited: -1 if not visited, otherwise v[B] is the distance in terms of edges #FROM A GIVEN ROOT, RECOVER THE STRUCTURE def parents_children_root_unrooted_tree(tree, n, root = 0): q = deque() visited = [0] * n parent = [-1] * n children = [[] for i in range(n)] q.append(root) while q: all_done = 1 visited[q[0]] = 1 for child in tree[q[0]]: if not visited[child]: all_done = 0 q.appendleft(child) if all_done: for child in tree[q[0]]: if parent[child] == -1: parent[q[0]] = child children[child].append(q[0]) q.popleft() return parent, children # CALCULATING LONGEST PATH FOR ALL THE NODES def all_longest_path_passing_from_node(parent, children, n): q = deque() visited = [len(children[i]) for i in range(n)] downwards = [[0,0] for i in range(n)] upward = [1] * n longest_path = [1] * n for i in range(n): if not visited[i]: q.append(i) downwards[i] = [1,0] while q: node = q.popleft() if parent[node] != -1: visited[parent[node]] -= 1 if not visited[parent[node]]: q.append(parent[node]) else: root = node for child in children[node]: downwards[node] = sorted([downwards[node][0], downwards[node][1], downwards[child][0] + 1], reverse = True)[0:2] s = [node] while s: node = s.pop() if parent[node] != -1: if downwards[parent[node]][0] == downwards[node][0] + 1: upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][1]) else: upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][0]) longest_path[node] = downwards[node][0] + downwards[node][1] + upward[node] - min([downwards[node][0], downwards[node][1], upward[node]]) - 1 for child in children[node]: s.append(child) return longest_path ### TBD SUCCESSOR GRAPH 7.5 ### TBD TREE QUERIES 10.2 da 2 a 4 ### TBD ADVANCED TREE 10.3 ### TBD GRAPHS AND MATRICES 11.3.3 e 11.4.3 e 11.5.3 (ON GAMES) ###################### ## END OF LIBRARIES ## ###################### def primes(N): smallest_prime = [1] * (N+1) prime = [] smallest_prime[0] = 0 smallest_prime[1] = 0 for i in range(2, N+1): if smallest_prime[i] == 1: prime.append(i) smallest_prime[i] = i j = 0 while (j < len(prime) and i * prime[j] <= N): smallest_prime[i * prime[j]] = min(prime[j], smallest_prime[i]) j += 1 return prime, smallest_prime from itertools import combinations n, m = li() a = li() b = sorted(a) _, smallest_prime = primes(m) dp = [0] * (m+1) dp[1] = bisect_elements(b, 1) def prod(d): p = 1 for el in d: p *= el return p for el in range(2, m+1): prime = set() i = el while i != 1: p = smallest_prime[i] prime.add(p) i = i // p dp[el] = bisect_elements(b, el) for k in range(1, len(prime)+1): c = list(combinations(prime, k)) for d in c: dp[el] += (-1) ** (k+1) * dp[el // prod(d)] mas = 0 ima = 0 for i in range(1, m+1): if mas < dp[i]: mas = dp[i] ima = i indici = [] for i in range(n): if ima % a[i] == 0: indici.append(i+1) print_list([mas, ima]) print_list(indici) ```
instruction
0
63,557
12
127,114
No
output
1
63,557
12
127,115
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given array a with n elements and the number m. Consider some subsequence of a and the value of least common multiple (LCM) of its elements. Denote LCM as l. Find any longest subsequence of a with the value l ≀ m. A subsequence of a is an array we can get by erasing some elements of a. It is allowed to erase zero or all elements. The LCM of an empty array equals 1. Input The first line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the size of the array a and the parameter from the problem statement. The second line contains n integers ai (1 ≀ ai ≀ 109) β€” the elements of a. Output In the first line print two integers l and kmax (1 ≀ l ≀ m, 0 ≀ kmax ≀ n) β€” the value of LCM and the number of elements in optimal subsequence. In the second line print kmax integers β€” the positions of the elements from the optimal subsequence in the ascending order. Note that you can find and print any subsequence with the maximum length. Examples Input 7 8 6 2 9 2 7 2 3 Output 6 5 1 2 4 6 7 Input 6 4 2 2 2 3 3 3 Output 2 3 1 2 3 Submitted Solution: ``` from collections import Counter n,m = map(int, input().split()) ints = list(map(int, input().split())) factors = [0] * (m+1) for k,v in Counter(ints).items(): for x in range(k, m+1, k): factors[x] += v length = max(factors) lcm = factors.index(length) print(lcm, length) print(' '.join(str(i+1) for i,x in enumerate(ints) if not lcm % x)) ```
instruction
0
63,558
12
127,116
No
output
1
63,558
12
127,117
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given array a with n elements and the number m. Consider some subsequence of a and the value of least common multiple (LCM) of its elements. Denote LCM as l. Find any longest subsequence of a with the value l ≀ m. A subsequence of a is an array we can get by erasing some elements of a. It is allowed to erase zero or all elements. The LCM of an empty array equals 1. Input The first line contains two integers n and m (1 ≀ n, m ≀ 106) β€” the size of the array a and the parameter from the problem statement. The second line contains n integers ai (1 ≀ ai ≀ 109) β€” the elements of a. Output In the first line print two integers l and kmax (1 ≀ l ≀ m, 0 ≀ kmax ≀ n) β€” the value of LCM and the number of elements in optimal subsequence. In the second line print kmax integers β€” the positions of the elements from the optimal subsequence in the ascending order. Note that you can find and print any subsequence with the maximum length. Examples Input 7 8 6 2 9 2 7 2 3 Output 6 5 1 2 4 6 7 Input 6 4 2 2 2 3 3 3 Output 2 3 1 2 3 Submitted Solution: ``` from sys import stdin input = stdin.buffer.readline n, m = map(int, input().split()) cnt = [0] * (m + 1) *b, = map(int, input().split()) for i in b: if i <= m: cnt[i] += 1 a = [0] * (m + 1) for i in range(1, m + 1): for j in range(i, m + 1, i): a[j] += cnt[i] mx = idx = 0 for i in range(1, m + 1): if mx < a[i]: mx, idx = a[i], i print(idx, mx) for i in range(n): if idx % b[i] == 0: print(i + 1) ```
instruction
0
63,559
12
127,118
No
output
1
63,559
12
127,119
Provide tags and a correct Python 3 solution for this coding contest problem. This is an interactive problem. You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x. More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti β‰  - 1, then valuenexti > valuei. You are given the number of elements in the list n, the index of the first element start, and the integer x. You can make up to 2000 queries of the following two types: * ? i (1 ≀ i ≀ n) β€” ask the values valuei and nexti, * ! ans β€” give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query. Write a program that solves this problem. Input The first line contains three integers n, start, x (1 ≀ n ≀ 50000, 1 ≀ start ≀ n, 0 ≀ x ≀ 109) β€” the number of elements in the list, the index of the first element and the integer x. Output To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer. Interaction To make a query of the first type, print ? i (1 ≀ i ≀ n), where i is the index of element you want to know information about. After each query of type ? read two integers valuei and nexti (0 ≀ valuei ≀ 109, - 1 ≀ nexti ≀ n, nexti β‰  0). It is guaranteed that if nexti β‰  - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element. Note that you can't ask more than 1999 queries of the type ?. If nexti = - 1 and valuei = - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream. Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer. To flush you can use (just after printing a query and line end): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * For other languages see documentation. Hacks format For hacks, use the following format: In the first line print three integers n, start, x (1 ≀ n ≀ 50000, 1 ≀ start ≀ n, 0 ≀ x ≀ 109). In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 ≀ valuei ≀ 109, - 1 ≀ nexti ≀ n, nexti β‰  0). The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend. Example Input 5 3 80 97 -1 58 5 16 2 81 1 79 4 Output ? 1 ? 2 ? 3 ? 4 ? 5 ! 81 Note You can read more about singly linked list by the following link: <https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list> The illustration for the first sample case. Start and finish elements are marked dark. <image>
instruction
0
63,624
12
127,248
Tags: brute force, interactive, probabilities Correct Solution: ``` from random import seed, sample from functools import lru_cache req_left = 1999 @lru_cache(None) def ask(x): global req_left req_left -= 1 print("?", x, flush=True) return tuple(map(int, input().split())) seed(1337) n, start, x = map(int, input().split()) ch = sample(range(1, n+1), n // 50 + 1) if start not in ch: ch.append(start) mv = 0 mc = start for c in ch: v, n = ask(c) if v < x: if v > mv: mv = v mc = c if v == x: print("!", v, flush=True) exit(0) #print("probing done") nxt = mc while req_left > 0: if nxt == -1: break #print(nxt) v, nxt = ask(nxt) #print("aa") if v >= x: print("!", v, flush=True) exit(0) print("!", -1) ```
output
1
63,624
12
127,249
Provide tags and a correct Python 3 solution for this coding contest problem. This is an interactive problem. You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x. More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti β‰  - 1, then valuenexti > valuei. You are given the number of elements in the list n, the index of the first element start, and the integer x. You can make up to 2000 queries of the following two types: * ? i (1 ≀ i ≀ n) β€” ask the values valuei and nexti, * ! ans β€” give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query. Write a program that solves this problem. Input The first line contains three integers n, start, x (1 ≀ n ≀ 50000, 1 ≀ start ≀ n, 0 ≀ x ≀ 109) β€” the number of elements in the list, the index of the first element and the integer x. Output To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer. Interaction To make a query of the first type, print ? i (1 ≀ i ≀ n), where i is the index of element you want to know information about. After each query of type ? read two integers valuei and nexti (0 ≀ valuei ≀ 109, - 1 ≀ nexti ≀ n, nexti β‰  0). It is guaranteed that if nexti β‰  - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element. Note that you can't ask more than 1999 queries of the type ?. If nexti = - 1 and valuei = - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream. Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer. To flush you can use (just after printing a query and line end): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * For other languages see documentation. Hacks format For hacks, use the following format: In the first line print three integers n, start, x (1 ≀ n ≀ 50000, 1 ≀ start ≀ n, 0 ≀ x ≀ 109). In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 ≀ valuei ≀ 109, - 1 ≀ nexti ≀ n, nexti β‰  0). The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend. Example Input 5 3 80 97 -1 58 5 16 2 81 1 79 4 Output ? 1 ? 2 ? 3 ? 4 ? 5 ! 81 Note You can read more about singly linked list by the following link: <https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list> The illustration for the first sample case. Start and finish elements are marked dark. <image>
instruction
0
63,625
12
127,250
Tags: brute force, interactive, probabilities Correct Solution: ``` from random import sample def R(): return map(int, input().split()) def ask(i): print('?', i, flush=True) v, nxt = R() if v < 0: exit() return v, nxt def ans(v): print('!', v) exit() n, s, x = R() d = [None] * (n + 1) mv = -1 i = s count = 950 q = range(1, n + 1) if n > count: q = sample(q, count) if s not in q: q[0] = s for i in q: v, nxt = ask(i) if v == x or i == s and v > x: ans(v) if v < x: if nxt < 0: ans(-1) nv = d[nxt] if nv is None: if v > mv: mv, mnxt = v, nxt elif nv > x: ans(nv) d[i] = v while mv < x and mnxt >= 1: mv, mnxt = (d[mnxt], None) if d[mnxt] else ask(mnxt) ans(mv if mv >= x else -1) ```
output
1
63,625
12
127,251
Provide tags and a correct Python 3 solution for this coding contest problem. This is an interactive problem. You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x. More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti β‰  - 1, then valuenexti > valuei. You are given the number of elements in the list n, the index of the first element start, and the integer x. You can make up to 2000 queries of the following two types: * ? i (1 ≀ i ≀ n) β€” ask the values valuei and nexti, * ! ans β€” give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query. Write a program that solves this problem. Input The first line contains three integers n, start, x (1 ≀ n ≀ 50000, 1 ≀ start ≀ n, 0 ≀ x ≀ 109) β€” the number of elements in the list, the index of the first element and the integer x. Output To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer. Interaction To make a query of the first type, print ? i (1 ≀ i ≀ n), where i is the index of element you want to know information about. After each query of type ? read two integers valuei and nexti (0 ≀ valuei ≀ 109, - 1 ≀ nexti ≀ n, nexti β‰  0). It is guaranteed that if nexti β‰  - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element. Note that you can't ask more than 1999 queries of the type ?. If nexti = - 1 and valuei = - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream. Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer. To flush you can use (just after printing a query and line end): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * For other languages see documentation. Hacks format For hacks, use the following format: In the first line print three integers n, start, x (1 ≀ n ≀ 50000, 1 ≀ start ≀ n, 0 ≀ x ≀ 109). In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 ≀ valuei ≀ 109, - 1 ≀ nexti ≀ n, nexti β‰  0). The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend. Example Input 5 3 80 97 -1 58 5 16 2 81 1 79 4 Output ? 1 ? 2 ? 3 ? 4 ? 5 ! 81 Note You can read more about singly linked list by the following link: <https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list> The illustration for the first sample case. Start and finish elements are marked dark. <image>
instruction
0
63,626
12
127,252
Tags: brute force, interactive, probabilities Correct Solution: ``` from random import sample def R(): return map(int, input().split()) def ask(i): print('?', i, flush=True) v, nxt = R() if v < 0: exit() return v, nxt def ans(v): print('!', v) exit() n, s, x = R() mv = -1 i = s S = 900 q = range(1, n + 1) if n > S: q = sample(q, S) if s not in q: q[0] = s for i in q: v, nxt = ask(i) if v == x or i == s and v > x: ans(v) if v < x: if nxt < 0: ans(-1) if v > mv: mv, mnxt = v, nxt while mv < x and mnxt >= 1: mv, mnxt = ask(mnxt) ans(mv if mv >= x else -1) ```
output
1
63,626
12
127,253
Provide tags and a correct Python 3 solution for this coding contest problem. This is an interactive problem. You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x. More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti β‰  - 1, then valuenexti > valuei. You are given the number of elements in the list n, the index of the first element start, and the integer x. You can make up to 2000 queries of the following two types: * ? i (1 ≀ i ≀ n) β€” ask the values valuei and nexti, * ! ans β€” give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query. Write a program that solves this problem. Input The first line contains three integers n, start, x (1 ≀ n ≀ 50000, 1 ≀ start ≀ n, 0 ≀ x ≀ 109) β€” the number of elements in the list, the index of the first element and the integer x. Output To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer. Interaction To make a query of the first type, print ? i (1 ≀ i ≀ n), where i is the index of element you want to know information about. After each query of type ? read two integers valuei and nexti (0 ≀ valuei ≀ 109, - 1 ≀ nexti ≀ n, nexti β‰  0). It is guaranteed that if nexti β‰  - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element. Note that you can't ask more than 1999 queries of the type ?. If nexti = - 1 and valuei = - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream. Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer. To flush you can use (just after printing a query and line end): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * For other languages see documentation. Hacks format For hacks, use the following format: In the first line print three integers n, start, x (1 ≀ n ≀ 50000, 1 ≀ start ≀ n, 0 ≀ x ≀ 109). In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 ≀ valuei ≀ 109, - 1 ≀ nexti ≀ n, nexti β‰  0). The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend. Example Input 5 3 80 97 -1 58 5 16 2 81 1 79 4 Output ? 1 ? 2 ? 3 ? 4 ? 5 ! 81 Note You can read more about singly linked list by the following link: <https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list> The illustration for the first sample case. Start and finish elements are marked dark. <image>
instruction
0
63,627
12
127,254
Tags: brute force, interactive, probabilities Correct Solution: ``` import random SIZE = 900 def R(): return map(int, input().split()) def ask(i): print('?', i, flush=True) v, nxt = R() if v < 0: exit() return v, nxt n, s, x = R() q = range(1, n + 1) if n > SIZE: q = random.sample(q, SIZE) v, nxt = max((t for t in map(ask, q) if t[0] < x), default=(-1, s)) while v < x and ~nxt: v, nxt = ask(nxt) print('!', v if v >= x else -1) ```
output
1
63,627
12
127,255
Provide tags and a correct Python 3 solution for this coding contest problem. This is an interactive problem. You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x. More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti β‰  - 1, then valuenexti > valuei. You are given the number of elements in the list n, the index of the first element start, and the integer x. You can make up to 2000 queries of the following two types: * ? i (1 ≀ i ≀ n) β€” ask the values valuei and nexti, * ! ans β€” give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query. Write a program that solves this problem. Input The first line contains three integers n, start, x (1 ≀ n ≀ 50000, 1 ≀ start ≀ n, 0 ≀ x ≀ 109) β€” the number of elements in the list, the index of the first element and the integer x. Output To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer. Interaction To make a query of the first type, print ? i (1 ≀ i ≀ n), where i is the index of element you want to know information about. After each query of type ? read two integers valuei and nexti (0 ≀ valuei ≀ 109, - 1 ≀ nexti ≀ n, nexti β‰  0). It is guaranteed that if nexti β‰  - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element. Note that you can't ask more than 1999 queries of the type ?. If nexti = - 1 and valuei = - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream. Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer. To flush you can use (just after printing a query and line end): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * For other languages see documentation. Hacks format For hacks, use the following format: In the first line print three integers n, start, x (1 ≀ n ≀ 50000, 1 ≀ start ≀ n, 0 ≀ x ≀ 109). In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 ≀ valuei ≀ 109, - 1 ≀ nexti ≀ n, nexti β‰  0). The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend. Example Input 5 3 80 97 -1 58 5 16 2 81 1 79 4 Output ? 1 ? 2 ? 3 ? 4 ? 5 ! 81 Note You can read more about singly linked list by the following link: <https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list> The illustration for the first sample case. Start and finish elements are marked dark. <image>
instruction
0
63,628
12
127,256
Tags: brute force, interactive, probabilities Correct Solution: ``` from random import sample def R(): return map(int, input().split()) def ask(i): print('?', i, flush=True) v, nxt = R() if v < 0: exit() return v, nxt def ans(v): print('!', v) exit() n, s, x = R() mv = -1 i = s count = 950 q = range(1, n + 1) if n > count: q = sample(q, count) if s not in q: q[0] = s for i in q: v, nxt = ask(i) if v == x or i == s and v > x: ans(v) if mv < v < x: mv, mnxt = v, nxt while mv < x and mnxt >= 1: mv, mnxt = ask(mnxt) ans(mv if mv >= x else -1) ```
output
1
63,628
12
127,257
Provide tags and a correct Python 3 solution for this coding contest problem. This is an interactive problem. You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x. More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti β‰  - 1, then valuenexti > valuei. You are given the number of elements in the list n, the index of the first element start, and the integer x. You can make up to 2000 queries of the following two types: * ? i (1 ≀ i ≀ n) β€” ask the values valuei and nexti, * ! ans β€” give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query. Write a program that solves this problem. Input The first line contains three integers n, start, x (1 ≀ n ≀ 50000, 1 ≀ start ≀ n, 0 ≀ x ≀ 109) β€” the number of elements in the list, the index of the first element and the integer x. Output To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer. Interaction To make a query of the first type, print ? i (1 ≀ i ≀ n), where i is the index of element you want to know information about. After each query of type ? read two integers valuei and nexti (0 ≀ valuei ≀ 109, - 1 ≀ nexti ≀ n, nexti β‰  0). It is guaranteed that if nexti β‰  - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element. Note that you can't ask more than 1999 queries of the type ?. If nexti = - 1 and valuei = - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream. Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer. To flush you can use (just after printing a query and line end): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * For other languages see documentation. Hacks format For hacks, use the following format: In the first line print three integers n, start, x (1 ≀ n ≀ 50000, 1 ≀ start ≀ n, 0 ≀ x ≀ 109). In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 ≀ valuei ≀ 109, - 1 ≀ nexti ≀ n, nexti β‰  0). The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend. Example Input 5 3 80 97 -1 58 5 16 2 81 1 79 4 Output ? 1 ? 2 ? 3 ? 4 ? 5 ! 81 Note You can read more about singly linked list by the following link: <https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list> The illustration for the first sample case. Start and finish elements are marked dark. <image>
instruction
0
63,629
12
127,258
Tags: brute force, interactive, probabilities Correct Solution: ``` from random import sample SIZE = 950 def R(): return map(int, input().split()) def ask(i): print('?', i, flush=True) v, nxt = R() if v < 0: exit() return v, nxt def ans(v): print('!', v) exit() n, s, x = R() v, nxt = ask(s) if v >= x: ans(v) q = range(1, n + 1) if n > SIZE: q = sample(q, SIZE) v, nxt = max(filter(lambda t: t[0] < x, map(ask, q)), key=lambda t: t[0], default=(v, nxt)) while v < x and nxt >= 1: v, nxt = ask(nxt) ans(v if v >= x else -1) ```
output
1
63,629
12
127,259
Provide tags and a correct Python 3 solution for this coding contest problem. This is an interactive problem. You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x. More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti β‰  - 1, then valuenexti > valuei. You are given the number of elements in the list n, the index of the first element start, and the integer x. You can make up to 2000 queries of the following two types: * ? i (1 ≀ i ≀ n) β€” ask the values valuei and nexti, * ! ans β€” give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query. Write a program that solves this problem. Input The first line contains three integers n, start, x (1 ≀ n ≀ 50000, 1 ≀ start ≀ n, 0 ≀ x ≀ 109) β€” the number of elements in the list, the index of the first element and the integer x. Output To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer. Interaction To make a query of the first type, print ? i (1 ≀ i ≀ n), where i is the index of element you want to know information about. After each query of type ? read two integers valuei and nexti (0 ≀ valuei ≀ 109, - 1 ≀ nexti ≀ n, nexti β‰  0). It is guaranteed that if nexti β‰  - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element. Note that you can't ask more than 1999 queries of the type ?. If nexti = - 1 and valuei = - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream. Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer. To flush you can use (just after printing a query and line end): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * For other languages see documentation. Hacks format For hacks, use the following format: In the first line print three integers n, start, x (1 ≀ n ≀ 50000, 1 ≀ start ≀ n, 0 ≀ x ≀ 109). In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 ≀ valuei ≀ 109, - 1 ≀ nexti ≀ n, nexti β‰  0). The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend. Example Input 5 3 80 97 -1 58 5 16 2 81 1 79 4 Output ? 1 ? 2 ? 3 ? 4 ? 5 ! 81 Note You can read more about singly linked list by the following link: <https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list> The illustration for the first sample case. Start and finish elements are marked dark. <image>
instruction
0
63,630
12
127,260
Tags: brute force, interactive, probabilities Correct Solution: ``` from random import sample n,start,x=map(int,input().split()) f = min(n, 998) def get(i): print('?', i) return list(map(int, input().split())) def answer(a): print('!', a) exit(0) arr=[] arr.append(get(start)) if arr[0][0] >= x: answer(arr[0][0]) for i in sample(range(1, n+1), f): a=get(i) if a[0]<x: arr.append(a) elif a[0]==x: answer(a[0]) u = max(arr)[1] while u!=-1: q = get(u) if q[0] >= x: answer(q[0]) u = q[1] answer(-1) ```
output
1
63,630
12
127,261
Provide tags and a correct Python 3 solution for this coding contest problem. This is an interactive problem. You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x. More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti β‰  - 1, then valuenexti > valuei. You are given the number of elements in the list n, the index of the first element start, and the integer x. You can make up to 2000 queries of the following two types: * ? i (1 ≀ i ≀ n) β€” ask the values valuei and nexti, * ! ans β€” give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query. Write a program that solves this problem. Input The first line contains three integers n, start, x (1 ≀ n ≀ 50000, 1 ≀ start ≀ n, 0 ≀ x ≀ 109) β€” the number of elements in the list, the index of the first element and the integer x. Output To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer. Interaction To make a query of the first type, print ? i (1 ≀ i ≀ n), where i is the index of element you want to know information about. After each query of type ? read two integers valuei and nexti (0 ≀ valuei ≀ 109, - 1 ≀ nexti ≀ n, nexti β‰  0). It is guaranteed that if nexti β‰  - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element. Note that you can't ask more than 1999 queries of the type ?. If nexti = - 1 and valuei = - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream. Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer. To flush you can use (just after printing a query and line end): * fflush(stdout) in C++; * System.out.flush() in Java; * stdout.flush() in Python; * flush(output) in Pascal; * For other languages see documentation. Hacks format For hacks, use the following format: In the first line print three integers n, start, x (1 ≀ n ≀ 50000, 1 ≀ start ≀ n, 0 ≀ x ≀ 109). In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 ≀ valuei ≀ 109, - 1 ≀ nexti ≀ n, nexti β‰  0). The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend. Example Input 5 3 80 97 -1 58 5 16 2 81 1 79 4 Output ? 1 ? 2 ? 3 ? 4 ? 5 ! 81 Note You can read more about singly linked list by the following link: <https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list> The illustration for the first sample case. Start and finish elements are marked dark. <image>
instruction
0
63,631
12
127,262
Tags: brute force, interactive, probabilities Correct Solution: ``` from random import sample def R(): return map(int, input().split()) def ask(i): print('?', i, flush=True) v, nxt = R() if v < 0: exit() return v, nxt def ans(v): print('!', v) exit() n, s, x = R() mv = -1 i = s S = 800 q = range(1, n + 1) if n > S: q = sample(q, S) if s not in q: q[0] = s for i in q: v, nxt = ask(i) if v == x or i == s and v > x: ans(v) if v < x: if nxt < 0: ans(-1) if v > mv: mv, mnxt = v, nxt while mv < x and mnxt >= 1: mv, mnxt = ask(mnxt) ans(mv if mv >= x else -1) ```
output
1
63,631
12
127,263
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,656
12
127,312
Tags: brute force, dp, greedy, math, number theory Correct Solution: ``` def gcd(a: int, b: int): return gcd(b, a % b) if b else a nb = int(input()) s = input() numbers = [int(i) for i in s.split(" ")] number = -1 flag = True op = 0 def count1(s): res = 0 for i in s: if i == 1: res += 1 return res if nb == 1: if numbers[0] == 1: print(0) else: print(-1) else: while op < 2010: new = [] for i in range(0, len(numbers) - 1): g = gcd(numbers[i], numbers[i + 1]) if g == 1: number = nb + op - count1(numbers) numbers = [] print(number) exit(0) else: new.append(g) numbers = new op += 1 print(number) ```
output
1
63,656
12
127,313
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,657
12
127,314
Tags: brute force, dp, greedy, math, number theory Correct Solution: ``` n = int(input()) arr = list(map(int, input().split())) def GCD(x, y): if y > 0: return GCD(y, x % y) else: return x allGCD = arr[0] for i in arr: allGCD = GCD(allGCD, i) if allGCD > 1: print(-1) else: ones = 0 for i in arr: if i == 1: ones += 1 if ones > 1: print(n - ones) else: res = 2000000000 for i in range(n): tmp = arr[i] for j in range(i, n): tmp = GCD(tmp, arr[j]) if tmp == 1: res = min(res, j-i) print(n + res - 1) ''' 85% copied. LEL ''' ```
output
1
63,657
12
127,315
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,658
12
127,316
Tags: brute force, dp, greedy, math, number theory Correct Solution: ``` from math import gcd def read(): return [int(v) for v in input().split()] def main(): n = read()[0] a = read() g = a[0] cnt = a[0] != 1 for i in range(1, n): g = gcd(g, a[i]) if a[i] != 1: cnt += 1 if g != 1: print(-1) return b = a[:] loops = 0 found = False if cnt == n: while not found: c = [] for i in range(len(b) - 1): v = gcd(b[i], b[i+1]) if v == 1: found = True break c.append(v) b = c loops += 1 loops -= 1 print(loops + cnt) if __name__ == '__main__': main() ```
output
1
63,658
12
127,317
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,659
12
127,318
Tags: brute force, dp, greedy, math, number theory Correct 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=9999999999999999 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) ```
output
1
63,659
12
127,319
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,660
12
127,320
Tags: brute force, dp, greedy, math, number theory Correct Solution: ``` from math import gcd n=int(input()) a=list(map(int,input().split())) m=a.count(1) if m>0: print(n-m) exit() ans=-1 for i in range(n): d=a[i] c=i for j in range(i+1,n): d=gcd(d,a[j]) if d==1: c=j break if c>i: if ans<0: ans=c-i+n-1 else: ans=min(ans,c-i+n-1) print(ans) ```
output
1
63,660
12
127,321
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,661
12
127,322
Tags: brute force, dp, greedy, math, number theory Correct Solution: ``` import math def gcd(a, b): if b == 0: return a return gcd(b, a % b) n = int(input()) arr = [int(i) for i in input().split()] ones = 0 for i in range(n): if arr[i] == 1: ones += 1 if ones > 0: print(n - ones) else: ans = 10000000000 for i in range(n): a1 = arr[i] count = 0 for j in range(i+1, n): a1 = gcd(a1, arr[j]) count += 1 if a1 == 1: ans = min(ans, n+count-1) break if ans == 10000000000: print(-1) else: print(ans) ```
output
1
63,661
12
127,323