About multiplication inverse a*b=1 (mod p) A is b about the multiplication inverse of P
(1) solve the problem of division cannot be touched
(A + b)% P = (a%p + b%p)%p (pair)
(A-B)% P = (a%p-b%p)%p (pair)
(A * b)% P = (a%p * b%p)%p (pair)
(A/b)% P = (a%p/b%p)%p (wrong) this is wrong. The correct operation of this we can ask B about the multiplication inverse of P x
(A/b)% p= (a%p) * (x% p)%p; (yes)
The multiplication inverse of a about B is only gcd (A, B) to have the inverse element.
(1) Expanding Euclidean algorithm
#include <bits/stdc++.h>using namespacestd;voidEXGCD (intAintBint&x,int&y) { if(b==0) {x=1, y=0; return; } exgcd (B,a%b,x,y); intxx=x; X=y; Y=xx-a/b*y;} int32_t Main () {intA=5; intb=3; if(__GCD (A, b) = =1) { intXinty; exgcd (a,b,x,y); cout<< (x%b+b)%b<<Endl; //x A guan Yu B de Chengfaniyuan }}
(1) Fermat theorem P are prime gcd (a,p) = 1; a^ (p-2) and a mutually multiply inverse (mod p);
This is not necessarily the smallest inverse of the p can be found to find the smallest
#include <bits/stdc++.h>using namespacestd;intQuickintAintN) { intans=1; intt=A; while(n!=0) { if(n%2==0) {n=n/2; t=t*T;} Else if(n%2==1) {n--; ans*=T;} } returnans;} int32_t Main () {intA=5; intb=3; if(__GCD (A, b) = =1) {cout<<quick (a,b-2)%b<<Endl; }}
(3) This I don't know when P is a prime number. INV (a) = (P-P/a) * INV (p.% a)% p does not prove (source of code below) https://www.cnblogs.com/linyujun/p/5194184 . html#3804000
#include <cstdio>typedefLong LongLL; LL INV (ll T, ll P)
{
//ask T about the inverse of P, note: T to be less than p, it is better to pass the t%p before the argument returnt = =1?1: (P-P/T) * INV (p.% T, p)%p;}intMain () {LL A, p; while(~SCANF ("%lld%lld", &a, &p)) {printf ("%lld\n", INV (a%p, p)); }}
Multiplication of n number by inverse O (n)
#include <cstdio>Const intN =200000+5;Const intMOD = (int) 1e9 +7;intInv[n];intinit () {inv[1] =1; for(inti =2; i < N; i + +) {Inv[i]= (mod-mod/i) * 1LL * inv[mod% i]%MOD; }}intMain () {init ();}
Multiplication Inverse element