杜教筛+习题解析

· · 个人记录

杜教筛

在解决数论函数相关问题时,我们经常需要计算数论函数的前缀和:

\sum_{i=1}^n f(i)

如果 n 比较大,比如到了 10^9 这个数量级,直接线性筛空间显然会爆,此时就需要用到杜教筛。

杜教筛的核心逻辑如下:

先构造另外一个数论函数 g,使得 h=g*f,那么:

\begin{aligned} \sum_{i=1}^n h(n)&=\sum_{i=1}^n(g*f)(n)\\ &=\sum_{i=1}^n\sum_{d\mid n} g(d) f(\frac{i}{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)\\ \end{aligned}

其中,\displaystyle S(n)=\sum_{i=1}^n f(i)。

把 d=1 拆出来,那么:

\sum_{i=1}^n h(i)=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)

此时,只要 \displaystyle \sum_{i=1}^n h(i) 好计算,那么就可以根据右边的式子算出左边的 S(n)。因为右边用到了 \displaystyle S(\lfloor\frac{n}{d}\rfloor),所以可以递归+记忆化求解。使用整除分块求解,整体复杂度为 O(n^{\frac{3}{4}})。

也可以设置一个阈值 B,不超过 B 的部分直接用线性筛处理,超过的部分递归求解(记忆化)。一般设 B=n^{\frac{2}{3}},此时整体复杂度为 O(n^\frac{2}{3})。

典型例题:

(1)计算 \varphi(n) 的前缀和

取 g=\mathbf{1},那么 g(1)=1,\displaystyle \sum_{i=1}^n g(i)=n;此时 h=\mathbf{1}*\varphi=\mathrm{id},那么:

\sum_{i=1}^n h(i) = \frac{n(n+1)}{2}

所以:

S(n)=\frac{n(n+1)}{2}-\sum_{i=2}^n S(\lfloor\frac{n}{d}\rfloor)
模板:
int sum_g(int x){
    return x;
}
int sum_phi(int n){
    if(n < N) return phi[n];//不超过阈值直接调用数组
    if(mem_phi.count(n)) return mem_phi[n];//记忆化
    int res = n * (n + 1) / 2;
    for(int l = 2, r; l <= n; l = r + 1){
        r = n / (n / l);
        res -= (sum_g(r) - sum_g(l - 1)) * sum_phi(n / l); 
    }
    return mem_phi[n] = res;
}
(2)计算 \mu(n) 的前缀和

取 g=\mathbf{1},那么 g(1)=1,\displaystyle \sum_{i=1}^n g(i)=n;此时 h=\mathbf{1}*\mu=\varepsilon,那么:

\sum_{i=1}^n h(i) = 1

所以:

S(n)=1-\sum_{i=2}^n S(\lfloor\frac{n}{d}\rfloor)
模板:
int sum_g(int x){
    return x;
}
int sum_mu(int n){
    if(n < N) return mu[n];
    if(mem_mu.count(n)) return mem_mu[n];
    int res = 1;
    for(int l = 2, r; l <= n; l = r + 1){
        r = n / (n / l);
        res -= (sum_g(r) - sum_g(l - 1)) * sum_mu(n / l); 
    }
    return mem_mu[n] = res;
}
(3)计算 \varphi(n)*n 的前缀和

取 g(n)=n,那么 g(1)=1,\displaystyle \sum_{i=1}^n g(i)=\frac{n(n+1)}{2};此时:

\begin{aligned} \sum_{i=1}^n h(i) &= \sum_{i=1}^n \sum_{d|i}g(\frac{i}{d})f(d)\\ &= \sum_{i=1}^n \sum_{d|i}\frac{i}{d}\cdot \varphi(d)\cdot d\\ &= \sum_{i=1}^n i \sum_{d|i}\varphi(d)\\ &= \sum_{i=1}^n i^2\\ &= \frac{n(n+1)(2n+1)}{6}\\ \end{aligned}

所以:

