Problem:
Score : 400 points

Problem Statement

Given is a number sequence A of length N.

Find the number of integers i \left(1 \leq i \leq N\right) with the following property:

For every integer j \left(1 \leq j \leq N\right) such that i \neq j , A_j does not divide A_i.

Constraints

All values in input are integers.

1 \leq N \leq 2 \times 10^5

1 \leq A_i \leq 10^6

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the answer.

Sample Input 1

5
24 11 8 3 16

Sample Output 1

3

The integers with the property are 2, 3, and 4.

Sample Input 2

4
5 5 5 5

Sample Output 2

0

Note that there can be multiple equal numbers.

Sample Input 3

10
33 18 45 28 8 19 89 86 2 4

Sample Output 3

5

Solution in Go (1.14.1) (judge verdict: Runtime Error)

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
)

func main() {
	sc := NewScanner()

	n := sc.ReadInt()
	as := make([]*aElem, n)
	for i := 0; i < n; i++ {
		as[i] = &aElem{i, sc.ReadInt(), false}
	}

	factorMap := make(map[int]*aElemList)
	for _, a := range as {
		factors := Factorize(a.value)
		for _, factor := range factors {
			if factorMap[factor] == nil {
				factorMap[factor] = newAElemList()
			}
			factorMap[factor].Add(a)
		}
	}

	for _, a := range as {
		factorMap[a.value].Each(func(elem *aElem) {
			if elem != a {
				elem.marked = true
			}
		})
	}

	count := 0
	for _, a := range as {
		if !a.marked {
			count++
		}
	}

	fmt.Println(count)
}

type aElem struct {
	index  int
	value  int
	marked bool
}

type aElemList struct {
	first *aElemListNode
	last  *aElemListNode
}

type aElemListNode struct {
	child *aElemListNode
	value *aElem
}

func newAElemList() *aElemList {
	root := aElemListNode{nil, nil}
	return &aElemList{&root, &root}
}

func (list *aElemList) Add(value *aElem) {
	node := aElemListNode{nil, value}
	list.last.child = &node
	list.last = &node
}

func (list *aElemList) Each(f func(value *aElem)) {
	cur := list.first
	for cur.child != nil {
		cur = cur.child
		f(cur.value)
	}
}

func Cartesian(optionArrays [][]int) [][]int {
	optionArrayCount := len(optionArrays)

	productLen := 1
	for _, optionArray := range optionArrays {
		productLen *= len(optionArray)
	}

	product := make([][]int, 0, productLen)
	optionArrayIndexes := make([]int, optionArrayCount)
	for optionArrayIndexes[0] < len(optionArrays[0]) {
		variation := make([]int, optionArrayCount)
		for i, optionArray := range optionArrays {
			optionArrayIndex := optionArrayIndexes[i]
			variation[i] = optionArray[optionArrayIndex]
		}
		product = append(product, variation)

		optionArrayIndexes[optionArrayCount-1]++
		for i := optionArrayCount - 1; i > 0; i-- {
			if optionArrayIndexes[i] >= len(optionArrays[i]) {
				optionArrayIndexes[i] = 0
				optionArrayIndexes[i-1]++
			} else {
				break
			}
		}
	}

	return product
}

func Factorize(n int) []int {
	primeCounts := PrimeFactorize(n)

	primeMultiples := make([][]int, len(primeCounts))
	{
		index := 0
		for prime, count := range primeCounts {
			multiples := make([]int, count+1)
			multiples[0] = 1
			for i := 1; i < count+1; i++ {
				multiples[i] = multiples[i-1] * prime
			}
			primeMultiples[index] = multiples
			index++
		}
	}

	variations := Cartesian(primeMultiples)

	factors := make([]int, len(variations))
	for i, variation := range variations {
		product := 1
		for _, num := range variation {
			product *= num
		}
		factors[i] = product
	}
	// TODO: sort
	return factors
}

func PrimeFactorize(n int) map[int]int {
	primeCounts := make(map[int]int)

	sqrtMax := int(math.Sqrt(float64(n)))

	for n%2 == 0 {
		primeCounts[2]++
		n /= 2
	}
	for n%3 == 0 {
		primeCounts[3]++
		n /= 3
	}

	flag := true
	for i := 5; n > 1 && i <= sqrtMax; {
		for n%i == 0 {
			primeCounts[i]++
			n /= i
		}

		if flag {
			i += 2
		} else {
			i += 4
		}
		flag = !flag
	}

	if n > 1 {
		primeCounts[n]++
	}

	return primeCounts
}

// util
// *   math/simple

func Abs(x int) int {
	if x >= 0 {
		return x
	}
	return -x
}

func Min(values ...int) int {
	if len(values) == 0 {
		panic("no values")
	}
	min := values[0]
	for _, v := range values {
		if v < min {
			min = v
		}
	}
	return min
}

func Max(values ...int) int {
	if len(values) == 0 {
		panic("no values")
	}
	max := values[0]
	for _, v := range values {
		if v > max {
			max = v
		}
	}
	return max
}

func Pow(base int, exponent uint) int {
	answer := 1
	for i := uint(0); i < exponent; i++ {
		answer *= base
	}
	return answer
}

func Ceil(divident, dividor int) int {
	quo := divident / dividor
	rem := divident % dividor

	if rem != 0 {
		if (divident > 0 && dividor > 0) ||
			(divident < 0 && dividor < 0) {
			return quo + 1
		}
	}
	return quo
}

// *   sortutil

func ReverseInts(a []int) {
	sort.Sort(sort.Reverse(sort.IntSlice(a)))
}

func ReverseStrings(a []string) {
	sort.Sort(sort.Reverse(sort.StringSlice(a)))
}

// *   io

type Scanner struct {
	bufScanner *bufio.Scanner
}

func NewScanner() *Scanner {
	bufSc := bufio.NewScanner(os.Stdin)
	bufSc.Split(bufio.ScanWords)
	bufSc.Buffer(nil, 100000000)
	return &Scanner{bufSc}
}

func (sc *Scanner) ReadString() string {
	bufSc := sc.bufScanner
	bufSc.Scan()
	return bufSc.Text()
}

func (sc *Scanner) ReadInt() int {
	bufSc := sc.bufScanner
	bufSc.Scan()
	text := bufSc.Text()

	num, err := strconv.Atoi(text)
	if err != nil {
		panic(err)
	}
	return num
}