Not Adding (Codeforces 1627D)
Tags: Math, number theory

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.

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$$$.

Sample Inputs:
5
4 20 1 25 30

3
6 10 15

Sample Outputs:
3

4

Solution (Python):
""""""
LARGEST_NUMBER = 10 ** 6
from math import gcd

def max_operations(a):
    resulting_array = [False] * (LARGEST_NUMBER + 1)
    for i in a:
        resulting_array[i] = True
    res = 0
    for i in range(LARGEST_NUMBER, 0, -1):
        if resulting_array[i]:
            continue
        first_delimeter = 0
        for j in range(i * 2, LARGEST_NUMBER + 1, i):
            if resulting_array[j]:
                if first_delimeter == 0:
                    first_delimeter = j
                elif gcd(first_delimeter, j) == i:
                    resulting_array[i] = True
                    res += 1
                    break
    return res

def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    print(max_operations(a))

def test():
    assert max_operations([4, 20, 1, 25, 30]) == 3
    assert max_operations([6, 10, 15]) == 4
    assert max_operations([16, 24]) == 1
    assert max_operations([16, 32]) == 0
if __name__ == '__main__':
    main()

Solution (Python):
import sys, bisect, math, io, os
from collections import deque, Counter
sys.stdin = open('input.txt', 'r') if os.path.exists('input.txt') else sys.stdin
for_strings = 0
input = (lambda : sys.stdin.readline().strip()) if os.getenv('IS_LOCAL_MACHINE', for_strings) else io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
ceil = lambda x: math.ceil(x)
mod = 10 ** 9 + 7

def solve():
    n = int(input())
    a = list(map(int, input().split()))
    m = max(a)
    ispresent = [False] * (m + 1)
    for i in a:
        ispresent[i] = True
    ans = 0
    for i in range(m, 0, -1):
        if not ispresent[i]:
            g = 0
            for j in range(2 * i, m + 1, i):
                if ispresent[j]:
                    if g == 0:
                        g = j
                    elif math.gcd(g, j) == i:
                        g = i
                        break
            if g == i:
                ans += 1
                ispresent[i] = True
    return ans
if __name__ == '__main__':
    t = 1
    for _ in range(t):
        print(solve())

Solution (Python):
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
if __name__ == '__main__':
    n = int(input())
    a = read_int_list()
    print(solve(a))