设最后的组成为x=x0a+x1b,y=y0a+y1b。那么容易发现x0和y0奇偶性相同、x1和y1奇偶性相同。于是考虑奇偶两种情况,问题就变为是否存在x和y使ax+by=c,那么其充要条件是gcd(a,b)|c。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
int gcd(int n,int m){return m==?n:gcd(m,n%m);}
bool check(int a,int b,long long x,long long y)
{
if (x&) return ;
if (y&) return ;
x>>=,y>>=;
int n=gcd(a,b);
return x%n==&&y%n==;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj2299.in","r",stdin);
freopen("bzoj2299.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
int T=read();
while (T--)
{
int a=read(),b=read();long long x=read(),y=read();
if (check(a,b,x,y)||check(a,b,x-a,y-b)||check(a,b,x-b,y-a)||check(a,b,x-a-b,y-a-b)) printf("Y\n");
else printf("N\n");
}
return ;
}