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.

Solution in Kotlin (1.3.71) (judge verdict: Wrong Answer)

import java.io.IOException
import java.lang.NumberFormatException
import java.lang.StringBuilder
import java.util.ArrayList

object AMain{
    fun solve(){
        val n = nextInt()
        val a = ArrayList<Long>()
        for(i in 1..n){
            a.add(nextLong())
        }

        var ispair = true


        a.sort()
        var pre = a[0]
        var mi = 1
        while(mi < a.size){
            if(a[mi] == pre){
                ispair = false
                a.removeAt(mi)
            }else{
                mi++
            }
        }


        val isprime = IntArray(1000001){1}
        val prime = ArrayList<Int>()
        isprime[0] = 0
        isprime[1] = 0
        for(i in 2..1000000){
            if(isprime[i] > 0){
                for(j in i until 1000001 step i){
                    isprime[j] = 0
                }
                prime.add(i)
            }
        }
//        println(prime.size)
        if(ispair) {
            loop@ for (i in a.indices) {
                var ma = a[i].toInt()
                if (ma == 1) continue
                if (isprime[ma] > 0) {
                    if (!check(isprime, ma)) {
                        ispair = false
                        break
                    }
                } else {
                    var pos = 0
                    var checked = false
                    while (ma > 1 && pos < prime.size) {
                        if (ma % prime[pos] == 0) {
                            ma /= prime[pos]
                            if (checked) continue
                            if (!check(isprime, ma)) {
                                ispair = false
                                break@loop
                            }
                            if (!check(isprime, prime[pos])) {
                                ispair = false
                                break@loop
                            }
                            checked = true
                        } else {
                            pos++
                            checked = false
                        }
                    }
                }
            }
        }

//        var ispair = true
//        prime.forEach {
//            var num = 0
//            for(i in a.indices){
//                if(a[i] % it == 0L) num++
//                if(num >= 2){
//                    ispair = false
//                    return@forEach
//                }
//            }
//        }

        if(ispair){
            println("pairwise coprime")
            return
        }

        var cur = a[0]
        for(i in 1 until a.size){
            cur = gcd(cur, a[i])
        }

        if(cur == 1L){
            println("setwise coprime")
        }else{
            println("not coprime")
        }

    }

    fun check(isprime: IntArray, x: Int): Boolean{
        if(x == 1) return true
        isprime[x]++
        if(isprime[x] > 2){
            return false
        }
        return true
    }

    fun gcd(a: Long, b: Long): Long{
        if(b == 0L) return a
        return gcd(b, a % b)
    }





    // Scanner based on uwi-san's Java code
    val stream = System.`in`
    val buffer = ByteArray(1024)
    var ptr = 0
    var buflen = 0

    fun hasNextByte(): Boolean{
        if(ptr < buflen){
            return true
        }else{
            ptr = 0
            try{
                buflen = stream.read(buffer)
            }catch (e: IOException){
                e.printStackTrace()
            }
            if(buflen <= 0){
                return false
            }
        }
        return true
    }

    fun readByte() =
        if(hasNextByte()) buffer[ptr++]
        else -1

    fun isPrintableChar(c: Byte) = c in 33..126

    fun hasNext(): Boolean{
        while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++
        return hasNextByte()
    }

    fun next(): String{
        if(!hasNext()) throw NoSuchElementException()
        val sb = StringBuilder()
        var b = readByte()
        while(isPrintableChar(b)){
            sb.appendCodePoint(b.toInt())
            b = readByte()
        }
        return sb.toString()
    }

    fun nextLong(): Long{
        if(!hasNext()) throw NoSuchElementException()
        var n = 0L
        var minus = false
        var b = readByte()
        if(b == '-'.toByte()){
            minus = true
            b = readByte()
        }
        if(b !in '0'.toByte()..'9'.toByte())
            throw NumberFormatException()
        while(true){
            if(b in '0'.toByte()..'9'.toByte()){
                n *= 10
                n += b - '0'.toByte()
            }else if(!isPrintableChar(b)){
                return if(minus) -n else n
            }else{
                throw NumberFormatException()
            }
            b = readByte()
        }
    }

    fun nextInt(): Int{
        val n1 = nextLong()
        if(n1 !in Int.MIN_VALUE..Int.MAX_VALUE)
            throw NumberFormatException()
        return n1.toInt()
    }

    fun NextDouble() = next().toDouble()
}

fun main(){
    AMain.solve()
}