When I first learn Möbius function, all the resources I found just explain everything algebraically (e.g. substitute $$$[\gcd = 1]$$$ using the identity $$$\sum\limits_{d | \gcd}\mu(d) = [\gcd = 1]$$$ when solving certain kind of gcd counting problem, then start to do a bunch of algebra and boom! the problem is solved, and I don't know why it works lol) which is not very motivated in my opinion. But after rethinking about it after solving this problem, I think I can shed some light on the motivation behind Möbius function, from a combinatorics perspective.
Definition of Möbius function
What a weird function? Why do we want to define something based on number of prime divisors and why do we want to let everything not square-free to be zero? You might ask.
But before I explain the motivation behind it, let's first recall how to find the sum of elements in a rectangular region of a 2D suffix sum array, for some reason. So we have the following problem now.
Problem: Given a 2D suffix sum array $$$g$$$ obtained from another 2D array $$$f$$$, i.e. $$$g(i, j) = \sum\limits_{i' \ge i}\sum\limits_{j' \ge j}f(i', j')$$$, and a query $$$(l, r, d, u) (l \le r, d \le u)$$$, find the sum of elements in the respective rectangular region $$$[l, r] \times [d, u]$$$ of $$$f$$$, i.e. $$$\sum\limits_{i = l}^r\sum\limits_{j = d}^u f(i, j)$$$.
Solution: We can solve the above problem by adding the region $$$[l, -] \times [d, -]$$$, excluding $$$[r + 1, -] \times [d, -]$$$ and $$$[l, -] \times [u + 1, -]$$$ then adding $$$[r + 1, u + 1]$$$ back. So the answer is just $$$g(l, d) - g(r + 1, d) - g(l, u + 1) + g(r + 1, u + 1)$$$.
Ok, but how does this problem related to Möbius function? Isn't we have some prime divisor stuff at the beginning?
Actually, we can map natural number to an array with infinite dimensions, where the $$$k$$$-th dimension correspond to the count of $$$k$$$-th prime of certain number's prime factorization. For example, when $$$x = 20 = 2^2 \times 5^1$$$, its corresponding position in the array is $$$(2, 0, 1, 0, 0, \ldots)$$$ because $$$20$$$ have two $$$2$$$, zero $$$3$$$, one $$$5$$$, etc...
I would call such mapping from natural numbers to multidimension array "prime factorization mapping" in the rest of the article.
For convenience, let's assume we only have two dimension now. That is, we only consider natural numbers whose prime divisors are only $$$2$$$ or/and $$$3$$$. The following picture gives an example on how these natural numbers correspond to each position.

We can found that for a fix $$$(i, j)$$$, $$$2^i \times 3^j$$$ would divide $$$2^k \times 3^l$$$ if and only if $$$i \le k, j \le l$$$ hold. Thus the $$$k$$$-th entry of suffix sum array should contain everything that is multiple of $$$k$$$ and the rectangular region $$$[l, r] \times [d, u]$$$ would correspond to all natural numbers divisible by $$$2^l \times 3^d$$$, but not divisible by $$$2^{r + 1}$$$ and $$$3^{u + 1}$$$. So if we repharse the above problem in the context of "prime factorization mapping", we would get the following problem.
Problem: Given a suffix sum array $$$g$$$ obtained from another array $$$f$$$, where the suffix sum is defined in the manner of divisibility, i.e. $$$g_n = \sum\limits_{n | m}f(m)$$$, and a query $$$(l, r, d, u) (0 \le l \le r, 0 \le d \le u)$$$, find the sum of elements in the entries divisible by $$$2^l \times 3^d$$$ but not divisible by $$$2^{r + 1}$$$ and $$$3^{u + 1}$$$, i.e. $$$\sum\limits_{(i, j) \in [l, r] \times [d, u]}f(2^i3^j)$$$.
Solution: By similar logic, we can add everything that is multiple of $$$2^l3^d$$$ then excluding everything that is multiple of $$$2^{r + 1}3^d$$$ and $$$2^l3^{u + 1}$$$ then adding everything that is multiple of $$$2^{r + 1}3^{u + 1}$$$ back. So the answer is $$$g(2^l3^d) - g(2^{r + 1}3^d) - g(2^l3^{u + 1}) + g(2^{r + 1}3^{u + 1})$$$.
Now let's consider a variation of above problem, where $$$l = r, d = u$$$ hold, and the dimension of "prime factorization mapping" become infinite, so we can generalize things to all natural number instead of numbers with prime factors $$$\in \{2, 3\}$$$.
Problem: Given a suffix sum array $$$g$$$ obtained from another array $$$f$$$, where the suffix sum is defined by the divisibility relation, i.e. $$$g(n) = \sum\limits_{n | m}f(m)$$$, and a query $$$n$$$, find $$$f(n)$$$.
Solution: Similar to above problems, except we have infinite dimension rather than 2 dimension. So we can add everything that is multiple of $$$n$$$, excluding everything that is multiple of $$$2n, 3n, 5n\ldots$$$ then adding multiple of $$$2\times 3n, 2\times 5n, 2\times 7n, \ldots, 3\times 5n, 3 \times 7n\ldots$$$ etc. Then the answer would be $$$\sum\limits_{r \in \mathbb{N}}g(rn)[r \text{ don't have duplicate prime factor}](-1)^{\text{number of prime factors } r \text{ have}}$$$, by the inclusion-exclusion principle.
If you can't convince yourself the above solution is correct, let's recall how we solve the first version of problem, and imagine we have a $$$3$$$ dimension array with axis correspond to prime factors $$$2, 3, 5$$$, then what we are doing above is just take all elements that are multiple of $$$n$$$, then excluding red blocks (multiple of $$$2n, 3n, 5n$$$), adding blue blocks (multiple of $$$2\times 3 n, 2 \times 5 n, 3 \times 5 n$$$) then excluding the purple block (multiple of $$$2\times 3 \times 5 n$$$) in the following pictures

(I can't find a 3D graphic renderer draws better than this, sorry. But you got the idea.)
And one can find that, this is equivalent to $$$\sum\limits_{r \in \mathbb{N}}g(rn)\mu(r)$$$. Then it become quite motivated why we want to define $$$\mu$$$ like that. It's just a convenient function for us to do inclusion-exclusion for the suffix sum array with divisibility relation. Btw, this is what you would get when using the substitution $$$\sum\limits_{d | \gcd}\mu(d) = [\gcd = 1]$$$ if you know what it is.
Now let's talk about another thing related to Möbius function.
the Möbius inversion formula
Let $$$f, g$$$ be a function defined on natural number, and $$$g$$$ satisfy
for every $$$n \in \mathbb{N}$$$, then
holds for every $$$n \in \mathbb{N}$$$.
Again, let's consider everything under the context of "prime factorization mapping". the first equation is esentially tell us $$$g$$$ is obtained by doing prefix sum on $$$f$$$ under divisibility relation, so we can relate it to the following problem.
Problem: Given a prefix sum array $$$g$$$ obtained from another array $$$f$$$, where the prefix sum is defined by the divisibility relation, i.e. $$$g(n) = \sum\limits_{d | n}f(d)$$$, and a query $$$n$$$, find $$$f(n)$$$.
Solution: the only difference compared to previous problem is we got the prefix sum array instead of suffix sum array, so the solution is almost the same, i.e. add everything that is divisor of $$$n$$$, exclude everything that is divisor of $$$\frac{n}{d}$$$ for every prime divisor $$$d$$$ of $$$n$$$, then adding everything that is divisor of $$$\frac{n}{d_1d_2}$$$ for every prime divisor $$$d_1 \neq d_2$$$ of $$$n$$$, etc. In other word, the answer is $$$\sum\limits_{d | n} g(\frac{n}{d})[d \text{ don't have duplicate prime factor}](-1)^{\text{number of prime factors } d \text{ have}}$$$, which is equivalent to $$$\sum\limits_{d | n}g(\frac{n}{d})\mu(d)$$$, then you can find that, again, $$$\mu$$$ is just a convenient function for us to do inclusion-exclusion for the prefix sum array with divisibility relation.
And again, you can refer above 3D block picture, rotate it by $$$180$$$ degree to convience yourself this is correct.
wrap-up
So in conclusion, the Möbius function and Möbius inversion formula is just doing inclusion-exclusion on the prefix/suffix sum array under divisibility relation.
And if you wonder why we want to do such thing, well, in some circumstance, it's easier to find the prefix/suffix sum array compared to finding the original array directly, if you wonder what's the exact circumstance, I refer you to another blog about Möbius function, which contain a few related problems, but now you can solve it while having intuition on what and why are you doing exactly in each step!
Finally I invite you to challenge the problem mentioned in the beginning, and I would leave a few hint if you need.








Auto comment: topic has been updated by Misuki (previous revision, new revision, compare).
clear and useful.
Nice! I conducted a lecture on Dirichlet convolution in 2021, here are the slides -- I also tried to draw an analogy between the Möbius function and prefix sums there (and then went full algebra).
(also, when I finished making the slides, I thought to myself: "probably this is too little for 80 minutes, I need to add more", and proceeded to add the last section on sublinear summation. Turned out the initial amount of material was just right)
I read the sublinear summation part of the slide and understand everything in around 20 minutes, the slide is very concise and I learn something new today, thanks you!
Though I have a little question, if I understand correctly, it can be applied to any function $$$f$$$ s.t. there exist $$$g, h$$$ satisfy $$$f * g = h$$$ and prefix sum of $$$g, h$$$ are easy to compute. And I think it can be applied to $$$\mu$$$ and $$$\phi$$$ because of $$$\mu * 1 = [n = 1]$$$ and $$$\phi * 1 = id$$$. And I wonder if there are more function can be compute in such manner except $$$\mu, \phi$$$?
Good question! I don't remember exactly, maybe there were some cases in my practice, but I think they felt kinda artificial
where can i find your slides for any other topic, if possible
the slides looks good and well defined , its not easy finding a good blog or a resource for number theory :)
Thanks! There were no regular lectures by me. This one was because I was asked to read the lecture in that school. I also used to tell students about inclusion-exclusion principle, but I don't remember where the materials are and if they are even not in Russian.
Oh, now I kinda get how the formulas in my solutions work
This problem can also be solved with Mobius — [Variants] An interesting counting problem related to square product
Given $$$n$$$, count the number of pairs $$$(a, b)$$$ that $$$1 \leq a \lt b \leq n$$$ that $$$a \times b$$$ is a perfect square.
I expanded problem in many variants, including number of kth tuples $$$(a_1, a_2, \ldots, a_k)$$$ and sufficiently high number of $$$n$$$.
While there are NO practical problems known to exists, I see some several academic works related to the problem.
Still, I have one question, that is how can we intuitively understand the solution with Möbius, says, with the idea that there is one and only one way of making $$$X = U * P^2$$$ where $$$P^2$$$ is the largest perfect square divisor. We can make a lot of solutions.
If we oriented about fixing $$$U$$$ and count $$$P^2$$$, we can have $$$O(N^3)$$$ brute-forces of all possible triplets, $$$O(N^2 \log N^2)$$$ bruteforces of all possible perfect squares, $$$O(N^2)$$$ bruteforces of all possible pair(a, b), $$$O(N \sqrt{N})$$$ checking squarefree property, $$$O(N \log N)$$$ sieve and factorizations, $$$O(N \log \log N)$$$ optimized sieve, $$$O(N)$$$ linear sieve. Each can easily be intuitively understand via a related problem about prime and prime sieve.
But if we oriented about fixing $$$P^2$$$ and count $$$U$$$, we can have $$$O\left(\sqrt{n}^2\right)$$$ bruteforces with Möbius sieve, $$$O(\sqrt{n} \log \sqrt{n})$$$ iterate through each perfect square divisors combined with Möbius, $$$O\left(\sqrt{\frac{n}{k}} \log \log \sqrt{\frac{n}{k}}\right)$$$ iterate through each prime divisors without the need of Möbius
I tried to teach my students for lessons, sometimes I addeed the problems with lores but it is really hard to convince the need of Möbius, and how the fuck can we think about Möbius in a first place. Says, if we come to a related combinatorics problem, how can we easily recognize and identify this problem can be solvable via Möbius intuitively ?
With already clear knowledge of maths, one might slowly come up to the solutions by solving systems of equations or related techniques. But most of my students only focus on programming and lazy to solve math, and in order to get them to learn math in a useful way, I usually tried to make problems that has some practicals, or atleast have a good relation with some funny ideas. But still, my tutorials related to those Möbius are very hard to convince someone about its practicality, and deemed not worth learning.
The problem you said don't need Möbius function, it can be solved by just noticing $$$x$$$ is perfect square is equivalent to each prime factor appear enen number of times in $$$x$$$'s prime factorization, so you only care the parity of each primes occurrence, thus you can replace every $$$x$$$ by the product of $$$x$$$'s distinct prime factor(let's call it $$$x'$$$), then $$$xy$$$ is perfect square iff $$$x'=y'$$$, which is trivially enough to solve directly.
Regard to the use of Möbius, as the blog said, it's essentially PIE on prefix/suffix sum of multidimension array with divisibility relation, so you would use it whenever you have a prefix/suffix sum array and want to obtain the original array.