Coin Piles
Decide whether two piles can both be emptied when each move removes two coins from one pile and one coin from the other.
Problem Summary
Original task summary for this archive page. The official CSES statement is linked in the header instead of being mirrored here.
Each move removes \(2\) coins from one pile and \(1\) coin from the other. For each pair of pile sizes, decide whether it is possible to empty both piles exactly.
Editorial
The solution write-up is rendered from the LaTeX source, with equations kept live through MathJax.
Two conditions are necessary.
First, every move removes exactly \(3\) coins in total, so the combined number of coins must be divisible by \(3\): \[ (a + b) \bmod 3 = 0. \]
Second, one pile cannot be too large compared with the other. In every move, the larger pile can lose at most \(2\) coins while the smaller pile loses at least \(1\). If one pile starts with more than twice as many coins as the other, the smaller pile will run out too early. That gives \[ \max(a, b) \le 2 \cdot \min(a, b). \]
These conditions are also sufficient. When both hold, the piles can be reduced to zero by choosing which pile loses \(2\) coins at each step.
So the entire problem collapses to a constant-time arithmetic check.
Complexity Analysis
Time and memory costs for the approach used in the implementation below.
Time: \(O(1)\) per test case
Memory: \(O(1)\)
C++ Solution
The exact repository source used for this solution page.
#include <algorithm>
#include <cstdint>
#include <iostream>
int main() {
int t;
std::cin >> t;
while (t--) {
std::int64_t a, b;
std::cin >> a >> b;
const bool divisible_by_three = (a + b) % 3 == 0;
const bool balanced_enough = 2 * std::min(a, b) >= std::max(a, b);
std::cout << (divisible_by_three && balanced_enough ? "YES" : "NO") << '\n';
}
return 0;
}
Notes / Pitfalls
Short reminders about edge cases, construction details, or common mistakes.
Do not simulate the moves. The arithmetic conditions fully characterize the answer.
Source Files and Assets
Raw files are still available here when you want the original TeX, C++, or statement assets.