#include<iostream>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<map>
#include<iomanip>
#include<sstream>
#include<vector>
#include<stack>
#include<math.h>
#include<queue>
#include<complex>
#include<random>
#include<ctime>
#include<set>

using namespace std;
#define SAY_YES cout<<"YES"<<endl;
#define SAY_Yes cout<<"Yes"<<endl;
#define SAY_NO cout<<"NO"<<endl;
#define SAY_No cout<<"No"<<endl;
#define IFYES(TRUE_OR_FALSE) if(TRUE_OR_FALSE){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;} 
#define IFYes(TRUE_OR_FALSE) if(TRUE_OR_FALSE){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;} 
#define IFyes(TRUE_OR_FALSE) if(TRUE_OR_FALSE){cout<<"yes"<<endl;}else{cout<<"no"<<endl;} 
#define DEBUG_OUTPUT_ARRAY(XXX,ONE) for(int i=0;i<(ONE);i++){cout<<"DEBUG: i = "<<i<<" -> "<<XXX[i]<<endl;}
#define DEBUG_OUTPUT_ARRAY2(XXX,ONE,TWO) for(int i=0;i<(ONE);i++){cout<<"<<< i = "<<i<<" >>>"<<endl;for(int j=0;j<(TWO);j++){cout<<"DEBUG: j = "<<j<<" -> "<<XXX[i][j]<<endl; } }
#define DEBUG_OUTPUT_ARRAY2_BOX(XXX,ONE,TWO) for(int i=0;i<(ONE);i++){cout<<i<<"  ";for(int j=0;j<(TWO);j++){cout<<XXX[i][j]<<" "; } cout<<endl;}

typedef pair<long long int,long long int> pll;


const long long int mod=1000000007;
const long long int INF=99999999999999999;

long long int k,q,d[5050],n[5050],x[5050],m[5050];

string str;


//階乗先に計算
class Kaizyou{
    public:
    long long int *kaizyou;
    Kaizyou(){};
    Kaizyou(long long int n,long long int mod){
        kaizyou=new long long int[n+1];
        kai(1,n,mod);
        return;
    }

    long long int kai(long long int a, long long int b, long long int mod) {
        long long int tmp = 1;
		kaizyou[0]=1;
        for (long long int i = a; i <= b; i++) {
            tmp *= i;
            tmp %= mod;

            kaizyou[i] = tmp;
        }
        return tmp;
    }
};

//累乗(繰り返し2乗法)　aのb乗(mod)を求める。
class Ruizyou{
    public:
    long long int rui(long long int a, long long int b, long long mod) {
        int memo[65] = {};
        long long int A[65] = {};
        long long int tmp = 1;
        for (int i = 0; i < 65; i++) {
            memo[i] = b % 2;
            b /= 2;
        }

        A[0] = a;
        A[0] %= mod;

        for (int i = 1; i < 65; i++) {
            A[i] = A[i - 1] * A[i - 1];
            A[i] %= mod;
        }
        for (int i = 0; i < 65; i++) {
            if (memo[i] == 1) {
                tmp *= A[i];
                tmp %= mod;
            }
        }
        tmp %= mod;
        return tmp;
    }
};
//コンビネーション計算
class Combination{
    public:
        Kaizyou * Kaizyou_Instance;
        Ruizyou Ruizyou_Instance;
		long long int CombinationMod;
    Combination(long long int n,long long int mod){
        Kaizyou_Instance=new Kaizyou(n,mod);
		CombinationMod=mod;
    }
    long long int comb(long long int n, long long int r) {
        long long int tmp;
		if(r>n){return 0;}
        tmp = (Kaizyou_Instance->kaizyou[n] * Ruizyou_Instance.rui(Kaizyou_Instance->kaizyou[r], CombinationMod - 2, CombinationMod)) % CombinationMod;
        tmp *= Ruizyou_Instance.rui(Kaizyou_Instance->kaizyou[n - r], CombinationMod - 2, CombinationMod);
        tmp %= CombinationMod;
        if (tmp < 0) { tmp = (CombinationMod - tmp) % CombinationMod; }
        return tmp;
    }
	//start~endまでのn+iCiの和を出力
	long long int combRange1(long long int n,long long int start,long long int end){
		if(start!=0)return (comb(n+end+1,end)-comb(n+start,start-1)+3*CombinationMod)%CombinationMod;
		else return comb(n+end+1,end)%CombinationMod;
	}
	//start1~end1のiとstart2~end2のjまでのi+jCiの和を出力
	long long int combRange2(long long int start1,long long int end1,long long int start2,long long int end2){
		return (comb(end1+end2+2,end1+1)-comb(end1+start2+1,start2)-comb(end2+start1+1,start1)+comb(start1+start2,start1)+5*CombinationMod)%CombinationMod;		
	}
};	

long long int sumd[5050]={};
int main() {
	cout << fixed << setprecision(18);
    Combination comb(400050,mod);
    cin>>k>>q;
    for(int i=0;i<k;i++){
        cin>>d[i];
    }
    for(int j=0;j<q;j++){
        cin>>n[j]>>x[j]>>m[j];
    }
    sumd[0]=d[0];
    for(int i=1;i<k;i++){
        sumd[i]=sumd[i-1]+d[i];
    
    }
    //a_N=(N/k)*sumd[k]+sumd[N%k-1]+x_i
    long long int res=0,memo=0;
    for(int i=0;i<q;i++){
        
        long long int d2[5050]={},sumd2[5050]={};
        for(int j=0;j<k;j++){
            d2[j]=d[j]%m[i];
            if(d2[j]==0){d2[j]=m[i];}
        }
        sumd2[0]=d2[0];
        for(int j=1;j<k;j++){
            sumd2[j]=sumd2[j-1]+d2[j];
        }
        res=0;
        memo=x[i]%m[i];
        n[i]--;
        if(n[i]%k==0){memo+=(n[i]/k)*sumd2[k-1];}
        else{memo+=(n[i]/k)*sumd2[k-1]+sumd2[n[i]%k-1];}
        //cout<<memo<<endl;
        res=n[i]-(memo)/m[i];
        
        
        cout<<res<<endl;
    }

} 


Measured memory_bytes: 3712