message stringlengths 2 44.5k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 276 109k | cluster float64 23 23 | __index_level_0__ int64 552 217k |
|---|---|---|---|---|---|
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with R rows and C columns. We call the cell in the r-th row and c-th column (r,c).
Mr. Takahashi wrote non-negative integers into N of the cells, that is, he wrote a non-negative integer a_i into (r_i,c_i) for each i (1≤i≤N). After that he fell asleep.
Mr. Aoki found the grid and tries to surprise Mr. Takahashi by writing integers into all remaining cells. The grid must meet the following conditions to really surprise Mr. Takahashi.
* Condition 1: Each cell contains a non-negative integer.
* Condition 2: For any 2×2 square formed by cells on the grid, the sum of the top left and bottom right integers must always equal to the sum of the top right and bottom left integers.
Determine whether it is possible to meet those conditions by properly writing integers into all remaining cells.
Constraints
* 2≤R,C≤10^5
* 1≤N≤10^5
* 1≤r_i≤R
* 1≤c_i≤C
* (r_i,c_i) ≠ (r_j,c_j) (i≠j)
* a_i is an integer.
* 0≤a_i≤10^9
Input
The input is given from Standard Input in the following format:
R C
N
r_1 c_1 a_1
r_2 c_2 a_2
:
r_N c_N a_N
Output
Print `Yes` if it is possible to meet the conditions by properly writing integers into all remaining cells. Otherwise, print `No`.
Examples
Input
2 2
3
1 1 0
1 2 10
2 1 20
Output
Yes
Input
2 3
5
1 1 0
1 2 10
1 3 20
2 1 30
2 3 40
Output
No
Input
2 2
3
1 1 20
1 2 10
2 1 0
Output
No
Input
3 3
4
1 1 0
1 3 10
3 1 10
3 3 20
Output
Yes
Input
2 2
4
1 1 0
1 2 10
2 1 30
2 2 20
Output
No | instruction | 0 | 90,831 | 23 | 181,662 |
"Correct Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# D-Grid and Integers
from collections import defaultdict
import sys
sys.setrecursionlimit(10**6)
def ReadInput():
return [int(i) for i in input().split(" ")]
(R, C) = ReadInput()
N = int(input())
VectorSet = set()
Grid = defaultdict(list)
for i in range(N):
(r, c, a) = ReadInput()
Grid[("R", r)].append((("C", c), a))
Grid[("C", c)].append((("R", r), a))
VectorSet.add(("R", r))
VectorSet.add(("C", c))
def iteration(vector):
VectorSet.discard(vector)
for (v, a) in Grid[vector]:
temp = a - VectorCover[vector]
if (v in VectorCover):
if(temp != VectorCover[v]):
print("No")
exit()
else:
VectorCover[v] = temp
iteration(v)
while(len(VectorSet) != 0):
vector = VectorSet.pop()
VectorCover = dict()
VectorCover[vector] = 0
iteration(vector)
minR = min(a for (p, a) in VectorCover.items() if (p[0] == "R"))
minC = min(a for (p, a) in VectorCover.items() if (p[0] == "C"))
if (minR + minC < 0):
print("No")
exit()
print("Yes")
``` | output | 1 | 90,831 | 23 | 181,663 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with R rows and C columns. We call the cell in the r-th row and c-th column (r,c).
Mr. Takahashi wrote non-negative integers into N of the cells, that is, he wrote a non-negative integer a_i into (r_i,c_i) for each i (1≤i≤N). After that he fell asleep.
Mr. Aoki found the grid and tries to surprise Mr. Takahashi by writing integers into all remaining cells. The grid must meet the following conditions to really surprise Mr. Takahashi.
* Condition 1: Each cell contains a non-negative integer.
* Condition 2: For any 2×2 square formed by cells on the grid, the sum of the top left and bottom right integers must always equal to the sum of the top right and bottom left integers.
Determine whether it is possible to meet those conditions by properly writing integers into all remaining cells.
Constraints
* 2≤R,C≤10^5
* 1≤N≤10^5
* 1≤r_i≤R
* 1≤c_i≤C
* (r_i,c_i) ≠ (r_j,c_j) (i≠j)
* a_i is an integer.
* 0≤a_i≤10^9
Input
The input is given from Standard Input in the following format:
R C
N
r_1 c_1 a_1
r_2 c_2 a_2
:
r_N c_N a_N
Output
Print `Yes` if it is possible to meet the conditions by properly writing integers into all remaining cells. Otherwise, print `No`.
Examples
Input
2 2
3
1 1 0
1 2 10
2 1 20
Output
Yes
Input
2 3
5
1 1 0
1 2 10
1 3 20
2 1 30
2 3 40
Output
No
Input
2 2
3
1 1 20
1 2 10
2 1 0
Output
No
Input
3 3
4
1 1 0
1 3 10
3 1 10
3 3 20
Output
Yes
Input
2 2
4
1 1 0
1 2 10
2 1 30
2 2 20
Output
No | instruction | 0 | 90,832 | 23 | 181,664 |
"Correct Solution:
```
import sys
readline = sys.stdin.readline
class UFP():
def __init__(self, num):
self.par = [-1]*num
self.dist = [0]*num
def find(self, x):
if self.par[x] < 0:
return x
else:
res = 0
xo = x
while self.par[x] >= 0:
res += self.dist[x]
x = self.par[x]
self.dist[xo] = res
self.par[xo] = x
return x
def union(self, x, y, d):
rx = self.find(x)
ry = self.find(y)
if rx != ry:
if self.par[rx] > self.par[ry]:
rx, ry = ry, rx
x, y = y, x
d = -d
self.par[rx] += self.par[ry]
self.par[ry] = rx
self.dist[ry] = d + self.dist[x] - self.dist[y]
return True
else:
if d + self.dist[x] - self.dist[y]:
return False
return True
INF = 3*10**9
def check():
H, W = map(int, readline().split())
N = int(readline())
Hm = [INF]*H
Wm = [INF]*W
HP = [[] for _ in range(H)]
WP = [[] for _ in range(W)]
for _ in range(N):
h, w, a = map(int, readline().split())
h -= 1
w -= 1
Hm[h] = min(Hm[h], a)
Wm[w] = min(Wm[w], a)
HP[h].append((w, a))
WP[w].append((h, a))
Th = UFP(H)
Tw = UFP(W)
for h in range(H):
L = len(HP[h])
if L > 1:
HP[h].sort()
for i in range(L-1):
wp, ap = HP[h][i]
wn, an = HP[h][i+1]
if not Tw.union(wp, wn, an-ap):
return False
for w in range(W):
L = len(WP[w])
if L > 1:
WP[w].sort()
for i in range(L-1):
hp, ap = WP[w][i]
hn, an = WP[w][i+1]
if not Th.union(hp, hn, an-ap):
return False
cmh = [INF]*H
for h in range(H):
rh = Th.find(h)
cmh[rh] = min(cmh[rh], Th.dist[h])
cmw = [INF]*W
for w in range(W):
rw = Tw.find(w)
cmw[rw] = min(cmw[rw], Tw.dist[w])
for h in range(H):
if Hm[h] - Th.dist[h] + cmh[Th.find(h)] < 0:
return False
for w in range(W):
if Wm[w] - Tw.dist[w] + cmw[Tw.find(w)] < 0:
return False
return True
print('Yes' if check() else 'No')
``` | output | 1 | 90,832 | 23 | 181,665 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with R rows and C columns. We call the cell in the r-th row and c-th column (r,c).
Mr. Takahashi wrote non-negative integers into N of the cells, that is, he wrote a non-negative integer a_i into (r_i,c_i) for each i (1≤i≤N). After that he fell asleep.
Mr. Aoki found the grid and tries to surprise Mr. Takahashi by writing integers into all remaining cells. The grid must meet the following conditions to really surprise Mr. Takahashi.
* Condition 1: Each cell contains a non-negative integer.
* Condition 2: For any 2×2 square formed by cells on the grid, the sum of the top left and bottom right integers must always equal to the sum of the top right and bottom left integers.
Determine whether it is possible to meet those conditions by properly writing integers into all remaining cells.
Constraints
* 2≤R,C≤10^5
* 1≤N≤10^5
* 1≤r_i≤R
* 1≤c_i≤C
* (r_i,c_i) ≠ (r_j,c_j) (i≠j)
* a_i is an integer.
* 0≤a_i≤10^9
Input
The input is given from Standard Input in the following format:
R C
N
r_1 c_1 a_1
r_2 c_2 a_2
:
r_N c_N a_N
Output
Print `Yes` if it is possible to meet the conditions by properly writing integers into all remaining cells. Otherwise, print `No`.
Examples
Input
2 2
3
1 1 0
1 2 10
2 1 20
Output
Yes
Input
2 3
5
1 1 0
1 2 10
1 3 20
2 1 30
2 3 40
Output
No
Input
2 2
3
1 1 20
1 2 10
2 1 0
Output
No
Input
3 3
4
1 1 0
1 3 10
3 1 10
3 3 20
Output
Yes
Input
2 2
4
1 1 0
1 2 10
2 1 30
2 2 20
Output
No | instruction | 0 | 90,833 | 23 | 181,666 |
"Correct Solution:
```
from collections import defaultdict
import sys
sys.setrecursionlimit(10**6)
def ReadInput():
return [int(i) for i in input().split(" ")]
(R, C) = ReadInput()
N = int(input())
VectorSet = set()
Grid = defaultdict(list)
for i in range(N):
(r, c, a) = ReadInput()
Grid[("R", r)].append((("C", c), a))
Grid[("C", c)].append((("R", r), a))
VectorSet.add(("R", r))
VectorSet.add(("C", c))
def iteration(vector):
VectorSet.discard(vector)
for (v, a) in Grid[vector]:
temp = a - VectorCover[vector]
if (v in VectorCover):
if(temp != VectorCover[v]):
print("No")
exit()
else:
VectorCover[v] = temp
iteration(v)
while(len(VectorSet) != 0):
vector = VectorSet.pop()
VectorCover = dict()
VectorCover[vector] = 0
iteration(vector)
minR = min(a for (p, a) in VectorCover.items() if (p[0] == "R"))
minC = min(a for (p, a) in VectorCover.items() if (p[0] == "C"))
if (minR + minC < 0):
print("No")
exit()
print("Yes")
``` | output | 1 | 90,833 | 23 | 181,667 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with R rows and C columns. We call the cell in the r-th row and c-th column (r,c).
Mr. Takahashi wrote non-negative integers into N of the cells, that is, he wrote a non-negative integer a_i into (r_i,c_i) for each i (1≤i≤N). After that he fell asleep.
Mr. Aoki found the grid and tries to surprise Mr. Takahashi by writing integers into all remaining cells. The grid must meet the following conditions to really surprise Mr. Takahashi.
* Condition 1: Each cell contains a non-negative integer.
* Condition 2: For any 2×2 square formed by cells on the grid, the sum of the top left and bottom right integers must always equal to the sum of the top right and bottom left integers.
Determine whether it is possible to meet those conditions by properly writing integers into all remaining cells.
Constraints
* 2≤R,C≤10^5
* 1≤N≤10^5
* 1≤r_i≤R
* 1≤c_i≤C
* (r_i,c_i) ≠ (r_j,c_j) (i≠j)
* a_i is an integer.
* 0≤a_i≤10^9
Input
The input is given from Standard Input in the following format:
R C
N
r_1 c_1 a_1
r_2 c_2 a_2
:
r_N c_N a_N
Output
Print `Yes` if it is possible to meet the conditions by properly writing integers into all remaining cells. Otherwise, print `No`.
Examples
Input
2 2
3
1 1 0
1 2 10
2 1 20
Output
Yes
Input
2 3
5
1 1 0
1 2 10
1 3 20
2 1 30
2 3 40
Output
No
Input
2 2
3
1 1 20
1 2 10
2 1 0
Output
No
Input
3 3
4
1 1 0
1 3 10
3 1 10
3 3 20
Output
Yes
Input
2 2
4
1 1 0
1 2 10
2 1 30
2 2 20
Output
No | instruction | 0 | 90,834 | 23 | 181,668 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
R, C = map(int, input().split())
n = int(input())
G = [[] for _ in range(R+C)]
for _ in range(n):
r, c, a = map(int, input().split())
r -= 1
c -= 1
G[r].append((R+c, a))
G[R+c].append((r, a))
D = [-1]*(R+C)
def dfs(v):
rmin, cmin = float("inf"), float("inf")
stack = [(v, 0)]
while stack:
nv, cost = stack.pop()
D[nv] = cost
if nv<R:
rmin = min(rmin, cost)
else:
cmin = min(cmin, cost)
for i, nc in G[nv]:
if D[i] != -1:
if D[nv]+D[i] != nc:
return False
else:
stack.append((i, nc-D[nv]))
return rmin+cmin >= 0
for i in range(R+C):
if D[i] == -1:
if not dfs(i):
print("No")
break
else:
print("Yes")
``` | output | 1 | 90,834 | 23 | 181,669 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with R rows and C columns. We call the cell in the r-th row and c-th column (r,c).
Mr. Takahashi wrote non-negative integers into N of the cells, that is, he wrote a non-negative integer a_i into (r_i,c_i) for each i (1≤i≤N). After that he fell asleep.
Mr. Aoki found the grid and tries to surprise Mr. Takahashi by writing integers into all remaining cells. The grid must meet the following conditions to really surprise Mr. Takahashi.
* Condition 1: Each cell contains a non-negative integer.
* Condition 2: For any 2×2 square formed by cells on the grid, the sum of the top left and bottom right integers must always equal to the sum of the top right and bottom left integers.
Determine whether it is possible to meet those conditions by properly writing integers into all remaining cells.
Constraints
* 2≤R,C≤10^5
* 1≤N≤10^5
* 1≤r_i≤R
* 1≤c_i≤C
* (r_i,c_i) ≠ (r_j,c_j) (i≠j)
* a_i is an integer.
* 0≤a_i≤10^9
Input
The input is given from Standard Input in the following format:
R C
N
r_1 c_1 a_1
r_2 c_2 a_2
:
r_N c_N a_N
Output
Print `Yes` if it is possible to meet the conditions by properly writing integers into all remaining cells. Otherwise, print `No`.
Examples
Input
2 2
3
1 1 0
1 2 10
2 1 20
Output
Yes
Input
2 3
5
1 1 0
1 2 10
1 3 20
2 1 30
2 3 40
Output
No
Input
2 2
3
1 1 20
1 2 10
2 1 0
Output
No
Input
3 3
4
1 1 0
1 3 10
3 1 10
3 3 20
Output
Yes
Input
2 2
4
1 1 0
1 2 10
2 1 30
2 2 20
Output
No | instruction | 0 | 90,835 | 23 | 181,670 |
"Correct Solution:
```
from sys import exit, setrecursionlimit
from functools import reduce
from itertools import *
from collections import defaultdict
def read():
return int(input())
def reads():
return [int(x) for x in input().split()]
setrecursionlimit(1000000)
(R, C) = reads()
N = read()
d = defaultdict(list)
V = set()
for _ in range(N):
(r, c, a) = reads()
(r, c) = (r-1, c-1)
d["R", r].append((("C", c), a))
d["C", c].append((("R", r), a))
V.add(("R", r))
V.add(("C", c))
def walk(v):
V.discard(v)
for (w, a) in d[v]:
wcol = a - col[v]
if w in col:
if col[w] != wcol:
print("No"); exit()
else:
col[w] = wcol
walk(w)
while len(V) > 0:
v = V.pop()
col = dict()
col[v] = 0
walk(v)
rcol = min(a for (v, a) in col.items() if v[0] == "R")
ccol = min(a for (v, a) in col.items() if v[0] == "C")
if rcol + ccol < 0:
print("No"); exit()
print("Yes")
``` | output | 1 | 90,835 | 23 | 181,671 |
Provide a correct Python 3 solution for this coding contest problem.
There is a grid with R rows and C columns. We call the cell in the r-th row and c-th column (r,c).
Mr. Takahashi wrote non-negative integers into N of the cells, that is, he wrote a non-negative integer a_i into (r_i,c_i) for each i (1≤i≤N). After that he fell asleep.
Mr. Aoki found the grid and tries to surprise Mr. Takahashi by writing integers into all remaining cells. The grid must meet the following conditions to really surprise Mr. Takahashi.
* Condition 1: Each cell contains a non-negative integer.
* Condition 2: For any 2×2 square formed by cells on the grid, the sum of the top left and bottom right integers must always equal to the sum of the top right and bottom left integers.
Determine whether it is possible to meet those conditions by properly writing integers into all remaining cells.
Constraints
* 2≤R,C≤10^5
* 1≤N≤10^5
* 1≤r_i≤R
* 1≤c_i≤C
* (r_i,c_i) ≠ (r_j,c_j) (i≠j)
* a_i is an integer.
* 0≤a_i≤10^9
Input
The input is given from Standard Input in the following format:
R C
N
r_1 c_1 a_1
r_2 c_2 a_2
:
r_N c_N a_N
Output
Print `Yes` if it is possible to meet the conditions by properly writing integers into all remaining cells. Otherwise, print `No`.
Examples
Input
2 2
3
1 1 0
1 2 10
2 1 20
Output
Yes
Input
2 3
5
1 1 0
1 2 10
1 3 20
2 1 30
2 3 40
Output
No
Input
2 2
3
1 1 20
1 2 10
2 1 0
Output
No
Input
3 3
4
1 1 0
1 3 10
3 1 10
3 3 20
Output
Yes
Input
2 2
4
1 1 0
1 2 10
2 1 30
2 2 20
Output
No | instruction | 0 | 90,836 | 23 | 181,672 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(10**6)
class Edge:
def __init__(self, v1, v2, w):
self.v1 = v1
self.v2 = v2
self.w = w
def __repr__(self):
return 'Edges({},{},{})'.format(self.v1, self.v2, self.w)
def dfs(edges, v, visited, i, m, r):
m[0 if i<r else 1] = min(m[0 if i<r else 1], v[i])
for edge in edges[i]:
if visited[edge.v2]:
if (v[i] + v[edge.v2]) != edge.w:
return False
continue
visited[edge.v2] = True
v[edge.v2] = edge.w - v[i]
if not dfs(edges, v, visited, edge.v2, m, r):
return False
return True
r, c = map(int, input().split())
edges = [list() for _ in range(r + c)]
v = [0] * (r + c)
visited = [False] * (r + c)
n = int(input())
for _ in range(n):
ri, ci, ai = map(int, input().split())
ri, ci = ri-1, ci-1
edges[ri].append(Edge(ri, r+ci, ai))
edges[r+ci].append(Edge(r+ci, ri, ai))
flag = True
for i in range(r + c):
if visited[i]:
continue
m = [10**10, 10**10]
if (not dfs(edges, v, visited, i, m, r)) or sum(m) < 0:
flag = False
if flag:
print('Yes')
else:
print('No')
``` | output | 1 | 90,836 | 23 | 181,673 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a grid with R rows and C columns. We call the cell in the r-th row and c-th column (r,c).
Mr. Takahashi wrote non-negative integers into N of the cells, that is, he wrote a non-negative integer a_i into (r_i,c_i) for each i (1≤i≤N). After that he fell asleep.
Mr. Aoki found the grid and tries to surprise Mr. Takahashi by writing integers into all remaining cells. The grid must meet the following conditions to really surprise Mr. Takahashi.
* Condition 1: Each cell contains a non-negative integer.
* Condition 2: For any 2×2 square formed by cells on the grid, the sum of the top left and bottom right integers must always equal to the sum of the top right and bottom left integers.
Determine whether it is possible to meet those conditions by properly writing integers into all remaining cells.
Constraints
* 2≤R,C≤10^5
* 1≤N≤10^5
* 1≤r_i≤R
* 1≤c_i≤C
* (r_i,c_i) ≠ (r_j,c_j) (i≠j)
* a_i is an integer.
* 0≤a_i≤10^9
Input
The input is given from Standard Input in the following format:
R C
N
r_1 c_1 a_1
r_2 c_2 a_2
:
r_N c_N a_N
Output
Print `Yes` if it is possible to meet the conditions by properly writing integers into all remaining cells. Otherwise, print `No`.
Examples
Input
2 2
3
1 1 0
1 2 10
2 1 20
Output
Yes
Input
2 3
5
1 1 0
1 2 10
1 3 20
2 1 30
2 3 40
Output
No
Input
2 2
3
1 1 20
1 2 10
2 1 0
Output
No
Input
3 3
4
1 1 0
1 3 10
3 1 10
3 3 20
Output
Yes
Input
2 2
4
1 1 0
1 2 10
2 1 30
2 2 20
Output
No
Submitted Solution:
```
# 重み付き UnionFind を初めて書いた
class WeightedUnionFind:
# https://qiita.com/drken/items/cce6fc5c579051e64fab
def __init__(self, n, SUM_UNITY=0):
self.par = list(range(n))
self.rank = [0] * n
self.diff_weight = [SUM_UNITY] * n
def root(self, x):
p = self.par[x]
if p == x:
return x
else:
r = self.root(p)
self.diff_weight[x] += self.diff_weight[p]
self.par[x] = r
return r
def weight(self, x):
self.root(x)
return self.diff_weight[x]
def same(self, x, y):
return self.root(x) == self.root(y)
def unite(self, x, y, w):
if self.same(x, y):
return self.diff(x, y) == w
w += self.weight(x); w -= self.weight(y)
x, y = self.root(x), self.root(y)
# if x == y:
# return False
if self.rank[x] < self.rank[y]:
x, y = y, x
w = -w
if self.rank[x] == self.rank[y]:
self.rank[x] += 1
self.par[y] = x
self.diff_weight[y] = w
return True
def diff(self, x, y):
return self.weight(y) - self.weight(x)
def get_roots_weights(self):
roots = []
weights = []
for x in range(len(self.par)):
roots.append(self.root(x))
weights.append(self.weight(x))
return roots, weights
import sys
from operator import itemgetter
from itertools import groupby
from collections import defaultdict
def main():
R, C = map(int, input().split())
N = int(input())
RCA = list(zip(*[iter(map(int, sys.stdin.read().split()))]*3))
uf_r = WeightedUnionFind(R+1)
uf_c = WeightedUnionFind(C+1)
RCA.sort(key=itemgetter(0))
for _, g in groupby(RCA, key=itemgetter(0)):
_, c0, a0 = next(g)
for _, c, a in g:
if not uf_c.unite(c0, c, a-a0):
print("No")
exit()
RCA.sort(key=itemgetter(1))
for _, g in groupby(RCA, key=itemgetter(1)):
r0, _, a0 = next(g)
for r, _, a in g:
if not uf_r.unite(r0, r, a-a0):
print("No")
exit()
r_roots, r_weights = uf_r.get_roots_weights()
c_roots, c_weights = uf_c.get_roots_weights()
r_roots_inv = defaultdict(list)
for i, r in enumerate(r_roots):
r_roots_inv[r].append(i)
c_roots_inv = defaultdict(list)
for i, r in enumerate(c_roots):
c_roots_inv[r].append(i)
Closed_r = set()
Closed_c = set()
for r, c, a in RCA:
root_r, root_c = r_roots[r], c_roots[c]
if root_r in Closed_r:
assert root_c in Closed_c
continue
Closed_r.add(root_r)
Closed_c.add(root_c)
mi = float("inf")
for v in r_roots_inv[root_r]:
mi = min(mi, r_weights[v])
a += mi - r_weights[r]
for v in c_roots_inv[root_c]:
a_ = a + c_weights[v] - c_weights[c]
if a_ < 0:
print("No")
exit()
print("Yes")
main()
``` | instruction | 0 | 90,838 | 23 | 181,676 |
Yes | output | 1 | 90,838 | 23 | 181,677 |
Provide a correct Python 3 solution for this coding contest problem.
The fundamental idea in the JPEG compression algorithm is to sort coeffi- cient of given image by zigzag path and encode it. In this problem, we don’t discuss about details of the algorithm, but you are asked to make simple pro- gram. You are given single integer N , and you must output zigzag path on a matrix where size is N by N . The zigzag scanning is start at the upper-left corner (0, 0) and end up at the bottom-right corner. See the following Figure and sample output to make sure rule of the zigzag scanning. For example, if you are given N = 8, corresponding output should be a matrix shown in right-side of the Figure. This matrix consists of visited time for each element.
<image>
Input
Several test cases are given. Each test case consists of one integer N (0 < N < 10) in a line. The input will end at a line contains single zero.
Output
For each input, you must output a matrix where each element is the visited time. All numbers in the matrix must be right justified in a field of width 3. Each matrix should be prefixed by a header “Case x:” where x equals test case number.
Example
Input
3
4
0
Output
Case 1:
1 2 6
3 5 7
4 8 9
Case 2:
1 2 6 7
3 5 8 13
4 9 12 14
10 11 15 16 | instruction | 0 | 90,878 | 23 | 181,756 |
"Correct Solution:
```
import sys
def p(t):
for l in t:
for n in l:
print("{0:>3}".format(n),end="")
print("")
c = 0
for n in sys.stdin:
n = int(n)
if n == 0:
break
t = [ [ 0 for _ in range(n) ] for _ in range(n) ]
c += 1
print("Case {}:".format(c))
x = 0
y = 0
up= True
for i in range(1,n*n+1):
# print("{} {}".format(x,y))
t[y][x] = i
if up:
if y == 0:
if x < n-1:
x += 1
up = False
else:
y += 1
up = False
else:
if x < n-1:
x += 1
y -= 1
else:
y += 1
up = False
else:
if x == 0:
if y < n-1:
y += 1
up = True
else:
x += 1
up = True
else:
if y < n-1:
x -= 1
y += 1
else:
x += 1
up = True
p(t)
``` | output | 1 | 90,878 | 23 | 181,757 |
Provide a correct Python 3 solution for this coding contest problem.
The fundamental idea in the JPEG compression algorithm is to sort coeffi- cient of given image by zigzag path and encode it. In this problem, we don’t discuss about details of the algorithm, but you are asked to make simple pro- gram. You are given single integer N , and you must output zigzag path on a matrix where size is N by N . The zigzag scanning is start at the upper-left corner (0, 0) and end up at the bottom-right corner. See the following Figure and sample output to make sure rule of the zigzag scanning. For example, if you are given N = 8, corresponding output should be a matrix shown in right-side of the Figure. This matrix consists of visited time for each element.
<image>
Input
Several test cases are given. Each test case consists of one integer N (0 < N < 10) in a line. The input will end at a line contains single zero.
Output
For each input, you must output a matrix where each element is the visited time. All numbers in the matrix must be right justified in a field of width 3. Each matrix should be prefixed by a header “Case x:” where x equals test case number.
Example
Input
3
4
0
Output
Case 1:
1 2 6
3 5 7
4 8 9
Case 2:
1 2 6 7
3 5 8 13
4 9 12 14
10 11 15 16 | instruction | 0 | 90,880 | 23 | 181,760 |
"Correct Solution:
```
case = 1
while True:
n = int(input())
if not n:
break
jpeg = [[0] * n for _ in range(n)]
n1 = n - 1
px, cur = [0, 0], 1
while px[0] < n:
i, j = px
jpeg[i][j] = cur
odd = (i + j) % 2
if px[not odd] == n1:
px[odd] += 1
elif not px[odd]:
px[not odd] += 1
else:
px[not odd] += 1
px[odd] -= 1
cur += 1
print('Case {}:'.format(case))
for row in jpeg:
print(''.join('{:>3}'.format(pixel) for pixel in row))
case += 1
``` | output | 1 | 90,880 | 23 | 181,761 |
Provide a correct Python 3 solution for this coding contest problem.
The fundamental idea in the JPEG compression algorithm is to sort coeffi- cient of given image by zigzag path and encode it. In this problem, we don’t discuss about details of the algorithm, but you are asked to make simple pro- gram. You are given single integer N , and you must output zigzag path on a matrix where size is N by N . The zigzag scanning is start at the upper-left corner (0, 0) and end up at the bottom-right corner. See the following Figure and sample output to make sure rule of the zigzag scanning. For example, if you are given N = 8, corresponding output should be a matrix shown in right-side of the Figure. This matrix consists of visited time for each element.
<image>
Input
Several test cases are given. Each test case consists of one integer N (0 < N < 10) in a line. The input will end at a line contains single zero.
Output
For each input, you must output a matrix where each element is the visited time. All numbers in the matrix must be right justified in a field of width 3. Each matrix should be prefixed by a header “Case x:” where x equals test case number.
Example
Input
3
4
0
Output
Case 1:
1 2 6
3 5 7
4 8 9
Case 2:
1 2 6 7
3 5 8 13
4 9 12 14
10 11 15 16 | instruction | 0 | 90,881 | 23 | 181,762 |
"Correct Solution:
```
# AOJ 1007: JPEG Compression
# Python3 2018.7.5 bal4u
import sys
from sys import stdin
input = stdin.readline
cno = 0
while True:
n = int(input())
if n == 0: break
cno += 1
a = [[0 for j in range(12)] for i in range(12)]
m = k = f = 1;
while True:
if f:
for r in range(k-1, -1, -1):
a[r][k-1-r] = m
m += 1
else:
for c in range(k-1, -1, -1):
a[k-1-c][c] = m
m += 1
f = 1-f
k += 1
if k > n: break
k = n-1
while True:
if f:
for c in range(n-k, n):
a[2*n-1-k-c][c] = m
m += 1
else:
for r in range(n-k, n):
a[r][2*n-1-k-r] = m
m += 1
f = 1-f
k -= 1
if k < 1: break
print("Case ", cno, ":", sep='')
for r in range(n):
for c in range(n): print(format(a[r][c], "3d"), end='')
print()
``` | output | 1 | 90,881 | 23 | 181,763 |
Provide a correct Python 3 solution for this coding contest problem.
The fundamental idea in the JPEG compression algorithm is to sort coeffi- cient of given image by zigzag path and encode it. In this problem, we don’t discuss about details of the algorithm, but you are asked to make simple pro- gram. You are given single integer N , and you must output zigzag path on a matrix where size is N by N . The zigzag scanning is start at the upper-left corner (0, 0) and end up at the bottom-right corner. See the following Figure and sample output to make sure rule of the zigzag scanning. For example, if you are given N = 8, corresponding output should be a matrix shown in right-side of the Figure. This matrix consists of visited time for each element.
<image>
Input
Several test cases are given. Each test case consists of one integer N (0 < N < 10) in a line. The input will end at a line contains single zero.
Output
For each input, you must output a matrix where each element is the visited time. All numbers in the matrix must be right justified in a field of width 3. Each matrix should be prefixed by a header “Case x:” where x equals test case number.
Example
Input
3
4
0
Output
Case 1:
1 2 6
3 5 7
4 8 9
Case 2:
1 2 6 7
3 5 8 13
4 9 12 14
10 11 15 16 | instruction | 0 | 90,882 | 23 | 181,764 |
"Correct Solution:
```
t = 1
while 1:
N = int(input())
if N == 0:
break
print("Case %d:" % t); t += 1
M = [[0]*N for i in range(N)]
c = 1
for i in range(2*N-1):
for j in range(max(i-N+1, 0), min(N, i+1)):
if i % 2 == 0:
M[i-j][j] = "%3d" % c
else:
M[j][i-j] = "%3d" % c
c += 1
for l in M:
print(*l,sep='')
``` | output | 1 | 90,882 | 23 | 181,765 |
Provide a correct Python 3 solution for this coding contest problem.
Write a program which prints the central coordinate ($cx$,$cy$) and the radius $r$ of a circumscribed circle of a triangle which is constructed by three points ($x_1$, $y_1$), ($x_2$, $y_2$) and ($x_3$, $y_3$) on the plane surface.
Constraints
* $-10000 \leq x_i, y_i \leq 10000$
* The three points are not on the same straight line
Input
The input is given in the following format
$x_1$ $y_1$
$x_2$ $y_2$
$x_3$ $y_3$
All the input are integers.
Output
Print $cx$, $cy$ and $r$ separated by a single space in a line. The output val ues should be in a decimal fraction with an error less than 0.000001.
Examples
Input
1 -2
3 2
-2 0
Output
0.62500000000000000000 0.68750000000000000000 2.71353666826155124291
Input
0 3
4 0
0 0
Output
2.00000000000000000000 1.50000000000000000000 2.50000000000000000000 | instruction | 0 | 90,936 | 23 | 181,872 |
"Correct Solution:
```
from decimal import Decimal
x1, y1 = map(str, input().split())
x2, y2 = map(str, input().split())
x3, y3 = map(str, input().split())
x1, y1 = Decimal(x1), Decimal(y1)
x2, y2 = Decimal(x2), Decimal(y2)
x3, y3 = Decimal(x3), Decimal(y3)
#3辺の長さ
a = (((x1-x2)**Decimal('2'))+((y1-y2)**Decimal('2')))**Decimal('0.5')
b = (((x3-x2)**Decimal('2'))+((y3-y2)**Decimal('2')))**Decimal('0.5')
c = (((x1-x3)**Decimal('2'))+((y1-y3)**Decimal('2')))**Decimal('0.5')
s = ((a+b+c)/Decimal('2'))
r = (a*b*c)/(((s*(s-a)*(s-b)*(s-c))**Decimal('0.5'))*Decimal('4'))
xa, ya = (x1+x2)/2, (y1+y2)/2
xb, yb = (x3+x2)/2, (y3+y2)/2
if (y3-y2) != 0 and (y2-y1) != 0:
a1, b1 = -(x3-x2)/(y3-y2), ((x3-x2)*xb+(y3-y2)*yb)/(y3-y2)
a2, b2 = -(x2-x1)/(y2-y1), ((x2-x1)*xa+(y2-y1)*ya)/(y2-y1)
cx = (b2-b1)/(a1-a2)
cy = a1 * cx + b1
if y3-y2 == 0:
cx = xb
a2, b2 = -(x2-x1)/(y2-y1), ((x2-x1)*xa+(y2-y1)*ya)/(y2-y1)
cy = a2 * cx + b2
if y2-y1 == 0:
cx = xa
a1, b1 = -(x3-x2)/(y3-y2), ((x3-x2)*xb+(y3-y2)*yb)/(y3-y2)
cy = a1 * cx + b1
print(cx, cy, r)
``` | output | 1 | 90,936 | 23 | 181,873 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of length n and array b of length m both consisting of only integers 0 and 1. Consider a matrix c of size n × m formed by following rule: c_{i, j} = a_i ⋅ b_j (i.e. a_i multiplied by b_j). It's easy to see that c consists of only zeroes and ones too.
How many subrectangles of size (area) k consisting only of ones are there in c?
A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x_1, x_2, y_1, y_2 (1 ≤ x_1 ≤ x_2 ≤ n, 1 ≤ y_1 ≤ y_2 ≤ m) a subrectangle c[x_1 ... x_2][y_1 ... y_2] is an intersection of the rows x_1, x_1+1, x_1+2, ..., x_2 and the columns y_1, y_1+1, y_1+2, ..., y_2.
The size (area) of a subrectangle is the total number of cells in it.
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 40 000, 1 ≤ k ≤ n ⋅ m), length of array a, length of array b and required size of subrectangles.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1), elements of a.
The third line contains m integers b_1, b_2, …, b_m (0 ≤ b_i ≤ 1), elements of b.
Output
Output single integer — the number of subrectangles of c with size (area) k consisting only of ones.
Examples
Input
3 3 2
1 0 1
1 1 1
Output
4
Input
3 5 4
1 1 1
1 1 1 1 1
Output
14
Note
In first example matrix c is:
<image>
There are 4 subrectangles of size 2 consisting of only ones in it:
<image>
In second example matrix c is:
<image> | instruction | 0 | 91,050 | 23 | 182,100 |
Tags: binary search, greedy, implementation
Correct Solution:
```
import math, collections, sys
input = sys.stdin.readline
factors = set()
def Divisors(n):
i = 1
while i <= math.sqrt(n):
if (n % i == 0):
factors.add(i)
factors.add(n//i)
i+=1
n, m, k = map(int, input().split())
Divisors(k)
a = list(map(int, input().split()))
b = list(map(int, input().split()))
countA = collections.defaultdict(int)
countB = collections.defaultdict(int)
za, zb = [0], [0]
for i in a:
za.append(za[-1]+i)
for i in b:
zb.append(zb[-1]+i)
for i in factors:
for j in range(n-i+1):
if za[j+i] - za[j] == i:
countA[i]+=1
for j in range(m-i+1):
if zb[j+i] - zb[j] == i:
countB[i]+=1
ans = 0
x = 1
for i in factors:
ans+=countA[i]*countB[k//i]
print(ans)
``` | output | 1 | 91,050 | 23 | 182,101 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of length n and array b of length m both consisting of only integers 0 and 1. Consider a matrix c of size n × m formed by following rule: c_{i, j} = a_i ⋅ b_j (i.e. a_i multiplied by b_j). It's easy to see that c consists of only zeroes and ones too.
How many subrectangles of size (area) k consisting only of ones are there in c?
A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x_1, x_2, y_1, y_2 (1 ≤ x_1 ≤ x_2 ≤ n, 1 ≤ y_1 ≤ y_2 ≤ m) a subrectangle c[x_1 ... x_2][y_1 ... y_2] is an intersection of the rows x_1, x_1+1, x_1+2, ..., x_2 and the columns y_1, y_1+1, y_1+2, ..., y_2.
The size (area) of a subrectangle is the total number of cells in it.
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 40 000, 1 ≤ k ≤ n ⋅ m), length of array a, length of array b and required size of subrectangles.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1), elements of a.
The third line contains m integers b_1, b_2, …, b_m (0 ≤ b_i ≤ 1), elements of b.
Output
Output single integer — the number of subrectangles of c with size (area) k consisting only of ones.
Examples
Input
3 3 2
1 0 1
1 1 1
Output
4
Input
3 5 4
1 1 1
1 1 1 1 1
Output
14
Note
In first example matrix c is:
<image>
There are 4 subrectangles of size 2 consisting of only ones in it:
<image>
In second example matrix c is:
<image> | instruction | 0 | 91,051 | 23 | 182,102 |
Tags: binary search, greedy, implementation
Correct Solution:
```
from sys import stdin
from math import sqrt, factorial, ceil
from collections import Counter, defaultdict
from heapq import heapify, heapreplace, heappush, heappop
from bisect import bisect_left
# def fac(n):
# f = []
# for i in range(1, int(sqrt(n)) + 1):
# if n % i == 0:
# f.append(i)
# return f
n, m, k = map(int, stdin.readline().split())
a = list(map(int, stdin.readline().split()))
b = list(map(int, stdin.readline().split()))
# mat = [[0 for _ in range(m)] for j in range(n)]
# for i in range(n):
# for j in range(m):
# mat[i][j] = a[i] * b[j]
sa,sb=[0]*50000, [0]*50000
i, j = 0, 0
while i<n:
j=i
while j<n:
if a[j]==0:break
j+=1
if j>i:
temp=j-i
c=1
for x in range(temp,0,-1):
sa[x]+=c
c+=1
i=j+1
i, j = 0, 0
while i<m:
j=i
while j<m:
if b[j]==0:break
j+=1
if j>i:
temp=j-i
c=1
for x in range(temp,0,-1):
sb[x]+=c
c+=1
i=j+1
rect=0
for x in range(1,int(sqrt(k))+1):
if k%x==0:
if max(x,k//x)<=max(n,m):
rect+=sa[x]*sb[k//x]
if x!=k//x:
rect+=sa[k//x]*sb[x]
print(rect)
``` | output | 1 | 91,051 | 23 | 182,103 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of length n and array b of length m both consisting of only integers 0 and 1. Consider a matrix c of size n × m formed by following rule: c_{i, j} = a_i ⋅ b_j (i.e. a_i multiplied by b_j). It's easy to see that c consists of only zeroes and ones too.
How many subrectangles of size (area) k consisting only of ones are there in c?
A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x_1, x_2, y_1, y_2 (1 ≤ x_1 ≤ x_2 ≤ n, 1 ≤ y_1 ≤ y_2 ≤ m) a subrectangle c[x_1 ... x_2][y_1 ... y_2] is an intersection of the rows x_1, x_1+1, x_1+2, ..., x_2 and the columns y_1, y_1+1, y_1+2, ..., y_2.
The size (area) of a subrectangle is the total number of cells in it.
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 40 000, 1 ≤ k ≤ n ⋅ m), length of array a, length of array b and required size of subrectangles.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1), elements of a.
The third line contains m integers b_1, b_2, …, b_m (0 ≤ b_i ≤ 1), elements of b.
Output
Output single integer — the number of subrectangles of c with size (area) k consisting only of ones.
Examples
Input
3 3 2
1 0 1
1 1 1
Output
4
Input
3 5 4
1 1 1
1 1 1 1 1
Output
14
Note
In first example matrix c is:
<image>
There are 4 subrectangles of size 2 consisting of only ones in it:
<image>
In second example matrix c is:
<image> | instruction | 0 | 91,052 | 23 | 182,104 |
Tags: binary search, greedy, implementation
Correct Solution:
```
from bisect import bisect_left as bl, bisect_right as br, insort
import sys
import heapq
from math import *
from collections import defaultdict as dd, deque
def data(): return sys.stdin.readline().strip()
def mdata(): return map(int, data().split())
#sys.setrecursionlimit(100000)
def fac(x):
f=set()
for i in range(1,int(x**0.5)+1):
if x%i==0:
f.add(i)
f.add(x//i)
return f
n,m,k=mdata()
a=list(mdata())
b=list(mdata())
f=sorted(fac(k))
t=0
a1={}
b1={}
fa={}
fb={}
for i in a:
if i==1:
t+=1
elif t!=0:
if a1.get(t)==None:
a1[t]=1
else:
a1[t]+=1
t=0
if t!=0:
if a1.get(t) == None:
a1[t] = 1
else:
a1[t] += 1
for i in f:
fa[i]=0
for j in a1:
if i<=j:
fa[i]+=(j-i+1)*a1[j]
t=0
for i in b:
if i==1:
t+=1
elif t!=0:
if b1.get(t)==None:
b1[t]=1
else:
b1[t]+=1
t=0
if t!=0:
if b1.get(t) == None:
b1[t] = 1
else:
b1[t] += 1
for i in f:
fb[i] = 0
for j in b1:
if i<=j:
fb[i]+=(j-i+1)*b1[j]
ans=0
for i in fa:
if k//i in fb:
ans+=fa[i]*fb[k//i]
print(ans)
``` | output | 1 | 91,052 | 23 | 182,105 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of length n and array b of length m both consisting of only integers 0 and 1. Consider a matrix c of size n × m formed by following rule: c_{i, j} = a_i ⋅ b_j (i.e. a_i multiplied by b_j). It's easy to see that c consists of only zeroes and ones too.
How many subrectangles of size (area) k consisting only of ones are there in c?
A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x_1, x_2, y_1, y_2 (1 ≤ x_1 ≤ x_2 ≤ n, 1 ≤ y_1 ≤ y_2 ≤ m) a subrectangle c[x_1 ... x_2][y_1 ... y_2] is an intersection of the rows x_1, x_1+1, x_1+2, ..., x_2 and the columns y_1, y_1+1, y_1+2, ..., y_2.
The size (area) of a subrectangle is the total number of cells in it.
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 40 000, 1 ≤ k ≤ n ⋅ m), length of array a, length of array b and required size of subrectangles.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1), elements of a.
The third line contains m integers b_1, b_2, …, b_m (0 ≤ b_i ≤ 1), elements of b.
Output
Output single integer — the number of subrectangles of c with size (area) k consisting only of ones.
Examples
Input
3 3 2
1 0 1
1 1 1
Output
4
Input
3 5 4
1 1 1
1 1 1 1 1
Output
14
Note
In first example matrix c is:
<image>
There are 4 subrectangles of size 2 consisting of only ones in it:
<image>
In second example matrix c is:
<image> | instruction | 0 | 91,053 | 23 | 182,106 |
Tags: binary search, greedy, implementation
Correct Solution:
```
import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(map(int, input().split()))
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
def revn(n): return str(n)[::-1]
def dd(): return collections.defaultdict(int)
def ddl(): return collections.defaultdict(list)
def sieve(n):
if n < 2: return list()
prime = [True for _ in range(n + 1)]
p = 3
while p * p <= n:
if prime[p]:
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 2
r = [2]
for p in range(3, n + 1, 2):
if prime[p]:
r.append(p)
return r
def divs(n, start=1):
r = []
for i in range(start, int(math.sqrt(n) + 1)):
if (n % i == 0):
if (n / i == i):
r.append(i)
else:
r.extend([i, n // i])
return r
def divn(n, primes):
divs_number = 1
for i in primes:
if n == 1:
return divs_number
t = 1
while n % i == 0:
t += 1
n //= i
divs_number *= t
def prime(n):
if n == 2: return True
if n % 2 == 0 or n <= 1: return False
sqr = int(math.sqrt(n)) + 1
for d in range(3, sqr, 2):
if n % d == 0: return False
return True
def convn(number, base):
newnumber = 0
while number > 0:
newnumber += number % base
number //= base
return newnumber
def cdiv(n, k): return n // k + (n % k != 0)
def pars(arr, num):
t = dd()
c = 0
for i in range(num):
if arr[i]:
c += 1
else:
if c: t[c] += 1
c = 0
if c:
t[c] += 1
return t
n, m, k = mi()
a = li()
b = li()
aones = pars(a, n)
bones = pars(b, m)
ans = 0
abones = {}
for i in aones:
for j in bones:
abones[(i, j)] = aones[i] * bones[j]
for t in abones:
for d in divs(k):
ans += abones[t] * max(0, t[0] - d + 1) * max(0, t[1] - k // d + 1)
print(ans)
``` | output | 1 | 91,053 | 23 | 182,107 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of length n and array b of length m both consisting of only integers 0 and 1. Consider a matrix c of size n × m formed by following rule: c_{i, j} = a_i ⋅ b_j (i.e. a_i multiplied by b_j). It's easy to see that c consists of only zeroes and ones too.
How many subrectangles of size (area) k consisting only of ones are there in c?
A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x_1, x_2, y_1, y_2 (1 ≤ x_1 ≤ x_2 ≤ n, 1 ≤ y_1 ≤ y_2 ≤ m) a subrectangle c[x_1 ... x_2][y_1 ... y_2] is an intersection of the rows x_1, x_1+1, x_1+2, ..., x_2 and the columns y_1, y_1+1, y_1+2, ..., y_2.
The size (area) of a subrectangle is the total number of cells in it.
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 40 000, 1 ≤ k ≤ n ⋅ m), length of array a, length of array b and required size of subrectangles.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1), elements of a.
The third line contains m integers b_1, b_2, …, b_m (0 ≤ b_i ≤ 1), elements of b.
Output
Output single integer — the number of subrectangles of c with size (area) k consisting only of ones.
Examples
Input
3 3 2
1 0 1
1 1 1
Output
4
Input
3 5 4
1 1 1
1 1 1 1 1
Output
14
Note
In first example matrix c is:
<image>
There are 4 subrectangles of size 2 consisting of only ones in it:
<image>
In second example matrix c is:
<image> | instruction | 0 | 91,054 | 23 | 182,108 |
Tags: binary search, greedy, implementation
Correct Solution:
```
import sys
input = sys.stdin.readline
from collections import *
n, m, k = map(int, input().split())
a = list(map(int, input().split()))+[0]
b = list(map(int, input().split()))+[0]
cnt1 = [0]*(n+1)
now = 0
for i in range(n+1):
if a[i]==0:
for j in range(1, now+1):
cnt1[j] += now-j+1
now = 0
else:
now += 1
cnt2 = [0]*(m+1)
now = 0
for i in range(m+1):
if b[i]==0:
for j in range(1, now+1):
cnt2[j] += now-j+1
now = 0
else:
now += 1
ans = 0
for i in range(1, int(k**0.5)+1):
if k%i==0:
if i<=n and k//i<=m:
ans += cnt1[i]*cnt2[k//i]
if i!=k//i:
if k//i<=n and i<=m:
ans += cnt1[k//i]*cnt2[i]
print(ans)
``` | output | 1 | 91,054 | 23 | 182,109 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of length n and array b of length m both consisting of only integers 0 and 1. Consider a matrix c of size n × m formed by following rule: c_{i, j} = a_i ⋅ b_j (i.e. a_i multiplied by b_j). It's easy to see that c consists of only zeroes and ones too.
How many subrectangles of size (area) k consisting only of ones are there in c?
A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x_1, x_2, y_1, y_2 (1 ≤ x_1 ≤ x_2 ≤ n, 1 ≤ y_1 ≤ y_2 ≤ m) a subrectangle c[x_1 ... x_2][y_1 ... y_2] is an intersection of the rows x_1, x_1+1, x_1+2, ..., x_2 and the columns y_1, y_1+1, y_1+2, ..., y_2.
The size (area) of a subrectangle is the total number of cells in it.
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 40 000, 1 ≤ k ≤ n ⋅ m), length of array a, length of array b and required size of subrectangles.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1), elements of a.
The third line contains m integers b_1, b_2, …, b_m (0 ≤ b_i ≤ 1), elements of b.
Output
Output single integer — the number of subrectangles of c with size (area) k consisting only of ones.
Examples
Input
3 3 2
1 0 1
1 1 1
Output
4
Input
3 5 4
1 1 1
1 1 1 1 1
Output
14
Note
In first example matrix c is:
<image>
There are 4 subrectangles of size 2 consisting of only ones in it:
<image>
In second example matrix c is:
<image> | instruction | 0 | 91,055 | 23 | 182,110 |
Tags: binary search, greedy, implementation
Correct Solution:
```
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a_lenths = {}
curr_len = 0
for i in range(len(a)):
if a[i] == 1:
curr_len += 1
else:
a_lenths[curr_len] = a_lenths.get(curr_len, 0) + 1
curr_len = 0
if curr_len != 0:
a_lenths[curr_len] = a_lenths.get(curr_len, 0) + 1
b_lenths = {}
curr_len = 0
for i in range(len(b)):
if b[i] == 1:
curr_len += 1
else:
b_lenths[curr_len] = b_lenths.get(curr_len, 0) + 1
curr_len = 0
if curr_len != 0:
b_lenths[curr_len] = b_lenths.get(curr_len, 0) + 1
# print("a_lenths", a_lenths)
# print("b_lenths", b_lenths)
k_divs = []
for i in range(1, int(k ** 0.5)+1):
if k % i == 0:
k_divs.append(i)
if k // i != i:
k_divs.append(k // i)
ans = 0
for seg_a in a_lenths:
for div1 in k_divs:
if div1 <= seg_a:
div2 = k // div1
num_seg_a = (seg_a - div1 + 1) * a_lenths[seg_a]
for seg_b in b_lenths:
if div2 <= seg_b:
num_seg_b = (seg_b - div2 + 1) * b_lenths[seg_b]
ans += num_seg_a * num_seg_b
# print("div1", div1)
# print("div2", div2)
# print("num_seg_a", num_seg_a)
# print("num_seg_b", num_seg_b)
# print("ans", ans)
print(ans)
``` | output | 1 | 91,055 | 23 | 182,111 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of length n and array b of length m both consisting of only integers 0 and 1. Consider a matrix c of size n × m formed by following rule: c_{i, j} = a_i ⋅ b_j (i.e. a_i multiplied by b_j). It's easy to see that c consists of only zeroes and ones too.
How many subrectangles of size (area) k consisting only of ones are there in c?
A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x_1, x_2, y_1, y_2 (1 ≤ x_1 ≤ x_2 ≤ n, 1 ≤ y_1 ≤ y_2 ≤ m) a subrectangle c[x_1 ... x_2][y_1 ... y_2] is an intersection of the rows x_1, x_1+1, x_1+2, ..., x_2 and the columns y_1, y_1+1, y_1+2, ..., y_2.
The size (area) of a subrectangle is the total number of cells in it.
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 40 000, 1 ≤ k ≤ n ⋅ m), length of array a, length of array b and required size of subrectangles.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1), elements of a.
The third line contains m integers b_1, b_2, …, b_m (0 ≤ b_i ≤ 1), elements of b.
Output
Output single integer — the number of subrectangles of c with size (area) k consisting only of ones.
Examples
Input
3 3 2
1 0 1
1 1 1
Output
4
Input
3 5 4
1 1 1
1 1 1 1 1
Output
14
Note
In first example matrix c is:
<image>
There are 4 subrectangles of size 2 consisting of only ones in it:
<image>
In second example matrix c is:
<image> | instruction | 0 | 91,056 | 23 | 182,112 |
Tags: binary search, greedy, implementation
Correct Solution:
```
#!/usr/bin/env python3
import io, os
from sys import setrecursionlimit
from collections import defaultdict
from itertools import groupby
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def rint():
return map(int, input().split())
def oint():
return int(input())
def get_str():
return input().decode().strip()
n, m, k = rint()
fla= [len(a) for a in get_str().replace(' ', '').split('0') if len(a)]
flb= [len(a) for a in get_str().replace(' ', '').split('0') if len(a)]
ans = 0
i = 1
while i*i <= k:
if k%i == 0:
sr = i
sc = k//sr
ans += sum(r-sr+1 for r in fla if sr <= r) * sum(c-sc+1 for c in flb if sc <= c)
if sr == sc:
break
sr, sc = sc, sr
ans += sum(r-sr+1 for r in fla if sr <= r) * sum(c-sc+1 for c in flb if sc <= c)
i += 1
print(ans)
``` | output | 1 | 91,056 | 23 | 182,113 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given an array a of length n and array b of length m both consisting of only integers 0 and 1. Consider a matrix c of size n × m formed by following rule: c_{i, j} = a_i ⋅ b_j (i.e. a_i multiplied by b_j). It's easy to see that c consists of only zeroes and ones too.
How many subrectangles of size (area) k consisting only of ones are there in c?
A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x_1, x_2, y_1, y_2 (1 ≤ x_1 ≤ x_2 ≤ n, 1 ≤ y_1 ≤ y_2 ≤ m) a subrectangle c[x_1 ... x_2][y_1 ... y_2] is an intersection of the rows x_1, x_1+1, x_1+2, ..., x_2 and the columns y_1, y_1+1, y_1+2, ..., y_2.
The size (area) of a subrectangle is the total number of cells in it.
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 40 000, 1 ≤ k ≤ n ⋅ m), length of array a, length of array b and required size of subrectangles.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1), elements of a.
The third line contains m integers b_1, b_2, …, b_m (0 ≤ b_i ≤ 1), elements of b.
Output
Output single integer — the number of subrectangles of c with size (area) k consisting only of ones.
Examples
Input
3 3 2
1 0 1
1 1 1
Output
4
Input
3 5 4
1 1 1
1 1 1 1 1
Output
14
Note
In first example matrix c is:
<image>
There are 4 subrectangles of size 2 consisting of only ones in it:
<image>
In second example matrix c is:
<image> | instruction | 0 | 91,057 | 23 | 182,114 |
Tags: binary search, greedy, implementation
Correct Solution:
```
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
aa=[]
bb=[]
c=0
f=0
for i in range(n):
if(a[i]==1):
if(f==0):
c=1
f=1
else:
c+=1
else:
if(f==1):
f=0
aa.append(c)
c=0
else:
continue
if(f==1):
aa.append(c)
c=0
######
f=0
c=0
for i in range(m):
if(b[i]==1):
if(f==0):
c=1
f=1
else:
c+=1
else:
if(f==1):
f=0
bb.append(c)
c=0
else:
continue
if(f==1):
bb.append(c)
c=0
x=[]
for i in range(1,int(k**(0.5))+1):
if(k%i==0):
if(i!=k//i):
x.append(i)
x.append(k//i)
else:
x.append(i)
ans=0
for p in x:
q=k//p
y=0
z=0
for i in aa:
if(i>=p):
y+=i-p+1
for i in bb:
if(i>=q):
z+=i-q+1
ans+=y*z
print(int(ans))
``` | output | 1 | 91,057 | 23 | 182,115 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length n and array b of length m both consisting of only integers 0 and 1. Consider a matrix c of size n × m formed by following rule: c_{i, j} = a_i ⋅ b_j (i.e. a_i multiplied by b_j). It's easy to see that c consists of only zeroes and ones too.
How many subrectangles of size (area) k consisting only of ones are there in c?
A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x_1, x_2, y_1, y_2 (1 ≤ x_1 ≤ x_2 ≤ n, 1 ≤ y_1 ≤ y_2 ≤ m) a subrectangle c[x_1 ... x_2][y_1 ... y_2] is an intersection of the rows x_1, x_1+1, x_1+2, ..., x_2 and the columns y_1, y_1+1, y_1+2, ..., y_2.
The size (area) of a subrectangle is the total number of cells in it.
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 40 000, 1 ≤ k ≤ n ⋅ m), length of array a, length of array b and required size of subrectangles.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1), elements of a.
The third line contains m integers b_1, b_2, …, b_m (0 ≤ b_i ≤ 1), elements of b.
Output
Output single integer — the number of subrectangles of c with size (area) k consisting only of ones.
Examples
Input
3 3 2
1 0 1
1 1 1
Output
4
Input
3 5 4
1 1 1
1 1 1 1 1
Output
14
Note
In first example matrix c is:
<image>
There are 4 subrectangles of size 2 consisting of only ones in it:
<image>
In second example matrix c is:
<image>
Submitted Solution:
```
from __future__ import division, print_function
import sys
if sys.version_info[0] < 3:
from __builtin__ import xrange as range
from future_builtins import ascii, filter, hex, map, oct, zip
import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
#from functools import lru_cache #use @lru_cache(None)
if os.path.exists('in.txt'): sys.stdin=open('in.txt','r')
if os.path.exists('out.txt'): sys.stdout=open('out.txt', 'w')
#
def input(): return sys.stdin.readline()
def mapi(arg=0): return map(int if arg==0 else str,input().split())
#------------------------------------------------------------------
#always use bit 1-indexed
n,m,k = mapi()
a = list(mapi())
b = list(mapi())
res=0
bit = [0]*(n+1)
nit = [0]*(m+1)
def update(bit,n,idx,val):
while idx<=n:
bit[idx]+=val
idx+=idx&(-idx)
def query(bit,idx):
res = 0
while idx>0:
res+=bit[idx]
idx-=idx&(-idx)
return res
cnt =0
for i in a:
if i==1:
cnt+=1
update(bit,n,1,1)
update(bit,n,cnt+1,-1)
else:
cnt = 0
cnt =0
for i in b:
if i==1:
cnt+=1
update(nit,m,1,1)
update(nit,m,cnt+1,-1)
else:
cnt = 0
for i in range(1,int(k**.5)+1):
if k%i!=0: continue
if i<=n and k//i<=m:
res+=query(bit,i)*query(nit,k//i)
if k//i!=i and k//i<=n and i<=m:
res+=query(bit,k//i)*query(nit,i)
print(res)
``` | instruction | 0 | 91,058 | 23 | 182,116 |
Yes | output | 1 | 91,058 | 23 | 182,117 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length n and array b of length m both consisting of only integers 0 and 1. Consider a matrix c of size n × m formed by following rule: c_{i, j} = a_i ⋅ b_j (i.e. a_i multiplied by b_j). It's easy to see that c consists of only zeroes and ones too.
How many subrectangles of size (area) k consisting only of ones are there in c?
A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x_1, x_2, y_1, y_2 (1 ≤ x_1 ≤ x_2 ≤ n, 1 ≤ y_1 ≤ y_2 ≤ m) a subrectangle c[x_1 ... x_2][y_1 ... y_2] is an intersection of the rows x_1, x_1+1, x_1+2, ..., x_2 and the columns y_1, y_1+1, y_1+2, ..., y_2.
The size (area) of a subrectangle is the total number of cells in it.
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 40 000, 1 ≤ k ≤ n ⋅ m), length of array a, length of array b and required size of subrectangles.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1), elements of a.
The third line contains m integers b_1, b_2, …, b_m (0 ≤ b_i ≤ 1), elements of b.
Output
Output single integer — the number of subrectangles of c with size (area) k consisting only of ones.
Examples
Input
3 3 2
1 0 1
1 1 1
Output
4
Input
3 5 4
1 1 1
1 1 1 1 1
Output
14
Note
In first example matrix c is:
<image>
There are 4 subrectangles of size 2 consisting of only ones in it:
<image>
In second example matrix c is:
<image>
Submitted Solution:
```
import math
# Method to print the divisors
def printDivisors(n,l) :
list = []
# List to store half of the divisors
for i in range(1, int(math.sqrt(n) + 1)) :
if (n % i == 0) :
# Check if divisors are equal
if (n / i == i) :
l.append(i)
else :
# Otherwise print both
l.append(i)
list.append(int(n / i))
# The list will be printed in reverse
for i in list[::-1] :
l.append(i)
n,m,k=map(int,input().split())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
seq1=[]
seq2=[]
l=0
for i in range(n):
if(l1[i]==1):
l+=1
if(i==n-1):
seq1.append(l)
else:
if(l>0):
seq1.append(l)
l=0
l=0
for i in range(m):
if(l2[i]==1):
l+=1
if(i==m-1):
seq2.append(l)
else:
if(l>0):
seq2.append(l)
l=0
list_of_div=[]
printDivisors(k,list_of_div)
#print(list_of_div)
#print(seq1)
#print(seq2)
summ=0
for i in range(len(list_of_div)):
a=0
b=0
for j in range(len(seq1)):
if(seq1[j]>=list_of_div[i]):
a+=seq1[j]-list_of_div[i]+1
for j in range(len(seq2)):
if(seq2[j]>=(k//list_of_div[i])):
b+=seq2[j]-(k//list_of_div[i])+1
summ+=(a*b)
print(summ)
``` | instruction | 0 | 91,059 | 23 | 182,118 |
Yes | output | 1 | 91,059 | 23 | 182,119 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length n and array b of length m both consisting of only integers 0 and 1. Consider a matrix c of size n × m formed by following rule: c_{i, j} = a_i ⋅ b_j (i.e. a_i multiplied by b_j). It's easy to see that c consists of only zeroes and ones too.
How many subrectangles of size (area) k consisting only of ones are there in c?
A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x_1, x_2, y_1, y_2 (1 ≤ x_1 ≤ x_2 ≤ n, 1 ≤ y_1 ≤ y_2 ≤ m) a subrectangle c[x_1 ... x_2][y_1 ... y_2] is an intersection of the rows x_1, x_1+1, x_1+2, ..., x_2 and the columns y_1, y_1+1, y_1+2, ..., y_2.
The size (area) of a subrectangle is the total number of cells in it.
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 40 000, 1 ≤ k ≤ n ⋅ m), length of array a, length of array b and required size of subrectangles.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1), elements of a.
The third line contains m integers b_1, b_2, …, b_m (0 ≤ b_i ≤ 1), elements of b.
Output
Output single integer — the number of subrectangles of c with size (area) k consisting only of ones.
Examples
Input
3 3 2
1 0 1
1 1 1
Output
4
Input
3 5 4
1 1 1
1 1 1 1 1
Output
14
Note
In first example matrix c is:
<image>
There are 4 subrectangles of size 2 consisting of only ones in it:
<image>
In second example matrix c is:
<image>
Submitted Solution:
```
from collections import defaultdict
from math import factorial
from sys import stdin
#input = stdin.readline
def read_tuple(): return map(int,input().split())
def read_int(): return int(input())
def read_list(): return list(map(int,input().split()))
def factors(n):
fact = []
for i in range(1, int(n**0.5)+1):
if n%i == 0:
if n//i == i:
fact.append(i)
else:
fact.append(i)
fact.append(n//i)
fact.sort()
return fact
def comb(n,r):
return factorial(n) // ( factorial(n-r)*factorial(r) )
n,m,k = read_tuple()
a = read_list()
b = read_list()
cnta, cntb = defaultdict(int), defaultdict(int)
cnta[0] = 0
cntb[0] = 0
cnt=0
for i in range(n):
if a[i]==1: cnt+=1
else:cnta[cnt]+=1; cnt=0
cnta[cnt]+=1
cnt = 0
for i in range(m):
if b[i]==1: cnt+=1
else: cntb[cnt]+=1; cnt=0
cntb[cnt]+=1
factor = factors(k)
ans = 0
for p1, cnt1 in cnta.items():
for p2, cnt2 in cntb.items():
if p1*p2 < k: continue
for f in factor:
f1, f2 = f, k//f
if p1 >= f1 and p2 >= f2:
ans += cnt1*cnt2*(p1-f1+1)*(p2-f2+1)
print(ans)
``` | instruction | 0 | 91,060 | 23 | 182,120 |
Yes | output | 1 | 91,060 | 23 | 182,121 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length n and array b of length m both consisting of only integers 0 and 1. Consider a matrix c of size n × m formed by following rule: c_{i, j} = a_i ⋅ b_j (i.e. a_i multiplied by b_j). It's easy to see that c consists of only zeroes and ones too.
How many subrectangles of size (area) k consisting only of ones are there in c?
A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x_1, x_2, y_1, y_2 (1 ≤ x_1 ≤ x_2 ≤ n, 1 ≤ y_1 ≤ y_2 ≤ m) a subrectangle c[x_1 ... x_2][y_1 ... y_2] is an intersection of the rows x_1, x_1+1, x_1+2, ..., x_2 and the columns y_1, y_1+1, y_1+2, ..., y_2.
The size (area) of a subrectangle is the total number of cells in it.
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 40 000, 1 ≤ k ≤ n ⋅ m), length of array a, length of array b and required size of subrectangles.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1), elements of a.
The third line contains m integers b_1, b_2, …, b_m (0 ≤ b_i ≤ 1), elements of b.
Output
Output single integer — the number of subrectangles of c with size (area) k consisting only of ones.
Examples
Input
3 3 2
1 0 1
1 1 1
Output
4
Input
3 5 4
1 1 1
1 1 1 1 1
Output
14
Note
In first example matrix c is:
<image>
There are 4 subrectangles of size 2 consisting of only ones in it:
<image>
In second example matrix c is:
<image>
Submitted Solution:
```
from math import sqrt
def gen_dzielniki(val):
ret = list()
for i in range(1, int(sqrt(val)) + 1):
if(not (val % i)):
ret.append(i)
for i in range(int(sqrt(val)), 0, -1):
if(not (val % i) and i * i != val):
ret.append(val//i)
return ret
def zamien(lista):
ret = []
last = -1
for i in range(len(lista)):
if lista[i] == 0 and i != 0 and lista[i-1] != 0:
ret.append(i - last - 1)
last = i
elif lista[i] == 1 and i == len(lista) - 1:
ret.append(i - last)
elif lista[i] == 0:
last = i
else:
pass
ret2 = {}
for u in ret:
if not ret2.get(u):
ret2[u] = 1
else:
ret2[u] += 1
return ret2
n, m, k = list(map(int, input().split()))
dzielniki = gen_dzielniki(k)
def policz(a, b):
# print(str(a) + ' ' + str(b) + " " + str(k))
ans = 0
for i in dzielniki:
A = a - i
B = b - k/i
# print(A, B, i)
if A < 0 or B < 0:
continue
ans += (A + 1) * (B + 1)
# print("wynik to " + str(ans))
return ans
mapa = {}
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A = zamien(A)
B = zamien(B)
ans = 0
for u in A:
for v in B:
ans += A[u] * B[v] * policz(u, v)
print(int(ans))
``` | instruction | 0 | 91,061 | 23 | 182,122 |
Yes | output | 1 | 91,061 | 23 | 182,123 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length n and array b of length m both consisting of only integers 0 and 1. Consider a matrix c of size n × m formed by following rule: c_{i, j} = a_i ⋅ b_j (i.e. a_i multiplied by b_j). It's easy to see that c consists of only zeroes and ones too.
How many subrectangles of size (area) k consisting only of ones are there in c?
A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x_1, x_2, y_1, y_2 (1 ≤ x_1 ≤ x_2 ≤ n, 1 ≤ y_1 ≤ y_2 ≤ m) a subrectangle c[x_1 ... x_2][y_1 ... y_2] is an intersection of the rows x_1, x_1+1, x_1+2, ..., x_2 and the columns y_1, y_1+1, y_1+2, ..., y_2.
The size (area) of a subrectangle is the total number of cells in it.
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 40 000, 1 ≤ k ≤ n ⋅ m), length of array a, length of array b and required size of subrectangles.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1), elements of a.
The third line contains m integers b_1, b_2, …, b_m (0 ≤ b_i ≤ 1), elements of b.
Output
Output single integer — the number of subrectangles of c with size (area) k consisting only of ones.
Examples
Input
3 3 2
1 0 1
1 1 1
Output
4
Input
3 5 4
1 1 1
1 1 1 1 1
Output
14
Note
In first example matrix c is:
<image>
There are 4 subrectangles of size 2 consisting of only ones in it:
<image>
In second example matrix c is:
<image>
Submitted Solution:
```
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
d1={}
flag=False
ct=0
for i in range(len(a)):
if a[i]==1 and not flag:
ct=1
flag=True
elif a[i]==1:
ct+=1
else:
flag=False
if d1.get(ct)==None:
d1[ct]=1
else:
d1[ct]+=1
ct=0
if ct!=0 and flag:
if d1.get(ct)==None:
d1[ct]=1
else:
d1[ct]+=1
d2={}
ct=0
flag=False
for i in range(len(b)):
if b[i]==1 and not flag:
ct=1
flag=True
elif b[i]==1:
ct+=1
else:
flag=False
if d2.get(ct)==None:
d2[ct]=1
else:
d2[ct]+=1
ct=0
if ct!=0 and flag:
if d2.get(ct)==None:
d2[ct]=1
else:
d2[ct]+=1
ans=0
#print(d1)
for i in d1:
for j in d2:
if i*j>=k:
for p in range(1,i+1):
for q in range(1,j+1):
if p*q==k:
ans+=(i-p+1)*(j-q+1)
print(ans)
``` | instruction | 0 | 91,062 | 23 | 182,124 |
No | output | 1 | 91,062 | 23 | 182,125 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length n and array b of length m both consisting of only integers 0 and 1. Consider a matrix c of size n × m formed by following rule: c_{i, j} = a_i ⋅ b_j (i.e. a_i multiplied by b_j). It's easy to see that c consists of only zeroes and ones too.
How many subrectangles of size (area) k consisting only of ones are there in c?
A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x_1, x_2, y_1, y_2 (1 ≤ x_1 ≤ x_2 ≤ n, 1 ≤ y_1 ≤ y_2 ≤ m) a subrectangle c[x_1 ... x_2][y_1 ... y_2] is an intersection of the rows x_1, x_1+1, x_1+2, ..., x_2 and the columns y_1, y_1+1, y_1+2, ..., y_2.
The size (area) of a subrectangle is the total number of cells in it.
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 40 000, 1 ≤ k ≤ n ⋅ m), length of array a, length of array b and required size of subrectangles.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1), elements of a.
The third line contains m integers b_1, b_2, …, b_m (0 ≤ b_i ≤ 1), elements of b.
Output
Output single integer — the number of subrectangles of c with size (area) k consisting only of ones.
Examples
Input
3 3 2
1 0 1
1 1 1
Output
4
Input
3 5 4
1 1 1
1 1 1 1 1
Output
14
Note
In first example matrix c is:
<image>
There are 4 subrectangles of size 2 consisting of only ones in it:
<image>
In second example matrix c is:
<image>
Submitted Solution:
```
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
d1={}
flag=False
ct=0
for i in range(len(a)):
if a[i]==1 and not flag:
ct=1
flag=True
elif a[i]==1:
ct+=1
else:
flag=False
if d1.get(ct)==None:
d1[ct]=1
else:
d1[ct]+=1
ct=0
if ct!=0 and flag:
if d1.get(ct)==None:
d1[ct]=1
else:
d1[ct]+=1
d2={}
ct=0
flag=False
for i in range(len(b)):
if b[i]==1 and not flag:
ct=1
flag=True
elif b[i]==1:
ct+=1
else:
flag=False
if d2.get(ct)==None:
d2[ct]=1
else:
d2[ct]+=1
ct=0
if ct!=0 and flag:
if d2.get(ct)==None:
d2[ct]=1
else:
d2[ct]+=1
ans=0
#print(d1,d2)
for i in d1:
for j in d2:
if i*j>=k:
ans+=(i*j-k+1)*d1[i]*d2[j]
if i>=k:
ans+=(i-k+1)*d1[i]
if j>=k:
ans+=(j-k+1)*d2[j]
print(ans)
``` | instruction | 0 | 91,063 | 23 | 182,126 |
No | output | 1 | 91,063 | 23 | 182,127 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length n and array b of length m both consisting of only integers 0 and 1. Consider a matrix c of size n × m formed by following rule: c_{i, j} = a_i ⋅ b_j (i.e. a_i multiplied by b_j). It's easy to see that c consists of only zeroes and ones too.
How many subrectangles of size (area) k consisting only of ones are there in c?
A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x_1, x_2, y_1, y_2 (1 ≤ x_1 ≤ x_2 ≤ n, 1 ≤ y_1 ≤ y_2 ≤ m) a subrectangle c[x_1 ... x_2][y_1 ... y_2] is an intersection of the rows x_1, x_1+1, x_1+2, ..., x_2 and the columns y_1, y_1+1, y_1+2, ..., y_2.
The size (area) of a subrectangle is the total number of cells in it.
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 40 000, 1 ≤ k ≤ n ⋅ m), length of array a, length of array b and required size of subrectangles.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1), elements of a.
The third line contains m integers b_1, b_2, …, b_m (0 ≤ b_i ≤ 1), elements of b.
Output
Output single integer — the number of subrectangles of c with size (area) k consisting only of ones.
Examples
Input
3 3 2
1 0 1
1 1 1
Output
4
Input
3 5 4
1 1 1
1 1 1 1 1
Output
14
Note
In first example matrix c is:
<image>
There are 4 subrectangles of size 2 consisting of only ones in it:
<image>
In second example matrix c is:
<image>
Submitted Solution:
```
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
h = []
not_b = [-1]
ans = 0
w_i = 0
i = 0
while i < n:
while i < n and a[i] == 1:
w_i += 1
i += 1
if w_i:
h.append(w_i)
while i < n and a[i] == 0:
i += 1
w_i = 0
print(h)
w_j = 0
j = 0
while j < m:
while j < m and b[j] == 1:
w_j += 1
j += 1
if w_j:
print(w_j)
for x in h:
if w_j * x >= k:
for p in range(1, x + 1):
if k % p == 0 and k % p <= w_j:
w = k // p
print(w)
ans += (w_j - w + 1) * (x - p + 1)
while j < m and b[j] == 0:
j += 1
w_j = 0
print(ans)
``` | instruction | 0 | 91,064 | 23 | 182,128 |
No | output | 1 | 91,064 | 23 | 182,129 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a of length n and array b of length m both consisting of only integers 0 and 1. Consider a matrix c of size n × m formed by following rule: c_{i, j} = a_i ⋅ b_j (i.e. a_i multiplied by b_j). It's easy to see that c consists of only zeroes and ones too.
How many subrectangles of size (area) k consisting only of ones are there in c?
A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x_1, x_2, y_1, y_2 (1 ≤ x_1 ≤ x_2 ≤ n, 1 ≤ y_1 ≤ y_2 ≤ m) a subrectangle c[x_1 ... x_2][y_1 ... y_2] is an intersection of the rows x_1, x_1+1, x_1+2, ..., x_2 and the columns y_1, y_1+1, y_1+2, ..., y_2.
The size (area) of a subrectangle is the total number of cells in it.
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 40 000, 1 ≤ k ≤ n ⋅ m), length of array a, length of array b and required size of subrectangles.
The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 1), elements of a.
The third line contains m integers b_1, b_2, …, b_m (0 ≤ b_i ≤ 1), elements of b.
Output
Output single integer — the number of subrectangles of c with size (area) k consisting only of ones.
Examples
Input
3 3 2
1 0 1
1 1 1
Output
4
Input
3 5 4
1 1 1
1 1 1 1 1
Output
14
Note
In first example matrix c is:
<image>
There are 4 subrectangles of size 2 consisting of only ones in it:
<image>
In second example matrix c is:
<image>
Submitted Solution:
```
R = lambda:map(int,input().split())
n, m, k = R()
a = list(R())
b = list(R())
a_counts = [0]*(n+1)
b_counts = [0]*(m+1)
count = 0
for i in range(n):
if a[i] == 1:
count += 1
if a[i]==0 or i==n-1:
for j in range(1, count+1):
a_counts[j] = a_counts[j] + count - j + 1
count = 0
count = 0
for i in range(m):
if b[i] == 1:
count += 1
if b[i]==0 or i==m-1:
for j in range(1, count+1):
b_counts[j] = b_counts[j] + count - j + 1
count = 0
ans = 0
for i in range(1, (int)(k**(1/2)) + 1) :
if (k % i == 0) :
if (k / i == i) and i<=min(m,n):
ans += a_counts[i]* b_counts[i]
else :
m2 = k//i
if i<n and k//i < m:
ans += a_counts[i]* b_counts[k // i]
if i<m and k//i < n:
ans += b_counts[i]* a_counts[k // i]
print(ans)
``` | instruction | 0 | 91,065 | 23 | 182,130 |
No | output | 1 | 91,065 | 23 | 182,131 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Pashmak has fallen in love with an attractive girl called Parmida since one year ago...
Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.
Input
The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.
Output
If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.
Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).
Examples
Input
0 0 0 1
Output
1 0 1 1
Input
0 0 1 1
Output
0 1 1 0
Input
0 0 1 2
Output
-1 | instruction | 0 | 91,275 | 23 | 182,550 |
Tags: implementation
Correct Solution:
```
x1,y1,x2,y2=input().split()
x1,y1,x2,y2=[int(x1),int(y1),int(x2),int(y2)]
s1=abs(x2-x1)
s2=abs(y2-y1)
if(s1!=s2 and s1!=0 and s2!=0):
print("-1")
else:
if(x1==x2):
print(s2+x1,y1,s2+x2,y2)
elif(y1==y2):
print(x1,s1+y1,x2,s1+y2)
else:
print(x1,y2,x2,y1)
``` | output | 1 | 91,275 | 23 | 182,551 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Pashmak has fallen in love with an attractive girl called Parmida since one year ago...
Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.
Input
The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.
Output
If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.
Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).
Examples
Input
0 0 0 1
Output
1 0 1 1
Input
0 0 1 1
Output
0 1 1 0
Input
0 0 1 2
Output
-1 | instruction | 0 | 91,276 | 23 | 182,552 |
Tags: implementation
Correct Solution:
```
__author__ = 'myduomilia'
x1, y1, x2, y2 = list(map(int, input().split()))
b = False
if x1 != x2 and y1 != y2 and abs(x1 - x2) != abs(y1 - y2):
print(-1)
elif x1 == x2:
print(x1 + abs(y1 - y2), y1, x2 + abs(y1 - y2), y2)
elif y1 == y2:
print(x1, y1 + abs(x1 - x2), x2, y2 + abs(x1 - x2))
else:
print(x1, y2, x2, y1)
``` | output | 1 | 91,276 | 23 | 182,553 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Pashmak has fallen in love with an attractive girl called Parmida since one year ago...
Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.
Input
The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.
Output
If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.
Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).
Examples
Input
0 0 0 1
Output
1 0 1 1
Input
0 0 1 1
Output
0 1 1 0
Input
0 0 1 2
Output
-1 | instruction | 0 | 91,277 | 23 | 182,554 |
Tags: implementation
Correct Solution:
```
x1,y1,x2,y2 = map(int,input().split())
a = abs(x2 - x1)
b = abs(y2 - y1)
g = (((a * a) + (b * b)) ** 0.5)
if ((a == 0) or (b == 0)):
g = (((a * a) + (b * b)) ** 0.5)
if (a == 0):
x3 = x1 + g
x4 = x2 + g
y3 = y1
y4 = y2
elif (b == 0):
y3 = y1 + g
y4 = y2 + g
x3 = x1
x4 = x2
print(round(x3),round(y3),round(x4),round(y4))
elif (a == b):
y3 = y2
x3 = x1
y4 = y1
x4 = x2
print(round(x3),round(y3),round(x4),round(y4))
else:
print('-1')
``` | output | 1 | 91,277 | 23 | 182,555 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Pashmak has fallen in love with an attractive girl called Parmida since one year ago...
Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.
Input
The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.
Output
If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.
Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).
Examples
Input
0 0 0 1
Output
1 0 1 1
Input
0 0 1 1
Output
0 1 1 0
Input
0 0 1 2
Output
-1 | instruction | 0 | 91,278 | 23 | 182,556 |
Tags: implementation
Correct Solution:
```
x1,y1,x2,y2=map(int,input().split())
if x1==x2:
Dist=abs(y1-y2)
NewX=x1+Dist
print(NewX,y1,NewX,y2)
elif y1==y2:
Dist=abs(x1-x2)
NewY=y1+Dist
print(x1,NewY,x2,NewY)
else:
if(abs(y2-y1)/abs(x2-x1)==1):
print(x1,y2,x2,y1)
else:
print(-1)
``` | output | 1 | 91,278 | 23 | 182,557 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Pashmak has fallen in love with an attractive girl called Parmida since one year ago...
Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.
Input
The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.
Output
If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.
Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).
Examples
Input
0 0 0 1
Output
1 0 1 1
Input
0 0 1 1
Output
0 1 1 0
Input
0 0 1 2
Output
-1 | instruction | 0 | 91,279 | 23 | 182,558 |
Tags: implementation
Correct Solution:
```
x1,y1,x2,y2=map(int,input().split())
t=abs(x1-x2)
a=abs(y1-y2)
if a!=t and t and a:
print(-1)
else:
if t==0:
print(x1+a,y1,x1+a,y2)
elif a==0:
print(x1,y1+t,x2,y1+t)
else:
print(x1,y2,x2,y1)
``` | output | 1 | 91,279 | 23 | 182,559 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Pashmak has fallen in love with an attractive girl called Parmida since one year ago...
Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.
Input
The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.
Output
If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.
Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).
Examples
Input
0 0 0 1
Output
1 0 1 1
Input
0 0 1 1
Output
0 1 1 0
Input
0 0 1 2
Output
-1 | instruction | 0 | 91,280 | 23 | 182,560 |
Tags: implementation
Correct Solution:
```
x1,y1,x2,y2=map(int,input().split())
if abs(x2-x1)==abs(y2-y1):
print(x1,y2,x2,y1)
elif abs(y2-y1)==0 :
print(max(x2,x1),abs(x2-x1)+y1,min(x2,x1),abs(x2-x1)+y1)
elif abs(x2-x1)==0:
print(abs(y2-y1)+x1,min(y2,y1),abs(y2-y1)+x1,max(y2,y1))
else:
print(-1)
``` | output | 1 | 91,280 | 23 | 182,561 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Pashmak has fallen in love with an attractive girl called Parmida since one year ago...
Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.
Input
The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.
Output
If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.
Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).
Examples
Input
0 0 0 1
Output
1 0 1 1
Input
0 0 1 1
Output
0 1 1 0
Input
0 0 1 2
Output
-1 | instruction | 0 | 91,281 | 23 | 182,562 |
Tags: implementation
Correct Solution:
```
ip = input()
ip = ip.split(" ")
x1 = int(ip[0])
y1 = int(ip[1])
x2 = int(ip[2])
y2 = int(ip[3])
possible = True
if x1 == x2 and y1 != y2:
s = y2 - y1
x3 = x1 + s
y3 = y1
x4 = x2 + s
y4 = y2
elif x1 != x2 and y1 == y2:
s = x2 - x1
y3 = y1 + s
x3 = x1
y4 = y2 + s
x4 = x2
else:
m = (y2-y1)/(x2-x1)
if m == 1 or m == -1:
x3 = x1
y3 = y2
x4 = x2
y4 = y1
else:
possible = False
print(-1)
if possible == True:
print(f"{x3} {y3} {x4} {y4}")
``` | output | 1 | 91,281 | 23 | 182,563 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Pashmak has fallen in love with an attractive girl called Parmida since one year ago...
Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.
Input
The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.
Output
If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.
Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).
Examples
Input
0 0 0 1
Output
1 0 1 1
Input
0 0 1 1
Output
0 1 1 0
Input
0 0 1 2
Output
-1 | instruction | 0 | 91,282 | 23 | 182,564 |
Tags: implementation
Correct Solution:
```
a,b,c,d=map(int,input().split())
r=abs(a-c if a!=c else b-d)
if a-c!=0 and abs(a-c)!=r or b-d!=0 and abs(b-d)!=r:print(-1)
elif a==c:print(a-r,b,c-r,d)
elif b==d:print(a,b-r,c,d-r)
else:print(a,d,c,b)
``` | output | 1 | 91,282 | 23 | 182,565 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pashmak has fallen in love with an attractive girl called Parmida since one year ago...
Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.
Input
The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.
Output
If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.
Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).
Examples
Input
0 0 0 1
Output
1 0 1 1
Input
0 0 1 1
Output
0 1 1 0
Input
0 0 1 2
Output
-1
Submitted Solution:
```
s = list(map(int, input().split()))
x = s[0], 1000, s[2]
m = x.index(min(x))
x1, y1 = s[m], s[m+1]
x2, y2 = s[2-m], s[2-m+1]
if x2 - x1 == y2 - y1:
L = x2-x1
print(x1+L, y1, x1, y1+L)
elif x2 - x1 == y1 - y2:
L = x2 - x1
print(x1, y1-L, x1+L, y2+L)
elif x1 == x2:
L = y2 - y1
print(x1+L, y1, x1+L, y2)
elif y1 == y2:
L = x2 - x1
print(x1, y1+L, x2, y2+L)
else:
print(-1)
``` | instruction | 0 | 91,283 | 23 | 182,566 |
Yes | output | 1 | 91,283 | 23 | 182,567 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pashmak has fallen in love with an attractive girl called Parmida since one year ago...
Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.
Input
The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.
Output
If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.
Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).
Examples
Input
0 0 0 1
Output
1 0 1 1
Input
0 0 1 1
Output
0 1 1 0
Input
0 0 1 2
Output
-1
Submitted Solution:
```
x1,y1,x2,y2 = map(int,input().split())
if x1!=x2 and y1!=y2 and (abs(x1-x2)!=abs(y1-y2)):
print(-1)
elif x1==x2:
print(x1+abs(y1-y2),y1,x2+abs(y1-y2),y2)
elif y1==y2:
print(x1,y1+abs(x1-x2),x2,y2+abs(x1-x2))
else:
print(x1,y2,x2,y1)
``` | instruction | 0 | 91,284 | 23 | 182,568 |
Yes | output | 1 | 91,284 | 23 | 182,569 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pashmak has fallen in love with an attractive girl called Parmida since one year ago...
Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.
Input
The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.
Output
If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.
Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).
Examples
Input
0 0 0 1
Output
1 0 1 1
Input
0 0 1 1
Output
0 1 1 0
Input
0 0 1 2
Output
-1
Submitted Solution:
```
l=list(map(int,input().split()))
x1=l[0]
x2=l[2]
y1=l[1]
y2=l[3]
if x1 != x2 and y1 != y2 and abs(x1 - x2) != abs(y1 - y2):
print('-1')
elif (x1 == x2):
print(x1 + abs(y1 - y2),y1,x2 + abs(y1 - y2),y2,sep=' ')
elif(y1 == y2):
print(x1,y1 + abs(x1 - x2),x2,y2 + abs(x1 - x2),sep=' ')
else:
print(x1,y2,x2,y1,sep=' ')
``` | instruction | 0 | 91,285 | 23 | 182,570 |
Yes | output | 1 | 91,285 | 23 | 182,571 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pashmak has fallen in love with an attractive girl called Parmida since one year ago...
Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.
Input
The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.
Output
If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.
Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).
Examples
Input
0 0 0 1
Output
1 0 1 1
Input
0 0 1 1
Output
0 1 1 0
Input
0 0 1 2
Output
-1
Submitted Solution:
```
x1,y1,x2,y2 = map(int, input().split())
if x1 == x2:
x3 = x1 + abs(y1-y2)
print (x3,y1,x3,y2)
elif y1 == y2:
y3 = y1 + abs(x1 - x2)
print (x1,y3,x2,y3)
else:
if abs(x1 - x2) == abs(y1 - y2):
print (x1,y2,x2,y1)
else:
print (-1)
``` | instruction | 0 | 91,286 | 23 | 182,572 |
Yes | output | 1 | 91,286 | 23 | 182,573 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pashmak has fallen in love with an attractive girl called Parmida since one year ago...
Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.
Input
The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.
Output
If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.
Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).
Examples
Input
0 0 0 1
Output
1 0 1 1
Input
0 0 1 1
Output
0 1 1 0
Input
0 0 1 2
Output
-1
Submitted Solution:
```
a,b,c,d=map(int,input().split())
if abs(c-a)>0 and abs(d-b)>0 and (c-a)==(d-b):
print(c,b,a,d)
elif (abs(c-a)>0 and abs(d-b)==0) or (abs(c-a)==0 and abs(d-b)>0):
if abs(c-a)>0:
print(a,b+(c-a),c,d+(c-a))
else:
print(a+(d-b),b,c+(d-b),d)
else:
print("-1")
``` | instruction | 0 | 91,287 | 23 | 182,574 |
No | output | 1 | 91,287 | 23 | 182,575 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pashmak has fallen in love with an attractive girl called Parmida since one year ago...
Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.
Input
The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.
Output
If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.
Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).
Examples
Input
0 0 0 1
Output
1 0 1 1
Input
0 0 1 1
Output
0 1 1 0
Input
0 0 1 2
Output
-1
Submitted Solution:
```
import math
x1,y1,x2,y2 = map(int,input().split())
if x1==x2:
x3 = x1+abs(y2-y1)
print(x3,y1,x3,y2)
elif y1==y2:
y3 = y1+abs(x2-x1)
print(x1,y3,x2,y3)
else:
if abs((y2-y1)//(x2-x1)) == 1:
s = math.sqrt(pow(x2-x1,2)+pow(y2-y1,2))//(math.sqrt(2))
if x1>x2:
print(int(x2+s),y2,int(x1-s),y1)
else:
print(int(x1+s),y1,int(x2-s),y2)
else:
print(-1)
``` | instruction | 0 | 91,288 | 23 | 182,576 |
No | output | 1 | 91,288 | 23 | 182,577 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pashmak has fallen in love with an attractive girl called Parmida since one year ago...
Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.
Input
The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.
Output
If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.
Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).
Examples
Input
0 0 0 1
Output
1 0 1 1
Input
0 0 1 1
Output
0 1 1 0
Input
0 0 1 2
Output
-1
Submitted Solution:
```
a,b,c,d = map(int,input().split())
e=0
f=0
g=0
h=0
if a==c:
e = a+(b-d)
g = a+(b-d)
h = d
f = b
print(e, f, g, h)
elif b==d:
f = b+(b-d)
h = b+(b-d)
e = a
g = c
print(e, f, g, h)
else:
if abs(a-c)==abs(b-d):
e = c
f = b
g = a
h = d
print(e,f,g,h)
else:
print(-1)
``` | instruction | 0 | 91,289 | 23 | 182,578 |
No | output | 1 | 91,289 | 23 | 182,579 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Pashmak has fallen in love with an attractive girl called Parmida since one year ago...
Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.
Input
The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≤ x1, y1, x2, y2 ≤ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.
Output
If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.
Note that x3, y3, x4, y4 must be in the range ( - 1000 ≤ x3, y3, x4, y4 ≤ 1000).
Examples
Input
0 0 0 1
Output
1 0 1 1
Input
0 0 1 1
Output
0 1 1 0
Input
0 0 1 2
Output
-1
Submitted Solution:
```
x1,y1,x2,y2=map(int,input().split())
length1= x2-x1
length2= y2-y1
s=''
if length1==0 or length2==0:
if length1==0:
s+=str(x1+y2-y1)+' '+str(y1)
s+=' '+str(x1+y2-y1)+' '+str(y2)
if length2==0:
s+=str(x1)+' '+str(x2-x1+y1)
s+=' '+str(x2-x1+y1)+' '+str(x2)
print(s)
else:
if length1==length2:
s+=str(x1)+' '+str(x2-x1)
s+=' '+str(x2)+' '+str(x1)
print(s)
else:
print(-1)
``` | instruction | 0 | 91,290 | 23 | 182,580 |
No | output | 1 | 91,290 | 23 | 182,581 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other.
Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible.
Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length
<image> The picture corresponds to the first example
Input
The first line contains single integer n (1 ≤ n ≤ 5·105) — the number of rectangles.
n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≤ x1 < x2 ≤ 109, - 109 ≤ y1 < y2 ≤ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle.
It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other.
Output
Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color.
Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≤ ci ≤ 4) — the color of i-th rectangle.
Example
Input
8
0 0 5 3
2 -1 5 0
-3 -4 2 -1
-1 -1 2 0
-3 0 0 5
5 2 10 3
7 -3 10 2
4 -2 7 -1
Output
YES
1
2
2
3
2
2
4
1 | instruction | 0 | 91,333 | 23 | 182,666 |
Tags: constructive algorithms, geometry
Correct Solution:
```
n=int(input())
print("YES")
while n:
(x,y,a,b)=list(map(int, input().split()))
print(1+x%2+2*(y%2))
n=n-1
``` | output | 1 | 91,333 | 23 | 182,667 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other.
Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible.
Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length
<image> The picture corresponds to the first example
Input
The first line contains single integer n (1 ≤ n ≤ 5·105) — the number of rectangles.
n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≤ x1 < x2 ≤ 109, - 109 ≤ y1 < y2 ≤ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle.
It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other.
Output
Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color.
Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≤ ci ≤ 4) — the color of i-th rectangle.
Example
Input
8
0 0 5 3
2 -1 5 0
-3 -4 2 -1
-1 -1 2 0
-3 0 0 5
5 2 10 3
7 -3 10 2
4 -2 7 -1
Output
YES
1
2
2
3
2
2
4
1 | instruction | 0 | 91,334 | 23 | 182,668 |
Tags: constructive algorithms, geometry
Correct Solution:
```
'''plan
noticed that if both upperle
'''
from sys import stdin, stdout
n = int(stdin.readline().rstrip())
# n = int(input())
coordinates = []
# for i in range(n):
# coordinates.append([int(x) % 2 for x in input().split()])
for i in range(n):
coordinates.append([int(x) % 2 for x in stdin.readline().rstrip().split()])
stdout.write('YES\n')
for coordinate in coordinates:
x1, y1, x2, y2 = coordinate
stdout.write(str(2 * x2 + y2 + 1) + '\n')
``` | output | 1 | 91,334 | 23 | 182,669 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other.
Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible.
Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length
<image> The picture corresponds to the first example
Input
The first line contains single integer n (1 ≤ n ≤ 5·105) — the number of rectangles.
n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≤ x1 < x2 ≤ 109, - 109 ≤ y1 < y2 ≤ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle.
It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other.
Output
Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color.
Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≤ ci ≤ 4) — the color of i-th rectangle.
Example
Input
8
0 0 5 3
2 -1 5 0
-3 -4 2 -1
-1 -1 2 0
-3 0 0 5
5 2 10 3
7 -3 10 2
4 -2 7 -1
Output
YES
1
2
2
3
2
2
4
1 | instruction | 0 | 91,335 | 23 | 182,670 |
Tags: constructive algorithms, geometry
Correct Solution:
```
n=int(input())
print("YES")
for i in range(0,n):
a=list(map(int,input().split()))
ans=2*(a[0]&1)+(a[1]&1)+1
print(ans)
``` | output | 1 | 91,335 | 23 | 182,671 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other.
Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible.
Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length
<image> The picture corresponds to the first example
Input
The first line contains single integer n (1 ≤ n ≤ 5·105) — the number of rectangles.
n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≤ x1 < x2 ≤ 109, - 109 ≤ y1 < y2 ≤ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle.
It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other.
Output
Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color.
Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≤ ci ≤ 4) — the color of i-th rectangle.
Example
Input
8
0 0 5 3
2 -1 5 0
-3 -4 2 -1
-1 -1 2 0
-3 0 0 5
5 2 10 3
7 -3 10 2
4 -2 7 -1
Output
YES
1
2
2
3
2
2
4
1 | instruction | 0 | 91,336 | 23 | 182,672 |
Tags: constructive algorithms, geometry
Correct Solution:
```
print('YES')
for _ in range(int(input())):
a,b,c,d = map(int,input().split())
print((2*(a%2))+(b%2)+1)
``` | output | 1 | 91,336 | 23 | 182,673 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other.
Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible.
Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length
<image> The picture corresponds to the first example
Input
The first line contains single integer n (1 ≤ n ≤ 5·105) — the number of rectangles.
n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≤ x1 < x2 ≤ 109, - 109 ≤ y1 < y2 ≤ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle.
It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other.
Output
Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color.
Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≤ ci ≤ 4) — the color of i-th rectangle.
Example
Input
8
0 0 5 3
2 -1 5 0
-3 -4 2 -1
-1 -1 2 0
-3 0 0 5
5 2 10 3
7 -3 10 2
4 -2 7 -1
Output
YES
1
2
2
3
2
2
4
1 | instruction | 0 | 91,337 | 23 | 182,674 |
Tags: constructive algorithms, geometry
Correct Solution:
```
'''plan
noticed that if both upperle
'''
from sys import stdin, stdout
from itertools import islice
# n = int(stdin.readline().rstrip())
# n = int(input())
all_lines = stdin.read().split('\n')
stdout.write('YES\n')
for line in islice(all_lines, 1, len(all_lines) - 1):
x1, y1, x2, y2 = (int(x) % 2 for x in line.split())
num = 2 * x2 + y2 + 1
stdout.write(str(num) + '\n')
#print(num)
#stdout.flush()
#exit()
# for i in range(n):
# coordinates.append([int(x) % 2 for x in input().split()])
# for i in range(n):
# coordinates.append([int(x) % 2 for x in stdin.readline().rstrip().split()])
# stdout.write('YES\n')
# for coordinate in coordinates:
# x1, y1, x2, y2 = coordinate
# stdout.write(str(2 * x2 + y2 + 1) + '\n')
``` | output | 1 | 91,337 | 23 | 182,675 |
Provide tags and a correct Python 3 solution for this coding contest problem.
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other.
Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible.
Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length
<image> The picture corresponds to the first example
Input
The first line contains single integer n (1 ≤ n ≤ 5·105) — the number of rectangles.
n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≤ x1 < x2 ≤ 109, - 109 ≤ y1 < y2 ≤ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle.
It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other.
Output
Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color.
Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≤ ci ≤ 4) — the color of i-th rectangle.
Example
Input
8
0 0 5 3
2 -1 5 0
-3 -4 2 -1
-1 -1 2 0
-3 0 0 5
5 2 10 3
7 -3 10 2
4 -2 7 -1
Output
YES
1
2
2
3
2
2
4
1 | instruction | 0 | 91,338 | 23 | 182,676 |
Tags: constructive algorithms, geometry
Correct Solution:
```
'''plan
noticed that if both upperle
'''
from sys import stdin, stdout
# n = int(stdin.readline().rstrip())
# n = int(input())
all_lines = stdin.read().split('\n')
stdout.write('YES\n')
for line in all_lines[1:-1]:
x1, y1, x2, y2 = (int(x) % 2 for x in line.split())
num = 2 * x2 + y2 + 1
# stdout.write(str(x2) + ' ' + str(y2) + '\n')
print(str(num) + '\n')
#stdout.flush()
#exit()
# for i in range(n):
# coordinates.append([int(x) % 2 for x in input().split()])
# for i in range(n):
# coordinates.append([int(x) % 2 for x in stdin.readline().rstrip().split()])
# stdout.write('YES\n')
# for coordinate in coordinates:
# x1, y1, x2, y2 = coordinate
# stdout.write(str(2 * x2 + y2 + 1) + '\n')
``` | output | 1 | 91,338 | 23 | 182,677 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.