AcWing 876 快速幂求逆元 题解 (快速幂)
作者:互联网
#include<cstdio>
typedef long long ll;
int qmi(int b, int k, int p){
int res = 1;
while(k){
if(k & 1) res = (ll)res * b % p;
k >>= 1;
b = (ll)b * b % p;
}
return res;
}
int main()
{
int n;
scanf("%d", &n);
while(n -- ){
int b, p;
scanf("%d%d", &b, &p);
if(b % p) printf("%d\n", qmi(b, p - 2, p));//当b和p不是互为质数是,不可能同mod一个数余1, 就返回impossible
else puts("impossible");
}
return 0;
}
标签:幂求,876,题解,ll,long,qmi,int,res,impossible 来源: https://blog.csdn.net/qiaodxs/article/details/120198972