Blog of RuSun

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

P3312 [SDOI2014]数表

P3312 [SDOI2014]数表

$$
\begin {aligned}
ANS & = \sum _ {i = 1} ^ n \sum _ {j = 1} ^ m \sigma ((i, j)) [\sigma((i, j)) \le a] \\
& = \sum _ {i = 1} ^ n \sum _ {j = 1} ^ m \sum _ {d = 1} ^ n \sigma(d)[\sigma(d) \le a] [(i, j) = d] \\
& = \sum _ {d = 1} ^ n \sigma (d) [\sigma(d) \le a] \sum _ {i = 1} ^ {\lfloor \frac n d \rfloor} \sum _ {j = 1} ^ {\lfloor \frac n d \rfloor} [(i, j) = 1] \\
& = \sum _ {d = 1} ^ n \sigma (d) [\sigma(d) \le a] \sum _ {i = 1} ^ {\lfloor \frac n d \rfloor} \sum _ {j = 1} ^ {\lfloor \frac n d \rfloor} \sum _ {e | (i, j)} \mu(e) \\
& = \sum _ {d = 1} ^ n \sigma (d) [\sigma(d) \le a] \sum _ {e = 1} ^ n \mu(e) \sum _ {i = 1} ^ {\lfloor \frac n {de} \rfloor} \sum _ {j = 1} ^ {\lfloor \frac n {de} \rfloor} 1\\
& = \sum _ {T = 1} ^ n \lfloor \frac n T \rfloor \lfloor \frac m T \rfloor \sum _ {d | T} \sigma (d) [\sigma(d) \le a] \mu (\frac T d)
\end {aligned}
$$
如果没有后面 $a$ 的限制,那么可以发现后者是一个卷积的形式,$\sigma * \mu = id * 1 * \mu = id * \epsilon = id$ ,那么这个式子将十分优美,并且得到一个在线的 $O(q\sqrt n)$ 的算法。

但是不得不考虑 $a$ 的限制,记 $f(T) = \sum _ {d | T} \sigma (d) [\sigma(d) \le a] \mu (\frac T d)$ ,这是随着 $a$ 的增加而增加的函数,那么考虑将所有询问离线,将 $\sigma$ 和 $a$ 排序,每次询问前考察新增加的贡献,枚举倍数暴力加上。在数论分块中,我们需要快速地查询 $f$ 的前缀和,并且支持修改,考虑树状数组。

复杂度 $O(n \ln n \log n + q \sqrt n \log n)$ 。

查看代码
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <cstdio>
#include <algorithm>
#define fi first
#define se second
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 << 3) + (x << 1) + c - '0';
if (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)
{
if (x < 0) putchar('-'), x = ~x + 1;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
typedef long long LL;
typedef unsigned int UI;
typedef pair <int, int> PII;
const int N = 1e5 + 10, M = 2e4 + 10, mod = (1 << 31) - 1;
int m;
PII d[N];
UI tr[N], ans[M];
int cnt, p[N], low[N], mu[N];
void modify (int x, int k)
{
for (; x < N; x += x & -x)
(tr[x] += k) &= mod;
}
UI query (int x)
{
UI res = 0;
for (; x; x -= x & -x)
(res += tr[x]) &= mod;
return res;
}
struct Query
{
int n, m, a, id;
bool operator < (const Query &_) const { return a < _.a; }
} q[M];
void init ()
{
low[1] = 1;
mu[1] = 1, d[1].fi = 1;
for (int i = 1; i < N; ++i)
{
if (!low[i])
{
p[++cnt] = i;
low[i] = i;
mu[i] = -1, d[i].fi = i + 1;
}
for (int j = 1; j <= cnt && i * p[j] < N; ++j)
{
if (i % p[j] == 0)
{
low[i * p[j]] = low[i] * p[j];
mu[i * p[j]] = 0;
d[i * p[j]].fi = low[i] == i ? i * p[j] + d[i].fi : d[i / low[i]].fi * d[p[j] * low[i]].fi;
break;
}
low[i * p[j]] = p[j];
mu[i * p[j]] = mu[i] * mu[p[j]];
d[i * p[j]].fi = d[i].fi * d[p[j]].fi;
}
d[i].se = i;
}
}
int main ()
{
read(m);
init();
for (int i = 1; i <= m; ++i)
{
read(q[i].n, q[i].m, q[i].a);
q[i].id = i;
}
sort(q + 1, q + m + 1);
sort(d + 1, d + N);
int t = 1;
for (int i = 1; i <= m; ++i)
{
for (; t < N && d[t].fi <= q[i].a; ++t)
for (int j = 1; d[t].se * j < N; ++j)
modify(d[t].se * j, d[t].fi * mu[j]);
UI res = 0;
if (q[i].n > q[i].m) swap(q[i].n, q[i].m);
for (int l = 1, r; l <= q[i].n; l = r + 1)
{
r = min(q[i].n / (q[i].n / l), q[i].m / (q[i].m / l));
(res += ((LL)(query(r) - query(l - 1)) * (q[i].n / l) & mod) * (q[i].m / l) & mod) &= mod;
}
ans[q[i].id] = res;
}
for (int i = 1; i <= m; ++i)
write(ans[i]), puts("");
return 0;
}