About the number of relevant

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 \)
  1. \(a=0, \quad return \quad b; \quad \mid \quad b=0, \quad return \quad a;\)

  2. \ (A <B, A \ Quad XOR = \ Quad B, \ Quad B \ Quad XOR = \ A Quad, \ Quad A \ Quad XOR = \ Quad B; \) (unique binary exchange)

  3. \(a\) & \(1\)\(b\) & \(1\),$ \quad ans=2gcd(a >> 1,b>>1);$

  4. \(a\) & \(1\) 且 !\(b\) & \(1\),$ \quad ans=gcd(a >> 1,b);$

  5. \(a\) & \(1\)\(b\) & \(1\),$ \quad ans=gcd(a,b >> 1);$

  6. \(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:
  1. \ (∀n> 1,1-n \ ) with \ (n-\) prime number and is \ (n-* [Phi] (n-) / 2 \) .

  2. If \ (GCD (A, B) =. 1 \) , i.e. \ (a, b \) prime, then \ ([Phi] (ab &) = [Phi] (A) [Phi] (B) \) .

  3. Set \ (P \) is a prime number, if \ (p \ mid n \) and \ (P ^ 2 \ MID n-\) , then \ ([Phi] (n-) = [Phi] (n-/ P) * P \) .

  4. Set \ (P \) is a prime number, if \ (p \ mid n \) and \ (P ^ 2 \ the nmid n-\) , then \ (φ (n) = φ (n / p) * (p-1) \) .

  5. \(\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

  1. 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

  1. \ ([Phi] (n-) \) - Euler function

  2. \ ([sigma] (n-) \) - divisors function

  3. \ ([mu] (n-) \) - Mobius function

  4. \ (σ_0 (n-) \) - about a few number of functions

  5. \ (σ_k (n-) \) - about the number and function number

  6. \ (GCD (n-, K) \) - the greatest common divisor function, when \ (K \) the fixed time

  7. \ (1 (the n-) = 1 \) - I do not know what this is

  8. \ (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:

  1. Codomain: arbitrarily set value range comprising

  2. 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:

  1. \ (F * F * G = G \) (commutative)

  2. \ ((F * G) = F * H * (F * G) \) (associative law)

  3. \ (F * (G + H) + = F * F * G H \) (distributive property)

  4. 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:

  1. Theorem three: Let \ (P \) is a prime number, if \ (p \ mid n \\) and $ P ^ 2 \ MID n- \ (, then \) [Phi] (n-) = [Phi] (n-/ P) * P $ .

  2. 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

  1. P1029 greatest common divisor and least common multiple problems

  2. P2205 [USACO13JAN] painting the fence Painting the Fence

  3. UVA12716 GCD equal XOR GCD XOR

  4. P2303 [SDOi2012] Longge problem

  5. UVA10791 least common multiple of the minimum and Minimum Sum LCM

  6. P1072 Hankson interesting questions

  7. P2261 [CQOI2007] remainder sum

  8. P2520 [HAOI2011] Vector

  9. P2152 [SDOI2009]SuperGCD

  10. P1463 [POI2002] [HAOI2007] anti-prime

  11. P2455 [SDOI2006] Linear Equations

  12. P3213 [HNOI2011] Pythagorean theorem

  13. P3327 [SDOI2015] about the number and the number of

  14. P3166 [CQOI2014] triangular numbers

  15. P2500 [SDOI2012] set

  16. P2086 [NOI2012] magical chessboard

  17. P3307 [SDOI2013] necklace


\(END\)

\(PS:\)

More than explain the sequence and contents Reference: Li Yudong "algorithm contest Step-up Guide"

Guess you like

Origin www.cnblogs.com/Ning-H/p/11567272.html