### Vasiljko's blog

By Vasiljko, history, 4 years ago,

Given array of N elements. Find number of coprime pairs.

1 <= N <= 30000
1 <= A[i] <= 10^8

TL: 0.6s
ML: 64MB

• +5

 » 4 years ago, # |   0 Hmmm, I don't really know. But, I'll let you know as soon as I come up with a solution! (If I come up with one) :D
 » 4 years ago, # | ← Rev. 2 →   +9 Preprocess all the primes smaller than 108 by Eratosthenes Sieve.Now, for all the numbers ai,  make a vector of it's prime divisors (not counting the multiplicity). We will actually find the numbers of non-coprime pairs of numbers and subtract it from n * (n - 1) / 2Suppose that, for some i,  we have ai = p1 * p2 * ... * pk. (We, again, disregard multiplicity). Note that the time allows us to go through all subsets of {p1, p2, ..., pk} for all i. Build two maps, cnt[] and sgn[]. cnt[X] tells us how many times subset with product equal to X appears in all elements of array a[], while sgn[X] tells us how many elements the subset has.And for the finale just use the Inclusion — Exclusion Principle.
•  » » 13 months ago, # ^ | ← Rev. 2 →   0 Can you please explain the time complexity part? I couldn't understand how the time is sufficient here. Since the primes vector size is ~1200(number of primes in [1,10000]), won't it take time to iterate through all these primes, for every element (n=30000, so 30000*1200 in the worst case), to get its vector of prime divisors? I am not aware of any other optimized approach here to get the vector. Any further explanation regarding what I am missing would be extremely helpful.
•  » » » 4 months ago, # ^ |   0 Since a[i] <= 10^8 and the smallest prime number is 2.Consider a[i] has some x number of prime factors(including multiplicity) then a[i] >= 2^x. Using a[i] <= 10^8, we can get maximum value of x can be around 25-30 and hence it satisfies the given time constraints.
•  » » » » 4 months ago, # ^ |   0 Got It. Thanks!
•  » » 7 months ago, # ^ |   0 Can anyone show the implementation part ?I am unable to put the Inclusion Exclusion principle with the rest of solution.
•  » » » 5 weeks ago, # ^ | ← Rev. 2 →   +6 Maybe there is another ways, mine is here: (idk is it late to answer) int f(int n, vector &arr){ for(int b = 0; b < (1<
•  » » » » 5 weeks ago, # ^ |   0 No, it's not late. Tysm.
•  » » 7 months ago, # ^ |   0 But how will you preprocess 10^8 items? won't there be space issues?
 » 4 years ago, # | ← Rev. 3 →   +6 I'm the author of this task :) It appeared on the national programming competition in Serbia this year.Problem 6 (In Serbian)The intended solution for 100 points is what The_Wolfpack has written, with a small correction — you only need to process primes up to 104.
