Dirichlet Convolution

This article introduces Dirichlet convolution and Dirichlet generating functions.

Dirichlet convolution

For arithmetic functions f(n)f(n) and g(n)g(n), their Dirichlet convolution (Dirichlet convolution), denoted by f∗gf \ast g, is the arithmetic function

(f∗g)(n)=∑k∣nf(k)g(nk)=∑kℓ=nf(k)g(ℓ).(f \ast g)(n) = \sum_{k\mid n}f(k)g\left(\dfrac{n}{k}\right) = \sum_{k\ell=n}f(k)g(\ell).

Dirichlet convolution is an important operation on arithmetic functions. Many properties of arithmetic functions can be uncovered through this operation.

Examples
  1. The identity function ε\varepsilon is the Dirichlet convolution of the Möbius function μ\mu and the constant function 11:

    ε=μ∗1  ⟺  ε(n)=∑d∣nμ(d).\varepsilon=\mu \ast 1 \iff\varepsilon(n)=\sum_{d\mid n}\mu(d).
  2. The divisor-counting function τ\tau is the Dirichlet convolution of the constant function 11 with itself:

    τ=1∗1  ⟺  τ(n)=∑d∣n1.\tau=1 \ast 1 \iff \tau(n)=\sum_{d\mid n}1.
  3. The divisor-sum function σ\sigma is the Dirichlet convolution of the identity function id\mathrm{id} and the constant function 11:

    σ=id∗1  ⟺  σ(n)=∑d∣nd.\sigma=\mathrm{id} \ast 1 \iff\sigma(n)=\sum_{d\mid n}d.
  4. Euler's totient function φ\varphi is the Dirichlet convolution of the identity function id\mathrm{id} and the Möbius function μ\mu:

    φ=id∗μ  ⟺  φ(n)=∑d∣nd⋅μ(nd).\varphi=\mathrm{id}\ast \mu \iff\varphi(n)=\sum_{d\mid n}d\cdot\mu\left(\frac{n}{d}\right).

Möbius inversion uses ε=μ∗1\varepsilon=\mu \ast 1 to transform identities of arithmetic functions.

Properties

Dirichlet convolution has a series of algebraic properties.

Theorem

Let f,g,hf,g,h be arithmetic functions. Then:

  1. Commutativity: f∗g=g∗ff\ast g=g\ast f.

  2. Associativity: (f∗g)∗h=f∗(g∗h)(f\ast g)\ast h=f\ast(g\ast h).

  3. Distributivity: (f+g)∗h=f∗h+g∗h(f+g)\ast h = f\ast h + g\ast h.

  4. Identity element: f∗ε=ε∗f=ff\ast\varepsilon = \varepsilon \ast f = f, where ε(n)=[n=1]\varepsilon(n) = [n=1] is the identity element for convolution, and [⋅][\cdot] is the Iverson bracket.

  5. Inverse: If and only if f(1)≠0f(1)\neq 0, there exists gg such that f∗g=g∗f=εf\ast g=g\ast f=\varepsilon. The function gg is called the Dirichlet inverse of ff and can be denoted by f−1f^{-1}. Moreover, the inverse gg satisfies the recurrence

    g(n)=ε(n)−∑kℓ=n, k≠1f(k)g(ℓ)f(1).g(n) = \dfrac{\varepsilon(n) - \sum_{k\ell = n,~k\neq 1}f(k)g(\ell)}{f(1)}.
Proof

To verify commutativity, direct computation gives

(f∗g)(n)=∑kℓ=nf(k)g(ℓ)=(g∗f)(n).(f\ast g)(n) = \sum_{k\ell=n}f(k)g(\ell) = (g\ast f)(n).

To verify associativity, direct computation gives

((f∗g)∗h)(n)=∑kℓm=nf(k)g(ℓ)h(m)=(f∗(g∗h))(n).((f\ast g)\ast h)(n) = \sum_{k\ell m = n}f(k)g(\ell)h(m) = (f\ast (g\ast h))(n).

To verify distributivity, direct computation gives

