The modular modular multiplicative inverse of an integer a modulo m is an integer x such that . This is equivalent to . a-1≡x (mod m)
ax≡1 (mod m)
Input
There are multiple test cases. The first line of input is an integer T ≈ 2000 indicating the number of test cases.
Each test case contains two integers 0 < a ≤ 1000 and 0 < m ≤ 1000.
OutputFor each test case, output the smallest positive x. If such x doesn't exist, output "Not Exist".
Sample Input3 3 11 4 12 5 13Sample Output
4 Not Exist 8
References
#include<bits/stdc++.h> using namespace std; typedef long long ll; /* It's really too yin, it's still my own inertia, too strong, including myself not very familiar with Ni Yuan So it is directly very confusing Even when it was 1, you can mod at the same time on both sides and serve it. */ intmain() { ios::sync_with_stdio(false); int T; cin>>T; while(T--){ ll a,m,ans; cin>>a>>m; int flag=0; for(ll i=1;i<=m;i++){ if((a*i)%m==1%m){ flag=1; ans=i; break; } } if(flag) cout<<ans<<endl; else cout<<"Not Exist"<<endl; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll exgcd(ll a,ll b,ll& d,ll& x,ll&y){ if(!b){ d=a;x=1;y=0; } else{ exgcd(b,a%b,d,y,x); y-=x*(a/b); } } /* If you turn this off, don't use scanf, otherwise it will be wa */ intmain() { ios::sync_with_stdio(false); int T; cin>>T; while(T--){ ll a,m; cin>>a>>m; if(m==1){ cout<<"1"<<endl; continue; } ll x,y,gcd; exgcd(a,m,gcd,x,y); if(gcd!=1){ cout<<"Not Exist"<<endl; continue; } if(x<=0){ x=(x%m+m)%m; } cout<<x<<endl; } return 0; }