HDU 1222   Wolf and Rabbit   (最大公约数)解题报告

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88159#problem/G

题目:

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个山洞,狼按逆时针的顺序抓兔子,判断兔子是否能活下来。
 
分析:
这是一个求最大公约数的问题。可以用书上的gcd算法--辗转相除法。gcd(a,b)=gcd(b,a%b),还要考虑它和边界条件gcd(a,0)=a。
 
代码:
 #include<cstdio>
#include<iostream>
using namespace std; int gcd(int a,int b)//辗转相除法
{
return b==?a:gcd(b,a%b);
} int main()
{
int p;
int m,n;
scanf("%d",&p);
while(p--)
{
scanf("%d%d",&m,&n);
gcd(m,n);
if(gcd(m,n)==)
printf("NO\n");
else
printf("YES\n");
}
return ;
}

HDU 1222 Wolf and Rabbit(gcd)的更多相关文章

  1. Wolf and Rabbit(gcd)

    Wolf and Rabbit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. HDU 1222 Wolf and Rabbit(数学,找规律)

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

  3. HDU 1222 Wolf and Rabbit( 简单拓欧 )

    链接:传送门 题意:狼抓兔子,狼从 0 出发沿逆时针寻找兔子,每走一步的距离为 m ,所有洞窟的编号为 0 - n-1 ,问是否存在一个洞窟使得兔子能够安全躲过无数次狼的搜捕. 思路:简单的拓展欧几里 ...

  4. HDU 1222 Wolf and Rabbit(欧几里得)

    Wolf and Rabbit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. hdu 1222 Wolf and Rabbit

    Problem Description There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbi ...

  6. HDU 1222 - Wolf and Rabbit & HDU 1108 - [最大公约数&最小公倍数]

    水题,只是想借此记一下gcd函数的模板 #include<cstdio> int gcd(int m,int n){return n?gcd(n,m%n):m;} int main() { ...

  7. IOS多线程(GCD)

    简介 Grand Central Dispatch 简称(GCD)是苹果公司开发的技术,以优化的应用程序支持多核心处理器和其他的对称多处理系统的系统.这建立在任务并行执行的线程池模式的基础上的.它首次 ...

  8. IOS学习之十七:Grand Central Dispatch(GCD)编程基础

    IOS学习之十七:Grand Central Dispatch(GCD)编程基础   有过编程经验的人,基本都会接触到多线程这块. 在java中以及Android开发中,大量的后台运行,异步消息队列, ...

  9. Swift - 多线程实现方式(3) - Grand Central Dispatch(GCD)

    1,Swift继续使用Object-C原有的一套线程,包括三种多线程编程技术: (1)NSThread (2)Cocoa NSOperation(NSOperation和NSOperationQueu ...

随机推荐

  1. IOS 特定于设备的开发:处理基本方向

    UIDevice类使用内置的orientation属性获取设备的物理方向.IOS设备支持这个属性的7个可能的值. >UIDeviceOrientationUnknown:方向目前未知. > ...

  2. 【原创】Libjpeg 库使用心得(一) JPEG图像DCT系数的获取和访问

    [原创]继续我的项目研究,现在采用Libjpeg库函数来进行处理,看了库函数之后发现C语言被这些人用的太牛了,五体投地啊...废话不多说,下面就进入正题. Libjpeg库在网上下载还是挺方便的,这里 ...

  3. css 自适应布局阮一峰

    转载一篇文章: 自适应网页设计(Responsive Web Design) 作者: 阮一峰 移动设备正超过桌面设备,成为访问互联网的最常见终端.于是,网页设计师不得不面对一个难题:如何才能在不同大小 ...

  4. /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.15' not found,解压rpm包

    如果是64位系统报错信息如下: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.15' not found (required by 原因是没有GLIB ...

  5. js判断undefined类型,undefined,null,NaN的区别

    js判断undefined类型 今天使用showModalDialog打开页面,返回值时.当打开的页面点击关闭按钮或直接点浏览器上的关闭则返回值是undefined   所以自作聪明判断       ...

  6. raphael入门到精通---属性和事件篇

    属性的使用 上一篇文章我们介绍了raphael如何生成基本的图形(元素),对于每个元素来讲,我们可以添加很多的元素(attr) 下面我就来简单的介绍下元素属性的使用(path元素属性我后面单独列出来介 ...

  7. ##DAY4 事件的基本概念、触摸的基本概念、响应者链、手势

    ##DAY4  事件的基本概念.触摸的基本概念.响应者链.手势 #pragma mark ———————事件的基本概念 ——————————— 事件的基本概念: 1)事件是当用户的手指触击屏幕及在屏幕 ...

  8. SqlHelp

    using System.Configuration;using System.Data; public class SqlHelp { private static string connectio ...

  9. Retrofit2 上传图片等文件

    普通写法: //创建表单的普通字段public static RequestBody createFormBody(String content) { RequestBody body = Reque ...

  10. C++模板:文件操作

    freopen("demo.in","r",stdin); freopen("demo.out","w",stdout) ...