Let .

Du’s First Division 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 in the problem in time and space.

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