((f+g)∗h)(n)=∑kℓ=n(f(k)+g(k))h(ℓ)=∑kℓ=nf(k)h(ℓ)+∑kℓ=ng(k)h(ℓ)=(f∗h+g∗h)(n).\begin{aligned} ((f+g)\ast h)(n) &= \sum_{k\ell = n}(f(k) + g(k))h(\ell) \\ &= \sum_{k\ell=n}f(k)h(\ell) + \sum_{k\ell=n}g(k)h(\ell) = (f\ast h+g\ast h)(n). \end{aligned}

To verify that ε(n)\varepsilon(n) is the identity element, direct computation gives

(f∗ε)(n)=∑kℓ=nf(k)ε(ℓ)=f(n).(f\ast\varepsilon)(n) = \sum_{k\ell = n}f(k)\varepsilon(\ell) = f(n).

The second equality holds because ε(ℓ)\varepsilon(\ell) is nonzero only when ℓ=1\ell=1, that is, when k=nk=n.

Finally, we need to prove that f−1f^{-1} exists if and only if f(1)≠0f(1)\neq 0. For any ff, suppose that there is a gg such that f∗g=εf\ast g=\varepsilon. This means that

(f∗g)(n)=∑kℓ=nf(k)g(ℓ)=ε(n).(f\ast g)(n) = \sum_{k\ell = n}f(k)g(\ell) = \varepsilon(n).

This gives a system of equations for the values g(n)g(n), from which g(n)g(n) can be determined directly. In particular, when n=1n=1, the equation becomes f(1)g(1)=1f(1)g(1)=1, so the existence of gg requires at least f(1)≠0f(1)\neq 0. Conversely, whenever f(1)≠0f(1)\neq 0, we can solve directly for

g(n)=ε(n)−∑kℓ=n, k≠1f(k)g(ℓ)f(1).g(n) = \dfrac{\varepsilon(n) - \sum_{k\ell = n,~k\neq 1}f(k)g(\ell)}{f(1)}.

This can be used to compute the values of g(n)g(n) recursively. Therefore, the inverse gg exists if and only if f(1)≠0f(1)\neq 0.

In the language of abstract algebra, these algebraic properties mean that all arithmetic functions form a commutative ring under pointwise addition and Dirichlet convolution, and its units are precisely the functions that are nonzero at n=1n=1. This ring is called the Dirichlet ring (Dirichlet ring).

Multiplicative functions are a special class of arithmetic functions. They are closed under Dirichlet convolution and Dirichlet inversion.

Theorem

Let f,gf,g be multiplicative functions. Then f∗gf\ast g is also multiplicative. Moreover, the inverse f−1f^{-1} always exists and is also multiplicative.

Proof

For the first claim, let h=f∗gh=f\ast g. Direct verification shows that, for n1⊥n2n_1\perp n_2,

h(n1)h(n2)=(∑k1ℓ1=n1f(k1)g(ℓ1))(∑k2ℓ2=n2f(k2)g(ℓ2))=∑k1ℓ1=n1, k2ℓ2=n2f(k1)f(k2)g(ℓ1)g(ℓ2)=∑kℓ=n1n2f(k)g(ℓ)=h(n1n2).\begin{aligned} h(n_1)h(n_2) &= \left(\sum_{k_1\ell_1=n_1}f(k_1)g(\ell_1)\right)\left(\sum_{k_2\ell_2=n_2}f(k_2)g(\ell_2)\right)\\ &= \sum_{k_1\ell_1=n_1,~k_2\ell_2=n_2}f(k_1)f(k_2)g(\ell_1)g(\ell_2)\\ &= \sum_{k\ell = n_1n_2}f(k)g(\ell) \\ &= h(n_1n_2). \end{aligned}

The logic behind changing the order of summation in the third equality is as follows: when kk ranges over the divisors of n1n2n_1n_2, kk's prime factors can be divided into two groups according to whether they are prime factors of n1n_1 or n2n_2. Multiplying the prime factors, with multiplicity, in the two groups separately gives k1k_1 and k2k_2, which range over the divisors of n1n_1 and n2n_2, respectively. Conversely, given divisors k1k_1 and k2k_2 of n1n_1 and n2n_2, we can always obtain the divisor k=k1k2k=k_1k_2 of n1n2n_1n_2. For the second claim, let g=f−1g=f^{-1} and apply mathematical induction. First, g(1)=1/f(1)=1g(1)=1/f(1)=1. The recurrence for the inverse can then be written as

