Hdu 1222 Wolf and Rabbit (GCD)

Source: Internet
Author: User

Hdu 1222 Wolf and Rabbit (GCD)

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 are signed from 0 to n-1.



A rabbit must hide in one of the holes. A wolf searches the rabbit in anticlockwise Z records? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> latency + CgoKIAo8YnI + latency + CgoKIAo8YnI + latency" YES ", else output" NO "in a single line.

Sample Input
21 22 2

Sample Output
NOYES



A wolf and a rabbit rotate around a hill where n rabbit holes are evenly distributed. Every time a wolf passes through m holes, it enters the cave, this hole is insecure. Q: Are there safe holes in the n holes that make the rabbits hide in order not to be cute rabbits.


Resolution: The first thing that comes to mind at the beginning is that if n % m = 0, it will exist. However, when m = n = 1, it is not true, and n <m still exists. Likewise, there may be no safe caves. So you can't simply think about it. Later, it was found that as long as the two elements are mutually exists, that is, gcd (n, m) = 1, there is a safe cave, otherwise it does not exist.

PS: the recursive gcd version cannot be used because the data in this question is large. It must time out !!! Therefore, manually write non-recursive versions.



AC code:

# Include
 
  
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", & n, & m); printf ("% s \ n", gcd (n, m) = 1? "NO": "YES");} return 0 ;}
 





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.