题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3609
思路:
(ax-1)%m == 0 -> ax-mk = 1; 求 x
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<sstream>
#include<vector>
#include<string>
#include<set>
using namespace std;
#define IOS ios::sync_with_stdio(false); cin.tie(0);
int read(){
int r=0,f=1;char p=getchar();
while(p>'9'||p<'0'){if(p=='-')f=-1;p=getchar();}
while(p>='0'&&p<='9'){r=r*10+p-48;p=getchar();}return r*f;
}
typedef long long ll;
const int Maxn = 30010;
const long long LINF = 1e18;
const int INF = 0x3f3f3f3f;
void exgcd (int a,int b,int &d,int &x, int &y) {
if(!b) { d = a; x = 1; y = 0; }
else {
exgcd (b,a%b,d,y,x); y-=(a/b)*x;
}
}
int main (void)
{
int t,a,m,ans;
cin >> t;
while (t--) {
cin >> a >> m;
int xx,yy,g;
exgcd(a,m,g,xx,yy); // xx为a的系数,yy为m的系数,g是gcd(a,m)
if(g == 1) {
while (xx <= 0) xx+=m; // y的通解 y0-k*a; x的通解 x0+k*b; k为任意整数,y和x统一一个k
cout << xx << endl;
}
else cout << "Not Exist" << endl;
}
return 0;
}