Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

Min_25 筛 模板

求积性函数前缀和。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <cstdio>
#include <cmath>
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0';
flag && (x = ~x + 1);
}
template <class Type, class ...rest>
void read (Type &x, rest &...y) { read(x), read(y...); }
template <class Type>
void write (Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar('0' + x % 10);
}
typedef long long LL;
const int N = 2e5 + 10, mod = 1e9 + 7; // N = 2 sqrt(n)
bool vis[N];
LL n, w[N];
int B, tot, f[N], g[N], sg[N];
int cnt, p[N], id1[N], id2[N];
void init ()
{
vis[1] = true;
for (int i = 2; i < N; ++i)
{
if (!vis[i]) p[++cnt] = i;
for (int j = 1; j <= cnt && i * p[j] < N; ++j)
{
vis[i * p[j]] = true;
if (i % p[j] == 0) break;
}
}
}
int &id (LL x) { return x <= B ? id1[x] : id2[n / x]; }
void solve () // 计算所有质数处的答案,需要保证为完全积性函数
{
for (int i = 1; i <= cnt; ++i)
{
g[i] /*计算质数 p _ i 的值*/;
sg[i] = (sg[i - 1] + g[i]) % mod;
}
for (int i = 1; i <= tot; ++i)
f[i] /*按照计算质数的方法计算 [2, w _ i] 的值*/;
for (int i = 1; i <= cnt && p[i] <= B; ++i) // f _ {i, j} 表示 k \in [2, w_j] 中满足 k \in P or minP(k) > p _ i 的答案和
for (int j = 1; j <= tot && w[j] >= (LL)p[i] * p[i]; ++j)
f[j] = (f[j] - (LL)g[i] * (f[id(w[j] / p[i])] - sg[i - 1])) % mod;
}
int S (LL a, int b)
{
if (p[b] >= a) return 0;
int res = (f[id(a)] - sg[b]) % mod;
for (int i = b + 1; i <= cnt && (LL)p[i] * p[i] <= a; ++i)
{
LL t = p[i];
for (int k = 1; t <= a; ++k, t *= p[i])
res = (res + /*计算 p ^ k 的答案 * */ (S(a / t, i) + (k > 1))) % mod;
}
return res;
}
int main ()
{
init();
read(n); B = sqrt(n);
for (LL l = 1, r; l <= n; l = r + 1)
{
r = n / (n / l);
w[id(n / l) = ++tot] = n / l;
}
solve(); // 拆为若干个完全积性函数分别计算
for (int i = 1; i <= cnt; ++i)
sg[i] = ((LL)sg[i - 1] /* + 计算 p _ i 的答案*/) % mod;
write((S(n, 0)/*计算 1 的答案*/ + mod) % mod);
return 0;
}