Prefix Sums of Multiplicative Functions
Authors: Tianqin Meng & Benjamin Qi,
Rare
Prerequisite Skills
Definition of multiplicative functions
- If a function f(n) maps positive integers to complex numbers (f: ℤ⁺ → ℂ), it's an arithmetic function.
- If f(n) is an arithmetic function, f(1) = 1 and f(p⋅q) = f(p)⋅f(q) for any coprime positive integers p,q, it's a multiplicative function.
- Further, if f(n) is multiplicative and f(p⋅q) = f(p)⋅f(q) for any positive integers p,q, it's a completely multiplicative function.
Properties and Examples of Multiplicative Functions
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.
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:
Dirichlet Convolution and Möbius Inversion
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 multiplicative function and the identity function . For example, if and ,then:
Möbius inversion is also a discussion regarding , but it does not require to be multiplicative and is applicable in cases where is known and is to be determined. Since , ,and . Similarly, ,Binomial inversion is also a similar technique. An example illustrates the relationship between the Euler function and the Möbius function: since ,then ,which implies
Focus problem # 1
Find the sum of divisors of the first positive integers, i.e., , where .
Approach:
It's obviously not feasible to compute directly, but we can derive it as follows:
When , there are only distinct values for . Similarly, when , has only distinct values. For a fixed , the values of form a contiguous interval, which is . Therefore, the calculation can be done in time.
Similarly, the sum of the number of divisors for the first positive integers can be calculated in the same manner. I leave this as an exercise for the reader.
Another thing to note is that . This is also a common representation form.
Focus problem # 2
Now let's increase the difficulty a bit. We want to find the sum of Euler's totient function for the first positive integers, i.e., , where .
Approach:
Currently, the formulas related to Euler's totient function mentioned in this article are limited. Can we use them to simplify the problem? The answer is yes, and now we'll utilize the formula to simplify the expression.
This formula can also be seen as . Let's denote , then we have:
So as long as you calculate the values of for times, you can compute . What about the complexity of such an operation?
Suppose the complexity of calculating is , then we have . Here, we only expand one layer because deeper complexities are higher-order terms. So, we have .
Since is a prefix sum of a multiplicative function, the sieve method can preprocess a portion. Assuming the preprocessing includes the first positive integers' where , the complexity becomes . When , we can achieve a good complexity of .
How did we come up with the place where we utilized ? Let's take a look at this:
If we can construct a function through Dirichlet convolution that computes prefix sums more efficiently and if another function suitable for convolution is also easy to calculate, we can simplify the calculation process. For example, in the above problem, we used the property of . But remember, not all problems of this type can be easily solved by just pairing with an identity function . Sometimes, a more careful observation is needed.
More information below left over by Benjamin Qi
https://codeforces.com/blog/entry/54150
Linear Time Sieve
https://judge.yosupo.jp/problem/enumerate_primes
Counting Primes
https://judge.yosupo.jp/problem/counting_primes
Totient Function
https://judge.yosupo.jp/problem/sum_of_totient_function
template <int SZ> struct Sieve {vi pr;int sp[SZ], phi[SZ]; // smallest prime that dividesSieve() { // above is fastermemset(sp, 0, sizeof sp);phi[1] = 1;FOR(i, 2, SZ) {if (sp[i] == 0) {sp[i] = i, pr.pb(i);phi[i] = i - 1;
(project euler)
(topcoder problem)
Practice problems
Here are some practice problems for everyone to understand the methods mentioned above. Such problems are relatively few, so if you have other sources of problems, please feel free to share.
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!