S(n)=\frac{n(n+1)(2n+1)}{6}-\sum_{i=2}^n d \cdot S(\lfloor\frac{n}{d}\rfloor)
模板:
int sum_g(int x){
    return x * (x + 1) / 2;
}
int sum_f(int n){
    if(n < N) return f[n];
    if(mem_mu.count(n)) return mem_f[n];
    int res = n * (n + 1) / 2 * (2 * n + 1) / 3;
    for(int l = 2, r; l <= n; l = r + 1){
        r = n / (n / l);
        res -= (gsum(r) - gsum(l - 1)) * sum_f(n / l); 
    }
    return mem_f[n] = res;
}

习题解析

P3768 简单的数学题

\begin{aligned} \sum_{i=1}^n \sum_{j=1}^nij\gcd(i,j)&=\sum_{d=1}^n d\sum_{i=1}^n \sum_{j=1}^nij[\gcd(i,j)=d]\\ &=\sum_{d=1}^n d\sum_{i=1}^{\lfloor\frac{n}{d} \rfloor} \sum_{j=1}^{\lfloor\frac{n}{d} \rfloor}idjd[\gcd(i,j)=1]\\ &=\sum_{d=1}^n d^3\sum_{i=1}^{\lfloor\frac{n}{d} \rfloor} \sum_{j=1}^{\lfloor\frac{n}{d} \rfloor}ij\sum_{x\mid g\gcd(i,j)} \mu(x) \\ &=\sum_{d=1}^n d^3 \sum_{x\mid d} \mu(x) \sum_{i=1}^{\lfloor\frac{n}{dx} \rfloor} ix\sum_{j=1}^{\lfloor\frac{n}{dx} \rfloor}jx \\ &=\sum_{d=1}^n d^3 \sum_{x\mid d} \mu(x)\cdot x^2 \sum_{i=1}^{\lfloor\frac{n}{dx} \rfloor} i\sum_{j=1}^{\lfloor\frac{n}{dx} \rfloor}j \\ \end{aligned}

令:T=dx,\displaystyle sum(n) = \sum_{i=1}^n i,则:

\begin{aligned} 原式&=\sum_{T=1}^n sum(\lfloor\frac{n}{T} \rfloor)^2T^2 \sum_{d\mid T} d \mu(\frac{T}{x}) \\ &=\sum_{T=1}^n sum(\lfloor\frac{n}{T} \rfloor)^2T^2 \varphi(T) \\ \end{aligned}

令 f(n)=\displaystyle \sum_{i=1}^n i^2 \varphi(i),显然我们需要求出 f 的前缀和,这样才能用整除分块计算原式的值。

取 g(n)=n^2,那么 g(1)=1,\displaystyle \sum_{i=1}^n g(i)=\frac{n(n+1)(2n+1)}{6};此时:

\begin{aligned} \sum_{i=1}^n h(i) &= \sum_{i=1}^n \sum_{d|i}g(\frac{i}{d})f(d)\\ &= \sum_{i=1}^n \sum_{d|i}(\frac{i}{d})^2\cdot d^2\cdot\varphi(d)\\ &= \sum_{i=1}^n i^2 \sum_{d|i}\varphi(d)\\ &= \sum_{i=1}^n i^3\\ &= \frac{n^2(n+1)^2}{4}\\ \end{aligned}

所以:

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

P5218 无聊的水题 II

莫比乌斯反演倍数形式的典型应用。

由裴蜀定理可以把问题转换为:从 1\sim n 中选出若干个数使得其最大公因数为 1 的方案数。

记 f(i) 表示从 1\sim n 中取出若干个数使得最大公因数为 i 的倍数的方案数,显然:

f(i)=2^{\lfloor \frac{n}{i} \rfloor}-1

记 g(i) 表示从 1\sim n 中取出若干个数使得最大公因数 =i 的方案数,那么:

f(i)=\sum_{i\mid d} g(d)

由莫比乌斯反演可得:

g(i)=\sum_{i\mid d} \mu(d)f(\frac{d}{i})

那么:

g(1)=\sum_{i=1}^n \mu(i)f(i)=\sum_{i=1}^n \mu(i)(2^{\lfloor \frac{n}{i} \rfloor}-1)

P9238 [蓝桥杯 2023 省 A] 翻转硬币

分析可得,如果一个数存在平方因子,那么它会被它的因数们刚好翻到正面朝上的状态,所以题目本质是求:

\sum_{i=1}^n \mu(i)^2

直接求解只能拿到 70 分,剩下的分数需要用到一个结论:

