hide handkerchief

Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6646 Accepted Submission(s): 2173
 
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
 

最一开始并没有发现这道题的规律,只是盲目地将前几种情况列了出来。

发现了一些“端倪”。。。然并卵,还是看了别人的博客

http://blog.sina.com.cn/s/blog_86a6befd01012bqi.html

发现了解法应该是利用辗转相除求解N和M-1的公约数

 #include <iostream>
using namespace std;
int main(){
int n,m,t;
while () {
scanf("%d%d",&n,&m);
if(n==-&&m==-)break;
while (m!=) {
t=n%m;
n=m;
m=t;
}
if(n==)printf("YES\n");
else printf("POOR Haha\n");
}
return ;
}

总结:1.无限循环tips-->while(1){}

HOJ———丢手绢的更多相关文章

  1. python玩丢手绢问题,出局的顺序

    # 丢手绢问题# 游戏规则: 有N个小朋友玩丢手绢游戏,做成一圈,从第一个小朋友开始数数,从一开始数,数到指定数字的小朋友要出列,然后下一个小朋友继续从1开始数,依次类推,算出最后一个留下来的小朋友是 ...

  2. NC207040 丢手绢

    NC207040 丢手绢 题目 题目描述 "丢丢丢手绢,轻轻地放在小朋友的后面,大家不要告诉她,快点快点抓住她,快点快点抓住她." 牛客幼儿园的小朋友们围成了一个圆圈准备玩丢手绢的 ...

  3. 丢手绢问题(约瑟夫问题)的python实现

    约瑟夫问题是个有名的问题:N个人围成一圈,从第一个开始报数,第M个将被杀掉,最后剩下一个,其余人都将被杀掉. def fnA(p, personNum, cnt): times = cnt // pe ...

  4. Josephus problem(约瑟夫问题,丢手绢问题)

    约瑟夫问题 约瑟夫环问题是一个数学应用题:已知n个人(以编号1,2,3.....,n)围坐在一张圆桌的周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出列 ...

  5. ZJNU 1531 - 丢手绢--中级

    可以将相同的人数分块存在数组gp中先 例如RRGGGRBBBBRR 则gp[1~5]={2,3,1,4,2} 首先可以知道,如果要让没有相邻的相同,只需要每个gp[i]/2向下取整即可得出最少需要改变 ...

  6. 数学--数论--HDU 2104 丢手绢(离散数学 mod N+ 剩余类 生成元)+(最大公约数)

    The Children's Day has passed for some days .Has you remembered something happened at your childhood ...

  7. Java 解决约瑟夫问题

    约瑟夫问题(有时也称为约瑟夫斯置换,是一个出现在计算机科学和数学中的问题.在计算机编程的算法中,类似问题又称为约瑟夫环.又称“丢手绢问题”.) 有这样一个故事,15个教徒和15个非教徒在深海遇险必须讲 ...

  8. 约瑟夫问题(Josephus Problem)的两种快速递归算法

    博文链接:http://haoyuanliu.github.io/2016/04/18/Josephus/ 对,我是来骗访问量的!O(∩_∩)O~~ 约瑟夫问题(Josephus Problem)也称 ...

  9. 约瑟夫问题-Josephus--及实例说明

    //---我保证所有的代码都已经通过测试---// 类似约瑟夫的问题又称为约瑟夫环.又称“丢手绢问题”. 这个问题来自于这样的一个关于著名犹太历史学家 Josephus传说: 在罗马人占领乔塔帕特后, ...

随机推荐

  1. 【推荐分享】大量Python电子书籍教程pdf合集下载

    网上搜集的,点击即可下载,希望提供给有需要的人^_^   O'Reilly.Python.And.XML.pdf 2.02 MB   OReilly - Programming Python 2nd. ...

  2. NUnit单元测试资料汇总

    NUnit单元测试资料汇总 从安装到配置 首先到官网http://www.nunit.org/下载如下图的资料,安装NUnit-2.6.1.msi包. 然后挂在VS2010外部工具这个地方来使用,工具 ...

  3. 【Oracle】-【COMMIT对索引的影响】-从trace看COMMIT对索引的影响

    之前看过老杨http://yangtingkun.itpub.net/post/468/231000的一篇文章,讲述了INSERT操作对全文索引无操作,但DELETE时为了防止删除的数据仍能通过索引的 ...

  4. 错误与修复:ASP.NET无法检测IE10,IE11,导致_doPostBack未定义JavaScript错误,恒处于F5卷动条位置

    <browsers>  <browser id="IE11" parentID="Mozilla">    <identifica ...

  5. RabbmitMQ集群搭建流程

    参考资料 1.rabbmitMQ集群搭建http://my.oschina.net/guol/blog/186445http://blog.ftofficer.com/2010/03/translat ...

  6. nhibernate+autofac+mvc的demo

    想自己做一个小的demo.目的是能够提供一个系统架构,在这个基础上,可以快速开发一些小型的系统.

  7. D15

    T3: 树上的递归,很裸 T4:题目模型:二分染色问题 以及根据ccy大神指点,理解树形dp可以从 没有上司的舞会 聚会的快乐 这两题入手

  8. windows下python的安装

    首先进入python的官方网站:http://www.python.org在下载处,我们找到windows下有两个版本,下载最新版本Python3.4.0

  9. 安装mono和jexus,运行asp.net程序

    随笔- 62  文章- 1  评论- 7  raspberrypi(树莓派)上安装mono和jexus,运行asp.net程序   参考网址: http://www.linuxdot.net/ htt ...

  10. float的深入剖析

    float的深入剖析   float是什么? float即为浮动,在html中的作用是使元素脱离正常的文档流并使其移动到其父元素的“最左边”或“最右边”.下面解释下这个定义中的几个名词的概念: 文档流 ...