g(n)=ε(n)−∑kℓ=n, k≠1f(k)g(ℓ).g(n) = \varepsilon(n) - \sum_{k\ell = n,~k\neq 1} f(k)g(\ell).

Thus, for n1⊥n2n_1\perp n_2 with n1n2>1n_1n_2 > 1, we have

g(n1n2)=−∑kℓ=n1n2, k≠1f(k)g(ℓ)=−∑k1ℓ1=n1, k2ℓ2=n2, k1k2≠1f(k1)f(k2)g(ℓ1)g(ℓ2)=f(1)f(1)g(n1)g(n2)−∑k1ℓ1=n1, k2ℓ2=n2f(k1)f(k2)g(ℓ1)g(ℓ2)=g(n1)g(n2)−(∑k1ℓ1=n1f(k1)g(ℓ1))(∑k2ℓ2=n2f(k2)g(ℓ2))=g(n1)g(n2)−ε(n1)ε(n2)=g(n1)g(n2).\begin{aligned} g(n_1n_2) &= -\sum_{k\ell=n_1n_2,~k\neq 1}f(k)g(\ell) \\ &= -\sum_{k_1\ell_1=n_1,~k_2\ell_2=n_2,~k_1k_2\neq 1}f(k_1)f(k_2)g(\ell_1)g(\ell_2)\\ &= f(1)f(1)g(n_1)g(n_2) - \sum_{k_1\ell_1=n_1,~k_2\ell_2=n_2}f(k_1)f(k_2)g(\ell_1)g(\ell_2)\\ &= g(n_1)g(n_2) - \left(\sum_{k_1\ell_1=n_1}f(k_1)g(\ell_1)\right)\left(\sum_{k_2\ell_2=n_2}f(k_2)g(\ell_2)\right) \\ &= g(n_1)g(n_2) - \varepsilon(n_1)\varepsilon(n_2)\\ &= g(n_1)g(n_2). \end{aligned}

The second equality uses the induction hypothesis: for ℓ1ℓ2<n1n2\ell_1\ell_2 < n_1n_2 with ℓ1⊥ℓ2\ell_1\perp\ell_2, the condition g(ℓ1ℓ2)=g(ℓ1)g(ℓ2)g(\ell_1\ell_2)=g(\ell_1)g(\ell_2) holds.

In the language of abstract algebra, all multiplicative functions form a subgroup of the multiplicative group of the Dirichlet ring.

An even more special class is that of completely multiplicative functions.

Theorem

Let α\alpha be a completely multiplicative function, and let f,gf,g be arithmetic functions. Then:

  1. Distributivity: (αf)∗(αg)=α⋅(f∗g)(\alpha f)\ast(\alpha g) = \alpha\cdot(f\ast g).
  2. Inverse: (αf)−1=αf−1(\alpha f)^{-1}=\alpha f^{-1}, provided that f−1f^{-1} exists.
  3. A multiplicative function ff is completely multiplicative if and only if f−1=μff^{-1}=\mu f, where μ\mu is the Möbius function.
Proof

For the first statement, direct verification gives

((αf)∗(αg))(n)=∑kℓ=n(αf)(k)(αg)(ℓ)=∑kℓ=nα(k)f(k)α(ℓ)g(ℓ)=∑kℓ=nα(n)f(k)g(ℓ)=α(n)(f∗g)(n).\begin{aligned} ((\alpha f)\ast(\alpha g))(n) &= \sum_{k\ell = n}(\alpha f)(k)(\alpha g)(\ell) \\ &= \sum_{k\ell = n}\alpha(k)f(k)\alpha(\ell)g(\ell) \\ &= \sum_{k\ell = n}\alpha(n)f(k)g(\ell) \\ &= \alpha(n)(f\ast g)(n). \end{aligned}

The third equality uses the property of completely multiplicative functions that α(n)=α(k)α(ℓ)\alpha(n)=\alpha(k)\alpha(\ell) for every n=kℓn=k\ell.

