1. 程式人生 > 實用技巧 >HDU2104 - hide handkerchief - gcd輾轉相除法

HDU2104 - hide handkerchief - gcd輾轉相除法

題意

給出一個N和M,輸入-1 -1結束。
N代表N個人,Haha開始圍繞這N個人丟手絹,每次丟在間隔為M-1的人身邊,比如M=2,那麼Haha從扔給A,下一次就應該扔給C。
如果每個人全部都能被丟到一次,那麼輸出“YES”,否則輸出“POOR Haha”。

思路

如果N和M不存在公因數,即__gcd(N,M)==1 的時候,輸出YES即可。
畫個圖自己舉個例子就能明白了。

AC程式碼

//#include<bits/stdc++.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<list>
#include<stdlib.h>
#include<map>
#include<stack>
#include<stdio.h>
#include<queue>
using namespace std;
typedef long long ll;
#define sc(T) scanf("%d",&T)
#define scc(x,y) scanf("%d %d",&x,&y)
#define pr(T) printf("%d\n",T)
#define f(a,b,c) for (int a=b;a<=c;a++)
#define ff(a,b,c) for (int a=b;a>=c;a--)
#define inf 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define eps 1e-9
#define PI acos(-1)

int main()
{
    int x,y;
    while(~scc(x,y))
    {
        if(x==-1&&y==-1)
            break;
        if(__gcd(x,y)==1)
            cout<<"YES"<<endl;
        else
            cout<<"POOR Haha"<<endl;
    }
    return 0;
}