\mu(i)^2=\sum_{d^2\mid i} \mu(d)

那么原来的式子可以转换为

\sum_{i=1}^n\sum_{d\mid i} \mu(d)=\sum_{d=1}^{\sqrt{n}} \mu(d)\lfloor\frac{n}{d^2}\rfloor

这样就可以用整除分块解决 10^{18} 这个数量级的问题。

需要注意:对于 \displaystyle \lfloor\frac{n}{d^2}\rfloor,它和常规的整除分块有点不一样,在已知左端点 l 的情况下,同一块的右端点为

r=\sqrt{\frac{n}{\lfloor\frac{n}{l^2}\rfloor}}

P6055 [RC-02] GCD

\begin{aligned} \sum_{i=1}^N\sum_{j=1}^N\sum_{p=1}^{\lfloor \frac{N}{j} \rfloor}\sum_{q=1}^{\lfloor \frac{N}{j} \rfloor}[\gcd(i,j)=1][\gcd(p,q)=1]&=\sum_{i=1}^N\sum_{j=1}^N[\gcd(i,j)=1]\sum_{p=1}^{\lfloor \frac{N}{j} \rfloor}\sum_{q=1}^{\lfloor \frac{N}{j} \rfloor}[\gcd(p,q)=1]\\ &=\sum_{i=1}^N\sum_{j=1}^N\sum_{d\mid gcd(i,j)}\mu(d)\sum_{p=1}^{\lfloor \frac{N}{j} \rfloor}\sum_{q=1}^{\lfloor \frac{N}{j} \rfloor}[\gcd(p,q)=1]\\ &=\sum_{d=1}^N\mu(d) {\lfloor \frac{N}{d} \rfloor}\sum_{j=1}^{\lfloor \frac{N}{d} \rfloor}\sum_{p=1}^{\lfloor \frac{N}{jd} \rfloor}\sum_{q=1}^{\lfloor \frac{N}{jd} \rfloor}[\gcd(p,q)=1]\\ \end{aligned}

设 f(n)=\displaystyle \sum_{j=1}^{n}\sum_{p=1}^{\lfloor \frac{n}{j} \rfloor}\sum_{q=1}^{\lfloor \frac{n}{j} \rfloor}[\gcd(p,q)=1],则:

\begin{aligned} f(n)&=\sum_{j=1}^{n}\sum_{p=1}^{\lfloor \frac{n}{j} \rfloor}\sum_{q=1}^{\lfloor \frac{n}{j} \rfloor}\sum_{d\mid gcd(p,q)}\mu(d)\\ &=\sum_{d=1}^n\mu(d)\sum_{j=1}^{\lfloor \frac{n}{d} \rfloor} {\lfloor \frac{n}{jd} \rfloor}^2 \end{aligned}

令 T=jd,则:

\begin{aligned} f(n)&=\sum_{T=1}^{n}\sum_{d\mid T} \mu(d) {\lfloor \frac{n}{T} \rfloor}^2\\ &=\sum_{T=1}^{n}{\lfloor \frac{n}{T} \rfloor}^2\sum_{d\mid T} \mu(d)\\ &=\sum_{T=1}^{n}{\lfloor \frac{n}{T} \rfloor}^2\varepsilon(T)\\ &=n^2 \end{aligned}

那么:

\begin{aligned} 原式=\sum_{d=1}^N\mu(d) {\lfloor \frac{N}{d} \rfloor} f({\lfloor \frac{N}{d} \rfloor})=\sum_{d=1}^N\mu(d) {\lfloor \frac{N}{d} \rfloor}^3 \end{aligned}

其实也可以直接在最开始的式子上做一些调整,因为枚举 j 的目的主要就是为了得到 p,q,所以可以直接枚举 p,q。

\begin{aligned} \sum_{i=1}^N\sum_{j=1}^N\sum_{p=1}^{\lfloor \frac{N}{j} \rfloor}\sum_{q=1}^{\lfloor \frac{N}{j} \rfloor}[\gcd(i,j)=1][\gcd(p,q)=1]&=\sum_{i=1}^N\sum_{p=1}^{N}\sum_{q=1}^{N}[\gcd(i,p,q)=1]\\ &=\sum_{i=1}^N\sum_{p=1}^{N}\sum_{q=1}^{N}\sum_{d\mid gcd(i,p,q)}\mu(d)\\ &=\sum_{d=1}^N\mu(d) {\lfloor \frac{N}{d} \rfloor}^3\\ \end{aligned}

