美文网首页
Hide handkerchief (判断互质)

Hide handkerchief (判断互质)

作者: 敬轩大大 | 来源:发表于2017-01-26 00:32 被阅读0次

题目来源HDU - 2104

一道判断互质的题目

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.共有n个人,Haha每隔m-1个人查找一次,问经过无数次循环后,Haha是否可以遍历所有人,若可以输出YES,否则输出NO。


起初的思路为计算第一次完成一次循环后,Haha所寻找人的序号作为flag1.然后对一次完整的循环所有序号与该序号对比,若相等则表示只能在几个人之间循环,无法遍历所有人,否则可以。
思路完全是通过找规律找到的,没有证明思路的正确性。后来上网查需要证明n和m两个数互质,即n和m最大公约数为1

因为每次循环遍历的人的序号一定是n和m最大公约数的倍数。所以只要满足最大公约数为1,即可遍历所有人。

判断互质的方法为辗转相除法

互质数定义:

  • 两个数的公因数只有1的两个非零自然数,叫做互质数。
  • 多个数的若干个最大公因数只有1的正整数,叫做互质数
  • 两个不同的质数,为互质数
  • 1和任何自然数互质。
  • 任何相邻的两个数互质。

具体代码如下:

#include<stdio.h>
int main()
{
    long long n,m;  
    while (scanf("%lld %lld", &n, &m) && n != -1 && m != -1)
    {
        //确保n最大
        if (n < m)
        {
            n = m + n;
            m = n - m;
            n = n - m;
        }
        while (n%m!=1)
        {
            if (n%m == 0)   //n是m的倍数
                break;
            else
            {
                n = n%m;    
                n = n + m;
                m = n - m;
                n = n - m;
            }
        }

        if (n%m==0)
        {
            printf("POOR Haha\n");
        }
        else
        {
            printf("YES\n");
        }
    }
    return 0;
}

ACM新手,如有说错或可以改进的地方请大家不吝赐教!

相关文章

  • Hide handkerchief (判断互质)

    题目来源HDU - 2104 一道判断互质的题目 The Children’s Day has passed fo...

  • 排序与互质handkerchief

    最近暑假在家里做ACM试题时,看到了这道有趣的题目;题目的大致意思如下: haha从N个箱子里找手帕,每一次他都隔...

  • Stardust 星尘DAY2

    Words and Expressions seized the handkerchief, and blew h...

  • 欧拉函数

    欧拉函数 定义:若则称a,b互质。gcd(a,b,c)称为a,b,c互质,而称为两两互质。例如2,3,4互质但是不...

  • 无标题文章

    The best way to carry a handkerchief is to lend it. Women...

  • Día 15

    leña firewood pañuelo handkerchief muñeca doll engañar to...

  • CRT中国剩余定理

    特别版(除数两两互质) 普通版(任意情况)两两合并变成互质情况。

  • 判断当前的Fragment显示或者隐藏

    当多个Fragment使用 hide 和 show 的方法来显示隐藏时,判断当前fragment的显示状态!

  • 数学基础,笔记

    最大公约数,最小公倍数, 关系 互质 分数,分子与分母的关系,互质 质数指数计数法

  • jQuery效果笔记

    显示和隐藏show与hide $("#hide").cilck(function(){ $("p").hide...

网友评论

      本文标题:Hide handkerchief (判断互质)

      本文链接:https://www.haomeiwen.com/subject/odjxittx.html