# LOJ 2085: 洛谷 P1587: bzoj 4652: 「NOI2016」循环之美

### 题解：

\begin{aligned}\mathbf{Ans}&=\sum_{i=1}^{n}\sum_{j=1}^{m}[i\perp j][j\perp k]\\&=\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{d|i,d|j}\mu(d)[j\perp k]\\&=\sum_{d=1}^{\min(n,m)}\mu(d)\sum_{i=1}^{n\div d}\sum_{j=1}^{m\div d}[jd\perp k]\\&=\sum_{d=1}^{\min(n,m)}\mu(d)(n\div d)\sum_{j=1}^{m\div d}[j\perp k][d\perp k]\\&=\sum_{d=1}^{\min(n,m)}\mu(d)[d\perp k](n\div d)S_{[x\perp k]}(m\div d)\end{aligned}

$$S_{[x\perp k]}(n)=(n\div k)\varphi(k)+S_{[x\perp k]}(n\bmod k)$$ 可以 $$\mathcal{O}(k)$$ 预处理，$$\mathcal{O}(1)$$ 回答询问。

$$\displaystyle S(n,k)=\sum_{i=1}^{n}\mu(i)[i\perp k]$$，则有：

\begin{aligned}S(n,k)&=\sum_{i=1}^{n}\mu(i)[i\perp k]\\&=\sum_{i=1}^{n}\mu(i)\sum_{d|i,d|k}\mu(d)\\&=\sum_{d|k}\mu(d)\sum_{i=1}^{n\div d}\mu(id)\\&=\sum_{d|k}\mu(d)\sum_{i=1}^{n\div d}\mu(i)\mu(d)[i\perp d]\\&=\sum_{d|k}\mu^2(d)\sum_{i=1}^{n\div d}\mu(i)[i\perp d]\\&=\sum_{d|k}\mu^2(d)S(n\div d,d)\end{aligned}

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>

typedef long long LL;
const int MK = 2005;
const int S = 31622;
const int MN23 = 1000005;
const int MP = 78505;
const int MD = 25;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

bool ip[MN23];
int p[MP], pc;
int mu[MN23], Smu[MN23];

inline void Init(int N) {
mu[1] = 1;
for (int i = 2; i <= N; ++i) {
if (!ip[i]) p[++pc] = i, mu[i] = -1;
for (int j = 1; j <= pc && p[j] * i <= N; ++j) {
ip[p[j] * i] = 1;
if (i % p[j]) mu[p[j] * i] = -mu[i];
else break;
}
}
for (int i = 1; i <= N; ++i) Smu[i] = Smu[i - 1] + mu[i];
}

int N, M, K, N23;
int A[MK], Vl[MD], cd;

std::map<int, LL> mp[MD];

LL Sum(int N, int K) {
if (!N) return 0;
if (K == 1 && N <= N23) return Smu[N];
if (mp[K].count(N)) return mp[K][N];
if (K > 1) {
LL Ans = 0;
for (int j = 1; j <= K; ++j)
if (Vl[K] % Vl[j] == 0 && mu[Vl[j]])
Ans += Sum(N / Vl[j], j);
return mp[K][N] = Ans;
}
LL Ans = 1;
for (int i = 2, j; i <= N; i = j + 1) {
j = N / (N / i);
Ans -= (j - i + 1) * Sum(N / i, 1);
}
return mp[1][N] = Ans;
}

LL Ans;

int main() {
scanf("%d%d%d", &N, &M, &K);
for (int i = 1; i <= K; ++i) A[i] = A[i - 1] + (gcd(i, K) == 1);
Init(N23 = std::max((int)pow(N, 2./3), K));
for (int i = 1; i <= K; ++i) if (K % i == 0 && (mu[i] || i == K)) Vl[++cd] = i;
for (int i = 1, kN, kM, j; i <= N && i <= M; i = j + 1) {
kN = N / i, kM = M / i;
j = std::min(N / kN, M / kM);
Ans = Ans + kN * ((LL)kM / K * A[K] + A[kM % K]) * (Sum(j, cd) - Sum(i - 1, cd));
}
printf("%lld\n", Ans);
return 0;
}

posted @ 2019-07-10 02:20  粉兔  阅读(571)  评论(0编辑  收藏  举报