Finish coin piles problem

This commit is contained in:
Jake Bauer 2023-11-01 20:17:31 +01:00
parent 7917bc182f
commit 9e776097b3
1 changed files with 49 additions and 0 deletions

49
coin-stack.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <iostream>
#include <utility>
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;
}