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. MFC 动态创建控件

    动态控件是指在需要时由Create()创建的控件,这与预先在对话框中放置的控件是不同的.   一.创建动态控件:   为了对照,我们先来看一下静态控件的创建.   放置静态控件时必须先建立一个容器,一 ...

  2. 利用 XPath-jQuery 集锦手册在 XPath 和 jQuery 之间做选择

    利用 XPath-jQuery 集锦手册在 XPath 和 jQuery 之间做选择 利用 XPath-jQuery 集锦手册在 XPath 和 jQuery 之间做选择

  3. Static block start new thread

    Static block start new thread public class StaticThreadInit { static{ Threadt = newThread(){ public ...

  4. SQL Server数据转换【包括Geometry类型】的技巧总结

    1. 字段的组合: update new_master_location set tmp_street_unique=street+'_'+city+'_'+state+'_'+zip+'_'+con ...

  5. SQL Server索引进阶:第八级,唯一索引

    原文地址: Stairway to SQL Server Indexes: Level 8,Unique Indexes 本文是SQL Server索引进阶系列(Stairway to SQL Ser ...

  6. [学习笔记]HTML5之canvas

    虐了一下午的canvas 先撸了一个七巧板 <!doctype html> <html> <head> <meta charset="utf-8&q ...

  7. POJ 1459 Power Network(网络流 最大流 多起点,多汇点)

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22987   Accepted: 12039 D ...

  8. asp.net中实现MD5加密、解密的方法

    这个MD5加密.解密的方法会使用即可. 使用时的代码备忘:Response.Write(FormsAuthentication.HashPasswordForStoringInConfigFile(& ...

  9. iOS 7 标签栏控制器进行模态视图跳转后变成透明

    要解决此问题,需要设置tabBar的如下属性: self.tabBar.translucent = NO;

  10. SQLite For .Net 已经整合了32位和64位

    以前引用SQLite.DLL的时候,如果是winform等桌面程序,还要分32位和64位不一样的DLL,但最近已经整合为一个包了 打开vs的程序包管理器控制器,输入: install-package ...