You are given a sequence of k numbers: d_0, d_1, ..., d_{k - 1}. Process q queries, each containing three integers n_i, x_i, and m_i. For each query, calculate a sequence of n_i numbers and count the occurrences where (a_j mod m_i) is less than (a_{j + 1} mod m_i). The input consists of k, q, d_i values, and n_i, x_i, m_i for each query. Output the count for each query. Constraints: 1 ≤ k, q ≤ 5000, 0 ≤ d_i ≤ 10^9, 2 ≤ n_i, x_i, m_i ≤ 10^9.

# Buggy version:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.readline
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import combinations
import numpy as np
def run():
    k,q = map(int, sysread().split())
    d = list(map(int,sysread().split()))
    d = np.array(d, dtype=np.uint32)

    def check(n ,x, m):
        _d = d % m
        d0 = d % m == 0
        # n_step
        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_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(check(n, x, m))

if __name__ == "__main__":
    run()

# Fixed version:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.readline
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import combinations
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_step
        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_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)))

if __name__ == "__main__":
    run()

# Buggy version:
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)
    print(rem_sum)
if __name__ == '__main__':
  main()

# Fixed version:
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)
if __name__ == '__main__':
  main()

# Buggy version:
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())

        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)


if __name__ == '__main__':
    main()


# Fixed version:
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)


if __name__ == '__main__':
    main()

# Buggy version:
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 d[i] % m == 0:
			a += m
		if n % k > i:
			res += a * (n//k + 1)
		else:
			res += a * (n//k)
	print( (n-1) - ((x+res)//m - x//m) )


# Fixed version:
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) )

# Buggy version:
#>=はmの倍数をまたいだ回数
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]
    ##あまり0をあまりmで処理
    for i in range(k):
        if dx[i]==0:
            dx[i]=m
    sm = x%m+sum(dx)*(n//k)+sum(dx[:n%k])
    print(n-1 -sm//m)


# Fixed version:
#>=はmの倍数をまたいだ回数
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]
    ##あまり0をあまりmで処理
    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)

# Buggy version:
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 + sum(md)) // m - md0 * roop - mda0
        print(res)


main()


# Fixed version:
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()

# Buggy version:
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
        print(i, c)
        ans += c
        f += c * (d[i] % m)
    print(ans, f)
    ans -= f // m
    print(ans)

for _ in range(q):
    solve()

# Fixed version:
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()

# Buggy version:
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-((n//k)*sum(l)+sum(l[:n%k-1]))//m)

# Fixed version:
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)

# Buggy version:
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 - (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)

# Fixed version:
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)

# Buggy version:
k, q = list(map(int, input().split()))
d = list(map(int, input().split()))
nxm = [list(map(int, input().split())) for _ in range(q)]

for i in range(q):
  n, x, m = nxm[i]
  D = [[0] * 2 for _ in range(k)]
  D[0][0] = d[0] % m

  for j in range(k):
    D[j] = [(D[j - 1][0] + d[j] % m), 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
  ans -= t // m
  t = t % m
  if M != 0:
    ans += D[M - 1][1]
    t += D[M - 1][0]
    ans -= t // m
  
  print(ans)
     



# Fixed version:
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)

# Buggy version:
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
    ans += a*((n-1)//k)
    #print(pd)
    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])

# Fixed version:
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)
    #print(pd)
    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])

# Buggy version:
,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)


# Fixed version:
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))

# Buggy version:
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
    num0 = (n-1)//K*zeroes[K] + zeroes[(n-1)%K]
    print(n-1-s//m-num0)

# Fixed version:
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]
    #print(zeroes, sum)  
    s = (n-1)//K*sum[K] + sum[(n-1)%K]
    s += x%m
    #print(s)
    num0 = (n-1)//K*zeroes[K] + zeroes[(n-1)%K]
    print(n-1-s//m-num0)

# Buggy version:
k, q = map(int, input().split())
d = list(map(int, input().split()))
for _ in range(q):
    n, x, m = map(int, input().split())
    cum = [0]*(k)
    cnt = [0]*(k)
    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//k)+cum[(n-1)%k]
    s = res//m
    print(n-1-s-cnt[-1]*(n//k)-cnt[(n-1)%k])


# Fixed version:
k, q = map(int, input().split())
d = list(map(int, input().split()))
for _ 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])