Title Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3609
Surface:
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
Solving:
At first glance, it should be the Chinese remainder theorem, or what linear congruence equation, but the original learning this piece did not learn, carefully see can water, and so on after review, then to supplement other solutions.
Ideas:
Because (a*x)%m==1%m, can write (A * (x ' +n*m))%m==1%m, when the value of X is more than M, actually has been repeated, and M in the range of 0 to 1000, so the direct burst is good.
Compared to the pit is, when writing, directly on the right to write 1, did not take into account the situation of m=1. Therefore, the details are quite important, before submitting, you need to carefully examine the boundary data.
Code:
#include <iostream> #include <algorithm> #include <string> #include <iomanip> #include < Cstdio> #include <cstring>using namespace Std;int main () { int t,a,m,x; BOOL Flag; scanf ("%d", &t); while (t--) { scanf ("%d%d", &a,&m); if (m==1) { printf ("1\n"); Continue;} Flag=false; for (int i=1;i<=m;i++) { if ((a*i)%m==1) { flag=true; printf ("%d\n", I); break; } } if (!flag) printf ("Not exist\n"); } return 0;}
ZOJ 3609 Modular Inverse