Under construction

Still being built. Things may change

GCD and K-th Query

Time limit1sMemory limit1024 MB

Summary
Given a sequence of N positive integers, answer queries for the K-th smallest gcd over all subarrays inside a range [L, R].
Level

Hard9 of 10

Topics
Binary search, Number theory, Divide and conquer
Solved
No attempts yet

Problem

A sequence of positive integers A1,A2,…,ANA_1, A_2, \dots, A_N of length NN is given. Write a program that processes the following queries.

  • LL RR KK: For every ordered pair (i,j)(i, j) with L≤i≤j≤RL \leq i \leq j \leq R, compute gcd⁡(Ai,Ai+1,…,Aj)\gcd(A_i, A_{i+1}, \dots, A_j). Sort these values in non-decreasing order and output the KK-th value.

Here gcd⁡(Ai)=Ai\gcd(A_i) = A_i.

For the sequence (1,2,4)(1, 2, 4) with L=1L = 1 and R=3R = 3, the possible gcd values are (1,1,1,2,2,4)(1, 1, 1, 2, 2, 4). If K=4K=4, the answer is 22.

Input

The first line contains the length NN of the sequence. (1≤N≤200001 \leq N \leq 20000)

The second line contains NN integers A1,A2,…,ANA_1, A_2, \dots, A_N separated by spaces. (1≤Ai≤5000001 \leq A_i \leq 500000)

The third line contains the number of queries QQ. (1≤Q≤1000001 \leq Q \leq 100000)

From the fourth line, each of the next QQ lines contains one query LL, RR, KK. (1≤L≤R≤N1 \leq L \leq R \leq N, 1≤K≤(R−L+1)(R−L+2)/21 \leq K \leq (R-L+1)(R-L+2)/2)

Output

For each query, print the result on its own line.

Examples1

  1. Example 1

    Input
    8
    1 2 4 8 72 27 9 3
    10
    1 1 1
    2 2 1
    1 2 3
    2 7 3
    2 7 10
    2 7 15
    2 7 21
    1 8 1
    1 8 18
    1 8 36
    
    Expected output
    1
    2
    2
    1
    2
    8
    72
    1
    2
    72