AtCoder Beginner Contest 156 - Modularness

We have a sequence of k numbers: d_0,d_1,...,d_{k - 1}.
Process the following q queries in order:
 - The i-th query contains three integers n_i, x_i, and m_i.
Let a_0,a_1,...,a_{n_i - 1} be the following sequence of n_i numbers: \begin{eqnarray} 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{eqnarray}
Print the number of j~(0 \leq j < n_i - 1) such that (a_j~\textrm{mod}~m_i) < (a_{j + 1}~\textrm{mod}~m_i).
Here (y~\textrm{mod}~z) denotes the remainder of y divided by z, for two integers y and z~(z > 0).

-----Constraints-----
 - All values in input are integers.
 - 1 \leq k, q \leq 5000
 - 0 \leq d_i \leq 10^9
 - 2 \leq n_i \leq 10^9
 - 0 \leq x_i \leq 10^9
 - 2 \leq m_i \leq 10^9

-----Input-----
Input is given from Standard Input in the following format:
k q
d_0 d_1 ... d_{k - 1}
n_1 x_1 m_1
n_2 x_2 m_2
:
n_q x_q m_q

-----Output-----
Print q lines.
The i-th line should contain the response to the i-th query.

-----Sample Input-----
3 1
3 1 4
5 3 2

-----Sample Output-----
1

For the first query, the sequence {a_j} will be 3,6,7,11,14.
 - (a_0~\textrm{mod}~2) > (a_1~\textrm{mod}~2)
 - (a_1~\textrm{mod}~2) < (a_2~\textrm{mod}~2)
 - (a_2~\textrm{mod}~2) = (a_3~\textrm{mod}~2)
 - (a_3~\textrm{mod}~2) > (a_4~\textrm{mod}~2)
Thus, the response to this query should be 1.

Difficulty: MEDIUM_HARD

Starter code:

Solutions:
def i1():
	return int(input())

def i2():
	return [int(i) for i in input().split()]
[k, q] = i2()
d = i2()
for i in range(q):
	[n, x, m] = i2()
	di = []
	dz = []
	for i in d:
		di.append(i % m)
		if i % m == 0:
			dz.append(1)
		else:
			dz.append(0)
	x = (n - 1) // k * sum(di[:k]) + x % m
	if (n - 1) % k:
		x += sum(di[:(n - 1) % k])
	ans = n - 1 - x // m - (n - 1) // k * sum(dz[:k])
	if (n - 1) % k:
		ans -= sum(dz[:(n - 1) % k])
	print(ans)


