message
stringlengths 2
45.8k
| message_type
stringclasses 2
values | message_id
int64 0
1
| conversation_id
int64 254
108k
| cluster
float64 3
3
| __index_level_0__
int64 508
217k
|
|---|---|---|---|---|---|
Provide a correct Python 3 solution for this coding contest problem.
There are N pinholes on the xy-plane. The i-th pinhole is located at (x_i,y_i).
We will denote the Manhattan distance between the i-th and j-th pinholes as d(i,j)(=|x_i-x_j|+|y_i-y_j|).
You have a peculiar pair of compasses, called Manhattan Compass. This instrument always points at two of the pinholes. The two legs of the compass are indistinguishable, thus we do not distinguish the following two states: the state where the compass points at the p-th and q-th pinholes, and the state where it points at the q-th and p-th pinholes.
When the compass points at the p-th and q-th pinholes and d(p,q)=d(p,r), one of the legs can be moved so that the compass will point at the p-th and r-th pinholes.
Initially, the compass points at the a-th and b-th pinholes. Find the number of the pairs of pinholes that can be pointed by the compass.
Constraints
* 2≦N≦10^5
* 1≦x_i, y_i≦10^9
* 1≦a < b≦N
* When i ≠ j, (x_i, y_i) ≠ (x_j, y_j)
* x_i and y_i are integers.
Input
The input is given from Standard Input in the following format:
N a b
x_1 y_1
:
x_N y_N
Output
Print the number of the pairs of pinholes that can be pointed by the compass.
Examples
Input
5 1 2
1 1
4 3
6 1
5 5
4 8
Output
4
Input
6 2 3
1 3
5 3
3 5
8 4
4 7
2 5
Output
4
Input
8 1 2
1 5
4 3
8 2
4 7
8 8
3 3
6 6
4 8
Output
7
|
instruction
| 0
| 1,569
| 3
| 3,138
|
"Correct Solution:
```
import bisect
import sys
from collections import defaultdict
class RandomizedBinarySearchTree:
def __init__(self):
self.children = [[-1], [-1]]
self.values = [0]
self.counts = [0]
self.root = 0
def merge(self, left_root, right_root):
children = self.children
counts = self.counts
li = left_root
ri = right_root
stack = []
switch = 0
while li != 0 and ri != 0:
if switch:
stack.append((li, 1))
li = children[1][li]
else:
stack.append((ri, 0))
ri = children[0][ri]
switch ^= 1
i = li if li != 0 else ri
while stack:
pi, is_right = stack.pop()
children[is_right][pi] = i
counts[pi] = counts[children[0][pi]] + counts[children[1][pi]] + 1
i = pi
return i
def split(self, root, x):
i = root
lefts, rights = self.children
values = self.values
counts = self.counts
l_stack = []
r_stack = []
while i != 0:
if x < values[i]:
r_stack.append(i)
i = lefts[i]
else:
l_stack.append(i)
i = rights[i]
li, ri = 0, 0
while l_stack:
pi = l_stack.pop()
rights[pi] = li
counts[pi] = counts[lefts[pi]] + counts[li] + 1
li = pi
while r_stack:
pi = r_stack.pop()
lefts[pi] = ri
counts[pi] = counts[ri] + counts[rights[pi]] + 1
ri = pi
return li, ri
def insert(self, x):
ni = len(self.values)
self.children[0].append(0)
self.children[1].append(0)
self.values.append(x)
self.counts.append(1)
li, ri = self.split(self.root, x)
self.root = self.merge(self.merge(li, ni), ri)
def delete(self, x):
li, mri = self.split(self.root, x - 1)
mi, ri = self.split(mri, x)
if mi == 0:
self.root = self.merge(li, ri)
return
self.root = self.merge(li, ri)
return
def upper_bound(self, x, default=-1):
i = self.root
lefts, rights = self.children
values = self.values
counts = self.counts
y = default
c = counts[i]
j = 0
while i != 0:
if x < values[i]:
y = values[i]
c = j + counts[lefts[i]]
i = lefts[i]
else:
j += counts[lefts[i]] + 1
i = rights[i]
return y, c
def lower_bound(self, x, default=-1):
i = self.root
lefts, rights = self.children
values = self.values
counts = self.counts
y = default
c = counts[i]
j = 0
while i != 0:
if x <= values[i]:
y = values[i]
c = j + counts[lefts[i]]
i = lefts[i]
else:
j += counts[lefts[i]] + 1
i = rights[i]
return y, c
def get_k_th(self, k, default=-1):
i = self.root
children = self.children
values = self.values
counts = self.counts
if counts[i] <= k:
return default
j = k
while i != 0:
left_count = counts[children[0][i]]
if left_count == j:
return values[i]
elif left_count > j:
i = children[0][i]
else:
j -= left_count + 1
i = children[1][i]
return default
def debug_print(self):
print('Lefts ', self.children[0])
print('Rights', self.children[1])
print('Values', self.values)
print('Counts', self.counts)
self._debug_print(self.root, 0)
def _debug_print(self, i, depth):
if i != -1:
self._debug_print(self.children[0][i], depth + 1)
print(' ' * depth, self.values[i], self.counts[i])
self._debug_print(self.children[1][i], depth + 1)
def x_check(x_dict, nx, y, d, stack, stacked):
counter = 0
if nx in x_dict:
rbst, lst = x_dict[nx]
ny, _ = rbst.lower_bound(y - d, y + d + 1)
while ny <= y + d:
z = (nx << 32) | ny
if z not in stacked:
stack.append(z)
stacked.add(z)
rbst.delete(ny)
ny, _ = rbst.lower_bound(y - d, y + d + 1)
i = bisect.bisect_left(lst, y - d)
j = bisect.bisect(lst, y + d)
counter = j - i
return counter
def y_check(y_dict, ny, x, d, stack, stacked):
counter = 0
if ny in y_dict:
rbst, lst = y_dict[ny]
nx, _ = rbst.lower_bound(x - d, x + d + 1)
while nx <= x + d:
z = (nx << 32) | ny
if z not in stacked:
stack.append(z)
stacked.add(z)
rbst.delete(nx)
nx, _ = rbst.lower_bound(x - d, x + d + 1)
i = bisect.bisect(lst, x - d)
j = bisect.bisect_left(lst, x + d)
counter = j - i
return counter
n, a, b, *xy = map(int, sys.stdin.buffer.read().split())
xxx = xy[0::2]
yyy = xy[1::2]
x_dict = defaultdict(lambda: [RandomizedBinarySearchTree(), []])
y_dict = defaultdict(lambda: [RandomizedBinarySearchTree(), []])
OFFSET = 10 ** 9
MASK = (1 << 32) - 1
for i in range(n):
x = xxx[i] - yyy[i] + OFFSET
y = xxx[i] + yyy[i]
x_dict[x][0].insert(y)
y_dict[y][0].insert(x)
x_dict[x][1].append(y)
y_dict[y][1].append(x)
for _, lst in x_dict.values():
lst.sort()
for _, lst in y_dict.values():
lst.sort()
a -= 1
b -= 1
ax_, ay_ = xxx[a], yyy[a]
bx_, by_ = xxx[b], yyy[b]
ax, ay = ax_ - ay_ + OFFSET, ax_ + ay_
bx, by = bx_ - by_ + OFFSET, bx_ + by_
az = (ax << 32) | ay
bz = (bx << 32) | by
d = max(abs(ax - bx), abs(ay - by))
x_dict[ax][0].delete(ay)
x_dict[bx][0].delete(by)
y_dict[ay][0].delete(ax)
y_dict[by][0].delete(bx)
stack = [az, bz]
stacked = {az, bz}
ans = 0
while stack:
z = stack.pop()
y = z & MASK
x = z >> 32
ans += x_check(x_dict, x - d, y, d, stack, stacked)
ans += x_check(x_dict, x + d, y, d, stack, stacked)
ans += y_check(y_dict, y - d, x, d, stack, stacked)
ans += y_check(y_dict, y + d, x, d, stack, stacked)
print(ans // 2)
```
|
output
| 1
| 1,569
| 3
| 3,139
|
Provide a correct Python 3 solution for this coding contest problem.
There are N pinholes on the xy-plane. The i-th pinhole is located at (x_i,y_i).
We will denote the Manhattan distance between the i-th and j-th pinholes as d(i,j)(=|x_i-x_j|+|y_i-y_j|).
You have a peculiar pair of compasses, called Manhattan Compass. This instrument always points at two of the pinholes. The two legs of the compass are indistinguishable, thus we do not distinguish the following two states: the state where the compass points at the p-th and q-th pinholes, and the state where it points at the q-th and p-th pinholes.
When the compass points at the p-th and q-th pinholes and d(p,q)=d(p,r), one of the legs can be moved so that the compass will point at the p-th and r-th pinholes.
Initially, the compass points at the a-th and b-th pinholes. Find the number of the pairs of pinholes that can be pointed by the compass.
Constraints
* 2≦N≦10^5
* 1≦x_i, y_i≦10^9
* 1≦a < b≦N
* When i ≠ j, (x_i, y_i) ≠ (x_j, y_j)
* x_i and y_i are integers.
Input
The input is given from Standard Input in the following format:
N a b
x_1 y_1
:
x_N y_N
Output
Print the number of the pairs of pinholes that can be pointed by the compass.
Examples
Input
5 1 2
1 1
4 3
6 1
5 5
4 8
Output
4
Input
6 2 3
1 3
5 3
3 5
8 4
4 7
2 5
Output
4
Input
8 1 2
1 5
4 3
8 2
4 7
8 8
3 3
6 6
4 8
Output
7
|
instruction
| 0
| 1,570
| 3
| 3,140
|
"Correct Solution:
```
N, a, b = map(int, input().split()); a -= 1; b -= 1
P = []
Q = []
for i in range(N):
x, y = map(int, input().split())
P.append((x-y, x+y, i))
Q.append((x+y, x-y, i))
d = max(abs(P[a][0] - P[b][0]), abs(P[a][1] - P[b][1]))
*parent, = range(N)
def root(x):
if x == parent[x]:
return x
y = parent[x] = root(parent[x])
return y
def unite(x, y):
px = root(x); py = root(y)
if px < py:
parent[py] = px
else:
parent[px] = py
C = [0]*N
D = [0]*N
def check(P0, i0, j0):
return abs(P0[i0][0] - P0[j0][0]) == abs(P0[i0][1] - P0[j0][1])
def solve(P0):
P = P0[:]
P.sort()
s = t = 0; prev = -1
for i in range(N):
x, y, i0 = P[i]
while t < N and P[t][0] < x-d or (P[t][0] == x-d and P[t][1] <= y+d): t += 1
while s < N and (P[s][0] < x-d or (P[s][0] == x-d and P[s][1] < y-d)): s += 1
if s < t:
j0 = P[s][2]
unite(i0, j0)
if check(P0, i0, j0):
D[i0] += 1
else:
C[i0] += 1
if s < t-1:
j0 = P[t-1][2]
if check(P0, i0, j0):
D[i0] += 1
C[i0] += t-s-2
else:
C[i0] += t-s-1
for j in range(max(prev, s), t-1):
unite(P[j][2], P[j+1][2])
prev = t-1
solve(P)
solve(Q)
S = T = 0
r = root(a)
for i in range(N):
if root(i) == r:
S += C[i]; T += D[i]
print(S + T//2)
```
|
output
| 1
| 1,570
| 3
| 3,141
|
Provide a correct Python 3 solution for this coding contest problem.
There are N pinholes on the xy-plane. The i-th pinhole is located at (x_i,y_i).
We will denote the Manhattan distance between the i-th and j-th pinholes as d(i,j)(=|x_i-x_j|+|y_i-y_j|).
You have a peculiar pair of compasses, called Manhattan Compass. This instrument always points at two of the pinholes. The two legs of the compass are indistinguishable, thus we do not distinguish the following two states: the state where the compass points at the p-th and q-th pinholes, and the state where it points at the q-th and p-th pinholes.
When the compass points at the p-th and q-th pinholes and d(p,q)=d(p,r), one of the legs can be moved so that the compass will point at the p-th and r-th pinholes.
Initially, the compass points at the a-th and b-th pinholes. Find the number of the pairs of pinholes that can be pointed by the compass.
Constraints
* 2≦N≦10^5
* 1≦x_i, y_i≦10^9
* 1≦a < b≦N
* When i ≠ j, (x_i, y_i) ≠ (x_j, y_j)
* x_i and y_i are integers.
Input
The input is given from Standard Input in the following format:
N a b
x_1 y_1
:
x_N y_N
Output
Print the number of the pairs of pinholes that can be pointed by the compass.
Examples
Input
5 1 2
1 1
4 3
6 1
5 5
4 8
Output
4
Input
6 2 3
1 3
5 3
3 5
8 4
4 7
2 5
Output
4
Input
8 1 2
1 5
4 3
8 2
4 7
8 8
3 3
6 6
4 8
Output
7
|
instruction
| 0
| 1,571
| 3
| 3,142
|
"Correct Solution:
```
import sys
input = sys.stdin.readline
from collections import defaultdict
import bisect
INF = 10 ** 12
N,a,b = map(int,input().split())
X_to_Y = defaultdict(lambda: [-INF,INF])
Y_to_X = defaultdict(lambda: [-INF,INF])
for i in range(1,N+1):
x,y = map(int,input().split())
x,y = x+y,x-y
X_to_Y[x].append(y)
Y_to_X[y].append(x)
if i == a:
sx,sy = x,y
elif i == b:
tx,ty = x,y
R = max(abs(sx-tx), abs(sy-ty))
for key, arr in X_to_Y.items():
arr.sort()
L = len(arr)
move_right = list(range(1,L)) + [None]
X_to_Y[key] = [arr,move_right]
for key, arr in Y_to_X.items():
arr.sort()
L = len(arr)
move_right = list(range(1,L)) + [None]
Y_to_X[key] = [arr,move_right]
equiv_class = set([(sx,sy), (tx,ty)])
answer = 0
task = [(sx,sy), (tx,ty)]
while task:
x,y = task.pop()
# 既出の元も含めて辺の個数を数える
# 未出の元を同値に追加
# x,y両方満たすものは、x側にだけ入れる
for x1 in [x-R,x+R]:
if not (x1 in X_to_Y):
continue
arr, move_right = X_to_Y[x1]
left = bisect.bisect_left(arr, y-R)
right = bisect.bisect_right(arr, y+R)
# [left, right) が辺で結べるターゲット
answer += right - left # 辺の個数
i = left
while i < right:
y1 = arr[i]
if (x1,y1) not in equiv_class:
equiv_class.add((x1,y1))
task.append((x1,y1))
# なるべく、既に居ない元は見ないで済ませるように
next_i = move_right[i]
if next_i >= right:
break
move_right[i] = right
i = next_i
for y1 in [y-R,y+R]:
if not y1 in Y_to_X:
continue
arr, move_right = Y_to_X[y1]
left = bisect.bisect_left(arr, x-R+1)
right = bisect.bisect_right(arr, x+R-1)
# [left, right) が辺で結べるターゲット
answer += right - left # 辺の個数
i = left
while i < right:
x1 = arr[i]
if (x1,y1) not in equiv_class:
equiv_class.add((x1,y1))
task.append((x1,y1))
# なるべく、既に居ない元は見ないで済ませるように
next_i = move_right[i]
if next_i >= right:
break
move_right[i] = right
i = next_i
answer //= 2 # 両方向から辺を見た
print(answer)
```
|
output
| 1
| 1,571
| 3
| 3,143
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N pinholes on the xy-plane. The i-th pinhole is located at (x_i,y_i).
We will denote the Manhattan distance between the i-th and j-th pinholes as d(i,j)(=|x_i-x_j|+|y_i-y_j|).
You have a peculiar pair of compasses, called Manhattan Compass. This instrument always points at two of the pinholes. The two legs of the compass are indistinguishable, thus we do not distinguish the following two states: the state where the compass points at the p-th and q-th pinholes, and the state where it points at the q-th and p-th pinholes.
When the compass points at the p-th and q-th pinholes and d(p,q)=d(p,r), one of the legs can be moved so that the compass will point at the p-th and r-th pinholes.
Initially, the compass points at the a-th and b-th pinholes. Find the number of the pairs of pinholes that can be pointed by the compass.
Constraints
* 2≦N≦10^5
* 1≦x_i, y_i≦10^9
* 1≦a < b≦N
* When i ≠ j, (x_i, y_i) ≠ (x_j, y_j)
* x_i and y_i are integers.
Input
The input is given from Standard Input in the following format:
N a b
x_1 y_1
:
x_N y_N
Output
Print the number of the pairs of pinholes that can be pointed by the compass.
Examples
Input
5 1 2
1 1
4 3
6 1
5 5
4 8
Output
4
Input
6 2 3
1 3
5 3
3 5
8 4
4 7
2 5
Output
4
Input
8 1 2
1 5
4 3
8 2
4 7
8 8
3 3
6 6
4 8
Output
7
Submitted Solution:
```
import bisect
import sys
from collections import defaultdict
class RandomizedBinarySearchTree:
def __init__(self):
self.children = [[-1], [-1]]
self.values = [0]
self.counts = [0]
self.indices = set()
self.root = 0
def merge(self, left_root, right_root):
children = self.children
counts = self.counts
li = left_root
ri = right_root
stack = []
switch = 0
while li != 0 and ri != 0:
if switch:
stack.append((li, 1))
li = children[1][li]
else:
stack.append((ri, 0))
ri = children[0][ri]
switch ^= 1
i = li if li != 0 else ri
while stack:
pi, is_right = stack.pop()
children[is_right][pi] = i
counts[pi] = counts[children[0][pi]] + counts[children[1][pi]] + 1
i = pi
return i
def split(self, root, x):
i = root
lefts, rights = self.children
values = self.values
counts = self.counts
l_stack = []
r_stack = []
while i != 0:
if x < values[i]:
r_stack.append(i)
i = lefts[i]
else:
l_stack.append(i)
i = rights[i]
li, ri = 0, 0
while l_stack:
pi = l_stack.pop()
rights[pi] = li
counts[pi] = counts[lefts[pi]] + counts[li] + 1
li = pi
while r_stack:
pi = r_stack.pop()
lefts[pi] = ri
counts[pi] = counts[ri] + counts[rights[pi]] + 1
ri = pi
return li, ri
def insert(self, x):
if self.indices:
ni = self.indices.pop()
self.children[0][ni] = self.children[1][ni] = 0
self.values[ni] = x
self.counts[ni] = 1
else:
ni = len(self.values)
self.children[0].append(0)
self.children[1].append(0)
self.values.append(x)
self.counts.append(1)
li, ri = self.split(self.root, x)
self.root = self.merge(self.merge(li, ni), ri)
def delete(self, x):
li, mri = self.split(self.root, x - 1)
mi, ri = self.split(mri, x)
if mi == 0:
self.root = self.merge(li, ri)
return
self.indices.add(mi)
self.root = self.merge(li, ri)
return
def upper_bound(self, x, default=-1):
i = self.root
lefts, rights = self.children
values = self.values
counts = self.counts
y = default
c = counts[i]
j = 0
while i != 0:
if x < values[i]:
y = values[i]
c = j + counts[lefts[i]]
i = lefts[i]
else:
j += counts[lefts[i]] + 1
i = rights[i]
return y, c
def lower_bound(self, x, default=-1):
i = self.root
lefts, rights = self.children
values = self.values
counts = self.counts
y = default
c = counts[i]
j = 0
while i != 0:
if x <= values[i]:
y = values[i]
c = j + counts[lefts[i]]
i = lefts[i]
else:
j += counts[lefts[i]] + 1
i = rights[i]
return y, c
def get_k_th(self, k, default=-1):
i = self.root
children = self.children
values = self.values
counts = self.counts
if counts[i] <= k:
return default
j = k
while i != 0:
left_count = counts[children[0][i]]
if left_count == j:
return values[i]
elif left_count > j:
i = children[0][i]
else:
j -= left_count + 1
i = children[1][i]
return default
def debug_print(self):
print('Lefts ', self.children[0])
print('Rights', self.children[1])
print('Values', self.values)
print('Counts', self.counts)
self._debug_print(self.root, 0)
def _debug_print(self, i, depth):
if i != -1:
self._debug_print(self.children[0][i], depth + 1)
print(' ' * depth, self.values[i], self.counts[i])
self._debug_print(self.children[1][i], depth + 1)
def x_check(x_dict, rev_dict, nx, y, d, stack, stacked):
counter = 0
if nx in x_dict:
rbst, lst = x_dict[nx]
ny, _ = rbst.lower_bound(y - d, y + d + 1)
while ny <= y + d:
q = rev_dict[nx, ny]
if q not in stacked:
stack.append(q)
stacked.add(q)
rbst.delete(ny)
ny, _ = rbst.lower_bound(y - d, y + d + 1)
i = bisect.bisect_left(lst, y - d)
j = bisect.bisect(lst, y + d)
counter = j - i
return counter
def y_check(y_dict, rev_dict, ny, x, d, stack, stacked):
counter = 0
if ny in y_dict:
rbst, lst = y_dict[ny]
nx, _ = rbst.lower_bound(x - d, x + d + 1)
while nx <= x + d:
q = rev_dict[nx, ny]
if q not in stacked:
stack.append(q)
stacked.add(q)
rbst.delete(nx)
nx, _ = rbst.lower_bound(x - d, x + d + 1)
i = bisect.bisect(lst, x - d)
j = bisect.bisect_left(lst, x + d)
counter = j - i
return counter
n, a, b, *xy = map(int, sys.stdin.buffer.read().split())
xxx = xy[0::2]
yyy = xy[1::2]
x_dict = defaultdict(lambda: [RandomizedBinarySearchTree(), []])
y_dict = defaultdict(lambda: [RandomizedBinarySearchTree(), []])
ppp = []
rev_dict = {}
for i in range(n):
x = xxx[i] - yyy[i]
y = xxx[i] + yyy[i]
x_dict[x][0].insert(y)
y_dict[y][0].insert(x)
x_dict[x][1].append(y)
y_dict[y][1].append(x)
ppp.append((x, y))
rev_dict[x, y] = i
for _, lst in x_dict.values():
lst.sort()
for _, lst in y_dict.values():
lst.sort()
a -= 1
b -= 1
ax, ay = ppp[a]
bx, by = ppp[b]
d = max(abs(ax - bx), abs(ay - by))
x_dict[ax][0].delete(ay)
x_dict[bx][0].delete(by)
y_dict[ay][0].delete(ax)
y_dict[by][0].delete(bx)
stack = [a, b]
stacked = {a, b}
ans = 0
while stack:
p = stack.pop()
x, y = ppp[p]
ans += x_check(x_dict, rev_dict, x - d, y, d, stack, stacked)
ans += x_check(x_dict, rev_dict, x + d, y, d, stack, stacked)
ans += y_check(y_dict, rev_dict, y - d, x, d, stack, stacked)
ans += y_check(y_dict, rev_dict, y + d, x, d, stack, stacked)
print(ans // 2)
```
|
instruction
| 0
| 1,572
| 3
| 3,144
|
No
|
output
| 1
| 1,572
| 3
| 3,145
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N pinholes on the xy-plane. The i-th pinhole is located at (x_i,y_i).
We will denote the Manhattan distance between the i-th and j-th pinholes as d(i,j)(=|x_i-x_j|+|y_i-y_j|).
You have a peculiar pair of compasses, called Manhattan Compass. This instrument always points at two of the pinholes. The two legs of the compass are indistinguishable, thus we do not distinguish the following two states: the state where the compass points at the p-th and q-th pinholes, and the state where it points at the q-th and p-th pinholes.
When the compass points at the p-th and q-th pinholes and d(p,q)=d(p,r), one of the legs can be moved so that the compass will point at the p-th and r-th pinholes.
Initially, the compass points at the a-th and b-th pinholes. Find the number of the pairs of pinholes that can be pointed by the compass.
Constraints
* 2≦N≦10^5
* 1≦x_i, y_i≦10^9
* 1≦a < b≦N
* When i ≠ j, (x_i, y_i) ≠ (x_j, y_j)
* x_i and y_i are integers.
Input
The input is given from Standard Input in the following format:
N a b
x_1 y_1
:
x_N y_N
Output
Print the number of the pairs of pinholes that can be pointed by the compass.
Examples
Input
5 1 2
1 1
4 3
6 1
5 5
4 8
Output
4
Input
6 2 3
1 3
5 3
3 5
8 4
4 7
2 5
Output
4
Input
8 1 2
1 5
4 3
8 2
4 7
8 8
3 3
6 6
4 8
Output
7
Submitted Solution:
```
import bisect
import sys
import random
from collections import defaultdict
class RandomizedBinarySearchTree:
def __init__(self):
self.children = [[-1], [-1]]
self.values = [0]
self.counts = [0]
self.indices = set()
self.root = 0
def merge(self, left_root, right_root):
children = self.children
counts = self.counts
li = left_root
ri = right_root
stack = []
while li != 0 and ri != 0:
if random.randrange(counts[li] + counts[ri]) < counts[li]:
stack.append((li, 1))
li = children[1][li]
else:
stack.append((ri, 0))
ri = children[0][ri]
i = li if li != 0 else ri
while stack:
pi, is_right = stack.pop()
children[is_right][pi] = i
counts[pi] = counts[children[0][pi]] + counts[children[1][pi]] + 1
i = pi
return i
def split(self, root, x):
i = root
lefts, rights = self.children
values = self.values
counts = self.counts
l_stack = []
r_stack = []
while i != 0:
if x < values[i]:
r_stack.append(i)
i = lefts[i]
else:
l_stack.append(i)
i = rights[i]
li, ri = 0, 0
while l_stack:
pi = l_stack.pop()
rights[pi] = li
counts[pi] = counts[lefts[pi]] + counts[li] + 1
li = pi
while r_stack:
pi = r_stack.pop()
lefts[pi] = ri
counts[pi] = counts[ri] + counts[rights[pi]] + 1
ri = pi
return li, ri
def insert(self, x):
if self.indices:
ni = self.indices.pop()
self.children[0][ni] = self.children[1][ni] = 0
self.values[ni] = x
self.counts[ni] = 1
else:
ni = len(self.values)
self.children[0].append(0)
self.children[1].append(0)
self.values.append(x)
self.counts.append(1)
li, ri = self.split(self.root, x)
self.root = self.merge(self.merge(li, ni), ri)
def delete(self, x):
li, mri = self.split(self.root, x - 1)
mi, ri = self.split(mri, x)
if mi == 0:
self.root = self.merge(li, ri)
return
self.indices.add(mi)
self.root = self.merge(li, ri)
return
def upper_bound(self, x, default=-1):
i = self.root
lefts, rights = self.children
values = self.values
counts = self.counts
y = default
c = counts[i]
j = 0
while i != 0:
if x < values[i]:
y = values[i]
c = j + counts[lefts[i]]
i = lefts[i]
else:
j += counts[lefts[i]] + 1
i = rights[i]
return y, c
def lower_bound(self, x, default=-1):
i = self.root
lefts, rights = self.children
values = self.values
counts = self.counts
y = default
c = counts[i]
j = 0
while i != 0:
if x <= values[i]:
y = values[i]
c = j + counts[lefts[i]]
i = lefts[i]
else:
j += counts[lefts[i]] + 1
i = rights[i]
return y, c
def get_k_th(self, k, default=-1):
i = self.root
children = self.children
values = self.values
counts = self.counts
if counts[i] <= k:
return default
j = k
while i != 0:
left_count = counts[children[0][i]]
if left_count == j:
return values[i]
elif left_count > j:
i = children[0][i]
else:
j -= left_count + 1
i = children[1][i]
return default
def debug_print(self):
print('Lefts ', self.children[0])
print('Rights', self.children[1])
print('Values', self.values)
print('Counts', self.counts)
self._debug_print(self.root, 0)
def _debug_print(self, i, depth):
if i != -1:
self._debug_print(self.children[0][i], depth + 1)
print(' ' * depth, self.values[i], self.counts[i])
self._debug_print(self.children[1][i], depth + 1)
def x_check(x_dict, rev_dict, nx, y, d):
ret = set()
counter = 0
if nx in x_dict:
rbst, lst = x_dict[nx]
ny, _ = rbst.lower_bound(y - d, y + d + 1)
while ny <= y + d:
q = rev_dict[nx, ny]
ret.add(q)
rbst.delete(ny)
ny, _ = rbst.lower_bound(y - d, y + d + 1)
i = bisect.bisect_left(lst, y - d)
j = bisect.bisect(lst, y + d)
counter = j - i
return ret, counter
def y_check(y_dict, rev_dict, ny, x, d):
ret = set()
counter = 0
if ny in y_dict:
rbst, lst = y_dict[ny]
nx, _ = rbst.lower_bound(x - d, x + d + 1)
while nx <= x + d:
q = rev_dict[nx, ny]
ret.add(q)
rbst.delete(nx)
nx, _ = rbst.lower_bound(x - d, x + d + 1)
i = bisect.bisect(lst, x - d)
j = bisect.bisect_left(lst, x + d)
counter = j - i
return ret, counter
n, a, b, *xy = map(int, sys.stdin.buffer.read().split())
xxx = xy[0::2]
yyy = xy[1::2]
x_dict = defaultdict(lambda: [RandomizedBinarySearchTree(), []])
y_dict = defaultdict(lambda: [RandomizedBinarySearchTree(), []])
ppp = []
rev_dict = {}
for i in range(n):
x = xxx[i] - yyy[i]
y = xxx[i] + yyy[i]
x_dict[x][0].insert(y)
y_dict[y][0].insert(x)
x_dict[x][1].append(y)
y_dict[y][1].append(x)
ppp.append((x, y))
rev_dict[x, y] = i
for _, lst in x_dict.values():
lst.sort()
for _, lst in y_dict.values():
lst.sort()
a -= 1
b -= 1
ax, ay = ppp[a]
bx, by = ppp[b]
d = max(abs(ax - bx), abs(ay - by))
x_dict[ax][0].delete(ay)
x_dict[bx][0].delete(by)
y_dict[ay][0].delete(ax)
y_dict[by][0].delete(bx)
stack = [a, b]
stacked = {a, b}
ans = 0
while stack:
p = stack.pop()
x, y = ppp[p]
qqq = set()
qs, c = x_check(x_dict, rev_dict, x - d, y, d)
qqq.update(qs)
ans += c
qs, c = x_check(x_dict, rev_dict, x + d, y, d)
qqq.update(qs)
ans += c
qs, c = y_check(y_dict, rev_dict, y - d, x, d)
qqq.update(qs)
ans += c
qs, c = y_check(y_dict, rev_dict, y + d, x, d)
qqq.update(qs)
ans += c
for q in qqq:
if q not in stacked:
stack.append(q)
stacked.add(q)
print(ans // 2)
```
|
instruction
| 0
| 1,573
| 3
| 3,146
|
No
|
output
| 1
| 1,573
| 3
| 3,147
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N pinholes on the xy-plane. The i-th pinhole is located at (x_i,y_i).
We will denote the Manhattan distance between the i-th and j-th pinholes as d(i,j)(=|x_i-x_j|+|y_i-y_j|).
You have a peculiar pair of compasses, called Manhattan Compass. This instrument always points at two of the pinholes. The two legs of the compass are indistinguishable, thus we do not distinguish the following two states: the state where the compass points at the p-th and q-th pinholes, and the state where it points at the q-th and p-th pinholes.
When the compass points at the p-th and q-th pinholes and d(p,q)=d(p,r), one of the legs can be moved so that the compass will point at the p-th and r-th pinholes.
Initially, the compass points at the a-th and b-th pinholes. Find the number of the pairs of pinholes that can be pointed by the compass.
Constraints
* 2≦N≦10^5
* 1≦x_i, y_i≦10^9
* 1≦a < b≦N
* When i ≠ j, (x_i, y_i) ≠ (x_j, y_j)
* x_i and y_i are integers.
Input
The input is given from Standard Input in the following format:
N a b
x_1 y_1
:
x_N y_N
Output
Print the number of the pairs of pinholes that can be pointed by the compass.
Examples
Input
5 1 2
1 1
4 3
6 1
5 5
4 8
Output
4
Input
6 2 3
1 3
5 3
3 5
8 4
4 7
2 5
Output
4
Input
8 1 2
1 5
4 3
8 2
4 7
8 8
3 3
6 6
4 8
Output
7
Submitted Solution:
```
import numpy as np
n, a, b = map(int, input().split())
holes = np.fromiter((complex(*map(int, input().split())) for _ in range(n)), dtype=complex)
ab = holes[a - 1] - holes[b - 1]
d = abs(ab.real) + abs(ab.imag)
links = [set() for _ in range(n)]
for i in range(0, n, 100):
xy1, xy2 = np.meshgrid(holes, holes[i:i + 100])
diff = xy1 - xy2
dists = abs(diff.real) + abs(diff.imag)
for x, y in zip(*np.where(dists == d)):
links[x].add(y)
visited = set()
queue = [a - 1]
while queue:
p = queue.pop()
if p in visited:
continue
visited.add(p)
queue.extend(q for q in links[p] if q not in visited)
print(sum(p < q for p in visited for q in links[p]))
```
|
instruction
| 0
| 1,574
| 3
| 3,148
|
No
|
output
| 1
| 1,574
| 3
| 3,149
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N pinholes on the xy-plane. The i-th pinhole is located at (x_i,y_i).
We will denote the Manhattan distance between the i-th and j-th pinholes as d(i,j)(=|x_i-x_j|+|y_i-y_j|).
You have a peculiar pair of compasses, called Manhattan Compass. This instrument always points at two of the pinholes. The two legs of the compass are indistinguishable, thus we do not distinguish the following two states: the state where the compass points at the p-th and q-th pinholes, and the state where it points at the q-th and p-th pinholes.
When the compass points at the p-th and q-th pinholes and d(p,q)=d(p,r), one of the legs can be moved so that the compass will point at the p-th and r-th pinholes.
Initially, the compass points at the a-th and b-th pinholes. Find the number of the pairs of pinholes that can be pointed by the compass.
Constraints
* 2≦N≦10^5
* 1≦x_i, y_i≦10^9
* 1≦a < b≦N
* When i ≠ j, (x_i, y_i) ≠ (x_j, y_j)
* x_i and y_i are integers.
Input
The input is given from Standard Input in the following format:
N a b
x_1 y_1
:
x_N y_N
Output
Print the number of the pairs of pinholes that can be pointed by the compass.
Examples
Input
5 1 2
1 1
4 3
6 1
5 5
4 8
Output
4
Input
6 2 3
1 3
5 3
3 5
8 4
4 7
2 5
Output
4
Input
8 1 2
1 5
4 3
8 2
4 7
8 8
3 3
6 6
4 8
Output
7
Submitted Solution:
```
import sys
from collections import defaultdict
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]
def main():
def dist(i,j):
x0,y0=xy[i]
x1,y1=xy[j]
return abs(x0-x1)+abs(y0-y1)
def dfs(i):
res=0
x,y=xy[i]
zx,zy=x//d,y//d
for dx,dy in [(0,0),(1,0),(0,1),(1,1),(-1,0),(0,-1),(-1,-1),(-1,1),(1,-1)]:
for j in zone[zx+dx,zx+dy]:
if fin[i,j]:continue
if dist(i,j)==d:
fin[i,j]=fin[j,i]=True
res+=1
if not vis[j]:
vis[j]=True
res+=dfs(j)
return res
n,a,b=MI()
a,b=a-1,b-1
xy=LLI(n)
d=dist(a,b)
zone=defaultdict(list)
for i,(x,y) in enumerate(xy):zone[x//d,y//d].append(i)
#print(zone)
vis=[False]*n
fin=defaultdict(bool)
print(dfs(a))
main()
```
|
instruction
| 0
| 1,575
| 3
| 3,150
|
No
|
output
| 1
| 1,575
| 3
| 3,151
|
Provide a correct Python 3 solution for this coding contest problem.
Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive?
Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA".
You may assume that the number of holes is less than 10000.
Input
The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows:
A B C
n
R1
R2
.
.
Rn
n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole.
Output
For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole.
Example
Input
10 6 8
5
4
8
6
2
5
0 0 0
Output
NA
OK
OK
NA
NA
|
instruction
| 0
| 1,577
| 3
| 3,154
|
"Correct Solution:
```
while True:
d, w, h = map(int, input().split())
if d == 0:
break
n = int(input())
a, b = sorted([d, w, h])[:2]
t = pow(pow(a, 2) + pow(b, 2), 0.5) // 2
for _ in range(n):
print("OK" if int(input()) > t else "NA")
```
|
output
| 1
| 1,577
| 3
| 3,155
|
Provide a correct Python 3 solution for this coding contest problem.
Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive?
Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA".
You may assume that the number of holes is less than 10000.
Input
The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows:
A B C
n
R1
R2
.
.
Rn
n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole.
Output
For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole.
Example
Input
10 6 8
5
4
8
6
2
5
0 0 0
Output
NA
OK
OK
NA
NA
|
instruction
| 0
| 1,578
| 3
| 3,156
|
"Correct Solution:
```
while True:
Size_lis = list(map(int,input().split()))
if Size_lis == [0,0,0]:
break
n = int(input())
Size_lis.remove(max(Size_lis))
m_r = (Size_lis[0] / 2) ** 2 + (Size_lis[1] / 2) ** 2
for i in range(n):
r = int(input())
if m_r >= r ** 2:
print("NA")
else:
print("OK")
```
|
output
| 1
| 1,578
| 3
| 3,157
|
Provide a correct Python 3 solution for this coding contest problem.
Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive?
Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA".
You may assume that the number of holes is less than 10000.
Input
The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows:
A B C
n
R1
R2
.
.
Rn
n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole.
Output
For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole.
Example
Input
10 6 8
5
4
8
6
2
5
0 0 0
Output
NA
OK
OK
NA
NA
|
instruction
| 0
| 1,579
| 3
| 3,158
|
"Correct Solution:
```
while 1:
try:
dwh=list(map(int,input().split()))
if dwh[0]==dwh[1]==dwh[2]==0:break
dwh.sort()
std=(dwh[0]**2+dwh[1]**2)**0.5/2
n=int(input())
for i in range(n):
r=int(input())
if std<r:print("OK")
else:print("NA")
except:break
```
|
output
| 1
| 1,579
| 3
| 3,159
|
Provide a correct Python 3 solution for this coding contest problem.
Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive?
Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA".
You may assume that the number of holes is less than 10000.
Input
The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows:
A B C
n
R1
R2
.
.
Rn
n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole.
Output
For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole.
Example
Input
10 6 8
5
4
8
6
2
5
0 0 0
Output
NA
OK
OK
NA
NA
|
instruction
| 0
| 1,580
| 3
| 3,160
|
"Correct Solution:
```
while True:
d, w, h = (int(x) for x in input().split())
if d == 0: break
q = int(input())
vmin = min(d * d + w * w, d * d + h * h, w * w + h * h)
for i in range(0,q):
r = int(input())
ans = 'NA'
if (2 * r) ** 2 > vmin:
ans = 'OK'
print(ans)
```
|
output
| 1
| 1,580
| 3
| 3,161
|
Provide a correct Python 3 solution for this coding contest problem.
Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive?
Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA".
You may assume that the number of holes is less than 10000.
Input
The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows:
A B C
n
R1
R2
.
.
Rn
n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole.
Output
For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole.
Example
Input
10 6 8
5
4
8
6
2
5
0 0 0
Output
NA
OK
OK
NA
NA
|
instruction
| 0
| 1,581
| 3
| 3,162
|
"Correct Solution:
```
while 1:
cheese=list(map(int,input().split()))
if cheese[0]==0:break
n=int(input())
cheese=sorted(cheese)
for i in range(n):
radius = int(input())
if radius*2>(cheese[0]**2+cheese[1]**2)**0.5:print("OK")
else:print("NA")
```
|
output
| 1
| 1,581
| 3
| 3,163
|
Provide a correct Python 3 solution for this coding contest problem.
Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive?
Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA".
You may assume that the number of holes is less than 10000.
Input
The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows:
A B C
n
R1
R2
.
.
Rn
n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole.
Output
For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole.
Example
Input
10 6 8
5
4
8
6
2
5
0 0 0
Output
NA
OK
OK
NA
NA
|
instruction
| 0
| 1,582
| 3
| 3,164
|
"Correct Solution:
```
import math
output = []
while True:
depth, width, height = [int(item) for item in input().split(" ")]
if depth == 0 and width == 0 and height == 0:
break
cheeseRadius = math.sqrt((width / 2)**2 + (height / 2)**2)
inputCount = int(input())
for lp in range(inputCount):
entranceRadius = int(input())
if cheeseRadius < entranceRadius:
output.append("OK")
else:
output.append("NA")
print("\n".join(output))
```
|
output
| 1
| 1,582
| 3
| 3,165
|
Provide a correct Python 3 solution for this coding contest problem.
Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive?
Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA".
You may assume that the number of holes is less than 10000.
Input
The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows:
A B C
n
R1
R2
.
.
Rn
n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole.
Output
For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole.
Example
Input
10 6 8
5
4
8
6
2
5
0 0 0
Output
NA
OK
OK
NA
NA
|
instruction
| 0
| 1,583
| 3
| 3,166
|
"Correct Solution:
```
while True:
d, w, h = sorted(map(int, input().split()))
if d == w == h == 0:
break
dw = (d**2 + w**2)
n = int(input())
for i in range(n):
y = int(input())
if (2*y)**2 > dw:
print("OK")
else:
print("NA")
```
|
output
| 1
| 1,583
| 3
| 3,167
|
Provide a correct Python 3 solution for this coding contest problem.
Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive?
Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA".
You may assume that the number of holes is less than 10000.
Input
The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows:
A B C
n
R1
R2
.
.
Rn
n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole.
Output
For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole.
Example
Input
10 6 8
5
4
8
6
2
5
0 0 0
Output
NA
OK
OK
NA
NA
|
instruction
| 0
| 1,584
| 3
| 3,168
|
"Correct Solution:
```
while 1:
a,b,_=sorted(map(int,input().split()))
if a==0:break
c=(a**2+b**2)**0.5//2
for _ in range(int(input())):print('OK' if c<int(input()) else 'NA')
```
|
output
| 1
| 1,584
| 3
| 3,169
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive?
Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA".
You may assume that the number of holes is less than 10000.
Input
The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows:
A B C
n
R1
R2
.
.
Rn
n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole.
Output
For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole.
Example
Input
10 6 8
5
4
8
6
2
5
0 0 0
Output
NA
OK
OK
NA
NA
Submitted Solution:
```
def get_input():
while True:
try:
yield ''.join(input())
except EOFError:
break
while True:
d = [int(i) for i in input().split()]
if d[0] == 0 and d[1] == 0 and d[2] == 0:
break
d.sort()
cheese = d[0] * d[0] + d[1] * d[1]
N = int(input())
for l in range(N):
R = int(input())*2
if cheese < R*R:
print("OK")
else:
print("NA")
```
|
instruction
| 0
| 1,585
| 3
| 3,170
|
Yes
|
output
| 1
| 1,585
| 3
| 3,171
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive?
Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA".
You may assume that the number of holes is less than 10000.
Input
The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows:
A B C
n
R1
R2
.
.
Rn
n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole.
Output
For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole.
Example
Input
10 6 8
5
4
8
6
2
5
0 0 0
Output
NA
OK
OK
NA
NA
Submitted Solution:
```
import math
while True :
d, w, h = map(int, input().split())
if d == 0 and w == 0 and h == 0 :
break
length = [d, w, h]
len_1 = sorted([d, w, h])[0]
len_2 = sorted([d, w, h])[1]
max_d = math.sqrt(len_1**2 + len_2**2)
n = int(input())
for i in range(n) :
r = int(input())
if 2 * r > max_d :
print("OK")
else :
print("NA")
```
|
instruction
| 0
| 1,586
| 3
| 3,172
|
Yes
|
output
| 1
| 1,586
| 3
| 3,173
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive?
Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA".
You may assume that the number of holes is less than 10000.
Input
The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows:
A B C
n
R1
R2
.
.
Rn
n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole.
Output
For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole.
Example
Input
10 6 8
5
4
8
6
2
5
0 0 0
Output
NA
OK
OK
NA
NA
Submitted Solution:
```
while True:
ch = list(map(int,input().split()))
if (0 in ch):
break
ch.sort()
n = int(input())
a = ch[0]
b = ch[1]
d = a**2 + b**2
for i in range(n):
r = int(input())
R = 2*r
if (R**2 > d):
print('OK')
else:
print('NA')
```
|
instruction
| 0
| 1,587
| 3
| 3,174
|
Yes
|
output
| 1
| 1,587
| 3
| 3,175
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive?
Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA".
You may assume that the number of holes is less than 10000.
Input
The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows:
A B C
n
R1
R2
.
.
Rn
n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole.
Output
For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole.
Example
Input
10 6 8
5
4
8
6
2
5
0 0 0
Output
NA
OK
OK
NA
NA
Submitted Solution:
```
while True:
d, w,h =list(map(int, input().split()))
if d ==0 and w==0 and h==0:
break
m = min(d*d+w*w, d*d+h*h, w*w+h*h)
for i in range(int(input())):
r = int(input())
if (2*r)**2 >m:
print("OK")
else:
print("NA")
```
|
instruction
| 0
| 1,588
| 3
| 3,176
|
Yes
|
output
| 1
| 1,588
| 3
| 3,177
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive?
Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA".
You may assume that the number of holes is less than 10000.
Input
The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows:
A B C
n
R1
R2
.
.
Rn
n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole.
Output
For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole.
Example
Input
10 6 8
5
4
8
6
2
5
0 0 0
Output
NA
OK
OK
NA
NA
Submitted Solution:
```
import math
while True:
d,w,h = map(int,input().split())
if d == 0:
break
n = int(input())
dist = [d**2+w**2, d**2+h**2, w**2+h**2]
leng = min(dist)
for i in range(n):
if int(input())-leng > 0:
print('OK')
else:
print('NA')
```
|
instruction
| 0
| 1,589
| 3
| 3,178
|
No
|
output
| 1
| 1,589
| 3
| 3,179
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive?
Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA".
You may assume that the number of holes is less than 10000.
Input
The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows:
A B C
n
R1
R2
.
.
Rn
n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole.
Output
For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole.
Example
Input
10 6 8
5
4
8
6
2
5
0 0 0
Output
NA
OK
OK
NA
NA
Submitted Solution:
```
import itertools
def ch(l,r):
t=itertools.combinations(l,2)
for i in t:
if i[0]**2+i[1]**2<4*r**2:
return True
return False
l=list(map(int,input().split()))
o=[ch(l,int(input())) for i in range(int(input()))]
input()
[print("OK") if i else print("NA") for i in o]
```
|
instruction
| 0
| 1,590
| 3
| 3,180
|
No
|
output
| 1
| 1,590
| 3
| 3,181
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive?
Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA".
You may assume that the number of holes is less than 10000.
Input
The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows:
A B C
n
R1
R2
.
.
Rn
n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole.
Output
For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole.
Example
Input
10 6 8
5
4
8
6
2
5
0 0 0
Output
NA
OK
OK
NA
NA
Submitted Solution:
```
import sys
for a in sys.stdin:
if' 'in a:
s=list(map(int,a.split()))
c=0
while 1:
t=s;s=[t.count(e)for e in t]
if t==s:break
c+=1
print(c);print(*s)
```
|
instruction
| 0
| 1,591
| 3
| 3,182
|
No
|
output
| 1
| 1,591
| 3
| 3,183
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Jerry is a little mouse. He is trying to survive from the cat Tom. Jerry is carrying a parallelepiped-like piece of cheese of size A × B × C. It is necessary to trail this cheese to the Jerry's house. There are several entrances in the Jerry's house. Each entrance is a rounded hole having its own radius R. Could you help Jerry to find suitable holes to be survive?
Your task is to create a program which estimates whether Jerry can trail the cheese via each hole. The program should print "OK" if Jerry can trail the cheese via the corresponding hole (without touching it). Otherwise the program should print "NA".
You may assume that the number of holes is less than 10000.
Input
The input is a sequence of datasets. The end of input is indicated by a line containing three zeros. Each dataset is formatted as follows:
A B C
n
R1
R2
.
.
Rn
n indicates the number of holes (entrances) and Ri indicates the radius of i-th hole.
Output
For each datasets, the output should have n lines. Each line points the result of estimation of the corresponding hole.
Example
Input
10 6 8
5
4
8
6
2
5
0 0 0
Output
NA
OK
OK
NA
NA
Submitted Solution:
```
import sys
for a in sys.stdin:
if'0'in a:break
if' 'in a:
s=list(map(int,a.split()))
c=0
while 1:
t=s;s=[t.count(e)for e in t]
if t==s:break
c+=1
print(c);print(*s)
```
|
instruction
| 0
| 1,592
| 3
| 3,184
|
No
|
output
| 1
| 1,592
| 3
| 3,185
|
Provide a correct Python 3 solution for this coding contest problem.
An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as:
$ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $
On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows.
<image>
This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $.
The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows:
$ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $
$ 7 + 6i + 7j + 8k $
$ + 14i + 12i ^ 2 + 14ij + 16ik $
$ + 21j + 18ji + 21j ^ 2 + 24jk $
$ + 28k + 24ki + 28kj + 32k ^ 2 $
By applying the table above
$ = -58 + 16i + 36j + 32k $
It will be.
Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $.
input
Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
$ n $
$ data_1 $
$ data_2 $
::
$ data_n $
The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format:
$ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $
All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50.
output
Prints the product of a given set of quaternions for each dataset.
Example
Input
2
1 2 3 4 7 6 7 8
5 6 7 8 3 2 3 4
0
Output
-58 16 36 32
-50 32 28 48
|
instruction
| 0
| 1,593
| 3
| 3,186
|
"Correct Solution:
```
while 1:
n=int(input())
if n==0:break
for _ in range(n):
x1,y1,z1,w1,x2,y2,z2,w2=map(int,input().split())
print((x1*x2) - (y1*y2) -(z1*z2) -(w1*w2),
(x1*y2) + (y1*x2) + (z1*w2) - (w1*z2),
(x1*z2) - (y1*w2) + (z1*x2) + (w1*y2),
(x1*w2) + (y1*z2) - (z1*y2) + (w1*x2))
```
|
output
| 1
| 1,593
| 3
| 3,187
|
Provide a correct Python 3 solution for this coding contest problem.
An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as:
$ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $
On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows.
<image>
This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $.
The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows:
$ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $
$ 7 + 6i + 7j + 8k $
$ + 14i + 12i ^ 2 + 14ij + 16ik $
$ + 21j + 18ji + 21j ^ 2 + 24jk $
$ + 28k + 24ki + 28kj + 32k ^ 2 $
By applying the table above
$ = -58 + 16i + 36j + 32k $
It will be.
Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $.
input
Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
$ n $
$ data_1 $
$ data_2 $
::
$ data_n $
The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format:
$ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $
All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50.
output
Prints the product of a given set of quaternions for each dataset.
Example
Input
2
1 2 3 4 7 6 7 8
5 6 7 8 3 2 3 4
0
Output
-58 16 36 32
-50 32 28 48
|
instruction
| 0
| 1,594
| 3
| 3,188
|
"Correct Solution:
```
# from sys import exit
while(True):
N = int(input())
if N == 0:
break
for _ in range(N):
a, b, c, d, A, B, C, D = [int(n) for n in input().split()]
z = a*A - b*B - c*C - d*D
i = a*B + b*A + c*D - d*C
j = a*C - b*D + c*A + d*B
k = a*D + b*C - c*B + d*A
print(z, i, j, k)
# a = [int(input()) for _ in range(N)]
# S = str(input())
# L = len(S)
# T = str(input())
```
|
output
| 1
| 1,594
| 3
| 3,189
|
Provide a correct Python 3 solution for this coding contest problem.
An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as:
$ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $
On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows.
<image>
This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $.
The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows:
$ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $
$ 7 + 6i + 7j + 8k $
$ + 14i + 12i ^ 2 + 14ij + 16ik $
$ + 21j + 18ji + 21j ^ 2 + 24jk $
$ + 28k + 24ki + 28kj + 32k ^ 2 $
By applying the table above
$ = -58 + 16i + 36j + 32k $
It will be.
Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $.
input
Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
$ n $
$ data_1 $
$ data_2 $
::
$ data_n $
The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format:
$ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $
All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50.
output
Prints the product of a given set of quaternions for each dataset.
Example
Input
2
1 2 3 4 7 6 7 8
5 6 7 8 3 2 3 4
0
Output
-58 16 36 32
-50 32 28 48
|
instruction
| 0
| 1,595
| 3
| 3,190
|
"Correct Solution:
```
while 1:
n = int(input())
if n == 0:
break
for _ in range(n):
data = list(map(int, input().split()))
c = data[0]*data[4] - data[1]*data[5] - \
data[2]*data[6] - data[3]*data[7]
i = data[0]*data[5] + data[1]*data[4] + \
data[2]*data[7] - data[3]*data[6]
j = data[0]*data[6] - data[1]*data[7] + \
data[2]*data[4] + data[3]*data[5]
k = data[0]*data[7] + data[1]*data[6] - \
data[2]*data[5] + data[3]*data[4]
print(c, i, j, k)
```
|
output
| 1
| 1,595
| 3
| 3,191
|
Provide a correct Python 3 solution for this coding contest problem.
An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as:
$ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $
On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows.
<image>
This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $.
The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows:
$ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $
$ 7 + 6i + 7j + 8k $
$ + 14i + 12i ^ 2 + 14ij + 16ik $
$ + 21j + 18ji + 21j ^ 2 + 24jk $
$ + 28k + 24ki + 28kj + 32k ^ 2 $
By applying the table above
$ = -58 + 16i + 36j + 32k $
It will be.
Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $.
input
Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
$ n $
$ data_1 $
$ data_2 $
::
$ data_n $
The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format:
$ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $
All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50.
output
Prints the product of a given set of quaternions for each dataset.
Example
Input
2
1 2 3 4 7 6 7 8
5 6 7 8 3 2 3 4
0
Output
-58 16 36 32
-50 32 28 48
|
instruction
| 0
| 1,596
| 3
| 3,192
|
"Correct Solution:
```
while 1:
N = int(input())
if N == 0:
break
P = [
[1, 2, 3, 4],
[2, -1, 4, -3],
[3, -4, -1, 2],
[4, 3, -2, -1],
]
for i in range(N):
*X, = map(int, input().split())
X0 = X[:4]; X1 = X[4:]
Y = [0]*4
for i, x0 in enumerate(X0):
for j, x1 in enumerate(X1):
z = P[i][j]
if z > 0:
Y[z-1] += x0*x1
else:
Y[-z-1] -= x0*x1
print(*Y)
```
|
output
| 1
| 1,596
| 3
| 3,193
|
Provide a correct Python 3 solution for this coding contest problem.
An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as:
$ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $
On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows.
<image>
This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $.
The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows:
$ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $
$ 7 + 6i + 7j + 8k $
$ + 14i + 12i ^ 2 + 14ij + 16ik $
$ + 21j + 18ji + 21j ^ 2 + 24jk $
$ + 28k + 24ki + 28kj + 32k ^ 2 $
By applying the table above
$ = -58 + 16i + 36j + 32k $
It will be.
Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $.
input
Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
$ n $
$ data_1 $
$ data_2 $
::
$ data_n $
The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format:
$ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $
All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50.
output
Prints the product of a given set of quaternions for each dataset.
Example
Input
2
1 2 3 4 7 6 7 8
5 6 7 8 3 2 3 4
0
Output
-58 16 36 32
-50 32 28 48
|
instruction
| 0
| 1,597
| 3
| 3,194
|
"Correct Solution:
```
import sys
f = sys.stdin
index = [[0,1,2,3],
[1,0,3,2],
[2,3,0,1],
[3,2,1,0]]
sign = [[1, 1, 1, 1],
[1,-1, 1,-1],
[1,-1,-1, 1],
[1, 1,-1,-1]]
while True:
n = int(f.readline())
if n == 0:
break
for _ in range(n):
ab = list(map(int, f.readline().split()))
ret = [0] * 4
for i, a in enumerate(ab[:4]):
for j, b in enumerate(ab[4:]):
ret[index[i][j]] += sign[i][j] * a * b
print(*ret)
```
|
output
| 1
| 1,597
| 3
| 3,195
|
Provide a correct Python 3 solution for this coding contest problem.
An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as:
$ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $
On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows.
<image>
This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $.
The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows:
$ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $
$ 7 + 6i + 7j + 8k $
$ + 14i + 12i ^ 2 + 14ij + 16ik $
$ + 21j + 18ji + 21j ^ 2 + 24jk $
$ + 28k + 24ki + 28kj + 32k ^ 2 $
By applying the table above
$ = -58 + 16i + 36j + 32k $
It will be.
Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $.
input
Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
$ n $
$ data_1 $
$ data_2 $
::
$ data_n $
The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format:
$ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $
All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50.
output
Prints the product of a given set of quaternions for each dataset.
Example
Input
2
1 2 3 4 7 6 7 8
5 6 7 8 3 2 3 4
0
Output
-58 16 36 32
-50 32 28 48
|
instruction
| 0
| 1,598
| 3
| 3,196
|
"Correct Solution:
```
b = [[1,2,3,4],[2,-1,4,-3],[3,-4,-1,2],[4,3,-2,-1]]
while 1:
n = int(input())
if n == 0:break
for _ in range(n):
x = tuple(map(int, input().split()))
x1, x2 = x[:4], x[4:]
a = [0,0,0,0]
for i in range(4):
for j in range(4):
if b[i][j] > 0:a[b[i][j] - 1] += x1[i] * x2[j]
else:a[-b[i][j] - 1] -= x1[i] * x2[j]
print(*a)
```
|
output
| 1
| 1,598
| 3
| 3,197
|
Provide a correct Python 3 solution for this coding contest problem.
An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as:
$ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $
On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows.
<image>
This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $.
The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows:
$ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $
$ 7 + 6i + 7j + 8k $
$ + 14i + 12i ^ 2 + 14ij + 16ik $
$ + 21j + 18ji + 21j ^ 2 + 24jk $
$ + 28k + 24ki + 28kj + 32k ^ 2 $
By applying the table above
$ = -58 + 16i + 36j + 32k $
It will be.
Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $.
input
Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
$ n $
$ data_1 $
$ data_2 $
::
$ data_n $
The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format:
$ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $
All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50.
output
Prints the product of a given set of quaternions for each dataset.
Example
Input
2
1 2 3 4 7 6 7 8
5 6 7 8 3 2 3 4
0
Output
-58 16 36 32
-50 32 28 48
|
instruction
| 0
| 1,599
| 3
| 3,198
|
"Correct Solution:
```
while True :
n = int(input())
if n == 0 :
break
for i in range(n) :
x1, y1, z1, w1, x2, y2, z2, w2 = map(int, input().split())
print((x1*x2 - y1*y2 - z1*z2 - w1*w2), (x1*y2 + x2*y1 + z1*w2 - z2*w1), (x1*z2 - y1*w2 + x2*z1 + y2*w1), (x1*w2 + y1*z2 - y2*z1 + x2*w1))
```
|
output
| 1
| 1,599
| 3
| 3,199
|
Provide a correct Python 3 solution for this coding contest problem.
An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as:
$ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $
On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows.
<image>
This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $.
The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows:
$ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $
$ 7 + 6i + 7j + 8k $
$ + 14i + 12i ^ 2 + 14ij + 16ik $
$ + 21j + 18ji + 21j ^ 2 + 24jk $
$ + 28k + 24ki + 28kj + 32k ^ 2 $
By applying the table above
$ = -58 + 16i + 36j + 32k $
It will be.
Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $.
input
Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
$ n $
$ data_1 $
$ data_2 $
::
$ data_n $
The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format:
$ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $
All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50.
output
Prints the product of a given set of quaternions for each dataset.
Example
Input
2
1 2 3 4 7 6 7 8
5 6 7 8 3 2 3 4
0
Output
-58 16 36 32
-50 32 28 48
|
instruction
| 0
| 1,600
| 3
| 3,200
|
"Correct Solution:
```
while 1:
n=int(input())
if n==0:break
ans=[0]*(n+1)**2
for i in range(0,n):
a,b,c,d,e,f,g,h=map(int,input().split())
print(a*e-b*f-c*g-d*h,a*f+b*e+c*h-d*g,a*g-b*h+c*e+d*f,a*h+b*g-c*f+d*e)
```
|
output
| 1
| 1,600
| 3
| 3,201
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as:
$ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $
On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows.
<image>
This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $.
The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows:
$ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $
$ 7 + 6i + 7j + 8k $
$ + 14i + 12i ^ 2 + 14ij + 16ik $
$ + 21j + 18ji + 21j ^ 2 + 24jk $
$ + 28k + 24ki + 28kj + 32k ^ 2 $
By applying the table above
$ = -58 + 16i + 36j + 32k $
It will be.
Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $.
input
Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
$ n $
$ data_1 $
$ data_2 $
::
$ data_n $
The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format:
$ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $
All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50.
output
Prints the product of a given set of quaternions for each dataset.
Example
Input
2
1 2 3 4 7 6 7 8
5 6 7 8 3 2 3 4
0
Output
-58 16 36 32
-50 32 28 48
Submitted Solution:
```
# coding: utf-8
# Your code here!
while True:
N = int(input())
if N == 0:
break
for i in range(N):
x1,y1,z1,w1,x2,y2,z2,w2 = [int(j) for j in input().split()]
a1 = x1*x2 - y1*y2 - z1*z2 - w1*w2
a2 = x1*y2 + y1*x2 + z1*w2 - w1*z2
a3 = x1*z2 - y1*w2 + z1*x2 + w1*y2
a4 = x1*w2 + y1*z2 - z1*y2 + w1*x2
print(a1,a2,a3,a4)
```
|
instruction
| 0
| 1,601
| 3
| 3,202
|
Yes
|
output
| 1
| 1,601
| 3
| 3,203
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as:
$ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $
On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows.
<image>
This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $.
The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows:
$ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $
$ 7 + 6i + 7j + 8k $
$ + 14i + 12i ^ 2 + 14ij + 16ik $
$ + 21j + 18ji + 21j ^ 2 + 24jk $
$ + 28k + 24ki + 28kj + 32k ^ 2 $
By applying the table above
$ = -58 + 16i + 36j + 32k $
It will be.
Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $.
input
Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
$ n $
$ data_1 $
$ data_2 $
::
$ data_n $
The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format:
$ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $
All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50.
output
Prints the product of a given set of quaternions for each dataset.
Example
Input
2
1 2 3 4 7 6 7 8
5 6 7 8 3 2 3 4
0
Output
-58 16 36 32
-50 32 28 48
Submitted Solution:
```
while True:
n = int(input())
if n == 0:
break
for _ in range(n):
x1, y1, z1, w1, x2, y2, z2, w2 = map(int, input().split())
x3 = x1 * x2 - y1 * y2 - z1 * z2 - w1 * w2
y3 = x1 * y2 + y1 * x2 + z1 * w2 - w1 * z2
z3 = x1 * z2 - y1 * w2 + z1 * x2 + w1 * y2
w3 = x1 * w2 + y1 * z2 - z1 * y2 + w1 * x2
print(x3, y3, z3, w3)
```
|
instruction
| 0
| 1,602
| 3
| 3,204
|
Yes
|
output
| 1
| 1,602
| 3
| 3,205
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as:
$ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $
On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows.
<image>
This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $.
The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows:
$ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $
$ 7 + 6i + 7j + 8k $
$ + 14i + 12i ^ 2 + 14ij + 16ik $
$ + 21j + 18ji + 21j ^ 2 + 24jk $
$ + 28k + 24ki + 28kj + 32k ^ 2 $
By applying the table above
$ = -58 + 16i + 36j + 32k $
It will be.
Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $.
input
Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
$ n $
$ data_1 $
$ data_2 $
::
$ data_n $
The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format:
$ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $
All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50.
output
Prints the product of a given set of quaternions for each dataset.
Example
Input
2
1 2 3 4 7 6 7 8
5 6 7 8 3 2 3 4
0
Output
-58 16 36 32
-50 32 28 48
Submitted Solution:
```
while 1:
n = int(input())
if n == 0:break
mark = (1,1,1,1,
1,-1,1,-1,
1,-1,-1,1,
1,1,-1,-1)
k = (0,1,2,3,
1,0,3,2,
2,3,0,1,
3,2,1,0)
for i in range(n):
result = [0 for i in range(4)]
inp = list(map(int,input().split()))
for x_i,x in enumerate(inp[:4]):
for y_i ,y in enumerate(inp[4:]):
idx = x_i*4 + y_i
#print(mark[k[idx]])# * (x*y))
result[k[idx]] += (mark[idx] * (x*y))
print(' '.join(map(str,result)))
```
|
instruction
| 0
| 1,603
| 3
| 3,206
|
Yes
|
output
| 1
| 1,603
| 3
| 3,207
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as:
$ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $
On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows.
<image>
This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $.
The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows:
$ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $
$ 7 + 6i + 7j + 8k $
$ + 14i + 12i ^ 2 + 14ij + 16ik $
$ + 21j + 18ji + 21j ^ 2 + 24jk $
$ + 28k + 24ki + 28kj + 32k ^ 2 $
By applying the table above
$ = -58 + 16i + 36j + 32k $
It will be.
Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $.
input
Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
$ n $
$ data_1 $
$ data_2 $
::
$ data_n $
The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format:
$ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $
All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50.
output
Prints the product of a given set of quaternions for each dataset.
Example
Input
2
1 2 3 4 7 6 7 8
5 6 7 8 3 2 3 4
0
Output
-58 16 36 32
-50 32 28 48
Submitted Solution:
```
# Aizu Problem 0241: Quaternion Multiplication
import sys, math, os, bisect
# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
sys.stdin = open("sample-input.txt", "rt")
while True:
n = int(input())
if n == 0:
break
for k in range(n):
a1, a2, a3, a4, b1, b2, b3, b4 = [int(__) for __ in input().split()]
p1 = a1 * b1 - a2 * b2 - a3 * b3 - a4 * b4
p2 = a1 * b2 + a2 * b1 - a4 * b3 + a3 * b4
p3 = a1 * b3 - a2 * b4 + a3 * b1 + a4 * b2
p4 = a1 * b4 + a2 * b3 - a3 * b2 + a4 * b1
print(p1, p2, p3, p4)
```
|
instruction
| 0
| 1,604
| 3
| 3,208
|
Yes
|
output
| 1
| 1,604
| 3
| 3,209
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as:
$ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $
On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows.
<image>
This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $.
The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows:
$ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $
$ 7 + 6i + 7j + 8k $
$ + 14i + 12i ^ 2 + 14ij + 16ik $
$ + 21j + 18ji + 21j ^ 2 + 24jk $
$ + 28k + 24ki + 28kj + 32k ^ 2 $
By applying the table above
$ = -58 + 16i + 36j + 32k $
It will be.
Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $.
input
Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
$ n $
$ data_1 $
$ data_2 $
::
$ data_n $
The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format:
$ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $
All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50.
output
Prints the product of a given set of quaternions for each dataset.
Example
Input
2
1 2 3 4 7 6 7 8
5 6 7 8 3 2 3 4
0
Output
-58 16 36 32
-50 32 28 48
Submitted Solution:
```
for _ in range(int(input())):
x = tuple(map(int, input().split()))
x1, x2 = x[:4], x[4:]
a = [0,0,0,0]
b = [[1,2,3,4],[2,-1,4,-3],[3,-4,-1,2],[4,3,-2,-1]]
for i in range(4):
for j in range(4):
if b[i][j] > 0:a[b[i][j] - 1] += x1[i] * x2[j]
else:a[-b[i][j] - 1] -= x1[i] * x2[j]
print(*a)
```
|
instruction
| 0
| 1,605
| 3
| 3,210
|
No
|
output
| 1
| 1,605
| 3
| 3,211
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as:
$ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $
On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows.
<image>
This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $.
The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows:
$ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $
$ 7 + 6i + 7j + 8k $
$ + 14i + 12i ^ 2 + 14ij + 16ik $
$ + 21j + 18ji + 21j ^ 2 + 24jk $
$ + 28k + 24ki + 28kj + 32k ^ 2 $
By applying the table above
$ = -58 + 16i + 36j + 32k $
It will be.
Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $.
input
Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
$ n $
$ data_1 $
$ data_2 $
::
$ data_n $
The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format:
$ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $
All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50.
output
Prints the product of a given set of quaternions for each dataset.
Example
Input
2
1 2 3 4 7 6 7 8
5 6 7 8 3 2 3 4
0
Output
-58 16 36 32
-50 32 28 48
Submitted Solution:
```
import sys
f = sys.stdin
index = [[0,1,2,3],
[1,0,3,2],
[2,3,0,1],
[3,2,1,0]]
sign = [[1, 1, 1, 1],
[1,-1, 1,-1],
[1,-1,-1, 1],
[1, 1,-1,-1]]
n = int(f.readline())
for _ in range(n):
ab = list(map(int, f.readline().split()))
ret = [0] * 4
for i, a in enumerate(ab[:4]):
for j, b in enumerate(ab[4:]):
ret[index[i][j]] += sign[i][j] * a * b
print(*ret)
```
|
instruction
| 0
| 1,606
| 3
| 3,212
|
No
|
output
| 1
| 1,606
| 3
| 3,213
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as:
$ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $
On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows.
<image>
This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $.
The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows:
$ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $
$ 7 + 6i + 7j + 8k $
$ + 14i + 12i ^ 2 + 14ij + 16ik $
$ + 21j + 18ji + 21j ^ 2 + 24jk $
$ + 28k + 24ki + 28kj + 32k ^ 2 $
By applying the table above
$ = -58 + 16i + 36j + 32k $
It will be.
Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $.
input
Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
$ n $
$ data_1 $
$ data_2 $
::
$ data_n $
The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format:
$ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $
All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50.
output
Prints the product of a given set of quaternions for each dataset.
Example
Input
2
1 2 3 4 7 6 7 8
5 6 7 8 3 2 3 4
0
Output
-58 16 36 32
-50 32 28 48
Submitted Solution:
```
# Aizu Problem 0241: Quaternion Multiplication
import sys, math, os, bisect
# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
sys.stdin = open("sample-input.txt", "rt")
n = int(input())
for k in range(n):
a1, a2, a3, a4, b1, b2, b3, b4 = [int(__) for __ in input().split()]
p1 = a1 * b1 - a2 * b2 - a3 * b3 - a4 * b4
p2 = a1 * b2 + a2 * b1 - a4 * b3 + a3 * b4
p3 = a1 * b3 - a2 * b4 + a3 * b1 + a4 * b2
p4 = a1 * b4 + a2 * b3 - a3 * b2 + a4 * b1
print(p1, p2, p3, p4)
```
|
instruction
| 0
| 1,607
| 3
| 3,214
|
No
|
output
| 1
| 1,607
| 3
| 3,215
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An extension of a complex number is called a quaternion. It is a convenient number that can be used to control the arm of a robot because it is convenient for expressing the rotation of an object. Quaternions are $ using four real numbers $ x $, $ y $, $ z $, $ w $ and special numbers (extended imaginary numbers) $ i $, $ j $, $ k $. It is expressed as x + yi + zj + wk $. The sum of such quaternions is defined as:
$ (x_1 + y_1 i + z_1 j + w_1 k) + (x_2 + y_2 i + z_2 j + w_2 k) = (x_1 + x_2) + (y_1 + y_2) i + (z_1 + z_2) j + (w_1 + w_2) k $
On the other hand, the product between 1, $ i $, $ j $, and $ k $ is given as follows.
<image>
This table represents the product $ AB $ of two special numbers $ A $ and $ B $. For example, the product $ ij $ of $ i $ and $ j $ is $ k $, and the product $ ji $ of $ j $ and $ i $ is $ -k $.
The product of common quaternions is calculated to satisfy this relationship. For example, the product of two quaternions, $ 1 + 2i + 3j + 4k $ and $ 7 + 6i + 7j + 8k $, is calculated as follows:
$ (1 + 2i + 3j + 4k) \ times (7 + 6i + 7j + 8k) = $
$ 7 + 6i + 7j + 8k $
$ + 14i + 12i ^ 2 + 14ij + 16ik $
$ + 21j + 18ji + 21j ^ 2 + 24jk $
$ + 28k + 24ki + 28kj + 32k ^ 2 $
By applying the table above
$ = -58 + 16i + 36j + 32k $
It will be.
Two quaternions ($ x_1 + y_1 i + z_1 j + w_1 k $) and ($) where the four coefficients $ x $, $ y $, $ z $, $ w $ are integers and not all zeros x_2 + y_2 i + z_2 j + w_2 k $), and the product is ($ x_3 + y_3 i + z_3 j + w_3 k $), $ x_3 $, $ y_3 $, $ z_3 $, $ Create a program that outputs w_3 $.
input
Given multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
$ n $
$ data_1 $
$ data_2 $
::
$ data_n $
The first line gives the number of pairs of quaternions to process $ n $ ($ n \ leq 10 $). The following $ n $ line is given the $ i $ th quaternion pair of information $ data_i $ in the following format:
$ x_1 $ $ y_1 $ $ z_1 $ $ w_1 $ $ x_2 $ $ y_2 $ $ z_2 $ $ w_2 $
All coefficients given should be -1000 or more and 1000 or less. The number of datasets does not exceed 50.
output
Prints the product of a given set of quaternions for each dataset.
Example
Input
2
1 2 3 4 7 6 7 8
5 6 7 8 3 2 3 4
0
Output
-58 16 36 32
-50 32 28 48
Submitted Solution:
```
b = [[1,2,3,4],[2,-1,4,-3],[3,-4,-1,2],[4,3,-2,-1]]
for _ in range(int(input())):
x = tuple(map(int, input().split()))
x1, x2 = x[:4], x[4:]
a = [0,0,0,0]
for i in range(4):
for j in range(4):
if b[i][j] > 0:a[b[i][j] - 1] += x1[i] * x2[j]
else:a[-b[i][j] - 1] -= x1[i] * x2[j]
print(*a)
```
|
instruction
| 0
| 1,608
| 3
| 3,216
|
No
|
output
| 1
| 1,608
| 3
| 3,217
|
Provide tags and a correct Python 3 solution for this coding contest problem.
This is an interactive problem.
Natasha is going to fly to Mars. Finally, Natasha sat in the rocket. She flies, flies... but gets bored. She wishes to arrive to Mars already! So she decides to find something to occupy herself. She couldn't think of anything better to do than to calculate the distance to the red planet.
Let's define x as the distance to Mars. Unfortunately, Natasha does not know x. But it is known that 1 ≤ x ≤ m, where Natasha knows the number m. Besides, x and m are positive integers.
Natasha can ask the rocket questions. Every question is an integer y (1 ≤ y ≤ m). The correct answer to the question is -1, if x<y, 0, if x=y, and 1, if x>y. But the rocket is broken — it does not always answer correctly. Precisely: let the correct answer to the current question be equal to t, then, if the rocket answers this question correctly, then it will answer t, otherwise it will answer -t.
In addition, the rocket has a sequence p of length n. Each element of the sequence is either 0 or 1. The rocket processes this sequence in the cyclic order, that is 1-st element, 2-nd, 3-rd, …, (n-1)-th, n-th, 1-st, 2-nd, 3-rd, …, (n-1)-th, n-th, …. If the current element is 1, the rocket answers correctly, if 0 — lies. Natasha doesn't know the sequence p, but she knows its length — n.
You can ask the rocket no more than 60 questions.
Help Natasha find the distance to Mars. Assume, that the distance to Mars does not change while Natasha is asking questions.
Your solution will not be accepted, if it does not receive an answer 0 from the rocket (even if the distance to Mars is uniquely determined by the already received rocket's answers).
Input
The first line contains two integers m and n (1 ≤ m ≤ 10^9, 1 ≤ n ≤ 30) — the maximum distance to Mars and the number of elements in the sequence p.
Interaction
You can ask the rocket no more than 60 questions.
To ask a question, print a number y (1≤ y≤ m) and an end-of-line character, then do the operation flush and read the answer to the question.
If the program reads 0, then the distance is correct and you must immediately terminate the program (for example, by calling exit(0)). If you ignore this, you can get any verdict, since your program will continue to read from the closed input stream.
If at some point your program reads -2 as an answer, it must immediately end (for example, by calling exit(0)). You will receive the "Wrong answer" verdict, and this will mean that the request is incorrect or the number of requests exceeds 60. If you ignore this, you can get any verdict, since your program will continue to read from the closed input stream.
If your program's request is not a valid integer between -2^{31} and 2^{31}-1 (inclusive) without leading zeros, then you can get any verdict.
You can get "Idleness limit exceeded" if you don't print anything or if you forget to flush the output.
To flush the output buffer you can use (after printing a query and end-of-line):
* fflush(stdout) in C++;
* System.out.flush() in Java;
* stdout.flush() in Python;
* flush(output) in Pascal;
* See the documentation for other languages.
Hacking
Use the following format for hacking:
In the first line, print 3 integers m,n,x (1≤ x≤ m≤ 10^9, 1≤ n≤ 30) — the maximum distance to Mars, the number of elements in the sequence p and the current distance to Mars.
In the second line, enter n numbers, each of which is equal to 0 or 1 — sequence p.
The hacked solution will not have access to the number x and sequence p.
Example
Input
5 2
1
-1
-1
1
0
Output
1
2
4
5
3
Note
In the example, hacking would look like this:
5 2 3
1 0
This means that the current distance to Mars is equal to 3, Natasha knows that it does not exceed 5, and the rocket answers in order: correctly, incorrectly, correctly, incorrectly ...
Really:
on the first query (1) the correct answer is 1, the rocket answered correctly: 1;
on the second query (2) the correct answer is 1, the rocket answered incorrectly: -1;
on the third query (4) the correct answer is -1, the rocket answered correctly: -1;
on the fourth query (5) the correct answer is -1, the rocket answered incorrectly: 1;
on the fifth query (3) the correct and incorrect answer is 0.
|
instruction
| 0
| 1,651
| 3
| 3,302
|
Tags: binary search, interactive
Correct Solution:
```
from sys import stdin
from math import *
from sys import stdout
line = stdin.readline().rstrip().split()
m = int(line[0])
n = int(line[1])
bits = []
for i in range(n):
print(m)
stdout.flush()
line = stdin.readline().rstrip().split()
res = int(line[0])
if res == 0:
exit(0)
if res == 1:
bits.append(-1)
else:
bits.append(1)
minN = 1
maxN = m
i = 0
while True:
current = int((minN + maxN) / 2)
print(current)
stdout.flush()
line = stdin.readline().rstrip().split()
res = int(line[0])
if res == 0:
exit(0)
res *= bits[i]
i = (i+1) % n
if res == 1:
minN = current + 1
else:
maxN = current - 1
```
|
output
| 1
| 1,651
| 3
| 3,303
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem.
Natasha is going to fly to Mars. Finally, Natasha sat in the rocket. She flies, flies... but gets bored. She wishes to arrive to Mars already! So she decides to find something to occupy herself. She couldn't think of anything better to do than to calculate the distance to the red planet.
Let's define x as the distance to Mars. Unfortunately, Natasha does not know x. But it is known that 1 ≤ x ≤ m, where Natasha knows the number m. Besides, x and m are positive integers.
Natasha can ask the rocket questions. Every question is an integer y (1 ≤ y ≤ m). The correct answer to the question is -1, if x<y, 0, if x=y, and 1, if x>y. But the rocket is broken — it does not always answer correctly. Precisely: let the correct answer to the current question be equal to t, then, if the rocket answers this question correctly, then it will answer t, otherwise it will answer -t.
In addition, the rocket has a sequence p of length n. Each element of the sequence is either 0 or 1. The rocket processes this sequence in the cyclic order, that is 1-st element, 2-nd, 3-rd, …, (n-1)-th, n-th, 1-st, 2-nd, 3-rd, …, (n-1)-th, n-th, …. If the current element is 1, the rocket answers correctly, if 0 — lies. Natasha doesn't know the sequence p, but she knows its length — n.
You can ask the rocket no more than 60 questions.
Help Natasha find the distance to Mars. Assume, that the distance to Mars does not change while Natasha is asking questions.
Your solution will not be accepted, if it does not receive an answer 0 from the rocket (even if the distance to Mars is uniquely determined by the already received rocket's answers).
Input
The first line contains two integers m and n (1 ≤ m ≤ 10^9, 1 ≤ n ≤ 30) — the maximum distance to Mars and the number of elements in the sequence p.
Interaction
You can ask the rocket no more than 60 questions.
To ask a question, print a number y (1≤ y≤ m) and an end-of-line character, then do the operation flush and read the answer to the question.
If the program reads 0, then the distance is correct and you must immediately terminate the program (for example, by calling exit(0)). If you ignore this, you can get any verdict, since your program will continue to read from the closed input stream.
If at some point your program reads -2 as an answer, it must immediately end (for example, by calling exit(0)). You will receive the "Wrong answer" verdict, and this will mean that the request is incorrect or the number of requests exceeds 60. If you ignore this, you can get any verdict, since your program will continue to read from the closed input stream.
If your program's request is not a valid integer between -2^{31} and 2^{31}-1 (inclusive) without leading zeros, then you can get any verdict.
You can get "Idleness limit exceeded" if you don't print anything or if you forget to flush the output.
To flush the output buffer you can use (after printing a query and end-of-line):
* fflush(stdout) in C++;
* System.out.flush() in Java;
* stdout.flush() in Python;
* flush(output) in Pascal;
* See the documentation for other languages.
Hacking
Use the following format for hacking:
In the first line, print 3 integers m,n,x (1≤ x≤ m≤ 10^9, 1≤ n≤ 30) — the maximum distance to Mars, the number of elements in the sequence p and the current distance to Mars.
In the second line, enter n numbers, each of which is equal to 0 or 1 — sequence p.
The hacked solution will not have access to the number x and sequence p.
Example
Input
5 2
1
-1
-1
1
0
Output
1
2
4
5
3
Note
In the example, hacking would look like this:
5 2 3
1 0
This means that the current distance to Mars is equal to 3, Natasha knows that it does not exceed 5, and the rocket answers in order: correctly, incorrectly, correctly, incorrectly ...
Really:
on the first query (1) the correct answer is 1, the rocket answered correctly: 1;
on the second query (2) the correct answer is 1, the rocket answered incorrectly: -1;
on the third query (4) the correct answer is -1, the rocket answered correctly: -1;
on the fourth query (5) the correct answer is -1, the rocket answered incorrectly: 1;
on the fifth query (3) the correct and incorrect answer is 0.
Submitted Solution:
```
import sys
[m,n] = [int(i) for i in input().split()]
seq = []
for i in range(n):
sys.stdout.write(str(m+1)+"\n")
sys.stdout.flush()
ans = int(input())
if ans == -1:
seq.append(1)
if ans == 1:
seq.append(0)
start = 0
end = m
for i in range(60-n):
mid = (start+end)//2
sys.stdout.write(str(mid)+"\n")
sys.stdout.flush()
ans = int(input())
if ans == 0:
sys.exit()
elif ans == 1:
if seq[i%n] == 0:
end -= 1
else:
start += 1
elif ans == -1:
if seq[i%n] == 0:
start += 1
else:
end -= 1
else:
sys.stderr.write("Unknown command detected "+str(ans)+"\n")
sys.stderr.flush()
```
|
instruction
| 0
| 1,652
| 3
| 3,304
|
No
|
output
| 1
| 1,652
| 3
| 3,305
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem.
Natasha is going to fly to Mars. Finally, Natasha sat in the rocket. She flies, flies... but gets bored. She wishes to arrive to Mars already! So she decides to find something to occupy herself. She couldn't think of anything better to do than to calculate the distance to the red planet.
Let's define x as the distance to Mars. Unfortunately, Natasha does not know x. But it is known that 1 ≤ x ≤ m, where Natasha knows the number m. Besides, x and m are positive integers.
Natasha can ask the rocket questions. Every question is an integer y (1 ≤ y ≤ m). The correct answer to the question is -1, if x<y, 0, if x=y, and 1, if x>y. But the rocket is broken — it does not always answer correctly. Precisely: let the correct answer to the current question be equal to t, then, if the rocket answers this question correctly, then it will answer t, otherwise it will answer -t.
In addition, the rocket has a sequence p of length n. Each element of the sequence is either 0 or 1. The rocket processes this sequence in the cyclic order, that is 1-st element, 2-nd, 3-rd, …, (n-1)-th, n-th, 1-st, 2-nd, 3-rd, …, (n-1)-th, n-th, …. If the current element is 1, the rocket answers correctly, if 0 — lies. Natasha doesn't know the sequence p, but she knows its length — n.
You can ask the rocket no more than 60 questions.
Help Natasha find the distance to Mars. Assume, that the distance to Mars does not change while Natasha is asking questions.
Your solution will not be accepted, if it does not receive an answer 0 from the rocket (even if the distance to Mars is uniquely determined by the already received rocket's answers).
Input
The first line contains two integers m and n (1 ≤ m ≤ 10^9, 1 ≤ n ≤ 30) — the maximum distance to Mars and the number of elements in the sequence p.
Interaction
You can ask the rocket no more than 60 questions.
To ask a question, print a number y (1≤ y≤ m) and an end-of-line character, then do the operation flush and read the answer to the question.
If the program reads 0, then the distance is correct and you must immediately terminate the program (for example, by calling exit(0)). If you ignore this, you can get any verdict, since your program will continue to read from the closed input stream.
If at some point your program reads -2 as an answer, it must immediately end (for example, by calling exit(0)). You will receive the "Wrong answer" verdict, and this will mean that the request is incorrect or the number of requests exceeds 60. If you ignore this, you can get any verdict, since your program will continue to read from the closed input stream.
If your program's request is not a valid integer between -2^{31} and 2^{31}-1 (inclusive) without leading zeros, then you can get any verdict.
You can get "Idleness limit exceeded" if you don't print anything or if you forget to flush the output.
To flush the output buffer you can use (after printing a query and end-of-line):
* fflush(stdout) in C++;
* System.out.flush() in Java;
* stdout.flush() in Python;
* flush(output) in Pascal;
* See the documentation for other languages.
Hacking
Use the following format for hacking:
In the first line, print 3 integers m,n,x (1≤ x≤ m≤ 10^9, 1≤ n≤ 30) — the maximum distance to Mars, the number of elements in the sequence p and the current distance to Mars.
In the second line, enter n numbers, each of which is equal to 0 or 1 — sequence p.
The hacked solution will not have access to the number x and sequence p.
Example
Input
5 2
1
-1
-1
1
0
Output
1
2
4
5
3
Note
In the example, hacking would look like this:
5 2 3
1 0
This means that the current distance to Mars is equal to 3, Natasha knows that it does not exceed 5, and the rocket answers in order: correctly, incorrectly, correctly, incorrectly ...
Really:
on the first query (1) the correct answer is 1, the rocket answered correctly: 1;
on the second query (2) the correct answer is 1, the rocket answered incorrectly: -1;
on the third query (4) the correct answer is -1, the rocket answered correctly: -1;
on the fourth query (5) the correct answer is -1, the rocket answered incorrectly: 1;
on the fifth query (3) the correct and incorrect answer is 0.
Submitted Solution:
```
from sys import stdout
def scan(obj=map, type=int, sep=' '):
return obj(type, input().split(sep))
def output(text):
print(f'{text}\n')
stdout.flush()
return int(input())
m, n = scan()
p = []
ans = 0
for i in range(n):
text = '1' if n % 2 == 0 else str(m)
ans = output(text)
if ans == 0 or ans == -2:
exit()
if ans == -1:
p.append(-1)
else:
p.append(1)
ans = m // 2 + (1 if m % 2 != 0 else 0)
x = 1
y = m
for i in range(31):
res = output(str(ans)) * p[i % n]
if res == 1:
x = ans
temp = (x + y) // 2
ans = temp + (1 if temp == ans else 0)
elif res == -1:
y = ans
temp = (x + y) // 2
ans = temp - (1 if temp == ans else 0)
elif res == 0 or res == -2:
exit()
```
|
instruction
| 0
| 1,653
| 3
| 3,306
|
No
|
output
| 1
| 1,653
| 3
| 3,307
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem.
Natasha is going to fly to Mars. Finally, Natasha sat in the rocket. She flies, flies... but gets bored. She wishes to arrive to Mars already! So she decides to find something to occupy herself. She couldn't think of anything better to do than to calculate the distance to the red planet.
Let's define x as the distance to Mars. Unfortunately, Natasha does not know x. But it is known that 1 ≤ x ≤ m, where Natasha knows the number m. Besides, x and m are positive integers.
Natasha can ask the rocket questions. Every question is an integer y (1 ≤ y ≤ m). The correct answer to the question is -1, if x<y, 0, if x=y, and 1, if x>y. But the rocket is broken — it does not always answer correctly. Precisely: let the correct answer to the current question be equal to t, then, if the rocket answers this question correctly, then it will answer t, otherwise it will answer -t.
In addition, the rocket has a sequence p of length n. Each element of the sequence is either 0 or 1. The rocket processes this sequence in the cyclic order, that is 1-st element, 2-nd, 3-rd, …, (n-1)-th, n-th, 1-st, 2-nd, 3-rd, …, (n-1)-th, n-th, …. If the current element is 1, the rocket answers correctly, if 0 — lies. Natasha doesn't know the sequence p, but she knows its length — n.
You can ask the rocket no more than 60 questions.
Help Natasha find the distance to Mars. Assume, that the distance to Mars does not change while Natasha is asking questions.
Your solution will not be accepted, if it does not receive an answer 0 from the rocket (even if the distance to Mars is uniquely determined by the already received rocket's answers).
Input
The first line contains two integers m and n (1 ≤ m ≤ 10^9, 1 ≤ n ≤ 30) — the maximum distance to Mars and the number of elements in the sequence p.
Interaction
You can ask the rocket no more than 60 questions.
To ask a question, print a number y (1≤ y≤ m) and an end-of-line character, then do the operation flush and read the answer to the question.
If the program reads 0, then the distance is correct and you must immediately terminate the program (for example, by calling exit(0)). If you ignore this, you can get any verdict, since your program will continue to read from the closed input stream.
If at some point your program reads -2 as an answer, it must immediately end (for example, by calling exit(0)). You will receive the "Wrong answer" verdict, and this will mean that the request is incorrect or the number of requests exceeds 60. If you ignore this, you can get any verdict, since your program will continue to read from the closed input stream.
If your program's request is not a valid integer between -2^{31} and 2^{31}-1 (inclusive) without leading zeros, then you can get any verdict.
You can get "Idleness limit exceeded" if you don't print anything or if you forget to flush the output.
To flush the output buffer you can use (after printing a query and end-of-line):
* fflush(stdout) in C++;
* System.out.flush() in Java;
* stdout.flush() in Python;
* flush(output) in Pascal;
* See the documentation for other languages.
Hacking
Use the following format for hacking:
In the first line, print 3 integers m,n,x (1≤ x≤ m≤ 10^9, 1≤ n≤ 30) — the maximum distance to Mars, the number of elements in the sequence p and the current distance to Mars.
In the second line, enter n numbers, each of which is equal to 0 or 1 — sequence p.
The hacked solution will not have access to the number x and sequence p.
Example
Input
5 2
1
-1
-1
1
0
Output
1
2
4
5
3
Note
In the example, hacking would look like this:
5 2 3
1 0
This means that the current distance to Mars is equal to 3, Natasha knows that it does not exceed 5, and the rocket answers in order: correctly, incorrectly, correctly, incorrectly ...
Really:
on the first query (1) the correct answer is 1, the rocket answered correctly: 1;
on the second query (2) the correct answer is 1, the rocket answered incorrectly: -1;
on the third query (4) the correct answer is -1, the rocket answered correctly: -1;
on the fourth query (5) the correct answer is -1, the rocket answered incorrectly: 1;
on the fifth query (3) the correct and incorrect answer is 0.
Submitted Solution:
```
from sys import stdout
def solve(m, n):
answers = []
for i in range(n):
print(1)
stdout.flush()
answer = int(input().strip())
if answer == 0:
return
answers.append(-1*answer)
l = 1
r = m
for i in range(30):
mid = (l+r) // 2
print(mid)
stdout.flush()
answer = int(input().strip())
if answer == 0:
return
answer *= answers[(i%n)]
if answer == 1:
r = m-1
else:
l = m+1
m, n = [int(x) for x in input().strip().split()]
solve(m, n)
```
|
instruction
| 0
| 1,654
| 3
| 3,308
|
No
|
output
| 1
| 1,654
| 3
| 3,309
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is an interactive problem.
Natasha is going to fly to Mars. Finally, Natasha sat in the rocket. She flies, flies... but gets bored. She wishes to arrive to Mars already! So she decides to find something to occupy herself. She couldn't think of anything better to do than to calculate the distance to the red planet.
Let's define x as the distance to Mars. Unfortunately, Natasha does not know x. But it is known that 1 ≤ x ≤ m, where Natasha knows the number m. Besides, x and m are positive integers.
Natasha can ask the rocket questions. Every question is an integer y (1 ≤ y ≤ m). The correct answer to the question is -1, if x<y, 0, if x=y, and 1, if x>y. But the rocket is broken — it does not always answer correctly. Precisely: let the correct answer to the current question be equal to t, then, if the rocket answers this question correctly, then it will answer t, otherwise it will answer -t.
In addition, the rocket has a sequence p of length n. Each element of the sequence is either 0 or 1. The rocket processes this sequence in the cyclic order, that is 1-st element, 2-nd, 3-rd, …, (n-1)-th, n-th, 1-st, 2-nd, 3-rd, …, (n-1)-th, n-th, …. If the current element is 1, the rocket answers correctly, if 0 — lies. Natasha doesn't know the sequence p, but she knows its length — n.
You can ask the rocket no more than 60 questions.
Help Natasha find the distance to Mars. Assume, that the distance to Mars does not change while Natasha is asking questions.
Your solution will not be accepted, if it does not receive an answer 0 from the rocket (even if the distance to Mars is uniquely determined by the already received rocket's answers).
Input
The first line contains two integers m and n (1 ≤ m ≤ 10^9, 1 ≤ n ≤ 30) — the maximum distance to Mars and the number of elements in the sequence p.
Interaction
You can ask the rocket no more than 60 questions.
To ask a question, print a number y (1≤ y≤ m) and an end-of-line character, then do the operation flush and read the answer to the question.
If the program reads 0, then the distance is correct and you must immediately terminate the program (for example, by calling exit(0)). If you ignore this, you can get any verdict, since your program will continue to read from the closed input stream.
If at some point your program reads -2 as an answer, it must immediately end (for example, by calling exit(0)). You will receive the "Wrong answer" verdict, and this will mean that the request is incorrect or the number of requests exceeds 60. If you ignore this, you can get any verdict, since your program will continue to read from the closed input stream.
If your program's request is not a valid integer between -2^{31} and 2^{31}-1 (inclusive) without leading zeros, then you can get any verdict.
You can get "Idleness limit exceeded" if you don't print anything or if you forget to flush the output.
To flush the output buffer you can use (after printing a query and end-of-line):
* fflush(stdout) in C++;
* System.out.flush() in Java;
* stdout.flush() in Python;
* flush(output) in Pascal;
* See the documentation for other languages.
Hacking
Use the following format for hacking:
In the first line, print 3 integers m,n,x (1≤ x≤ m≤ 10^9, 1≤ n≤ 30) — the maximum distance to Mars, the number of elements in the sequence p and the current distance to Mars.
In the second line, enter n numbers, each of which is equal to 0 or 1 — sequence p.
The hacked solution will not have access to the number x and sequence p.
Example
Input
5 2
1
-1
-1
1
0
Output
1
2
4
5
3
Note
In the example, hacking would look like this:
5 2 3
1 0
This means that the current distance to Mars is equal to 3, Natasha knows that it does not exceed 5, and the rocket answers in order: correctly, incorrectly, correctly, incorrectly ...
Really:
on the first query (1) the correct answer is 1, the rocket answered correctly: 1;
on the second query (2) the correct answer is 1, the rocket answered incorrectly: -1;
on the third query (4) the correct answer is -1, the rocket answered correctly: -1;
on the fourth query (5) the correct answer is -1, the rocket answered incorrectly: 1;
on the fifth query (3) the correct and incorrect answer is 0.
Submitted Solution:
```
import sys
m,n=map(int,input().split())
p=[]
t=0
for i in range(n):
print(1)
t+=1
sys.stdout.flush()
x=int(input())
if x==0:
sys.exit()
if x==1:
p.append(1)
else:
p.append(-1)
low=1
high=m
y=0
while high-low>1:
mid=(low+high)//2
print(mid)
t+=1
sys.stdout.flush()
x=int(input())*p[y]
y+=1
y=y%n
if x==0:
sys.exit()
if x==-1:
high=mid
else:
low=mid
print(low)
t+=1
sys.stdout.flush()
if x==0:
sys.exit()
print(high)
t+=1
sys.stdout.flush()
if x==0:
sys.exit()
```
|
instruction
| 0
| 1,655
| 3
| 3,310
|
No
|
output
| 1
| 1,655
| 3
| 3,311
|
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a forest that we model as a plane and live n rare animals. Animal number i has its lair in the point (x_{i}, y_{i}). In order to protect them, a decision to build a nature reserve has been made.
The reserve must have a form of a circle containing all lairs. There is also a straight river flowing through the forest. All animals drink from this river, therefore it must have at least one common point with the reserve. On the other hand, ships constantly sail along the river, so the reserve must not have more than one common point with the river.
For convenience, scientists have made a transformation of coordinates so that the river is defined by y = 0. Check whether it is possible to build a reserve, and if possible, find the minimum possible radius of such a reserve.
Input
The first line contains one integer n (1 ≤ n ≤ 10^5) — the number of animals.
Each of the next n lines contains two integers x_{i}, y_{i} (-10^7 ≤ x_{i}, y_{i} ≤ 10^7) — the coordinates of the i-th animal's lair. It is guaranteed that y_{i} ≠ 0. No two lairs coincide.
Output
If the reserve cannot be built, print -1. Otherwise print the minimum radius. Your answer will be accepted if absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1
0 1
Output
0.5
Input
3
0 1
0 2
0 -3
Output
-1
Input
2
0 1
1 1
Output
0.625
Note
In the first sample it is optimal to build the reserve with the radius equal to 0.5 and the center in (0,\ 0.5).
In the second sample it is impossible to build a reserve.
In the third sample it is optimal to build the reserve with the radius equal to 5/8 and the center in (1/2,\ 5/8).
|
instruction
| 0
| 1,673
| 3
| 3,346
|
Tags: binary search, geometry, ternary search
Correct Solution:
```
from sys import exit
from math import sqrt
eps = 1e-9
def mysqrt(x):
if x <= 0:
return 0
return sqrt(x)
def good(R):
global xs
global ys
global n
left = -10**20
right = 10**20
for i in range(n):
# (x - xi)**2 + (y - yi)**2 = R**2
# y = R
xi = xs[i]
yi = abs(ys[i])
D = 2 * R * yi - yi**2
if D + eps < 0:
return False
sD = mysqrt(D)
new_left = xi - sD
new_right = xi + sD
left = max(left, new_left)
right = min(right, new_right)
if left + eps > right:
return False
return True
def solve(lower, high, md):
R = 0
for _ in range(150):
R = md(lower, high)
if good(R):
high = R
else:
lower = R
return R
n = int(input())
xs = [0.0 for _ in range(n)]
ys = [0.0 for _ in range(n)]
for i in range(n):
xs[i], ys[i] = map(float, input().split())
if i > 0:
if ys[i] * ys[0] < 0:
print(-1)
exit()
R = None
if good(1):
R = solve(0, 1, lambda x, y: (x + y) / 2)
else:
R = solve(1, 1e16, lambda x, y: sqrt(x * y))
print('{:.16}'.format(R))
```
|
output
| 1
| 1,673
| 3
| 3,347
|
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a forest that we model as a plane and live n rare animals. Animal number i has its lair in the point (x_{i}, y_{i}). In order to protect them, a decision to build a nature reserve has been made.
The reserve must have a form of a circle containing all lairs. There is also a straight river flowing through the forest. All animals drink from this river, therefore it must have at least one common point with the reserve. On the other hand, ships constantly sail along the river, so the reserve must not have more than one common point with the river.
For convenience, scientists have made a transformation of coordinates so that the river is defined by y = 0. Check whether it is possible to build a reserve, and if possible, find the minimum possible radius of such a reserve.
Input
The first line contains one integer n (1 ≤ n ≤ 10^5) — the number of animals.
Each of the next n lines contains two integers x_{i}, y_{i} (-10^7 ≤ x_{i}, y_{i} ≤ 10^7) — the coordinates of the i-th animal's lair. It is guaranteed that y_{i} ≠ 0. No two lairs coincide.
Output
If the reserve cannot be built, print -1. Otherwise print the minimum radius. Your answer will be accepted if absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1
0 1
Output
0.5
Input
3
0 1
0 2
0 -3
Output
-1
Input
2
0 1
1 1
Output
0.625
Note
In the first sample it is optimal to build the reserve with the radius equal to 0.5 and the center in (0,\ 0.5).
In the second sample it is impossible to build a reserve.
In the third sample it is optimal to build the reserve with the radius equal to 5/8 and the center in (1/2,\ 5/8).
|
instruction
| 0
| 1,674
| 3
| 3,348
|
Tags: binary search, geometry, ternary search
Correct Solution:
```
import math
n = int(input())
points = []
npt = 0
for i in range(n):
point = list(map(int, input().split()))
if point[1] < 0:
npt += 1
point[1] = -point[1]
points.append(point)
if npt < n and npt > 0:
print(-1)
else:
l, r = 0, 1e16
count = 200
while abs(r - l) > 1e-6 and count > 0:
count -= 1
ok = True
mid = (l + r) / 2
a, b = -float('inf'), float('inf')
for i in range(n):
x, y = points[i]
if y > 2 * mid:
l = mid
ok = False
break
delta = math.sqrt(y * (2*mid - y))
# delta = math.sqrt(math.pow(mid, 2) - math.pow(mid-y, 2)) # 数据精度丢失
a, b = max(a, x - delta), min(b, x + delta)
#print("x: {}, y: {}, a: {}, b: {}, delta: {}".format(x, y, a, b, delta))
if not ok:
continue
#print(a, b, l, r, mid)
if a > b:
l = mid
else:
r = mid
print((l + r) / 2)
```
|
output
| 1
| 1,674
| 3
| 3,349
|
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a forest that we model as a plane and live n rare animals. Animal number i has its lair in the point (x_{i}, y_{i}). In order to protect them, a decision to build a nature reserve has been made.
The reserve must have a form of a circle containing all lairs. There is also a straight river flowing through the forest. All animals drink from this river, therefore it must have at least one common point with the reserve. On the other hand, ships constantly sail along the river, so the reserve must not have more than one common point with the river.
For convenience, scientists have made a transformation of coordinates so that the river is defined by y = 0. Check whether it is possible to build a reserve, and if possible, find the minimum possible radius of such a reserve.
Input
The first line contains one integer n (1 ≤ n ≤ 10^5) — the number of animals.
Each of the next n lines contains two integers x_{i}, y_{i} (-10^7 ≤ x_{i}, y_{i} ≤ 10^7) — the coordinates of the i-th animal's lair. It is guaranteed that y_{i} ≠ 0. No two lairs coincide.
Output
If the reserve cannot be built, print -1. Otherwise print the minimum radius. Your answer will be accepted if absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1
0 1
Output
0.5
Input
3
0 1
0 2
0 -3
Output
-1
Input
2
0 1
1 1
Output
0.625
Note
In the first sample it is optimal to build the reserve with the radius equal to 0.5 and the center in (0,\ 0.5).
In the second sample it is impossible to build a reserve.
In the third sample it is optimal to build the reserve with the radius equal to 5/8 and the center in (1/2,\ 5/8).
|
instruction
| 0
| 1,675
| 3
| 3,350
|
Tags: binary search, geometry, ternary search
Correct Solution:
```
def bs(x,y,n,p):
r=0
for i in range(n):
#z= (0.5*((x[i]-p)**2 +(y[i])**2))/y[i]
z=(p-x[i])*(p-x[i]) + 1.0*y[i]*y[i]
z=z/2.0
z=z/y[i]
z=abs(z)
r=max(z,r)
return r
n=int(input())
y=[]
x=[]
k=z=0
for i in range(n):
a,b=[int(i) for i in input().split()]
if b>0:
k=1
if b<0:
z=1
x.append(a)
y.append(b)
if k==1 and z==1:
print(-1)
else:
z=120
l=min(x)
rt=max(x)
while(z):
#p1=(rt+2*l)/3
#p2=(l+2*rt)/3
p1=(2*l+rt)/3
p2=(2*rt+l)/3
if bs(x,y,n,p1)<bs(x,y,n,p2):
#rt=p2
rt=p2
else:
l=p1
z=z-1
print(bs(x,y,n,l))
```
|
output
| 1
| 1,675
| 3
| 3,351
|
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a forest that we model as a plane and live n rare animals. Animal number i has its lair in the point (x_{i}, y_{i}). In order to protect them, a decision to build a nature reserve has been made.
The reserve must have a form of a circle containing all lairs. There is also a straight river flowing through the forest. All animals drink from this river, therefore it must have at least one common point with the reserve. On the other hand, ships constantly sail along the river, so the reserve must not have more than one common point with the river.
For convenience, scientists have made a transformation of coordinates so that the river is defined by y = 0. Check whether it is possible to build a reserve, and if possible, find the minimum possible radius of such a reserve.
Input
The first line contains one integer n (1 ≤ n ≤ 10^5) — the number of animals.
Each of the next n lines contains two integers x_{i}, y_{i} (-10^7 ≤ x_{i}, y_{i} ≤ 10^7) — the coordinates of the i-th animal's lair. It is guaranteed that y_{i} ≠ 0. No two lairs coincide.
Output
If the reserve cannot be built, print -1. Otherwise print the minimum radius. Your answer will be accepted if absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1
0 1
Output
0.5
Input
3
0 1
0 2
0 -3
Output
-1
Input
2
0 1
1 1
Output
0.625
Note
In the first sample it is optimal to build the reserve with the radius equal to 0.5 and the center in (0,\ 0.5).
In the second sample it is impossible to build a reserve.
In the third sample it is optimal to build the reserve with the radius equal to 5/8 and the center in (1/2,\ 5/8).
|
instruction
| 0
| 1,676
| 3
| 3,352
|
Tags: binary search, geometry, ternary search
Correct Solution:
```
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")
def check(m):
r = -1
for j in range(n):
r = max(r, (x[j]-m)**2/(2*y[j])+(y[j]/2))
return r
n=int(input())
x=[]
y=[]
for j in range(n):
a,b=map(int,input().split())
x.append(a)
y.append(b)
p=0
q=0
for j in y:
if j<0:
p=1
else:
q=1
if p==1 and q==1:
print(-1)
else:
if p==1:
for j in range(n):
y[j]=abs(y[j])
l=-10**7
h=10**7
for i in range(200):
m1=(3*l+h)/4
m2=(3*h+l)/4
if check(m1)>check(m2):
l=m1
else:
h=m2
print(check(l))
```
|
output
| 1
| 1,676
| 3
| 3,353
|
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a forest that we model as a plane and live n rare animals. Animal number i has its lair in the point (x_{i}, y_{i}). In order to protect them, a decision to build a nature reserve has been made.
The reserve must have a form of a circle containing all lairs. There is also a straight river flowing through the forest. All animals drink from this river, therefore it must have at least one common point with the reserve. On the other hand, ships constantly sail along the river, so the reserve must not have more than one common point with the river.
For convenience, scientists have made a transformation of coordinates so that the river is defined by y = 0. Check whether it is possible to build a reserve, and if possible, find the minimum possible radius of such a reserve.
Input
The first line contains one integer n (1 ≤ n ≤ 10^5) — the number of animals.
Each of the next n lines contains two integers x_{i}, y_{i} (-10^7 ≤ x_{i}, y_{i} ≤ 10^7) — the coordinates of the i-th animal's lair. It is guaranteed that y_{i} ≠ 0. No two lairs coincide.
Output
If the reserve cannot be built, print -1. Otherwise print the minimum radius. Your answer will be accepted if absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1
0 1
Output
0.5
Input
3
0 1
0 2
0 -3
Output
-1
Input
2
0 1
1 1
Output
0.625
Note
In the first sample it is optimal to build the reserve with the radius equal to 0.5 and the center in (0,\ 0.5).
In the second sample it is impossible to build a reserve.
In the third sample it is optimal to build the reserve with the radius equal to 5/8 and the center in (1/2,\ 5/8).
|
instruction
| 0
| 1,677
| 3
| 3,354
|
Tags: binary search, geometry, ternary search
Correct Solution:
```
def check(mid):
mx=0
for i in range(n):
mx=max(mx,(x[i]-mid)**2/(2*y[i])+(y[i]/2))
return mx
n=int(input())
l=-100000000
r= 100000000
x=[]
y=[]
c1,c2=0,0
for i in range(n):
a,b=map(int,input().split())
if b>=0:
c1+=1
else:
c2+=1
x.append(a)
y.append(abs(b))
if c1 and c2:
print(-1)
exit()
for i in range(100):
m1=l+(r-l)/3
m2=r-(r-l)/3
if check(m1)>check(m2):
l=m1
else:
r=m2
print(check(l))
```
|
output
| 1
| 1,677
| 3
| 3,355
|
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a forest that we model as a plane and live n rare animals. Animal number i has its lair in the point (x_{i}, y_{i}). In order to protect them, a decision to build a nature reserve has been made.
The reserve must have a form of a circle containing all lairs. There is also a straight river flowing through the forest. All animals drink from this river, therefore it must have at least one common point with the reserve. On the other hand, ships constantly sail along the river, so the reserve must not have more than one common point with the river.
For convenience, scientists have made a transformation of coordinates so that the river is defined by y = 0. Check whether it is possible to build a reserve, and if possible, find the minimum possible radius of such a reserve.
Input
The first line contains one integer n (1 ≤ n ≤ 10^5) — the number of animals.
Each of the next n lines contains two integers x_{i}, y_{i} (-10^7 ≤ x_{i}, y_{i} ≤ 10^7) — the coordinates of the i-th animal's lair. It is guaranteed that y_{i} ≠ 0. No two lairs coincide.
Output
If the reserve cannot be built, print -1. Otherwise print the minimum radius. Your answer will be accepted if absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
1
0 1
Output
0.5
Input
3
0 1
0 2
0 -3
Output
-1
Input
2
0 1
1 1
Output
0.625
Note
In the first sample it is optimal to build the reserve with the radius equal to 0.5 and the center in (0,\ 0.5).
In the second sample it is impossible to build a reserve.
In the third sample it is optimal to build the reserve with the radius equal to 5/8 and the center in (1/2,\ 5/8).
|
instruction
| 0
| 1,678
| 3
| 3,356
|
Tags: binary search, geometry, ternary search
Correct Solution:
```
import math
n = int(input())
points = []
npt = 0
for i in range(n):
point = list(map(int, input().split()))
if point[1] < 0:
npt += 1
point[1] = -point[1]
points.append(point)
if npt < n and npt > 0:
print(-1)
else:
l, r = 0, 1e16
count = 100
while abs(r - l) > 1e-6 and count > 0:
count -= 1
ok = True
mid = (l + r) / 2
a, b = -float('inf'), float('inf')
for i in range(n):
x, y = points[i]
if y > 2 * mid:
l = mid
ok = False
break
delta = math.sqrt(y * (2*mid - y))
# delta = math.sqrt(math.pow(mid, 2) - math.pow(mid-y, 2)) # 数据精度丢失
a, b = max(a, x - delta), min(b, x + delta)
#print("x: {}, y: {}, a: {}, b: {}, delta: {}".format(x, y, a, b, delta))
if not ok:
continue
#print(a, b, l, r, mid)
if a > b:
l = mid
else:
r = mid
print((l + r) / 2)
```
|
output
| 1
| 1,678
| 3
| 3,357
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.