P4318 完全平方数

如果 i 是一个平方数的倍数,那么 \mu(i)=0;反之 \mu(i)=\pm 1,此时 \mu(i)^2=1。

那么问题转换为求最小的 n,使得 \displaystyle \sum_{i=1}^n \mu(i)^2=K。

令 S(n)=\displaystyle \sum_{i=1}^n \mu(i)^2 ,显然 S(n) 的值是单调不减的,所以可以二分求出最小 n。

关键在于如何快速计算 S(n) 的值。

1.杜教筛

令 f(n)=\mu(n)^2,构造 g(n)=[n=平方数],那么

h(n)=(g*f)(n)=\sum_{d\mid n} g(d)f(\frac{n}{d})

如果 d 不是平方数,那么 g(d)=0;如果 \displaystyle \frac{n}{d} 中存在平方因子,那么 \displaystyle f(\frac{n}{d})=0。所以只有当 d 为 n 中最大的平方因子时,g(d)=1,\displaystyle f(\frac{n}{d})=(-1)^2=1,那么:

\sum_{i=1}^n h(n)=n

使用杜教筛的变换:

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

可得:

S(n)=n-\sum_{i=2}^{\sqrt{n}} S(\lfloor \frac{n}{i^2} \rfloor)

2.容斥原理

非平方数的倍数,显然可以用整体减去平方数的倍数得到。

整体可以看作 1^2 的倍数,先减去质数的平方 p^2 的倍数。至于合数部分,(p^k)^2 的倍数会包含在 p^2 的倍数中,不需要重复扣除;而 (pq)^2 的倍数会包含在 p^2、q^2 的倍数中,被重复减去,需要加回来;……这个系数就是莫比乌斯函数,所以可得:

S(n)=\sum_{i=1}^{\sqrt{n}}\mu(i)\lfloor \frac{n}{i^2} \rfloor

P10584 [蓝桥杯 2024 国 A] 数学题

设 i',j' 分别为 i,j 中的最大平方因子,要使得 ij 为平方数,那么 \displaystyle \frac{i}{i'} =\frac{j}{j'}。设这个相等的值为 x,显然 x 不能包含任何平方因子,即 \mu(x)=\pm 1,那么 \mu(x)^2=1。枚举 x 可得:

\sum_{i=1}^n\sum_{j=1}^m |f(ij)|=\sum_{x=1}^{min(n,m)} \mu(x)^2 \lfloor \sqrt{\frac{n}{x} }\rfloor \lfloor \sqrt{\frac{m}{x}} \rfloor

前面部分继续使用 \displaystyle \mu(x)^2=\sum_{d^2\mid x} \mu(d) 进行转换求出前缀和,然后整除分块即可求出答案。

几点需要注意:

①计算 \mu(x)^2 时,同样也要分区间。如果 x 的值比较小,那么可以直接在线性筛的时候算出 \mu(x)^2 的前缀和,这样直接 O(1) 调用即可;如果 x 的值比较大再使用杜教筛;

②最外层的整除分块计算的是 \displaystyle \lfloor \sqrt{\frac{n}{x}} \rfloor ,对于同一块,满足:

\lfloor \sqrt{\frac{n}{r}} \rfloor \ge \lfloor \sqrt{\frac{n}{l}} \rfloor =k

所以:

\frac{n}{r}\ge k^2

那么:

r\le \lfloor \frac{n}{k^2} \rfloor=\lfloor \frac{n}{{\lfloor \sqrt{\frac{n}{l}} \rfloor}^2} \rfloor

③整体的复杂度为 O(n^\frac{4}{9}),所以线性筛的阈值要设置为 2\times 10^7。

T852951 杜教筛2

第一问根据 \mu(i) 的定义可得:答案固定为 1。

第二问根据 \varphi(i) 的定义:\varphi(i^2) 表示 1\sim i^2 中和 i^2 互质的个数,与 i^2 互质就是和 i 互质,所以:

\varphi(i^2)=i\cdot \varphi(i)

再用杜教筛求解。