For the second statement, the first statement gives

(αf)∗(αf−1)=α(f∗f−1)=αε=ε.(\alpha f)\ast(\alpha f^{-1}) = \alpha(f\ast f^{-1}) = \alpha\varepsilon = \varepsilon.

Here, the last equality uses only α(1)=1\alpha(1)=1. By the definition of the inverse, (αf)−1=αf−1(\alpha f)^{-1}=\alpha f^{-1}.

For the third statement, using the second statement and 1−1=μ1^{-1}=\mu, if ff is completely multiplicative, then

f−1=(1f)−1=1−1⋅f=μf.f^{-1} = (1f)^{-1} = 1^{-1}\cdot f = \mu f.

Here, 11 is the constant function. Conversely, if ff is multiplicative and f−1=μff^{-1}=\mu f, it is enough to prove that f(pe)=f(p)ef(p^e)=f(p)^e for every prime pp and e∈N+e\in\mathbf N_+ in order to prove that ff is completely multiplicative. To this end, apply mathematical induction on e∈N+e\in\mathbf N_+. The statement is obvious at the base case e=1e=1. For any e>1e > 1, the inverse recurrence gives

f−1(pe)=−∑i=1ef(pi)f−1(pe−i)=−∑i=1ef(pi)μ(pe−i)f(pe−i)=−f(pe)f(1)μ(1)−f(pe−1)μ(p)f(p)=−f(pe)+f(p)e.\begin{aligned} f^{-1}(p^e) &= -\sum_{i=1}^{e}f(p^i)f^{-1}(p^{e-i}) \\ &= -\sum_{i=1}^{e}f(p^i)\mu(p^{e-i})f(p^{e-i})\\ &= -f(p^e)f(1)\mu(1) - f(p^{e-1})\mu(p)f(p)\\ &= -f(p^e) + f(p)^e. \end{aligned}

The last equality uses the induction hypothesis f(pe−1)=f(p)e−1f(p^{e-1})=f(p)^{e-1}. Applying f−1=μff^{-1}=\mu f gives

f−1(pe)=μ(pe)f(pe)=0.f^{-1}(p^e) = \mu(p^e)f(p^e) = 0.

Substituting into the previous equation gives

f(pe)=f(p)e.f(p^e) = f(p)^e.

Thus, the induction step holds, and the original statement follows.

In the language of abstract algebra, if α\alpha is completely multiplicative, the map f↦αff\mapsto \alpha f is an endomorphism of the Dirichlet ring.

Dirichlet generating functions

Closely related to Dirichlet convolution are Dirichlet generating functions.

The Dirichlet generating function (Dirichlet series generating function, DGF) corresponding to an arithmetic function f(n)f(n)—that is, the sequence {f(n)}\{f(n)\}—is defined as the formal Dirichlet series (formal Dirichlet series):

F(s)=∑n=1∞f(n)ns.F(s) = \sum_{n=1}^{\infty}\dfrac{f(n)}{n^s}.

The ss in the series is a formal variable. In common Dirichlet generating functions, ss can often be regarded as a complex variable, allowing one to study the analytic properties of Dirichlet series, but this lies beyond the scope of algorithmic competitions.

The product of Dirichlet generating functions corresponds to the Dirichlet convolution of the associated arithmetic functions:

Theorem

For arithmetic functions f,gf,g and their Dirichlet generating functions F,GF,G, the generating function of their Dirichlet convolution f∗gf\ast g is F⋅GF\cdot G.

Proof

Direct verification:

F(s)G(s)=(∑k=1∞f(k)ks)(∑ℓ=1∞g(ℓ)ℓs)=∑k=1∞∑ℓ=1∞f(k)g(ℓ)(kℓ)s=∑n=1∞∑kℓ=nf(k)g(ℓ)ns=∑n=1∞(f∗g)(n)ns.\begin{aligned} F(s)G(s) &= \left(\sum_{k=1}^\infty\dfrac{f(k)}{k^s}\right)\left(\sum_{\ell=1}^\infty\dfrac{g(\ell)}{\ell^s}\right)= \sum_{k=1}^\infty\sum_{\ell=1}^\infty\dfrac{f(k)g(\ell)}{(k\ell)^s}\\ &= \sum_{n=1}^{\infty}\dfrac{\sum_{k\ell = n}f(k)g(\ell)}{n^s} = \sum_{n=1}^\infty\dfrac{(f\ast g)(n)}{n^s}. \end{aligned}

