[Knowledge] inversion yuan

Competition is generally used (Fermat's little theorem / Euler's theorem)

 LL qkpow(LL a,LL p,LL mod)//快速幂
{
    LL t=1,tt=a%mod;
    while(p)
    {
        if(p&1)t=t*tt%mod;
        tt=tt*tt%mod;
        p>>=1;
    }
    return t;
}
LL getInv(LL a,LL mod)
{
    return qkpow(a,mod-2,mod);//a的mod-2次方
}

 

 

Performance Analysis:

  • O (logmod)
  • Scope: In general, when a mod is a prime number, a little faster than the expansion in Europe and good writing.

 

Extended Euclidean Algorithm:

Exgcd LL (LL A, B LL, LL & X, Y & LL) // Extended Euclidean Algorithm 
{
     IF (B == 0 ) 
    { 
        X = . 1 , Y = 0 ;
         return A; 
    } 
    LL RET = exgcd (B , a% B, Y, X); 
    Y - = a / B * X;
     return RET; 
} 
LL getInv ( int a, int mod) // find a mod in the inverse element, there is no inverse element -1 
{ 
    LL X, Y; 
    LL D = exgcd (A, MOD, X, Y);
     return D == . 1 (X% + MOD MOD) MOD%: -? . 1;
}

 

 

  • Time complexity: O (logn) (actually a Fibonacci number)
  • Scope: As long as there is inverse to seek, not much but a lot of mod time, is the most common method to seek the inverse applies to the number.

Guess you like

Origin www.cnblogs.com/carrotmvp/p/12154344.html