BZOJ2299 HAOI2011向量(数论)

  设最后的组成为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=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
bool check(int a,int b,long long x,long long y)
{
    if (x&1) return 0;
    if (y&1) return 0;
    x>>=1,y>>=1;
    int n=gcd(a,b);
    return x%n==0&&y%n==0; 
}
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 0;
}

 

posted @ 2018-08-30 08:40  Gloid  阅读(235)  评论(0编辑  收藏  举报