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: Time Limit Exceeded

Solution code:
import fractions
import functools

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

factors = [[] for _ in range(int(1e6)+1)]
for i in range(2, int(1e6)//2+1):
    for j in range(i, int(1e6)+1, i):
        factors[j].append(i)

def classify():
    set_factors = [False]*int(1e6+1)
    for a in A:
        for f in factors[a]:
            if set_factors[f]: break
            set_factors[f] = True
    else:
        return 'pairwise coprime'

    if functools.reduce(fractions.gcd, A) == 1:
        return 'setwise coprime'

    return 'not coprime'

print(classify())

Submission status: Time Limit Exceeded

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

import math
import sys

def get_sieve_of_eratosthenes(n):
    if not isinstance(n, int):
        raise TypeError('n is int type.')
    if n < 2:
        raise ValueError('n is more than 2')
    prime = []
    limit = math.sqrt(n)
    data = [i + 1 for i in range(1, n)]
    D=list(range(2,n+1))
    while True:
        l=len(data)
        p = data[0]
        if limit <= p:
            return D
        prime.append(p)
        temp=[]
        for i in range(l):
          if data[i]%p!=0:
            temp.append(data[i])
          else:
            D[data[i]-2]=p
        data=temp

B=get_sieve_of_eratosthenes(10**6)
        
def fac(n):
  s=set()
  k=n
  while k!=B[k-2]:
    s.add(B[k-2])
    k=k//B[k-2]
  s.add(k)
  return s

com=set()
g=0
for i in range(N):
  g=math.gcd(g,A[i])
  if com=={0}:
    continue
  if fac(A[i])&com==set():
    com=com.union(fac(A[i]))
  else:
    com={0}

if com!={0}:
  print("pairwise coprime")
  sys.exit
elif g==1:
  print("setwise coprime")
else:
  print("not coprime")

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import sys
input = sys.stdin.readline


def factorization(n, primes):
    arr = []
    temp = n
    for i in primes:
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append(i)

    if temp != 1:
        arr.append(temp)

    return arr


def get_sieve_of_eratosthenes_new(n):
    import math
    if not isinstance(n, int):
        raise TypeError('n is int type.')
    if n < 2:
        raise ValueError('n is more than 2')
    prime = []
    limit = math.sqrt(n)
    data = [i + 1 for i in range(1, n)]
    while True:
        p = data[0]
        if limit <= p:
            return prime + data
        prime.append(p)
        data = [e for e in data if e % p != 0]


def main():
    n = int(input())
    A = list(map(int, input().split()))

    primes = get_sieve_of_eratosthenes_new(10**6)
    fact_dict = defaultdict(int)
    for a in A:
        facts = factorization(a, primes)
        for fact in facts:
            fact_dict[fact] += 1

    if not fact_dict.values():
        count = 1
    else:
        count = max(fact_dict.values())
    if count == 1:
        print("pairwise coprime")
    elif count != n:
        print("setwise coprime")
    else:
        print("not coprime")


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import sys
input = sys.stdin.readline


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append(i)

    if temp != 1:
        arr.append(temp)

    return arr


def main():
    n = int(input())
    A = list(map(int, input().split()))
    fact_dict = defaultdict(int)
    for a in A:
        facts = factorization(a)
        for fact in facts:
            fact_dict[fact] += 1

    if not fact_dict.values():
        count = 1
    else:
        count = max(fact_dict.values())
    if count == 1:
        print("pairwise coprime")
    elif count != n:
        print("setwise coprime")
    else:
        print("not coprime")


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def prime(p):
    memo = set()
    for i in range(2,(int(p**0.5)+1)):
        while p%i==0:
            p //= i
            memo.add(i)
    if p != 1:
        memo.add(p)
    return memo

from math import gcd

N = int(input())

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

tmp_gcd = A[0]

for i in range(1,N):
    tmp_gcd = gcd(tmp_gcd, A[i])

if 1 < tmp_gcd:
    print('not coprime')
    exit()

servive_prime = set()
for a in A:
    now_prime = prime(a)
    tmp_prime = servive_prime.union(now_prime)
    if len(tmp_prime) < len(servive_prime) + len(now_prime):
        print('setwise coprime')
        exit()
    servive_prime = tmp_prime
print('pairwise coprime')

Submission status: Time Limit Exceeded

Solution code:
import sys
#ユークリッドの互除法
def gcd(x, y):
    if x % y == 0:
        return y
    else:
        return gcd(y, x % y)
#入力
N = int(input())
A = list(map(int, input().split()))
#カウントしていく
count= [0] * (1000001)
setwise_gcd = A[0]
for i in range(0,N):
    count[A[i]]+= 1
    setwise_gcd = gcd(setwise_gcd, A[i])

#全体のgcdが1でないならnot coprime
if setwise_gcd != 1:
    print("not coprime")
    sys.exit(0)
#A[x] % i == 0 && A[y] % i == 0 となるx, y が存在しないならpairwise coprime
for i in range(2,1000001):
    divided_count = count[i]
    now_number = 2 * i
    while(now_number<1000001):
        divided_count+= count[now_number]
        now_number+= i
    if(divided_count >= 2):
        print("setwise coprime")
        exit(0)
print("pairwise coprime")

Submission status: Time Limit Exceeded

Solution code:
import sys
from functools import reduce
from math import gcd

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    
    pairwise = True
    used = [False] * (max(A) + 1)
    
    for a in A:
        for i in range(2, int(a ** 0.5) + 1):
            if a % i == 0:
                if used[i]:
                    pairwise = False
                else:
                    used[i] = True
                    while a % i == 0:
                        a //= i
        if a != 1:
            if used[a]:
                pairwise = False
                break
            else:
                used[a] = True


    g = reduce(gcd, A)

    if pairwise:
        print('pairwise coprime')
    else:
        if g == 1:
            print('setwise coprime')
        else:
            print('not coprime')


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from math import gcd

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

def isSetwise(A):
    ret = A[0]
    for a in A:
        ret = gcd(ret, a)
    return ret == 1

def isPairwise(A):
    prd = 1
    for a in A:
        if gcd(a, prd) > 1:
            return False
        prd *= a
    return True

if not isSetwise(A):
    print('not coprime')
    exit()

if isPairwise(A):
    print('pairwise coprime')
else:
    print('setwise coprime')

Submission status: Time Limit Exceeded

Solution code:
import sys,random
from math import gcd
input=sys.stdin.readline
n=int(input())
*a,=map(int, input().split())
random.shuffle(a)
tmp=a[0]
for i in range(1,n):
    tmp=gcd(tmp,a[i])
if tmp>1:
    print('not coprime')
    exit()

# pairwize coprime なら素因数が2度現れることはない

def f(x):
    res=set()
    for i in range(2,int(x**(1/2))+3):
        if x%i==0:
            while x%i==0:
                x//=i
            res.add(i)
    if x>1:
        res.add(x)
    return res,x
tmp=set()
tmp2=set()
for ai in a:
    for p in tmp:
        if ai%p==0:
            print('setwise coprime')
            exit()
    pp,x=f(ai)
    tmp|=pp 
    if x>1 and x in tmp2:
        print('setwise coprime')
        exit()
    tmp2.add(x)
    
        
print('pairwise coprime')

Submission status: Time Limit Exceeded

Solution code:
import sys,math
n=int(input())
a=list(map(int,input().split()))
numbers={}
flag=0
for i in range(n):
  if a[i] in numbers:
    numbers[a[i]]+=1
  else:
    numbers[a[i]]=1
pointer=2
while pointer<10**6+1:
  count=0
  pointer2=pointer
  while pointer2<10**6+1:
    if pointer2 in numbers:
      count+=numbers[pointer2]
    pointer2+=pointer
  if count>=2:
    flag=1
    break
  pointer+=1
if flag==0:
  print('pairwise coprime')
  sys.exit()
ans=a[0]
for i in range(1,n):
  ans=math.gcd(a[i],ans)
if ans==1:
  print('setwise coprime')
else:
  print('not coprime')

Submission status: Time Limit Exceeded

Solution code:
from math import gcd

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

def isSetwise(A):
    ret = A[0]
    for a in A:
        ret = gcd(ret, a)
    return ret == 1

def createPrimeList(N, isTable=True):
    isPrime = [True] * (N + 1)
    isPrime[0] = False
    isPrime[1] = False
    for i in range(2, N + 1):
        if not isPrime[i]:
            continue
        for p in range(i * 2, N, i):
            isPrime[p] = False
    return isPrime if isTable else [i for i in range(2, N) if isPrime[i]]

def isPairwise(A):
    primes = set(createPrimeList(max(A) + 1, isTable=False))

    for a in A:
        P = set()
        for p in primes:
            if a == 1:
                break
            if a % p == 0:
                P.add(p)
            while a % p == 0:
                a //= p
        if a != 1:
            return False
        primes -= P
    return True

if not isSetwise(A):
    print('not coprime')
    exit()

if isPairwise(A):
    print('pairwise coprime')
else:
    print('setwise coprime')

Submission status: Time Limit Exceeded

Solution code:
import math

N = int(input())
A = list(map(int, input().split()))
C = [0] * (10 ** 6 + 1)

for i in range(N):
    C[A[i]] += 1

is_pairwise = True
max_a = max(A) + 1
X = [0] * max_a
for i in range(2, max_a):
    count = 0
    for j in range(i, max_a, i):
        count += C[j]
    if count > 1:
        is_pairwise = False

if is_pairwise:
    print('pairwise coprime')
    exit()

tmp = A[0]
for i in range(1, N):
    tmp = math.gcd(tmp, A[i])

if tmp == 1:
    print('setwise coprime')
else:
    print('not coprime')

Submission status: Time Limit Exceeded

Solution code:
import sys,random
from math import gcd
input=sys.stdin.readline
n=int(input())
*a,=map(int, input().split())
random.shuffle(a)
tmp=a[0]
for i in range(1,n):
    tmp=gcd(tmp,a[i])
if tmp>1:
    print('not coprime')
    exit()

# pairwize coprime なら素因数が2度現れることはない

def f(x):
    res=set()
    for i in range(2,int(x**(1/2))+3):
        if x%i==0:
            while x%i==0:
                x//=i
            res.add(i)
    if x>1:
        res.add(x)
    return res
tmp=set()
for ai in a:
    for p in tmp:
        if ai%p==0:
            print('setwise coprime')
            exit()
    pp=f(ai)
    tmp|=pp 
print('pairwise coprime')

Submission status: Time Limit Exceeded

Solution code:
from math import gcd

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

def createPrimeList(N, isTable=True):
    isPrime = [True] * (N + 1)
    isPrime[0] = False
    isPrime[1] = False
    for i in range(2, N + 1):
        if not isPrime[i]:
            continue
        for p in range(i * 2, N, i):
            isPrime[p] = False
    return isPrime if isTable else [i for i in range(2, N) if isPrime[i]]

primeList = createPrimeList(10**5, False)
def primeFactorization2(N):
    primes = set()
    for p in primeList:
        if N % p == 0:
            primes.add(p)
    return primes

def isSetwise(A):
    ret = A[0]
    for a in A:
        ret = gcd(ret, a)
    return ret == 1

def isPairwise(A):
    P = set()
    for a in A:
        for p in primeFactorization2(a):
            if p in P:
                return False
            P.add(p)
    return True

if not isSetwise(A):
    print('not coprime')
    exit()

if isPairwise(A):
    print('pairwise coprime')
else:
    print('setwise coprime')

Submission status: Time Limit Exceeded

Solution code:
from math import gcd

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

def createPrimeList(N, isTable=True):
    isPrime = [True] * (N + 1)
    isPrime[0] = False
    isPrime[1] = False
    for i in range(2, N + 1):
        if not isPrime[i]:
            continue
        for p in range(i * 2, N, i):
            isPrime[p] = False
    return isPrime if isTable else [i for i in range(2, N) if isPrime[i]]

from collections import Counter
primeList = createPrimeList(10**5, False)
def primeFactorization2(N):
    primes = Counter()
    for p in primeList:
        while N % p == 0:
            N //= p
            primes[p] += 1
    return primes

def isSetwise(A):
    ret = A[0]
    for a in A:
        ret = gcd(ret, a)
    return ret == 1

def isPairwise(A):
    P = set()
    for a in A:
        ps = primeFactorization2(a)
        for p in ps.keys():
            if p in P:
                return False
            P.add(p)
    return True

if not isSetwise(A):
    print('not coprime')
    exit()

if isPairwise(A):
    print('pairwise coprime')
else:
    print('setwise coprime')

Submission status: Time Limit Exceeded

Solution code:
#abc177 e
import sys,math,collections,itertools
import functools
input = sys.stdin.readline


N=int(input())
A=list(map(int,input().split()))
fact = set()
for a in A:
    if a==1:
        fact.add(1)
        continue
    if a%2 == 0 and 2 in fact:
        break
    while a%2==0:
        fact.add(2)
        a//=2
            
    i =3
    while i*i <= a:
        if a%i ==0 and i in fact:
            break
        while a%i==0:
            fact.add(i)
            a//=i
        else:
            i +=2
    else:
        if a != 1:
            if a in fact:
                break
            fact.add(a)
        continue
    break
else:
    print('pairwise coprime')
    exit()
if functools.reduce(math.gcd,A)!=1:
    print('not coprime')
else:
    print('setwise coprime')

Submission status: Time Limit Exceeded

Solution code:
from math import gcd

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

def createPrimeList(N, isTable=True):
    isPrime = [True] * (N + 1)
    isPrime[0] = False
    isPrime[1] = False
    for i in range(2, N + 1):
        if not isPrime[i]:
            continue
        for p in range(i * 2, N, i):
            isPrime[p] = False
    return isPrime if isTable else [i for i in range(2, N) if isPrime[i]]

from collections import Counter
primeList = createPrimeList(10**5, False)
def primeFactorization2(N):
    primes = Counter()
    for p in primeList:
        while N % p == 0:
            N //= p
            primes[p] += 1
    return primes

def isSetwise(A):
    ret = A[0]
    for a in A:
        ret = gcd(ret, a)
    return ret == 1

def isPairwise(A):
    prd = Counter()
    for a in A:
        ps = primeFactorization2(a)
        for p, c in ps.items():
            if prd[p] >= 1:
                return False
            prd[p] += c
    return True

if not isSetwise(A):
    print('not coprime')
    exit()

if isPairwise(A):
    print('pairwise coprime')
else:
    print('setwise coprime')

Submission status: Time Limit Exceeded

Solution code:
#abc177 e
import sys,math,collections,itertools
input = sys.stdin.readline

def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            while temp%i==0:
                temp //= i
            arr.append(i)

    if temp!=1:
        arr.append(temp)

    if arr==[]:
        arr.append(n)
    return arr


N=int(input())
A=list(map(int,input().split()))
num = [0]*(10**6+1)

fact = [0]*(10**6+1)
fact[1]=1

for a in A:
    if a != 1:
        flag = 0
        temp = a
        for i in range(2, int(-(-a**0.5//1))+1):
            if temp%i==0:
                while temp%i==0:
                    temp //= i
                flag = 1
                fact[i]+=1
        if temp!=1:
            fact[temp]+=1
            flag = 1
        if flag==0:
            fact[n]+=1


        
mn= max(fact)

if mn == 1:
    print('pairwise coprime')
elif mn ==N:
    print('not coprime')
else:
    print('setwise coprime')

Submission status: Time Limit Exceeded

Solution code:
#abc177 e
import sys,math,collections,itertools
input = sys.stdin.readline

def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            while temp%i==0:
                temp //= i
            arr.append(i)

    if temp!=1:
        arr.append(temp)

    if arr==[]:
        arr.append(n)
    return arr


N=int(input())
A=list(map(int,input().split()))
num = [0]*(10**6+1)

fact = [1]

for a in A:
    if a != 1:
        fact += factorization(a)
mn= collections.Counter(fact).most_common()[0][1]

if mn == 1:
    print('pairwise coprime')
elif mn ==N:
    print('not coprime')
else:
    print('setwise coprime')

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict


def get_prime_factors(x):
    out = set()
    if x < 1:
        return out
    while not x%2:
        out.add(2)
        x //= 2
    i = 3
    while i*i <= x:
        if not x%i:
            out.add(i)
            x //= i
        else:
            i += 2
    if x != 1:
        out.add(x)
    return out

def main():
    n = int(input())
    a = list(map(int, input().split()))
    df = defaultdict(lambda : 0)
    
    
    for ai in a:
        primes = get_prime_factors(ai)
        for prime in primes:
            df[prime] += 1
    if df.values():
        val = max(df.values())
        if val == 1:
            print("pairwise coprime")
        elif val == n:
            print("not coprime")
        else:
            print("setwise coprime")
    else:
        print("pairwise coprime")


main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict


def get_prime_factors(x):
    out = set()
    if x < 1:
        return out
    while not x%2:
        out.add(2)
        x //= 2
    i = 3
    while i*i <= x:
        if not x%i:
            out.add(i)
            x //= i
        else:
            i += 2
    if x != 1:
        out.add(x)
    return out


def main():
    n = int(input())
    a = list(map(int, input().split()))
    df = defaultdict(lambda : 0)
    for ai in a:
        primes = get_prime_factors(ai)
        for prime in primes:
            df[prime] += 1
    if df.values():
        val = max(df.values())
        if val == 1:
            print("pairwise coprime")
        elif val == n:
            print("not coprime")
        else:
            print("setwise coprime")
    else:
        print("pairwise coprime")

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def factorize(n, d):
  # if n == 0 or n == 1:
    # return []
    arr = []
    tmp = n
    while True:
      i = d[tmp]
      if i == 1:
        break
      #cnt=0
      while tmp%i==0:
        #cnt+=1
        tmp //= i
      arr.append(i)
    if tmp!=1:
      arr.append(tmp)
    if arr==[]:
      arr.append(n)
    return arr

def minimum_prime(n):
  # 試し割りをする際の最小の素数
  # エラトステネスの篩と同様の実装
    d = list(range(n+1))
    d[0] = 1
    for i in range(2, int(n**0.5) + 1):
        if d[i] != i:
          continue
        for j in range(i * 2, n + 1, i):
          if d[j] == j:
            d[j] = i
    return d  

def main():
  n = int(input())
  arr = list(map(int, input().split()))
  max_arr = max(arr)
  d = minimum_prime(max_arr)
  dp = [0]*(max_arr+1)
  
  
  searched = []
  for a in arr:
    if a != 1:
      primes = factorize(a, d)
      for p in primes:
        dp[p] += 1
  
  max_dp = max(dp)
  if max_dp < 2:
    print('pairwise coprime')
  elif max_dp == n:
    print('not coprime')
  else:
    print('setwise coprime')
    

main()

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

def factorize(n, d):
  # if n == 0 or n == 1:
    # return []
    arr = []
    tmp = n
    while True:
      i = d[tmp]
      if i == 1:
        break
      #cnt=0
      while tmp%i==0:
        #cnt+=1
        tmp //= i
      arr.append(i)
    if tmp!=1:
      arr.append(tmp)
    if arr==[]:
      arr.append(n)
    return arr

def minimum_prime(n):
  # 試し割りをする際の最小の素数
  # エラトステネスの篩と同様の実装
    d = list(range(n+1))
    d[0] = 1
    for i in range(2, int(n**0.5) + 1):
        if d[i] != i:
          continue
        for j in range(i * 2, n + 1, i):
          if d[j] == j:
            d[j] = i
    return d  

def main():
  n = int(input())
  arr = list(map(int, input().split()))
  max_arr = max(arr)
  d = minimum_prime(max_arr)
  
  if gcd(*arr) != 1:
    print('not coprime')
    exit()
  
  searched = []
  for a in arr:
    if a != 1:
      primes = factorize(a, d)
      for p in primes:
        if p in searched:
          print('setwise coprime')
          exit()
        searched.append(p)

  print('pairwise coprime')
    
  
main()

Submission status: Time Limit Exceeded

Solution code:
from math import gcd

N = 1000001
 
spf = [*range(N)]
i = 2
while i * i < N:
  if spf[i] == i:
    for j in range(i * i, N, i):
      if spf[j] == j:
        spf[j] = i
  i += 1
  
r = 'pairwise'

input()
d = None
p = 1
for x in map(int, input().split()):
  d = gcd(d or x, x)
  e = gcd(p, x)
  if e > 1:
    r = 'setwise'
  p = p // e * x
print('not' if d > 1 else r, 'coprime')

Submission status: Time Limit Exceeded

Solution code:
import math

N = 1000001
 
spf = [*range(N)]
i = 2
while i * i < N:
  if spf[i] == i:
    for j in range(i * i, N, i):
      if spf[j] == j:
        spf[j] = i
  i += 1
  
r = 'pairwise'

input()
d = None
u = set()
for x in map(int, input().split()):
  d = math.gcd(d or x, x);
  s = set()
  while x > 1:
    p = spf[x]
    if p in u:
      r = 'setwise'
    s.add(p)
    x //= p
  u |= s
print('not' if d > 1 else r, 'coprime')

Submission status: Time Limit Exceeded

Solution code:
import bisect
import collections
import copy
import functools
import heapq
import math
import sys
from collections import deque
from collections import defaultdict
input = sys.stdin.readline
sys.setrecursionlimit(10**9)
MOD = 10**9+7

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

def find_prime(n):
    prime_list = []
    find_list = list(range(2,n+1))
    while True:
        if find_list[0] > n**(1/2):
            prime_list.extend(find_list)
            break
        else:
            head = find_list[0]
            prime_list.append(head)
            find_list.pop(0)
            find_list = [num for num in find_list if num % head != 0]
    return prime_list

prime = find_prime(10**6)
def factorization(n):
    arr = []
    temp = n
    for i in prime:
        if i > n:
            break
        if temp % i == 0:
            cnt = 0
            while temp % i ==0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

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

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

    return arr

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

def multiple(a, b):
    return a*b // euclid(a, b)

def gcd(nums):
    return functools.reduce(euclid, nums)

if gcd(A) != 1:
    print("not coprime")

else:
    """
    flag = 0
    M = [False]*(10**6+1)
    for i in range(N):
        n = A[i]
        if M[n]:
            flag = 1
            break
        if n != 1:
            k = 1
            while n*k <= 10**6:
                M[n*k] = True
                k += 1
    """
    flag = 0
    M = [False]*(10**6+1)
    for i in range(N):
        n = A[i]
        if n == 1:
            continue
        l = factorization(n)
        for j in range(len(l)):
            p = l[j][0]
            if M[p]:
                flag = 1
                break
            M[p] = True
    
    if flag == 0:
        print("pairwise coprime")
    else:
        print("setwise coprime")

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect as bi
import math
from collections import defaultdict as dd
import heapq
import itertools
input=sys.stdin.readline
from random import randint
##import numpy as np
##sys.setrecursionlimit(10**7)
mo=10**9+7
def cin():
    return map(int,sin().split())
def ain():            
    return list(map(int,sin().split()))
def sin():
    return input()
def inin():
    return int(input())
import sys
import bisect as bi
import math
from collections import defaultdict as dd
import heapq
import itertools
input=sys.stdin.readline
##from random import randint
##import numpy as np
##sys.setrecursionlimit(10**7)
mo=10**9+7
def cin():
    return map(int,sin().split())
def ain():            
    return list(map(int,sin().split()))
def sin():
    return input()
def inin():
    return int(input())
##def gcd(a,b): 
##    if a == 0: 
##        return b 
##    return gcd(b % a, a)
prime1=[]
def SieveOfEratosthenes(n): #list of primes upto n
    global prime1
    prime1 = [True for i in range(n+1)] 
    p = 2
    while (p * p <= n): 
        if (prime1[p] == True): 
            for i in range(p * p, n+1, p): 
                prime1[i] = False
        p += 1
    f=[]
    for p in range(2, n): 
        if prime1[p]: 
            f.append(p)
    return f
for _ in range(1):
    n=inin()
    l=ain()
    f1,f2=0,0
    gc=l[0]
    d=dd(int)
    if(l.count(1)>0):gc=1
    else:
      for i in range(1,n):
        gc=math.gcd(gc,l[i])
    if(gc!=1):print("not coprime")
    else:
        k=max(l)
        lp=SieveOfEratosthenes(k)
        for i in l:
          if(i!=1):
            for j in lp:
              if(i%j==0):
                if(d[j]==1):
                  f1=1
                  break
                else:d[j]=1
            if(f1):break
        if(f1):
            print("setwise coprime")
        else:
            print("pairwise coprime")
        
        
    
    
    

 
    
    
    
    
        
        
    
    
    
    


    




##def msb(n):n|=n>>1;n|=n>>2;n|=n>>4;n|=n>>8;n|=n>>16;n|=n>>32;n|=n>>64;return n-(n>>1) #2 ki power
##def pref(a,n,f):             
##    pre=[0]*n
##    if(f==0):         ##from beginning
##        pre[0]=a[0]
##        for i in range(1,n):
##            pre[i]=a[i]+pre[i-1]
##    else:              ##from end
##        pre[-1]=a[-1]
##        for i in range(n-2,-1,-1):
##            pre[i]=pre[i+1]+a[i]
##    return pre
##maxint=10**24 
##def kadane(a,size): 
##    max_so_far = -maxint - 1
##    max_ending_here = 0
##       
##    for i in range(0, size): 
##        max_ending_here = max_ending_here + a[i] 
##        if (max_so_far < max_ending_here): 
##            max_so_far = max_ending_here 
##  
##        if max_ending_here < 0: 
##            max_ending_here = 0   
##    return max_so_far
##def power(x, y):
##    if(y == 0):return 1
##    temp = power(x, int(y / 2))%mo
##    if (y % 2 == 0):return (temp * temp)%mo 
##    else:
##        if(y > 0):return (x * temp * temp)%mo 
##        else:return ((temp * temp)//x )%mo

            
        
        
        
            
    
        
        
    
    
    

 
    
    
    
    
        
        
    
    
    
    


    




##def msb(n):n|=n>>1;n|=n>>2;n|=n>>4;n|=n>>8;n|=n>>16;n|=n>>32;n|=n>>64;return n-(n>>1) #2 ki power
##def pref(a,n,f):             
##    pre=[0]*n
##    if(f==0):         ##from beginning
##        pre[0]=a[0]
##        for i in range(1,n):
##            pre[i]=a[i]+pre[i-1]
##    else:              ##from end
##        pre[-1]=a[-1]
##        for i in range(n-2,-1,-1):
##            pre[i]=pre[i+1]+a[i]
##    return pre
##maxint=10**24 
##def kadane(a,size): 
##    max_so_far = -maxint - 1
##    max_ending_here = 0
##       
##    for i in range(0, size): 
##        max_ending_here = max_ending_here + a[i] 
##        if (max_so_far < max_ending_here): 
##            max_so_far = max_ending_here 
##  
##        if max_ending_here < 0: 
##            max_ending_here = 0   
##    return max_so_far
##def power(x, y):
##    if(y == 0):return 1
##    temp = power(x, int(y / 2))%mo
##    if (y % 2 == 0):return (temp * temp)%mo 
##    else:
##        if(y > 0):return (x * temp * temp)%mo 
##        else:return ((temp * temp)//x )%mo

Submission status: Time Limit Exceeded

Solution code:
import bisect
import collections
import copy
import functools
import heapq
import math
import sys
from collections import deque
from collections import defaultdict
input = sys.stdin.readline
sys.setrecursionlimit(10**9)
MOD = 10**9+7

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

def find_prime(n):
    prime_list = []
    find_list = list(range(2,n+1))
    while True:
        if find_list[0] > n**(1/2):
            prime_list.extend(find_list)
            break
        else:
            head = find_list[0]
            prime_list.append(head)
            find_list.pop(0)
            find_list = [num for num in find_list if num % head != 0]
    return prime_list

prime = find_prime(10**6)
def factorization(n):
    arr = []
    temp = n
    for i in prime:
        if temp % i == 0:
            cnt = 0
            while temp % i ==0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

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

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

    return arr

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

def multiple(a, b):
    return a*b // euclid(a, b)

def gcd(nums):
    return functools.reduce(euclid, nums)

if gcd(A) != 1:
    print("not coprime")

else:
    """
    flag = 0
    M = [False]*(10**6+1)
    for i in range(N):
        n = A[i]
        if M[n]:
            flag = 1
            break
        if n != 1:
            k = 1
            while n*k <= 10**6:
                M[n*k] = True
                k += 1
    """
    flag = 0
    M = [False]*(10**6+1)
    for i in range(N):
        n = A[i]
        if n == 1:
            continue
        l = factorization(n)
        for j in range(len(l)):
            p = l[j][0]
            if M[p]:
                flag = 1
                break
            M[p] = True
    
    if flag == 0:
        print("pairwise coprime")
    else:
        print("setwise coprime")