题目
780.到达终点
题目大意
给定四个整数 sx
, sy
,tx
和 ty
,如果通过一系列的转换可以从起点 (sx, sy)
到达终点 (tx, ty)
,则返回 true
,否则返回 false
。
从点 (x, y)
可以转换到 (x, x+y)
或者 (x+y, y)
。
样例
示例 1:
输入: sx = 1, sy = 1, tx = 3, ty = 5
输出: true
解释:
可以通过以下一系列转换从起点转换到终点:
(1, 1) -> (1, 2)
(1, 2) -> (3, 2)
(3, 2) -> (3, 5)
示例 2:
输入: sx = 1, sy = 1, tx = 2, ty = 2
输出: false
示例 3:
输入: sx = 1, sy = 1, tx = 1, ty = 1
输出: true
数据规模
提示:
- 1 < = s x , s y , t x , t y < = 1 0 9 1 <= sx, sy, tx, ty <= 10^9 1<=sx,sy,tx,ty<=109
思路
考虑终点状态 ( t x , t y ) (tx,ty) (tx,ty)的上一个状态: ( t x − t y , t y ) (tx-ty,ty) (tx−ty,ty)或者 ( t x , t y − t x ) (tx,ty-tx) (tx,ty−tx),因为 ( s x , s y ) ( s x , s y ≥ 1 ) (sx,sy)(sx,sy\geq 1 ) (sx,sy)(sx,sy≥1),所以必须分别保证 t x > t y tx>ty tx>ty或者 t y > t x ty>tx ty>tx。那么对于 t x , t y tx,ty tx,ty之间的关系,考虑三种情况:
- t x = = t y tx==ty tx==ty:则必然上一状态会有 0 0 0状态,所以此时就是起点状态(即不存在上一状态),那么要想满足条件,必须有 t x = = s x & & t y = = s y tx==sx\&\&ty==sy tx==sx&&ty==sy;
- t x > t y tx>ty tx>ty:则上一状态为 ( t x − t y , t y ) (tx-ty,ty) (tx−ty,ty)
- t x < t y tx<ty tx<ty:则上一状态为 ( t x , t y − t x ) (tx,ty-tx) (tx,ty−tx)
所以从终点状态进行回推状态是固定的,而由起点状态往后推则是不固定的。
由于每一步反向操作一定是将 t x tx tx和 t y ty ty中的较大的值减小,比如: t x > t y tx>ty tx>ty可以直接把 t x tx tx更新到比 t y ty ty小,即 t x = t x % t y tx=tx\%ty tx=tx%ty;: t x < t y tx<ty tx<ty可以直接把 t y ty ty更新到比 t x tx tx小,即 t y = t y % t x ty=ty\%tx ty=ty%tx。
当反向操作的条件不成立时,根据 t x tx tx和 t y ty ty的不同情况分别判断是否可以从起点转换到终点:
- t x = = s x & & t y = = s y tx==sx\&\&ty==sy tx==sx&&ty==sy:满足条件;
- t x = = s x & & t y ≠ s y tx==sx\&\&ty\not=sy tx==sx&&ty=sy:只有当 ty > sy \textit{ty} > \textit{sy} ty>sy且 ( ty − sy ) m o d tx = 0 (\textit{ty} - \textit{sy}) \bmod \textit{tx} = 0 (ty−sy)modtx=0时可以从起点转换到终点。
- t y = = s y & & t x ≠ s x ty==sy\&\&tx\not=sx ty==sy&&tx=sx:只有当 tx > sx \textit{tx} > \textit{sx} tx>sx且 ( tx − sx ) m o d ty = 0 (\textit{tx} - \textit{sx}) \bmod \textit{ty} = 0 (tx−sx)modty=0时可以从起点转换到终点。
- 其余的都不满足条件。
代码
// short int long float double bool char string void
// array vector stack queue auto const operator
// class public private static friend extern
// sizeof new delete return cout cin memset malloc
// relloc size length memset malloc relloc size length
// for while if else switch case continue break system
// endl reverse sort swap substr begin end iterator
// namespace include define NULL nullptr exit equals
// index col row arr err left right ans res vec que sta
// state flag ch str max min default charray std
// maxn minn INT_MAX INT_MIN push_back insert
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int>PII;
typedef pair<int, string>PIS;
const int maxn=3e4+50;//注意修改大小
long long read(){long long x=0,f=1;char c=getchar();while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}while(isdigit(c)){x=x*10+c-'0';c=getchar();}return x*f;}
ll qpow(ll x,ll q,ll Mod){ll ans=1;while(q){if(q&1)ans=ans*x%Mod;q>>=1;x=(x*x)%Mod;}return ans%Mod;}
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
bool reachingPoints(int sx, int sy, int tx, int ty) {
while(tx>sx&&ty>sy&&tx!=ty){
if(tx>ty){
tx%=ty;
}
else{
ty%=tx;
}
}
if (sx == tx && sy == ty){
return 1;
}
if (tx == sx)
{
if (ty == sy || (ty - sy) % tx == 0)
return 1;
}
if (ty == sy)
{
if (tx == sx || (tx - sx) % ty == 0)
return 1;
}
return 0;
}
};