Jumping Frog(时间限制: 1 Sec 内存限制: 128 MB)数论&思维

11 篇文章 0 订阅
11 篇文章 0 订阅

题目描述
A frog is located at the coordinate (x1 ,y1 ). He wants to go to the coordinate (x2 ,y2 ). He will perform one or more jumps to reach his destination. The rule of the jumping is as follows:
Suppose the frog is located at the coordinate (x,y); then he can jump to the following four squares:

  1. (x+y,y)
  2. (x-y,y)
  3. (x,y+x)
  4. (x,y-x)

Given the coordinates (x1 ,y1 ) and (x2 ,y2 ), you need to determine if it is possible for the frog to travel from (x1 ,y1 ) to (x2 ,y2 ) through a series of jumps as described.

输入
The first input line contains an integer, n (1 ≤ n ≤ 100), indicating the number of test cases. Each test case consists of four integers (between -1,000,000,000 to +1,000,000,000 inclusive) separated by a single space denoting x1 , y1 , x2 and y2 , respectively.

输出
For each test case, output 1 if the frog can travel from (x 1 ,y 1 ) to (x 2 ,y 2 ) through a series of jumps as described or 0 otherwise.

样例输入
3
-6 8 17 25
13 17 -16 11
0 0 5 6

样例输出
0
1
0

题目大意
一只青蛙位于坐标(x1,y1);它想去坐标(x2,y2);它可以进行一次或多次符合规则的跳跃,请问你它最后是否可以到达目的地。跳跃的规则是:如果它当前的坐标是(x,y),那么它可以走到以下四个点:

  1. (x+y,y)
  2. (x-y,y)
  3. (x,y+x)
  4. (x,y-x)

如果它最后可以走到目的地,输出1,否则输出0。

思路:
由青蛙的跳跃方式可以联想到求gcd的辗转相减法。然后会发现如果刚开始青蛙的坐标是(x,y),那么青蛙最终一定可以到达( gcd(x,y) , 0 );
根据问题可以想到如果哪两个坐标点拥有一个可以共同到达的点,那么青蛙就可以到达它的目的地。然后因为每个坐标点都可以转移到(gcd(x,y),0),所以就判断这两个点的gcd(x1,y1)和gcd(x2,y2)是否相同就可以了,如果相等就输出1,否则就输出0.

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b)
{
    if(b==0)    return a;
    return gcd(b,a%b);
}
int main()
{
    ll t,c,d,a,b;
    cin>>t;
    while(t--)
    {
        cin>>a>>b>>c>>d;
        a=abs(a);
        b=abs(b);
        c=abs(c);
        d=abs(d);
        if(gcd(a,b)==gcd(c,d))
        {
            cout<<"1"<<endl;
        }
        else
            cout<<"0"<<endl;
    }
    return 0;
}
 
/**************************************************************
      Language: C++
    Result: 正确
    Time:2 ms
    Memory:2024 kb
****************************************************************/
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值