You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
495 B
28 lines
495 B
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
#define int long long
|
|
|
|
// 快速幂 (a^k)%p
|
|
int qmi(int a, int k, int p) {
|
|
int res = 1;
|
|
while (k) {
|
|
if (k & 1) res = res * a % p;
|
|
a = a * a % p;
|
|
k >>= 1;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
signed main() {
|
|
int n;
|
|
cin >> n;
|
|
while (n--) {
|
|
int a, p;
|
|
cin >> a >> p;
|
|
if (a % p == 0)
|
|
puts("impossible"); // 不互质
|
|
else
|
|
printf("%lld\n", qmi(a, p - 2, p));
|
|
}
|
|
} |