# BZOJ2005 [Noi2010]能量采集

【样例输入1】
5 4
【样例输入2】
3 4

【样例输出1】
36
【样例输出2】
20

## 题解

\begin{aligned} ans &= \sum_{i, j} 2(gcd(i,j)-1)+1\\ &= 2\left(\sum_{i, j}gcd(i, j)\right)-nm\\ &= 2\left(\sum_{d}d\sum_{i, j}\left[gcd(i, j)=d\right]\right)-nm\\ &= 2\left(\sum_{d}d\sum_{d|t}\mu\left(\frac td\right)\sum_{i, j}\left[t|gcd(i, j)\right]\right)-nm\\ &= 2\left(\sum_{d}d\sum_{d|t}\mu\left(\frac td\right)\left\lfloor\frac nt\right\rfloor\left\lfloor\frac mt\right\rfloor\right)-nm\\ &= 2\left(\sum_{t}\left\lfloor\frac nt\right\rfloor\left\lfloor\frac mt\right\rfloor\sum_{d|t}d\mu\left(\frac td\right)\right)-nm\\ &= 2\left(\sum_{t}\left\lfloor\frac nt\right\rfloor\left\lfloor\frac mt\right\rfloor\phi(t)\right)-nm \end{aligned}

#include <algorithm>
#include <cstdio>
#include <cstring>
typedef long long LL;
const int N = 100050;
int phi[N];
void getPhi() {
for (int i = 1; i < N; ++i) phi[i] = i;
for (int i = 2; i < N; ++i) if (phi[i] == i)
for (int j = i; j < N; j += i)
phi[j] -= phi[j] / i;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
LL ans = 0;
getPhi();
for (int i = 1; i <= n && i <= m; ++i)
ans += (LL)phi[i] * (n / i) * (m / i);
printf("%lld\n", ans * 2 - (LL)n * m);
return 0;
}


posted @ 2017-08-04 19:11  _rqy  阅读(167)  评论(0编辑  收藏  举报