Problem 818
Continuous Subarray Sum
Medium · 20 pts · Array, Hash Table, Prefix Sum, Math
Problem
Decide whether the array has a block of consecutive elements of length at least 2 whose sum is a multiple of K (zero counts as a multiple of every K). A block from i to j has a sum divisible by K exactly when the prefix sums before i and after j leave the same remainder modulo K. Store the first index where each remainder appears and check whether the same remainder shows up again at least two positions later. Some values computed along the way can be larger than a 32-bit int can hold (about 2.1 × 10^9), even when the input looks small, so use a 64-bit integer type: long long in C++, long in Java (Python needs nothing special).
Input
The first line contains N and K. The second line contains the N integers.
Output
Print YES if such a subarray exists, otherwise NO.
Constraints
1 <= N <= 100000; 0 <= A[i] <= 10^9; 1 <= K <= 10^9
Example input
5 6 23 2 4 6 7
Example output
YES
Explanation
In 23 2 4 6 7 with K = 6, the block 2 4 has sum 6, a multiple of 6, and length 2. So the answer is YES.