Mohamed_Saad62's blog

By Mohamed_Saad62, history, 2 years ago, In English

How to find number of values x which achieve GCD(n, x) = 1
n is constant
x <= m
m can be bigger than n
the related problem to this question that I am trying to solve:

https://codeforces.net/contest/1750/problem/D

  • Vote: I like it
  • +8
  • Vote: I do not like it

»
2 years ago, # |
Rev. 2   Vote: I like it +6 Vote: I do not like it

a number $$$x$$$ having $$$gcd(n, x) = 1$$$ means that it doesn't have any common prime factors with $$$n$$$ so we basically need to count numbers from 1 to m not having any common prime factors with $$$n$$$.

We can solve the reverse problem which is counting the numbers from 1 to m having at least one common prime factor with $$$n$$$.

To do so we need to factorise $$$n$$$ then use inclusion-exclusion principle to count such integers. Let the prime factors be $$$[p_1, p_2, ..., p_k]$$$, first we are going to count numbers from 1 to m having $$$p_1$$$ as a prime factor or $$$p_2$$$ or $$$p_3$$$ or ... etc but if a number has both $$$p_1$$$ and $$$p_2$$$ then it was counted twice so we need to subtract all numbers having any combination of these or any other combination of 2 prime factors but it can be noticed again that if a number has $$$p_1$$$, $$$p_2$$$ and $$$p_3$$$ then it is counted 3 times then subtracted 3 times so we didn't count it at all so we need to count and add the numbers having all 3 prime factors together $$$p_1$$$, $$$p_2$$$ and $$$p_3$$$ and any other combination of 3 prime factors then subtract the ones having combination of 4 prime factors and add the ones having combination of 5 prime factors etc for the same reason.