The equation of the same remainder in the valley 1082

Source: Internet
Author: User

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

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.