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
#include <cstdio>int gcd (int a,int b) { return b==0?a:gcd (b,a%b);} int main () { int i,j,k; int a,m; int T; scanf ("%d", &t); while (t--) { scanf ("%d%d", &a,&m); if (gcd (a,m)!=1) {printf ("not exist\n"); continue;} In qualifying, it's been here. WA dropped, remember: //a three B (mod m) is the same as : a mod m = b mod m //Can be expressed as A=b+m*k where K is starting from 0 for (K=0;K&L t;=1000;k++) { if ((m*k+1)%a==0) {printf ("%d\n", (m*k+1)/a); return 0;}
Winter D3 D Modular Inverse