Problem:


Score : 500 points

Problem Statement

We have N integers. The i-th number is A_i.

\{A_i\} is said to be pairwise coprime when GCD(A_i,A_j)=1 holds for every pair (i, j) such that 1\leq i < j \leq N.

\{A_i\} is said to be setwise coprime when \{A_i\} is not pairwise coprime but GCD(A_1,\ldots,A_N)=1.

Determine if \{A_i\} is pairwise coprime, setwise coprime, or neither.

Here, GCD(\ldots) denotes greatest common divisor.

Constraints

2 \leq N \leq 10^6

1 \leq A_i\leq 10^6

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

If \{A_i\} is pairwise coprime, print pairwise coprime; if \{A_i\} is setwise coprime, print setwise coprime; if neither, print not coprime.

Sample Input 1

3
3 4 5

Sample Output 1

pairwise coprime

GCD(3,4)=GCD(3,5)=GCD(4,5)=1, so they are pairwise coprime.

Sample Input 2

3
6 10 15

Sample Output 2

setwise coprime

Since GCD(6,10)=2, they are not pairwise coprime. However, since GCD(6,10,15)=1, they are setwise coprime.

Sample Input 3

3
6 10 16

Sample Output 3

not coprime

GCD(6,10,16)=2, so they are neither pairwise coprime nor setwise coprime.



Sample input:
3
3 4 5


Sample output:
pairwise coprime

Submission status: Accepted

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = float('inf')
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()
    DD = ((1,0),(0,1),(-1,0),(0,-1))

    N = NI()
    A = LI()

    # 1000までの素数列
    t = [True] * 1000
    for i in range(2,33):
        j = 2
        while i * j < 1000:
            t[i*j] = False
            j += 1
    p = []
    for i in range(2,1000):
        if t[i]: p.append(i)

    cnt = [0] * 1000000
    for x in A:
        nf = True
        for d in p:
            if d > x: break
            if x % d == 0:
                while x % d == 0:
                    x //= d
                if cnt[d]:
                    nf = False
                    break
                cnt[d] += 1
        else:
            if x > 1:
                if cnt[x]:
                    nf = False
                    break
                cnt[x] += 1
            continue
        if nf: continue
        break
    else:
        print('pairwise coprime')
        return

    g = A[0]
    for x in A[1:]:
        g = math.gcd(g,x)
        if g == 1:
            print('setwise coprime')
            return
    print('not coprime')

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math
N = int(input())
A = list(map(int, input().split()))

NUM = 10**6

gcd = A[0]
for i in range(1, N):
    gcd = math.gcd(gcd, A[i])
if gcd != 1:
    print("not coprime")
    sys.exit()


def osa_k(num):
    S = list(i for i in range(num+1))
    for i in range(2, int(num**0.5)+1):
        if S[i] < i:
            continue
        j = i**2
        while j <= num:
            if S[j] == j:
                S[j] = i
            j += i
    return S


prime = osa_k(NUM)
S = [False]*(NUM+1)
for num in A:
    tmp = prime[num]
    while num > 1:
        num //= tmp
        if tmp == prime[num]:
            # S[tmp] = True
            continue
        if S[tmp]:
            print("setwise coprime")
            sys.exit()
        S[tmp] = True
        tmp = prime[num]
print("pairwise coprime")

Submission status: Accepted

Solution code:
from sys import stdin
readline = stdin.buffer.readline
def r_map(): return map(int, readline().rstrip().split())
def r_list(): return list(r_map())

def main():
    N = int(readline().rstrip())
    A = r_list()
    M = max(A)
    d = [0] * (M + 1)
    g = True
    for a in A:
        d[a] += 1
    for i in range(2, M + 1):
        c = sum(d[i::i])
        if c == N:
            print("not coprime")
            exit()
        elif c > 1:
            g = False
    if g:
        print('pairwise coprime')
    else:
        print('setwise coprime')

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin
readline = stdin.buffer.readline
def r_map(): return map(int, readline().rstrip().split())
def r_list(): return list(r_map())

def main():
    N = int(readline().rstrip())
    A = r_list()
    M = 10**6
    d = [0] * (M + 1)
    g = True
    for a in A:
        d[a] += 1
    for i in range(2, M + 1):
        c = sum(d[i::i])
        if c == N:
            print("not coprime")
            exit()
        elif c > 1:
            g = False
    if g:
        print('pairwise coprime')
    else:
        print('setwise coprime')

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def divplis(N):
    lis = [i for i in range(N+1)]
    for i in range(int(N**0.5+1),1,-1):
        if lis[i] == i:
            lis[i::i] = [i]*(N//i)
    return lis

divp = divplis(10**6+1)
N = int(input())
d = [0]*(10**6+1)
A = [int(i) for i in input().split()]


def pfact(N):
    b = 0
    while N != 1:
        n = divp[N]
        if b != n:
            d[n] += 1
        b = n
        N = N // n
    return 0
    
for a in A:
    pfact(a)
m = max(d)
if m <= 1:
    print("pairwise coprime")
elif m == N:
    print("not coprime")
else:
    print("setwise coprime")

Submission status: Accepted

Solution code:
from math import gcd, sqrt
from functools import reduce

n, *A = map(int, open(0).read().split())
def f(A):
    sup = max(A)+1
    table = [i for i in range(sup)]
    for i in range(2, int(sqrt(sup))+1):
        if table[i] == i:
            for j in range(i**2, sup, i):
                table[j] = i

    D = set()
    for a in A:
        while a != 1:
            if a not in D:
                D.add(a)
                a //= table[a]
            else:
                return False
    return True

if reduce(gcd, A) == 1:
    if f(A):
        print('pairwise coprime')
    else:
        print('setwise coprime')
else:
    print('not coprime')