Skip to content

1 min read中文

Editorial | "HDU 3501" Calculation 2

Editorial for HDU 3501, using Euler's totient function to prove that the sum of numbers less than n and coprime to n equals n×φ(n)/2.

Machine-translated from the Chinese original.

Problem statement backup

Problem

Compute the sum of positive integers less than a positive integer nn that are not coprime to nn.

Editorial

A basic Euler’s totient function problem.

Conclusion: the sum of numbers less than nn and coprime to nn equals n×φ(n)2\frac{n\times\varphi(n)} 2.

Code backup

Proof

Consider the following fact:

If a,ba,b are coprime, then ba,bb-a,b are coprime.

List all numbers less than nn and coprime to nn, forming a table of length φ(n)\varphi(n): [a1,a2,...,aφ(n)][a_1,a_2,...,a_{\varphi(n)}].

Then list a new table [na1,na2,...,naφ(n)][n-a_1,n-a_2,...,n-a_{\varphi(n)}].

By our reasoning, both tables consist of numbers less than nn and coprime to nn, so the two tables should be identical.

Adding corresponding entries of the two tables gives nn, and both tables have length φ(n)\varphi(n), so the sum of numbers less than nn and coprime to nn equals n×φ(n)2\frac{n\times\varphi(n)} 2; the proposition is proved.

On coding habits

Personally, I’d rather not use #define for constants, and use const or constexpr instead. Also, code shouldn’t be full of mysterious magic numbers; use meaningful constant names instead.

#define int long long is an extremely bad habit. For the sake of runtime efficiency, you should think carefully about a variable’s value range and pick an appropriate type (though that’s a different story on Codeforces, since you don’t have time to sweat those details when you’re rushing out code).

hero.webp