pk842's blog

By pk842, history, 5 years ago, In English

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 :(

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it +4 Vote: I do not like it

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