Using the correspondence between Dirichlet convolution and products of Dirichlet generating functions, we can understand the properties of Dirichlet convolution from the perspective of Dirichlet generating functions. Since multiplication of formal Dirichlet series satisfies commutativity, associativity, and distributivity over addition, Dirichlet convolution of arithmetic functions satisfies the same algebraic properties.

Euler products

The special nature of multiplicative functions is also reflected in Dirichlet generating functions. Since integers have the unique factorization theorem, the generating function F(s)F(s) of a multiplicative function f(n)f(n) can be written as follows:

F(s)=∑n=1∞f(n)ns=∑n=1∞∏p∈Pf(pe)pes=∏p∈P∑e=0∞f(pe)pes=∏p∈P(1+f(p)ps+f(p2)p2s+f(p3)p3s+⋯ ).\begin{aligned} F(s) &= \sum_{n=1}^{\infty}\dfrac{f(n)}{n^s} = \sum_{n=1}^{\infty}\prod_{p\in\mathbf P}\dfrac{f(p^{e})}{p^{es}} = \prod_{p\in\mathbf P}\sum_{e=0}^{\infty}\dfrac{f(p^e)}{p^{es}}\\ &= \prod_{p\in\mathbf P}\left(1 + \dfrac{f(p)}{p^s} + \dfrac{f(p^2)}{p^{2s}} + \dfrac{f(p^3)}{p^{3s}} + \cdots\right). \end{aligned}

This means that F(s)F(s) can be decomposed into a product of several Fp(s)F_p(s), where the arithmetic function corresponding to each Fp(s)F_p(s) can be nonzero only at powers of pp. This infinite product is also called an Euler product (Euler product). If both F(s)F(s) and G(s)G(s) can be decomposed in this form, then so can their product; translated back to arithmetic functions, this observation says that the Dirichlet convolution of multiplicative functions is still multiplicative.

Furthermore, if f(n)f(n) is also completely multiplicative, then f(pe)=f(p)ef(p^e)=f(p)^e, and the above expression can be simplified further:

F(s)=∏p∈P∑e=0∞f(p)epes=∏p∈P(1−f(p)ps)−1.F(s) = \prod_{p\in\mathbf P}\sum_{e=0}^{\infty}\dfrac{f(p)^e}{p^{es}} = \prod_{p\in\mathbf P}\left(1-\dfrac{f(p)}{p^s}\right)^{-1}.

Unlike multiplicative functions, the forms of Dirichlet generating functions of completely multiplicative functions are not closed under multiplication. Therefore, the Dirichlet convolution and Dirichlet inverse of completely multiplicative functions need not be completely multiplicative, although they are always multiplicative.

