# bzoj 2005 [Noi2010]能量采集

【样例输入1】

5 4

【样例输入2】

3 4

【样例输出1】

36

【样例输出2】

20

### 题解

\large \begin{aligned} ans &= \sum_{i = 1}^n \sum_{j = 1}^m 2((i,j) – 1) + 1 \\ &= \sum_{i = 1}^n \sum_{j = 1}^m 2(i,j) – 1\\ &= -nm+2\sum_{i = 1}^n \sum_{j = 1}^m (i,j)\\ f(n,m) &= \sum_{i =1}^n\sum_{j = 1}^m(i, j)\\ &= \sum_dd\sum_{i = 1}^{n\over d} \sum_{j = 1}^{m\over d}[(i,j)=1]\\ &= \sum_dd\sum_{i = 1}^{n\over d} \sum_{j = 1}^{m\over d}\sum_{p|(i,j)}\mu(p)\\ &= \sum_dd\sum_p\mu(p){n\over dp}{m\over dp}\\ &= \sum_dd\sum_p\mu(p){n\over T}{m\over T}\\ &= \sum_{T = 1}^{min(n,m)}{n\over T}{m\over T}\sum_{d|T}\mu({T\over d})d\;\;\;\;\;(\mu * id) = \varphi\\ &= \sum_{T = 1}^{min(n,m)}{n\over T}{m\over T}\varphi(T) \end{aligned}

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 100010;
bool p[N + 10];
int n, m;
long long phi[N + 10], pr[N + 10], ans;
void init() {
phi[1] = 1;
for (int i = 2; i <= N; ++i) {
if (!p[i])
pr[++pr[0]] = i, phi[i] = i - 1;
for (int j = 1; j <= pr[0] && i * pr[j] <= N; ++j) {
p[i * pr[j]] = 1;
if (i % pr[j] == 0) {
phi[i * pr[j]] = phi[i] * pr[j];
break;
}
phi[i * pr[j]] = phi[i] * phi[pr[j]];
}
}
for (int i = 1; i <= N; ++i) phi[i] += phi[i - 1];
}
main() {
scanf("%d%d", &n, &m);
init();
if (n > m) swap(n, m);
for (int i = 1, las; i <= n; i = las + 1) {
las = min(n / (n / i), m / (m / i));
ans += 1ll * (n / i) * (m / i) * (phi[las] - phi[i - 1]);
if (las == n) break;
}
printf("%lld\n", 2ll * ans - 1ll * n * m);
}