Number theory
1. Fast Power
typedefLong LongLL; LL Qpow (intAintb) {//Fast PowerLL res =1; while(b) {if(B &1) Res = res * A%p; A= A * A%p; b>>=1; } returnRes;} LL Mul (intAintb) {//Fast MultiplyLL res =0; while(b) {if(B &1) Res = (res + a)%p; A= (A + a)%p; b>>=1; } returnRes;}
2. Inverse element
(1) Fermat theorem seeking inverse element
If P is prime, and p is not an approximate number of a
According to Fermat theorem There are: ap-1≡1 (mod p)
So the inverse of a is x = Ap-2
(2) Euler theorem seeking inverse element
If A and P coprime
According to Euler's theorem there are: aφ (P) = 1 (mod p)
So the inverse of a is x = Aφ (p)-1
3. Greatest common Divisor GCD
(1) from large to small enumeration
(2) Decomposition factorization solution
(3) More subtractive gcd (A, b) = GCD (A, B)
(4) gcd (A, b) = GCA (a% B, b)
int gcd (intint y) { return0 ? x:gcd (x, x% y);}
4. Least common multiple LCM
int LCM (intint y) { return x/gcd (x, y) * y;}
5. Extended Euclid EXGCD
(1) ax + by = gcd (A, B)
(2) x = x + b/gcd * k, Y = y-a/gcd * k;
(3) ax + by! = K*GCD (A, b) (k! = 0) No solution at this time
(4) Make k = C/GCD (A, b)
Equivalent to ask K * (ax + by) = GCD (A, b) * k
After you find ax + by = gcd (A, B), you will be able to multiply K
void EX_GCD (ll A, ll B, LL &d, LL &x, LL &y) { if10
; }
else {EX_GCD (b, a% B, D, y, x); y-= x * (A/
b);}}
6. Congruence
A≡b (mod p) → (A-b) mod p = 0;
Ax≡1 (mod b) equivalent to ax + by = 1
When a, b coprime, there is a solution
A group of solutions are obtained by using EXGCD, and then the solution of the minimum positive integer is obtained.
7. Congruence equations
Consider the equation set X≡R1 (mod m1)
X≡R2 (mod m2)
x = K1 * m1 + r1 = K2 * m2 + r2→k1 * m1-k2 * m2 = r2-r1
Equivalent to ax + by = C
8. Discrete logarithm
9. Linear Sieve Primes
for(inti =2; I <= N; ++i) {if(!bz[i]) pri[++cnt] =i; for(intj = i; J <= CNT; ++j) {if(i * pri[j] > N) Break; Bz[i* Pri[j]] =1; if(i% pri[j] = =0) Break;//guaranteed that each number will only be sifted once.//will only be sifted away by their own minimal quality factor. }}
10. Euler functions
Euler function by linear sieve method
//the Euler function is obtained by the way of the Sieve prime for(inti =2; I <= N; ++i) {if(!bz[i]) pri[++cnt] = i, phi[i] = i-1; for(intj =1; J <= CNT; ++j) {if(i * pri[j] > N) Break; Bz[i* Pri[j]] =1; if(i% pri[j]! =0) Phi[i * Pri[j]] = phi[i] * (Pri[j]-1); if(i% pri[j] = =0) {Phi[i* Pri[j]] = phi[i] *Pri[j]; Break; } }}
11. Number of combinations
(1) C (m, n) = m! /n! (M-N)!
Example: NOIP2016 d2t1 Combinatorial number problem
Solution: Yang Hui triangle pretreatment, the two-dimensional prefix and f[i][j], and in the process of K modulus, according to the value of F[i][j] is 0 to determine whether a multiple of K
(2) Lucas theorem
(3) If the data range is small (0≤n≤m≤105, 1≤p≤109), can directly preprocess the factorial and factorial of the inverse, directly calculated
(4) Some more useful formulas
12. Principle of tolerance and repulsion
With a total area of a + B + C-AC-AB-BC + ABC
The important function of tolerance is to simplify and simplify
13. Fibonacci Sequence
F[0] = f[1] = 1;
F[i] = F[i-1] + f[i-2];
14. Number of Cattleya
C (1) = 1;
C (N) = C (1) * C (n-1) + C (2) * C (n-2) + ... + c (n-1) * C (1);
General formula: C (n + 1) = C (2n, N)-C (2n, n-1);
15. Wrong row
Consider an arrangement with n elements, and if all elements in an arrangement are not in their original position, then it is called a permutation of the original arrangement.
N-Elements with a wrong row of D (n)
D (1) = 0; D (2) = 1;
D (n) = (n-1) (d (n-1) + D (n-2));
16. Gaussian elimination element
(1) for solving multiple systems of one-time equations
(2) different or Gaussian elimination elements
The equivalent of adding or reducing to a different or, in fact, simpler
17.
Hui si 18 National Day number theory