博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
杭电_ACM_Wolf and Rabbit
阅读量:6655 次
发布时间:2019-06-25

本文共 2144 字,大约阅读时间需要 7 分钟。

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
21 22 2
 
Sample Output
NOYES
View Code
1 #include 
2 //get the greatest common divisor 3 int gcd(int a, int b) 4 { 5 int r; 6 while (b) 7 { 8 r = a % b; 9 a = b;10 b = r;11 }12 return a;13 }14 int main()15 {16 int N, m, n;17 scanf("%d", &N);18 while(N--)19 {20 scanf("%d %d", &n, &m);21 if(gcd(n, m) == 1)22 printf("NO\n");23 else24 printf("YES\n");25 }26 return 0;27 }

The reason why we can judge by greatest common divisor(gcd):

Condition:

firstly, r is the gcd of a, b and a is bigger than b, t is the lowest common multiple(lcm) of a, b

then you can get 

r = gcd(a, b)

t = a * b / r

secondly, the wolf find the t hold, the wolf will stop, because it's will be circulatory.

-------------------------

Question

the question is to find 0 < x < t / a and 0 < y < t / b to satisfy the equality

ax + r = by

for the equality, you will find (by - ax) / r is a integer, so you can find x, y making (by - ax) / r = 1

------------------------

Conclusion

"ax + r = by" means the r hole will be found, in other words, the gcd will be found.

so, if gcd is equal to 1, it means all holes will be found. if gcd is equal to other number, it means there are safe holes.

转载于:https://www.cnblogs.com/chuanlong/archive/2012/10/30/2745787.html

你可能感兴趣的文章
手写Function.bind函数
查看>>
这么多开源框架,该用哪个好?
查看>>
httpSecurity
查看>>
【Android】21.1 画板资源
查看>>
Sql 查询过慢,尝试重建索引
查看>>
雷林鹏分享:Yii(yiiframework)框架(三):gii页面出现403错误的解决方法
查看>>
第十二周CorelDRAW课总结
查看>>
【转】Android 环境变量 和 AVD 环境变量 配置
查看>>
[三]java8 函数式编程Stream 概念深入理解 Stream 运行原理 Stream设计思路
查看>>
【转】【SQL SERVER】怎样处理作业中的远程服务器错误(42000)
查看>>
jquery做表格变色效果-demo
查看>>
jquery 实现导航栏滑动效果
查看>>
linux系统下安装mysql数据库(mysql-5.7)
查看>>
MFC控件Slider Control的使用
查看>>
DOM的概念及子节点类型
查看>>
winform程序登陆后关闭登录窗体
查看>>
STL简介_18
查看>>
invalid application of ‘sizeof’ to incomplete type
查看>>
去掉表的identity属性
查看>>
libGDX游戏的生命周期
查看>>