HDU 1222 Wolf and Rabbit (GCD)

Source: Internet
Author: User

Wolf and Rabbit
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 5502 Accepted Submission (s): 2765


Problem Descriptionthere is a hill with n holes around. The holes is 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 was the one signed with 0. Then he'll get into the hole every m holes. For example, m=2 and n=6, the Wolf would get into the holes which is signed 0,2,4,0. If the rabbit hides in the hole which signed 1,3 or 5, she'll survive. So we call these holes the safe holes.

Inputthe 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).

Outputfor each input m n, if safe holes exist, you should output "YES", and Else Output "NO" in a.

Sample Input
21 22 2

Sample Output
NOYES



Test instructions: A Wolf a rabbit, the wolf around a uniform distribution of n rabbit hole in the mountain circle, the wolf every through the M hole, will enter the hole, then this hole is not safe. Ask N a hole, whether there is a safe hole for the rabbit to hide in order not to be a lovely rabbit murdered.


Parsing: When starting to think, the first thing to think about is, if n% m = = 0, then there is. But when m = = N = 1 o'clock, it is not established, and there are n < m cases, there may be no safe caves. So, it can't be so simple to think. Later found that as long as the two mutually, namely GCD (n, m) = = 1, there is a safe cave, otherwise does not exist.

PS: Because the data is very large, can not use the recursive version of the GCD, must timeout!!! So, write a non-recursive version manually.



AC Code:

#include <stdio.h>int gcd (int n, int m) {          //non-recursive gcd  while (m!=0)  {      int t=n%m;      n=m;      m=t;  }  return n;  } int main () {    int m,n,i,k;    scanf ("%d", &k);    for (i=1;i<=k;i++) {        scanf ("%d%d", &n,&m);        printf ("%s\n", GCD (n, m) = = 1? "NO": "Yes");    }    return 0;}





HDU 1222 Wolf and Rabbit (GCD)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.