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

#CT0000072. Đếm đoạn chia hết (Divisible Subarrays)

Bài tập này chưa được xếp vào lộ trình.

Đếm đoạn chia hết (Divisible Subarrays)

Divisible Subarrays

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an array of nn integers a1,a2,…,ana_1,a_2,\ldots,a_n and a positive integer kk.

A contiguous subarray is determined by indices l,rl,r with 1≤l≤r≤n1\le l\le r\le n. It contains al,al+1,…,ara_l,a_{l+1},\ldots,a_r, and its sum is

al+al+1+⋯+ar. a_l+a_{l+1}+\cdots+a_r.

A subarray [l,r][l,r] is valid if its sum is divisible by kk. Subarrays are considered different if at least one endpoint differs; equal sums do not make two subarrays identical.

Task: Count all valid contiguous subarrays.

Input

  • The first line contains two positive integers nn and kk.
  • The second line contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n in array order.

Output

Print one integer: the number of contiguous subarrays whose sum is divisible by kk.

Subtasks

  • Subtask 1 (30 points): 1≤n≤2001\le n\le 200; 1≤k≤1061\le k\le 10^6; −109≤ai≤109-10^9\le a_i\le 10^9 for all 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤50001\le n\le 5000; 1≤k≤1061\le k\le 10^6; −109≤ai≤109-10^9\le a_i\le 10^9 for all 1≤i≤n1\le i\le n.
  • Subtask 3 (40 points): 1≤n≤3⋅1051\le n\le 3\cdot10^5; 1≤k≤1061\le k\le 10^6; −109≤ai≤109-10^9\le a_i\le 10^9 for all 1≤i≤n1\le i\le n.

Examples

Example 1

Input

5 3
1 2 3 4 5

Output

7

Explanation

For 1,2,3,4,51,2,3,4,5 with k=3k=3, the seven valid intervals are [1,2][1,2], [1,3][1,3], [1,5][1,5], [2,4][2,4], [3,3][3,3], [3,5][3,5], and [4,5][4,5]. Their sums are 3,6,15,9,3,12,93,6,15,9,3,12,9, respectively, all divisible by 33.

Example 2

Input

4 2
-1 1 -1 1

Output

4

Explanation

The array is −1,1,−1,1-1,1,-1,1. The four intervals [1,2][1,2], [2,3][2,3], [3,4][3,4], and [1,4][1,4] each sum to 00, which is divisible by 22. All other intervals have odd sums.

Example 3

Input

3 1
0 0 0

Output

6

Explanation

An array of length 33 has six choices of 1≤l≤r≤31\le l\le r\le 3. Every element is 00, so all six sums equal 00 and are divisible by 11.