---
id: 1289
lang: en
translation_of: index.md
source_sha256: b65400bf73aad48d9f7d7cdd1aec26975c9ffa081a8ced1bf9de838e8292ce9b
title: "P1447: [NOI2010] Energy Harvesting — Solution"
description: "This is a textbook problem that tests knowledge of Möbius inversion and Dirichlet convolution."
tags: ["OI","Number Theory","Möbius Inversion","Divisor-Block Decomposition","Dirichlet Convolution","Convolution","NOI"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "Problem name 能量采集 rendered as “Energy Harvesting” (literally “energy collection”)."
  - "带入 (sic) read as 代入, “substitute”."
  - "条件表达式 rendered as “conditional expression” and 判别表达式 as “indicator expression”."
---

## Approach

This is <span class="slang" data-zh="裸题" data-gloss="luǒtí, “naked problem”. A problem that is a direct application of a standard algorithm.">a textbook problem</span> 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 )
$$

<!--more-->

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](../1230-mobius-inversion-and-du-sieve/index.en.html#mobius-dirichlet-1)), 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

```cpp
// 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;
}
```
