hdu 1222 Wolf and Rabbit (GCD)

Wolf and Rabbit

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5502    Accepted Submission(s): 2765

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

题意:一狼一兔,狼围绕一个均匀分布着n个兔子洞的山转圈,狼每经过m个洞口,就会进入洞中,那么这个洞就是不安全的。问n个洞中,是否存在安全的洞让兔子藏身才能不是可爱的兔子惨遭毒手呢。

解析:开始想的时候,最先想到的就是,如果n % m == 0,那么就存在。但是当m == n == 1时,就不成立了,并且还存在n < m的情况,同样也可能不存在安全的洞穴。所以,就不能这么简单的想了。后来发现只要两者互素,即gcd(n, m)== 1,就存在安全洞穴,否则不存在。

PS:本题由于数据很大,不能用递归版本的gcd,必超时!!!所以,还是手动写非递归版本吧。

AC代码:

#include<stdio.h>

int gcd(int n, int m){          //非递归的gcd
  while(m!=0)
  {
      int t=n%m;
      n=m;
      m=t;
  }
  return n;
}

int main()
{
    int m,n,i,k;
    scanf("%d",&k);
    for(i=1;i<=k;i++){
        scanf("%d%d",&n,&m);
        printf("%s\n", gcd(n, m) == 1 ? "NO" : "YES");
    }
    return 0;
}
时间: 2024-10-28 11:24:39

hdu 1222 Wolf and Rabbit (GCD)的相关文章

Wolf and Rabbit(gcd)

Wolf and Rabbit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5922    Accepted Submission(s): 2972点我 Problem Description There is a hill with n holes around. The holes are signed from 0 to n-1

HDU 1222: Wolf and Rabbit

HDU 1222: Wolf and Rabbit ///@author Sycamore, ZJNU ///@accepted_on 2017-01-24 #include<iostream> using namespace std; unsigned gcd(unsigned a, unsigned b) { return b == 0 ? a : gcd(b, a % b); } int main() { int P; cin >> P; while (P--) { unsi

HDU 1222 Wolf and Rabbit (扩展欧几里德应用)

Wolf and Rabbit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6292    Accepted Submission(s): 3142 Problem Description There is a hill with n holes around. The holes are signed from 0 to n-1.

HDU 1695(GCD)

GCD Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u [Submit]   [Go Back]   [Status] Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common

hdu 5640 King&#39;s Cake(模拟)

Problem Description It is the king's birthday before the military parade . The ministers prepared a rectangle cake of size n×m(1≤n,m≤10000) . The king plans to cut the cake himself. But he has a strange habit of cutting cakes. Each time, he will cut

hdu 4930 Fighting the Landlords (模拟)

Fighting the Landlords Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 160    Accepted Submission(s): 52 Problem Description Fighting the Landlords is a card game which has been a heat for ye

HDU 4738 Caocao&#39;s Bridges(割边)

乍一看一个模板题,仔细一看还是模板题,但是三个坑.1,不是连通图,放0个.2 守卫为0,放1个. 3注意重边. #include<iostream> #include<cstdio> #include<vector> #include<queue> #include<algorithm> #include<stack> #include<cstring> using namespace std; #define maxn

hdu 5623 KK&#39;s Number(dp)

问题描述 我们可爱的KK有一个有趣的数学游戏:这个游戏需要两个人,有N\left(1\leq N\leq 5*{10}^{4} \right)N(1≤N≤5∗10?4??)个数,每次KK都会先拿数.每次可以拿任意多个数,直到NN个数被拿完.每次获得的得分为取的数中的最小值,KK和对手的策略都是尽可能使得自己的得分减去对手的得分更大.在这样的情况下,最终KK的得分减去对手的得分会是多少? 输入描述 第一行一个数T\left( 1\leq T\leq 10\right)T(1≤T≤10),表示数据组

HDU 5624 KK&#39;s Reconstruction(最小生成树)

题目链接:点击打开链接 题意:n个城市, m条可以修建的路, 修每条路有一个费用, 要求修建路将n个城市全部联通,并且最大费用减去最小费用最小. 思路:枚举最小边, 然后重新求一遍最小生成树,复杂度m^2, 出的数据水了, 左边BC水过了.. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #inc