Wolf and Rabbit
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Problem Description
There is a hill with n holes around. The holes are signed from 0 to n-1.
A rabbit must hide in one of the holes. A wolf searches the rabbit in anticlockwise order. The first hole he get into is the one signed with 0. Then he will get into the hole every m holes. For example, m=2 and n=6, the wolf will get into the holes which are signed 0,2,4,0. If the rabbit hides in the hole which signed 1,3 or 5, she will survive. So we call these holes the safe holes.
A rabbit must hide in one of the holes. A wolf searches the rabbit in anticlockwise order. The first hole he get into is the one signed with 0. Then he will get into the hole every m holes. For example, m=2 and n=6, the wolf will get into the holes which are signed 0,2,4,0. If the rabbit hides in the hole which signed 1,3 or 5, she will survive. So we call these holes the safe holes.
Input
The input starts with a positive integer P which indicates the number of test cases. Then on the following P lines,each line consists 2 positive integer m and n(0<m,n<2147483648).
Output
For each input m n, if safe holes exist, you should output "YES", else output "NO" in a single line.
Sample Input
2 1 2 2 2
Sample Output
NO YES
题意:有0~n-1这n个洞,小兔子会藏在其中的一个洞中,而狼会每隔m个洞进洞查看洞里是不是有兔子。现给你m和n,问是否有安全的洞,即狼不会进这个洞查看。
一开始做这题的时候,还想着是不是跟奇偶性什么的有关,然而并不是,比如说m和n均为奇数的情况下,有的是存在安全的洞的,有的则是没有的,比如
m=3 n=5 是不存在安全的洞的
而
m=5 n=15 则是存在安全的洞的
于是此题就成了判断m与n是否赋值的问题,这个还是比较简单的,辗转相除法就可以搞定了
有一点需要提醒一下的是以往求公约数的时候,更相减损法还可以用用,但是此题就不行了,因为n和m可能差得比较大,这样计算次数是要明显多于辗转相除法的,故会导致TLE
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 16;
const int inf = 2147483647;
const int mod = 2009;
int gcd(int a,int b)
{
if(a%b)
return gcd(b,a%b);
return b;
}
int main()
{
int t,m,n;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
if(gcd(m,n)>1)
puts("YES");
else
puts("NO");
}
return 0;
}
菜鸟成长记