令 f(n)=n\cdot \varphi(n),g(n)=n,则:

h(n)=(g*f)(n)=\sum_{d\mid n}g(d)f(\frac{n}{d})=\sum_{d\mid n}d\cdot\frac{n}{d}\varphi(\frac{n}{d})=n\sum_{d\mid n}\varphi({\frac{n}{d}})=n^2

令 \displaystyle S(n)=\sum_{i=1}^nf(i),根据杜教筛的推导式:

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

所以:

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

T852929 约数之和

根据约数的定义可得:

\sigma(ij)=\sum_{x\mid i} \sum_{y\mid j}[\gcd(x,y)=1]\frac{xj}{y}

此时原式可以进行莫比乌斯反演:

\begin{aligned} \sum_{i=1}^N\sum_{j=1}^N\sum_{x\mid i} \sum_{y\mid j}[\gcd(x,y)=1]\frac{xj}{y}&=\sum_{x=1}^N\sum_{y=1}^N [\gcd(x,y)=1]\frac{x}{y}\lfloor \frac{N}{x}\rfloor\sum_{y|j}j\\ &=\sum_{x=1}^N\sum_{y=1}^N [\gcd(x,y)=1]x\lfloor \frac{N}{x}\rfloor\sum_{j=1}^{\lfloor \frac{N}{y}\rfloor}j\\ &=\sum_{x=1}^N\sum_{y=1}^N \sum_{d|gcd(x,y)}\mu(d)x\cdot \lfloor \frac{N}{x}\rfloor\cdot \sum_{j=1}^{\lfloor \frac{N}{y}\rfloor}j\\ &=\sum_{d=1}^N\mu(d)\sum_{x=1}^{\lfloor\frac{N}{d}\rfloor}dx \cdot \lfloor \frac{N}{dx}\rfloor\sum_{y=1}^{\lfloor\frac{N}{d}\rfloor}\sum_{j=1}^{\lfloor \frac{N}{dy}\rfloor}j\\ \end{aligned}

令 \displaystyle F(n)=\sum_{y=1}^n\sum_{j=1}^{\lfloor \frac{n}{y}\rfloor} j,交换枚举顺序可得:

F(n)=\sum_{j=1}^nj\sum_{y=1}^n[j|y]=\sum_{j=1}^nj\lfloor\frac{n}{j}\rfloor

注:\displaystyle F(n)=\sum_{j=1}^n \sigma(j),即 F(n) 本质上是 1\sim n 的因数和之和。

所以:

原式=\sum_{d=1}^N\mu(d)d\sum_{x=1}^{\lfloor\frac{N}{d}\rfloor}x\lfloor\frac{N}{dx}\rfloor\sum_{y=1}^{\lfloor\frac{N}{d}\rfloor}x\lfloor\frac{N}{dy}\rfloor=\sum_{d=1}^N\mu(d)d\cdot (F({\lfloor\frac{N}{d}\rfloor}))^2

对于 \mu(d)d 部分,使用杜教筛处理其前缀和;对于 \displaystyle f({\lfloor\frac{N}{d}\rfloor}) 部分,直接分块计算。

参考代码

:::success[P3768 简单的数学题]

#include <bits/stdc++.h>
using namespace std ;
#define int long long
const int N = 1e7 + 5 ;
int n , mod , np[N] , phi[N] , p[N] , tot ;
map <int , int> mem_phi , mem_mu ;
int get_g(int x)
{
    return (__int128)x * (x + 1) * (2 * x + 1) / 6 % mod ;
}
int Sum(int x)
{
    return (__int128)x * (x + 1) / 2 ;
}
void init()
{
     np[0] = np[1] = 1 ;
     phi[1] = 1 ;
     for(int i = 2 ; i < N ; i ++)
     {
        if(!np[i])
        {
            p[++ tot] = i ;
            phi[i] = i - 1 ;
        }
        for(int j = 1 ; j <= tot ; j ++)
        {
            if(p[j] * i >= N) break ;
            np[p[j] * i] = 1 ;
            if(i % p[j] == 0)
            {
                phi[i * p[j]] = phi[i] * p[j] ;
                break ;
            }
            phi[i * p[j]] = phi[i] * phi[p[j]] ;
        }
     }
     for(int i = 1 ; i < N ; i ++) phi[i] = phi[i - 1] + (phi[i] * i % mod * i % mod) ;
}
int get_phi(int x)
{
    if(x < N) return phi[x] ;
    if(mem_phi.count(x)) return mem_phi[x] ;
    int res = Sum(x) * Sum(x) ;
    for(int l = 2 , r ; l <= x ; l = r + 1)
    {
        r = x / (x / l) ;
        res -= (get_g(r) - get_g(l - 1)) * get_phi(x / l) % mod ;
        res = (res % mod + mod) % mod ;
    }
    mem_phi[x] = res ;
    return res ;
}
signed main()
{
    int ans = 0 ;
    cin >> mod >> n ;
    init() ;
    for(int l = 1 , r ; l <= n ; l = r + 1)
    {
        r = n / (n / l) ;
        ans += (get_phi(r) - get_phi(l - 1)) * Sum(n / l) % mod * Sum(n / l) % mod ;
    }
    cout << (ans % mod + mod) % mod ;
    return 0 ;
}

