Saad_aa's blog

By Saad_aa, 2 weeks ago, In English

This question is not from Cf or somewhere else. If you found any questions that match it by coincidence, it would be really helpful if you shared them.

I was trying to make a question mixed with pre-computing and gcd. Brute Force may be applied. But I cannot find another way to solve the bigger constraints. Help needed.


Time limit per test: 2 seconds memory limit per test: 265 megabytes

Anish found a magical number in the dark forest. Becoming greedy, he made a lot of numbers with it.

let the magical number be x .

You are given an array that Anish made. Where the i th number is

ai = x * n ; where n is an arbitrary integer ( possibly 0 )

Now, print the maximum possible number Anish could have found. and,

You will be given q queries. Each query consists of 2 indices, i and j. For each query, print the maximum number required to make the numbers within the interval [i, j] in that array.

Input:

The first line contains a single integer t, the number of test cases. (1 <= t <= 1000)

Each test case consists of:

  1. An integer n — size of the array (1 <= n <= 100,000)
  1. n integers a1, a2, …, an (1 <= ai <= 10^9)
  1. An integer q — number of queries (1 <= q <= 100,000)
  1. q lines, each containing two integers i and j . (1 <= i <= j <= n)

Sum of all n over all test cases ≤ 2×10^5

Sum of all q over all test cases ≤ 2×10^5.

Output:

For each test case:

  1. Print a single integer: maximum possible number Anish could have found . (for the whole array)
  1. Next q lines: for each query, print the maximum possible number to make the numbers in that subarray [i, j]

Example:

Input:

1
4
7 15 30 39
3
2 3
2 4
1 3

Output:

1
15
3
1
  • Vote: I like it
  • 0
  • Vote: I do not like it

»
2 weeks ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

If I'm understanding the problem correctly, the optimal answer for each range is the gcd of it. So, you can build a segment tree to solve it in Q*log N time. I don't understand the first 1 in the output though, is that a mistake?

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

    I think the first 1 is just the answer for the whole array, so its 1 since the gcd is 1.

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

    Since gcd works in O(log(A)), the actual time compexity would be O((N+Q)log(N)log(A)) But since there are no update queries, you can use a sparse table instead, making the solution O(Nlog(N)log(A)+Qlog(A)) (you can do the "min/max" query optimization, since gcd(x,x)==x)

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

      Yes, for this sparse table would be more optimal, though are there any problems where segment tree would get tle?

      • »
        »
        »
        »
        2 weeks ago, hide # ^ |
        Rev. 2  
        Vote: I like it 0 Vote: I do not like it

        In this particular case both should pass the time limit, but according to this test someone made:

        For n,q up to 1e6, building+querying using a sparse table is ~2.2-4.8 times faster depending on the segment tree implementation, which when you multiply this by log(A)~=30 in this problem, this becomes a speedup by ~66 to ~144 times!

        Answering your question — no, I don't think I have seen any problem that require using a sparse table, thou I think if you searched hard enough you would find one, but this specific problem is one that if the limits were made ~2-5 times larger, a segment tree (or at least the reqursive version) could've got a tle.

»
2 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Also, 474F - Ant colony is a kinda similar but more difficult problem you might want to check out.