(k, q) = map(int, input().split())
d = list(map(int, input().split()))
nxm = [list(map(int, input().split())) for i in range(q)]
for (n, x, m) in nxm:
	prev = x
	ans = 0
	cur = 0
	for i in range(k):
		prev += d[i] % m * ((n + k - i - 2) // k)
		if d[i] % m == 0:
			cur += (n + k - i - 2) // k
	print(n - prev // m + x // m - cur - 1)


(k, q) = map(int, input().split())
d = list(map(int, input().split()))
for i in range(q):
	(n, x, m) = map(int, input().split())
	l = [i % m if i % m != 0 else m for i in d]
	print(n - 1 - (x + (n - 1) // k * sum(l) + sum(l[:(n - 1) % k])) // m + x // m)


import sys
input = sys.stdin.readline
(k, q) = map(int, input().split())
D = [int(i) for i in input().split()]
for _ in range(q):
	(n, x, m) = map(int, input().split())
	DD = [d % m for d in D]
	x %= m
	ans = n - 1
	(u, v) = ((n - 1) // k, (n - 1) % k)
	(z1, z2, r, s) = (0, 0, 0, 0)
	for (i, d) in enumerate(DD):
		s += d
		if i < v:
			r += d
			if d == 0:
				z1 += 1
				z2 += 1
		elif d == 0:
			z2 += 1
	ans -= z2 * u + z1 + (x + s * u + r) // m
	print(ans)


import sys
readline = sys.stdin.readline
(K, Q) = map(int, readline().split())
D = list(map(int, readline().split()))
Ans = [None] * Q
for q in range(Q):
	(n, x, mod) = map(int, readline().split())
	x %= mod
	D2 = [d % mod for d in D]
	res = 0
	cnt = 0
	zero = 0
	for d2 in D2:
		res += d2
		if res > mod:
			cnt += 1
			res -= mod
		if not d2:
			zero += 1
	loop = (n - 1) // K
	minus = loop * cnt
	zero *= loop
	res2 = 0
	for i in range((n - 1) % K):
		res2 += D2[i]
		if not D2[i]:
			zero += 1
		if res2 > mod:
			res2 -= mod
			minus += 1
	minus += (x % mod + loop * res + res2) // mod
	Ans[q] = n - 1 - zero - minus
print('\n'.join(map(str, Ans)))


import math
import sys
sys.setrecursionlimit(10 ** 5 + 10)

def input():
	return sys.stdin.readline().strip()

def resolve():
	(k, q) = map(int, input().split())
	d = list(map(int, input().split()))
	for qi in range(q):
		(n, x, m) = map(int, input().split())
		last = x
		eq = 0
		for i in range(k):
			num = (n - 1) // k
			if (n - 1) % k >= i + 1:
				num += 1
			last += d[i] % m * num
			if d[i] % m == 0:
				eq += num
		ans = n - 1 - (last // m - x // m) - eq
		print(ans)
resolve()


def solve():
	from itertools import islice, tee
	(k, q) = map(int, input().split())
	(*d,) = map(int, input().split())
	for g in tee(d, q):
		(n, x, m) = map(int, input().split())
		g = map(lambda x: x % m, g)
		(g1, g2, g3) = tee(g, 3)
		(rep, rest) = divmod(n - 1, k)
		last = x + sum(g1) * rep + sum(islice(g2, 0, rest))
		eq = sum((rep + (1 if i < rest else 0) for (i, gg) in enumerate(g3) if gg == 0))
		ans = n - 1 - eq - (last // m - x // m)
		yield ans

def main():
	print(*solve(), sep='\n')
main()


import sys

def LI():
	return list(map(int, sys.stdin.readline().rstrip().split()))
(k, q) = LI()
d = LI()
from itertools import accumulate
for _ in range(q):
	(n, x, m) = LI()
	e = [0] + [d[i] % m for i in range(k)]
	f = list(accumulate(e))
	g = [0] + [d[i] % m == 0 for i in range(k)]
	h = list(accumulate(g))
	p = (n - 1) // k
	r = (n - 1) % k
	print(n - 1 - p * h[k] - h[r] - (p * f[k] + f[r] + x % m) // m)


import sys
input = sys.stdin.readline
(k, q) = map(int, input().split())
d = list(map(int, input().split()))
info = [list(map(int, input().split())) for i in range(q)]
for (n, x, mod) in info:
	d_mod = [d[i] % mod for i in range(k)]
	(laps, rem) = divmod(n - 1, k)
	start = x % mod
	end = start + sum(d_mod) * laps + sum(d_mod[0:rem])
	ans = n - (end // mod - start // mod + 1)
	is0 = [d_mod[i] == 0 for i in range(k)]
	cnt0 = sum(is0) * laps + sum(is0[0:rem])
	print(ans - cnt0)


import sys
input = sys.stdin.readline
(k, q) = map(int, input().split())
d_array = list(map(int, input().split()))
ans_array = []
for _ in range(q):
	(n, x, m) = map(int, input().split())
	d_mod_array = [d % m for d in d_array]
	d_sum = 0
	d_part = 0
	zero_count = 0
	zero_part = 0
	part = (n - 1) % k
	for (i, d) in enumerate(d_mod_array):
		d_sum += d
		if i < part:
			d_part += d
		if d == 0:
			zero_count += 1
			if i < part:
				zero_part += 1
	mod_total = x % m + d_sum * ((n - 1) // k) + d_part
	reduce_total = mod_total // m
	zero_total = zero_count * ((n - 1) // k) + zero_part
	ans = n - 1 - reduce_total - zero_total
	ans_array.append(str(ans))
print('\n'.join(ans_array))


import sys
input = sys.stdin.readline
(k, q) = map(int, input().split())
D = tuple(map(int, input().split()))
for _ in range(q):
	(n, x, m) = map(int, input().split())
	E = tuple((d % m for d in D))
	temp = n - 1
	b = x
	for (i, e) in enumerate(E, 1):
		c = -(-(n - i) // k)
		if e == 0:
			temp -= c
		b += e * c
	temp -= b // m - x // m
	print(temp)


def main():
	(k, q) = map(int, input().split())
	(*d,) = map(int, input().split())
	for _ in range(q):
		(n, x, m) = map(int, input().split())
		(*g,) = map(lambda x: x % m, d)
		(rep, rest) = divmod(n - 1, k)
		last = x + sum(g) * rep + sum(g[:rest])
		eq = sum((rep + (1 if i < rest else 0) for (i, gg) in enumerate(g) if gg == 0))
		ans = n - 1 - eq - (last // m - x // m)
		print(ans)
main()


import sys, math, itertools, collections, bisect
input = lambda : sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf')
mod = 10 ** 9 + 7
mans = inf
ans = 0
count = 0
pro = 1
(k, q) = map(int, input().split())
D = list(map(int, input().split()))
for _ in range(q):
	(n, x, m) = map(int, input().split())
	DD = [(d - 1 + m) % m + 1 for d in D]
	tmp = x
	s = (n - 1) // k
	for (i, di) in enumerate(DD):
		tmp += di * (s + int((n - 1) % k > i))
	print(n - 1 - (tmp // m - x // m))


def main():
	(K, Q) = list(map(int, input().split()))
	D = list(map(int, input().split()))
	query_list = [list(map(int, input().split())) for _ in range(Q)]
	ans = [0 for _ in range(Q)]
	for (q, query) in enumerate(query_list):
		(N, X, M) = query
		mod_d = [d % M for d in D]
		cnt_no_change = 0
		for (i, d) in enumerate(mod_d):
			if d != 0:
				continue
			cnt = (N - 2) // K
			if i <= (N - 2) % K:
				cnt += 1
			cnt_no_change += cnt
		initial_a = X
		final_a = initial_a
		for (i, d) in enumerate(mod_d):
			cnt = (N - 2) // K
			if i <= (N - 2) % K:
				cnt += 1
			final_a += cnt * d
		cnt_decrease = final_a // M - initial_a // M
		ans[q] = N - 1 - cnt_no_change - cnt_decrease
	for a in ans:
		print(a)
main()


(k, q) = map(int, input().split())
d = list(map(int, input().split()))
for _ in range(q):
	(n, x, m) = map(int, input().split())
	x %= m
	dm = []
	for dx in d:
		dm.append((dx - 1) % m + 1)
	sumd = (n - 1) // k * sum(dm) + sum(dm[:(n - 1) % k])
	ans = n - 1 - (x + sumd) // m + x // m
	print(ans)


import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10 ** 9))
write = lambda x: sys.stdout.write(x + '\n')
(k, q) = map(int, input().split())
d = list(map(int, input().split()))

def sub(n, x, m):
	X = 0
	init = x % m
	x %= m
	n -= 1
	if n <= k:
		for i in range(n):
			if x < (x + d[i]) % m:
				X += 1
			x = (x + d[i]) % m
		ans = X
	else:
		for i in range(k):
			if x < (x + d[i]) % m:
				X += 1
			x = (x + d[i]) % m
		diff = x - init
		ans = n // k * X - (init + diff * (n // k)) // m
		nn = n - n // k * k
		xx = (init + diff * (n // k)) % m
		for i in range(nn):
			if xx < (xx + d[i]) % m:
				ans += 1
			xx = (xx + d[i]) % m
	return ans
ans = [None] * q
for i in range(q):
	(n, x, m) = map(int, input().split())
	ans[i] = sub(n, x, m)
write('\n'.join(map(str, ans)))


(k, q) = map(int, input().split())
d = list(map(int, input().split()))
for i in range(q):
	(n, x, m) = map(int, input().split())
	d2 = [d[i] % m for i in range(k)]
	sum_d = [0] * (k + 1)
	count_d = [0] * (k + 1)
	for i in range(k):
		sum_d[i + 1] = sum_d[i] + d2[i]
		if d2[i] == 0:
			count_d[i + 1] = count_d[i] + 1
		else:
			count_d[i + 1] = count_d[i]
	a = (n - 1) // k
	b = (n - 1) % k
	c = (x + sum_d[k] * a + sum_d[b]) // m - x // m
	print(n - 1 - c - count_d[k] * a - count_d[b])


from itertools import accumulate
import sys
input = sys.stdin.readline
(k, q) = map(int, input().split())
d = list(map(int, input().split()))
for _ in range(q):
	(n, x, m) = map(int, input().split())
	ans = n - 1
	s = x % m
	dm = [i % m for i in d]
	accum = list(accumulate(dm))
	count = [0] * k
	for i in range(k):
		if dm[i] == 0:
			count[i] += 1
	count = list(accumulate(count))
	s += accum[-1] * ((n - 1) // k)
	ans -= count[-1] * ((n - 1) // k)
	if (n - 1) % k > 0:
		s += accum[(n - 1) % k - 1]
		ans -= count[(n - 1) % k - 1]
	ans -= s // m
	print(ans)


import numpy as np
(k, q) = list(map(int, input().split()))
d = np.array(list(map(int, input().split())))
for _ in range(q):
	(n, x, m) = list(map(int, input().split()))
	dm = d % m
	a = (n - 1) % k
	ans = n - 1
	ans -= np.count_nonzero(dm[:a] == 0) * ((n - 1) // k + 1)
	ans -= np.count_nonzero(dm[a:] == 0) * ((n - 1) // k)
	ans -= (x % m + np.sum(dm[:a]) * ((n - 1) // k + 1) + np.sum(dm[a:]) * ((n - 1) // k)) // m
	print(ans)


import sys
from itertools import accumulate

def solve(k: int, q: int, d: 'List[int]', n: 'List[int]', x: 'List[int]', m: 'List[int]'):
	for query in range(q):
		(N, X, M) = (n[query], x[query], m[query])
		count = [0] * k
		D = [0] * k
		c = 0
		for i in range(k):
			D[i] = d[i] % M
			if D[i] == 0:
				c += 1
			count[i] = c
		accum = list(accumulate(D))
		SUMD = accum[-1]
		COUNTD = count[-1]
		if (N - 1) % k == 0:
			last = X + (N - 1) // k * SUMD
			equal_count = COUNTD * ((N - 1) // k)
		else:
			last = X + SUMD * ((N - 1) // k) + accum[(N - 1) % k - 1]
			equal_count = COUNTD * ((N - 1) // k) + count[(N - 1) % k - 1]
		shounari_count = last // M - X // M
		dainari_count = N - shounari_count - equal_count - 1
		print(dainari_count)
	return

def main():

	def iterate_tokens():
		for line in sys.stdin:
			for word in line.split():
				yield word
	tokens = iterate_tokens()
	k = int(next(tokens))
	q = int(next(tokens))
	d = [int(next(tokens)) for _ in range(k - 1 - 0 + 1)]
	n = [int()] * q
	x = [int()] * q
	m = [int()] * q
	for i in range(q):
		n[i] = int(next(tokens))
		x[i] = int(next(tokens))
		m[i] = int(next(tokens))
	solve(k, q, d, n, x, m)
main()


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop, heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10 ** 9 + 7
inf = float('inf')

def I():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))
(k, q) = LI()
d = LI()
nxm = [LI() for _ in range(q)]
for (n, x, m) in nxm:
	e = list(map(lambda x: (x - 1) % m + 1, d))
	f = list(accumulate(e))
	s = f[-1] * ((n - 1) // k)
	if (n - 1) % k != 0:
		s += f[(n - 1) % k - 1]
	tmp = (s + x % m) // m
	print(n - 1 - tmp)


import numpy as np

def solve(k, q, d_list, query_list):
	res = []
	d_arr = np.array(d_list)
	for query in query_list:
		(n, x, m) = query
		d_arr_mod = (d_arr - 1) % m + 1
		x = x % m
		a_n1 = x + (n - 1) // k * d_arr_mod.sum() + d_arr_mod[:(n - 1) % k].sum()
		res.append(n - 1 - a_n1 // m)
	return res

def main():
	(k, q) = list(map(int, input().split()))
	d_list = list(map(int, input().split()))
	query_list = [list(map(int, input().split())) for _ in range(q)]
	res = solve(k, q, d_list, query_list)
	for r in res:
		print(r)

def test():
	assert solve(3, 1, [3, 1, 4], [(5, 3, 2)]) == [1]
	assert solve(7, 3, [27, 18, 28, 18, 28, 46, 1000000000], [(1000000000, 1, 7), (1000000000, 2, 10), (1000000000, 3, 12)]) == [224489796, 214285714, 559523809]

def __starting_point():
	main()
__starting_point()


def main():
	import sys
	input = sys.stdin.readline
	(K, q) = map(int, input().split())
	D_raw = list(map(int, input().split()))
	for _ in range(q):
		(N, x, M) = map(int, input().split())
		x %= M
		a = (N - 1) % K
		b = (N - 1) // K
		D = [0] * K
		zero_num = 0
		zero_num_2 = 0
		for i in range(K):
			d = D_raw[i] % M
			D[i] = d
			if not d:
				zero_num += 1
				if i < a:
					zero_num_2 += 1
		ans = N - 1 - zero_num * b - zero_num_2 - (sum(D) * b + sum(D[:a]) + x) // M
		print(ans)
main()


(k, q) = map(int, input().split())
dbase = list(map(int, input().split()))
for _ in range(q):
	(n, x, m) = map(int, input().split())
	x %= m
	dls = [d % m for d in dbase]
	(q, r) = divmod(n - 1, k)
	end = x
	minus = 0
	for i in range(k):
		if i < r:
			end += dls[i] * (q + 1)
			if dls[i] == 0:
				minus += q + 1
		else:
			end += dls[i] * q
			if dls[i] == 0:
				minus += q
	minus += end // m
	print(n - minus - 1)


def main():
	(k, q) = map(int, input().split())
	d = list(map(int, input().split()))
	nxm = [list(map(int, input().split())) for _ in [0] * q]
	for (n, x, m) in nxm:
		d2 = sum([(i - 1) % m + 1 for i in d[:(n - 2) % k + 1]])
		d3 = sum([(i - 1) % m + 1 for i in d[(n - 2) % k + 1:]])
		print(n - 1 - (x % m + (d2 + d3) * ((n - 2) // k) + d2) // m)
main()


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
import time, random
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def main():
	(k, q) = LI()
	d = LI()
	aa = [LI() for _ in range(q)]

	def f(n, x, m):
		e = [c % m for c in d]
		n -= 1
		s = sum(e)
		t = s * (n // k) + sum(e[:n % k])
		xt = x + t
		z = [1 if c == 0 else 0 for c in e]
		zs = sum(z) * (n // k) + sum(z[:n % k])
		return n - (xt // m - x // m) - zs
	r = [f(n, x, m) for (n, x, m) in aa]
	return JA(r, '\n')
print(main())


from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
import pprint
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 13

def LI():
	return list(map(int, sys.stdin.buffer.readline().split()))

def I():
	return int(sys.stdin.buffer.readline())

def LS():
	return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()

def S():
	return sys.stdin.buffer.readline().rstrip().decode('utf-8')

def IR(n):
	return [I() for i in range(n)]

def LIR(n):
	return [LI() for i in range(n)]

def SR(n):
	return [S() for i in range(n)]

def LSR(n):
	return [LS() for i in range(n)]

def SRL(n):
	return [list(S()) for i in range(n)]

def MSRL(n):
	return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007
(k, q) = LI()
D = LI()
for _ in range(q):
	(n, x, m) = LI()
	ret = x
	D_m = [d % m for d in D]
	zero_or_one = list(accumulate([1 if d == 0 else 0 for d in D_m]))
	Dm_acc = list(accumulate(D_m))
	ret = x + (n - 1) // k * Dm_acc[-1]
	zero_ret = (n - 1) // k * zero_or_one[-1]
	if (n - 1) % k:
		ret += Dm_acc[(n - 1) % k - 1]
		zero_ret += zero_or_one[(n - 1) % k - 1]
	print(n - 1 - (ret // m - x // m) - zero_ret)


import numpy as np
(k, q) = map(int, input().split())
d = np.array(list(map(int, input().split())))
for i in range(q):
	(n, x, m) = map(int, input().split())
	mod_d = d % m
	modsum_d = mod_d.cumsum()
	(_q, _m) = divmod(n - 1, k)
	ans = modsum_d[k - 1] * _q
	if _m > 0:
		ans += modsum_d[_m - 1]
	ans = n - 1 - (ans + x % m) // m - np.count_nonzero(mod_d == 0) * _q
	if _m > 0:
		ans -= np.count_nonzero(mod_d[0:_m] == 0)
	print(ans)


import sys
debug = False

def solve(ds, n, x, m):
	ds = [d % m for d in ds]
	nr_loop = (n - 1) // len(ds)
	lastloop_remaining = (n - 1) % len(ds)
	a_n = x
	cnt_zero = 0
	for i in range(0, len(ds)):
		dmod = ds[i] % m
		a_n += dmod * nr_loop
		if dmod == 0:
			cnt_zero += nr_loop
		if i < lastloop_remaining:
			a_n += dmod
			if dmod == 0:
				cnt_zero += 1
	cnt_decrease = a_n // m - x // m
	return n - 1 - cnt_decrease - cnt_zero

def read_int_list(sep=' '):
	return [int(s) for s in sys.stdin.readline().split(sep)]

def dprint(*args, **kwargs):
	if debug:
		print(*args, **kwargs)
	return

def main():
	(k, q) = read_int_list()
	d = read_int_list()
	for _ in range(0, q):
		(n, x, m) = read_int_list()
		print(str(solve(d, n, x, m)))
main()


import sys
input = sys.stdin.readline
(k, q) = map(int, input().split())
D = list(map(int, input().split()))
A = []
for i in range(q):
	(n, x, m) = map(int, input().split())
	mD = list(map(lambda i: i % m, D))
	ans = 0
	cnt = x % m
	a = (n - 1) // k
	b = (n - 1) % k
	for d in mD[:b]:
		if d == 0:
			ans += a + 1
		cnt += d * (a + 1)
	for d in mD[b:]:
		if d == 0:
			ans += a
		cnt += d * a
	ans += cnt // m
	print(n - 1 - ans)


import sys
input = sys.stdin.readline
(k, q) = map(int, input().split())
d_ = list(map(int, input().split()))
for _ in range(q):
	(n, x, m) = map(int, input().split())
	d = [di % m for di in d_]
	ans = n - 1
	zero_acc = [0]
	sum_acc = [0]
	for di in d:
		zero_acc.append(zero_acc[-1] + (1 if di == 0 else 0))
		sum_acc.append(sum_acc[-1] + di)
	(s, a) = divmod(n - 1, k)
	ans -= s * zero_acc[-1] + zero_acc[a]
	last = x + s * sum_acc[-1] + sum_acc[a]
	ans -= last // m - x // m
	print(ans)


import sys
input = sys.stdin.readline
(K, Q) = map(int, input().split())
D = list(map(int, input().split()))
Query = [list(map(int, input().split())) for _ in range(Q)]
for (n, x, mod) in Query:
	Td = []
	for d in D:
		Td.append(d % mod)
	Init = [0]
	zero_count = 0
	zero_tmp = 0
	for (i, d) in enumerate(Td):
		Init.append(Init[-1] + d)
		if d == 0:
			zero_count += 1
			if i < (n - 1) % K:
				zero_tmp += 1
	c = Init.pop()
	last = x + c * ((n - 1) // K) + Init[(n - 1) % K]
	over_count = last // mod - x // mod
	print(n - 1 - over_count - zero_count * ((n - 1) // K) - zero_tmp)


(K, Q) = map(int, input().split())
D = [int(a) for a in input().split()]
for _ in range(Q):
	(N, X, M) = map(int, input().split())
	t = X % M
	for i in range(K):
		t += ((D[i] - 1) % M + 1) * ((N - i - 2) // K + 1)
	print(N - 1 - t // M)


import sys
INF = float('inf')

def solve(k: int, q: int, d: 'List[int]', n: 'List[int]', x: 'List[int]', m: 'List[int]'):
	for i in range(q):
		dmod = [v % m[i] for v in d]
		zero = 0
		(c, p) = divmod(n[i] - 1, k)
		last = x[i]
		for j in range(k):
			if j < p:
				last += (c + 1) * dmod[j]
				if dmod[j] == 0:
					zero += c + 1
			else:
				last += c * dmod[j]
				if dmod[j] == 0:
					zero += c
		print(n[i] - 1 - (zero + (last // m[i] - x[i] // m[i])))
	return

def main():

	def iterate_tokens():
		for line in sys.stdin:
			for word in line.split():
				yield word
	tokens = iterate_tokens()
	k = int(next(tokens))
	q = int(next(tokens))
	d = [int(next(tokens)) for _ in range(k - 1 - 0 + 1)]
	n = [int()] * q
	x = [int()] * q
	m = [int()] * q
	for i in range(q):
		n[i] = int(next(tokens))
		x[i] = int(next(tokens))
		m[i] = int(next(tokens))
	solve(k, q, d, n, x, m)
main()


import numpy as np
(k, q) = map(int, input().split())
d = np.array(list(map(int, input().split())))
n = [0] * q
x = n.copy()
m = n.copy()
for i in range(q):
	(n[i], x[i], m[i]) = map(int, input().split())
for i in range(q):
	d_ = d % m[i]
	j = np.sum(d_ * ((n[i] - 1) // k)) + np.sum(d_[:(n[i] - 1) % k])
	s = np.sum(np.sum(d_ == 0) * ((n[i] - 1) // k)) + np.sum(d_[:(n[i] - 1) % k] == 0)
	count = n[i] - 1 - (j + x[i] % m[i]) // m[i] - s
	print(count)


(K, Q) = map(int, input().split())
D = list(map(int, input().split()))
que = [list(map(int, input().split())) for i in range(Q)]
for (n, x, mod) in que:
	x %= mod
	d = []
	if D[0] % mod == 0:
		d.append(mod)
	else:
		d.append(D[0] % mod)
	for i in range(1, K):
		if D[i] % mod == 0:
			d.append(d[-1] + mod)
		else:
			d.append(d[-1] + D[i] % mod)
	s = d[-1]
	num = x + s * ((n - 2) // K) + d[(n - 2) % K]
	print(n - 1 - num // mod)


import numpy as np
(k, q) = map(int, input().split())
d = np.array(input().split(), dtype=np.int)
for _ in range(q):
	(n, x, m) = map(int, input().split())
	quot = (n - 1) // k
	rest = (n - 1) % k
	dmod = d % m
	same = (k - np.count_nonzero(dmod)) * quot + rest - np.count_nonzero(dmod[:rest])
	a_last = x % m + dmod.sum() * quot + dmod[:rest].sum()
	beyond = a_last // m
	print(n - 1 - same - beyond)


(k, q) = map(int, input().split())
d = list(map(int, input().split()))
for _ in range(q):
	(n, x, m) = map(int, input().split())
	e = list(map(lambda x: (x - 1) % m + 1, d))
	s = sum(e) * ((n - 1) // k) + sum(e[:(n - 1) % k])
	tmp = (s + x) // m - x // m
	print(n - 1 - tmp)


import sys
input = sys.stdin.readline

def main():
	(K, Q) = map(int, input().split())
	D = list(map(int, input().split()))
	for _ in range(Q):
		(n, x, m) = map(int, input().split())
		md = [D[i] % m for i in range(K)]
		smda = 0
		mda0 = 0
		for i in range((n - 1) % K):
			if md[i] == 0:
				mda0 += 1
			smda += md[i]
		smd = smda
		md0 = mda0
		for i in range((n - 1) % K, K):
			if md[i] == 0:
				md0 += 1
			smd += md[i]
		roop = (n - 1) // K
		res = n - 1 - (x % m + smd * roop + smda) // m - md0 * roop - mda0
		print(res)
main()


(k, q) = map(int, input().split())
d = list(map(int, input().split()))
al = []
for i in range(q):
	(n, x, m) = map(int, input().split())
	pd = []
	count = 0
	for j in range(k):
		pd.append(d[j] % m)
		if d[j] % m == 0:
			count += 1
	a = sum(pd)
	ans = x % m
	ans += a * ((n - 1) // k)
	c = 0
	for j in range((n - 1) % k):
		ans += pd[j]
		if pd[j] == 0:
			c += 1
	al.append(n - 1 - ans // m - count * ((n - 1) // k) - c)
for i in range(q):
	print(al[i])


(k, q) = map(int, input().split())
d = list(map(int, input().split()))
for _ in range(q):
	(n, x, m) = map(int, input().split())
	li = [0] * k
	tmp = 0
	for i in range(k):
		li[i] = d[i] % m
	cnt = x % m
	n -= 1
	for i in range(k):
		cnt += (n // k + (n % k > i)) * li[i]
		if li[i] == 0:
			tmp += n // k + (n % k > i)
	print(n - cnt // m - tmp)


import sys
input = sys.stdin.readline
(k, q) = map(int, input().split())
d = list(map(int, input().split()))
for _ in range(q):
	(n, x, m) = map(int, input().split())
	dm = [di % m if di % m > 0 else m for di in d]
	oa = on = 0
	for (i, a) in enumerate(dm):
		oa += a
		if i + 1 == (n - 1) % k:
			on = oa
	first = x
	last = x + oa * ((n - 1) // k) + on
	print(n - 1 - (last // m - first // m))


(k, q) = map(int, input().split())
d = [int(i) for i in input().split()]
for i in range(q):
	(n, x, m) = map(int, input().split())
	cum = [0] * (k + 1)
	cnt = [0] * (k + 1)
	for i in range(k):
		cum[i] = cum[i - 1] + d[i] % m
		cnt[i] = cnt[i - 1]
		if d[i] % m == 0:
			cnt[i] += 1
	res = x % m + cum[k - 1] * ((n - 1) // k) + cum[(n - 1) % k - 1]
	s = res // m
	print(n - 1 - s - cnt[k - 1] * ((n - 1) // k) - cnt[(n - 1) % k - 1])


import sys
input = sys.stdin.readline
(k, q) = map(int, input().split())
d = list(map(int, input().split()))
for _ in range(q):
	(n, x, m) = map(int, input().split())
	dd = [d[i] % m for i in range(k)]
	for i in range(k):
		if dd[i] == 0:
			dd[i] = m
	s = x % m + sum(dd) * ((n - 1) // k) + sum(dd[:(n - 1) % k])
	print(n - 1 - s // m)


(k, q) = map(int, input().split())
d = list(map(int, input().split()))
for _ in range(q):
	(n, x, m) = map(int, input().split())
	tmp = x
	a = list(map(lambda y: y % m, d))
	c = 0
	s = 0
	c2 = 0
	s2 = 0
	for x in a:
		if x == 0:
			c += 1
		s += x
	for x in a[:(n - 1) % k]:
		if x == 0:
			c2 += 1
		s2 += x
	t = s * ((n - 1) // k) + s2 + tmp % m
	ans = n - 1 - t // m - c * ((n - 1) // k) - c2
	print(ans)


import sys
input = sys.stdin.readline
(k, q) = map(int, input().split())
d = list(map(int, input().split()))
query = [None] * q
for i in range(q):
	query[i] = list(map(int, input().split()))

def answer_query(n, x, m):
	l = [m if i % m == 0 else i % m for i in d]
	count = n // k
	rem = n % k
	s = sum(l)
	total = s % m * count + x % m
	for i in range(rem):
		total += l[i]
	total //= m
	total += s // m * count
	print(n - total)
for (n, x, m) in query:
	answer_query(n - 1, x, m)


import sys
input = sys.stdin.readline
(k, q) = map(int, input().split())
D = tuple(map(int, input().split()))

def sum_count(A):
	S = 0
	C = 0
	for a in A:
		if a == 0:
			C += 1
		else:
			S += a
	return (S, C)
for query in range(q):
	(n, x, m) = map(int, input().split())
	D2 = [d % m for d in D]
	(S, D0) = sum_count(D2)
	R = (n - 1) // k
	ANS = R * (k - D0) - (R * S + x % m) // m
	x2 = (x + S * R) % m
	rest = n - R * k - 1
	(S2, DR0) = sum_count(D2[:rest])
	ANSR = rest - DR0 - (x2 + S2) // m
	print(ANS + ANSR)


import sys
input = sys.stdin.readline

def main():
	(k, q) = map(int, input().split())
	D = tuple(map(int, input().split()))
	for _ in range(q):
		(n, x, m) = map(int, input().split())
		x %= m
		Di = []
		zero = 0
		for d in D:
			tmp = d % m
			if tmp == 0:
				zero += 1
			Di.append(tmp)
		s = sum(Di)
		cycles = (n - 1) // k
		cycle_sum = cycles * s
		remain = (n - 1) % k
		last = x + cycle_sum
		for i in range(remain):
			last += Di[i]
		q = n - 1
		q -= zero * cycles
		for i in range(remain):
			if Di[i] == 0:
				q -= 1
		q -= last // m
		print(q)
main()


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
(k, q) = map(int, input().split())
d = list(map(int, input().split()))
for i in range(q):
	(n, x, mod) = map(int, input().split())
	dd = [di % mod if di % mod else mod for di in d]
	(a, b) = divmod(n - 1, k)
	tmp = x % mod + a * sum(dd) + sum(dd[:b])
	print(n - 1 - tmp // mod)


from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())

def LS():
	return [list(x) for x in sys.stdin.readline().split()]

def S():
	res = list(sys.stdin.readline())
	if res[-1] == '\n':
		return res[:-1]
	return res

def IR(n):
	return [I() for i in range(n)]

def LIR(n):
	return [LI() for i in range(n)]

def SR(n):
	return [S() for i in range(n)]

def LSR(n):
	return [LS() for i in range(n)]
sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
	(k, q) = LI()
	d = LI()
	for _ in range(q):
		(n, x, m) = LI()
		a = [(i - 1) % m + 1 for i in d]
		S = sum(a)
		r = (n - 1) // k
		s = S * r + x % m
		for i in range((n - 1) % k):
			s += a[i]
		print(n - 1 - s // m)
	return
solve()


(K, Q) = map(int, input().split())
D = [int(a) for a in input().split()]
for _ in range(Q):
	(N, X, M) = map(int, input().split())
	if N <= K + 1:
		A = [0] * N
		A[0] = X % M
		ans = 0
		for i in range(N - 1):
			A[i + 1] = (A[i] + D[i]) % M
			if A[i + 1] > A[i]:
				ans += 1
		print(ans)
		continue
	ans = N - 1
	t = X % M
	for i in range(K):
		d = (D[i] - 1) % M + 1
		a = (N - i - 2) // K + 1
		t += d * a
	ans -= t // M
	print(ans)


import sys
from operator import itemgetter
from collections import deque, Counter
from itertools import permutations, combinations
from heapq import heapify, heappop, heappush
from bisect import bisect, bisect_left

def search(n, x, m, K, D):
	ini = x % m
	modD = [D[i] % m for i in range(K)]
	(roop, rem) = ((n - 1) // K, (n - 1) % K)
	last = ini
	equal = 0
	for i in range(K):
		if i < rem and rem > 0:
			if modD[i] == 0:
				equal += roop + 1
			else:
				last += (roop + 1) * modD[i]
		elif modD[i] == 0:
			equal += roop
		else:
			last += roop * modD[i]
	up = last // m - ini // m
	return n - 1 - equal - up

def solve():
	input = sys.stdin.readline
	(K, Q) = map(int, input().split())
	D = [int(d) for d in input().split()]
	Ans = []
	for _ in range(Q):
		(n, x, m) = map(int, input().split())
		Ans.append(search(n, x, m, K, D))
	for (i, a) in enumerate(Ans):
		print(a)
	return 0
solve()


import math
import fractions

def make_divisors(n):
	divisors = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			divisors.append(i)
			if i != n // i:
				divisors.append(n // i)
	return divisors

def ValueToBits(x, digit):
	res = [0 for i in range(digit)]
	now = x
	for i in range(digit):
		res[i] = now % 2
		now = now >> 1
	return res

def BitsToValue(arr):
	n = len(arr)
	ans = 0
	for i in range(n):
		ans += arr[i] * 2 ** i
	return ans

def ValueToArray10(x, digit):
	ans = [0 for i in range(digit)]
	now = x
	for i in range(digit):
		ans[digit - i - 1] = now % 10
		now = now // 10
	return ans
(k, q) = list(map(int, input().split()))
d = list(map(int, input().split()))
que = []
for i in range(q):
	que.append(list(map(int, input().split())))
dsum = [0]
for i in range(k):
	dsum.append(dsum[-1] + d[i])
for i in range(q):
	dr = [d[j] % que[i][2] for j in range(len(d))]
	drsum = [0 for i in range(k + 1)]
	for j in range(k):
		drsum[j + 1] = drsum[j] + dr[j]
	count0 = [0 for i in range(k + 1)]
	for j in range(k):
		if dr[j] == 0:
			count0[j + 1] = count0[j] + 1
		else:
			count0[j + 1] = count0[j]
	times = (que[i][0] - 1) // k
	amari = (que[i][0] - 1) % k
	total = que[i][1] + drsum[-1] * times + drsum[amari]
	num0 = count0[-1] * times + count0[amari]
	ans = total // que[i][2] - que[i][1] // que[i][2]
	ans = que[i][0] - ans - num0 - 1
	print(ans)


import sys
input = sys.stdin.readline
(K, Q) = map(int, input().split())
d = list(map(int, input().split()))
for _ in range(Q):
	(n, x, m) = map(int, input().split())
	x %= m
	dq = [y % m for y in d]
	for i in range(K):
		dq[i] += (dq[i] == 0) * m
	res = (sum(dq) * ((n - 1) // K) + sum(dq[:(n - 1) % K]) + x) // m
	print(n - 1 - res)


import sys
import numpy as np
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]

def main():
	(k, q) = MI()
	dd = LI()
	dd = np.array(dd, dtype='i8')
	for _ in range(q):
		(n, x, m) = MI()
		cc = dd % m
		zall = np.sum(cc == 0)
		zlast = np.sum(cc[:(n - 1) % k] == 0)
		s = np.sum(cc) * ((n - 1) // k)
		r = np.sum(cc[:(n - 1) % k])
		last = x % m + s + r
		no = last // m
		print(n - 1 - no - zall * ((n - 1) // k) - zlast)
main()


(k, q) = map(int, input().split())
d = list(map(int, input().split()))
for loop in range(q):
	(n, x, m) = map(int, input().split())
	x %= m
	nd = []
	for i in d:
		nd.append(i % m)
	znum = [0]
	lsum = [0]
	for i in nd:
		if i == 0:
			znum.append(znum[-1] + 1)
		else:
			znum.append(znum[-1])
		lsum.append(lsum[-1] + i)
	zpick = (n - 1) // k * znum[-1] + znum[(n - 1) % k]
	lpick = (x + (n - 1) // k * lsum[-1] + lsum[(n - 1) % k]) // m
	print(n - 1 - zpick - lpick)


def main():
	import numpy as np
	(k, q) = map(int, input().split())
	d = np.array(list(map(int, input().split())))
	for i in range(q):
		(n, x, m) = map(int, input().split())
		x %= m
		d_mod = d % m
		zero_d = k - np.count_nonzero(d_mod)
		d_mod_sum = np.sum(d_mod) + m * zero_d
		rep = (n - 1) // k
		rem = (n - 1) % k
		zero_rem = len(d_mod[:rem]) - np.count_nonzero(d_mod[:rem])
		rem_sum = np.sum(d_mod[:rem]) + m * zero_rem
		ans = n - 1 - (x + d_mod_sum * rep + rem_sum) // m
		print(ans)

def __starting_point():
	main()
__starting_point()


import numpy as np
(k, q) = list(map(int, input().split()))
d = np.array(list(map(int, input().split()))).astype(np.int64)
queries = [tuple(map(int, input().split())) for _ in range(q)]
for (n, x, m) in queries:
	d_mod = d % m
	(quotient, rem) = divmod(n - 1, k)
	rem_d_mod = d_mod[:rem]
	print(quotient * np.count_nonzero(d_mod) + np.count_nonzero(rem_d_mod) - (x % m + quotient * d_mod.sum() + rem_d_mod.sum()) // m)


from numpy import int64, count_nonzero
(K, Q, *I) = map(int, open(0).read().split())
(D, NXM) = (int64(I[:K]), I[K:])
for (n, x, m) in zip(*[iter(NXM)] * 3):
	(q, r) = divmod(n - 1, K)
	T = D % m
	a = (K - count_nonzero(T)) * q + r - count_nonzero(T[:r])
	b = (x % m + T.sum() * q + T[:r].sum()) // m
	print(n - 1 - a - b)


(K, Q) = map(int, input().split())
D = list(map(int, input().split()))
for _ in range(Q):
	(n, x, m) = map(int, input().split())
	ds = []
	sumd = 0
	r = 0
	for (i, d) in enumerate(D):
		tmp = -(-(n - 1 - i) // K)
		ds.append(d % m)
		sumd += d % m * tmp
		if d % m == 0:
			r += tmp
	r += (sumd + x) // m - x // m
	print(n - 1 - r)


import sys
input = sys.stdin.readline
(k, q) = map(int, input().split())
d = tuple(map(int, input().split()))

def query(n, x, m):
	dd = tuple((di % m for di in d))
	ans = n - 1
	a0 = a1 = x
	for i in range(k):
		a1 += ((n - 1 - (i + 1)) // k + 1) * dd[i]
		if dd[i] == 0:
			ans -= (n - 1 - (i + 1)) // k + 1
	ans -= a1 // m - a0 // m
	return ans
for _ in range(q):
	(n_i, x_i, m_i) = map(int, input().split())
	ans_i = query(n_i, x_i, m_i)
	print(ans_i)


(k, q) = input().split()
k = int(k)
q = int(q)
d = input().split()
for i in range(k):
	d[i] = int(d[i]) - 1
import numpy as np
d = np.array(d)
for r in range(q):
	(n, x, m) = input().split()
	n = int(n)
	x = int(x)
	m = int(m)
	dd = d % m + np.ones(k, dtype='int')
	DD = np.concatenate((np.zeros(1, dtype='int'), np.cumsum(dd)))
	alast = x + DD[k] * ((n - 1) // k) + DD[(n - 1) % k]
	kuriagari = alast // m - x // m
	print(n - 1 - kuriagari)


(k, q) = map(int, input().split())
d = [int(x) for x in input().split()]
for _ in range(q):
	(n, x, m) = map(int, input().split())
	z = 0
	last = x
	ans = 0
	for i in range(k):
		dnum = (n - 1 - i + (k - 1)) // k
		last += d[i] % m * dnum
		if d[i] % m == 0:
			z += dnum
	ans = n - 1 - (last // m - x // m) - z
	print(ans)


import numpy as np

def main():
	(k, q) = [int(x) for x in input().split()]
	d = np.fromstring(input(), np.int64, sep=' ')

	def solve1(d, n):
		zero = np.where(d == 0)[0]
		n_zero = zero.shape[0] * ((n - 1) // k)
		n_zero += np.count_nonzero(zero < (n - 1) % k)
		return n_zero

	def solve2(d, n, x, m):
		a_max = np.sum(d) * ((n - 1) // k) + np.sum(d[:(n - 1) % k])
		return (a_max + x) // m - x // m
	for _ in range(q):
		(n, x, m) = [int(x) for x in input().split()]
		dm = d % m
		n_zero = solve1(dm, n)
		n_dec = solve2(dm, n, x, m)
		print(n - 1 - n_zero - n_dec)

def __starting_point():
	main()
__starting_point()


import math
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

def ceil(a, b):
	return (a + b - 1) // b

def main():
	(k, q) = map(int, input().split())
	d = list(map(int, input().split()))
	for i in range(q):
		(n, x, m) = map(int, input().split())
		last = x
		eq = 0
		for j in range(k):
			num = ceil(n - 1 - j, k)
			last += d[j] % m * num
			if d[j] % m == 0:
				eq += num
		ans = n - 1 - (last // m - x // m) - eq
		print(ans)
main()


import sys
stdin = sys.stdin
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
ns = lambda : stdin.readline().rstrip()
(K, Q) = na()
d = na()
for z in range(Q):
	(n, x, m) = na()
	n -= 1
	rem = n % K
	pre = m - 1
	x %= m
	ans = 0
	for i in range(rem):
		pre = x
		x = (x + d[i]) % m
		if pre < x:
			ans += 1
	lpre = 0
	turn = 0
	for v in d[rem:] + d[:rem]:
		cur = (lpre + v) % m
		if lpre < cur:
			turn += 1
		lpre = cur
	my = turn * m - lpre
	ans += (my * (n // K) + m - x - 1) // m - (m - x - 1) // m
	print(ans)


import sys
input = sys.stdin.buffer.readline
(K, Q) = map(int, input().split())
D = list(map(int, input().split()))
NXM = [list(map(int, input().split())) for _ in range(Q)]
for (N, X, M) in NXM:
	E = [d % M for d in D]
	cnt0 = 0
	tmp0 = 0
	for e in E:
		if e > 0:
			cnt0 += 1
			tmp0 += e
	cnt = (N - 1) // K * cnt0
	tmp = (N - 1) // K * tmp0
	for i in range((N - 1) % K):
		if E[i] > 0:
			cnt += 1
		tmp += E[i]
	tmp += X % M
	print(cnt - tmp // M)


import sys
INF = 1 << 60
MOD = 10 ** 9 + 7
sys.setrecursionlimit(2147483647)
input = lambda : sys.stdin.readline().rstrip()

def resolve():
	(k, q) = map(int, input().split())
	D = list(map(int, input().split()))
	for _ in range(q):
		(n, x, m) = map(int, input().split())
		b = x
		for i in range(k):
			s = (n - 2 - i) // k + 1
			t = D[i] % m
			if t == 0:
				t = m
			b += s * t
		print(n - 1 - (b // m - x // m))
resolve()


(k, q) = map(int, input().split())
d = list(map(int, input().split()))
n = [0 for i in range(q)]
x = [0 for i in range(q)]
m = [0 for i in range(q)]
for i in range(0, q):
	(n[i], x[i], m[i]) = map(int, input().split())
x = [x[i] % m[i] for i in range(0, q)]
S = sum(d)
for i in range(0, q):
	p = [d[j] % m[i] for j in range(0, k)]
	test = 0
	subtest = 0
	for j in range(0, k):
		if p[j] != 0:
			test += 1
	s = x[i] % m[i] + sum(p) * ((n[i] - 1) // k) + sum((p[j] for j in range(0, (n[i] - 1) % k)))
	for j in range(0, (n[i] - 1) % k):
		if p[j] != 0:
			subtest += 1
	if s % m[i] == 0:
		print(test * ((n[i] - 1) // k) + subtest - s // m[i])
	else:
		print(test * ((n[i] - 1) // k) + subtest - s // m[i])


(k, q) = map(int, input().split())
d = list(map(int, input().split()))
from collections import Counter
for i in range(q):
	(n, x, m) = map(int, input().split())
	dx = [di % m for di in d]
	for i in range(k):
		if dx[i] == 0:
			dx[i] = m
	sm = x % m + sum(dx) * ((n - 1) // k) + sum(dx[:(n - 1) % k])
	print(n - 1 - sm // m)


(k, q) = map(int, input().split())
d = list(map(int, input().split()))

def solve(n, x, m):
	S = [0] * (k + 1)
	cnt_z = [0] * (k + 1)
	for i in range(k):
		a = d[i] % m
		S[i + 1] = S[i] + a
		cnt_z[i + 1] = cnt_z[i]
		if a == 0:
			cnt_z[i + 1] += 1
	pp = (n - 1) // k
	r = (n - 1) % k
	s = x + S[-1] * pp + S[r]
	ans = n - 1 - s // m - cnt_z[-1] * pp - cnt_z[r]
	print(ans)
for i in range(q):
	(n, x, m) = map(int, input().split())
	x %= m
	solve(n, x, m)


def process(k, d, n, x, m):
	d_mod = [di % m for di in d]
	sum_d_mod = sum(d_mod)
	inc_times1 = len([d_mod_i for d_mod_i in d_mod if d_mod_i])
	rem_d_mod = sum(d_mod[0:(n - 1) % k])
	inc_times2 = len([d_mod_i for d_mod_i in d_mod[0:(n - 1) % k] if d_mod_i])
	last = x + sum_d_mod * ((n - 1) // k) + rem_d_mod
	first = x
	ans = inc_times1 * ((n - 1) // k) + inc_times2
	ans -= last // m - first // m
	print(ans)

def main():
	(k, q) = map(int, input().split())
	d = list(map(int, input().split()))
	for i in range(q):
		(n, x, m) = map(int, input().split())
		process(k, d, n, x, m)
main()


import sys
import numpy as np
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep='\n')
printH = lambda x: print(' '.join(map(str, x)))

def IS():
	return sys.stdin.readline()[:-1]

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LI1():
	return list(map(int1, sys.stdin.readline().split()))

def LII(rows_number):
	return [II() for _ in range(rows_number)]

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def LLI1(rows_number):
	return [LI1() for _ in range(rows_number)]

def main():
	(k, q) = MI()
	d = LI()
	d = np.array(d)
	ans = [0] * q
	for i in range(q):
		(n, x, m) = MI()
		d_modm = d % m
		zero = (n - 1) // k * np.sum(d_modm == 0) + np.sum((d_modm == 0)[:(n - 1) % k])
		last = x + (n - 1) // k * np.sum(d_modm) + np.sum(d_modm[:(n - 1) % k])
		ans[i] = n - 1 - zero - (last // m - x // m)
	printV(ans)

def __starting_point():
	main()
__starting_point()


(k, q) = map(int, input().split())
d = [int(i) for i in input().split()]
nxm = [[int(i) for i in input().split()] for _ in range(q)]
for (n, x, m) in nxm:
	an = x
	for i in range(k):
		if d[i] % m != 0:
			an += ((n - i - 2) // k + 1) * (d[i] % m)
		else:
			an += ((n - i - 2) // k + 1) * m
	print(n - 1 - (an // m - x // m))


(k, q) = map(int, input().split())
d = list(map(int, input().split()))

def solve():
	(n, x, m) = map(int, input().split())
	ans = 0
	rn = (n - 1) % k
	f = x % m
	for i in range(k):
		if d[i] % m == 0:
			continue
		if i < rn:
			c = (n - 1) // k + 1
		else:
			c = (n - 1) // k
		ans += c
		f += c * (d[i] % m)
	ans -= f // m
	print(ans)
for _ in range(q):
	solve()


def sRaw():
	return input().rstrip('\r')

def iRaw():
	return int(input())

def ssRaw():
	return input().split()

def isRaw():
	return list(map(int, ssRaw()))
INF = 1 << 29
DIV = 10 ** 9 + 7

def mod_inv_prime(a, mod=DIV):
	return pow(a, mod - 2, mod)

def ncr(n, r, mod=DIV):
	r = min(r, n - r)
	ret = 1
	for i in range(1, r + 1):
		ret = ret * (n - i + 1) % mod
		ret = ret * mod_inv(i, mod) % mod
	return ret

def mod_inv(a, b):
	r = a
	w = b
	u = 1
	v = 0
	while w != 0:
		t = r // w
		r -= t * w
		(r, w) = (w, r)
		u -= t * v
		(u, v) = (v, u)
	return (u % b + b) % b

def CONV_TBL(max, mod=DIV):
	(fac, finv, inv) = ([0] * max, [0] * max, [0] * max)
	fac[0] = fac[1] = 1
	finv[0] = finv[1] = 1
	inv[1] = 1
	for i in range(2, max):
		fac[i] = fac[i - 1] * i % mod
		inv[i] = mod - inv[mod % i] * (mod // i) % mod
		finv[i] = finv[i - 1] * inv[i] % mod

	class CONV:

		def __init__(self):
			pass

		def ncr(self, n, k):
			if n < k:
				return 0
			if n < 0 or k < 0:
				return 0
			return fac[n] * (finv[k] * finv[n - k] % mod) % mod
	return CONV()

def main():
	(k, q) = isRaw()
	ds_orig = isRaw()
	for qidx in range(q):
		(n, x, m) = isRaw()
		ds = [d % m for d in ds_orig]
		numPluses = [(n - 1) // k + (0 if idx >= (n - 1) % k else 1) for idx in range(k)]
		totals = x % m
		numGe = 0
		for (idx, d) in enumerate(ds):
			totals += d * numPluses[idx]
			if totals >= m:
				numGe += totals // m
				totals = totals % m
			if d == 0:
				numGe += numPluses[idx]
		print(n - 1 - numGe)
	return n - 1 - numGe
main()


import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
from collections import defaultdict
con = 10 ** 9 + 7
INF = float('inf')

def getlist():
	return list(map(int, input().split()))

def main():
	(K, Q) = getlist()
	D = getlist()
	for _ in range(Q):
		(N, X, M) = getlist()
		X %= M
		d = [0] * K
		zeroCount = 0
		for i in range(K):
			var = D[i] % M
			d[i] = var
			if var == 0:
				zeroCount += 1
		Dsum = sum(d)
		p = int((N - 1) // K)
		q = (N - 1) % K
		An = X + Dsum * p
		zero = 0
		for i in range(q):
			An += d[i]
			if d[i] == 0:
				zero += 1
		ans = N - 1 - (zeroCount * p + zero) - (int(An // M) - int(X // M))
		print(ans)
main()


import sys, collections as cl, bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10 ** 9 + 7
Max = sys.maxsize

def l():
	return list(map(int, input().split()))

def m():
	return map(int, input().split())

def onem():
	return int(input())

def s(x):
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x) - 1):
		if aa != x[i + 1]:
			a.append([aa, su])
			aa = x[i + 1]
			su = 1
		else:
			su += 1
	a.append([aa, su])
	return a

def jo(x):
	return ' '.join(map(str, x))

def max2(x):
	return max(map(max, x))

def In(x, a):
	k = bs.bisect_left(a, x)
	if k != len(a) and a[k] == x:
		return True
	else:
		return False
mod = 10 ** 9 + 7
N = 10 ** 5 * 2

def cmb(n, r):
	if r < 0 or r > n:
		return 0
	r = min(r, n - r)
	return g1[n] * g2[r] * g2[n - r] % mod

def p(n, r):
	if r < 0 or r > n:
		return 0
	return g1[n] * g2[n - r] % mod
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]
for i in range(2, N + 1):
	g1.append(g1[-1] * i % mod)
	inverse.append(-inverse[mod % i] * (mod // i) % mod)
	g2.append(g2[-1] * inverse[-1] % mod)
import fractions
from functools import reduce

def gcd(*numbers):
	return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
	return reduce(fractions.gcd, numbers)

def lcm_base(x, y):
	return x * y // fractions.gcd(x, y)

def lcm(*numbers):
	return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
	return reduce(lcm_base, numbers, 1)
(k, q) = m()
d = l()
for i in range(q):
	(n, x, mm) = m()
	co = 0
	po = []
	for j in range(k):
		po.append(d[j] % mm if d[j] % mm > 0 else mm)
	pp = sum(po) * ((n - 1) // k)
	pp += x
	for j in range((n - 1) % k):
		pp += po[j]
	co += pp // mm - x // mm
	print(n - co - 1)


(K, Q) = map(int, input('').split())
d_ = list(map(int, input('').split()))
for _ in range(Q):
	(n, x, m) = map(int, input('').split())
	d = [0] * K
	zeroes = [0] * (K + 1)
	sum = [0] * (K + 1)
	for i in range(K):
		d[i] = d_[i] % m
	for i in range(K):
		zeroes[i + 1] = zeroes[i] + (d[i] == 0)
	for i in range(K):
		sum[i + 1] = sum[i] + d[i]
	s = (n - 1) // K * sum[K] + sum[(n - 1) % K]
	s += x % m
	num0 = (n - 1) // K * zeroes[K] + zeroes[(n - 1) % K]
	print(n - 1 - s // m - num0)


import sys
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]

def main():
	(k, q) = MI()
	dd = LI()
	for _ in range(q):
		(n, x, m) = MI()
		sall = spart = 0
		zall = zpart = 0
		for (i, d) in enumerate(dd):
			d %= m
			if i < (n - 1) % k:
				spart += d
				if d == 0:
					zpart += 1
			sall += d
			if d == 0:
				zall += 1
		last = x % m + sall * ((n - 1) // k) + spart
		no = last // m
		print(n - 1 - no - zall * ((n - 1) // k) - zpart)
main()


import sys
input = sys.stdin.readline
(k, q) = [int(item) for item in input().split()]
d = [int(item) for item in input().split()]
nxm = []
for _ in range(q):
	(n, x, m) = [int(item) for item in input().split()]
	nxm.append((n, x, m))
for (n, x, m) in nxm:
	n -= 1
	newd = []
	is_zero = []
	sum_newd = 0
	for item in d:
		sum_newd += item % m
		newd.append(item % m)
		is_zero.append(item % m == 0)
	loop = n // k
	rest = n % k
	total = loop * sum_newd + x % m
	zeros = loop * sum(is_zero)
	total += sum(newd[:rest])
	zeros += sum(is_zero[:rest])
	rollover = total // m
	ret = n - rollover - zeros
	print(ret)


(k, q) = map(int, input().split())
d = list(map(int, input().split()))
for _ in range(q):
	(n, x, m) = map(int, input().split())
	ans = n - 1
	dm = [el % m for el in d]
	loop = (n - 1) // k
	rem = (n - 1) % k
	eqcnt = 0
	lscnt = 0
	totsum = 0
	remsum = 0
	for i in range(k):
		totsum += dm[i]
		if i < rem:
			remsum += dm[i]
		if dm[i] == 0:
			eqcnt += 1
			if i < rem:
				ans -= 1
	ans -= eqcnt * loop
	lscnt += totsum // m * loop + remsum // m
	lscnt += (totsum % m * loop + remsum % m + x % m) // m
	ans -= lscnt
	print(ans)


import numpy as np
(k, q) = list(map(int, input().split()))
d = np.array(list(map(int, input().split())), dtype=np.int64)
queries = [tuple(map(int, input().split())) for _ in range(q)]
for (n, x, m) in queries:
	d_mod = d % m
	rem_d_mod = d_mod[:(n - 1) % k]
	print(n - 1 - (x % m + (n - 1) // k * np.sum(d_mod) + np.sum(rem_d_mod)) // m - (n - 1) // k * np.count_nonzero(d_mod == 0) - np.count_nonzero(rem_d_mod == 0))


(k, q) = list(map(int, input().split()))
d = list(map(int, input().split()))
nxm = [list(map(int, input().split())) for _ in range(q)]
D = [[0] * 2 for _ in range(k)]
for i in range(q):
	(n, x, m) = nxm[i]
	D[-1] = [0, 0]
	D[0][0] = d[0] % m
	D[0][1] = 0
	for j in range(k):
		D[j][0] = D[j - 1][0] + d[j] % m
		D[j][1] = D[j - 1][1]
		if D[j - 1][0] != D[j][0]:
			D[j][1] += 1
	(N, M) = divmod(n - 1, k)
	ans = N * D[-1][1]
	t = N * D[-1][0] + x % m
	ans -= t // m
	t = t % m
	if M != 0:
		ans += D[M - 1][1]
		t += D[M - 1][0]
		ans -= t // m
	print(ans)


def main():
	import numpy as np
	(k, q) = list(map(int, input().split()))
	d = np.array(list(map(int, input().split())), dtype=np.int64)
	nxm = [list(map(int, input().split())) for _ in [0] * q]
	for (n, x, m) in nxm:
		d2 = np.sum((d[:(n - 2) % k + 1] - 1) % m + 1)
		d3 = np.sum((d[(n - 2) % k + 1:] - 1) % m + 1)
		print(n - 1 - (x % m + (d2 + d3) * ((n - 2) // k) + d2) // m)
main()


import math
(k, q) = map(int, input().split())
_d = list(map(int, input().split()))
for _ in range(q):
	(n, x, m) = map(int, input().split())
	d = map(lambda xx: xx % m, _d)
	ans = 0
	s = 0
	for (i, di) in enumerate(d):
		if di == 0:
			ans += math.ceil((n - i - 1) / k)
		else:
			s += di * math.ceil((n - i - 1) / k)
	ans += (s + x) // m - x // m
	print(n - 1 - ans)


import sys
input = sys.stdin.readline
(k, q) = map(int, input().split())
D = list(map(int, input().split()))
A = [list(map(int, input().split())) for i in range(q)]
for i in range(q):
	n = A[i][0]
	x = A[i][1]
	m = A[i][2]
	ans = 0
	B = [x % m]
	BP = [0] * (k + 1)
	n -= 1
	for j in range(k):
		B.append((B[-1] + D[j]) % m)
	ans0 = 0
	for j in range(len(B) - 1):
		if B[j + 1] > B[j]:
			BP[j + 1] = 1
			ans0 += 1
	if B[-1] != B[0]:
		if B[-1] > B[0]:
			ans1 = [ans0 - 1, ans0]
		elif B[-1] < B[0]:
			ans1 = [ans0, ans0 + 1]
		d = (B[-1] - B[0]) % m
		kn = n // k
		c = (B[0] + d * kn) % m
		sag = (B[0] + d * kn) // m
		ans = ans1[0] * sag + ans1[1] * (kn - sag)
		for ii in range(n % k):
			cm = c
			c += D[ii]
			if c >= m:
				c %= m
			if c > cm:
				ans += 1
		print(ans)
	else:
		print(ans0 * (n // k) + sum(BP[:n % k + 1]))


import sys
input = sys.stdin.buffer.readline
(k, q) = map(int, input().split())
d = list(map(int, input().split()))
for qi in range(q):
	(n, x, m) = map(int, input().split())
	last = x
	eq = 0
	for i in range(k):
		num = (n - 1 - i + (k - 1)) // k
		last += d[i] % m * num
		if d[i] % m == 0:
			eq += num
	ans = n - 1 - (last // m - x // m) - eq
	print(ans)


import math
MOD = 10 ** 9 + 7
INF = float('inf')
kaijo_memo = []

def kaijo(n):
	if len(kaijo_memo) > n:
		return kaijo_memo[n]
	if len(kaijo_memo) == 0:
		kaijo_memo.append(1)
	while len(kaijo_memo) <= n:
		kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
	return kaijo_memo[n]
gyaku_kaijo_memo = []

def gyaku_kaijo(n):
	if len(gyaku_kaijo_memo) > n:
		return gyaku_kaijo_memo[n]
	if len(gyaku_kaijo_memo) == 0:
		gyaku_kaijo_memo.append(1)
	while len(gyaku_kaijo_memo) <= n:
		gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo), MOD - 2, MOD) % MOD)
	return gyaku_kaijo_memo[n]

def nCr(n, r):
	if n == r:
		return 1
	if n < r or r < 0:
		return 0
	ret = 1
	ret = ret * kaijo(n) % MOD
	ret = ret * gyaku_kaijo(r) % MOD
	ret = ret * gyaku_kaijo(n - r) % MOD
	return ret
(k, q) = map(int, input().split())
d = list(map(int, input().split()))

def query(n, x, m):
	x %= m
	ans = n - 1
	n -= 1
	s1 = 0
	t1 = 0
	for i in range(k):
		s1 += d[i] % m
		if d[i] % m == 0:
			t1 += 1
	s2 = 0
	t2 = 0
	for i in range(n % k):
		s2 += d[i] % m
		if d[i] % m == 0:
			t2 += 1
	x += s1 * (n // k) + s2
	ans -= x // m
	ans -= t1 * (n // k) + t2
	print(ans)
for _ in range(q):
	(n, x, m) = map(int, input().split())
	query(n, x, m)


from collections import *
from itertools import *
from bisect import *
import copy
(k, q) = map(int, input().split())
D = list(map(int, input().split()))
nxm = [list(map(int, input().split())) for i in range(q)]
for (n, x, m) in nxm:
	dlst = [d % m for d in D]
	count0 = [(dlst[0] == 0) * 1]
	lst = [dlst[0]]
	for i in range(1, k):
		lst.append(lst[i - 1] + dlst[i])
		count0.append(count0[i - 1] + 1 * (dlst[i] == 0))
	num2 = (n - 1) // k * lst[-1] + x
	a = (n - 1) % k - 1
	if a >= 0:
		num2 += lst[a]
	num1 = n - 1 - (n - 1) // k * count0[-1] - (num2 // m - x // m)
	if a >= 0:
		num1 -= count0[a]
	print(num1)


import sys
from itertools import accumulate

def solve(n, x, m):
	eee = [d % m for d in ddd]
	acc = list(accumulate(eee))
	zero = [int(e == 0) for e in eee]
	zero_acc = list(accumulate(zero))
	(loop, rem) = divmod(n - 1, k)
	tot = x % m + acc[-1] * loop
	tot_zero = zero_acc[-1] * loop
	if rem > 0:
		tot += acc[rem - 1]
		tot_zero += zero_acc[rem - 1]
	dame = tot // m
	return n - 1 - dame - tot_zero
(k, q) = map(int, input().split())
ddd = list(map(int, input().split()))
buf = []
for line in sys.stdin:
	(n, x, m) = map(int, line.split())
	buf.append(solve(n, x, m))
print('\n'.join(map(str, buf)))


(K, Q) = map(int, input().split())
D = list(map(int, input().split()))
query = []
for i in range(Q):
	query.append(tuple(map(int, input().split())))
for (n, x, m) in query:
	Dmod = [d % m for d in D]
	equal = (n - 1) // K * sum((d == 0 for d in Dmod)) + sum((d == 0 for d in Dmod[:(n - 1) % K]))
	a0 = x
	an = x + sum(Dmod) * ((n - 1) // K) + sum(Dmod[:(n - 1) % K])
	small = an // m - a0 // m
	ans = n - 1 - equal - small
	print(ans)


import numpy as np
(k, q) = list(map(int, input().split()))
d = np.array(input().split(), np.int64)
nxm = [list(map(int, input().split())) for _ in range(q)]
for (n, x, m) in nxm:
	dd = d % m
	ans = n - 1
	(divq, divr) = divmod(n - 1, k)
	dd_r = dd[:divr]
	ans -= (k - np.count_nonzero(dd)) * divq
	ans -= divr - np.count_nonzero(dd_r)
	last = x + np.sum(dd) * divq + np.sum(dd_r)
	ans -= last // m - x // m
	print(ans)


(k, q) = map(int, input().split())
d = list(map(int, input().split()))
for _ in range(q):
	(n, x, m) = map(int, input().split())
	res = 0
	for i in range(k):
		a = d[i] % m
		if a == 0:
			a += m
		res += a * ((n - i - 2) // k + 1)
	print(n - 1 - ((x + res) // m - x // m))


def f_modularness():
	(K, Q) = [int(i) for i in input().split()]
	D = [int(i) for i in input().split()]
	Queries = [[int(i) for i in input().split()] for j in range(Q)]

	def divceil(a, b):
		return (a + b - 1) // b
	ans = []
	for (n, x, m) in Queries:
		(last, eq) = (x, 0)
		for (i, d) in enumerate(D):
			num = divceil(n - 1 - i, K)
			last += d % m * num
			if d % m == 0:
				eq += num
		ans.append(n - 1 - (last // m - x // m) - eq)
	return '\n'.join(map(str, ans))
print(f_modularness())


(K, Q) = map(int, input().split())
D = list(map(int, input().split()))
NXM = [tuple(map(int, input().split())) for i in range(Q)]
for (N, X, M) in NXM:
	ds = [d % M for d in D]
	(a, b) = divmod(N - 1, K)
	s = 0
	z = 0
	for (i, d) in enumerate(ds):
		if i == b:
			sb = s
			zb = z
		if d == 0:
			z += 1
		s += d
	x = (X + s * a + sb) // M - X // M
	y = z * a + zb
	print(N - 1 - x - y)


(k, q) = map(int, input().split())
d = list(map(int, input().split()))

def query(n, x, m):
	res = n - 1
	n -= 1
	x %= m
	s1 = 0
	t1 = 0
	for i in range(k):
		s1 += d[i] % m
		if d[i] % m == 0:
			t1 += 1
	s2 = 0
	t2 = 0
	for i in range(n % k):
		s2 += d[i] % m
		if d[i] % m == 0:
			t2 += 1
	x += s1 * (n // k) + s2
	res -= x // m
	res -= t1 * (n // k) + t2
	return res
for i in range(q):
	(n, x, m) = map(int, input().split())
	print(query(n, x, m))


import sys
sysread = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
import numpy as np

def run():
	(k, q) = map(int, sysread().split())
	d = list(map(int, sysread().split()))
	d = np.array(d, dtype=np.uint64)

	def check(n, x, m):
		_d = d % m
		d0 = d % m == 0
		n_d = (n - 1) // k
		n_rest = (n - 1) % k
		x %= m
		(_d_rest, d0_rest) = (0, 0)
		if n_rest != 0:
			_d_rest = _d[:n_rest].sum()
			d0_rest = d0[:n_rest].sum()
		a_last = x + n_d * _d.sum() + _d_rest
		n_step = a_last // m
		n_same = n_d * d0.sum() + d0_rest
		return n - 1 - n_same - n_step
	for _ in range(q):
		(n, x, m) = map(int, sysread().split())
		print(int(check(n, x, m)))

def __starting_point():
	run()
__starting_point()


import sys
import itertools
import collections
input = sys.stdin.readline

def main():
	(k, q) = map(int, input().split())
	d = list(map(int, input().split()))
	for _ in range(q):
		(n, x, m) = map(int, input().split())
		dmodm = [y % m for y in d]
		kmod = (n - 1) % k
		kdiv = (n - 1) // k
		cnt = 0
		cntm = 0
		for i in range(k):
			cnt += dmodm[i] == 0
			cntm += dmodm[i] == 0 and i < kmod
		dmodcum = list(itertools.accumulate(dmodm))
		p0 = cnt * kdiv + cntm
		an = x + (kmod != 0) * dmodcum[kmod - 1] + dmodcum.pop() * kdiv
		p1 = an // m - x // m
		ans = n - (p0 + p1) - 1
		print(ans)
main()


(K, Q) = map(int, input().split())
(*D,) = map(int, input().split())
for q in range(Q):
	(n, x, m) = map(int, input().split())
	r = (n - 1) % K
	D1r_count0 = 0
	D1_count0 = 0
	D1_sum = 0
	D1r_sum = 0
	for k in range(K):
		if k < r:
			if D[k] % m == 0:
				D1r_count0 += 1
			D1r_sum += D[k] % m
		if D[k] % m == 0:
			D1_count0 += 1
		D1_sum += D[k] % m
	cnt_0 = D1_count0 * ((n - 1) // K) + D1r_count0
	a = x
	b = a + D1_sum * ((n - 1) // K) + D1r_sum
	cnt_p = b // m - a // m
	cnt_n = n - 1 - cnt_0 - cnt_p
	print(cnt_n)