Inverse element:
If the formula is satisfied, then a is the inverse of B and B is also the inverse of a.
Application of Inverse element:
Set C as the inverse of B in the sense of the remainder of M;
When solving the equation (A/b)% m, if B can be very large, so there will be a problem of burst accuracy, this time you need to convert division into multiplication, that is:
(A/b)% M = (A * c)%m.
The method of inverse element:
One, the expansion of Euclid to seek the inverse of the yuan
Complexity: O (LOGN) (actually the Fibonacci sequence)
The formula (b, p known) a?b≡1 (mod p) is converted to A?b+k?p=1, and a is the inverse of B to P, and only if A and p coprime are inverse elements .
Note: As long as there is an inverse element can be asked, applicable to the number of inverses, but the mod is very large.
Attached to a Baidu encyclopedia example to deepen understanding:
?
Code:
/*time:2018/8/31 Writer:sykai Function: Using extended Euclidean to seek inverse element*/#include<iostream>#include<cstdio>#include<algorithm>#include<Set>#include<queue>#defineINF 0x3f3f3f3fusing namespacestd;Const intMAXN = 1e6 + -;Const intMOD = 1e9 +7; typedefLong LongLl;typedef pair<int,int>P;//The formula a?b+k?p=1 A is a,p bll EXGCD (ll A,ll b,ll& x,ll&y) { if(b = =0) {x=1; Y=0; returnA; } ll Res= EXGCD (b, a%b, y, x); Y-= a/b*x; returnRes;}//The formula A?b+k?p=1 A is a,p, which is the MoD.ll GETINV (intAintMoD//find the inverse of a under mod. Returns 1 if it does not exist{ll x, y; ll Res=EXGCD (a,mod,x,y); returnres =1? (X%mod + MoD)%mod:-1;//return res = 1? (x + MoD)%mod:-1;}intMain () {ll a=5; printf ("%lld\n", GETINV (A,mod)); return 0;}
Second, Fermat theorem to seek inverse element
Complexity: O (LOGN)
Fermat theorem: if P is prime, and gcd (a,p) = 1, then a^ (p-1) ≡1 (mod p).
a*a^ (p-2) ≡1 (mod p), then there is a^ (p-2) is the inverse of the P-remainder of a
Note: when P is a prime number, the Fermat theorem is generally used to find the inverse element.
Code:
/*time:2018/8/31 Writer:sykai Function: Using the fee Ma Xiao theorem to find the inverse element*/#include<iostream>#include<cstdio>#include<algorithm>#include<Set>#include<queue>#defineINF 0x3f3f3f3fusing namespacestd;Const intMAXN = 1e6 + -;Const intMOD = 1e9 +7; typedefLong LongLl;typedef pair<int,int>P;ll Qpow (ll a,ll b)//a*a^ (p-2) ≡1 (mod p) in a^ (p-2){ll res=1; while(b) {if(b&1) Res= Res * A%MOD; A= A * A%MOD; b>>=1; } returnRes;} ll GETINV (ll a,ll MoD) {returnQpow (a,mod-2);}intMain () {intA =5; printf ("%lld\n", GETINV (A,mod)); return 0;}
Iii. recursive derivation of inverse element
Complexity: O (N)
Attention:
1, the MoD needs to be prime, to obtain is 1~n about MoD's inverse element.
2, apply to mod is not too big, and is called multiple times.
3, before the start of the program need to pre-play table.
Code:
/*time:2018/8/31 Writer:sykai Function: linear inverse element*/#include<iostream>#include<cstdio>#include<algorithm>#include<Set>#include<queue>#defineINF 0x3f3f3f3fusing namespacestd;Const intMAXN = 1e6 + -;Const intMOD = 1e9 +7; typedefLong LongLl;typedef pair<int,int>P;ll INV[MAXN];//the size of the array needs to be adjusted according to the actual situationvoidGetinv () {inv[1] =1; for(inti =2; i < MAXN; i++) Inv[i]= (mod-mod/i) *inv[mod%i]%MOD;}intMain () {GETINV (); printf ("%lld\n", inv[5]); return 0;}
Four, recursion to seek inverse element
Complexity: O (LOGN)
Note: MoD needs to be a prime number (the Chinese remainder theorem is not very useful)
/*time:2018/8/31 Writer:sykai Function: Recursive seeking inverse element*/#include<iostream>#include<cstdio>#include<algorithm>#include<Set>#include<queue>#defineINF 0x3f3f3f3fusing namespacestd;Const intMAXN = 1e6 + -;Const intMOD = 1e9 +7; typedefLong LongLl;typedef pair<int,int>P;ll INV[MAXN];//the size of the array needs to be adjusted according to the actual situationll Getinv (ll x) {if(x = =1)return 1; return(mod-mod/x) *GETINV (mod%x)%MOD;}intMain () {printf ("%lld\n", GETINV (5)); return 0;}
V. To find the inverse element of factorial
Code: (not sure how to use it)
/*time:2018/8/31 Writer:sykai Function: Recursive seeking inverse element*/#include<iostream>#include<cstdio>#include<algorithm>#include<Set>#include<queue>#defineINF 0x3f3f3f3fusing namespacestd;Const intMAXN = 1e6 + -;Const intMOD = 1e9 +7; typedefLong LongLl;typedef pair<int,int>P;ll INV[MAXN];//the size of the array needs to be adjusted according to the actual situationll fac[maxn+1];//Order Multiplier Groupll Qpow (ll A,ll b) {ll res=1; while(b) {if(b&1) Res= Res * A%MOD; A= A * A%MOD; b>>=1; } returnRes;}intMain () {INV[MAXN]= Qpow (fac[maxn],mod-2); for(LL i = maxn-1; i>=0; i--) {Inv[i]= (inv[i+1]* (i+1))%MOD; } return 0;}
Reference blog:
75268911
79644386
Inverse Inv (template + application)