Misuki's blog

By Misuki, history, 11 months ago, In English

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

$$$\mu(n) = \begin{cases}(-1)^c & \text{ if } n \text{ is square-free and } n \text { have } c \text{ different prime divisors}\\ 0 & \text{ if } n \text{ is not square-free}\end{cases}$$$

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

$$$g(n) = \sum\limits_{d | n}f(d)$$$

for every $$$n \in \mathbb{N}$$$, then

$$$f(n) = \sum\limits_{d | n}g(\frac{n}{d})\mu(d)$$$

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.

hint1
hint2

Full text and comments »

  • Vote: I like it
  • +229
  • Vote: I do not like it

By Misuki, 20 months ago, In English

Except hints/solutions, I try to add a "general idea" section discuss about some general strategy and standard ideas used to solve the problem, hope you would like it :)

2001A - Make All Equal

idea & solution: Misuki

Hint 1
Hint 2
General idea
Solution
Code

2001B - Generate Permutation

idea & solution: Misuki

Hints/General idea of solution path 1
Hints/General idea of solution path 2
Solution
Code

2001C - Guess The Tree

idea: feecle6418, solution: Kaey

Hint 1 for solution 2
Hint 2 for solution 2
Solution
Solution 2
Code
Code 2

2001D - Longest Max Min Subsequence

idea & solution: Misuki, tester's code: SorahISA

Hint 1
Hint 2
General idea
Solution
Code
Tester's code

2001E1 - Deterministic Heap (Easy Version)

idea & solution: Misuki

Hint 1
Hint 2
Further hints for solution path 1 (official solution)
Further hints for solution path 2 (alternative solution)
General idea
Solution
Code

2001E2 - Deterministic Heap (Hard Version)

idea & solution: Misuki

Hint 1
Hint 2
Hint 3
General idea
Solution
Code

Full text and comments »

  • Vote: I like it
  • +212
  • Vote: I do not like it

By Misuki, 20 months ago, In English

Hello Codeforces! (你好, Codeforces!)

Kaey and I are glad to invite you to Codeforces Round 967 (Div. 2) which will start on Aug/20/2024 17:35 (Moscow time).

The contest will last for 2 hours with 5 tasks for you to solve, and 1 task will have subtasks. The contest will only be rated for those with a rating not higher than 2099, but higher rated users are also more than welcome to participate out of competition. There is at least one interactive problem, so please read the guide for interactive problems if you are unfamiliar with them.

Holding the contest would have been impossible without the help from:

The score distribution is $$$500 - 1000 - 1500 - 2000 - (2000 - 2000)$$$.

Good luck and have fun!

UPD1: Editorial

UPD2:

Congratulations to the winners:

Div.1 + Div.2:

  1. maspy
  2. Mangooste
  3. kotatsugame
  4. Rubikun
  5. potato167

Div.2:

  1. kkkksc03
  2. GouMah01
  3. rajer_that
  4. activedeltorre
  5. suuuuuu

Full text and comments »

  • Vote: I like it
  • +420
  • Vote: I do not like it

By Misuki, 2 years ago, In English

Hello codeforces community! Today I want to share my solution to a counting problem from recent contest: 1842G — Tenzing and Random Operations.

But first, why would I write a blog specifically for this problem? Because I found out this solution comprises a lot of common technique in counting problems and basic problem solving skill, which may be served as a wrap up of standard counting technique and idea used in CP, also providing an evidence that you can solve recent CF problems by just mastering the basic problem solving skill and common techniques, even on a 2800* one! And last but not least, provide an alternative solution to the problem :)

Now, let get started with the solution itself.

The problem description

For people who haven't try it, I recommend you to try it first before opening the spoiler!

Spoiler

Full text and comments »

  • Vote: I like it
  • +35
  • Vote: I do not like it

By Misuki, 3 years ago, In English

Recently I am learning generating function and some application about it, and found out this algorithm is very elegant but seems to only be well-known in Japan, also I have nothing to do during lunar new year, so I decide to write a blog to share this algorithm with CF community!

edit: Actually this algorithm have been discussed before, you can also see the other explanation in the link of this comment.

If there have some mistake or question, please comment below to let me know :)

Prerequistes

  • FFT/NTT
  • basic understanding of generating function
  • basic understanding of linear recurrence
  • knowing how to compute inv of sparse formal power series in $$$O(nk)$$$

the connection between $$$\frac{P(x)}{Q(x)}$$$ and linear recurrence

In this section, I will show the relation between $$$\frac{P(x)}{Q(x)}$$$ and linear recurrence, by proving the following lemma.

