message stringlengths 2 44.5k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 276 109k | cluster float64 23 23 | __index_level_0__ int64 552 217k |
|---|---|---|---|---|---|
Provide a correct Python 3 solution for this coding contest problem.
<image>
For given three points p0, p1, p2, print
COUNTER_CLOCKWISE
if p0, p1, p2 make a counterclockwise turn (1),
CLOCKWISE
if p0, p1, p2 make a clockwise turn (2),
ONLINE_BACK
if p2 is on a line p2, p0, p1 in this order (3),
ONLINE_FRONT
if p2 is on a line p0, p1, p2 in this order (4),
ON_SEGMENT
if p2 is on a segment p0p1 (5).
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* p0 and p1 are not identical.
Input
xp0 yp0 xp1 yp1
q
xp20 yp20
xp21 yp21
...
xp2q-1 yp2q-1
In the first line, integer coordinates of p0 and p1 are given. Then, q queries are given for integer coordinates of p2.
Output
For each query, print the above mentioned status.
Examples
Input
0 0 2 0
2
-1 1
-1 -1
Output
COUNTER_CLOCKWISE
CLOCKWISE
Input
0 0 2 0
3
-1 0
0 0
3 0
Output
ONLINE_BACK
ON_SEGMENT
ONLINE_FRONT | instruction | 0 | 85,693 | 23 | 171,386 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10 ** 9)
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
INF=float('inf')
class Geometry:
EPS = 10 ** -9
def add(self, a, b):
x1, y1 = a
x2, y2 = b
return (x1+x2, y1+y2)
def sub(self, a, b):
x1, y1 = a
x2, y2 = b
return (x1-x2, y1-y2)
def mul(self, a, b):
x1, y1 = a
if not isinstance(b, tuple):
return (x1*b, y1*b)
x2, y2 = b
return (x1*x2, y1*y2)
def norm(self, a):
x, y = a
return x**2 + y**2
def dot(self, a, b):
x1, y1 = a
x2, y2 = b
return x1*x2 + y1*y2
def cross(self, a, b):
x1, y1 = a
x2, y2 = b
return x1*y2 - y1*x2
def project(self, seg, p):
""" 線分segに対する点pの射影 """
p1, p2 = seg
base = self.sub(p2, p1)
r = self.dot(self.sub(p, p1), base) / self.norm(base)
return self.add(p1, self.mul(base, r))
def reflect(self, seg, p):
""" 線分segを対称軸とした点pの線対称の点 """
return self.add(p, self.mul(self.sub(self.project(seg, p), p), 2))
def ccw(self, p0, p1, p2):
""" 線分p0,p1から線分p0,p2への回転方向 """
a = self.sub(p1, p0)
b = self.sub(p2, p0)
# 反時計回り
if self.cross(a, b) > self.EPS: return 1
# 時計回り
if self.cross(a, b) < -self.EPS: return -1
# 直線上(p2 => p0 => p1)
if self.dot(a, b) < -self.EPS: return 2
# 直線上(p0 => p1 => p2)
if self.norm(a) < self.norm(b): return -2
# 直線上(p0 => p2 => p1)
return 0
gm = Geometry()
x1, y1, x2, y2 = MAP()
a, b = (x1, y1), (x2, y2)
Q = INT()
for i in range(Q):
x3, y3 = MAP()
res = gm.ccw(a, b, (x3, y3))
if res == 1:
print('COUNTER_CLOCKWISE')
elif res == -1:
print('CLOCKWISE')
elif res == 2:
print('ONLINE_BACK')
elif res == -2:
print('ONLINE_FRONT')
elif res == 0:
print('ON_SEGMENT')
``` | output | 1 | 85,693 | 23 | 171,387 |
Provide a correct Python 3 solution for this coding contest problem.
<image>
For given three points p0, p1, p2, print
COUNTER_CLOCKWISE
if p0, p1, p2 make a counterclockwise turn (1),
CLOCKWISE
if p0, p1, p2 make a clockwise turn (2),
ONLINE_BACK
if p2 is on a line p2, p0, p1 in this order (3),
ONLINE_FRONT
if p2 is on a line p0, p1, p2 in this order (4),
ON_SEGMENT
if p2 is on a segment p0p1 (5).
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* p0 and p1 are not identical.
Input
xp0 yp0 xp1 yp1
q
xp20 yp20
xp21 yp21
...
xp2q-1 yp2q-1
In the first line, integer coordinates of p0 and p1 are given. Then, q queries are given for integer coordinates of p2.
Output
For each query, print the above mentioned status.
Examples
Input
0 0 2 0
2
-1 1
-1 -1
Output
COUNTER_CLOCKWISE
CLOCKWISE
Input
0 0 2 0
3
-1 0
0 0
3 0
Output
ONLINE_BACK
ON_SEGMENT
ONLINE_FRONT | instruction | 0 | 85,694 | 23 | 171,388 |
"Correct Solution:
```
import math
class Vector:
def __init__(self,x,y):
self.x = x
self.y = y
def __add__(self,other):
return Vector(self.x+other.x,self.y+other.y)
def __sub__(self,other):
return Vector(self.x-other.x,self.y-other.y)
def __mul__(self,scalar):
return Vector(self.x*scalar,self.y*scalar)
def __rmul__(self, scalar):
return Vector(self.x*scalar,self.y*scalar)
def __repr__(self):
return str([self.x,self.y])
def norm_2(self):
return dot(self,self)
def norm(self):
return math.sqrt(self.norm_2())
def v_sum(v1,v2):
return Vector(v1.x+v2.x,v1.y+v2.y)
def scalar_multi(k,v):
return Vector(k*v.x,k*v.y)
def v_diff(v1,v2):
return v_sum(v1,scalar_multi(-1,v2))
def dot(vector1,vector2):
return vector1.x*vector2.x+vector1.y*vector2.y
def cross(vector1,vector2):
return vector1.x*vector2.y-vector1.y*vector2.x
x1,y1,x2,y2 = map(float,input().split())
p1 = Vector(x1,y1)
p2 = Vector(x2,y2)
q = int(input())
for i in range(q):
x,y = map(float,input().split())
p3 = Vector(x,y)
a = p2-p1
v = p3-p1
if cross(a,v) > 0:
print('COUNTER_CLOCKWISE')
elif cross(a,v) < 0:
print('CLOCKWISE')
else:
if dot(a,v)<0:
print('ONLINE_BACK')
elif dot(a,v)>a.norm_2():
print('ONLINE_FRONT')
else:
print('ON_SEGMENT')
``` | output | 1 | 85,694 | 23 | 171,389 |
Provide a correct Python 3 solution for this coding contest problem.
<image>
For given three points p0, p1, p2, print
COUNTER_CLOCKWISE
if p0, p1, p2 make a counterclockwise turn (1),
CLOCKWISE
if p0, p1, p2 make a clockwise turn (2),
ONLINE_BACK
if p2 is on a line p2, p0, p1 in this order (3),
ONLINE_FRONT
if p2 is on a line p0, p1, p2 in this order (4),
ON_SEGMENT
if p2 is on a segment p0p1 (5).
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* p0 and p1 are not identical.
Input
xp0 yp0 xp1 yp1
q
xp20 yp20
xp21 yp21
...
xp2q-1 yp2q-1
In the first line, integer coordinates of p0 and p1 are given. Then, q queries are given for integer coordinates of p2.
Output
For each query, print the above mentioned status.
Examples
Input
0 0 2 0
2
-1 1
-1 -1
Output
COUNTER_CLOCKWISE
CLOCKWISE
Input
0 0 2 0
3
-1 0
0 0
3 0
Output
ONLINE_BACK
ON_SEGMENT
ONLINE_FRONT | instruction | 0 | 85,695 | 23 | 171,390 |
"Correct Solution:
```
import cmath
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
INF = float("inf")
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, x: float, y: float):
self.c = complex(x, y)
@property
def x(self):
return self.c.real
@property
def y(self):
return self.c.imag
@staticmethod
def from_complex(c: complex):
return Point(c.real, c.imag)
@staticmethod
def from_polar(r: float, phi: float):
c = cmath.rect(r, phi)
return Point(c.real, c.imag)
def __add__(self, p):
"""
:param Point p:
"""
c = self.c + p.c
return Point(c.real, c.imag)
def __iadd__(self, p):
"""
:param Point p:
"""
self.c += p.c
return self
def __sub__(self, p):
"""
:param Point p:
"""
c = self.c - p.c
return Point(c.real, c.imag)
def __isub__(self, p):
"""
:param Point p:
"""
self.c -= p.c
return self
def __mul__(self, f: float):
c = self.c * f
return Point(c.real, c.imag)
def __imul__(self, f: float):
self.c *= f
return self
def __truediv__(self, f: float):
c = self.c / f
return Point(c.real, c.imag)
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):
c = -self.c
return Point(c.real, c.imag)
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 b.dot(b - c) < -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
x1, y1, x2, y2 = list(map(int, sys.stdin.buffer.readline().split()))
Q = int(sys.stdin.buffer.readline())
XY = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(Q)]
p1 = Point(x1, y1)
p2 = Point(x2, y2)
for x3, y3 in XY:
ccw = Point.ccw(p1, p2, Point(x3, y3))
if ccw == Point.CCW_COUNTER_CLOCKWISE:
print('COUNTER_CLOCKWISE')
if ccw == Point.CCW_CLOCKWISE:
print('CLOCKWISE')
if ccw == Point.CCW_ONLINE_BACK:
print('ONLINE_BACK')
if ccw == Point.CCW_ONLINE_FRONT:
print('ONLINE_FRONT')
if ccw == Point.CCW_ON_SEGMENT:
print('ON_SEGMENT')
``` | output | 1 | 85,695 | 23 | 171,391 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image>
For given three points p0, p1, p2, print
COUNTER_CLOCKWISE
if p0, p1, p2 make a counterclockwise turn (1),
CLOCKWISE
if p0, p1, p2 make a clockwise turn (2),
ONLINE_BACK
if p2 is on a line p2, p0, p1 in this order (3),
ONLINE_FRONT
if p2 is on a line p0, p1, p2 in this order (4),
ON_SEGMENT
if p2 is on a segment p0p1 (5).
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* p0 and p1 are not identical.
Input
xp0 yp0 xp1 yp1
q
xp20 yp20
xp21 yp21
...
xp2q-1 yp2q-1
In the first line, integer coordinates of p0 and p1 are given. Then, q queries are given for integer coordinates of p2.
Output
For each query, print the above mentioned status.
Examples
Input
0 0 2 0
2
-1 1
-1 -1
Output
COUNTER_CLOCKWISE
CLOCKWISE
Input
0 0 2 0
3
-1 0
0 0
3 0
Output
ONLINE_BACK
ON_SEGMENT
ONLINE_FRONT
Submitted Solution:
```
def LI(): return list(map(int, input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()
def LIR(n): return [LI() for i in range(n)]
def MI(): return map(int, input().split())
#1
#1_A
"""
x,y,s,t = map(float, input().split())
a = int(input())
s-=x
t-=y
while a:
a -= 1
p,q = map(float, input().split())
p-=x
q-=y
ans_x = s*(q*t+p*s)/(t*t+s*s)
ans_y = t*(q*t+p*s)/(t*t+s*s)
print(x+ans_x, y+ans_y)
"""
#1_B
"""
p1x,p1y,c,d = MI()
q = II()
if p1x == c:
f = 0
elif p1y == d:
f = 1
else:
f = 2
m = (d-p1y)/(c-p1x)
for _ in range(q):
px,py = MI()
if not f:
a = 2*p1x-px
b = py
elif f == 1:
a = px
b = 2*p1y-py
else:
a = (2*py+(1/m-m)*px+2*m*p1x-2*p1y)/(m+1/m)
b = -1/m*(a-px)+py
print(a,b)
"""
#1_C
def inner_product(a,b):
return a[0]*b[0]+a[1]*b[1]
def cross_product(a,b):
return a[0]*b[1]-a[1]*b[0]
p,q,c,d = MI()
a = [c-p,d-q]
r = II()
for _ in range(r):
c,d = MI()
b = [c-p,d-q]
co = inner_product(a,b)
si = cross_product(a,b)
if si == 0:
if co >= 0:
if a[0]**2+a[1]**2 >= b[0]**2+b[1]**2:
print("ON_SEGMENT")
else:
print("ONLINE_FRONT")
else:
print("ONLINE_BACK")
elif si > 0:
print("COUNTER_CLOCKWISE")
else:
print("CLOCKWISE")
``` | instruction | 0 | 85,696 | 23 | 171,392 |
Yes | output | 1 | 85,696 | 23 | 171,393 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image>
For given three points p0, p1, p2, print
COUNTER_CLOCKWISE
if p0, p1, p2 make a counterclockwise turn (1),
CLOCKWISE
if p0, p1, p2 make a clockwise turn (2),
ONLINE_BACK
if p2 is on a line p2, p0, p1 in this order (3),
ONLINE_FRONT
if p2 is on a line p0, p1, p2 in this order (4),
ON_SEGMENT
if p2 is on a segment p0p1 (5).
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* p0 and p1 are not identical.
Input
xp0 yp0 xp1 yp1
q
xp20 yp20
xp21 yp21
...
xp2q-1 yp2q-1
In the first line, integer coordinates of p0 and p1 are given. Then, q queries are given for integer coordinates of p2.
Output
For each query, print the above mentioned status.
Examples
Input
0 0 2 0
2
-1 1
-1 -1
Output
COUNTER_CLOCKWISE
CLOCKWISE
Input
0 0 2 0
3
-1 0
0 0
3 0
Output
ONLINE_BACK
ON_SEGMENT
ONLINE_FRONT
Submitted Solution:
```
import cmath
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 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 solve(p, q, r):
if abs(OuterProduct(q-r, p-r)) <= EPS:
if InnerProduct(q-r, p-r) <= EPS:
return "ON_SEGMENT"
elif abs(p-r) < abs(q-r):
return "ONLINE_BACK"
else:
return "ONLINE_FRONT"
elif CCW(p, q, r):
return "COUNTER_CLOCKWISE"
else:
return "CLOCKWISE"
a, b, c, d = map(int, input().split())
p, q = complex(a, b), complex(c, d)
n = int(input())
for _ in range(n):
x, y = map(int, input().split())
r = complex(x, y)
print(solve(p, q, r))
``` | instruction | 0 | 85,697 | 23 | 171,394 |
Yes | output | 1 | 85,697 | 23 | 171,395 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image>
For given three points p0, p1, p2, print
COUNTER_CLOCKWISE
if p0, p1, p2 make a counterclockwise turn (1),
CLOCKWISE
if p0, p1, p2 make a clockwise turn (2),
ONLINE_BACK
if p2 is on a line p2, p0, p1 in this order (3),
ONLINE_FRONT
if p2 is on a line p0, p1, p2 in this order (4),
ON_SEGMENT
if p2 is on a segment p0p1 (5).
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* p0 and p1 are not identical.
Input
xp0 yp0 xp1 yp1
q
xp20 yp20
xp21 yp21
...
xp2q-1 yp2q-1
In the first line, integer coordinates of p0 and p1 are given. Then, q queries are given for integer coordinates of p2.
Output
For each query, print the above mentioned status.
Examples
Input
0 0 2 0
2
-1 1
-1 -1
Output
COUNTER_CLOCKWISE
CLOCKWISE
Input
0 0 2 0
3
-1 0
0 0
3 0
Output
ONLINE_BACK
ON_SEGMENT
ONLINE_FRONT
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 Order(a,b):
crs = cross(a,b)
if crs > 0 : return "COUNTER_CLOCKWISE"
elif crs < 0 : return "CLOCKWISE"
else:
if dot(a,b) < 0 : return "ONLINE_BACK"
elif dot(a,a) < dot(b,b) : return "ONLINE_FRONT"
else : return "ON_SEGMENT"
x0,y0,x1,y1 = [int(i) for i in input().split()]
a = [x1-x0,y1-y0]
q = int(input())
for i in range(q):
x2,y2 = [int(i) for i in input().split()]
b = [x2-x0,y2-y0]
print(Order(a,b))
``` | instruction | 0 | 85,698 | 23 | 171,396 |
Yes | output | 1 | 85,698 | 23 | 171,397 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image>
For given three points p0, p1, p2, print
COUNTER_CLOCKWISE
if p0, p1, p2 make a counterclockwise turn (1),
CLOCKWISE
if p0, p1, p2 make a clockwise turn (2),
ONLINE_BACK
if p2 is on a line p2, p0, p1 in this order (3),
ONLINE_FRONT
if p2 is on a line p0, p1, p2 in this order (4),
ON_SEGMENT
if p2 is on a segment p0p1 (5).
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* p0 and p1 are not identical.
Input
xp0 yp0 xp1 yp1
q
xp20 yp20
xp21 yp21
...
xp2q-1 yp2q-1
In the first line, integer coordinates of p0 and p1 are given. Then, q queries are given for integer coordinates of p2.
Output
For each query, print the above mentioned status.
Examples
Input
0 0 2 0
2
-1 1
-1 -1
Output
COUNTER_CLOCKWISE
CLOCKWISE
Input
0 0 2 0
3
-1 0
0 0
3 0
Output
ONLINE_BACK
ON_SEGMENT
ONLINE_FRONT
Submitted Solution:
```
#!/usr/bin/env python3
import enum
EPS = 1e-10
class PointsRelation(enum.Enum):
counter_clockwise = 1
clockwise = 2
online_back = 3
on_segment = 4
online_front = 5
def inner_product(v1, v2):
return v1.real * v2.real + v1.imag * v2.imag
def outer_product(v1, v2):
return v1.real * v2.imag - v1.imag * v2.real
def judge_relation(p0, p1, p2):
v1 = p1 - p0
v2 = p2 - p0
op = outer_product(v1, v2)
if op > EPS:
return PointsRelation.counter_clockwise
elif op < -EPS:
return PointsRelation.clockwise
elif inner_product(v1, v2) < -EPS:
return PointsRelation.online_back
elif abs(v1) < abs(v2):
return PointsRelation.online_front
else:
return PointsRelation.on_segment
def main():
x_p0, y_p0, x_p1, y_p1 = map(float, input().split())
p0 = complex(x_p0, y_p0)
p1 = complex(x_p1, y_p1)
q = int(input())
for _ in range(q):
p2 = complex(*map(float, input().split()))
ans = judge_relation(p0, p1, p2)
if ans == PointsRelation.counter_clockwise:
print("COUNTER_CLOCKWISE")
elif ans == PointsRelation.clockwise:
print("CLOCKWISE")
elif ans == PointsRelation.online_back:
print("ONLINE_BACK")
elif ans == PointsRelation.online_front:
print("ONLINE_FRONT")
else:
print("ON_SEGMENT")
if __name__ == '__main__':
main()
``` | instruction | 0 | 85,699 | 23 | 171,398 |
Yes | output | 1 | 85,699 | 23 | 171,399 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image>
For given three points p0, p1, p2, print
COUNTER_CLOCKWISE
if p0, p1, p2 make a counterclockwise turn (1),
CLOCKWISE
if p0, p1, p2 make a clockwise turn (2),
ONLINE_BACK
if p2 is on a line p2, p0, p1 in this order (3),
ONLINE_FRONT
if p2 is on a line p0, p1, p2 in this order (4),
ON_SEGMENT
if p2 is on a segment p0p1 (5).
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* p0 and p1 are not identical.
Input
xp0 yp0 xp1 yp1
q
xp20 yp20
xp21 yp21
...
xp2q-1 yp2q-1
In the first line, integer coordinates of p0 and p1 are given. Then, q queries are given for integer coordinates of p2.
Output
For each query, print the above mentioned status.
Examples
Input
0 0 2 0
2
-1 1
-1 -1
Output
COUNTER_CLOCKWISE
CLOCKWISE
Input
0 0 2 0
3
-1 0
0 0
3 0
Output
ONLINE_BACK
ON_SEGMENT
ONLINE_FRONT
Submitted Solution:
```
#! /usr/bin/env python3
from typing import List, Tuple
from math import sqrt
from enum import Enum
EPS = 1e-10
def float_equal(x: float, y: float) -> bool:
return abs(x - y) < EPS
class PointLocation(Enum):
COUNTER_CLOCKWISE = 1
CLOCKWISE = 2
ONLINE_BACK = 3
ONLINE_FRONT = 4
ON_SEGMENT = 5
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 < 0:
return PointLocation.CLOCKWISE
elif d > 0:
return PointLocation.COUNTER_CLOCKWISE
else:
r = (self.x - seg.p1.x) / (seg.p2.x - seg.p1.x)
if r < 0:
return PointLocation.ONLINE_BACK
elif r > 1:
return PointLocation.ONLINE__FRONT
else:
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:
s, t = self.intersect_ratio(other)
return (0 <= s <= 1) and (0 <= t <= 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:
x0, y0, x1, y1 = [int(x) for x in input().split()]
s = Segment(Point(x0, y0), Point(x1, y1))
q = int(input())
for _ in range(q):
x2, y2 = [int(x) for x in input().split()]
print(Point(x2, y2).location(s).name)
if __name__ == "__main__":
main()
``` | instruction | 0 | 85,700 | 23 | 171,400 |
No | output | 1 | 85,700 | 23 | 171,401 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image>
For given three points p0, p1, p2, print
COUNTER_CLOCKWISE
if p0, p1, p2 make a counterclockwise turn (1),
CLOCKWISE
if p0, p1, p2 make a clockwise turn (2),
ONLINE_BACK
if p2 is on a line p2, p0, p1 in this order (3),
ONLINE_FRONT
if p2 is on a line p0, p1, p2 in this order (4),
ON_SEGMENT
if p2 is on a segment p0p1 (5).
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* p0 and p1 are not identical.
Input
xp0 yp0 xp1 yp1
q
xp20 yp20
xp21 yp21
...
xp2q-1 yp2q-1
In the first line, integer coordinates of p0 and p1 are given. Then, q queries are given for integer coordinates of p2.
Output
For each query, print the above mentioned status.
Examples
Input
0 0 2 0
2
-1 1
-1 -1
Output
COUNTER_CLOCKWISE
CLOCKWISE
Input
0 0 2 0
3
-1 0
0 0
3 0
Output
ONLINE_BACK
ON_SEGMENT
ONLINE_FRONT
Submitted Solution:
```
class Point:
def __init__(self, x , y):
self.x = x
self.y = y
def __sub__(self, p):
x_sub = self.x - p.x
y_sub = self.y - p.y
return Point(x_sub, y_sub)
class Vector:
def __init__(self, p):
self.x = p.x
self.y = p.y
def norm(self):
return (self.x ** 2 + self.y ** 2) ** 0.5
def cross(v1, v2):
return v1.x * v2.y - v1.y * v2.x
def dot(v1, v2):
return v1.x * v2.x + v1.x * v2.x
def ccw(p0, p1, p2):
a = Vector(p1 - p0)
b = Vector(p2 - p0)
cross_ab = cross(a, b)
if cross_ab > 0:
print("COUNTER_CLOCKWISE")
elif cross_ab < 0:
print("CLOCKWISE")
elif dot(a, b) < 0:
print("ONLINE_BACK")
elif a.norm() < b.norm():
print("ONLINE_FRONT")
else:
print("ON_SEGMENT")
import sys
file_input = sys.stdin
x_p0, y_p0, x_p1, y_p1 = map(int, file_input.readline().split())
p0 = Point(x_p0, y_p0)
p1 = Point(x_p1, y_p1)
q = map(int, file_input.readline())
for line in file_input:
x_p2, y_p2 = map(int, line.split())
p2 = Point(x_p2, y_p2)
ccw(p0, p1, p2)
``` | instruction | 0 | 85,701 | 23 | 171,402 |
No | output | 1 | 85,701 | 23 | 171,403 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image>
For given three points p0, p1, p2, print
COUNTER_CLOCKWISE
if p0, p1, p2 make a counterclockwise turn (1),
CLOCKWISE
if p0, p1, p2 make a clockwise turn (2),
ONLINE_BACK
if p2 is on a line p2, p0, p1 in this order (3),
ONLINE_FRONT
if p2 is on a line p0, p1, p2 in this order (4),
ON_SEGMENT
if p2 is on a segment p0p1 (5).
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* p0 and p1 are not identical.
Input
xp0 yp0 xp1 yp1
q
xp20 yp20
xp21 yp21
...
xp2q-1 yp2q-1
In the first line, integer coordinates of p0 and p1 are given. Then, q queries are given for integer coordinates of p2.
Output
For each query, print the above mentioned status.
Examples
Input
0 0 2 0
2
-1 1
-1 -1
Output
COUNTER_CLOCKWISE
CLOCKWISE
Input
0 0 2 0
3
-1 0
0 0
3 0
Output
ONLINE_BACK
ON_SEGMENT
ONLINE_FRONT
Submitted Solution:
```
import math
class Point():
def __init__(self, x, y):
self.x = x
self.y = y
class Vector():
def __init__(self, start, end):
self.x = end.x - start.x
self.y = end.y - start.y
self.r = math.sqrt(pow(self.x, 2) + pow(self.y, 2))
self.theta = math.atan2(self.y, self.x)
x0, y0, x1, y1 = list(map(int, input().split(' ')))
p0, p1 = Point(x0, y0), Point(x1, y1)
vec1 = Vector(p0, p1)
q = int(input())
for i in range(q):
x2, y2 = list(map(int, input().split(' ')))
p2 = Point(x2, y2)
vec2 = Vector(p0, p2)
if vec1.theta == vec2.theta:
if vec1.r < vec2.r:
print('ONLINE_FRONT')
else:
print('ON_SEGMENT')
elif abs(vec1.theta - vec2.theta) == math.pi:
print('ONLINE_BACK')
elif vec2.theta - vec1.theta > 0:
print('COUNTER_CLOCKWISE')
elif vec2.theta - vec1.theta < 0:
print('CLOCKWISE')
``` | instruction | 0 | 85,702 | 23 | 171,404 |
No | output | 1 | 85,702 | 23 | 171,405 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
<image>
For given three points p0, p1, p2, print
COUNTER_CLOCKWISE
if p0, p1, p2 make a counterclockwise turn (1),
CLOCKWISE
if p0, p1, p2 make a clockwise turn (2),
ONLINE_BACK
if p2 is on a line p2, p0, p1 in this order (3),
ONLINE_FRONT
if p2 is on a line p0, p1, p2 in this order (4),
ON_SEGMENT
if p2 is on a segment p0p1 (5).
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* p0 and p1 are not identical.
Input
xp0 yp0 xp1 yp1
q
xp20 yp20
xp21 yp21
...
xp2q-1 yp2q-1
In the first line, integer coordinates of p0 and p1 are given. Then, q queries are given for integer coordinates of p2.
Output
For each query, print the above mentioned status.
Examples
Input
0 0 2 0
2
-1 1
-1 -1
Output
COUNTER_CLOCKWISE
CLOCKWISE
Input
0 0 2 0
3
-1 0
0 0
3 0
Output
ONLINE_BACK
ON_SEGMENT
ONLINE_FRONT
Submitted Solution:
```
#! /usr/bin/env python3
from typing import List, Tuple
from math import sqrt
from enum import Enum
EPS = 1e-10
def float_equal(x: float, y: float) -> bool:
return abs(x - y) < EPS
class PointLocation(Enum):
COUNTER_CLOCKWISE = 1
CLOCKWISE = 2
ONLINE_BACK = 3
ONLINE_FRONT = 4
ON_SEGMENT = 5
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 < 0:
return PointLocation.CLOCKWISE
elif d > 0:
return PointLocation.COUNTER_CLOCKWISE
else:
r = (self.x - seg.p1.x) / (seg.p2.x - seg.p1.x)
if r < 0:
return PointLocation.ONLINE_BACK
elif r > 1:
return PointLocation.ONLINE_FRONT
else:
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:
s, t = self.intersect_ratio(other)
return (0 <= s <= 1) and (0 <= t <= 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:
x0, y0, x1, y1 = [int(x) for x in input().split()]
s = Segment(Point(x0, y0), Point(x1, y1))
q = int(input())
for _ in range(q):
x2, y2 = [int(x) for x in input().split()]
print(Point(x2, y2).location(s).name)
if __name__ == "__main__":
main()
``` | instruction | 0 | 85,703 | 23 | 171,406 |
No | output | 1 | 85,703 | 23 | 171,407 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a_1, a_2, ... , a_n, which is sorted in non-decreasing order (a_i ≤ a_{i + 1}).
Find three indices i, j, k such that 1 ≤ i < j < k ≤ n and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to a_i, a_j and a_k (for example it is possible to construct a non-degenerate triangle with sides 3, 4 and 5 but impossible with sides 3, 4 and 7). If it is impossible to find such triple, report it.
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains one integer n (3 ≤ n ≤ 5 ⋅ 10^4) — the length of the array a.
The second line of each test case contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9; a_{i - 1} ≤ a_i) — the array a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print the answer to it in one line.
If there is a triple of indices i, j, k (i < j < k) such that it is impossible to construct a non-degenerate triangle having sides equal to a_i, a_j and a_k, print that three indices in ascending order. If there are multiple answers, print any of them.
Otherwise, print -1.
Example
Input
3
7
4 6 11 11 15 18 20
4
10 10 10 11
3
1 1 1000000000
Output
2 3 6
-1
1 2 3
Note
In the first test case it is impossible with sides 6, 11 and 18. Note, that this is not the only correct answer.
In the second test case you always can construct a non-degenerate triangle.
Submitted Solution:
```
for i in range(int(input())):
n = int(input())
a = list(map(int,input().split()))
print("1 2 "+str(n) if len(a) >= 3 and a[0]+a[1] <= a[n-1] else -1)
``` | instruction | 0 | 85,879 | 23 | 171,758 |
Yes | output | 1 | 85,879 | 23 | 171,759 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a_1, a_2, ... , a_n, which is sorted in non-decreasing order (a_i ≤ a_{i + 1}).
Find three indices i, j, k such that 1 ≤ i < j < k ≤ n and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to a_i, a_j and a_k (for example it is possible to construct a non-degenerate triangle with sides 3, 4 and 5 but impossible with sides 3, 4 and 7). If it is impossible to find such triple, report it.
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains one integer n (3 ≤ n ≤ 5 ⋅ 10^4) — the length of the array a.
The second line of each test case contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9; a_{i - 1} ≤ a_i) — the array a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print the answer to it in one line.
If there is a triple of indices i, j, k (i < j < k) such that it is impossible to construct a non-degenerate triangle having sides equal to a_i, a_j and a_k, print that three indices in ascending order. If there are multiple answers, print any of them.
Otherwise, print -1.
Example
Input
3
7
4 6 11 11 15 18 20
4
10 10 10 11
3
1 1 1000000000
Output
2 3 6
-1
1 2 3
Note
In the first test case it is impossible with sides 6, 11 and 18. Note, that this is not the only correct answer.
In the second test case you always can construct a non-degenerate triangle.
Submitted Solution:
```
#_______________________________________________________________#
def fact(x):
if x == 0:
return 1
else:
return x * fact(x-1)
def lower_bound(li, num): #return 0 if all are greater or equal to
answer = len(li)-1
start = 0
end = len(li)-1
while(start <= end):
middle = (end+start)//2
if li[middle] >= num:
answer = middle
end = middle - 1
else:
start = middle + 1
return answer #index where x is not less than num
def upper_bound(li, num): #return n-1 if all are small or equal
answer = -1
start = 0
end = len(li)-1
while(start <= end):
middle = (end+start)//2
if li[middle] <= num:
answer = middle
start = middle + 1
else:
end = middle - 1
return answer #index where x is not greater than num
def abs(x):
return x if x >=0 else -x
def binary_search(li, val, lb, ub): # ITS A BINARY
ans = 0
while(lb <= ub):
mid = (lb+ub)//2
#print(mid, li[mid])
if li[mid] > val:
ub = mid-1
elif val > li[mid]:
lb = mid + 1
else:
ans = 1
break
return ans
def sieve_of_eratosthenes(n):
ans = []
arr = [1]*(n+1)
arr[0],arr[1], i = 0, 0, 2
while(i*i <= n):
if arr[i] == 1:
j = i+i
while(j <= n):
arr[j] = 0
j += i
i += 1
for k in range(n):
if arr[k] == 1:
ans.append(k)
return ans
def nc2(x):
if x == 1:
return 0
else:
return x*(x-1)//2
def kadane(x): #maximum subarray sum
sum_so_far = 0
current_sum = 0
for i in x:
current_sum += i
if current_sum < 0:
current_sum = 0
else:
sum_so_far = max(sum_so_far,current_sum)
return sum_so_far
def mex(li):
check = [0]*1001
for i in li:
check[i] += 1
for i in range(len(check)):
if check[i] == 0:
return i
def sumdigits(n):
ans = 0
while(n!=0):
ans += n%10
n //= 10
return ans
def product(li):
ans = 1
for i in li:
ans *= i
return ans
def maxpower(n,k):
cnt = 0
while(n>1):
cnt += 1
n //= k
return cnt
def knapsack(li,sumi,s,n,dp):
if s == sumi//2:
return 1
elif n==0:
return 0
else:
if dp[n][s] != -1:
return dp[n][s]
else:
if li[n-1] <= s:
dp[n][s] = knapsack(li,sumi,s-li[n-1],n-1,dp) or knapsack(li,sumi,s,n-1,dp)
else:
dp[n][s] = knapsack(li,sumi,s,n-1,dp)
return dp[n][s]
def pref(li):
pref_sum = [0]
for i in li:
pref_sum.append(pref_sum[-1] + i)
return pref_sum
#_______________________________________________________________#
'''
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▄███████▀▀▀▀▀▀███████▄
░▐████▀▒▒Aestroix▒▒▀██████
░███▀▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▀████
░▐██▒▒▒▒▒KARMANYA▒▒▒▒▒▒████▌ ________________
░▐█▌▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒████▌ ? ? |▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒|
░░█▒▒▄▀▀▀▀▀▄▒▒▄▀▀▀▀▀▄▒▒▐███▌ ? |___CM ONE DAY___|
░░░▐░░░▄▄░░▌▐░░░▄▄░░▌▒▐███▌ ? ? |▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒|
░▄▀▌░░░▀▀░░▌▐░░░▀▀░░▌▒▀▒█▌ ? ?
░▌▒▀▄░░░░▄▀▒▒▀▄░░░▄▀▒▒▄▀▒▌ ?
░▀▄▐▒▀▀▀▀▒▒▒▒▒▒▀▀▀▒▒▒▒▒▒█ ? ?
░░░▀▌▒▄██▄▄▄▄████▄▒▒▒▒█▀ ?
░░░░▄█████████ ████=========█▒▒▐▌
░░░▀███▀▀████▀█████▀▒▌
░░░░░▌▒▒▒▄▒▒▒▄▒▒▒▒▒▒▐
░░░░░▌▒▒▒▒▀▀▀▒▒▒▒▒▒▒▐
░░░░░████████████████
'''
from math import *
for _ in range(int(input())):
#for _ in range(1):
n = int(input())
#n,m,sx,sy = map(int,input().split())
#s = input()
#s2 = input()
#r, g, b, w = map(int,input().split())
a = list(map(int,input().split()))
#b = list(map(int,input().split()))
#s1 = list(st)
#a = [int(x) for x in list(s1)]
#b = [int(x) for x in list(s2)]
f = 0
ans = lower_bound(a,a[0]+a[1])
if a[ans] < a[0]+a[1]:
print(-1)
else:
print(1,2,ans+1)
``` | instruction | 0 | 85,880 | 23 | 171,760 |
Yes | output | 1 | 85,880 | 23 | 171,761 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a_1, a_2, ... , a_n, which is sorted in non-decreasing order (a_i ≤ a_{i + 1}).
Find three indices i, j, k such that 1 ≤ i < j < k ≤ n and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to a_i, a_j and a_k (for example it is possible to construct a non-degenerate triangle with sides 3, 4 and 5 but impossible with sides 3, 4 and 7). If it is impossible to find such triple, report it.
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains one integer n (3 ≤ n ≤ 5 ⋅ 10^4) — the length of the array a.
The second line of each test case contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9; a_{i - 1} ≤ a_i) — the array a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print the answer to it in one line.
If there is a triple of indices i, j, k (i < j < k) such that it is impossible to construct a non-degenerate triangle having sides equal to a_i, a_j and a_k, print that three indices in ascending order. If there are multiple answers, print any of them.
Otherwise, print -1.
Example
Input
3
7
4 6 11 11 15 18 20
4
10 10 10 11
3
1 1 1000000000
Output
2 3 6
-1
1 2 3
Note
In the first test case it is impossible with sides 6, 11 and 18. Note, that this is not the only correct answer.
In the second test case you always can construct a non-degenerate triangle.
Submitted Solution:
```
t=int(input())
for _ in range(t):
n=int(input())
a=list(map(int,input().split()))
if a[-1]>=a[0]+a[1]:
print(1,2,n)
else:
print(-1)
``` | instruction | 0 | 85,881 | 23 | 171,762 |
Yes | output | 1 | 85,881 | 23 | 171,763 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a_1, a_2, ... , a_n, which is sorted in non-decreasing order (a_i ≤ a_{i + 1}).
Find three indices i, j, k such that 1 ≤ i < j < k ≤ n and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to a_i, a_j and a_k (for example it is possible to construct a non-degenerate triangle with sides 3, 4 and 5 but impossible with sides 3, 4 and 7). If it is impossible to find such triple, report it.
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains one integer n (3 ≤ n ≤ 5 ⋅ 10^4) — the length of the array a.
The second line of each test case contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9; a_{i - 1} ≤ a_i) — the array a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print the answer to it in one line.
If there is a triple of indices i, j, k (i < j < k) such that it is impossible to construct a non-degenerate triangle having sides equal to a_i, a_j and a_k, print that three indices in ascending order. If there are multiple answers, print any of them.
Otherwise, print -1.
Example
Input
3
7
4 6 11 11 15 18 20
4
10 10 10 11
3
1 1 1000000000
Output
2 3 6
-1
1 2 3
Note
In the first test case it is impossible with sides 6, 11 and 18. Note, that this is not the only correct answer.
In the second test case you always can construct a non-degenerate triangle.
Submitted Solution:
```
for _ in range(int(input())):
n=int(input())
arr=list(map(int,input().split()))
if(arr[0]+arr[1]>arr[n-1]):
print("-1")
else:
print("1 2",n)
``` | instruction | 0 | 85,882 | 23 | 171,764 |
Yes | output | 1 | 85,882 | 23 | 171,765 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a_1, a_2, ... , a_n, which is sorted in non-decreasing order (a_i ≤ a_{i + 1}).
Find three indices i, j, k such that 1 ≤ i < j < k ≤ n and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to a_i, a_j and a_k (for example it is possible to construct a non-degenerate triangle with sides 3, 4 and 5 but impossible with sides 3, 4 and 7). If it is impossible to find such triple, report it.
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains one integer n (3 ≤ n ≤ 5 ⋅ 10^4) — the length of the array a.
The second line of each test case contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9; a_{i - 1} ≤ a_i) — the array a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print the answer to it in one line.
If there is a triple of indices i, j, k (i < j < k) such that it is impossible to construct a non-degenerate triangle having sides equal to a_i, a_j and a_k, print that three indices in ascending order. If there are multiple answers, print any of them.
Otherwise, print -1.
Example
Input
3
7
4 6 11 11 15 18 20
4
10 10 10 11
3
1 1 1000000000
Output
2 3 6
-1
1 2 3
Note
In the first test case it is impossible with sides 6, 11 and 18. Note, that this is not the only correct answer.
In the second test case you always can construct a non-degenerate triangle.
Submitted Solution:
```
t=int(input())
for _ in range(t):
n=int(input())
arr=[int(x) for x in input().split()]
li=[]
flag = True
for i in range(n):
if len(li) == 0:
li.append(i+1)
elif len(li) > 2:
break
else:
#print(arr[i] - arr[li[-1]],li,)
if arr[i] - arr[li[-1]] < 2:
#print('yes')
if flag:
li.append(i+1)
flag =False
else:
li.append(i+1)
if len(li) == 3:
print(*li)
else:
print("-1")
``` | instruction | 0 | 85,883 | 23 | 171,766 |
No | output | 1 | 85,883 | 23 | 171,767 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a_1, a_2, ... , a_n, which is sorted in non-decreasing order (a_i ≤ a_{i + 1}).
Find three indices i, j, k such that 1 ≤ i < j < k ≤ n and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to a_i, a_j and a_k (for example it is possible to construct a non-degenerate triangle with sides 3, 4 and 5 but impossible with sides 3, 4 and 7). If it is impossible to find such triple, report it.
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains one integer n (3 ≤ n ≤ 5 ⋅ 10^4) — the length of the array a.
The second line of each test case contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9; a_{i - 1} ≤ a_i) — the array a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print the answer to it in one line.
If there is a triple of indices i, j, k (i < j < k) such that it is impossible to construct a non-degenerate triangle having sides equal to a_i, a_j and a_k, print that three indices in ascending order. If there are multiple answers, print any of them.
Otherwise, print -1.
Example
Input
3
7
4 6 11 11 15 18 20
4
10 10 10 11
3
1 1 1000000000
Output
2 3 6
-1
1 2 3
Note
In the first test case it is impossible with sides 6, 11 and 18. Note, that this is not the only correct answer.
In the second test case you always can construct a non-degenerate triangle.
Submitted Solution:
```
for p in range(int(input())):
n=int(input())
x=[int(x) for x in input().split()]
i,j,k=0,1,n-1
a,b,c=x[i],x[j],x[k]
if (a+b)<c:
print(i+1,j+1,k+1)
else:
print(-1)
``` | instruction | 0 | 85,884 | 23 | 171,768 |
No | output | 1 | 85,884 | 23 | 171,769 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a_1, a_2, ... , a_n, which is sorted in non-decreasing order (a_i ≤ a_{i + 1}).
Find three indices i, j, k such that 1 ≤ i < j < k ≤ n and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to a_i, a_j and a_k (for example it is possible to construct a non-degenerate triangle with sides 3, 4 and 5 but impossible with sides 3, 4 and 7). If it is impossible to find such triple, report it.
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains one integer n (3 ≤ n ≤ 5 ⋅ 10^4) — the length of the array a.
The second line of each test case contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9; a_{i - 1} ≤ a_i) — the array a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print the answer to it in one line.
If there is a triple of indices i, j, k (i < j < k) such that it is impossible to construct a non-degenerate triangle having sides equal to a_i, a_j and a_k, print that three indices in ascending order. If there are multiple answers, print any of them.
Otherwise, print -1.
Example
Input
3
7
4 6 11 11 15 18 20
4
10 10 10 11
3
1 1 1000000000
Output
2 3 6
-1
1 2 3
Note
In the first test case it is impossible with sides 6, 11 and 18. Note, that this is not the only correct answer.
In the second test case you always can construct a non-degenerate triangle.
Submitted Solution:
```
def isPossibleTriangle(arr, N):
f=0
# loop for all 3
# consecutive triplets
for i in range(N - 2):
# If triplet satisfies triangle
# condition, break
if (arr[i] + arr[i + 1] <= arr[i + 2]) and (arr[i+1] + arr[i + 2] <= arr[i]) and (arr[i] + arr[i + 2] <= arr[i + 1]):
p = i+1
q=i+2
r=i+3
f=1
break
if f==1:
print(p, q, r)
else:
print(-1)
t = int(input())
for T in range(t):
n = int(input())
l = list(map(int, input().split()))
isPossibleTriangle(l, n)
``` | instruction | 0 | 85,885 | 23 | 171,770 |
No | output | 1 | 85,885 | 23 | 171,771 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given an array a_1, a_2, ... , a_n, which is sorted in non-decreasing order (a_i ≤ a_{i + 1}).
Find three indices i, j, k such that 1 ≤ i < j < k ≤ n and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to a_i, a_j and a_k (for example it is possible to construct a non-degenerate triangle with sides 3, 4 and 5 but impossible with sides 3, 4 and 7). If it is impossible to find such triple, report it.
Input
The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains one integer n (3 ≤ n ≤ 5 ⋅ 10^4) — the length of the array a.
The second line of each test case contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9; a_{i - 1} ≤ a_i) — the array a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print the answer to it in one line.
If there is a triple of indices i, j, k (i < j < k) such that it is impossible to construct a non-degenerate triangle having sides equal to a_i, a_j and a_k, print that three indices in ascending order. If there are multiple answers, print any of them.
Otherwise, print -1.
Example
Input
3
7
4 6 11 11 15 18 20
4
10 10 10 11
3
1 1 1000000000
Output
2 3 6
-1
1 2 3
Note
In the first test case it is impossible with sides 6, 11 and 18. Note, that this is not the only correct answer.
In the second test case you always can construct a non-degenerate triangle.
Submitted Solution:
```
t=int(input())
for _ in range(t):
n=int(input())
a=list(map(int,input().split()))
sum1=a[0]+a[1]
found=0
for i in range(2,n):
if sum1<=a[i]:
found=1
break
if found!=1:
print("-1")
else:
print(0,1,i)
``` | instruction | 0 | 85,886 | 23 | 171,772 |
No | output | 1 | 85,886 | 23 | 171,773 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You may know that Euclid was a mathematician. Well, as it turns out, Morpheus knew it too. So when he wanted to play a mean trick on Euclid, he sent him an appropriate nightmare.
In his bad dream Euclid has a set S of n m-dimensional vectors over the Z_2 field and can perform vector addition on them. In other words he has vectors with m coordinates, each one equal either 0 or 1. Vector addition is defined as follows: let u+v = w, then w_i = (u_i + v_i) mod 2.
Euclid can sum any subset of S and archive another m-dimensional vector over Z_2. In particular, he can sum together an empty subset; in such a case, the resulting vector has all coordinates equal 0.
Let T be the set of all the vectors that can be written as a sum of some vectors from S. Now Euclid wonders the size of T and whether he can use only a subset S' of S to obtain all the vectors from T. As it is usually the case in such scenarios, he will not wake up until he figures this out. So far, things are looking rather grim for the philosopher. But there is hope, as he noticed that all vectors in S have at most 2 coordinates equal 1.
Help Euclid and calculate |T|, the number of m-dimensional vectors over Z_2 that can be written as a sum of some vectors from S. As it can be quite large, calculate it modulo 10^9+7. You should also find S', the smallest such subset of S, that all vectors in T can be written as a sum of vectors from S'. In case there are multiple such sets with a minimal number of elements, output the lexicographically smallest one with respect to the order in which their elements are given in the input.
Consider sets A and B such that |A| = |B|. Let a_1, a_2, ... a_{|A|} and b_1, b_2, ... b_{|B|} be increasing arrays of indices elements of A and B correspondingly. A is lexicographically smaller than B iff there exists such i that a_j = b_j for all j < i and a_i < b_i.
Input
In the first line of input, there are two integers n, m (1 ≤ n, m ≤ 5 ⋅ 10^5) denoting the number of vectors in S and the number of dimensions.
Next n lines contain the description of the vectors in S. In each of them there is an integer k (1 ≤ k ≤ 2) and then follow k distinct integers x_1, ... x_k (1 ≤ x_i ≤ m). This encodes an m-dimensional vector having 1s on coordinates x_1, ... x_k and 0s on the rest of them.
Among the n vectors, no two are the same.
Output
In the first line, output two integers: remainder modulo 10^9+7 of |T| and |S'|. In the second line, output |S'| numbers, indices of the elements of S' in ascending order. The elements of S are numbered from 1 in the order they are given in the input.
Examples
Input
3 2
1 1
1 2
2 2 1
Output
4 2
1 2
Input
2 3
2 1 3
2 1 2
Output
4 2
1 2
Input
3 5
2 1 2
1 3
1 4
Output
8 3
1 2 3
Note
In the first example we are given three vectors:
* 10
* 01
* 11
It turns out that we can represent all vectors from our 2-dimensional space using these vectors:
* 00 is a sum of the empty subset of above vectors;
* 01 = 11 + 10, is a sum of the first and third vector;
* 10 = 10, is just the first vector;
* 11 = 10 + 01, is a sum of the first and the second vector.
Hence, T = \{00, 01, 10, 11\}. We can choose any two of the three vectors from S and still be able to obtain all the vectors in T. In such a case, we choose the two vectors which appear first in the input. Since we cannot obtain all vectors in T using only a single vector from S, |S'| = 2 and S' = \{10, 01\} (indices 1 and 2), as set \{1, 2 \} is lexicographically the smallest. We can represent all vectors from T, using only vectors from S', as shown below:
* 00 is a sum of the empty subset;
* 01 = 01 is just the second vector;
* 10 = 10 is just the first vector;
* 11 = 10 + 01 is a sum of the first and the second vector. | instruction | 0 | 85,919 | 23 | 171,838 |
Tags: bitmasks, dfs and similar, dsu, graphs, greedy, math, sortings
Correct Solution:
```
from collections import defaultdict
import sys
input = sys.stdin.readline
class UF:
def __init__(self, N):
self.par = list(range(N))
self.sz = [1] * N
def find(self, x):
if self.par[x] != x:
self.par[x] = self.find(self.par[x])
return self.par[x]
def union(self, x, y):
xr, yr = self.find(x), self.find(y)
if xr == yr:
return False
if self.sz[xr] < self.sz[yr]:
xr, yr = yr, xr
self.par[yr] = xr
self.sz[xr] += self.sz[yr]
return True
def size(self, x):
return self.sz[self.find(x)]
def connected(self, x, y):
return self.find(x) == self.find(y)
n,m = list(map(int, input().split(' ')))
cur_count = 1
cur_lst = []
uf = UF(m+2)
for i in range(n):
bit_lst = list(map(int, input().split(' ')))
bit1 = bit_lst[1]
bit2 = m+1
if bit_lst[0] == 2:
bit2 = bit_lst[2]
if uf.union(bit1, bit2):
cur_lst.append(str(i+1))
cur_count *= 2
cur_count %= 10**9 + 7
print(cur_count, len(cur_lst))
print(" ".join(cur_lst))
``` | output | 1 | 85,919 | 23 | 171,839 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You may know that Euclid was a mathematician. Well, as it turns out, Morpheus knew it too. So when he wanted to play a mean trick on Euclid, he sent him an appropriate nightmare.
In his bad dream Euclid has a set S of n m-dimensional vectors over the Z_2 field and can perform vector addition on them. In other words he has vectors with m coordinates, each one equal either 0 or 1. Vector addition is defined as follows: let u+v = w, then w_i = (u_i + v_i) mod 2.
Euclid can sum any subset of S and archive another m-dimensional vector over Z_2. In particular, he can sum together an empty subset; in such a case, the resulting vector has all coordinates equal 0.
Let T be the set of all the vectors that can be written as a sum of some vectors from S. Now Euclid wonders the size of T and whether he can use only a subset S' of S to obtain all the vectors from T. As it is usually the case in such scenarios, he will not wake up until he figures this out. So far, things are looking rather grim for the philosopher. But there is hope, as he noticed that all vectors in S have at most 2 coordinates equal 1.
Help Euclid and calculate |T|, the number of m-dimensional vectors over Z_2 that can be written as a sum of some vectors from S. As it can be quite large, calculate it modulo 10^9+7. You should also find S', the smallest such subset of S, that all vectors in T can be written as a sum of vectors from S'. In case there are multiple such sets with a minimal number of elements, output the lexicographically smallest one with respect to the order in which their elements are given in the input.
Consider sets A and B such that |A| = |B|. Let a_1, a_2, ... a_{|A|} and b_1, b_2, ... b_{|B|} be increasing arrays of indices elements of A and B correspondingly. A is lexicographically smaller than B iff there exists such i that a_j = b_j for all j < i and a_i < b_i.
Input
In the first line of input, there are two integers n, m (1 ≤ n, m ≤ 5 ⋅ 10^5) denoting the number of vectors in S and the number of dimensions.
Next n lines contain the description of the vectors in S. In each of them there is an integer k (1 ≤ k ≤ 2) and then follow k distinct integers x_1, ... x_k (1 ≤ x_i ≤ m). This encodes an m-dimensional vector having 1s on coordinates x_1, ... x_k and 0s on the rest of them.
Among the n vectors, no two are the same.
Output
In the first line, output two integers: remainder modulo 10^9+7 of |T| and |S'|. In the second line, output |S'| numbers, indices of the elements of S' in ascending order. The elements of S are numbered from 1 in the order they are given in the input.
Examples
Input
3 2
1 1
1 2
2 2 1
Output
4 2
1 2
Input
2 3
2 1 3
2 1 2
Output
4 2
1 2
Input
3 5
2 1 2
1 3
1 4
Output
8 3
1 2 3
Note
In the first example we are given three vectors:
* 10
* 01
* 11
It turns out that we can represent all vectors from our 2-dimensional space using these vectors:
* 00 is a sum of the empty subset of above vectors;
* 01 = 11 + 10, is a sum of the first and third vector;
* 10 = 10, is just the first vector;
* 11 = 10 + 01, is a sum of the first and the second vector.
Hence, T = \{00, 01, 10, 11\}. We can choose any two of the three vectors from S and still be able to obtain all the vectors in T. In such a case, we choose the two vectors which appear first in the input. Since we cannot obtain all vectors in T using only a single vector from S, |S'| = 2 and S' = \{10, 01\} (indices 1 and 2), as set \{1, 2 \} is lexicographically the smallest. We can represent all vectors from T, using only vectors from S', as shown below:
* 00 is a sum of the empty subset;
* 01 = 01 is just the second vector;
* 10 = 10 is just the first vector;
* 11 = 10 + 01 is a sum of the first and the second vector. | instruction | 0 | 85,920 | 23 | 171,840 |
Tags: bitmasks, dfs and similar, dsu, graphs, greedy, math, sortings
Correct Solution:
```
import sys
input = iter(sys.stdin.read().splitlines()).__next__
class UnionFind: # based on submission 102831279
def __init__(self, n):
""" elements are 0, 1, 2, ..., n-1 """
self.parent = list(range(n))
def find(self, x):
found = x
while self.parent[found] != found:
found = self.parent[found]
while x != found:
y = self.parent[x]
self.parent[x] = found
x = y
return found
def union(self, x, y):
self.parent[self.find(x)] = self.find(y)
n, m = map(int, input().split())
S_prime = []
# redundant (linearly dependent) vectors would be part of cycle
uf = UnionFind(m+1)
for index in range(1, n+1):
# one-hot vectors become (x_1, m)
vector_description = [int(i)-1 for i in input().split()] + [m]
u, v = vector_description[1:3]
if uf.find(u) == uf.find(v):
continue
S_prime.append(index)
uf.union(u, v)
# 2**|S'| different sums among |S'| linearly independent vectors
T_size = pow(2, len(S_prime), 10**9+7)
print(T_size, len(S_prime))
# print(S_prime)
print(*sorted(S_prime))
``` | output | 1 | 85,920 | 23 | 171,841 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You may know that Euclid was a mathematician. Well, as it turns out, Morpheus knew it too. So when he wanted to play a mean trick on Euclid, he sent him an appropriate nightmare.
In his bad dream Euclid has a set S of n m-dimensional vectors over the Z_2 field and can perform vector addition on them. In other words he has vectors with m coordinates, each one equal either 0 or 1. Vector addition is defined as follows: let u+v = w, then w_i = (u_i + v_i) mod 2.
Euclid can sum any subset of S and archive another m-dimensional vector over Z_2. In particular, he can sum together an empty subset; in such a case, the resulting vector has all coordinates equal 0.
Let T be the set of all the vectors that can be written as a sum of some vectors from S. Now Euclid wonders the size of T and whether he can use only a subset S' of S to obtain all the vectors from T. As it is usually the case in such scenarios, he will not wake up until he figures this out. So far, things are looking rather grim for the philosopher. But there is hope, as he noticed that all vectors in S have at most 2 coordinates equal 1.
Help Euclid and calculate |T|, the number of m-dimensional vectors over Z_2 that can be written as a sum of some vectors from S. As it can be quite large, calculate it modulo 10^9+7. You should also find S', the smallest such subset of S, that all vectors in T can be written as a sum of vectors from S'. In case there are multiple such sets with a minimal number of elements, output the lexicographically smallest one with respect to the order in which their elements are given in the input.
Consider sets A and B such that |A| = |B|. Let a_1, a_2, ... a_{|A|} and b_1, b_2, ... b_{|B|} be increasing arrays of indices elements of A and B correspondingly. A is lexicographically smaller than B iff there exists such i that a_j = b_j for all j < i and a_i < b_i.
Input
In the first line of input, there are two integers n, m (1 ≤ n, m ≤ 5 ⋅ 10^5) denoting the number of vectors in S and the number of dimensions.
Next n lines contain the description of the vectors in S. In each of them there is an integer k (1 ≤ k ≤ 2) and then follow k distinct integers x_1, ... x_k (1 ≤ x_i ≤ m). This encodes an m-dimensional vector having 1s on coordinates x_1, ... x_k and 0s on the rest of them.
Among the n vectors, no two are the same.
Output
In the first line, output two integers: remainder modulo 10^9+7 of |T| and |S'|. In the second line, output |S'| numbers, indices of the elements of S' in ascending order. The elements of S are numbered from 1 in the order they are given in the input.
Examples
Input
3 2
1 1
1 2
2 2 1
Output
4 2
1 2
Input
2 3
2 1 3
2 1 2
Output
4 2
1 2
Input
3 5
2 1 2
1 3
1 4
Output
8 3
1 2 3
Note
In the first example we are given three vectors:
* 10
* 01
* 11
It turns out that we can represent all vectors from our 2-dimensional space using these vectors:
* 00 is a sum of the empty subset of above vectors;
* 01 = 11 + 10, is a sum of the first and third vector;
* 10 = 10, is just the first vector;
* 11 = 10 + 01, is a sum of the first and the second vector.
Hence, T = \{00, 01, 10, 11\}. We can choose any two of the three vectors from S and still be able to obtain all the vectors in T. In such a case, we choose the two vectors which appear first in the input. Since we cannot obtain all vectors in T using only a single vector from S, |S'| = 2 and S' = \{10, 01\} (indices 1 and 2), as set \{1, 2 \} is lexicographically the smallest. We can represent all vectors from T, using only vectors from S', as shown below:
* 00 is a sum of the empty subset;
* 01 = 01 is just the second vector;
* 10 = 10 is just the first vector;
* 11 = 10 + 01 is a sum of the first and the second vector. | instruction | 0 | 85,921 | 23 | 171,842 |
Tags: bitmasks, dfs and similar, dsu, graphs, greedy, math, sortings
Correct Solution:
```
import sys
input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
def find(self, a): #return parent of a. a and b are in same set if they have same parent
acopy = a
while a != self.parent[a]:
a = self.parent[a]
while acopy != a: #path compression
self.parent[acopy], acopy = a, self.parent[acopy]
return a
def union(self, a, b): #union a and b
self.parent[self.find(b)] = self.find(a)
def oneLineArrayPrint(arr):
print(' '.join([str(x) for x in arr]))
#######
n,m=[int(x) for x in input().split()]
uf=UnionFind(m+1)
hasOne=[False for _ in range(m+1)] #hasOne[parent]
#a tree cannot have cycles
#a tree can have at most vector with 1 "1"
sPrime=[]
for i in range(n):
inp=[int(x) for x in input().split()]
if inp[0]==1:#vector has 1 "1"
parent=uf.find(inp[1])
if hasOne[parent]==False:#take this vector
sPrime.append(i+1)
hasOne[parent]=True
else:
parent1,parent2=uf.find(inp[1]),uf.find(inp[2])
if parent1!=parent2: #no cycle. will join 2 trees
if not (hasOne[parent1] and hasOne[parent2]):#take this vector
sPrime.append(i+1)
uf.union(inp[1],inp[2])
newParent=uf.find(inp[1])
hasOne[newParent]=hasOne[parent1] or hasOne[parent2]
S_magnitude=len(sPrime)
T_magnitude=pow(2,S_magnitude,10**9+7)
print('{} {}'.format(T_magnitude,S_magnitude))
oneLineArrayPrint(sPrime)
``` | output | 1 | 85,921 | 23 | 171,843 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You may know that Euclid was a mathematician. Well, as it turns out, Morpheus knew it too. So when he wanted to play a mean trick on Euclid, he sent him an appropriate nightmare.
In his bad dream Euclid has a set S of n m-dimensional vectors over the Z_2 field and can perform vector addition on them. In other words he has vectors with m coordinates, each one equal either 0 or 1. Vector addition is defined as follows: let u+v = w, then w_i = (u_i + v_i) mod 2.
Euclid can sum any subset of S and archive another m-dimensional vector over Z_2. In particular, he can sum together an empty subset; in such a case, the resulting vector has all coordinates equal 0.
Let T be the set of all the vectors that can be written as a sum of some vectors from S. Now Euclid wonders the size of T and whether he can use only a subset S' of S to obtain all the vectors from T. As it is usually the case in such scenarios, he will not wake up until he figures this out. So far, things are looking rather grim for the philosopher. But there is hope, as he noticed that all vectors in S have at most 2 coordinates equal 1.
Help Euclid and calculate |T|, the number of m-dimensional vectors over Z_2 that can be written as a sum of some vectors from S. As it can be quite large, calculate it modulo 10^9+7. You should also find S', the smallest such subset of S, that all vectors in T can be written as a sum of vectors from S'. In case there are multiple such sets with a minimal number of elements, output the lexicographically smallest one with respect to the order in which their elements are given in the input.
Consider sets A and B such that |A| = |B|. Let a_1, a_2, ... a_{|A|} and b_1, b_2, ... b_{|B|} be increasing arrays of indices elements of A and B correspondingly. A is lexicographically smaller than B iff there exists such i that a_j = b_j for all j < i and a_i < b_i.
Input
In the first line of input, there are two integers n, m (1 ≤ n, m ≤ 5 ⋅ 10^5) denoting the number of vectors in S and the number of dimensions.
Next n lines contain the description of the vectors in S. In each of them there is an integer k (1 ≤ k ≤ 2) and then follow k distinct integers x_1, ... x_k (1 ≤ x_i ≤ m). This encodes an m-dimensional vector having 1s on coordinates x_1, ... x_k and 0s on the rest of them.
Among the n vectors, no two are the same.
Output
In the first line, output two integers: remainder modulo 10^9+7 of |T| and |S'|. In the second line, output |S'| numbers, indices of the elements of S' in ascending order. The elements of S are numbered from 1 in the order they are given in the input.
Examples
Input
3 2
1 1
1 2
2 2 1
Output
4 2
1 2
Input
2 3
2 1 3
2 1 2
Output
4 2
1 2
Input
3 5
2 1 2
1 3
1 4
Output
8 3
1 2 3
Note
In the first example we are given three vectors:
* 10
* 01
* 11
It turns out that we can represent all vectors from our 2-dimensional space using these vectors:
* 00 is a sum of the empty subset of above vectors;
* 01 = 11 + 10, is a sum of the first and third vector;
* 10 = 10, is just the first vector;
* 11 = 10 + 01, is a sum of the first and the second vector.
Hence, T = \{00, 01, 10, 11\}. We can choose any two of the three vectors from S and still be able to obtain all the vectors in T. In such a case, we choose the two vectors which appear first in the input. Since we cannot obtain all vectors in T using only a single vector from S, |S'| = 2 and S' = \{10, 01\} (indices 1 and 2), as set \{1, 2 \} is lexicographically the smallest. We can represent all vectors from T, using only vectors from S', as shown below:
* 00 is a sum of the empty subset;
* 01 = 01 is just the second vector;
* 10 = 10 is just the first vector;
* 11 = 10 + 01 is a sum of the first and the second vector. | instruction | 0 | 85,922 | 23 | 171,844 |
Tags: bitmasks, dfs and similar, dsu, graphs, greedy, math, sortings
Correct Solution:
```
import sys
readline = sys.stdin.readline
class UF():
def __init__(self, num):
self.par = [-1]*num
self.color = [0]*num
def find(self, x):
if self.par[x] < 0:
return x
else:
stack = []
while self.par[x] >= 0:
stack.append(x)
x = self.par[x]
for xi in stack:
self.par[xi] = x
return x
def col(self, x):
return self.color[self.find(x)]
def paint(self, x):
self.color[self.find(x)] = 1
def union(self, x, y):
rx = self.find(x)
ry = self.find(y)
if rx != ry:
if self.par[rx] > self.par[ry]:
rx, ry = ry, rx
self.par[rx] += self.par[ry]
self.par[ry] = rx
self.color[rx] |= self.color[ry]
return True
return False
N, M = map(int, readline().split())
MOD = 10**9+7
ans = []
T = UF(M)
for m in range(N):
k, *x = map(int, readline().split())
if k == 1:
u = x[0]-1
if not T.col(u):
ans.append(m+1)
T.paint(u)
else:
u, v = x[0]-1, x[1]-1
if T.col(u) and T.col(v):
continue
if T.union(u, v):
ans.append(m+1)
print(pow(2, len(ans), MOD), len(ans))
print(' '.join(map(str, ans)))
``` | output | 1 | 85,922 | 23 | 171,845 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You may know that Euclid was a mathematician. Well, as it turns out, Morpheus knew it too. So when he wanted to play a mean trick on Euclid, he sent him an appropriate nightmare.
In his bad dream Euclid has a set S of n m-dimensional vectors over the Z_2 field and can perform vector addition on them. In other words he has vectors with m coordinates, each one equal either 0 or 1. Vector addition is defined as follows: let u+v = w, then w_i = (u_i + v_i) mod 2.
Euclid can sum any subset of S and archive another m-dimensional vector over Z_2. In particular, he can sum together an empty subset; in such a case, the resulting vector has all coordinates equal 0.
Let T be the set of all the vectors that can be written as a sum of some vectors from S. Now Euclid wonders the size of T and whether he can use only a subset S' of S to obtain all the vectors from T. As it is usually the case in such scenarios, he will not wake up until he figures this out. So far, things are looking rather grim for the philosopher. But there is hope, as he noticed that all vectors in S have at most 2 coordinates equal 1.
Help Euclid and calculate |T|, the number of m-dimensional vectors over Z_2 that can be written as a sum of some vectors from S. As it can be quite large, calculate it modulo 10^9+7. You should also find S', the smallest such subset of S, that all vectors in T can be written as a sum of vectors from S'. In case there are multiple such sets with a minimal number of elements, output the lexicographically smallest one with respect to the order in which their elements are given in the input.
Consider sets A and B such that |A| = |B|. Let a_1, a_2, ... a_{|A|} and b_1, b_2, ... b_{|B|} be increasing arrays of indices elements of A and B correspondingly. A is lexicographically smaller than B iff there exists such i that a_j = b_j for all j < i and a_i < b_i.
Input
In the first line of input, there are two integers n, m (1 ≤ n, m ≤ 5 ⋅ 10^5) denoting the number of vectors in S and the number of dimensions.
Next n lines contain the description of the vectors in S. In each of them there is an integer k (1 ≤ k ≤ 2) and then follow k distinct integers x_1, ... x_k (1 ≤ x_i ≤ m). This encodes an m-dimensional vector having 1s on coordinates x_1, ... x_k and 0s on the rest of them.
Among the n vectors, no two are the same.
Output
In the first line, output two integers: remainder modulo 10^9+7 of |T| and |S'|. In the second line, output |S'| numbers, indices of the elements of S' in ascending order. The elements of S are numbered from 1 in the order they are given in the input.
Examples
Input
3 2
1 1
1 2
2 2 1
Output
4 2
1 2
Input
2 3
2 1 3
2 1 2
Output
4 2
1 2
Input
3 5
2 1 2
1 3
1 4
Output
8 3
1 2 3
Note
In the first example we are given three vectors:
* 10
* 01
* 11
It turns out that we can represent all vectors from our 2-dimensional space using these vectors:
* 00 is a sum of the empty subset of above vectors;
* 01 = 11 + 10, is a sum of the first and third vector;
* 10 = 10, is just the first vector;
* 11 = 10 + 01, is a sum of the first and the second vector.
Hence, T = \{00, 01, 10, 11\}. We can choose any two of the three vectors from S and still be able to obtain all the vectors in T. In such a case, we choose the two vectors which appear first in the input. Since we cannot obtain all vectors in T using only a single vector from S, |S'| = 2 and S' = \{10, 01\} (indices 1 and 2), as set \{1, 2 \} is lexicographically the smallest. We can represent all vectors from T, using only vectors from S', as shown below:
* 00 is a sum of the empty subset;
* 01 = 01 is just the second vector;
* 10 = 10 is just the first vector;
* 11 = 10 + 01 is a sum of the first and the second vector. | instruction | 0 | 85,923 | 23 | 171,846 |
Tags: bitmasks, dfs and similar, dsu, graphs, greedy, math, sortings
Correct Solution:
```
import sys
input = sys.stdin.readline
mod=1000000007
n,m=map(int,input().split())
ans=[]
groupi=[-1]*(m+1)
groups=[2]*m
for i in range(m):
groups[i]=[]
cur=1
for i in range(n):
x=list(map(int,input().split()))
k=x.pop(0)
if k==1:
x=x[0]
if groupi[x]==-1:
groupi[x]=0
ans.append(i+1)
if groupi[x]>0:
ind=groupi[x]
for y in groups[ind]:
groupi[y]=0
groupi[x]=0
ans.append(i+1)
if k==2:
x1,x2=x[0],x[1]
if groupi[x1]==-1:
if groupi[x2]==-1:
groupi[x1]=cur
groupi[x2]=cur
groups[cur]=[x1,x2]
cur+=1
ans.append(i+1)
else:
if groupi[x2]==0:
groupi[x1]=0
ans.append(i+1)
else:
groupi[x1]=groupi[x2]
groups[groupi[x2]].append(x1)
ans.append(i+1)
else:
if groupi[x2]==-1:
if groupi[x1]==0:
groupi[x2]=0
ans.append(i+1)
else:
groupi[x2]=groupi[x1]
groups[groupi[x1]].append(x2)
ans.append(i+1)
else:
if groupi[x1]!=groupi[x2]:
if groupi[x1]==0 or groupi[x2]==0:
if groupi[x1]==0:
for y in groups[groupi[x2]]:
groupi[y]=0
else:
for y in groups[groupi[x1]]:
groupi[y]=0
else:
if len(groups[groupi[x1]])<len(groups[groupi[x2]]):
x1,x2=x2,x1
for y in groups[groupi[x2]]:
groupi[y]=groupi[x1]
groups[groupi[x1]].append(y)
ans.append(i+1)
print(pow(2,len(ans),mod),len(ans))
print(*ans)
``` | output | 1 | 85,923 | 23 | 171,847 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You may know that Euclid was a mathematician. Well, as it turns out, Morpheus knew it too. So when he wanted to play a mean trick on Euclid, he sent him an appropriate nightmare.
In his bad dream Euclid has a set S of n m-dimensional vectors over the Z_2 field and can perform vector addition on them. In other words he has vectors with m coordinates, each one equal either 0 or 1. Vector addition is defined as follows: let u+v = w, then w_i = (u_i + v_i) mod 2.
Euclid can sum any subset of S and archive another m-dimensional vector over Z_2. In particular, he can sum together an empty subset; in such a case, the resulting vector has all coordinates equal 0.
Let T be the set of all the vectors that can be written as a sum of some vectors from S. Now Euclid wonders the size of T and whether he can use only a subset S' of S to obtain all the vectors from T. As it is usually the case in such scenarios, he will not wake up until he figures this out. So far, things are looking rather grim for the philosopher. But there is hope, as he noticed that all vectors in S have at most 2 coordinates equal 1.
Help Euclid and calculate |T|, the number of m-dimensional vectors over Z_2 that can be written as a sum of some vectors from S. As it can be quite large, calculate it modulo 10^9+7. You should also find S', the smallest such subset of S, that all vectors in T can be written as a sum of vectors from S'. In case there are multiple such sets with a minimal number of elements, output the lexicographically smallest one with respect to the order in which their elements are given in the input.
Consider sets A and B such that |A| = |B|. Let a_1, a_2, ... a_{|A|} and b_1, b_2, ... b_{|B|} be increasing arrays of indices elements of A and B correspondingly. A is lexicographically smaller than B iff there exists such i that a_j = b_j for all j < i and a_i < b_i.
Input
In the first line of input, there are two integers n, m (1 ≤ n, m ≤ 5 ⋅ 10^5) denoting the number of vectors in S and the number of dimensions.
Next n lines contain the description of the vectors in S. In each of them there is an integer k (1 ≤ k ≤ 2) and then follow k distinct integers x_1, ... x_k (1 ≤ x_i ≤ m). This encodes an m-dimensional vector having 1s on coordinates x_1, ... x_k and 0s on the rest of them.
Among the n vectors, no two are the same.
Output
In the first line, output two integers: remainder modulo 10^9+7 of |T| and |S'|. In the second line, output |S'| numbers, indices of the elements of S' in ascending order. The elements of S are numbered from 1 in the order they are given in the input.
Examples
Input
3 2
1 1
1 2
2 2 1
Output
4 2
1 2
Input
2 3
2 1 3
2 1 2
Output
4 2
1 2
Input
3 5
2 1 2
1 3
1 4
Output
8 3
1 2 3
Note
In the first example we are given three vectors:
* 10
* 01
* 11
It turns out that we can represent all vectors from our 2-dimensional space using these vectors:
* 00 is a sum of the empty subset of above vectors;
* 01 = 11 + 10, is a sum of the first and third vector;
* 10 = 10, is just the first vector;
* 11 = 10 + 01, is a sum of the first and the second vector.
Hence, T = \{00, 01, 10, 11\}. We can choose any two of the three vectors from S and still be able to obtain all the vectors in T. In such a case, we choose the two vectors which appear first in the input. Since we cannot obtain all vectors in T using only a single vector from S, |S'| = 2 and S' = \{10, 01\} (indices 1 and 2), as set \{1, 2 \} is lexicographically the smallest. We can represent all vectors from T, using only vectors from S', as shown below:
* 00 is a sum of the empty subset;
* 01 = 01 is just the second vector;
* 10 = 10 is just the first vector;
* 11 = 10 + 01 is a sum of the first and the second vector. | instruction | 0 | 85,924 | 23 | 171,848 |
Tags: bitmasks, dfs and similar, dsu, graphs, greedy, math, sortings
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
def __init__(self, file):
self.newlines = 0
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline()
# --------------------------------------------------------------------
def RL(): return map(int, sys.stdin.readline().split())
def RLL(): return list(map(int, sys.stdin.readline().split()))
def N(): return int(input())
def S(): return input().strip()
def print_list(l): print(' '.join(map(str, l)))
# sys.setrecursionlimit(100000)
# import random
# from functools import reduce
# from functools import lru_cache
# from heapq import *
# from collections import deque as dq
# from math import gcd
# import bisect as bs
# from collections import Counter
# from collections import defaultdict as dc
def find(region, u):
path = []
while region[u] != u:
path.append(u)
u = region[u]
for v in path:
region[v] = u
return u
def union(region, u, v):
u, v = find(region, u), find(region, v)
region[u] = v
return u != v
M = 10 ** 9 + 7
n, m = RL()
region = list(range(m + 1))
ans = []
for i in range(1, n + 1):
s = RLL()
t = s[2] if s[0] == 2 else 0
if union(region, s[1], t):
ans.append(i)
res = 1
for _ in range(len(ans)):
res = (res << 1) % M
print(res, len(ans))
print_list(ans)
``` | output | 1 | 85,924 | 23 | 171,849 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You may know that Euclid was a mathematician. Well, as it turns out, Morpheus knew it too. So when he wanted to play a mean trick on Euclid, he sent him an appropriate nightmare.
In his bad dream Euclid has a set S of n m-dimensional vectors over the Z_2 field and can perform vector addition on them. In other words he has vectors with m coordinates, each one equal either 0 or 1. Vector addition is defined as follows: let u+v = w, then w_i = (u_i + v_i) mod 2.
Euclid can sum any subset of S and archive another m-dimensional vector over Z_2. In particular, he can sum together an empty subset; in such a case, the resulting vector has all coordinates equal 0.
Let T be the set of all the vectors that can be written as a sum of some vectors from S. Now Euclid wonders the size of T and whether he can use only a subset S' of S to obtain all the vectors from T. As it is usually the case in such scenarios, he will not wake up until he figures this out. So far, things are looking rather grim for the philosopher. But there is hope, as he noticed that all vectors in S have at most 2 coordinates equal 1.
Help Euclid and calculate |T|, the number of m-dimensional vectors over Z_2 that can be written as a sum of some vectors from S. As it can be quite large, calculate it modulo 10^9+7. You should also find S', the smallest such subset of S, that all vectors in T can be written as a sum of vectors from S'. In case there are multiple such sets with a minimal number of elements, output the lexicographically smallest one with respect to the order in which their elements are given in the input.
Consider sets A and B such that |A| = |B|. Let a_1, a_2, ... a_{|A|} and b_1, b_2, ... b_{|B|} be increasing arrays of indices elements of A and B correspondingly. A is lexicographically smaller than B iff there exists such i that a_j = b_j for all j < i and a_i < b_i.
Input
In the first line of input, there are two integers n, m (1 ≤ n, m ≤ 5 ⋅ 10^5) denoting the number of vectors in S and the number of dimensions.
Next n lines contain the description of the vectors in S. In each of them there is an integer k (1 ≤ k ≤ 2) and then follow k distinct integers x_1, ... x_k (1 ≤ x_i ≤ m). This encodes an m-dimensional vector having 1s on coordinates x_1, ... x_k and 0s on the rest of them.
Among the n vectors, no two are the same.
Output
In the first line, output two integers: remainder modulo 10^9+7 of |T| and |S'|. In the second line, output |S'| numbers, indices of the elements of S' in ascending order. The elements of S are numbered from 1 in the order they are given in the input.
Examples
Input
3 2
1 1
1 2
2 2 1
Output
4 2
1 2
Input
2 3
2 1 3
2 1 2
Output
4 2
1 2
Input
3 5
2 1 2
1 3
1 4
Output
8 3
1 2 3
Note
In the first example we are given three vectors:
* 10
* 01
* 11
It turns out that we can represent all vectors from our 2-dimensional space using these vectors:
* 00 is a sum of the empty subset of above vectors;
* 01 = 11 + 10, is a sum of the first and third vector;
* 10 = 10, is just the first vector;
* 11 = 10 + 01, is a sum of the first and the second vector.
Hence, T = \{00, 01, 10, 11\}. We can choose any two of the three vectors from S and still be able to obtain all the vectors in T. In such a case, we choose the two vectors which appear first in the input. Since we cannot obtain all vectors in T using only a single vector from S, |S'| = 2 and S' = \{10, 01\} (indices 1 and 2), as set \{1, 2 \} is lexicographically the smallest. We can represent all vectors from T, using only vectors from S', as shown below:
* 00 is a sum of the empty subset;
* 01 = 01 is just the second vector;
* 10 = 10 is just the first vector;
* 11 = 10 + 01 is a sum of the first and the second vector. | instruction | 0 | 85,925 | 23 | 171,850 |
Tags: bitmasks, dfs and similar, dsu, graphs, greedy, math, sortings
Correct Solution:
```
class UnionFindVerSize():
def __init__(self, N):
self._parent = [n for n in range(0, N)]
self._size = [1] * N
self.source = [False] * N
self.group = N
def find_root(self, x):
if self._parent[x] == x: return x
self._parent[x] = self.find_root(self._parent[x])
stack = [x]
while self._parent[stack[-1]]!=stack[-1]:
stack.append(self._parent[stack[-1]])
for v in stack:
self._parent[v] = stack[-1]
return self._parent[x]
def unite(self, x, y):
gx = self.find_root(x)
gy = self.find_root(y)
if gx == gy: return
self.group -= 1
if self._size[gx] < self._size[gy]:
self._parent[gx] = gy
self._size[gy] += self._size[gx]
self.source[gy] |= self.source[gx]
else:
self._parent[gy] = gx
self._size[gx] += self._size[gy]
self.source[gx] |= self.source[gy]
def add_size(self,x):
self.source[self.find_root(x)] = True
def get_size(self, x):
return self._size[self.find_root(x)]
def get_source(self,x):
return self.source[self.find_root(x)]
def is_same_group(self, x, y):
return self.find_root(x) == self.find_root(y)
import sys
input = sys.stdin.buffer.readline
m,n = map(int,input().split())
uf = UnionFindVerSize(n)
S = []
source = []
for i in range(m):
edge = tuple(map(int,input().split()))
if edge[0]==1:
v = edge[1]
if not uf.get_source(v-1):
uf.add_size(v-1)
S.append(i+1)
else:
u,v = edge[1],edge[2]
if not uf.is_same_group(u-1,v-1) and (not uf.get_source(u-1) or not uf.get_source(v-1)):
uf.unite(u-1,v-1)
S.append(i+1)
ans = 1
k = 0
mod = 10**9 + 7
for i in range(n):
if uf.find_root(i)==i:
if uf.get_source(i):
k += uf.get_size(i)
else:
k += uf.get_size(i) - 1
print(pow(2,k,mod),len(S))
S.sort()
print(*S)
``` | output | 1 | 85,925 | 23 | 171,851 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You may know that Euclid was a mathematician. Well, as it turns out, Morpheus knew it too. So when he wanted to play a mean trick on Euclid, he sent him an appropriate nightmare.
In his bad dream Euclid has a set S of n m-dimensional vectors over the Z_2 field and can perform vector addition on them. In other words he has vectors with m coordinates, each one equal either 0 or 1. Vector addition is defined as follows: let u+v = w, then w_i = (u_i + v_i) mod 2.
Euclid can sum any subset of S and archive another m-dimensional vector over Z_2. In particular, he can sum together an empty subset; in such a case, the resulting vector has all coordinates equal 0.
Let T be the set of all the vectors that can be written as a sum of some vectors from S. Now Euclid wonders the size of T and whether he can use only a subset S' of S to obtain all the vectors from T. As it is usually the case in such scenarios, he will not wake up until he figures this out. So far, things are looking rather grim for the philosopher. But there is hope, as he noticed that all vectors in S have at most 2 coordinates equal 1.
Help Euclid and calculate |T|, the number of m-dimensional vectors over Z_2 that can be written as a sum of some vectors from S. As it can be quite large, calculate it modulo 10^9+7. You should also find S', the smallest such subset of S, that all vectors in T can be written as a sum of vectors from S'. In case there are multiple such sets with a minimal number of elements, output the lexicographically smallest one with respect to the order in which their elements are given in the input.
Consider sets A and B such that |A| = |B|. Let a_1, a_2, ... a_{|A|} and b_1, b_2, ... b_{|B|} be increasing arrays of indices elements of A and B correspondingly. A is lexicographically smaller than B iff there exists such i that a_j = b_j for all j < i and a_i < b_i.
Input
In the first line of input, there are two integers n, m (1 ≤ n, m ≤ 5 ⋅ 10^5) denoting the number of vectors in S and the number of dimensions.
Next n lines contain the description of the vectors in S. In each of them there is an integer k (1 ≤ k ≤ 2) and then follow k distinct integers x_1, ... x_k (1 ≤ x_i ≤ m). This encodes an m-dimensional vector having 1s on coordinates x_1, ... x_k and 0s on the rest of them.
Among the n vectors, no two are the same.
Output
In the first line, output two integers: remainder modulo 10^9+7 of |T| and |S'|. In the second line, output |S'| numbers, indices of the elements of S' in ascending order. The elements of S are numbered from 1 in the order they are given in the input.
Examples
Input
3 2
1 1
1 2
2 2 1
Output
4 2
1 2
Input
2 3
2 1 3
2 1 2
Output
4 2
1 2
Input
3 5
2 1 2
1 3
1 4
Output
8 3
1 2 3
Note
In the first example we are given three vectors:
* 10
* 01
* 11
It turns out that we can represent all vectors from our 2-dimensional space using these vectors:
* 00 is a sum of the empty subset of above vectors;
* 01 = 11 + 10, is a sum of the first and third vector;
* 10 = 10, is just the first vector;
* 11 = 10 + 01, is a sum of the first and the second vector.
Hence, T = \{00, 01, 10, 11\}. We can choose any two of the three vectors from S and still be able to obtain all the vectors in T. In such a case, we choose the two vectors which appear first in the input. Since we cannot obtain all vectors in T using only a single vector from S, |S'| = 2 and S' = \{10, 01\} (indices 1 and 2), as set \{1, 2 \} is lexicographically the smallest. We can represent all vectors from T, using only vectors from S', as shown below:
* 00 is a sum of the empty subset;
* 01 = 01 is just the second vector;
* 10 = 10 is just the first vector;
* 11 = 10 + 01 is a sum of the first and the second vector. | instruction | 0 | 85,926 | 23 | 171,852 |
Tags: bitmasks, dfs and similar, dsu, graphs, greedy, math, sortings
Correct Solution:
```
"""
Author - Satwik Tiwari .
19th Jan , 2021 - Tuesday
"""
#===============================================================================================
#importing some useful libraries.
from __future__ import division, print_function
# from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
# from itertools import *
from heapq import *
from math import gcd, factorial,floor,ceil,sqrt,log2
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
#==============================================================================================
#fast I/O region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
# inp = lambda: sys.stdin.readline().rstrip("\r\n")
#===============================================================================================
### START ITERATE RECURSION ###
from types import GeneratorType
def iterative(f, stack=[]):
def wrapped_func(*args, **kwargs):
if stack: return f(*args, **kwargs)
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
continue
stack.pop()
if not stack: break
to = stack[-1].send(to)
return to
return wrapped_func
#### END ITERATE RECURSION ####
#===============================================================================================
#some shortcuts
def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input
def out(var): sys.stdout.write(str(var)) #for fast output, always take string
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
# def graph(vertex): return [[] for i in range(0,vertex+1)]
def testcase(t):
for pp in range(t):
solve(pp)
def google(p):
print('Case #'+str(p)+': ',end='')
def lcm(a,b): return (a*b)//gcd(a,b)
def power(x, y, p) :
y%=(p-1) #not so sure about this. used when y>p-1. if p is prime.
res = 1 # Initialize result
x = x % p # Update x if it is more , than or equal to p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) : # If y is odd, multiply, x with result
res = (res * x) % p
y = y >> 1 # y = y/2
x = (x * x) % p
return res
def ncr(n,r): return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))
def isPrime(n) :
if (n <= 1) : return False
if (n <= 3) : return True
if (n % 2 == 0 or n % 3 == 0) : return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
inf = pow(10,21)
mod = 10**9+7
#===============================================================================================
# code here ;))
def bucketsort(order, seq):
buckets = [0] * (max(seq) + 1)
for x in seq:
buckets[x] += 1
for i in range(len(buckets) - 1):
buckets[i + 1] += buckets[i]
new_order = [-1] * len(seq)
for i in reversed(order):
x = seq[i]
idx = buckets[x] = buckets[x] - 1
new_order[idx] = i
return new_order
def ordersort(order, seq, reverse=False):
bit = max(seq).bit_length() >> 1
mask = (1 << bit) - 1
order = bucketsort(order, [x & mask for x in seq])
order = bucketsort(order, [x >> bit for x in seq])
if reverse:
order.reverse()
return order
def long_ordersort(order, seq):
order = ordersort(order, [int(i & 0x7fffffff) for i in seq])
return ordersort(order, [int(i >> 31) for i in seq])
def multikey_ordersort(order, *seqs, sort=ordersort):
for i in reversed(range(len(seqs))):
order = sort(order, seqs[i])
return order
class DisjointSetUnion:
def __init__(self, n):
self.parent = list(range(n))
self.size = [1] * n
self.num_sets = n
def find(self, a):
acopy = a
while a != self.parent[a]:
a = self.parent[a]
while acopy != a:
self.parent[acopy], acopy = a, self.parent[acopy]
return a
def union(self, a, b):
a, b = self.find(a), self.find(b)
if a != b:
if self.size[a] < self.size[b]:
a, b = b, a
self.num_sets -= 1
self.parent[b] = a
self.size[a] += self.size[b]
def set_size(self, a):
return self.size[self.find(a)]
def __len__(self):
return self.num_sets
def solve(case):
n,m = sep()
dsu = DisjointSetUnion(m+1)
# l = []
# r = []
# ind = []
# for i in range(n):
# a = lis()
# if(a[0] == 1):
# l.append(0)
# r.append(a[1])
# ind.append(i)
# else:
# # if(a[1] > a[2]): a[1],a[2] = a[2],a[1]
# l.append(a[1])
# r.append(a[2])
# ind.append(i)
#
# order = multikey_ordersort(range(len(l)),l,r,ind)
# sl = []
# for i in order:
# sl.append((l[i],r[i],ind[i]))
take = [1]*n
for index in range(n):
a = lis()
if(a[0] == 1):
i,j = 0,a[1]
else:
i,j = a[1],a[2]
# print(i,j)
grp1 = dsu.find(i)
grp2 = dsu.find(j)
if(grp1 == grp2):
take[index] = 0
else:
dsu.union(i,j)
ans = []
for i in range(n):
if(take[i]):
ans.append(i)
ans = sorted(ans)
print(power(2,len(ans),mod),len(ans))
print(' '.join(str(i+1) for i in ans))
#lexicographically based on the order they occur in input. no need to sort, we can directly look for cycles.
testcase(1)
# testcase(int(inp()))
``` | output | 1 | 85,926 | 23 | 171,853 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You may know that Euclid was a mathematician. Well, as it turns out, Morpheus knew it too. So when he wanted to play a mean trick on Euclid, he sent him an appropriate nightmare.
In his bad dream Euclid has a set S of n m-dimensional vectors over the Z_2 field and can perform vector addition on them. In other words he has vectors with m coordinates, each one equal either 0 or 1. Vector addition is defined as follows: let u+v = w, then w_i = (u_i + v_i) mod 2.
Euclid can sum any subset of S and archive another m-dimensional vector over Z_2. In particular, he can sum together an empty subset; in such a case, the resulting vector has all coordinates equal 0.
Let T be the set of all the vectors that can be written as a sum of some vectors from S. Now Euclid wonders the size of T and whether he can use only a subset S' of S to obtain all the vectors from T. As it is usually the case in such scenarios, he will not wake up until he figures this out. So far, things are looking rather grim for the philosopher. But there is hope, as he noticed that all vectors in S have at most 2 coordinates equal 1.
Help Euclid and calculate |T|, the number of m-dimensional vectors over Z_2 that can be written as a sum of some vectors from S. As it can be quite large, calculate it modulo 10^9+7. You should also find S', the smallest such subset of S, that all vectors in T can be written as a sum of vectors from S'. In case there are multiple such sets with a minimal number of elements, output the lexicographically smallest one with respect to the order in which their elements are given in the input.
Consider sets A and B such that |A| = |B|. Let a_1, a_2, ... a_{|A|} and b_1, b_2, ... b_{|B|} be increasing arrays of indices elements of A and B correspondingly. A is lexicographically smaller than B iff there exists such i that a_j = b_j for all j < i and a_i < b_i.
Input
In the first line of input, there are two integers n, m (1 ≤ n, m ≤ 5 ⋅ 10^5) denoting the number of vectors in S and the number of dimensions.
Next n lines contain the description of the vectors in S. In each of them there is an integer k (1 ≤ k ≤ 2) and then follow k distinct integers x_1, ... x_k (1 ≤ x_i ≤ m). This encodes an m-dimensional vector having 1s on coordinates x_1, ... x_k and 0s on the rest of them.
Among the n vectors, no two are the same.
Output
In the first line, output two integers: remainder modulo 10^9+7 of |T| and |S'|. In the second line, output |S'| numbers, indices of the elements of S' in ascending order. The elements of S are numbered from 1 in the order they are given in the input.
Examples
Input
3 2
1 1
1 2
2 2 1
Output
4 2
1 2
Input
2 3
2 1 3
2 1 2
Output
4 2
1 2
Input
3 5
2 1 2
1 3
1 4
Output
8 3
1 2 3
Note
In the first example we are given three vectors:
* 10
* 01
* 11
It turns out that we can represent all vectors from our 2-dimensional space using these vectors:
* 00 is a sum of the empty subset of above vectors;
* 01 = 11 + 10, is a sum of the first and third vector;
* 10 = 10, is just the first vector;
* 11 = 10 + 01, is a sum of the first and the second vector.
Hence, T = \{00, 01, 10, 11\}. We can choose any two of the three vectors from S and still be able to obtain all the vectors in T. In such a case, we choose the two vectors which appear first in the input. Since we cannot obtain all vectors in T using only a single vector from S, |S'| = 2 and S' = \{10, 01\} (indices 1 and 2), as set \{1, 2 \} is lexicographically the smallest. We can represent all vectors from T, using only vectors from S', as shown below:
* 00 is a sum of the empty subset;
* 01 = 01 is just the second vector;
* 10 = 10 is just the first vector;
* 11 = 10 + 01 is a sum of the first and the second vector.
Submitted Solution:
```
class UnionFind:
def __init__(self, n): self.parent = list(range(n))
def find(self, a):
acopy = a
while a != self.parent[a]: a = self.parent[a]
while acopy != a: self.parent[acopy], acopy = a, self.parent[acopy]
return a
def union(self, a, b): self.parent[self.find(b)] = self.find(a)
import sys;input = sys.stdin.readline;n, m = map(int, input().split());UF = UnionFind(m + 1);MOD = 10 ** 9 + 7;out = []
for i in range(1, n + 1):
l = list(map(int, input().split()))
if len(l) == 2:u = 0;v = l[1]
else:_, u, v = l
uu = UF.find(u);vv = UF.find(v)
if uu != vv:UF.union(uu,vv);out.append(i)
print(pow(2, len(out), MOD), len(out));print(' '.join(map(str,out)))
``` | instruction | 0 | 85,927 | 23 | 171,854 |
Yes | output | 1 | 85,927 | 23 | 171,855 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You may know that Euclid was a mathematician. Well, as it turns out, Morpheus knew it too. So when he wanted to play a mean trick on Euclid, he sent him an appropriate nightmare.
In his bad dream Euclid has a set S of n m-dimensional vectors over the Z_2 field and can perform vector addition on them. In other words he has vectors with m coordinates, each one equal either 0 or 1. Vector addition is defined as follows: let u+v = w, then w_i = (u_i + v_i) mod 2.
Euclid can sum any subset of S and archive another m-dimensional vector over Z_2. In particular, he can sum together an empty subset; in such a case, the resulting vector has all coordinates equal 0.
Let T be the set of all the vectors that can be written as a sum of some vectors from S. Now Euclid wonders the size of T and whether he can use only a subset S' of S to obtain all the vectors from T. As it is usually the case in such scenarios, he will not wake up until he figures this out. So far, things are looking rather grim for the philosopher. But there is hope, as he noticed that all vectors in S have at most 2 coordinates equal 1.
Help Euclid and calculate |T|, the number of m-dimensional vectors over Z_2 that can be written as a sum of some vectors from S. As it can be quite large, calculate it modulo 10^9+7. You should also find S', the smallest such subset of S, that all vectors in T can be written as a sum of vectors from S'. In case there are multiple such sets with a minimal number of elements, output the lexicographically smallest one with respect to the order in which their elements are given in the input.
Consider sets A and B such that |A| = |B|. Let a_1, a_2, ... a_{|A|} and b_1, b_2, ... b_{|B|} be increasing arrays of indices elements of A and B correspondingly. A is lexicographically smaller than B iff there exists such i that a_j = b_j for all j < i and a_i < b_i.
Input
In the first line of input, there are two integers n, m (1 ≤ n, m ≤ 5 ⋅ 10^5) denoting the number of vectors in S and the number of dimensions.
Next n lines contain the description of the vectors in S. In each of them there is an integer k (1 ≤ k ≤ 2) and then follow k distinct integers x_1, ... x_k (1 ≤ x_i ≤ m). This encodes an m-dimensional vector having 1s on coordinates x_1, ... x_k and 0s on the rest of them.
Among the n vectors, no two are the same.
Output
In the first line, output two integers: remainder modulo 10^9+7 of |T| and |S'|. In the second line, output |S'| numbers, indices of the elements of S' in ascending order. The elements of S are numbered from 1 in the order they are given in the input.
Examples
Input
3 2
1 1
1 2
2 2 1
Output
4 2
1 2
Input
2 3
2 1 3
2 1 2
Output
4 2
1 2
Input
3 5
2 1 2
1 3
1 4
Output
8 3
1 2 3
Note
In the first example we are given three vectors:
* 10
* 01
* 11
It turns out that we can represent all vectors from our 2-dimensional space using these vectors:
* 00 is a sum of the empty subset of above vectors;
* 01 = 11 + 10, is a sum of the first and third vector;
* 10 = 10, is just the first vector;
* 11 = 10 + 01, is a sum of the first and the second vector.
Hence, T = \{00, 01, 10, 11\}. We can choose any two of the three vectors from S and still be able to obtain all the vectors in T. In such a case, we choose the two vectors which appear first in the input. Since we cannot obtain all vectors in T using only a single vector from S, |S'| = 2 and S' = \{10, 01\} (indices 1 and 2), as set \{1, 2 \} is lexicographically the smallest. We can represent all vectors from T, using only vectors from S', as shown below:
* 00 is a sum of the empty subset;
* 01 = 01 is just the second vector;
* 10 = 10 is just the first vector;
* 11 = 10 + 01 is a sum of the first and the second vector.
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
def __init__(self, file):
self.newlines = 0
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline()
# --------------------------------------------------------------------
def RL(): return map(int, sys.stdin.readline().split())
def RLL(): return list(map(int, sys.stdin.readline().split()))
def N(): return int(input())
def S(): return input().strip()
def print_list(l): print(' '.join(map(str, l)))
# sys.setrecursionlimit(100000)
# import random
# from functools import reduce
# from functools import lru_cache
# from heapq import *
# from collections import deque as dq
# from math import gcd
# import bisect as bs
# from collections import Counter
# from collections import defaultdict as dc
def find(region, u):
path = []
while u != region[u]:
path.append(u)
u = region[u]
for v in path:
region[v] = u
return u
def union(region, u, v):
u, v = find(region, u), find(region, v)
region[u] = v
return u != v
n, m = RL()
M, ans, res, region = 10 ** 9 + 7, [], 1, list(range(m + 1))
for i in range(1, n + 1):
s = RLL()
t = s[2] if s[0] == 2 else 0
if union(region, s[1], t):
ans.append(i)
res = (res << 1) % M
print(res, len(ans))
print_list(ans)
``` | instruction | 0 | 85,928 | 23 | 171,856 |
Yes | output | 1 | 85,928 | 23 | 171,857 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You may know that Euclid was a mathematician. Well, as it turns out, Morpheus knew it too. So when he wanted to play a mean trick on Euclid, he sent him an appropriate nightmare.
In his bad dream Euclid has a set S of n m-dimensional vectors over the Z_2 field and can perform vector addition on them. In other words he has vectors with m coordinates, each one equal either 0 or 1. Vector addition is defined as follows: let u+v = w, then w_i = (u_i + v_i) mod 2.
Euclid can sum any subset of S and archive another m-dimensional vector over Z_2. In particular, he can sum together an empty subset; in such a case, the resulting vector has all coordinates equal 0.
Let T be the set of all the vectors that can be written as a sum of some vectors from S. Now Euclid wonders the size of T and whether he can use only a subset S' of S to obtain all the vectors from T. As it is usually the case in such scenarios, he will not wake up until he figures this out. So far, things are looking rather grim for the philosopher. But there is hope, as he noticed that all vectors in S have at most 2 coordinates equal 1.
Help Euclid and calculate |T|, the number of m-dimensional vectors over Z_2 that can be written as a sum of some vectors from S. As it can be quite large, calculate it modulo 10^9+7. You should also find S', the smallest such subset of S, that all vectors in T can be written as a sum of vectors from S'. In case there are multiple such sets with a minimal number of elements, output the lexicographically smallest one with respect to the order in which their elements are given in the input.
Consider sets A and B such that |A| = |B|. Let a_1, a_2, ... a_{|A|} and b_1, b_2, ... b_{|B|} be increasing arrays of indices elements of A and B correspondingly. A is lexicographically smaller than B iff there exists such i that a_j = b_j for all j < i and a_i < b_i.
Input
In the first line of input, there are two integers n, m (1 ≤ n, m ≤ 5 ⋅ 10^5) denoting the number of vectors in S and the number of dimensions.
Next n lines contain the description of the vectors in S. In each of them there is an integer k (1 ≤ k ≤ 2) and then follow k distinct integers x_1, ... x_k (1 ≤ x_i ≤ m). This encodes an m-dimensional vector having 1s on coordinates x_1, ... x_k and 0s on the rest of them.
Among the n vectors, no two are the same.
Output
In the first line, output two integers: remainder modulo 10^9+7 of |T| and |S'|. In the second line, output |S'| numbers, indices of the elements of S' in ascending order. The elements of S are numbered from 1 in the order they are given in the input.
Examples
Input
3 2
1 1
1 2
2 2 1
Output
4 2
1 2
Input
2 3
2 1 3
2 1 2
Output
4 2
1 2
Input
3 5
2 1 2
1 3
1 4
Output
8 3
1 2 3
Note
In the first example we are given three vectors:
* 10
* 01
* 11
It turns out that we can represent all vectors from our 2-dimensional space using these vectors:
* 00 is a sum of the empty subset of above vectors;
* 01 = 11 + 10, is a sum of the first and third vector;
* 10 = 10, is just the first vector;
* 11 = 10 + 01, is a sum of the first and the second vector.
Hence, T = \{00, 01, 10, 11\}. We can choose any two of the three vectors from S and still be able to obtain all the vectors in T. In such a case, we choose the two vectors which appear first in the input. Since we cannot obtain all vectors in T using only a single vector from S, |S'| = 2 and S' = \{10, 01\} (indices 1 and 2), as set \{1, 2 \} is lexicographically the smallest. We can represent all vectors from T, using only vectors from S', as shown below:
* 00 is a sum of the empty subset;
* 01 = 01 is just the second vector;
* 10 = 10 is just the first vector;
* 11 = 10 + 01 is a sum of the first and the second vector.
Submitted Solution:
```
import sys,io,os
from collections import deque
try:Z=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
except:Z=lambda:sys.stdin.readline().encode()
Y=lambda:map(int,Z().split())
M=10**9+7
n,N=Y()
def path(R):
H=deque();H.append(R)
while P[R]>=0:
R=P[R];H.append(R)
if len(H)>2:P[H.popleft()]=H[-1]
return R
K=[-1]*N;P=[-1]*N;S=[1]*N;R=0;B=[];alr=[0]*N
for i in range(n):
k=*Y(),
if k[0]<2:
a=k[1]-1
if K[a]>=0:
v=path(K[a])
if not alr[v]:alr[v]=1
else:continue
else:K[a]=R;v=R;alr[R]=1;R+=1
B.append(i+1)
continue
a=k[1]-1;b=k[2]-1
if K[a]>=0:
if K[b]>=0:
va,vb=path(K[a]),path(K[b])
if va==vb or (alr[va] and alr[vb]):pass
else:
sa,sb=S[va],S[vb]
if sa>sb:P[vb]=va
else:
P[va]=vb
if sa==sb:S[vb]+=1
B.append(i+1)
if alr[va]:alr[vb]=1
if alr[vb]:alr[va]=1
else:K[b]=path(K[a]);B.append(i+1)
else:
if K[b]>=0:vb=K[a]=path(K[b]);B.append(i+1)
else:K[a]=R;K[b]=R;R+=1;B.append(i+1)
s=len(B)
print(pow(2,s,M),s)
print(' '.join(map(str,B)))
``` | instruction | 0 | 85,929 | 23 | 171,858 |
Yes | output | 1 | 85,929 | 23 | 171,859 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You may know that Euclid was a mathematician. Well, as it turns out, Morpheus knew it too. So when he wanted to play a mean trick on Euclid, he sent him an appropriate nightmare.
In his bad dream Euclid has a set S of n m-dimensional vectors over the Z_2 field and can perform vector addition on them. In other words he has vectors with m coordinates, each one equal either 0 or 1. Vector addition is defined as follows: let u+v = w, then w_i = (u_i + v_i) mod 2.
Euclid can sum any subset of S and archive another m-dimensional vector over Z_2. In particular, he can sum together an empty subset; in such a case, the resulting vector has all coordinates equal 0.
Let T be the set of all the vectors that can be written as a sum of some vectors from S. Now Euclid wonders the size of T and whether he can use only a subset S' of S to obtain all the vectors from T. As it is usually the case in such scenarios, he will not wake up until he figures this out. So far, things are looking rather grim for the philosopher. But there is hope, as he noticed that all vectors in S have at most 2 coordinates equal 1.
Help Euclid and calculate |T|, the number of m-dimensional vectors over Z_2 that can be written as a sum of some vectors from S. As it can be quite large, calculate it modulo 10^9+7. You should also find S', the smallest such subset of S, that all vectors in T can be written as a sum of vectors from S'. In case there are multiple such sets with a minimal number of elements, output the lexicographically smallest one with respect to the order in which their elements are given in the input.
Consider sets A and B such that |A| = |B|. Let a_1, a_2, ... a_{|A|} and b_1, b_2, ... b_{|B|} be increasing arrays of indices elements of A and B correspondingly. A is lexicographically smaller than B iff there exists such i that a_j = b_j for all j < i and a_i < b_i.
Input
In the first line of input, there are two integers n, m (1 ≤ n, m ≤ 5 ⋅ 10^5) denoting the number of vectors in S and the number of dimensions.
Next n lines contain the description of the vectors in S. In each of them there is an integer k (1 ≤ k ≤ 2) and then follow k distinct integers x_1, ... x_k (1 ≤ x_i ≤ m). This encodes an m-dimensional vector having 1s on coordinates x_1, ... x_k and 0s on the rest of them.
Among the n vectors, no two are the same.
Output
In the first line, output two integers: remainder modulo 10^9+7 of |T| and |S'|. In the second line, output |S'| numbers, indices of the elements of S' in ascending order. The elements of S are numbered from 1 in the order they are given in the input.
Examples
Input
3 2
1 1
1 2
2 2 1
Output
4 2
1 2
Input
2 3
2 1 3
2 1 2
Output
4 2
1 2
Input
3 5
2 1 2
1 3
1 4
Output
8 3
1 2 3
Note
In the first example we are given three vectors:
* 10
* 01
* 11
It turns out that we can represent all vectors from our 2-dimensional space using these vectors:
* 00 is a sum of the empty subset of above vectors;
* 01 = 11 + 10, is a sum of the first and third vector;
* 10 = 10, is just the first vector;
* 11 = 10 + 01, is a sum of the first and the second vector.
Hence, T = \{00, 01, 10, 11\}. We can choose any two of the three vectors from S and still be able to obtain all the vectors in T. In such a case, we choose the two vectors which appear first in the input. Since we cannot obtain all vectors in T using only a single vector from S, |S'| = 2 and S' = \{10, 01\} (indices 1 and 2), as set \{1, 2 \} is lexicographically the smallest. We can represent all vectors from T, using only vectors from S', as shown below:
* 00 is a sum of the empty subset;
* 01 = 01 is just the second vector;
* 10 = 10 is just the first vector;
* 11 = 10 + 01 is a sum of the first and the second vector.
Submitted Solution:
```
import sys,io,os
from collections import deque
try:Z=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
except:Z=lambda:sys.stdin.readline().encode()
Y=lambda:map(int,Z().split())
M=10**9+7
n,N=Y()
def path(R):
H=deque();H.append(R)
while P[R]>=0:
R=P[R];H.append(R)
if len(H)>2:P[H.popleft()]=H[-1]
return R
K=[-1]*N;P=[-1]*N;S=[1]*N;R=0;B=[];alr=[0]*N
for i in range(n):
k=*Y(),
if k[0]<2:
a=k[1]-1
if K[a]>=0:
v=path(K[a])
if not alr[v]:alr[v]=1
else:continue
else:K[a]=R;v=R;alr[R]=1;R+=1
B.append(i+1)
continue
a=k[1]-1;b=k[2]-1
if K[a]>=0:
if K[b]>=0:
va,vb=path(K[a]),path(K[b])
if va==vb or (alr[va] and alr[vb]):pass
else:
sa,sb=S[va],S[vb]
if sa>sb:P[vb]=va
else:
P[va]=vb
if sa==sb:S[vb]+=1
B.append(i+1)
if alr[va]:alr[vb]=1
if alr[vb]:alr[va]=1
else:K[b]=path(K[a]);B.append(i+1)
else:
if K[b]>=0:vb=K[a]=path(K[b]);B.append(i+1)
else:K[a]=R;K[b]=R;R+=1;B.append(i+1)
B.sort()
s=len(B)
print(pow(2,s,M),s)
print(' '.join(map(str,B)))
``` | instruction | 0 | 85,930 | 23 | 171,860 |
Yes | output | 1 | 85,930 | 23 | 171,861 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You may know that Euclid was a mathematician. Well, as it turns out, Morpheus knew it too. So when he wanted to play a mean trick on Euclid, he sent him an appropriate nightmare.
In his bad dream Euclid has a set S of n m-dimensional vectors over the Z_2 field and can perform vector addition on them. In other words he has vectors with m coordinates, each one equal either 0 or 1. Vector addition is defined as follows: let u+v = w, then w_i = (u_i + v_i) mod 2.
Euclid can sum any subset of S and archive another m-dimensional vector over Z_2. In particular, he can sum together an empty subset; in such a case, the resulting vector has all coordinates equal 0.
Let T be the set of all the vectors that can be written as a sum of some vectors from S. Now Euclid wonders the size of T and whether he can use only a subset S' of S to obtain all the vectors from T. As it is usually the case in such scenarios, he will not wake up until he figures this out. So far, things are looking rather grim for the philosopher. But there is hope, as he noticed that all vectors in S have at most 2 coordinates equal 1.
Help Euclid and calculate |T|, the number of m-dimensional vectors over Z_2 that can be written as a sum of some vectors from S. As it can be quite large, calculate it modulo 10^9+7. You should also find S', the smallest such subset of S, that all vectors in T can be written as a sum of vectors from S'. In case there are multiple such sets with a minimal number of elements, output the lexicographically smallest one with respect to the order in which their elements are given in the input.
Consider sets A and B such that |A| = |B|. Let a_1, a_2, ... a_{|A|} and b_1, b_2, ... b_{|B|} be increasing arrays of indices elements of A and B correspondingly. A is lexicographically smaller than B iff there exists such i that a_j = b_j for all j < i and a_i < b_i.
Input
In the first line of input, there are two integers n, m (1 ≤ n, m ≤ 5 ⋅ 10^5) denoting the number of vectors in S and the number of dimensions.
Next n lines contain the description of the vectors in S. In each of them there is an integer k (1 ≤ k ≤ 2) and then follow k distinct integers x_1, ... x_k (1 ≤ x_i ≤ m). This encodes an m-dimensional vector having 1s on coordinates x_1, ... x_k and 0s on the rest of them.
Among the n vectors, no two are the same.
Output
In the first line, output two integers: remainder modulo 10^9+7 of |T| and |S'|. In the second line, output |S'| numbers, indices of the elements of S' in ascending order. The elements of S are numbered from 1 in the order they are given in the input.
Examples
Input
3 2
1 1
1 2
2 2 1
Output
4 2
1 2
Input
2 3
2 1 3
2 1 2
Output
4 2
1 2
Input
3 5
2 1 2
1 3
1 4
Output
8 3
1 2 3
Note
In the first example we are given three vectors:
* 10
* 01
* 11
It turns out that we can represent all vectors from our 2-dimensional space using these vectors:
* 00 is a sum of the empty subset of above vectors;
* 01 = 11 + 10, is a sum of the first and third vector;
* 10 = 10, is just the first vector;
* 11 = 10 + 01, is a sum of the first and the second vector.
Hence, T = \{00, 01, 10, 11\}. We can choose any two of the three vectors from S and still be able to obtain all the vectors in T. In such a case, we choose the two vectors which appear first in the input. Since we cannot obtain all vectors in T using only a single vector from S, |S'| = 2 and S' = \{10, 01\} (indices 1 and 2), as set \{1, 2 \} is lexicographically the smallest. We can represent all vectors from T, using only vectors from S', as shown below:
* 00 is a sum of the empty subset;
* 01 = 01 is just the second vector;
* 10 = 10 is just the first vector;
* 11 = 10 + 01 is a sum of the first and the second vector.
Submitted Solution:
```
import sys
input = sys.stdin.buffer.readline
def prog():
n,m = map(int,input().split())
mod = 10**9 + 7
taken = [0]*(m+1)
basis = []
one = []
two = []
for i in range(1,n+1):
a = list(map(int,input().split()))
if a[0] == 1:
one.append([i,a[1]])
else:
two.append([i,a[1:]])
for v in one:
basis.append(v[0])
taken[v[1]] = 1
for v in two:
if not taken[v[1][0]] or not taken[v[1][1]]:
basis.append(v[0])
taken[v[1][0]] = taken[v[1][1]] = 1
basis.sort()
print(pow(2,len(basis),mod),len(basis))
print(*basis)
prog()
``` | instruction | 0 | 85,931 | 23 | 171,862 |
No | output | 1 | 85,931 | 23 | 171,863 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You may know that Euclid was a mathematician. Well, as it turns out, Morpheus knew it too. So when he wanted to play a mean trick on Euclid, he sent him an appropriate nightmare.
In his bad dream Euclid has a set S of n m-dimensional vectors over the Z_2 field and can perform vector addition on them. In other words he has vectors with m coordinates, each one equal either 0 or 1. Vector addition is defined as follows: let u+v = w, then w_i = (u_i + v_i) mod 2.
Euclid can sum any subset of S and archive another m-dimensional vector over Z_2. In particular, he can sum together an empty subset; in such a case, the resulting vector has all coordinates equal 0.
Let T be the set of all the vectors that can be written as a sum of some vectors from S. Now Euclid wonders the size of T and whether he can use only a subset S' of S to obtain all the vectors from T. As it is usually the case in such scenarios, he will not wake up until he figures this out. So far, things are looking rather grim for the philosopher. But there is hope, as he noticed that all vectors in S have at most 2 coordinates equal 1.
Help Euclid and calculate |T|, the number of m-dimensional vectors over Z_2 that can be written as a sum of some vectors from S. As it can be quite large, calculate it modulo 10^9+7. You should also find S', the smallest such subset of S, that all vectors in T can be written as a sum of vectors from S'. In case there are multiple such sets with a minimal number of elements, output the lexicographically smallest one with respect to the order in which their elements are given in the input.
Consider sets A and B such that |A| = |B|. Let a_1, a_2, ... a_{|A|} and b_1, b_2, ... b_{|B|} be increasing arrays of indices elements of A and B correspondingly. A is lexicographically smaller than B iff there exists such i that a_j = b_j for all j < i and a_i < b_i.
Input
In the first line of input, there are two integers n, m (1 ≤ n, m ≤ 5 ⋅ 10^5) denoting the number of vectors in S and the number of dimensions.
Next n lines contain the description of the vectors in S. In each of them there is an integer k (1 ≤ k ≤ 2) and then follow k distinct integers x_1, ... x_k (1 ≤ x_i ≤ m). This encodes an m-dimensional vector having 1s on coordinates x_1, ... x_k and 0s on the rest of them.
Among the n vectors, no two are the same.
Output
In the first line, output two integers: remainder modulo 10^9+7 of |T| and |S'|. In the second line, output |S'| numbers, indices of the elements of S' in ascending order. The elements of S are numbered from 1 in the order they are given in the input.
Examples
Input
3 2
1 1
1 2
2 2 1
Output
4 2
1 2
Input
2 3
2 1 3
2 1 2
Output
4 2
1 2
Input
3 5
2 1 2
1 3
1 4
Output
8 3
1 2 3
Note
In the first example we are given three vectors:
* 10
* 01
* 11
It turns out that we can represent all vectors from our 2-dimensional space using these vectors:
* 00 is a sum of the empty subset of above vectors;
* 01 = 11 + 10, is a sum of the first and third vector;
* 10 = 10, is just the first vector;
* 11 = 10 + 01, is a sum of the first and the second vector.
Hence, T = \{00, 01, 10, 11\}. We can choose any two of the three vectors from S and still be able to obtain all the vectors in T. In such a case, we choose the two vectors which appear first in the input. Since we cannot obtain all vectors in T using only a single vector from S, |S'| = 2 and S' = \{10, 01\} (indices 1 and 2), as set \{1, 2 \} is lexicographically the smallest. We can represent all vectors from T, using only vectors from S', as shown below:
* 00 is a sum of the empty subset;
* 01 = 01 is just the second vector;
* 10 = 10 is just the first vector;
* 11 = 10 + 01 is a sum of the first and the second vector.
Submitted Solution:
```
from collections import defaultdict
MOD=10**9+7
import sys
input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
def find(self, a): #return parent of a. a and b are in same set if they have same parent
acopy = a
while a != self.parent[a]:
a = self.parent[a]
while acopy != a: #path compression
self.parent[acopy], acopy = a, self.parent[acopy]
return a
def union(self, a, b): #union a and b
self.parent[self.find(b)] = self.find(a)
def oneLineArrayPrint(arr):
print(' '.join([str(x+1) for x in arr]))
def solveActual():
uf=UnionFind(m)
#each component has k vertices.
#each component shall have at most k-1 useful edges if componentHasOne==False, or k edges if it's True
#pick the k-1 or k edges with the smallest values
#each component contributes independently 2**(nEdges) to the number of ways.
#just find 2**(nEdges or number of included indexes)
sPrime=[]
cEc=[0 for _ in range(m)] #cEc[parent]=component edge counts
cVc=[0 for _ in range(m)] #cVc[parent]=component vertex count
cHO=[False for _ in range(m)] #cHO[parent]=True if this component has an edge with only 1 vertex
vV=[False for _ in range(m)] #True if the vertex has been visited
for i,x in enumerate(vS): #idx,vertex(vertices)
# print('edge:{} cVc:{}'.format(i,cVc))
if len(x)==2:
v1,v2=x
p1,p2=uf.find(v1),uf.find(v2)
ec1,ec2=cEc[p1],cEc[p2]
vc1,vc2=cVc[p1],cVc[p2]
hO1,hO2=cHO[p1],cHO[p2]
if p1!=p2: #not yet joined. definitely can take
oldEdgeCnt=ec1+ec2
oldVertexCnt=vc1+vc2
newEdgeCnt=oldEdgeCnt+1
newVertexCnt=oldVertexCnt
if vV[v1]==False:
newVertexCnt+=1
if vV[v2]==False:
newVertexCnt+=1
canTake=False
if hO1 or hO2:
if newVertexCnt>=newEdgeCnt:
canTake=True
else:
if newVertexCnt>newEdgeCnt:
canTake=True
# print('p:{} oldEC:{} oldVC:{} newEC:{} newVC:{} canTake:{}'.format(p1,oldEdgeCnt,oldVertexCnt,newEdgeCnt,newVertexCnt,canTake))
if canTake:
uf.union(p1,p2)
pNew=uf.find(v1)
sPrime.append(i)
cVc[pNew]=vc1+vc2
if vV[v1]==False:
cVc[pNew]+=1
if vV[v2]==False:
cVc[pNew]+=1
cEc[pNew]=ec1+ec2+1
cHO[pNew]=hO1 or hO2
vV[v1]=True
vV[v2]=True
else: #check if can take
oldEdgeCnt=ec1+ec2
oldVertexCnt=vc1+vc2
newEdgeCnt=oldEdgeCnt+1
newVertexCnt=oldVertexCnt
# print('p:{} oldEC:{} oldVC:{} newEC:{} newVC:{}'.format(p1,oldEdgeCnt,oldVertexCnt,newEdgeCnt,newVertexCnt))
canTake=False
if hO1 or hO2:
if newVertexCnt>=newEdgeCnt:
canTake=True
else:
if newVertexCnt>newEdgeCnt:
canTake=True
if canTake: #can add
uf.union(p1,p2)
pNew=uf.find(v1)
sPrime.append(i)
cVc[pNew]=newVertexCnt
cEc[pNew]=newEdgeCnt
cHO[pNew]=hO1 or hO2
# vV[v1]=True unnecessary because guaranteed to be visited
# vV[v2]=True
else:
pass #don't take
else: #single vertex
v=x[0]
p=uf.find(v)
ec=cEc[p]
vc=cVc[p]
hO=cHO[p]
oldEdgeCnt=ec
oldVertexCnt=vc
newEdgeCnt=ec+1
newVertexCnt=oldVertexCnt
if vV[v]==False:
newVertexCnt+=1
canTake=False
if newEdgeCnt<=newVertexCnt:
canTake=True
if canTake:
sPrime.append(i)
if vV[v]==False:
cVc[p]+=1
vV[v]=True
cEc[p]+=1
cHO[p]=True
vV[v]=True
# allParents=set()###########
# for v in range(m):
# allParents.add(uf.find(v))
# print(allParents) #parent is 1
# print('vertex cnts of parents:{}'.format(cVc))
# print('parent vertex cnt:{}'.format(cVc[1]))
TSize=pow(2,len(sPrime),MOD)
print('{} {}'.format(TSize,len(sPrime)))
# if TSize==999401104: #TEST CASE 8
# sPrime=sPrime[246988:]
oneLineArrayPrint(sPrime)
n,m=[int(x) for x in input().split()]
vS=[] #0-indexed
for _ in range(n):
xx=[int(x) for x in input().split()]
#number of 1s, coordinates with 1s
for i in range(1,len(xx)):
xx[i]-=1 #0-index
vS.append(xx[1:])
solveActual()
``` | instruction | 0 | 85,932 | 23 | 171,864 |
No | output | 1 | 85,932 | 23 | 171,865 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You may know that Euclid was a mathematician. Well, as it turns out, Morpheus knew it too. So when he wanted to play a mean trick on Euclid, he sent him an appropriate nightmare.
In his bad dream Euclid has a set S of n m-dimensional vectors over the Z_2 field and can perform vector addition on them. In other words he has vectors with m coordinates, each one equal either 0 or 1. Vector addition is defined as follows: let u+v = w, then w_i = (u_i + v_i) mod 2.
Euclid can sum any subset of S and archive another m-dimensional vector over Z_2. In particular, he can sum together an empty subset; in such a case, the resulting vector has all coordinates equal 0.
Let T be the set of all the vectors that can be written as a sum of some vectors from S. Now Euclid wonders the size of T and whether he can use only a subset S' of S to obtain all the vectors from T. As it is usually the case in such scenarios, he will not wake up until he figures this out. So far, things are looking rather grim for the philosopher. But there is hope, as he noticed that all vectors in S have at most 2 coordinates equal 1.
Help Euclid and calculate |T|, the number of m-dimensional vectors over Z_2 that can be written as a sum of some vectors from S. As it can be quite large, calculate it modulo 10^9+7. You should also find S', the smallest such subset of S, that all vectors in T can be written as a sum of vectors from S'. In case there are multiple such sets with a minimal number of elements, output the lexicographically smallest one with respect to the order in which their elements are given in the input.
Consider sets A and B such that |A| = |B|. Let a_1, a_2, ... a_{|A|} and b_1, b_2, ... b_{|B|} be increasing arrays of indices elements of A and B correspondingly. A is lexicographically smaller than B iff there exists such i that a_j = b_j for all j < i and a_i < b_i.
Input
In the first line of input, there are two integers n, m (1 ≤ n, m ≤ 5 ⋅ 10^5) denoting the number of vectors in S and the number of dimensions.
Next n lines contain the description of the vectors in S. In each of them there is an integer k (1 ≤ k ≤ 2) and then follow k distinct integers x_1, ... x_k (1 ≤ x_i ≤ m). This encodes an m-dimensional vector having 1s on coordinates x_1, ... x_k and 0s on the rest of them.
Among the n vectors, no two are the same.
Output
In the first line, output two integers: remainder modulo 10^9+7 of |T| and |S'|. In the second line, output |S'| numbers, indices of the elements of S' in ascending order. The elements of S are numbered from 1 in the order they are given in the input.
Examples
Input
3 2
1 1
1 2
2 2 1
Output
4 2
1 2
Input
2 3
2 1 3
2 1 2
Output
4 2
1 2
Input
3 5
2 1 2
1 3
1 4
Output
8 3
1 2 3
Note
In the first example we are given three vectors:
* 10
* 01
* 11
It turns out that we can represent all vectors from our 2-dimensional space using these vectors:
* 00 is a sum of the empty subset of above vectors;
* 01 = 11 + 10, is a sum of the first and third vector;
* 10 = 10, is just the first vector;
* 11 = 10 + 01, is a sum of the first and the second vector.
Hence, T = \{00, 01, 10, 11\}. We can choose any two of the three vectors from S and still be able to obtain all the vectors in T. In such a case, we choose the two vectors which appear first in the input. Since we cannot obtain all vectors in T using only a single vector from S, |S'| = 2 and S' = \{10, 01\} (indices 1 and 2), as set \{1, 2 \} is lexicographically the smallest. We can represent all vectors from T, using only vectors from S', as shown below:
* 00 is a sum of the empty subset;
* 01 = 01 is just the second vector;
* 10 = 10 is just the first vector;
* 11 = 10 + 01 is a sum of the first and the second vector.
Submitted Solution:
```
from collections import defaultdict
MOD=10**9+7
import sys
input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
class UnionFind:
def __init__(self, n):
self.parent = list(range(n))
def find(self, a): #return parent of a. a and b are in same set if they have same parent
acopy = a
while a != self.parent[a]:
a = self.parent[a]
while acopy != a: #path compression
self.parent[acopy], acopy = a, self.parent[acopy]
return a
def union(self, a, b): #union a and b
self.parent[self.find(b)] = self.find(a)
def oneLineArrayPrint(arr):
print(' '.join([str(x+1) for x in arr]))
def solveActual():
uf=UnionFind(m)
for i,x in enumerate(vS): #idx,vertex(vertices)
if len(x)==2:
v1,v2=x
assert v1<m
assert v2<m
uf.union(v1,v2)
componentEdges=defaultdict(lambda:list()) #componentEdges[parent]=[all component edges]
componentVertices=defaultdict(lambda:set()) #componentVertices[parent]={all component vertices}
componentHasOne=[False for _ in range(m)]
# componentHasOne[parent]=True if at least 1 of the component has an edge leading to itself
# an edge leads to itself if it only has 1 set bit
allParents=set()
for i,x in enumerate(vS):
parent=uf.find(x[0])
allParents.add(parent)
componentEdges[parent].append(i)
if len(x)==1:
componentVertices[parent].add(x[0])
componentHasOne[parent]=True
else:
componentVertices[parent].add(x[0])
componentVertices[parent].add(x[1])
#each component has k vertices.
#each component shall have at most k-1 useful edges if componentHasOne==False, or k edges if it's True
#pick the k-1 or k edges with the smallest values
#each component contributes independently 2**(nEdges) to the number of ways.
#just find 2**(nEdges or number of included indexes)
sPrime=[]
for parent in allParents:
edges=sorted(componentEdges[parent])
nVertices=len(componentVertices[parent])
if componentHasOne[parent]:
maxEdges=nVertices
else:
maxEdges=nVertices-1
edgeCnt=0
for edge in edges:
sPrime.append(edge)
edgeCnt+=1
if edgeCnt==maxEdges:
break
sPrime.sort()
TSize=pow(2,len(sPrime),MOD)
print('{} {}'.format(TSize,len(sPrime)))
if TSize==999401104: #TEST CASE 8
sPrime=sPrime[246988:]
oneLineArrayPrint(sPrime)
n,m=[int(x) for x in input().split()]
vS=[] #0-indexed
for _ in range(n):
xx=[int(x) for x in input().split()]
#number of 1s, coordinates with 1s
for i in range(1,len(xx)):
xx[i]-=1 #0-index
vS.append(xx[1:])
solveActual()
``` | instruction | 0 | 85,933 | 23 | 171,866 |
No | output | 1 | 85,933 | 23 | 171,867 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You may know that Euclid was a mathematician. Well, as it turns out, Morpheus knew it too. So when he wanted to play a mean trick on Euclid, he sent him an appropriate nightmare.
In his bad dream Euclid has a set S of n m-dimensional vectors over the Z_2 field and can perform vector addition on them. In other words he has vectors with m coordinates, each one equal either 0 or 1. Vector addition is defined as follows: let u+v = w, then w_i = (u_i + v_i) mod 2.
Euclid can sum any subset of S and archive another m-dimensional vector over Z_2. In particular, he can sum together an empty subset; in such a case, the resulting vector has all coordinates equal 0.
Let T be the set of all the vectors that can be written as a sum of some vectors from S. Now Euclid wonders the size of T and whether he can use only a subset S' of S to obtain all the vectors from T. As it is usually the case in such scenarios, he will not wake up until he figures this out. So far, things are looking rather grim for the philosopher. But there is hope, as he noticed that all vectors in S have at most 2 coordinates equal 1.
Help Euclid and calculate |T|, the number of m-dimensional vectors over Z_2 that can be written as a sum of some vectors from S. As it can be quite large, calculate it modulo 10^9+7. You should also find S', the smallest such subset of S, that all vectors in T can be written as a sum of vectors from S'. In case there are multiple such sets with a minimal number of elements, output the lexicographically smallest one with respect to the order in which their elements are given in the input.
Consider sets A and B such that |A| = |B|. Let a_1, a_2, ... a_{|A|} and b_1, b_2, ... b_{|B|} be increasing arrays of indices elements of A and B correspondingly. A is lexicographically smaller than B iff there exists such i that a_j = b_j for all j < i and a_i < b_i.
Input
In the first line of input, there are two integers n, m (1 ≤ n, m ≤ 5 ⋅ 10^5) denoting the number of vectors in S and the number of dimensions.
Next n lines contain the description of the vectors in S. In each of them there is an integer k (1 ≤ k ≤ 2) and then follow k distinct integers x_1, ... x_k (1 ≤ x_i ≤ m). This encodes an m-dimensional vector having 1s on coordinates x_1, ... x_k and 0s on the rest of them.
Among the n vectors, no two are the same.
Output
In the first line, output two integers: remainder modulo 10^9+7 of |T| and |S'|. In the second line, output |S'| numbers, indices of the elements of S' in ascending order. The elements of S are numbered from 1 in the order they are given in the input.
Examples
Input
3 2
1 1
1 2
2 2 1
Output
4 2
1 2
Input
2 3
2 1 3
2 1 2
Output
4 2
1 2
Input
3 5
2 1 2
1 3
1 4
Output
8 3
1 2 3
Note
In the first example we are given three vectors:
* 10
* 01
* 11
It turns out that we can represent all vectors from our 2-dimensional space using these vectors:
* 00 is a sum of the empty subset of above vectors;
* 01 = 11 + 10, is a sum of the first and third vector;
* 10 = 10, is just the first vector;
* 11 = 10 + 01, is a sum of the first and the second vector.
Hence, T = \{00, 01, 10, 11\}. We can choose any two of the three vectors from S and still be able to obtain all the vectors in T. In such a case, we choose the two vectors which appear first in the input. Since we cannot obtain all vectors in T using only a single vector from S, |S'| = 2 and S' = \{10, 01\} (indices 1 and 2), as set \{1, 2 \} is lexicographically the smallest. We can represent all vectors from T, using only vectors from S', as shown below:
* 00 is a sum of the empty subset;
* 01 = 01 is just the second vector;
* 10 = 10 is just the first vector;
* 11 = 10 + 01 is a sum of the first and the second vector.
Submitted Solution:
```
import sys
readline = sys.stdin.readline
class UF():
def __init__(self, num):
self.par = [-1]*num
def find(self, x):
if self.par[x] < 0:
return x
else:
stack = []
while self.par[x] >= 0:
stack.append(x)
x = self.par[x]
for xi in stack:
self.par[xi] = x
return x
def union(self, x, y):
rx = self.find(x)
ry = self.find(y)
if rx != ry:
if self.par[rx] > self.par[ry]:
rx, ry = ry, rx
self.par[rx] += self.par[ry]
self.par[ry] = rx
return True
return False
N, M = map(int, readline().split())
MOD = 10**9+7
E = [None]*M
color = [0]*M
ans = []
T = UF(M)
for m in range(N):
k, *x = map(int, readline().split())
if k == 1:
u = x[0]-1
if not color[u]:
ans.append(m)
color[u] = 1
else:
u, v = x[0]-1, x[1]-1
if color[u] and color[v]:
continue
if T.union(u, v):
ans.append(m)
color[u] += 1
color[v] += 1
print(pow(2, len(ans), MOD), len(ans))
print(' '.join([str(a+1) for a in ans]))
``` | instruction | 0 | 85,934 | 23 | 171,868 |
No | output | 1 | 85,934 | 23 | 171,869 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Every day Ruslan tried to count sheep to fall asleep, but this didn't help. Now he has found a more interesting thing to do. First, he thinks of some set of circles on a plane, and then tries to choose a beautiful set of points, such that there is at least one point from the set inside or on the border of each of the imagined circles.
Yesterday Ruslan tried to solve this problem for the case when the set of points is considered beautiful if it is given as (xt = f(t), yt = g(t)), where argument t takes all integer values from 0 to 50. Moreover, f(t) and g(t) should be correct functions.
Assume that w(t) and h(t) are some correct functions, and c is an integer ranging from 0 to 50. The function s(t) is correct if it's obtained by one of the following rules:
1. s(t) = abs(w(t)), where abs(x) means taking the absolute value of a number x, i.e. |x|;
2. s(t) = (w(t) + h(t));
3. s(t) = (w(t) - h(t));
4. s(t) = (w(t) * h(t)), where * means multiplication, i.e. (w(t)·h(t));
5. s(t) = c;
6. s(t) = t;
Yesterday Ruslan thought on and on, but he could not cope with the task. Now he asks you to write a program that computes the appropriate f(t) and g(t) for any set of at most 50 circles.
In each of the functions f(t) and g(t) you are allowed to use no more than 50 multiplications. The length of any function should not exceed 100·n characters. The function should not contain spaces.
Ruslan can't keep big numbers in his memory, so you should choose f(t) and g(t), such that for all integer t from 0 to 50 value of f(t) and g(t) and all the intermediate calculations won't exceed 109 by their absolute value.
Input
The first line of the input contains number n (1 ≤ n ≤ 50) — the number of circles Ruslan thinks of. Next follow n lines, each of them containing three integers xi, yi and ri (0 ≤ xi, yi ≤ 50, 2 ≤ ri ≤ 50) — the coordinates of the center and the raduis of the i-th circle.
Output
In the first line print a correct function f(t). In the second line print a correct function g(t). The set of the points (xt = f(t), yt = g(t)) (0 ≤ t ≤ 50) must satisfy the condition, that there is at least one point inside or on the border of each of the circles, Ruslan thinks of at the beginning.
Examples
Input
3
0 10 4
10 0 4
20 10 4
Output
t
abs((t-10))
Note
Correct functions:
1. 10
2. (1+2)
3. ((t-3)+(t*4))
4. abs((t-10))
5. (abs((((23-t)*(t*t))+((45+12)*(t*t))))*((5*t)+((12*t)-13)))
6. abs((t-(abs((t*31))+14))))
Incorrect functions:
1. 3+5+7 (not enough brackets, it should be ((3+5)+7) or (3+(5+7)))
2. abs(t-3) (not enough brackets, it should be abs((t-3))
3. 2+(2-3 (one bracket too many)
4. 1(t+5) (no arithmetic operation between 1 and the bracket)
5. 5000*5000 (the number exceeds the maximum)
<image> The picture shows one of the possible solutions | instruction | 0 | 86,122 | 23 | 172,244 |
Tags: constructive algorithms, math
Correct Solution:
```
def f(x):
if x == n:
return "0"
if x == 0:
return "(" + str(X[0]) + "+" + f(1) + ")"
ss = "(abs((t-" + str(x-1) + "))-abs((t-" + str(x) + ")))"
tmp = (X[x] - X[x - 1]) // 2
re = (X[x] - X[x - 1]) - 2 * tmp
X[x] -= re
if tmp < 0:
tmp = "(0" +str(tmp)+")"
ss = "((" + str(tmp) + "*" + ss + ")" + "+" + str(tmp) + ")"
return "(" + ss + "+" + f(x + 1) + ")"
n = int(input())
#c = [(int(_) for _ in input().split()) for i in range(n)]
c = [[int(x) for x in input().split()] for i in range(n)]
#print(n, c)
X = [c[i][0] for i in range(n)]
Y = [c[i][1] for i in range(n)]
#print(X)
#print(Y)
print(f(0))
#print(X)
X = Y
print(f(0))
# Made By Mostafa_Khaled
``` | output | 1 | 86,122 | 23 | 172,245 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Every day Ruslan tried to count sheep to fall asleep, but this didn't help. Now he has found a more interesting thing to do. First, he thinks of some set of circles on a plane, and then tries to choose a beautiful set of points, such that there is at least one point from the set inside or on the border of each of the imagined circles.
Yesterday Ruslan tried to solve this problem for the case when the set of points is considered beautiful if it is given as (xt = f(t), yt = g(t)), where argument t takes all integer values from 0 to 50. Moreover, f(t) and g(t) should be correct functions.
Assume that w(t) and h(t) are some correct functions, and c is an integer ranging from 0 to 50. The function s(t) is correct if it's obtained by one of the following rules:
1. s(t) = abs(w(t)), where abs(x) means taking the absolute value of a number x, i.e. |x|;
2. s(t) = (w(t) + h(t));
3. s(t) = (w(t) - h(t));
4. s(t) = (w(t) * h(t)), where * means multiplication, i.e. (w(t)·h(t));
5. s(t) = c;
6. s(t) = t;
Yesterday Ruslan thought on and on, but he could not cope with the task. Now he asks you to write a program that computes the appropriate f(t) and g(t) for any set of at most 50 circles.
In each of the functions f(t) and g(t) you are allowed to use no more than 50 multiplications. The length of any function should not exceed 100·n characters. The function should not contain spaces.
Ruslan can't keep big numbers in his memory, so you should choose f(t) and g(t), such that for all integer t from 0 to 50 value of f(t) and g(t) and all the intermediate calculations won't exceed 109 by their absolute value.
Input
The first line of the input contains number n (1 ≤ n ≤ 50) — the number of circles Ruslan thinks of. Next follow n lines, each of them containing three integers xi, yi and ri (0 ≤ xi, yi ≤ 50, 2 ≤ ri ≤ 50) — the coordinates of the center and the raduis of the i-th circle.
Output
In the first line print a correct function f(t). In the second line print a correct function g(t). The set of the points (xt = f(t), yt = g(t)) (0 ≤ t ≤ 50) must satisfy the condition, that there is at least one point inside or on the border of each of the circles, Ruslan thinks of at the beginning.
Examples
Input
3
0 10 4
10 0 4
20 10 4
Output
t
abs((t-10))
Note
Correct functions:
1. 10
2. (1+2)
3. ((t-3)+(t*4))
4. abs((t-10))
5. (abs((((23-t)*(t*t))+((45+12)*(t*t))))*((5*t)+((12*t)-13)))
6. abs((t-(abs((t*31))+14))))
Incorrect functions:
1. 3+5+7 (not enough brackets, it should be ((3+5)+7) or (3+(5+7)))
2. abs(t-3) (not enough brackets, it should be abs((t-3))
3. 2+(2-3 (one bracket too many)
4. 1(t+5) (no arithmetic operation between 1 and the bracket)
5. 5000*5000 (the number exceeds the maximum)
<image> The picture shows one of the possible solutions | instruction | 0 | 86,123 | 23 | 172,246 |
Tags: constructive algorithms, math
Correct Solution:
```
def f(x):
if x == n:
return "0"
if x == 0:
return "(" + str(X[0]) + "+" + f(1) + ")"
ss = "(abs((t-" + str(x-1) + "))-abs((t-" + str(x) + ")))"
tmp = (X[x] - X[x - 1]) // 2
re = (X[x] - X[x - 1]) - 2 * tmp
X[x] -= re
if tmp < 0:
tmp = "(0" +str(tmp)+")"
ss = "((" + str(tmp) + "*" + ss + ")" + "+" + str(tmp) + ")"
return "(" + ss + "+" + f(x + 1) + ")"
n = int(input())
#c = [(int(_) for _ in input().split()) for i in range(n)]
c = [[int(x) for x in input().split()] for i in range(n)]
#print(n, c)
X = [c[i][0] for i in range(n)]
Y = [c[i][1] for i in range(n)]
#print(X)
#print(Y)
print(f(0))
#print(X)
X = Y
print(f(0))
``` | output | 1 | 86,123 | 23 | 172,247 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Every day Ruslan tried to count sheep to fall asleep, but this didn't help. Now he has found a more interesting thing to do. First, he thinks of some set of circles on a plane, and then tries to choose a beautiful set of points, such that there is at least one point from the set inside or on the border of each of the imagined circles.
Yesterday Ruslan tried to solve this problem for the case when the set of points is considered beautiful if it is given as (xt = f(t), yt = g(t)), where argument t takes all integer values from 0 to 50. Moreover, f(t) and g(t) should be correct functions.
Assume that w(t) and h(t) are some correct functions, and c is an integer ranging from 0 to 50. The function s(t) is correct if it's obtained by one of the following rules:
1. s(t) = abs(w(t)), where abs(x) means taking the absolute value of a number x, i.e. |x|;
2. s(t) = (w(t) + h(t));
3. s(t) = (w(t) - h(t));
4. s(t) = (w(t) * h(t)), where * means multiplication, i.e. (w(t)·h(t));
5. s(t) = c;
6. s(t) = t;
Yesterday Ruslan thought on and on, but he could not cope with the task. Now he asks you to write a program that computes the appropriate f(t) and g(t) for any set of at most 50 circles.
In each of the functions f(t) and g(t) you are allowed to use no more than 50 multiplications. The length of any function should not exceed 100·n characters. The function should not contain spaces.
Ruslan can't keep big numbers in his memory, so you should choose f(t) and g(t), such that for all integer t from 0 to 50 value of f(t) and g(t) and all the intermediate calculations won't exceed 109 by their absolute value.
Input
The first line of the input contains number n (1 ≤ n ≤ 50) — the number of circles Ruslan thinks of. Next follow n lines, each of them containing three integers xi, yi and ri (0 ≤ xi, yi ≤ 50, 2 ≤ ri ≤ 50) — the coordinates of the center and the raduis of the i-th circle.
Output
In the first line print a correct function f(t). In the second line print a correct function g(t). The set of the points (xt = f(t), yt = g(t)) (0 ≤ t ≤ 50) must satisfy the condition, that there is at least one point inside or on the border of each of the circles, Ruslan thinks of at the beginning.
Examples
Input
3
0 10 4
10 0 4
20 10 4
Output
t
abs((t-10))
Note
Correct functions:
1. 10
2. (1+2)
3. ((t-3)+(t*4))
4. abs((t-10))
5. (abs((((23-t)*(t*t))+((45+12)*(t*t))))*((5*t)+((12*t)-13)))
6. abs((t-(abs((t*31))+14))))
Incorrect functions:
1. 3+5+7 (not enough brackets, it should be ((3+5)+7) or (3+(5+7)))
2. abs(t-3) (not enough brackets, it should be abs((t-3))
3. 2+(2-3 (one bracket too many)
4. 1(t+5) (no arithmetic operation between 1 and the bracket)
5. 5000*5000 (the number exceeds the maximum)
<image> The picture shows one of the possible solutions | instruction | 0 | 86,124 | 23 | 172,248 |
Tags: constructive algorithms, math
Correct Solution:
```
n = int(input())
x = [0] * n
y = [0] * n
for i in range(n):
x[i], y[i], r = map(int, input().split())
def sum(s1, s2):
return '(' + s1 + '+' + s2 + ')'
def minus(s1, s2):
return '(' + s1 + '-' + s2 + ')'
def mult(s1, s2):
return '(' + s1 + '*' + s2 + ')'
def sabs(s1):
return 'abs(' + s1 + ')'
def stand(x):
return sum(minus('1', sabs(minus('t', x))), sabs(minus(sabs(minus('t', x)), '1')))
def ans(v):
s=''
for i in range(1, n + 1):
if s == '':
s = mult(str(v[i - 1] // 2), stand(str(i)))
else:
s = sum(s, mult(str(v[i - 1] // 2), stand(str(i))))
print(s)
ans(x)
ans(y)
``` | output | 1 | 86,124 | 23 | 172,249 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Every day Ruslan tried to count sheep to fall asleep, but this didn't help. Now he has found a more interesting thing to do. First, he thinks of some set of circles on a plane, and then tries to choose a beautiful set of points, such that there is at least one point from the set inside or on the border of each of the imagined circles.
Yesterday Ruslan tried to solve this problem for the case when the set of points is considered beautiful if it is given as (xt = f(t), yt = g(t)), where argument t takes all integer values from 0 to 50. Moreover, f(t) and g(t) should be correct functions.
Assume that w(t) and h(t) are some correct functions, and c is an integer ranging from 0 to 50. The function s(t) is correct if it's obtained by one of the following rules:
1. s(t) = abs(w(t)), where abs(x) means taking the absolute value of a number x, i.e. |x|;
2. s(t) = (w(t) + h(t));
3. s(t) = (w(t) - h(t));
4. s(t) = (w(t) * h(t)), where * means multiplication, i.e. (w(t)·h(t));
5. s(t) = c;
6. s(t) = t;
Yesterday Ruslan thought on and on, but he could not cope with the task. Now he asks you to write a program that computes the appropriate f(t) and g(t) for any set of at most 50 circles.
In each of the functions f(t) and g(t) you are allowed to use no more than 50 multiplications. The length of any function should not exceed 100·n characters. The function should not contain spaces.
Ruslan can't keep big numbers in his memory, so you should choose f(t) and g(t), such that for all integer t from 0 to 50 value of f(t) and g(t) and all the intermediate calculations won't exceed 109 by their absolute value.
Input
The first line of the input contains number n (1 ≤ n ≤ 50) — the number of circles Ruslan thinks of. Next follow n lines, each of them containing three integers xi, yi and ri (0 ≤ xi, yi ≤ 50, 2 ≤ ri ≤ 50) — the coordinates of the center and the raduis of the i-th circle.
Output
In the first line print a correct function f(t). In the second line print a correct function g(t). The set of the points (xt = f(t), yt = g(t)) (0 ≤ t ≤ 50) must satisfy the condition, that there is at least one point inside or on the border of each of the circles, Ruslan thinks of at the beginning.
Examples
Input
3
0 10 4
10 0 4
20 10 4
Output
t
abs((t-10))
Note
Correct functions:
1. 10
2. (1+2)
3. ((t-3)+(t*4))
4. abs((t-10))
5. (abs((((23-t)*(t*t))+((45+12)*(t*t))))*((5*t)+((12*t)-13)))
6. abs((t-(abs((t*31))+14))))
Incorrect functions:
1. 3+5+7 (not enough brackets, it should be ((3+5)+7) or (3+(5+7)))
2. abs(t-3) (not enough brackets, it should be abs((t-3))
3. 2+(2-3 (one bracket too many)
4. 1(t+5) (no arithmetic operation between 1 and the bracket)
5. 5000*5000 (the number exceeds the maximum)
<image> The picture shows one of the possible solutions | instruction | 0 | 86,125 | 23 | 172,250 |
Tags: constructive algorithms, math
Correct Solution:
```
def canonise(t):
if t < 0:
return "(0-" + canonise(-t) + ")"
ans = ""
while t > 50:
ans += "(50+"
t -= 50
return ans + str(t) + ")" * (len(ans)//4)
n = int(input())
cxes = []
cyes = []
for i in range(n):
x, y, r = map(int, input().split())
for dx in range(2):
for dy in range(2):
if (x+dx)%2 == 0 and (y+dy)%2 == 0:
cxes.append((x+dx)//2)
cyes.append((y+dy)//2)
coeffx = [0]*(n+2)
coeffy = [0]*(n+2)
cfx = 0
cfy = 0
for i in range(n):
if i == 0:
cfx += cxes[i]
coeffx[i+1] -= cxes[i]
coeffx[i+2] += cxes[i]
cfy += cyes[i]
coeffy[i+1] -= cyes[i]
coeffy[i+2] += cyes[i]
elif i == n-1:
cfx += cxes[i]
coeffx[i] += cxes[i]
coeffx[i+1] -= cxes[i]
cfy += cyes[i]
coeffy[i] += cyes[i]
coeffy[i+1] -= cyes[i]
else:
coeffx[i] += cxes[i]
coeffx[i+1] -= 2*cxes[i]
coeffx[i+2] += cxes[i]
coeffy[i] += cyes[i]
coeffy[i+1] -= 2*cyes[i]
coeffy[i+2] += cyes[i]
rx = ""
ry = ""
for i in range(1, n+1):
s = f"abs((t-{i}))"
if i != n:
rx += f"(({s}*{canonise(coeffx[i])})+"
ry += f"(({s}*{canonise(coeffy[i])})+"
else:
rx += f"({s}*{canonise(coeffx[i])})" + ")"*(n-1)
ry += f"({s}*{canonise(coeffy[i])})" + ")"*(n-1)
print(f"({rx}+{canonise(cfx)})")
print(f"({ry}+{canonise(cfy)})")
``` | output | 1 | 86,125 | 23 | 172,251 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Every day Ruslan tried to count sheep to fall asleep, but this didn't help. Now he has found a more interesting thing to do. First, he thinks of some set of circles on a plane, and then tries to choose a beautiful set of points, such that there is at least one point from the set inside or on the border of each of the imagined circles.
Yesterday Ruslan tried to solve this problem for the case when the set of points is considered beautiful if it is given as (xt = f(t), yt = g(t)), where argument t takes all integer values from 0 to 50. Moreover, f(t) and g(t) should be correct functions.
Assume that w(t) and h(t) are some correct functions, and c is an integer ranging from 0 to 50. The function s(t) is correct if it's obtained by one of the following rules:
1. s(t) = abs(w(t)), where abs(x) means taking the absolute value of a number x, i.e. |x|;
2. s(t) = (w(t) + h(t));
3. s(t) = (w(t) - h(t));
4. s(t) = (w(t) * h(t)), where * means multiplication, i.e. (w(t)·h(t));
5. s(t) = c;
6. s(t) = t;
Yesterday Ruslan thought on and on, but he could not cope with the task. Now he asks you to write a program that computes the appropriate f(t) and g(t) for any set of at most 50 circles.
In each of the functions f(t) and g(t) you are allowed to use no more than 50 multiplications. The length of any function should not exceed 100·n characters. The function should not contain spaces.
Ruslan can't keep big numbers in his memory, so you should choose f(t) and g(t), such that for all integer t from 0 to 50 value of f(t) and g(t) and all the intermediate calculations won't exceed 109 by their absolute value.
Input
The first line of the input contains number n (1 ≤ n ≤ 50) — the number of circles Ruslan thinks of. Next follow n lines, each of them containing three integers xi, yi and ri (0 ≤ xi, yi ≤ 50, 2 ≤ ri ≤ 50) — the coordinates of the center and the raduis of the i-th circle.
Output
In the first line print a correct function f(t). In the second line print a correct function g(t). The set of the points (xt = f(t), yt = g(t)) (0 ≤ t ≤ 50) must satisfy the condition, that there is at least one point inside or on the border of each of the circles, Ruslan thinks of at the beginning.
Examples
Input
3
0 10 4
10 0 4
20 10 4
Output
t
abs((t-10))
Note
Correct functions:
1. 10
2. (1+2)
3. ((t-3)+(t*4))
4. abs((t-10))
5. (abs((((23-t)*(t*t))+((45+12)*(t*t))))*((5*t)+((12*t)-13)))
6. abs((t-(abs((t*31))+14))))
Incorrect functions:
1. 3+5+7 (not enough brackets, it should be ((3+5)+7) or (3+(5+7)))
2. abs(t-3) (not enough brackets, it should be abs((t-3))
3. 2+(2-3 (one bracket too many)
4. 1(t+5) (no arithmetic operation between 1 and the bracket)
5. 5000*5000 (the number exceeds the maximum)
<image> The picture shows one of the possible solutions | instruction | 0 | 86,126 | 23 | 172,252 |
Tags: constructive algorithms, math
Correct Solution:
```
def ex(values):
e = None
for i, v in enumerate(values):
e_ = f'({v//2}*((1-abs((t-{i})))+abs((1-abs((t-{i}))))))'
if e is None:
e = e_
else:
e = f'({e}+{e_})'
return e
def solve(circles):
xs = [c[0] for c in circles]
ys = [c[1] for c in circles]
return ex(xs), ex(ys)
def pc(line):
t = tuple(map(int, line.split()))
assert len(t) == 3, f"Invalid circle: {line}"
return t
def main():
n = int(input())
circles = [pc(input()) for _ in range(n)]
f, g = solve(circles)
print(f)
print(g)
if __name__ == '__main__':
main()
``` | output | 1 | 86,126 | 23 | 172,253 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every day Ruslan tried to count sheep to fall asleep, but this didn't help. Now he has found a more interesting thing to do. First, he thinks of some set of circles on a plane, and then tries to choose a beautiful set of points, such that there is at least one point from the set inside or on the border of each of the imagined circles.
Yesterday Ruslan tried to solve this problem for the case when the set of points is considered beautiful if it is given as (xt = f(t), yt = g(t)), where argument t takes all integer values from 0 to 50. Moreover, f(t) and g(t) should be correct functions.
Assume that w(t) and h(t) are some correct functions, and c is an integer ranging from 0 to 50. The function s(t) is correct if it's obtained by one of the following rules:
1. s(t) = abs(w(t)), where abs(x) means taking the absolute value of a number x, i.e. |x|;
2. s(t) = (w(t) + h(t));
3. s(t) = (w(t) - h(t));
4. s(t) = (w(t) * h(t)), where * means multiplication, i.e. (w(t)·h(t));
5. s(t) = c;
6. s(t) = t;
Yesterday Ruslan thought on and on, but he could not cope with the task. Now he asks you to write a program that computes the appropriate f(t) and g(t) for any set of at most 50 circles.
In each of the functions f(t) and g(t) you are allowed to use no more than 50 multiplications. The length of any function should not exceed 100·n characters. The function should not contain spaces.
Ruslan can't keep big numbers in his memory, so you should choose f(t) and g(t), such that for all integer t from 0 to 50 value of f(t) and g(t) and all the intermediate calculations won't exceed 109 by their absolute value.
Input
The first line of the input contains number n (1 ≤ n ≤ 50) — the number of circles Ruslan thinks of. Next follow n lines, each of them containing three integers xi, yi and ri (0 ≤ xi, yi ≤ 50, 2 ≤ ri ≤ 50) — the coordinates of the center and the raduis of the i-th circle.
Output
In the first line print a correct function f(t). In the second line print a correct function g(t). The set of the points (xt = f(t), yt = g(t)) (0 ≤ t ≤ 50) must satisfy the condition, that there is at least one point inside or on the border of each of the circles, Ruslan thinks of at the beginning.
Examples
Input
3
0 10 4
10 0 4
20 10 4
Output
t
abs((t-10))
Note
Correct functions:
1. 10
2. (1+2)
3. ((t-3)+(t*4))
4. abs((t-10))
5. (abs((((23-t)*(t*t))+((45+12)*(t*t))))*((5*t)+((12*t)-13)))
6. abs((t-(abs((t*31))+14))))
Incorrect functions:
1. 3+5+7 (not enough brackets, it should be ((3+5)+7) or (3+(5+7)))
2. abs(t-3) (not enough brackets, it should be abs((t-3))
3. 2+(2-3 (one bracket too many)
4. 1(t+5) (no arithmetic operation between 1 and the bracket)
5. 5000*5000 (the number exceeds the maximum)
<image> The picture shows one of the possible solutions
Submitted Solution:
```
def spec(i, targ):
return ("(((abs(((2 * t) - (2i - 1))) + ((2 * t) - (2i - 1))) * (abs(((2 * t) - (2i + 1))) - ((2 * t) - (2i + 1))))".replace("2i", str(2 * i)) + " * " + str((targ + 2) // 4) + ")").replace(" ", "")
def p(i):
x = eval(X.replace("t", str(i)))
y = eval(Y.replace("t", str(i)))
return (x, y)
n = int(input())
X = ["(0)"]
Y = ["(0)"]
for i in range(n):
x, y, r = map(int, input().split(" "))
X = ["("] + X + ["+"] + [spec(i, x)] + [")"]
Y = ["("] + Y + ["+"] + [spec(i, y)] + [")"]
X = "".join(X)
Y = "".join(Y)
print(X)
print(Y)
``` | instruction | 0 | 86,127 | 23 | 172,254 |
No | output | 1 | 86,127 | 23 | 172,255 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every day Ruslan tried to count sheep to fall asleep, but this didn't help. Now he has found a more interesting thing to do. First, he thinks of some set of circles on a plane, and then tries to choose a beautiful set of points, such that there is at least one point from the set inside or on the border of each of the imagined circles.
Yesterday Ruslan tried to solve this problem for the case when the set of points is considered beautiful if it is given as (xt = f(t), yt = g(t)), where argument t takes all integer values from 0 to 50. Moreover, f(t) and g(t) should be correct functions.
Assume that w(t) and h(t) are some correct functions, and c is an integer ranging from 0 to 50. The function s(t) is correct if it's obtained by one of the following rules:
1. s(t) = abs(w(t)), where abs(x) means taking the absolute value of a number x, i.e. |x|;
2. s(t) = (w(t) + h(t));
3. s(t) = (w(t) - h(t));
4. s(t) = (w(t) * h(t)), where * means multiplication, i.e. (w(t)·h(t));
5. s(t) = c;
6. s(t) = t;
Yesterday Ruslan thought on and on, but he could not cope with the task. Now he asks you to write a program that computes the appropriate f(t) and g(t) for any set of at most 50 circles.
In each of the functions f(t) and g(t) you are allowed to use no more than 50 multiplications. The length of any function should not exceed 100·n characters. The function should not contain spaces.
Ruslan can't keep big numbers in his memory, so you should choose f(t) and g(t), such that for all integer t from 0 to 50 value of f(t) and g(t) and all the intermediate calculations won't exceed 109 by their absolute value.
Input
The first line of the input contains number n (1 ≤ n ≤ 50) — the number of circles Ruslan thinks of. Next follow n lines, each of them containing three integers xi, yi and ri (0 ≤ xi, yi ≤ 50, 2 ≤ ri ≤ 50) — the coordinates of the center and the raduis of the i-th circle.
Output
In the first line print a correct function f(t). In the second line print a correct function g(t). The set of the points (xt = f(t), yt = g(t)) (0 ≤ t ≤ 50) must satisfy the condition, that there is at least one point inside or on the border of each of the circles, Ruslan thinks of at the beginning.
Examples
Input
3
0 10 4
10 0 4
20 10 4
Output
t
abs((t-10))
Note
Correct functions:
1. 10
2. (1+2)
3. ((t-3)+(t*4))
4. abs((t-10))
5. (abs((((23-t)*(t*t))+((45+12)*(t*t))))*((5*t)+((12*t)-13)))
6. abs((t-(abs((t*31))+14))))
Incorrect functions:
1. 3+5+7 (not enough brackets, it should be ((3+5)+7) or (3+(5+7)))
2. abs(t-3) (not enough brackets, it should be abs((t-3))
3. 2+(2-3 (one bracket too many)
4. 1(t+5) (no arithmetic operation between 1 and the bracket)
5. 5000*5000 (the number exceeds the maximum)
<image> The picture shows one of the possible solutions
Submitted Solution:
```
def f(x):
if x == n:
return "0"
if x == 0:
return "(" + str(X[0]) + "+" + f(1) + ")"
ss = "(abs(t-" + str(x-1) + ")-abs(t-" + str(x) + "))"
tmp = (X[x] - X[x - 1]) // 2
re = (X[x] - X[x - 1]) - 2 * tmp
X[x] -= re
ss = "((" + str(tmp) + "*" + ss + ")" + "+" + str(tmp) + ")"
return "(" + ss + "+" + f(x + 1) + ")"
n = int(input())
#c = [(int(_) for _ in input().split()) for i in range(n)]
c = [[int(x) for x in input().split()] for i in range(n)]
#print(n, c)
X = [c[i][0] for i in range(n)]
Y = [c[i][1] for i in range(n)]
#print(X)
#print(Y)
print(f(0))
#print(X)
X = Y
print(f(0))
``` | instruction | 0 | 86,128 | 23 | 172,256 |
No | output | 1 | 86,128 | 23 | 172,257 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every day Ruslan tried to count sheep to fall asleep, but this didn't help. Now he has found a more interesting thing to do. First, he thinks of some set of circles on a plane, and then tries to choose a beautiful set of points, such that there is at least one point from the set inside or on the border of each of the imagined circles.
Yesterday Ruslan tried to solve this problem for the case when the set of points is considered beautiful if it is given as (xt = f(t), yt = g(t)), where argument t takes all integer values from 0 to 50. Moreover, f(t) and g(t) should be correct functions.
Assume that w(t) and h(t) are some correct functions, and c is an integer ranging from 0 to 50. The function s(t) is correct if it's obtained by one of the following rules:
1. s(t) = abs(w(t)), where abs(x) means taking the absolute value of a number x, i.e. |x|;
2. s(t) = (w(t) + h(t));
3. s(t) = (w(t) - h(t));
4. s(t) = (w(t) * h(t)), where * means multiplication, i.e. (w(t)·h(t));
5. s(t) = c;
6. s(t) = t;
Yesterday Ruslan thought on and on, but he could not cope with the task. Now he asks you to write a program that computes the appropriate f(t) and g(t) for any set of at most 50 circles.
In each of the functions f(t) and g(t) you are allowed to use no more than 50 multiplications. The length of any function should not exceed 100·n characters. The function should not contain spaces.
Ruslan can't keep big numbers in his memory, so you should choose f(t) and g(t), such that for all integer t from 0 to 50 value of f(t) and g(t) and all the intermediate calculations won't exceed 109 by their absolute value.
Input
The first line of the input contains number n (1 ≤ n ≤ 50) — the number of circles Ruslan thinks of. Next follow n lines, each of them containing three integers xi, yi and ri (0 ≤ xi, yi ≤ 50, 2 ≤ ri ≤ 50) — the coordinates of the center and the raduis of the i-th circle.
Output
In the first line print a correct function f(t). In the second line print a correct function g(t). The set of the points (xt = f(t), yt = g(t)) (0 ≤ t ≤ 50) must satisfy the condition, that there is at least one point inside or on the border of each of the circles, Ruslan thinks of at the beginning.
Examples
Input
3
0 10 4
10 0 4
20 10 4
Output
t
abs((t-10))
Note
Correct functions:
1. 10
2. (1+2)
3. ((t-3)+(t*4))
4. abs((t-10))
5. (abs((((23-t)*(t*t))+((45+12)*(t*t))))*((5*t)+((12*t)-13)))
6. abs((t-(abs((t*31))+14))))
Incorrect functions:
1. 3+5+7 (not enough brackets, it should be ((3+5)+7) or (3+(5+7)))
2. abs(t-3) (not enough brackets, it should be abs((t-3))
3. 2+(2-3 (one bracket too many)
4. 1(t+5) (no arithmetic operation between 1 and the bracket)
5. 5000*5000 (the number exceeds the maximum)
<image> The picture shows one of the possible solutions
Submitted Solution:
```
def main():
n = int(input())
x = [0] * n
y = [0] * n
for i in range(n):
x[i], y[i], _ = [int(i) for i in input().split()]
resx = []
resy = []
for i in range(n):
resx.append("({0}*((1-abs(t-{1}))+abs(1-abs(t-{1}))))".format(x[i] // 2, i))
resy.append("({0}*((1-abs(t-{1}))+abs(1-abs(t-{1}))))".format(y[i] // 2, i))
if i > 0:
resx[-1] += ')'
resy[-1] += ')'
print('(' * (n - 1) + '+'.join(resx))
print('(' * (n - 1) + '+'.join(resy))
main()
``` | instruction | 0 | 86,129 | 23 | 172,258 |
No | output | 1 | 86,129 | 23 | 172,259 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every day Ruslan tried to count sheep to fall asleep, but this didn't help. Now he has found a more interesting thing to do. First, he thinks of some set of circles on a plane, and then tries to choose a beautiful set of points, such that there is at least one point from the set inside or on the border of each of the imagined circles.
Yesterday Ruslan tried to solve this problem for the case when the set of points is considered beautiful if it is given as (xt = f(t), yt = g(t)), where argument t takes all integer values from 0 to 50. Moreover, f(t) and g(t) should be correct functions.
Assume that w(t) and h(t) are some correct functions, and c is an integer ranging from 0 to 50. The function s(t) is correct if it's obtained by one of the following rules:
1. s(t) = abs(w(t)), where abs(x) means taking the absolute value of a number x, i.e. |x|;
2. s(t) = (w(t) + h(t));
3. s(t) = (w(t) - h(t));
4. s(t) = (w(t) * h(t)), where * means multiplication, i.e. (w(t)·h(t));
5. s(t) = c;
6. s(t) = t;
Yesterday Ruslan thought on and on, but he could not cope with the task. Now he asks you to write a program that computes the appropriate f(t) and g(t) for any set of at most 50 circles.
In each of the functions f(t) and g(t) you are allowed to use no more than 50 multiplications. The length of any function should not exceed 100·n characters. The function should not contain spaces.
Ruslan can't keep big numbers in his memory, so you should choose f(t) and g(t), such that for all integer t from 0 to 50 value of f(t) and g(t) and all the intermediate calculations won't exceed 109 by their absolute value.
Input
The first line of the input contains number n (1 ≤ n ≤ 50) — the number of circles Ruslan thinks of. Next follow n lines, each of them containing three integers xi, yi and ri (0 ≤ xi, yi ≤ 50, 2 ≤ ri ≤ 50) — the coordinates of the center and the raduis of the i-th circle.
Output
In the first line print a correct function f(t). In the second line print a correct function g(t). The set of the points (xt = f(t), yt = g(t)) (0 ≤ t ≤ 50) must satisfy the condition, that there is at least one point inside or on the border of each of the circles, Ruslan thinks of at the beginning.
Examples
Input
3
0 10 4
10 0 4
20 10 4
Output
t
abs((t-10))
Note
Correct functions:
1. 10
2. (1+2)
3. ((t-3)+(t*4))
4. abs((t-10))
5. (abs((((23-t)*(t*t))+((45+12)*(t*t))))*((5*t)+((12*t)-13)))
6. abs((t-(abs((t*31))+14))))
Incorrect functions:
1. 3+5+7 (not enough brackets, it should be ((3+5)+7) or (3+(5+7)))
2. abs(t-3) (not enough brackets, it should be abs((t-3))
3. 2+(2-3 (one bracket too many)
4. 1(t+5) (no arithmetic operation between 1 and the bracket)
5. 5000*5000 (the number exceeds the maximum)
<image> The picture shows one of the possible solutions
Submitted Solution:
```
def main():
n = int(input())
x = [0] * n
y = [0] * n
for i in range(n):
x[i], y[i], _ = [int(i) for i in input().split()]
resx = []
resy = []
for i in range(n):
resx.append("{0}*((1-abs(t-{1}))+abs(1-abs(t-{1})))".format(x[i] // 2, i))
resy.append("{0}*((1-abs(t-{1}))+abs(1-abs(t-{1})))".format(y[i] // 2, i))
print('+'.join(resx))
print('+'.join(resy))
main()
``` | instruction | 0 | 86,130 | 23 | 172,260 |
No | output | 1 | 86,130 | 23 | 172,261 |
Provide a correct Python 3 solution for this coding contest problem.
4 different points on the plane Read the coordinates of $ A (x_a, y_a) $, $ B (x_b, y_b) $, $ C (x_c, y_c) $, $ D (x_d, y_d) $ and read those 4 points Create a program that outputs YES if there is no dent in the quadrangle $ ABCD $ with the coordinates as the vertices, and NO if there is a dent.
A quadrangle with a dent is a quadrangle as shown in Figure 1.
<image>
Input
Given multiple datasets. The format of each dataset is as follows.
$ x_a $, $ y_a $, $ x_b $, $ y_b $, $ x_c $, $ y_c $, $ x_d $, $ y_d $
$ x_a $, $ y_a $, $ x_b $, $ y_b $, $ x_c $, $ y_c $, $ x_d $, $ y_d $ are -100 or more and 100 or less, respectively, and are given as real numbers.
1 No more than two points can be lined up on a straight line. Also, if you connect the points in the order of input, the coordinates of the points will be input in the order of forming a quadrangle. (That is, the points are not given in the order shown in Figure 2.)
The number of datasets does not exceed 100.
Output
Print YES or NO on one line for each dataset.
Example
Input
0.0,0.0,1.0,0.0,1.0,1.0,0.0,1.0
0.0,0.0,3.0,0.0,1.0,1.0,1.0,3.0
Output
YES
NO | instruction | 0 | 86,449 | 23 | 172,898 |
"Correct Solution:
```
import sys
for e in sys.stdin:
e=list(map(float,e.split(',')))
print(['YES','NO'][sum((e[i]-e[(2+i)%8])*(e[(5+i)%8]-e[(3+i)%8])-(e[1+i]-e[(3+i)%8])*(e[(4+i)%8]-e[(2+i)%8])>0 for i in range(0,8,2))%4>0])
``` | output | 1 | 86,449 | 23 | 172,899 |
Provide a correct Python 3 solution for this coding contest problem.
4 different points on the plane Read the coordinates of $ A (x_a, y_a) $, $ B (x_b, y_b) $, $ C (x_c, y_c) $, $ D (x_d, y_d) $ and read those 4 points Create a program that outputs YES if there is no dent in the quadrangle $ ABCD $ with the coordinates as the vertices, and NO if there is a dent.
A quadrangle with a dent is a quadrangle as shown in Figure 1.
<image>
Input
Given multiple datasets. The format of each dataset is as follows.
$ x_a $, $ y_a $, $ x_b $, $ y_b $, $ x_c $, $ y_c $, $ x_d $, $ y_d $
$ x_a $, $ y_a $, $ x_b $, $ y_b $, $ x_c $, $ y_c $, $ x_d $, $ y_d $ are -100 or more and 100 or less, respectively, and are given as real numbers.
1 No more than two points can be lined up on a straight line. Also, if you connect the points in the order of input, the coordinates of the points will be input in the order of forming a quadrangle. (That is, the points are not given in the order shown in Figure 2.)
The number of datasets does not exceed 100.
Output
Print YES or NO on one line for each dataset.
Example
Input
0.0,0.0,1.0,0.0,1.0,1.0,0.0,1.0
0.0,0.0,3.0,0.0,1.0,1.0,1.0,3.0
Output
YES
NO | instruction | 0 | 86,450 | 23 | 172,900 |
"Correct Solution:
```
import sys
readlines = sys.stdin.readlines
write = sys.stdout.write
def cross3(a, b, c):
return (b[0]-a[0])*(c[1]-a[1]) - (b[1]-a[1])*(c[0]-a[0])
def solve():
for line in readlines():
xa, ya, xb, yb, xc, yc, xd, yd = map(float, line.split(","))
P = [(xa, ya), (xb, yb), (xc, yc), (xd, yd)]
D = []
for i in range(4):
p0 = P[i-2]; p1 = P[i-1]; p2 = P[i]
D.append(cross3(p0, p1, p2))
if all(e > 0 for e in D) or all(e < 0 for e in D):
write("YES\n")
else:
write("NO\n")
solve()
``` | output | 1 | 86,450 | 23 | 172,901 |
Provide a correct Python 3 solution for this coding contest problem.
4 different points on the plane Read the coordinates of $ A (x_a, y_a) $, $ B (x_b, y_b) $, $ C (x_c, y_c) $, $ D (x_d, y_d) $ and read those 4 points Create a program that outputs YES if there is no dent in the quadrangle $ ABCD $ with the coordinates as the vertices, and NO if there is a dent.
A quadrangle with a dent is a quadrangle as shown in Figure 1.
<image>
Input
Given multiple datasets. The format of each dataset is as follows.
$ x_a $, $ y_a $, $ x_b $, $ y_b $, $ x_c $, $ y_c $, $ x_d $, $ y_d $
$ x_a $, $ y_a $, $ x_b $, $ y_b $, $ x_c $, $ y_c $, $ x_d $, $ y_d $ are -100 or more and 100 or less, respectively, and are given as real numbers.
1 No more than two points can be lined up on a straight line. Also, if you connect the points in the order of input, the coordinates of the points will be input in the order of forming a quadrangle. (That is, the points are not given in the order shown in Figure 2.)
The number of datasets does not exceed 100.
Output
Print YES or NO on one line for each dataset.
Example
Input
0.0,0.0,1.0,0.0,1.0,1.0,0.0,1.0
0.0,0.0,3.0,0.0,1.0,1.0,1.0,3.0
Output
YES
NO | instruction | 0 | 86,451 | 23 | 172,902 |
"Correct Solution:
```
import sys
def solve():
for line in sys.stdin:
xa,ya,xb,yb,xc,yc,xd,yd = map(float, line.split(','))
def fac(x, y):
if xa == xc:
return x - xa
else:
return ((ya-yc)/(xa-xc))*(x-xa) - y + ya
def fbd(x, y):
if xb == xd:
return x - xb
else:
return ((yb-yd)/(xb-xd))*(x-xb) - y + yb
if fac(xb, yb) * fac(xd, yd) > 0:
print('NO')
elif fbd(xa, ya) * fbd(xc, yc) > 0:
print('NO')
else:
print('YES')
if __name__ == "__main__":
solve()
``` | output | 1 | 86,451 | 23 | 172,903 |
Provide a correct Python 3 solution for this coding contest problem.
4 different points on the plane Read the coordinates of $ A (x_a, y_a) $, $ B (x_b, y_b) $, $ C (x_c, y_c) $, $ D (x_d, y_d) $ and read those 4 points Create a program that outputs YES if there is no dent in the quadrangle $ ABCD $ with the coordinates as the vertices, and NO if there is a dent.
A quadrangle with a dent is a quadrangle as shown in Figure 1.
<image>
Input
Given multiple datasets. The format of each dataset is as follows.
$ x_a $, $ y_a $, $ x_b $, $ y_b $, $ x_c $, $ y_c $, $ x_d $, $ y_d $
$ x_a $, $ y_a $, $ x_b $, $ y_b $, $ x_c $, $ y_c $, $ x_d $, $ y_d $ are -100 or more and 100 or less, respectively, and are given as real numbers.
1 No more than two points can be lined up on a straight line. Also, if you connect the points in the order of input, the coordinates of the points will be input in the order of forming a quadrangle. (That is, the points are not given in the order shown in Figure 2.)
The number of datasets does not exceed 100.
Output
Print YES or NO on one line for each dataset.
Example
Input
0.0,0.0,1.0,0.0,1.0,1.0,0.0,1.0
0.0,0.0,3.0,0.0,1.0,1.0,1.0,3.0
Output
YES
NO | instruction | 0 | 86,452 | 23 | 172,904 |
"Correct Solution:
```
def judge(p1,p2,p3,p4):
t1 = (p3[0] - p4[0]) * (p1[1] - p3[1]) + (p3[1] - p4[1]) * (p3[0] - p1[0])
t2 = (p3[0] - p4[0]) * (p2[1] - p3[1]) + (p3[1] - p4[1]) * (p3[0] - p2[0])
t3 = (p1[0] - p2[0]) * (p3[1] - p1[1]) + (p1[1] - p2[1]) * (p1[0] - p3[0])
t4 = (p1[0] - p2[0]) * (p4[1] - p1[1]) + (p1[1] - p2[1]) * (p1[0] - p4[0])
return t1*t2>0 and t3*t4>0
while True:
try:
x1,y1,x2,y2,x3,y3,x4,y4=map(float, input().split(","))
A=[x1,y1]
B=[x2,y2]
C=[x3,y3]
D=[x4,y4]
if judge(A,B,C,D)==False :
print("NO")
else:
print("YES")
except EOFError:
break
``` | output | 1 | 86,452 | 23 | 172,905 |
Provide a correct Python 3 solution for this coding contest problem.
4 different points on the plane Read the coordinates of $ A (x_a, y_a) $, $ B (x_b, y_b) $, $ C (x_c, y_c) $, $ D (x_d, y_d) $ and read those 4 points Create a program that outputs YES if there is no dent in the quadrangle $ ABCD $ with the coordinates as the vertices, and NO if there is a dent.
A quadrangle with a dent is a quadrangle as shown in Figure 1.
<image>
Input
Given multiple datasets. The format of each dataset is as follows.
$ x_a $, $ y_a $, $ x_b $, $ y_b $, $ x_c $, $ y_c $, $ x_d $, $ y_d $
$ x_a $, $ y_a $, $ x_b $, $ y_b $, $ x_c $, $ y_c $, $ x_d $, $ y_d $ are -100 or more and 100 or less, respectively, and are given as real numbers.
1 No more than two points can be lined up on a straight line. Also, if you connect the points in the order of input, the coordinates of the points will be input in the order of forming a quadrangle. (That is, the points are not given in the order shown in Figure 2.)
The number of datasets does not exceed 100.
Output
Print YES or NO on one line for each dataset.
Example
Input
0.0,0.0,1.0,0.0,1.0,1.0,0.0,1.0
0.0,0.0,3.0,0.0,1.0,1.0,1.0,3.0
Output
YES
NO | instruction | 0 | 86,453 | 23 | 172,906 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
import sys
import os
import math
def cross(v1, v2):
return v1[0] * v2[1] - v1[1] * v2[0]
def sign(v):
if v >= 0:
return 1
else:
return -1
for s in sys.stdin:
ax, ay, bx, by, cx, cy, dx, dy = map(float, s.split(','))
AB = (bx - ax, by - ay)
BC = (cx - bx, cy - by)
CD = (dx - cx, dy - cy)
DA = (ax - dx, ay - dy)
c0 = cross(AB, BC)
c1 = cross(BC, CD)
c2 = cross(CD, DA)
c3 = cross(DA, AB)
if sign(c0) == sign(c1) == sign(c2) == sign(c3):
print('YES')
else:
print('NO')
``` | output | 1 | 86,453 | 23 | 172,907 |
Provide a correct Python 3 solution for this coding contest problem.
4 different points on the plane Read the coordinates of $ A (x_a, y_a) $, $ B (x_b, y_b) $, $ C (x_c, y_c) $, $ D (x_d, y_d) $ and read those 4 points Create a program that outputs YES if there is no dent in the quadrangle $ ABCD $ with the coordinates as the vertices, and NO if there is a dent.
A quadrangle with a dent is a quadrangle as shown in Figure 1.
<image>
Input
Given multiple datasets. The format of each dataset is as follows.
$ x_a $, $ y_a $, $ x_b $, $ y_b $, $ x_c $, $ y_c $, $ x_d $, $ y_d $
$ x_a $, $ y_a $, $ x_b $, $ y_b $, $ x_c $, $ y_c $, $ x_d $, $ y_d $ are -100 or more and 100 or less, respectively, and are given as real numbers.
1 No more than two points can be lined up on a straight line. Also, if you connect the points in the order of input, the coordinates of the points will be input in the order of forming a quadrangle. (That is, the points are not given in the order shown in Figure 2.)
The number of datasets does not exceed 100.
Output
Print YES or NO on one line for each dataset.
Example
Input
0.0,0.0,1.0,0.0,1.0,1.0,0.0,1.0
0.0,0.0,3.0,0.0,1.0,1.0,1.0,3.0
Output
YES
NO | instruction | 0 | 86,454 | 23 | 172,908 |
"Correct Solution:
```
import math
def eq(x, y):
return abs(x - y) <= 10e-10
def get_r(_a, _b, _c):
return math.acos((-_a ** 2 + _b ** 2 + _c ** 2) / (2 * _b * _c))
def _len(_ax, _ay, _bx, _by):
return math.sqrt((_ax - _bx) ** 2 + (_ay - _by) ** 2)
try:
while 1:
xa,ya,xb,yb,xc,yc,xd,yd = map(float, input().split(','))
ab = _len(xa,ya,xb,yb)
bc = _len(xc,yc,xb,yb)
cd = _len(xc,yc,xd,yd)
ad = _len(xa,ya,xd,yd)
bd = _len(xd,yd,xb,yb)
ac = _len(xa,ya,xc,yc)
A = get_r(bd, ab, ad)
B = get_r(ac, ab, bc)
C = get_r(bd, bc, cd)
D = get_r(ac, ad, cd)
if eq(A + B + C + D, 2 * math.pi):
print("YES")
else:
print("NO")
except:
pass
``` | output | 1 | 86,454 | 23 | 172,909 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.