Task
The following problem was passed down through the generations in the InfO(1)Cup Kingdom - can you solve it?
You are given a sequence A0,…,AN−1 of N non-negative integers, and Q queries of the form (Li,Ri,Mi) for i=0,…,Q−1. For each query, compute:
(ALi mod Mi)+⋯+(ARi mod Mi)For example, if L=2, R=3, M=5, A2=3, A3=4, then the answer is (3mod5)+(4mod5)=7.
Implementation details
You should implement the following function:
std::vector<long long> solve(
int N, int Q,
std::vector<int> A,
std::vector<int> L,
std::vector<int> R,
std::vector<int> M
);
This function should return the answer for the given Q instructions. It will only be called once per execution by the committee's grader. Note that the array A is indexed from 0 to N−1, and the arrays L, R, M are indexed from 0 to Q−1.
Remember to include the header modulosum.h! Also, remember that you should not implement the main function, only solve.
Sample grader behaviour
The sample grader will read N, Q, the array A, then Q triplets (Li,Ri,Mi). It will then call solve(N,Q,A,L,R,M), and output the returned values to standard output, one on a line. The input/output files below work for this grader.
Constraints and clarifications
- 1≤N≤100 000;
- 1≤Q≤100 000;
- 0≤Li≤Ri<N;
- 1≤Mi≤1 000 000 000;
- 1≤Ai≤300 000.
| # |
Points |
Restrictions |
| 1 |
8 |
1≤N,Q≤1 000 |
| 2 |
5 |
Mi=Mj, 1≤i,j≤Q |
| 3 |
9 |
1≤N⋅Mi≤2 000 000 |
| 4 |
11 |
1≤N⋅Mi≤200 000 000 |
| 5 |
25 |
10 000≤Mi |
| 6 |
42 |
No further restrictions. |
Example 1
stdin
5 4
5 1 4 2 3
0 2 4
1 4 3
2 3 3
0 4 10
stdout
2
4
3
15
Explanation
In the first question, (5 mod 4)+(1 mod 4)+(4 mod 4)=1+1+0=2.
In the second question, (1 mod 3)+(4 mod 3)+(2 mod 3)+(3 mod 3)=1+1+2+0=4.
In the third question, (4 mod 3)+(2 mod 3)=1+2=3.
In the fourth question, (5 mod 10)+(1 mod 10)+(4 mod 10)+(2 mod 10)+(3 mod 10)=5+1+4+2+3=15.