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

#PS0000166. Đếm đoạn có tổng đồng dư R (Count Subarrays With Given Residue)

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 đồng dư R (Count Subarrays With Given Residue)

Count Subarrays With Given Residue

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given an array of nn integers a1,a2,…,ana_1,a_2,\ldots,a_n, numbered from 1. A nonempty contiguous interval [l,r][l,r] contains all elements from ll through rr and has length r−l+1r-l+1. Given a positive integer MM and a residue RR, count intervals whose sum TT satisfies T≡R(modM)T\equiv R\pmod M. Equivalently, MM divides T−RT-R, even when TT is negative. Intervals with different endpoints are counted separately.

Input

The first line contains three integers n,M,Rn,M,R. The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n.

Output

Print one integer: the number of qualifying intervals.

Subtasks

  • Subtask 1 (25 points): 1≤n≤2001\le n\le 200; −109≤aj≤109-10^9\le a_j\le10^9 for all 1≤j≤n1\le j\le n; 1≤M≤1091\le M\le 10^9; 0≤R<M0\le R<M.
  • Subtask 2 (25 points): 1≤n≤2000001\le n\le 200000; −109≤aj≤109-10^9\le a_j\le10^9 for all 1≤j≤n1\le j\le n; 1≤M≤201\le M\le 20; 0≤R<M0\le R<M.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; −109≤aj≤109-10^9\le a_j\le10^9 for all 1≤j≤n1\le j\le n; 1≤M≤1091\le M\le 10^9; 0≤R<M0\le R<M.

Example 1

Input

3 3 0
1 2 3

Output

3

Explanation

Intervals [1,2], [3,3], [1,3] have sums 3,3,6, all divisible by 3.

Example 2

Input

2 3 1
-2 1

Output

2

Explanation

The sums in interval order [1,1], [1,2], [2,2] are -2,-1,1, with residues 1,2,1. Two qualify.