:::

:::success[P5218 无聊的水题 II]

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9+7 , N = 1e7+1 ;
unordered_map<long long,int>mp;
int n , ans , mu[N+5] ;
bool np[N+5] ;
vector<int>p;
void init()
{
    np[0]=np[1]=mu[1]=1;
    for ( int i = 2 ; i < N ; i ++ )
    {
        if (!np[i])
        {
            mu[i]=-1;
            p.push_back(i);
        }
        for ( auto j : p )
        {
            if (i*j>=N)break;
            np[i*j]=1;
            if (i%j==0)break;
            mu[i*j]=-mu[i];
        }
    }
    for ( int i = 2 ; i < N ; i ++ )
        mu[i]+=mu[i-1];
}
int S_mu(int n)
{
    if (n<N)return mu[n];
    if (mp.count(n))return mp[n];
    int ans = 1 ;
    for ( int l = 2 , r ; l <= n ; l = r+1 )
    {
        r=n/(n/l);
        ans=(ans-1ll*(r-l+1)%mod*S_mu(n/l)%mod)%mod;
    }
    return mp[n]=ans;
}
int ksm(int x,int y)
{
    int num = 1 ;
    while (y)
    {
        if (y&1)num=1ll*num*x%mod;
        x=1ll*x*x%mod; y>>=1;
    }
    return num;
}
signed main()
{
    cin >> n ; init();
    for ( int l = 1 , r ; l <= n ; l = r+1 )
    {
        r=n/(n/l);
        ans=(ans+1ll*(S_mu(r)-S_mu(l-1))*(ksm(2,n/l)-1)%mod)%mod;
    }
    cout << (ans+mod)%mod ;
    return 0;
}

:::

:::success[P9238 [蓝桥杯 2023 省 A] 翻转硬币]

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e7 + 5;
int n, ans;
int p[N], ptot;
bool np[N];
int mu[N];
unordered_map<int, int> s_f;
void init(){
    np[0] = np[1] = 1;
    mu[1] = 1;
    for(int i = 2; i < N; i++){
        if(!np[i]){
            p[++ptot] = i;
            mu[i] = -1;
        }
        for(int j = 1; j <= ptot && i * p[j] < N; j++){
            np[i * p[j]] = 1;
            if(i % p[j] == 0) break;
            mu[i * p[j]] = -mu[i];
        }
    }
    for(int i = 1; i < N; i++) mu[i] += mu[i - 1];
}
int get_f(int n){
    if(n < N) return mu[n];
    if(s_f.count(n)) return s_f[n];
    int res = 1;
    for(int l = 2, r; l <= n; l = r + 1){
        r = n / (n / l);
        res -= (r - l + 1) * get_f(n / l); 
    }
    return s_f[n] = res;
}
signed main(){
    init();
    cin >> n;
    for(int l = 1, r; l <= sqrt(n); l = r + 1){
        int k = n / (l * l);
        r = sqrt(n / k);
        ans += k * (get_f(r) - get_f(l - 1));
    }
    cout << ans; 
    return 0;
}

:::

