---
id: 1230
lang: en
translation_of: index.md
source_sha256: ab356bf38f4234c800a4382f03519ca2e9365637c3d3c739d58504bca8ce59a6
title: "Möbius Inversion and Du's Sieve"
description: "Notes on what I learned studying Möbius inversion and Du's sieve, for the math problems of OI contests at provincial selection level and above."
tags: ["OI","Number Theory","Euler's Totient Function","Möbius Function","Möbius Inversion","Dirichlet","Dirichlet Convolution","Convolution","Arithmetic Functions"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "数论函数 rendered as “arithmetic function” throughout, 积性 as “multiplicative”, and 不完全(的)积性函数 as “not completely multiplicative”."
  - "二维运算 (Dirichlet convolution) rendered literally as “two-dimensional operation”."
  - "标号函数 (literally “label function”) rendered as “identity function”; 单位元 rendered as “identity element”."
  - "带入 (sic) read as 代入, “substitute”."
  - "Problem names: 约数个数和 rendered as “Sum of Divisor Counts” (as in the translation of post 1023), and 【模板】杜教筛（Sum） as “[Template] Du's Sieve (Sum)”."
  - "The ZAP-Queries statement keeps its odd grammar (作为FGD的同学，FGD希望得到你的帮助 → “As FGD's classmate, FGD hopes to get your help”)."
---

## Overview

Math problems in OI contests at the provincial team selection level and above are much harder than NOIp math problems. In this post I'll record some of what I took away from learning Möbius inversion and Du's sieve. Here is the outline of this post:

- Overview
- [Arithmetic Functions](#number-theory-function)
  - [Multiplicativity](#multiply-attribute)
  - [Dirichlet Convolution of Arithmetic Functions](#dirichlet-convolution)
  - Properties of Arithmetic Functions
    - [Basic Arithmetic Functions](#basic-function)
    - [Euler's Totient Function](#euler-function)
    - [The Möbius Function](#mobius-function)
    - [Divisor Functions](#divide-function)
- [Möbius Inversion](#mobius-inversion)
- [Du's Sieve](#dujiao-sieve)

<!--more-->

## <a id="number-theory-function"></a>Arithmetic Functions

**Arithmetic functions** are functions whose domain is the positive integers. Arithmetic functions have the following kinds of operations:

1. $(f+g)$: termwise addition of two arithmetic functions over the domain.
2. $n \cdot f$: multiplying an arithmetic function by a number
3. $(f*g)()$: Dirichlet convolution
4. $\dots$

Before introducing the arithmetic functions commonly used in OI, I'll first introduce the multiplicative property of arithmetic functions, which will be very useful later.

### <a id="multiply-attribute"></a>Multiplicativity

An arithmetic function has the **multiplicative property** if and only if $f(x) \cdot f(y) = f(xy), gcd(x, y) = 1$. Such functions are also called **multiplicative functions**. A class of functions that, for arbitrary $x, y$, satisfies $f(xy) = f(x)f(y)$ is called **completely multiplicative functions**. The multiplicative property of some functions can help us sieve out the function values during the linear sieve. For example, for Euler's totient function $\varphi(x)$, we can make a few changes to the linear sieve code to sieve out its values:

```cpp
void preprocess()
{
    phi[1] = 1;
    for (ll i = 2; i < MAX_N; i++)
    {
        if (!vis[i])
            prime[++tot] = i, phi[i] = i - 1;
        for (ll j = 1; j <= tot && 1ll * i * prime[j] < MAX_N; j++)
        {
            vis[i * prime[j]] = true;
            if (i % prime[j] == 0)
            {
                phi[i * prime[j]] = phi[i] * prime[j];
                break;
            }
            phi[i * prime[j]] = phi[i] * phi[prime[j]];
        }
    }
}
```

### <a id="dirichlet-convolution"></a>Dirichlet Convolution of Arithmetic Functions

Dirichlet convolution is a two-dimensional operation, usually written $f*g$, and the operation only makes sense when $f,g$ are multiplicative functions. Expanding this two-dimensional operation gives:

$$
f * g = \sum_{d|n} f(d)g(\frac{n}{d})
$$

So the result of a Dirichlet convolution is also a multiplicative function. Dirichlet convolution is just a shorthand for an operation, and it will come into play later in Du's sieve. Dirichlet convolution basically supports the four basic laws of operation:

1. Commutativity: $f*g = g*f$
2. Associativity: $f*(g*h) = (f*g)*h$
3. Multiplication by the identity element: $f*\epsilon = f$, where $\epsilon(x) = [x = 1]$, which is covered below.
4. Inverse under Dirichlet convolution: there always exists a $g$ such that $f * g = \epsilon$, and then $g$ is called the inverse of $f$.

### Properties of Arithmetic Functions

#### <a id="basic-function"></a>Basic Arithmetic Functions

- $id(x) = x$ is the identity function.
- $I(x) = 1$ is the $1$ function.
- $\epsilon(x) = [x = 1]$ is the identity element function.

Clearly, all of the above functions are completely multiplicative.

#### <a id="euler-function"></a>Euler's Totient Function: $\varphi(x)$

1. Meaning: the number of integers in $[1, n - 1]$ that are coprime to $n$.
2. Basic properties:
   1. Not completely multiplicative;
   2. For $\sum_{i = 1}^n i[gcd(i, n) = 1] = \frac{ [n = 1] + n\varphi(n) }{2}$;
3. Dirichlet-related properties:
   1. <a id="euler-dirichlet-1"></a>$\varphi * I = id$; written in the form of a Dirichlet convolution: $\sum_{d|x} \varphi(d) \cdot 1 = id(n) = n$.
   2. $\varphi = \mu * id$, **Proof:**
      $$
      \begin{aligned} \varphi * I &= id = id * \epsilon \\ &= id * \mu * I = id * \mu \end{aligned}
      $$
      The proof of $\epsilon = id * \mu$ is [here](#mobius-attr-1-proof).
4. Linear sieve code:

```cpp
void preprocess()
{
    phi[1] = 1;
    for (ll i = 2; i < MAX_N; i++)
    {
        if (!vis[i])
            prime[++tot] = i, phi[i] = i - 1;
        for (ll j = 1; j <= tot && 1ll * i * prime[j] < MAX_N; j++)
        {
            vis[i * prime[j]] = true;
            if (i % prime[j] == 0)
            {
                phi[i * prime[j]] = phi[i] * prime[j];
                break;
            }
            phi[i * prime[j]] = phi[i] * phi[prime[j]];
        }
    }
}
```

#### <a id="mobius-function"></a>The Möbius Function: $\mu(x)$

1. Meaning: it serves as the inclusion–exclusion coefficient in Möbius inversion.
2. Precise definition: for a number $x = \prod_{i = 1}^m p_i^{c_i}$, we have:\
   $\mu(x) = \begin{cases} 1, x = 1 \\ (-1)^m, \forall c_i = 1 \\ 0, others \end{cases}$
3. Basic properties:
   1. <a id="mobius-attribute-1"></a>$\sum_{d|x} \mu(d) = [x = 1]$
   2. The Möbius function is not completely multiplicative, and its values can be sieved out with the linear sieve.
4. Properties related to Dirichlet convolution:
   1. <a id="mobius-dirichlet-1"></a>$\mu * I = \epsilon$; expanded, this is just basic property 1.
   2. $\varphi = \mu * id$, **Proof:**
      $$
      \varphi * I = id = id * \epsilon = id * \mu * I = id * \mu
      $$
      The proof of $\epsilon = id * \mu$ is [here](#mobius-attr-1-proof).
5. Linear sieve code:

```cpp
mu[1] = 1;
for (register int i = 2; i < MAX_N; i++)
{
    if (!vis[i])
        prime[++tot] = i, mu[i] = -1;
    for (register int j = 1; j <= tot && prime[j] * i < MAX_N; j++)
    {
        vis[i * prime[j]] = true;
        if (i % prime[j] == 0)
        {
            mu[i * prime[j]] = 0;
            break;
        }
        else
            mu[i * prime[j]] = -mu[i];
    }
}
```

#### <a id="divide-function"></a>Divisor-Related Functions: $\sigma(x)$

1. Definition: $\sigma(x)_k = \sum_{d|x} d^k$, where for $k$ equal to $1$ it is the sum of divisors.
2. Basic properties
   1. Not completely multiplicative for any $k$.
   2. In the case $k=0$, $\sigma_0$ is really just the number of divisors; factorizing a number as $x = \prod_{i = 1}^m p_{i}^{c_i}$, $\sigma_0(x) = \prod_{i = 1}^m (c_i + 1)$
   3. When $k=0$, $\sigma_0 (xy) = \sum_{i|x} \sum_{j|y} [gcd(i, j) = 1]$; for the detailed proof see the Extended section of [this solution](https://siyuan.blog.luogu.org/solution-p3327).
3. Linear sieve code (for the case $k = 0$):

```cpp
const int MAX_N = 1e6 + 2000;

int prime[MAX_N], tot, d0[MAX_N], num[MAX_N];
bool vis[MAX_N];

void sieve()
{
    d0[1] = 1;
    for (int i = 2; i < MAX_N; i++)
    {
        if (!vis[i])
            prime[++tot] = i, d0[i] = 2, num[i] = 1;
        for (int j = 1; j <= tot && 1LL * prime[j] * i < MAX_N; j++)
        {
            vis[i * prime[j]] = true;
            if (i % prime[j] == 0)
            {
                num[i * prime[j]] = num[i] + 1;
                d0[i * prime[j]] = d0[i] / (num[i] + 1) * (num[i * prime[j]] + 1);
                break;
            }
            d0[i * prime[j]] = d0[i] * d0[prime[j]];
            num[i * prime[j]] = 1;
        }
    }
}
```

Let me explain the principle behind this approach. We split it into cases:

1. In the case $i \bmod prime[j] > 0$, the number $i$ itself does not contain the prime factor $prime[j]$, so we can just multiply multiplicatively, i.e.
   $$
   \sigma_0 (i) = \prod_{i = 1}^m (c_i + 1) \to \sigma_0 (i \times prime[j]) = \prod_{i = 1}^{m + 1} (c_i + 1)
   $$
   and record the exponent of the smallest prime factor.
2. In the case $i \bmod prime[j] = 0$, the number $i$ contains the prime factor $prime[j]$, so we first have to cancel the earlier factor $( c_{prime[j]}' + 1 )$ and replace it with $( c_{prime[j]} + 1 ) = ( c_{prime[j]}' + 2 )$, and record the exponent of the current smallest prime factor.

## <a id="mobius-inversion"></a>Möbius Inversion

Möbius inversion is a result of inclusion–exclusion, and it can directly simplify some expressions. First, let me write down a classic result:

$$
\begin{gathered} \text{For }F(n) = \sum_{d|n} f(d), \\ f(n) = \sum_{d|n} \mu(\frac{n}{d}) F(d) \end{gathered}
$$

<a id="mobius-attr-1-proof"></a>First, to prove this result, let's first get to know [basic property 1](#mobius-attribute-1), mentioned above:

$$
\sum_{d|x} \mu(d) = [x = 1]
$$

**Basic property 1** is often used in Möbius inversion problems, and it is <strong>very important.</strong> In many problems we can consider directly replacing the identity element to do the inversion. Let's prove it first:

**Proof** If $n=1$, it obviously holds: by the definition of the Möbius function, the function value at $1$ equals $1$. So we only need to prove the other cases, that is, the case $\sum_{d|n} \mu(d) = 0$. For a number $n=p_1^{c_1} p_2^{c_2} p_3^{c_3} \dots p_m^{c_m},d=p_1^{k_1} p_2^{k_2} p_3^{k_3} \dots p_m^{k_m}$, we can get:
$$
\sum_{d|n} \mu(d) = \sum_{r = 1}^m {m \choose r} (-1)^r
$$
Then, by the binomial theorem, we have:
$$
(x+y)^n = \sum_{i=1}^{n} {n \choose i} x^i y^{n-i}
$$
So we transform the expression above a bit, that is, let $x=-1,y=1$, and work it out:
$$
\sum_{d|n} \mu(d) = (-1+1)^m = 0
$$
Done proving.

Having proved that, we find that the result mentioned at the start can actually be turned into convolution form and proved that way.

**Proof**

$$
\begin{aligned} F(n) &= \sum_{d|n} f(d) \\ f(n) &= \sum_{d|n} \mu(\frac{n}{d}) F(d) \end{aligned}
$$

First, look at condition 1, that is:

$$
F(n) = \sum_{d|n} f(d), F = f * I
$$

Using the result we just proved above, consider the inverse of $I$:

$$
\begin{aligned} \sum_{d|x} \mu(d) &= [x = 1] \\ I*\mu &= \epsilon \end{aligned}
$$

We find that the equation above holds if and only if $I^{-1} = \mu$.

$$
\begin{aligned} f &= F * I^{-1} = F * \mu \\ f(n) &= \sum_{d|n} I^{-1}(\frac{n}{d}) F(d) \\ f(n) &= \sum_{d|n} (\frac{n}{d}) F(d) \end{aligned}
$$

End of proof.

Let's try working through two example problems to deepen our understanding:

### Example A: [[POI2007]ZAP-Queries](https://www.lydsy.com/JudgeOnline/problem.php?id=1101)

> FGD is cracking a password, and he needs to answer many similar questions: for given integers a, b and d, how many pairs of positive integers x, y are there such that x<=a, y<=b, and gcd(x,y)=d. As FGD's classmate, FGD hopes to get your help.

First, write down the expression for the answer:

$$
\sum_{i = 1}^a \sum_{j = 1}^b [gcd(x, y) = d]
$$

Consider dividing out $d$ first; this thing is annoying.

$$
\sum_{i = 1}^{\lfloor \frac{a}{d} \rfloor} \sum_{j = 1}^{\lfloor \frac{b}{d} \rfloor} [gcd(i, j) = 1]
$$

We notice the expression at the end looks familiar, namely: $\sum_{d|x} \mu(d) = [x = 1]$. Consider substituting it in:

$$
\sum_{i = 1}^{\lfloor \frac{a}{d} \rfloor} \sum_{j = 1}^{\lfloor \frac{b}{d} \rfloor} \sum_{h|gcd(i, j)} \mu(h)
$$

We can consider replacing that $h|gcd(i, j)$ at the very end:

$$
\sum_{i = 1}^{\lfloor \frac{a}{d} \rfloor} \sum_{j = 1}^{\lfloor \frac{b}{d} \rfloor} \sum_{h|i, h|j} \mu(h)
$$

Change the enumerated term to $d$:

$$
\sum_{h = 1}^{min(a, b)} \mu(h) \sum_{i = 1}^{\lfloor \frac{a}{hd} \rfloor} \sum_{j = 1}^{\lfloor \frac{b}{hd} \rfloor} 1
$$

Let $T = hd$ and enumerate again:

$$
\sum_{T = 1}^{min(a, b)} \mu(\lfloor \frac{T}{d} \rfloor) \lfloor \frac{a}{T} \rfloor \lfloor \frac{b}{T} \rfloor
$$

Precompute the prefix sums of $\mu()$ and then we can do divisor-block decomposition; the time complexity is on the square-root level.

### Example B: [[SDOI2015] Sum of Divisor Counts](https://www.luogu.org/problemnew/show/P3327)

> Let d(x) be the number of divisors of x. Given N and M, find $\sum_{i = 1}^N \sum_{j = 1}^M d(ij)$, where $d(x)$ is the number of divisors of a number.

Let's just use $\sigma_0$ in place of $d$. The expression for the answer is already written clearly enough; see the basic property of the divisor function from before:

$$
\sigma_0 (xy) = \sum_{i|x} \sum_{j|y} [gcd(i, j) = 1]
$$

We can consider swapping this property straight into the expression:

$$
\begin{aligned} & \sum_{i = 1}^N \sum_{j = 1}^M \sum_{x|i} \sum_{y|j} [gcd(x, y) = 1] \\ =&
\sum_{i = 1}^N \sum_{j = 1}^M [gcd(x, y) = 1] \lfloor \frac{N}{i} \rfloor \lfloor \frac{M}{j} \rfloor \end{aligned}
$$

We notice the identity element form $[gcd(x, y) = 1]$, so consider applying Möbius inversion:

$$
\begin{aligned} &= \sum_{i = 1}^N \sum_{j = 1}^M \sum_{x|i} \sum_{y|j} \sum_{d | gcd(x, y)} \mu(d) \\ &= \sum_{i = 1}^N \sum_{j = 1}^M \sum_{x|i} \sum_{y|j} \sum_{d|x, d|y} \mu(d) \end{aligned}
$$

We find that we can enumerate $d$ and at the same time eliminate two of the sums:

$$
\begin{aligned} &= \sum_{i = 1}^N \sum_{j = 1}^M \lfloor \frac{N}{i} \rfloor \lfloor \frac{M}{j} \rfloor \sum_{d|gcd(i, j)} \mu(d) \\ &= \sum_{i = 1}^N \sum_{j = 1}^M \lfloor \frac{N}{i} \rfloor \lfloor \frac{M}{j} \rfloor \sum_{d = 1}^{min(n, m)} \mu(d) [d|gcd(i, j)] \\ &= \sum_{d = 1}^{min(n, m)} \mu(d) \sum_{i = 1}^N \sum_{j = 1}^M \lfloor \frac{N}{i} \rfloor \lfloor \frac{M}{j} \rfloor [d|gcd(i, j)] \\ &= \sum_{d = 1}^{min(n, m)} \mu(d) \sum_{i = 1}^{\frac{N}{d}} \sum_{j = 1}^{\frac{M}{d}} \lfloor \frac{N}{di} \rfloor \lfloor \frac{M}{dj} \rfloor \end{aligned}
$$

We find that the last two sums can be considered separately, and then they can be solved in $O(1)$ time with high-school math. For the full simplification, see: [P3327: [SDOI2015] Sum of Divisor Counts — Solution](../1023-p3327-sdoi2015-divisor-count-sum/index.en.html)

## <a id="dujiao-sieve"></a>Du's Sieve

Let's look back at the ZAP-Queries problem: we needed to precompute the prefix sums of $\mu$. Fortunately, in that problem the data range was not large, so we could finish it directly with an $O(n)$ linear sieve. However, sometimes we need to handle data with $n \geq 10^{8}$ and above, and the linear sieve will time out. That is when Du's sieve is needed.

Du's sieve applies to multiplicative functions, and the time complexity of the preprocessing is at most $O(n^{\frac{2}{3}})$.

### Principle

Say we want to sieve a multiplicative function $f$. We can sieve it with the following <span class="slang" data-zh="套路" data-gloss="tàolù, “routine, set pattern”. OI slang for a standard, well-known trick that works on a whole class of problems.">standard trick</span>:

First, use the linear sieve to compute the prefix sums of the first $n^{\frac{2}{3}}$. Then let $h = f * g$, where $h, g$ are both multiplicative functions. We need to find, based on the specific $f$, the other $h, g$ that make this hold. Once we've found them, we expand the convolution form (where we let $S(n) = \sum_{i = 1}^n f(i)$):

$$
\begin{aligned} h &= f * g \to h(n) = \sum_{d|n} f(\frac{n}{d}) g(d) \\ \sum_{i = 1}^n h(i) &= \sum_{i = 1}^n \sum_{d|i} f(\frac{i}{d}) g(d) \\ &= \sum_{d = 1}^n g(d) \sum_{i = 1}^{\frac{n}{d}} f(i) \\ &= \sum_{d = 1}^n g(d) S(\lfloor \frac{n}{d} \rfloor) \\ &= g(1) S(n) + \sum_{d = 2}^n g(d) S(\lfloor \frac{n}{d} \rfloor) \\ g(1) S(n) &= \sum_{i = 1}^n h(i) - \sum_{d = 2}^n g(d) S(\lfloor \frac{n}{d} \rfloor) \end{aligned}
$$

Usually the $h()$ you derive when solving won't be very <span class="slang" data-zh="毒瘤" data-gloss="dúliú, “tumor”. A problem (or problem setter) that is painfully hard, tedious, or full of traps.">nasty</span>, so we focus on the latter part: we find that $S(\lfloor \frac{n}{d} \rfloor)$ appears, so we can consider doing divisor-block decomposition and recursing down. Along the way, it's best to use an unordered_map to store the values already known, and then it's pretty excellent.

### Example: [P4213 [Template] Du's Sieve (Sum)](https://www.luogu.org/problemnew/show/P4213)

Readers should pay special attention to the template problem and the derivations for the following two functions, and try their best to understand how the $\mu$ and $\varphi$ functions are derived into expressions suited to Du's sieve.

First, let's think about $ans_1$, that is, $\sum_{i = 1}^N \varphi(i)$. First let $S(n) = \sum_{i = 1}^N \varphi(i)$, to make the derivation convenient. Following the <span class="slang" data-zh="套路" data-gloss="tàolù, “routine, set pattern”. OI slang for a standard, well-known trick that works on a whole class of problems.">standard trick</span>, first write out a convolution expression:

$$
\begin{gathered} h = \varphi * g \\ h(n) = \sum_{d|n} \varphi(\frac{n}{d}) g(d) \\ \sum_{i = 1}^n h(i) = \sum_{i = 1}^n \sum_{d|i} \varphi(\frac{i}{d}) g(d) \end{gathered}
$$

Consider swapping the order of $i$ and $d$ in the latter expression:

$$
\begin{aligned} h &= \varphi * g \\ h(n) &= \sum_{d|n} \varphi(\frac{n}{d}) g(d) \\ \sum_{i = 1}^n h(i) &= \sum_{i = 1}^n \sum_{d|i} \varphi(\frac{i}{d}) g(d) \\ &= \sum_{d = 1}^n g(d) \sum_{i = 1}^n \varphi(\frac{i}{d})[d|i] \\ &= \sum_{i = 1}^n \sum_{d|i} \varphi(\frac{i}{d}) g(d) \\ &= \sum_{d = 1}^n g(d) \sum_{i = 1}^{\lfloor \frac{n}{d} \rfloor} \varphi(i) \\ &= \sum_{d = 1}^n g(d) S(\lfloor \frac{n}{d} \rfloor) \end{aligned}
$$

We've already got it out; just move the terms over:

$$
\begin{aligned} \sum_{i = 1}^n h(i) &= \sum_{d = 1}^n g(d) S(\lfloor \frac{n}{d} \rfloor) \\ g(1) S(n) &= \sum_{i = 1}^n h(i) - \sum_{d = 2}^n g(d)S(\lfloor \frac{n}{d} \rfloor) \end{aligned}
$$

Now let's consider what $h, g$ are. Let's go back to the original expression:

$$
h = \varphi * g
$$

This brings to mind [a property](#euler-dirichlet-1), $\varphi * I = id$, i.e. $h = id, g = I$. Substituting into the original expression:

$$
S(n) = \frac{n(n + 1)}{2} - \sum_{d = 2}^n S(\lfloor \frac{n}{d} \rfloor)
$$

Now think about $ans_2$. In the same way, we can set up functions and derive the convolution, and we get:

$$
g(1) S(n) = \sum_{i = 1}^n h(i) - \sum_{d = 2}^n g(d)S(\lfloor \frac{n}{d} \rfloor)
$$

Now think about how to choose $h, g$. Consider the [convolution property](#mobius-dirichlet-1) of the $\mu$ function mentioned before:

$$
\mu * I = \epsilon
$$

Just substitute it in; I won't go over it again. Next is the code implementation:

```cpp
// P4213.cpp
#include <bits/stdc++.h>
#define reg register
#define ll long long
using namespace std;

const int MAX_N = 6e6 + 200;
unordered_map<int, ll> ump_phi, ump_mu;
ll phi[MAX_N], mu[MAX_N], prime[MAX_N], tot;
bool vis[MAX_N];

ll sieve_phi(int num)
{
    if (num < MAX_N)
        return phi[num];
    if (ump_phi.count(num))
        return ump_phi[num];
    ll ans = (unsigned long long)num * (num + 1) / 2;
    for (reg int i = 2, gx; i <= num; i = gx + 1)
    {
        gx = num / (num / i);
        ans -= (gx - i + 1) * sieve_phi(num / i);
    }
    return ump_phi[num] = ans;
}

ll sieve_mu(int num)
{
    if (num < MAX_N)
        return mu[num];
    if (ump_mu.count(num))
        return ump_mu[num];
    ll ans = 1;
    for (reg int i = 2, gx; i <= num; i = gx + 1)
    {
        gx = num / (num / i);
        ans -= (gx - i + 1) * sieve_mu(num / i);
    }
    return ump_mu[num] = ans;
}

void preprocess()
{
    tot = 0, phi[1] = mu[1] = 1;
    for (reg int i = 2; i < MAX_N; i++)
    {
        if (!vis[i])
            prime[++tot] = i, mu[i] = -1, phi[i] = i - 1;
        for (reg int j = 1; j <= tot && i * prime[j] < MAX_N; j++)
        {
            vis[i * prime[j]] = true;
            if (i % prime[j] == 0)
            {
                mu[prime[j] * i] = 0, phi[prime[j] * i] = prime[j] * phi[i];
                break;
            }
            phi[i * prime[j]] = phi[prime[j]] * phi[i];
            mu[i * prime[j]] = -mu[i];
        }
    }
    for (reg int i = 2; i < MAX_N; i++)
        phi[i] += phi[i - 1], mu[i] += mu[i - 1];
}

int main()
{
    int q;
    preprocess();
    scanf("%d", &q);
    while (q--)
    {
        ll num;
        scanf("%lld", &num);
        printf("%lld %lld\n", sieve_phi(num), sieve_mu(num));
    }
    return 0;
}
```
