On today's POI training camp round I have learnt a nice technique that could possibly be useful in some number theory problems. I couldn't find any CF article on it, so I think it's fair enough to share it on my own.
Remark on used notation
In some sums I will use an Iverson notation
Problem: Squarefree Function
Let's define a Squarefree Function b(x) for any positive integer x as x divided by a greatest perfect square, that divides x.
For example: b(1)=1, b(2)=2, b(4)=1, b(6)=6, b(27)=3, b(54)=6, b(800)=2
Given an array a of n≤105 positive integers, where each ai≤105 compute sum
∑1≤i<j≤nb(ai⋅aj)
Technique: GCD Convolution
You might probably heard about a Sum Convolution. For two arrays b, and c, it is defined as an array d such that dk=∑i,jbi⋅cj[i+j=k] If not, it's basically the same thing as a polynomial multiplication. If B(x)=b0+b1x+b2x2+...+bnxn, and C(x)=c0+c1x+c2x2+...+cmxm, then (B⋅C)(x)=d0+d1x+d2x2+...+dn+mxn+m
Let's define GCD Convolution by analogy
Definition
A GCD Convolution of two arrays b, and c, consisting of positive integers, is an array d such that dk=∑i,jbi⋅cj[gcd(i,j)=k]
Algorithm to find GCD Convolution
Of course, we can compute it naively by iterating over all pairs of indicies. If size of b is n, and size of c is m, the overall complexity would be O(nmlog(max(b)+max(c))). But it turns out, that we could do better.