Statement
In this problem, you will be given q queries (1 ≤ q ≤ 1000000). Each query will contain a single integer n (1 ≤ n ≤ 1000000).
For each query you have to output the following:
i=1∑nigcd(i,n)
Solution
S=i=1∑nigcd(i,n)S=i=1∑nik∣n∑kgcd(i/k,n/k)=1
Where k is the gcd(i,n). It means that k∣n and k∣i.
S=i=1∑nik∣n∑kd∣n∑μ(d)d∣gcd(i/k,n/k)S=i=1∑nik∣n∑kd∣n∑μ(d)d∣i/kd∣n/kS=d∣n∑μ(d)k∣n∑kd∣n/ki=1∑nid∣i/kS=d∣n∑μ(d)k∣n∑kd∣n/kj=1∑n/kjkd∣j
The last sum is easy to calculate because it's just the sum of multiples of d up to n/k.
S=d∣n∑μ(d)k∣n∑k2d∣n/kd2dn/k(dn/k+1)S=d∣n∑μ(d)dk∣n∑(n/k)2d∣k2k/d(k/d+1)S=d∣n∑μ(d)dk∈ddiv(n/d)∑(n/k)22k/d(k/d+1)S=d∣n∑μ(d)/dk∣n/d∑(n/k)22k(k+1)S=2nd∣n∑μ(d)k∣n/d∑(k+1)kn/dS=2nd∣n∑μ(d)(dnσ0(n/d)+σ1(n/d))S=2n(n+d∣n∑μ(n/d)dσ0(d))