Multiplicative sieve

Some definitions

  • Arithmetical functions: domain as a function of positive integers.
  • Multiplicative function: satisfies \ (f (a) f ( b) = f (ab) \ (gcd (a, b) = 1) \) arithmetic function.
  • Integrable sieve: obtaining multiplicative function and prefix algorithm wonderful time less than linear.

Some multiplicative function

  • \ (\ varphi (the X-) \) : Euler function.
  • \ (\ MU (the X-) \) : Mobius function.
  • \ (\ Epsilon (the X-) \) : metafunction. \ (\ Epsilon (X) = \ delta_time_unit_address X1} {\) .
  • \ (D (X) \) : approximately logarithmic function. \ (D (X) = \ sum_ {D | X}. 1 \) .
  • \ (\ Sigma (the X-) \) : about the number and function. \ (\ Sigma (X) = \ sum_ {D | D} X \) .
  • \ (The I (the X-) \) : identity function. \ (The I (X) =. 1 \) .
  • \ (the above mentioned id (the X-) \) : Unit functions. \ (the above mentioned id (the X-) = the X-\) .

Dirichlet convolution

  • Two arithmetic functions \ (f (x), g (x) \) of Dirichlet convolution is defined as: \ (F * G = \ sum_ {D |} n-F (D) G (\ Nd FRAC) \ ) .
  • Obviously, the Dirichlet convolution is commutative, associative, distributive law. And we can easily find a property: Dirichlet convolution of two multiplicative function is still multiplicative function.

Du teach sieve

  • dalao invention: Du%%% Yu Hao%

Algorithmic process

  • Set \ (S (n-) = \ sum_ = {I}. 1 ^ NF (I) \) . We require a \ (S (the n-) \) .
  • Provided with a further multiplicative function \ (G \) , and set \ (F * G = H \) , there are: \ [\ sum_ {I}. 1 ^ = NH (I) = \ sum_ = {I}. 1 ^ n \ sum_ {d | i } f (d) g (\ frac id) = \ sum_ {d = 1} ^ ng (d) \ sum_ {i = 1} ^ {\ lfloor \ frac nd \ rfloor} f (i) = \ sum_ {d = 1} ^ ng (d) S (\ lfloor \ frac nd \ rfloor) \]
  • 因此,\(g(1)S(n)=\sum_{i=1}^nh(i)-\sum_{d=2}^ng(d)S(\lfloor\frac nd\rfloor)\)
  • Because of \ (G \) is a multiplicative function, \ (G (. 1). 1 = \) , so \ (S (n) \) is equal to the right formula.

  • For \ (\ H SUM (I) \) , we need a fast seek its algorithm (typically does not exceed the time required \ (O (\ sqrt n-) \) ); For minuend, we can first block then recursive solution \ (S (\ lfloor \ FRAC Nd \ rfloor) \) . Because of \ (\ lfloor \ FRAC {\ lfloor \ FRAC ab & \ rfloor} C \ rfloor = \ lfloor \ FRAC A {BC} \ rfloor \) , we counted the \ (S (x) \) must satisfy the \ (X = \ lfloor \ FRAC Nd \ rfloor (D \ in [. 1, n-]) \) , it is possible to use two buckets (one \ (≤ \ sqrt n \) , one \ (> \ sqrt n \) the course can also be synthesized a barrel) memory of it.
  • Space complexity is clearly \ (O (\ sqrt the n-) \) .
  • Time, can be found in \ (x = \ lfloor \ frac nd \ rfloor (d \ in [1, n]) \) only \ (O (\ sqrt n) \) kinds of values, and which are counted once no longer forget (the memory of). Thus, \ (T (n-) = \ sum_. 1 = {I} ^ {\ sqrt} n-O (\ sqrt I) + O (\ sqrt {\ lfloor \ FRAC Ni \ rfloor}) \) . May find a definite integral, glance \ (T (n-) = O (n-^ {\} 34 is FRAC) \) . In other words, we probably can expel too \ (n = 10 ^ {10 } \) data, but \ (n = 10 ^ {11 } \) when a bit mysterious.

Simple examples

T1
  • \(S(n)=\sum_{i=1}^n\mu(i)\)
  • We know that \ (\ mu \) has a nature: \ (\ * MU the I = \ Epsilon \) . Obtained can then directly set the formula: \ (S (n-) = l- \ sum_ {D} = 2 ^ nS (\ lfloor \ FRAC Nd \ rfloor) \) .
T2
  • Defined \ (F (n-) = n-\ varphi (n-) \) , seeking \ (S (n-) = \ sum_ = {I}. 1 ^ NF (I) \) .
  • Set \ (H = F * ID \) , found \ (H (n-) = \ sum_ {D |} n-n-\ varphi (D) = n-2 ^ \) . Then: \ (S (n-) = \ {n-FRAC (n-+. 1) (2N +. 1)} 6- \ sum_ {D} = 2 ^ NDS (\ lfloor \ FRAC Nd \ rfloor) \) .
T3: 51nod2026
  • In fact, the subject is required \ ((S (n-) = \ sum_ = {I}. 1 ^ NF (I)) ^ 2 \) .
  • Slightly push equation, \ (S (n-) = \ sum_. 1} ^ {n-I = \ sum_ {D | D} I \ MU (D) = \ sum_. 1} ^ {D = Nd \ MU (D) \ lfloor \ FRAC Nd \ rfloor \) . Behind \ (\ lfloor \ frac nd \ rfloor \) can be divided into blocks to handle, and now we are dealing with only several \ (s (x) = \ sum_ {d = 1} ^ xd \ mu (d) \) only (and these \ (X \) are also \ (n-\) divisible by a number of come).
  • It remains on the volume \ (ID \) , \ (H (n-) = \ sum_ {D |} n-n-\ MU (D) = \ Epsilon (n-) \) . Then: \ (S (n-) = l- \ sum_ {D} = 2 ^ NDS (\ lfloor \ FRAC Nd \ rfloor) \) .
#include <cmath>
#include <cstdio>
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;

const int M=1e5,N=2e5,mo=1e9+7;
int n,k,s[N],S;
bool v[N];

int C(int x) {return x<=k?M+x:n/x;}
int P(int&x,int y) {x=(1ll*x+y)%mo;}
void gets(int x)
{
    v[C(x)]=s[C(x)]=1;
    for(int i,l=2,r; l<=x; l=r+1)
    {
        r=x/(i=x/l);
        if(!v[C(i)]) gets(i);
        P(s[C(x)],mo-1ll*(l+r)*(r-l+1)/2%mo*s[C(i)]%mo);
    }
}

int main()
{
    scanf("%d",&n), k=sqrt(n);
    gets(n);
    for(int i,l=1,r; l<=n; l=r+1)
    {
        r=n/(i=n/l);
        P(S,1ll*i*(1ll*mo+s[C(r)]-s[C(l-1)])%mo);
    }
    printf("%lld",1ll*S*S%mo);
}

min25 screen

  • dalao invention:% m% i% n% 2% 5%

Guess you like

Origin www.cnblogs.com/Iking123/p/11593443.html