Examples
  1. The identity function ε(n)\varepsilon(n) is completely multiplicative. Its Dirichlet generating function is the constant function of the formal variable ss

    E(s)=∑n=1∞ε(n)ns=1.E(s) = \sum_{n=1}^{\infty}\dfrac{\varepsilon(n)}{n^s} = 1.
  2. The constant function 1(n)1(n) is completely multiplicative. Its Dirichlet generating function is the Riemann zeta function

    I(s)=∑n=1∞1ns=∏p∈P11−p−s=ζ(s).I(s) = \sum_{n=1}^{\infty}\dfrac{1}{n^s} = \prod_{p\in\mathbf P}\dfrac{1}{1-p^{-s}} = \zeta(s).
  3. The Möbius function μ(n)\mu(n) is the Dirichlet inverse of the constant function. Its Dirichlet generating function is the reciprocal of ζ(s)\zeta(s):

    M(s)=∑n=1∞μ(n)ns=∏p∈P(1−p−s)=1ζ(s).M(s) = \sum_{n=1}^{\infty}\dfrac{\mu(n)}{n^s} = \prod_{p\in\mathbf P}(1-p^{-s}) = \dfrac{1}{\zeta(s)}.
  4. The power function id⁡k(n)=nk\operatorname{id}_k(n)=n^k is completely multiplicative. In particular, when k=0k=0, it is the constant function; when k=1k=1, it is the identity function. Its Dirichlet generating function is

    Ik(s)=∑n=1∞nkns=∏p∈P11−pk−s=ζ(s−k).I_k(s) = \sum_{n=1}^{\infty}\dfrac{n^k}{n^s} = \prod_{p\in\mathbf P}\dfrac{1}{1-p^{k-s}} = \zeta(s-k).
  5. Euler's totient function φ(n)\varphi(n) is multiplicative. Its Dirichlet generating function is

    Φ(s)=∏p∈P(1+p−1ps+p(p−1)p2s+p2(p−1)p3s+⋯ )=∏p∈P(11−p1−s−1ps11−p1−s)=∏p∈P1−p−s1−p1−s=ζ(s−1)ζ(s).\begin{aligned} \Phi(s) &= \prod_{p\in\mathbf P}\left(1+\dfrac{p-1}{p^s} + \dfrac{p(p-1)}{p^{2s}} + \dfrac{p^2(p-1)}{p^{3s}}+\cdots\right)\\ &= \prod_{p\in\mathbf P}\left(\dfrac{1}{1-p^{1-s}}-\dfrac{1}{p^s}\dfrac{1}{1-p^{1-s}}\right) = \prod_{p\in\mathbf P}\dfrac{1-p^{-s}}{1-p^{1-s}} = \dfrac{\zeta(s-1)}{\zeta(s)}. \end{aligned}

    Combining this with the Dirichlet expression for the power function gives id=φ∗1\mathrm{id} = \varphi\ast 1.

  6. The divisor function σk(n)=∑d∣ndk\sigma_k(n)=\sum_{d\mid n}d^k is multiplicative. Its Dirichlet generating function is

    Σk(s)=∏p∈P(1+1+pkps+1+pk+p2kp2s+1+pk+p2k+p3kp3s+⋯ )=∏p∈P11−pk((1−pk)+1−p2kps+1−p3kp2s+1−p4kp3k+⋯ )=∏p∈P11−pk(11−p−s−pk1−pk−s)=∏p∈P1(1−p−s)(1−pk−s)=ζ(s−k)ζ(s).\begin{aligned} \Sigma_k(s) &= \prod_{p\in\mathbf P}\left(1+\dfrac{1+p^k}{p^s}+\dfrac{1+p^k+p^{2k}}{p^{2s}}+\dfrac{1+p^k+p^{2k}+p^{3k}}{p^{3s}}+\cdots\right) \\ &= \prod_{p\in\mathbf P}\dfrac{1}{1-p^k}\left((1-p^k)+\dfrac{1-p^{2k}}{p^s}+\dfrac{1-p^{3k}}{p^{2s}}+\dfrac{1-p^{4k}}{p^{3k}}+\cdots\right)\\ &= \prod_{p\in\mathbf P}\dfrac{1}{1-p^k}\left(\dfrac{1}{1-p^{-s}} - \dfrac{p^k}{1-p^{k-s}}\right)\\ &= \prod_{p\in\mathbf P}\dfrac{1}{(1-p^{-s})(1-p^{k-s})} = \zeta(s-k)\zeta(s). \end{aligned}

    Combining this with the Dirichlet expression for the power function gives σk=idk∗1\sigma_k = \mathrm{id}_k\ast 1. This is exactly the defining formula for σk\sigma_k.

  7. The indicator function u(n)=∣μ(n)∣u(n)=|\mu(n)| of square-free numbers is multiplicative. Its Dirichlet generating function is

    U(s)=∏p∈P(1+p−s)=∏p∈P1−p−2s1−p−s=ζ(s)ζ(2s).U(s) = \prod_{p\in\mathbf P}(1+p^{-s}) = \prod_{p\in\mathbf P}\dfrac{1-p^{-2s}}{1-p^{-s}} = \dfrac{\zeta(s)}{\zeta(2s)}.

Applications

Dirichlet generating functions can be used to express multiplicative functions as Dirichlet convolutions.

