HDU 1222 Wolf and Rabbit(公约数)

32 篇文章 0 订阅

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.
 

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不互质的话,是存在安全洞的

于是此题就成了判断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;
}
菜鸟成长记


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值