876. 快速幂求逆元
题目链接https://www.acwing.com/problem/content/878/
题目:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
int n;
int niyuan(int a,int k,int p){
int res=1;
while(k){
if(k&1) res=(LL)res*a%p;
k>>=1;
a=(LL)a*a%p;
}
return res;
}
int main(){
int a,p;
cin>>n;
while(n--){
scanf("%d%d",&a,&p);
if(a%p==0) printf("impossible\n");
else
printf("%d\n",niyuan(a,p-2,p));
}
return 0;
}