ModuloSum

Time limit: 1.6s Memory limit: 128MB Input: Output:

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−1A_0, \dots, A_{N-1} of NN non-negative integers, and QQ queries of the form (Li,Ri,Mi)(L_i, R_i, M_i) for i=0,…,Q−1i = 0, \dots, Q-1. For each query, compute:

(ALi mod Mi)+⋯+(ARi mod Mi)(A_{L_i} \ \text{mod} \ M_i) + \dots + (A_{R_i} \ \text{mod} \ M_i)

For example, if L=2L = 2, R=3R = 3, M=5M = 5, A2=3A_2 = 3, A3=4A_3 = 4, then the answer is (3mod5)+(4mod5)=7(3 \text{mod} 5) + (4 \text{mod} 5) = 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 QQ instructions. It will only be called once per execution by the committee's grader. Note that the array AA is indexed from 00 to N−1N-1, and the arrays LL, RR, MM are indexed from 00 to Q−1Q - 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 NN, QQ, the array AA, then QQ triplets (Li,Ri,Mi)(L_i, R_i, M_i). It will then call solve(N,Q,A,L,R,M)\text{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 0001 \leq N \leq 100 \ 000;
  • 1≤Q≤100 0001 \leq Q \leq 100 \ 000;
  • 0≤Li≤Ri<N0 \leq L_i \leq R_i \lt N;
  • 1≤Mi≤1 000 000 0001 \leq M_i \leq 1 \ 000 \ 000 \ 000;
  • 1≤Ai≤300 0001 \leq A_i \leq 300 \ 000.
# Points Restrictions
1 8 1≤N,Q≤1 0001 \leq N, Q \leq 1 \ 000
2 5 Mi=MjM_i = M_j, 1≤i,j≤Q1 \leq i, j \leq Q
3 9 1≤N⋅Mi≤2 000 0001 \leq N \cdot M_i \leq 2 \ 000 \ 000
4 11 1≤N⋅Mi≤200 000 0001 \leq N \cdot M_i \leq 200 \ 000 \ 000
5 25 10 000≤Mi10 \ 000 \leq M_i
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(5 mod 4)+(14) + (1 mod 4)+(44) + (4 mod 4)=1+1+0=24) = 1 + 1 + 0 = 2.
In the second question, (1(1 mod 3)+(43) + (4 mod 3)+(23) + (2 mod 3)+(33) + (3 mod 3)=1+1+2+0=43) = 1 + 1 + 2 + 0 = 4.
In the third question, (4(4 mod 3)+(23) + (2 mod 3)=1+2=33) = 1 + 2 = 3.
In the fourth question, (5(5 mod 10)+(110) + (1 mod 10)+(410) + (4 mod 10)+(210) + (2 mod 10)+(310) + (3 mod 10)=5+1+4+2+3=1510) = 5 + 1 + 4 + 2 + 3 = 15.

Log in or sign up to be able to send submissions!