Problem Statement
We have a sequence of k numbers: d0,d1,...,dk−1.
Process the following q queries in order:
- The i-th query contains three integers ni, xi, and mi. Let a0,a1,...,ani−1 be the following sequence of ni numbers: \[ \begin{aligned} a_j = \begin{cases} x_i & ( j = 0 ) \\ a_{j - 1} + d_{(j - 1)~\textrm{mod}~k} & ( 0 < j \leq n_i - 1 ) \end{cases}\end{aligned} \] Print the number of j (0leqj<ni−1) such that $(a_j~\\textrm{mod}~m_i) < (a_{j + 1}~\\textrm{mod}~m_i)$.
Here (y textrmmod z) denotes the remainder of y divided by z, for two integers y and z (z>0).
Constraints
- All values in input are integers.
- 1leqk,qleq5000
- 0leqdileq109
- 2leqnileq109
- 0leqxileq109
- 2leqmileq109
Input is given from Standard Input in the following format:
k q
d0 d1 ... dk−1
n1 x1 m1
n2 x2 m2
:
nq xq mq
Output
Print q lines.
The i-th line should contain the response to the i-th query.
3 1
3 1 4
5 3 2
Sample Output 1
1
For the first query, the sequence {aj} will be 3,6,7,11,14.
- (a0 textrmmod 2)>(a1 textrmmod 2)
- (a1 textrmmod 2)<(a2 textrmmod 2)
- (a2 textrmmod 2)=(a3 textrmmod 2)
- (a3 textrmmod 2)>(a4 textrmmod 2)
Thus, the response to this query should be 1.
7 3
27 18 28 18 28 46 1000000000
1000000000 1 7
1000000000 2 10
1000000000 3 12
Sample Output 2
224489796
214285714
559523809