#P15741. [JAG 2024 Summer Camp #2] Max Sum of GCD

[JAG 2024 Summer Camp #2] Max Sum of GCD

题目描述

For a sequence of positive integers X=(X1,X2,,XM)\boldsymbol{X} = (X_1, X_2, \ldots, X_M) where M2M \geq 2, let f(X)f(\boldsymbol{X}) be the answer to the following problem:

  • Among the MM positive integers X1,X2,,XMX_1, X_2, \ldots, X_M, paint at least one and at most M1M - 1 of them red, and paint the rest blue. Let RR be the greatest common divisor (GCD) of the integers painted red, and BB be the GCD of the integers painted blue. Find the maximum possible value of R+BR + B.

You are given a sequence of NN positive integers A=(A1,A2,,AN)\boldsymbol{A} = (A_1, A_2, \ldots, A_N). You will also be given QQ queries. For each query, you will be given two integers lil_i and rir_i such that 1li<riN1 \leq l_i < r_i \leq N. For each query, let $\boldsymbol{X} = (A_{l_i}, A_{l_i + 1}, \ldots, A_{r_i})$, and find f(X)f(\boldsymbol{X}).

输入格式

The input is given in the following format:

$$\begin{aligned} &N \\ &A_1 \ A_2 \ \ldots \ A_N \\ &Q \\ &l_1 \ r_1 \\ &l_2 \ r_2 \\ &\vdots \\ &l_Q \ r_Q \end{aligned} $$
  • 2N200,0002 \leq N \leq 200,000
  • 1Ai10181 \leq A_i \leq 10^{18} for all 1iN1 \leq i \leq N
  • 1Q200,0001 \leq Q \leq 200,000
  • 1li<riN1 \leq l_i < r_i \leq N for all 1iQ1 \leq i \leq Q

输出格式

Print QQ lines. On the ii-th line (1iQ1 \leq i \leq Q), output the answer to the ii-th query.

6
3 6 2 5 4 4
3
1 3
4 6
1 6
7
9
7