Zhou Sieve

Prerequisite knowledge

Definition

The Zhouge sieve is a sieve that computes the prefix sums of most multiplicative functions in sublinear time.

We use ∑i=1nf(i)\displaystyle\sum_{i=1}^nf(i) as an example to explain its principle.

Conventions

  • P\mathbb P denotes the set of primes, and pip_i denotes the ii-th prime.
  • mm denotes the number of primes not exceeding n\sqrt n.

Requirement

For p∈P,c∈Np\in\mathbb P,c\in\mathbb N, f(pc)f(p^c) must be a low-degree polynomial in pp.

Idea

  • Any integer in [1,n][1,n] has at most one >n>\sqrt n prime factor.
  • The values ⌊ni⌋(i∈[1,n]∩N)\left\lfloor\dfrac ni\right\rfloor(i\in[1,n]\cap\mathbb N) take only about n\sqrt n distinct values, which can be used to reduce the time complexity.

Process

Partition all integers in [1,n][1,n] according to whether they have a >n>\sqrt n prime factor:

∑i=1nf(i)=∑i=1n[∃d∈(n,n]∩P,d∣i]f(i)+∑i=1n[∀d∈(n,n]∩P,d∤i]f(i)\sum_{i=1}^nf(i)=\sum_{i=1}^n\left[\exists d\in(\sqrt n,n]\cap\mathbb P,d\mid i\right]f(i)+\sum_{i=1}^n\left[\forall d\in(\sqrt n,n]\cap\mathbb P,d\nmid i\right]f(i)

For the first part, enumerate the largest factor and use the multiplicativity of the function to obtain

∑i=1nf(i)=∑i=1nf(i)⋅(∑d=⌊n⌋+1⌊ni⌋[d∈P]f(d))+∑i=1n[∀d∈(n,n]∩P,d∤i]f(i)\sum_{i=1}^nf(i)=\sum_{i=1}^{\sqrt n}f(i)\cdot\left(\sum_{d=\lfloor\sqrt n\rfloor+1}^{\lfloor\frac ni\rfloor}[d\in\mathbb P]f(d)\right)+\sum_{i=1}^n\left[\forall d\in(\sqrt n,n]\cap\mathbb P,d\nmid i\right]f(i)

The two parts can be computed separately.

Part 1

Compute ∑i=1nf(i)⋅(∑d=⌊n⌋+1⌊ni⌋[d∈P]f(d))\displaystyle\sum_{i=1}^{\sqrt n}f(i)\cdot\left(\sum_{d=\lfloor\sqrt n\rfloor+1}^{\lfloor\frac ni\rfloor}[d\in\mathbb P]f(d)\right).

Enumerate ii, then compute the expression in parentheses in O(1)O(1) time.

Let g(t,l)=∑i=1l[∀j∈[1,t],gcd⁡(i,pj)=1]f(i)\displaystyle g(t,l)=\sum_{i=1}^l[\forall j\in[1,t],\gcd(i,p_j)=1]f(i), namely, the sum over the numbers in [1,l][1,l] that are coprime to all of p1,p2,…,ptp_1,p_2,\dots,p_t of their ff values.

The computation in Part 1 becomes ∑i=1nf(i)⋅g(m,⌊ni⌋)\displaystyle\sum_{i=1}^{\sqrt n}f(i)\cdot g\left(m,\left\lfloor\frac ni\right\rfloor\right).

The base case is g(0,l)=∑i=1lf(i)g(0,l)=\sum_{i=1}^lf(i), and the transition is g(t,l)=g(t−1,l)−f(pt)⋅g(t−1,⌊lpt⌋)g(t,l)=g(t-1,l)-f(p_t)\cdot g\left(t-1,\left\lfloor\frac l{p_t}\right\rfloor\right).

The variable ll has about n\sqrt n possible values, and each requires enumerating its prime factors, giving complexity O(nln⁡n⋅n)=O(nlog⁡n)\displaystyle O\left(\frac{\sqrt n}{\ln\sqrt n}\cdot\sqrt n\right)= O\left(\frac n{\log n}\right). This needs to be optimized.

Observe that when pt+12>lp_{t+1}^2>l, the only number satisfying the condition is 11, so g(t,l)=f(1)=1g(t,l)=f(1)=1.

When pt2>lp_t^2>l, substituting this into the recurrence gives g(t,l)=g(t−1,l)−f(pt)g(t,l)=g(t-1,l)-f(p_t).

Therefore, once pt2>lp_t^2>l is found, stop the transition and call this tt value tlt_l. Then ∀t>tl,g(t,l)=g(tl,l)−∑i=tlt−1f(pi)\forall t>t_l,g(t,l)=g(t_l,l)-\sum_{i=t_l}^{t-1}f(p_i).

By preprocessing the prefix sums of ff over the primes, gg can be obtained quickly, reducing the time complexity to O(n34log⁡n)O\left(\dfrac{n^{\frac34}}{\log n}\right).

Part 2

Compute ∑i=1n[∀d∈(n,n]∩P,d∤i]f(i)\displaystyle\sum_{i=1}^n\left[\forall d\in(\sqrt n,n]\cap\mathbb P,d\nmid i\right]f(i).

Let h(t,l)=∑i=1l[i=∏j=tmpjcj,cj∈N]f(i)\displaystyle h(t,l)=\sum_{i=1}^l\left[i=\prod_{j=t}^mp_j^{c_j},c_j\in\mathbb N\right]f(i), namely, the sum over all numbers in [1,l][1,l] whose prime factors are restricted to pt,pt+1,…,pmp_t,p_{t+1},\dots,p_m of their ff values.

Part 2 asks for h(0,n)h(0,n).

The base case is h(m+1,l)=1h(m+1,l)=1, and the transition is h(t,l)=h(t+1,l)+∑c∈N∗f(ptc)⋅h(t+1,⌊lptc⌋)\displaystyle h(t,l)=h(t+1,l)+\sum_{c\in\mathbb N^*}f(p_t^c)\cdot h\left(t+1,\left\lfloor\frac l{p_t^c}\right\rfloor\right).

The variable ll has about n\sqrt n possible values, so the direct transition takes O(n⋅nln⁡n)=O(nlog⁡n)\displaystyle O\left(\sqrt n\cdot\frac{\sqrt n}{\ln\sqrt n}\right)= O\left(\frac n{\log n}\right) time. This needs to be optimized.

As with the optimization for gg, when pt>lp_t>l, the only number that can be formed from pt,pt+1,…,pmp_t,p_{t+1},\dots,p_m is 11, so h(t,l)=f(1)=1h(t,l)=f(1)=1.

Similarly, ∀pt2>l,h(t,l)=h(t+1,l)+f(pt)\forall p_t^2>l,h(t,l)=h(t+1,l)+f(p_t).

Therefore, once pt2>lp_t^2>l is found, stop the transition and call this tt value tlt_l. Whenever hh is subsequently used, add the current hh value plus ∑i=ptlmin⁡(l,n)[i∈P]f(i)\displaystyle\sum_{i=p_{t_l}}^{\min(l,\sqrt n)}[i\in\mathbb P]f(i), as needed.

The time complexity is reduced to O(n34log⁡n)O\left(\dfrac{n^{\frac34}}{\log n}\right).

Summation

The sum of the answers to Part 1 and Part 2 is ∑i=1nf(i)\displaystyle\sum_{i=1}^nf(i).

References

Notes on linear sieves, the Du sieve, and the Zhouge sieve for multiplicative functions

This content comes from OI Wiki: Zhou Sieve. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.