Problem:
Score : 600 points

Problem Statement

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 1

3 1
3 1 4
5 3 2

Sample Output 1

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.

Sample Input 2

7 3
27 18 28 18 28 46 1000000000
1000000000 1 7
1000000000 2 10
1000000000 3 12

Sample Output 2

224489796
214285714
559523809

Solution in C# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Linq;
using System.Collections.Generic;

namespace Contest
{
    partial class Program
    {
        static void Main(string[] args)
        {
            ABC156F();
        }

        static void ABC156A()
        {
            var nr = ReadIntN();
            var N = nr[0];
            var R = nr[1];
            Console.WriteLine(N >= 10 ? R : R + 100 * (10 - N));
        }
        static void ABC156B()
        {
            var nk = ReadIntN();
            var N = nk[0];
            var K = nk[1];
            int keta = 0;
            while (N > 0)
            {
                N /= K;
                keta++;
            }
            Console.WriteLine(keta);
        }
        static void ABC156C()
        {
            var N = ReadInt();
            var X = ReadIntN();

            int min = Int32.MaxValue;
            for(int p = 1; p <= 100; ++p)
            {
                var cost = X.Select(x => (x - p) * (x - p)).Sum();
                min = Math.Min(min, cost);
            }
            Console.WriteLine(min);
        }
        static void ABC156D()
        {
            var nab = ReadIntN();
            var N = nab[0];
            var A = nab[1];
            var B = nab[2];

            var inv = Enumerable.Range(0, 200001).Select(x => Util.Inverse(x, Util.P)).ToArray();
            Func<int, int, long> func = (X, Y) =>
            {
                long mult = 1;
                for(int i = 1; i <= Y; ++i)
                {
                    mult = (((mult * (X + 1 - i)) % Util.P) * inv[i]) % Util.P;
                }
                return mult;
            };


            long[] pow1 = new long[40];
            long[] pow2 = new long[40];
            pow1[0] = 1;
            pow2[0] = 2;
            for (int i = 1; i < pow1.Length; ++i)
            {
                pow1[i] = (pow1[i - 1] * 2);
                pow2[i] = (pow2[i - 1] * pow2[i - 1]) % Util.P;
            }
            long total = 1;

            for (int i = 0; i < pow1.Length; ++i)
            {
                if ((N & pow1[i]) > 0)
                {
                    total = (total * pow2[i]) % Util.P;
                }
            }

            total += Util.P - 1;
            total = (total + Util.P * 2 - func(N, A)) % Util.P;
            total = (total + Util.P * 2 - func(N, B)) % Util.P;
            Console.WriteLine(total);
        }
        static void ABC156E()
        {
            var nk = ReadIntN();
            var N = nk[0];
            var K = nk[1];

            var func = Util.Combin(N * 2, Util.P);

            if (N <= K)
            {
                Console.WriteLine(func(N * 2 - 1, N));
                return;
            }
            long total = 0;
            for (int i = 0; i <= K; ++i)
            {
                var c1 = func(N, i);
                var c2 = func(N - 1, i);
                total = (total + c1 * c2) % Util.P;
            }
            Console.WriteLine(total);

        }
        static void ABC156F()
        {
            var kq = ReadIntN();
            var D = ReadLongN();
            var K = kq[0];
            for(var q = 0; q < kq[1]; ++q)
            {
                var nxm = ReadLongN();
                var N = nxm[0];
                var X = nxm[1];
                var M = nxm[2];

                var D2 = D.Select(x => x % M).ToArray();
                var D2Sum = D2.Sum();
                var count0 = D2.Count(x => x == 0);

                var last = (X % M) + ((N-1) / K) * D2Sum + D2.Take((int)((N-1) % K)).Sum();
                var zero = ((N-1) / K) * count0 + D2.Take((int)((N-1) % K)).Count(x => x == 0);

                var des = last / M;

                Console.WriteLine(N - des - zero - 1);
            }
        }


        class Util
        {
            public static readonly long P = 1000000007;

