The primary idea is to shift the water in the larger jug to smaller jug, hence the difference of the capacities of the jugs will be used as a constraint to find out variable c volume. we will also terminate the process of finding c if the difference between both the jugs is zero.
There are 4 conditions1) if diff >= min then diff=diff-min_capacity
2) if diff < min then diff=max_capacity - ( min_capacity - diff)
3) if diff == c then return "YES"
4) if diff < 0 then return "NO"
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <algorithm>
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
using namespace std;
int jug(int a,int b,int c)
{
int mx=max(a,b);
int mn=min(a,b);
if(a==c || b==c)
return 1;
int diff=abs(b-a);
while(diff > 0)
{
if( diff==c )
return 1;
if(diff < mn)
{
diff=mx-(mn-diff);
} else if(diff >= mn)
{
diff=diff-mn;
}
}
return 0;
}
int main() {
int a,b,c,mx,mn,diff,t;
cin >> t;
while(t--)
{
cin >>a>>b>>c;
if(jug(a,b,c)==1)
printf("YES\n");
else printf("NO\n");
}
return 0;
}