0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-05 06:38:48 +00:00
OI-codes/AcWing/876/876.cpp

36 lines
563 B
C++

#include <iostream>
using std::cin;
using std::cout;
const char endl = '\n';
int n, a, p;
long long binpow(int a, int b, int m) {
long long res = 1;
a %= m;
while (b) {
if (b & 1) res = res * a % m;
a = 1ll * a * a % m;
b >>= 1;
}
return res;
}
int main() {
std::ios::sync_with_stdio(false);
cin >> n;
while (n--) {
cin >> a >> p;
if (a % p) {
cout << binpow(a, p - 2, p) << endl;
} else {
cout << "impossible" << endl;
}
}
return 0;
}