You have an array $a_1, a_2, \dots, a_n$ consisting of $n$ distinct integers. You are allowed to perform the following operation on it:

Choose two elements from the array $a_i$ and $a_j$ ($i \ne j$) such that $\gcd(a_i, a_j)$ is not present in the array, and add $\gcd(a_i, a_j)$ to the end of the array. Here $\gcd(x, y)$ denotes greatest common divisor (GCD) of integers $x$ and $y$.

Note that the array changes after each operation, and the subsequent operations are performed on the new array.

What is the maximum number of times you can perform the operation on the array?


-----Input-----

The first line consists of a single integer $n$ ($2 \le n \le 10^6$).

The second line consists of $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^6$). All $a_i$ are distinct.


-----Output-----

Output a single line containing one integer — the maximum number of times the operation can be performed on the given array.


-----Examples-----

Input
5
4 20 1 25 30
Output
3
Input
3
6 10 15
Output
4


-----Note-----

In the first example, one of the ways to perform maximum number of operations on the array is:

Pick $i = 1, j= 5$ and add $\gcd(a_1, a_5) = \gcd(4, 30) = 2$ to the array.

Pick $i = 2, j= 4$ and add $\gcd(a_2, a_4) = \gcd(20, 25) = 5$ to the array.

Pick $i = 2, j= 5$ and add $\gcd(a_2, a_5) = \gcd(20, 30) = 10$ to the array.

It can be proved that there is no way to perform more than $3$ operations on the original array.

In the second example one can add $3$, then $1$, then $5$, and $2$.

Source: codeforces | Difficulty: HARD
Tags: Dynamic programming

Number theory

Mathematics

Complete search


Solutions:
import sys
input = sys.stdin.readline

def read_int_list():
	return list(map(int, input().split()))

def solve(a):
	max_value = max(a)
	present = [False] * (max_value + 1)
	for value in a:
		present[value] = True
	count = 0
	for i in range(max_value, 0, -1):
		first = -1
		second = -1
		if present[i]:
			continue
		for j in range(2 * i, max_value + 1, i):
			if present[j]:
				if first == -1:
					first = j
				elif j % first != 0:
					second = j
					break
		if first != -1 and second != -1:
			count += 1
			present[i] = True
	return count
n = int(input())
a = read_int_list()
print(solve(a))


from math import gcd

def problemD(n, nums):
	maxNum = max(nums)
	isPresent = [False] * (maxNum + 1)
	for num in nums:
		isPresent[num] = True
	ans = 0
	for i in range(1, maxNum + 1):
		if isPresent[i]:
			continue
		num = 0
		for j in range(2 * i, maxNum + 1, i):
			if isPresent[j]:
				if num == 0:
					num = j
				else:
					num = gcd(num, j)
					if num == i:
						ans += 1
						isPresent[i] = True
						break
	return ans
n = int(input())
nums = list(map(int, input().split()))
print(problemD(n, nums))