P1447 [NOI2010] 能量采集
求
$$
\begin {aligned}
& \sum _ {i = 1} ^ n \sum _ {j = 1} ^ m 2(i, j) - 1 \\
= & 2\sum _ {d = 1} ^ n \varphi (d) \lfloor \frac n d \rfloor \lfloor \frac m d \rfloor - nm
\end {aligned}
$$
还是数论分块。
查看代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| #include <cstdio> #include <algorithm> using namespace std; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; if (flag) x = ~x + 1; } template <class Type, class ...rest> void read(Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write(Type x) { if (x < 0) putchar('-'), x = ~x + 1; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } typedef long long LL; const int N = 1e5 + 10; int n, m; LL f[N]; int cnt, p[N]; bool vis[N]; void init() { f[1] = 1; vis[1] = true; for (int i = 2; i < N; ++i) { if (!vis[i]) p[++cnt] = i, f[i] = i - 1; for (int j = 1; i * p[j] < N; ++j) { vis[i * p[j]] = true; if (i % p[j] == 0) { f[i * p[j]] = f[i] * p[j]; break; } f[i * p[j]] = f[i] * f[p[j]]; } } for (int i = 2; i < N; ++i) f[i] += f[i - 1]; } int main () { init(); read(n, m); if (n > m) swap(n, m); LL res = 0; for (int l = 1, r; l <= n; l = r + 1) { r = min(n / (n / l), m / (m / l)); res += (LL)(f[r] - f[l - 1]) * (n / l) * (m / l); } res = res * 2 - (LL)n * m; write(res); return 0; }
|