a*b Ξ 1 (mod p) ,则a, b 互为逆元(mod p)

 

求逆元

1.费马小定理 :  p 是一质数, GCD(a,p)==1 , 则 a^(p-1) Ξ 1 (mod p)

 由此 a^(p-2) *a Ξ 1 (mod p) ,  那么 a^(p-2) 就是a的逆元

2.exgcd

#define int long long
const int mod =1e9+7;
 int gcd(int a,int b,int &x,int &y){
    if (b == 0){
        x=1, y=0;
        return a;
    }
    int t = gcd(b,a%b,y,x); y -= a/b*x;
    return t;
}
 int inv(int a){
    int x,y;
    gcd(a,mod,x,y) ;
    return (x%mod+mod)%mod  ;
 }

 

posted on 2022-12-24 12:47  towboat  阅读(19)  评论(0)    收藏  举报