hide handkerchief

Problem Description

The Children’s Day has passed for some days .Has you remembered something happened at your childhood? I remembered I often played a game called hide handkerchief with my friends.

Now I introduce the game to you. Suppose there are N people played the game ,who sit on the ground forming a circle ,everyone owns a box behind them .Also there is a beautiful handkerchief hid in a box which is one of the boxes .

Then Haha(a friend of mine) is called to find the handkerchief. But he has a strange habit. Each time he will search the next box which is separated by M-1 boxes from the current box. For example, there are three boxes named A,B,C, and now Haha is at place of A. now he decide the M if equal to 2, so he will search A first, then he will search the C box, for C is separated by 2-1 = 1 box B from the current box A . Then he will search the box B ,then he will search the box A.

So after three times he establishes that he can find the beautiful handkerchief. Now I will give you N and M, can you tell me that Haha is able to find the handkerchief or not. If he can, you should tell me "YES", else tell me "POOR Haha".

Input

There will be several test cases; each case input contains two integers N and M, which satisfy the relationship: 1<=M<=100000000 and 3<=N<=100000000. When N=-1 and M=-1 means the end of input case, and you should not process the data.

Output

For each input case, you should only the result that Haha can find the handkerchief or not.

Sample Input

3 2

-1 -1

Sample Output

YES

Source

HDU 2007-6 Programming Contest

Recommend

xhd

正确代码

	#include <cstdio>
#include <iostream>
using namespace std;
int gcd(int a,int b)
{
int c=a%b;
return c==0?b:gcd(b,c);
}
int main()
{
long long n,m;
while(cin>>n>>m)
{
if(n==-1&&m==-1)
break;
if(gcd(n,m)==1)
cout<<"YES"<<endl;
else
cout<<"POOR Haha"<<endl;
}
}

题意理解

较为简单的遍历算法题,问题的关键在于怎么判断不断跳过m-1次盒子是否可以遍历到所有n个盒子。当盒子的数量n与跳过盒子的数量m-1互为质数时,则能遍历所有盒子,若n与m-1有除1以外其他最大公约数时,haha就会回到起点重复之前的动作从而导致无法遍历所有盒子。

#C++初学记录(遍历)的更多相关文章

  1. sqlserver 脚本 多条记录遍历

    临时表方式实现多条记录遍历 declare @oper_cart_item_id bigint; declare @oper_cart_id bigint; declare @the_last_cha ...

  2. #C++初学记录(sort函数)

    sort函数 前言:当进行贪心算法的学习时,需要用到sort函数,因为初学c++汇编语言,sort的具体用法没有深入学习,所以这里进行sort学习记录并只有基础用法并借用贪心算法题目的代码. 百度百科 ...

  3. javaweb初学记录

    原文 链接 http://blog.csdn.net/iojust/article/details/52429805 - ---热情依旧 - 环境搭建: - jdk环境配置 jdk下载: http:/ ...

  4. #C++初学记录(算法4)

    A - Serval and Bus It is raining heavily. But this is the first day for Serval, who just became 3 ye ...

  5. #C++初学记录(动态规划 被3整除的子序列)

    原题:牛客网 动态规划dynamic programming 的入门级题目 题目描述 : 给你一个长度为50的数字串,问你有多少个子序列构成的数字可以被3整除 答案对1e9+7取模 输入描述: 输入一 ...

  6. #C++初学记录(动态规划(dynamic programming)例题1 钞票)

    浅入动态规划 dynamic programming is a method for solving a complex problem by breaking it down into a coll ...

  7. #C++初学记录(判断子串#数学结合)

    A Count Task Problem Description Count is one of WNJXYK's favorite tasks. Recently, he had a very lo ...

  8. Java 初学记录之可执行jar包

    环境 jdk7 jre7 当我用jdk7开发的时候,编写完成可执行的jar工具,并且成功使用. 当我在另一台机器安装了jre6,再次运行我的小工具jar 的时候,报错 解决: http://stack ...

  9. Java 初学记录之一 快速输入

    1. sysout 按回车 System.out.println();

随机推荐

  1. Node.js学习之(第三章:简易小demo)

    前言 我们前面已经学习完了Node中一些核心模块还有如何正确配置响应头的Content-Type,今天我们来实现一个简单的demo,巩固下之前学习的内容. 需求 我们平时访问百度或者其他大的门户网站的 ...

  2. DDL 操作数据库

    DDL 操作数据库:常用的操作 CRUD 一.C(create)创建 1.创建数据库 create database 数据库名称; 2.创建数据库,判断是否存在,再创建(如果存在,就不再创建) cre ...

  3. day35-python之协程

    1.协程 # import time # import queue # # def consumer(name): # # print("--->ready to eat baozi. ...

  4. IOS模拟器调试ANE

    来源:http://www.tuicool.com/articles/AFRJzi 利用iOS模拟器来检测和调试AIR应用程序补充篇 Air3.4来了 除去可以直接往模拟器里面部署应用,还可以往真机里 ...

  5. Python——迭代器&可迭代对象

    可迭代对象 什么是对象: Python中,一切皆对象.一个变量,一个列表,一个字符串,文件句柄,函数等等都可称为一个对象.一个对象就是一个实例,就是实实在在的东西. 什么是迭代 迭代就是一个重复的过程 ...

  6. 学习python的日常7

    ---恢复内容开始--- 正则表达式: 在正则表达式中,用\d可以匹配一个数字,\w可以匹配一个字母或数字,'.'可以匹配任意字符,用*表示任意个字符,用+表示至少一个字符,用?表示0个货一个字符,用 ...

  7. ISCC之misc复现-High起来!

    题目是赛后经高人指点,跳过坑的,各位看官看看就好 文件下载下来是一张png图片,但是无法打开,估计要修复一下,文件头修复一下,png格式文件头89504EE7 打开是一张二维码,经过扫描后,得到一串中 ...

  8. c#中关于textbox换行

    要让一个Windows Form的TextBox显示多行文本就得把它的Multiline属性设置为true. 这个大家都知道,可是当你要在代码中为Text属性设置多行文本[的时候可能会遇到点麻烦:) ...

  9. python算法与数据结构-算法介绍(31)

    一.算法和数据结构 什么是算法和数据结构?如果将最终写好运行的程序比作战场,我们程序员便是指挥作战的将军,而我们所写的代码便是士兵和武器. 那么数据结构和算法是什么?答曰:兵法!故,数据结构和算法是一 ...

  10. JQuery的入门(二)

    Jquery的遍历 jQuery对象本身就是数组对象,通过jquery选择器获得的都是满足该选择器条件的元素对象的集合体,因此在常常需要对jquery对象进行遍历.这里有三种遍历Jquery的方法. ...