# 求解逆元

1. 扩展欧几里得
$ax\equiv 1\left(mod$$ax≡1(mod$ $p\right)$$p)$ 相当于$ax=1+py$$ax=1+py$，也就是$ax-py=1\left(mod$$ax-py=1(mod$ $p\right)$$p)$

#include <cstdio>
#define M 9973
int exgcd(int a,int b,int &x,int &y){
if(!b){x=1,y=0;return a;}
int c=exgcd(b,a%b,y,x);
y-=a/b*x;
return c;
}
int main(){
int T,a,b,x,y;
scanf("%d",&T);
while(T--){
scanf("%d%d",&a,&b);
exgcd(b,M,x,y);
x=(x*a%M+M)%M;
printf("%d\n",x);
}
return 0;
} 

2.费马小定理

${a}^{p-2}$$a^{p-2}$也就是a得逆元
${a}^{p-2}$$a^{p-2}$就可以用快速幂等方法啦

#include <cstdio>
#define M 9973
int T,a,b;
int pow(int x,int y){
int t=1;
while(y){
if(y&1) t=t*x%M;
x=x*x%M;y>>=1;
}return t;
}
int main(){
freopen("hdu1576.in","r",stdin);
scanf("%d",&T);
while(T--){
scanf("%d%d",&a,&b);
b=b%M;
printf("%d\n",a*pow(b,M-2)%M);
}
return 0;
} 

3.逆元打表（递推）

$t=\frac{M}{i}，k=\phantom{\rule{1em}{0ex}}\mathrm{mod}\phantom{\rule{thinmathspace}{0ex}}\phantom{\rule{thinmathspace}{0ex}}i$

$k+t\ast i\equiv 0\left(\phantom{\rule{1em}{0ex}}\mathrm{mod}\phantom{\rule{thinmathspace}{0ex}}\phantom{\rule{thinmathspace}{0ex}}M\right)$

$⇒k\equiv -t\ast i\left(\phantom{\rule{1em}{0ex}}\mathrm{mod}\phantom{\rule{thinmathspace}{0ex}}\phantom{\rule{thinmathspace}{0ex}}M\right)$

$⇒\frac{1}{i}\equiv -\frac{t}{k}\left(\phantom{\rule{1em}{0ex}}\mathrm{mod}\phantom{\rule{thinmathspace}{0ex}}\phantom{\rule{thinmathspace}{0ex}}M\right)$

$⇒inv\left[i\right]=-t×inv\left[k\right]$

$inv\left[i\right]=\left(M-\frac{M}{i}\right)×inv\left[M\phantom{\rule{1em}{0ex}}\mathrm{mod}\phantom{\rule{thinmathspace}{0ex}}\phantom{\rule{thinmathspace}{0ex}}i\right]\phantom{\rule{1em}{0ex}}\mathrm{mod}\phantom{\rule{thinmathspace}{0ex}}\phantom{\rule{thinmathspace}{0ex}}M$

inv[1] = 1;
for(i=2;i<M;++i) inv[i]=(M-M/i)*inv[M%i]%M;

4.欧拉定理

$\varphi \left(n\right)$$\phi(n)$就是欧拉函数，表示不超过n，且与n互质的正整数的个数

n 1 2 3 4 5 6 7 8 9 10
$\varphi \left(n\right)$$\phi(n)$ 1 1 2 2 4 2 6 4 6 4

• 引理1
• （1）如果n为素数，则$\varphi \left(n\right)=n-1$$\phi(n)=n-1$
• （2）如果n为素数p的m次方，则$\varphi \left(n\right)=\varphi \left({p}^{m}\right)=\left(p-1\right)×{p}^{m-1}$$\phi(n)=\phi(p^m)=(p-1)\times p^{m-1}$
• （3）如果n为任意两个互质的数a、b的积，则$\varphi \left(n\right)=\varphi \left(a×b\right)=\varphi \left(a\right)×\varphi \left(b\right)$$\phi(n)=\phi(a\times b)=\phi(a)\times \phi(b)$
• 引理2
• $n={p}_{1}^{{a}_{1}}×{p}_{2}^{{a}_{2}}×{p}_{2}^{{a}_{2}}\cdots ×{p}_{k}^{{a}_{k}}$$n=p_1^{a_1}\times p_2^{a_2}\times p_2^{a_2}\cdots\times p_k^{a_k}$（其中${p}_{i}$$p_i$为质数），则$\varphi \left(n\right)=n×\left(1-\frac{1}{{p}_{1}}\right)×\left(1-\frac{1}{{p}_{2}}\right)\cdots ×\left(1-\frac{1}{{p}_{k}}\right)$$\phi(n)=n\times(1-\frac{1}{p_1})\times(1-\frac{1}{p_2})\cdots\times(1-\frac{1}{p_k})$

$\varphi \left(p\ast x\right)=\varphi \left(x\right)\ast p$

$\varphi \left(p\ast x\right)=\varphi \left(x\right)\ast \varphi \left(p\right)=\varphi \left(x\right)\ast \left(p-1\right)$
;

#include <cstdio>
#include <cstring>
#define N 3300000
int prime[N],isprime[N],phi[N],tot=0,a,b;
void euler(){
memset(isprime,true,sizeof(prime));isprime[0]=isprime[1]=false;
for(int i=2;i<=3000000;i++){
if(isprime[i]) prime[++tot]=i,phi[i]=i-1;
for(int j=1;i*prime[j]<=3000000;j++){
int k=i*prime[j];
isprime[k]=false;
if(i%prime[j]==0){
phi[k]=phi[i]*prime[j];
break;
}else phi[k]=phi[i]*(prime[j]-1);
}
}
}
int main(){
euler();
while(scanf("%d%d",&a,&b)>0){
long long ans=0;
for(int i=a;i<=b;i++) ans+=(long long)phi[i];
printf("%lld\n",ans);
}
return 0;
} 

5.特殊情况

$\frac{a}{b}\equiv d\left(\phantom{\rule{1em}{0ex}}\mathrm{mod}\phantom{\rule{thinmathspace}{0ex}}\phantom{\rule{thinmathspace}{0ex}}p\right)$

$\frac{a}{b}=d+px$

$⇒a=bd+bpx$

$⇒a\equiv bd\left(\phantom{\rule{1em}{0ex}}\mathrm{mod}\phantom{\rule{thinmathspace}{0ex}}\phantom{\rule{thinmathspace}{0ex}}bp\right)$

$⇒\frac{a\left(mod-bp\right)}{b}=d$