:::success[P6055 [RC-02] GCD]

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e6 + 5;
const int mod = 998244353;
int n, ans;
int p[N], ptot;
bool np[N];
int mu[N];
unordered_map<int, int> s_mu;
void init(){
    np[0] = np[1] = 1;
    mu[1] = 1;
    for(int i = 2; i < N; i++){
        if(!np[i]){
            p[++ptot] = i;
            mu[i] = -1;
        }
        for(int j = 1; j <= ptot && i * p[j] < N; j++){
            np[i * p[j]] = 1;
            if(i % p[j] == 0) break;
            mu[i * p[j]] = -mu[i];
        }
    }
    for(int i = 1; i < N; i++) mu[i] += mu[i - 1];
}
int gsum(int x){
    return x;
}
int get_mu(int n){
    if(n < N) return mu[n];
    if(s_mu.count(n)) return s_mu[n];
    int res = 1;
    for(int l = 2, r; l <= n; l = r + 1){
        r = n / (n / l);
        res -= (gsum(r) - gsum(l - 1)) * get_mu(n / l); 
        res = (res % mod + mod) % mod;
    }
    return s_mu[n] = res;
}
int fpow(int x, int y){
    int res = 1;
    while(y){
        if(y % 2) res = res * x % mod;
        x = x * x % mod;
        y /= 2;
    }
    return res;
}
signed main(){
    init();
    cin >> n;
    for(int l = 1, r; l <= n; l = r + 1){
        r = n / (n / l);
        ans += (get_mu(r) - get_mu(l - 1)) * fpow(n / l, 3);
        ans = (ans % mod + mod) % mod;
    }
    cout << ans << "\n";
    return 0;
}

:::

:::success[P4318 完全平方数]

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e6 + 5;
int a, b;
int p[N], ptot;
bool np[N];
int mu[N];
void init(int B){
    np[0] = np[1] = 1;
    mu[1] = 1;
    for(int i = 2; i <= B; i++){
        if(!np[i]){
            p[++ptot] = i;
            mu[i] = -1;
        }
        for(int j = 1; j <= ptot && i * p[j] <= B; j++){
            np[i * p[j]] = 1;
            if(i % p[j] == 0) break;
            mu[i * p[j]] = -mu[i];
        }
    }
}
int get_mu(int n){
    int res = 0;
    for(int l = 1, r; l <= sqrt(n); l = r + 1){
        r = n / (n / l);
        res += mu[l] * (n / (l * l)); 
    }
    return res;
}
void sol(){
    int K; cin >> K;
    int l = K, r = 2 * K;
    while(l < r){
        int mid = l + r >> 1;
        if(get_mu(mid) >= K) r = mid;
        else l = mid + 1;
    }
    cout << l << "\n";
}
signed main(){
    init(100000);
    int T; cin >> T;
    while(T--) sol();   
    return 0;
}

:::

:::success[P10584 [蓝桥杯 2024 国 A] 数学题]

#include <bits/stdc++.h>
#define int unsigned long long
using namespace std;
const int N = 2e7 + 5;
int n, m, ans;
int p[N], ptot;
bool np[N];
signed mu[N], smu[N];
unordered_map<int, int> s_f;
void init(){
    np[0] = np[1] = 1;
    mu[1] = 1;
    for(int i = 2; i < N; i++){
        if(!np[i]){
            p[++ptot] = i;
            mu[i] = -1;
        }
        for(int j = 1; j <= ptot && i * p[j] < N; j++){
            np[i * p[j]] = 1;
            if(i % p[j] == 0) break;
            mu[i * p[j]] = -mu[i];
        }
    }
    for(int i = 1; i < N; i++){     
        smu[i] = smu[i - 1] + mu[i] * mu[i];
        mu[i] += mu[i - 1];
    }
}
int get_f(int n){
    if(n < N) return mu[n];
    if(s_f.count(n)) return s_f[n];
    int res = 1;
    for(int l = 2, r; l <= n; l = r + 1){
        r = n / (n / l);
        res -= (r - l + 1) * get_f(n / l); 
    }
    return s_f[n] = res;
}
int sum_f(int n){
    if(n < N) return smu[n];
    int res = 0;
    for(int l = 1, r; l <= sqrt(n); l = r + 1){
        int k = n / (l * l);
        r = sqrt(n / k);
        res += k * (get_f(r) - get_f(l - 1));
    }   
    return res;
}
signed main(){
    init();
    cin >> n >> m;
    if(n > m) swap(n, m);
    for(int l = 1, r; l <= n; l = r + 1){
        int x = sqrt(n / l), y = sqrt(m / l);
        r = min(n / (x * x), m / (y * y));  
        ans += (sum_f(r) - sum_f(l - 1)) * x * y;
    }
    cout << ans; 
    return 0;
}

