GCDCNT - Editorial




Author: Vipul Sharma

Tester: Triveni Mahatha

Editorialist: Adarsh Kumar




Inclusion exclusion, Ordered Set


You are given an array with N elements. You need to support two types of operations on this array. First one is point update. For the second operation, you will be given query range l,r and an another number G as input, you need to find number of elements that are co-prime to G in the given range.


Let’s try to find the number of integers co-prime to G in the given query range, represented by ANS(G,L,R). Say,

$G = p_1^{a_1} . p_2^{a_2} . ..... p_q^{a_q}$

We will be using inclusion-exclusion principle to find the required expression. Lets represent number of integers divisible by x from the range L,R as F(x,L,R). Then, we can write:

ANS(G,L,R) = F(1,L,R) - \sum \limits_{i=1}^q F(p_i,L,R) \\ + \sum \limits_{i=1}^q \sum \limits_{j=i+1}^q F(p_i.p_j,L,R) \\ - (\text{3 at a time}) \\ + (\text{4 at a time}) \\ - ... + (-1)^q.F(\prod \limits_{i=1}^q p_i,L,R)

Number of terms in expansion of ANS(G,L,R) is 2^q, where q is number of distinct prime factors of G. Also, notice that first argument to function F is always a square free divisor of G. If we can answer F(x,L,R) in logarithmic complexity, we can have a solution that will work in O(2^q.logN) for each query.

We will maintain an ordered set for each of the number 1 \le x \le 10^5. For all A[i] (1 \le i \le N), we will iterate over its square free divisors and insert this index i in the ordered set of corresponding divisors. Since, a number upto 10^5 can have at-max 64 square free divisors, this will take a time of O(K.N.logN) where K=64. You can answer F(x,L,R) in O(logN) time by querying over ordered set corresponding to x. We can also support the update in similar fashion. Say, current update operation is to change A[x] to y. First, we will iterate over square free divisors of A[x] and erase index x from the ordered set of corresponding divisors. Then, we will iterate over square free divisors of y and insert index x into the ordered set of corresponding divisors. Finally, make A[x]=y.


This solution requires knowledge of mobius inversion. You can refer to some of these awesome links(L1, L2), in case you are not familiar with the same. Lets represent unit function by e(n). Then, we can write:

ANS(G,L,R) = \sum \limits_{i=L}^R e(gcd(G,A[i]))
\Rightarrow ANS(G,L,R) = \sum \limits_{i=L}^R \text{ } \sum \limits_{d|G,d|A[i]} \mu (d)
\Rightarrow ANS(G,L,R) = \sum \limits_{d|G} \mu (d).F(d,L,R)$</center>

Notice that above formula is same as the one which is derived using inclusion-exclusion principle. Another informal way of looking at this is, mobius function is the sign in the inclusion-exclusion principle.

Later part of the problem can be solved in similar fashion as above. There can be many different approach for this part too. Feel free to discuss them in comments if you find them interesting.

Time Complexity:



Setter’s solution

Tester’s solution

1 Like

For calculating F(x,L,R) we can use sqrt decomposition also. Complexity would be O(n*sqrt(n)*64)

1 Like

setter’s solution is showing ‘Access Denied’ please fix it.

can you please explain your approach.

Great Question on Mobius! I have used sqrt decompostion for calculating F(x,L,R). If anyone used segment trees,do explain!

I used dynamic segment trees, here’s my code https://www.codechef.com/viewsolution/17818958. Though this is really overkill! :smiley:

Hello Community, Can anyone please help me optimize my solution…I am getting TLE in a single Test Case…


I tried a lot but couldn't convert single TLE test case to AC.

  [1]: https://www.codechef.com/viewsolution/17819588

I used AVL trees for this.
Explanation is given in this solution:


Dynamic Segment Trees Solution :smiley:

Here is the link


Could u explain your solution?

My Square root decomposition solution.
Some observations,
as N=50,000, so square root (N) = 225 (approx)
So No. of blocks = Block Size = 225
In each block, I am storing all the frequencies of square free divisors, by an array of size 100,000

Total space used = 225100,000 = 2.2510^7. This much memory can be allocated easily, by declaring a global 2D array.

Lets see query and Update Time

Update takes O(1) i.e constant time because you have to remove frequencies of old number and add frequencies of new number.

For example, suppose the earlier number of 10 and it is updated to 30.

10–> [ 2, 5, 10] remove freq of these numbers by 1

30–>[ 2, 3, 5, 6, 15, 10, 30] add freq of these numbers by 1.

If anyone has doubt about square free divisor please feel free to ask.

Now go come back to query

We find all square free divisor of G, maximum 64 numbers. Search all the blocks which contain L, R for these divisors.

Complexity: O(sqrt(N)*64)

Total complexity of queries O(N*sqrt(N)*64) ~ 10^7.

My first question of Square root decomposition, it was great when I got all ACs.

Here is link to my solution


I tried to solve it by Map, unordered_map and segment tree. Took me 75 TLES to solve but at the end, it was worth it.


I tried to solve it using map, but map is too slow, even unordered map, because of rehashing or other overheads. I tried for several days using map, no result. Use an array for O(1) look up and use square root decomposition.

Hello Community, I would like to share something with you all…I had been using vector<unordered_map<int,int> > bit; for my fenwick tree…And probably I had made more than 30 submissions since yesterday…But I was getting TLE in single Test Case (code in above comment)…Then I used Hash Map from Mike Mirzayanov’s github repo…

Not only was it time efficient but also memory efficient and It passed with 0.44s with suprise memory usage of just 18.2 MB.
Here is my code:


I used policy based data structure in c++… and i think that will do in logn… link to my soln is…
ask any questions if u want…

Great question but it was totally similar to the one in codeforces discussion which result in so many AC’s. http://codeforces.com/blog/entry/54419

See this link here everything was explained how to get AC including link to policy based data structures.

I have used inclusion and exclusion that uses frequency of the factor of G between L to R.
Since maximum value in an array can be 10^5 so we will be building 10^5 segment trees where segment tree i can tell u the frequency of a no i between L and R.
Of course this will lead to MLE.

So we can use dynamic segment trees to solve this problem HOW?
For each factor x among 2^6 factors(in worst case) of A[i] we will update segment tree x on its ith index.And this can be done by allocating log2(50000) nodes in the worst case and during this process if the node is already created we will be using it else create a new one.

In worst case there are a total of 2*50000 nos (including updates) that will be needing 10^5 * 2^6 * log2(50000) = 99901699 nodes that is well within the Memory Limit.

We have solved the problem in O( 2^6 * Q * log2(N) ) time and O( 9*10^7 ) memory

@nilmadhab1 what is square free divisor?

@l_returns i also used the policy-based data structure still, i keep getting WA. Can you please help me out?


  [1]: https://www.codechef.com/viewsolution/17830697