Prefix Sums of Multiplicative Functions
Authors: Tianqin Meng, Benjamin Qi
Covering Dirichlet convolution, Möbius inversion, and binomial inversion.
Initial Overview
Definition
- If a function maps positive integers to complex numbers, it's an arithmetic function.
- If is an arithmetic function, and f(p⋅q) = for any coprime positive integers p,q, it's a multiplicative function.
- If is multiplicative and = for any positive integers p,q, 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.
Dirichlet Convolution
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 we have
Möbius Inversion
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, ,
Example - Sum of Divisors
Focus Problem – try your best to solve this problem before continuing!
Explanation
It's 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.
Implementation
Time Complexity:
C++
class Solution {public:long long sumOfDivisors(int n) {long long sum = 0;int i;for (i = 1; i * i <= n; i++) {sum += i * (n / i);int b = n / i;int a = n / (i + 1) + 1;// Adding the sum of elements from 'a' to 'b'
Python
class Solution:def sumOfDivisors(self, n):res = 0i = 1while i * i <= n:res += i * (n // i)b = n // ia = (n // (i + 1)) + 1# Adding the sum of elements from 'a' to 'b'res += (((a + b) * (b - a + 1)) // 2) * i
Example - Totient Sum
Focus Problem – try your best to solve this problem before continuing!
Explanation
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.
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;using ll = long long;Code Snippet: ModInt (Click to expand)template <int SZ> struct Sieve {vector<int> prime;int sp[SZ], phi[SZ]; // smallest prime that dividesSieve() { // above is faster
Example - Counting Primes
Focus Problem – try your best to solve this problem before continuing!
There's two ways to do this problem. The first solution has a higher time complexity but a less complex implementation, while the second has a lower time complexity at the cost of a more complex implementation.
For a more complete explanation of these algorithms, refer to this CF blog post.
Explanation 1
Utilizes a dynamic programming approach based on a recursion relation derived from sieving.
The algorithm iteratively reduces the count of numbers that are not divisible by primes, utilizing a recursive formula. It achieves a complexity of .
Implementation
Time Complexity:
C++
#include <algorithm>#include <cmath>#include <iostream>#include <vector>using ll = long long;using std::cout;using std::endl;using std::pair;using std::vector;
Explanation 2
There exists an implementation; see Maksim1744’s Codeforces blog for more details. Below is an implementation with a BIT. Note that the fastest solutions to this library checker problem look like they run in .
Implementation
Time Complexity:
C++
#include <algorithm>#include <cmath>#include <iostream>#include <vector>using ll = long long;using std::cout;using std::endl;using std::pair;using std::vector;
Problems
Status | Source | Problem Name | Difficulty | Tags | |
---|---|---|---|---|---|
HDU | Normal | ||||
GeeksForGeeks | Normal | ||||
SPOJ | Hard | ||||
SPOJ | 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!