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

#PS0000160. Đếm đoạn có tổng thuộc tập lớp dư (Count Subarrays With Sum in a Residue Set)

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 thuộc tập lớp dư (Count Subarrays With Sum in a Residue Set)

Count Subarrays With Sum in a Residue Set

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given integers a1,…,ana_1,\ldots,a_n, a positive integer MM and a set SS of tt distinct residues in {0,1,…,M−1}\{0,1,\ldots,M-1\}, define the residue of any integer xx modulo MM as the unique integer rr with 0≤r<M0\le r<M and MM dividing x−rx-r. Count nonempty contiguous intervals whose sum has residue in SS. Intervals with different endpoints are counted separately.

Input

The first line contains three integers n,M,tn,M,t. The second line contains nn integers a1,…,ana_1,\ldots,a_n. The third line contains tt distinct integers s1,…,sts_1,\ldots,s_t representing SS.

Output

Print one integer: the number of qualifying intervals.

Subtasks

  • Subtask 1 (20 points): 1≤n≤5001\le n\le 500; −109≤aj≤109-10^9\le a_j\le10^9 for all 1≤j≤n1\le j\le n; 1≤M≤301\le M\le30; 1≤t≤M1\le t\le M; 0≤sj<M0\le s_j<M for all 1≤j≤t1\le j\le t; the sjs_j are pairwise distinct.
  • Subtask 2 (30 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≤301\le M\le30; t=1t=1; 0≤sj<M0\le s_j<M for all 1≤j≤t1\le j\le t; the sjs_j are pairwise distinct.
  • 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≤301\le M\le30; 1≤t≤M1\le t\le M; 0≤sj<M0\le s_j<M for all 1≤j≤t1\le j\le t; the sjs_j are pairwise distinct.

Example 1

Input

3 3 2
-2 3 1
1 2

Output

5

Explanation

The six intervals have sums -2, 1, 2, 3, 4, 1 in endpoint order. Their residues are 1, 1, 2, 0, 1, 1. Only [2,2] has residue 0, so five qualify.

Example 2

Input

2 1 1
0 -1
0

Output

3

Explanation

Every integer has residue 0 modulo 1. All three intervals [1,1], [1,2], [2,2] qualify.