A general knowledge of number theory

Source: Internet
Author: User
Tags greatest common divisor integer division

1) Prime number


There are infinitely many theorem primes

Proof. If there is a maximum prime number P_max, set x = (P_1 * p_2 ... * p_max) + 1, at this time if X is a prime number, then x > P_max, contradiction, if X is composite, then there must be one of its prime factor p_x and p_x > P_max, contradiction.


It is obvious that the more sparse the distribution is in the larger range.

Determination of Prime number

The brute force method is to enumerate each digit that is less than N, and the simple optimization is to enumerate all prime numbers of 2~SQRT (n).

Because some pseudo-prime order Fermat theorem Converse is not established, and the back of a pseudo-prime table is not very graceful, miller-rabin algorithm with the resulting.


Strong pseudo Prime number
Set n is an odd integer greater than 4, s and T are positive integers that make (n-1) =2^s*t, where T is odd, and set B (n) is a set of integers defined as follows:
A belongs to set B (n) when and only if 2≤a≤n-2 and

2: Existence of integer i,0<=i<s satisfies a^ ((2^i) *t) mod n=n-1
When n is a prime number, any A in 2 and n-1, a belongs to set B (n)
When n is composite, if a belongs to set B (n), then n is a strong pseudo-prime number based on a (base), and a strong pseudo-evidence of N primality is called.
N is a prime number, indicating that it is a strong pseudo prime for all the bottom