            public static long GCD(long a, long b)
            {
                while (a % b > 0)
                {
                    var c = a % b;
                    a = b;
                    b = c;
                }
                return b;
            }
            public static Dictionary<long,int> Factorize(long x)
            {
                var dict = new Dictionary<long, int>();
                long minp = 2;
                while (x > 1)
                {
                    bool ok = false;
                    for(long p = minp; p * p <= x; ++p)
                    {
                        if (x % p == 0)
                        {
                            if (!dict.ContainsKey(p))
                            {
                                dict.Add(p, 0);
                            }
                            dict[p]++;
                            minp = p;
                            x /= p;
                            ok = true;
                            break;
                        }
                    }
                    if (!ok)
                    {
                        if (!dict.ContainsKey(x)) { dict.Add(x, 0); }
                        dict[x]++;
                        break;
                    }
                }
                return dict;
            }
            public static long Inverse(long a, long p)
            {
                long b = p, c = 1, d = 0, e = 0, f = 0;
                while (b != 0)
                {
                    e = a / b;
                    f = b; b = a - e * b; a = f;
                    f = d; d = c - e * d; c = f;
                }
                c %= p;
                return c + (c < 0 ? p : 0);
            }
            public static long[] Facts(int n, long p)
            {
                var facts = new long[n + 1];
                facts[0] = 1;
                for (int i = 1; i <= n; ++i)
                {
                    facts[i] = (i * facts[i - 1]) % p;
                }
                return facts;
            }
            public static Func<int,int,long> Combin(int n, long P)
            {
                var fact = Facts(n, P);
                var inv = fact.Select(x => Inverse(x, P)).ToArray();
                return (N, M) =>
                {
                    if (N < 0 || N < M || M < 0) { return 0; }
                    return (((fact[N] * inv[M]) % P) * inv[N - M]) % P;
                };
            }
        }
        class Geometric
        {
            /// <summary>
            /// 2点間の距離
            /// </summary>
            public static double Distance(Tuple<double, double> s, Tuple<double, double> t)
            {
                var diff1 = s.Item1 - t.Item1;
                var diff2 = s.Item2 - t.Item2;
                return Math.Sqrt(diff1 * diff1 + diff2 * diff2);
            }
            /// <summary>
            /// 2点の中点
            /// </summary>
            public static Tuple<double,double> Center(Tuple<double, double> s, Tuple<double, double> t)
            {
                return new Tuple<double, double>((s.Item1 + t.Item1) / 2, (s.Item2 + t.Item2) / 2);
            }

            /// <summary>
            /// 垂直二等分線 ax+by+c=0の係数(a,b,c)を出力
            /// </summary>
            public static Tuple<double,double,double> Bisector(Tuple<double, double> s, Tuple<double, double> t)
            {
                var a = t.Item1 - s.Item1;
                var b = t.Item2 - s.Item2;
                var c = -a * (s.Item1 + t.Item1) / 2 - b * (s.Item2 + t.Item2) / 2;
                return new Tuple<double, double, double>(a, b, c);
            }

            /// <summary>
            /// 2本の直線が平行か(絶対誤差Epsilon)
            /// </summary>
            public static bool IsParallel(Tuple<double, double, double> line1, Tuple<double, double, double> line2, double epsilon)
            {
                return Math.Abs(line1.Item1 * line2.Item2 - line1.Item2 * line2.Item1) < epsilon;
            }

            /// <summary>
            /// 2本の直線の交点(平行でないことを仮定)
            /// </summary>
            public static Tuple<double,double> Intersection(Tuple<double, double, double> line1, Tuple<double, double, double> line2)
            {
                var div = line1.Item1 * line2.Item2 - line1.Item2 * line2.Item1;
                var x = (line1.Item2 * line2.Item3 - line1.Item3 * line2.Item2) / div;
                var y = (line1.Item3 * line2.Item1 - line1.Item1 * line2.Item3) / div;
                return new Tuple<double, double>(x, y);
            }
        }

        static string ReadStr()
        {
            return Console.ReadLine();
        }
        static string[] ReadStrN()
        {
            return Console.ReadLine().Split(' ');
        }
        static int ReadInt()
        {
            return Convert.ToInt32(Console.ReadLine());
        }
        static int[] ReadIntN()
        {
            return Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray();
        }
        static long ReadLong()
        {
            return Convert.ToInt64(Console.ReadLine());
        }
        static long[] ReadLongN()
        {
            return Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray();
        }
        static Tuple<string, string>[] ReadTupleStr(int n)
        {
            List<Tuple<string, string>> list = new List<Tuple<string, string>>();
            for (int i = 0; i < n; ++i)
            {
                var ab = ReadStrN();
                list.Add(new Tuple<string, string>(ab[0], ab[1]));
            }
            return list.ToArray();
        }
        static Tuple<long, long>[] ReadTupleLong(int n)
        {
            List<Tuple<long, long>> list = new List<Tuple<long, long>>();
            for (int i = 0; i < n; ++i)
            {
                var ab = ReadLongN();
                list.Add(new Tuple<long, long>(ab[0], ab[1]));
            }
            return list.ToArray();
        }
        static Tuple<double, double>[] ReadTupleDouble(int n)
        {
            List<Tuple<double, double>> list = new List<Tuple<double, double>>();
            for (int i = 0; i < n; ++i)
            {
                var ab = ReadLongN();
                list.Add(new Tuple<double, double>(ab[0], ab[1]));
            }
            return list.ToArray();
        }
    }
}