Lemma

Let .

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

Algorithm

Lemma

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

std::unordered_map<int, int> du(int n, const std::unordered_map<int, int> &sg, const std::unordered_map<int, int> &sh) {
	int m = std::sqrt(n);
 
	std::vector<int> d;
	for (int i = 1; i < m; i++) {
		if (n / (n / i) == i) {
			d.push_back(i);
		}
	}
	for (int i = n / m; i > 0; i--) {
		d.push_back(n / i);
	}
	d.erase(std::unique(d.begin(), d.end()), d.end());
 
	std::unordered_map<int, int> sf;
	for (int i : d) {
		sf[i] = sh.at(i);
		for (int j = 2; j <= i; j = i / (i / j) + 1) {
			sf[i] -= (sg.at(i / (i / j)) - sg.at(j - 1)) * sf[i / j];
		}
		sf[i] /= sg.at(1);
	}
	return sf;
}