题意:ax≡1 (mod m) 求x
思路:就是一个裸的扩展欧几里得,但是要注意一下m为1的情况
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define MAXN 20100
#define MAXE 5
#define INF 1000000000
#define MOD 9901
#define LL long long
#define pi acos(-1.0)
using namespace std;
void exgcd(LL a, LL b, LL &d, LL &x, LL &y) {
if (!b) {
d = a;
x = 1;
y = 0;
} else {
exgcd(b, a % b, d, y, x);
y -= x * (a / b);
}
}
int main() {
std::ios::sync_with_stdio(false);
int T;
cin >> T;
LL a, m;
for (int kase = 1; kase <= T; ++kase) {
cin >> a >> m;
if (m == 1) {
cout << 1 << endl;
} else {
LL d, x, y;
exgcd(a, m, d, x, y);
if (d != 1) {
cout << "Not Exist\n";
} else {
cout << (x + m) % m << endl;
}
}
}
return 0;
}