Btest (a,n) {
n is an odd number and returns True. That is, the return true indicates that N is a strong pseudo prime
s←0; t←n-1;//t begins to be even
Until t mod 2 = 1;//n-1=2st T is odd
X←at mod n;
If X=1 or x=n-1 then return true;//satisfies ①or②.
For i←1 to S-1 do{
X←X2 mod n;
If X=n-1 then return true;//satisfies ②,
return false;}

By this definition, it is found that in odd composite less than 1000, the probability of randomly selecting a strong pseudo-evidence is less than 1%.
More importantly, to any odd composite, the proportion of strong perjury is very small
Therefore, we can run the following algorithm multiple times, we can reduce the error probability we can control the range

Millrab (n) {//Odd n>4, returns true-time representation of prime number, false representation composite
A←uniform (2..n-2);
Return Btest (A,n); Tests whether n is a strong pseudo prime number
}//the algorithm is 3/4-correct, partial false.

Repeatmillrob (n,k) {
For I←1 to K do
If Millrob (n) =false Then
return false; Must be composite.
return true;

Although Miller-rabin is a random algorithm, the first 9 primes are guaranteed to be correct within the 10^18 range.

The code is simple.

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <  Cmath> #define LL Long longusing namespace Std;int t, prim[15] = {0, 2, 3, 5, 7, one,,, 23};ll U;ll (ll X, ll K, ll MoD) {ll ret = 1;while (k) {if (K & 1) (ret *= x)%= mod; (x *= x)%= mod;k >>= 1;} return ret;} BOOL MR (ll A, ll N) {if (a >= N) return 1;ll w = Mypow (A, u, N), if (w = = 1) return 1;for (int i = 0; i < T; i + +) {if (w = = n-1) return 1; (w *= W)%= N;} return 0;} BOOL PD (ll N) {if (n = = 2) return 1;if (N < 2 | | (! (N & 1))) return 0;t = 0; U = N-1;while ((u & 1) = = 0) u >>= 1, t ++;for (int i = 1; I <= 9; i + +) if (!MR (Prim[i], n)) return 0;return 1;} int main () {int n;while (scanf ("%d", &n)! = EOF) {int cnt = 0;for (int i = 1; I <= N; i + +) {ll x; scanf ("%i64d", &x ); if (PD (x)) CNT + +;} printf ("%d\n", CNT);} return 0;}


Sieve method

All prime numbers not exceeding n are calculated.

First, you can miller-rabin each number, time complexity O (NLOGN)

Consider the Sieve method. The direct idea is that every time a prime number is sifted out, it is marked as composite in multiples of less than n, and the Complexity O (NLN (n))

In the above sieve, some of the prime numbers that have multiple mass factors will be sifted out many times, resulting in a lot of useless expenses. How to reduce redundancy? The direct idea is to mark each composite and mark it only once, and use its smallest quality factor to sift it out.

Then there is an optimized sieve: Enumerate each number, enumerate the prime numbers that were sifted out before, mark their product as composite, and break off if the current number is a multiple of the current prime.

Doing so will only be filtered out by the smallest quality factor. Because the smallest quality factor of the product is either the prime number of the enumeration or the smallest mass factor of the enumerated integer, the smallest factor of the composite that is filtered before break is the prime number of the enumeration, but if it does not break, The minimal quality factor of the composite that is then filtered out is the smallest mass factor of the integer rather than the prime number of the enumeration.

for (int i = 2; I <= n; i + +) {if (!isprim[i]) prim[++ pp] = i;for (int j = 1; j <= pp && i * prim[j] <= N; j + +) {Isprim[i * prim[j]] = 1;if (i% prim[j] = = 0) break;}


2) Greatest common divisor

Euclidean algorithm

GCD (A, b) = gcd (b, a% b)

If M | A and M | B (a >= b), then M | A-B, then M | A-k*b (k >= 0 and K*b <= a)

Find the solution of an indefinite equation ax + by = M.

If M is not a multiple of gcd (A, B), then the original equation obviously does not exist in the integer solution.

So the problem turns into ax + by = gcd (A, B)

Set ax1 + by1 = gcd (A, b); Bx2 + (a% b) y2 = gcd (b, a% b)

-Ax1 + by1 = bx2 + (a%b) y2 (Euclidean principle)

Below we want to get rid of the original formula in order to obtain X1, y1 value.

Ax1 + by1 = bx2 + (A-[A/b] * b) y2

= Ay2 + B (X2-[A/b] y2)

So we can get X1, y1 a set of solutions by recursion, the boundary is B = 0 o'clock, then the original degenerate into a Yi Yuanfang, obviously there is x = 1, y = 0


int exgcd (int a, int b, int &x, int &y) {if (!b) {x = 1, y = 0; return A;} int r = EXGCD (b, a% B, x, y), t = x;x = y, y = t-(A/b) * Y;return R;}

Set x0, y0 is a set of solutions for the indefinite equation ax + by = m, (A, B) = g, then the solution set is

{(x0 + b * t/g, y0 + A * t/g): T∈z}

This gives you the need for arbitrary solutions.

Linear congruence equations

If M1, M2 ... mn 22 coprime, the Chinese remainder theorem can be used.

China remainder theorem Description: Assume an integer m1,m2, ...,mn 22 coprime, then an arbitrary integer:a1, a 2, ..., a n, asolution to the equation group , and the general solution can be constructed in the following way:

Set is an integer m1, m2,..., mThe product of N, and is set apart mOutside of I N-a product of 1 integers. The general solution of the inverse equations of number theory set as modulo is that in the sense of modulo m, the equation set (S) has only one solution:

The correctness of this theorem is obvious. For each item in the original I, in the sense of the mode MI is only its own contribution to the AI * ti * mi, that is, AI * 1, the other items are a multiple of MI. So each modulo mi will be AI.

Of course, in M1 ... MN does not satisfy 22 coprime is the Chinese remainder theorem is unable to use, even the inverse element can not be obtained.

There is a more straightforward approach, but the complexity is O (LOGN).

Consider the first two equations x A1 (mod M1) and X≡A2 (mod m2)

can be written m1 * k1 + a1 = x, M2 * k2 + a2 = X

i.e. M1 * k1 + a1 = m2 * k2 + A2

i.e. M1 * k1-m2 * K2 = A2-A1

Set g = GCD (m1, m2), L = LCM (m1, M2)

M1 * k1/g-m2 * k2/g = (A2-A1)/g

At this time can be used to find out the K1, K2 a set of solutions, and then the K1 back to the original, so that the same residual equation group is merged into such a congruence equation:

X≡A1 + m1 * K1 (mod LCM (m1, M2))

Inverse element

Set positive integer m, for any positive integer a satisfies (a , m ) = 1, there is always a unique b satisfying a x b 1
(mod m ) and 1 b < m, called B is the inverse of modulo m .

The inverse element is applied in integer division in the modal sense.

The calculation of inverse element is mainly two kinds of methods

One is directly based on the definition b:a * b 1 (mod m) with the expansion of the Euclidean calculation.

Or with the Fermat theorem.

A ^ (p-1) 1 (mod p)

-a ^ (p-2) 1/p (mod p)

Of course, when using the Fermat theorem to ensure that P is a prime number, the expansion of the European time to ensure that a and P coprime can be.

3) Number theory function

In number theory, an arithmetic function (or number theory function) refers to a function that defines a positive integer for a field and a complex number, and each arithmetic function can be considered a sequence of complex numbers. (Well, I don't know what this phrase means,,)

Möbius function

The product of μ is very obvious.

There are theorems:

Σ (D|n) μ (d) = [n = 1]

I don't know what it's for.

This theorem can be used to introduce this equation: μ* {1} = E

Möbius inversion formula

The above are two forms of Möbius inversion.


Number theory function e (n) = [n = 1]

f * e = f, μ* {1} = E

->f (n) =σ (D|n) g (d) i.e. F = G * {1}

g (n) =σ (d|n) μ (d) * f (n/d) i.e. G = f *μ

-F *μ= G * {1} *μ= G * e = g

G * {1} = f *μ* {1} = f * e = f

Get proof

Möbius function and the principle of tolerance and repulsion

Set f (n) =∑ (d|n) φ (d), by the theorem in the Euler function below, f (n) = n

and φ (n) =∑ (d|n) μ (d) * f (n/d), i.e. φ (n) =∑ (d|n) μ (d) * n/d

The Möbius inversion is interpreted emotionally by examples:

Considering the positive integer k not exceeding N, according to the definition of Euler's function, I
We have to figure out how many K and n coprime.
Make gcd (n, k) =??, when?? >1 to be removed, consider the collection of mass
?? ={2,3,5,7,11,13,...},?? >1, obviously? Some of
Multiples of prime numbers and p does not contain a square factor. If there is a n/to meet such conditions, K?? A.
These need to be removed, but it is easy to see that there is repetition in the middle.

For P with two factors, to add n/p, there are three to subtract n/p ... With T-one to add ( -1) ^ T * (n/p)

Found that this is actually Möbius inversion of the introduction of the formula!

It's really beautiful.

Euler functions

Euler's theorem

For coprime positive integers a and N, there is aφ (n) ≡1 mod n.


Elimination Law: if GCD (c,p) = 1, then AC≡BC mod p⇒a≡b mod p.

(1) Make Zn = {x1, x2, ..., xφ (n)}, S = {A * x1 mod n, a * x2 mod n, ..., A * xφ (n) mod n},
Then Zn = S.
① because a with n coprime, Xi (1≤i≤φ (n)) and n coprime, so a * XI with n coprime, so a * XI mod n∈zn.
② if i≠j, then XI≠XJ, and by a, n coprime can get a * XI mod n≠a * XJ mod N (Elimination law).

(2) aφ (n) * x1 * x2 * ... * xφ (n) mod n
≡ (A * x1) * (A * x2) * ... * (A * xφ (n)) mod n
≡ (A * x1 mod n) * (A * x2 mod n) * ... * (A * xφ (n) mod n) mod n
≡X1 * X2 * ... * xφ (n) mod n
The left and right sides of the equation are compared, since XI (1≤i≤φ (n)) and n coprime, so aφ (n) ≡1 mod n (Elimination law).
O (≧v≦) o~~ Great

Fermat theorem is a special case when Euler theorem N is prime.

Two theorems

∑ (d|n) φ (d) = n

When N>1, an integer in 1...N with n coprime and n *φ (n)/2

The product of Euler's function

n = A * b, gcd (A, b) = 1, then:

φ (n) =φ (a) *φ (b)


For each positive integer of ≤n, it can be expressed as U = A * q + R (0 <= Q < b, 1 <= r <= a)

If you want to prove that gcd (U, N) = 1, is equivalent to proving gcd (U, a) = 1 and gcd (U, b) = 1

Consider the guaranteed gcd (U, a) = 1, i.e. for each OK Q, where r is not coprime with a, when and only if u and a are not coprime

For each q that is determined to be desirable, R has φ (a) extraction.

Consider the following to ensure that gcd (U, b) = 1, which is the value we will determine for R, for u1 = A * q1 + r, U2 = A * q2 + R, if q1! = Q2, there is u1! = U2 (mod n).

Disprove if u1 = U2 (mod n), a * q1 = a * Q2 (mod n), q1 = Q2 (mod n) (elimination rate), contradiction.

So sn = {u1, U2 ... ub} = {1, 2, .... b}, so the number of the UI and B coprime is φ (b).

For each R that is determined to be desirable, Q has φ (b) Medium extraction.

Synthetic, φ (n) =φ (a) *φ (b)

General formula of Euler function

(1) The Euler function of P^k is p^k-p^ (k-1)

This is obviously, in the p^k number of p in multiples of a total of p^ (k-1) A

(2) If GCD (p, q) = 1, then φ (p * q) =φ (p) *φ (q)

The nature of the integrable function has just been proven.

(3) Combining the first two, it is obvious that the Euler function formula of any integer can be obtained:

If n = pi^ki, then φ (n) = ∏ (pi^ki-pi^ (ki-1)) = ∏ pi^ki * (1-1/PI) = n * ∏(1-1/pi )

4) Original Root


Set (A, m) = 1, the smallest positive integer r that satisfies a^r = 1 (mod m) is called the order of a-modulo m.

There is a more obvious theorem: if R is the order of a-mode m, and a^k = 1 (mod m), when and only if R | K.

Also: R is the order of a-mode m and only if the following two conditions are true:

1, a^r = 1 (mod m)

2, for each r of the mass factor p, there is a^ (r/p)! = 1 (mod m)

Well, it's obvious, too,

Original root

If the whole number of a-mode m is φ (m), then A is called the original root of modulo m

Theorem: For positive integer m, modulo m has the original root and only if m= 2;4;PA;2PA,

where P is an odd prime and a >= 1.

Because the original root is usually very small, it is common to use enumerations to find the original root.

All the original roots of odd prime numbers

Theorem: If P is prime, then the prime number p must exist in the original root, and P's original root is Phi (p-1).

Set M is a positive integer, A is an integer, if the order of a-mode m is equal to φ (m), then A is called a primary root of modulo m.

Suppose a number g is the original root for P, then the result of G^i mod p is 22 different, and there is 1<g<p, 0<i<p, then G can be called a root of P, in the final analysis is g^ (P-1) = 1 (mod P) when and

Set up only when the index is P-1. (Here p is the prime number).

The current practice of seeking the original root is only to enumerate from 2, and then the Brute force Judgment g^ (P-1) = 1 (mod P) is set when the exponent is P-1. And because the original root is generally not big, so can be violent.

Find all the original root methods of an odd prime.

Set G is the square non-surplus of p, is the standard decomposition of P-1, if the constant is established,

Then G is the original root of P.

5) Combinatorial number finding mode

n * M can accept the word direct nm DP.

If M is small (m * Logp can accept) directly to the numerator and the denominator are listed to calculate the good (the numerator denominator is the number of O (m) level) for each number of numerator and denominator to the factor of P is the element of a single proposed to seek, the other directly multiply the denominator and multiply an inverse of the can.

N, M is very large and p is prime, you can use Lucas theorem.

is to put N, M are converted to P-system and then each individual count is good, very simple and practical.

The following question is if n, M are large but p is composite what to do about T T. Obviously if the composite of each quality factor of the prime numbers are <=1, then the direct CRT merging a bit better, so the following question is how to find C (n, m)% (P ^ c)


Number theory pdf by di

Jia Zhipeng Linear Sieve

Jzpkil problem solving report by Gyz

Grandson's theorem Baidu encyclopedia

The Euler theorem of number theory proves csdn basil people

A general knowledge of number theory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.