Blog of RuSun

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

P5572 [CmdOI2019]简单的数论题

P5572 [CmdOI2019]简单的数论题

$$
\begin {aligned}
\sum _ {i = 1} ^ n \sum _ {j = 1} ^ m \varphi (\frac {\mathrm{lcm}(i, j)} {(i, j)}) & = \sum _ {i = 1} ^ n \sum _ {j = 1} ^ m \varphi (\frac i {(i, j)} \frac j {(i, j)}) \\
& = \sum _ {i = 1} ^ n \sum _ {j = 1} ^ m \varphi(\frac i {(i, j)})\varphi(\frac j {(i, j)}) \\
& = \sum _ {d = 1} ^ n \sum _ {i = 1} ^ n \sum _ {j = 1} ^ m \varphi(\frac i d)\varphi(\frac j d) [(i, j) = d] \\
& = \sum _ {d = 1} ^ n \sum _ {i = 1} ^ {\lfloor \frac n d \rfloor } \sum _ {j = 1} ^ {\lfloor \frac m d \rfloor } \varphi(i)\varphi(j) [(i, j) = 1] \\
& = \sum _ {d = 1} ^ n \sum _ {i = 1} ^ {\lfloor \frac n d \rfloor } \sum _ {j = 1} ^ {\lfloor \frac m d \rfloor } \varphi(i)\varphi(j) \sum _ {k | (i, j)} \mu(k) \\
& = \sum _ {d = 1} ^ n \sum _{k = 1} ^ n \mu(k) \sum _ {i = 1} ^ {\lfloor \frac n {kd} \rfloor } \sum _ {j = 1} ^ {\lfloor \frac m {kd} \rfloor } \varphi(ik)\varphi(jk) \\
& = \sum _ {T = 1} ^ n \sum _ {d | T} \mu(d) \sum _ {i = 1} ^ {\lfloor \frac n T \rfloor } \varphi(id) \sum _ {j = 1} ^ {\lfloor \frac m T \rfloor } \varphi(jd) \\
\end {aligned}
$$

记 $g(a, b) = \sum _ {i = 1} ^ b \varphi(ia)$ ,状态共 $n \ln n$ 种,可以直接预处理出来。这样暴力可以得到一个 $O(Tn\ln n)$ 的算法。

记 $c(x, i, j) = \sum _ {d | x} \mu(d) g(d, \lfloor \frac n x \rfloor) g(d, \lfloor \frac m x \rfloor)$ 。

考虑数论分块,对于每一组 $(\lfloor \frac n x \rfloor, \lfloor \frac m x \rfloor)$ ,维护 $c(x, \lfloor \frac n x \rfloor, \lfloor \frac m x \rfloor)$ 的前缀和,时空复杂度为 $O(B n \ln B)$。考虑维护 $\lfloor \frac n x \rfloor < B$ 的部分,剩下的暴力计算,将 $i$ 的约数个数级别视为 $\sqrt i$ ,那么复杂度级别为 $O((\frac n B) ^ {\frac 3 2})$。$B = \sqrt[3]T$ 左右最优。

查看代码
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
#include <cstdio>
#include <vector>
#define pb push_back
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);
}
const int N = 5e4 + 10, B = 35, mod = 23333;
bool vis[N];
int cnt, p[N], mu[N], phi[N];
vector <int> g[N], f[N], h[B][B];
int calc (int x, int i, int j)
{
int res = 0;
for (int d : f[x])
res = (res + mu[d] * g[d][i] * g[d][j]) % mod;
return res;
}
void init ()
{
for (int i = 1; i < N; ++i)
for (int j = 1; i * j < N; ++j)
f[i * j].pb(i);
vis[1] = true;
mu[1] = 1, phi[1] = 1;
for (int i = 2; i < N; ++i)
{
if (!vis[i])
{
p[++cnt] = i;
mu[i] = -1, phi[i] = i - 1;
}
for (int j = 1; j <= cnt && i * p[j] < N; ++j)
{
vis[i * p[j]] = true;
if (i % p[j] == 0)
{
mu[i * p[j]] = 0, phi[i * p[j]] = phi[i] * p[j];
break;
}
mu[i * p[j]] = -mu[i], phi[i * p[j]] = phi[i] * phi[p[j]];
}
}
for (int i = 1; i < N; ++i)
{
g[i].resize(N / i + 1);
g[i][0] = 0;
for (int j = 1; i * j < N; ++j)
g[i][j] = (g[i][j - 1] + phi[i * j]) % mod;
}
for (int i = 1; i < B; ++i)
for (int j = i; j < B; ++j)
{
h[i][j].resize(N / j + 1);
h[i][j][0] = 0;
for (int k = 1; j * k < N; ++k)
h[i][j][k] = (h[i][j][k - 1] + calc(k, i, j)) % mod;
}
}
int main ()
{
init();
int T; read(T);
for (int n, m; T; --T)
{
read(n, m);
if (n > m) swap(n, m);
int res = 0;
for (int i = 1; i <= m / B; ++i)
res = (res + calc(i, n / i, m / i)) % mod;
for (int l = m / B + 1, r; l <= n; l = r + 1)
{
int a = n / l, b = m / l;
r = min(n / a, m / b);
res = (res + h[a][b][r] - h[a][b][l - 1]) % mod;
}
write((res + mod) % mod), puts("");
}
return 0;
}