Problem p03006 (python)

Fixed submission:
from collections import Counter


def main():
    N = int(input())
    points = [tuple(map(int, input().split())) for _ in range(N)]
    counter = Counter()

    if N == 1:
        print(1)
        return

    for i in range(N):
        for j in range(N):
            if j == i:
                continue
            a = points[i]
            b = points[j]
            dx = b[0] - a[0]
            dy = b[1] - a[1]
            counter[(dx, dy)] += 1

    vec, _ = counter.most_common()[0]
    dx, dy = vec
    visited = set()
    pointset = set(points)
    cost = 0

    for p in points:
        if p in visited:
            continue
        visited.add(p)
        x, y = p
        cost += 1

        i = 1
        while True:
            n = (x + i * dx, y + i * dy)
            i += 1
            if n in pointset:
                visited.add(n)
            else:
                break
        i = 1
        while True:
            n = (x - i * dx, y - i * dy)
            i += 1
            if n in pointset:
                visited.add(n)
            else:
                break

    print(cost)


main()

Buggy submission (verdict: Runtime Error):
from collections import Counter

N = int(input())
points = [tuple(map(int, input().split())) for _ in range(N)]
counter = Counter()

for i, a in enumerate(points):
    for j in range(N):
        if j == i:
            continue
        b = points[j]
        dx = b[0] - a[0]
        dy = b[1] - a[1]
        counter[(dx, dy)] += 1

vec, _ = counter.most_common()[0]
dx, dy = vec
visited = set()
cost = 0

for p in points:
    if p in visited:
        continue
    x, y = p
    visited.add(p)
    if (x + dx, y + dy) in visited or (x - dx, y - dy) in visited:
        continue
    cost += 1

print(cost)

Buggy submission (verdict: Runtime Error):
from collections import Counter



N = int(input())
points = [tuple(map(int, input().split())) for _ in range(N)]
vecs = {}
counter = Counter()

for i, a in enumerate(points):
    for j in range(N):
        if j == i:
            continue
        b = points[j]
        dx = b[0] - a[0]
        dy = b[1] - a[1]
        vecs[(i, j)] = (dx, dy)
        counter[(dx, dy)] += 1

vec, _ = counter.most_common()[0]
dx, dy = vec
visited = set()
cost = 0

for i, p in enumerate(points):
    if i in visited:
        continue
    x, y = p
    if (x + dx, y + dy) in visited or (x - dx, y - dy) in visited:
        continue
    cost += 1
    visited.add(p)

print(cost)

Buggy submission (verdict: Runtime Error):
from collections import Counter


def mathgcd(x, y):
    if y == 0:
        return x
    else:
        return mathgcd(y, x % y)


N = int(input())
points = [tuple(map(int, input().split())) for _ in range(N)]
vecs = {}
counter = Counter()

for i, p in enumerate(points):
    for j in range(i, N):
        if j == i:
            continue
        q = points[j]
        a = min(p, q)
        b = max(p, q)
        dx = b[0] - a[0]
        dy = b[1] - a[1]
        gcd = mathgcd(dx, dy)
        if gcd != 0:
            dx //= gcd
            dy //= gcd
        vecs[(i, j)] = (dx, dy)
        counter[(dx, dy)] += 1

vec, _ = counter.most_common()[0]
dx, dy = vec
visited = set()
cost = 0

for i, p in enumerate(points):
    if i in visited:
        continue
    cost += 1
    for j in range(i, N):
        if j == i:
            continue
        if vecs[(i, j)] == (dx, dy):
            visited.add(j)

print(cost)

Buggy submission (verdict: Runtime Error):
import math
from collections import Counter


N = int(input())
points = [tuple(map(int, input().split())) for _ in range(N)]
vecs = {}
counter = Counter()

for i, p in enumerate(points):
    for j in range(i, N):
        if j == i:
            continue
        q = points[j]
        a = min(p, q)
        b = max(p, q)
        dx = b[0] - a[0]
        dy = b[1] - a[1]
        gcd = math.gcd(dx, dy)
        if gcd != 0:
            dx //= gcd
            dy //= gcd
        vecs[(i, j)] = (dx, dy)
        counter[(dx, dy)] += 1

vec, _ = counter.most_common()[0]
dx, dy = vec
visited = set()
cost = 0

for i, p in enumerate(points):
    if i in visited:
        continue
    cost += 1
    for j in range(i, N):
        if j == i:
            continue
        if vecs[(i, j)] == (dx, dy):
            visited.add(j)

print(cost)