#PS0000160. Đếm đoạn có tổng thuộc tập lớp dư (Count Subarrays With Sum in a Residue Set)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đế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 , a positive integer and a set of distinct residues in , define the residue of any integer modulo as the unique integer with and dividing . Count nonempty contiguous intervals whose sum has residue in . Intervals with different endpoints are counted separately.
Input
The first line contains three integers . The second line contains integers . The third line contains distinct integers representing .
Output
Print one integer: the number of qualifying intervals.
Subtasks
- Subtask 1 (20 points): ; for all ; ; ; for all ; the are pairwise distinct.
- Subtask 2 (30 points): ; for all ; ; ; for all ; the are pairwise distinct.
- Subtask 3 (50 points): ; for all ; ; ; for all ; the 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.