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
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đế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 integers , numbered from 1. A nonempty contiguous interval contains all elements from through and has length . Given a positive integer and a residue , count intervals whose sum satisfies . Equivalently, divides , even when is negative. Intervals with different endpoints are counted separately.
Input
The first line contains three integers . The second line contains integers .
Output
Print one integer: the number of qualifying intervals.
Subtasks
- Subtask 1 (25 points): ; for all ; ; .
- Subtask 2 (25 points): ; for all ; ; .
- Subtask 3 (50 points): ; for all ; ; .
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.