时间限制:
10000ms
单点时限:
1000ms
内存限制:
256MB
-
2 1 1 8 13 2 2 100 101
样例输出
-
YES NO
描述
小Hi被坏女巫抓进一座由无限多个格子组成的矩阵迷宫。
小Hi一开始处于迷宫(x, y)的位置,迷宫的出口在(a, b)。小Hi发现迷宫被女巫施加了魔法,假设当前他处在(x, y)的位置,那么他只能移动到(x+y, y)或者(x, x+y)的位置上。
小Hi想知道自己能不能逃离迷宫。
输入
第一行包含一个整数T,代表测试数据的组数。
以下N行每行包含4个整数x, y, a, b,表示起点在(x, y),出口在(a, b)。
对于30%的数据,1 ≤ T ≤ 10, 1 ≤ x, y, a, b ≤ 100
对于80%的数据,1 ≤ T ≤ 1000, 1 ≤ x, y, a, b ≤ 1000
对于100%的数据,1 ≤ T ≤ 10000, 1 ≤ x, y, a, b ≤ 109
输出
对于每组数据输出一行YES或者NO,表示小Hi是否能逃离迷宫。
题解:首先,暴力是过不了的,遇到一个10亿和1,会爆栈,并且TLE。
我的思路是不停的让A和B中大的减去小的,在这个过程中,如果A与目标的X对应相等了,那么就判断一下Y和B是否对A同余就可以了,就不用一直暴力的减了。另外,如果A和B没有跟X和Y相等的,那么直接MOD一下就行,很快的。
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <bits/stdc++.h>
using namespace std;
int T;
int x,y,a,b;
bool dfs(int X,int Y,int A,int B){
if(A<X||B<Y||A<1||B<1)return 0;
if(X==A&&Y==B)return 1;
if(A==B){
if(A==X||B==Y)return 1;
}
if(A>B){
if(B==Y){
if(A%B==X%B){
return 1;
}
}
else{
if(dfs(X,Y,A%B,B))return 1;
}
}
if(A<B){
if(A==X){
if(B%A==Y%A){
return 1;
}
}
else{
if(dfs(X,Y,A,B%A))return 1;
}
}
return 0;
}
int main(){
cin>>T;
while(T--){
cin>>x>>y>>a>>b;
if (dfs(x,y,a,b)){
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
}
return 0;
}
//
// _oo0oo_
// o8888888o
// 88" . "88
// (| -_- |)
// 0\ = /0
// ___/`---'\___
// .' \\| |// '.
// / \\||| : |||// \
// / _||||| -:- |||||- \
// | | \\\ - /// | |
// | \_| ''\---/'' |_/ |
// \ .-\__ '-' ___/-. /
// ___'. .' /--.--\ `. .'___
// ."" '< `.___\_<|>_/___.' >' "".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `_. \_ __\ /__ _/ .-` / /
// =====`-.____`.___ \_____/___.-`___.-'=====
// `=---='
//
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// 佛祖保佑 永无BUG
//
//
//