mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-27 14:56:27 +00:00
876. 快速幂求逆元
https://www.acwing.com/problem/content/submission/code_detail/13591616/
This commit is contained in:
parent
f46f0b40d2
commit
1372178916
35
AcWing/876/876.cpp
Normal file
35
AcWing/876/876.cpp
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user