Vasiljko's blog

By Vasiljko, history, 23 months ago, , Given array of N elements. Find number of coprime pairs.

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

TL: 0.6s
ML: 64MB Comments (7)
 » 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
 » 23 months ago, # | ← Rev. 2 →   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.
 » 23 months ago, # | ← Rev. 3 →   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.
•  » » Hvala obojici :)
•  » » Can I get the aforementioned problem in English? I really liked that concept. Thank You very much.
•  » » » 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.
•  » » can you please provide that solution