Coin Piles problem on CSES

Question Link
https://cses.fi/problemset/task/1754
Why am I getting this error? I tried but could not find out why.

terminate called after throwing an instance of ‘std::bad_alloc’
what(): std::bad_alloc
Aborted (core dumped)

Here is the code: #include <bits/stdc++.h>#define mod 1000000007;#define ll long longusing n - Pastebin.com

a and b can be as large as 10^9, you’re trying to allocate huge arrays

what can be the solution to this problem? I could only think of making a 2d matrix to optimize my code.

Isnt this problem pretty trivial? U can solve this by basic maths.

Let x be the number of operations of the first type and let y be the operations of the second type then 2x+y=a and 2y+x=b. Solving these equations yields the values of x,y as \{\frac{2a-b}{3}, \frac{2b-a}{3}\}. Just check whether these two values are nonegitive and integers and this will solve the problem

#include <bits/stdc++.h>
#define ll long long
#define mod 998244353
#define rep(i,s) for(long long i=0; i<s ; i++)
#define f(i,a,b) for(long long i(a); i<b ; i++)
#define inf -pow(10,9)
#define MAXN 100000 
const int INF = 1000000000;
const ll modi = 1000000007;
const ll N=100001;
using namespace std;

int main() 
{
	int t;
	cin >> t;
	rep(w2,t){
		ll n,m;
		cin >> n >> m;
		ll r1=(2*n-m);
		ll r2=(2*m-n);
		if(r1>=0 && r2>=0){
			if(r1%3==0 && r2%3==0){
				cout << "YES" << endl;
			}
			else{
				cout << "NO" << endl;
			}
		}
		else{
			cout << "NO" << endl;
		}
	}
}

My code which got ac

1 Like