Problems > Sum of Pairwise GCDs > Editorial

Sum of Pairwise GCDs — Solution & Editorial

Back to the Problem

For each d, count pairs whose gcd is a MULTIPLE of d = C(cnt-multiples, 2), then recover pairs with gcd EXACTLY d by a downward subtractive sieve; sum d·exact[d].

Complexity: O(V log V + N)

Watch out: The answer reaches ~105·C(N,2) ≈ 5×1014.