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
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đế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 integers and a positive integer , count the nonempty contiguous intervals whose element sum is a multiple of . Zero and negative multiples of also qualify.
Input
- Line 1: two integers , the array length and the divisor.
- Line 2: integers in order.
Output
Print the number of qualifying intervals.
Subtasks
- Subtask 1 (20 points): ; for every ; .
- Subtask 2 (30 points): ; for every ; .
- Subtask 3 (50 points): ; for every ; .
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.