# Coin Piles problem from cses

Hi all
I am trying to solve this problem but unable to find the solution.
Any kind of hint would be great.

1 Like

This is what I did:
Try removing coins from bottom, It’s more intuitive that way.

if(a<b)swap(a,b);
if(a>2*b){
cout<<"NO\n";
}else{
a%=3,b%=3;
if(a<b)swap(a,b);
if((a==2 && b==1) || (a==b && b==0)){
cout<<"YES\n";
}else{
cout<<"NO\n";
}
}

4 Likes

I really wish they had solutions for cses, It has really good problems.

1 Like

I was also thinking this way but couldn’t come with a%=3,b%=3; this step.

1 Like

Here is my solution.

  if(a<b) swap(a,b);

if(a>2*b)
{
cout<<"NO" << endl;
return 0;
}

if((a+b)%3==0)
{
cout<<"YES"<< endl;
}
else cout<<"NO"<< endl;
1 Like

I know this is from a while ago, but i thought there wasn’t sufficient explanations in any of the other answers.

Lets say x times we take 2 from a , and 1 from b
and y times we take 2 from b and 1 from a

meaning:
a = 2x + 1y
b = 1x + 2y

on solving for a and b

2a - b = 3x
2b - a = 3y

x and y should be non negative , and from here we can derive (a+b)%3==0.

10 Likes

how did you come up?

since he swaps a and b if a is less than b,so a must be atmost 2b if we want to empty both piles as 2 coins from a and 1 coin from b will be used but if a > 2b then whole pile of b will become empty and some coins will still be left in a.

Let’s assume that we are removing x number of times 2 coins from pile a and 1 coin from pile b.

Let’s assume that we are removing y number of times 1 coin from pile a and 2 coins from pile b.

Mathematically we can write our assumptions as:
a = 2x + y \;\;\cdots (i)
b = x + 2y \;\; \cdots (ii)

We have two equations and we have two unknowns, means we can solve for x and y.
After solving the above equations for x and y, you will get:
x = \frac{2a - b}{3}
y = \frac{2b - a}{3}

Clearing x and y \in Z^{+} and they must be a multiple of 3.

One more important information we will get when we subtract (ii) from (i) i.e.
|a - b| = |x - y|

Source Code: https://github.com/strikersps/Competitive-Programming/tree/master/CSES/Coin-Piles

1 Like

My solution.

   #include<iostream>
void swap(int *,int *);
int main(){
int t, a, b, x;
std::cin >> t;
while ( t-- ) {
bool flag = false;
std::cin >> a >> b;
if (b>a) swap(&a , &b) ;
x = a-b;
if (x > b) flag = true;
a -= 2 * x;
b -= x;
if (a != b) flag = true;
if (a % 3 != 0) flag = true;
if (flag) std::cout << "NO\n" ;
else std :: cout << "YES\n" ;
}
return 0;
}
void swap (int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}

I am confused by what you mean when you say the following

I do not understand what you mean by x number of times and y number of times, and also I am confused as to why taking 1 coin from pile b affects the a equation (i.e. a = 2x + y).

Any sort of clarification would be very helpful. Thank you.

This is what I did :

void solve(){
ll a, b;
cin >> a >> b;
if ((a + b) % 3 == 0 && 2 * a >= b && 2 * b >= a )
{
cout << "YES";
}
else cout << "NO";


}

It means …When we reduce “a” by 2 then we must reduce “b” by 1 and when we reduce “a” by 1 the we must reduce “b” by 2. So if We assume that we take x times 2 and y times 1 to reach a to 0 then we must take x times 1 and y times 2 to reduce “b” to 0. i.e
a=2x+1y
b=1x+2y

Thanks for the clear explanation!!

My sol:
#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
long long unsigned int t,a,b;

cin>>t;
while(t--)
{
cin>>a>>b;
if((2*a-b)%3==0 and (2*b-a)%3==0)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}

return 0;


}

Thank you for explaining it in detail

thanks">

?>

why you are doing this

if(a>2*b){
cout<<"NO\n";
}


Hey, In the first line, he swaps a,b if a is smaller. So now, a will always be greater than equal to b.
In the worst case, if a is very large (as compared to b). We will always remove 2 from a and 1 from b (as a is the larger one).
eg - 24 12.
but if a is greater than 24 in this case - say 25,26,100 etc and b is 12.
The best you can do is keep removing 2 from a and 1 from b. but if a > 24, b will become 0 and a will still be nonzero.