#K67457. Divisible Subsequence
Divisible Subsequence
Divisible Subsequence
You are given a sequence of n integers. Your task is to determine whether there exists at least one non-empty subsequence whose sum is divisible by 13.
A subsequence is a sequence that can be derived from the original sequence by deleting some or no elements without changing the order of the remaining elements.
In mathematical terms, given a sequence \(a_1, a_2, \dots, a_n\), find if there exists a non-empty subset of indices \(I \subseteq \{1,2,\dots,n\}\) such that
\(\sum_{i \in I} a_i \equiv 0 \pmod{13}\)
If such a subsequence exists, print YES
; otherwise, print NO
.
inputFormat
The input is given via standard input and consists of two lines:
- The first line contains a single integer n (\(1 \le n \le 10^5\)), which is the number of elements in the sequence.
- The second line contains n space-separated integers \(a_1, a_2, \dots, a_n\) (\(|a_i| \le 10^9\)).
Note: In practice, given the constraints, an efficient algorithm (using dynamic programming with modulo arithmetic) is required to solve this problem.
outputFormat
Output a single line to standard output. The output should be YES
if there exists at least one subsequence whose sum is divisible by 13, and NO
otherwise.
5
1 2 3 4 5
YES
</p>