HDOJ 2104 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
题意翻译:儿童节已经过去好几天了,你还记得小时候发生的事吗?我记得我经常和朋友玩一个叫藏手帕的游戏。
现在我向大家介绍这个游戏。假设有N个人玩这个游戏,他们坐在地上围成一个圈,每个人身后都有一个盒子,还有一块漂亮的手帕藏在盒子里,盒子就是其中的一个。
然后哈哈(我的一个朋友)被叫来找手帕。但他有一个奇怪的习惯。每次他都会从当前框中搜索下一个由M-1框分隔的框。例如,有三个框名为A,B,C,现在Haha在A的位置上。现在他决定M是否等于2,所以他将首先搜索A,然后搜索C框,因为C与当前框A之间用2-1=1框B隔开。然后他会搜索B框,然后他会搜索A框。
三次之后,他确定他能找到那条漂亮的手帕。现在我给你N和M,你能告诉我哈哈能不能找到手帕。如果他可以,你应该告诉我“是的”,否则就告诉我“可怜的哈哈”
输入内容翻译:将有多个测试用例;每个用例输入包含两个整数N和M,它们满足关系:1<=M<=100000000和3<=N<=100000000。当N=-1和M=-1表示输入大小写结束时,不应处理数据。
丢手帕分析图
辗转相除法又名欧几里得算法,假如需要求 1997 和 615 两个正整数的最大公约数,用欧几里得算法,是这样进行的:
1997 / 615 = 3 (余 152)
615 / 152 = 4(余7)
152 / 7 = 21(余5)
7 / 5 = 1 (余2)
5 / 2 = 2 (余1)
2 / 1 = 2 (余0)
至此,最大公约数为1
以除数和余数反复做除法运算,当余数为 0 时,取当前算式除数为最大公约数,所以就得出了 1997 和 615 的最大公约数 1。
#include<stdio.h>
int gcd(int, int);
int main(void)
{
int n, m;
while (scanf("%d%d", &n, &m) && (n != -1 && m != -1))
if (gcd(n, m) == 1)//最大公约数为1,那么这两个数互相为质数符合题意
printf("YES\n");
else
printf("POOR Haha\n");
return 0;
}
int gcd(int a, int b)
{
int temp;
while (b)//辗转相除法找最大公约数
{
temp = a % b;
a = b;
b = temp;
}
return a;
}