Please read the new rule regarding the restriction on the use of AI tools. ×

rishichowdhury711's blog

By rishichowdhury711, history, 3 hours ago, In English

Hello Codeforces, I have encountered a difficult GCD problem which I am unable to solve any help would be appreciated.

Problem : you are given two arrays A and B . for every i from [1 , N] , find the number of pairs (i,j) such that gcd(A[i],B[j]) !=1.

1 <= N <= 1e5

1 <= A[i], B[j] <= 1e6

Note: one based indexing is being followed here .

I am unable to come up with any approach to solve this problem,any help would be appreciated !

»
2 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Euler's totient function

  • »
    »
    104 minutes ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    can you elaborate on how to do it?

»
2 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

mobius inversion example

»
2 hours ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

Define $$$dp[v]$$$ as the number of pairs that have a gcd of $$$v$$$. $$$dp[v]$$$ can be calculated as the number of pairs of multiples of $$$v$$$ subtracted from $$$dp[2 \cdot v] + dp[3 \cdot v] + dp[4 \cdot v] \cdots$$$. Then that value is multiplied by $$$-1$$$ to get $$$dp[v]$$$. The answer is the total number of pairs minus $$$dp[1]$$$.

  • »
    »
    103 minutes ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    I dont understand , can you explain the logic once more?

    • »
      »
      »
      86 minutes ago, # ^ |
      Rev. 2   Vote: I like it +2 Vote: I do not like it

      Sure! So we are gonna define $$$dp[v]$$$ as the number of pairs in the array that have a gcd of $$$v$$$. How many pairs could potentially have a gcd of $$$v$$$? That would be all the multiples of $$$v$$$. Of course, for any $$$a,\,b$$$ where $$$a$$$ and $$$b$$$ are both multiples of $$$v$$$, we could have the case where $$$gcd(a, b) \ne v$$$. However, notice that $$$gcd(a, b)$$$ must be a multiple of $$$v$$$. Therefore, if we have $$$dp[m \cdot v]$$$ already calculated, where $$$m \ge 2$$$, we can calculate $$$dp[v]$$$ as $$$\frac{n \cdot (n - 1)}{2} - \sum_{m = 2}^{\infty} dp[m \cdot v]$$$, where $$$n$$$ is the number of elements in the array that are multiples of $$$v$$$. This relationship basically says that $$$dp[v]$$$ is the number of pairs of multiples of $$$v$$$ minus the number of pairs of multiples of $$$v$$$ that have a gcd that is a multiple of $$$v$$$ (and not equal to $$$v$$$). You can see that, upon doing this, all we are left with are the pairs of multiples of $$$v$$$ that have a gcd of $$$v$$$.

      Of course, you don't have to do the summation ad infinitum. Because there will never be a pair $$$(a,\,b)$$$ in the array with $$$gcd(a, b) > A$$$, where $$$A$$$ is the maximum element in the array, we only have to create a $$$dp$$$ array that stores values for elements $$$1..A$$$ inclusive.

      The final answer will be $$$\frac{N \cdot (N - 1)}{2} - dp[1]$$$ — the number of pairs in the entire array with a $$$gcd \ne 1$$$. I hope that this has helped.

      EDIT: I just realized that I misread the problem. In the case of two arrays, you can create an array $$$C = A + B$$$ and run this $$$dp$$$ on $$$C$$$. Then you can run this $$$dp$$$ on each individual array $$$A$$$ and $$$B$$$ and the final answer should be $$$answer_C - answer_A - answer_B$$$.

»
69 minutes ago, # |
  Vote: I like it +1 Vote: I do not like it

isnt this basically ur problem: https://cses.fi/problemset/task/2417