For example, in the process of the Du Jiao sieve, to compute the prefix sum of a multiplicative function ff, we need to find another multiplicative function gg such that both f∗gf\ast g and gg have prefix sums that can be computed quickly. This process can be derived using Dirichlet generating functions.

Taking the example Luogu P3768 Simple Mathematical Problem from the Du Jiao sieve section, we need to construct an arithmetic function g(n)g(n) satisfying the conditions above for f(n)=n2φ(n)f(n)=n^2\varphi(n). Since ff is multiplicative, its Dirichlet generating function is

F(s)=∏p∈P(1+∑k=1∞p3k−1(p−1)pks)=∏p∈P1−p2−s1−p3−s=ζ(s−3)ζ(s−2).F(s) = \prod_{p\in\mathbf P}\left(1 + \sum_{k=1}^{\infty}\dfrac{p^{3k-1}(p-1)}{p^{ks}}\right) = \prod_{p\in\mathbf P}\dfrac{1-p^{2-s}}{1-p^{3-s}} = \dfrac{\zeta(s-3)}{\zeta(s-2)}.

Comparing this with the Dirichlet generating function of the power function, we see that taking g=id2g = \mathrm{id}_2 gives f∗g=id3f \ast g = \mathrm{id}_3. Both prefix sums can be computed quickly.

Computing Dirichlet convolution

This section discusses computing Dirichlet convolution: given the sequences {f(k)}k=1n\{f(k)\}_{k=1}^n and {g(k)}k=1n\{g(k)\}_{k=1}^n, find the first several terms {h(k)}k=1n\{h(k)\}_{k=1}^n of the Dirichlet convolution h=f∗gh=f\ast g. The complexity of the algorithm varies slightly depending on the properties of the functions involved.

General case

If f,g,hf,g,h have no special properties, Dirichlet convolution can only be computed from its definition:

h(n)=∑kℓ=nf(k)g(ℓ).h(n) = \sum_{k\ell = n}f(k)g(\ell).

Enumerate kk and ℓ\ell, and add the contribution f(k)g(ℓ)f(k)g(\ell) to h(kℓ)h(k\ell). The enumeration complexity is

O(∑k=1nnk)=O(nlog⁡n).O\left(\sum_{k=1}^{n}\dfrac{n}{k}\right) = O(n\log n).

The reference implementation is as follows:

Reference Implementation
// Compute the Dirichlet convolution h = f * g.
auto dirichlet_convolute(const std::vector<int>& f, const std::vector<int>& g) {
  int n = f.size() - 1;
  std::vector<int> h(n + 1);
  for (int k = 1; k <= n; ++k) {
    for (int d = 1; k * d <= n; ++d) {
      h[k * d] += f[k] * g[d];
    }
  }
  return h;
}

Case of convolution with a multiplicative function

If gg is multiplicative, the computation of Dirichlet convolution can be accelerated using the Euler product. Computing hh is equivalent to computing the coefficients of its Dirichlet generating function HH. Since

H(s)=F(s)G(s)=F(s)∏p∈PGp(s).H(s) = F(s)G(s) = F(s)\prod_{p\in\mathbf P}G_p(s).

Here, Gp(s)G_p(s) is a factor in the Euler product decomposition of G(s)G(s); it contains only the coefficients at powers of pp:

Gp(s)=∑pk≤nf(pk)pks=1+f(p)ps+f(p2)p2s+⋯ .G_p(s) = \sum_{p^k\le n}\dfrac{f(p^k)}{p^{ks}} = 1 + \dfrac{f(p)}{p^s} + \dfrac{f(p^2)}{p^{2s}} + \cdots.

Starting with F(s)F(s), we can traverse every prime pp not exceeding nn and multiply by Gp(s)G_p(s) one by one to obtain the final result H(s)H(s). When multiplying by Gp(s)G_p(s), we can directly apply the brute-force enumeration algorithm from the general case. The total number of enumerations is

