HDU 2104 hide handkerchief(辗转相除法--GCD)

hide handkerchief

Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4693 Accepted Submission(s): 1547

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

题目大意及思路:

给你两个数N,M,求是否在N的范围内不断的循环可以把这些数都遍历一遍,每次移动M步,其实只要两个数互质,经过转N次之后,每个数字都会被访问到。

#include<iostream>
#include<cstdio>

using namespace std;

int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF&&(m!=-1||n!=-1)){
        while(n%m!=0){//辗转相除法.
            m=n%m;n=m;
        }
        if(m==1) printf("YES\n");
        else printf("POOR Haha\n");
    }
    return 0;
}

HDU 2104 hide handkerchief(辗转相除法--GCD)

时间: 2024-11-05 09:45:51

HDU 2104 hide handkerchief(辗转相除法--GCD)的相关文章

(HDUStep 1.2.2)hide handkerchief(用辗转相除法来求最大公约数)

hide handkerchief Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5050 Accepted Submission(s): 1676 Problem Description The Children's Day has passed for some days .Has you remembered something h

杭电OJ(HDU)-ACM Steps-Chapter Two-《Biker&#39;s Trip Odometer》《Climbing Worm》《hide handkerchief》《Nasty Hac》

1.2.1 Biker's Trip Odometer #include<stdio.h> #include<math.h> const double PI=acos(-1.0); /* 计算题,根据公式做就行,PI*d*r/(12*5280);res1/t*3600; Sample Input 26 1000 5 27.25 873234 3000 26 0 1000 Sample Output Trip #1: 1.29 928.20 Trip #2: 1179.86 1415

hdu hide handkerchief

hide handkerchief Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description The Children’s Day has passed for some days .Has you remembered something happened at your childhood? I remembered I often play

hide handkerchief

Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5029 Accepted Submission(s): 1670   Problem Description The Children’s Day has passed for some days .Has you remembered something happened at your

hdu 2104(判断互素)

hide handkerchief Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3970    Accepted Submission(s): 1884 Problem Description The Children’s Day has passed for some days .Has you remembered someth

ACM step 1.2.3 hide handkerchief

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 g

hdu 5381 The sum of gcd(线段树+gcd)

题目链接:hdu 5381 The sum of gcd 将查询离线处理,按照r排序,然后从左向右处理每个A[i],碰到查询时处理.用线段树维护,每个节点表示从[l,i]中以l为起始的区间gcd总和.所以每次修改时需要处理[1,i-1]与i的gcd值,但是因为gcd值是递减的,成log级,对于每个gcd值记录其区间即可.然后用线段树段修改,但是是修改一个等差数列. #include <cstdio> #include <cstring> #include <vector>

HDU 4910 HDOJ Problem about GCD BestCoder #3 第四题

首先 m = 1 时 ans = 0对于 m > 1 的 情况 由于 1 到 m-1 中所有和m互质的数字,在 对m的乘法取模 运算上形成了群 ai = ( 1<=a<m && gcd(a,m) == 1 ) 所以 对于 a 必然存在b = a^(-1) = inv(a) 使得 a * b = 1 (mod m) 这里存在两种情况 a != b 那么最后的连乘式中a b均出现一次,相乘得1 a == b 那么最后的连乘式中只出现一个a 实际上所有 a = inv(a) 的

hdu (欧拉函数+容斥原理) GCD

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1695 看了别人的方法才会做 参考博客http://blog.csdn.net/shiren_Bod/article/details/5787722 题意 a,b,c,d,k五个数,a与c可看做恒为1,求在a到b中选一个数x,c到d中选一个数y,使得gcd(x,y)等于k,求x和y有多少对. 首先可以想到选取的必是k的倍数,假设是x和y倍,则x和y一定是互质的在,那么就变成了求1到b/k和1到d/k的之