#PS0000149. Xóa đoạn ngắn nhất để tổng chia hết (Make Sum Divisible by P)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Xóa đoạn ngắn nhất để tổng chia hết (Make Sum Divisible by P)
Make Sum Divisible by P
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given positive integers and a positive integer , delete a contiguous interval, possibly empty, so that the sum of the remaining entries is divisible by . Deleting the entire array is forbidden. Find the minimum number of entries to delete. Return 0 if no deletion is needed, or if no permitted deletion works.
Input
- Line 1: two integers , the array length and the divisor.
- Line 2: integers in order.
Output
Print the minimum deletion length, or -1 if impossible.
Subtasks
- Subtask 1 (20 points): ; for every ; .
- Subtask 2 (30 points): ; for every ; .
- Subtask 3 (50 points): ; for every ; .
Example 1
Input
4 6
3 1 4 2
Output
1
Explanation
The original sum is 10, not divisible by 6. Deleting the entry 4 at position 3 leaves sum 6. An empty deletion fails, so length 1 is optimal.
Example 2
Input
4 9
6 3 5 2
Output
2
Explanation
The sum 16 leaves remainder 7 modulo 9. No single entry has remainder 7. Deleting the final two entries removes sum 7 and leaves sum 9.
Example 3
Input
3 3
1 2 3
Output
0
Explanation
The sum is already divisible by 3, so nothing needs to be removed.