Vasiljko's blog

By Vasiljko, history, 12 months ago, In English,

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

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

TL: 0.6s
ML: 64MB

 
 
 
 
  • Vote: I like it  
  • +5
  • Vote: I do not like it  

»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
12 months ago, # |
Rev. 2   Vote: I like it +9 Vote: I do not like it

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) / 2

Suppose 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.

»
12 months ago, # |
Rev. 3   Vote: I like it +6 Vote: I do not like it

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.

  • »
    »
    12 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Hvala obojici :)

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Can I get the aforementioned problem in English? I really liked that concept. Thank You very much.

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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.