Description
Input
输入文件包含多组测试数据。
Output
T行,每行一个整数,表示你所求的答案。
Sample Input
7 4
5 6
Sample Output
121
HINT
1<=N, M<=50000
题解
先给出一个结论: $$ \sigma_0(ij) = \sum_{a | i} \sum_{b | j} [gcd(a, b) = 1]$$
证明(摘自Ken_He):
我们令 $i = p_1^{a_1} p_2^{a_2} \cdots$ , $j = p_1^{b_1} p_2^{b_2} \cdots$ , $d | ij$ 且 $d = p_1^{c_1} p_2^{c_2} \cdots$ , 则 $c_n \le a_n + b_n$ 。
考虑如何不重复地统计每一个 $d$ : 令 $c_n = A_n + B_n$ , 其中 $A_n$ 和 $B_n$ 分别为 $i$ 和 $j$ 对 $c_n$ 的贡献, 则我们要求 \begin{cases}B_n = 0 & A_n < a_n \\B_n \ge 0 & A_n = a_n\end{cases}
这样一来, $c_n$ 的表示形式就变成唯一的了,因而不会被重复统计。我们再考虑如何统计这样的 $A_n$ 和 $B_n$ :我们令 $A_n' = a_n - A_n$ ,则约束条件变为\begin{cases}B_n = 0 & A_n' \ne 0 \\B_n \ge 0 & A_n' = 0\end{cases}
等价于 $gcd(a, b) = 1$ 。
因此得证。
\begin{aligned}ans&=\sum_{i=1}^N\sum_{j=1}^M\sum_{a\mid i}\sum_{b\mid j}[gcd(a,b)=1]\\&=\sum_{i=1}^N\sum_{j=1}^M\sum_{a\mid i}\sum_{b\mid j}\sum_{d\mid gcd(a,b)}\mu(d)\\&=\sum_{a=1}^N\sum_{b=1}^M\sum_{i=1}^{\left\lfloor\frac{N}{a}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{M}{b}\right\rfloor}\sum_{d\mid gcd(a,b)}\mu(d)\\&=\sum_{d=1}^{min\{N,M\}}\mu(d)\sum_{a=1}^{\left\lfloor\frac{N}{d}\right\rfloor}\sum_{b=1}^{\left\lfloor\frac{M}{d}\right\rfloor}\left\lfloor\frac{N}{ad}\right\rfloor\left\lfloor\frac{M}{bd}\right\rfloor\\&=\sum_{d=1}^{min\{N,M\}}\mu(d)\left(\sum_{a=1}^{\left\lfloor\frac{N}{d}\right\rfloor}\left\lfloor\frac{N}{ad}\right\rfloor\right)\left(\sum_{b=1}^{\left\lfloor\frac{M}{d}\right\rfloor}\left\lfloor\frac{M}{bd}\right\rfloor\right)\end{aligned}
设 $t(x)=\sum_{i=1}^x \left\lfloor\frac{x}{i}\right\rfloor$ ,
显然 $$\Rightarrow ans=\sum_{d=1}^{min\{N,M\}}\mu(d)\cdot t\left(\left\lfloor\frac{N}{d}\right\rfloor\right)\cdot t\left(\left\lfloor\frac{M}{d}\right\rfloor\right)$$
显然我们用 $O(n\sqrt n)$ 预处理出函数 $t$ ,再用 $O(T\sqrt n)$ 回答询问即可。
1 //It is made by Awson on 2018.1.22 2 #include <set> 3 #include <map> 4 #include <cmath> 5 #include <ctime> 6 #include <queue> 7 #include <stack> 8 #include <cstdio> 9 #include <string> 10 #include <vector> 11 #include <cstdlib> 12 #include <cstring> 13 #include <iostream> 14 #include <algorithm> 15 #define LL long long 16 #define Abs(a) ((a) < 0 ? (-(a)) : (a)) 17 #define Max(a, b) ((a) > (b) ? (a) : (b)) 18 #define Min(a, b) ((a) < (b) ? (a) : (b)) 19 #define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b)) 20 #define writeln(x) (write(x), putchar('\n')) 21 #define lowbit(x) ((x)&(-(x))) 22 using namespace std; 23 const int N = 50000; 24 void read(int &x) { 25 char ch; bool flag = 0; 26 for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar()); 27 for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar()); 28 x *= 1-2*flag; 29 } 30 void write(LL x) { 31 if (x > 9) write(x/10); 32 putchar(x%10+48); 33 } 34 35 int mu[N+5], t[N+5], n, m; 36 37 void get_mu() { 38 int prime[N+5], isprime[N+5], tot = 0; 39 memset(isprime, 1, sizeof(isprime)); isprime[1] = 0, mu[1] = 1; 40 for (int i = 2; i <= N; i++) { 41 if (isprime[i]) prime[++tot] = i, mu[i] = -1; 42 for (int j = 1; j <= tot && i*prime[j] <= N; j++) { 43 isprime[i*prime[j]] = 0; 44 if (i%prime[j]) mu[i*prime[j]] = -mu[i]; 45 else {mu[i*prime[j]] = 0; break; } 46 } 47 mu[i] += mu[i-1]; 48 } 49 } 50 int get_t(int x) { 51 int ans = 0; 52 for (int i = 1, last; i <= x; i = last+1) { 53 last = x/(x/i); ans += (last-i+1)*(x/i); 54 } 55 return ans; 56 } 57 58 LL cal(int n, int m) { 59 if (n > m) Swap(n, m); LL ans = 0; 60 for (int i = 1, last; i <= n; i = last+1) { 61 last = Min(n/(n/i), m/(m/i)); 62 ans += (LL)(mu[last]-mu[i-1])*t[n/i]*t[m/i]; 63 } 64 return ans; 65 } 66 void work() { 67 read(n), read(m); writeln(cal(n,m)); 68 } 69 int main() { 70 int T; read(T); get_mu(); 71 for (int i = 1; i <= N; i++) t[i] = get_t(i); 72 while (T--) work(); 73 return 0; 74 }
来源:https://www.cnblogs.com/NaVi-Awson/p/8328396.html