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
Compute the sum of positive integers less than a positive integer that are not coprime to .
Editorial
A basic Euler’s totient function problem.
Conclusion: the sum of numbers less than and coprime to equals .
Proof
Consider the following fact:
If are coprime, then are coprime.
List all numbers less than and coprime to , forming a table of length : .
Then list a new table .
By our reasoning, both tables consist of numbers less than and coprime to , so the two tables should be identical.
Adding corresponding entries of the two tables gives , and both tables have length , so the sum of numbers less than and coprime to equals ; 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).
