---
id: 1901
lang: en
translation_of: index.md
source_sha256: a119774f2d54b2643a267e78fe3a541e594bfaea892ae0a438a02919f0b24687
title: "P5221: Product — Solution"
description: "First, let's do the usual standard trick and turn the enumerated term into gcd."
tags: ["OI","Euler's Totient Function"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "仪仗队 is the name of a classic problem (SDOI2008, Luogu P2158), rendered as “Guard of Honor”."
---

## Derivation

First, let's do the usual <span class="slang" data-zh="套路" data-gloss="tàolù, “routine, set pattern”. OI slang for a standard, well-known trick that works on a whole class of problems.">standard trick</span> and turn the enumerated term into $\gcd$.

$$
\begin{aligned} \prod_{i = 1}^n \prod_{j = 1}^n \frac{lcm(i, j)}{gcd(i, j)} &= \prod_{i = 1}^n \prod_{j = 1}^n \frac{ij}{gcd(i, j)^2} \\ &= \prod_{d = 1}^n d^{\sum_{i = 1}^n \sum_{j = 1}^n [\gcd(i, j) = d]} \end{aligned}
$$

<!--more-->

Notice that this expression in the upper-right corner is actually just <span class="slang" data-zh="仪仗队" data-gloss="yízhàngduì, “honor guard”. A classic problem (SDOI2008) that counts coprime pairs in a grid using Euler's totient function.">Guard of Honor</span>:

$$
\begin{aligned} \sum_{i = 1}^n \sum_{j = 1}^n [\gcd(i, j) = d] &= \sum_{i = 1}^{\lfloor \frac{n}{d} \rfloor} \sum_{j = 1}^{\lfloor \frac{n}{d} \rfloor} [\gcd(i, j) = 1] \\ &= (2\sum_{i = 1}^{\lfloor \frac{n}{d} \rfloor} \varphi(i)) - 1 \end{aligned}
$$

Done. <span class="slang" data-zh="卡下空间" data-gloss="kǎ xià kōngjiān, “jam the space a bit”. Trimming a solution's memory use so it fits under the memory limit.">Squeeze the memory</span> a little, then handle the modulus with Fermat's little theorem, and that's it.

## Code

```cpp
// P5221.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e6 + 5, mod = 104857601;

int primes[MAX_N >> 1], tot, phi[MAX_N], n;
bitset<MAX_N> vis;

int quick_pow(int bas, int tim)
{
    int ret = 1;
    while (tim)
    {
        if (tim & 1)
            ret = 1LL * ret * bas % mod;
        bas = 1LL * bas * bas % mod;
        tim >>= 1;
    }
    return ret;
}

int main()
{
    scanf("%d", &n), phi[1] = 1;
    for (int i = 2; i <= n; i++)
    {
        if (!vis[i])
            primes[++tot] = i, phi[i] = (i - 1);
        for (int j = 1; j <= tot && 1LL * i * primes[j] <= n; j++)
        {
            vis[i * primes[j]] = true;
            if (i % primes[j] == 0)
            {
                phi[i * primes[j]] = phi[i] * primes[j];
                break;
            }
            phi[i * primes[j]] = phi[i] * phi[primes[j]];
        }
    }
    for (int i = 1; i <= n; i++)
        phi[i] = (2LL * phi[i] + phi[i - 1]) % (mod - 1);
    int ans = 1, tmp = 1;
    for (int i = 1; i <= n; i++)
        ans = 1LL * ans * i % mod;
    ans = quick_pow(ans, n << 1);
    for (int d = 2; d <= n; d++)
        tmp = 1LL * tmp * quick_pow(d, phi[n / d] - 1) % mod;
    printf("%lld\n", 1LL * ans * quick_pow(1LL * tmp * tmp % mod, mod - 2) % mod);
    return 0;
}
```
