# 【题解】BZOJ 2005 [Noi2010]能量采集

### Description$Description$$Description$

$\sum _{i=1}^{n}\sum _{j=1}^{m}\left(2gcd\left(i,j\right)-1\right)=2\sum _{i=1}^{n}\sum _{j=1}^{m}gcd\left(i,j\right)-nm$

### Solution$Solution$$Solution$

$\sum _{i=1}^{n}\sum _{j=1}^{m}gcd\left(i,j\right)$

$t=min\left\{n,m\right\}\phantom{\rule{0ex}{0ex}}\sum _{i=1}^{n}\sum _{j=1}^{m}gcd\left(i,j\right)=\sum _{k=1}^{t}f\left(k\right)$

$\left[\frac{n}{x}\right]\cdot \left[\frac{m}{x}\right]$

$\left(2,2\right),\left(2,4\right),\left(2,6\right),\left(2,8\right)\phantom{\rule{0ex}{0ex}}\left(4,2\right),\left(4,4\right),\left(4,6\right),\left(4,8\right)\phantom{\rule{0ex}{0ex}}\left(6,2\right),\left(6,4\right),\left(6,6\right),\left(6,8\right)$

$12$$12$ 对，但是可以枚举得到 $f\left(2\right)=9$$f(2) = 9$ ，满足的数对有：
$\left(2,2\right),\left(2,4\right),\left(2,6\right),\left(2,8\right),\left(4,2\right),\left(4,6\right),\left(6,2\right),\left(6,4\right),\left(6,8\right)$

$f\left(x\right)=\left[\frac{n}{x}\right]\cdot \left[\frac{m}{x}\right]-\sum _{i=2}^{\left[\frac{t}{x}\right]}f\left(i\cdot x\right)$

Tex 代码如下：

\renewcommand{\tab}[1]{\hskip{#1 em}\hskip{#1 em}}
\renewcommand{\func}{\text}
\renewcommand{\for}{\textbf{for} ~~}
\renewcommand{\to}{~~ \textbf{to} ~~}
\renewcommand{\downto}{~~ \textbf{downto} ~~}
\renewcommand{\return}{\textbf{return} ~~}
\renewcommand{\div}{~~ \textbf{div} ~~}
\renewcommand{\type}[1]{\textbf {#1} ~}
\begin{aligned}
& \tab{0} \func{Solve}(\type{int} n, \type{int} m) \\
& \tab{1} ans \leftarrow 0 \\
& \tab{1} t \leftarrow \min \{n, m\} \\
& \tab{1} \for s \leftarrow t \downto 1 \\
& \tab{2} f[s] \leftarrow (n \div s) \times (m \div s) \\
& \tab{2} \for i \leftarrow 2 \to  t \div s \\
& \tab{3} f[s] \leftarrow f[s] - f[i \times s] \\
& \tab{2} ans \leftarrow ans + f[s] \times s \\
& \tab{1} \return ans
\end{aligned}

### Code$Code$$Code$

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define min(x, y) ((x) < (y) ? (x) : (y))
long long f[100005];
int main() {
long long n, m, ans = 0;
scanf("%lld%lld", &n, &m);
long long t = min(n, m);
for (long long s = t; s >= 1; s--) {
f[s] = (n / s) * (m / s);
for (long long i = 2; i * s <= t; i++)
f[s] -= f[i * s];
}
for (long long i = 1; i <= t; i++)
ans += f[i] * i;
printf("%lld\n", 2 * ans - n * m);
return 0;
}

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客