HDU 1222 Wolf and Rabbit( 简单拓欧 )


**链接:****传送门 **

题意:狼抓兔子,狼从 0 出发沿逆时针寻找兔子,每走一步的距离为 m ,所有洞窟的编号为 0 ~ n-1 ,问是否存在一个洞窟使得兔子能够安全躲过无数次狼的搜捕。

思路:简单的拓展欧几里德,设 st 为兔子洞窟编号( 0 <= st < n ),很简单就可以得到一个方程 0 + m * x = n * y + st,化简一下得到 m * x - n * y = st,如果这个方程有解,那么兔子一定能被狼抓到。方程有解的条件是 st % d == 0 ,当 d == 1 时,显然是一定成立的,当 d != 1时,d <= min(n,m),则一定能从 [ 0 , n )中找到一个值使得 st % d != 0 成立,这时兔子是绝对安全的......


/*************************************************************************
    > File Name: hdu1222.cpp
    > Author:    WArobot 
    > Blog:      http://www.cnblogs.com/WArobot/ 
    > Created Time: 2017年05月21日 星期日 18时51分52秒
 ************************************************************************/

#include<bits/stdc++.h>
using namespace std;

#define ll long long
ll exgcd(ll a,ll b,ll& x,ll& y){
	if( b == 0 ){
		x = 1; y = 0; return a;
	}
	ll d = exgcd( b , a%b , x , y );
	ll tmp = x;
	x = y;	y = tmp - a/b*y;
	return d;
}
int main(){
	int t;
	ll n , m , x , y;
	scanf("%d",&t);
	while(t--){
		scanf("%lld%lld",&m,&n);
		ll d = exgcd( m , n , x , y );
		if( d != 1 ) 	printf("YES
");
		else		printf("NO
");
	}
	return 0;
}
原文地址:https://www.cnblogs.com/WArobot/p/6885674.html