【BZOJ3994】【SDOI2015】约数个数和(莫比乌斯反演)

Description

给定$n$,$m$,求$$\sum_{i=1}^{n}\sum_{j=1}^{m}d(i\times j)$$
其中$d(x)$表示$x$的约数个数。

Solution

一个结论:

Conclusion:$$d(nm)=\sum_{i|n}\sum_{j|m}[\gcd(i,j)=1]$$
Proof: $n=p_1^{x_1}p_2^{x^2}\dots$,$m=p_1^{y_1}p_2^{y_2}\dots$,$i=p_1^{a_1}p_2^{a_2}\dots$,$j=p_1^{b_1}p_2^{b_2}\dots$
$\because \gcd(i,j)=1$
$\therefore a_i b_i=0$
$a_i,b_i$一共有$(x_i+y_i+1)$中取值
则总数为$\prod x_i+y_i+1$
即$nm$的约数和

有了这个结论,就可以开始推式子了:
$$\sum_{i=1}^{n}\sum_{j=1}^{m}d(ij)$$
$$=\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{x|i}\sum_{y|j}[\gcd(x,y)=1]$$
$$=\sum_{i=1}^{n}\sum_{j=1}^m\lfloor\frac{n}{i}\rfloor\lfloor\frac{m}{j}\rfloor[\gcd(i,j)=1]$$
设$$f(x)=\sum_{i=1}^{n}\sum_{j=1}^m\lfloor\frac{n}{i}\rfloor\lfloor\frac{m}{j}\rfloor[\gcd(i,j)=x]$$
$$F(x)=\sum_{i=1}^{n}\sum_{j=1}^m\lfloor\frac{n}{i}\rfloor\lfloor\frac{m}{j}\rfloor[x|\gcd(i,j)]=\sum_{i=1}^{\lfloor \frac{n}{x}\rfloor}\sum_{j=1}^{\lfloor \frac{m}{x}\rfloor}\lfloor \frac{n}{xi}\rfloor\lfloor \frac{m}{xj}\rfloor$$
则答案为:$$f(1)=\sum_{x=1}^{min\{n,m\}}\mu(x)\sum_{i=1}^{\lfloor \frac{n}{x}\rfloor}\sum_{j=1}^{\lfloor \frac{m}{x}\rfloor}\lfloor \frac{n}{xi}\rfloor\lfloor \frac{m}{xj}\rfloor=\sum_{x=1}^{min\{n,m\}}\mu(x)\sum_{i=1}^{\lfloor \frac{n}{x}\rfloor}\lfloor \frac{\lfloor \frac{n}{i}\rfloor}{x}\rfloor\sum_{j=1}^{\lfloor \frac{m}{x}\rfloor}\lfloor \frac{\lfloor \frac{m}{j}\rfloor}{x}\rfloor$$
设$g(n)=\sum_{i=1}^{n}\lfloor\frac{n}{i}\rfloor$
则$$f(1)=\sum_{x=1}^{min\{n,m\}}\mu(x)g(\lfloor \frac{n}{x}\rfloor)g(\lfloor \frac{m}{x}\rfloor)$$
对$g(i)$预处理或者要求的时候再算然后存下来。
对$\mu(i)$线性筛出来然后求前缀和。
最后算的时候整除分块即可。

调了一上午,一直RE,把cout改成printf就A掉了??

Source

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
/**************************************
* Au: Hany01
* Date: Feb 2nd, 2018
* Prob: BZOJ3994 & SDOI2015 约数个数和
* Email: hany01@foxmail.com
**************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define fir first
#define sec second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read() {
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
inline void File() {
#ifdef hany01
freopen("bzoj3994.in", "r", stdin);
freopen("bzoj3994.out", "w", stdout);
#endif
}
const int maxn = 50010;
int n, m, mu[maxn], Sum_mu[maxn], pr[maxn >> 1], cnt, np[maxn];
LL Ans, g[maxn];
inline void Get_mu(int n)
{
mu[1] = 1;
for (register int i = 2; i <= n; ++ i) {
if (!np[i]) pr[++ cnt] = i, mu[i] = -1;
for (register int j = 1; j <= cnt && i * pr[j] <= n; ++ j) {
np[i * pr[j]] = 1;
if (i % pr[j]) mu[i * pr[j]] = -mu[i]; else { mu[i * pr[j]] = 0; break; }
}
}
For(i, 1, n) Sum_mu[i] = Sum_mu[i - 1] + mu[i];
}
inline LL Get_g(int n)
{
if (g[n]) return g[n];
for (register int l = 1, r = 1; l <= n; l = r + 1) r = n / (n / l), g[n] += (LL)n / l * (LL)(r - l + 1);
return g[n];
}
inline void Solve()
{
for (register int T = read(); T --; ) {
n = read(), m = read(), Ans = 0;
if (n > m) swap(n, m);
for (register int l = 1, r; l <= n; l = r + 1)
r = min(n / (n / l), m / (m / l)), Ans += Get_g(n / l) * Get_g(m / l) * (LL)(Sum_mu[r] - Sum_mu[l - 1]);
printf("%lld\n", Ans);
}
}
int main()
{
File();
Get_mu(maxn - 10);
Solve();
return 0;
}
Contents
  1. 1. Description
  2. 2. Solution
  3. 3. Source