Problem p02574 (python)

Fixed submission:
#!/usr/bin/env python3
import sys
import numpy as np

input = sys.stdin.readline


def ST():
    return input().rstrip()


def I():
    return int(input())


def MI():
    return map(int, input().split())


def LI():
    return list(MI())


N = I()
A = LI()


def is_pair_coprime(A):
    n = max(A)
    D = [0] * (n + 1)
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n ** 0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
            if not D[j]:
                D[j] = i

    used = [False] * (n + 1)
    for a in A:
        temp = a

        while D[temp]:
            mod = D[temp]
            if used[mod]:
                return False
            else:
                used[mod] = True
            while temp % mod == 0:
                temp //= mod

        if temp != 1:
            if used[temp]:
                return False
            else:
                used[temp] = True

    return True


# def pre_fac(n):
#     D = [0] * (n + 1)
#     is_prime = [True] * (n + 1)
#     is_prime[0] = False
#     is_prime[1] = False
#     for i in range(2, int(n ** 0.5) + 1):
#         if not is_prime[i]:
#             continue
#         for j in range(i * 2, n + 1, i):
#             is_prime[j] = False
#             if not D[j]:
#                 D[j] = i
#     return D


# def fac(prime, n):
#     if n == 1:
#         return [[1, 1]]
#     arr, temp = [], n
#
#     while prime[temp]:
#         mod = prime[temp]
#         cnt = 0
#         while temp % mod == 0:
#             cnt += 1
#             temp //= mod
#         arr.append([mod, cnt])
#
#     if temp != 1:
#         arr.append([temp, 1])
#
#     return arr

if is_pair_coprime(A):
    print("pairwise coprime")
elif np.gcd.reduce(A) == 1:
    print("setwise coprime")
else:
    print("not coprime")

# prime = pre_fac(10 ** 6)
#
# d = defaultdict(int)
# for a in A:
#     tmp = fac(prime, a)
#     for t in tmp:
#         d[t[0]] += 1
#
# flag = True
# for k, v in d.items():
#     if flag and v != 1:
#         if k != 1:
#             flag = False
#     if v == N:
#         if k == 1:
#             print("pairwise coprime")
#         else:
#             print("not coprime")
#             exit()
#
#
# if flag:
#     print("pairwise coprime")
# elif 1 in A:
#     print("not coprime")
# else:
#     print("setwise coprime")

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys
from collections import defaultdict

input = sys.stdin.readline


def ST():
    return input().rstrip()


def I():
    return int(input())


def MI():
    return map(int, input().split())


def LI():
    return list(MI())


N = I()
A = LI()


def pre_fac(n):
    D = [0] * (n + 1)
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n ** 0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
            if not D[j]:
                D[j] = i
    return D


def fac(prime, n):

    if n == 1:
        return [[1, 1]]
    arr, temp = [], n

    while prime[temp]:
        mod = prime[temp]
        cnt = 0
        while temp % mod == 0:
            cnt += 1
            temp //= mod
        arr.append([mod, cnt])

    if temp != 1:
        arr.append([temp, 1])

    return arr


prime = pre_fac(N)

d = defaultdict(int)
for i, a in enumerate(A):
    tmp = fac(prime, a)
    for t in tmp:
        d[t[0]] += 1

flag = True
for v in d.values():
    if flag and v != 1:
        flag = False
    if v == N:
        print("not coprime")
        exit()


if flag:
    print("pairwise coprime")
elif 1 in A:
    print("not coprime")
else:
    print("setwise coprime")