•  » » 4 years ago, # ^ |   0 Hvala obojici :)
•  » » 3 years ago, # ^ |   0 Can I get the aforementioned problem in English? I really liked that concept. Thank You very much.
•  » » » 3 years ago, # ^ |   0 It's identical to the problem posted in the blog: Find the number of pairs i, j (1 ≤ i < j ≤ n) such that gcd(ai, aj) = 1. Constraints are: 1 ≤ N ≤ 30000, 1 ≤ ai ≤ 108. TL = 0.6s. Problem statements are traditionally long and full of unnecessary details and stories.
•  » » » » 5 weeks ago, # ^ |   0 Can you explain in brief how can we apply inclusion-exclusion after calculating above mentioned things by The_Wolfpack?
•  » » » » » 5 weeks ago, # ^ | ← Rev. 2 →   0 If we have stored X = p1*p2, then we would also have stored X=p1 and X=p2 in our maps (because we are traversing over all subsets of distinct primes for each element of the array). Now, all those pairs of elements which have either p1 or p2 or both p1 and p2 common in their prime factorisation, they can't be co-primes. So, now we can use inclusion-exclusion to find all such pairs. int allNonCoprimes = 0; for (auto itr : sgn) { int mul = itr.first, x = itr.second, y = cnt[itr.first]; //If 4 elements of the array share this product, then 4*3 are //the number of pairs which can't be co-primes (contains //duplicates, 1-2 & 2-1 are counted as distinct) int toRemove = y * (y - 1); if (x % 2 == 0) allNonCoprimes -= toRemove; else allNonCoprimes += toRemove; } 
•  » » » » » » 13 days ago, # ^ |   0 Thank a lot for the info! I was able to solve this codechef problem from your idea. It's not the same but asks us to find the number of triplets.But there is one mistake in your code. We subtract toRemove if x is odd and add when it's even.
•  » » » » » » » 13 days ago, # ^ |   0 Umm, why do we have to subtract toRemove if x is odd? The code just implements the Incl-Excl directly.
•  » » 2 years ago, # ^ |   0 can you please provide that solution
•  » » 4 months ago, # ^ |   0 can you tell that 'small correction' so that we only need to process upto $10^4$. I mean let's say I have number whose prime divisor are >$10^4$ then how we can check it?
•  » » » 4 months ago, # ^ |   0 if there are any prime divisors which are greater than $10^4$ then the quotient will be obviously less than $10^4$. And the quotient will have prime factors which are already calculated. So you just divide the number again and again by those prime factors. If you end up with a number greater than 1 then that is also a prime factor.
 » 16 months ago, # |   +26 I think that there exist a simpler solution, which allow us to calculate this value straightforwardly. We want to maintain array $DP[x]$ — number of pairs $(a, b)$, that $GCD(a, b) = x$.Let's iterate over all possible results of $GCD$ starting from the greatest element in array (lets call it $M$).To count $DP[x]$ we only have to consider elements of array that are multiple of $x$. If $f(x)$ is the number of all pairs we can obtain from the set of $x$ elements, and $q(x)$ is number of elements that are multiple of $x$, then $DP[x] = f(q(x)) -\sum_{k = 2}^{floor(\frac{M}{x})} DP[k \cdot x]$The result is of course $DP[1]$Overall complexity: $O(M\log{M})$
•  » » 16 months ago, # ^ |   -32 Can you please explain your logic with some example? I mean how will it manipulates in code.. Thanks in Advance :D
•  » » 16 months ago, # ^ |   -7 It's simpler but unfortunately it won't work here because $M = 10^8$.
 » 16 months ago, # | ← Rev. 2 →   -13 I have this approach, but I dont know is this a good way. Correct me if I am wrongI think you can calculate the number of coprimes to n aka ϕ(n)we can calculate ϕ(1 -> n) in O(n log log n) Euler totient functionvoid genPhi(int n) { vi phi(n + 1); for (int i = 0; i <= n; i++) phi[i] = i; for (int i = 2; i <= n; i++) if (phi[i] == i) for (int j = i; j <= n; j += i) phi[j] -= phi[j] / i; } After that we just need to calculate the sum of ϕ(1) + ϕ(2) + ... + ϕ(n)If you need to solve for queies. Using prefix sum array can help you answer the problem in O(1) after O(n log log n) precalculating
•  » » 16 months ago, # ^ |   -9 I think you should read the problem again carefully, particularly this comment
•  » » » 16 months ago, # ^ |   -13 I think the problem asking about find number of pair(i, j) where 1 ≤ i, j ≤ n and gcd(i, j) = 1.So I calculate ϕ(j) for every 1 ≤ j ≤ n. Then I will know the number i in range [1..j] that gcd(i, j) = 1Then I calculate the sum of all ϕ(j) in range [1..n].Is my approach correct problemsetter ?
•  » » » 16 months ago, # ^ | ← Rev. 5 →   -13 Ah, number of pair(ai, aj). Sorry for my reading the problem carelesslySo how about this new approachFor each pair (i, j) that 1 ≤ i ≤ j ≤ nI increase the result whenever gcd(ai, aj) = 1Time Complexity: O(n ^ 2 log (n ^ 2))
 » 16 months ago, # | ← Rev. 2 →   -18 2 numbers are co-prime if their SPFs are different. Preprocess all the SPFs and then store the SPFs of the numbers in the array in map. Now you will have the numbers whose SPFs is the same. Let ans = n*(n+1)/2 (n = size of the array), now you could iterate through the map and subtract the sizeofmapC2 from the answer. Edit: the above idea of 2 numbers being co-prime is just an observation. Point it if i'm wrong
•  » » 16 months ago, # ^ |   0 False: 10, 15
•  » » 16 months ago, # ^ |   +5 What the hell is SPF? Googling doesn't give much after filtering out sunscreen and sender policy framework.
•  » » » 16 months ago, # ^ |   0 smallest prime factor. ignore it though i'm very very wrong in my solution ,i didn't realize it.