Greatest of the Greatest Common Divisors

시간 제한1초메모리 제한2048 MB

요약
수열과 q개의 구간 질의가 주어질 때, 각 구간 안에서 서로 다른 두 원소의 최대공약수 가운데 가장 큰 값을 구한다.
난이도

어려움10점 중 9점

유형
정수론, 세그먼트 트리, 분할 정복, 동적 계획법
정답자
아직 제출이 없습니다

문제

You are given a sequence of integers and a number of intervals in the sequence. The intervals are specified by their leftmost and rightmost positions. An interval consisting of kk integers has k(k−1)/2k(k - 1)/2 pairs of integers at different positions, which have their greatest common divisors. For each given interval, find the greatest one among such greatest common divisors.

For example, when the sequence is (a_1,…,a_6)=(10,20,30,40,50,60)(a\_1, \dots , a\_6) = (10, 20, 30, 40, 50, 60), and the whole sequence is specified as the interval, the following 1515 pairs of two integers at different positions xx and yy, and their greatest common divisors should be considered.

xx111111111122222222333333444455
yy223344556633445566445566556666
a_xa\_x101010101010101010102020202020202020303030303030404040405050
a_ya\_y202030304040505060603030404050506060404050506060505060606060
gcd⁡(a_x,a_y)\gcd(a\_x,a\_y)101010101010101010101010202010102020101010103030101020201010

The greatest of the greatest common divisors of the 1515 pairs is gcd⁡(30,60)=30\gcd(30, 60) = 30, in this case.

입력

The input consists of a single test case of the following format.

nn

a_1a\_1 ⋯\cdots a_na\_n

qq

l_1l\_1 r_1r\_1

⋮\vdots

l_ql\_q r_qr\_q

The first line contains an integer n, which is the number of integers in the given sequence, satisfying 2≤n≤1052 ≤ n ≤ 10^5. The second line contains nn positive integers a_1a\_1 through a_na\_n specifying the sequence. None of them exceeds 10510^5.

The third line contains a positive integer qq, specifying the number of intervals in the sequence to be considered, which does not exceed 10510^5. It is followed by qq lines, each specifying an interval in the sequence to be considered. The ii-th line of them has two integers, l_il\_i and r_ir\_i (1≤l_i<r_i≤n1 ≤ l\_i < r\_i ≤ n), specifying the interval a_l_ia\_{l\_i} through a_r_ia\_{r\_i} in the sequence.

출력

Output qq lines, the ii-th line of which should have the greatest of the greatest common divisors of all pairs in the interval specified by l_il\_i and r_ir\_i.

예제2

  1. 예제 1

    입력
    6
    10 20 30 40 50 60
    3
    1 6
    2 5
    4 5
    
    예상 출력
    30
    20
    10
    
  2. 예제 2

    입력
    10
    13 2 35 4 13 2 5 1 7 4
    5
    1 4
    4 10
    3 8
    3 9
    1 10
    
    예상 출력
    2
    4
    5
    7
    13