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".
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
本题最大的难点就是如何想到要使haha能遍历N必要的条件是M和N互质,然后就是简单的辗转相除法解题了,可以多举几个例子寻找规律,想A题重点还是要多思考。
附辗转相除法证明:
设两数为a、b(a>b),用gcd(a,b)表示a,b的
最大公约数,r=a (mod b) 为a除以b的余数,k为a除以b的商,即a÷b=k
.......r。辗转相除法即是要证明gcd(a,b)=gcd(b,r)。
第一步:令c=gcd(a,b),则设a=mc,b=nc
第二步:根据前提可知r =a-kb=mc-knc=(m-kn)c
第三步:根据第二步结果可知c也是r的因数
第四步:可以断定m-kn与n
互质(假设m-kn=xd,n=yd (d>1),则m=kn+xd=kyd+xd=(ky+x)d,则a=mc=(ky+x)cd,b=nc=ycd,则a与b的一个公约数cd>c,故c非a与b的最大公约数,与前面结论矛盾),因此c也是b与r的最大公约数(
b=n*c,r=(m-k*n)*c,
m-kn与n
互质)。
从而可知gcd(b,r)=c,继而gcd(a,b)=gcd(b,r)。
证毕。
以上步骤的操作是建立在刚开始时r≠0的基础之上的。即m与n亦互质。
利用辗转相除法求最大公因数的步骤如下:
第一步:用较大的数m除以较小的数n得到一个商q0和一个余数r0;
第二步:若r0=0,则n为m,n的最大公因数;若r0≠0,则用除数n除以余数r0得到一个商q1和一个余数r1;
第三步:若r1=0,则r1为m,n的最大公因数;若r1≠0,则用除数r0除以余数r1得到一个商q2和一个余数r2;
……
依次计算直至r(n)=0,此时所得到的r(n-1)即为所求的最大公因数。
第一步:用较大的数m除以较小的数n得到一个商q0和一个余数r0;
第二步:若r0=0,则n为m,n的最大公因数;若r0≠0,则用除数n除以余数r0得到一个商q1和一个余数r1;
第三步:若r1=0,则r1为m,n的最大公因数;若r1≠0,则用除数r0除以余数r1得到一个商q2和一个余数r2;
……
依次计算直至r(n)=0,此时所得到的r(n-1)即为所求的最大公因数。
#include <iostream>
#include <cstdio>
#include <cstring>
int gcd(int a,int b)//辗转相除法
{
if(b==0)
return a;
gcd(b,a%b);
}
/*int gcd(int n,int m) //辗转相除另一种写法
{
int r;
while(m)
{
r=n%m;
n=m;
m=r;
}
return n;
} */
using namespace std;
int main()
{
int m,n;
while(scanf("%d %d",&m,&n)&&m!=-1&&n!=-1)
{
if(gcd(m,n)!=1)
printf("POOR Haha\n");
else
printf("YES\n");
}
return 0;
}