While I was attempting this problem, I misread it after writing it down on paper and instead solved
S=xy≤n∑xygcd(x,y)
Here the sum is over positive integers (x,y) satisfying xy≤n, rather than x,y≤n. In my opinion it turned out more interesting than the original one, so I wanted to share.
Compute S=∑xy≤nxygcd(x,y) in O(nlogn).
—Source: Accidental misread
Solution
Setting up notation first, let M be set of multiplicative functions, and * be the Dirichlet
product, then (M,∗) is an abelian group. Let
I, τ, ϕ be the
identity, divisor count, divisor sum, Mobius, and totient functions
respectively. Note that they are all multiplicative.
Write k=xy so that we can iterate over k as follows:
S=k≤n∑kd∣k∑gcd(d,k/d)
Key Lemma
d2∣k∑τ(k/d2)ϕ(d)=d∣k∑gcd(d,k/d)
Note here that d does not represent the same iterator, but I kept the variable name anyways.
Proof of Lemma.
A cool fact is that any sum of a product of multiplicative functions with the form of the left side is multiplicative. We can define here
f(d2)={0ϕ(d)d is not a squareotherwise
Note that f is multiplicative, and the left side is actually f∗τ, making it multiplicative.
We can verify the right side is multiplicative too, with just the textbook approach. One can notice that if gcd(p,q)=1, d∣pq⟹d=rs, where r∣p and s∣q. Also, gcd(rs,pq/(rs))=gcd(r,p/r)gcd(s,q/s). Therefore,
d∣pq∑gcd(d,pq/d)=r∣p∑gcd(r,p/r)s∣q∑gcd(s,q/s)
It remains to simply check both sides coincide at prime powers:
So we’re done. The sum telescopes in the middle. □
Therefore
S=k≤n∑kd2∣k∑τ(k/d2)ϕ(d)
The goal here was to turn the sum into one consisting of multiplicative functions, so we can try applying Dirichlet’s Hyperbola Method.
We can quickly make the observation d2≤k≤n, and it looks promising to try iterating over
d≤n instead:
S=d≤n∑ϕ(d)d2∣k∑kτ(k/d2)
Now k has a really simple characterization: it is just d2α
for α≤n/d2. Let’s iterate over α,
S=d≤n∑ϕ(d)d2α≤n/d2∑ατ(α)
So we would be happy if we can compute partial sums of Iτ quickly,
luckily it is multiplicative and simple enough, so let’s try to write it as a Dirichlet product:
Iτ=I∗I
Using the Hyperbola Method on partial sums of Iτ, choose splitting point pq=s:
∑sIτ=∑pi∑s/ij+∑qi∑s/ij−∑qi∑pj
Of course, partial sums of I is an O(1) calculation, we can optimally choose p=q=n, making the Iτ calculation O(n).
Now to resolve
S=d≤n∑ϕ(d)d2α≤n/d2∑ατ(α)
We can linear sieve the first n values of ϕ in the S sum at no additional symptotic cost. Thus the total time complexity is