题目链接:快速幂求逆元
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
LL qmi(int a, int k, int p)
{
LL res = 1;
while(k)
{
if(k & 1) res = (LL) res * a % p;
k >>= 1;
a = (LL) a * a % p;
}
return res;
}
int main()
{
int n;
scanf("%d", &n);
while(n --)
{
int a, p;
scanf("%d%d", &a, &p);
int t = qmi(a, p - 2, p);
if(a % p) printf("%d\n", t);
else printf("impossible\n");
}
return 0;
}