---
id: 1035
lang: en
translation_of: index.md
source_sha256: ad2fb4e32846c6401072c2eb7b8e4a143d6dd4aa00a8712f211917cb3b38ec34
title: "P1829: [National Training Team] Crash's Number Table — Solution"
description: "Assume N < M, write out the answer's formula, enumerate the gcd, simplify with Möbius inversion, then cut the complexity with divisor blocks."
tags: ["OI","Number Theory","Möbius Inversion","Divisor-Block Decomposition"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "Problem name Crash 的数字表格 rendered as “Crash's Number Table”; Crash is the character's name as written."
  - "那些地方 read as 哪些地方 (sic), “which places”."
  - "数列求和的形式 rendered as “the form of a series sum”."
---

## Main Idea

First, we stipulate that $N<M$. Then write out the expression that counts the answer:

$$
\begin{gathered} \sum_{i=1}^N \sum_{j=1}^M lcm(i,j) \\ \sum_{i=1}^N \sum_{j=1}^M \frac{ij}{\gcd(i,j)} \end{gathered}
$$

Then we can try enumerating the greatest common divisor $gcd(i,j)=d$, and then let $i,j$ become the only two coprime factors.

<!--more-->

$$
\begin{gathered} \sum_{d=1}^N \sum_{i=1}^{\frac{N}{d}} \sum_{j=1}^{\frac{M}{d}} \frac{id*jd}{d}[gcd(i,j)=1] \\ \sum_{d=1}^N d \sum_{i=1}^{\frac{N}{d}} \sum_{j=1}^{\frac{M}{d}} ij \sum_{x|gcd(i,j)} \mu(x) \end{gathered}
$$

Then enumerate $gcd(i,j) = x$ once again:

$$
\begin{gathered} \sum_{d=1}^N d \sum_{x=1}^{\frac{N}{d}} \mu(x) \sum_{i=1}^{\frac{N}{x}} \sum_{j=1}^{\frac{M}{x}} i x^2 j \\ \sum_{d=1}^N d \sum_{x=1}^{\frac{N}{d}} \mu(x) x^2 (\sum_{i=1}^{\frac{N}{x}} i) (\sum_{j=1}^{\frac{M}{x}} j) \end{gathered}
$$

We can use divisor-block decomposition to bring the complexity down: precompute the prefix sums of $\mu(x)x^2$, and write $(\sum_{i=1}^{\frac{N}{x}} i) (\sum_{j=1}^{\frac{M}{x}} j)$ in the form of a series sum:

$$
\sum_{d=1}^N d \sum_{x=1}^{\frac{N}{d}} \mu(x) x^2 \frac{(\frac{N}{x}+1)\frac{N}{x}}{2} \frac{\frac{M}{x}(\frac{M}{x}+1)}{2}
$$

To make it easier to understand, define two functions $calc(a,b),g(a,b)$:

$$
\begin{gathered} g(a,b) = \frac{a(a+1)}{2} \frac{b(b+1)}{2} \\ calc(a,b) = \sum_{x=1}^{a} \mu(x) x^2 * g(\lfloor \frac{a}{x} \rfloor, \lfloor \frac{b}{x} \rfloor) \\ ans = \sum_{d=1}^N d*calc(\frac{N}{d},\frac{N}{d}) \end{gathered}
$$

This way you can see, simply and clearly, which places need divisor-block decomposition.

## Code

```cpp
// P1829.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 1e7 + 100, mod = 20101009;
ll n, m, mu[MAX_N], musum[MAX_N], prime[MAX_N], tot;
bool vis[MAX_N];
ll g(ll a, ll b) { return (((a * (a + 1)) / 2) % mod) * (((b * (b + 1)) / 2) % mod) % mod; }
ll sum(ll a, ll b)
{
    ll ans = 0;
    for (ll x = 1, y = 0; x <= a; x = y + 1)
    {
        y = min(a / (a / x), b / (b / x));
        ans = (ans + (musum[y] - musum[x - 1] + mod) % mod * g(a / x, b / x) % mod) % mod;
    }
    return ans;
}
int main()
{
    scanf("%lld%lld", &n, &m);
    if (n > m)
        swap(n, m);
    mu[1] = 1;
    for (ll i = 2; i < MAX_N; i++)
    {
        if (!vis[i])
            prime[++tot] = i, mu[i] = -1;
        for (ll j = 1; j <= tot && i * prime[j] < MAX_N; j++)
        {
            vis[i * prime[j]] = true;
            if (i % prime[j] == 0)
                break;
            else
                mu[i * prime[j]] = -mu[i];
        }
    }
    for (ll i = 1; i < MAX_N; i++)
        musum[i] = (musum[i - 1] + (mu[i] + mod) % mod * (i * i % mod) % mod) % mod;
    ll ans = 0;
    for (ll x = 1, y = 0; x <= n; x = y + 1)
    {
        y = min(n / (n / x), m / (m / x));
        ans = (ans + (y - x + 1) * (y + x) / 2 % mod * sum(n / x, m / x) % mod) % mod;
    }
    printf("%lld", ans);
    return 0;
}
```
