Table of Contents
Initial OverviewDefinitionExamplesDirichlet ConvolutionMöbius InversionExample - Sum of DivisorsExplanationImplementationExample - Totient SumExplanationImplementationExample - Enumerate PrimesExplanationImplementationExample - Counting PrimesExplanation 1ImplementationExplanation 2ImplementationProblemsPrefix Sums of Multiplicative Functions
Authors: Tianqin Meng, Benjamin Qi
This module delves into the topic of prefix sum with multiplicative functions.
Table of Contents
Initial OverviewDefinitionExamplesDirichlet ConvolutionMöbius InversionExample - Sum of DivisorsExplanationImplementationExample - Totient SumExplanationImplementationExample - Enumerate PrimesExplanationImplementationExample - Counting PrimesExplanation 1ImplementationExplanation 2ImplementationProblemsInitial Overview
Definition
- If a function maps positive integers to complex numbers, it's an arithmetic function.
- If is an arithmetic function, and f(p⋅q) = for any coprime positive integers p,q, it's a multiplicative function.
- If is multiplicative and = for any positive integers p,q, it's a completely multiplicative function.
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
Examples
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.
Dirichlet Convolution
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 we have
Möbius Inversion
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
Example - Sum of Divisors
Focus Problem – try your best to solve this problem before continuing!
Explanation
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.
Implementation
Time Complexity:
C++
#include <algorithm>#include <iostream>using namespace std;int main() {long long n;cin >> n; // Taking input for nlong long sum = 0;int i; // Initializing i globally to keep its value after the loopfor (i = 1; i * i <= n; i++) {
Example - Totient Sum
Focus Problem – try your best to solve this problem before continuing!
Explanation
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.
Implementation
Time Complexity:
C++
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;
Example - Enumerate Primes
Focus Problem – try your best to solve this problem before continuing!
Explanation
Remember the old Eratosthenes' sieve here?
To solve this problem, we have to improve the complexity of the sieve to . Check out this CF blog entry for further information.
C++
std::vector<int> prime;bool is_composite[MAX_N];void sieve(int n) {std::fill(is_composite, is_composite + n, false);for (int i = 2; i < n; ++i) {if (!is_composite[i]) { prime.push_back(i); }for (int j = 2; i * j < n; ++j) { is_composite[i * j] = true; }}}
This problem requires finding the count of prime numbers not exceeding and then calculating the sum of primes based on specific conditions.
- We first run our sieve to get all the prime numbers not exceeding .
- 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.
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;const int MAX_N = 500000000;using ll = long long;Code Snippet: Sieve Implementation (Click to expand)int main() {
Example - Counting Primes
Focus Problem – try your best to solve this problem before continuing!
There's two ways to do this problem. The first solution has a higher time complexity but a less complex implementation, while the second has a lower time complexity at the cost of a more complex implementation.
For a more complete explanation of these algorithms, refer to this CF blog post.
Explanation 1
Utilizes a dynamic programming approach based on a recursion relation derived from sieving.
The algorithm iteratively reduces the count of numbers that are not divisible by primes, utilizing a recursive formula. It achieves a complexity of .
Implementation
Time Complexity:
C++
#include <algorithm>#include <cmath>#include <iostream>#include <vector>using ll = long long;using std::cout;using std::endl;using std::pair;using std::vector;
Explanation 2
There exists a faster way to compute primes in O(n^2/3), see Maksim1744’s Codeforces Blog for more details. Below is the implementation using the fenwick code snippet from the PURS module
Implementation
Time Complexity:
C++
#include <algorithm>#include <cmath>#include <iostream>#include <vector>using ll = long long;using std::cout;using std::endl;using std::pair;using std::vector;
Problems
Status | Source | Problem Name | Difficulty | Tags | |
---|---|---|---|---|---|
51nod | Normal | ||||
51nod | Normal | ||||
BZOJ | Normal | ||||
HDU | Normal | ||||
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!