 # CDZ14D - Editorial

Practice

Contest

Author: sikander_nsit

Tester: sikander_nsit

Editorialist: sikander_nsit

MEDIUM

### PROBLEM:

In this problem, we had to find the count of numbers between a range[R,L] such that their GCD with N is X.

### EXPLANATION:

It would be the same as finding the count of numbers between L/X and R/X such that their GCD with N/X is 1. This is because if divide two numbers by their GCD then the resulting numbers would be coprime. So the task remains to find the count of numbers between a range which are coprime to N.

Let f© be the number of integers from 1 to A which are coprime to N.
So we can calculate our answer as f®-f(L-1).
f© is A minus the number of integers that are not relatively prime to N.

If N is a prime power p^a, it is easy.
The numbers in the interval [1,C] that are not relatively prime to N are the multiples of p.

Thus

where ⌊x⌋ is the usual “floor” function.

If N has prime power factorization p^a*q^b, where p and q are distinct primes, then g© is the number of integers in [1,C] that are divisible by p or q or both. By Inclusion/Exclusion, we obtain

The reason is that when we add the first two terms above, we are counting twice all the multiples of pq.

If N has prime power factorization p^a*q^b*r^c, the same basic idea works. We get