From 9e776097b3e70499aab8aad5720d2fc1388d21f9 Mon Sep 17 00:00:00 2001 From: Jake Bauer Date: Wed, 1 Nov 2023 20:17:31 +0100 Subject: [PATCH] Finish coin piles problem --- coin-stack.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 coin-stack.cpp diff --git a/coin-stack.cpp b/coin-stack.cpp new file mode 100644 index 0000000..e68e558 --- /dev/null +++ b/coin-stack.cpp @@ -0,0 +1,49 @@ +#include +#include + +int +main(void) +{ + long t; + std::cin >> t; + + long a, b; + for (long i = 0; i < t; i++) + { + std::cin >> a >> b; + + // A pattern emerges where, if the sum of the coin stacks is + // even, there is no way to empty them following this scheme + // unless the sum is divisible by 3. Also, the size of + // one stack cannot be more than double the size of the other. + + // First solution was failing on some numbers, but since it's + // too difficult to go through the hundred thousand outputs that + // were tested, let's just try doing this manually. + if (a < b) + { + std::swap(a, b); + } + + // No way to empty stacks without having one with leftover coins + if (a > 2 * b) + { + std::cout << "NO" << std::endl; + } + else + { + // Stacks must be divisible by three to be valid, so + // let's reduce the values to the bare minimum. + a %= 3; + b %= 3; + if (a < b) + std::swap(a, b); + if ((a == 2 && b == 1) || (a == b && b == 0)) + std::cout << "YES" << std::endl; + else + std::cout << "NO" << std::endl; + + } + } + return 0; +}