∑p∈P, p≤n∑k=1∞⌊npk⌋≤∑p∈P, p≤nnp−1≤∑p∈P, p≤n2np∈O(nlog⁡log⁡n).\sum_{p\in\mathbf P,~p\le n}\sum_{k=1}^{\infty}\left\lfloor\dfrac{n}{p^k}\right\rfloor \le \sum_{p\in\mathbf P,~p\le n}\dfrac{n}{p-1} \le \sum_{p\in\mathbf P,~p\le n}\dfrac{2n}{p} \in O(n\log\log n).

The complexity estimate in the last step is the same as the proof of the complexity of the Sieve of Eratosthenes. Therefore, the time complexity of this algorithm is O(nlog⁡log⁡n)O(n\log\log n).

The reference implementation is as follows:

Reference Implementation
// Compute the Dirichlet convolution h = f * g.
// Assume that g is multiplicative.
auto dirichlet_convolute(const std::vector<int>& f, const std::vector<int>& g) {
  int n = f.size() - 1;
  std::vector<int> h(f);
  std::vector<bool> vis(n + 1);
  for (int i = 2; i <= n; ++i) {
    if (vis[i]) continue;
    // Reverse the order for in-place computation.
    for (int k = n / i * i; k; k -= i) {
      vis[k] = true;
      int d = k;
      while (true) {
        d /= i;
        h[k] += h[d] * g[k / d];
        if (d % i) break;
      }
    }
  }
  return h;
}

In particular, when the multiplicative function gg is completely multiplicative or is a Dirichlet inverse, such as when g=1g = 1 or g=μg = \mu, the algorithm can be simplified further. In this case, the Dirichlet convolution h=f∗gh = f\ast g can be computed using the Dirichlet prefix-sum/difference algorithm with a smaller constant, but the time complexity remains O(nlog⁡log⁡n)O(n\log\log n).

Case where the result is multiplicative

Finally, consider the case where hh is multiplicative. In particular, when both f,gf,g are multiplicative, h=f∗gh=f \ast g is multiplicative. To compute hh, it is enough to determine its values at prime powers, after which it can be computed in O(n)O(n) time using the linear sieve. For a prime power pep^e, the value h(pe)h(p^e) can be computed by brute force directly:

h(pe)=∑i=0ef(pi)g(pe−i).h(p^e) = \sum_{i=0}^e f(p^i)g(p^{e-i}).

The number of iterations required for these brute-force computations is

∑p∈P, p≤n∑e=1⌊log⁡pn⌋(e+1)≤∑p∈P, p≤n⌊log⁡pn⌋2+∑p∈P, n<p≤n1≤n(log⁡2n)2+n∈O(n).\begin{aligned} \sum_{p\in\mathbf P,~p\le n}\sum_{e=1}^{\lfloor\log_p n\rfloor}(e+1) &\le \sum_{p\in\mathbf P,~p\le\sqrt{n}}\lfloor\log_p n\rfloor^2 + \sum_{p\in\mathbf P,~\sqrt{n} < p\le n}1 \\ &\le \sqrt{n}(\log_2 n)^2 + n \in O(n). \end{aligned}

Therefore, the total time complexity of this algorithm is O(n)O(n).

The reference implementation is as follows:

Reference Implementation
// Compute the Dirichlet convolution h = f * g.
// Assume that h is multiplicative.
auto dirichlet_convolute(const std::vector<int>& f, const std::vector<int>& g) {
  int n = f.size() - 1;
  std::vector<int> h(n + 1), primes, rem(n + 1), lpf(n + 1);
  std::vector<bool> vis(n + 1);
  h[1] = 1;
  for (int x = 2; x <= n; ++x) {
    if (!vis[x]) {
      primes.push_back(x);
      rem[x] = 1;
      lpf[x] = x;
    }
    for (int p : primes) {
      if (x * p > n) break;
      vis[x * p] = true;
      rem[x * p] = x % p ? x : rem[x];
      lpf[x * p] = p;
      if (x % p == 0) break;
    }
    if (rem[x] == 1) {  // prime powers.
      for (int k = x; k; k /= lpf[x]) {
        h[x] += f[k] * g[x / k];
      }
    } else {  // other cases.
      h[x] = h[rem[x]] * h[x / rem[x]];
    }
  }
  return h;
}

References and notes

This content comes from OI Wiki: Dirichlet Convolution. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.