lemma: A sequence $$$(a_n)_{n\ge 0}$$$ is linearly recurrent of order $$$d$$$ $$$\Leftrightarrow$$$ its generating function is $$$\frac{P(x)}{Q(x)}$$$ where $$$deg(P(x)) \le d - 1, deg(Q(x)) = d$$$.

$$$\Rightarrow$$$: By definition, we have $$$a_i = \sum\limits_{j = 1}^{d}c_j a_{i - j}$$$ for $$$i \ge d$$$, and let $$$A(x)$$$ be the power series of sequence $$$(a_n)_{n\ge 0}$$$, i.e., $$$A(x) = \sum\limits_{i = 0}^{\infty} a_i x^i$$$

Consider construct a polynomial $$$Q(x) = 1 - \sum\limits_{i = 1}^{d}c_i x^i$$$, let $$$P(x) = Q(x)A(x)$$$, then for the $$$i$$$-th term of $$$P(x)$$$ satisfy $$$i \ge d$$$, we have

$$$p_i = \sum\limits_{j = 0}^d q_j a_{i - j} = a_i - \sum\limits_{j = 1}^d c_j a_{i - j} = 0$$$

Since $$$p_i = 0$$$ for all $$$i \ge d$$$, we have $$$deg(P(x)) \le d - 1$$$, and obviously $$$deg(Q(x)) = d$$$. And since $$$q_0 = 1$$$, $$$Q(x)^{-1}$$$ exist, we can multiply $$$Q(x)^{-1}$$$ on both side of $$$P(x) = Q(x)A(x)$$$, given us $$$\frac{P(x)}{Q(x)} = A(x)$$$.

$$$\Leftarrow$$$: Similarly, consider multiply $$$Q(x)$$$ on both side of $$$\frac{P(x)}{Q(x)} = A(x)$$$, which give us $$$A(x)Q(x) = P(x)$$$, then again, consider $$$i$$$-th term of $$$P(x)$$$ for all $$$i \ge d$$$, we have

$$$p_i = 0 = a_i - \sum\limits_{j = 1}^d c_j a_{i - j} \Rightarrow a_i = \sum\limits_{j = 1}^d c_j a_{i - j}$$$

which imply sequence $$$(a_n)_{n\ge 0}$$$ is linearly recurrent of order $$$d$$$.

the algorithm

Given $$$P(x)$$$ and $$$Q(x)$$$ of the linear recurrence, Bostan-Mori try to find the $$$k$$$-th term of linear recurrence with the following inductive structure.

base case, $$$k = 0$$$

From the equation $$$P(x) = A(x)Q(x)$$$, we know $$$p_0 = a_0q_0 = a_0 = P(0)$$$, so it is just $$$P(0)$$$.

inductive step, $$$k \ge 1$$$

Consider multiply $$$\frac{P(x)}{Q(x)}$$$ with $$$Q(-x)$$$ on both denominator and numerator, which give us $$$\frac{P(x)Q(-x)}{Q(x)Q(-x)}$$$, consider the $$$t$$$-th term of $$$Q(x)Q(-x)$$$ for some odd $$$t$$$, which is $$$\sum\limits_{i + j = t \newline j \text{ is even}} q_i q_j + \sum\limits_{i + j = t \newline j \text{ is odd}} q_i (-q_j)$$$, we can see that they cancel each other, thus it is $$$0$$$, which means $$$Q(x)Q(-x)$$$ is a polynomial with only even terms, and there exist some $$$V(x^2) = Q(x)Q(-x)$$$.

Since $$$V(x^2)$$$ is a even polynomial, if $$$k$$$ is even(odd), then odd(even) terms of $$$P(x)Q(-x)$$$ is not important. Why? Let $$$U(x) = P(x)Q(-x)$$$, then the new linear recurrence will be $$$\frac{U(x)}{V(x^2)}$$$, where $$$deg(U(x)) \le 2d - 1, deg(V(x^2)) = 2d$$$, by the above lemma, this is a linear recurrence with order $$$2d$$$, and let try to derive the recurrence relation from the definition of $$$Q(x)$$$.

$$$V(x^2) = \sum\limits_{i = 0}^d v_i x_i^2 = Q(x)Q(-x) = 1 - \sum\limits_{i = 1}^d c'_{2i} x^{2i}$$$

We can see that $$$c'$$$ is the coefficient of the new linear recurrence, and only have non-zero coefficient in even terms, so if we want to compute $$$k$$$-th term of linear recurrence, we only need to know $$$k - 2, k - 4, k - 6...$$$ terms, and the terms with another parity will be useless, so we only need even(odd) term of $$$P(x)$$$.

