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 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个人围成的一个圈,其中有一个人的盒子里面有一个漂亮的帽子,haha从1号开始找,每隔m-1个人找一次,问他最后是否一定能够找到这个帽子。

题解:一定找到这个帽子的话就每个人都要找一遍,n,m互素.

#include <stdio.h>
using namespace std;
int gcd(int a,int b){
    return b==0?a:gcd(b,a%b);
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF,n!=-1&&m!=-1){
        if(gcd(n,m)!=1) printf("POOR Haha\n");
        else printf("YES\n");
    }
    return 0;
}
时间: 2024-10-26 07:52:51

hdu 2104(判断互素)的相关文章

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 h

hdu 4587 判断孤立点+割点+ 删除点之后,剩下多少连通分量

做了很久...... 题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4587 先枚举删除的第一个点,第二个点就是找割点,没有割点当然也有答案 学到的: 1.图论硬套模板不太现实,比如这道题,我能想到孤立点是特殊情况,删除孤立点,连通分支个数会减少一,但是一直处理不好,最后按缩点的做法搞了, 判断是不是孤立点的方法: 就是先用一个数组scnt[i]=j,vv[j]++  表示点i在以j为祖先的联通分支里,而且每次都让vv[j]++,就使得vv[j

[转载]HDU 3478 判断奇环

题意:给定n个点,m条边的无向图(没有重边和子环).从给定点出发,每个时间走到相邻的点,可以走重复的边,相邻时间不能停留在同一点,判断是否存在某个时间停留在任意的n个点. 分析: (1)首先,和出发点的位置没有关系.因为可以走重复的边,且时间没有限制大小. (2)图必须是联通的 (3) 1)图为:2-0-1-3 从0点出发(时间为0),一个时间后到达1或2(时间为1),再一个时间后到达0或3(时间为2)... 可以发现,点分为两类,奇数时间到达和偶数时间到达,答案为NO 2)图为:2-0-1-2

hdu 2108 Shape of HDU【判断多边形是否是凸多边形模板】

#include<stdio.h> #include<math.h> const int maxn = 100000; struct Point{ double x,y; Point() {} Point(double _x, double _y) { x = _x; y = _y; } Point operator -(const Point &B) const { return Point(x-B.x, y-B.y); } }p[maxn]; double eps =

hdu 1325 判断有向图是否为树

题意:判断有向图是否为树 链接:点我 这题用并查集判断连通,连通后有且仅有1个入度为0,其余入度为1,就是树了 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std;

hdu 5365 判断正方形

题意:给出n个点(坐标均为整数),判断可以构成多少个正三角形.正四边形.正五边形.正六边形. 官方题解:地球人都知道整点是不能构成正五边形和正三边形和正六边形的,所以只需暴力枚举四个点判断是否是正四边形即可.假如你不是地球人,那么即使暴力枚举正三边形和稍微不那么暴力地找正五边形和正六边形也是可以通过的(反正找不到). ......看来我不是地球人...... 至于判断正方形,只要判断四边相等和一条对角线是边长的根号2倍就行了. 1 #include <iostream> 2 #include

hdu 2104 数论

判断两个数是否互质即可. 设x为走的步数,m为间距,则需要判断 x * m % n 是否可以充满0到n - 1的闭区间. 互质的话,存在逆元,所以一定可以. 1 #include <iostream> 2 using namespace std; 3 4 int gcd( int x, int y ) 5 { 6 return y ? gcd( y, x % y ) : x; 7 } 8 9 int main() 10 { 11 int a, b; 12 while ( cin >>

hdu 1272 判断所给的图是不是生成树 (并查集)

判断所给的图是不是生成树,如果有重边就不是,如果没重边但连通分量大于1也不是 find函数 用之前那个递归的写的话 会无限栈溢出 Orz 栈溢出的话 就加上这一串 #pragma comment(linker, "/STACK:1024000000,1024000000") Sample Input6 8 5 3 5 2 6 45 6 0 0 8 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 0 3 8 6 8 6 45 3 5 6 5 2 0 0 -1 -1 Samp

hdu 1756(判断点是否在多边形中)

Cupid's Arrow Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3105    Accepted Submission(s): 1103 Problem Description 传说世上有一支丘比特的箭,凡是被这支箭射到的人,就会深深的爱上射箭的人.世上无数人都曾经梦想得到这支箭.Lele当然也不例外.不过他想,在得到这支箭前