operational rules for modulesArithmetic RulesThe modulo operation is somewhat similar to the basic arithmetic, but with the exception of division. The rules are as follows: (A + b)% P = (a% p + b% p)% p (1) (A-a)% P = (a% p-b p)% P (2) (A * b)% P = (a% p * b% p)% P (3) (A^B) % P = ((a% p) ^b)% P (4) Binding Law: ((a+b)% p + c)% P = (A + (b+c)% p)% P (5) ((a*b)% P * C)% P = (A * b*c)% P (6)//(A%p*b )%p= (A*B)%p Exchange Law: (A + b)% P = (b+a)% P (7) (A * b)% P = (b * A)% P (8) Distributive Law: ((a +b)% P * C)% P = ((A * c)% p + (b * c)% p)% P (9) Important theorem: if A≡b (% p), for any C, there is (A + c) ≡ (b + c) (%p), (10) if the A≡b (% p), for any C, there is (A * c) ≡ (b * c) (%p), (11) If the A≡b (% p), C≡d (% p), then (A + c) ≡ (b + D) (%p), (a-c) ≡ (b-d) (%p), (A * c) ≡ (b * d) (%p), (A/C) ≡ (b/d) (%p); (12)multiplication Inverse:Definition: The K value satisfying a*k≡1 (mod p) is a multiplicative inverse of p. Why do we have to multiply the inverse element? When we ask for (A/b) mod P's value, and a is large, and cannot directly obtain a A/b value, we will use the multiplication inverse. We can use the B to multiply the inverse k of p, multiply a by the K-mode p, ie (a*k) mod p. The result is equivalent to (A/b) mod p. Card: (actually very simple ...) According to B*k≡1 (mod p) there is b*k=p*x+1. k= (p*x+1)/b. Put K into (a*k) mod p, get: (A * (p*x+1)/b) mod p = ((a*p*x)/b+a/b) mod p =[((a*p*x)/b) mod p + (A/b)] mod p =[(p* (a*x)/b) mod p + (A/b) ] MoD p//P*[(a*x)/b] MoD p=0 so the original equals: (A/b) mod p
The multiplication inverse is used when calculating a large number of MODP. That is, the/a becomes * (f (a)), where F (a) is a multiplicative inverse of a in modulo P meaning, i.e. a*f (a) mod p=1There are two methods for calculating the multiplication inverse, extending the GCD or the fast power modulo based on Euler's formula. ------------------------------------------------------------------------------------------------------extending GCD is solving equation ax=1the smallest integer solution (mod P). Set AX=1+y*p, i.e. a*f (a,p) =1+p*g (a,p), the solution of X and Y as a function of a,p. AWhen >=p, set a=p*k+R, then the equation becomes: P*k*f (a,p) +r*f (a,p) =1+p*g (a,p), move item, get R*f (a,p) =1+p* (g (a,p)-k*f (a,p)) then get F (a mod p,p)=f (A,p), g (a mod p,p) =g (a,p)-[a/p]*f (a,p);p>=A is almost a meaning. So the f,g is set to a global variable, like ordinary gcd do, instant update f,g can. ----------------------------------------------------------------------------------------------------set Euler function phi (x)= in [1, X-1] The number of digits in and x coprime. So Euler's formula: a^phi (P) =1(mod P) with the exception of a, you can get a^ (Phi (P)-1mod p is the multiplicative inverse of a in the sense of modulo p. Exception:
If P is a prime number, then Phi (p)=p-1The multiplication inverse of A is a^ (P-2), the fast power can be.
#include <iostream>using namespacestd;intEXTENDED_GCD (intAintBint&x,int&y) { if(b = =0) {x=1; Y=0; returnA; } Else { intGCD = EXTENDED_GCD (b, a%b, x, y); intt=x%MoD; X=y%MoD; Y= ((t-a/b*x)%mod+mod)%MoD; returnGCD; }}intMain () {intI, x, y; Const intP = -; for(i =1; i < P; ++i) {extended_gcd (I, P, x, y); while(X <0) x + =P; printf ("1 div%d =%d\n", I, X); } return 0;}
Multiplication Inverse element