"Information security mathematical basis of a" first chapter notes
Divisible
- Is defined
in an integer domain, if \ (A = Q \ CDOT B \) , then \ (B \) divisible \ (A \) , denoted \ (a | b \) - nature
- If \ (A | b, \ b | c \) , then \ (a | c \)
- If \ (c | a_ {i} , \ i = 1, \ 2, \ .., \ n \) then \ (C \) is also divisible \ (a_ {i} \) linear combination
Prime numbers, the number of case
- Defined prime numbers
except \ (1 \) and itself, the number of no factor, called the prime number, also known as prime numbers, the other numbers are called composite. - \ (2 \) is the smallest prime number
Distinguishing Prime Number
- A Theorem
for Composite Number \ (n-\) , there must be no more than \ (\ sqrt {n} \ ) prime factors.Set \ (n-= PQ \) , provided \ (P \) is the minimum factor, the \ (p \ leq q \)
then \ (n = pq \ geq p
^ {2} \) so \ (p \ leq \ sqrt {n} \)
is assumed \ (P \) is a composite number, the decomposition may proceed with the title set does not match, so \ (P \) is a prime number. - Theorem inference
for a number \ (n-\) , no more than if it is not present \ (\ sqrt {n} \ ) prime factors, then it is a prime number. - Determining prime ordinary
enumerator \ (2- \ sqrt {n} \) prime numbers, if present (n-\) \ factor, then the \ (n-\) is a prime number, otherwise it is a composite number.
Sieve
Sieve method is used to find \ (2-n \) in all prime numbers within.
Egyptian-style screen
-
Algorithm thinking
to assume that all numbers are prime.
For Number \ (X \) , if it is a prime number, all of the screen to a multiple, and the mark is a composite number; if it is a composite number, then no change. -
Algorithm optimization
- For prime \ (x \) , from \ (x \) times begin to enumerate . Because the fold \ (X * Y, \ Y <X \) , has determined that \ (Y \) screens out the time.
For example, \ (35 = 5 * 7 \) , it is determined prime \ (5 \) sieve when to go once determined prime \ (7 \) when the can from the direct \ (49 = 7 * 7 \) starts sieve. - \ (n-\) primes within judged to \ (\ sqrt {n} \ ) can.
- For prime \ (x \) , from \ (x \) times begin to enumerate . Because the fold \ (X * Y, \ Y <X \) , has determined that \ (Y \) screens out the time.
-
Time complexity is \ (O (nlglgn) \)
-
\(code\)
int n, vis[N]; void getPrime() { for(int i = 2; i <= n; ++i) vis[i] = 1; for(int i = 2; i * i <= n; ++i){ if(vis[i]){ for(int j = i * i; j <= n; j += i) vis[j] = 0; } } }
Euler screen
-
The plight of Egyptian-style screen and improve
some composite number will be screened to the times, for example \ (7 * 9 = 63 = 3 * 21 \) . -
Optimized
so that each engagement is only the minimum number of prime factors to screen again, the complexity can be controlled to \ (O (n) \) -
detail
- By default, all first numbers are prime numbers, considered in the following cycle.
- If the current number is a prime number, all of its entire screen to multiple factors, which ensures that the composite number is the minimum prime factor to the screen.
- If the current number is a composite number, the screen to which prime multiple \ (X \) , if the \ (X \) divisible current composite number loop is exited, as the cycle continues to give new multiples can after being \ (X \ ) divisible.
-
\(code\)
int n, prime[N], vis[N]; int Euler_sieve() { int cnt = 0;//length of prime table for(int i = 2; i <= n; ++i) vis[i] = 1; for(int i = 2; i <= n; ++i){ if(vis[i]) prime[++cnt] = i; for(int j = 1; j <= cnt && prime[j] * i <= n; ++j){ vis[prime[j] * i] = 0; if(i % prime[j] == 0) break; } } return cnt; }
Hexadecimal conversion
Consider \ (K \) band, then a digit \ (digit for \) ranges \ ([0, \ k) \)
- To decimal conversion
for a (n-\) \ bit \ (K \) nary, namely into a decimal number
-
Converted to decimal number \ (K \) band
first mode \ (K \) obtained in the low \ (K \) value in binary, then in addition to \ (K \) rounding low.//k 进制的字符串向十进制转换 int num = 0; for(int i = 0; i < s.length(); ++i) num *= 10, num += s[i] - '0'; //十进制转换为 k 进制 stack<int> sta; while(num) { sta.push(num % k); num /= k; } while(!sta.empty()) cout<<sta.top()<<endl, sta.pop();
The greatest common factor and the least common multiple
-
Both the definition of
the word to its name. The former is the largest common factor, which is the smallest common multiple. -
relationship
\[[a,\ b] = \frac{ab}{(a,\ b)} \]\[[a_{0},\ a_{1},\ ...\ ,a_{n}] = \frac{\prod_{i = 0}^{n}a_{i}}{(a_{0},\ a_{1},\ ...\ ,a_{n})} \]By proving \ ((\ FRAC {A} {(A, \ B)}, \ \ FRAC {B} {(A, \ B)}) =. 1 \) , and \ ((a, \ b) = 1 , \ [a, \ b] = ab \) can be shown on the formula.
Euclidean algorithm
Euclidean algorithm is the Euclidean calculation greatest common divisor.
- Theorem \ (. 1 \)
\ (A = B + C CDOT Q \ \) in, \ ((A, \ B) = (B, \ C) \)Set \ ((a, \ b)
= d_ {1}, \ (b, \ c) = d_ {2} \) then \ (d_ {1} | a , \ d_ {1} | b, \ d_ { }. 1 | C \) , so \ (d_ {1} \ leq
d_ {2} \) Similarly \ (d_ {2} | b , \ d_ {2} | c, \ d_ {2} | a \) Therefore \ (d_ {2} \ leq
d_ {1} \) so \ (d_ {1} = d_ {2} \) - Theorem \ (2 \)
\ ((A, \ 0) = A \)\ (A \) is \ (A \) maximum factor \ (A \) is \ (0 \) factor, the \ ((a, \ 0) = a \)
- Euclidean algorithm
set \ (0} = R_ {A, \ R_ {} = B. 1, \ + R_ {n-0. 1} = \) , the following equation\[r_{0} = r_{1}q_{1} + r_{2} \\r_{1} = r_{2}q_{2} + r_{3} \\... \\r_{i - 1} = r_{i}q_{i} + r_{i + 1} \\... \\r_{n - 1} = r_{n}q_{n} + r_{n + 1} \]According to Theorem \ (1 \) and Theorem \ (2 \) and, after the division will be able to get the greatest common factor
Bézout's identity
- \(s\cdot a + t\cdot b = (a,\ b)\)
- Guess \ (R_ {I} = S_ {I} T_ {A + B} I, \ I \ in [0, \ n-] \) , using the mathematical induction to prove this conjecture.
Expand Euclidean algorithm
Seeking \ ((a, \ b) \) determined simultaneously \ (s \ cdot a + t \ cdot b = (a, \ b) \) is a particular solution \ (s_ {0}, \ t_ {0 } \)
-
Recursive algorithm
consideration has been demonstrated by the mathematical induction conjecture\[r_{i} = s_{i}a + t_{i}b,\ i\in [0,\ n] \]Simultaneous
\[r_{i} = r_{i - 2} - q_{i - 1}r_{i - 1} \]Get recursive
\[s_{i} = s_{i - 2} - s_{i - 1}q_{i - 1} \]\[t_{i} = t_{i - 2} - t_{i - 1}q_{i - 1} \]Wherein, \ (I \) from \ (2 \) starts counting, i.e. \ (i \ in [2, \ n] \)
\[q_{i} = \left [\frac{r_{i - 1}}{r_{i}}\right ] \]\[r_{i} = r_{i - 1}\ mod\ r_{i} = r_{i - 1} - q_{i}r_{i} \]Wherein, \ (I \) from \ (1 \) starts counting, i.e. \ (i \ in [1, \ n - 1] \)
Consider the initial value, easy to get
\[s_{0} = 1,\ s_{1} = 0 \]\[t_{0} = 0,\ t_{1} = 1 \]\[r_{0} = a,\ r_{1} = b \]\[q_{1} = \left[\frac{a}{b}\right] \]So recursive way, you can get the final special solution.
Consider space optimization, can only \ (s_ {0}, \ s_ {1}, \ t_ {0}, \ t_ {1}, \ r_ {0}, \ r_ {1}, \ q_ {0} \ ) several variables for recursion, when introduced into the assignment need to save the value of the intermediate variable, the final spatial complexity is a constant.
int s0, s1, s2, t0, t1, t2, r0, r1, r2, q1, cnt; pair<int, int> exgcd(int a, int b) { s0 = 1, s1 = 0, t0 = 0, t1 = 1; r0 = b, r1 = a % b, q1 = a / b; while(r1) { s2 = s1, s1 = s0 - s1 * q1, s0 = s2; t2 = t1, t1 = t0 - t1 * q1, t0 = t2; q1 = r0 / r1; r2 = r1, r1 = r0 - q1 * r1, r0 = r2; } return pair<int, int> {s1, t1}; }
-
Recursive algorithm
considering recursive end with \ (B = 0, \ (A, \ B) = A \) , so \ (s = 1, t =
0 \) considered has determined the \ ((b, \ a \ % b) \) in the case where \ (X ', \ Y' \) , back to solve \ ((a, b \) \) under \ (x, \ y \)
of\[bx'+ (a - \left \lfloor \frac{a}{b}\right \rfloor b)y' = ax + by \]solve
\[x = y',\ y = x' - \left \lfloor \frac{a}{b}\right \rfloor y' \]Thus all the way back, it is possible to obtain a group of specialized solutions \ (x_ {0}, \ y_ {0} \)
-
\(code\)
int exgcd(int a, int b, int &x, int &y) { if(!b) {x = 1, y = 0; return a;} int r = exgcd(b, a % b, y, x);//y的值被修改为x',x的值被修改为y' y -= (a / b) * x; return r; }
Fundamental Theorem of Arithmetic
- Fundamental Theorem of Arithmetic
For any larger than \ (1 \) integer \ (n-\) , which can be split into prime factors of the product in the form of power, called \ (n-\) standard factorization:\[n = \prod_{i = 1}^{k}p_{i}^{\alpha_{i}} \]Wherein, \ (P_ {I} \) is a prime number. - The number of factors
known by the multiplication principle, \ (n-\) number is a factor\[n = \prod_{i = 1}^{k}(1 + \alpha_{i}) \]