ax对m求余等于1对m求余 -> ax + my = 1 = gcd(a,m)
直接用扩展欧几里得算法就行,gcd(a,m)不等于1时,不存在答案
#include <iostream>
using namespace std;
typedef long long ll;
void ex_gcd(ll a,ll b,ll &x,ll &y,ll &d){
if(!b){
d = a;x = 1;y = 0;
}
else{
ex_gcd(b,a % b,y,x,d);
y -= x * (a / b);
}
}
int main(void){
int t;
cin >> t;
while(t--){
ll a,m;
ll d,x,y;
cin >> a >> m;
ex_gcd(a,m,x,y,d);
if(d == 1){
while(x <= 0){
x += m;
}
cout << x << endl;
}
else{
cout << "Not Exist" << endl;
}
}
return 0;
}