Let .
Du’s Second Multiplication 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 Multiplication Sieve in time and space.
Algorithm
Lemma
Proof
-
For each in , find .
-
Find using the results from Step 1.
-
Find using the results from Step 2.
-
For each in , applying the lemma to find .
This algorithm solves the problem in time and space.
std::vector<int> h(n23 + 1);
h[1] = 1;
for (int i = 2; i <= n23; i++) {
if (pk[i] == i) {
h[i] = 0;
for (int j = i; j; j /= pf[i]) {
h[i] += f[j] * g[i / j];
}
} else {
h[i] = f[i / pk[i]] * g[pk[i]];
}
}
std::unordered_map<int, int> sh;
for (int i = 1; i <= n23; i++) {
sh[i] = sh[i - 1] + h[i];
}
for (int i = n / n23; i > 0; i--) {
sh[n / i] = 0;
for (int j = 1; j <= n / i; j = n / i / (n / i / j) + 1) {
sh[n / i] += (sg[n / i / (n / i / j)] - sg[j - 1]) * sf[n / i / j];
}
}
return sh;Proof
Lemma
Proof
Applying the lemma yields that
Therefore, since
it follows that
Therefore,