题意:给定a,m,求a=1(mod m)
思路:典型的求乘法逆元,但是WA了五次,因为x要是正数,不能是0,坑死我了
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int egcd(int a,int b,int &x,int &y)
{
if(b==0){
x=1;y=0;
return a;
}
int d=egcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
int main()
{
int k,a,m,x,y,d;
scanf("%d",&k);
while(k--)
{
scanf("%d%d",&a,&m);
d=egcd(a,m,x,y);
if(d!=1) {printf("Not Exist\n");continue;}
while(x<=0) x+=m;
printf("%d\n",x);
}
}
题目:
The modular modular multiplicative inverse of an integer a modulo m is an integer xsuch that a-1≡x (mod m)
. This is equivalent to ax≡1 (mod m)
.
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.
<h4< dd="">For each test case, output the smallest positive x. If such x doesn't exist, output "Not Exist".
<h4< dd="">3 3 11 4 12 5 13<h4< dd="">
4 Not Exist 8
References