Let .

Du’s First Multiplication Sieve is an algorithm that computes for arithmetic functions and in time and space, if and are given.

Algorithm

Lemma

Applying the lemma yields an algorithm that solves the problem in time and space.

std::unordered_map<int, int> sh;
for (int i : s) {
	for (int j = 1; j <= i; j = i / (i / j) + 1) {
		sh[i] += (sg[i / (i / j)] - sg[j - 1]) * sf[i / j];
	}
}
return sh;