Arithmetic Sequence and Queries
Time limit2sMemory limit512 MB
Given an arithmetic sequence with first term a and difference d, answer queries for the sum or the gcd of a contiguous range of terms.
- Level
Medium6 of 10
- Topics
- Math, Number theory, Prefix sum, Implementation
- Solved
- No attempts yet
Problem
An arithmetic sequence is a sequence in which the difference between consecutive terms is constant. For two consecutive terms, the value obtained by subtracting the earlier term from the later term is called the common difference. You are given an arithmetic sequence whose first term is and whose common difference is . Let be the -th element of the sequence. Write a program that processes the following queries.
1 l r: Print the sum of .2 l r: Print the greatest common divisor of . This is the largest positive integer among the common divisors of .
Input
The first line gives the first term and the common difference of the sequence.
The second line gives the number of queries .
Each of the next lines gives one query.
Output
For each query, print the answer on its own line.
Constraints
- ,