from collections import defaultdict def preprocess_prefix_sums(A, K): N = len(A) prefix = [0] * (N + 1) for i in range(1, N + 1): prefix[i] = prefix[i - 1] + (1 if A[i - 1] % K == 0 else 0) return prefix import sys input = sys.stdin.read data = input().split() N = int(data[0]) Q = int(data[1]) A = list(map(int, data[2:N+2])) queries = data[N+2:] unique_ks = set() query_data = [] for i in range(Q): L = int(queries[3 * i]) - 1 R = int(queries[3 * i + 1]) K = int(queries[3 * i + 2]) unique_ks.add(K) query_data.append((L, R, K)) prefix_sums = {} for K in unique_ks: prefix_sums[K] = preprocess_prefix_sums(A, K) results = [] for L, R, K in query_data: prefix = prefix_sums[K] result = prefix[R] - prefix[L] results.append(str(result)) sys.stdout.write('\n'.join(results) + '\n')