Under construction

Still being built. Things may change

GCD SUM

Time limit5sMemory limit1024 MB

Summary
Given an array, answer online range queries that ask for the sum of gcd over every contiguous subarray inside the range [l, r], with l and r XOR-encoded by the previous answer.
Level

Medium7 of 10

Topics
Segment tree, Number theory, Divide and conquer
Solved
No attempts yet

Problem

A sequence of length NN, a1,a2,⋯ ,aNa_{1}, a_{2}, \cdots, a_{N}, is given. Write a program that handles the following query.

  • ll rr: Print the sum of the gcd of every contiguous subsequence of al,al+1,⋯ ,ara_{l}, a_{l+1}, \cdots, a_{r}. In other words, print ∑i=lr∑j=irgcd⁡(ai,ai+1,⋯ ,aj)\sum_{i=l}^{r}\sum_{j=i}^{r}\gcd(a_{i}, a_{i+1}, \cdots, a_{j}).

The sum of the gcd over all contiguous subsequences of aa is guaranteed not to exceed 101810^{18} for every input.

Input

The first line contains an integer NN.

The second line contains NN integers a1,a2,⋯ ,aNa_{1}, a_{2}, \cdots, a_{N}.

The third line contains an integer QQ, the number of queries.

Each of the next QQ lines contains two integers xix_{i} and yiy_{i}. For the ii-th query, let lil_{i} and rir_{i} be the values of ll and rr. Then li=xi⊕ansi−1l_{i} = x_{i} \oplus ans_{i-1} and ri=yi⊕ansi−1r_{i} = y_{i} \oplus ans_{i-1}, where ⊕\oplus is the bitwise XOR operation. If i=0i=0, then ans0=0ans_{0}=0. If i≥1i \ge 1, ansians_{i} is the answer to the ii-th query.

Output

Print QQ numbers, one per line. The ii-th line contains the answer to the ii-th query.

Constraints

  • 1≤N≤500 0001 \le N \le 500\,000
  • 1≤ai≤1091 \le a_{i} \le 10^{9} (1≤i≤N)(1 \le i \le N)
  • 1≤Q≤100 0001 \le Q \le 100\,000
  • 0≤xi,yi<2600 \le x_{i}, y_{i} < 2^{60} (1≤i≤Q)(1 \le i \le Q)
  • The sum of the gcd over all contiguous subsequences of aa does not exceed 101810^{18}.
  • 1≤li≤ri≤N1 \le l_i \le r_i \le N (1≤i≤Q)(1 \le i \le Q)

Examples1

  1. Example 1

    Input
    5
    3 6 4 8 2
    3
    1 5
    40 40
    6 0
    
    Expected output
    43
    4
    26