Let .

Du’s Second Division Sieve is an algorithm that computes for multiplicative functions and in time and space, if , , , and are given.

Hint

This problem can also be solved by Du’s First Division Sieve in time and space.

Algorithm

Lemma

  1. For each in , find .

  2. Find using the results from Step 1.

  3. Find using the results from Step 2.

  4. For each in , applying the lemma to find .

std::vector<int> f(n23 + 1);
f[1] = 1;
for (int i = 2; i <= n23; i++) {
	if (pk[i] == i) {
		f[i] = h[i];
		for (int j = i / pf[i]; j; j /= pf[i]) {
			f[i] -= f[j] * g[i / j];
		}
	} else {
		f[i] = f[i / pk[i]] * f[pk[i]];
	}
}
 
std::unordered_map<int, int> sf;
for (int i = 1; i <= n23; i++) {
	sf[i] = sf[i - 1] + f[i];
}
for (int i = n / n23; i > 0; i--) {
	sf[n / i] = sh[n / i];
	for (int j = 2; j <= n / i; j = n / i / (n / i / j) + 1) {
		sf[n / i] -= (sg[n / i / (n / i / j)] - sg[j - 1]) * sf[n / i / j];
	}
}
return sf;