:::

:::success[T852951 杜教筛2]

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e6 + 5;
const int mod = 1e9 + 7; 
int n, ans;
int p[N], ptot;
bool np[N];
int phi[N];
unordered_map<int, int> mem_phi;
void init(){
    np[0] = np[1] = 1;
    phi[1] = 1;
    for(int i = 2; i < N; i++){
        if(!np[i]){
            p[++ptot] = i;
            phi[i] = (i - 1) % mod;
        }
        for(int j = 1; j <= ptot && i * p[j] < N; j++){
            np[i * p[j]] = 1;
            if(i % p[j] == 0){
                phi[i * p[j]] = phi[i] * p[j] % mod;
                break;
            }
            phi[i * p[j]] = phi[i] * phi[p[j]] % mod;
        }
    }
    for(int i = 1; i < N; i++){
        phi[i] = (phi[i - 1] + phi[i] * i % mod) % mod;
    }
}
int sum_g(int x){
    return (1 + x) * x / 2 % mod;
}
int sum_f(int n){
    if(n < N) return phi[n];
    if(mem_phi.count(n)) return mem_phi[n];
    int res = (__int128)n * (n + 1) * (2 * n + 1) / 6 % mod;
    for(int l = 2, r; l <= n; l = r + 1){
        r = n / (n / l);
        res -= (sum_g(r) - sum_g(l - 1)) * sum_f(n / l) % mod; 
        res = (res % mod + mod) % mod;
    }
    return mem_phi[n] = res;
}

signed main(){  
    cin >> n;
    init();
    cout << 1 << "\n" << sum_f(n);
    return 0;
}

:::

:::success[T852929 约数之和]

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e6 + 5;
const int mod = 1e9 + 7; 
int n, ans;
int p[N], ptot;
bool np[N];
int mu[N], sf[N];
unordered_map<int, int> mem_mu;
void init(){
    np[0] = np[1] = 1;
    mu[1] = 1;
    for(int i = 2; i < N; i++){
        if(!np[i]){
            p[++ptot] = i;
            mu[i] = (mod - 1) % mod;
        }
        for(int j = 1; j <= ptot && i * p[j] < N; j++){
            np[i * p[j]] = 1;
            if(i % p[j] == 0) break;
            mu[i * p[j]] = (mod - mu[i]) % mod;
        }
    }
    for(int i = 1; i < N; i++){
        for(int j = i; j < N; j += i) sf[j] += i;
    }
    for(int i = 1; i < N; i++){
        mu[i] = (mu[i - 1] + mu[i] * i % mod) % mod;
        sf[i] = (sf[i - 1] + sf[i]) % mod;
    }
}
int sum_g(int x){
    return (1 + x) * x / 2 % mod;
}
int sum_f(int n){
    if(n < N) return mu[n];
    if(mem_mu.count(n)) return mem_mu[n];
    int res = 1;
    for(int l = 2, r; l <= n; l = r + 1){
        r = n / (n / l);
        res -= (sum_g(r) - sum_g(l - 1)) * sum_f(n / l) % mod; 
        res = (res % mod + mod) % mod;
    }
    return mem_mu[n] = res;
}
int F(int n){
    if(n < N) return sf[n];
    int res = 0;
    for(int l = 1, r; l <= n; l = r + 1){
        r = n / (n / l);
        res += (sum_g(r) - sum_g(l - 1)) % mod * (n / l) % mod;
        res = (res % mod + mod) % mod;
    }
    return res;
}
signed main(){  
    cin >> n;
    init();
    for(int l = 1, r; l <= n; l = r + 1){
        r = n / (n / l);
        int k = F(n / l);
        ans += (sum_f(r) - sum_f(l - 1)) * k % mod * k % mod;
        ans = (ans % mod + mod) % mod;
    }
    cout << ans;
    return 0;
}

:::