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 ;}