---
id: 837
lang: en
translation_of: index.md
source_sha256: 0afb07d786151d4fa12271adc0c1105975d924771008b13425eba363f324be75
title: "POJ2480: Longge's Problem — Solution"
description: "The statement is refreshingly clean, but deriving the formulas was heartbreaking. At first I tried printing tables and never found the pattern."
tags: ["OI","Math","Euler's Totient Function","Factorization"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "The TeX \\phi(因数) is kept verbatim (it is not inside \\text{}), so Chinese characters remain inside that formula."
  - "欧拉定理 rendered literally as “Euler's theorem”, as the author wrote, without correcting it."
  - "打表的方法 chipped as a phrase because the author uses 打表 in the sense of 打表找规律 (printing small cases to find a pattern), not the glossary's hardcoding sense."
---

## Main Idea and Conclusions

> Given $n$, compute $\sum_{i=1}^{n} \gcd(i,n)$.

The statement is really <span class="slang" data-zh="小清新" data-gloss="xiǎo qīngxīn, “small and fresh”. Internet slang for something light, clean and pleasant; here: neat and simple.">refreshingly clean</span>, but deriving the formulas was heartbreaking. At first I used <span class="slang" data-zh="打表的方法" data-gloss="dǎbiǎo de fāngfǎ, “the make-a-table method”. Here: brute-forcing answers for small inputs and looking for a pattern in them.">the table-printing approach</span>, and never managed to find the pattern in it. So the ability to derive formulas really is quite important.

Let's analyze it. For $i\in [1,n], gcd(i,n)=1$, their contribution to the answer is $\phi(n)$. Next, look at the other part, that is, $j\in [1,n], gcd(j,n)=k, k \neq 1$. We can divide it out, turning it into $k*gcd(\frac{j}{k},\frac{n}{k})=k, gcd(\frac{j}{k},\frac{n}{k})=1$, where by Euler's theorem there are $\phi(k)$ such $gcd$, so we derive $k*\phi(k)$. In the end, the answer just needs to sum the divisors of $k$ times $\phi(因数)$.

Finally, you can consider using a map to record the Euler values already computed, which gives a constant-factor optimization.

## Code

```cpp
// POJ2480.cpp
#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#define ll long long
using namespace std;
map<ll, ll> prefix;
vector<ll> getFracted(ll x)
{
    vector<ll> res;
    for (ll i = 1; i * i <= x; i++)
        if (x % i == 0)
        {
            res.push_back(i);
            if (i != x / i)
                res.push_back(x / i);
        }
    return res;
}
ll phi(ll x)
{
    ll ans = x;
    for (ll i = 2; i * i <= x; i++)
        if (x % i == 0)
        {
            ans -= ans / i;
            while (x % i == 0)
                x /= i;
        }
    if (x > 1)
        ans -= ans / x;
    return ans;
}
int main()
{
    ll n;
    while (scanf("%lld", &n) != EOF)
    {
        ll ans = 0;
        vector<ll> container = getFracted(n);
        ll siz = container.size();
        for (int i = 0; i < siz; i++)
        {
            if (!prefix.count(n / container[i]))
                prefix[n / container[i]] = phi(n / container[i]);
            ll e = prefix[n / container[i]];
            ans += e * container[i];
        }
        printf("%lld\n", ans);
    }
    return 0;
}
```

src: [https://www.cnblogs.com/neopenx/p/4095691.html](https://www.cnblogs.com/neopenx/p/4095691.html)
