#P5497. Universal m-Sequence Problem
Universal m-Sequence Problem
Universal m-Sequence Problem
Given a positive integer sequence \(a = [a_1, a_2, \dots, a_n]\), we call it an m-sequence if there exists at least one contiguous subarray whose sum is a multiple of \(m\). Formally, \(a\) is an m-sequence if there exist indices \(1 \leq i \leq j \leq n\) such that
\[ \sum_{k=i}^{j} a_k \equiv 0 \pmod{m} \]
Your task is to determine whether every sequence of \(n\) positive integers is an m-sequence. In other words, given positive integers \(n\) and \(m\), answer whether for any sequence \(a\) of length \(n\), there always exists a contiguous segment whose sum is divisible by \(m\).
Hint: Use the pigeonhole principle on the prefix sums modulo \(m\).
inputFormat
The input consists of a single line containing two space-separated positive integers \(n\) and \(m\).
outputFormat
Output a single line containing YES
if every sequence of \(n\) positive integers is an m-sequence, or NO
otherwise.
Note: According to the pigeonhole principle, if \(n \ge m\), then any sequence will have two prefix sums that are congruent modulo \(m\) (or a prefix sum congruent to 0), hence the answer is YES
. Otherwise, the answer is NO
.
sample
5 3
YES