Question address: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 2678.
Question: 1 the answer to this question is gcd (m, n)
2. The point on the board is (I, j) 0 <= I <= s-1; 0 <= j <= n-1; from (a, B) the point at which the K step is reached is (a + k) % m, (B + k) % N) the problem now is, at least how many integer pairs are needed
(A, B) forms a point set, making it possible for any I, j, 0 <= I <= s-1; 0 <= j <= n-1; A (A0, B0) and an integer k must exist in this vertex set.
(A0 + k) % m, (b0 + k) % n) = (I, j)
This is a problem of homogeneous equations. It can be proved in number theory that the necessary and sufficient conditions for the solution of X = a (mod m) and X = B (mod n) are (m, n) | (a-B)
In this question, we must (m, n) | j-I-(B-)
3. The adequacy and necessity indicate that the elements of the point set are (m, n );
Adequacy: only (m, n) points are required. (M, n) points: (0, Y) 0 <= Y <= (M. n)-1; The "B-a" composed of such points can traverse the complete series of MOD (m, n), so no matter whether J-I is in mod (m, n) we can take out all the values in the system, so that we can remove them.
Necessity: less than (m, n) elements do not work: once the number of points is less than (m, n) then the value of "B-a" will be less (because it may be repeated), that is to say, B-A cannot take the complete series of MOD (m, n.
Obviously, J-I can be traversed (Set M <= n to make I = 0, and J motion is acceptable), regardless of how (I, j) all of them have B-a to satisfy the conflicts between them.
In summary, the number of elements in the smallest point set is (m, n)
import java.math.*;import java.util.*;import java.io.*;public class Main { public static void main(String[] args) throws Exception { Scanner cin=new Scanner(System.in); boolean first=true; while(cin.hasNext()) { if(first==false) System.out.println(); if(first==true) first=false; BigInteger m=cin.nextBigInteger(); BigInteger n=cin.nextBigInteger(); System.out.println(m.gcd(n)); } } }