Problem p02574 (python)

Fixed submission:
INF=10**9+7
D = [INF]*(10**6+1)
L = [True]*(10**6+1)
L[0] = False
L[1] = False
D[1] = 1
for i in range(2, 10**6+1):
    if L[i]:
        D[i] = i
        for j in range(i*2, 10**6+1, i):
            L[j] = False
            D[j] = min(i, D[j])
# print(D[:101])


def egcd(a, b):
    if a == 0:
        return b
    else:
        g = egcd(b % a, a)
        return g


def factorization(n):
    ans = set()
    if n == 1:
        ans.add(1)
        return ans
    while n>1:
        ans.add(D[n])
        n//=D[n]
    return ans


N = int(input())
A = list(map(int, input().split()))

s = set(factorization(A[0])-{1})
for i in range(1, N):
    tmp = factorization(A[i])-{1}
    if tmp & s != set():
        break
    s |= tmp
else:
    print('pairwise coprime')
    exit()

L1 = A
while len(L1) > 1:
    l = len(L1)//2
    k = [0]*l
    for i in range(l):
        k[i] = egcd(L1[2*i], L1[2*i+1])
    if len(L1) % 2 == 1:
        k.append(L1[-1])
    L1 = k

if L1[0] == 1:
    print('setwise coprime')
else:
    print('not coprime')

Buggy submission (verdict: Runtime Error):
D = [10**9+7]*(10**6+1)
L = [True]*(10**6+1)
L[0] = False
L[1] = False
D[1] = 1
for i in range(2, 10**6+1):
    if L[i]:
        D[i] = i
        for j in range(i*2, 10**6+1, i):
            L[j] = False
            D[j] = min(i, D[j])


def egcd(a, b):
    if a == 0:
        return b
    else:
        g = egcd(b % a, a)
        return g


def factorization(n):
    if n == 1:
        return {1}
    ans = set()
    ans.add(D[n])
    factorization(n//D[n])
    return ans


N = int(input())
A = list(map(int, input().split()))

s = set(factorization(A[0])-{1})
for i in range(1, N):
    tmp = factorization(A[i])-{1}
    if tmp & s != set():
        break
    s |= tmp
    print(s)
else:
    print('pairwise coprime')
    exit()

L1 = A
while len(L1) > 1:
    l = len(L1)//2
    k = [0]*l
    for i in range(l):
        k[i] = egcd(L1[2*i], L1[2*i+1])
    if len(L1) % 2 == 1:
        k.append(L1[-1])
    L1 = k

if L1[0] == 1:
    print('setwise coprime')
else:
    print('not coprime')

Buggy submission (verdict: Runtime Error):
D = [10**9+7]*(10**6+1)
L = [True]*(10**6+1)
L[0] = False
L[1] = False
D[1] = 1
for i in range(2, 10**6+1):
    if L[i]:
        D[i] = i
        for j in range(i*2, 10**6+1, i):
            L[j] = False
            D[j] = min(i, D[j])


def egcd(a, b):
    if a == 0:
        return b
    else:
        g = egcd(b % a, a)
        return g


def factorization(n):
    if n == 1:
        return {1}
    ans = set()
    ans.add(D[n])
    factorization(n//D[n])
    return ans


N = int(input())
A = list(map(int, input().split()))

s = set(factorization(A[0])-{1})
for i in range(1, N):
    tmp = factorization(A[i])-{1}
    if tmp & s != set():
        break
    s |= tmp
    print(s)
else:
    print('pairwise coprime')
    exit()

L1 = A
while len(L1) > 1:
    l = len(L1)//2
    k = [0]*l
    for i in range(l):
        k[i] = egcd(L1[2*i], L1[2*i+1])
    if len(L1) % 2 == 1:
        k.append(L1[-1])
    L1 = k
    print(k)

if L1[0] == 1:
    print('setwise coprime')
else:
    print('not coprime')

Buggy submission (verdict: Runtime Error):
# ABC_177_E_3.py

# 解説を見て高速素因数分解を実装する

D = [0]*(10**6+1)
L = [True]*(10**6+1)
L[0] = False
L[1] = False
D[1] = 1
for i in range(2, 10**6+1):
    if L[i]:
        D[i] = i
        for j in range(i*2, 10**6+1, i):
            L[j] = False
            D[j] = i


def egcd(a, b):
    if a == 0:
        return b
    else:
        g = egcd(b % a, a)
        return g


def factorization(n):
    if n == 1:
        return {1}
    ans = set()
    ans.add(D[n])
    factorization(n//D[n])
    return ans


N = int(input())
A = list(map(int, input().split()))

s = set()
for i in range(N):
    tmp = factorization(A[i])-{1}
    if tmp & s != set():
        break
    s.add(tmp)
else:
    print('pairwise coprime')
    exit()

L1 = A
while len(L1) > 1:
    l = len(L1)//2
    tmp = [0]*l
    for i in range(l):
        tmp[i] = egcd(L1[2*i], L1[2*i+1])
    if len(L1) % 2 == 1:
        tmp.append(L1[-1])
    L1 = tmp

if L1[0] == 1:
    print('setwise coprime')
else:
    print('not coprime')