Modular inverse (modulo inverse element, extended Euclidean)

Source: Internet
Author: User

Modular Inverse Time limit: 2 Seconds Memory Limit: 65536 KB

The modular modular multiplicative inverse of an integer a modulo are an m integer x such that a-1≡x (mod m) . This was equivalent to ax≡1 (mod m) .

Input

There is multiple test cases. The first line of input was an integer T ≈2000 indicating the number of test cases.

Each test case contains integers 0 < a ≤1000 and 0 < m ≤1000.

Output

For each test case, output the smallest positive x . If such x doesn ' t exist, output "not exist".

Sample Input
33 114 125 13
Sample Output
4Not Exist8
Solution: To find the smallest positive integer, in fact, the general solution of X is x0+b/gcd*t, because T is an integer, then ans=x0+b/gcd*t=x0 mod b=x0%b; because ans, if it's a positive integer,
So when the B/GCD is negative, it is equal to the absolute value, because there is t ah, when the x0%b negative, plus a B; it is, because ans= (x0+b)%b;
Code:
1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cstring>5#include <cmath>6 using namespacestd;7 Const intinf=0x3f3f3f3f;8typedefLong LongLL;9 voidE_GCD (LL a,ll b,ll &d,ll &x,ll &y) {Ten     if(!b) { OneD=A; Ax=1; -y=0; -     } the     Else{ -E_GCD (b,a%b,d,x,y); -LL temp=x; -x=y; +y=temp-a/b*y; -     } + } ALL Cal (intAintBintc) { at LL x,y,d; - E_GCD (a,b,d,x,y); -     if(c%d!=0)return-1;//ax+by=c/(C/GCD); -x*=c/D; -B/=d;//because the general solution of X is x0+ (B/GCD) t; -      if(b<0) b=-b; inLL ans=x%b; -      if(ans<=0) ans+=b; to      returnans; + } - intMain () { the LL t,a,b,d,x,y; *scanf"%d",&T); $      while(t--){Panax Notoginsengscanf"%lld%lld",&a,&b); -LL Ans=cal (A, B,1); the         if(ans==-1) puts ("Not Exist"); +         Elseprintf"%lld\n", ans); A     } the     return 0; +}

Data on the problem comparison of water, data range 1000, violence can be:

#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>using namespacestd;Const intinf=0x3f3f3f3f; typedefLong LongLL;intMain () {intt,a,m; scanf ("%d",&T);  while(t--) {//(1-ax)%m;scanf"%d%d",&a,&m); intflot=0;  for(intx=1; x<= +; x + +){            if((1-A*X)%m==0) {Flot=1; printf ("%d\n", x);  Break; }        }        if(Flot)Continue; Puts ("Not Exist"); }    return 0;}

Modular inverse (modulo inverse element, extended Euclidean)

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.