[Notes] Notes on linear sieve

This article talked about

  1. Linear sieve prime number
  2. Linear sieve $ \ varphi (n) $
  3. Linear sieve $ \ mu (n) $
  4. Linear sieve $ d (n) $ (cycle number)
  5. Linear sieve $ \ sigma (n) $ (divisors)

 

A linear prime sieve

  • Throw a bar code, the specific detailed evaluation screen Euler Euler linear function
    inline void PRI(int n) {
        for(R i=2;i<=n;++i) {
            if(!v[i]) pri[++cnt]=i;
            for(R j=1;j<=cnt&&i*pri[j]<=n;++j) {
                v[i*pri[j]]=true; if(i%pri[j]==0) break;
            }
        }
    }

Second, the linear sieve $ \ varphi (n) $

  • See also evaluate and screen Euler Euler linear function
    inline void PHI(int n) { p[1]=1;
        for(R i=2;i<=n;++i) {
            if(!v[i]) pri[++cnt]=i,p[i]=i-1;
            for(R j=1;j<=cnt&&i*pri[j]<=n;++j) {
                v[i*pri[j]]=true; 
                if(i%pri[j]==0) {p[i*pri[j]]=p[i]*pri[j]; break;}
                p[i*pri[j]]=p[i]*p[pri[j]];
            }
        }
    }

Third, linear sieve $ \ mu (n) $

  • $ \ Mu (n) = \ begin {cases} 0 & square with a number of prime factors 1 \\ & prime factor is an even number of prime factors \\ -1 & odd \ end {cases} $
  • According to the definition directly to the screen
    inline void MU(int n) { mu[1]=1;
        for(R i=2;i<=n;++i) {
            if(!v[i]) pri[++cnt]=i,mu[i]=-1;
            for(R j=1;j<=cnt&&i*pri[j]<=n;++j) {
                v[i*pri[j]]=true;
                if(i%pri[j]==0) break;
                mu[i*pri[j]]=-mu[i];
            }
        } 
    }

     

Fourth, the linear sieve $ d (n) $ (cycle number)

  • First equation: $ n = \ prod_ {i = 1} ^ k \ space p_i ^ {c_i} $, then $ d (n) = \ prod_ {i = 1} ^ k \ space (c_i + 1) $
  • Note $ d [n] $ $ n-factor is the number of $, $ c [n] $ is the number of occurrences of n-$ $ smallest prime factor;
    inline void D(int n) { d[1]=1;
        for(R i=2;i<=n;++i) {
            if(!v[i]) pri[++cnt]=i,d[i]=2,c[i]=1;
            for(R j=1;j<=cnt&&i*pri[j]<=n;++j) {
                v[i*pri[j]]=true;
                if(i%pri[j]==0) {
                    c[i*pri[j]]=c[i]+1;
                    d[i*pri[j]]=d[i]/(c[i]+1)*(c[i]+2);
                    break;
                } d[i*pri[j]]=d[i]*2;
                c[i*pri[j]]=1;
            }
        }
    }
  • If $ I $ is a prime number, it is clear that $ d [i] = 2, c [i] = 1 $
  • If $ i $ not a prime number, we will have to Category talk
  1. If $ i% pri [j] == 0 $, description $ pri [j] $ $ I $ is the smallest prime factor of (as small to large primes enumeration), so $ c [i * pri [j] ] = c [i] + 1 $, and $ d [i] = (c [i] +1) * (k_1 + 1) * (k_2 + 1) * ... $, so in this case $ c [i * pri [j]] $ than $ c [i] $ $ $ 1 is increased, so $ d [i * pri [j]] = d [i] / (c [i] +1) * (c [i ] +2) $
  2. If $ i% pri [j]! = 0 $, description $ pri [j] $ a $ i * pri [j] $ smallest prime factor, so $ c [i * pri [j]] = 1 $, for $ I $, the equivalent of a previously added prime factor $ pri [j] $, so $ d [i * pri [j]] = d [i] * 2 $.

V. Linear sieve $ \ sigma (n) $ (divisors)

  • 首先公式:$n=\prod_{i=1}^k\space p_i^{c_i}$,那么$\sigma(n)=\prod_{i=1}^k 1+p_i+p_i^2+...+p_i^{c_i}=\prod_{i=1}^k\sum_{j=0}^{c_i} p_i^j$
  • Note $ s [n] $ is $ n-factor and $, $ c [n] $ is a $ 1 + p_ {min} + p_ {min} ^ 2 + ... + p_ {min} ^ c $, $ wherein p_ {min} $ $ represents the smallest prime factor of n-$, $ c $ is the smallest prime factor of the number of occurrences of n-$ $ in FIG.
    inline void SIGMA() { s[1]=1;
        for(R i=2;i<=n;++i) {
            if(!v[i]) pri[++cnt]=i,s[i]=i+1,c[i]=i+1;
            for(R j=1;j<=cnt&&i*pri[j]<=n;++j) {
                v[i*pri[j]]=true;
                if(i%pri[j]==0) {
                    c[i*pri[j]]=c[i]*pri[j]+1;
                    s[i*pri[j]]=s[i]/c[i]*c[i*pri[j]];
                    break;
                C} [i * pri [j]] = pri [j] + 1 ; 
                s [i * pri [j]] = s [i] * (pri [j] + 1 ); 
            } 
        } 
    }
  • If $ I $ is a prime number, it is clear that $ s [i] = i + 1, c [i] = i + 1 $
  • If $ i $ is not prime, you have to Category talk
  1. If $ i% pri [j] == 0 $, description $ pri [j] $ $ I $ is the smallest prime factor of (as small to large primes enumeration), so the original $ \\ 1 + p_ { min} + p_ {min} ^ 2 + ... + p_ {min} ^ c \\ $, to become $ \\ 1 + p_ {min} + p_ {min} ^ 2 + ... + p_ { min} ^ {c + 1} \\ $, $ p $ so on the basis of the original superior, plus a $ 1 $ like, so $ c [i * pri [j]] = c [i] * pri [j] + 1 $, and $ d [i] = (c [i] +1) * (c_1 + 1) * (c_2 + 1) * ... $, so in this case, $ s [i * pri [J]] $ correspondingly transformed like.
  2. If $ i% pri [j]! = 0 $, description $ pri [j] $ a $ i * pri [j] $ smallest prime factor, so $ c [i * pri [j]] = pri [j] + $ 1, $ I $ to, the equivalent of $ PRI add a prime factor of a previously [J] $, so $ s [i * pri [j]] = s [i] * (pri [j] +1) $.

If you think my four and five do not speak clearly, Amway out of big brother's blog


 

2019.06.08

Guess you like

Origin www.cnblogs.com/Jackpei/p/10991603.html