Divisor
About divisor
Definitions :
When the remainder of the integer n by an integer d is 0, i.e., d divisible by n, is called d n, about the number, n is a multiple of d, denoted by d | n
In the Fundamental Theorem of Arithmetic \ (N \) can be decomposed into the following way
\[N=\prod_{i=1}^m p_i^ {c_i}, \ p_1<p_2<…<p_m , \ c_i ∈ N^*\]
Then \ (N \) number is a positive number from about:
\[(c_i+1)*(c_i+2)*…(c_m+1)=\prod_{i=1}^{m}(c^i+1)\]
\ (N \) of all positive divisors are:
\[\prod_{i=1}^{m}{(\sum_{j=0}^{c^i}(p_i)^j)}\]
Solution \ (N \) is set about several positive
- Trial division
$ \ $ Qquad if a number \ (X \) is the \ (N \) divisors, then \ (N / d≤ \ sqrt N \) is also \ (N \) divisors of.
$ \ Qquad $ because the divisor always come in pairs, so scanning \ (the X-= 1- \ sqrt N \ ∈ Z \) , try whether \ (the X-| N \) . But we have to special sentenced perfect square, because for a perfect square \ (\ sqrt N \ ∈ Z \) .
int factor[1600], num = 0;
for(int i = 1; i * i <= n; i++) {
if(n % i == 0) {
factor[++num] = i;
if(i != n/i)
factor[++num] = n / i;
}
}
$ \ $ Qquad request \ (1- \ sqrt N \) n-number divisor of each set - COLORED
The basic idea:
Different from the trial division, we can consider each in turn number \ (x \) , places \ (x \) is a divisor number is \ (x, 2x, 3x ... \)
vector<int> factor[SIZE];
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n / i; j++)
factor[i*j].push_back(i);
//输出
for(int i = 1; i <= n ;i++) {
for(int j = 0; j < factor[i].size; j++)
printf("%d ",factor[i][j]);
putchar('\n');
}
$ \ $ Qquad in a small data ( \ (n∈ [4,16] \) ), is faster than the trial division multiple methods, complexity \ (O (N \ sqrt N ) \)
Divisor related content:
1. The greatest common divisor of | \ (gcd \)
- Definitions :
If the natural numbers \ (x \) satisfies \ (x | a \) and \ (the X-| b \) , called \ (x \) is \ (a \) is (b \) \ common divisor, then \ ( max (x) \) is \ (a \) and \ (B \) is the greatest common divisor, referred to as \ (GCD (a, B) \) .
Similarly, if while meeting \ (a | x \) and \ (B | X \) , then \ (X \) is the \ (A \) and (B \) \ common multiple, in such \ (X \ ) in a minimum, for the \ (a, b \) the least common multiple, referred to as \ (LCM (a, B) \) .
theorem:
\[∀a,b∈N, \qquad \qquad gcd(a,b)*lcm(a,b)=a*b \]
Proof reader as defined above can try yourself, you can also "reference algorithm contest Step-up Guide"
- Solution
In On prime factor decomposition we have already mentioned solving \ (gcd \) algorithm: 1. Decreases Technique 2. Euclidean algorithm.
"Nine Chapters on Arithmetic" are:
\[∀a,b∈N,a≥b,\quad gcd(a,b)=gcd(b,a-b)=gcd(a,a-b)\]
\[∀a,b∈N,\quad gcd(2a,2b)=2gcd(a,b)\]
It is important above two theorems, which involve binary Optimized behind us \ (gcd \)
Euclidean algorithm:
\[∀a,b∈N,b≠0,\qquad gcd(a,b)=gcd(b,a \quad mod \quad b)\]
This gives the familiar Euclidean algorithm :
//递归形式
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
//非递归形式
int gcd(int a, int b) {
int temp;
while(b) {
temp=a % b;
a = b;
b = temp;
}
return a;
}
$ \ Qquad \ qquad \ (Euclidean algorithm complexity is \) O (log (A + B)) $. For high-precision arithmetic, modulo not easy to achieve, it is recommended Decreases Technique instead.
$ \ Qquad \ qquad \ (Of course, \) gcd $ can also be optimized (optimization is binary mentioned earlier).
- Why can optimize? Because use of the Euclidean algorithm modulo operation, so that constant slow running large, so with optimized binary Decreases + \ (GCD \)
\(a=0, \quad return \quad b; \quad \mid \quad b=0, \quad return \quad a;\)
\ (A <B, A \ Quad XOR = \ Quad B, \ Quad B \ Quad XOR = \ A Quad, \ Quad A \ Quad XOR = \ Quad B; \) (unique binary exchange)
\(a\) & \(1\) 且 \(b\) & \(1\),$ \quad ans=2gcd(a >> 1,b>>1);$
\(a\) & \(1\) 且 !\(b\) & \(1\),$ \quad ans=gcd(a >> 1,b);$
!\(a\) & \(1\) 且 \(b\) & \(1\),$ \quad ans=gcd(a,b >> 1);$
!\(a\) & \(1\) 且 !\(b\) & \(1\),$ \quad ans=gcd(a - b,b)$。
//下面代码返回gcd(a,b)的值同时把b赋予这个值,不需要可以把&去掉
int gcd(int a,int &b) {
if(a == 0 || b == 0) return b = a + b;
int n = 0, m = 0;
for(; !(a & 1); a >>= 1, n++);
for(; !(b & 1); b >>= 1, m++);
n = m < n ? m : n;
while(a) {
if(a < b) { a ^= b, b ^= a, a ^= b;}
if(!(a -= b)) return b <<= n;
while(!(a & 1)) a >>= 1;
}
}
However, note that when the above code is wrong negative, because the negative right \ (1 \) bit and other \ (2 \) not the same as, the above negative encountered normal use bit operation instead of division.
2. The prime function of Euler
- definition:
\ (∀a, b∈N \) , if the \ (GCD (A, B) =. 1 \) , called \ (a, b \) prime.
For three numbers and above analogy to the situation, not repeat them here, readers can access relevant information on their own.
Euler function
$ \ $ Qquad \ (. 1-N \) with \ (N \) prime number is called the Euler function, referred to as \ ([Phi] (N) \) .
In the basic arithmetic theorem, \ (N = \ prod_. 1} = {I ^ m ^ {P_i} C_i \) , then:
\[φ(N)=N*\frac{1-p_1}{p_1}*\frac{1-p_2}{p_2}*\frac{1-p_3}{p_3}*…*\frac{1-p_m}{p_m}=N*\prod_{prime \ p|N}(1-\frac{1}{p})\]
The calculation formula of Euler function, we need only decomposition of the quality factor, to obtain the Euler function:
//参考代码(源于《算法竞赛进阶指南》)
int phi(int n) {
int ans = n;
for(int i = 2; i <= sqrt(n); i++)
if(n % i == 0) {
ans = ans / i * (i - 1);
while(n % i == 0) n /= i;
}
if(n > 1) ans = ans / n * (n - 1);
return ans;
}
- The nature of the Euler function:
\ (∀n> 1,1-n \ ) with \ (n-\) prime number and is \ (n-* [Phi] (n-) / 2 \) .
If \ (GCD (A, B) =. 1 \) , i.e. \ (a, b \) prime, then \ ([Phi] (ab &) = [Phi] (A) [Phi] (B) \) .
Set \ (P \) is a prime number, if \ (p \ mid n \) and \ (P ^ 2 \ MID n-\) , then \ ([Phi] (n-) = [Phi] (n-/ P) * P \) .
Set \ (P \) is a prime number, if \ (p \ mid n \) and \ (P ^ 2 \ the nmid n-\) , then \ (φ (n) = φ (n / p) * (p-1) \) .
\(\sum_{d \mid n}φ(d)=n\)。
Multiplicative function:
$ \ Qquad $ if \ (gcd (A, b) = 1 \) , there \ (f (ab) = f (A) f (b) \) , then known as the function \ (f \) is a multiplicative function
- If \ (F \) is the product of the function and the Fundamental Theorem of Arithmetic in \ (n-= \ prod_ {I =. 1} ^ m P_i ^ {C_i} \) , then $ f (n) = \ prod_ {i = 1} ^ mf (p_i ^ { c_i}) $.
(Sixth with the nature of the Euler function)
\ (* \) Is completely multiplicative function:
\[∀a,b∈Z, \qquad f(ab)=f(a)f(b)\]
Expand on multiplicative function is very large, the content is more esoteric, following a brief introduction:
Common multiplicative function as
\ ([Phi] (n-) \) - Euler function
\ ([sigma] (n-) \) - divisors function
\ ([mu] (n-) \) - Mobius function
\ (σ_0 (n-) \) - about a few number of functions
\ (σ_k (n-) \) - about the number and function number
\ (GCD (n-, K) \) - the greatest common divisor function, when \ (K \) the fixed time
\ (1 (the n-) = 1 \) - I do not know what this is
\ (f (the n-) = the n-\) - I still do not know what is
$ \ Qquad $ Another point is multiplicative function are linearly screen
- Dirichlet convolution
First, start by supplementing the next number theoretic function definition:
Codomain: arbitrarily set value range comprising
Arithmetical functions: domain is a positive integer, accompany domain is a complex function
Well, we can begin to introduce Dirichlet convolution of.
Definition of \ (f, g \) number theoretic function, their Dirichlet convolution can be expressed as \ (F * G \) , provided \ (F * G = H \) :
\[h(n)=\sum _{d|n}f(d)g\Big(\frac{n}{d}\Big)\]
If \ (f, g \) is a multiplicative function, obviously, \ (H \) is also a multiplicative function.
prove
$ \ $ Disposed qquad \ (n = a * b \ ) and \ (a, b \) coprime, i.e. \ (GCD (A, B) =. 1 \) :
\[h(n)=\sum _{d_1|a, d_2|b}f(d_1d_2)g\Big(\frac{a}{d_1}\frac{b}{d_2}\Big)\]
\[=\sum_{d_1|a, d_2|b}f(d_1)f(d_2)g\Big(\frac{a}{d_1}\Big)g\Big(\frac{b}{d_2}\Big)\]
\[=\sum_{d_1|a}f(d_1)g\Big(\frac{a}{d_1}\Big)\sum_{d_2|b}f(d_2)g\Big(\frac{b}{d_2}\Big)\]
\[=h(a)*h(b)\]
$ \ Qquad $ proof.
Algorithms
Dirichlet convolution operations to meet:
\ (F * F * G = G \) (commutative)
\ ((F * G) = F * H * (F * G) \) (associative law)
\ (F * (G + H) + = F * F * G H \) (distributive property)
If \ (f, g \) is the product of a function, the \ (f * g \) is multiplicative function. (nature)
Dirichlet convolution related
Below or return to the Euler function:
$ \ $ Qquad we can use the \ (of Eratosthenes \) sieve, is calculated according to the Euler function, the \ (O (NlogN) \) within solved \ (2-N \) in each of the Euler number function.
int phi[SIZE];
void eluer(int n) {
for(int i = 2; i <= n; i++) phi[i] = i;
for(int i = 2; i <= n; i++)
if(phi[i] == i)
for(int j = i; j <= n; j += i;)
phi[j] = phi[j] / i * (i - 1);
}
$ \ Qquad $ but since that is a multiplicative function can be linear sieve, then how to optimize a linear function of the Euler it? Let's review a prime number sieve linear thinking ( prime number sieve Detailed ), linear sieve, each composite number \ (n \) will be its prime factors sieve once, use the following several properties:
Theorem three: Let \ (P \) is a prime number, if \ (p \ mid n \\) and $ P ^ 2 \ MID n- \ (, then \) [Phi] (n-) = [Phi] (n-/ P) * P $ .
Theorem four: Let \ (P \) is a prime number, if \ (p \ mid n \\) and $ P ^ 2 \ the nmid n- \ (, then \) [Phi] (n-) = [Phi] (n-/ P) * (P -1) $
We can use in screening these two composite number theorem, from (φ (n / p) \ ) \ recursive to \ ([phi] (the n-) \) .
About prime number sieve, linear sieve because there are two kinds of writing, in fact, is much the same, but for the convenience of the reader, here are given by:
//法一 :
int v[SIZE], pri[SIZE], phi[SIZE], num;
void promoted_eluer(int n) {
for(int i = 2; i <= n; i++) {
if(v[i] == 0)
pri[++num] = i, phi[i] = i - 1;
for(int j = 1; j <= num && i * pri[j] <=n; j++) {
v[i * pri[j]] = 1;
phi[i * pri[j]]=
phi[i] * (i % pri[j] ? pri[j] - 1 : pri[j]);
if(i % pri[j] == 0) break;
}
}
}
//法二:
int v[SIZE], pri[SIZE], phi[SIZE], num;
void promoted_eluer(int n) {
for(int i = 2; i <= n; i++) {
if(v[i] == 0)
pri[++num] = i, phi[i] = i - 1, v[i] = i;
for(int j = 1; j <= num; j++) {
if(pri[j] > v[i] || pri[j] > n / i) break;
v[i * pri[j]] = pri[j];
phi[i * pri[j]]=
phi[i] * (i % pri[j] ? pri[j] - 1 : pri[j]);
}
}
}
※ end of chapter exercises
P1029 greatest common divisor and least common multiple problems
UVA10791 least common multiple of the minimum and Minimum Sum LCM
\(END\)
\(PS:\)
More than explain the sequence and contents Reference: Li Yudong "algorithm contest Step-up Guide"