2016HUAS_ACM Summer Camp 4F-Number theory

Source: Internet
Author: User
Tags gcd greatest common divisor

This topic, if not find the direction, really a bit confused. But if you look in the right direction, AC is a matter of minutes. The answer is to see if N and M have the number of conventions other than 1.

Simple proof: Set N and M greatest common divisor not 1, assuming p. N and M can always be a number multiplied by the form of K, may wish to make n=a*k,m=b*k (temporarily do not know what is the use); So the first time the wolf traversed the hole numbered 0,m,2m ... (assuming that these holes are numbered within n-1), assuming that the wolf I will be more than n-1, then this should be i*m, but i*m>n-1, so this hole number is (i*m)%n, but, but, but (i*m)%n=i*b*k-a*k= (i* B-A) *k. (A*k,b*k comes in handy) [if (b<a<2b), A%b=a-b], description of the hole number or a multiple of k ah ... But p>1, so that the wolf can not traverse all the caves, then the rabbit is a place to hide.

The main topic: There are N caves, numbered 0~n-1, now a wolf from the mouth of the No. 0, every m-1 (here I have a little doubt about the original question, the original question is every m holes, but the case is m-1 only to) a hole to catch the rabbit, ask the rabbit finally can escape a robbery. For example, there are n=6 a hole, the number is 0, 1, 2, 3, 4, 5, the wolf every m-1=1 a hole to catch the rabbit, then the wolf into the cave is 0, 2, 4, 0, 2, 4 ... (infinite loop). If rabbits hide in holes 1, 3 and 5th, they are safe.

Sample Input

2//Number of test cases

1 2//m and N

2 2

Sample Output

NO

YES

#include <iostream>using namespacestd;intT,m,n;intgcdintAintb//This greatest common divisor function is best remembered, very important.{    returnb==0? A:GCD (b,a%b);}intMain () {CIN>>T;  while(t--) {cin>>m>>N; if(GCD (m,n) = =1) cout<<"no\n"; Elsecout<<"yes\n"; }    return 0;}

2016HUAS_ACM Summer Camp 4F-Number theory

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.