HDU Wolf and Rabbit

 

Wolf and Rabbit

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1289 Accepted Submission(s): 750
 
Problem Description
There is a hill with n holes around. The holes are signed from 0 to n-1.
A rabbit must hide in one of the holes. A wolf searches the rabbit in anticlockwise order. The first hole he get into is the one signed with 0. Then he will get into the hole every m holes. For example, m=2 and n=6, the wolf will get into the holes which are signed 0,2,4,0. If the rabbit hides in the hole which signed 1,3 or 5, she will survive. So we call these holes the safe holes.
 
Input
The input starts with a positive integer P which indicates the number of test cases. Then on the following P lines,each line consists 2 positive integer m and n(0<m,n<2147483648).
 
Output
            For each input m n, if safe holes exist, you should output "YES", else output "NO" in a single line.
 
Sample Input
2
1 2
2 2
 
Sample Output
NO
YES
 
Author
weigang Lee
 
Source
杭州电子科技大学第三届程序设计大赛
 
Recommend
Ignatius.L

 

 

该题是要求判断出是否能通过约瑟夫环的数数方式将所有的点都数出来,结论很简单,就是判定总人数和跳跃人数是否互质,如果最大公约数是一的话,那么输出NO,否则输出YES。证明如下:

  设在N个点的圈中,按每数M个标记一下,那么能够把所有的点遍历到即要满足在循环完一圈后,新的起点一定要在1-M中的任何一点出现一次,这个不难理解吧。那么问题就转化为能否遍历1-M中所有的点,依次如此进行下去,当发现不能满足小区间的要求时,那么其父层亦不能满足要求。那么每次起点的偏移量是否有规律呢?答案是有的,以初始的N和M为例,每次的偏移量是 N%M( 假设N> M反之交换位置 ),也即取余的一个过程,那么接下来的新的N就是M,M也就变成了 N%M, 即 N2<-- M1;  M2<-- N1% M1;我们设想当N 不为1,而偏移量M又已经为0,那么整个样例也就不可能做到一一查询了。      哦哦,这就是 闻名遐迩的 gcd。

 

#include<stdio.h>

int n,m;

int gcd(int a,int b){
    return b==0?a:gcd(b,a%b);
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        if(gcd(n,m)==1)
            printf("NO\n");
        else
            printf("YES\n");
    }
    return 0;
}
posted @ 2013-03-17 08:32  Jack Ge  阅读(282)  评论(0编辑  收藏  举报