So let try to split $$$U(x)$$$ into two polynomial with odd/even terms, $$$U_e(x) = \sum\limits_{i = 0}^{d - 1}u_{2i} x^i, U_o = \sum\limits_{i = 0}^{d - 1}u_{2i + 1} x^i$$$, then we have $$$\frac{P(x)Q(-x)}{Q(x)Q(-x)} = \frac{U(x)}{V(x^2)} = \frac{U_e(x^2) + xU_o(x^2)}{V(x^2)}$$$.

Finally, consider case working for parity of $$$k$$$, we have

$$$[x^k]\frac{U_e(x^2) + xU_o(x^2)}{V(x^2)} = \begin{cases} [x^k]\frac{U_e(x^2)}{V(x^2)} & \text{if } k \text{ is even} \\ [x^k]\frac{xU_o(x^2)}{V(x^2)} & \text{if } k \text{ is odd} \end{cases} = \begin{cases} [x^{\frac{k}{2}}]\frac{U_e(x)}{V(x)} & \text{if } k \text{ is even} \\ [x^{\frac{k - 1}{2}}]\frac{U_o(x)}{V(x)} & \text{if } k \text{ is odd} \end{cases}$$$

the implementation (psudo-code) and complexity analysis

Actually, it is no need to force $$$q_0 = 1$$$, we can let $$$q_0$$$ be any non-zero value, the above inductive step will still hold, which just need an little modification at base case, instead of returning $$$P(0)$$$, return $$$\frac{P(0)}{Q(0)}$$$.

Bostan-Mori(P, Q, k)
  while(k >= 1)
    U(x) = P(x)Q(-x)
    if k is even
      P(x) = U_e(x)
    else
      P(x) = U_o(x)
    V(x^2) = Q(x)Q(-x)
    Q(x) = V(x)
    k = floor(k/2)
  return P(0) / Q(0)

For each phrase, all we need to do is just FFT/NTT $$$O(1)$$$ times, and since after each phrase, $$$k$$$ will be divide by $$$2$$$, so there are $$$O(\lg k)$$$ phrases, and the time complexity is $$$O(d\lg d \lg k)$$$, where $$$d$$$ is the order the linear recurrence.

Finally, here is my implementation, though it is not very fast, it works.

compare with other algorithm

  • using matrix exponentiation can compute $$$k$$$-th term of linear recurrence in $$$O(d^3 \lg k)$$$, if multiply matrix naively.

  • using kitamasa method can compute $$$k$$$-th term of linear recurrence in $$$O(d^2 \lg k)$$$, or $$$O(d \lg d \lg k)$$$ using FFT/NTT.

reference

A Simple and Fast Algorithm for Computing the N-th Term of a Linearly Recurrent Sequence (original paper)

線形漸化的数列のN項目の計算 (blog written by Ryuhei Mori, one of the creator of this algorithm, written in Japanese)

Full text and comments »

  • Vote: I like it
  • +123
  • Vote: I do not like it

By Misuki, history, 4 years ago, In English

Recenetly I was learning Berlekamp-Massey and applying it when our dp can be seen as a linear recurrence, if you don't know how it works, here is an simple description of it.(or a more detail description in this blog)

simple description

And the question arise when I encounter this problem 506E — Mr. Kitayuta's Gift, I write a dp solution below.

Spoiler

Apparently, we can see $$$dp[i]$$$ as a $$$|s|^2$$$ times $$$1$$$ vector, which means the size of transition matrix will be $$$|s|^2$$$ times $$$|s|^2$$$, so according to Cayley-Hamilton theorem, the linear recurrence of this dp will have at most $$$|s|^2$$$ terms, so if we compute the first $$$2|s|^2$$$ vectors of $$$dp[i] (i \le 2|s|^2)$$$ then plug them into Berlekamp-Massey, calculate $$$dp[(n + |s|) / 2]$$$, the time complexity will be $$$O(26|s|^4 + |s|(|s|^2 + |s|^2lg(n+|s|))$$$, which obviously will not fit in the TL.

But it turns out that it will only have about $$$350$$$ terms at most, which is far from $$$|s|^2$$$, and will fit in the TL, you can see my submission. Here comes the question, do we have any method to estimate how many terms a linear recurrence have? If we don't, then how to determine the number of terms we need to compute to plug into Berlekamp-Massey?

btw, sorry for my bad english.

Full text and comments »

  • Vote: I like it
  • +19
  • Vote: I do not like it

By Misuki, history, 5 years ago, In English

Today I was solving pG in this problem list. When I submit my code it seems alright at the first few test cases but turns out to be an judgement fail. Does someone know the reason behind it?

picture

Here is my code:

code

UPD: The problem has already be fixed, and the code has been rejudged :D

Full text and comments »

  • Vote: I like it
  • +14
  • Vote: I do not like it