Блог пользователя vidit_123

Автор vidit_123, история, 7 лет назад, По-английски

Given an array,how to solve queries (10^5) of type Q:Value,L,R where value is a number and we need to report the count of all the numbers in the range [L,R] of the array such that gcd(Value,A[i])>1 where L<=i<=R. Given ARRAY SIZE :- 10^5 Each number 1<= A[i] <=10^5 TL = 1 Sec

  • Проголосовать: нравится
  • +16
  • Проголосовать: не нравится

»
7 лет назад, # |
  Проголосовать: нравится +11 Проголосовать: не нравится

What are the constraints for the number of elements and the values (ai)?

»
7 лет назад, # |
Rev. 4   Проголосовать: нравится +27 Проголосовать: не нравится

I'd like to know more details (time limit, amount of numbers in array and constraints on the numbers), but anyway, I'll share the solution that came to mind.

  • No number up to 109 has more than 1344 divisors, so we can find divisors of every element of array and store a list of positions that divide every number. This may or may not fit in memory or time, depending on the constraints. This step is , where M is the maximum value an array element can take.
  • Now for each query, make a list of the primes that divide Value. There are at most 9 such primes, because the product of the first 10 primes is  $>10^9$. Now for every possible mask of primes, check how many positions of range [L, R] divide their product with a binary search and use inclusion-exclusion principle. This process is O(29  *  logN) for each query, making the whole program O(Q * 29 * logN). This, again, may or may not fit in time depending on the constraints.
»
6 лет назад, # |
Rev. 2   Проголосовать: нравится +6 Проголосовать: не нравится

this problem is from codechef march long challange..

it still running dude, don't cheat

»
6 лет назад, # |
  Проголосовать: нравится -44 Проголосовать: не нравится

At first generate a list of primes that divide a number, for all number till M, the maximum number. This takes .

Now, for each number of the array ai, lets say the primes p1, p2, ..., pk divide that number, you can get this quickly from step 1. Go through all subsets of these primes. If a subset is pi1, pi2, ..., pir, then insert i to list pospi1·pi2... pir. This step takes O(26n).

Now when you receive a query X, take the primes p1, p2, ..., pk, that divides X, from the list generated at first. Now go through all of its subsets. Let the chosen subset be pi1, pi2, ..., pir, then count how many times a number from [l, r] appears in pospi1·pi2... pir. It can be done with two binary searches. If r is odd then add the count to answer, else subtract this count. It can be proven that this gives the answer, by inclusion-exclusion principal. Basically this counts number of numbers with at least one prime common with the query X. This step takes

»
6 лет назад, # |
  Проголосовать: нравится -7 Проголосовать: не нравится

Why are you upvoting this post? It is cheating, and this post should be deleted immediately, I think because it violates CodeChef rules and principles of the competitive programming.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +1 Проголосовать: не нравится

    This blog post was created 6 months ago. However the current interest in this post can be attributed to CodeChef's problem.

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +23 Проголосовать: не нравится

    This blog has nothing to do with CodeChef, the thing should be deleted is CodeChef problem. CodeChef should really care about problems of long challenge, because in 10 days even we can find code of problem from internet.

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится +7 Проголосовать: не нравится

      Sorry, I missed this fact. So it's good that later comments are downvoted and unclarified.

»
6 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

You also can do it with bitset. numbers less than 10^5 has less than 7 prime divisors. So for every primes less than 1e5 set the bit of positions of those numbers which are divided by.

GCD(x, a[i]) has no common prime divisors If their GCD is 1. you can travel through prime divisors and check how many numbers you can divide in the given range. then the ans might be (r-l+1- NumberofSetBit).

you can solve it easily than other technique.

  • »
    »
    5 лет назад, # ^ |
      Проголосовать: нравится -9 Проголосовать: не нравится

    Can you please explain more on this line "So for every primes less than 1e5 set the bit of positions of those numbers which are divided by." If possible, please post the link to code of this approach.