Given an array of integers $$$(A[i] <= 10^5)$$$. We have to find number of unordered pairs (i, j) such that their GCD is greater than B.
All value <= $$$10^5$$$
Can someone give me a general idea how to approach such kinds of problems? Because this type of question appears frequently in programming contests and every time I devote much time solving this but failed each time :(
You can work backward, let dp[i] be the ammount of pairs that have gcd equals to i in vector, for a value A[j] we know that the gcd(A[j], y) = d such that d is a divisor of A[j], so lets make freq[i] = ammount of values that have i as divisor. dp[i] = (freq[i] choose 2) — dp[2*i] — dp[3*i] — ... — dp[k*i] We can fill dp array in nlog(n), the answer is the suffix-sum of dp array
I am thinking in this way only. But failed to land on this solution.
Do you know any blog or some questions similar to this?
D. The Millennium Prize Problems