I found this question somewhere online, which went as follows: For each integer x in a given range [L, R], find the count of integers in the given range that are co-prime with x. Constraints: 1 <= L <= R <= 1e5
Example: For L = 2, R = 4, the co-prime pairs would be:
2 -> (2,3) so count = 1 3 -> (3,2), (3,4) so count = 2 4 -> (4,3) so count = 1
Does anyone have an optimal solution for this?
i didnt get it. do you mean just the numbers between L R or there is a array and you want this for ai -> L <= i <= R!? but in these kind of problems there are some solution that works with this -> add edge between them (if you get the numbers or indexes as a node) and then you want the number of edges in range L, R. and you can do this with segment tree. there is a similar problem "Yaroslav and Divisors"
There's no array, I meant all the integers in the given range.
Auto comment: topic has been updated by darth_chef (previous revision, new revision, compare).
You can do some inclusion-exclusion/Mobius stuff to solve this.
Let's fix a particular $$$x$$$. Note that it suffices for us to be able to count the number of integers coprime with $$$x$$$ in the range $$$[1,n]$$$, for some $$$n$$$.
All uses of
/
indicate integer division, by the way.Let's suppose $$$x = 12$$$. Let's initialize a variable
count = n
, since initially there are $$$n$$$ numbers in the given range. A number is definitely not coprime with $$$x$$$ if it has a common prime factor with $$$x$$$. So, let's remove all the multiples of $$$2$$$ and all multiples of $$$3$$$, since those definitely arent coprime with $$$12$$$. We docount -= n/2
andcount -= n/3
. However, when we did this, we subtracted all multiples of $$$6$$$ twice when we really only should have to subtract them once. We account for this by adding back incount += n/6
.Great, let's look at another example. Suppose $$$x = 700$$$. Again, we begin at
count = n
. Then, remove all multiples of $$$2$$$, $$$5$$$, and $$$7$$$, socount -= n/2
,count -= n/5
, andcount -= n/7
. This time, we double-counted the multiples of $$$10$$$, $$$14$$$, and $$$35$$$ (the ones where two distinct prime factors showed up). So, we add back incount += n/10
,count += n/14
, andcount += n/35
. However, now we are under-counting the multiples of $$$70$$$ (where all three prime factors appear), so we do againcount -= n/70
.In general, we get the following inclusion-exclusion for $$$x$$$:
count := n
initially-=
update for all prime divisors of $$$x$$$.+=
update for all divisors of $$$x$$$ that are exactly two distinct prime factors.-=
update for all divisors of $$$x$$$ that are exactly three distinct prime factors.This information is captured succinctly in the Mobius function, which you can Google, but long story short, we have a function $$$\mu$$$ such that $$$\mu(d) = 0$$$ if $$$d$$$ is not squarefree; otherwise, $$$\mu(d)=1$$$ if $$$d$$$ has an even number of prime factors, and $$$\mu(d)=-1$$$ if $$$d$$$ has an odd number of prime factors (this describes the inclusion-exclusion alternating parity seen above).
Then, for our counting problem, the answer is
where $$$f(d) = n/d$$$. As you can see, $$$\mu$$$ is just a compact way of saying whether you should add, subtract, or ignore a given divisor to match the inclusion-exclusion formulation. You can look up how to quickly compute $$$\mu(d)$$$ for all $$$d$$$ from $$$1$$$ to $$$n$$$ using a sieve.
Thus, the complexity to answer for a single value of $$$x$$$ is $$$\tau(x)$$$, the number of divisors of $$$x$$$. The worst case complexity to answer all $$$x$$$ in the range $$$[1,n]$$$ is
I knew Mobius function was to be used, just couldn't figure it out till the end. Brilliant explanation though, thanks.
Hi. I was thinking of using function similar to totient function(see this) but the problem is that it gives wrong answer. I am not able to think why it gives wrong answer though. Can you please help. Help is appriciated:)
How are you using the totient function?
You'll notice the main difference between this problem and the derivation of the totient function is that we are counting all coprime to $$$x$$$ in the range $$$[1,n]$$$; unlike in $$$\varphi(n)$$$, in our problem $$$x$$$ and $$$n$$$ are not necessarily the same.
yes i know that. in the above link in combinatorial proof of totient function ,what if we use prime factors of x instead of prime factors of n. Thanks for replying
Just try it for concrete values and you'll find that it doesn't count what you want to be counting
Thanks man i understood it. Thanks for help
can you please share the problem link?