zjuoj 3609 Modular Inverse
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3609
The modular modular multiplicative inverse of an integer a modulo m is an integer x such that a-1≡x (mod m)
. This is equivalent to 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.
Output
For each test case, output the smallest positive x. If such x doesn't exist, output "Not Exist".
Sample Input
3 3 11 4 12 5 13
Sample Output
4 Not Exist 8
References
Author: WU, Zejun
Contest: The 9th Zhejiang Provincial Collegiate Programming Contest
分析:
题目要求给出a和m的值 , 求出 ax % m == 1 % m成立时的x 的最小值 , 直接x枚举到m即可。
一开始写的时候没有想到是枚举到m, 后来队友推出m。
AC代码:
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<queue> 5 #include<iostream> 6 #include<stack> 7 #include<map> 8 #include<string> 9 using namespace std; 10 int main(){ 11 int n, x, a, m; 12 scanf("%d", &n); 13 while(n--){ 14 bool flag = true; 15 scanf("%d%d", &a, &m); 16 for(x = 1; x <= m; x++){ 17 if((a*x)%m == 1%m){ 18 flag = false; 19 printf("%d\n", x); 20 break; 21 } 22 } 23 if(flag){ 24 printf("Not Exist\n"); 25 } 26 } 27 return 0; 28 }
悠游天地间 all rights reserved. © 2013 -- 1 << 64