Table of Contents
Multiplicative FunctionsDefinitionExamplesResourcesWarm-UpImplementationExample - Sum of DivisorsSolutionImplementationDirichlet ConvolutionIntroductionExample - Dirichlet Convolution and Prefix SumsSolutionImplementationExample - Dirichlet Inverse and Prefix SumsSolutionImplementationExample - Totient SumSolutionImplementationProblemsIn 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 SolutionFocus 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
- If a function maps positive integers to complex numbers, it's an arithmetic function.
- If is an arithmetic function, and for any coprime positive integers , , it's a multiplicative function.
- If is multiplicative and = for any positive integers , , it's a completely multiplicative function.
If is a multiplicative function, then for a positive integer , we have
If is a completely multiplicative function, then for a positive integer , we have
Examples
Common multiplicative functions are
- Divisor function: , representing the sum of the th powers of divisors of . Note that and are different.
- Divisor count function: , representing the count of divisors of , also denoted as .
- Divisor sum function: , representing the sum of divisors of .
- Euler's totient function: , representing the count of positive integers less than or equal to and coprime to . Additionally, , is even.
- Möbius function: , serving as the multiplicative inverse of the identity function in Dirichlet convolution, , for a square-free number , , and for a number with square factors, .
- Unit function: , serving as the identity element in Dirichlet convolution, completely multiplicative.
- Constant function: , completely multiplicative.
- Identity function: , completely multiplicative.
- Power function: , completely multiplicative.
The two classic formulas regarding the Möbius function and the Euler function are:
- , Interpreting as the coefficients of the inclusion-exclusion principle proves it.
- . To prove it, we can count the number of occurrences of 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.
| Resources | |||||
|---|---|---|---|---|---|
| CF | |||||
| CF | |||||
| CF | |||||
| CF | see number theory section for blog posts on related topics | ||||
Warm-Up
Let's start with introducing a set of numbers we often work with when computing prefix sums of multiplicative functions up to .
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 then .
Why?
Implementation
Code
Example - Sum of Divisors
Focus Problem – try your best to solve this problem before continuing!
View Internal SolutionHint: 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 and is defined as . Dirichlet convolution satisfies commutativity, associativity, and distributivity with respect to addition. There exists an identity function such that 。If and are multiplicative functions, then is also multiplicative.
A common technique with Dirichlet convolution involves dealing with the convolution of a function and the identity function . For example, if and , then . If is multiplicative, then we have
If we wish to recover from , we can write . That is, . 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 and , then if we define , we can compute in sublinear time.
Solution
Explanation
Implementation
Code
Example - Dirichlet Inverse and Prefix Sums
Suppose instead of finding given and , we'd like to recover given and .
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.
| Status | Source | Problem Name | Difficulty | Tags | ||
|---|---|---|---|---|---|---|
| HDU | Normal | |||||
| SPOJ | Hard | |||||
| YS | Hard | |||||
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!