PrevNext

In this module we introduce how to compute prefix sums of certain number-theoretic functions in sublinear time. Here are some examples:

Focus Problem – try your best to solve this problem before continuing!

View Internal Solution

Focus Problem – try your best to solve this problem before continuing!

Focus Problem – try your best to solve this problem before continuing!

This module (part 1) will focus on topics relating to the first two focus problems. Prime counting and related applications will be deferred to part 2.

Multiplicative Functions

The functions that we'd like to compute prefix sums over in the first two focus problems are both multiplicative.

Definition

  1. If a function f:Z+Cf: \mathbb{Z}^+ \rightarrow \mathbb{C} maps positive integers to complex numbers, it's an arithmetic function.
  2. If f(n)f(n) is an arithmetic function, f(1)=1f(1) = 1 and f(pq)=f(p)f(q)f(p\cdot q) = f(p) \cdot f(q) for any coprime positive integers pp, qq, it's a multiplicative function.
  3. If f(n)f(n) is multiplicative and f(pq)f(p \cdot q) = f(p)f(q)f(p)\cdot f(q) for any positive integers pp, qq, it's a completely multiplicative function.

If f(n)f(n) is a multiplicative function, then for a positive integer n=i=1kpikin = \prod_{i=1}^{k} p_i^{k_i}, we have

f(n)=i=1kf(piki)f(n) = \prod_{i=1}^{k} f(p_i^{k_i})

If f(n)f(n) is a completely multiplicative function, then for a positive integer n=i=1kpikin = \prod_{i=1}^{k} p_i^{k_i}, we have

f(n)=i=1kf(pi)kif(n) = \prod_{i=1}^{k} f(p_i)^{k_i}

Examples

Common multiplicative functions are

  • Divisor function: σk(n)=dndk\sigma_k(n) = \sum_{d|n} d^k, representing the sum of the kkth powers of divisors of nn. Note that σk(n)\sigma_k(n) and σk(n)σ^k(n) are different.
  • Divisor count function: τ(n)=σ0(n)=dn1\tau(n) = \sigma_0(n) = \sum_{d|n} 1, representing the count of divisors of nn, also denoted as d(n)d(n).
  • Divisor sum function: σ(n)=σ1(n)=dnd\sigma(n) = \sigma_1(n) = \sum_{d|n} d, representing the sum of divisors of nn.
  • Euler's totient function: φ(n)=i=1n[(n,i)=1]1\varphi(n) = \sum_{i=1}^n [(n,i)=1] \cdot 1, representing the count of positive integers less than or equal to nn and coprime to nn. Additionally, i=1n[(n,i)=1]i=\sum_{i=1}^n [(n,i)=1] \cdot i = nφ(n)+[n=1]2\frac{n\varphi(n) + [n=1]}{2}, φ(n)\varphi(n) is even.
  • Möbius function: μ(n)\mu(n), serving as the multiplicative inverse of the identity function in Dirichlet convolution, μ(1)=1\mu(1) = 1, for a square-free number n=i=1tpin = \prod_{i=1}^t p_i, μ(n)=(1)t\mu(n) = (-1)^t, and for a number with square factors, μ(n)=0\mu(n) = 0.
  • Unit function: e(n)=[n=1]e(n) = [n=1], serving as the identity element in Dirichlet convolution, completely multiplicative.
  • Constant function: I(n)=1I(n) = 1, completely multiplicative.
  • Identity function: id(n)=nid(n) = n, completely multiplicative.
  • Power function: idk(n)=nkid^k(n) = n^k, completely multiplicative.

The two classic formulas regarding the Möbius function and the Euler function are:

  • [n=1]=dnμ(d)[n=1] = \sum_{d|n} \mu(d), Interpreting μ(d)\mu(d) as the coefficients of the inclusion-exclusion principle proves it.
  • n=dnφ(d)n = \sum_{d|n} \varphi(d). To prove it, we can count the number of occurrences of 1n(1in)\frac{1}{n}(1 \leq i \leq n) in its simplest fraction form.

Resources

As this module is supposed to serve as a gentle introduction to this topic, we will usually describe only the simplest solution that passes the given constraints. Solutions with asymptotically better time complexities can be found in blogs such as the following, though keep in mind that they might not be much faster under the given constraints.

Warm-Up

Let's start with introducing a set of numbers QNQ_N we often work with when computing prefix sums of multiplicative functions up to NN.

Focus Problem – try your best to solve this problem before continuing!

Exercise for the reader: How large can this set be?

Answer

An important property of this set is that if xQNx\in Q_N then QxQNQ_x \subseteq Q_N.

Why?

Implementation

Code

Example - Sum of Divisors

Focus Problem – try your best to solve this problem before continuing!

View Internal Solution

Hint: The time complexity is the same as for the previous problem.

Solution

Explanation

Implementation

Code

Dirichlet Convolution

Introduction

The Dirichlet convolution of number-theoretic functions ff and gg is defined as (fg)(n)=dnf(d)g(nd)(f*g)(n) = \sum_{d|n} f(d) \cdot g(\frac{n}{d}). Dirichlet convolution satisfies commutativity, associativity, and distributivity with respect to addition. There exists an identity function e(n)=[n=1]e(n) = [n=1] such that fe=f=eff*e = f = e*f。If ff and gg are multiplicative functions, then fgf*g is also multiplicative.

A common technique with Dirichlet convolution involves dealing with the convolution of a function ff and the identity function II. For example, if n=i=1tpikin = \prod_{i=1}^{t} p_i^{k_i} and g=fIg=f* I, then g(n)=dnf(d)g(n) = \sum_{d|n} f(d). If ff is multiplicative, then we have

g(n)=i=1tj=0kif(pij).g(n) = \prod_{i=1}^{t} \sum_{j=0}^{k_i} f(p_i^j).

If we wish to recover ff from gg, we can write gμ=(fI)μ=f(Iμ)=fe=fg* \mu = (f* I)* \mu=f* (I* \mu)=f* e=f. That is, f(n)=dng(d)μ(nd)f(n) = \sum_{d|n} g(d) \cdot \mu\left(\frac{n}{d}\right). This is known as Möbius inversion.

Example - Dirichlet Convolution and Prefix Sums

Focus Problem – try your best to solve this problem before continuing!

Given {fxxQN}\{f_x | x \in Q_N\} and {gxxQN}\{g_x | x \in Q_N\}, then if we define h=fgh=f*g, we can compute {hxxQN}\{h_x | x \in Q_N\} in sublinear time.

Solution

Explanation

Implementation

Code

Example - Dirichlet Inverse and Prefix Sums

Suppose instead of finding h=fgh=f*g given ff and gg, we'd like to recover gg given hh and ff.

Focus Problem – try your best to solve this problem before continuing!

Solution

Explanation

Implementation

Code

Example - Totient Sum

Focus Problem – try your best to solve this problem before continuing!

Solution

Explanation

Implementation

Code

Problems

The first two problems are from the first resource.

StatusSourceProblem NameDifficultyTags
HDUNormal
SPOJHard
YSHard

Module Progress:

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!

PrevNext