杜教筛
杜教筛
作用
线性筛是一种亚线性的数论函数前缀和算法,它利用狄利克雷卷积构造递推式,配合整出分块与记忆化搜索,可以在
给定积性函数
其核心思想是构造两个辅助函数
公式推导
设
对
交换求和顺序,有
我们将
积性函数满足
这就是杜教筛的核心公式了。
例题
P4213 【模板】杜教筛
:::info[求莫比乌斯函数前缀和]{open}
求
我们已知狄利克雷卷积恒等式:
带入杜教筛公式
:::
:::info[求莫比乌斯函数前缀和]{open}
求
我们已知狄利克雷卷积恒等式:
带入杜教筛公式
:::
:::success[代码]
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 1e6 + 5;
LL t, n, k, p[N], v[2][N];
bool h[N];
unordered_map<LL, LL> w[2];
void I() {
v[0][1] = v[1][1] = 1;
for (int i = 2; i <= 1e6; i++) {
!h[i] && (p[++k] = i, v[0][i] = -1, v[1][i] = i - 1);
for (int j = 1; j <= k && i * p[j] <= 1e6; j++) {
LL d = i * p[j];
h[d] = 1;
if (!(i % p[j])) {
v[1][d] = v[1][i] * p[j];
break;
}
v[0][d] = -v[0][i];
v[1][d] = v[1][i] * v[1][p[j]];
}
}
partial_sum(v[0], v[0] + N, v[0]);
partial_sum(v[1], v[1] + N, v[1]);
}
LL C(LL n, bool o) {
if (n <= 1e6) return v[o][n];
if (w[o].count(n)) return w[o][n];
LL s = (o ? n * (n + 1) / 2 : 1);
for (LL l = 2, r; l <= n; l = r + 1) {
r = n / (n / l);
s -= (r - l + 1) * C(n / l, o);
}
return w[o][n] = s;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
for (I(), cin >> t; t; t--) {
cin >> n;
cout << C(n, 1) << " " << C(n, 0) << "\n";
}
return 0;
}
:::
P3768 简单的数学题
整除分块,用杜教筛求欧拉函数的前缀和即可。
:::success[代码]
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 1e6 + 5;
LL M, n, k, p[N], v[N];
bool h[N];
unordered_map<LL, LL> w;
void I() {
v[1] = 1;
for (int i = 2; i <= 1e6; i++) {
!h[i] && (p[++k] = i, v[i] = i - 1);
for (int j = 1; j <= k && i * p[j] <= 1e6; j++) {
h[i * p[j]] = 1;
if (!(i % p[j])) {
v[i * p[j]] = v[i] * p[j] % M;
break;
}
v[i * p[j]] = v[i] * v[p[j]] % M;
}
}
for (LL i = 1; i <= 1e6; i++) {
v[i] = (v[i - 1] + i * i % M * v[i] % M) % M;
}
}
LL V(__int128 n, bool o) {
return o ? n * (n + 1) * (2 * n + 1) / 6 % M : n * (n + 1) / 2 % M * (n * (n + 1) / 2 % M) % M;
}
LL C(LL n) {
if (n <= 1e6) return v[n];
if (w.count(n)) return w[n];
LL s = V(n, 0);
for (LL l = 2, r; l <= n; l = r + 1) {
r = n / (n / l);
s = (s - (V(r, 1) - V(l - 1, 1) + M) * C(n / l) % M + M) % M;
}
return w[n] = s;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> M >> n, I();
LL s = 0;
for (LL l = 1, r; l <= n; l = r + 1) {
r = n / (n / l);
s = (s + (C(r) - C(l - 1) + M) * V(n / l, 0) % M) % M;
}
cout << s;
return 0;
}
:::
P6055 [RC-02] GCD
很巧妙的一个转换。
设
:::success[代码]
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 1e6 + 5, M = 998244353;
LL n, k, p[N], v[N];
bool h[N];
unordered_map<LL, LL> w;
void I() {
v[1] = 1;
for (int i = 2; i <= 1e6; i++) {
!h[i] && (p[++k] = i, v[i] = -1);
for (int j = 1; j <= k && i * p[j] <= 1e6; j++) {
h[i * p[j]] = 1;
if (!(i % p[j])) break;
v[i * p[j]] = -v[i];
}
}
partial_sum(v, v + N, v);
}
LL C(LL n) {
if (n <= 1e6) return v[n];
if (w.count(n)) return w[n];
LL s = 1;
for (LL l = 2, r; l <= n; l = r + 1) {
r = n / (n / l);
s = (s - (r - l + 1) * C(n / l) % M + M) % M;
}
return w[n] = s;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
I(), cin >> n;
LL s = 0;
for (LL l = 1, r; l <= n; l = r + 1) {
r = n / (n / l);
s = (s + (n / l) * (n / l) % M * (n / l) % M * (C(r) - C(l - 1) + M) % M) % M;
}
cout << s;
return 0;
}
:::