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

#PS0000148. Kiểm tra đoạn có tổng chia hết (Continuous Subarray Sum)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Kiểm tra đoạn có tổng chia hết (Continuous Subarray Sum)

Continuous Subarray Sum

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given an array of nn nonnegative integers and a positive integer kk, determine whether there is a contiguous interval of at least two elements whose sum is a multiple of kk. A sum of zero is also a multiple of kk.

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 true if such an interval exists, otherwise false (lowercase).

Subtasks

  • Subtask 1 (20 points): 1≤n≤10001\le n\le 1000; 0≤aj≤10000000000\le a_j\le 1000000000 for every 1≤j≤n1\le j\le n; 1≤k≤21474836471\le k\le 2147483647; 0≤∑j=1naj≤231−10\le\sum_{j=1}^n a_j\le 2^{31}-1.
  • Subtask 2 (30 points): 1≤n≤1000001\le n\le 100000; 0≤aj≤10000000000\le a_j\le 1000000000 for every 1≤j≤n1\le j\le n; 1≤k≤101\le k\le 10; 0≤∑j=1naj≤231−10\le\sum_{j=1}^n a_j\le 2^{31}-1.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 0≤aj≤10000000000\le a_j\le 1000000000 for every 1≤j≤n1\le j\le n; 1≤k≤21474836471\le k\le 2147483647; 0≤∑j=1naj≤231−10\le\sum_{j=1}^n a_j\le 2^{31}-1.

Example 1

Input

5 6
23 2 4 6 7

Output

true

Explanation

Positions 2 through 3 contain 2 and 4, have length 2, and sum to 6.

Example 2

Input

5 6
23 2 6 4 7

Output

true

Explanation

The entire array has sum 42, equal to 7⋅67\cdot6, and length 5.

Example 3

Input

5 13
23 2 6 4 7

Output

false

Explanation

The sums of intervals of length at least 2 are 25, 31, 35, 42, 8, 12, 19, 10, 17, 11. None is a multiple of 13.