求逆元
费马小定理求逆元
a^(p-2) ≡ inv(a) (mod p)
(使用快速幂)复杂度nlogn
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);
}
线性求逆元
const int p; //p为模数 int n; int inv[maxn]; //线性求逆元 void getinv(int n) { inv[1]=1; for(int i=2;i<=n;++i) inv[i]=p-(long long)p/i*inv[p%i]%p; }
阶乘逆元(打表)
LL quick_pow(LL x,LL n) { LL res=1; x=x%p; while(n) { if(n%2==1)res=(res*x)%p; n=n/2; x=(x*x)%p; } return res; } void init() { fac[0]=1; for(int i=1;i<=maxn;i++) fac[i]=(fac[i-1]*i)%p;//阶乘取余打表 //切记,求阶乘逆元时maxn最大值为mod-1,因为用这个公式时要保证待求逆元的数(此处为n!)要和mod互质。 inv_fac[maxn]=quick_pow(fac[maxn],p-2);//最大阶乘逆元 for(int i=maxn-1;i>=0;i--) inv_fac[i]=(inv_fac[i+1]*(i+1))%p;//递推阶乘逆元 }
吾志所向
一往无前
愈挫愈奋
再接再励
——卓

浙公网安备 33010602011771号