之前看了好几次都没看懂什么意思,一直get不到点。
意思就是说(a/b)%p != a%p/b%p,那么求(a/b)%p 的式子可以转换为(a*b^-1)%p, b^-1就是b关于p的逆元。
快速幂求逆元
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
typedef long long LL;
using namespace std;
LL pow_mod(LL a, LL b, LL p){//a的b次方求余p
LL ret = 1;
while(b){
if(b & 1) ret = (ret * a) % p;
a = (a * a) % p;
b >>= 1;
}
return ret;
}
LL Fermat(LL a, LL p){//费马求a关于b的逆元
return pow_mod(a, p-2, p);
}
int main()
{
int a,b;
while(cin>>a>>b)
cout<<Fermat(a,b)<<endl;
return 0;
}