#C++初学记录(遍历)
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++初学记录(遍历)的更多相关文章
- sqlserver 脚本 多条记录遍历
临时表方式实现多条记录遍历 declare @oper_cart_item_id bigint; declare @oper_cart_id bigint; declare @the_last_cha ...
- #C++初学记录(sort函数)
sort函数 前言:当进行贪心算法的学习时,需要用到sort函数,因为初学c++汇编语言,sort的具体用法没有深入学习,所以这里进行sort学习记录并只有基础用法并借用贪心算法题目的代码. 百度百科 ...
- javaweb初学记录
原文 链接 http://blog.csdn.net/iojust/article/details/52429805 - ---热情依旧 - 环境搭建: - jdk环境配置 jdk下载: http:/ ...
- #C++初学记录(算法4)
A - Serval and Bus It is raining heavily. But this is the first day for Serval, who just became 3 ye ...
- #C++初学记录(动态规划 被3整除的子序列)
原题:牛客网 动态规划dynamic programming 的入门级题目 题目描述 : 给你一个长度为50的数字串,问你有多少个子序列构成的数字可以被3整除 答案对1e9+7取模 输入描述: 输入一 ...
- #C++初学记录(动态规划(dynamic programming)例题1 钞票)
浅入动态规划 dynamic programming is a method for solving a complex problem by breaking it down into a coll ...
- #C++初学记录(判断子串#数学结合)
A Count Task Problem Description Count is one of WNJXYK's favorite tasks. Recently, he had a very lo ...
- Java 初学记录之可执行jar包
环境 jdk7 jre7 当我用jdk7开发的时候,编写完成可执行的jar工具,并且成功使用. 当我在另一台机器安装了jre6,再次运行我的小工具jar 的时候,报错 解决: http://stack ...
- Java 初学记录之一 快速输入
1. sysout 按回车 System.out.println();
随机推荐
- 那些年伴我一起成长的SAP装备
今天这篇文章无关技术,我们来聊聊SAP装备,即打上了SAP logo的那些物品. 但凡在SAP圈子工作过一段时间的从业者们,手上或多或少都拥有一些此类装备.Jerry当然也不例外,这些装备无论物品本身 ...
- TypeScript_泛型
typescript 中很多地方都和 java 和 C# 相似,如果 有 java 和 C# 的同学入手typeScript 会简单很多, 下面这里使用代码来表示和展现泛型的定义和使用 //泛型:在类 ...
- 批量导入数据到InnoDB表速度优化
1.使用Load data: 2. SET autocommit=0; ... SQL import statements ... COMMIT; 3. SET unique_checks=0; .. ...
- 解决linux下创建用户时出现 Creating mailbox file: 文件已存在
原来linux下添加用户后,会在系统里自动加一个邮箱(系统邮箱),路径是:/var/spool/mail/用户名. 可以直接用命令#rm -rf /var/spool/mail/用户名 ...
- 【Bug】MQ消息与事务提交
项目联调期间,遇到个bug,涉及MQ消息传递和事务提交时间问题,简单记录下. 背景 审核服务(审核创建项目),点击审核通过,后台代码会在提交事务前发送MQ消息(该消息由项目管理服务消费),发送成功后, ...
- Flink原理(二)——资源
前言 本文主要是想简要说明Flink在集群部署.任务提交.任务运行过程中资源情况,若表述有误欢迎大伙留言分享,非常感谢! 一.集群部署阶段 集群部署这里指的是Flink standalone模式,因为 ...
- 运维开发笔记整理-Django模型语法
运维开发笔记整理-Django模型语法 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.模型基本概念 1>.什么是模型 模型是你的数据唯一的,权威的信息源.它包含你所存储数 ...
- HTML&CSS基础-CSS的语法
HTML&CSS基础-CSS的语法 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.dome.html源代码 <!DOCTYPE html> <html ...
- 后台将数据传回前台的三种绑定的方式(Model,Map.ModelAndView)
//方式1:通过model 将数据绑定 @RequestMapping(value = "findByIdModel", method = RequestMethod.GET) p ...
- 02 c++的封装性 (构造和析构)
封装性: 关键字:public private protected 破坏封装:友元函数 friend 实现数据的隐藏:class类 默认是私有,结构体默认是公有. 类和对象:(定义类的注意事项) 在类 ...