You are given a sequence of numbers a1, a2, ..., an, and a number m.
Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m.
Input
The first line contains two numbers, n and m (1 ≤ n ≤ 106, 2 ≤ m ≤ 103) — the size of the original sequence and the number such that sum should be divisible by it.
The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).
Output
In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist.
Examples
Input
3 5
1 2 3
Output
YES
Input
1 6
5
Output
NO
Input
4 6
3 1 1 3
Output
YES
Input
6 6
5 5 5 5 5 5
Output
YES
Note
In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5.
In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist.
In the third sample test you need to choose two numbers 3 on the ends.
In the fourth sample test you can take the whole subsequence.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given n up to 1e6, m up to 1000. We need to check if there exists a non-empty subsequence (not necessarily contiguous) whose sum is divisible by m.
This is a classic problem: given n numbers and modulo m, if n >= m, then by pigeonhole principle, there is always a subset with sum divisible by m. Actually the standard claim: For any set of n integers, there exists a non-empty subset whose sum is divisible by n. But here m is up to 1000 and n may be large. However the classic pigeonhole argument: Consider prefix sums modulo m. If any prefix sum is 0 mod m, we are done. If not, then we have n prefix sums modulo m, each in {1,...,m-1}. If n >= m, then by pigeonhole principle, two prefix sums are equal, and the difference (subarray sum) is divisible by m. But careful: That gives a contiguous subarray, not an arbitrary subsequence. However the problem asks for subsequence, not necessarily contiguous. But if a contiguous subarray works, it's also a subsequence, so that's sufficient. But if n < m, the pigeonhole argument may not guarantee a subarray sum divisible by m. However we need subsequence, not subarray. Is the problem about any subsequence (non contiguous) or subarray? The problem statement: "subsequence aij" but likely means subsequence, i.e., choose any set of indices (order preserved but not necessarily contiguous). In many such problems, if n >= m, the answer is always YES because we can consider the prefix sums modulo m as above; that gives a contiguous subarray. But is that always valid for subsequence? Yes, a contiguous subarray is a subsequence, so the answer is YES if n >= m? Let's check: The classic pigeonhole: Given n numbers, consider the m remainders of prefix sums. If n >= m, then either some prefix sum is 0 mod m (done) or there are m prefixes (including 0 at index 0) so we have m+1? Wait careful: The typical argument: Let prefix sums S_k = sum_{i=1}^k a_i mod m for k=0,...,n, where S_0 = 0. That's n+1 numbers. If n+1 > m, by pigeonhole, two are equal, so the sum of the segment between them is divisible by m. So if n >= m, then n+1 >= m+1 > m, so we have a contiguous subarray. So indeed if n >= m, answer always YES regardless of the numbers? Is that always true? For the pigeonhole argument, we consider residues modulo m. The number of residues possible is m. With n+1 prefix sums (including 0), if n+1 > m, then there must be at least two with the same residue, so difference divisible by m. So the condition is n+1 > m, i.e., n >= m. So for n >= m, answer is always YES. But is there any catch? The numbers a_i can be 0? Yes, they can be zero. But that doesn't break the argument. However, what about the possibility that the subsequence needs to be non-empty, but the segment sum might be zero? The segment could be of length zero if we compare prefix sums at the same index, but we require distinct indices. So we take two different indices, and the subarray sum is positive length (if both indices are distinct). So okay.
But wait: In the problem, n can be up to 1e6, m up to 1000, so if n >= m, answer always YES. So we can just check if n >= m: answer YES. But is that always correct? Let's test examples: Example 1: n=3, m=5, n<m, but answer YES because there exists subsequence sum 5? Actually they found 2+3=5, divisible by 5. So n<m still can be YES. Example 2: n=1, m=6, n<m, answer NO. Example 3: n=4, m=6, n<m (4<6) but answer YES. So the n>=m condition gives trivial YES for n>=6. But in example 4: n=6, m=6, n>=m (6>=6), answer YES. So indeed for n>=m, answer always YES? Let's test with some small counterexamples: Suppose m=3, n=3, numbers: [1,1,1]. Prefix sums: 1,2,0 mod3? Actually compute: a1=1 => S1=1; S2=2; S3=0 mod3 (since 3). So S3=0 => subarray [1,1,1] sum=3 divisible by3 -> YES. Okay. Suppose numbers: [1,1,2] for m=3: prefix sums:1,2,1 mod3? Actually S1=1, S2=2, S3=4 mod3=1. So S0=0 and S3=1, no zero. But there are two equal residues: S1 and S3 both 1 -> subarray from index 2 to 3 sum = a2+a3=1+2=3 divisible by 3 -> YES. So it seems to work. But could there be a case where n>=m but no non-empty subset sum divisible by m? The pigeonhole argument for contiguous subarray works if we consider prefix sums including S0. So yes, always there is a contiguous subarray. So answer is YES. So the only interesting case is when n < m. In that case, we need to determine if any non-empty subsequence sum is divisible by m. n is less than m, but m ≤ 1000, so n ≤ 999 (since n < m ≤ 1000). Actually n can be up to 1e6, but when n >= m we already answer YES, so the problematic case is n < m, where n ≤ 999 (since m ≤ 1000). So we can handle that by checking all subsets? But n can be up to 999, which is too many subsets (2^999). However we can use dynamic programming on modulo m, because m is small (≤1000). Since n is less than m, but could be up to 999, we can do DP to check reachable sums modulo m. That's O(nm) which in worst-case n=999, m=1000 => about 1e6 operations, fine. But careful: n may be up to 1e6, but if n >= m we skip DP, else we do DP. So overall algorithm: if n >= m: print YES. else: DP on booleans of size m: reachable[0..m-1]. Initially reachable[0]=True (empty subset sum 0). For each a_i, compute r = a_i % m. Then update reachable: For all s from 0 to m-1, if reachable[s] then new_reachable[(s+r)%m] = True. But careful: typical DP for subset sum modulo m must avoid double counting same element? We can process each element and update using a copy, or do backwards? Since each element can be used at most once (subsequence, i.e., each element can be chosen at most once), it's exactly subset sum. So we can do DP with bitset or boolean array. Complexity O(nm) which for n< m <= 1000, at most 1e6, fine.
But there's a nuance: The pigeonhole principle for n >= m always gives a subarray, which is a subsequence. So answer always YES. But is it always true? Let's double-check: For the prefix sum argument, we considered S_0 = 0, S_1, ..., S_n. There are n+1 prefix sums. If n+1 > m, two have same remainder mod m. Their difference (sum of elements between indices i+1 to j) is divisible by m. This is a contiguous segment, which is a valid subsequence. So yes, always a non-empty subsequence exists. So answer YES.
Wait, but is there any subtlety about a_i being zero? No. Or about the fact that a_i can be up to 1e9, but mod m we only care about remainders. The prefix sums may be large, but mod m they are fine. So the argument holds regardless of the actual values. So for n >= m, answer always YES. This is a known result: If you have n integers, there always exists a non-empty subset whose sum is divisible by n. But that's for subset, not necessarily contiguous, and modulus equals n. However here modulus m can be less than n. The pigeonhole argument with prefix sums yields a contiguous subarray, which is a subset, so indeed a subset sum divisible by m exists if n >= m. So correct.
But is there any scenario where n = m? Then n+1 > m, so yes. For m=2, n=1? n<m, need to check. n=2? n>=m? 2>=2 -> yes, always YES. Indeed any two numbers? Consider a=[1,1], m=2: sum of both is 2 divisible by 2. Yes. If a=[1,2], sum 3 mod2=1, but subarray? prefix sums: S0=0, S1=1, S2=3 mod2=1 -> two equal residues (S1 and S2) => subarray from index 2: a2=2 => 2 mod2=0 -> yes. So always.
Thus algorithm: read n, m, if n >= m: print YES and exit. else: read array, compute DP.
But wait, we must consider that a_i can be 0 and m up to 1000. DP will handle it.
Edge cases: n could be 0? No, n>=1.
Time constraints: n up to 1e6, but reading input is okay. If n >= m, we can just read the rest of the line and ignore? Actually we need to consume input; but we can after reading n,m, if n>=m, we can just read the rest of line (the n numbers) and print YES. That's fine, reading 1e6 integers is okay but may be a bit heavy; but we must read them anyway because input is given. We can just read all numbers but not process. In C++ we can read and discard; but reading 1e6 ints is fine.
But we must ensure that the DP is only done when n < m, which is at most 999 numbers. So we can handle.
Let's test with examples:
Example1: n=3, m=5 => n<5, DP: a=[1,2,3] => modulo:1,2,3. DP: reachable[0]=true. After processing: 1-> reachable[1]=true; 2: from 0->2, from1->(1+2)=3; also 0,1 already; so reachable {0,1,2,3}. 3: from0->3, from1->4, from2->5%5=0, from3->1; so reachable[4] and 0 already; reachable[0]=true -> YES. So prints YES.
Example2: n=1, m=6, n<6 => DP: a=[5] => r=5; reachable: {0,5}; 0 is true from empty, but need non-empty; 0 from empty doesn't count; we need a subset with sum%6=0 and non-empty. reachable[0] from non-empty? Actually in DP we start with empty set sum=0 reachable. After processing, reachable[0] may become true via using elements that sum to multiple of m. For a single element 5, (0+5)%6=5, so reachable[0] remains false (unless we also have element 0). So answer NO.
Example3: n=4, m=6, n<6 => a=[3,1,1,3] => modulo:3,1,1,3. DP: we can find subset 3+3=6 => YES. DP should find reachable[0] true after processing. So YES.
Example4: n=6, m=6, n>=m => YES.
Thus algorithm seems correct.
But is there any scenario where n >= m but no non-empty subsequence sum divisible by m? Consider the argument: The prefix sums are S_0=0, S_i = sum_{j=1}^i a_j mod m. If any S_i = 0 for i>=1, we have subarray from 1 to i. If not, then all S_i for i>=1 are non-zero, and there are n such sums plus S_0 = 0. Total n+1 residues. Since n+1 > m (n>=m), and there are only m possible residues (0...m-1), by pigeonhole at least two indices i<j have same residue. Then the subarray from i+1 to j has sum ≡ 0 mod m. So always yes. So no counterexample.
But wait: The argument requires that there are n+1 prefix sums. But if n >= m, n+1 > m. So indeed there must be a collision. However, what if some a_i are zero? That doesn't affect. So okay.
Thus the solution is trivial when n >= m.
But is it possible that the subsequence can be any subset, not necessarily contiguous? Yes, but we have stronger condition, so it's fine.
Let's double-check the constraints: n up to 10^6, m up to 10^3. So n may be huge, but we just need O(n) time to read and possibly O(1) for answer. So it's efficient.
But there is one nuance: The problem statement says "subsequence" meaning any subset (preserving order, but order irrelevant for sum). So the pigeonhole principle gives a subarray, which is a special subsequence. So yes.
Thus final solution: If n >= m: print YES. Else: DP for subset sums modulo m; if reachable[0] from non-empty set: print YES else NO.
Implementation details: For DP we can use a bitset of size m. Initialize bitset with bit 0 set. For each a_i, compute r = a_i % m. Then we can do: bitset = bitset | (bitset << r) shifted and then modulo? In C++ we can use bitset<1000> and implement shift with modulo wrapping. But easier: Use boolean array reachable[1000] and a temporary copy. Since m ≤ 1000, we can do:
bool reachable[m] = {false};
reachable[0] = true;
for each a:
int r = a % m;
bool new_reachable[m];
copy from reachable to new_reachable initially same? Actually we need to update based on previous state before considering this element. So we can create a copy of reachable and then for each s where reachable[s] is true, set new_reachable[(s+r)%m] = true. But careful to not use the updated reachable within same iteration. So we can either iterate backwards? For subset DP, if we iterate s from m-1 down to 0, we can update in-place without affecting later s because we are using reachable[s] from previous state (since we go downwards and we only set (s+r)%m which might be larger or smaller, but if we go downwards, it's not safe because (s+r)%m could be less than s, and we might then use that updated value when processing a later s (with smaller index). That's why we need a separate array. Since m small, we can just use two arrays or a bitset and shift with OR then modulo. Using bitset shift and then OR, but modulo wrap needed. There's trick: bitset b; b = b | ((b << r) | (b >> (m-r))) for modulo? Actually for circular shift: (b << r) will shift left r bits, bits beyond m-1 are lost; but we need to take modulo m, so bits that go beyond m-1 should wrap around to lower bits. So we can compute (b << r) and then OR with (b >> (m - r)) for the wrap part. But careful: r could be 0. In that case we skip. This is efficient: bitset<1000> reachable; reachable.set(0); for each a: r = a%m; if(r==0) continue; reachable = reachable | ((reachable << r) | (reachable >> (m - r))); But note that the right shift should fill with zeros on left, which is fine. However, bitset shift is not circular; we need to manually wrap. So we can do:
But we must ensure that we don't double count the same element multiple times? Actually using OR with shifted is like adding the element to each existing subset, but because we are using the current reachable, this might allow using the element multiple times? Wait, in subset DP, when we process an element, we want to update reachable to include subsets that include this element, but we must use the state before adding this element. If we do:
reachable = reachable | (reachable << r); // but this is using the current reachable for both sides; that would allow using the element multiple times? Actually if we do reachable = reachable | (reachable << r), where reachable on RHS is the state before update, but in typical bitset we cannot refer to old state after assignment; so we need to store old state. For bitset we can:
But here both uses of reachable refer to the state before update. So that's correct: We compute the shift of the old reachable and OR with old reachable to get new reachable. That's the typical method for 0/1 knapsack with bitset, but we need modulo wrap. However, this method effectively does unlimited uses? Actually because we only shift once and OR, each element is considered exactly once, because we are using the old reachable to compute contributions and then OR with old reachable to produce new reachable. That's correct for one copy of each element.
But careful: (reachable << r) will shift bits beyond m-1 out of range. But we need modulo m, so we need to wrap the bits that go beyond. So we need to combine left shift and right shift to simulate circular shift modulo m. However, is that correct for subset sum modulo m? Let's think: For each existing remainder s, we want to add r to get (s+r)%m. The shift left by r gives bits at positions s+r, but if s+r >= m, they go out of range; we need to bring them back to (s+r) mod m, which is s+r - m (if s+r >= m). That's essentially a left shift with wrap-around. So we need to compute:
new_reachable = old_reachable OR (old_reachable rotated left by r). Where rotate left by r means for each bit at position i, move to (i+r) % m. So we can simulate with: ((old_reachable << r) | (old_reachable >> (m - r))) but careful: The right shift will shift bits to lower indices and fill left with zeros. For a bit at position i, if i+r < m, it goes to i+r via left shift; if i+r >= m, then i+r - m = i - (m - r). So it goes to i - (m - r). To get that, we need to take old_reachable, shift right by (m - r), which moves bits from higher indices down. However, this right shift will also shift bits from positions < (m - r) down? Actually right shift by k moves bit at position i to i - k (if i >= k), and bits at positions less than k disappear (shifted out). That's exactly what we want: bits at i where i >= m - r? Let's formalize: For rotation left by r: bit at i goes to (i+r) mod m. This can be expressed as: if i < m - r: new position = i + r; else: new position = i - (m - r). So we can compute: left shift by r: sets bits at i+r for i from 0 to m-1-r; those are the bits that satisfy i+r < m. For bits where i >= m - r, left shift would set bits beyond m-1, which we need to wrap. The right shift by (m - r) takes bits at positions i where i >= m - r and shifts them to i - (m - r), which is exactly the wrapped positions. However, right shift also moves bits from positions i where i < m - r? They shift to negative and disappear, which is fine because those correspond to bits that were handled by left shift. So the combination (old_reachable << r) | (old_reachable >> (m - r)) yields exactly the rotated bits. But caution: The right shift may also shift bits from positions i < m - r? For a bitset, right shift by k moves bit at position i+k to i? Wait, typical bitset shift: bitset << r shifts bits to the left: new[i+r] = old[i] for i from 0 to m-1-r, and bits beyond m-1 are discarded. So left shift sets bits at positions i+r where i from 0 to m-1-r. For right shift: new[i] = old[i+r]? Actually in C++ bitset, right shift (>>) shifts bits to the right: new[i] = old[i+r] for i such that i+r < m. But that's opposite: Let's check: If we have bitset b; b >> r; then the bit at position i in the result equals the bit at position i+r in the original b (if i+r < m), else 0. So it's like moving bits to lower indices. So if we want to take bits at positions i where i >= m - r and move them to i - (m - r), we need to do a right shift by (m - r) of the original bits. That would give: new[j] = old[j + (m - r)] for j such that j+(m-r) < m, i.e., j < r. So the right shift will place bits from original positions (m - r) ... (m-1) into positions 0 ... r-1. That's exactly the wrapped bits. So yes, the expression ((old << r) | (old >> (m - r))) yields the rotated left by r. But note: The right shift part may also include bits from original positions less than m-r? Actually if we do old >> (m - r), bits at original positions 0 ... (m - r -1) are shifted to negative indices and disappear. Bits at positions (m - r) ... (m-1) become at positions 0 ... r-1. So it's fine.
Thus we can use bitset for efficiency and readability. But we need to ensure that m is constant? In C++, bitset size must be compile-time constant. Since m ≤ 1000, we can use bitset<1000> or bitset<1024>. That's fine.
Implementation steps:
Read n, m.
If n >= m: read all a_i (maybe just consume input) and print "YES". But we still need to read the second line of n integers. Could skip, but easier to just read all.
However, note: If n is large (1e6) and we just print YES without reading the rest, the input reading function may leave unread data, which might cause issues with next test case? But this is a single test case program? Typically Codeforces problems have a single test case. So we could simply ignore reading the numbers and just print YES and exit. But that would skip reading the rest of input, which might be considered acceptable? In online judges, they only provide the input once, and we can exit without reading everything, but it's generally not a problem because the OS will discard unread data. However, sometimes judges expect the program to consume all input; not doing so might cause runtime errors if using fast I/O that expects to read everything? Typically it's safe to exit early, but to be safe, we might read the numbers anyway. But reading 1e6 ints is trivial and fast.
But we can also read them using scanf in a loop but not process. So it's fine.
However, there is nuance: if n >= m, we answer YES and we don't need DP. So we can output YES and return 0. No need to read the numbers? But we must read the numbers because they are part of input; if we don't read them, the program might attempt to read them later for subsequent operations (if any), but we exit immediately. So it's okay: we print and then return. The unread data in stdin will be discarded. Most judges don't mind. But to be safe, we can just read them anyway; it's not expensive.
We can read the second line with a loop reading n numbers but do nothing.
In C++ we can just use cin to read numbers; even if we exit after printing YES, the input buffer may not be fully consumed, but it's fine.
Else (n < m): read the array, maybe store it? Since we only need DP, we can process on the fly. For each a_i, compute r = a_i % m, then update DP.
DP: use bitset reachable; reachable.set(0);
For each a:
int r = a % m;
if (r == 0) { reachable.set(0); } Actually if r==0, then the element alone gives sum 0 mod m, so we can set reachable[0] = true. But we can also handle generically via rotation.
else {
bitset shifted = ((reachable << r) | (reachable >> (m - r)));
reachable = reachable | shifted;
}
// early exit: if reachable[0] is true, we can break and print YES.
After processing, if reachable[0] is true (but careful: reachable[0] could be from empty set; but we started with empty set true. So we need to ensure that the true for 0 corresponds to a non-empty subset. In DP, if we ever set reachable[0] via a non-empty combination, we will have true as well. But initially true. So we cannot simply check reachable[0] at the end; we need to know if it came from empty. However, we can track whether we have found a non-empty subset that sums to 0 modulo m. During processing, if we ever set reachable[0] from a shift (i.e., from a non-empty combination), we can detect it. But if we just check reachable[0] at the end, it's true initially, so always YES? That would be wrong. So we need to differentiate. We can either not set reachable[0] initially, but then we need to consider that empty subset is not allowed. So we can start with reachable all false, and for each element, we update: if we include the element alone, we get r. Also we can combine with previous reachable. But it's easier to start with reachable[0] = false, and also keep a separate boolean for empty? Actually the DP can be done as: Let reachable[mod] be whether there exists a non-empty subset with that remainder. Initially all false. For each a_i, we compute r = a_i % m. Then we can update: new_reachable = reachable OR (shifted reachable) OR {r}? Actually we need to include the subset consisting of just this element. So we can set new_reachable[r] = true. And also for each existing subset s, we can create (s+r)%m. So the update can be: new_reachable = reachable OR (rotate_left(reachable, r)) OR set bit r. But we also need to consider that the element alone gives r, and also combined with previous subsets. So we can incorporate the element alone by setting bit r in reachable before shift? Actually we can handle in one go: start with a bitset current. For each element, we compute new = current | (rotate_left(current, r)) | bitset with bit r set. That yields the new reachable after including this element as an option. However, we must ensure we don't double count the element itself when combined with empty? The empty subset corresponds to bit 0 not set (since empty is not allowed). So if we set bit r directly, that corresponds to the subset containing only this element. That's correct. Then rotating left of current (which doesn't include empty) gives subsets that include this element combined with previous non-empty subsets. That's fine.
Given the small constraints, it's simpler to use DP with boolean arrays.
Implement DP as:
bool dp[m] = {false};
// Process each number
for each a:
int r = a % m;
bool newdp[m];
copy dp to newdp initially (since old subsets remain)
// Actually we need to create newdp from dp: we start with newdp = dp (subsets without using this element)
// Then for each s where dp[s] is true, we set newdp[(s+r)%m] = true.
// Also, the subset consisting of only this element: newdp[r] = true.
But careful: when we do newdp = dp and then for each s, set newdp[(s+r)%m] = true, we might be adding the element to subsets that already include it? No, because dp is the set of subsets before considering this element. So newdp will include: all old subsets (dp), all old subsets with this element added (thus new remainders), and the singleton subset of this element. However, the singleton subset is actually a special case of adding to empty subset if empty subset were in dp. Since we don't have empty, we need to explicitly set newdp[r] = true. So that works.
Implementation:
bool dp[m] = {false};
for each a:
int r = a % m;
bool ndp[m];
memcpy(ndp, dp, sizeof dp); // copy old
// add singleton
ndp[r] = true;
// add combinations
for (int s = 0; s < m; ++s) {
if (dp[s]) {
ndp[(s+r)%m] = true;
}
}
// copy back
memcpy(dp, ndp, sizeof dp);
if (dp[0]) { // non-empty subset sum 0 found
print YES and exit.
}
Complexity: O(n*m) which for n <= 999, m <= 1000 is up to ~1e6, fine.
But note: We are copying arrays of size up to 1000 each iteration, which is okay.
However, we can optimize by using bitset and mask, but not necessary.
But there is even simpler: Since n < m, we can also use the pigeonhole principle on subset sums? Actually there is known result: If you have n numbers and n < m, it's not guaranteed; we need to check. But DP is fine.
But wait: The problem constraints: n up to 1e6, m up to 1000. So if n >= m we answer YES; else n < m, and in that case n <= 999, so DP is O(n*m) ≤ 1e6, which is acceptable.
Edge Cases:
If any a_i % m == 0, then we can answer YES immediately. Even in the DP case, we can early exit. Similarly, if we find subset sum 0.
In the case n >= m, we can answer YES without checking anything.
But is there any scenario where n >= m but the subarray found by prefix sums may be of length zero? That would happen if two prefix sums are equal, and we take i and j such that i < j, then subarray from i+1 to j has length j-i >0. So it's non-empty. So fine.
Now, let's double-check the pigeonhole principle: We have n+1 prefix sums (including S0). If n+1 > m, there exist i<j with same remainder. The subarray sum from i+1 to j is S_j - S_i. That sum may be zero? If S_j = S_i, then difference is 0 mod m, but that sum might be 0 in integer sense? Actually the sum could be a multiple of m, possibly 0. But if the sum is 0, then the subarray elements sum to 0, which is divisible by m. That's fine. So it's a valid subsequence. So yes.
Therefore, the solution is straightforward.
But before finalizing, consider a potential pitfall: The problem says subsequence, not subarray. But our pigeonhole gives subarray, which is a subsequence. So it's valid. However, is it possible that the subarray we get is of length zero? Only if i=j, but we consider i<j, so length >0.
So algorithm stands.
Now, I'll produce the final answer with explanation and code.
Implementation in C++:
code
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
scanf("%d %d", &n, &m);
if (n >= m) {
// read and discard numbers
for (int i = 0; i < n; ++i) scanf("%d", &x);
puts("YES");
return 0;
}
// n < m <= 1000
vector<bool> dp(m, false);
for (int i = 0; i < n; ++i) {
int x;
scanf("%d", &x);
int r = x % m;
vector<bool> ndp = dp; // copy
// singleton
ndp[r] = true;
// combinations
for (int s = 0; s < m; ++s) {
if (dp[s]) {
ndp[(s + r) % m] = true;
}
}
dp.swap(ndp);
if (dp[0]) {
puts("YES");
return 0;
}
}
puts("NO");
return 0;
}
But we can optimize a bit: Instead of copying the whole array each time, we can update in place using a temporary array from old state. The above uses copy each iteration (O(m) per element), which is fine. Could also use bitset with rotate.
But one nuance: In the DP, when we set ndp = dp and then for each s where dp[s] true, set ndp[(s+r)%m] = true, we may be setting ndp[r] twice (once from singleton, once from dp[0] if dp[0] true and (0+r)%m = r). That's okay.
But careful: Starting with dp all false, first element: ndp = dp (all false), then ndp[r]=true, then for s loop, nothing. So dp becomes {r: true}. Good.
Second element: dp currently has r1 true. Then ndp = dp (so includes r1). Then ndp[r2] = true. Then for s: for s=r1, ndp[(r1+r2)%m] = true. So we get subsets: {a1}, {a2}, {a1,a2}. Good.
Thus correct.
Potential improvement: if dp[0] becomes true, we can early exit.
Now, consider when n is very large and we answer YES without reading all numbers. We might skip reading the numbers. But as mentioned, we can just read and discard. I'll write code that reads the numbers anyway to avoid unused input warnings? Not necessary. But for simplicity and safety, I'll read all numbers even when n>=m. However, if n is 1e6, reading them is okay. But we can also skip reading by ignoring the rest of input after printing YES and exiting. I think it's fine to exit immediately; the judge doesn't require consuming entire input. So I'll do: if (n >= m) { printf("YES\n"); return 0; } but we haven't read the numbers yet. However, after reading n and m, there are still n integers in the input. If we exit without reading them, the input buffer will still have them, but the program terminates; the OS will clean up. In online judges, it's generally acceptable to not read all input if you have already determined the answer. But to be on safe side (some judges might have multiple test cases, but this problem appears to be single test case), it's fine. I'll adopt reading them anyway as it's not a performance issue. However, reading 1e6 ints is trivial. But the early exit would save time? Not needed.
But there's a catch: If n >= m, we answer YES always. But what if the input includes numbers that cause issues? No.
Thus solution done.
But wait: There's also a known trick: if any a_i % m == 0, answer YES. But our DP would catch that quickly. In the case n >= m, we still answer YES even if no zero mod. So it's fine.
Now, we need to provide a thorough explanation for the answer. The user said "You are given a sequence ..." and we need to output solution. So we will provide explanation and code.
However, the user might be expecting an answer that is the solution (like in Codeforces editorial). So I'll produce a final answer: description of algorithm and code.
Potential alternative: There's also the possibility of using meet-in-the-middle if n is large but we haven't used it. But our solution is fine.
One more nuance: What about when m is large, up to 1000, but n could be 10^6. Our condition n>=m holds often, so answer YES quickly. That's efficient.
Edge Cases:
n = 0? Not allowed.
m = 1? But constraints say m >= 2. So not needed.
a_i can be up to 10^9, but modulo works.
The DP must handle r = 0: then ndp[0] = true via singleton. So early exit.
Now, check example 3: n=4,m=6 <6? Actually 4<6. DP: first a=3: dp[3]=true; not 0. second a=1: ndp copy dp[3]=true; ndp[1]=true; combinations: from s=3 -> (3+1)%6=4 => ndp[4]=true; dp now {1,3,4}. third a=1: ndp copy {1,3,4}; ndp[1] true (already); combinations: from s=1 -> (1+1)=2 -> ndp[2]=true; from s=3 -> (3+1)=4 (already); from s=4 -> (4+1)=5 -> ndp[5]=true; so dp {1,2,3,4,5}. fourth a=3: ndp copy {1,2,3,4,5}; ndp[3] true (already); combinations: from s=1 -> (1+3)=4 (already); s=2->5 (already); s=3->0 -> ndp[0]=true => YES. So works.
Now, complexity: O(min(n,m) * m) essentially.
Now, the code:
code
#include <bits/stdc++.h>
using namespace std;
const int MAXM = 1000;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
if (n >= m) {
// consume the rest of input
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
}
cout << "YES\n";
return 0;
}
vector<bool> dp(m, false);
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
int r = x % m;
vector<bool> ndp = dp;
ndp[r] = true;
for (int s = 0; s < m; ++s) {
if (dp[s]) {
ndp[(s + r) % m] = true;
}
}
dp.swap(ndp);
if (dp[0]) {
cout << "YES\n";
return 0;
}
}
cout << "NO\n";
return 0;
}
But using cin for 1e6 numbers may be slower than scanf. However, with ios::sync_with_stdio(false) and cin.tie(0), it's fast enough for 1e6 integers (typical 1e6 ints ~ a few MB). But to be safe, we can use scanf for faster input. However, for simplicity, cin is okay. But many competitive programmers use scanf for large input. Since n can be 1e6, reading with cin may be borderline but still fine with optimization. I'll use scanf for reliability.
Rewrite with scanf:
code
#include <cstdio>
#include <vector>
using namespace std;
int main() {
int n, m;
scanf("%d %d", &n, &m);
if (n >= m) {
int x;
for (int i = 0; i < n; ++i) scanf("%d", &x);
puts("YES");
return 0;
}
vector<bool> dp(m, false);
for (int i = 0; i < n; ++i) {
int x;
scanf("%d", &x);
int r = x % m;
vector<bool> ndp = dp;
ndp[r] = true;
for (int s = 0; s < m; ++s) {
if (dp[s]) {
ndp[(s + r) % m] = true;
}
}
dp.swap(ndp);
if (dp[0]) {
puts("YES");
return 0;
}
}
puts("NO");
return 0;
}
Potential improvement: Use bitset to avoid loops over m for each element? But m is small, loop is fine.
But note: vector may not be efficient due to bit packing, but for m up to 1000, it's fine.
Alternative: Use static array bool dp[1000] and bool ndp[1000]; with memcpy. That's also fine.
I'll produce final answer with explanation and code.
Now, double-check the example where n>=m but m is 1000 and n is 1000000, the program will loop reading all numbers? In my code, if n>=m, we loop reading n numbers and then print YES. That's O(n) which is okay (1e6). But we could early exit without reading, but it's fine.
But we could also do: if (n >= m) { puts("YES"); return 0; } without reading the numbers. However, we need to skip the rest of input. But if we don't read, the program ends and the OS discards the rest. That's acceptable. But in some judges (like Codeforces), it's fine to exit without reading all input. I'd prefer to read to avoid any potential issues. But it's not necessary.
I'll choose to read the numbers anyway.
But there is a nuance: if n >= m, we always answer YES, but we must be careful: is it always true even if m=0? m>=2. So fine.
Now, I think solution is correct.
I'll produce final answer with explanation and code.
We'll also mention the pigeonhole principle.
Thus final response. The problem asks whether there exists a non‑empty subsequence whose sum is divisible by (m).
The key observation is: if (n \ge m), the answer is always YES. Proof: Consider the prefix sums (S_0=0, S_k = \sum_{i=1}^k a_i \pmod m) for (k=1,\dots,n).
There are (n+1) values, each in ({0,1,\dots,m-1}). Since (n+1 > m) (because (n \ge m)), by the pigeonhole principle two of them are equal, say (S_i = S_j) with (i<j). Then the sum of elements (a_{i+1},a_{i+2},\dots,a_j) is (S_j - S_i \equiv 0 \pmod m). This contiguous subarray is a valid subsequence.
If (n < m) (now (n \le 999) because (m \le 1000)), we cannot guarantee existence. We must check all possible subsequences. Since both (n) and (m) are small in this case, we can use dynamic programming over residues modulo (m):
Let dp[r] be true if there exists a non‑empty subsequence with sum (\equiv r \pmod m).
Initially all dp[r] are false.
For each number x, compute r = x % m. Create a temporary array ndp = dp (keep previous subsequences), set ndp[r] = true (subsequence consisting only of this element), and for every s with dp[s] = true set ndp[(s+r)%m] = true (add this element to existing subsequences).
If after any step dp[0] becomes true, we can stop and answer YES.
If after processing all numbers dp[0] is still false, the answer is NO.
The overall complexity is (O(\min(n,m) \cdot m)) which is fast for the given limits ((n \le 10^6), (m \le 10^3)).
cpp
#include <cstdio>
#include <vector>
int main() {
int n, m;
scanf("%d %d", &n, &m);
// If n >= m, answer is always YES (pigeonhole principle)
if (n >= m) {
int x;
for (int i = 0; i < n; ++i) scanf("%d", &x); // consume input
puts("YES");
return 0;
}
// n < m, so n <= 999, m <= 1000 – use DP over residues
std::vector<bool> dp(m, false);
for (int i = 0; i < n; ++i) {
int x;
scanf("%d", &x);
int r = x % m;
std::vector<bool> ndp = dp; // old subsequences remain
ndp[r] = true; // single-element subsequence
// add current element to every existing subsequence
for (int s = 0; s < m; ++s) {
if (dp[s]) {
ndp[(s + r) % m] = true;
}
}
dp.swap(ndp);
if (dp[0]) { // found a subsequence sum ≡ 0 (mod m)
puts("YES");
return 0;
}
}
puts("NO");
return 0;
}
So we can initialize current bitset all false. Then for each a_i:
int r = a_i % m;
// create a bitset with just this element
bitset single; single.set(r);
bitset rotated = ((current << r) | (current >> (m - r)));
current = current | rotated | single;
// early exit: if r == 0 or current[0] becomes true, print YES and return.
But we must also consider the case where we have two elements that sum to multiple of m, etc.
However, note that using rotate left on current (which doesn't have empty) is fine because we only combine with existing subsets.
Alternatively, we could use DP array with two states: a boolean array reachable[1000] and initialize reachable[0] = true for empty, but then at the end, we check if any subset other than empty gives 0. We can track if we have ever set reachable[0] by a non-empty combination. That can be done by using a boolean flag or by not counting empty. Simpler: use DP array reachable[1000] with meaning "we can achieve this remainder with some subset (possibly empty)". We'll keep two arrays: old and new. We'll start old[0]=true (empty). For each element, we compute new = old (i.e., keep old subsets) plus subsets formed by adding the element to any old subset. So new[ (s+r)%m ] = true if old[s] true. Then set old = new. At the end, if old[0] is true and it came from a non-empty subset, we need to know. But since we started with empty, old[0] may remain true even if no non-empty subset sums to 0. However, we can detect if there is any non-empty subset giving 0 by checking if we ever set new[0] = true from a transition where s != 0 or from adding the element to a non-empty subset? Actually if we start with old[0]=true, then when we process an element, if we add the element to the empty subset (i.e., s=0), we get new[r] = true, not 0. So only way to set new[0] is if there exists an old subset with remainder s such that (s+r)%m == 0, i.e., s = m - r mod m. That old subset could be empty only if s=0, which would require r==0. So if r==0, then adding the element to any subset will give same remainder s, and in particular, adding to empty gives 0? Actually if r=0, then (s+0)%m = s, so we wouldn't set new[0] from transition unless s=0. But s=0 from empty subset: new[0] = old[0] (since we keep old). That's not new from non-empty. However, if we include the element alone (which is essentially adding to empty), that gives remainder 0? Wait, if element's remainder is 0, then the subset consisting of just that element has sum 0 mod m. In our DP update, we should set new[0] = true because either we keep old subsets (which includes empty) but we need to also include the subset consisting of just this element. The transition from s=0 with adding the element yields remainder 0? Actually (0+0)%m = 0. So if we process element with r=0 and we consider transition from empty (s=0), we would set new[0] = true. But that uses empty as the base. That's allowed because we are creating a subset that includes the element, which is non-empty. So if r=0, new[0] will become true via transition from empty, representing the non-empty subset of that single element. So that's correct: using old[0]=true for empty, the DP transition will correctly mark non-empty subsets that sum to 0. So at the end, old[0] being true could be due to empty alone. However, if there is no non-empty subset summing to 0, old[0] would still be true because empty remains. But we can differentiate by checking if we ever had a non-empty subset that gave 0. One way: at the end, if old[0] is true and we have processed at least one element and either n>0 and we have a subset that gives 0, but we need to be sure. So we can use a boolean flag found = false; whenever we update new[0] and the source s was not 0 (or if r=0? Actually we can just check if new[0] becomes true and either (s != 0) or (r == 0 and we are considering transition from s=0? That's messy). Better approach: start with reachable[0] = true only for empty, but at the end we can check if reachable[0] is true and there exists at least one element used to achieve it, i.e., the DP path is non-empty. But we don't store that. Another approach: use reachable[0] = false initially, and treat empty separately. For DP we can track reachable_nonempty[m] as booleans. Simpler: use bitset and start with 0 not set. Then after processing all elements, if bit 0 is set, then there is a non-empty subset summing to 0. But careful: If we start with empty not represented, then we need to include single-element subsets as we go. The update: current = current | (rotate_left(current, r)) | (1 << r). That's exactly what we described. That works because empty is not included, so any set we get corresponds to a non-empty subset. However, one nuance: If an element itself has remainder 0, then we set bit 0 via the single set, so that's fine. Also, combination of elements may yield 0. So at the end, if bit 0 is set, we have a non-empty subset sum divisible by m. So we can do that. The bitset approach without empty is simpler for checking.
However, we must be careful with the rotate operation: When we compute rotated = ((current << r) | (current >> (m - r))), this only rotates the bits that are set in current. But current does not contain bit 0 initially. So rotated may be empty at first. Then we OR with single (bit r). That's fine.
But we also need to consider that we might want to keep the previous subsets (i.e., we don't lose them). So current = current | rotated | single. This includes all previous subsets (current) plus new subsets formed by adding this element to any previous subset (rotated) plus the single-element subset. That's correct.
However, note that if r == 0, then rotated = ((current << 0) | (current >> m)) = current, because left shift 0 leaves current; right shift by m yields all zeros because shifting by full size yields 0. So rotated = current. Then current = current | current | single (where single has bit 0). So current will have bit 0 set (since single). So it works.
But there's a subtle point: For r != 0, the right shift by (m - r) could shift bits from current that are less than m - r? As discussed, that's fine because those bits correspond to positions i where i < m - r; shifting right by (m - r) would move bit at i to i - (m - r) if i >= m - r, else disappears. Since i < m - r, they disappear; they are the bits that are handled by left shift. So it's correct.
Also, if m - r equals 0? That occurs when r == m, but r is modulo m, so r ∈ [0, m-1]. So m - r > 0 when r > 0. So safe.
So we can implement bitset with size 1000 (or 1024). But we need m variable; we only use first m bits. The bits beyond m-1 in the bitset will remain unused. The shift operations may set bits beyond m-1; but we only care about bits 0..m-1. When we left shift by r, bits may go to indices >= m. Those we discard because we only consider bits within range? Actually they could be set in the bitset but we don't care, but they could interfere with later wrap if we don't mask. However, our rotate operation uses right shift by (m - r) to bring those bits back; but the left shift will set bits beyond m-1, and those bits are not used in the OR because they are outside our considered range. But then when we later do right shift by (m - r), those bits beyond m-1 are not shifted into range because they are beyond the size; the right shift only shifts bits that are within the bitset; bits beyond m-1 are not part of the bitset. Actually in C++ bitset, the size is fixed. When you left shift, bits that go beyond size-1 are discarded. So left shift << r will only set bits at positions i+r that are < N (size). So if we have bitset<1000> and we only care about indices 0..m-1 (m<=1000), left shift will discard bits beyond 999. So if i+r >= 1000, they are lost. But we need to wrap within modulo m, not modulo 1000. Since m may be less than 1000, some bits might be lost if i+r >= 1000? But i is at most m-1 ≤ 999, r ≤ m-1 ≤ 999, so i+r could be up to 1998. But with bitset size 1000, left shift will only keep bits where i+r < 1000. That is, if i+r >= 1000, the bit is lost. However, we want to wrap modulo m, not modulo 1000. But we are only interested in bits 0..m-1 (m ≤ 1000). So for wrapping, we need to bring bits where i+r >= m into range 0..m-1 by subtracting m. But left shift with size 1000 could also bring bits that go beyond m-1 but still within 0..999, which we don't want to lose; we need to wrap them. However, using only bitset size 1000, we can handle wrap by combining left shift and right shift appropriately. But we must consider that i+r might be in [m, 999] (i.e., within the bitset size but beyond our considered range). We need to map those to (i+r - m) which is also within 0..999 if m ≤ 1000? Actually i+r - m could be as low as negative? i ≤ m-1, r ≤ m-1, so i+r - m ≤ 2m-2 - m = m-2 ≥ -? It can be negative if i+r < m, then we don't wrap. So the wrapped indices are in [0, m-2]. So they are within 0..998, which is still within bitset size. So the bits we need to wrap are also within the bitset size. However, our left shift will place bits at i+r regardless of whether i+r >= m. That's not what we want: we need to place them at (i+r) mod m, not at i+r. So if we just left shift, we would set bits at positions i+r, which for i+r >= m are not the correct modulo positions. So we need to wrap them. That's why we need rotate, not shift.
The rotate operation: ((current << r) | (current >> (m - r))) gives us the bits at positions (i+r) mod m. But note that both shifts are on the whole bitset of size N. When we left shift by r, bits that go to indices >= N are lost. But we only care about indices 0..m-1, and m ≤ N. However, for i such that i+r < N (i.e., less than bitset size), the left shift will set bit at i+r. But we only want to keep those where i+r mod m is correct; we want to map i+r to (i+r) mod m, which might be less than i+r. So the left shift alone sets bits at i+r (which may be > m-1). That's not correct. So to perform rotation modulo m, we need to ensure that we only keep bits within indices 0..m-1 after the operation. That means we must mask the result to only bits 0..m-1. So we can compute rotated = ((current << r) | (current >> (m - r))) and then mask with (1<<m)-1? In bitset, we can use a mask. Or we can just operate on a bitset of size m? But bitset size must be constant; we could declare bitset<1000> and then after each operation, clear bits beyond m-1. That's doable: after computing rotated, we can do rotated &= mask; where mask is a bitset with bits 0..m-1 set. Since m is at most 1000, we can create a mask: bitset<1000> mask = (1<<m)-1? But (1<<m) for m up to 1000 is huge and won't fit in integer. So we need to set mask manually: loop set bits 0..m-1, which is O(m) but m ≤ 1000, okay per iteration? But we have n up to 999 iterations, so up to 1e6 operations, fine. Alternatively, we can use vector DP and avoid bitset tricks. Simpler: Use boolean array DP of size m, and for each element, we compute new reachable as described using two arrays.