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. psql: FATAL: role “postgres” does not exist 解决方案

    当时想做的事情,是运行一个创建数据库的脚本.找到的解决方案差不多和下面这个链接相同. http://stackoverflow.com/questions/15301826/psql-fatal-ro ...

  2. MSDN官方XmlSerializer类导致内存泄漏和性能低

    MSDN官方XmlSerializer类使用说明链接: http://msdn.microsoft.com/zh-CN/library/system.xml.serialization.xmlseri ...

  3. [置顶] c# 验证码生成

    今儿有一个任务是输出一串字符,要求用GDI画出于是: Bitmap bm = new Bitmap(200, 200);             Graphics g = Graphics.FromI ...

  4. 1关于script标签属性,注意点,浏览器文档模式,各种数据类型的转化

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. IOS学习之路七(通过xib自定义UITableViewCell)

    一.新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是product name是:UITableViewCe ...

  6. API HOOK库

    API HOOK库 API HOOK有两种做法,一种是SetWindowHookEx,简单易用,但如果做其它的HOOK,如HOOK OpenProcess,就需要修改内存地址了,内存地址可以通过Wri ...

  7. 自定义生命周期的设计(iOS篇)

    自定义生命周期的设计(iOS篇) 首先要确定一点,我们的App,要基于XIB文件进行编程,而不是在每个相应的ViewController里面去手动创建页面的每个控件.这样做的好处是,将页面布局与业务逻 ...

  8. PhoneGap+Xcode6环境配置

    安装PhoneGap,请先安装好NodeJS $ sudo npm install -g phonegap 创建项目 $ phonegap create my-app 编译项目 $ cd my-app ...

  9. CSS案例

    1.美团网效果 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> ...

  10. 对用户控件(ascx)属性(property)赋值

    对用户控件(ascx)属性(property)赋值 Insus.NET写此博文,是对用户控件(ASCX)的属性赋值经验与技巧分享.是这样子的,在做新闻站点时,一般都会有分很多类别. 在站点首页会显示最 ...