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.
In this problem, a n Γ m rectangular matrix a is called increasing if, for each row of i, when go from left to right, the values strictly increase (that is, a_{i,1}<a_{i,2}<...<a_{i,m}) and for each column j, when go from top to bottom, the values strictly increase (that is, a_{1,j}<a_{2,j}<...<a_{n,j}).
In a given matrix of non-negative integers, it is necessary to replace each value of 0 with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.
It is guaranteed that in a given value matrix all values of 0 are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
Input
The first line contains integers n and m (3 β€ n, m β€ 500) β the number of rows and columns in the given matrix a.
The following lines contain m each of non-negative integers β the values in the corresponding row of the given matrix: a_{i,1}, a_{i,2}, ..., a_{i,m} (0 β€ a_{i,j} β€ 8000).
It is guaranteed that for all a_{i,j}=0, 1 < i < n and 1 < j < m are true.
Output
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
Examples
Input
4 5
1 3 5 6 7
3 0 7 0 9
5 0 0 0 10
8 9 10 11 12
Output
144
Input
3 3
1 2 3
2 0 4
4 5 6
Output
30
Input
3 3
1 2 3
3 0 4
4 5 6
Output
-1
Input
3 3
1 2 3
2 3 4
3 4 2
Output
-1
Note
In the first example, the resulting matrix is as follows:
1 3 5 6 7
3 6 7 8 9
5 7 8 9 10
8 9 10 11 12
In the second example, the value 3 must be put in the middle cell.
In the third example, the desired resultant matrix does not exist.
Submitted Solution:
```
import sys,math
n,m=map(int,input().split())
a=[[*map(int,input().split())] for _ in range(n)]
try:
for i in range(n-1,-1,-1):
for j in range(m-1,-1,-1):
if a[i][j]==0:
a[i][j]=min(a[i+1][j],a[i][j+1])-1
if i+1<n:assert a[i][j]<a[i+1][j]
if j+1<m:assert a[i][j]<a[i][j+1]
print(sum(map(sum,a)))
except AssertionError:
print(-1)
``` | instruction | 0 | 66,541 | 12 | 133,082 |
Yes | output | 1 | 66,541 | 12 | 133,083 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem, a n Γ m rectangular matrix a is called increasing if, for each row of i, when go from left to right, the values strictly increase (that is, a_{i,1}<a_{i,2}<...<a_{i,m}) and for each column j, when go from top to bottom, the values strictly increase (that is, a_{1,j}<a_{2,j}<...<a_{n,j}).
In a given matrix of non-negative integers, it is necessary to replace each value of 0 with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.
It is guaranteed that in a given value matrix all values of 0 are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
Input
The first line contains integers n and m (3 β€ n, m β€ 500) β the number of rows and columns in the given matrix a.
The following lines contain m each of non-negative integers β the values in the corresponding row of the given matrix: a_{i,1}, a_{i,2}, ..., a_{i,m} (0 β€ a_{i,j} β€ 8000).
It is guaranteed that for all a_{i,j}=0, 1 < i < n and 1 < j < m are true.
Output
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
Examples
Input
4 5
1 3 5 6 7
3 0 7 0 9
5 0 0 0 10
8 9 10 11 12
Output
144
Input
3 3
1 2 3
2 0 4
4 5 6
Output
30
Input
3 3
1 2 3
3 0 4
4 5 6
Output
-1
Input
3 3
1 2 3
2 3 4
3 4 2
Output
-1
Note
In the first example, the resulting matrix is as follows:
1 3 5 6 7
3 6 7 8 9
5 7 8 9 10
8 9 10 11 12
In the second example, the value 3 must be put in the middle cell.
In the third example, the desired resultant matrix does not exist.
Submitted Solution:
```
a=[int(a)for a in input().split()]
b=list(list())
su=0
ans=0
cp=0
for j in range (a[0]):
c=[int(a) for a in input().split()][:a[1]]
b.append(c)
for i in range(a[0]-1,-1,-1):
for j in range(a[1]-1,-1,-1):
if b[i][j]==0:
b[i][j]=int(min(b[i][j+1],b[i+1][j]))-1
for z in b:
for r in z:
su+=r
for i in range(a[0]):
for j in range(a[1]):
try:
if b[i][j]>=b[i+1][j]:
ans=-1
break
except:
cp+=1
#print(i,j)
try:
if b[i][j]>=b[i][j+1]:
ans=-1
break
except:
cp+=1
#print(i,j)
if ans==-1:
print(-1)
else:
print(su)
``` | instruction | 0 | 66,542 | 12 | 133,084 |
Yes | output | 1 | 66,542 | 12 | 133,085 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem, a n Γ m rectangular matrix a is called increasing if, for each row of i, when go from left to right, the values strictly increase (that is, a_{i,1}<a_{i,2}<...<a_{i,m}) and for each column j, when go from top to bottom, the values strictly increase (that is, a_{1,j}<a_{2,j}<...<a_{n,j}).
In a given matrix of non-negative integers, it is necessary to replace each value of 0 with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.
It is guaranteed that in a given value matrix all values of 0 are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
Input
The first line contains integers n and m (3 β€ n, m β€ 500) β the number of rows and columns in the given matrix a.
The following lines contain m each of non-negative integers β the values in the corresponding row of the given matrix: a_{i,1}, a_{i,2}, ..., a_{i,m} (0 β€ a_{i,j} β€ 8000).
It is guaranteed that for all a_{i,j}=0, 1 < i < n and 1 < j < m are true.
Output
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
Examples
Input
4 5
1 3 5 6 7
3 0 7 0 9
5 0 0 0 10
8 9 10 11 12
Output
144
Input
3 3
1 2 3
2 0 4
4 5 6
Output
30
Input
3 3
1 2 3
3 0 4
4 5 6
Output
-1
Input
3 3
1 2 3
2 3 4
3 4 2
Output
-1
Note
In the first example, the resulting matrix is as follows:
1 3 5 6 7
3 6 7 8 9
5 7 8 9 10
8 9 10 11 12
In the second example, the value 3 must be put in the middle cell.
In the third example, the desired resultant matrix does not exist.
Submitted Solution:
```
def read():
n, m = map(int, input().split())
a = []
for i in range(n):
a.append(list(map(int, input().split())))
return n, m, a
def solve(n, m, a):
for i in range(n - 1, -1, -1):
for j in range(m - 1, -1, -1):
if a[i][j] == 0:
if i == n - 1:
if j == m - 1:
a[i][j] = 8000
else:
a[i][j] = a[i][j + 1] - 1
elif j == m - 1:
a[i][j] = a[i + 1][j] - 1
else:
a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1
if not ((i == 0 or a[i][j] > a[i - 1][j]) and (j == 0 or a[i][j] > a[i][j - 1])):
return -1
s = 0
for i in range(n):
for j in range(m):
s += a[i][j]
return s
result = solve(*read())
print(result)
``` | instruction | 0 | 66,543 | 12 | 133,086 |
Yes | output | 1 | 66,543 | 12 | 133,087 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem, a n Γ m rectangular matrix a is called increasing if, for each row of i, when go from left to right, the values strictly increase (that is, a_{i,1}<a_{i,2}<...<a_{i,m}) and for each column j, when go from top to bottom, the values strictly increase (that is, a_{1,j}<a_{2,j}<...<a_{n,j}).
In a given matrix of non-negative integers, it is necessary to replace each value of 0 with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.
It is guaranteed that in a given value matrix all values of 0 are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
Input
The first line contains integers n and m (3 β€ n, m β€ 500) β the number of rows and columns in the given matrix a.
The following lines contain m each of non-negative integers β the values in the corresponding row of the given matrix: a_{i,1}, a_{i,2}, ..., a_{i,m} (0 β€ a_{i,j} β€ 8000).
It is guaranteed that for all a_{i,j}=0, 1 < i < n and 1 < j < m are true.
Output
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
Examples
Input
4 5
1 3 5 6 7
3 0 7 0 9
5 0 0 0 10
8 9 10 11 12
Output
144
Input
3 3
1 2 3
2 0 4
4 5 6
Output
30
Input
3 3
1 2 3
3 0 4
4 5 6
Output
-1
Input
3 3
1 2 3
2 3 4
3 4 2
Output
-1
Note
In the first example, the resulting matrix is as follows:
1 3 5 6 7
3 6 7 8 9
5 7 8 9 10
8 9 10 11 12
In the second example, the value 3 must be put in the middle cell.
In the third example, the desired resultant matrix does not exist.
Submitted Solution:
```
n, m = map(int, input().split())
matr = [list(map(int, input().split())) for _ in range(n)]
for i, st in enumerate(matr):
for j, elem in enumerate(st):
if elem == 0:
if i != 0 and j != 0:
matr[i][j] = max(matr[i - 1][j], matr[i][j - 1]) + 1
elif i == 0 and j != 0:
matr[i][j] = matr[i][j - 1] + 1
elif i != 0 and j == 0:
matr[i][j] = matr[i - 1][j] + 1
for i, st in enumerate(matr):
for j, elem in enumerate(matr):
if i != 0 and j != 0:
if matr[i][j] <= max(matr[i - 1][j], matr[i][j - 1]):
print('-1')
exit()
elif i == 0 and j != 0:
if matr[i][j] <= matr[i][j - 1]:
print('-1')
exit()
elif i != 0 and j == 0:
if matr[i][j] <= matr[i - 1][j]:
print('-1')
exit()
print(sum(sum(l) for l in matr))
``` | instruction | 0 | 66,544 | 12 | 133,088 |
No | output | 1 | 66,544 | 12 | 133,089 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem, a n Γ m rectangular matrix a is called increasing if, for each row of i, when go from left to right, the values strictly increase (that is, a_{i,1}<a_{i,2}<...<a_{i,m}) and for each column j, when go from top to bottom, the values strictly increase (that is, a_{1,j}<a_{2,j}<...<a_{n,j}).
In a given matrix of non-negative integers, it is necessary to replace each value of 0 with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.
It is guaranteed that in a given value matrix all values of 0 are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
Input
The first line contains integers n and m (3 β€ n, m β€ 500) β the number of rows and columns in the given matrix a.
The following lines contain m each of non-negative integers β the values in the corresponding row of the given matrix: a_{i,1}, a_{i,2}, ..., a_{i,m} (0 β€ a_{i,j} β€ 8000).
It is guaranteed that for all a_{i,j}=0, 1 < i < n and 1 < j < m are true.
Output
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
Examples
Input
4 5
1 3 5 6 7
3 0 7 0 9
5 0 0 0 10
8 9 10 11 12
Output
144
Input
3 3
1 2 3
2 0 4
4 5 6
Output
30
Input
3 3
1 2 3
3 0 4
4 5 6
Output
-1
Input
3 3
1 2 3
2 3 4
3 4 2
Output
-1
Note
In the first example, the resulting matrix is as follows:
1 3 5 6 7
3 6 7 8 9
5 7 8 9 10
8 9 10 11 12
In the second example, the value 3 must be put in the middle cell.
In the third example, the desired resultant matrix does not exist.
Submitted Solution:
```
n,m=map(int,input().split())
l=[list(map(int,input().split())) for _ in range(n)]
ans=0
for i in range(n-1,-1,-1):
for j in range(m-1, -1, -1):
if l[i][j]==0:
mr=l[i][j+1]-1
mc=l[i+1][j]-1
l[i][j]=min(mr,mc)
ans+=l[i][j]
else:
ans+=l[i][j]
dic={}
ch=0
for i in range(n):
for j in range(m):
if l[i][j] not in dic:
dic[l[i][j]]=1
else:
ch=1
break
if ch:
print(-1)
else:
print(ans)
``` | instruction | 0 | 66,545 | 12 | 133,090 |
No | output | 1 | 66,545 | 12 | 133,091 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem, a n Γ m rectangular matrix a is called increasing if, for each row of i, when go from left to right, the values strictly increase (that is, a_{i,1}<a_{i,2}<...<a_{i,m}) and for each column j, when go from top to bottom, the values strictly increase (that is, a_{1,j}<a_{2,j}<...<a_{n,j}).
In a given matrix of non-negative integers, it is necessary to replace each value of 0 with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.
It is guaranteed that in a given value matrix all values of 0 are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
Input
The first line contains integers n and m (3 β€ n, m β€ 500) β the number of rows and columns in the given matrix a.
The following lines contain m each of non-negative integers β the values in the corresponding row of the given matrix: a_{i,1}, a_{i,2}, ..., a_{i,m} (0 β€ a_{i,j} β€ 8000).
It is guaranteed that for all a_{i,j}=0, 1 < i < n and 1 < j < m are true.
Output
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
Examples
Input
4 5
1 3 5 6 7
3 0 7 0 9
5 0 0 0 10
8 9 10 11 12
Output
144
Input
3 3
1 2 3
2 0 4
4 5 6
Output
30
Input
3 3
1 2 3
3 0 4
4 5 6
Output
-1
Input
3 3
1 2 3
2 3 4
3 4 2
Output
-1
Note
In the first example, the resulting matrix is as follows:
1 3 5 6 7
3 6 7 8 9
5 7 8 9 10
8 9 10 11 12
In the second example, the value 3 must be put in the middle cell.
In the third example, the desired resultant matrix does not exist.
Submitted Solution:
```
n, m = list(map(int,input().split()))
matrix = [0] * n
ok = 0
for q in range(0,n):
row = list(map(int,input().split()))
matrix[q] = row
zero = 0
def when_no_zero():
for z in range(0,n-1):
for x in range(0,m-1):
if(matrix[z][x]<matrix[z][x+1] and matrix[z][x]<matrix[z+1][x]):
pass
else:
print("-1")
ok = 1
break
def find_zero_value():
for i in range(n-1,0,-1):
for j in range(m-1,0,-1):
if matrix[i][j] == 0:
right = matrix[i][j+1]
down = matrix[i+1][j]
op_min = min([right,down])
up = matrix[i-1][j]
left = matrix[i][j-1]
op_max = max([up,left])
if(op_min-1>op_max):
matrix[i][j] = op_min-1
else:
return -1
return matrix
def find_sum(maz):
su = 0
for i in range(0,n):
for j in range(0,m):
su = su + matrix[i][j]
return su
for i in range(0,n):
for j in range(0,m):
if matrix[i][j] == 0:
zero = 1
break
if(zero==0):
when_no_zero()
else:
maz = find_zero_value()
if(maz == -1):
print("-1")
else:
summ = find_sum(maz)
print(summ)
``` | instruction | 0 | 66,546 | 12 | 133,092 |
No | output | 1 | 66,546 | 12 | 133,093 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this problem, a n Γ m rectangular matrix a is called increasing if, for each row of i, when go from left to right, the values strictly increase (that is, a_{i,1}<a_{i,2}<...<a_{i,m}) and for each column j, when go from top to bottom, the values strictly increase (that is, a_{1,j}<a_{2,j}<...<a_{n,j}).
In a given matrix of non-negative integers, it is necessary to replace each value of 0 with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.
It is guaranteed that in a given value matrix all values of 0 are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
Input
The first line contains integers n and m (3 β€ n, m β€ 500) β the number of rows and columns in the given matrix a.
The following lines contain m each of non-negative integers β the values in the corresponding row of the given matrix: a_{i,1}, a_{i,2}, ..., a_{i,m} (0 β€ a_{i,j} β€ 8000).
It is guaranteed that for all a_{i,j}=0, 1 < i < n and 1 < j < m are true.
Output
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
Examples
Input
4 5
1 3 5 6 7
3 0 7 0 9
5 0 0 0 10
8 9 10 11 12
Output
144
Input
3 3
1 2 3
2 0 4
4 5 6
Output
30
Input
3 3
1 2 3
3 0 4
4 5 6
Output
-1
Input
3 3
1 2 3
2 3 4
3 4 2
Output
-1
Note
In the first example, the resulting matrix is as follows:
1 3 5 6 7
3 6 7 8 9
5 7 8 9 10
8 9 10 11 12
In the second example, the value 3 must be put in the middle cell.
In the third example, the desired resultant matrix does not exist.
Submitted Solution:
```
from collections import deque
def valid(grid, r, c):
for i in range(r):
for j in range(1, c):
if grid[i][j] <= grid[i][j-1]:
return False
for i in range(c):
for j in range(1, r):
if grid[j][i] <= grid[j-1][i]:
return False
return True
def solve(r, c, grid):
for i in range(r):
for j in range(c-2, 0, -1):
if grid[i][j] == 0:
if grid[i+1][j]:
val = min(grid[i][j+1]-1, grid[i+1][j]-1)
else:
val = grid[i][j+1]-1
if grid[i][j-1]==val or grid[i-1][j]==val:
return False
grid[i][j] = val
return True
r, c = map(int, input().split())
grid = []
for i in range(r):
a = list(map(int, input().split()))
grid.append(a)
ans = solve(r, c, grid)
#print(grid)
if not ans:
print(-1)
else:
if valid(grid, r, c):
total=0
for i in range(r):
for j in range(c):
total += grid[i][j]
print(total)
else:
print(-1)
``` | instruction | 0 | 66,547 | 12 | 133,094 |
No | output | 1 | 66,547 | 12 | 133,095 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. In one move, you can jump from the position i to the position i - a_i (if 1 β€ i - a_i) or to the position i + a_i (if i + a_i β€ n).
For each position i from 1 to n you want to know the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa).
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the i-th element of a.
Output
Print n integers d_1, d_2, ..., d_n, where d_i is the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa) or -1 if it is impossible to reach such a position.
Example
Input
10
4 5 7 6 7 5 4 4 6 4
Output
1 1 1 2 -1 1 1 3 1 1 | instruction | 0 | 66,549 | 12 | 133,098 |
Tags: dfs and similar, graphs, shortest paths
Correct Solution:
```
n = int(input())
a = list(map(int,input().split()))
e = [[] for i in range(n)]
for i in range(n):
v = a[i]
if i - v >= 0:
e[i - v].append(i)
if i + v < n:
e[i + v].append(i)
q = [0]*n
res = [-1]*n
for rem in range(2):
d = [None]*n
ql = 0
qr = 0
for i in range(n):
if a[i] % 2 == rem:
q[qr] = i
qr += 1
d[i] = 0
while ql < qr:
x = q[ql]
ql += 1
dd = d[x] + 1
for v in e[x]:
if d[v] is None:
d[v] = dd
res[v] = dd
q[qr] = v
qr += 1
print(*res)
``` | output | 1 | 66,549 | 12 | 133,099 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. In one move, you can jump from the position i to the position i - a_i (if 1 β€ i - a_i) or to the position i + a_i (if i + a_i β€ n).
For each position i from 1 to n you want to know the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa).
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the i-th element of a.
Output
Print n integers d_1, d_2, ..., d_n, where d_i is the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa) or -1 if it is impossible to reach such a position.
Example
Input
10
4 5 7 6 7 5 4 4 6 4
Output
1 1 1 2 -1 1 1 3 1 1 | instruction | 0 | 66,550 | 12 | 133,100 |
Tags: dfs and similar, graphs, shortest paths
Correct Solution:
```
import sys
input = sys.stdin.readline
from collections import deque
N = 2*10**5 + 5
g = [[] for _ in range(N)]
n = int(input())
a = list(map(int, input().split()))
dist = [10**9]*N
for i in range(n):
if i - a[i] >= 0:
g[i-a[i]].append(i)
if a[i-a[i]]%2 != a[i]%2:
dist[i] = 1
if i + a[i] < n:
g[i+a[i]].append(i)
if a[i+a[i]]%2 != a[i]%2:
dist[i] = 1
q = deque([])
for i in range(n):
if dist[i] == 1:
q.append(i)
while len(q):
cur = q[0]
q.popleft()
for nxt in g[cur]:
if dist[nxt] > dist[cur]+1:
dist[nxt] = dist[cur]+1
q.append(nxt)
print(*[(dist[i] if dist[i] < 1e9 else -1) for i in range(n)])
``` | output | 1 | 66,550 | 12 | 133,101 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. In one move, you can jump from the position i to the position i - a_i (if 1 β€ i - a_i) or to the position i + a_i (if i + a_i β€ n).
For each position i from 1 to n you want to know the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa).
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the i-th element of a.
Output
Print n integers d_1, d_2, ..., d_n, where d_i is the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa) or -1 if it is impossible to reach such a position.
Example
Input
10
4 5 7 6 7 5 4 4 6 4
Output
1 1 1 2 -1 1 1 3 1 1 | instruction | 0 | 66,551 | 12 | 133,102 |
Tags: dfs and similar, graphs, shortest paths
Correct Solution:
```
import sys
from sys import stdin
from collections import deque
n = int(stdin.readline())
a = list(map(int,stdin.readline().split()))
lis = [ [] for i in range(n) ]
d = [float("inf")] * n
q = deque([])
for i in range(n):
if ( 0 <= i-a[i] and a[i]%2 != a[i-a[i]]%2) or (i+a[i] < n and a[i]%2 != a[i+a[i]]%2):
d[i] = 1
q.append(i)
else:
if i-a[i] >= 0:
lis[i-a[i]].append(i)
if i+a[i] < n:
lis[i+a[i]].append(i)
while q:
v = q.popleft()
for nex in lis[v]:
if d[nex] > d[v] + 1:
d[nex] = d[v] + 1
q.append(nex)
for i in range(n):
if d[i] == float("inf"):
d[i] = -1
print (*d)
``` | output | 1 | 66,551 | 12 | 133,103 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. In one move, you can jump from the position i to the position i - a_i (if 1 β€ i - a_i) or to the position i + a_i (if i + a_i β€ n).
For each position i from 1 to n you want to know the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa).
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the i-th element of a.
Output
Print n integers d_1, d_2, ..., d_n, where d_i is the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa) or -1 if it is impossible to reach such a position.
Example
Input
10
4 5 7 6 7 5 4 4 6 4
Output
1 1 1 2 -1 1 1 3 1 1 | instruction | 0 | 66,552 | 12 | 133,104 |
Tags: dfs and similar, graphs, shortest paths
Correct Solution:
```
from collections import deque
import sys
input = sys.stdin.readline
def bfs0(s):
q = deque()
dist = [inf] * n
for i in s:
q.append(i)
dist[i] = 0
while q:
i = q.popleft()
di = dist[i]
for j in G[i]:
if dist[j] > di + 1:
q.append(j)
dist[j] = di + 1
return dist
def bfs1(s):
q = deque()
dist = [inf] * n
for i in s:
q.append(i)
dist[i] = 0
while q:
i = q.popleft()
di = dist[i]
for j in H[i]:
if dist[j] > di + 1:
q.append(j)
dist[j] = di + 1
return dist
n = int(input())
a = list(map(int, input().split()))
G = [[] for _ in range(n)]
H = [[] for _ in range(n)]
s0, s1 = set(), set()
for i in range(n):
ai = a[i]
if ai % 2 == 0:
if i + ai < n:
G[i + ai].append(i)
if a[i + ai] % 2 == 1:
s0.add(i + ai)
if 0 <= i - ai:
G[i - ai].append(i)
if a[i - ai] % 2 == 1:
s0.add(i - ai)
else:
if i + ai < n:
H[i + ai].append(i)
if a[i + ai] % 2 == 0:
s1.add(i + ai)
if 0 <= i - ai:
H[i - ai].append(i)
if a[i - ai] % 2 == 0:
s1.add(i - ai)
inf = 1145141919
dist0 = bfs0(s0)
dist1 = bfs1(s1)
ans = []
for i in range(n):
m = inf
if 0 < dist0[i]:
m = min(m, dist0[i] + 1)
if 0 < dist1[i]:
m = min(m, dist1[i] + 1)
m %= inf
m -= 1
ans.append(m)
print(*ans)
``` | output | 1 | 66,552 | 12 | 133,105 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. In one move, you can jump from the position i to the position i - a_i (if 1 β€ i - a_i) or to the position i + a_i (if i + a_i β€ n).
For each position i from 1 to n you want to know the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa).
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the i-th element of a.
Output
Print n integers d_1, d_2, ..., d_n, where d_i is the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa) or -1 if it is impossible to reach such a position.
Example
Input
10
4 5 7 6 7 5 4 4 6 4
Output
1 1 1 2 -1 1 1 3 1 1 | instruction | 0 | 66,553 | 12 | 133,106 |
Tags: dfs and similar, graphs, shortest paths
Correct Solution:
```
n = int(input())
parities = list(map(int, input().strip().split()))
first_layer = []
ans = [-1 for i in range(n)]
hopTo = [[] for i in range(n)]
for i in range(n):
appended = False
if i - parities[i] >= 0:
hopTo[i - parities[i]].append(i)
if (parities[i] + parities[i - parities[i]]) %2:
first_layer.append(i)
appended = True
if i + parities[i] <= n-1:
hopTo[i + parities[i]].append(i)
if (parities[i] + parities[i + parities[i]]) %2 and appended == False:
first_layer.append(i)
calculated = 0
for i in first_layer:
ans[i] = 1
calculated += 1
def next_layer(cur_layer,calced,layer_num):
new_layer = []
for point in cur_layer:
for next_point in hopTo[point]:
if ans[next_point] == -1:
ans[next_point] = layer_num + 1
new_layer.append(next_point)
calced += 1
return [new_layer, calced, layer_num+1]
cur_layer = first_layer
layer_num = 1
while 1:
data = next_layer(cur_layer,calculated,layer_num)
cur_layer = data[0]
calculated = data[1]
layer_num = data[2]
if len(cur_layer) == 0:
for i in ans:
print(i, end = ' ')
break
``` | output | 1 | 66,553 | 12 | 133,107 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. In one move, you can jump from the position i to the position i - a_i (if 1 β€ i - a_i) or to the position i + a_i (if i + a_i β€ n).
For each position i from 1 to n you want to know the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa).
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the i-th element of a.
Output
Print n integers d_1, d_2, ..., d_n, where d_i is the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa) or -1 if it is impossible to reach such a position.
Example
Input
10
4 5 7 6 7 5 4 4 6 4
Output
1 1 1 2 -1 1 1 3 1 1 | instruction | 0 | 66,554 | 12 | 133,108 |
Tags: dfs and similar, graphs, shortest paths
Correct Solution:
```
# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------- fast io --------------------
from math import gcd, ceil
def pre(s):
n = len(s)
pi = [0] * n
for i in range(1, n):
j = pi[i - 1]
while j and s[i] != s[j]:
j = pi[j - 1]
if s[i] == s[j]:
j += 1
pi[i] = j
return pi
def prod(a):
ans = 1
for each in a:
ans = (ans * each)
return ans
def lcm(a, b): return a * b // gcd(a, b)
from math import inf
from collections import deque
def binary(x, length=16):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
for _ in range(int(input()) if not True else 1):
n = int(input())
a = list(map(int, input().split()))
ans = [-1]*n
oof = [[] for __ in range(n)]
oof2 = [[] for __ in range(n)]
q1, q2 = deque(), deque()
for i in range(n):
if a[i] % 2:
q2.append(i)
if i+a[i] < n:
oof2[i+a[i]] += [i]
if i - a[i] >= 0:
oof2[i-a[i]] += [i]
else:
q1.append(i)
if i+a[i] < n:
oof[i+a[i]] += [i]
if i - a[i] >= 0:
oof[i-a[i]] += [i]
ans1, ans2 = [0]*n, [0]*n
while q1:
next = q1.popleft()
for each in oof2[next]:
if not ans1[each]:
ans1[each] = ans1[next] + 1
q1.append(each)
while q2:
next = q2.popleft()
for each in oof[next]:
if not ans2[each]:
ans2[each] = ans2[next] + 1
q2.append(each)
for i in range(n):
if (a[i] % 2 and ans1[i]) or (not a[i] % 2 and ans2[i]):
ans[i] = max(ans1[i], ans2[i])
print(*ans)
``` | output | 1 | 66,554 | 12 | 133,109 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a consisting of n integers. In one move, you can jump from the position i to the position i - a_i (if 1 β€ i - a_i) or to the position i + a_i (if i + a_i β€ n).
For each position i from 1 to n you want to know the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa).
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the i-th element of a.
Output
Print n integers d_1, d_2, ..., d_n, where d_i is the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa) or -1 if it is impossible to reach such a position.
Example
Input
10
4 5 7 6 7 5 4 4 6 4
Output
1 1 1 2 -1 1 1 3 1 1 | instruction | 0 | 66,555 | 12 | 133,110 |
Tags: dfs and similar, graphs, shortest paths
Correct Solution:
```
from sys import stdin,stdout
from collections import deque
n = int(stdin.readline().strip())
alist = list(map(int, stdin.readline().split()))
nop = {}
p = [set() for _ in range(n)]
def back(next):
while next:
i,c = next.popleft()
if c < nop[i]:
nop[i] = c
c+=1
for j in p[i]:
next.append((j,c))
def bfs(x):
if x not in nop:
next = deque([x])
pr = alist[x]%2
nop[x] = n
while next:
i = next.popleft()
for j in [i-alist[i],i+alist[i]]:
if j >= 0 and j<n:
p[j].add(i)
if alist[j]%2!=pr:
back(deque([(i,1)]))
elif j in nop:
back(deque([(i,nop[j]+1)]))
else:
nop[j] = n
next.append(j)
return nop[x] if nop[x]<n else -1
stdout.write(' '.join(str(bfs(x)) for x in range(n)))
``` | output | 1 | 66,555 | 12 | 133,111 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers. In one move, you can jump from the position i to the position i - a_i (if 1 β€ i - a_i) or to the position i + a_i (if i + a_i β€ n).
For each position i from 1 to n you want to know the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa).
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the i-th element of a.
Output
Print n integers d_1, d_2, ..., d_n, where d_i is the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa) or -1 if it is impossible to reach such a position.
Example
Input
10
4 5 7 6 7 5 4 4 6 4
Output
1 1 1 2 -1 1 1 3 1 1
Submitted Solution:
```
from collections import deque
n = int(input())
a = list(map(int, input().split()))
d = [-1] * n
nb = [[] for i in range(n)]
for i in range(n):
for j in [i - a[i], i + a[i]]:
if 0 <= j < n:
if (a[i] & 1) == (a[j] & 1):
nb[j].append(i)
else:
d[i] = 1
queue = deque()
for i in range(n):
if d[i] == 1:
queue.append(i)
while len(queue) > 0:
cur = queue.popleft()
for x in nb[cur]:
if d[x] == -1:
d[x] = d[cur] + 1
queue.append(x)
print(*d)
``` | instruction | 0 | 66,556 | 12 | 133,112 |
Yes | output | 1 | 66,556 | 12 | 133,113 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers. In one move, you can jump from the position i to the position i - a_i (if 1 β€ i - a_i) or to the position i + a_i (if i + a_i β€ n).
For each position i from 1 to n you want to know the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa).
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the i-th element of a.
Output
Print n integers d_1, d_2, ..., d_n, where d_i is the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa) or -1 if it is impossible to reach such a position.
Example
Input
10
4 5 7 6 7 5 4 4 6 4
Output
1 1 1 2 -1 1 1 3 1 1
Submitted Solution:
```
# -*- coding: utf-8 -*-
import sys
from collections import deque
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
N = INT()
A = LIST()
que = deque()
nodes = [[] for i in range(N)]
for i, a in enumerate(A):
if i + a < N:
nodes[i+a].append(i)
if a % 2 != A[i+a] % 2:
que.append((i, 1))
continue
if i - a >= 0:
nodes[i-a].append(i)
if a % 2 != A[i-a] % 2:
que.append((i, 1))
ans = [-1] * N
while que:
u, cost = que.popleft()
if ans[u] != -1:
continue
ans[u] = cost
for v in nodes[u]:
que.append((v, cost+1))
print(*ans)
``` | instruction | 0 | 66,558 | 12 | 133,116 |
Yes | output | 1 | 66,558 | 12 | 133,117 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers. In one move, you can jump from the position i to the position i - a_i (if 1 β€ i - a_i) or to the position i + a_i (if i + a_i β€ n).
For each position i from 1 to n you want to know the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa).
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the i-th element of a.
Output
Print n integers d_1, d_2, ..., d_n, where d_i is the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa) or -1 if it is impossible to reach such a position.
Example
Input
10
4 5 7 6 7 5 4 4 6 4
Output
1 1 1 2 -1 1 1 3 1 1
Submitted Solution:
```
import sys
from collections import deque
input=sys.stdin.readline
n=int(input())
a=list(map(int,input().split()))
INF=10**18
ans=[INF]*n
def bfs(starts,ends):
global ans
d=[INF]*n
dq=deque([])
for x in starts:
d[x]=0
dq.append(x)
while dq:
ver=dq.popleft()
for to in inv_g[ver]:
if d[to]==INF:
d[to]=d[ver]+1
dq.append(to)
for i in ends:
if d[i]!=INF:
ans[i]=d[i]
inv_g=[[] for i in range(n)]
for i in range(n):
if i-a[i]>=0:
inv_g[i-a[i]].append(i)
if i+a[i]<n:
inv_g[i+a[i]].append(i)
odd=[];even=[]
for i in range(n):
if a[i]%2:
odd.append(i)
else:
even.append(i)
bfs(odd,even)
bfs(even,odd)
for i in range(n):
if ans[i]==INF:
ans[i]=-1
print(*ans)
``` | instruction | 0 | 66,559 | 12 | 133,118 |
Yes | output | 1 | 66,559 | 12 | 133,119 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers. In one move, you can jump from the position i to the position i - a_i (if 1 β€ i - a_i) or to the position i + a_i (if i + a_i β€ n).
For each position i from 1 to n you want to know the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa).
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the i-th element of a.
Output
Print n integers d_1, d_2, ..., d_n, where d_i is the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa) or -1 if it is impossible to reach such a position.
Example
Input
10
4 5 7 6 7 5 4 4 6 4
Output
1 1 1 2 -1 1 1 3 1 1
Submitted Solution:
```
from sys import stdin, stdout, setrecursionlimit
input = stdin.readline
# import string
# characters = string.ascii_lowercase
# digits = string.digits
# setrecursionlimit(int(1e6))
# dir = [-1,0,1,0,-1]
# moves = 'NESW'
inf = float('inf')
from functools import cmp_to_key
from collections import defaultdict as dd
from collections import Counter, deque
from heapq import *
import math
from math import floor, ceil, sqrt
def geti(): return map(int, input().strip().split())
def getl(): return list(map(int, input().strip().split()))
def getis(): return map(str, input().strip().split())
def getls(): return list(map(str, input().strip().split()))
def gets(): return input().strip()
def geta(): return int(input())
def print_s(s): stdout.write(s+'\n')
def solve():
n = geta()
a = getl()
cost = [inf]*n
vis = [False]*n
def dfs(index):
# print(index)
vis[index] = True
next = a[index] + index
prev = index - a[index]
for i in [next, prev]:
if 0 <= i < n:
if (a[i]^a[index])&1:
cost[index] = 1
return
elif cost[i] != inf:
cost[index] = min(cost[index], cost[i] + 1)
for i in [next, prev]:
if 0 <= i < n:
if not vis[i]:
dfs(i)
cost[index] = min(cost[index], cost[i] + 1)
for i in range(n):
if not vis[i]:
# print('s', i)
dfs(i)
# print(cost)
cost = [i if i != inf else -1 for i in cost]
print(*cost)
if __name__=='__main__':
solve()
``` | instruction | 0 | 66,560 | 12 | 133,120 |
No | output | 1 | 66,560 | 12 | 133,121 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers. In one move, you can jump from the position i to the position i - a_i (if 1 β€ i - a_i) or to the position i + a_i (if i + a_i β€ n).
For each position i from 1 to n you want to know the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa).
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the i-th element of a.
Output
Print n integers d_1, d_2, ..., d_n, where d_i is the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa) or -1 if it is impossible to reach such a position.
Example
Input
10
4 5 7 6 7 5 4 4 6 4
Output
1 1 1 2 -1 1 1 3 1 1
Submitted Solution:
```
n = int(input())
parities = list(map(int, input().strip().split()))
first_layer = []
ans = [-1 for i in range(n)]
hopTo = [[] for i in range(n)]
for i in range(n):
appended = False
if i - parities[i] >= 0:
hopTo[i - parities[i]].append(i)
if (parities[i] + parities[i - parities[i]]) %2:
first_layer.append(i)
appended = True
if i + parities[i] <= n-1:
hopTo[i + parities[i]].append(i)
if (parities[i] + parities[i + parities[i]]) %2 and appended == False:
first_layer.append(i)
calculated = 0
for i in first_layer:
ans[i] = 1
calculated += 1
def next_layer(cur_layer,calced,layer_num):
new_layer = []
for point in cur_layer:
for next_point in hopTo[point]:
if ans[next_point] == -1:
ans[next_point] = layer_num + 1
new_layer.append(next_point)
calced += 1
return [new_layer, calced, layer_num+1]
cur_layer = first_layer
print(first_layer)
layer_num = 1
while 1:
data = next_layer(cur_layer,calculated,layer_num)
cur_layer = data[0]
calculated = data[1]
layer_num = data[2]
if len(cur_layer) == 0:
for i in ans:
print(i, end = ' ')
break
``` | instruction | 0 | 66,561 | 12 | 133,122 |
No | output | 1 | 66,561 | 12 | 133,123 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a consisting of n integers. In one move, you can jump from the position i to the position i - a_i (if 1 β€ i - a_i) or to the position i + a_i (if i + a_i β€ n).
For each position i from 1 to n you want to know the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa).
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ n), where a_i is the i-th element of a.
Output
Print n integers d_1, d_2, ..., d_n, where d_i is the minimum the number of moves required to reach any position j such that a_j has the opposite parity from a_i (i.e. if a_i is odd then a_j has to be even and vice versa) or -1 if it is impossible to reach such a position.
Example
Input
10
4 5 7 6 7 5 4 4 6 4
Output
1 1 1 2 -1 1 1 3 1 1
Submitted Solution:
```
n = int(input())
A = [0] + [int(x) for x in input().strip().split()]
# print(A)
inf = 1000000
dic = {}
visited = set()
def fstep(p,f):
# print("fstep", p)
if (p,f) in dic:
return dic[(p,f)]
if (p,f) in visited:
# print ("visited", p)
return inf
parity = A[p] % 2
visited.add((p,f))
m = inf
# print(parity)
if p - A[p] > 0 and A[p - A[p]] % 2 != parity:
# print("q1")
m = 1
elif p + A[p] <= n and A[p + A[p]] % 2 != parity:
# print("q2")
m = 1
else:
if p - A[p] > 0:
m = 1+fstep(p - A[p],p)
if p + A[p] <= n:
m = min(m, 1+ fstep(p + A[p],p))
dic[(p,f)] = m
# print ("dict", p, m)
return m
# print (fstep(97))
print(' '.join([str(-1 if fstep(i,i) >= inf else fstep(i,i)) for i in range(1, n+1)]))
``` | instruction | 0 | 66,562 | 12 | 133,124 |
No | output | 1 | 66,562 | 12 | 133,125 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have an array a_1, a_2, ..., a_n where a_i = i.
In one step, you can choose two indices x and y (x β y) and set a_x = \leftβ (a_x)/(a_y) \rightβ (ceiling function).
Your goal is to make array a consist of n - 1 ones and 1 two in no more than n + 5 steps. Note that you don't have to minimize the number of steps.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains the single integer n (3 β€ n β€ 2 β
10^5) β the length of array a.
It's guaranteed that the sum of n over test cases doesn't exceed 2 β
10^5.
Output
For each test case, print the sequence of operations that will make a as n - 1 ones and 1 two in the following format: firstly, print one integer m (m β€ n + 5) β the number of operations; next print m pairs of integers x and y (1 β€ x, y β€ n; x β y) (x may be greater or less than y) β the indices of the corresponding operation.
It can be proven that for the given constraints it's always possible to find a correct sequence of operations.
Example
Input
2
3
4
Output
2
3 2
3 2
3
3 4
4 2
4 2
Note
In the first test case, you have array a = [1, 2, 3]. For example, you can do the following:
1. choose 3, 2: a_3 = \leftβ (a_3)/(a_2) \rightβ = 2 and array a = [1, 2, 2];
2. choose 3, 2: a_3 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1].
You've got array with 2 ones and 1 two in 2 steps.
In the second test case, a = [1, 2, 3, 4]. For example, you can do the following:
1. choose 3, 4: a_3 = \leftβ 3/4 \rightβ = 1 and array a = [1, 2, 1, 4];
2. choose 4, 2: a_4 = \leftβ 4/2 \rightβ = 2 and array a = [1, 2, 1, 2];
3. choose 4, 2: a_4 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1, 1]. | instruction | 0 | 66,656 | 12 | 133,312 |
Tags: brute force, constructive algorithms, math, number theory
Correct Solution:
```
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
inp = lambda: list(map(int,sys.stdin.readline().rstrip("\r\n").split()))
#______________________________________________________________________________________________________
from math import *
# from bisect import *
# from heapq import *
# from collections import defaultdict as dd
# from collections import OrderedDict as odict
# from collections import Counter as cc
# from collections import deque
# sys.setrecursionlimit(2*(10**5)+100) this is must for dfs
# mod = 10**9+7; md = 998244353
# ______________________________________________________________________________________________________
# segment tree for range minimum query
# sys.setrecursionlimit(10**5)
# n = int(input())
# a = list(map(int,input().split()))
# st = [float('inf') for i in range(4*len(a))]
# def build(a,ind,start,end):
# if start == end:
# st[ind] = a[start]
# else:
# mid = (start+end)//2
# build(a,2*ind+1,start,mid)
# build(a,2*ind+2,mid+1,end)
# st[ind] = min(st[2*ind+1],st[2*ind+2])
# build(a,0,0,n-1)
# def query(ind,l,r,start,end):
# if start>r or end<l:
# return float('inf')
# if l<=start<=end<=r:
# return st[ind]
# mid = (start+end)//2
# return min(query(2*ind+1,l,r,start,mid),query(2*ind+2,l,r,mid+1,end))
# ______________________________________________________________________________________________________
# Checking prime in O(root(N))
# def isprime(n):
# if (n % 2 == 0 and n > 2) or n == 1: return 0
# else:
# s = int(n**(0.5)) + 1
# for i in range(3, s, 2):
# if n % i == 0:
# return 0
# return 1
# def lcm(a,b):
# return (a*b)//gcd(a,b)
# ______________________________________________________________________________________________________
# nCr under mod
# def C(n,r,mod):
# if r>n:
# return 0
# num = den = 1
# for i in range(r):
# num = (num*(n-i))%mod
# den = (den*(i+1))%mod
# return (num*pow(den,mod-2,mod))%mod
# M = 10**5 +10
# ______________________________________________________________________________________________________
# For smallest prime factor of a number
# M = 1000010
# pfc = [i for i in range(M)]
# def pfcs(M):
# for i in range(2,M):
# if pfc[i]==i:
# for j in range(i+i,M,i):
# if pfc[j]==j:
# pfc[j] = i
# return
# pfcs(M)
# ______________________________________________________________________________________________________
tc = 1
tc, = inp()
for _ in range(tc):
n, = inp()
m = n
a = [n]
while(m!=2):
t = ceil(m**0.5)
a.append(t)
m = t
s = set(a)
ans = [i for i in range(1,n+1)]
res = []
for j in range(3,n):
if j not in s:
res.append((j,j+1))
for i in range(1,len(a)):
res.append((a[i-1],a[i]))
res.append((a[i-1],a[i]))
print(len(res))
for i,j in res:
print(i,j)
``` | output | 1 | 66,656 | 12 | 133,313 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have an array a_1, a_2, ..., a_n where a_i = i.
In one step, you can choose two indices x and y (x β y) and set a_x = \leftβ (a_x)/(a_y) \rightβ (ceiling function).
Your goal is to make array a consist of n - 1 ones and 1 two in no more than n + 5 steps. Note that you don't have to minimize the number of steps.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains the single integer n (3 β€ n β€ 2 β
10^5) β the length of array a.
It's guaranteed that the sum of n over test cases doesn't exceed 2 β
10^5.
Output
For each test case, print the sequence of operations that will make a as n - 1 ones and 1 two in the following format: firstly, print one integer m (m β€ n + 5) β the number of operations; next print m pairs of integers x and y (1 β€ x, y β€ n; x β y) (x may be greater or less than y) β the indices of the corresponding operation.
It can be proven that for the given constraints it's always possible to find a correct sequence of operations.
Example
Input
2
3
4
Output
2
3 2
3 2
3
3 4
4 2
4 2
Note
In the first test case, you have array a = [1, 2, 3]. For example, you can do the following:
1. choose 3, 2: a_3 = \leftβ (a_3)/(a_2) \rightβ = 2 and array a = [1, 2, 2];
2. choose 3, 2: a_3 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1].
You've got array with 2 ones and 1 two in 2 steps.
In the second test case, a = [1, 2, 3, 4]. For example, you can do the following:
1. choose 3, 4: a_3 = \leftβ 3/4 \rightβ = 1 and array a = [1, 2, 1, 4];
2. choose 4, 2: a_4 = \leftβ 4/2 \rightβ = 2 and array a = [1, 2, 1, 2];
3. choose 4, 2: a_4 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1, 1]. | instruction | 0 | 66,657 | 12 | 133,314 |
Tags: brute force, constructive algorithms, math, number theory
Correct Solution:
```
from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353
def inp(): return stdin.readline().strip()
def out(var, end="\n"): stdout.write(str(var)+"\n")
def outa(*var, end="\n"): stdout.write(' '.join(map(str, var)) + end)
def lmp(): return list(mp())
def mp(): return map(int, inp().split())
def smp(): return map(str, inp().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
def remadd(x, y): return 1 if x%y else 0
def ceil(a,b): return (a+b-1)//b
def isprime(x):
if x<=1: return False
if x in (2, 3): return True
if x%2 == 0: return False
for i in range(3, int(sqrt(x))+1, 2):
if x%i == 0: return False
return True
for _ in range(int(inp())):
n = int(inp())
arr = [2, 2**2, 2**4, 2**8, 2**16]
mx = n
ans = []
for i in range(n-1, 1, -1):
if i in arr:
k = mx
while(mx!=1):
mx = ceil(mx, i)
ans.append((k, i))
mx = i
else: ans.append((i, mx))
print(len(ans))
for i in ans:
print(*i)
``` | output | 1 | 66,657 | 12 | 133,315 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have an array a_1, a_2, ..., a_n where a_i = i.
In one step, you can choose two indices x and y (x β y) and set a_x = \leftβ (a_x)/(a_y) \rightβ (ceiling function).
Your goal is to make array a consist of n - 1 ones and 1 two in no more than n + 5 steps. Note that you don't have to minimize the number of steps.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains the single integer n (3 β€ n β€ 2 β
10^5) β the length of array a.
It's guaranteed that the sum of n over test cases doesn't exceed 2 β
10^5.
Output
For each test case, print the sequence of operations that will make a as n - 1 ones and 1 two in the following format: firstly, print one integer m (m β€ n + 5) β the number of operations; next print m pairs of integers x and y (1 β€ x, y β€ n; x β y) (x may be greater or less than y) β the indices of the corresponding operation.
It can be proven that for the given constraints it's always possible to find a correct sequence of operations.
Example
Input
2
3
4
Output
2
3 2
3 2
3
3 4
4 2
4 2
Note
In the first test case, you have array a = [1, 2, 3]. For example, you can do the following:
1. choose 3, 2: a_3 = \leftβ (a_3)/(a_2) \rightβ = 2 and array a = [1, 2, 2];
2. choose 3, 2: a_3 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1].
You've got array with 2 ones and 1 two in 2 steps.
In the second test case, a = [1, 2, 3, 4]. For example, you can do the following:
1. choose 3, 4: a_3 = \leftβ 3/4 \rightβ = 1 and array a = [1, 2, 1, 4];
2. choose 4, 2: a_4 = \leftβ 4/2 \rightβ = 2 and array a = [1, 2, 1, 2];
3. choose 4, 2: a_4 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1, 1]. | instruction | 0 | 66,658 | 12 | 133,316 |
Tags: brute force, constructive algorithms, math, number theory
Correct Solution:
```
from math import ceil, sqrt
t = int(input())
for i in range(t):
n = int(input())
keep = []
temp_n = n
while temp_n >= 3:
keep.append(temp_n)
temp_n = ceil(sqrt(temp_n))
keep.append(2)
print(n - 3 + len(keep))
keep_index = 0
for j in range(n, 1, -1):
if keep[keep_index] == j:
keep_index += 1
else:
print(str(j) + ' ' + str(n))
for j in range(len(keep) - 1):
print(str(keep[j]) + ' ' + str(keep[j + 1]))
print(str(keep[j]) + ' ' + str(keep[j + 1]))
``` | output | 1 | 66,658 | 12 | 133,317 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have an array a_1, a_2, ..., a_n where a_i = i.
In one step, you can choose two indices x and y (x β y) and set a_x = \leftβ (a_x)/(a_y) \rightβ (ceiling function).
Your goal is to make array a consist of n - 1 ones and 1 two in no more than n + 5 steps. Note that you don't have to minimize the number of steps.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains the single integer n (3 β€ n β€ 2 β
10^5) β the length of array a.
It's guaranteed that the sum of n over test cases doesn't exceed 2 β
10^5.
Output
For each test case, print the sequence of operations that will make a as n - 1 ones and 1 two in the following format: firstly, print one integer m (m β€ n + 5) β the number of operations; next print m pairs of integers x and y (1 β€ x, y β€ n; x β y) (x may be greater or less than y) β the indices of the corresponding operation.
It can be proven that for the given constraints it's always possible to find a correct sequence of operations.
Example
Input
2
3
4
Output
2
3 2
3 2
3
3 4
4 2
4 2
Note
In the first test case, you have array a = [1, 2, 3]. For example, you can do the following:
1. choose 3, 2: a_3 = \leftβ (a_3)/(a_2) \rightβ = 2 and array a = [1, 2, 2];
2. choose 3, 2: a_3 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1].
You've got array with 2 ones and 1 two in 2 steps.
In the second test case, a = [1, 2, 3, 4]. For example, you can do the following:
1. choose 3, 4: a_3 = \leftβ 3/4 \rightβ = 1 and array a = [1, 2, 1, 4];
2. choose 4, 2: a_4 = \leftβ 4/2 \rightβ = 2 and array a = [1, 2, 1, 2];
3. choose 4, 2: a_4 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1, 1]. | instruction | 0 | 66,659 | 12 | 133,318 |
Tags: brute force, constructive algorithms, math, number theory
Correct Solution:
```
import sys
input = sys.stdin.readline
import math
t = int(input())
for _ in range(t):
n = int(input())
a = [0]+list(range(1, n+1))
ans = []
cnt = 0
for i in range(n-1, 2, -1):
if i*i < a[n]:
ans.append([n, i])
a[n] = math.ceil(a[n]/i)
cnt += 1
ans.append([i, n])
a[i] = math.ceil(a[i]/n)
cnt += 1
while a[n] > 1:
ans.append([n, 2])
a[n] = math.ceil(a[n]/2)
cnt += 1
print(len(ans))
for i in range(len(ans)):
print(*ans[i])
``` | output | 1 | 66,659 | 12 | 133,319 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have an array a_1, a_2, ..., a_n where a_i = i.
In one step, you can choose two indices x and y (x β y) and set a_x = \leftβ (a_x)/(a_y) \rightβ (ceiling function).
Your goal is to make array a consist of n - 1 ones and 1 two in no more than n + 5 steps. Note that you don't have to minimize the number of steps.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains the single integer n (3 β€ n β€ 2 β
10^5) β the length of array a.
It's guaranteed that the sum of n over test cases doesn't exceed 2 β
10^5.
Output
For each test case, print the sequence of operations that will make a as n - 1 ones and 1 two in the following format: firstly, print one integer m (m β€ n + 5) β the number of operations; next print m pairs of integers x and y (1 β€ x, y β€ n; x β y) (x may be greater or less than y) β the indices of the corresponding operation.
It can be proven that for the given constraints it's always possible to find a correct sequence of operations.
Example
Input
2
3
4
Output
2
3 2
3 2
3
3 4
4 2
4 2
Note
In the first test case, you have array a = [1, 2, 3]. For example, you can do the following:
1. choose 3, 2: a_3 = \leftβ (a_3)/(a_2) \rightβ = 2 and array a = [1, 2, 2];
2. choose 3, 2: a_3 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1].
You've got array with 2 ones and 1 two in 2 steps.
In the second test case, a = [1, 2, 3, 4]. For example, you can do the following:
1. choose 3, 4: a_3 = \leftβ 3/4 \rightβ = 1 and array a = [1, 2, 1, 4];
2. choose 4, 2: a_4 = \leftβ 4/2 \rightβ = 2 and array a = [1, 2, 1, 2];
3. choose 4, 2: a_4 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1, 1]. | instruction | 0 | 66,660 | 12 | 133,320 |
Tags: brute force, constructive algorithms, math, number theory
Correct Solution:
```
import math
def move2(n):
move=0
while n!=1:
n=math.ceil(n/2)
move+=1
return move
def moven(n,k):
move=0
while n!=1:
n=math.ceil(n/k)
move+=1
return move
def solve():
n=int(input())
ans=[]
for i in range(3,n+1):
move1=move2(i)
move=moven(n,i)
if move+move1<=9:
divisor=i
break
if n==3:
print(2)
print(3,2)
print(3,2)
else:
for i in range(3,n):
if i==divisor: continue
ans.append([i,i+1])
for i in range(move):
ans.append([n,divisor])
for i in range(move1):
ans.append([divisor,2])
print(len(ans))
for lst in ans: print(lst[0],lst[1])
for i in range(int(input())):
solve()
``` | output | 1 | 66,660 | 12 | 133,321 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have an array a_1, a_2, ..., a_n where a_i = i.
In one step, you can choose two indices x and y (x β y) and set a_x = \leftβ (a_x)/(a_y) \rightβ (ceiling function).
Your goal is to make array a consist of n - 1 ones and 1 two in no more than n + 5 steps. Note that you don't have to minimize the number of steps.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains the single integer n (3 β€ n β€ 2 β
10^5) β the length of array a.
It's guaranteed that the sum of n over test cases doesn't exceed 2 β
10^5.
Output
For each test case, print the sequence of operations that will make a as n - 1 ones and 1 two in the following format: firstly, print one integer m (m β€ n + 5) β the number of operations; next print m pairs of integers x and y (1 β€ x, y β€ n; x β y) (x may be greater or less than y) β the indices of the corresponding operation.
It can be proven that for the given constraints it's always possible to find a correct sequence of operations.
Example
Input
2
3
4
Output
2
3 2
3 2
3
3 4
4 2
4 2
Note
In the first test case, you have array a = [1, 2, 3]. For example, you can do the following:
1. choose 3, 2: a_3 = \leftβ (a_3)/(a_2) \rightβ = 2 and array a = [1, 2, 2];
2. choose 3, 2: a_3 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1].
You've got array with 2 ones and 1 two in 2 steps.
In the second test case, a = [1, 2, 3, 4]. For example, you can do the following:
1. choose 3, 4: a_3 = \leftβ 3/4 \rightβ = 1 and array a = [1, 2, 1, 4];
2. choose 4, 2: a_4 = \leftβ 4/2 \rightβ = 2 and array a = [1, 2, 1, 2];
3. choose 4, 2: a_4 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1, 1]. | instruction | 0 | 66,661 | 12 | 133,322 |
Tags: brute force, constructive algorithms, math, number theory
Correct Solution:
```
from sys import stdin,stdout #
import math #
import heapq #
#
t = 1 #
def aint(): #
return int(input().strip()) #
def lint(): #
return list(map(int,input().split())) #
def fint(): #
return list(map(int,stdin.readline().split())) #
#
########################################################
def operate(ar, x, y):
if x!=y:
ar[x] = (ar[x]-1)//ar[y] +1
def main():
n=aint()
i=n-1
ar=[i for i in range(n+1)]
ans = []
while i>2:
if i*i>=ar[n]:
operate(ar, i, n)
ans.append([i,n])
i-=1
else:
operate(ar, n, i)
ans.append([n,i])
while ar[n]!=1:
operate(ar, n, 2)
ans.append([n,2])
print(len(ans))
for i in ans:
print(*i)
#solve
t=int(input())
########################################################
for i in range(t): #
#print("Case #"+str(i+1)+":",end=" ") #
main() #
``` | output | 1 | 66,661 | 12 | 133,323 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have an array a_1, a_2, ..., a_n where a_i = i.
In one step, you can choose two indices x and y (x β y) and set a_x = \leftβ (a_x)/(a_y) \rightβ (ceiling function).
Your goal is to make array a consist of n - 1 ones and 1 two in no more than n + 5 steps. Note that you don't have to minimize the number of steps.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains the single integer n (3 β€ n β€ 2 β
10^5) β the length of array a.
It's guaranteed that the sum of n over test cases doesn't exceed 2 β
10^5.
Output
For each test case, print the sequence of operations that will make a as n - 1 ones and 1 two in the following format: firstly, print one integer m (m β€ n + 5) β the number of operations; next print m pairs of integers x and y (1 β€ x, y β€ n; x β y) (x may be greater or less than y) β the indices of the corresponding operation.
It can be proven that for the given constraints it's always possible to find a correct sequence of operations.
Example
Input
2
3
4
Output
2
3 2
3 2
3
3 4
4 2
4 2
Note
In the first test case, you have array a = [1, 2, 3]. For example, you can do the following:
1. choose 3, 2: a_3 = \leftβ (a_3)/(a_2) \rightβ = 2 and array a = [1, 2, 2];
2. choose 3, 2: a_3 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1].
You've got array with 2 ones and 1 two in 2 steps.
In the second test case, a = [1, 2, 3, 4]. For example, you can do the following:
1. choose 3, 4: a_3 = \leftβ 3/4 \rightβ = 1 and array a = [1, 2, 1, 4];
2. choose 4, 2: a_4 = \leftβ 4/2 \rightβ = 2 and array a = [1, 2, 1, 2];
3. choose 4, 2: a_4 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1, 1]. | instruction | 0 | 66,662 | 12 | 133,324 |
Tags: brute force, constructive algorithms, math, number theory
Correct Solution:
```
t = int(input())
for i in range(t):
n = int(input())
if n == 3:
print(2)
print(3,2)
print(3,2)
continue
if n < 28:
num = n
temp = 0
while num > 1:
temp2 = int(num % 3 != 0)
num //= 3
num += temp2
temp += 1
print(n-4+temp+2)
for i in range(3,n-1):
print(i+1,n)
for i in range(temp):
print(n,3)
print(3,2)
print(3,2)
elif n < 257:
num = n
temp = 0
while num > 1:
temp2 = int(num % 27 != 0)
num //= 27
num += temp2
temp += 1
print(n-5+temp+5)
for i in range(3,26):
print(i+1,n)
for i in range(27,n-1):
print(i+1,n)
for i in range(temp):
print(n,27)
print(27,3)
print(27,3)
print(27,3)
print(3,2)
print(3,2)
else:
num = n
temp = 0
while num > 1:
temp2 = int(num % 256 != 0)
num //= 256
num += temp2
temp += 1
print(n-5+temp+6)
print(3,n)
for i in range(4,255):
print(i+1,n)
for i in range(256,n-1):
print(i+1,n)
for i in range(temp):
print(n,256)
print(256,4)
print(256,4)
print(256,4)
print(256,4)
print(4,2)
print(4,2)
``` | output | 1 | 66,662 | 12 | 133,325 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have an array a_1, a_2, ..., a_n where a_i = i.
In one step, you can choose two indices x and y (x β y) and set a_x = \leftβ (a_x)/(a_y) \rightβ (ceiling function).
Your goal is to make array a consist of n - 1 ones and 1 two in no more than n + 5 steps. Note that you don't have to minimize the number of steps.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains the single integer n (3 β€ n β€ 2 β
10^5) β the length of array a.
It's guaranteed that the sum of n over test cases doesn't exceed 2 β
10^5.
Output
For each test case, print the sequence of operations that will make a as n - 1 ones and 1 two in the following format: firstly, print one integer m (m β€ n + 5) β the number of operations; next print m pairs of integers x and y (1 β€ x, y β€ n; x β y) (x may be greater or less than y) β the indices of the corresponding operation.
It can be proven that for the given constraints it's always possible to find a correct sequence of operations.
Example
Input
2
3
4
Output
2
3 2
3 2
3
3 4
4 2
4 2
Note
In the first test case, you have array a = [1, 2, 3]. For example, you can do the following:
1. choose 3, 2: a_3 = \leftβ (a_3)/(a_2) \rightβ = 2 and array a = [1, 2, 2];
2. choose 3, 2: a_3 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1].
You've got array with 2 ones and 1 two in 2 steps.
In the second test case, a = [1, 2, 3, 4]. For example, you can do the following:
1. choose 3, 4: a_3 = \leftβ 3/4 \rightβ = 1 and array a = [1, 2, 1, 4];
2. choose 4, 2: a_4 = \leftβ 4/2 \rightβ = 2 and array a = [1, 2, 1, 2];
3. choose 4, 2: a_4 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1, 1]. | instruction | 0 | 66,663 | 12 | 133,326 |
Tags: brute force, constructive algorithms, 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
def SieveOfEratosthenes(n):
prime=[]
primes = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (primes[p] == True):
prime.append(p)
for i in range(p * p, n+1, p):
primes[i] = False
p += 1
return prime
#--------------------------------------------------------
for j in range(int(input())):
n = int(input())
y = []
while(n > 2):
i = n-1
while((i-1)**2 >= n):
y.append([i, n])
i -= 1
y.append([n, i])
y.append([n, i])
n = i
print(len(y))
for i in y:
print(*i)
``` | output | 1 | 66,663 | 12 | 133,327 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have an array a_1, a_2, ..., a_n where a_i = i.
In one step, you can choose two indices x and y (x β y) and set a_x = \leftβ (a_x)/(a_y) \rightβ (ceiling function).
Your goal is to make array a consist of n - 1 ones and 1 two in no more than n + 5 steps. Note that you don't have to minimize the number of steps.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains the single integer n (3 β€ n β€ 2 β
10^5) β the length of array a.
It's guaranteed that the sum of n over test cases doesn't exceed 2 β
10^5.
Output
For each test case, print the sequence of operations that will make a as n - 1 ones and 1 two in the following format: firstly, print one integer m (m β€ n + 5) β the number of operations; next print m pairs of integers x and y (1 β€ x, y β€ n; x β y) (x may be greater or less than y) β the indices of the corresponding operation.
It can be proven that for the given constraints it's always possible to find a correct sequence of operations.
Example
Input
2
3
4
Output
2
3 2
3 2
3
3 4
4 2
4 2
Note
In the first test case, you have array a = [1, 2, 3]. For example, you can do the following:
1. choose 3, 2: a_3 = \leftβ (a_3)/(a_2) \rightβ = 2 and array a = [1, 2, 2];
2. choose 3, 2: a_3 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1].
You've got array with 2 ones and 1 two in 2 steps.
In the second test case, a = [1, 2, 3, 4]. For example, you can do the following:
1. choose 3, 4: a_3 = \leftβ 3/4 \rightβ = 1 and array a = [1, 2, 1, 4];
2. choose 4, 2: a_4 = \leftβ 4/2 \rightβ = 2 and array a = [1, 2, 1, 2];
3. choose 4, 2: a_4 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1, 1].
Submitted Solution:
```
from math import ceil
def isvalid(a,b):
nops = 0
while a != 1 and b != 1:
nops += 1
a, b = ceil(max(a,b)/min(a,b)), min(a,b)
return nops <= 7 and b == 2 or a == 2
for tc in range(int(input())):
n = int(input())
"""i = 1
while 2*i < n:
i *= 2"""
i = 1
while True:
i += 1
if isvalid(i, n):
target = i
break
ops = []
for i in range(2,n):
if i != target:
ops.append((i,n))
a,b = target, n
while (a,b) not in ((1,2),(2,1)):
if a > b:
ops.append((target, n))
a = ceil(a/b)
else:
ops.append((n, target))
b = ceil(b/a)
print(len(ops))
for x,y in ops:
print(x,y)
``` | instruction | 0 | 66,664 | 12 | 133,328 |
Yes | output | 1 | 66,664 | 12 | 133,329 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have an array a_1, a_2, ..., a_n where a_i = i.
In one step, you can choose two indices x and y (x β y) and set a_x = \leftβ (a_x)/(a_y) \rightβ (ceiling function).
Your goal is to make array a consist of n - 1 ones and 1 two in no more than n + 5 steps. Note that you don't have to minimize the number of steps.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains the single integer n (3 β€ n β€ 2 β
10^5) β the length of array a.
It's guaranteed that the sum of n over test cases doesn't exceed 2 β
10^5.
Output
For each test case, print the sequence of operations that will make a as n - 1 ones and 1 two in the following format: firstly, print one integer m (m β€ n + 5) β the number of operations; next print m pairs of integers x and y (1 β€ x, y β€ n; x β y) (x may be greater or less than y) β the indices of the corresponding operation.
It can be proven that for the given constraints it's always possible to find a correct sequence of operations.
Example
Input
2
3
4
Output
2
3 2
3 2
3
3 4
4 2
4 2
Note
In the first test case, you have array a = [1, 2, 3]. For example, you can do the following:
1. choose 3, 2: a_3 = \leftβ (a_3)/(a_2) \rightβ = 2 and array a = [1, 2, 2];
2. choose 3, 2: a_3 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1].
You've got array with 2 ones and 1 two in 2 steps.
In the second test case, a = [1, 2, 3, 4]. For example, you can do the following:
1. choose 3, 4: a_3 = \leftβ 3/4 \rightβ = 1 and array a = [1, 2, 1, 4];
2. choose 4, 2: a_4 = \leftβ 4/2 \rightβ = 2 and array a = [1, 2, 1, 2];
3. choose 4, 2: a_4 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1, 1].
Submitted Solution:
```
from sys import stdin, exit
from bisect import bisect_left as bl, bisect_right as br
input = lambda: stdin.readline()[:-1]
intput = lambda: int(input())
sinput = lambda: input().split()
intsput = lambda: map(int, sinput())
def dprint(*args, **kwargs):
if debugging:
print(*args, **kwargs)
debugging = 1
# Code
t = intput()
for _ in range(t):
n = intput()
if n > 8:
print(n + 5)
for i in range(2, n):
if i not in (4, 8):
print(i, n)
for j in range(6):
print(n, 8)
print(8, 4)
print(4, 8)
print(4, 8)
else:
inst = []
for i in range(2, n - 1):
inst.append((i, n))
a, b = n - 1, n
swap = False
while (a, b) != (1, 2):
b = (b + a - 1) // a
if not swap:
inst.append((n, n - 1))
else:
inst.append((n - 1, n))
if a > b:
swap = not swap
a, b = b, a
print(len(inst))
for a, b in inst:
print(a, b)
``` | instruction | 0 | 66,665 | 12 | 133,330 |
Yes | output | 1 | 66,665 | 12 | 133,331 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have an array a_1, a_2, ..., a_n where a_i = i.
In one step, you can choose two indices x and y (x β y) and set a_x = \leftβ (a_x)/(a_y) \rightβ (ceiling function).
Your goal is to make array a consist of n - 1 ones and 1 two in no more than n + 5 steps. Note that you don't have to minimize the number of steps.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains the single integer n (3 β€ n β€ 2 β
10^5) β the length of array a.
It's guaranteed that the sum of n over test cases doesn't exceed 2 β
10^5.
Output
For each test case, print the sequence of operations that will make a as n - 1 ones and 1 two in the following format: firstly, print one integer m (m β€ n + 5) β the number of operations; next print m pairs of integers x and y (1 β€ x, y β€ n; x β y) (x may be greater or less than y) β the indices of the corresponding operation.
It can be proven that for the given constraints it's always possible to find a correct sequence of operations.
Example
Input
2
3
4
Output
2
3 2
3 2
3
3 4
4 2
4 2
Note
In the first test case, you have array a = [1, 2, 3]. For example, you can do the following:
1. choose 3, 2: a_3 = \leftβ (a_3)/(a_2) \rightβ = 2 and array a = [1, 2, 2];
2. choose 3, 2: a_3 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1].
You've got array with 2 ones and 1 two in 2 steps.
In the second test case, a = [1, 2, 3, 4]. For example, you can do the following:
1. choose 3, 4: a_3 = \leftβ 3/4 \rightβ = 1 and array a = [1, 2, 1, 4];
2. choose 4, 2: a_4 = \leftβ 4/2 \rightβ = 2 and array a = [1, 2, 1, 2];
3. choose 4, 2: a_4 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1, 1].
Submitted Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
ans = []
if n > 12:
for i in range(13, n):
ans.append((i, n))
prod = 1
while prod < n:
ans.append((n, 12))
prod *= 12
for i in range(5, 12):
ans.append((i, 12))
ans.append((12, 4))
ans.append((12, 3))
ans.append((3, 4))
ans.append((4, 2))
ans.append((4, 2))
else:
for i in range(3, n):
ans.append((i, n))
prod = 1
while prod < n:
ans.append((n, 2))
prod *= 2
print(len(ans))
for i, j in ans:
print(i, j)
``` | instruction | 0 | 66,666 | 12 | 133,332 |
Yes | output | 1 | 66,666 | 12 | 133,333 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have an array a_1, a_2, ..., a_n where a_i = i.
In one step, you can choose two indices x and y (x β y) and set a_x = \leftβ (a_x)/(a_y) \rightβ (ceiling function).
Your goal is to make array a consist of n - 1 ones and 1 two in no more than n + 5 steps. Note that you don't have to minimize the number of steps.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains the single integer n (3 β€ n β€ 2 β
10^5) β the length of array a.
It's guaranteed that the sum of n over test cases doesn't exceed 2 β
10^5.
Output
For each test case, print the sequence of operations that will make a as n - 1 ones and 1 two in the following format: firstly, print one integer m (m β€ n + 5) β the number of operations; next print m pairs of integers x and y (1 β€ x, y β€ n; x β y) (x may be greater or less than y) β the indices of the corresponding operation.
It can be proven that for the given constraints it's always possible to find a correct sequence of operations.
Example
Input
2
3
4
Output
2
3 2
3 2
3
3 4
4 2
4 2
Note
In the first test case, you have array a = [1, 2, 3]. For example, you can do the following:
1. choose 3, 2: a_3 = \leftβ (a_3)/(a_2) \rightβ = 2 and array a = [1, 2, 2];
2. choose 3, 2: a_3 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1].
You've got array with 2 ones and 1 two in 2 steps.
In the second test case, a = [1, 2, 3, 4]. For example, you can do the following:
1. choose 3, 4: a_3 = \leftβ 3/4 \rightβ = 1 and array a = [1, 2, 1, 4];
2. choose 4, 2: a_4 = \leftβ 4/2 \rightβ = 2 and array a = [1, 2, 1, 2];
3. choose 4, 2: a_4 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1, 1].
Submitted Solution:
```
from math import ceil
t = int(input())
for _ in range(t):
n = int(input())
l = []
l.append(n)
while(l[-1] != 2):
l.append(ceil(l[-1]**0.5))
ans = []
for i in range(2, n):
if i not in l:
ans.append((i, n))
for i in range(len(l)-1):
ans.append((l[i], l[i+1]))
ans.append((l[i], l[i+1]))
print(len(ans))
for item in ans:
print(item[0], item[1])
``` | instruction | 0 | 66,667 | 12 | 133,334 |
Yes | output | 1 | 66,667 | 12 | 133,335 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have an array a_1, a_2, ..., a_n where a_i = i.
In one step, you can choose two indices x and y (x β y) and set a_x = \leftβ (a_x)/(a_y) \rightβ (ceiling function).
Your goal is to make array a consist of n - 1 ones and 1 two in no more than n + 5 steps. Note that you don't have to minimize the number of steps.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains the single integer n (3 β€ n β€ 2 β
10^5) β the length of array a.
It's guaranteed that the sum of n over test cases doesn't exceed 2 β
10^5.
Output
For each test case, print the sequence of operations that will make a as n - 1 ones and 1 two in the following format: firstly, print one integer m (m β€ n + 5) β the number of operations; next print m pairs of integers x and y (1 β€ x, y β€ n; x β y) (x may be greater or less than y) β the indices of the corresponding operation.
It can be proven that for the given constraints it's always possible to find a correct sequence of operations.
Example
Input
2
3
4
Output
2
3 2
3 2
3
3 4
4 2
4 2
Note
In the first test case, you have array a = [1, 2, 3]. For example, you can do the following:
1. choose 3, 2: a_3 = \leftβ (a_3)/(a_2) \rightβ = 2 and array a = [1, 2, 2];
2. choose 3, 2: a_3 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1].
You've got array with 2 ones and 1 two in 2 steps.
In the second test case, a = [1, 2, 3, 4]. For example, you can do the following:
1. choose 3, 4: a_3 = \leftβ 3/4 \rightβ = 1 and array a = [1, 2, 1, 4];
2. choose 4, 2: a_4 = \leftβ 4/2 \rightβ = 2 and array a = [1, 2, 1, 2];
3. choose 4, 2: a_4 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1, 1].
Submitted Solution:
```
from math import ceil
for _ in range(int(input())):
n=int(input())
num=n
Div=[]
if n>8:
for i in range(3,n):
if i!=8:
Div.append([i,n])
while n!=1:
Div.append([num,8])
n=ceil(n/8)
n=8
while n!=1:
Div.append([8,2])
n//=2
print(len(Div))
for i in Div:
print(*i)
``` | instruction | 0 | 66,668 | 12 | 133,336 |
No | output | 1 | 66,668 | 12 | 133,337 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have an array a_1, a_2, ..., a_n where a_i = i.
In one step, you can choose two indices x and y (x β y) and set a_x = \leftβ (a_x)/(a_y) \rightβ (ceiling function).
Your goal is to make array a consist of n - 1 ones and 1 two in no more than n + 5 steps. Note that you don't have to minimize the number of steps.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains the single integer n (3 β€ n β€ 2 β
10^5) β the length of array a.
It's guaranteed that the sum of n over test cases doesn't exceed 2 β
10^5.
Output
For each test case, print the sequence of operations that will make a as n - 1 ones and 1 two in the following format: firstly, print one integer m (m β€ n + 5) β the number of operations; next print m pairs of integers x and y (1 β€ x, y β€ n; x β y) (x may be greater or less than y) β the indices of the corresponding operation.
It can be proven that for the given constraints it's always possible to find a correct sequence of operations.
Example
Input
2
3
4
Output
2
3 2
3 2
3
3 4
4 2
4 2
Note
In the first test case, you have array a = [1, 2, 3]. For example, you can do the following:
1. choose 3, 2: a_3 = \leftβ (a_3)/(a_2) \rightβ = 2 and array a = [1, 2, 2];
2. choose 3, 2: a_3 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1].
You've got array with 2 ones and 1 two in 2 steps.
In the second test case, a = [1, 2, 3, 4]. For example, you can do the following:
1. choose 3, 4: a_3 = \leftβ 3/4 \rightβ = 1 and array a = [1, 2, 1, 4];
2. choose 4, 2: a_4 = \leftβ 4/2 \rightβ = 2 and array a = [1, 2, 1, 2];
3. choose 4, 2: a_4 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1, 1].
Submitted Solution:
```
# import sys
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')
# input = sys.stdin.readline
t = int(input())
while t:
t -= 1
n = int(input())
ops = [(n, (n+1)//2)]*4
for i in range(n-1, 2, -1):
ops.append((i-1, i))
print(len(ops))
for op in ops:
print(*op)
``` | instruction | 0 | 66,669 | 12 | 133,338 |
No | output | 1 | 66,669 | 12 | 133,339 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have an array a_1, a_2, ..., a_n where a_i = i.
In one step, you can choose two indices x and y (x β y) and set a_x = \leftβ (a_x)/(a_y) \rightβ (ceiling function).
Your goal is to make array a consist of n - 1 ones and 1 two in no more than n + 5 steps. Note that you don't have to minimize the number of steps.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains the single integer n (3 β€ n β€ 2 β
10^5) β the length of array a.
It's guaranteed that the sum of n over test cases doesn't exceed 2 β
10^5.
Output
For each test case, print the sequence of operations that will make a as n - 1 ones and 1 two in the following format: firstly, print one integer m (m β€ n + 5) β the number of operations; next print m pairs of integers x and y (1 β€ x, y β€ n; x β y) (x may be greater or less than y) β the indices of the corresponding operation.
It can be proven that for the given constraints it's always possible to find a correct sequence of operations.
Example
Input
2
3
4
Output
2
3 2
3 2
3
3 4
4 2
4 2
Note
In the first test case, you have array a = [1, 2, 3]. For example, you can do the following:
1. choose 3, 2: a_3 = \leftβ (a_3)/(a_2) \rightβ = 2 and array a = [1, 2, 2];
2. choose 3, 2: a_3 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1].
You've got array with 2 ones and 1 two in 2 steps.
In the second test case, a = [1, 2, 3, 4]. For example, you can do the following:
1. choose 3, 4: a_3 = \leftβ 3/4 \rightβ = 1 and array a = [1, 2, 1, 4];
2. choose 4, 2: a_4 = \leftβ 4/2 \rightβ = 2 and array a = [1, 2, 1, 2];
3. choose 4, 2: a_4 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1, 1].
Submitted Solution:
```
import math
for i in range(int(input())):
n=int(input())
a=[i for i in range(1,n+1)]
g=[-1]*n
g[0]=0
g[1]=0
k=n
x=[]
y=[]
c=0
while k!=2:
g[k-1]=0
k=int(math.ceil(math.sqrt(k)))
for j in range(1,n):
if g[j]==-1:
c=c+1
x.append(j+1)
y.append(n)
for j in range(n-1,1,-1):
if g[j]==0:
c=c+2
x.append(int(math.ceil(math.sqrt(j+1))))
x.append(int(math.ceil(math.sqrt(j+1))))
y.append(j+1)
y.append(j+1)
print(c)
for j in range(c):
print(x[j],y[j])
``` | instruction | 0 | 66,670 | 12 | 133,340 |
No | output | 1 | 66,670 | 12 | 133,341 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have an array a_1, a_2, ..., a_n where a_i = i.
In one step, you can choose two indices x and y (x β y) and set a_x = \leftβ (a_x)/(a_y) \rightβ (ceiling function).
Your goal is to make array a consist of n - 1 ones and 1 two in no more than n + 5 steps. Note that you don't have to minimize the number of steps.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The first and only line of each test case contains the single integer n (3 β€ n β€ 2 β
10^5) β the length of array a.
It's guaranteed that the sum of n over test cases doesn't exceed 2 β
10^5.
Output
For each test case, print the sequence of operations that will make a as n - 1 ones and 1 two in the following format: firstly, print one integer m (m β€ n + 5) β the number of operations; next print m pairs of integers x and y (1 β€ x, y β€ n; x β y) (x may be greater or less than y) β the indices of the corresponding operation.
It can be proven that for the given constraints it's always possible to find a correct sequence of operations.
Example
Input
2
3
4
Output
2
3 2
3 2
3
3 4
4 2
4 2
Note
In the first test case, you have array a = [1, 2, 3]. For example, you can do the following:
1. choose 3, 2: a_3 = \leftβ (a_3)/(a_2) \rightβ = 2 and array a = [1, 2, 2];
2. choose 3, 2: a_3 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1].
You've got array with 2 ones and 1 two in 2 steps.
In the second test case, a = [1, 2, 3, 4]. For example, you can do the following:
1. choose 3, 4: a_3 = \leftβ 3/4 \rightβ = 1 and array a = [1, 2, 1, 4];
2. choose 4, 2: a_4 = \leftβ 4/2 \rightβ = 2 and array a = [1, 2, 1, 2];
3. choose 4, 2: a_4 = \leftβ 2/2 \rightβ = 1 and array a = [1, 2, 1, 1].
Submitted Solution:
```
ll=lambda:map(int,input().split())
t=lambda:int(input())
ss=lambda:input()
from math import log10 ,log2,ceil,factorial as f,gcd
#from itertools import combinations_with_replacement as cs
#from functools import reduce
#from bisect import bisect_right as br
#from collections import Counter
#from math import inf,ceil
'''
'''
#for _ in range(t()):
for _ in range(t()):
n=t()
print((n-1))
for i in range(3,n):
print(i,i+1)
m=n
while m>1:
print(n,2)
m=int(ceil(m/2))
#for i in range(n,2,-1):
``` | instruction | 0 | 66,671 | 12 | 133,342 |
No | output | 1 | 66,671 | 12 | 133,343 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The determinant of a matrix 2 Γ 2 is defined as follows:
<image>
A matrix is called degenerate if its determinant is equal to zero.
The norm ||A|| of a matrix A is defined as a maximum of absolute values of its elements.
You are given a matrix <image>. Consider any degenerate matrix B such that norm ||A - B|| is minimum possible. Determine ||A - B||.
Input
The first line contains two integers a and b (|a|, |b| β€ 109), the elements of the first row of matrix A.
The second line contains two integers c and d (|c|, |d| β€ 109) the elements of the second row of matrix A.
Output
Output a single real number, the minimum possible value of ||A - B||. Your answer is considered to be correct if its absolute or relative error does not exceed 10 - 9.
Examples
Input
1 2
3 4
Output
0.2000000000
Input
1 0
0 1
Output
0.5000000000
Note
In the first sample matrix B is <image>
In the second sample matrix B is <image> | instruction | 0 | 66,784 | 12 | 133,568 |
Tags: binary search, math
Correct Solution:
```
#!/usr/bin/python3
from math import sqrt
a, b = map(int, input().split())
c, d = map(int, input().split())
x = []
s = []
try:
x += [(-a*d + b*c)/(a - b - c + d)]
s += [1, 1, 1, 1]
except:
pass
try:
x += [-a/4 - b/4 + c/4 - d/4 - sqrt(a**2 + 2*a*b - 2*a*c - 6*a*d + b**2 + 6*b*c + 2*b*d + c**2 - 2*c*d + d**2)/4,
-a/4 - b/4 + c/4 - d/4 + sqrt(a**2 + 2*a*b - 2*a*c - 6*a*d + b**2 + 6*b*c + 2*b*d + c**2 - 2*c*d + d**2)/4]
s += [1, 1, 1, -1] * 2
except:
pass
try:
x += [-a/4 + b/4 - c/4 - d/4 - sqrt(a**2 - 2*a*b + 2*a*c - 6*a*d + b**2 + 6*b*c - 2*b*d + c**2 + 2*c*d + d**2)/4,
-a/4 + b/4 - c/4 - d/4 + sqrt(a**2 - 2*a*b + 2*a*c - 6*a*d + b**2 + 6*b*c - 2*b*d + c**2 + 2*c*d + d**2)/4]
s += [1, 1, -1, 1] * 2
except:
pass
try:
x += [(-a*d + b*c)/(a + b + c + d)]
s += [1, 1, -1, -1]
except:
pass
try:
x += [-a/4 - b/4 - c/4 + d/4 - sqrt(a**2 + 2*a*b + 2*a*c + 6*a*d + b**2 - 6*b*c - 2*b*d + c**2 - 2*c*d + d**2)/4,
-a/4 - b/4 - c/4 + d/4 + sqrt(a**2 + 2*a*b + 2*a*c + 6*a*d + b**2 - 6*b*c - 2*b*d + c**2 - 2*c*d + d**2)/4]
s += [1, -1, 1, 1] * 2
except:
pass
try:
x += [(a*d - b*c)/(a - b + c - d)]
s += [1, -1, 1, -1]
except:
pass
try:
x += [(a*d - b*c)/(a + b - c - d)]
s += [1, -1, -1, 1]
except:
pass
try:
x += [-a/4 + b/4 + c/4 + d/4 - sqrt(a**2 - 2*a*b - 2*a*c + 6*a*d + b**2 - 6*b*c + 2*b*d + c**2 + 2*c*d + d**2)/4,
-a/4 + b/4 + c/4 + d/4 + sqrt(a**2 - 2*a*b - 2*a*c + 6*a*d + b**2 - 6*b*c + 2*b*d + c**2 + 2*c*d + d**2)/4]
s += [1, -1, -1, -1] * 2
except:
pass
try:
x += [a/4 - b/4 - c/4 - d/4 - sqrt(a**2 - 2*a*b - 2*a*c + 6*a*d + b**2 - 6*b*c + 2*b*d + c**2 + 2*c*d + d**2)/4,
a/4 - b/4 - c/4 - d/4 + sqrt(a**2 - 2*a*b - 2*a*c + 6*a*d + b**2 - 6*b*c + 2*b*d + c**2 + 2*c*d + d**2)/4]
s += [-1, 1, 1, 1] * 2
except:
pass
try:
s += [-1, 1, 1, -1]
x += [(-a*d + b*c)/(a + b - c - d)]
except:
pass
try:
x += [(-a*d + b*c)/(a - b + c - d)]
s += [-1, 1, -1, 1]
except:
pass
try:
x += [a/4 + b/4 + c/4 - d/4 - sqrt(a**2 + 2*a*b + 2*a*c + 6*a*d + b**2 - 6*b*c - 2*b*d + c**2 - 2*c*d + d**2)/4,
a/4 + b/4 + c/4 - d/4 + sqrt(a**2 + 2*a*b + 2*a*c + 6*a*d + b**2 - 6*b*c - 2*b*d + c**2 - 2*c*d + d**2)/4]
s += [-1, 1, -1, -1] * 2
except:
pass
try:
x += [(a*d - b*c)/(a + b + c + d)]
s += [-1, -1, 1, 1]
except:
pass
try:
x += [a/4 - b/4 + c/4 + d/4 - sqrt(a**2 - 2*a*b + 2*a*c - 6*a*d + b**2 + 6*b*c - 2*b*d + c**2 + 2*c*d + d**2)/4, a/4 - b/4 + c/4 + d/4 + sqrt(a**2 - 2*a*b + 2*a*c - 6*a*d + b**2 + 6*b*c - 2*b*d + c**2 + 2*c*d + d**2)/4]
s += [-1, -1, 1, -1] * 2
except:
pass
try:
x += [a/4 + b/4 - c/4 + d/4 - sqrt(a**2 + 2*a*b - 2*a*c - 6*a*d + b**2 + 6*b*c + 2*b*d + c**2 - 2*c*d + d**2)/4, a/4 + b/4 - c/4 + d/4 + sqrt(a**2 + 2*a*b - 2*a*c - 6*a*d + b**2 + 6*b*c + 2*b*d + c**2 - 2*c*d + d**2)/4]
s += [-1, -1, -1, 1] * 2
except:
pass
try:
x += [(a*d - b*c)/(a - b - c + d)]
s += [-1, -1, -1, -1]
except:
pass
print (min(map(abs,x)))
for i in range(len(x)):
ss = s[4*i:4*i+4]
#print (i, x[i], ss)
aa = a + x[i] * ss[0]
dd = d + x[i] * ss[1]
bb = b + x[i] * ss[2]
cc = c + x[i] * ss[3]
#print (aa, bb, cc, dd)
#print (aa * dd - bb * cc)
``` | output | 1 | 66,784 | 12 | 133,569 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The determinant of a matrix 2 Γ 2 is defined as follows:
<image>
A matrix is called degenerate if its determinant is equal to zero.
The norm ||A|| of a matrix A is defined as a maximum of absolute values of its elements.
You are given a matrix <image>. Consider any degenerate matrix B such that norm ||A - B|| is minimum possible. Determine ||A - B||.
Input
The first line contains two integers a and b (|a|, |b| β€ 109), the elements of the first row of matrix A.
The second line contains two integers c and d (|c|, |d| β€ 109) the elements of the second row of matrix A.
Output
Output a single real number, the minimum possible value of ||A - B||. Your answer is considered to be correct if its absolute or relative error does not exceed 10 - 9.
Examples
Input
1 2
3 4
Output
0.2000000000
Input
1 0
0 1
Output
0.5000000000
Note
In the first sample matrix B is <image>
In the second sample matrix B is <image> | instruction | 0 | 66,785 | 12 | 133,570 |
Tags: binary search, math
Correct Solution:
```
a, b= map(int, input().split())
c, d = map(int, input().split())
if a * d - b * c == 0:
print(0)
else:
curpos = a * d - b * c >= 0
small = 0;
large = 1e18
for iteration in range(200):
avg = (small + large) / 2
works = False
for ach in range(-1, 2, 2):
for bch in range(-1, 2, 2):
for cch in range(-1, 2, 2):
for dch in range(-1, 2, 2):
newpos = (a + ach * avg) * (d + dch * avg) - (b + bch * avg) * (c + cch * avg) >= 0
if newpos != curpos:
works = True
if works:
large = avg
else:
small = avg
print(small)
``` | output | 1 | 66,785 | 12 | 133,571 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The determinant of a matrix 2 Γ 2 is defined as follows:
<image>
A matrix is called degenerate if its determinant is equal to zero.
The norm ||A|| of a matrix A is defined as a maximum of absolute values of its elements.
You are given a matrix <image>. Consider any degenerate matrix B such that norm ||A - B|| is minimum possible. Determine ||A - B||.
Input
The first line contains two integers a and b (|a|, |b| β€ 109), the elements of the first row of matrix A.
The second line contains two integers c and d (|c|, |d| β€ 109) the elements of the second row of matrix A.
Output
Output a single real number, the minimum possible value of ||A - B||. Your answer is considered to be correct if its absolute or relative error does not exceed 10 - 9.
Examples
Input
1 2
3 4
Output
0.2000000000
Input
1 0
0 1
Output
0.5000000000
Note
In the first sample matrix B is <image>
In the second sample matrix B is <image> | instruction | 0 | 66,786 | 12 | 133,572 |
Tags: binary search, math
Correct Solution:
```
import sys;A,B,C,D=map(int,sys.stdin.read().split());k=max(map(abs,[A+B+C+D,A+B-C-D,A-B+C-D,A-B-C+D]))+1e-9;print((0,abs(A*D-B*C)/k)[bool(k)])
``` | output | 1 | 66,786 | 12 | 133,573 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The determinant of a matrix 2 Γ 2 is defined as follows:
<image>
A matrix is called degenerate if its determinant is equal to zero.
The norm ||A|| of a matrix A is defined as a maximum of absolute values of its elements.
You are given a matrix <image>. Consider any degenerate matrix B such that norm ||A - B|| is minimum possible. Determine ||A - B||.
Input
The first line contains two integers a and b (|a|, |b| β€ 109), the elements of the first row of matrix A.
The second line contains two integers c and d (|c|, |d| β€ 109) the elements of the second row of matrix A.
Output
Output a single real number, the minimum possible value of ||A - B||. Your answer is considered to be correct if its absolute or relative error does not exceed 10 - 9.
Examples
Input
1 2
3 4
Output
0.2000000000
Input
1 0
0 1
Output
0.5000000000
Note
In the first sample matrix B is <image>
In the second sample matrix B is <image> | instruction | 0 | 66,787 | 12 | 133,574 |
Tags: binary search, math
Correct Solution:
```
A,B,C,D=map(int,(input()+' '+input()).split());k=max(map(abs,[A+B+C+D,A+B-C-D,A-B+C-D,A-B-C+D]))+1e-9;print(abs(A*D-B*C)/k)
``` | output | 1 | 66,787 | 12 | 133,575 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The determinant of a matrix 2 Γ 2 is defined as follows:
<image>
A matrix is called degenerate if its determinant is equal to zero.
The norm ||A|| of a matrix A is defined as a maximum of absolute values of its elements.
You are given a matrix <image>. Consider any degenerate matrix B such that norm ||A - B|| is minimum possible. Determine ||A - B||.
Input
The first line contains two integers a and b (|a|, |b| β€ 109), the elements of the first row of matrix A.
The second line contains two integers c and d (|c|, |d| β€ 109) the elements of the second row of matrix A.
Output
Output a single real number, the minimum possible value of ||A - B||. Your answer is considered to be correct if its absolute or relative error does not exceed 10 - 9.
Examples
Input
1 2
3 4
Output
0.2000000000
Input
1 0
0 1
Output
0.5000000000
Note
In the first sample matrix B is <image>
In the second sample matrix B is <image> | instruction | 0 | 66,788 | 12 | 133,576 |
Tags: binary search, math
Correct Solution:
```
a=abs;A,B,C,D=map(int,(input()+' '+input()).split());print(a(A*D-B*C)/(max(a(A+B)+a(C+D),a(A-B)+a(C-D))+1e-9))
``` | output | 1 | 66,788 | 12 | 133,577 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The determinant of a matrix 2 Γ 2 is defined as follows:
<image>
A matrix is called degenerate if its determinant is equal to zero.
The norm ||A|| of a matrix A is defined as a maximum of absolute values of its elements.
You are given a matrix <image>. Consider any degenerate matrix B such that norm ||A - B|| is minimum possible. Determine ||A - B||.
Input
The first line contains two integers a and b (|a|, |b| β€ 109), the elements of the first row of matrix A.
The second line contains two integers c and d (|c|, |d| β€ 109) the elements of the second row of matrix A.
Output
Output a single real number, the minimum possible value of ||A - B||. Your answer is considered to be correct if its absolute or relative error does not exceed 10 - 9.
Examples
Input
1 2
3 4
Output
0.2000000000
Input
1 0
0 1
Output
0.5000000000
Note
In the first sample matrix B is <image>
In the second sample matrix B is <image> | instruction | 0 | 66,789 | 12 | 133,578 |
Tags: binary search, math
Correct Solution:
```
a=abs;A,B,C,D=map(int,(input()+' '+input()).split());print(a(A*D-B*C)/max(a(A+B)+a(C+D),a(A-B)+a(C-D),1e-9))
``` | output | 1 | 66,789 | 12 | 133,579 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The determinant of a matrix 2 Γ 2 is defined as follows:
<image>
A matrix is called degenerate if its determinant is equal to zero.
The norm ||A|| of a matrix A is defined as a maximum of absolute values of its elements.
You are given a matrix <image>. Consider any degenerate matrix B such that norm ||A - B|| is minimum possible. Determine ||A - B||.
Input
The first line contains two integers a and b (|a|, |b| β€ 109), the elements of the first row of matrix A.
The second line contains two integers c and d (|c|, |d| β€ 109) the elements of the second row of matrix A.
Output
Output a single real number, the minimum possible value of ||A - B||. Your answer is considered to be correct if its absolute or relative error does not exceed 10 - 9.
Examples
Input
1 2
3 4
Output
0.2000000000
Input
1 0
0 1
Output
0.5000000000
Note
In the first sample matrix B is <image>
In the second sample matrix B is <image> | instruction | 0 | 66,790 | 12 | 133,580 |
Tags: binary search, math
Correct Solution:
```
A,B,C,D=map(int,input().split()+input().split());k=max(map(abs,[A+B+C+D,A+B-C-D,A-B+C-D,A-B-C+D]))+1e-9;print((0,abs(A*D-B*C)/k)[bool(k)])
``` | output | 1 | 66,790 | 12 | 133,581 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The determinant of a matrix 2 Γ 2 is defined as follows:
<image>
A matrix is called degenerate if its determinant is equal to zero.
The norm ||A|| of a matrix A is defined as a maximum of absolute values of its elements.
You are given a matrix <image>. Consider any degenerate matrix B such that norm ||A - B|| is minimum possible. Determine ||A - B||.
Input
The first line contains two integers a and b (|a|, |b| β€ 109), the elements of the first row of matrix A.
The second line contains two integers c and d (|c|, |d| β€ 109) the elements of the second row of matrix A.
Output
Output a single real number, the minimum possible value of ||A - B||. Your answer is considered to be correct if its absolute or relative error does not exceed 10 - 9.
Examples
Input
1 2
3 4
Output
0.2000000000
Input
1 0
0 1
Output
0.5000000000
Note
In the first sample matrix B is <image>
In the second sample matrix B is <image> | instruction | 0 | 66,791 | 12 | 133,582 |
Tags: binary search, math
Correct Solution:
```
#include <bits/stdc++.h>
#define m(x,y) (x>y?x:y)
#define a(x) m(x,-(x))
#define solve() printf("%.9f",(A||B||C||D?a(A*D-B*C)/m(m(a(A+B+C+D),a(A-B+C-D)),m(a(A-B-C+D),a(A+B-C-D))):0))
#define moo main(){double A,B,C,D;std::cin>>A>>B>>C>>D;solve();}
#define _ \
0; moo = 'moo'; import sys; A,B,C,D=map(int,sys.stdin.read().split()); k=max(map(abs,[A+B+C+D,A+B-C-D,A-B+C-D,A-B-C+D]))+1e-9; print('%.9f'%(0,abs(A*D-B*C)/k)[bool(k)]);
moo;
``` | output | 1 | 66,791 | 12 | 133,583 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help.
Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal.
Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal.
Input
The first line of the input contains an integer n (1 β€ n β€ 100 000) β the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 β€ ai β€ 109) β elements of the array.
Output
If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes).
Examples
Input
5
1 3 3 2 1
Output
YES
Input
5
1 2 3 4 5
Output
NO
Note
In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements. | instruction | 0 | 66,873 | 12 | 133,746 |
Tags: implementation, sortings
Correct Solution:
```
n = int(input())
sp = list(map(int, input().split()))
t = []
if n > 2:
for el in sp:
if not el in t: t.append(el)
if len(t) > 3: print('NO'); break
else:
if len(t) == 3:
t.sort()
if t[1]-t[0] == t[2]-t[1]: print('YES')
else: print('NO')
elif len(t) <= 2: print('YES')
else: print('YES')
``` | output | 1 | 66,873 | 12 | 133,747 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help.
Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal.
Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal.
Input
The first line of the input contains an integer n (1 β€ n β€ 100 000) β the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 β€ ai β€ 109) β elements of the array.
Output
If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes).
Examples
Input
5
1 3 3 2 1
Output
YES
Input
5
1 2 3 4 5
Output
NO
Note
In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements. | instruction | 0 | 66,874 | 12 | 133,748 |
Tags: implementation, sortings
Correct Solution:
```
n = int(input())
A = list(map(int,input().split()))
tf = False
ma = max(A)
mi = min(A)
sr = (ma+mi)/2
for i in A:
if i==ma or i==mi or i==sr:
tf = True
else:
print('NO')
exit()
if tf:
print('YES')
``` | output | 1 | 66,874 | 12 | 133,749 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help.
Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal.
Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal.
Input
The first line of the input contains an integer n (1 β€ n β€ 100 000) β the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 β€ ai β€ 109) β elements of the array.
Output
If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes).
Examples
Input
5
1 3 3 2 1
Output
YES
Input
5
1 2 3 4 5
Output
NO
Note
In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements. | instruction | 0 | 66,875 | 12 | 133,750 |
Tags: implementation, sortings
Correct Solution:
```
#!/usr/bin/python3
n = int(input())
array = list(set(list(map(int, input().split()))))
array.sort()
if len(array) < 3:
print("YES")
elif len(array) > 3 or array[1] - array[0] != array[2] - array[1]:
print("NO")
else:
print("YES")
``` | output | 1 | 66,875 | 12 | 133,751 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help.
Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal.
Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal.
Input
The first line of the input contains an integer n (1 β€ n β€ 100 000) β the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 β€ ai β€ 109) β elements of the array.
Output
If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes).
Examples
Input
5
1 3 3 2 1
Output
YES
Input
5
1 2 3 4 5
Output
NO
Note
In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements. | instruction | 0 | 66,876 | 12 | 133,752 |
Tags: implementation, sortings
Correct Solution:
```
import sys
import math
import bisect
import math
from itertools import accumulate
input = sys.stdin.readline
def inpit(): #int
return(int(input()))
def inplt(): #list
return(list(map(int,input().split())))
def inpstr(): #string
s = input()
return(list(s[:len(s) - 1]))
def inpspit(): #spaced intergers
return(map(int,input().split()))
pow = math.pow
fl = math.floor
ceil = math.ceil
dis = math.hypot # cartesian distance
def lcm(a):
return abs(math.prod(a)) // math.gcd(*a)
def cumulativeSum(input):
return (list(accumulate(input)))
n = inpit()
lt = inplt()
mn = min(lt)
mx = max(lt)
x =( mx - mn)
if(x %2 == 0):
x = int(x/2 )
p = mn +x
for i in lt[1:] :
if(i + x == p or i-x == p or i == p):
continue
else:
print('NO')
exit()
print("YES")
``` | output | 1 | 66,876 | 12 | 133,753 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help.
Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal.
Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal.
Input
The first line of the input contains an integer n (1 β€ n β€ 100 000) β the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 β€ ai β€ 109) β elements of the array.
Output
If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes).
Examples
Input
5
1 3 3 2 1
Output
YES
Input
5
1 2 3 4 5
Output
NO
Note
In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements. | instruction | 0 | 66,877 | 12 | 133,754 |
Tags: implementation, sortings
Correct Solution:
```
I=input
I()
s=sorted(set(map(int,I().split())))
t=len(s)
print(['NO','YES'][t<3or(t==3and s[0]+s[2]==s[1]*2)])
``` | output | 1 | 66,877 | 12 | 133,755 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help.
Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal.
Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal.
Input
The first line of the input contains an integer n (1 β€ n β€ 100 000) β the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 β€ ai β€ 109) β elements of the array.
Output
If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes).
Examples
Input
5
1 3 3 2 1
Output
YES
Input
5
1 2 3 4 5
Output
NO
Note
In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements. | instruction | 0 | 66,878 | 12 | 133,756 |
Tags: implementation, sortings
Correct Solution:
```
import sys
n = int(input())
s = set(input().split())
s = [ i for i in s ]
if len(s) < 3:
print('YES')
sys.exit(0)
a = int(s[0])
b = int(s[1])
c = int(s[2])
if len(s) <= 3 and (((a+b)//2 == c and (a+b)%2 == 0) or ((a+c)//2 == b and (a+c)%2 == 0) or ((b+c)//2 == a and (b+c)%2 == 0)):
print('YES')
else:
print('NO')
``` | output | 1 | 66,878 | 12 | 133,757 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.