The integration of data for personal use, non-my original, invasion delete apology
multiple
For natural numbers a, b, if there is a natural number k such that ka = b, then b is a multiple of, say a divides b, b can be a divisible, denoted a | b
For natural numbers a, b, if b is a multiple of b, then a is divisor, also known as factor
All numbers are about the number itself and comprising (or) 1
int D [MAXN]; // calculate [1, n] of each number divisor number // complexity of O (log n-n-) void calc_divisors ( int n-) { for ( int I = . 1 ; I < n-=; ++ i) { // enumerate all multiples of i for ( int J = i; J <= n-; = J + i) { D [J] ++ ; } } }
Prime Azukago number
If a divisor of the number of only two (1 and itself), then the prime number called (or prime)
// determines whether a number is a prime number BOOL is_prime ( int X) { // Laid subcontracting 1 IF (X == 1 ) return to false ; // from 2 to enumerate √x, determines whether or not divisible for ( int I = 2 ; I * I <= X; ++ I) { // if divisible, compared with the number of co- IF (% I X == 0 ) return to false ; } // if are not divisible, for the prime return to true ; }
Simple sieve method:
int VIS [MAXN], P [MAXN], CNT; // sieved [2, n] between all primes @ complexity: O (log n-n-) void naive_sieve ( int n-) { for ( int I = 2 ; i <= n-; ++ i) { IF (VIS [i]) P [CNT ++] =! i; // screen out all multiples of i itself except for ( int J = i + i; J <= n-; = J + I) { VIS [J] = . 1 ; } } }
Egyptian style sieve method:
int VIS [MAXN], P [MAXN], CNT; // sieved [2, n] between all primes @ complexity: O (log n-n-log) void eratosthenes_sieve ( int n-) { for ( int I = 2 ; i <= n-; ++ i) { IF (! VIS [i]) { P [CNT ++] = i; // screen out all multiples of i itself except for ( int J = i + i ; J <= n-; = J + I) { VIS [J] = . 1 ; } } } }
Euler sieve (sieve linear):
int VIS [MAXN], P [MAXN], CNT; // sieved [2, n] among all primes @ complexity: O (n-) void euler_sieve ( int n-) { for ( int I = 2 ; I <= n-; ++ I) { IF (VIS [I]) P [CNT ++] =! I; for ( int J = 0 ; I * P [J] <= n-; ++ J) { VIS [I P * [J]] = . 1 ; // ensure p [j] is i * p [j] is the smallest prime factor IF (I% p [j] == 0 ) BREAK ; } } }
The only decomposition theorem (Fundamental Theorem of Arithmetic)
(Unique Factorization Theorem) each a natural number greater than 1 can be written as a product of prime numbers, the prime factor and then by size, writing only one way.
105 = 3 × 5 × 7
200 = 2 × 2 × 2 × 5 × 5 = 23 × 5 2
Written as the product of powers of prime factors, x = p1 k1 p2 k2 p3 k3 · · · pn kn
If the number does not contain a prime factor, it can be regarded as 0
Corresponding to the number of multiplications is the prime factor is added, corresponding to the division number of prime factors is subtracted
If a number of factors are all greater than the prime number corresponding to the number of times equal to another prime factor, then it may be divisible
Factoring
// the prime factor decomposition of x, the result is stored in small to large P [], the number of prime factors to return int factorize ( int x, int P []) { int CNT = 0 ; // from 2 to enumerate √x , it is determined whether or not a divisor for ( int I = 2 ; I * I <= x; ++ I) { // If find a divisor, is removed continuously from x to the while (I% x == 0 ) { P [CNT ++] = I; x / = I; } } // if x> 1, then the remaining x is a prime number, but also into the array IF (x> . 1 ) P [CNT ++] = x; return cnt; }
The greatest common divisor (gcd)
Define (greatest common divisor) of two to about a natural number common to a number of the largest, is called the common denominator thereof (Greatest Common Divisor)
And using the unique decomposition theorem corollary, it was found that the number of gcd two prime number corresponding to the number obtained after taking min factor
In practical applications, usually Euclidean algorithm to calculate gcd
Euclidean algorithm (Euclidean algorithm)
Lemma (the principle of division removed)
For the definition of an arbitrary natural number a, gcd (0, a) = gcd (a, 0) = a. Then for an arbitrary natural numbers a, b, satisfying gcd (a, b) = gcd (a, b mod a)
Using this nature can be a, b, both of a larger shrinking until zero
Each time a large number of small numbers modulo reduced at least half, so complexity is O (log max (a, b))
Euclidean
// 循环实现 int gcd(int a, int b) { while (a>0) { int t=b% a; b= a; a= t; } return b; } // 递归实现 int gcd(int a, int b) { if (a ==0) return b; return gcd(b% a, a); }
The least common multiple (lcm)
Define (least common multiple) of two natural numbers common multiple of the smallest one, which is called the least common multiple (Least Common Multiple)
And using the unique decomposition theorem corollary, it was found that the number of lcm two prime number corresponding to the number obtained after taking factor max
Also not required to seek, as long as the formula can be transformed into a problem gcd
lcm(a, b) = ab gcd(a, b)
The only decomposition theorem can be used to prove
Extended Euclidean Algorithm
Theorem (Theorem Shu Pei)
For any integer a, b, exist infinitely many pairs of integers (x, y) satisfies indeterminate equation ax + by = d, where d = gcd (a, b)
While seeking gcd (a, b) may be obtained (about x, y a) ax + by = d a set of integer indefinite equation Solution
Consider the recursive computation: assumed to have been worked out (b% a, a) a set of solutions (x0, y0) satisfies (b% a) x0 + ay0 = d
It can be obtained (ba ⌊ ba ⌋) x0 + ay0 = d • finishing to give a (y0 - ⌊ ba ⌋ x0) + bx0 = d
For convenience, the exchange x0 and y0, to give a (x0 - ⌊ ba ⌋ y0) + by0 = d
Extended Euclidean Algorithm
// return gcd (a, b) and ax + by = a set of solutions gcd (a, b) equation int GCD ( int A, int B, int & X, int & Y) { IF (A == 0 ) { // GCD (0, B) = B, obviously 0 · 0 + 1 · b = b satisfy the condition X = 0 , Y = . 1 ; return B; } // here exchanged x0 and yO int D = GCD (B% A, A, Y, X); // X = x_0 - B / A \ Times y_0 // Y = y_0 X - = B / A * Y; return D; }
Modulo operation
For a natural number a, a positive integer m, a mod m = am ⌊ am ⌋ I also weighed, as the number is divisible by I actually obtained a mod m ∈ [0, m)
As slow a modulo operation and divider
Try not to take a negative mold
Number sense and operations under the mold
If a mod m = b mod m, can be denoted a ≡ b (mod m)
We can use all natural numbers [0, integer "representative" between m)
The modulus can be extended to negative, integers k a mod m = b (b ∈ [0, m)) ⇔, satisfying a = km + b
We can build a new digital computing system, wherein only 0 ~ m - 1 This number m, and establishing four basic arithmetic rules
Sum of two numbers, if exceeded m - 1, from 0 to add further on, similar to adding the multiplied
Subtract two numbers, if less than 0, the m - 1 down again Save
32-bit unsigned integer (unsigned int) operation is actually modulo 2 32
Nature of operation
(a + b)%m = (a%m + b%m)%m
(a − b)%m = (a%m − b%m)%m
(a × b)%m = (a%m) × (b%m)%m
If the answer to the requirements of the final title modulo m, then (in order to prevent an excessive number) may both modulo calculation at each step
Multiplicative inverse
Addition, subtraction, multiplication are defined, the division how to do it?
The dividend can continue to add m, until it can be divisible, but this efficiency is too low
When the recall previous learning scores, divided by a number, it can be converted to take reciprocal
Similarly, we can try to find a divisor multiplicative inverse, i.e. an integer satisfying xx-1 ≡ 1 (mod m) of x -1
If and only if m is a prime number, each number has a unique multiplicative inverse, so most OI modulus in question are primes
Fermat's Little Theorem
Theorem (Fermat's little theorem) For any prime number p and positive integer a <p, there ap-1 ≡ 1 (mod p) certificate.
Easy to prove, a, 2a, 3a,,... (P - 1) a (mod p) can take over 1 to p - all the numbers between 1
Thus a · 2a · 3a · · · (p - 1) a ≡ (p - 1)! (Mod p)
Since p is a prime number, and 1 ~ p - prime to all numbers between 1, it can be divided simultaneously on both sides of the equation (p - 1)!
Get the ap-1 ≡ 1 (mod p)
Rapid Inversion yuan
With Fermat's little theorem, you can quickly find multiplicative inverse of a number
Since the (mod p), it is ap-2% p inverse ap-2 · a ≡ ap-1 ≡ 1
Can be calculated by flash power, complexity of O (log p)
It is also possible to find the inverse element with Extended Euclidean Algorithm, ax + py = obtain a set of solutions 1, x% p is the inverse element
Chinese Remainder Theorem (CRT)
There are a number of individuals lined up, three in a row will be more than two people, three people more than five in a row, seven in a row more than two people, the number is seeking at least how much?
Essentially solving a linear congruence equation
x ≡ a1 (mod m1)
x ≡ a2 (mod m2)
. . .
x ≡ an (mod mn)
When m is prime to each other, some solvability
Let's try to construct this solution
设 M = m1 × m2 × · · · × mn,Mi = M mi
Mi is not zero in the mold mi only when the other mode m are zero
Because we want to get ai in the mold mi, it is necessary to multiply aiti, where ti is the multiplicative inverse Mi mi significance in the mold
Finally obtained a1t1M1 + a2t2M2 + · · · + antnMn, again modulo M, to the minimum number obtained solution, together with the general solution is kM