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 tags and a correct Python 3 solution for this coding contest problem.
There is a square painted on a piece of paper, the square's side equals n meters. John Doe draws crosses on the square's perimeter. John paints the first cross in the lower left corner of the square. Then John moves along the square's perimeter in the clockwise direction (first upwards, then to the right, then downwards, then to the left and so on). Every time he walks (n + 1) meters, he draws a cross (see picture for clarifications).
John Doe stops only when the lower left corner of the square has two crosses. How many crosses will John draw?
<image> The figure shows the order in which John draws crosses for a square with side 4. The lower left square has two crosses. Overall John paints 17 crosses.
Input
The first line contains integer t (1 ≤ t ≤ 104) — the number of test cases.
The second line contains t space-separated integers ni (1 ≤ ni ≤ 109) — the sides of the square for each test sample.
Output
For each test sample print on a single line the answer to it, that is, the number of crosses John will draw as he will move along the square of the corresponding size. Print the answers to the samples in the order in which the samples are given in the input.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
4 8 100
Output
17
33
401 | instruction | 0 | 60,957 | 23 | 121,914 |
Tags: math
Correct Solution:
```
def GCD(a, b):
if(b == 0):
return a
else:
return GCD(b, a % b)
#
t = int(input())
a = input().split()
for i in range(0, t):
print(int(4 * int(a[i]) / GCD(4 * int(a[i]), int(a[i]) + 1) + 1))
``` | output | 1 | 60,957 | 23 | 121,915 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a square painted on a piece of paper, the square's side equals n meters. John Doe draws crosses on the square's perimeter. John paints the first cross in the lower left corner of the square. Then John moves along the square's perimeter in the clockwise direction (first upwards, then to the right, then downwards, then to the left and so on). Every time he walks (n + 1) meters, he draws a cross (see picture for clarifications).
John Doe stops only when the lower left corner of the square has two crosses. How many crosses will John draw?
<image> The figure shows the order in which John draws crosses for a square with side 4. The lower left square has two crosses. Overall John paints 17 crosses.
Input
The first line contains integer t (1 ≤ t ≤ 104) — the number of test cases.
The second line contains t space-separated integers ni (1 ≤ ni ≤ 109) — the sides of the square for each test sample.
Output
For each test sample print on a single line the answer to it, that is, the number of crosses John will draw as he will move along the square of the corresponding size. Print the answers to the samples in the order in which the samples are given in the input.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
4 8 100
Output
17
33
401 | instruction | 0 | 60,958 | 23 | 121,916 |
Tags: math
Correct Solution:
```
from math import gcd
n = int(input())
arr = input().split()
res = [4*int(i)//gcd(4*int(i), int(i) + 1) + 1 for i in arr]
for i in res:
print(i)
``` | output | 1 | 60,958 | 23 | 121,917 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a square painted on a piece of paper, the square's side equals n meters. John Doe draws crosses on the square's perimeter. John paints the first cross in the lower left corner of the square. Then John moves along the square's perimeter in the clockwise direction (first upwards, then to the right, then downwards, then to the left and so on). Every time he walks (n + 1) meters, he draws a cross (see picture for clarifications).
John Doe stops only when the lower left corner of the square has two crosses. How many crosses will John draw?
<image> The figure shows the order in which John draws crosses for a square with side 4. The lower left square has two crosses. Overall John paints 17 crosses.
Input
The first line contains integer t (1 ≤ t ≤ 104) — the number of test cases.
The second line contains t space-separated integers ni (1 ≤ ni ≤ 109) — the sides of the square for each test sample.
Output
For each test sample print on a single line the answer to it, that is, the number of crosses John will draw as he will move along the square of the corresponding size. Print the answers to the samples in the order in which the samples are given in the input.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
4 8 100
Output
17
33
401 | instruction | 0 | 60,959 | 23 | 121,918 |
Tags: math
Correct Solution:
```
import math
n = int(input())
a = [int(i) for i in input().split()]
for i in a:
print(1 + ((4*i*(i+1))//math.gcd(4*i, i+1))//(i+1))
``` | output | 1 | 60,959 | 23 | 121,919 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There is a square painted on a piece of paper, the square's side equals n meters. John Doe draws crosses on the square's perimeter. John paints the first cross in the lower left corner of the square. Then John moves along the square's perimeter in the clockwise direction (first upwards, then to the right, then downwards, then to the left and so on). Every time he walks (n + 1) meters, he draws a cross (see picture for clarifications).
John Doe stops only when the lower left corner of the square has two crosses. How many crosses will John draw?
<image> The figure shows the order in which John draws crosses for a square with side 4. The lower left square has two crosses. Overall John paints 17 crosses.
Input
The first line contains integer t (1 ≤ t ≤ 104) — the number of test cases.
The second line contains t space-separated integers ni (1 ≤ ni ≤ 109) — the sides of the square for each test sample.
Output
For each test sample print on a single line the answer to it, that is, the number of crosses John will draw as he will move along the square of the corresponding size. Print the answers to the samples in the order in which the samples are given in the input.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
4 8 100
Output
17
33
401 | instruction | 0 | 60,960 | 23 | 121,920 |
Tags: math
Correct Solution:
```
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
n = int(input())
a = list(map(int, input().split()))
for i in a:
temp_gcd = gcd(4 * i, i + 1)
print(4 * i // temp_gcd + 1)
``` | output | 1 | 60,960 | 23 | 121,921 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a square painted on a piece of paper, the square's side equals n meters. John Doe draws crosses on the square's perimeter. John paints the first cross in the lower left corner of the square. Then John moves along the square's perimeter in the clockwise direction (first upwards, then to the right, then downwards, then to the left and so on). Every time he walks (n + 1) meters, he draws a cross (see picture for clarifications).
John Doe stops only when the lower left corner of the square has two crosses. How many crosses will John draw?
<image> The figure shows the order in which John draws crosses for a square with side 4. The lower left square has two crosses. Overall John paints 17 crosses.
Input
The first line contains integer t (1 ≤ t ≤ 104) — the number of test cases.
The second line contains t space-separated integers ni (1 ≤ ni ≤ 109) — the sides of the square for each test sample.
Output
For each test sample print on a single line the answer to it, that is, the number of crosses John will draw as he will move along the square of the corresponding size. Print the answers to the samples in the order in which the samples are given in the input.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
4 8 100
Output
17
33
401
Submitted Solution:
```
from math import gcd
t=int(input())
s=[int(i) for i in input().split()]
for i in range(t):
l=s[i]
print((((l+1)*4*l)//gcd((l+1),4*l))//(l+1)+1)
``` | instruction | 0 | 60,961 | 23 | 121,922 |
Yes | output | 1 | 60,961 | 23 | 121,923 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a square painted on a piece of paper, the square's side equals n meters. John Doe draws crosses on the square's perimeter. John paints the first cross in the lower left corner of the square. Then John moves along the square's perimeter in the clockwise direction (first upwards, then to the right, then downwards, then to the left and so on). Every time he walks (n + 1) meters, he draws a cross (see picture for clarifications).
John Doe stops only when the lower left corner of the square has two crosses. How many crosses will John draw?
<image> The figure shows the order in which John draws crosses for a square with side 4. The lower left square has two crosses. Overall John paints 17 crosses.
Input
The first line contains integer t (1 ≤ t ≤ 104) — the number of test cases.
The second line contains t space-separated integers ni (1 ≤ ni ≤ 109) — the sides of the square for each test sample.
Output
For each test sample print on a single line the answer to it, that is, the number of crosses John will draw as he will move along the square of the corresponding size. Print the answers to the samples in the order in which the samples are given in the input.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
4 8 100
Output
17
33
401
Submitted Solution:
```
i = int(input())
x = list(map(int, input().split(' ')))
for i in x:
print([4*i+1, 2*i+1, 4*i+1, i+1][i%4])
# Made By Mostafa_Khaled
``` | instruction | 0 | 60,962 | 23 | 121,924 |
Yes | output | 1 | 60,962 | 23 | 121,925 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a square painted on a piece of paper, the square's side equals n meters. John Doe draws crosses on the square's perimeter. John paints the first cross in the lower left corner of the square. Then John moves along the square's perimeter in the clockwise direction (first upwards, then to the right, then downwards, then to the left and so on). Every time he walks (n + 1) meters, he draws a cross (see picture for clarifications).
John Doe stops only when the lower left corner of the square has two crosses. How many crosses will John draw?
<image> The figure shows the order in which John draws crosses for a square with side 4. The lower left square has two crosses. Overall John paints 17 crosses.
Input
The first line contains integer t (1 ≤ t ≤ 104) — the number of test cases.
The second line contains t space-separated integers ni (1 ≤ ni ≤ 109) — the sides of the square for each test sample.
Output
For each test sample print on a single line the answer to it, that is, the number of crosses John will draw as he will move along the square of the corresponding size. Print the answers to the samples in the order in which the samples are given in the input.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
4 8 100
Output
17
33
401
Submitted Solution:
```
import math
n = int(input())
a = list(map(int,input().split()))
for i in a:
print(1 + ((4 * i * (i + 1)) // math.gcd(4 * i ,i + 1)) // (i + 1))
``` | instruction | 0 | 60,963 | 23 | 121,926 |
Yes | output | 1 | 60,963 | 23 | 121,927 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a square painted on a piece of paper, the square's side equals n meters. John Doe draws crosses on the square's perimeter. John paints the first cross in the lower left corner of the square. Then John moves along the square's perimeter in the clockwise direction (first upwards, then to the right, then downwards, then to the left and so on). Every time he walks (n + 1) meters, he draws a cross (see picture for clarifications).
John Doe stops only when the lower left corner of the square has two crosses. How many crosses will John draw?
<image> The figure shows the order in which John draws crosses for a square with side 4. The lower left square has two crosses. Overall John paints 17 crosses.
Input
The first line contains integer t (1 ≤ t ≤ 104) — the number of test cases.
The second line contains t space-separated integers ni (1 ≤ ni ≤ 109) — the sides of the square for each test sample.
Output
For each test sample print on a single line the answer to it, that is, the number of crosses John will draw as he will move along the square of the corresponding size. Print the answers to the samples in the order in which the samples are given in the input.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
4 8 100
Output
17
33
401
Submitted Solution:
```
def main():
T = int(input())
a = map(int, input().split())
for n in a:
for opt in (n, 2 * n, 4 * n):
if opt * (n + 1) % (4 * n) == 0:
print(opt + 1)
break
main()
``` | instruction | 0 | 60,964 | 23 | 121,928 |
Yes | output | 1 | 60,964 | 23 | 121,929 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a square painted on a piece of paper, the square's side equals n meters. John Doe draws crosses on the square's perimeter. John paints the first cross in the lower left corner of the square. Then John moves along the square's perimeter in the clockwise direction (first upwards, then to the right, then downwards, then to the left and so on). Every time he walks (n + 1) meters, he draws a cross (see picture for clarifications).
John Doe stops only when the lower left corner of the square has two crosses. How many crosses will John draw?
<image> The figure shows the order in which John draws crosses for a square with side 4. The lower left square has two crosses. Overall John paints 17 crosses.
Input
The first line contains integer t (1 ≤ t ≤ 104) — the number of test cases.
The second line contains t space-separated integers ni (1 ≤ ni ≤ 109) — the sides of the square for each test sample.
Output
For each test sample print on a single line the answer to it, that is, the number of crosses John will draw as he will move along the square of the corresponding size. Print the answers to the samples in the order in which the samples are given in the input.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
4 8 100
Output
17
33
401
Submitted Solution:
```
def readln(): return tuple(map(int, input().split()))
t, = readln()
for n in readln():
print(4 * n + 1)
``` | instruction | 0 | 60,965 | 23 | 121,930 |
No | output | 1 | 60,965 | 23 | 121,931 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a square painted on a piece of paper, the square's side equals n meters. John Doe draws crosses on the square's perimeter. John paints the first cross in the lower left corner of the square. Then John moves along the square's perimeter in the clockwise direction (first upwards, then to the right, then downwards, then to the left and so on). Every time he walks (n + 1) meters, he draws a cross (see picture for clarifications).
John Doe stops only when the lower left corner of the square has two crosses. How many crosses will John draw?
<image> The figure shows the order in which John draws crosses for a square with side 4. The lower left square has two crosses. Overall John paints 17 crosses.
Input
The first line contains integer t (1 ≤ t ≤ 104) — the number of test cases.
The second line contains t space-separated integers ni (1 ≤ ni ≤ 109) — the sides of the square for each test sample.
Output
For each test sample print on a single line the answer to it, that is, the number of crosses John will draw as he will move along the square of the corresponding size. Print the answers to the samples in the order in which the samples are given in the input.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
4 8 100
Output
17
33
401
Submitted Solution:
```
def readln(): return tuple(map(int, input().split()))
t, = readln()
for n in readln():
if n % 4 in (0, 2):
print(4 * n + 1)
elif n % 4 in (1, 4):
print(2 * n + 1)
``` | instruction | 0 | 60,966 | 23 | 121,932 |
No | output | 1 | 60,966 | 23 | 121,933 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a square painted on a piece of paper, the square's side equals n meters. John Doe draws crosses on the square's perimeter. John paints the first cross in the lower left corner of the square. Then John moves along the square's perimeter in the clockwise direction (first upwards, then to the right, then downwards, then to the left and so on). Every time he walks (n + 1) meters, he draws a cross (see picture for clarifications).
John Doe stops only when the lower left corner of the square has two crosses. How many crosses will John draw?
<image> The figure shows the order in which John draws crosses for a square with side 4. The lower left square has two crosses. Overall John paints 17 crosses.
Input
The first line contains integer t (1 ≤ t ≤ 104) — the number of test cases.
The second line contains t space-separated integers ni (1 ≤ ni ≤ 109) — the sides of the square for each test sample.
Output
For each test sample print on a single line the answer to it, that is, the number of crosses John will draw as he will move along the square of the corresponding size. Print the answers to the samples in the order in which the samples are given in the input.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
4 8 100
Output
17
33
401
Submitted Solution:
```
def readln(): return tuple(map(int, input().split()))
t, = readln()
for n in readln():
if n % 4 in (0, 2):
print(4 * n + 1)
elif n % 4 in (1, 3):
print(2 * n + 1)
``` | instruction | 0 | 60,967 | 23 | 121,934 |
No | output | 1 | 60,967 | 23 | 121,935 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a square painted on a piece of paper, the square's side equals n meters. John Doe draws crosses on the square's perimeter. John paints the first cross in the lower left corner of the square. Then John moves along the square's perimeter in the clockwise direction (first upwards, then to the right, then downwards, then to the left and so on). Every time he walks (n + 1) meters, he draws a cross (see picture for clarifications).
John Doe stops only when the lower left corner of the square has two crosses. How many crosses will John draw?
<image> The figure shows the order in which John draws crosses for a square with side 4. The lower left square has two crosses. Overall John paints 17 crosses.
Input
The first line contains integer t (1 ≤ t ≤ 104) — the number of test cases.
The second line contains t space-separated integers ni (1 ≤ ni ≤ 109) — the sides of the square for each test sample.
Output
For each test sample print on a single line the answer to it, that is, the number of crosses John will draw as he will move along the square of the corresponding size. Print the answers to the samples in the order in which the samples are given in the input.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
3
4 8 100
Output
17
33
401
Submitted Solution:
```
n = int(input())
lst = [int(i) for i in input().split()]
for i in range(n):
print(4*lst[i]+1)
``` | instruction | 0 | 60,968 | 23 | 121,936 |
No | output | 1 | 60,968 | 23 | 121,937 |
Provide a correct Python 3 solution for this coding contest problem.
For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xpi, ypi ≤ 10000
* p0 ≠ p1 and p2 ≠ p3.
Input
The entire input looks like:
q (the number of queries)
1st query
2nd query
...
qth query
Each query consists of integer coordinates of end points of s1 and s2 in the following format:
xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3
Output
For each query, print "1" or "0".
Example
Input
3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0
Output
1
1
0 | instruction | 0 | 61,443 | 23 | 122,886 |
"Correct Solution:
```
from math import pi, cos, sin, atan2
EPS = 10**(-9)
def eq(value1, value2):
return abs(value1-value2) <= EPS
class Point():
def __init__(self, x, y):
self.x = x
self.y = y
self.arg = atan2(y, x) # -PI ~ PI
def __str__(self):
return "{0:.8f} {1:.8f}".format(self.x, self.y)
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Point(self.x - other.x, self.y - other.y)
def __mul__(self, scal):
return Point(self.x*scal, self.y*scal)
def __truediv__(self, scal):
return Point(self.x/scal, self.y/scal)
def __eq__(self, other):
return eq(self.x, other.x) and eq(self.y, other.y)
# 原点からの距離
def __abs__(self):
return (self.x**2+self.y**2)**0.5
# 原点を中心にrad角だけ回転した点
def Rotation(vec: Point, rad):
return Point(vec.x*cos(rad)-vec.y*sin(rad), vec.x*sin(rad)+vec.y*cos(rad))
class Circle():
def __init__(self, p, r):
self.p = p
self.r = r
class Line():
# 点a, bを通る
def __init__(self, a, b):
self.a = a
self.b = b
self.arg = (a-b).arg % pi
def __str__(self):
return "[({0}, {1}) - ({2}, {3})]".format(self.a.x, self.a.y, self.b.x, self.b.y)
# pointを通って平行
def par(self, point):
return Line(point, point+(self.a-self.b))
# pointを通って垂直
def tan(self, point):
return Line(point, point + Rotation(self.a-self.b, pi/2))
class Segment(Line):
def __init__(self, a, b):
super().__init__(a, b)
# 符号付き面積
def cross(vec1: Point, vec2: Point):
return vec1.x*vec2.y - vec1.y*vec2.x
# 内積
def dot(vec1: Point, vec2: Point):
return vec1.x*vec2.x + vec1.y*vec2.y
# 点a->b->cの回転方向
def ccw(a, b, c):
if cross(b-a, c-a) > EPS: return +1 # COUNTER_CLOCKWISE
if cross(b-a, c-a) < -EPS: return -1 # CLOCKWISE
if dot(c-a, b-a) < -EPS: return +2 # c -> a -> b
if abs(b-a) < abs(c-a): return -2 # a -> b -> c
return 0 # a -> c -> b
# pのlへの射影
def projection(l, p):
t = dot(l.b-l.a, p-l.a) / abs(l.a-l.b)**2
return l.a + (l.b-l.a)*t
# pのlによる反射
def reflection(l, p):
return p + (projection(l, p) - p)*2
def isPararell(l1, l2):
return eq(cross(l1.a-l1.b, l2.a-l2.b), 0)
def isVertical(l1, l2):
return eq(dot(l1.a-l1.b, l2.a-l2.b), 0)
def isIntersect_lp(l, p):
return abs(ccw(l.a, l.b, p)) != 1
def isIntersect_ll(l1, l2):
return not isPararell(l1, l2) or isIntersect_lp(l1, l2.a)
def isIntersect_sp(s, p):
return ccw(s.a, s.b, p) == 0
def isIntersect_ss(s1, s2):
return ccw(s1.a, s1.b, s2.a)*ccw(s1.a, s1.b, s2.b) <= 0 and ccw(s2.a, s2.b, s1.a)*ccw(s2.a, s2.b, s1.b) <= 0
def isIntersect_ls(l, s):
return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS
def isIntersect_cp(c, p):
return abs(abs(c.p - p) - c.r) < EPS
def isIntersect_cl(c, l):
return distance_lp(l, c.p) <= c.r + EPS
def isIntersect_cs(c, s):
pass
def isIntersect_cc(c1, c2):
pass
def distance_pp(p1, p2):
return abs(p1-p2)
def distance_lp(l, p):
return abs(projection(l,p)-p)
def distance_ll(l1, l2):
return 0 if isIntersect_ll(l1, l2) else distance_lp(l1, l2.a)
def distance_sp(s, p):
r = projection(s, p)
if isIntersect_sp(s, r): return abs(r-p)
return min(abs(s.a-p), abs(s.b-p))
def distance_ss(s1, s2):
if isIntersect_ss(s1, s2): return 0
return min([distance_sp(s1, s2.a), distance_sp(s1, s2.b), distance_sp(s2, s1.a), distance_sp(s2, s1.b)])
def distance_ls(l, s):
if isIntersect_ls(l, s): return 0
return min(distance_lp(l, s.a), distance_lp(l, s.b))
def crosspoint_ll(l1, l2):
A = cross(l1.b - l1.a, l2.b - l2.a)
B = cross(l1.b - l1.a, l1.b - l2.a)
if eq(abs(A), 0) and eq(abs(B), 0): return l2.a
return l2.a + (l2.b - l2.a) * B / A
def crosspoint_ss(s1, s2):
return crosspoint_ll(s1, s2)
def crosspoint_lc(l, c):
if eq(distance_lp(l, c.p), c.r): return [c.p]
p = projection(l, c.p)
e = (l.b - l.a) / abs(l.b-l.a)
dis = (c.r**2-abs(p-c.p)**2)**0.5
return [p + e*dis, p - e*dis]
def crosspoint_sc(s, c):
pass
def crosspoint_cc(c1, c2):
d = abs(c1.p-c2.p)
if not abs(c1.r-c2.r) <= d <= c1.r+c2.r:
return []
mid_p = (c2.p * (c1.r**2-c2.r**2+d**2) + c1.p * (c2.r**2-c1.r**2+d**2)) / (2*d**2)
tanvec = Rotation(c1.p-c2.p, pi/2)
return crosspoint_lc(Line(mid_p, mid_p+tanvec), c1)
# pからのcの接点
def tangent_cp(c, p):
return crosspoint_cc(c, Circle(p, (abs(p-c.p)**2 - c.r**2)**0.5))
import sys
input = sys.stdin.readline
def verify_1A():
p1x, p1y, p2x, p2y = map(int, input().split())
l = Line(Point(p1x, p1y), Point(p2x, p2y))
Q = int(input())
Query = [list(map(int, input().split())) for _ in range(Q)]
for px, py in Query:
p = Point(px, py)
print(projection(l, p))
def verify_1B():
p1x, p1y, p2x, p2y = map(int, input().split())
l = Line(Point(p1x, p1y), Point(p2x, p2y))
Q = int(input())
Query = [list(map(int, input().split())) for _ in range(Q)]
for px, py in Query:
p = Point(px, py)
print(reflection(l, p))
def verify_1C():
p1x, p1y, p2x, p2y = map(int, input().split())
p1 = Point(p1x, p1y); p2 = Point(p2x, p2y)
Q = int(input())
Query = [list(map(int, input().split())) for _ in range(Q)]
for px, py in Query:
p = Point(px, py)
result = ccw(p1, p2, p)
if result == 1:
print("COUNTER_CLOCKWISE")
elif result == -1:
print("CLOCKWISE")
elif result == 2:
print("ONLINE_BACK")
elif result == -2:
print("ONLINE_FRONT")
else:
print("ON_SEGMENT")
def verify_2A():
Q = int(input())
Query = [list(map(int, input().split())) for _ in range(Q)]
for p0x, p0y, p1x, p1y, p2x, p2y, p3x, p3y in Query:
l1 = Line(Point(p0x, p0y), Point(p1x, p1y))
l2 = Line(Point(p2x, p2y), Point(p3x, p3y))
if isPararell(l1, l2):
print(2)
elif isVertical(l1, l2):
print(1)
else:
print(0)
def verify_2B():
Q = int(input())
Query = [list(map(int, input().split())) for _ in range(Q)]
for p0x, p0y, p1x, p1y, p2x, p2y, p3x, p3y in Query:
s1 = Segment(Point(p0x, p0y), Point(p1x, p1y))
s2 = Segment(Point(p2x, p2y), Point(p3x, p3y))
if isIntersect_ss(s1, s2):
print(1)
else:
print(0)
verify_2B()
``` | output | 1 | 61,443 | 23 | 122,887 |
Provide a correct Python 3 solution for this coding contest problem.
For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xpi, ypi ≤ 10000
* p0 ≠ p1 and p2 ≠ p3.
Input
The entire input looks like:
q (the number of queries)
1st query
2nd query
...
qth query
Each query consists of integer coordinates of end points of s1 and s2 in the following format:
xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3
Output
For each query, print "1" or "0".
Example
Input
3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0
Output
1
1
0 | instruction | 0 | 61,444 | 23 | 122,888 |
"Correct Solution:
```
from typing import Tuple
def cross(a: Tuple[int, int], b: Tuple[int, int]) -> float:
return float(a[0] * b[1] - a[1] * b[0])
if __name__ == "__main__":
q = int(input())
for _ in range(q):
x_p0, y_p0, x_p1, y_p1, x_p2, y_p2, x_p3, y_p3 = map(lambda x: int(x),
input().split())
if x_p1 < x_p0:
x_p0, x_p1 = x_p1, x_p0
y_p0, y_p1 = y_p1, y_p0
if x_p3 < x_p2:
x_p2, x_p3 = x_p3, x_p2
y_p2, y_p3 = y_p3, y_p2
min_x1, max_x1 = x_p0, x_p1
min_y1, max_y1 = (y_p0, y_p1) if y_p0 < y_p1 else (y_p1, y_p0)
min_x2, max_x2 = x_p2, x_p3
min_y2, max_y2 = (y_p2, y_p3) if y_p2 < y_p3 else (y_p3, y_p2)
if any((max_x1 < min_x2, max_x2 < min_x1, max_y1 < min_y2, max_y2 < min_y1)):
print(0)
continue
s01 = (x_p1 - x_p0, y_p1 - y_p0)
s02 = (x_p2 - x_p0, y_p2 - y_p0)
s03 = (x_p3 - x_p0, y_p3 - y_p0)
s21 = (x_p1 - x_p2, y_p1 - y_p2)
s20 = (x_p0 - x_p2, y_p0 - y_p2)
s23 = (x_p3 - x_p2, y_p3 - y_p2)
print(int(cross(s01, s02) * cross(s01, s03) < 1e-6
and cross(s23, s20) * cross(s23, s21) < 1e-6))
``` | output | 1 | 61,444 | 23 | 122,889 |
Provide a correct Python 3 solution for this coding contest problem.
For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xpi, ypi ≤ 10000
* p0 ≠ p1 and p2 ≠ p3.
Input
The entire input looks like:
q (the number of queries)
1st query
2nd query
...
qth query
Each query consists of integer coordinates of end points of s1 and s2 in the following format:
xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3
Output
For each query, print "1" or "0".
Example
Input
3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0
Output
1
1
0 | instruction | 0 | 61,445 | 23 | 122,890 |
"Correct Solution:
```
import cmath
import math
import os
import sys
if os.getenv("LOCAL"):
sys.stdin = open("_in.txt", "r")
sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353
PI = cmath.pi
TAU = cmath.pi * 2
EPS = 1e-10
class Point:
"""
2次元空間上の点
"""
# 反時計回り側にある
CCW_COUNTER_CLOCKWISE = 1
# 時計回り側にある
CCW_CLOCKWISE = -1
# 線分の後ろにある
CCW_ONLINE_BACK = 2
# 線分の前にある
CCW_ONLINE_FRONT = -2
# 線分上にある
CCW_ON_SEGMENT = 0
def __init__(self, c: complex):
self.c = c
@property
def x(self):
return self.c.real
@property
def y(self):
return self.c.imag
@staticmethod
def from_rect(x: float, y: float):
return Point(complex(x, y))
@staticmethod
def from_polar(r: float, phi: float):
return Point(cmath.rect(r, phi))
def __add__(self, p):
"""
:param Point p:
"""
return Point(self.c + p.c)
def __iadd__(self, p):
"""
:param Point p:
"""
self.c += p.c
return self
def __sub__(self, p):
"""
:param Point p:
"""
return Point(self.c - p.c)
def __isub__(self, p):
"""
:param Point p:
"""
self.c -= p.c
return self
def __mul__(self, f: float):
return Point(self.c * f)
def __imul__(self, f: float):
self.c *= f
return self
def __truediv__(self, f: float):
return Point(self.c / f)
def __itruediv__(self, f: float):
self.c /= f
return self
def __repr__(self):
return "({}, {})".format(round(self.x, 10), round(self.y, 10))
def __neg__(self):
return Point(-self.c)
def __eq__(self, p):
return abs(self.c - p.c) < EPS
def __abs__(self):
return abs(self.c)
@staticmethod
def ccw(a, b, c):
"""
線分 ab に対する c の位置
線分上にあるか判定するだけなら on_segment とかのが速い
Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C&lang=ja
:param Point a:
:param Point b:
:param Point c:
"""
b = b - a
c = c - a
det = b.det(c)
if det > EPS:
return Point.CCW_COUNTER_CLOCKWISE
if det < -EPS:
return Point.CCW_CLOCKWISE
if b.dot(c) < -EPS:
return Point.CCW_ONLINE_BACK
if c.norm() - b.norm() > EPS:
return Point.CCW_ONLINE_FRONT
return Point.CCW_ON_SEGMENT
def dot(self, p):
"""
内積
:param Point p:
:rtype: float
"""
return self.x * p.x + self.y * p.y
def det(self, p):
"""
外積
:param Point p:
:rtype: float
"""
return self.x * p.y - self.y * p.x
def dist(self, p):
"""
距離
:param Point p:
:rtype: float
"""
return abs(self.c - p.c)
def norm(self):
"""
原点からの距離
:rtype: float
"""
return abs(self.c)
def phase(self):
"""
原点からの角度
:rtype: float
"""
return cmath.phase(self.c)
def angle(self, p, q):
"""
p に向いてる状態から q まで反時計回りに回転するときの角度
-pi <= ret <= pi
:param Point p:
:param Point q:
:rtype: float
"""
return (cmath.phase(q.c - self.c) - cmath.phase(p.c - self.c) + PI) % TAU - PI
def area(self, p, q):
"""
p, q となす三角形の面積
:param Point p:
:param Point q:
:rtype: float
"""
return abs((p - self).det(q - self) / 2)
def projection_point(self, p, q, allow_outer=False):
"""
線分 pq を通る直線上に垂線をおろしたときの足の座標
Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A&lang=ja
:param Point p:
:param Point q:
:param allow_outer: 答えが線分の間になくても OK
:rtype: Point|None
"""
diff_q = q - p
# 答えの p からの距離
r = (self - p).dot(diff_q) / abs(diff_q)
# 線分の角度
phase = diff_q.phase()
ret = Point.from_polar(r, phase) + p
if allow_outer or (p - ret).dot(q - ret) < EPS:
return ret
return None
def reflection_point(self, p, q):
"""
直線 pq を挟んで反対にある点
Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B&lang=ja
:param Point p:
:param Point q:
:rtype: Point
"""
# 距離
r = abs(self - p)
# pq と p-self の角度
angle = p.angle(q, self)
# 直線を挟んで角度を反対にする
angle = (q - p).phase() - angle
return Point.from_polar(r, angle) + p
def on_segment(self, p, q, allow_side=True):
"""
点が線分 pq の上に乗っているか
Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C&lang=ja
:param Point p:
:param Point q:
:param allow_side: 端っこでギリギリ触れているのを許容するか
:rtype: bool
"""
if not allow_side and (self == p or self == q):
return False
# 外積がゼロ: 面積がゼロ == 一直線
# 内積がマイナス: p - self - q の順に並んでる
return abs((p - self).det(q - self)) < EPS and (p - self).dot(q - self) < EPS
class Line:
"""
2次元空間上の直線
"""
def __init__(self, a: float, b: float, c: float):
"""
直線 ax + by + c = 0
"""
self.a = a
self.b = b
self.c = c
@staticmethod
def from_gradient(grad: float, intercept: float):
"""
直線 y = ax + b
:param grad: 傾き
:param intercept: 切片
:return:
"""
return Line(grad, -1, intercept)
@staticmethod
def from_segment(p1, p2):
"""
:param Point p1:
:param Point p2:
"""
a = p2.y - p1.y
b = p1.x - p2.x
c = p2.y * (p2.x - p1.x) - p2.x * (p2.y - p1.y)
return Line(a, b, c)
@property
def gradient(self):
"""
傾き
"""
return INF if self.b == 0 else -self.a / self.b
@property
def intercept(self):
"""
切片
"""
return INF if self.b == 0 else -self.c / self.b
def is_parallel_to(self, l):
"""
平行かどうか
Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A&lang=ja
:param Line l:
"""
# 法線ベクトル同士の外積がゼロ
return abs(Point.from_rect(self.a, self.b).det(Point.from_rect(l.a, l.b))) < EPS
def is_orthogonal_to(self, l):
"""
直行しているかどうか
Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A&lang=ja
:param Line l:
"""
# 法線ベクトル同士の内積がゼロ
return abs(Point.from_rect(self.a, self.b).dot(Point.from_rect(l.a, l.b))) < EPS
def intersection_point(self, l):
"""
交差する点
Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B&lang=ja
Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C&lang=ja
FIXME: 誤差が気になる。EPS <= 1e-9 だと CGL_2_B ダメだった。
:param Line l:
:rtype: Point|None
"""
a1, b1, c1 = self.a, self.b, self.c
a2, b2, c2 = l.a, l.b, l.c
det = a1 * b2 - a2 * b1
if abs(det) < EPS:
# 並行
return None
x = (b1 * c2 - b2 * c1) / det
y = (a2 * c1 - a1 * c2) / det
return Point.from_rect(x, y)
def dist(self, p):
"""
他の点との最短距離
:param Point p:
"""
raise NotImplementedError()
def has_point(self, p):
"""
p が直線上に乗っているかどうか
:param Point p:
"""
return abs(self.a * p.x + self.b * p.y + self.c) < EPS
class Segment:
"""
2次元空間上の線分
"""
def __init__(self, p1, p2):
"""
:param Point p1:
:param Point p2:
"""
self.p1 = p1
self.p2 = p2
def norm(self):
"""
線分の長さ
"""
return abs(self.p1 - self.p2)
def phase(self):
"""
p1 を原点としたときの p2 の角度
"""
return cmath.phase(self.p2 - self.p1)
def is_parallel_to(self, s):
"""
平行かどうか
Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A&lang=ja
:param Segment s:
:return:
"""
# 外積がゼロ
return abs((self.p1 - self.p2).det(s.p1 - s.p2)) < EPS
def is_orthogonal_to(self, s):
"""
直行しているかどうか
Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A&lang=ja
:param Segment s:
:return:
"""
# 内積がゼロ
return abs((self.p1 - self.p2).dot(s.p1 - s.p2)) < EPS
def intersects_with(self, s, allow_side=True):
"""
交差するかどうか
Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B&lang=ja
:param Segment s:
:param allow_side: 端っこでギリギリ触れているのを許容するか
"""
if self.is_parallel_to(s):
# 並行なら線分の端点がもう片方の線分の上にあるかどうか
return (s.p1.on_segment(self.p1, self.p2, allow_side) or
s.p2.on_segment(self.p1, self.p2, allow_side) or
self.p1.on_segment(s.p1, s.p2, allow_side) or
self.p2.on_segment(s.p1, s.p2, allow_side))
else:
# allow_side ならゼロを許容する
det_lower = EPS if allow_side else -EPS
ok = True
# self の両側に s.p1 と s.p2 があるか
ok &= (self.p2 - self.p1).det(s.p1 - self.p1) * (self.p2 - self.p1).det(s.p2 - self.p1) < det_lower
# s の両側に self.p1 と self.p2 があるか
ok &= (s.p2 - s.p1).det(self.p1 - s.p1) * (s.p2 - s.p1).det(self.p2 - s.p1) < det_lower
return ok
def closest_point(self, p):
"""
線分上の、p に最も近い点
:param Point p:
"""
# p からおろした垂線までの距離
d = (p - self.p1).dot(self.p2 - self.p1) / self.norm()
# p1 より前
if d < EPS:
return self.p1
# p2 より後
if -EPS < d - self.norm():
return self.p2
# 線分上
return Point.from_polar(d, (self.p2 - self.p1).phase()) + self.p1
def dist(self, p):
"""
他の点との最短距離
:param Point p:
"""
return abs(p - self.closest_point(p))
def dist_segment(self, s):
"""
他の線分との最短距離
Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D&lang=ja
:param Segment s:
"""
if self.intersects_with(s):
return 0.0
return min(
self.dist(s.p1),
self.dist(s.p2),
s.dist(self.p1),
s.dist(self.p2),
)
def has_point(self, p, allow_side=True):
"""
p が線分上に乗っているかどうか
:param Point p:
:param allow_side: 端っこでギリギリ触れているのを許容するか
"""
return p.on_segment(self.p1, self.p2, allow_side=allow_side)
class Polygon:
"""
2次元空間上の多角形
"""
def __init__(self, points):
"""
:param list of Point points:
"""
self.points = points
def iter2(self):
"""
隣り合う2点を順に返すイテレータ
:rtype: typing.Iterator[(Point, Point)]
"""
return zip(self.points, self.points[1:] + self.points[:1])
def iter3(self):
"""
隣り合う3点を順に返すイテレータ
:rtype: typing.Iterator[(Point, Point, Point)]
"""
return zip(self.points,
self.points[1:] + self.points[:1],
self.points[2:] + self.points[:2])
def area(self):
"""
面積
Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_A&lang=ja
"""
# 外積の和 / 2
dets = []
for p, q in self.iter2():
dets.append(p.det(q))
return abs(math.fsum(dets)) / 2
def is_convex(self, allow_straight=False, allow_collapsed=False):
"""
凸多角形かどうか
Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_B&lang=ja
:param allow_straight: 3点がまっすぐ並んでるのを許容するかどうか
:param allow_collapsed: 面積がゼロの場合を許容するか
"""
ccw = []
for a, b, c in self.iter3():
ccw.append(Point.ccw(a, b, c))
ccw = set(ccw)
if len(ccw) == 1:
if ccw == {Point.CCW_CLOCKWISE}:
return True
if ccw == {Point.CCW_COUNTER_CLOCKWISE}:
return True
if allow_straight and len(ccw) == 2:
if ccw == {Point.CCW_ONLINE_FRONT, Point.CCW_CLOCKWISE}:
return True
if ccw == {Point.CCW_ONLINE_FRONT, Point.CCW_COUNTER_CLOCKWISE}:
return True
if allow_collapsed and len(ccw) == 3:
return ccw == {Point.CCW_ONLINE_FRONT, Point.CCW_ONLINE_BACK, Point.CCW_ON_SEGMENT}
return False
def has_point_on_edge(self, p):
"""
指定した点が辺上にあるか
:param Point p:
:rtype: bool
"""
for a, b in self.iter2():
if p.on_segment(a, b):
return True
return False
def contains(self, p, allow_on_edge=True):
"""
指定した点を含むか
Winding Number Algorithm
https://www.nttpc.co.jp/technology/number_algorithm.html
Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_C&lang=ja
:param Point p:
:param bool allow_on_edge: 辺上の点を許容するか
"""
angles = []
for a, b in self.iter2():
if p.on_segment(a, b):
return allow_on_edge
angles.append(p.angle(a, b))
# 一周以上するなら含む
return abs(math.fsum(angles)) > EPS
@staticmethod
def convex_hull(points, allow_straight=False):
"""
凸包。x が最も小さい点のうち y が最も小さい点から反時計回り。
Graham Scan O(N log N)
Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A&lang=ja
:param list of Point points:
:param allow_straight: 3点がまっすぐ並んでるのを許容するかどうか
:rtype: list of Point
"""
points = points[:]
points.sort(key=lambda p: (p.x, p.y))
# allow_straight なら 0 を許容する
det_lower = -EPS if allow_straight else EPS
sz = 0
#: :type: list of (Point|None)
ret = [None] * (N * 2)
for p in points:
while sz > 1 and (ret[sz - 1] - ret[sz - 2]).det(p - ret[sz - 1]) < det_lower:
sz -= 1
ret[sz] = p
sz += 1
floor = sz
for p in reversed(points[:-1]):
while sz > floor and (ret[sz - 1] - ret[sz - 2]).det(p - ret[sz - 1]) < det_lower:
sz -= 1
ret[sz] = p
sz += 1
ret = ret[:sz - 1]
if allow_straight and len(ret) > len(points):
# allow_straight かつ全部一直線のときに二重にカウントしちゃう
ret = points
return ret
@staticmethod
def diameter(points):
"""
直径
凸包構築 O(N log N) + カリパー法 O(N)
Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_B&lang=ja
:param list of Point points:
"""
# 反時計回り
points = Polygon.convex_hull(points, allow_straight=False)
if len(points) == 1:
return 0.0
if len(points) == 2:
return abs(points[0] - points[1])
# x軸方向に最も遠い点対
si = points.index(min(points, key=lambda p: (p.x, p.y)))
sj = points.index(max(points, key=lambda p: (p.x, p.y)))
n = len(points)
ret = 0.0
# 半周回転
i, j = si, sj
while i != sj or j != si:
ret = max(ret, abs(points[i] - points[j]))
ni = (i + 1) % n
nj = (j + 1) % n
# 2つの辺が並行になる方向にずらす
if (points[ni] - points[i]).det(points[nj] - points[j]) > 0:
j = nj
else:
i = ni
return ret
def convex_cut_by_line(self, line_p1, line_p2):
"""
凸多角形を直線 line_p1-line_p2 でカットする。
凸じゃないといけません
Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_C&lang=ja
:param line_p1:
:param line_p2:
:return: (line_p1-line_p2 の左側の多角形, line_p1-line_p2 の右側の多角形)
:rtype: (Polygon|None, Polygon|None)
"""
n = len(self.points)
line = Line.from_segment(line_p1, line_p2)
# 直線と重なる点
on_line_points = []
for i, p in enumerate(self.points):
if line.has_point(p):
on_line_points.append(i)
# 辺が直線上にある
has_on_line_edge = False
if len(on_line_points) >= 3:
has_on_line_edge = True
elif len(on_line_points) == 2:
# 直線上にある点が隣り合ってる
has_on_line_edge = abs(on_line_points[0] - on_line_points[1]) in [1, n - 1]
# 辺が直線上にある場合、どっちか片方に全部ある
if has_on_line_edge:
for p in self.points:
ccw = Point.ccw(line_p1, line_p2, p)
if ccw == Point.CCW_COUNTER_CLOCKWISE:
return Polygon(self.points[:]), None
if ccw == Point.CCW_CLOCKWISE:
return None, Polygon(self.points[:])
ret_lefts = []
ret_rights = []
d = line_p2 - line_p1
for p, q in self.iter2():
det_p = d.det(p - line_p1)
det_q = d.det(q - line_p1)
if det_p > -EPS:
ret_lefts.append(p)
if det_p < EPS:
ret_rights.append(p)
# 外積の符号が違う == 直線の反対側にある場合は交点を追加
if det_p * det_q < -EPS:
intersection = line.intersection_point(Line.from_segment(p, q))
ret_lefts.append(intersection)
ret_rights.append(intersection)
# 点のみの場合を除いて返す
l = Polygon(ret_lefts) if len(ret_lefts) > 1 else None
r = Polygon(ret_rights) if len(ret_rights) > 1 else None
return l, r
Q = int(sys.stdin.buffer.readline())
ROWS = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(Q)]
for x0, y0, x1, y1, x2, y2, x3, y3 in ROWS:
s1 = Segment(Point.from_rect(x0, y0), Point.from_rect(x1, y1))
s2 = Segment(Point.from_rect(x2, y2), Point.from_rect(x3, y3))
print(int(s1.intersects_with(s2, allow_side=True)))
``` | output | 1 | 61,445 | 23 | 122,891 |
Provide a correct Python 3 solution for this coding contest problem.
For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xpi, ypi ≤ 10000
* p0 ≠ p1 and p2 ≠ p3.
Input
The entire input looks like:
q (the number of queries)
1st query
2nd query
...
qth query
Each query consists of integer coordinates of end points of s1 and s2 in the following format:
xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3
Output
For each query, print "1" or "0".
Example
Input
3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0
Output
1
1
0 | instruction | 0 | 61,446 | 23 | 122,892 |
"Correct Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
input:
3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0
output:
1
1
0
"""
import sys
EPS = 1e-9
def cross(a, b):
return a.real * b.imag - a.imag * b.real
def dot(a, b):
return a.real * b.real + a.imag * b.imag
def check_ccw(p0, p1, p2):
# flag = float('inf')
a, b = p1 - p0, p2 - p0
if cross(a, b) > EPS:
# print('COUNTER_CLOCKWISE')
flag = 1
elif cross(a, b) < -1 * EPS:
# print('CLOCKWISE')
flag = -1
elif dot(a, b) < -1 * EPS:
# print('ONLINE_BACK')
flag = 2
elif abs(a) < abs(b):
# print('ONLINE_FRONT')
flag = -2
else:
# print('ON_SEGMENT')
flag = 0
return flag
def check_intersection(_lines):
for line in _lines:
line = tuple(map(int, line))
p0, p1, p2, p3 = (x + y * 1j for x, y in zip(line[::2], line[1::2]))
flag = (check_ccw(p0, p1, p2) * check_ccw(p0, p1, p3) <= 0) and \
(check_ccw(p2, p3, p0) * check_ccw(p2, p3, p1) <= 0)
if flag:
print('1')
else:
print('0')
return None
if __name__ == '__main__':
_input = sys.stdin.readlines()
l_num = int(_input[0])
lines = map(lambda x: x.split(), _input[1:])
check_intersection(lines)
``` | output | 1 | 61,446 | 23 | 122,893 |
Provide a correct Python 3 solution for this coding contest problem.
For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xpi, ypi ≤ 10000
* p0 ≠ p1 and p2 ≠ p3.
Input
The entire input looks like:
q (the number of queries)
1st query
2nd query
...
qth query
Each query consists of integer coordinates of end points of s1 and s2 in the following format:
xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3
Output
For each query, print "1" or "0".
Example
Input
3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0
Output
1
1
0 | instruction | 0 | 61,447 | 23 | 122,894 |
"Correct Solution:
```
class Line:
def __init__(self,p1,p2):
if p1[1] < p2[1]:self.s=p2;self.e=p1
elif p1[1] > p2[1]:self.s=p1;self.e=p2
else:
if p1[0] < p2[0]:self.s=p1;self.e=p2
else:self.s=p2;self.e=p1
def dot(a,b):return a[0]*b[0] + a[1]*b[1]
def cross(a,b):return a[0]*b[1] - a[1]*b[0]
def dif(a,b):return [x-y for x,y in zip(a,b)]
def InterSection(l,m):
a = dif(l.e,l.s);b = dif(m.e,l.s);c = dif(m.s,l.s)
d = dif(m.e,m.s);e = dif(l.e,m.s);f = dif(l.s,m.s)
g = lambda a, b : cross(a,b)==0 and dot(a,b)>0 and dot(b,b)<dot(a,a)
if g(a,b) or g(a,c) or g(d,e) or g(d,f):return True
elif l.s == m.e or l.s == m.s or l.e == m.e or l.e == m.s:return True
elif cross(a,b) * cross(a,c) >= 0 or cross(d,e) * cross(d,f) >= 0:return False
else:return True
q = int(input())
for i in range(q):
x0,y0,x1,y1,x2,y2,x3,y3 = [int(i) for i in input().split()]
a = [x0,y0] ; b = [x1,y1] ; c = [x2,y2] ; d = [x3,y3]
l1 = Line(b,a) ; l2 = Line(d,c)
if InterSection(l1,l2):print(1)
else:print(0)
``` | output | 1 | 61,447 | 23 | 122,895 |
Provide a correct Python 3 solution for this coding contest problem.
For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xpi, ypi ≤ 10000
* p0 ≠ p1 and p2 ≠ p3.
Input
The entire input looks like:
q (the number of queries)
1st query
2nd query
...
qth query
Each query consists of integer coordinates of end points of s1 and s2 in the following format:
xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3
Output
For each query, print "1" or "0".
Example
Input
3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0
Output
1
1
0 | instruction | 0 | 61,448 | 23 | 122,896 |
"Correct Solution:
```
EPS = 1e-4
#外積
def OuterProduct(one, two):
tmp = one.conjugate() * two
return tmp.imag
#内積
def InnerProduct(one, two):
tmp = one.conjugate() * two
return tmp.real
#点が直線上にあるか
def IsOnLine(point, begin, end):
return abs(OuterProduct(begin-point, end-point)) <= EPS
#点が線分上にあるか
def IsOnSegment(point, begin, end):
if abs(OuterProduct(begin-point, end-point)) <= EPS and InnerProduct(begin-point, end-point) <= EPS:
return True
else:
return False
#3点が反時計回りか
#一直線上のときの例外処理できていない→とりあえずF
def CCW(p, q, r):
one, two = q-p, r-q
if OuterProduct(one, two) > -EPS:
return True
else:
return False
#線分どうし交叉
def Intersect_SS(b1, e1, b2, e2):
if (CCW(b1, e1, b2) != CCW(b1, e1, e2)) and (CCW(b2, e2, b1) != CCW(b2, e2, e1)):
return True
else:
return False
def solve(a, b, c, d):
if IsOnSegment(a, c, d) or IsOnSegment(b, c, d) or IsOnSegment(c, a, b) or IsOnSegment(d, a, b):
return 1
elif abs(OuterProduct(b-a, d-c)) <= EPS:
return 0
elif Intersect_SS(a, b, c, d):
return 1
else:
return 0
n = int(input())
for _ in range(n):
pp = list(map(int, input().split()))
p = [complex(pp[i], pp[i+1]) for i in range(0, 8, 2)]
print(solve(p[0], p[1], p[2], p[3]))
``` | output | 1 | 61,448 | 23 | 122,897 |
Provide a correct Python 3 solution for this coding contest problem.
For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xpi, ypi ≤ 10000
* p0 ≠ p1 and p2 ≠ p3.
Input
The entire input looks like:
q (the number of queries)
1st query
2nd query
...
qth query
Each query consists of integer coordinates of end points of s1 and s2 in the following format:
xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3
Output
For each query, print "1" or "0".
Example
Input
3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0
Output
1
1
0 | instruction | 0 | 61,449 | 23 | 122,898 |
"Correct Solution:
```
def cross(v1,v2):
return v1.real*v2.imag-v1.imag*v2.real
def isOnSegment(p1,p2,p):
return abs(p1-p)+abs(p2-p)-abs(p1-p2)<=1e-8
def isIntersection(p1,p2,p3,p4):
if cross(p2-p1,p4-p3)==0:
return any(isOnSegment(q1,q2,q3)for q1,q2,q3 in ((p1,p2,p3),(p1,p2,p4),(p3,p4,p1),(p3,p4,p2)))
return cross(p2-p1,p3-p1)*cross(p2-p1,p4-p1)<=1e-8>=cross(p4-p3,p1-p3)*cross(p4-p3,p2-p3)
import sys
input = sys.stdin.buffer.readline
ans=[]
for _ in range(int(input())):
m=map(int,input().split())
ans.append(int(isIntersection(*[complex(x,y)for x,y in zip(*[iter(m)]*2)])))
print("\n".join(map(str,ans)))
``` | output | 1 | 61,449 | 23 | 122,899 |
Provide a correct Python 3 solution for this coding contest problem.
For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xpi, ypi ≤ 10000
* p0 ≠ p1 and p2 ≠ p3.
Input
The entire input looks like:
q (the number of queries)
1st query
2nd query
...
qth query
Each query consists of integer coordinates of end points of s1 and s2 in the following format:
xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3
Output
For each query, print "1" or "0".
Example
Input
3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0
Output
1
1
0 | instruction | 0 | 61,450 | 23 | 122,900 |
"Correct Solution:
```
EPS = 10**(-9)
def is_equal(a,b):
return abs(a-b) < EPS
def norm(v,i=2):
import math
ret = 0
n = len(v)
for j in range(n):
ret += abs(v[j])**i
return math.pow(ret,1/i)
class Vector(list):
"""
ベクトルクラス
対応演算子
+ : ベクトル和
- : ベクトル差
* : スカラー倍、または内積
/ : スカラー除法
** : 外積
+= : ベクトル和
-= : ベクトル差
*= : スカラー倍
/= : スカラー除法
メソッド
self.norm(i) : L{i}ノルムを計算
"""
def __add__(self,other):
n = len(self)
ret = [0]*n
for i in range(n):
ret[i] = super().__getitem__(i) + other.__getitem__(i)
return self.__class__(ret)
def __radd__(self,other):
n = len(self)
ret = [0]*n
for i in range(n):
ret[i] = other.__getitem__(i) + super().__getitem__(i)
return self.__class__(ret)
def __iadd__(self, other):
n = len(self)
for i in range(n):
self[i] += other.__getitem__(i)
return self
def __sub__(self,others):
n = len(self)
ret = [0]*n
for i in range(n):
ret[i] = super().__getitem__(i) - others.__getitem__(i)
return self.__class__(ret)
def __iadd__(self, other):
n = len(self)
for i in range(n):
self[i] -= other.__getitem__(i)
return self
def __rsub__(self,others):
n = len(self)
ret = [0]*n
for i in range(n):
ret[i] = others.__getitem__(i) - super().__getitem__(i)
return self.__class__(ret)
def __mul__(self,other):
n = len(self)
if isinstance(other,list):
ret = 0
for i in range(n):
ret += super().__getitem__(i)*other.__getitem__(i)
return ret
else:
ret = [0]*n
for i in range(n):
ret[i] = super().__getitem__(i)*other
return self.__class__(ret)
def __rmul__(self,other):
n = len(self)
if isinstance(other,list):
ret = 0
for i in range(n):
ret += super().__getitem__(i)*other.__getitem__(i)
return ret
else:
ret = [0]*n
for i in range(n):
ret[i] = super().__getitem__(i)*other
return self.__class__(ret)
def __truediv__(self,other):
"""
ベクトルのスカラー除法
Vector/scalar
"""
n = len(self)
ret = [0]*n
for i in range(n):
ret[i] = super().__getitem__(i)/other
return self.__class__(ret)
def norm(self,i):
"""
L{i}ノルム
self.norm(i)
"""
return norm(self,i)
def __pow__(self,other):
"""
外積
self**other
"""
n = len(self)
ret = [0]*3
x = self[:]
y = other[:]
if n == 2:
x.append(0)
y.append(0)
if n == 2 or n == 3:
for i in range(3):
ret[0],ret[1],ret[2] = x[1]*y[2]-x[2]*y[1],x[2]*y[0]-x[0]*y[2],x[0]*y[1]-x[1]*y[0]
ret = Vector(ret)
if n == 2:
return ret
else:
return ret
class Segment:
"""
線分クラス
"""
def __init__(self,v1,v2):
self.v1 = v1
self.v2 = v2
def length(self):
return norm(self.v1-self.v2)
def get_unit_vec(self):
#方向単位ベクトル
dist = norm(self.v2-self.v1)
if dist != 0:
return (self.v2-self.v1)/dist
else:
return False
def projection(self,vector):
#射影点(線分を直線と見たときの)
unit_vec = self.get_unit_vec()
t = unit_vec*(vector-self.v1)
return self.v1 + t*unit_vec
def is_vertical(self,other):
#線分の直交判定
return is_equal(0,self.get_unit_vec()*other.get_unit_vec())
def is_horizontal(self,other):
#線分の平行判定
return is_equal(0,self.get_unit_vec()**other.get_unit_vec())
def reflection(self,vector):
#反射点(線分を直線と見たときの)
projection = self.projection(vector)
v = projection - vector
return projection + vector
def include(self,vector):
#線分が点を含むか否か
proj = self.projection(vector)
if not is_equal(norm(proj-vector),0):
return False
else:
n = len(self.v1)
f = True
for i in range(n):
f &= ((self.v1[i] <= vector[i] <= self.v2[i]) or (self.v2[i] <= vector[i] <=self.v1[i]))
return f
def distance(self,other):
#点と線分の距離
if isinstance(other,Vector):
proj = self.projection(other)
if self.include(proj):
return norm(proj-other)
ret = []
ret.append(norm(self.v1-other))
ret.append(norm(self.v2-other))
return min(ret)
def ccw(self,vector):
"""
線分に対して点が反時計回りの位置にある(1)か時計回りの位置にある(-1)か線分上にある(0)か
"""
direction = self.v2 - self.v1
v = vector - self.v1
if self.include(vector):
return 0
else:
cross = direction**v
if cross[2] <= 0:
return 1
else:
return -1
def intersect(self,segment):
ccw12 = self.ccw(segment.v1)
ccw13 = self.ccw(segment.v2)
ccw20 = segment.ccw(self.v1)
ccw21 = segment.ccw(self.v2)
if ccw12*ccw13*ccw20*ccw21 == 0:
return True
else:
if ccw12*ccw13 < 0 and ccw20*ccw21 < 0:
return True
else:
return False
class Line(Segment):
"""
直線クラス
"""
#直線上に点が存在するか否か
def include(self,vector):
proj = self.projection(vector)
return is_equal(norm(proj-vector),0)
q = int(input())
P0s,P1s,P2s,P3s = [0]*q,[0]*q,[0]*q,[0]*q
for i in range(q):
tmp = list(map(int, input().split()))
P0s[i],P1s[i],P2s[i],P3s[i] = Vector(tmp[0:2]),Vector(tmp[2:4]),Vector(tmp[4:6]),Vector(tmp[6:8])
for i in range(q):
p0,p1,p2,p3 = P0s[i],P1s[i],P2s[i],P3s[i]
S1 = Segment(p0,p1)
S2 = Segment(p2,p3)
if S1.intersect(S2):
print(1)
else:
print(0)
``` | output | 1 | 61,450 | 23 | 122,901 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xpi, ypi ≤ 10000
* p0 ≠ p1 and p2 ≠ p3.
Input
The entire input looks like:
q (the number of queries)
1st query
2nd query
...
qth query
Each query consists of integer coordinates of end points of s1 and s2 in the following format:
xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3
Output
For each query, print "1" or "0".
Example
Input
3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0
Output
1
1
0
Submitted Solution:
```
#! /usr/bin/env python3
from typing import List, Tuple
from math import sqrt
from enum import IntEnum
EPS = 1e-10
def float_equal(x: float, y: float) -> bool:
return abs(x - y) < EPS
class PointLocation(IntEnum):
COUNTER_CLOCKWISE = 1
CCW = 1
CLOCKWISE = -1
CW = -1
ONLINE_BACK = 2
O_B = 2
ONLINE_FRONT = -2
O_F = -2
ON_SEGMENT = 0
O_S = 0
PL = PointLocation
class Point:
def __init__(self, x: float=0.0, y: float=0.0) -> None:
self.x = x
self.y = y
def __repr__(self) -> str:
return "Point({}, {})".format(self.x, self.y)
def __eq__(self, other: object) -> bool:
if not isinstance(other, Point):
# print("NotImplemented in Point")
return NotImplemented
return float_equal(self.x, other.x) and \
float_equal(self.y, other.y)
def __add__(self, other: 'Point') -> 'Point':
return Point(self.x + other.x, self.y + other.y)
def __sub__(self, other: 'Point') -> 'Point':
return Point(self.x - other.x, self.y - other.y)
def __mul__(self, k: float) -> 'Point':
return Point(self.x * k, self.y * k)
def __rmul__(self, k: float) -> 'Point':
return self * k
def __truediv__(self, k: float) -> 'Point':
return Point(self.x / k, self.y / k)
def __lt__(self, other: 'Point') -> bool:
return self.y < other.y \
if abs(self.x - other.x) < EPS \
else self.x < other.x
def norm(self):
return self.x * self.x + self.y * self.y
def abs(self):
return sqrt(self.norm())
def dot(self, other: 'Point') -> float:
return self.x * other.x + self.y * other.y
def cross(self, other: 'Point') -> float:
return self.x * other.y - self.y * other.x
def is_orthogonal(self, other: 'Point') -> bool:
return float_equal(self.dot(other), 0.0)
def is_parallel(self, other: 'Point') -> bool:
return float_equal(self.cross(other), 0.0)
def distance(self, other: 'Point') -> float:
return (self - other).abs()
def in_side_of(self, seg: 'Segment') -> bool:
return seg.vector().dot(
Segment(seg.p1, self).vector()) >= 0
def in_width_of(self, seg: 'Segment') -> bool:
return \
self.in_side_of(seg) and \
self.in_side_of(seg.reverse())
def distance_to_line(self, seg: 'Segment') -> float:
return \
abs((self - seg.p1).cross(seg.vector())) / \
seg.length()
def distance_to_segment(self, seg: 'Segment') -> float:
if not self.in_side_of(seg):
return self.distance(seg.p1)
if not self.in_side_of(seg.reverse()):
return self.distance(seg.p2)
else:
return self.distance_to_line(seg)
def location(self, seg: 'Segment') -> PointLocation:
p = self - seg.p1
d = seg.vector().cross(p)
if d > EPS:
return PointLocation.COUNTER_CLOCKWISE
if d < -EPS:
return PointLocation.CLOCKWISE
if seg.vector().dot(p) < 0.0:
return PointLocation.ONLINE_BACK
if seg.vector().norm() < p.norm():
return PointLocation.ONLINE_FRONT
return PointLocation.ON_SEGMENT
Vector = Point
class Segment:
def __init__(self, p1: Point = None, p2: Point = None) -> None:
self.p1: Point = Point() if p1 is None else p1
self.p2: Point = Point() if p2 is None else p2
def __repr__(self) -> str:
return "Segment({}, {})".format(self.p1, self.p2)
def __eq__(self, other: object) -> bool:
if not isinstance(other, Segment):
# print("NotImplemented in Segment")
return NotImplemented
return self.p1 == other.p1 and self.p2 == other.p2
def vector(self) -> Vector:
return self.p2 - self.p1
def reverse(self) -> 'Segment':
return Segment(self.p2, self.p1)
def length(self) -> float:
return self.p1.distance(self.p2)
def is_orthogonal(self, other: 'Segment') -> bool:
return self.vector().is_orthogonal(other.vector())
def is_parallel(self, other: 'Segment') -> bool:
return self.vector().is_parallel(other.vector())
def projection(self, p: Point) -> Point:
v = self.vector()
vp = p - self.p1
return v.dot(vp) / v.norm() * v + self.p1
def reflection(self, p: Point) -> Point:
x = self.projection(p)
return p + 2 * (x - p)
def intersect_ratio(self, other: 'Segment') -> Tuple[float, float]:
a = self.vector()
b = other.vector()
c = self.p1 - other.p1
s = b.cross(c) / a.cross(b)
t = a.cross(c) / a.cross(b)
return s, t
def intersects(self, other: 'Segment') -> bool:
d0 = self.p1.location(other)
d1 = self.p2.location(other)
d2 = other.p1.location(self)
d3 = other.p2.location(self)
return d0 * d1 * d2 * d3 == 0 or \
(d0 * d1 == -1 and d2 * d3 == -1)
def intersection(self, other: 'Segment') -> Point:
s, _ = self.intersect_ratio(other)
return self.p1 + s * self.vector()
def distance_with_segment(self, other: 'Segment') -> float:
if not self.is_parallel(other) and \
self.intersects(other):
return 0
else:
return min(
self.p1.distance_to_segment(other),
self.p2.distance_to_segment(other),
other.p1.distance_to_segment(self),
other.p2.distance_to_segment(self))
Line = Segment
class Circle:
def __init__(self, c: Point=None, r: float=0.0) -> None:
self.c: Point = Point() if c is None else c
self.r: float = r
def __eq__(self, other: object) -> bool:
if not isinstance(other, Circle):
return NotImplemented
return self.c == other.c and self.r == other.r
def __repr__(self) -> str:
return "Circle({}, {})".format(self.c, self.r)
def main() -> None:
q = int(input())
for _ in range(q):
x0, y0, x1, y1, x2, y2, x3, y3 = \
[int(x) for x in input().split()]
s1 = Segment(Point(x0, y0), Point(x1, y1))
s2 = Segment(Point(x2, y2), Point(x3, y3))
print(1 if s1.intersects(s2) else 0)
if __name__ == "__main__":
main()
``` | instruction | 0 | 61,451 | 23 | 122,902 |
Yes | output | 1 | 61,451 | 23 | 122,903 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xpi, ypi ≤ 10000
* p0 ≠ p1 and p2 ≠ p3.
Input
The entire input looks like:
q (the number of queries)
1st query
2nd query
...
qth query
Each query consists of integer coordinates of end points of s1 and s2 in the following format:
xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3
Output
For each query, print "1" or "0".
Example
Input
3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0
Output
1
1
0
Submitted Solution:
```
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())
###########################
# 幾何
###########################
def sgn(a):
if a < -eps: return -1
if a > eps: return 1
return 0
class Point:
def __init__(self,x,y):
self.x = x
self.y = y
pass
def tolist(self):
return [self.x,self.y]
def __add__(self,p):
return Point(self.x+p.x, self.y+p.y)
def __iadd__(self,p):
return self + p
def __sub__(self,p):
return Point(self.x - p.x, self.y - p.y)
def __isub__(self,p):
return self - p
def __truediv__(self,n):
return Point(self.x/n, self.y/n)
def __itruediv__(self,n):
return self / n
def __mul__(self,n):
return Point(self.x*n, self.y*n)
def __imul__(self,n):
return self * n
def __lt__(self,other):
tmp = sgn(self.x - other.x)
if tmp != 0:
return tmp < 0
else:
return sgn(self.y - other.y) < 0
def __eq__(self,other):
return sgn(self.x - other.x) == 0 and sgn(self.y - other.y) == 0
def abs(self):
return math.sqrt(self.x**2+self.y**2)
def dot(self,p):
return self.x * p.x + self.y*p.y
def det(self,p):
return self.x * p.y - self.y*p.x
def arg(self,p):
return math.atan2(y,x)
# 点の進行方向 a -> b -> c
def iSP(a,b,c):
tmp = sgn((b-a).det(c-a))
if tmp > 0: return 1 # 左に曲がる場合
elif tmp < 0: return -1 # 右に曲がる場合
else: # まっすぐ
if sgn((b-a).dot(c-a)) < 0: return -2 # c-a-b の順
if sgn((a-b).dot(c-b)) < 0: return 2 # a-b-c の順
return 0 # a-c-bの順
# ab,cd の直線交差
def isToleranceLine(a,b,c,d):
if sgn((b-a).det(c-d)) != 0: return 1 # 交差する
else:
if sgn((b-a).det(c-a)) != 0: return 0 # 平行
else: return -1 # 同一直線
# ab,cd の線分交差 重複,端点での交差もTrue
def isToleranceSegline(a,b,c,d):
return sgn(iSP(a,b,c)*iSP(a,b,d))<=0 and sgn(iSP(c,d,a)*iSP(c,d,b)) <= 0
# 直線ab と 直線cd の交点 (存在する前提)
def Intersection(a,b,c,d):
tmp1 = (b-a)*((c-a).det(d-c))
tmp2 = (b-a).det(d-c)
return a+(tmp1/tmp2)
# 直線ab と 点c の距離
def DistanceLineToPoint(a,b,c):
return abs(((c-a).det(b-a))/((b-a).abs()))
# 線分ab と 点c の距離
def DistanceSeglineToPoint(a,b,c):
if sgn((b-a).dot(c-a)) < 0: # <cab が鈍角
return (c-a).abs()
if sgn((a-b).dot(c-b)) < 0: # <cba が鈍角
return (c-b).abs()
return DistanceLineToPoint(a,b,c)
# 直線ab への 点c からの垂線の足
def Vfoot(a,b,c):
d = c + Point((b-a).y,-(b-a).x)
return Intersection(a,b,c,d)
# 多角形の面積
def PolygonArea(Plist):
Plist = ConvexHull(Plist)
L = len(Plist)
S = 0
for i in range(L):
tmpS = (Plist[i-1].det(Plist[i]))/2
S += tmpS
return S
# 多角形の重心
def PolygonG(Plist):
Plist = ConvexHull(Plist)
L = len(Plist)
S = 0
G = Point(0,0)
for i in range(L):
tmpS = (Plist[i-1].det(Plist[i]))/2
S += tmpS
G += (Plist[i-1]+Plist[i])/3*tmpS
return G/S
# 凸法
def ConvexHull(Plist):
Plist.sort()
L = len(Plist)
qu = deque([])
quL = 0
for p in Plist:
while quL >= 2 and iSP(qu[quL-2],qu[quL-1],p) == 1:
qu.pop()
quL -= 1
qu.append(p)
quL += 1
qd = deque([])
qdL = 0
for p in Plist:
while qdL >= 2 and iSP(qd[qdL-2],qd[qdL-1],p) == -1:
qd.pop()
qdL -= 1
qd.append(p)
qdL += 1
qd.pop()
qu.popleft()
hidari = list(qd) + list(reversed(qu)) # 左端開始,左回りPlist
return hidari
N = int(input())
for _ in range(N):
x0,y0,x1,y1,x2,y2,x3,y3 = inpl()
p0 = Point(x0,y0)
p1 = Point(x1,y1)
p2 = Point(x2,y2)
p3 = Point(x3,y3)
if isToleranceSegline(p0,p1,p2,p3):
print(1)
else:
print(0)
``` | instruction | 0 | 61,452 | 23 | 122,904 |
Yes | output | 1 | 61,452 | 23 | 122,905 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xpi, ypi ≤ 10000
* p0 ≠ p1 and p2 ≠ p3.
Input
The entire input looks like:
q (the number of queries)
1st query
2nd query
...
qth query
Each query consists of integer coordinates of end points of s1 and s2 in the following format:
xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3
Output
For each query, print "1" or "0".
Example
Input
3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0
Output
1
1
0
Submitted Solution:
```
#!/usr/bin/env python3
# CGL_2_B: Segments/Lines - Intersection
from math import sqrt
class Segment:
def __init__(self, p0, p1):
self.end_points = (p0, p1)
def intersect(self, other):
p0, p1 = self.end_points
p2, p3 = other.end_points
if convex(p0, p2, p1, p3):
return True
else:
if (p0 in other or p1 in other
or p2 in self or p3 in self):
return True
return False
def __contains__(self, p):
p0, p1 = self.end_points
x0, y0 = p0
x1, y1 = p1
x, y = p
v = (x1-x0, y1-y0)
v0 = (x-x0, y-y0)
v1 = (x-x1, y-y1)
if dot(orthogonal(v0), v1) == 0:
if abs(length(v0) + length(v1) - length(v)) < 1e-10:
return True
return False
def dot(v1, v2):
x1, y1 = v1
x2, y2 = v2
return x1 * x2 + y1 * y2
def orthogonal(v):
x, y = v
return -y, x
def length(v):
x, y = v
return sqrt(x**2 + y**2)
def convex(p0, p1, p2, p3):
ret = []
for pa, pb, pc in zip([p0, p1, p2, p3],
[p1, p2, p3, p0],
[p2, p3, p0, p1]):
xa, ya = pa
xb, yb = pb
xc, yc = pc
v1 = (xb - xa, yb - ya)
v2 = (xc - xb, yc - yb)
ret.append(dot(orthogonal(v1), v2))
return all([d > 0 for d in ret]) or all([d < 0 for d in ret])
def run():
q = int(input())
for _ in range(q):
x0, y0, x1, y1, x2, y2, x3, y3 = [int(i) for i in input().split()]
s1 = Segment((x0, y0), (x1, y1))
s2 = Segment((x2, y2), (x3, y3))
if s1.intersect(s2):
print(1)
else:
print(0)
if __name__ == '__main__':
run()
``` | instruction | 0 | 61,453 | 23 | 122,906 |
Yes | output | 1 | 61,453 | 23 | 122,907 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xpi, ypi ≤ 10000
* p0 ≠ p1 and p2 ≠ p3.
Input
The entire input looks like:
q (the number of queries)
1st query
2nd query
...
qth query
Each query consists of integer coordinates of end points of s1 and s2 in the following format:
xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3
Output
For each query, print "1" or "0".
Example
Input
3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0
Output
1
1
0
Submitted Solution:
```
import sys
from itertools import starmap
readline = sys.stdin.readline
EPS = 1e-9
ONLINE_FRONT = -2
CLOCKWISE = -1
ON_SEGMENT = 0
COUNTER_CLOCKWISE = 1
ONLINE_BACK = 2
class Segment(object):
__slots__ = ('fi', 'se')
def __init__(self, fi, se):
self.fi = fi
self.se = se
def cross(a, b):
return a.real * b.imag - a.imag * b.real
def dot(a, b):
return a.real * b.real + a.imag * b.imag
def norm(base):
return abs(base) ** 2
def project(s, p2):
base = s.fi - s.se
r = dot(p2 - s.fi, base) / norm(base)
return s.fi + base * r
def reflect(s, p):
return p + (project(s, p) - p) * 2.0
def ccw(p1, p2, p3):
a = p2 - p1
b = p3 - p1
if cross(a, b) > EPS: return 1
if cross(a, b) < -EPS: return -1
if dot(a, b) < -EPS: return 2
if norm(a) < norm(b): return -2
return 0
def intersect4(p1, p2, p3, p4):
return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 and
ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0)
def intersect2(s1, s2):
return intersect4(s1.fi, s1.se, s2.fi, s2.se)
def getDistance(a, b): return abs(a - b)
def getDistanceLP(l, p):
return abs(cross(l.se - l.fi, p - l.fi) / abs(l.se - l.fi))
def getDistanceSP(s, p):
if dot(s.se - s.fi, p - s.fi) < 0.0: return abs(p - s.fi)
if dot(s.fi - s.se, p - s.se) < 0.0: return abs(p - s.se)
return getDistanceLP(s, p)
def getDistances(s1, s2):
if intersect2(s1, s2): return 0.0
return min(getDistanceSP(s1, s2.fi), getDistanceSP(s1, s2.se),
getDistanceSP(s2, s1.fi), getDistanceSP(s2, s1.se))
n = int(readline())
for _ in [0] * n:
p0, p1, p2, p3 = starmap(complex, zip(*[map(int, input().split())] * 2))
print(1 if intersect4(p0, p1, p2, p3) else 0)
``` | instruction | 0 | 61,454 | 23 | 122,908 |
Yes | output | 1 | 61,454 | 23 | 122,909 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xpi, ypi ≤ 10000
* p0 ≠ p1 and p2 ≠ p3.
Input
The entire input looks like:
q (the number of queries)
1st query
2nd query
...
qth query
Each query consists of integer coordinates of end points of s1 and s2 in the following format:
xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3
Output
For each query, print "1" or "0".
Example
Input
3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0
Output
1
1
0
Submitted Solution:
```
def cross(a,b):return a[0]*b[1] - a[1]*b[0]
q = range(int(input()))
for i in q:
a,b,c,d,e,f,g,h = [int(x) for x in input().split()]
A = [c-a,d-b]; B = [e-a,f-b]; C = [g-a,h-a];
D = [g-e,h-f]; E = [a-e,b-f]; F = [c-e,d-f];
T = max(a,c) < min(e,g)
if max(a,c) < min(e,g) or max(e,g) < min(a,c) or max(b,d) < min(f,h) or max(f,h) < min(b,d):
print("0")
elif cross(A,B) * cross(A,C) <= 1e-12 and cross(D,E) * cross(D,F) <= 1e-12:
print("1")
else:
print("0")
``` | instruction | 0 | 61,455 | 23 | 122,910 |
No | output | 1 | 61,455 | 23 | 122,911 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xpi, ypi ≤ 10000
* p0 ≠ p1 and p2 ≠ p3.
Input
The entire input looks like:
q (the number of queries)
1st query
2nd query
...
qth query
Each query consists of integer coordinates of end points of s1 and s2 in the following format:
xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3
Output
For each query, print "1" or "0".
Example
Input
3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0
Output
1
1
0
Submitted Solution:
```
from itertools import starmap
def cross(a, b):
return a.real * b.imag - a.imag * b.real
q = int(input())
while q:
q -= 1
p0, p1, p2, p3 = starmap(complex, zip(*[map(int, input().split())] * 2))
max_x1, min_x1 = (p0.real, p1.real) if p0.real > p1.real else (p1.real, p0.real)
max_y1, min_y1 = (p0.imag, p1.imag) if p0.imag > p1.imag else (p1.imag, p0.imag)
max_x2, min_x2 = (p2.real, p3.real) if p2.real > p3.real else (p2.real, p3.real)
max_y2, min_y2 = (p2.imag, p3.imag) if p2.imag > p3.imag else (p2.imag, p3.imag)
if any((max_x1 < min_x2, max_x2 < min_x1, max_y1 < min_y2, max_y2 < min_y1)):
print(0)
continue
print(int(cross(p1 - p0, p2 - p0) * cross(p1 - p0, p3 - p0) < 1e-6 and
cross(p3 - p2, p0 - p2) * cross(p3 - p2, p1 - p2) < 1e-6))
``` | instruction | 0 | 61,456 | 23 | 122,912 |
No | output | 1 | 61,456 | 23 | 122,913 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xpi, ypi ≤ 10000
* p0 ≠ p1 and p2 ≠ p3.
Input
The entire input looks like:
q (the number of queries)
1st query
2nd query
...
qth query
Each query consists of integer coordinates of end points of s1 and s2 in the following format:
xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3
Output
For each query, print "1" or "0".
Example
Input
3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0
Output
1
1
0
Submitted Solution:
```
def dot(a,b):return a[0]*b[0] + a[1]*b[1]
def cross(a,b):return a[0]*b[1] - a[1]*b[0]
def isOn(a,b):
if cross(a,b) == 0:
if dot(a,b) < 0 or dot(a,a) < dot(b,b) : return False
else : return True
else :
return False
q = int(input())
for i in range(q):
x0,y0,x1,y1,x2,y2,x3,y3 = [int(i) for i in input().split()]
a = [x1-x0,y1-y0]
b = [x2-x0,y2-y0]
c = [x3-x0,y3-y0]
d = [x3-x2,y3-y2]
e = [x0-x2,y0-y2]
f = [x1-x2,y1-y2]
if isOn(a,b) or isOn(a,c):
print(1)
elif cross(a,b) == 0 and cross(a,c) == 0:
print(0)
elif cross(a,b) * cross(a,c) > 0 :
print(0)
elif cross(d,e) * cross(d,f) > 0 :
print(0)
else :
print(1,isOn(a,b),isOn(a,c))
``` | instruction | 0 | 61,457 | 23 | 122,914 |
No | output | 1 | 61,457 | 23 | 122,915 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xpi, ypi ≤ 10000
* p0 ≠ p1 and p2 ≠ p3.
Input
The entire input looks like:
q (the number of queries)
1st query
2nd query
...
qth query
Each query consists of integer coordinates of end points of s1 and s2 in the following format:
xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3
Output
For each query, print "1" or "0".
Example
Input
3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0
Output
1
1
0
Submitted Solution:
```
import math
class Point():
def __init__(self, x, y):
self.x = x
self.y = y
class Segment():
def __init__(self, x1, y1, x2, y2):
self.p1 = Point(x1, y1)
self.p2 = Point(x2, y2)
def is_intersect(self, seg):
a = (seg.p1.x - seg.p2.x) * (self.p1.y - seg.p1.y) + (seg.p1.y - seg.p2.y) * (seg.p1.x - self.p1.x)
b = (seg.p1.x - seg.p2.x) * (self.p2.y - seg.p1.y) + (seg.p1.y - seg.p2.y) * (seg.p1.x - self.p2.x)
c = (self.p1.x - self.p2.x) * (seg.p1.y - self.p1.y) + (self.p1.y - self.p2.y) * (self.p1.x - seg.p1.x)
d = (self.p1.x - self.p2.x) * (seg.p2.y - self.p1.y) + (self.p1.y - self.p2.y) * (self.p1.x - seg.p2.x)
return a*b < 0 and c*d < 0
q = int(input())
for i in range(q):
x0, y0, x1, y1, x2, y2, x3, y3 = list(map(int, input().split(' ')))
line1, line2 = Segment(x0, y0, x1, y1), Segment(x2, y2, x3, y3)
if line1.is_intersect(line2):
print(1)
else:
print(0)
``` | instruction | 0 | 61,458 | 23 | 122,916 |
No | output | 1 | 61,458 | 23 | 122,917 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice has a lovely piece of cloth. It has the shape of a square with a side of length a centimeters. Bob also wants such piece of cloth. He would prefer a square with a side of length b centimeters (where b < a). Alice wanted to make Bob happy, so she cut the needed square out of the corner of her piece and gave it to Bob. Now she is left with an ugly L shaped cloth (see pictures below).
Alice would like to know whether the area of her cloth expressed in square centimeters is [prime.](https://en.wikipedia.org/wiki/Prime_number) Could you help her to determine it?
Input
The first line contains a number t (1 ≤ t ≤ 5) — the number of test cases.
Each of the next t lines describes the i-th test case. It contains two integers a and b~(1 ≤ b < a ≤ 10^{11}) — the side length of Alice's square and the side length of the square that Bob wants.
Output
Print t lines, where the i-th line is the answer to the i-th test case. Print "YES" (without quotes) if the area of the remaining piece of cloth is prime, otherwise print "NO".
You can print each letter in an arbitrary case (upper or lower).
Example
Input
4
6 5
16 13
61690850361 24777622630
34 33
Output
YES
NO
NO
YES
Note
The figure below depicts the first test case. The blue part corresponds to the piece which belongs to Bob, and the red part is the piece that Alice keeps for herself. The area of the red part is 6^2 - 5^2 = 36 - 25 = 11, which is prime, so the answer is "YES".
<image>
In the second case, the area is 16^2 - 13^2 = 87, which is divisible by 3.
<image>
In the third case, the area of the remaining piece is 61690850361^2 - 24777622630^2 = 3191830435068605713421. This number is not prime because 3191830435068605713421 = 36913227731 ⋅ 86468472991 .
In the last case, the area is 34^2 - 33^2 = 67.
Submitted Solution:
```
import sys
from math import *
n = int(sys.stdin.readline().strip())
def prime(a,b):
c=True
for i in range(2,floor(sqrt(a+b))+1):
if (a+b)%i==0:
c=False
return(c and (a-b==1))
for i in range(n):
a, b = [int(x) for x in sys.stdin.readline().strip().split(" ") if x]
if prime(a,b):
print("YES")
else:
print("NO")
``` | instruction | 0 | 61,479 | 23 | 122,958 |
Yes | output | 1 | 61,479 | 23 | 122,959 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice has a lovely piece of cloth. It has the shape of a square with a side of length a centimeters. Bob also wants such piece of cloth. He would prefer a square with a side of length b centimeters (where b < a). Alice wanted to make Bob happy, so she cut the needed square out of the corner of her piece and gave it to Bob. Now she is left with an ugly L shaped cloth (see pictures below).
Alice would like to know whether the area of her cloth expressed in square centimeters is [prime.](https://en.wikipedia.org/wiki/Prime_number) Could you help her to determine it?
Input
The first line contains a number t (1 ≤ t ≤ 5) — the number of test cases.
Each of the next t lines describes the i-th test case. It contains two integers a and b~(1 ≤ b < a ≤ 10^{11}) — the side length of Alice's square and the side length of the square that Bob wants.
Output
Print t lines, where the i-th line is the answer to the i-th test case. Print "YES" (without quotes) if the area of the remaining piece of cloth is prime, otherwise print "NO".
You can print each letter in an arbitrary case (upper or lower).
Example
Input
4
6 5
16 13
61690850361 24777622630
34 33
Output
YES
NO
NO
YES
Note
The figure below depicts the first test case. The blue part corresponds to the piece which belongs to Bob, and the red part is the piece that Alice keeps for herself. The area of the red part is 6^2 - 5^2 = 36 - 25 = 11, which is prime, so the answer is "YES".
<image>
In the second case, the area is 16^2 - 13^2 = 87, which is divisible by 3.
<image>
In the third case, the area of the remaining piece is 61690850361^2 - 24777622630^2 = 3191830435068605713421. This number is not prime because 3191830435068605713421 = 36913227731 ⋅ 86468472991 .
In the last case, the area is 34^2 - 33^2 = 67.
Submitted Solution:
```
from math import sqrt, floor
def check(n):
for i in range(2, floor(sqrt(n)) + 1):
if (n % i == 0):
return False
return True
t = int(input())
for i in range(t):
a, b = map(int, input().split())
if (a - b == 1 and check(a + b)):
print("YES")
else:
print("NO")
``` | instruction | 0 | 61,480 | 23 | 122,960 |
Yes | output | 1 | 61,480 | 23 | 122,961 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice has a lovely piece of cloth. It has the shape of a square with a side of length a centimeters. Bob also wants such piece of cloth. He would prefer a square with a side of length b centimeters (where b < a). Alice wanted to make Bob happy, so she cut the needed square out of the corner of her piece and gave it to Bob. Now she is left with an ugly L shaped cloth (see pictures below).
Alice would like to know whether the area of her cloth expressed in square centimeters is [prime.](https://en.wikipedia.org/wiki/Prime_number) Could you help her to determine it?
Input
The first line contains a number t (1 ≤ t ≤ 5) — the number of test cases.
Each of the next t lines describes the i-th test case. It contains two integers a and b~(1 ≤ b < a ≤ 10^{11}) — the side length of Alice's square and the side length of the square that Bob wants.
Output
Print t lines, where the i-th line is the answer to the i-th test case. Print "YES" (without quotes) if the area of the remaining piece of cloth is prime, otherwise print "NO".
You can print each letter in an arbitrary case (upper or lower).
Example
Input
4
6 5
16 13
61690850361 24777622630
34 33
Output
YES
NO
NO
YES
Note
The figure below depicts the first test case. The blue part corresponds to the piece which belongs to Bob, and the red part is the piece that Alice keeps for herself. The area of the red part is 6^2 - 5^2 = 36 - 25 = 11, which is prime, so the answer is "YES".
<image>
In the second case, the area is 16^2 - 13^2 = 87, which is divisible by 3.
<image>
In the third case, the area of the remaining piece is 61690850361^2 - 24777622630^2 = 3191830435068605713421. This number is not prime because 3191830435068605713421 = 36913227731 ⋅ 86468472991 .
In the last case, the area is 34^2 - 33^2 = 67.
Submitted Solution:
```
def is_prime(n):
i = 2
while i * i <= n:
if n == 1:
return 0
if n == 2 or n == 3:
return 1
if n % i == 0:
return 0
else:
i += 1
return 1
t = int(input())
for i in range(t):
a, b = map(int, input().split())
if a - b == 1:
if is_prime(a + b) == 1:
print("YES")
else:
print("NO")
else:
print("NO")
``` | instruction | 0 | 61,481 | 23 | 122,962 |
Yes | output | 1 | 61,481 | 23 | 122,963 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice has a lovely piece of cloth. It has the shape of a square with a side of length a centimeters. Bob also wants such piece of cloth. He would prefer a square with a side of length b centimeters (where b < a). Alice wanted to make Bob happy, so she cut the needed square out of the corner of her piece and gave it to Bob. Now she is left with an ugly L shaped cloth (see pictures below).
Alice would like to know whether the area of her cloth expressed in square centimeters is [prime.](https://en.wikipedia.org/wiki/Prime_number) Could you help her to determine it?
Input
The first line contains a number t (1 ≤ t ≤ 5) — the number of test cases.
Each of the next t lines describes the i-th test case. It contains two integers a and b~(1 ≤ b < a ≤ 10^{11}) — the side length of Alice's square and the side length of the square that Bob wants.
Output
Print t lines, where the i-th line is the answer to the i-th test case. Print "YES" (without quotes) if the area of the remaining piece of cloth is prime, otherwise print "NO".
You can print each letter in an arbitrary case (upper or lower).
Example
Input
4
6 5
16 13
61690850361 24777622630
34 33
Output
YES
NO
NO
YES
Note
The figure below depicts the first test case. The blue part corresponds to the piece which belongs to Bob, and the red part is the piece that Alice keeps for herself. The area of the red part is 6^2 - 5^2 = 36 - 25 = 11, which is prime, so the answer is "YES".
<image>
In the second case, the area is 16^2 - 13^2 = 87, which is divisible by 3.
<image>
In the third case, the area of the remaining piece is 61690850361^2 - 24777622630^2 = 3191830435068605713421. This number is not prime because 3191830435068605713421 = 36913227731 ⋅ 86468472991 .
In the last case, the area is 34^2 - 33^2 = 67.
Submitted Solution:
```
def check(n):
i=2
while i*i<=n:
if n%i==0:
return 1
i+=1
return 0
for _ in range(int(input())):
a,b=map(int,input().split())
if (a-b)!=1:
print('NO')
else:
if check((a*a)-(b*b)):print('NO')
else:print('YES')
``` | instruction | 0 | 61,482 | 23 | 122,964 |
Yes | output | 1 | 61,482 | 23 | 122,965 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice has a lovely piece of cloth. It has the shape of a square with a side of length a centimeters. Bob also wants such piece of cloth. He would prefer a square with a side of length b centimeters (where b < a). Alice wanted to make Bob happy, so she cut the needed square out of the corner of her piece and gave it to Bob. Now she is left with an ugly L shaped cloth (see pictures below).
Alice would like to know whether the area of her cloth expressed in square centimeters is [prime.](https://en.wikipedia.org/wiki/Prime_number) Could you help her to determine it?
Input
The first line contains a number t (1 ≤ t ≤ 5) — the number of test cases.
Each of the next t lines describes the i-th test case. It contains two integers a and b~(1 ≤ b < a ≤ 10^{11}) — the side length of Alice's square and the side length of the square that Bob wants.
Output
Print t lines, where the i-th line is the answer to the i-th test case. Print "YES" (without quotes) if the area of the remaining piece of cloth is prime, otherwise print "NO".
You can print each letter in an arbitrary case (upper or lower).
Example
Input
4
6 5
16 13
61690850361 24777622630
34 33
Output
YES
NO
NO
YES
Note
The figure below depicts the first test case. The blue part corresponds to the piece which belongs to Bob, and the red part is the piece that Alice keeps for herself. The area of the red part is 6^2 - 5^2 = 36 - 25 = 11, which is prime, so the answer is "YES".
<image>
In the second case, the area is 16^2 - 13^2 = 87, which is divisible by 3.
<image>
In the third case, the area of the remaining piece is 61690850361^2 - 24777622630^2 = 3191830435068605713421. This number is not prime because 3191830435068605713421 = 36913227731 ⋅ 86468472991 .
In the last case, the area is 34^2 - 33^2 = 67.
Submitted Solution:
```
t = int(input())
for i in range(t):
a,b = map(int,input().split())
x = a-b
if (x==1):
print ('YES\n')
else:
print ('NO\n')
``` | instruction | 0 | 61,483 | 23 | 122,966 |
No | output | 1 | 61,483 | 23 | 122,967 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice has a lovely piece of cloth. It has the shape of a square with a side of length a centimeters. Bob also wants such piece of cloth. He would prefer a square with a side of length b centimeters (where b < a). Alice wanted to make Bob happy, so she cut the needed square out of the corner of her piece and gave it to Bob. Now she is left with an ugly L shaped cloth (see pictures below).
Alice would like to know whether the area of her cloth expressed in square centimeters is [prime.](https://en.wikipedia.org/wiki/Prime_number) Could you help her to determine it?
Input
The first line contains a number t (1 ≤ t ≤ 5) — the number of test cases.
Each of the next t lines describes the i-th test case. It contains two integers a and b~(1 ≤ b < a ≤ 10^{11}) — the side length of Alice's square and the side length of the square that Bob wants.
Output
Print t lines, where the i-th line is the answer to the i-th test case. Print "YES" (without quotes) if the area of the remaining piece of cloth is prime, otherwise print "NO".
You can print each letter in an arbitrary case (upper or lower).
Example
Input
4
6 5
16 13
61690850361 24777622630
34 33
Output
YES
NO
NO
YES
Note
The figure below depicts the first test case. The blue part corresponds to the piece which belongs to Bob, and the red part is the piece that Alice keeps for herself. The area of the red part is 6^2 - 5^2 = 36 - 25 = 11, which is prime, so the answer is "YES".
<image>
In the second case, the area is 16^2 - 13^2 = 87, which is divisible by 3.
<image>
In the third case, the area of the remaining piece is 61690850361^2 - 24777622630^2 = 3191830435068605713421. This number is not prime because 3191830435068605713421 = 36913227731 ⋅ 86468472991 .
In the last case, the area is 34^2 - 33^2 = 67.
Submitted Solution:
```
import math
t = int(input())
for i in range(0, t):
a, b = map(int, input().split())
if (a - b == 1) and ((a + b) % 3 != 0):
num = a + b
for i in range(1, num//6):
if num % (6*i-1) == 0 or num % (6*i+1) == 0:
print("NO")
break
else:
print("YES")
else: print("NO")
``` | instruction | 0 | 61,484 | 23 | 122,968 |
No | output | 1 | 61,484 | 23 | 122,969 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice has a lovely piece of cloth. It has the shape of a square with a side of length a centimeters. Bob also wants such piece of cloth. He would prefer a square with a side of length b centimeters (where b < a). Alice wanted to make Bob happy, so she cut the needed square out of the corner of her piece and gave it to Bob. Now she is left with an ugly L shaped cloth (see pictures below).
Alice would like to know whether the area of her cloth expressed in square centimeters is [prime.](https://en.wikipedia.org/wiki/Prime_number) Could you help her to determine it?
Input
The first line contains a number t (1 ≤ t ≤ 5) — the number of test cases.
Each of the next t lines describes the i-th test case. It contains two integers a and b~(1 ≤ b < a ≤ 10^{11}) — the side length of Alice's square and the side length of the square that Bob wants.
Output
Print t lines, where the i-th line is the answer to the i-th test case. Print "YES" (without quotes) if the area of the remaining piece of cloth is prime, otherwise print "NO".
You can print each letter in an arbitrary case (upper or lower).
Example
Input
4
6 5
16 13
61690850361 24777622630
34 33
Output
YES
NO
NO
YES
Note
The figure below depicts the first test case. The blue part corresponds to the piece which belongs to Bob, and the red part is the piece that Alice keeps for herself. The area of the red part is 6^2 - 5^2 = 36 - 25 = 11, which is prime, so the answer is "YES".
<image>
In the second case, the area is 16^2 - 13^2 = 87, which is divisible by 3.
<image>
In the third case, the area of the remaining piece is 61690850361^2 - 24777622630^2 = 3191830435068605713421. This number is not prime because 3191830435068605713421 = 36913227731 ⋅ 86468472991 .
In the last case, the area is 34^2 - 33^2 = 67.
Submitted Solution:
```
import collections
from functools import cmp_to_key
def readnums():
return list(map(lambda x: int(x), input().split(" ")))
MAXN = int(4 * 1e5)
prime = set(i for i in range(2, int(MAXN + 1)))
def sieve(n):
global MAXN
p = 2
while (p * p <= n):
# If prime[p] is not changed, then it is a prime
if (p in prime):
# Update all multiples of p
for i in range(p * 2, n+1, p):
if i in prime:
prime.remove(i)
p += 1
sieve(MAXN)
t = int(input())
for i in range(t):
a, b = readnums()
if a+b in prime and (a-b) == 1:
print ('YES')
else:
print ('NO')
``` | instruction | 0 | 61,485 | 23 | 122,970 |
No | output | 1 | 61,485 | 23 | 122,971 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice has a lovely piece of cloth. It has the shape of a square with a side of length a centimeters. Bob also wants such piece of cloth. He would prefer a square with a side of length b centimeters (where b < a). Alice wanted to make Bob happy, so she cut the needed square out of the corner of her piece and gave it to Bob. Now she is left with an ugly L shaped cloth (see pictures below).
Alice would like to know whether the area of her cloth expressed in square centimeters is [prime.](https://en.wikipedia.org/wiki/Prime_number) Could you help her to determine it?
Input
The first line contains a number t (1 ≤ t ≤ 5) — the number of test cases.
Each of the next t lines describes the i-th test case. It contains two integers a and b~(1 ≤ b < a ≤ 10^{11}) — the side length of Alice's square and the side length of the square that Bob wants.
Output
Print t lines, where the i-th line is the answer to the i-th test case. Print "YES" (without quotes) if the area of the remaining piece of cloth is prime, otherwise print "NO".
You can print each letter in an arbitrary case (upper or lower).
Example
Input
4
6 5
16 13
61690850361 24777622630
34 33
Output
YES
NO
NO
YES
Note
The figure below depicts the first test case. The blue part corresponds to the piece which belongs to Bob, and the red part is the piece that Alice keeps for herself. The area of the red part is 6^2 - 5^2 = 36 - 25 = 11, which is prime, so the answer is "YES".
<image>
In the second case, the area is 16^2 - 13^2 = 87, which is divisible by 3.
<image>
In the third case, the area of the remaining piece is 61690850361^2 - 24777622630^2 = 3191830435068605713421. This number is not prime because 3191830435068605713421 = 36913227731 ⋅ 86468472991 .
In the last case, the area is 34^2 - 33^2 = 67.
Submitted Solution:
```
from math import ceil, sqrt
def is_prime(n):
for i in [2] + [j for j in range(3, int(sqrt(n)) + 1, 2)]:
if n % i == 0:
return False
return True
n = int(input())
for _ in range(n):
a, b = [int(i) for i in input().split()]
prime = a - b == 1 or is_prime(2 * a - 1)
print('YES' if prime else 'NO')
``` | instruction | 0 | 61,486 | 23 | 122,972 |
No | output | 1 | 61,486 | 23 | 122,973 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew n distinct lines, given by equations y = x + p_i for some distinct p_1, p_2, …, p_n.
Then JLS drew on the same paper sheet m distinct lines given by equations y = -x + q_i for some distinct q_1, q_2, …, q_m.
DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.
Input
The first line contains one integer t (1 ≤ t ≤ 1000), the number of test cases in the input. Then follow the test case descriptions.
The first line of a test case contains an integer n (1 ≤ n ≤ 10^5), the number of lines drawn by DLS.
The second line of a test case contains n distinct integers p_i (0 ≤ p_i ≤ 10^9) describing the lines drawn by DLS. The integer p_i describes a line given by the equation y = x + p_i.
The third line of a test case contains an integer m (1 ≤ m ≤ 10^5), the number of lines drawn by JLS.
The fourth line of a test case contains m distinct integers q_i (0 ≤ q_i ≤ 10^9) describing the lines drawn by JLS. The integer q_i describes a line given by the equation y = -x + q_i.
The sum of the values of n over all test cases in the input does not exceed 10^5. Similarly, the sum of the values of m over all test cases in the input does not exceed 10^5.
In hacks it is allowed to use only one test case in the input, so t=1 should be satisfied.
Output
For each test case in the input print a single integer — the number of line pairs with integer intersection points.
Example
Input
3
3
1 3 2
2
0 3
1
1
1
1
1
2
1
1
Output
3
1
0
Note
The picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates.
<image> | instruction | 0 | 61,571 | 23 | 123,142 |
Tags: geometry, math
Correct Solution:
```
vezes = int(input())
for __ in range(vezes):
dic1 = {}
dic1["pares"] = 0
dic1["impar"] = 0
dic2 = {}
dic2["pares"] = 0
dic2["impar"] = 0
n = int(input())
linhasDLS = input().split()
for linha in linhasDLS:
if(int(linha) % 2 == 0):
dic1["pares"] += 1
else:
dic1["impar"] += 1
m = int(input())
linhasJLS = input().split()
for linha in linhasJLS:
if(int(linha) % 2 == 0):
dic2["pares"] += 1
else:
dic2["impar"] += 1
resultado = dic1["pares"]*dic2["pares"] + dic1["impar"]*dic2["impar"]
print(resultado)
``` | output | 1 | 61,571 | 23 | 123,143 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew n distinct lines, given by equations y = x + p_i for some distinct p_1, p_2, …, p_n.
Then JLS drew on the same paper sheet m distinct lines given by equations y = -x + q_i for some distinct q_1, q_2, …, q_m.
DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.
Input
The first line contains one integer t (1 ≤ t ≤ 1000), the number of test cases in the input. Then follow the test case descriptions.
The first line of a test case contains an integer n (1 ≤ n ≤ 10^5), the number of lines drawn by DLS.
The second line of a test case contains n distinct integers p_i (0 ≤ p_i ≤ 10^9) describing the lines drawn by DLS. The integer p_i describes a line given by the equation y = x + p_i.
The third line of a test case contains an integer m (1 ≤ m ≤ 10^5), the number of lines drawn by JLS.
The fourth line of a test case contains m distinct integers q_i (0 ≤ q_i ≤ 10^9) describing the lines drawn by JLS. The integer q_i describes a line given by the equation y = -x + q_i.
The sum of the values of n over all test cases in the input does not exceed 10^5. Similarly, the sum of the values of m over all test cases in the input does not exceed 10^5.
In hacks it is allowed to use only one test case in the input, so t=1 should be satisfied.
Output
For each test case in the input print a single integer — the number of line pairs with integer intersection points.
Example
Input
3
3
1 3 2
2
0 3
1
1
1
1
1
2
1
1
Output
3
1
0
Note
The picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates.
<image> | instruction | 0 | 61,572 | 23 | 123,144 |
Tags: geometry, math
Correct Solution:
```
count_inputs = int(input())
ii = 1
while ii <= count_inputs:
i = 0
j = 0
result = 0
DLS = int(input())
p_i = [int(x) for x in input().split()]
JLS = int(input())
q_j = [int(x) for x in input().split()]
sum_p = sum([i%2 for i in p_i])
sum_q = sum([i%2 for i in q_j])
result = sum_p*sum_q +(DLS - sum_p)*(JLS - sum_q)
# while i < len(p_i):
# while j < len(q_j):
# if (p_i[i]-q_j[j])%2==0:
# result += 1
# j += 1
# j = 0
# i += 1
ii += 1
print(result)
``` | output | 1 | 61,572 | 23 | 123,145 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew n distinct lines, given by equations y = x + p_i for some distinct p_1, p_2, …, p_n.
Then JLS drew on the same paper sheet m distinct lines given by equations y = -x + q_i for some distinct q_1, q_2, …, q_m.
DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.
Input
The first line contains one integer t (1 ≤ t ≤ 1000), the number of test cases in the input. Then follow the test case descriptions.
The first line of a test case contains an integer n (1 ≤ n ≤ 10^5), the number of lines drawn by DLS.
The second line of a test case contains n distinct integers p_i (0 ≤ p_i ≤ 10^9) describing the lines drawn by DLS. The integer p_i describes a line given by the equation y = x + p_i.
The third line of a test case contains an integer m (1 ≤ m ≤ 10^5), the number of lines drawn by JLS.
The fourth line of a test case contains m distinct integers q_i (0 ≤ q_i ≤ 10^9) describing the lines drawn by JLS. The integer q_i describes a line given by the equation y = -x + q_i.
The sum of the values of n over all test cases in the input does not exceed 10^5. Similarly, the sum of the values of m over all test cases in the input does not exceed 10^5.
In hacks it is allowed to use only one test case in the input, so t=1 should be satisfied.
Output
For each test case in the input print a single integer — the number of line pairs with integer intersection points.
Example
Input
3
3
1 3 2
2
0 3
1
1
1
1
1
2
1
1
Output
3
1
0
Note
The picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates.
<image> | instruction | 0 | 61,573 | 23 | 123,146 |
Tags: geometry, math
Correct Solution:
```
for _ in range(int(input())):
np = int(input())
even_q = 0
odd_q = 0
p = [1 for i in input().split() if int(i) % 2 == 0]
nq = int(input())
q = [1 for i in input().split() if int(i) % 2 == 0]
even_p = sum(p)
odd_p = np - even_p
even_q = sum(q)
odd_q = nq - even_q
print(even_p * even_q + odd_p * odd_q)
``` | output | 1 | 61,573 | 23 | 123,147 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew n distinct lines, given by equations y = x + p_i for some distinct p_1, p_2, …, p_n.
Then JLS drew on the same paper sheet m distinct lines given by equations y = -x + q_i for some distinct q_1, q_2, …, q_m.
DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.
Input
The first line contains one integer t (1 ≤ t ≤ 1000), the number of test cases in the input. Then follow the test case descriptions.
The first line of a test case contains an integer n (1 ≤ n ≤ 10^5), the number of lines drawn by DLS.
The second line of a test case contains n distinct integers p_i (0 ≤ p_i ≤ 10^9) describing the lines drawn by DLS. The integer p_i describes a line given by the equation y = x + p_i.
The third line of a test case contains an integer m (1 ≤ m ≤ 10^5), the number of lines drawn by JLS.
The fourth line of a test case contains m distinct integers q_i (0 ≤ q_i ≤ 10^9) describing the lines drawn by JLS. The integer q_i describes a line given by the equation y = -x + q_i.
The sum of the values of n over all test cases in the input does not exceed 10^5. Similarly, the sum of the values of m over all test cases in the input does not exceed 10^5.
In hacks it is allowed to use only one test case in the input, so t=1 should be satisfied.
Output
For each test case in the input print a single integer — the number of line pairs with integer intersection points.
Example
Input
3
3
1 3 2
2
0 3
1
1
1
1
1
2
1
1
Output
3
1
0
Note
The picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates.
<image> | instruction | 0 | 61,574 | 23 | 123,148 |
Tags: geometry, math
Correct Solution:
```
import math as mt
import collections as cc
import sys
I=lambda:set(map(int,input().split()))
for tc in range(int(input())):
n,=I()
x=I()
m,=I()
y=I()
o1,o2,e1,e2=[0,0,0,0]
for i in x:
if i%2:
o1+=1
else:
e1+=1
for j in y:
if j%2:
o2+=1
else:
e2+=1
#print(o1,e1,o2,e2)
print(e1*e2+o1*o2)
``` | output | 1 | 61,574 | 23 | 123,149 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew n distinct lines, given by equations y = x + p_i for some distinct p_1, p_2, …, p_n.
Then JLS drew on the same paper sheet m distinct lines given by equations y = -x + q_i for some distinct q_1, q_2, …, q_m.
DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.
Input
The first line contains one integer t (1 ≤ t ≤ 1000), the number of test cases in the input. Then follow the test case descriptions.
The first line of a test case contains an integer n (1 ≤ n ≤ 10^5), the number of lines drawn by DLS.
The second line of a test case contains n distinct integers p_i (0 ≤ p_i ≤ 10^9) describing the lines drawn by DLS. The integer p_i describes a line given by the equation y = x + p_i.
The third line of a test case contains an integer m (1 ≤ m ≤ 10^5), the number of lines drawn by JLS.
The fourth line of a test case contains m distinct integers q_i (0 ≤ q_i ≤ 10^9) describing the lines drawn by JLS. The integer q_i describes a line given by the equation y = -x + q_i.
The sum of the values of n over all test cases in the input does not exceed 10^5. Similarly, the sum of the values of m over all test cases in the input does not exceed 10^5.
In hacks it is allowed to use only one test case in the input, so t=1 should be satisfied.
Output
For each test case in the input print a single integer — the number of line pairs with integer intersection points.
Example
Input
3
3
1 3 2
2
0 3
1
1
1
1
1
2
1
1
Output
3
1
0
Note
The picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates.
<image> | instruction | 0 | 61,575 | 23 | 123,150 |
Tags: geometry, math
Correct Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
a1 = list(map(int, input().split()))
m = int(input())
a2 = list(map(int, input().split()))
e1 = 0
o1 = 0
e2 = 0
o2 = 0
for i in a1:
if i%2:
o1 += 1
else:
e1 += 1
for i in a2:
if i%2:
o2 += 1
else:
e2 += 1
print(o1*o2+e1*e2)
``` | output | 1 | 61,575 | 23 | 123,151 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew n distinct lines, given by equations y = x + p_i for some distinct p_1, p_2, …, p_n.
Then JLS drew on the same paper sheet m distinct lines given by equations y = -x + q_i for some distinct q_1, q_2, …, q_m.
DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.
Input
The first line contains one integer t (1 ≤ t ≤ 1000), the number of test cases in the input. Then follow the test case descriptions.
The first line of a test case contains an integer n (1 ≤ n ≤ 10^5), the number of lines drawn by DLS.
The second line of a test case contains n distinct integers p_i (0 ≤ p_i ≤ 10^9) describing the lines drawn by DLS. The integer p_i describes a line given by the equation y = x + p_i.
The third line of a test case contains an integer m (1 ≤ m ≤ 10^5), the number of lines drawn by JLS.
The fourth line of a test case contains m distinct integers q_i (0 ≤ q_i ≤ 10^9) describing the lines drawn by JLS. The integer q_i describes a line given by the equation y = -x + q_i.
The sum of the values of n over all test cases in the input does not exceed 10^5. Similarly, the sum of the values of m over all test cases in the input does not exceed 10^5.
In hacks it is allowed to use only one test case in the input, so t=1 should be satisfied.
Output
For each test case in the input print a single integer — the number of line pairs with integer intersection points.
Example
Input
3
3
1 3 2
2
0 3
1
1
1
1
1
2
1
1
Output
3
1
0
Note
The picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates.
<image> | instruction | 0 | 61,576 | 23 | 123,152 |
Tags: geometry, math
Correct Solution:
```
t=int(input())
for i in range (0,t):
k=0
k1=0
c=0
c1=0
n=int(input())
p=[]
i = []
for a in input().split():
p.append(int(a))
for j in range(0,n):
if p[j]%2==0:
k+=1
else:
k1+=1
m=int(input())
q=[]
for a in input().split():
q.append(int(a))
for j in range (0,m):
if q[j]%2==0:
c+=1
else:
c1+=1
print(k*c+k1*c1)
``` | output | 1 | 61,576 | 23 | 123,153 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew n distinct lines, given by equations y = x + p_i for some distinct p_1, p_2, …, p_n.
Then JLS drew on the same paper sheet m distinct lines given by equations y = -x + q_i for some distinct q_1, q_2, …, q_m.
DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.
Input
The first line contains one integer t (1 ≤ t ≤ 1000), the number of test cases in the input. Then follow the test case descriptions.
The first line of a test case contains an integer n (1 ≤ n ≤ 10^5), the number of lines drawn by DLS.
The second line of a test case contains n distinct integers p_i (0 ≤ p_i ≤ 10^9) describing the lines drawn by DLS. The integer p_i describes a line given by the equation y = x + p_i.
The third line of a test case contains an integer m (1 ≤ m ≤ 10^5), the number of lines drawn by JLS.
The fourth line of a test case contains m distinct integers q_i (0 ≤ q_i ≤ 10^9) describing the lines drawn by JLS. The integer q_i describes a line given by the equation y = -x + q_i.
The sum of the values of n over all test cases in the input does not exceed 10^5. Similarly, the sum of the values of m over all test cases in the input does not exceed 10^5.
In hacks it is allowed to use only one test case in the input, so t=1 should be satisfied.
Output
For each test case in the input print a single integer — the number of line pairs with integer intersection points.
Example
Input
3
3
1 3 2
2
0 3
1
1
1
1
1
2
1
1
Output
3
1
0
Note
The picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates.
<image> | instruction | 0 | 61,577 | 23 | 123,154 |
Tags: geometry, math
Correct Solution:
```
if __name__ == '__main__':
tests = int(input())
while tests > 0:
tests -= 1
ans = 0
n = int(input())
ps = list(map(lambda x: int(x), input().split(' ')))
m = int(input())
qs = list(map(lambda x: int(x), input().split(' ')))
p1 = 0
p2 = 0
q1 = 0
q2 = 0
for p in ps:
if p % 2 == 0:
p1 += 1
else:
p2 += 1
for p in qs:
if p % 2 == 0:
q1 += 1
else:
q2 += 1
print(p1 * q1 + q2 * p2)
"""
3
3
1 3 2
2
0 3
1
1
1
1
1
2
1
1
"""
``` | output | 1 | 61,577 | 23 | 123,155 |
Provide tags and a correct Python 3 solution for this coding contest problem.
DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew n distinct lines, given by equations y = x + p_i for some distinct p_1, p_2, …, p_n.
Then JLS drew on the same paper sheet m distinct lines given by equations y = -x + q_i for some distinct q_1, q_2, …, q_m.
DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.
Input
The first line contains one integer t (1 ≤ t ≤ 1000), the number of test cases in the input. Then follow the test case descriptions.
The first line of a test case contains an integer n (1 ≤ n ≤ 10^5), the number of lines drawn by DLS.
The second line of a test case contains n distinct integers p_i (0 ≤ p_i ≤ 10^9) describing the lines drawn by DLS. The integer p_i describes a line given by the equation y = x + p_i.
The third line of a test case contains an integer m (1 ≤ m ≤ 10^5), the number of lines drawn by JLS.
The fourth line of a test case contains m distinct integers q_i (0 ≤ q_i ≤ 10^9) describing the lines drawn by JLS. The integer q_i describes a line given by the equation y = -x + q_i.
The sum of the values of n over all test cases in the input does not exceed 10^5. Similarly, the sum of the values of m over all test cases in the input does not exceed 10^5.
In hacks it is allowed to use only one test case in the input, so t=1 should be satisfied.
Output
For each test case in the input print a single integer — the number of line pairs with integer intersection points.
Example
Input
3
3
1 3 2
2
0 3
1
1
1
1
1
2
1
1
Output
3
1
0
Note
The picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates.
<image> | instruction | 0 | 61,578 | 23 | 123,156 |
Tags: geometry, math
Correct Solution:
```
queries = int(input())
for __ in range(queries):
n = int(input())
arrayA = [int(i) for i in input().split()]
m = int(input())
arrayB = [int(j) for j in input().split()]
pares = 0
impares = 0
for e in arrayA:
if e % 2 == 0:
pares += 1
else:
impares += 1
total = 0
for e in arrayB:
if e % 2 == 0:
total += pares
else:
total += impares
print(total)
``` | output | 1 | 61,578 | 23 | 123,157 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew n distinct lines, given by equations y = x + p_i for some distinct p_1, p_2, …, p_n.
Then JLS drew on the same paper sheet m distinct lines given by equations y = -x + q_i for some distinct q_1, q_2, …, q_m.
DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.
Input
The first line contains one integer t (1 ≤ t ≤ 1000), the number of test cases in the input. Then follow the test case descriptions.
The first line of a test case contains an integer n (1 ≤ n ≤ 10^5), the number of lines drawn by DLS.
The second line of a test case contains n distinct integers p_i (0 ≤ p_i ≤ 10^9) describing the lines drawn by DLS. The integer p_i describes a line given by the equation y = x + p_i.
The third line of a test case contains an integer m (1 ≤ m ≤ 10^5), the number of lines drawn by JLS.
The fourth line of a test case contains m distinct integers q_i (0 ≤ q_i ≤ 10^9) describing the lines drawn by JLS. The integer q_i describes a line given by the equation y = -x + q_i.
The sum of the values of n over all test cases in the input does not exceed 10^5. Similarly, the sum of the values of m over all test cases in the input does not exceed 10^5.
In hacks it is allowed to use only one test case in the input, so t=1 should be satisfied.
Output
For each test case in the input print a single integer — the number of line pairs with integer intersection points.
Example
Input
3
3
1 3 2
2
0 3
1
1
1
1
1
2
1
1
Output
3
1
0
Note
The picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates.
<image>
Submitted Solution:
```
t = int(input())
for j in range(t):
n1 = int(input())
arr1 = list(map(int, input().split()))
n2 = int(input())
arr2 = list(map(int, input().split()))
p = 0
q = 0
for i in arr1:
if i%2==0:
p+=1
for i in arr2:
if i%2==0:
q+=1
print(p*q + (n1-p)*(n2-q))
``` | instruction | 0 | 61,579 | 23 | 123,158 |
Yes | output | 1 | 61,579 | 23 | 123,159 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew n distinct lines, given by equations y = x + p_i for some distinct p_1, p_2, …, p_n.
Then JLS drew on the same paper sheet m distinct lines given by equations y = -x + q_i for some distinct q_1, q_2, …, q_m.
DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.
Input
The first line contains one integer t (1 ≤ t ≤ 1000), the number of test cases in the input. Then follow the test case descriptions.
The first line of a test case contains an integer n (1 ≤ n ≤ 10^5), the number of lines drawn by DLS.
The second line of a test case contains n distinct integers p_i (0 ≤ p_i ≤ 10^9) describing the lines drawn by DLS. The integer p_i describes a line given by the equation y = x + p_i.
The third line of a test case contains an integer m (1 ≤ m ≤ 10^5), the number of lines drawn by JLS.
The fourth line of a test case contains m distinct integers q_i (0 ≤ q_i ≤ 10^9) describing the lines drawn by JLS. The integer q_i describes a line given by the equation y = -x + q_i.
The sum of the values of n over all test cases in the input does not exceed 10^5. Similarly, the sum of the values of m over all test cases in the input does not exceed 10^5.
In hacks it is allowed to use only one test case in the input, so t=1 should be satisfied.
Output
For each test case in the input print a single integer — the number of line pairs with integer intersection points.
Example
Input
3
3
1 3 2
2
0 3
1
1
1
1
1
2
1
1
Output
3
1
0
Note
The picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates.
<image>
Submitted Solution:
```
for _ in range(int(input())):
n=int(input())
ar=list(map(int,input().split()))[:n]
m=int(input())
b=list(map(int,input().split()))[:m]
s=0
eve=0
k=0
t=0
for i in range(n):
if(ar[i]%2):
s=s+1
else:
eve=eve+1
for i in range(m):
if(b[i]%2):
k=k+1
else:
t=t+1
y=(eve*t)+(s*k)
print(y)
``` | instruction | 0 | 61,580 | 23 | 123,160 |
Yes | output | 1 | 61,580 | 23 | 123,161 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew n distinct lines, given by equations y = x + p_i for some distinct p_1, p_2, …, p_n.
Then JLS drew on the same paper sheet m distinct lines given by equations y = -x + q_i for some distinct q_1, q_2, …, q_m.
DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.
Input
The first line contains one integer t (1 ≤ t ≤ 1000), the number of test cases in the input. Then follow the test case descriptions.
The first line of a test case contains an integer n (1 ≤ n ≤ 10^5), the number of lines drawn by DLS.
The second line of a test case contains n distinct integers p_i (0 ≤ p_i ≤ 10^9) describing the lines drawn by DLS. The integer p_i describes a line given by the equation y = x + p_i.
The third line of a test case contains an integer m (1 ≤ m ≤ 10^5), the number of lines drawn by JLS.
The fourth line of a test case contains m distinct integers q_i (0 ≤ q_i ≤ 10^9) describing the lines drawn by JLS. The integer q_i describes a line given by the equation y = -x + q_i.
The sum of the values of n over all test cases in the input does not exceed 10^5. Similarly, the sum of the values of m over all test cases in the input does not exceed 10^5.
In hacks it is allowed to use only one test case in the input, so t=1 should be satisfied.
Output
For each test case in the input print a single integer — the number of line pairs with integer intersection points.
Example
Input
3
3
1 3 2
2
0 3
1
1
1
1
1
2
1
1
Output
3
1
0
Note
The picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates.
<image>
Submitted Solution:
```
t=int(input())
for _ in range(t):
n=int(input())
p=list(map(int,input().split()))
m=int(input())
q=list(map(int,input().split()))
p1=0
q1=0
for i in range(n):
if(p[i]%2==0):
p1+=1
for i in range(m):
if(q[i]%2==0):
q1+=1
print((p1*q1)+(m-q1)*(n-p1))
``` | instruction | 0 | 61,581 | 23 | 123,162 |
Yes | output | 1 | 61,581 | 23 | 123,163 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew n distinct lines, given by equations y = x + p_i for some distinct p_1, p_2, …, p_n.
Then JLS drew on the same paper sheet m distinct lines given by equations y = -x + q_i for some distinct q_1, q_2, …, q_m.
DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.
Input
The first line contains one integer t (1 ≤ t ≤ 1000), the number of test cases in the input. Then follow the test case descriptions.
The first line of a test case contains an integer n (1 ≤ n ≤ 10^5), the number of lines drawn by DLS.
The second line of a test case contains n distinct integers p_i (0 ≤ p_i ≤ 10^9) describing the lines drawn by DLS. The integer p_i describes a line given by the equation y = x + p_i.
The third line of a test case contains an integer m (1 ≤ m ≤ 10^5), the number of lines drawn by JLS.
The fourth line of a test case contains m distinct integers q_i (0 ≤ q_i ≤ 10^9) describing the lines drawn by JLS. The integer q_i describes a line given by the equation y = -x + q_i.
The sum of the values of n over all test cases in the input does not exceed 10^5. Similarly, the sum of the values of m over all test cases in the input does not exceed 10^5.
In hacks it is allowed to use only one test case in the input, so t=1 should be satisfied.
Output
For each test case in the input print a single integer — the number of line pairs with integer intersection points.
Example
Input
3
3
1 3 2
2
0 3
1
1
1
1
1
2
1
1
Output
3
1
0
Note
The picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates.
<image>
Submitted Solution:
```
t=int(input())
while(t!=0):
n=int(input())
p=[int(i) for i in input().split()]
m=int(input())
q=[int(i) for i in input().split()]
c1=0
c2=0
c3=0
c4=0
for i in range(n):
if p[i]%2==0:
c1+=1
else:
c2+=1
for j in range(m):
if q[j]%2==0:
c3+=1
else:
c4+=1
c5=c1*c3+c2*c4
print(c5)
t=t-1
``` | instruction | 0 | 61,582 | 23 | 123,164 |
Yes | output | 1 | 61,582 | 23 | 123,165 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew n distinct lines, given by equations y = x + p_i for some distinct p_1, p_2, …, p_n.
Then JLS drew on the same paper sheet m distinct lines given by equations y = -x + q_i for some distinct q_1, q_2, …, q_m.
DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.
Input
The first line contains one integer t (1 ≤ t ≤ 1000), the number of test cases in the input. Then follow the test case descriptions.
The first line of a test case contains an integer n (1 ≤ n ≤ 10^5), the number of lines drawn by DLS.
The second line of a test case contains n distinct integers p_i (0 ≤ p_i ≤ 10^9) describing the lines drawn by DLS. The integer p_i describes a line given by the equation y = x + p_i.
The third line of a test case contains an integer m (1 ≤ m ≤ 10^5), the number of lines drawn by JLS.
The fourth line of a test case contains m distinct integers q_i (0 ≤ q_i ≤ 10^9) describing the lines drawn by JLS. The integer q_i describes a line given by the equation y = -x + q_i.
The sum of the values of n over all test cases in the input does not exceed 10^5. Similarly, the sum of the values of m over all test cases in the input does not exceed 10^5.
In hacks it is allowed to use only one test case in the input, so t=1 should be satisfied.
Output
For each test case in the input print a single integer — the number of line pairs with integer intersection points.
Example
Input
3
3
1 3 2
2
0 3
1
1
1
1
1
2
1
1
Output
3
1
0
Note
The picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates.
<image>
Submitted Solution:
```
t = int(input())
for i in range(t):
n = int(input())
p = list(map(int, input().split()))
m = int(input())
q = list(map(int, input().split()))
ans = 0
nn = 0
nm = []
for j in range(n):
if p[j] % 2 == 0:
nn += 1
else :
nm.append(p[j])
mm = 0
mn = []
for j in range(m):
if q[j] % 2 == 0: mm += 1
else: mn.append(q[j])
if nn != 0 and mm != 0:
ans += max(nn, mm)
for j in range(len(nm)):
for k in range(len(mn)):
if (nm[j] - mn[k]) % 2 == 0:
ans += 1
print(ans)
``` | instruction | 0 | 61,583 | 23 | 123,166 |
No | output | 1 | 61,583 | 23 | 123,167 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew n distinct lines, given by equations y = x + p_i for some distinct p_1, p_2, …, p_n.
Then JLS drew on the same paper sheet m distinct lines given by equations y = -x + q_i for some distinct q_1, q_2, …, q_m.
DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.
Input
The first line contains one integer t (1 ≤ t ≤ 1000), the number of test cases in the input. Then follow the test case descriptions.
The first line of a test case contains an integer n (1 ≤ n ≤ 10^5), the number of lines drawn by DLS.
The second line of a test case contains n distinct integers p_i (0 ≤ p_i ≤ 10^9) describing the lines drawn by DLS. The integer p_i describes a line given by the equation y = x + p_i.
The third line of a test case contains an integer m (1 ≤ m ≤ 10^5), the number of lines drawn by JLS.
The fourth line of a test case contains m distinct integers q_i (0 ≤ q_i ≤ 10^9) describing the lines drawn by JLS. The integer q_i describes a line given by the equation y = -x + q_i.
The sum of the values of n over all test cases in the input does not exceed 10^5. Similarly, the sum of the values of m over all test cases in the input does not exceed 10^5.
In hacks it is allowed to use only one test case in the input, so t=1 should be satisfied.
Output
For each test case in the input print a single integer — the number of line pairs with integer intersection points.
Example
Input
3
3
1 3 2
2
0 3
1
1
1
1
1
2
1
1
Output
3
1
0
Note
The picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates.
<image>
Submitted Solution:
```
for _ in range(int(input())):
n = int(input())
ai = list(map(int,input().split()))
m = int(input())
bi = list(map(int,input().split()))
odd_ai = [i for i in ai if i%2 != 0]
odd_bi = [i for i in bi if i%2 != 0]
even_bi = m - len(odd_bi)
even_ai = n - len(odd_ai)
#print(odd_ai,odd_bi)
#print(even_ai,even_bi)
res = 0
if len(odd_bi) > 0 and len(odd_ai) > 0:
res += max(len(odd_ai),len(odd_bi))
if even_bi > 0 and even_ai > 0:
res += max(even_ai,even_bi)
print(res)
``` | instruction | 0 | 61,584 | 23 | 123,168 |
No | output | 1 | 61,584 | 23 | 123,169 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.