PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000149. Xóa đoạn ngắn nhất để tổng chia hết (Make Sum Divisible by P)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 nn positive integers and a positive integer kk, delete a contiguous interval, possibly empty, so that the sum of the remaining entries is divisible by kk. Deleting the entire array is forbidden. Find the minimum number of entries to delete. Return 0 if no deletion is needed, or −1-1 if no permitted deletion works.

Input

  • Line 1: two integers n,kn,k, the array length and the divisor.
  • Line 2: nn integers a1,…,ana_1,\ldots,a_n in order.

Output

Print the minimum deletion length, or -1 if impossible.

Subtasks

  • Subtask 1 (20 points): 1≤n≤10001\le n\le 1000; 1≤aj≤10000000001\le a_j\le 1000000000 for every 1≤j≤n1\le j\le n; 1≤k≤10000000001\le k\le 1000000000.
  • Subtask 2 (30 points): 1≤n≤1000001\le n\le 100000; 1≤aj≤10000000001\le a_j\le 1000000000 for every 1≤j≤n1\le j\le n; 1≤k≤101\le k\le 10.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤aj≤10000000001\le a_j\le 1000000000 for every 1≤j≤n1\le j\le n; 1≤k≤10000000001\le k\le 1000000000.

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 1+2+3=61+2+3=6 is already divisible by 3, so nothing needs to be removed.