【題解】BZOJ-4176 Lucas的數論
阿新 • • 發佈:2022-01-18
Description
-
給定整數 \(n\),求
\[\left[\sum_{i = 1}^n \sum_{j = 1}^n d(ij)\right] \bmod (10^9 + 7) \] -
對於 \(100\%\) 的資料 \(n\le 10^9\)。
Solution
\[\begin{aligned} \sum_{i = 1}^n \sum_{j = 1}^n d(ij) & = \sum_{i = 1}^n \sum_{j = 1}^n \sum_{x\mid i} \sum_{y\mid j} [\gcd(x, y) = 1] \\ & = \sum_{x = 1}^n \sum_{y = 1}^n [\gcd(x, y) = 1] \sum_{i = 1}^n [x\mid i] \sum_{j = 1}^n [y\mid j] \\ & = \sum_{x = 1}^n \sum_{y = 1}^n [\gcd(x, y) = 1] \left\lfloor\dfrac{n}{x}\right\rfloor \left\lfloor\dfrac{n}{y}\right\rfloor \\ & = \sum_{d = 1}^n \mu(d) \sum_{x = 1}^n [d\mid x] \left\lfloor\dfrac{n}{x}\right\rfloor \sum_{y = 1}^n [d\mid y] \left\lfloor\dfrac{n}{y}\right\rfloor \\ & = \sum_{d = 1}^n \mu(d) \left(\sum_{i = 1}^{\left\lfloor\frac{n}{d}\right\rfloor} \left\lfloor\dfrac{n}{di}\right\rfloor\right)^2 & (1)\\ & = \sum_{d = 1}^n \mu(d) \left(\sum_{i = 1}^{\left\lfloor\frac{n}{d}\right\rfloor} d(i)\right)^2 & (2) \end{aligned} \]\((1)\)
\(\mu\) 用杜教篩,閾值為 \(10^6\)。
預處理 \(d\) 的字首和。
以 \(k\) 為閾值,當 \(\left\lfloor\dfrac{n}{d}\right\rfloor \le k\) 時用 \((2)\) 的公式,以預處理過的 \(d\) 的字首和來計算;當 \(\left\lfloor\dfrac{n}{d}\right\rfloor > k\) 時用 \((1)\) 中的整除分塊計算。
那麼預處理是 \(\Omicron(k)\) 的。
第一層整除分塊中,若 \(\left\lfloor\dfrac{n}{d}\right\rfloor \le k\)
第一層整除分塊中,若 \(\left\lfloor\dfrac{n}{d}\right\rfloor > k\),即 \(d < \left\lfloor\dfrac{n}{k}\right\rfloor\),那麼就會用整除分塊來計算,一共是 \(\sum_{d = 1}^{\left\lfloor\frac{n}{k}\right\rfloor} \sqrt{\left\lfloor\dfrac{n}{d}\right\rfloor}\)
也就是 \(\Omicron\left(\dfrac{n}{\sqrt{k}}\right)\) 的。
綜上,總時間複雜度為 \(\Omicron\left(k + \sqrt{n} + \dfrac{n}{\sqrt{k}} \right)\),\(k\) 大約取 \(n^{\frac{2}{3}}\) 時最優,和杜教篩一樣。
Code
// 18 = 9 + 9 = 18.
#include <iostream>
#include <cstdio>
#include <unordered_map>x
#define Debug(x) cout << #x << "=" << x << endl
typedef long long ll;
using namespace std;
const int MAXN = 1e6 + 5;
const int N = 1e6;
const int MOD = 1e9 + 7;
int p[MAXN], mu[MAXN], sum_mu[MAXN], d[MAXN], sum_d[MAXN], num[MAXN];
bool vis[MAXN];
void pre()
{
mu[1] = sum_mu[1] = d[1] = sum_d[1] = 1;
for (int i = 2; i <= N; i++)
{
if (!vis[i])
{
p[++p[0]] = i;
mu[i] = -1;
d[i] = 2;
num[i] = 1;
}
for (int j = 1; j <= p[0] && i * p[j] <= N; j++)
{
vis[i * p[j]] = true;
if (i % p[j] == 0)
{
mu[i * p[j]] = 0;
d[i * p[j]] = d[i] / (num[i] + 1) * (num[i] + 2);
num[i * p[j]] = num[i] + 1;
break;
}
mu[i * p[j]] = mu[i] * mu[p[j]];
d[i * p[j]] = d[i] * d[p[j]];
num[i * p[j]] = 1;
}
sum_mu[i] = sum_mu[i - 1] + mu[i];
sum_d[i] = (sum_d[i - 1] + d[i]) % MOD;
}
}
unordered_map<int, int> dp_mu;
int sublinear_mu(int n)
{
if (n <= N)
{
return sum_mu[n];
}
if (dp_mu.find(n) != dp_mu.end())
{
return dp_mu[n];
}
int res = 1;
for (int l = 2, r; l <= n; l = r + 1)
{
int k = n / l;
r = n / k;
res = (res - (ll)(r - l + 1) * sublinear_mu(k) % MOD + MOD) % MOD;
}
return dp_mu[n] = res;
}
int getsum_mu(int l, int r)
{
return (sublinear_mu(r) - sublinear_mu(l - 1) + MOD) % MOD;
}
int getsum_d(int n)
{
if (n <= N)
{
return sum_d[n];
}
int res = 0;
for (int l = 1, r; l <= n; l = r + 1)
{
int k = n / l;
r = n / k;
res = (res + (ll)(r - l + 1) * k % MOD) % MOD;
}
return res;
}
int block(int n)
{
int res = 0;
for (int l = 1, r; l <= n; l = r + 1)
{
int k = n / l;
r = n / k;
int tmp = getsum_d(k);
res = (res + (ll)getsum_mu(l, r) * tmp % MOD * tmp % MOD) % MOD;
}
return res;
}
int main()
{
pre();
int n;
scanf("%d", &n);
printf("%d\n", block(n));
return 0;
}