Möbius Inversion
Prerequisites: Number Theoretic Block, Dirichlet Convolution
Möbius inversion is an important topic in number theory. For some function \(f(n)\), if it is difficult to directly compute its value, but easy to compute its sum over multiples or divisors \(g(n)\), then Möbius inversion can simplify the computation and find the value of \(f(n)\).
Möbius Function
The Möbius function is defined as
Specifically, suppose the positive integer \(n\) has prime factorization \(n=\prod_{i=1}^kp_i^{e_i}\), where \(p_i\) are primes and \(e_i\) are positive integers. Then the three cases correspond to:
- \(\mu(1) = 1\);
- When there exists \(i\) such that \(e_i > 1\), i.e., any prime factor appears more than once, \(\mu(n)=0\);
- Otherwise, for all \(i\), \(e_i = 1\), i.e., every prime factor appears exactly once, \(\mu(n)=(-1)^k\), where \(k\) is the number of distinct prime factors.
Properties
From the definition, it is easy to verify that the Möbius function \(\mu(n)\) is multiplicative but not completely multiplicative. Besides, the most important property is the following identity:
Property
For a positive integer \(n\), we have
where \([\cdot]\) is the Iverson bracket.
Proof
Let \(n=\prod_{i=1}^kp_i^{e_i}\), and set \(n' = \prod_{i=1}^kp_i\). By the binomial theorem, we have
Using Dirichlet convolution, this expression can be written as \(\varepsilon = 1 * \mu\). That is, the Möbius function is the Dirichlet inverse of the constant function \(1\).
This property has a common application:
It transforms the coprime condition into a sum involving the Möbius function, facilitating further derivation.
Computation
If we need to compute the Möbius function \(\mu(n)\) for a single \(n\), we can use its prime factorization. For example, when \(n\) is not too large, we can compute \(\mu(n)\) in \(O(\sqrt{n})\) time.
Reference Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
If we need to precompute \(\mu(n)\) for the first \(n\) positive integers, we can use the fact that it is multiplicative and compute it via linear sieve in \(O(n)\) time.
Reference Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
Möbius Inversion
The most important application of the Möbius function is Möbius inversion.
Möbius Inversion
Let \(f(n),g(n)\) be two arithmetic functions. Then,
Proof 1
Direct verification:
The key step is swapping the order of summation and noting that \(k\mid d\mid n\) is equivalent to \(\dfrac{n}{d}\mid\dfrac{n}{k}\). The second last equality sums the Möbius function over divisors \(\dfrac{n}{d}\) of \(\dfrac{n}{k}\), which equals \(\left[\dfrac{n}{k} = 1\right]\). This expression is nonzero only when \(n=k\), yielding \(g(n)\).
Proof 2
Using Dirichlet convolution, the proposition is equivalent to
Using \(1 * \mu = \varepsilon\), convolving both sides of the left equation with \(\mu\) gives
In summations involving divisibility relations of arithmetic functions, Möbius inversion is a powerful transformation tool.
Examples
-
The Euler totient function \(\varphi(n)\) satisfies \(n = \sum_{d\mid n}\varphi(d)\), i.e., \(\mathrm{id}=1*\varphi\). Inverting it gives \(\varphi = \mu * \mathrm{id}\), i.e.,
\[ \varphi(n) = \sum_{d\mid n}d\mu\left(\dfrac{n}{d}\right). \] -
The divisor function \(\sigma_k(n) = \sum_{d\mid n}d^k\), i.e., \(\sigma_k = 1 * \mathrm{id}_k\). Inverting it gives \(\mathrm{id}_k = \mu * \sigma_k\), i.e.,
\[ n^k = \sum_{d\mid n}\mu\left(\dfrac{n}{d}\right)\sigma_k(d). \] -
The distinct prime factor count function \(\omega(n)=\sum_{d\mid n}[d\in\mathbf P]\), i.e., \(\omega = 1* \mathbf{1}_{\mathbf P}\), where \(\mathbf{1}_{\mathbf P}\) is the indicator function of the prime set \(\mathbf P\). Inverting it gives \(\mathbf{1}_{\mathbf P} = \mu * \omega\), i.e.,
\[ [n\in\mathbf P] = \sum_{d\mid n}\mu\left(\dfrac{n}{d}\right)\omega(d). \] -
Consider the arithmetic function \(\Lambda(n)\) satisfying \(\log n = \sum_{d\mid n}\Lambda(d)\). It is the Möbius inversion of the logarithm function, also known as the von Mangoldt function:
\[ \Lambda(n) = \sum_{d\mid n}\mu\left(\dfrac{n}{d}\right)\log d = \begin{cases} \log p, & n = p^e,~p\in\mathbf P,~e\in\mathbf N_+, \\ 0, &\text{otherwise}. \end{cases} \]
Appendix: Proof of \(\Lambda(n)\) expression
For prime powers \(n=p^e~(e\in\mathbf N_+)\), we have
For \(n=1\), obviously \(\Lambda(n)=\log 1=0\). For other composite \(n\), we have
By the property of the Möbius function, the coefficient of \(\log n\) is \([n=1]=0\). For the latter sum, further factor \(d\) into prime factors. For any prime \(p\mid n\), consider the coefficient of \(\log p\):
Thus, for composite \(n\) with more than one prime factor, \(\Lambda(n)=0\).
Extended Forms
Besides the basic form, Möbius inversion has some common extended forms. First, consider its sum-over-multiples form.
Extension 1
Let \(f(n),g(n)\) be two arithmetic functions. Then,
Proof
Direct verification:
This derivation is dual to the basic form.
Second, Möbius inversion is not limited to addition; it holds for any operation in an Abelian group. For example, it has the following multiplicative form:
Extension 2
Let \(f(n),g(n)\) be two arithmetic functions. Then,
Proof
Direct verification:
Here, \(a\uparrow b = a^b\) is the Knuth arrow. Compared to the proof of the basic form, the only difference is addition replaced by multiplication and multiplication replaced by exponentiation.
From the perspective of Dirichlet convolution, Möbius inversion merely uses the fact that "the Möbius function is the Dirichlet inverse of the constant function". It is easy to imagine that similar inversion relations hold for general Dirichlet inverses.
Extension 3
Let \(f(n),g(n),\alpha(n)\) be arithmetic functions, and let \(\alpha^{-1}(n)\) be the Dirichlet inverse of \(\alpha(n)\), i.e.,
Then,
Proof
Direct verification:
Compared to the proof of the basic form, only the second last equality is replaced by the definition of Dirichlet inverse.
Corollary
Let \(f(n),g(n)\) be arithmetic functions, and \(t(n)\) a completely multiplicative function. Then,
Proof
By the properties of Dirichlet convolution, for a completely multiplicative function \(t(n)\), its Dirichlet inverse is \(\mu(n)t(n)\).
Finally, Möbius inversion can be generalized to complex-valued functions on \([1,+\infty)\), not just arithmetic functions. The basic form of Möbius inversion can be seen as a special case where the complex-valued function is zero at all non-integer points.
Extension 4
Let \(F(x)\) and \(G(x)\) be complex-valued functions on \([1,+\infty)\). Then,
Proof
Extend the definitions of \(F\) and \(G\) by setting \(F(x)=G(x)=0\) for \(x < 1\). Then the proposition is equivalent to:
The sums are over \(n\in\mathbf N_+\).
Direct verification:
To obtain the second equality, set \(k = nd\).
Corollary
Let \(f(n),g(n)\) be arithmetic functions. Then,
Proof
Simply take \(F(x)=f(\lfloor x\rfloor)\) and \(G(x)=g(\lfloor x\rfloor)\).
These extended forms can be combined to yield more complex inversion relations.
Dirichlet Prefix Sum
Prerequisites: Prefix Sum and Difference
Consider the basic form of Möbius inversion:
In the left equation, \(f(n)\) is the sum of \(g(n)\) over all divisors of \(n\). If we interpret \(a\mid b\) as \(a\) preceding \(b\), then \(f(n)\) can be seen as a kind of prefix sum of \(g(n)\). Therefore, in the Chinese competitive programming community, the process of computing \(\{f(k)\}_{k=1}^n\) from \(\{g(k)\}_{k=1}^n\) is also called Dirichlet prefix sum, and the inverse process is called Dirichlet difference. These methods often appear when preprocessing the values of an arithmetic function at the first \(N\) points.
Next, we discuss the computation of Dirichlet prefix sums. If we treat each prime as a dimension, this becomes a kind of high-dimensional prefix sum. Recall the dimension-by-dimension prefix sum algorithm: iterate over all dimensions one by one, and accumulate the value at each position to its successor in that dimension. For arithmetic functions, this means iterating over all primes \(p\) from small to large, and accumulating the function value at \(n\) to \(np\). This traversal order is the same as in the Eratosthenes sieve. Therefore, this algorithm can compute the Dirichlet prefix sum of a sequence of length \(n\) in \(O(n\log\log n)\) time. Similarly, using dimension-by-dimension difference, the Dirichlet difference can be computed in the same time complexity.
Reference Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
This computation method can be generalized to the sum-over-multiples form (Extension 1), multiplicative form (Extension 2), and using completely multiplicative functions instead of the constant function (corollary of Extension 3).
Examples
This section demonstrates the application of Möbius inversion and some common transformation techniques through examples. First, an example to familiarize with basic techniques for handling greatest common divisor conditions in summations.
Luogu P2522 [HAOI 2011] Problem b
\(T\) groups of data. For each group, compute:
Data range: \(1\le T,x,y,n,m,k\le 5\times 10^4\).
Solution
By inclusion-exclusion, the original expression can be split into \(4\) blocks, each of the form
For such expressions, the following is a standard derivation process: extract common factors, apply Möbius function property, swap summation order.
First, since \(i,j\) can only be multiples of \(k\), we can extract this factor—this is equivalent to substituting \(i=ki'\) and \(j=kj'\), yielding:
Then using the property of the Möbius function:
Substituting into the expression and swapping summation order gives:
The advantage of this manipulation is that for fixed \(d\), the terms involving \(i\) and \(j\) are separated and can be summed independently. Next, since
we have
After preprocessing \(\mu(d)\) with linear sieve and its prefix sum, we can solve via number theoretic blocking. Total time complexity is \(O(N + T\sqrt{N})\), where \(N\) is the upper bound of \(n,m\), and \(T\) is the number of data groups.
Reference Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
The next two examples demonstrate handling by enumerating common divisors and using sieve methods to compute values of general multiplicative functions.
SPOJ LCMSUM
\(T\) groups of data. For each group, compute:
Data range: \(1\le T\le 3\times 10^5,~1\le n\le 10^6\).
Solution 1
The problem involves least common multiple, but greatest common divisor is often easier to handle. First, transform:
Factor out \(n\) and enumerate the greatest common divisor \(k\):
For the inner sum, this is the most common scenario involving gcd. Repeating the standard process:
Again, the sum over \(i\) is separated and can be handled independently. The final sum is essentially an arithmetic series sum: (set \(i=di'\))
Thus, we obtain:
After enumerating common divisors, such double sums are common. There is a fixed handling method: set the product as a new variable \(\ell=kd\), then swap summation order again. Since \(d\mid(n/k)\) is equivalent to \(d\mid\ell\mid n\), the expression becomes:
Let \(F(\ell)=\sum_{d\mid\ell}\mu(d)d\). Then:
Since \(\mu(d)d\) is multiplicative, its convolution with the constant function \(1\), \(F(n)\), is also multiplicative. Although the sum appears in Dirichlet convolution form, \(G(n)\) is not multiplicative. However, \(G(n)\) is a polynomial, so it is a linear combination of completely multiplicative functions. Thus,
These two terms (excluding coefficients) are multiplicative functions and can be preprocessed via linear sieve (or by sieving the inner function and then using Dirichlet prefix sum in \(O(N\log\log N)\) time). Specifically, let
To derive their expressions, it suffices to determine their values at prime powers. For prime \(p\) and positive exponent \(e\),
In particular, \(H_1(p^e)\equiv 1\) is constant, and
This is easily computed via linear sieve. After preprocessing \(H_2(n)\) with linear sieve, each query can be answered in \(O(1)\) time via \(f(n)=(n/2)(H_2(n)+1)\). Total time complexity is \(O(N+T)\), where \(N\) is the upper bound of \(n\), and \(T\) is the number of groups.
The reference implementation further optimizes the linear sieve part using specific properties of the expression, which is not mandatory. Using values at prime powers, preprocessing can still be done in \(O(N)\) time. These derivations are detailed in Solution 2.
Solution 2
For this problem, there is a more flexible approach. From Solution 1,
If we do not proceed with Möbius inversion here but observe that the sum is actually the sum of integers not exceeding \(d=n/k\) and coprime to \(d\). For \(d>1\), since integers coprime to \(d\) come in pairs, i.e., \(i\) and \(d-i\) are both coprime to \(d\), we have
For \(d=1\),
Thus, the original expression becomes
Since \(G(n)=\sum_{d\mid n}d\varphi(d)\) is the Dirichlet convolution of the multiplicative function \(n\varphi(n)\) and the constant function \(1\), it is also multiplicative and can be preprocessed via linear sieve. To do so, determine its values at prime powers. For prime \(p\) and positive exponent \(e\),
This matches the result derived in Solution 1. The total time complexity remains \(O(N+T)\).
Finally, using the multiplicative function expression for this problem, the linear sieve computation can be further optimized. For prime \(p\),
The key to linear sieve is computing \(G(pn)\) for general \(n\). This splits into two cases. When \(p\perp n\), since \(G\) is multiplicative,
Otherwise, when \(p\mid n\), let \(n=p^em\) with \(p\perp m\). Then
Direct verification shows this also holds for \(p\perp n\). Therefore,
Substituting,
This simplifies the linear sieve computation. Of course, this derivation is not necessary; for multiplicative functions without special properties, directly using \(G(pn)=G(p^{e+1})G(m)\) suffices.
Reference Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
BZOJ 2154 [National Training Team] Crash's Number Table
Compute:
Data range: \(1\le n,m\le 10^7\).
Solution
Ignore modulus during derivation. Let
Convert lcm to gcd, enumerate common divisor, and apply standard process:
Again, the sums separate for \(i\) and \(j\). Compute these inner sums first by extracting factors (set \(i=di'\)):
where \(G(n)=\dfrac{1}{2}n(n+1)\) is the arithmetic series sum; the last equality uses properties of the floor function. Symmetrically for the other sum. Substituting back,
As before, for such expressions enumerating common divisors, we often enumerate the product \(\ell = kd\) and swap summation order again:
Let
This is the product of the multiplicative function \(\ell\) and the multiplicative function \(\sum_{d\mid\ell}\mu(d)d\), so it is also multiplicative and can be preprocessed via linear sieve, along with its prefix sum. Then \(f(n,m)\) can be computed via number theoretic blocking. Total time complexity is \(O(\min\{n,m\})\).
Reference Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
The next example is special, requiring transformation of the divisor count function of a product.
LOJ 2185. [SDOI2015] Sum of Divisors
\(T\) groups of data. For each group, compute:
where \(\sigma_0(n)=\sum_{d \mid n}1\) denotes the number of divisors of \(n\).
Data range: \(1\le n,m,T\le 5\times 10^4\).
Solution
The difficulty lies in transforming \(\sigma_0(ij)\) into an expression involving gcd. Since \(\sigma_0\) is multiplicative, first consider prime power cases. For prime \(p\) and nonnegative exponents \(e_1,e_2\), let \(i=p^{e_1},~j=p^{e_2}\). Then
For the general case, let \(i=\prod_p i_p\) and \(j=\prod_p j_p\), where \(i_p,j_p\) are the powers of \(p\) in the prime factorizations of \(i,j\) respectively. Then
Note that for each prime power factor \(i_p\) of \(i\), enumerating its divisors \(x_p\) is equivalent to enumerating divisors \(x\) of \(i\) and then extracting all prime power factors \(x_p\); similarly for \(j\). Thus, using distributive law,
The last step uses the fact: \(x\perp y\) if and only if for every prime factor \(p\), \(x_p\perp y_p\).
With this expression, apply the standard process:
The last step means: the function is nonzero only when \(d\mid i\) and \(d\mid j\), and then enumerating \(x\) with \(d\mid x\mid i\) is equivalent to enumerating divisors \(\dfrac{x}{d}\) of \(\dfrac{i}{d}\); similarly for \(y\).
Substitute back into the original sum and swap summation order:
Let \(G(n)=\sum_{i=1}^n\sigma_0(i)\). Then
This can be solved via number theoretic blocking. Only need to precompute prefix sums of \(\mu(n)\) and \(\sigma_0(n)\). Total time complexity is \(O(N+T\sqrt{N})\), where \(N\) is the upper bound of \(n,m\), and \(T\) is the number of groups.
Reference Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
The final example demonstrates applying the multiplicative version of Möbius inversion.
Luogu P5221 Product
Compute:
Data range: \(1\le n\le 1\times 10^6\).
Solution 1
Ignore modulus during derivation. Let
Convert lcm to gcd:
Note that the product factors are independent and can be computed separately. Let
Then the original expression equals:
The key is computing \(g(n)\). The process is similar to before but adapted to multiplication. First, enumerate and extract common factors:
Here, \(a\uparrow b=a^b\) is the Knuth arrow. Then substitute \([\gcd(i,j)=1]=\sum_d\mu(d)[d\mid i][d\mid j]\), and convert the sum in the exponent to a product of powers:
Further extract factors (set \(i=di'\), \(j=dj'\)) and use properties of the floor function:
Separating the product over \(i,j\), we find they do not appear, so it's equivalent to raising the product to a power:
Since we enumerated common divisors, we need to swap product order again. Let \(\ell = kd\):
Let
This is easily recognized as the multiplicative Möbius inversion of \(\tilde F(n)=n\). Even without knowing its expression, it can be preprocessed in \(O(n\log\log n)\) time using Dirichlet difference. Of course, since \(\tilde F(n)\) is simple, \(F(n)\) can be directly derived:
The von Mangoldt function is its natural logarithm. After obtaining \(F(n)\), applying multiplicative number theoretic blocking yields \(g(n)\) in \(O(\sqrt{n})\) time, and thus \(f(n)\). Total time complexity is \(O(n)\).
Note: when dealing with products, Euler's theorem is often needed, so the modulus for exponentiation differs from the given modulus.
Solution 2
The difficulty with multiplicative derivation is handling products and exponents. Alternatively, take logarithms first. For this problem, consider only \(g(n)\). Taking logarithm:
For such expressions involving gcd, apply the standard process:
where \(\Lambda(n)\) is the von Mangoldt function. Exponentiating this yields the result from Solution 1.
Reference Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
Exercises
- Luogu P3312 [SDOI2014] Number Table
- Luogu P3700 [CQOI2017] Little Q's Table
- Luogu P3704 [SDOI2017] Number Table
- Luogu P3768 Simple Math Problem
- Luogu P4464 [National Training Team] JZPKIL
- Luogu P4619 [SDOI2018] Old Exam
- Luogu P5518 [MtOI2019] Phantom Ensemble
- Luogu P6222 Simple Problem Enhanced
- Luogu P6825「EZEC-4」Summation
- Luogu P7486「Stoi2031」Rainbow
- AtCoder Grand Contest 038 C - LCMs
- Codeforces 1139 D. Steps to One
References
- Möbius function - Wikipedia
- Möbius inversion formula - Wikipedia
- Von Mangoldt function - Wikipedia
- algocode Algorithm Blog
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用