#CT0000072. Đếm đoạn chia hết (Divisible Subarrays)
Bài tập này chưa được xếp vào lộ trình.
Mục lục đề ⌄
Đếm đoạn chia hết (Divisible Subarrays)
Divisible Subarrays
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an array of integers and a positive integer .
A contiguous subarray is determined by indices with . It contains , and its sum is
A subarray is valid if its sum is divisible by . Subarrays are considered different if at least one endpoint differs; equal sums do not make two subarrays identical.
Task: Count all valid contiguous subarrays.
Input
- The first line contains two positive integers and .
- The second line contains exactly integers in array order.
Output
Print one integer: the number of contiguous subarrays whose sum is divisible by .
Subtasks
- Subtask 1 (30 points): ; ; for all .
- Subtask 2 (30 points): ; ; for all .
- Subtask 3 (40 points): ; ; for all .
Examples
Example 1
Input
5 3
1 2 3 4 5
Output
7
Explanation
For with , the seven valid intervals are , , , , , , and . Their sums are , respectively, all divisible by .
Example 2
Input
4 2
-1 1 -1 1
Output
4
Explanation
The array is . The four intervals , , , and each sum to , which is divisible by . All other intervals have odd sums.
Example 3
Input
3 1
0 0 0
Output
6
Explanation
An array of length has six choices of . Every element is , so all six sums equal and are divisible by .
Liên quan
Trong các cuộc thi sau: