The minimum positive integer solution for ax≡1 (mod b) of the x congruence equation is obtained.
Enter a description input Description
Enter only one row, containing two positive integers a, b, separated by a space.
Outputs description Output Description
The output has only one row containing a positive integer x0, or a minimum positive integer solution, and the input data guarantees there must be a solution.
Sample input to sample
3 10
Sample output Sample Outputs
7
Data Size & Hint
"Data Range"
For 40% of data, 2≤b≤1,000;
For 60% of data, 2≤b≤50,000,000
For 100% of data, 2≤a, b≤2,000,000,000
"Problem-solving ideas"
First ax≡1 (mod b) can be converted to Ax+by=1 and then obtained by extending Euclidean theorem.
You asked me what to expand Euclid ... It's hard to explain, probably the enhanced version of the Division, the function format as shown in the code
1 ProgramTYFC; 2 varA,b,c,x,y:int64; 3 functionGCD (A,b:int64;varX,y:int64): int64;//The extended Euclidean theorem is the function, which can be4 varT:longint; 5 begin 6 ifb=0 ThenWhen b=0When you exit, A is greatest common divisor7 begin 8x:=1; 9y:=0; Ten exit (a); One End; AGCD:=GCD (b,aMoDb,x,y); -t:=x; -x:=y; theY:=t-(ADivb) *y; - End; - - begin + Read (A, b); -c:=gcd (a,b,x,y); + while(X (bDivc) >0) DoDetermine the x size if it is greater than 0 minus (bDivc), at this time y+ (aDivc) Equal AX:=x-bDivC; at ifx<0 ThenJudgment x is less than 0, plus (bDivc) - whilex<0 Do - begin -x:=x+ (bDivc); - End; - writeln (x); in End.
The equation of the same remainder in the valley 1082