P1447: [NOI2010] Energy Harvesting — Solution
Approach
This is a textbook problem that tests knowledge of Möbius inversion and Dirichlet convolution.
Here we stipulate \(n<m\); swapping the two does not affect the answer. Then let's observe the counting pattern: we find that each point contributes at least \(1\) to the answer, and the extra contribution is actually twice the number of lattice points (points with integer coordinates) on the current segment other than the point itself. In other words:
\[ ans = \sum_{i = 1}^n \sum_{j = 1}^m 1 + 2( gcd(i, j) - 1 ) \]
Simplifying a bit, this is:
\[ ans = 2 \left( \sum_{i = 1}^n \sum_{j = 1}^m gcd(i, j) \right) - nm \]
We can focus on \( \left( \sum_{i = 1}^n \sum_{j = 1}^m gcd(i, j) \right) \), and consider directly enumerating \(gcd(i, j) = d\) and then adding a conditional expression:
\[ \sum_{d = 1}^n \sum_{i = 1}^n \sum_{j = 1}^m d [gcd(i, j) = d] \]
Consider pulling out a \(d\), and dividing a \(d\) out of the two sums that follow:
\[ \sum_{d = 1}^n d \sum_{i = 1}^{\lfloor \frac{n}{d} \rfloor} \sum_{j = 1}^{\lfloor \frac{m}{d} \rfloor} [gcd(i, j) = 1] \]
Seeing the classic \(\epsilon\) function, Möbius inversion immediately comes to mind (\(\mu * I = \epsilon\)). Consider substituting:
\[ \sum_{d = 1}^n d \sum_{i = 1}^{\lfloor \frac{n}{d} \rfloor} \sum_{j = 1}^{\lfloor \frac{m}{d} \rfloor} \sum_{h|i, h|j} \mu(h) \]
Consider writing the condition in \( \sum_{h|i, h|j} \mu(h) \) as the indicator expression \( [h|i][h|j] \), and moving the enumeration of \(h\) to the front:
\[ \sum_{d = 1}^n d \sum_{h = 1}^{\lfloor \frac{n}{d} \rfloor} \mu(h) \sum_{i = 1}^{\lfloor \frac{n}{d} \rfloor} \sum_{j = 1}^{\lfloor \frac{m}{d} \rfloor} [h|i][h|j] \]
We find that we can simply divide out \(h\) to simplify:
\[ \begin{gathered} \sum_{d = 1}^n d \sum_{h = 1}^{\lfloor \frac{n}{hd} \rfloor} \mu(h) \sum_{i = 1}^{\lfloor \frac{n}{hd} \rfloor} \sum_{j = 1}^{\lfloor \frac{m}{hd} \rfloor} 1 \\ \sum_{d = 1}^n d \sum_{h = 1}^{\lfloor \frac{n}{hd} \rfloor} \mu(h) \lfloor \frac{n}{hd} \rfloor \lfloor \frac{m}{hd} \rfloor \end{gathered} \]
Consider enumerating \(T = hd\), and mind the conditions:
\[ \sum_{d = 1}^n d \sum_{d|T}^n \mu(\lfloor \frac{T}{d} \rfloor) \lfloor \frac{n}{T} \rfloor \lfloor \frac{m}{T} \rfloor \]
Consider changing the order of summation, swapping the enumeration of \(T\) and \(d\):
\[ \sum_{T = 1}^n \sum_{d|T} d \mu(\lfloor \frac{T}{d} \rfloor) \lfloor \frac{n}{T} \rfloor \lfloor \frac{m}{T} \rfloor \]
After pulling the factors out, we find a magical property:
\[ \sum_{T = 1}^n \lfloor \frac{n}{T} \rfloor \lfloor \frac{m}{T} \rfloor \sum_{d|T} d \mu(\frac{T}{d}) \]
Isn't this just a convolution: \( \sum_{d|T} d \mu(\frac{T}{d}) \)? Recall that \(id * \mu = \varphi\) (if you don't remember, here is a proof), and substitute \(\varphi\) without hesitation:
\[ \sum_{T = 1}^n \lfloor \frac{n}{T} \rfloor \lfloor \frac{m}{T} \rfloor \varphi(T) \]
After divisor-block decomposition, this problem is done.
Code
// P1447.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 101000;
ll phi[MAX_N], prime[MAX_N], n, m, tot;
bool vis[MAX_N];
int main()
{
scanf("%lld%lld", &n, &m);
if (n > m)
swap(n, m);
phi[1] = 1;
for (int i = 2; i < MAX_N; i++)
{
if (!vis[i])
phi[prime[++tot] = i] = i - 1;
for (int j = 1; j <= tot && i * prime[j] < MAX_N; j++)
{
vis[i * prime[j]] = true;
if (i % prime[j] == 0)
{
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
phi[i * prime[j]] = phi[i] * phi[prime[j]];
}
}
for (int i = 1; i < MAX_N; i++)
phi[i] += phi[i - 1];
ll ans = 0;
for (ll T = 1, gx; T <= n; T = gx + 1)
{
gx = min(n / (n / T), m / (m / T));
ans += (phi[gx] - phi[T - 1]) * (n / T) * (m / T);
}
ans = 2 * ans - n * m;
printf("%lld", ans);
return 0;
}
tql%%%