#C580. Reorder Coins for Divisible Triples
Reorder Coins for Divisible Triples
Reorder Coins for Divisible Triples
You are given several test cases. In each test case, you are provided with a set of coins, each inscribed with a number, along with a divisor k. The task is to determine whether it is possible to reorder the coins such that the sum of any three consecutive coins (considering the coins arranged in a circle) is divisible by k.
More formally, if the coins are arranged as a sequence p[0], p[1], ..., p[n-1], then for every index i (where indices wrap around modulo n), the following condition must hold:
\( (p[i] + p[(i+1) \bmod n] + p[(i+2) \bmod n]) \equiv 0 \pmod{k} \)
If such a reordering exists, output "YES"; otherwise, output "NO" for that test case.
inputFormat
The input is read from standard input (stdin) and has the following format:
T n1 k1 a1 a2 ... an1 n2 k2 b1 b2 ... bn2 ... nT kT c1 c2 ... cnT
Where:
- T is the number of test cases.
- For each test case, the first line contains two integers: n (the number of coins) and k (the divisor).
- The second line contains n space-separated integers representing the numbers on the coins.
outputFormat
For each test case, output a single line containing either "YES" if there exists a reordering of the coins meeting the condition, or "NO" otherwise. The output is written to standard output (stdout).
## sample3
3 3
1 2 3
4 5
5 10 15 20
5 6
7 13 9 5 18
YES
YES
NO
</p>