Timus1286(欧几里德算法的应用)

题目:http://acm.timus.ru/problem.aspx?space=1&num=1286

 

题意:

一个任意大的棋盘,一个Knight每次的移动可以从(i,j)到(i+q, j+p), (i−q, j+p),(i+q, j−p), (i−q, j−p),

(i+p, j+q), (i−p, j+q),(i+p, j−q), (i−p, j−q)  中的任意一个。给定初始坐标(sX,sY) 问Knight是否可以移动

到(dX,dY)?(所有的整数的绝对值均<=2·10^9 )。

 

#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;
typedef long long LL;

LL gcd(LL a,LL b)
{
    return b? gcd(b,a%b):a;
}

void Work(LL p,LL q,LL x1,LL y1,LL x2,LL y2)
{
    LL x,y;
    x = x2 - x1;
    y = y2 - y1;
    if(x == 0 && y == 0)
    {
         puts("YES");
         return;
    }
    if(p == 0 && q == 0)
    {
        puts("NO");
        return;
    }
    LL t = p*x - q*y;
    LL G = gcd(2*p*q,gcd(p*p-q*q,p*p+q*q));
    if(t % G == 0) puts("YES");
    else puts("NO");

}

int main()
{
    LL p,q,x1,y1,x2,y2;
    while(cin>>p>>q>>x1>>y1>>x2>>y2)
    {
        Work(p,q,x1,y1,x2,y2);
    }
    return 0;
}

 

你可能感兴趣的:(Timus1286(欧几里德算法的应用))