Dirichlet convolution and Möbius inversion formula
Golovanov399
Moscow Institute of Physics and Technology
2021
1/36
Problem
Given n ≤ 105 positive integers a1 , . . . , an (1 ≤ ai ≤ 106 ), and
also number m ≤ 105 . For each i from 1 to m print the number of
non-empty subsequences of {ai }ni=1 with LCM equal to i, modulo
998 244 353.
2/36
Problem
Given n ≤ 105 positive integers a1 , . . . , an (1 ≤ ai ≤ 106 ), and
also number m ≤ 105 . For each i from 1 to m print the number of
non-empty subsequences of {ai }ni=1 with LCM equal to i, modulo
998 244 353.
Thoughts.
What does it mean that a subsequence has its LCM equal to, say,
12?
2/36
Problem
Given n ≤ 105 positive integers a1 , . . . , an (1 ≤ ai ≤ 106 ), and
also number m ≤ 105 . For each i from 1 to m print the number of
non-empty subsequences of {ai }ni=1 with LCM equal to i, modulo
998 244 353.
Thoughts.
What does it mean that a subsequence has its LCM equal to, say,
12? It at least means that all its members are factors of 12, so a
first approach would be the number of subsequences containing
only factors of 12. If c12 is the number of factors of 12, then there
are 2c12 − 1 such subsequences.
2/36
Problem
Given n ≤ 105 positive integers a1 , . . . , an (1 ≤ ai ≤ 106 ), and
also number m ≤ 105 . For each i from 1 to m print the number of
non-empty subsequences of {ai }ni=1 with LCM equal to i, modulo
998 244 353.
Thoughts.
What does it mean that a subsequence has its LCM equal to, say,
12? It at least means that all its members are factors of 12, so a
first approach would be the number of subsequences containing
only factors of 12. If c12 is the number of factors of 12, then there
are 2c12 − 1 such subsequences. However, we need to exclude all
subsets of factors of 6, as their LCM is at most 6; the same about
4. Let’s adjust the answer: 2c12 − 2c6 − 2c4 + 1.
2/36
Problem
Given n ≤ 105 positive integers a1 , . . . , an (1 ≤ ai ≤ 106 ), and
also number m ≤ 105 . For each i from 1 to m print the number of
non-empty subsequences of {ai }ni=1 with LCM equal to i, modulo
998 244 353.
Thoughts.
What does it mean that a subsequence has its LCM equal to, say,
12? It at least means that all its members are factors of 12, so a
first approach would be the number of subsequences containing
only factors of 12. If c12 is the number of factors of 12, then there
are 2c12 − 1 such subsequences. However, we need to exclude all
subsets of factors of 6, as their LCM is at most 6; the same about
4. Let’s adjust the answer: 2c12 − 2c6 − 2c4 + 1. But now each
subsequence of factors of 2 (that is, subsequence containing only
ones and twos) is added once and subtracted twice, so we need to
add them again: 2c12 − 2c6 − 2c4 + 2c2 .
2/36
Problem
Given n ≤ 105 positive integers a1 , . . . , an (1 ≤ ai ≤ 106 ), and
also number m ≤ 105 . For each i from 1 to m print the number of
non-empty subsequences of {ai }ni=1 with LCM equal to i, modulo
998 244 353.
Thoughts.
What does it mean that a subsequence has its LCM equal to, say,
12? It at least means that all its members are factors of 12, so a
first approach would be the number of subsequences containing
only factors of 12. If c12 is the number of factors of 12, then there
are 2c12 − 1 such subsequences. However, we need to exclude all
subsets of factors of 6, as their LCM is at most 6; the same about
4. Let’s adjust the answer: 2c12 − 2c6 − 2c4 + 1. But now each
subsequence of factors of 2 (that is, subsequence containing only
ones and twos) is added once and subtracted twice, so we need to
add them again: 2c12 − 2c6 − 2c4 + 2c2 . Did we take everything into
account?..
2/36
Möbius inversion in a nutshell
g(0)
g(1)
g(2)
g(3)
g(4)
g(5)
g(n) =
g(6)
g(7)
g(8)
g(9) g(10)
Pn
i=0 f (i)
3/36
Möbius inversion in a nutshell
g(0)
g(1)
g(2)
g(3)
g(4)
g(5)
g(n) =
g(0)
g(1)
g(2)
g(3)
g(4)
g(6)
g(7)
g(8)
g(9) g(10)
g(7)
g(8)
g(9) g(10)
Pn
i=0 f (i)
g(5)
g(6)
f (n) = g(n) − g(n − 1)
3/36
Möbius inversion in a nutshell
g(0, 4) g(1, 4) g(2, 4) g(3, 4) g(4, 4) g(5, 4) g(6, 4) g(7, 4) g(8, 4) g(9, 4) g(10, 4)
g(0, 3) g(1, 3) g(2, 3) g(3, 3) g(4, 3) g(5, 3) g(6, 3) g(7, 3) g(8, 3) g(9, 3) g(10, 3)
g(0, 2) g(1, 2) g(2, 2) g(3, 2) g(4, 2) g(5, 2) g(6, 2) g(7, 2) g(8, 2) g(9, 2) g(10, 2)
g(0, 1) g(1, 1) g(2, 1) g(3, 1) g(4, 1) g(5, 1) g(6, 1) g(7, 1) g(8, 1) g(9, 1) g(10, 1)
g(0, 0) g(1, 0) g(2, 0) g(3, 0) g(4, 0) g(5, 0) g(6, 0) g(7, 0) g(8, 0) g(9, 0) g(10, 0)
g(n, m) =
Pn
i=0
Pm
j=0 f (i, j)
4/36
Möbius inversion in a nutshell
g(0, 4) g(1, 4) g(2, 4) g(3, 4) g(4, 4) g(5, 4) g(6, 4) g(7, 4) g(8, 4) g(9, 4) g(10, 4)
g(0, 3) g(1, 3) g(2, 3) g(3, 3) g(4, 3) g(5, 3) g(6, 3) g(7, 3) g(8, 3) g(9, 3) g(10, 3)
g(0, 2) g(1, 2) g(2, 2) g(3, 2) g(4, 2) g(5, 2) g(6, 2) g(7, 2) g(8, 2) g(9, 2) g(10, 2)
g(0, 1) g(1, 1) g(2, 1) g(3, 1) g(4, 1) g(5, 1) g(6, 1) g(7, 1) g(8, 1) g(9, 1) g(10, 1)
g(0, 0) g(1, 0) g(2, 0) g(3, 0) g(4, 0) g(5, 0) g(6, 0) g(7, 0) g(8, 0) g(9, 0) g(10, 0)
f (n, m) = g(n, m) − g(n − 1, m) − g(n, m − 1) + g(n − 1, m − 1)
5/36
Möbius inversion in a nutshell
g(20 34 ) g(21 34 ) g(22 34 ) g(23 34 ) g(24 34 ) g(25 34 ) g(26 34 ) g(27 34 ) g(28 34 ) g(29 34 )g(210 34 )
g(20 33 ) g(21 33 ) g(22 33 ) g(23 33 ) g(24 33 ) g(25 33 ) g(26 33 ) g(27 33 ) g(28 33 ) g(29 33 )g(210 33 )
g(20 32 ) g(21 32 ) g(22 32 ) g(23 32 ) g(24 32 ) g(25 32 ) g(26 32 ) g(27 32 ) g(28 32 ) g(29 32 )g(210 32 )
g(20 31 ) g(21 31 ) g(22 31 ) g(23 31 ) g(24 31 ) g(25 31 ) g(26 31 ) g(27 31 ) g(28 31 ) g(29 31 )g(210 31 )
g(20 30 ) g(21 30 ) g(22 30 ) g(23 30 ) g(24 30 ) g(25 30 ) g(26 30 ) g(27 30 ) g(28 30 ) g(29 30 )g(210 30 )
Pn Pm
i j
i=0
j=0 f (2 3 )
n−1 m
n m−1
g(2n 3m ) =
n m
n m
f (2 3 ) = g(2 3 ) − g(2
3 ) − g(2 3
) + g(2n−1 3m−1 )
6/36
Möbius inversion in a nutshell
g(20 34 ) g(21 34 ) g(22 34 ) g(23 34 ) g(24 34 ) g(25 34 ) g(26 34 ) g(27 34 ) g(28 34 ) g(29 34 )g(210 34 )
g(20 33 ) g(21 33 ) g(22 33 ) g(23 33 ) g(24 33 ) g(25 33 ) g(26 33 ) g(27 33 ) g(28 33 ) g(29 33 )g(210 33 )
g(20 32 ) g(21 32 ) g(22 32 ) g(23 32 ) g(24 32 ) g(25 32 ) g(26 32 ) g(27 32 ) g(28 32 ) g(29 32 )g(210 32 )
g(20 31 ) g(21 31 ) g(22 31 ) g(23 31 ) g(24 31 ) g(25 31 ) g(26 31 ) g(27 31 ) g(28 31 ) g(29 31 )g(210 31 )
g(20 30 ) g(21 30 ) g(22 30 ) g(23 30 ) g(24 30 ) g(25 30 ) g(26 30 ) g(27 30 ) g(28 30 ) g(29 30 )g(210 30 )
Pn Pm
i j
i=0
j=0 f (2 3 )
n−1 m
n m−1
g(2n 3m ) =
n m
n m
f (2 3 ) = g(2 3 ) − g(2
3 ) − g(2 3
) + g(2n−1 3m−1 )
We are going to develop this theory and generalize it on “higher
dimensions”.
6/36
Convolutions in general
What is a convolution?
7/36
Convolutions in general
What is a convolution?
Some of them:
7/36
Convolutions in general
What is a convolution?
Some of them:
▶ Polynomial multiplication is a convolution: (ai ), (bi ) → (ci ),
where
X
ci =
aj bk .
j+k=i
7/36
Convolutions in general
What is a convolution?
Some of them:
▶ Polynomial multiplication is a convolution: (ai ), (bi ) → (ci ),
where
X
ci =
aj bk .
j+k=i
▶ Bitwise and convolution: (ai ), (bi ) → (ci ), where
X
ci =
aj bk .
j & k=i
7/36
Convolutions in general
What is a convolution?
Some of them:
▶ Polynomial multiplication is a convolution: (ai ), (bi ) → (ci ),
where
X
ci =
aj bk .
j+k=i
▶ Bitwise and convolution: (ai ), (bi ) → (ci ), where
X
ci =
aj bk .
j & k=i
▶ Bitwise xor convolution: (ai ), (bi ) → (ci ), where
X
ci =
aj bk .
j⊕k=i
7/36
Convolutions in general
What is a convolution?
Some of them:
▶ Polynomial multiplication is a convolution: (ai ), (bi ) → (ci ),
where
X
ci =
aj bk .
j+k=i
▶ Bitwise and convolution: (ai ), (bi ) → (ci ), where
X
ci =
aj bk .
j & k=i
▶ Bitwise xor convolution: (ai ), (bi ) → (ci ), where
X
ci =
aj bk .
j⊕k=i
▶ etc.
7/36
Convolutions in general
If we generalize this:
If we have two functions f, g : (G, ·) → R (G is a set, and · is an
operation over this set. It can be (Z≥0 , +), (Z2k , ⊕) or something
else. R can be replaced by Z998 244 353 or smth), then
def
(f ∗ g)(i) =
X
f (j)g(k).
j·k=i
8/36
Convolutions in general
Indeed,
9/36
Convolutions in general
Indeed,
▶ If (G, ·) = (Z≥0 , +), then
(f ∗ g)(i) =
X
f (j)g(k).
j+k=i
9/36
Convolutions in general
Indeed,
▶ If (G, ·) = (Z≥0 , +), then
(f ∗ g)(i) =
X
f (j)g(k).
j+k=i
▶ If (G, ·) = (Z2k , & ), then
(f ∗ g)(i) =
X
f (j)g(k).
j & k=i
9/36
Convolutions in general
Indeed,
▶ If (G, ·) = (Z≥0 , +), then
(f ∗ g)(i) =
X
f (j)g(k).
j+k=i
▶ If (G, ·) = (Z2k , & ), then
(f ∗ g)(i) =
X
f (j)g(k).
j & k=i
▶ If (G, ·) = (Z2k , ⊕), then
(f ∗ g)(i) =
X
f (j)g(k).
j⊕k=i
9/36
Dirichlet convolution
Definition
Dirichlet convolution of two functions f, g : N → R
10/36
Dirichlet convolution
Definition
Dirichlet convolution of two functions f, g : N → R (or → Zmod )
10/36
Dirichlet convolution
Definition
Dirichlet convolution of two functions f, g : N → R (or → Zmod ) is
a convolution for (G, ·) = (N, ×):
10/36
Dirichlet convolution
Definition
Dirichlet convolution of two functions f, g : N → R (or → Zmod ) is
a convolution for (G, ·) = (N, ×):
(f ∗ g)(n) =
X
f (a)g(b)
ab=n
10/36
Dirichlet convolution
Definition
Dirichlet convolution of two functions f, g : N → R (or → Zmod ) is
a convolution for (G, ·) = (N, ×):
n
X
X
f (d)g
.
(f ∗ g)(n) =
f (a)g(b) =
d
ab=n
d|n
10/36
Dirichlet convolution
Definition
Dirichlet convolution of two functions f, g : N → R (or → Zmod ) is
a convolution for (G, ·) = (N, ×):
n
X
X
f (d)g
.
(f ∗ g)(n) =
f (a)g(b) =
d
ab=n
d|n
From now on we assume that ∗ is the Dirichlet convolution.
10/36
Which functions may be useful?
The following functions are usually considered when dealing with
the convolution/inversion formula:
11/36
Which functions may be useful?
The following functions are usually considered when dealing with
the convolution/inversion formula:
▶ id(n) ≡ n,
11/36
Which functions may be useful?
The following functions are usually considered when dealing with
the convolution/inversion formula:
▶ id(n) ≡ n,
▶ 1(n) ≡ 1,
11/36
Which functions may be useful?
The following functions are usually considered when dealing with
the convolution/inversion formula:
▶ id(n) ≡ n,
▶ 1(n) ≡ 1,
▶ 0(n) ≡ 0,
11/36
Which functions may be useful?
The following functions are usually considered when dealing with
the convolution/inversion formula:
▶ id(n) ≡ n,
▶ 1(n) ≡ 1,
▶ 0(n) ≡ 0,
▶ σ0 (n) ≡ #{d | n}
11/36
Which functions may be useful?
The following functions are usually considered when dealing with
the convolution/inversion formula:
▶ id(n) ≡ n,
▶ 1(n) ≡ 1,
▶ 0(n) ≡ 0,
P
▶ σ0 (n) ≡ #{d | n} = d|n d0 ,
P
▶ σ1 (n) ≡ d|n d,
11/36
Which functions may be useful?
The following functions are usually considered when dealing with
the convolution/inversion formula:
▶ id(n) ≡ n,
▶ 1(n) ≡ 1,
▶ 0(n) ≡ 0,
P
▶ σ0 (n) ≡ #{d | n} = d|n d0 ,
P
▶ σ1 (n) ≡ d|n d,
(
1, n = 1,
▶ χ1 (n) ≡ [n = 1] =
0, otherwise.
11/36
Which functions may be useful?
The following functions are usually considered when dealing with
the convolution/inversion formula:
▶ id(n) ≡ n,
▶ 1(n) ≡ 1,
▶ 0(n) ≡ 0,
P
▶ σ0 (n) ≡ #{d | n} = d|n d0 ,
P
▶ σ1 (n) ≡ d|n d,
(
1, n = 1,
▶ χ1 (n) ≡ [n = 1] =
0, otherwise.
▶ Euler totient function:
φ(n) ≡ #{a ≤ n : gcd(a, n) = 1)},
▶ Möbius function:
(
0,
n is not squarefree,
µ(n) ≡
(−1)k , n has k distinct prime factors.
11/36
Properties
▶ f ∗ g = g ∗ f,
12/36
Properties
▶ f ∗ g = g ∗ f,
▶ (f ∗ g) ∗ h = f ∗ (g ∗ h),
12/36
Properties
▶ f ∗ g = g ∗ f,
▶ (f ∗ g) ∗ h = f ∗ (g ∗ h),
▶ f ∗ (g + h) = f ∗ g + f ∗ h,
12/36
Properties
▶ f ∗ g = g ∗ f,
▶ (f ∗ g) ∗ h = f ∗ (g ∗ h),
▶ f ∗ (g + h) = f ∗ g + f ∗ h,
▶ f ∗ χ1 = f ,
12/36
Properties
▶ f ∗ g = g ∗ f,
▶ (f ∗ g) ∗ h = f ∗ (g ∗ h),
▶ f ∗ (g + h) = f ∗ g + f ∗ h,
▶ f ∗ χ1 = f ,
▶ f ∗ 0 = 0.
12/36
Properties
▶ f ∗ g = g ∗ f,
▶ (f ∗ g) ∗ h = f ∗ (g ∗ h),
▶ f ∗ (g + h) = f ∗ g + f ∗ h,
▶ f ∗ χ1 = f ,
▶ f ∗ 0 = 0.
One could say that such functions represent a ring (which is called
Dirichlet ring). If you do not know what a ring is, never mind.
Basically you can do to these functions about everything that you
can do, say, to remainders modulo 10. In particular, you can find an
inverse of some (not all) functions.
12/36
Inverse
Definition
An inverse of a function f is such function g that f ∗ g = χ1 . We
denote g by f −1 .
13/36
Inverse
Definition
An inverse of a function f is such function g that f ∗ g = χ1 . We
denote g by f −1 .
It can be shown that a function f has the (unique) inverse if and
only if f (1) ̸= 0. Basically in this case you can determine f −1 (1),
then f −1 (2), and so on. Let’s practice.
13/36
Inverses of some functions
▶ If g = χ−1
1 then g(1) = 1,
14/36
Inverses of some functions
▶ If g = χ−1
1 then g(1) = 1, g(2) = (0 − g(1)χ1 (2))/χ1 (1) = 0,
14/36
Inverses of some functions
▶ If g = χ−1
1 then g(1) = 1, g(2) = (0 − g(1)χ1 (2))/χ1 (1) = 0,
. . . , g(n) = 0, so g = χ1 .
14/36
Inverses of some functions
▶ If g = χ−1
1 then g(1) = 1, g(2) = (0 − g(1)χ1 (2))/χ1 (1) = 0,
. . . , g(n) = 0, so g = χ1 .
▶ 0 does not have an inverse, since 0(1) = 0.
14/36
Inverses of some functions
▶ If g = χ−1
1 then g(1) = 1, g(2) = (0 − g(1)χ1 (2))/χ1 (1) = 0,
. . . , g(n) = 0, so g = χ1 .
▶ 0 does not have an inverse, since 0(1) = 0.
▶ id−1 ? σ0−1 ? σ1−1 ? 1−1 ? φ−1 ? µ−1 ?
14/36
Multiplicative functions
Definition
A function f is multiplicative, if for every a, b ∈ N s. t.
gcd(a, b) = 1 one has f (ab) = f (a)f (b).
15/36
Multiplicative functions
Definition
A function f is multiplicative, if for every a, b ∈ N s. t.
gcd(a, b) = 1 one has f (ab) = f (a)f (b). Essentially it means that
if n = pα1 1 · · · pαk k then f (n) = f (pα1 1 ) · · · f (pαk k ).
15/36
Multiplicative functions
Definition
A function f is multiplicative, if for every a, b ∈ N s. t.
gcd(a, b) = 1 one has f (ab) = f (a)f (b). Essentially it means that
if n = pα1 1 · · · pαk k then f (n) = f (pα1 1 ) · · · f (pαk k ).
It turns out that all these functions are multiplicative:
15/36
Multiplicative functions
Definition
A function f is multiplicative, if for every a, b ∈ N s. t.
gcd(a, b) = 1 one has f (ab) = f (a)f (b). Essentially it means that
if n = pα1 1 · · · pαk k then f (n) = f (pα1 1 ) · · · f (pαk k ).
It turns out that all these functions are multiplicative:
▶ id(ab) = ab = id(a)id(b).
15/36
Multiplicative functions
Definition
A function f is multiplicative, if for every a, b ∈ N s. t.
gcd(a, b) = 1 one has f (ab) = f (a)f (b). Essentially it means that
if n = pα1 1 · · · pαk k then f (n) = f (pα1 1 ) · · · f (pαk k ).
It turns out that all these functions are multiplicative:
▶ id(ab) = ab = id(a)id(b).
▶ σ0 (pα1 1 · · · pαk k ) = (α1 + 1) · · · (αk + 1) = σ0 (pα1 1 ) · · · σ0 (pαk k ).
15/36
Multiplicative functions
Definition
A function f is multiplicative, if for every a, b ∈ N s. t.
gcd(a, b) = 1 one has f (ab) = f (a)f (b). Essentially it means that
if n = pα1 1 · · · pαk k then f (n) = f (pα1 1 ) · · · f (pαk k ).
It turns out that all these functions are multiplicative:
▶ id(ab) = ab = id(a)id(b).
▶ σ0 (pα1 1 · · · pαk k ) = (α1 + 1) · · · (αk + 1) = σ0 (pα1 1 ) · · · σ0 (pαk k ).
α1 +1
αk +1
1
k
▶ σ1 (pα1 1 · · · pαk k ) = p1p −1−1 · · · p1p −1−1 = σ1 (pα1 1 ) · · · σ1 (pαk k ).
15/36
Multiplicative functions
Definition
A function f is multiplicative, if for every a, b ∈ N s. t.
gcd(a, b) = 1 one has f (ab) = f (a)f (b). Essentially it means that
if n = pα1 1 · · · pαk k then f (n) = f (pα1 1 ) · · · f (pαk k ).
It turns out that all these functions are multiplicative:
▶ id(ab) = ab = id(a)id(b).
▶ σ0 (pα1 1 · · · pαk k ) = (α1 + 1) · · · (αk + 1) = σ0 (pα1 1 ) · · · σ0 (pαk k ).
α1 +1
αk +1
1
k
▶ σ1 (pα1 1 · · · pαk k ) = p1p −1−1 · · · p1p −1−1 = σ1 (pα1 1 ) · · · σ1 (pαk k ).
▶ φ(pα1 1 · · · pαk k ) = (pα1 1 − pα1 1 −1 ) · · · (pαk k − pαk k −1 ) =
φ(pα1 1 ) · · · φ(pαk k ).
15/36
Multiplicative functions
Definition
A function f is multiplicative, if for every a, b ∈ N s. t.
gcd(a, b) = 1 one has f (ab) = f (a)f (b). Essentially it means that
if n = pα1 1 · · · pαk k then f (n) = f (pα1 1 ) · · · f (pαk k ).
It turns out that all these functions are multiplicative:
▶ id(ab) = ab = id(a)id(b).
▶ σ0 (pα1 1 · · · pαk k ) = (α1 + 1) · · · (αk + 1) = σ0 (pα1 1 ) · · · σ0 (pαk k ).
α1 +1
αk +1
1
k
▶ σ1 (pα1 1 · · · pαk k ) = p1p −1−1 · · · p1p −1−1 = σ1 (pα1 1 ) · · · σ1 (pαk k ).
▶ φ(pα1 1 · · · pαk k ) = (pα1 1 − pα1 1 −1 ) · · · (pαk k − pαk k −1 ) =
φ(pα1 1 ) · · · φ(pαk k ).
▶ µ(pα1 1 · · · pαk k ) = (−1)k [α1 = 1] · · · [αk = 1] =
µ(pα1 1 ) · · · µ(pαk k ).
15/36
Inverses for multiplicative functions
It can be shown (exercise for the reader) that the inverse of a
multiplicative function is also multiplicative.
16/36
Inverses for multiplicative functions
It can be shown (exercise for the reader) that the inverse of a
multiplicative function is also multiplicative. This means that it
makes sense to find inverses only at prime powers: if g = f −1 then
we can fix a prime p and find
16/36
Inverses for multiplicative functions
It can be shown (exercise for the reader) that the inverse of a
multiplicative function is also multiplicative. This means that it
makes sense to find inverses only at prime powers: if g = f −1 then
we can fix a prime p and find
g(1) = 1/f (1),
16/36
Inverses for multiplicative functions
It can be shown (exercise for the reader) that the inverse of a
multiplicative function is also multiplicative. This means that it
makes sense to find inverses only at prime powers: if g = f −1 then
we can fix a prime p and find
g(1) = 1/f (1),
g(p) = −(g(1)f (p))/f (1),
16/36
Inverses for multiplicative functions
It can be shown (exercise for the reader) that the inverse of a
multiplicative function is also multiplicative. This means that it
makes sense to find inverses only at prime powers: if g = f −1 then
we can fix a prime p and find
g(1) = 1/f (1),
g(p) = −(g(1)f (p))/f (1),
...
g(pk ) = −(g(1)f (pk ) + . . . + g(pk−1 )f (p))/f (1).
16/36
Finding inverses for the remaining functions
▶ If g = id−1 , then g(1) = 1,
17/36
Finding inverses for the remaining functions
▶ If g = id−1 , then g(1) = 1, g(p) = −p,
17/36
Finding inverses for the remaining functions
▶ If g = id−1 , then g(1) = 1, g(p) = −p, g(p2 ) = −p2 + p2 = 0,
17/36
Finding inverses for the remaining functions
▶ If g = id−1 , then g(1) = 1, g(p) = −p, g(p2 ) = −p2 + p2 = 0,
g(p3 ) = −p3 + p3 = 0, and so on. Thus, id−1 (n) = n · µ(n).
17/36
Finding inverses for the remaining functions
▶ If g = id−1 , then g(1) = 1, g(p) = −p, g(p2 ) = −p2 + p2 = 0,
g(p3 ) = −p3 + p3 = 0, and so on. Thus, id−1 (n) = n · µ(n).
▶ If g = σ0−1 , then g(1) = 1,
17/36
Finding inverses for the remaining functions
▶ If g = id−1 , then g(1) = 1, g(p) = −p, g(p2 ) = −p2 + p2 = 0,
g(p3 ) = −p3 + p3 = 0, and so on. Thus, id−1 (n) = n · µ(n).
▶ If g = σ0−1 , then g(1) = 1, g(p) = −2,
17/36
Finding inverses for the remaining functions
▶ If g = id−1 , then g(1) = 1, g(p) = −p, g(p2 ) = −p2 + p2 = 0,
g(p3 ) = −p3 + p3 = 0, and so on. Thus, id−1 (n) = n · µ(n).
▶ If g = σ0−1 , then g(1) = 1, g(p) = −2, g(p2 ) = −3 + 4 = 1,
17/36
Finding inverses for the remaining functions
▶ If g = id−1 , then g(1) = 1, g(p) = −p, g(p2 ) = −p2 + p2 = 0,
g(p3 ) = −p3 + p3 = 0, and so on. Thus, id−1 (n) = n · µ(n).
▶ If g = σ0−1 , then g(1) = 1, g(p) = −2, g(p2 ) = −3 + 4 = 1,
g(p3 ) = −4 + 6 − 2 = 0,
17/36
Finding inverses for the remaining functions
▶ If g = id−1 , then g(1) = 1, g(p) = −p, g(p2 ) = −p2 + p2 = 0,
g(p3 ) = −p3 + p3 = 0, and so on. Thus, id−1 (n) = n · µ(n).
▶ If g = σ0−1 , then g(1) = 1, g(p) = −2, g(p2 ) = −3 + 4 = 1,
g(p3 ) = −4 + 6 − 2 = 0, g(p4 ) = −5 + 8 − 3 = 0, and so on.
17/36
Finding inverses for the remaining functions
▶ If g = id−1 , then g(1) = 1, g(p) = −p, g(p2 ) = −p2 + p2 = 0,
g(p3 ) = −p3 + p3 = 0, and so on. Thus, id−1 (n) = n · µ(n).
▶ If g = σ0−1 , then g(1) = 1, g(p) = −2, g(p2 ) = −3 + 4 = 1,
g(p3 ) = −4 + 6 − 2 = 0, g(p4 ) = −5 + 8 − 3 = 0, and so on.
▶ We skip σ1−1 . You can practice on it yourself, though.
17/36
Finding inverses for the remaining functions
▶ If g = id−1 , then g(1) = 1, g(p) = −p, g(p2 ) = −p2 + p2 = 0,
g(p3 ) = −p3 + p3 = 0, and so on. Thus, id−1 (n) = n · µ(n).
▶ If g = σ0−1 , then g(1) = 1, g(p) = −2, g(p2 ) = −3 + 4 = 1,
g(p3 ) = −4 + 6 − 2 = 0, g(p4 ) = −5 + 8 − 3 = 0, and so on.
▶ We skip σ1−1 . You can practice on it yourself, though.
▶ If g = 1−1 , then g(1) = 1,
17/36
Finding inverses for the remaining functions
▶ If g = id−1 , then g(1) = 1, g(p) = −p, g(p2 ) = −p2 + p2 = 0,
g(p3 ) = −p3 + p3 = 0, and so on. Thus, id−1 (n) = n · µ(n).
▶ If g = σ0−1 , then g(1) = 1, g(p) = −2, g(p2 ) = −3 + 4 = 1,
g(p3 ) = −4 + 6 − 2 = 0, g(p4 ) = −5 + 8 − 3 = 0, and so on.
▶ We skip σ1−1 . You can practice on it yourself, though.
▶ If g = 1−1 , then g(1) = 1, g(p) = −1,
17/36
Finding inverses for the remaining functions
▶ If g = id−1 , then g(1) = 1, g(p) = −p, g(p2 ) = −p2 + p2 = 0,
g(p3 ) = −p3 + p3 = 0, and so on. Thus, id−1 (n) = n · µ(n).
▶ If g = σ0−1 , then g(1) = 1, g(p) = −2, g(p2 ) = −3 + 4 = 1,
g(p3 ) = −4 + 6 − 2 = 0, g(p4 ) = −5 + 8 − 3 = 0, and so on.
▶ We skip σ1−1 . You can practice on it yourself, though.
▶ If g = 1−1 , then g(1) = 1, g(p) = −1, g(p2 ) = −1 + 1 = 0,
17/36
Finding inverses for the remaining functions
▶ If g = id−1 , then g(1) = 1, g(p) = −p, g(p2 ) = −p2 + p2 = 0,
g(p3 ) = −p3 + p3 = 0, and so on. Thus, id−1 (n) = n · µ(n).
▶ If g = σ0−1 , then g(1) = 1, g(p) = −2, g(p2 ) = −3 + 4 = 1,
g(p3 ) = −4 + 6 − 2 = 0, g(p4 ) = −5 + 8 − 3 = 0, and so on.
▶ We skip σ1−1 . You can practice on it yourself, though.
▶ If g = 1−1 , then g(1) = 1, g(p) = −1, g(p2 ) = −1 + 1 = 0,
g(p3 ) = −1 + 1 = 0, and so on. Thus, 1−1 = µ.
17/36
Finding inverses for the remaining functions
▶ If g = id−1 , then g(1) = 1, g(p) = −p, g(p2 ) = −p2 + p2 = 0,
g(p3 ) = −p3 + p3 = 0, and so on. Thus, id−1 (n) = n · µ(n).
▶ If g = σ0−1 , then g(1) = 1, g(p) = −2, g(p2 ) = −3 + 4 = 1,
g(p3 ) = −4 + 6 − 2 = 0, g(p4 ) = −5 + 8 − 3 = 0, and so on.
▶ We skip σ1−1 . You can practice on it yourself, though.
▶ If g = 1−1 , then g(1) = 1, g(p) = −1, g(p2 ) = −1 + 1 = 0,
g(p3 ) = −1 + 1 = 0, and so on. Thus, 1−1 = µ.
▶ This also means that µ−1 = 1.
17/36
Why did I choose to attend this lecture
Fun fact: if f = g ∗ 1 then
g = g ∗ χ1 = g ∗ (1 ∗ µ) = (g ∗ 1) ∗ µ = f ∗ µ.
18/36
Why did I choose to attend this lecture
Fun fact: if f = g ∗ 1 then
g = g ∗ χ1 = g ∗ (1 ∗ µ) = (g ∗ 1) ∗ µ = f ∗ µ.
P
In human language: if f (n) = d|n g(d) for all n, then
P
g(n) = d|n f (d)µ nd for all n.
18/36
Why did I choose to attend this lecture
Fun fact: if f = g ∗ 1 then
g = g ∗ χ1 = g ∗ (1 ∗ µ) = (g ∗ 1) ∗ µ = f ∗ µ.
P
In human language: if f (n) = d|n g(d) for all n, then
P
g(n) = d|n f (d)µ nd for all n. This fact is called Möbius
inversion formula.
18/36
Why did I choose to attend this lecture
Fun fact: if f = g ∗ 1 then
g = g ∗ χ1 = g ∗ (1 ∗ µ) = (g ∗ 1) ∗ µ = f ∗ µ.
P
In human language: if f (n) = d|n g(d) for all n, then
P
g(n) = d|n f (d)µ nd for all n. This fact is called Möbius
inversion formula.
This formula can be applied to whatever function is derived from
the statement, not necessarily from the list above.
18/36
Problem
Given n ≤ 105 positive integers a1 , . . . , an (1 ≤ ai ≤ 106 ), and
also number m ≤ 105 . For each i from 1 to m print the number of
non-empty subsequences of {ai }ni=1 with LCM equal to i, modulo
998 244 353.
Solution.
Let f (d) be the number of subsequences where all members are
factors of d.
19/36
Problem
Given n ≤ 105 positive integers a1 , . . . , an (1 ≤ ai ≤ 106 ), and
also number m ≤ 105 . For each i from 1 to m print the number of
non-empty subsequences of {ai }ni=1 with LCM equal to i, modulo
998 244 353.
Solution.
Let f (d) be the number of subsequences where all members are
factors of d. Let g(d) be the number of all subsequences with
LCM = d.
19/36
Problem
Given n ≤ 105 positive integers a1 , . . . , an (1 ≤ ai ≤ 106 ), and
also number m ≤ 105 . For each i from 1 to m print the number of
non-empty subsequences of {ai }ni=1 with LCM equal to i, modulo
998 244 353.
Solution.
Let f (d) be the number of subsequences where all members are
factors of d. Let g(d) be the number of all subsequences with
LCM = d. One can see that:
∀n f (n) =
X
g(d),
d|n
19/36
Problem
Given n ≤ 105 positive integers a1 , . . . , an (1 ≤ ai ≤ 106 ), and
also number m ≤ 105 . For each i from 1 to m print the number of
non-empty subsequences of {ai }ni=1 with LCM equal to i, modulo
998 244 353.
Solution.
Let f (d) be the number of subsequences where all members are
factors of d. Let g(d) be the number of all subsequences with
LCM = d. One can see that:
∀n f (n) =
X
g(d),
d|n
so
∀n g(n) =
X
d|n
f (d)µ
n
d
.
19/36
Problem
Given n ≤ 105 positive integers a1 , . . . , an (1 ≤ ai ≤ 106 ), and
also number m ≤ 105 . For each i from 1 to m print the number of
non-empty subsequences of {ai }ni=1 with LCM equal to i.
Solution.
Now we are facing three subproblems:
20/36
Problem
Given n ≤ 105 positive integers a1 , . . . , an (1 ≤ ai ≤ 106 ), and
also number m ≤ 105 . For each i from 1 to m print the number of
non-empty subsequences of {ai }ni=1 with LCM equal to i.
Solution.
Now we are facing three subproblems:
▶ How to find all f (d)?
20/36
Problem
Given n ≤ 105 positive integers a1 , . . . , an (1 ≤ ai ≤ 106 ), and
also number m ≤ 105 . For each i from 1 to m print the number of
non-empty subsequences of {ai }ni=1 with LCM equal to i.
Solution.
Now we are facing three subproblems:
▶ How to find all f (d)?
▶ How to find all µ(d) (which is a more general question)?
20/36
Problem
Given n ≤ 105 positive integers a1 , . . . , an (1 ≤ ai ≤ 106 ), and
also number m ≤ 105 . For each i from 1 to m print the number of
non-empty subsequences of {ai }ni=1 with LCM equal to i.
Solution.
Now we are facing three subproblems:
▶ How to find all f (d)?
▶ How to find all µ(d) (which is a more general question)?
▶ How to compute all g(d) efficiently using f and µ (or, in other
words, how to find the convolution of these two functions)?
20/36
How to find all µ(d)?
The simplest way is to use the sieve of Eratosthenes: if you know
min_prime[n] for every n,
21/36
How to find all µ(d)?
The simplest way is to use the sieve of Eratosthenes: if you know
min_prime[n] for every n, then
1, n = 1,
h i
µ(n) = −µ np , min_prime[n] = p ̸= min_prime np ,
0,
otherwise.
21/36
How to find all µ(d)?
The simplest way is to use the sieve of Eratosthenes: if you know
min_prime[n] for every n, then
1, n = 1,
h i
µ(n) = −µ np , min_prime[n] = p ̸= min_prime np ,
0,
otherwise.
mu[i] = 1;
for (int i = 2; i < MAXN; ++i) {
if (int p = min_prime[i]; p != min_prime[i / p]) {
mu[i] = -mu[i / p];
} else {
mu[i] = 0;
}
}
21/36
Finding all f (d) for d ≤ 106
It should be clear that we do not actually care about the ordering
of the input; what matters is how many times every number occurs.
Denote by cnti the number of occurrences of number i in the
input.
22/36
Finding all f (d) for d ≤ 106
It should be clear that we do not actually care about the ordering
of the input; what matters is how many times every number occurs.
Denote by cnti the number
of occurrences of number i in the
P
input. Then f (n) = 2 d|n cntd − 1.
22/36
Finding all f (d) for d ≤ 106
It should be clear that we do not actually care about the ordering
of the input; what matters is how many times every number occurs.
Denote by cnti the number
of occurrences of number i in the
P
input. Then f (n) = 2 d|n cntd − 1. The following code:
for (int d = 1; d < MAXN; ++d) {
for (int n = d; n < MAXN; n += d) {
f[n] += cnt[d];
}
f[d] = binpow(2, f[d]) - 1;
}
works in
M AXN
M AXN
+ ... +
= O(M AXN log M AXN ).
1
M AXN
22/36
Again: code for f = cnt ∗ 1
for (int d = 1; d < MAXN; ++d) {
for (int n = d; n < MAXN; n += d) {
f[n] += cnt[d];
}
}
23/36
Code for g = f ∗ µ
for (int d = 1; d < MAXN; ++d) {
for (int n = d; n < MAXN; n += d) {
g[n] += f[d] * mu[n / d];
}
}
24/36
Code for arbitrary h = f ∗ g
for (int d = 1; d < MAXN; ++d) {
for (int n = d; n < MAXN; n += d) {
h[n] += f[d] * g[n / d];
}
}
25/36
Problem
Given n ≤ 105 positive integers a1 , . . . , an (1 ≤ ai ≤ 106 ), and
also number m ≤ 105 . For each i from 1 to m print the number of
non-empty subsequences of {ai }ni=1 with LCM equal to i, modulo
998 244 353.
The rest of the problem is to calculate 2f (d) for various d, which
can be done either by binary multiplication, or by pre-calculating all
powers of two (f (d) ≤ n by definition). Thus the problem is solved
in O(106 log 106 ).
26/36
Another setup
Suppose we have a function func : S → N in the statement, and
the question is “How many elements s ∈ S are there so that
func(a) = 1?”.
27/36
Another setup
Suppose we have a function func : S → N in the statement, and
the question is “How many elements s ∈ S are there so that
func(a) = 1?”. Assume that we can easily calculate, given d, for
how many elements s one has func(a) is divisible by d.
27/36
Another setup
Suppose we have a function func : S → N in the statement, and
the question is “How many elements s ∈ S are there so that
func(a) = 1?”. Assume that we can easily calculate, given d, for
how many elements s one has func(a) is divisible by d. If
f (d) = #{s ∈ S : func(s) = d} and
g(d) = #{s
P ∈ S : d | func(s)}, then, by definition,
g(d) = ∞
i=1 f (di).
27/36
Another setup
Suppose we have a function func : S → N in the statement, and
the question is “How many elements s ∈ S are there so that
func(a) = 1?”. Assume that we can easily calculate, given d, for
how many elements s one has func(a) is divisible by d. If
f (d) = #{s ∈ S : func(s) = d} and
g(d) = #{s
P ∈ S : d | func(s)}, then, by definition,
g(d) = ∞
i=1 f (di). Note that:
∞
X
g(d)µ(d)
d=1
27/36
Another setup
Suppose we have a function func : S → N in the statement, and
the question is “How many elements s ∈ S are there so that
func(a) = 1?”. Assume that we can easily calculate, given d, for
how many elements s one has func(a) is divisible by d. If
f (d) = #{s ∈ S : func(s) = d} and
g(d) = #{s
P ∈ S : d | func(s)}, then, by definition,
g(d) = ∞
i=1 f (di). Note that:
∞
X
d=1
g(d)µ(d) =
∞ X
∞
X
f (di)µ(d)
d=1 i=1
27/36
Another setup
Suppose we have a function func : S → N in the statement, and
the question is “How many elements s ∈ S are there so that
func(a) = 1?”. Assume that we can easily calculate, given d, for
how many elements s one has func(a) is divisible by d. If
f (d) = #{s ∈ S : func(s) = d} and
g(d) = #{s
P ∈ S : d | func(s)}, then, by definition,
g(d) = ∞
i=1 f (di). Note that:
∞
X
d=1
g(d)µ(d) =
∞ X
∞
X
d=1 i=1
f (di)µ(d) =
∞
X
n=1
f (n)
X
µ(d) = f (1).
d|n
27/36
Another setup
Suppose we have a function func : S → N in the statement, and
the question is “How many elements s ∈ S are there so that
func(a) = 1?”. Assume that we can easily calculate, given d, for
how many elements s one has func(a) is divisible by d. If
f (d) = #{s ∈ S : func(s) = d} and
g(d) = #{s
P ∈ S : d | func(s)}, then, by definition,
g(d) = ∞
i=1 f (di). Note that:
∞
X
g(d)µ(d) =
d=1
Similarly, f (d) =
∞ X
∞
X
f (di)µ(d) =
d=1 i=1
∞
X
n=1
f (n)
X
µ(d) = f (1).
d|n
P∞
i=1 g(di)µ(i).
27/36
Problem 2
Given n ≤ 105 positive integers a1 , . . . , an (1 ≤ ai ≤ 106 ), and
also number m ≤ 105 . For each i from 1 to m print the number of
non-empty subsequences of {ai }ni=1 with GCD equal to i, modulo
998 244 353.
28/36
Problem 2
Given n ≤ 105 positive integers a1 , . . . , an (1 ≤ ai ≤ 106 ), and
also number m ≤ 105 . For each i from 1 to m print the number of
non-empty subsequences of {ai }ni=1 with GCD equal to i, modulo
998 244 353.
The solution if very similar to the previous one. We calculate g(d)
for each d, counting the number of subsequences of multiples of d
(again, in M AXN/1 + . . . + M AXN/M AXN ).
28/36
Problem 2
Given n ≤ 105 positive integers a1 , . . . , an (1 ≤ ai ≤ 106 ), and
also number m ≤ 105 . For each i from 1 to m print the number of
non-empty subsequences of {ai }ni=1 with GCD equal to i, modulo
998 244 353.
The solution if very similar to the previous one. We calculate g(d)
for each d, counting the number of subsequences of multiples of d
(again, in M AXN/1 + . . . + M AXN/M AXN ). Then we
accumulate the sum of g(di)µ(i), then we are done.
28/36
Sublinear summation
Suppose that we have a function f (n). Denote F (n) =
Our goal is to calculate F (n) in o(n).
Pn
i=1 f (n).
29/36
Sublinear summation
P
Suppose that we have a function f (n). Denote F (n) = ni=1 f (n).
Our goal is to calculate F (n) in o(n).
There are good functions, for which their prefix sum can be
computed quickly (maybe in O(1) or in O(log n)). If f is such a
function, then there is no problem.
29/36
Sublinear summation
P
Suppose that we have a function f (n). Denote F (n) = ni=1 f (n).
Our goal is to calculate F (n) in o(n).
There are good functions, for which their prefix sum can be
computed quickly (maybe in O(1) or in O(log n)). If f is such a
function, then there is no problem. In other case, suppose that we
have two other good functions g and h so that f ∗ g = h. Define G
and H accordingly.
29/36
Sublinear summation
P
Suppose that we have a function f (n). Denote F (n) = ni=1 f (n).
Our goal is to calculate F (n) in o(n).
There are good functions, for which their prefix sum can be
computed quickly (maybe in O(1) or in O(log n)). If f is such a
function, then there is no problem. In other case, suppose that we
have two other good functions g and h so that f ∗ g = h. Define G
and H accordingly. Also, for the sake of simplicity, assume that
g(1) = 1 (most of the times g is multiplicative anyway).
29/36
Sublinear summation
P
Suppose that we have a function f (n). Denote F (n) = ni=1 f (n).
Our goal is to calculate F (n) in o(n).
There are good functions, for which their prefix sum can be
computed quickly (maybe in O(1) or in O(log n)). If f is such a
function, then there is no problem. In other case, suppose that we
have two other good functions g and h so that f ∗ g = h. Define G
and H accordingly. Also, for the sake of simplicity, assume that
g(1) = 1 (most of the times
P g is multiplicative anyway).
We know that h(n) = d|n f (d)g(n/d).
29/36
Sublinear summation
P
Suppose that we have a function f (n). Denote F (n) = ni=1 f (n).
Our goal is to calculate F (n) in o(n).
There are good functions, for which their prefix sum can be
computed quickly (maybe in O(1) or in O(log n)). If f is such a
function, then there is no problem. In other case, suppose that we
have two other good functions g and h so that f ∗ g = h. Define G
and H accordingly. Also, for the sake of simplicity, assume that
g(1) = 1 (most of the times
P g is multiplicative anyway).
We know that h(n) = d|n f (d)g(n/d). In other words,
h(n) = f (n) +
X
f
d|n, d>1
⇒f (n) = h(n) −
X
d|n, d>1
f
n
d
n
d
g(d)
g(d).
29/36
Sublinear summation
P
Suppose that we have a function f (n). Denote F (n) = ni=1 f (n).
Our goal is to calculate F (n) in o(n).
There are good functions, for which their prefix sum can be
computed quickly (maybe in O(1) or in O(log n)). If f is such a
function, then there is no problem. In other case, suppose that we
have two other good functions g and h so that f ∗ g = h. Define G
and H accordingly. Also, for the sake of simplicity, assume that
g(1) = 1 (most of the times
P g is multiplicative anyway).
We know that h(n) = d|n f (d)g(n/d). In other words,
h(n) = f (n) +
X
f
d|n, d>1
⇒f (n) = h(n) −
X
d|n, d>1
f
n
d
n
d
g(d)
g(d).
29/36
Sublinear summation
h(n) = f (n) +
X
f
d|n, d>1
⇒f (n) = h(n) −
X
d|n, d>1
f
n
d
n
d
g(d)
g(d).
30/36
Sublinear summation
h(n) = f (n) +
X
f
d|n, d>1
⇒f (n) = h(n) −
X
d|n, d>1
f
n
d
n
d
g(d)
g(d).
Let’s sum it over n:
30/36
Sublinear summation
h(n) = f (n) +
X
f
n
d
d|n, d>1
⇒f (n) = h(n) −
X
f
n
d|n, d>1
d
g(d)
g(d).
Let’s sum it over n:
F (n) =
n
X
f (k) =
k=1
= H(n) −
n
X
k=1
n
X
X
k=1 d|k, d>1
h(k) −
X
d|k, d>1
k
f
g(d)
d
n
X
X k
k
f
g(d) = H(n) −
g(d)
f
d
d
= H(n) −
d=2
d|k
n
X
j n k
d=2
g(d)F
d
.
30/36
Sublinear summation
F (n) = H(n) −
n
X
d=2
g(d)F
j n k
d
.
31/36
Sublinear summation
F (n) = H(n) −
n
X
d=2
g(d)F
j n k
d
.
So to find F (n), we need to know all F (⌊n/d⌋).
31/36
Sublinear summation
F (n) = H(n) −
n
X
d=2
g(d)F
j n k
d
.
So to find F (n), we need to know all F (⌊n/d⌋). But if we do it
recurrently, if, say, inside computation of F (⌊n/a⌋) we need to
know F (⌊⌊n/a⌋/b⌋), then this is the same as F (⌊n/ab⌋) (also
exercise for the reader).
31/36
Sublinear summation
F (n) = H(n) −
n
X
d=2
g(d)F
j n k
d
.
So to find F (n), we need to know all F (⌊n/d⌋). But if we do it
recurrently, if, say, inside computation of F (⌊n/a⌋) we need to
know F (⌊⌊n/a⌋/b⌋), then this is the same as F (⌊n/ab⌋) (also
√
exercise for the reader). Since there are about 2 n values
√ of type
√
⌊n/d⌋ (about n of them correspond to small d, about d of
them correspond to small n/d), we can compute all of this for all
these values in ascending order.
31/36
Sublinear summation
F (n) = H(n) −
n
X
d=2
g(d)F
j n k
d
.
What remains is to determine for every value of ⌊n/d⌋ the segment
of d-s corresponding to this value.
32/36
Sublinear summation
F (n) = H(n) −
n
X
d=2
g(d)F
j n k
d
.
What remains is to determine for every value of ⌊n/d⌋ the segment
of d-s corresponding to this value. We know that the largest value,
⌊n/2⌋, corresponds to a segment starting with 2. If the current
value k is achieved
$
% for a segment starting with d, then this segment
n
ends with n :
j n kd
n
n
n
=k ⇔k ≤ <k+1⇔
<x≤ .
x
x
k+1
k
32/36
Sublinear summation
F (n) = H(n) −
n
X
d=2
g(d)F
j n k
d
.
Then the following code works:
long long F(long long n) {
long long ans = H(n);
long long d = 2;
while (d <= n) {
long long right = n / (n / d);
ans -= F_cached[n / k] * (G(right) - G(d - 1));
d = right + 1;
}
return F_cached[n] = ans;
}
33/36
Sublinear summation?
Let’s assume that both H and G can be computed in O(1), also
F_cached can be accessed in O(1). Then the whole procedure
works in the following time:
34/36
Sublinear summation?
Let’s assume that both H and G can be computed in O(1), also
F_cached can be accessed in O(1). Then the whole procedure
works in the following time:
√
⌊ n⌋
X
(TF (k) + TF (n/k)) ≈
Z √n √
k=1
Z √
=
1
1
r n
dk
k+
k
√
n
k
1/2
+
√
nk
−1/2
dk =
n
√
2 3/2
k + 2 nk 1/2
3
1 = O n3/4 .
34/36
Even faster summation
√
Let K > n be some constant. If f is multiplicative, then we can
compute its values up to K using the linear sieve of Eratosthenes,
also computing the values of F up to K. Then the complexity
becomes
⌊n/K⌋
K+
X
Z n/K r
(TF (n/k)) ≈ K +
1
k=1
Z n/K
=K+
1
n
dk
k
√ −1/2
√
nk
dk = K + 2 nk 1/2
n/K
.
1
35/36
Even faster summation
√
Let K > n be some constant. If f is multiplicative, then we can
compute its values up to K using the linear sieve of Eratosthenes,
also computing the values of F up to K. Then the complexity
becomes
⌊n/K⌋
K+
X
Z n/K r
(TF (n/k)) ≈ K +
1
k=1
Z n/K
=K+
1
n
dk
k
√ −1/2
√
nk
dk = K + 2 nk 1/2
n/K
.
1
If we assign K = n2/3 then the complexity becomes O(n2/3 ).
35/36
?
36/36
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )