zjuoj 3609 Modular Inverse

时间:2023-03-09 08:56:44
zjuoj 3609 Modular Inverse

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3609

Modular Inverse


Time Limit: 2 Seconds      Memory Limit: 65536 KB

The modular modular multiplicative inverse of an integer a modulo m is an integer x such that a-1x (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代码:

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#include<stack>
#include<map>
#include<string>
using namespace std;
int main(){
int n, x, a, m;
scanf("%d", &n);
while(n--){
bool flag = true;
scanf("%d%d", &a, &m);
for(x = ; x <= m; x++){
if((a*x)%m == %m){
flag = false;
printf("%d\n", x);
break;
}
}
if(flag){
printf("Not Exist\n");
}
}
return ;
}