HDU 1222 Wolf and Rabbit(公约数)
2015-08-25 19:13
288 查看
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.
![](http://acm.hdu.edu.cn/data/images/C9-1004-1.jpg)
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; }菜鸟成长记
相关文章推荐
- iptables的详细介绍及配置方法
- 6174问题求解
- shell 新学命令整理
- API
- HDU 1290 献给杭电五十周年校庆的礼物(空间分割)
- Cisco vWSA (virtual Web Security Appliance)
- UVA 11354 Bond(最小生成树+lca+倍增求祖先节点)
- Python一日一练06----怒刷点击量pyqt5版
- Swift 结构体
- GUI编程笔记(java)01:GUI和CLI
- hdu 3714 Error Curves(三分)
- 关于移动端键盘弹出
- HDU 2674 N!Again(规律)
- 技术债务偿还计划
- IOS高德地图开发
- CPU与内存的那些事
- 关闭看门狗
- android---护眼灯和兼容性高的手电筒实现
- 设计模式 - Template Method
- C# 、winform 添加皮肤后(IrisSkin2) label设置的颜色 无法显示