Coprime pairs mobius inversion

Write codeHard35 min

Your task

Given an array of positive integers, count the number of unordered pairs (i, j) with i < j such that gcd(arr[i], arr[j]) == 1. The naive O(n^2 log) pairwise approach is too slow when the array is large but values are bounded by a modest M (e.g. M <= 10^5). Use a divisor-multiple sieve and Mobius inversion over the count of elements divisible by each d. Return the number of coprime pairs (0 for an empty or single-element array).

Examples

count_coprime_pairs(arr: list[int]) -> int
Example 1
Inputcount_coprime_pairs([1, 2, 3, 4, 5])
Output9