GCD SUM
Time limit5sMemory limit1024 MB
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 , , is given. Write a program that handles the following query.
- : Print the sum of the gcd of every contiguous subsequence of . In other words, print .
The sum of the gcd over all contiguous subsequences of is guaranteed not to exceed for every input.
Input
The first line contains an integer .
The second line contains integers .
The third line contains an integer , the number of queries.
Each of the next lines contains two integers and . For the -th query, let and be the values of and . Then and , where is the bitwise XOR operation. If , then . If , is the answer to the -th query.
Output
Print numbers, one per line. The -th line contains the answer to the -th query.
Constraints
- The sum of the gcd over all contiguous subsequences of does not exceed .