Description
-
多测,\(T\) 组数据。
-
给定 \(n, m, p\),请求出
\[\left[ \sum_{i = 1}^n \sum_{j = 1}^m \sum_{k = 1}^p \gcd(ij, ik, jk)\cdot \gcd(i, j, k)\cdot \left(\dfrac{\gcd(i, j)}{\gcd(i, k)\cdot \gcd(j, k)} + \dfrac{\gcd(i, k)}{\gcd(i, j)\cdot \gcd(j, k)} + \dfrac{\gcd(j, k)}{\gcd(i, j)\cdot \gcd(i, k)} \right) \right] \bmod (10^9 + 7) \] -
\(T\le 10^3, n, m, p, \le 2\times 10^7\)。
Solution
引理:
\[\gcd(ij,ik,jk) = \dfrac{\gcd(i,j)\gcd(i,k)\gcd(j,k)}{\gcd(i,j,k)} \]证明:
只需证任意一种质因数 \(p\) 的次数相等即可,因为若两个数的任意质因数的次数都相等,则这两个数相等。
设 \(i, j, k\) 分别含 \(a, b, c\) 个质因数 \(p\),不妨设 \(a\le b\le c\)。
则 \(\gcd(ik, ik, jk)\) 中的质因数 \(p\) 的次数为 \(\min(a + b, a + c, b + c) = a + b\);
\(\dfrac{\gcd(i,j)\gcd(i,k)\gcd(j,k)}{\gcd(i,j,k)}\) 中的质因数 \(p\) 的次数为 \(\min(a, b) + \min(a, c) + \min(b, c) - \min(a, b, c) = a + a + b - a = a + b\)。
所以这两个数的质因数 \(p\) 的次数相等。
所以
\[\begin{aligned} ans & = \sum_{i = 1}^n \sum_{j = 1}^m \sum_{k = 1}^p \dfrac{\gcd(i,j)\gcd(i,k)\gcd(j,k)}{\gcd(i,j,k)} \cdot \gcd(i, j, k)\cdot \left(\dfrac{\gcd(i, j)}{\gcd(i, k)\cdot \gcd(j, k)} + \dfrac{\gcd(i, k)}{\gcd(i, j)\cdot \gcd(j, k)} + \dfrac{\gcd(j, k)}{\gcd(i, j)\cdot \gcd(i, k)} \right) \\ & = \sum_{i = 1}^n \sum_{j = 1}^m \sum_{k = 1}^p \gcd(i, j)^2 + \gcd(i, k)^2 + \gcd(j, k)^2 \\ & = p \sum_{i = 1}^n \sum_{j = 1}^m \gcd(i, j)^2 + m \sum_{i = 1}^n \sum_{k = 1}^p \gcd(i, k)^2 + n \sum_{j = 1}^m \sum_{k = 1}^p \gcd(j, k)^2 \end{aligned} \]设
\[f(n, m) = \sum_{i = 1}^n \sum_{j = 1}^m \gcd(i, j)^2 \]则
\[ans = p f(n, m) + m f(n, p) + n f(m, p) \]算 \(f(n, m)\)(不妨设 \(n\le m\))
\[\begin{aligned} f(n, m) & = \sum_{i = 1}^n \sum_{j = 1}^m \gcd(i, j)^2 \\ & = \sum_{d = 1}^n \sum_{i = 1}^n \sum_{j = 1}^m d^2 [\gcd(i, j) = d] \\ & = \sum_{d = 1}^n d^2 \sum_{i = 1}^{\left\lfloor\frac{n}{d}\right\rfloor} \sum_{j = 1}^{\left\lfloor\frac{m}{d}\right\rfloor} [\gcd(i, j) = 1] \\ & = \sum_{d = 1}^n d^2 \sum_{k = 1}^{\left\lfloor\frac{n}{d}\right\rfloor} \mu(k) \left\lfloor\dfrac{n}{dk}\right\rfloor \left\lfloor\dfrac{m}{dk}\right\rfloor \\ & = \sum_{T = 1}^n \left\lfloor\dfrac{n}{T}\right\rfloor \left\lfloor\dfrac{m}{T}\right\rfloor \sum_{k \mid T} \mu(k) \left(\dfrac{T}{k} \right)^2 \\ & = \sum_{T = 1}^n \left\lfloor\dfrac{n}{T}\right\rfloor \left\lfloor\dfrac{m}{T}\right\rfloor (\mu * \operatorname{Id}_2)(T) \end{aligned} \]整除分块即可。
考虑对积性函数 \(g = \mu * \operatorname{Id}_2\) 进行线性筛:
- \(i\in \mathbb{P}\):\(g(i) = p^2 - 1\);
- \(p_j\nmid i\):\(g(i\cdot p_j) = g(i) \cdot g(p_j)\);
- \(p_j\mid i\):显然只有原来对 \(i\) 有贡献的 \(\mu(k)\) 现在还会有贡献,唯一不同的是后面的 \(\left(\dfrac{T}{k}\right)^2\) 多乘了个 \(p_j^2\),即 \(g(i\cdot p_j) = g(i) \cdot p_j^2\)。
时间复杂度为 \(\Omicron(n + T\sqrt{n})\)。
Code
// 18 = 9 + 9 = 18.
#include <iostream>
#include <cstdio>
#define Debug(x) cout << #x << "=" << x << endl
typedef long long ll;
using namespace std;
const int MAXN = 2e7 + 5;
const int N = 2e7;
const int MOD = 1e9 + 7;
typedef int arr[MAXN];
int add(int a, int b) {return (a + b) % MOD;}
int sub(int a, int b) {return (a - b + MOD) % MOD;}
int mul(int a, int b) {return (ll)a * b % MOD;}
arr p, g, sum;
bool vis[MAXN];
void pre()
{
g[1] = sum[1] = 1;
for (int i = 2; i <= N; i++)
{
if (!vis[i])
{
p[++p[0]] = i;
g[i] = mul(i, i) - 1;
}
sum[i] = add(sum[i - 1], g[i]);
for (int j = 1; j <= p[0] && i * p[j] <= N; j++)
{
vis[i * p[j]] = true;
if (i % p[j] == 0)
{
g[i * p[j]] = mul(g[i], mul(p[j], p[j]));
break;
}
g[i * p[j]] = mul(g[i], g[p[j]]);
}
}
}
int GetSum(int l, int r)
{
return sub(sum[r], sum[l - 1]);
}
int f(int n, int m)
{
if (n > m)
{
swap(n, m);
}
int res = 0;
for (int l = 1, r; l <= n; l = r + 1)
{
int k1 = n / l, k2 = m / l;
r = min(n / k1, m / k2);
res = add(res, mul(mul(k1, k2), GetSum(l, r)));
}
return res;
}
int main()
{
pre();
int t;
scanf("%d", &t);
while (t--)
{
int n, m, p;
scanf("%d%d%d", &n, &m, &p);
printf("%d\n", add(add(mul(p, f(n, m)), mul(m, f(n, p))), mul(n, f(m, p))));
}
return 0;
}