This page is still under construction.

Parts of this page are still being built. What you see may change.

Odd GCD Matching

Time limit1sMemory limit512 MB

Summary
Given N integers, find the largest set of disjoint pairs where each pair has an odd greatest common divisor.
Level

Medium7 of 10

Topics
Number theory, Graph, Greedy, Math
Solved
No attempts yet

Problem

There are NN integers A1,…,ANA_1, \ldots, A_N. AiA_i can be paired with AjA_j if gcd⁡(Ai,Aj)\gcd(A_i, A_j) is odd. gcd⁡(a,b)\gcd(a, b) is the greatest common divisor of aa and bb. For example, 6 can be paired with 9 because gcd⁡(6,9)=3\gcd(6, 9) = 3 is odd; however, 12 cannot be paired with 8 because gcd⁡(12,8)=4\gcd(12, 8) = 4 is even.

An odd GCD matching of A1,…,ANA_1, \ldots, A_N is a set of pairs satisfying the following.

  • Each pair consists of two integers (i,j)(i, j) with 1≤i<j≤N1 \le i < j \le N.
  • Each integer ii appears at most once in the set.
  • If (i,j)(i, j) is in the set, then AiA_i can be paired with AjA_j.

Given A1,…,ANA_1, \ldots, A_N, find the size of a maximum odd GCD matching of A1,…,ANA_1, \ldots, A_N. An odd GCD matching is maximum if and only if no other odd GCD matching has more pairs than it.

For example, let A1,…,A5={6,8,9,12,13}A_1, \ldots, A_5 = \{6, 8, 9, 12, 13\}. The size of a maximum odd GCD matching in this example is 2; one such matching is {(1,3),(2,5)}\{(1, 3), (2, 5)\}, which pairs (A1=6(A_1 = 6 with A3=9)A_3 = 9) and (A2=8(A_2 = 8 with A5=13)A_5 = 13). Note that {(1,3)}\{(1, 3)\} with size 1 is also a valid odd GCD matching, but it is not maximum. On the other hand, {(2,4)}\{(2, 4)\} is not a valid odd GCD matching because A2=8A_2 = 8 cannot be paired with A4=12A_4 = 12 in this example.

Input

The first line contains an integer NN, the size of AA. (1≤N≤20 0001 \le N \le 20\,000) The next line contains NN integers AiA_i, the array AA. (1≤Ai≤1061 \le A_i \le 10^6)

Output

Output in one line an integer, the size of a maximum odd GCD matching of A1,…,ANA_1, \ldots, A_N.

Examples3

  1. Example 1

    Input
    5
    6 8 9 12 13
    
    Expected output
    2
    
  2. Example 2

    Input
    3
    10 10 10
    
    Expected output
    0
    
  3. Example 3

    Input
    7
    4 3 2 4 5 6 3
    
    Expected output
    3