Finish coin piles problem
This commit is contained in:
parent
7917bc182f
commit
9e776097b3
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue