Extended GCD Codevs 1200 congruence equation

Source: Internet
Author: User

Codevs 1200 congruence equation

2012 Noip National League Improvement Group

time limit: 1 sspace limit: 128000 KBtitle level: Diamonds DiamondTitle Description Description

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.

Output 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 Sample Input

3 10

Sample output Sample Output

7

Data range and Tips 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

Category labels Tags Click here to expandEuclidean theorem number theory Continental region NOIP National League increase Group 2012
1 /*ax≡1 (mod b) is ax=by+1, and x, y are integers, so ax mod b==1, for ax=by+1, with the extension GCD solution, and then find the right x output can be*/2#include <iostream>3 using namespacestd;4#include <cstdio>5 Long Longb;6 voidEXGCD (Long LongALong LongBLong Long&x,Long Long&y,Long Long&gcd)7 {8     if(b==0)9     {Tengcd=a;x=1; y=0; One       return; A     } -EXGCD (b,a%b,x,y,gcd); -     intt=x; thex=y; -Y=t-(A/b) *y; - } - intMain () + { -Cin>>a>>b; +     Long Longgcd,x,y; A EXGCD (A,B,X,Y,GCD); at     Long Longa0=a/gcd,b0=b/gcd; -     Long Longk=1/gcd; -x*=k;y*=K; -     if(x<=0) -     { -         intI=1; in          while(1) -         { to             if(A * (x+i*b0) +b* (y-i*a0) = =1) +             { -                 if(x+i*b0>0) the                 { *cout<< (x+i*b0) <<Endl; $                     return 0;Panax Notoginseng                 } -             } thei++; +         } A     } the     if(x>0) +     { -         inti=-1; $          while(1) $         { -             if(A * (x+i*b0) +b* (y-i*a0) = =1) -             { the                 if(x+i*b0<0) -                 {Wuyicout<<x<<Endl; the                     return 0; -                 } Wu             } -i--; About         } $     } -     return 0; -}

Extended GCD Codevs 1200 congruence equation

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.