Блог пользователя pk842

Автор pk842, история, 6 лет назад, По-английски

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

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
6 лет назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

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