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

#PS0000147. Đếm đoạn có tổng chia hết cho K (Subarray Sums Divisible by K)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đếm đoạn có tổng chia hết cho K (Subarray Sums Divisible by K)

Subarray Sums Divisible by K

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given nn integers a1,…,ana_1,\ldots,a_n and a positive integer kk, count the nonempty contiguous intervals whose element sum is a multiple of kk. Zero and negative multiples of kk also qualify.

Input

  • Line 1: two integers n,kn,k, the array length and the divisor.
  • Line 2: nn integers a1,…,ana_1,\ldots,a_n in order.

Output

Print the number of qualifying intervals.

Subtasks

  • Subtask 1 (20 points): 1≤n≤10001\le n\le 1000; −10000≤aj≤10000-10000\le a_j\le 10000 for every 1≤j≤n1\le j\le n; 2≤k≤100002\le k\le 10000.
  • Subtask 2 (30 points): 1≤n≤300001\le n\le 30000; 0≤aj≤100000\le a_j\le 10000 for every 1≤j≤n1\le j\le n; 2≤k≤100002\le k\le 10000.
  • Subtask 3 (50 points): 1≤n≤300001\le n\le 30000; −10000≤aj≤10000-10000\le a_j\le 10000 for every 1≤j≤n1\le j\le n; 2≤k≤100002\le k\le 10000.

Example 1

Input

6 5
4 5 0 -2 -3 1

Output

7

Explanation

For divisor 5, the qualifying intervals and their sums are: [1,6] (sum = 5), [2,2] (sum = 5), [2,3] (sum = 5), [2,5] (sum = 0), [3,3] (sum = 0), [3,5] (sum = -5), [4,5] (sum = -5). There are exactly 7 such intervals; no other interval has a divisible sum.

Example 2

Input

1 9
5

Output

0

Explanation

The only interval sums to 5, which is not divisible by 9.