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。
 
代码:
  1. #include<cstdio>
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. int gcd(int a,int b)//辗转相除法
  6. {
  7. return b==?a:gcd(b,a%b);
  8. }
  9.  
  10. int main()
  11. {
  12. int p;
  13. int m,n;
  14. scanf("%d",&p);
  15. while(p--)
  16. {
  17. scanf("%d%d",&m,&n);
  18. gcd(m,n);
  19. if(gcd(m,n)==)
  20. printf("NO\n");
  21. else
  22. printf("YES\n");
  23. }
  24. return ;
  25. }

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. [原创]Python批量操作文件,批量合并

    最近几个小伙伴在手动合并一些文本文件,感觉可以用Python批量实现,就有了这段代码 import os import re import sys def printEnter(f1): #每两个文件 ...

  2. openStack windows2008 centos6.* img

    1,下载 windows2008 系统iso介质包 http://download.microsoft.com/download/F/3/8/F384E78B-8F1D-42A6-A308-63E45 ...

  3. iOS开发获取缓存文件的大小并清除缓存

    移动应用在处理网络资源时,一般都会做离线缓存处理,其中以图片缓存最为典型,其中很流行的离线缓存框架为SDWebImage. 但是,离线缓存会占用手机存储空间,所以缓存清理功能基本成为资讯.购物.阅读类 ...

  4. HTTP学习笔记——URL与资源

    什么是URL? 所有的东西都有一个标准化的东西,公交有线路号,飞机有航班号,个人有身份证号,你坐出租车,告诉司机师傅我要到石牌华师,他就能明白你的意思了.URL就是因特网资源的标准化名称.URL指向一 ...

  5. C++中++运算符的重载

    void showTime() const; Clock& operator ++(); Clock operator ++(int); private: int hour,minute,se ...

  6. Matlab常用小技巧及部分快捷键

    Matlab常用小技巧一: 1. m文件如果是函数,保存的文件名最好与函数名一致,这点都很清楚.不过容易疏忽的是,m文件名的命名尽量不要是简单的英文单词,最好是由大小写英文/数字/下划线等组成.原因是 ...

  7. BZOJ 1452: [JSOI2009]Count(二维BIT)

    为每一个权值开一个二维树状数组. ------------------------------------------------------------------------- #include& ...

  8. Linux内核学习笔记-2.进程管理

    原创文章,转载请注明:Linux内核学习笔记-2.进程管理) By Lucio.Yang 部分内容来自:Linux Kernel Development(Third Edition),Robert L ...

  9. bzoj 1057: [ZJOI2007]棋盘制作 单调栈

    题目链接 1057: [ZJOI2007]棋盘制作 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 2027  Solved: 1019[Submit] ...

  10. 0520 python

    配置python环境变量我的电脑->右键->属性->高级系统设置->环境变量->(1)用户变量->新建 Path=C:\Python27(2)系统变量->编辑 ...