Prefix Sums of Multiplicative Functions
Authors: Tianqin Meng, Benjamin Qi
This module delves into the topic of multiplicative functions and their properties.
Prerequisite Skills
Definition of multiplicative functions
- If a function f(n) maps positive integers to complex numbers (f: ℤ⁺ → ℂ), it's an arithmetic function.
- If f(n) is an arithmetic function, f(1) = 1 and f(p⋅q) = f(p)⋅f(q) for any coprime positive integers p,q, it's a multiplicative function.
- Further, if f(n) is multiplicative and f(p⋅q) = f(p)⋅f(q) for any positive integers p,q, it's a completely multiplicative function.
Properties and Examples of Multiplicative Functions
Common multiplicative functions are
- Divisor function: , representing the sum of the th powers of divisors of . Note that and are different.
- Divisor count function: , representing the count of divisors of , also denoted as .
- Divisor sum function: , representing the sum of divisors of .
- Euler's totient function: , representing the count of positive integers less than or equal to and coprime to . Additionally, , is even.
- Möbius function: , serving as the multiplicative inverse of the identity function in Dirichlet convolution, , for a square-free number , , and for a number with square factors, .
- Unit function: , serving as the identity element in Dirichlet convolution, completely multiplicative.
- Constant function: , completely multiplicative.
- Identity function: , completely multiplicative.
- Power function: , completely multiplicative.
The two classic formulas regarding the Möbius function and the Euler function are:
- , Interpreting as the coefficients of the inclusion-exclusion principle proves it.
- . To prove it, we can count the number of occurrences of ) in its simplest fraction form.
If is a multiplicative function, then for a positive integer , we have ; If is a completely multiplicative function, then for a positive integer , we have:
Dirichlet Convolution and Möbius Inversion
The Dirichlet convolution of number-theoretic functions and is defined as . Dirichlet convolution satisfies commutativity, associativity, and distributivity with respect to addition. There exists an identity function such that 。If and are multiplicative functions, then is also multiplicative.
A common technique with Dirichlet convolution involves dealing with the convolution of a multiplicative function and the identity function . For example, if and ,then:
Möbius inversion is also a discussion regarding , but it does not require to be multiplicative and is applicable in cases where is known and is to be determined. Since , ,and . Similarly, ,Binomial inversion is also a similar technique. An example illustrates the relationship between the Euler function and the Möbius function: since ,then ,which implies
https://judge.yosupo.jp/problem/enumerate_primes
Focus problem # 1Let the prime numbers be (i.e. and so on).
You are given integers and . Find (the number of primes no greater than ), and print for nonnegative integers with .
Constraints
- where
Approach:
First a review on linear sieve, this will be used in both focus problem 1 and 3:
C++
void get_prime(int n) {for (int i = 2; i <= n; i++) {if (!st[i]) primes[++cnt] = i;for (int j = 1; primes[j] <= n / i; j++) {st[i * primes[j]] = true;if (i % primes[j] == 0) break;}}}
Focus problem 1 requires finding the count of prime numbers not exceeding a given integer N and then calculating the sum of primes based on specific conditions. The provided solution employs the Sieve of Eratosthenes algorithm, which efficiently finds prime numbers up to a given limit. Here's how the solution works:
- Sieve of Eratosthenes: The algorithm efficiently identifies prime numbers up to a given limit by marking multiples of primes as composite.
- Input and Initialization: The program reads input values N, A, and B, and initializes arrays to store prime numbers (p) and their count (pc).
- Counting and Summing Primes: After sieving for prime numbers up to N, the program calculates the count of terms (x) in the sum that do not exceed N. It does this by considering the quotient of the total count of prime numbers divided by A, adjusting for the remainder.
- Output: The program prints the count of prime numbers (pc) and the count of terms in the sum (x). Then, it outputs the prime numbers contributing to the sum, starting from index B and skipping A elements each time.
C++
#include <bits/stdc++.h>using namespace std;constexpr int N(5e8), M(3.2e7);int p[M], pc;void sieve(int n) {if (n < 2) return;vector<bool> np(n + 1);p[pc++] = 2;int i;for (i = 3; 1ll * i * i <= n; i += 2) {
Focus problem # 2
Find the sum of divisors of the first positive integers, i.e., , where .
Approach:
It's not feasible to compute directly, but we can derive it as follows:
When , there are only distinct values for . Similarly, when , has only distinct values. For a fixed , the values of form a contiguous interval, which is . Therefore, the calculation can be done in time.
Similarly, the sum of the number of divisors for the first positive integers can be calculated in the same manner. I leave this as an exercise for the reader.
Another thing to note is that . This is also a common representation form.
C++
#include <algorithm>#include <cstdio>#include <cstring>using namespace std;long long n, i, a, b;long long sum;int main() {scanf("%lld", &n);
Focus problem # 3
Now let's increase the difficulty a bit. We want to find the sum of Euler's totient function for the first positive integers, i.e., , where .
Approach:
Currently, the formulas related to Euler's totient function mentioned in this article are limited. Can we use them to simplify the problem? The answer is yes, and now we'll utilize the formula to simplify the expression.
This formula can also be seen as . Let's denote , then we have:
So as long as you calculate the values of for times, you can compute . What about the complexity of such an operation?
Suppose the complexity of calculating is , then we have . Here, we only expand one layer because deeper complexities are higher-order terms. So, we have .
Since is a prefix sum of a multiplicative function, the sieve method can preprocess a portion. Assuming the preprocessing includes the first positive integers' where , the complexity becomes . When , we can achieve a good complexity of .
How did we come up with the place where we utilized ? Let's take a look at this:
If we can construct a function through Dirichlet convolution that computes prefix sums more efficiently and if another function suitable for convolution is also easy to calculate, we can simplify the calculation process. For example, in the above problem, we used the property of . But remember, not all problems of this type can be easily solved by just pairing with an identity function . Sometimes, a more careful observation is needed.
C++
#include <cstring>#include <iostream>using namespace std;typedef long long ll;const int N = 1e6 + 10;int primes[N];ll phi[N];bool st[N];int cnt;
More information below left over by Benjamin Qi
https://codeforces.com/blog/entry/54150
Linear Time Sieve
https://judge.yosupo.jp/problem/enumerate_primes
Counting Primes
https://judge.yosupo.jp/problem/counting_primes
Totient Function
https://judge.yosupo.jp/problem/sum_of_totient_function
template <int SZ> struct Sieve {vi pr;int sp[SZ], phi[SZ]; // smallest prime that dividesSieve() { // above is fastermemset(sp, 0, sizeof sp);phi[1] = 1;FOR(i, 2, SZ) {if (sp[i] == 0) {sp[i] = i, pr.pb(i);phi[i] = i - 1;
(project euler)
(topcoder problem)
Problems
Here are some practice problems for everyone to understand the methods mentioned above. Such problems are relatively few, so if you have other sources of problems, please feel free to share.
Status | Source | Problem Name | Difficulty | Tags | |
---|---|---|---|---|---|
51nod | Hard | ||||
51nod | Hard | ||||
BZOJ | Hard | ||||
HDU | Hard | ||||
51nod | Hard | ||||
51nod | Hard | ||||
51nod | Hard | ||||
Tsinsen | Hard | ||||
SPOJ | Hard | ||||
51nod | Hard | ||||
BZOJ | Hard | ||||
51nod | Hard | ||||
51nod | Hard | ||||
ZOJ | Hard | ||||
BZOJ | Hard | ||||
ZOJ | Hard | ||||
SPOJ | Hard | ||||
51nod | Hard | ||||
51nod | Hard |
Module Progress:
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!