PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#QHD0000053. Đếm nhóm có tổng chia hết (Divisible Group Sums)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm nhóm có tổng chia hết (Divisible Group Sums)

Divisible Group Sums

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Given nn signed integers a1,…,ana_1,\ldots,a_n, answer qq independent queries about the same array. Each query specifies positive integers D,MD,M.

Count subsets of exactly MM distinct positions whose sum is divisible by DD. The order of selected positions does not matter, but equal values at different positions still count as different choices. Negative integers may occur and remainders must be normalized mathematically.

Input

The first line contains n,qn,q. The next nn lines contain one aia_i each. Then qq lines contain D,MD,M.

Output

Print qq lines. Line jj contains the exact number of valid groups for query jj; no modulo is applied. If M>nM>n, answer 00.

Subtasks

Scoring groups are independent. All constraints listed for a group apply simultaneously.

  • Subtask 1 (20%): 1≤n≤151\le n\le15, 1≤q≤31\le q\le3, 1≤D≤61\le D\le6, 1≤M≤41\le M\le4, −30≤ai≤30-30\le a_i\le30.
  • Subtask 2 (30%): 1≤n≤801\le n\le80, 1≤q≤61\le q\le6, 1≤D≤121\le D\le12, 1≤M≤71\le M\le7, −106≤ai≤106-10^6\le a_i\le10^6.
  • Subtask 3 (50%): 1≤n≤2001\le n\le200, 1≤q≤101\le q\le10, 1≤D≤201\le D\le20, 1≤M≤101\le M\le10, −231≤ai≤231−1-2^{31}\le a_i\le2^{31}-1.

Examples

Input

5 2
1
2
3
4
5
3 2
5 3

Output

4
2

Explanation

For D=3,M=2D=3,M=2, the valid pairs of positions are (1,2)(1,2), (1,5)(1,5), (2,4)(2,4), (4,5)(4,5). For D=5,M=3D=5,M=3, the valid triples are (1,4,5)(1,4,5) and (2,3,5)(2,3,5). The answers are 44 and 22.