0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:05:26 +00:00

876. 快速幂求逆元

https://www.acwing.com/problem/content/submission/code_detail/13591616/
This commit is contained in:
Baoshuo Ren 2022-04-21 19:23:01 +08:00
parent f46f0b40d2
commit 1372178916
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

35
AcWing/876/876.cpp Normal file
View File

@ -0,0 +1,35 @@
#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;
}