【数学】加性函数与积性函数
一、前置知识
艾佛森括号
其中 \(P\) 是一个可真可假的命题。
如 \([114514\le 1919810] = 1, [-1 \in \mathbb{N}] = 0\)。
二、加性函数
加性函数:对于 \(\forall n, m \in \mathbb{N}^*, \gcd(n, m) = 1\),若 \(f(n) + f(m) = f(nm)\),称函数 \(f\) 为加性函数;
注:此处加性函数指数论上的加性函数 \((\rm Additive\ function)\),应与代数中的加性函数 \((\rm Additive\ map)\) 区分。
完全加性函数:对于 \(\forall n, m \in \mathbb{N}^*\),若 \(f(n) + f(m) = f(nm)\),称函数 \(f\) 为完全加性函数。完全加性函数为加性函数的一种。
对于所有的加性函数,根据 \(f(1) = f(1\times 1) = f(1) + f(1)\) 可得 \(f(1) = 0\)。
加性函数 \(f\) 满足 \(f\left(\prod\limits_{i=1}^k p_i^{\alpha_i}\right) = \sum\limits_{i=1}^k f(p_i^{\alpha_i})\),完全加性函数 \(f\) 满足 \(f\left(\prod\limits_{i=1}^k p_i^{\alpha_i}\right) = \sum\limits_{i=1}^k f(p_i^{\alpha_i}) = \sum\limits_{i=1}^k f(p_i) \cdot \alpha_i\)。
证明时,先证明 \(f(1) = 0\)。
然后证明加性需证明 \(\gcd(n,m) = 1\) 时 \(f(n) + f(m) = f(nm)\)(废话);
证明完全加性只需证明 \(f(n) + f(p_i) = f(np_i), p_i\in \mathbb{P}\) 即可,因为证明 \(f(n) + f(m) = f(nm)\) 可将 \(m\) 分解质因数再合并。
常见加性函数
-
\(\omega(n) = \sum\limits_{p\in \mathbb{P}} [p\mid n]\) 表示 \(n\) 的本质不同质因数个数
证明:
\(\omega(1) = \sum\limits_{p\in \mathbb{P}} [p\mid 1] = 0\)
当 \(\gcd(n, m) = 1\) 时,
\[\begin{aligned} \omega(nm) & = \sum\limits_{p\in \mathbb{P}} [p\mid nm] \\ & = \sum\limits_{p\in \mathbb{P}} [p\mid n] \lor [p\mid m] \\ & = \sum\limits_{p\in \mathbb{P}} [p\mid n] + \sum\limits_{p\in \mathbb{P}} [p\mid m] - \sum\limits_{p\in \mathbb{P}} [p\mid n] \land [p\mid m] \\ & = \sum\limits_{p\in \mathbb{P}} [p\mid n] + \sum\limits_{p\in \mathbb{P}} [p\mid m] - \sum\limits_{p\in \mathbb{P}} [p\mid \gcd(n, m)] \\ & = \sum\limits_{p\in \mathbb{P}} [p\mid n] + \sum\limits_{p\in \mathbb{P}} [p\mid m] - \sum\limits_{p\in \mathbb{P}} [p\mid 1]\ (\textsf{不互质时不满足})\\ & = \omega(n) + \omega(m) - \omega(1) \\ & = \omega(n) + \omega(m) \end{aligned} \] -
\(a_1(n) = \sum\limits_{p\in \mathbb{P}} [p\mid n] p\) 表示 \(n\) 的本质不同质因数和
证明:
\(a_1(1) = \sum\limits_{p\in \mathbb{P}} [p\mid 1] p = 0\)
当 \(\gcd(n, m) = 1\) 时,
\[\begin{aligned} a_1(nm) & = \sum\limits_{p\in \mathbb{P}} [p\mid nm] p \\ & = \sum\limits_{p\in \mathbb{P}} ([p\mid n] \lor [p\mid m]) p \\ & = \sum\limits_{p\in \mathbb{P}} [p\mid n] p + \sum\limits_{p\in \mathbb{P}} [p\mid m] p - \sum\limits_{p\in \mathbb{P}} ([p\mid n] \land [p\mid m]) p \\ & = a_1(n) + a_1(m) - a_1(1)\ (\textsf{不互质时不满足})\\ & = a_1(n) + a_1(m) \end{aligned} \]
常见完全加性函数
-
\(\Omega(n) = \sum\limits_{p\in \mathbb{P}} \sum\limits_{p^\alpha \mid n} 1(\alpha > 0)\) 表示 \(n\) 的总质因数个数
证明:
\(\Omega(1) = \sum\limits_{p\in \mathbb{P}} \sum\limits_{p^\alpha \mid 1} 1 = 0\)
首先由 \(p_i\in \mathbb{P}\) 有 \(\Omega(p_i) = \sum\limits_{p\in \mathbb{P}} \sum\limits_{p^\alpha \mid p_i} 1 = 1\)。
\[\begin{aligned} \Omega(np_i) & = \sum\limits_{p\in \mathbb{P}} \sum\limits_{p^\alpha \mid np_i} 1 \\ & = \sum\limits_{p\in \mathbb{P} \land p\ne p_i} \sum\limits_{p^\alpha \mid np_i} 1 + \sum\limits_{p_i^\alpha \mid np_i} 1 \\ & = \sum\limits_{p\in \mathbb{P} \land p\ne p_i} \sum\limits_{p^\alpha \mid n} 1 + \left(\sum\limits_{p_i^\alpha \mid n} 1 + 1\right) \\ & = \left(\sum\limits_{p\in \mathbb{P} \land p\ne p_i} \sum\limits_{p^\alpha \mid n} 1 + \sum\limits_{p_i^\alpha \mid n} 1\right) + 1 \\ & = \sum\limits_{p\in \mathbb{P}} \sum\limits_{p^\alpha \mid n} 1 + 1 \\ & = \Omega(n) + \Omega(p_i) \end{aligned} \] -
\(a_0(n) = \sum\limits_{p\in \mathbb{P}} \sum\limits_{p^\alpha \mid n} p\) 表示 \(n\) 的总质因数和
证明:
\(a_0(1) = \sum\limits_{p\in \mathbb{P}} \sum\limits_{p^\alpha \mid 1} p = 0\)
首先由 \(p_i\in \mathbb{P}\) 有 \(a_0(p_i) = \sum\limits_{p\in \mathbb{P}} \sum\limits_{p^\alpha \mid p_i} p = p_i\)。
\[\begin{aligned} a_0(np_i) & = \sum\limits_{p\in \mathbb{P}} \sum\limits_{p^\alpha \mid np_i} p \\ & = \sum\limits_{p\in \mathbb{P} \land p\ne p_i} \sum\limits_{p^\alpha \mid np_i} p + \sum\limits_{p_i^\alpha \mid np_i} p_i \\ & = \sum\limits_{p\in \mathbb{P} \land p\ne p_i} \sum\limits_{p^\alpha \mid n} p + \sum\limits_{p_i^\alpha \mid n} p_i + p_i \\ & = \sum\limits_{p\in \mathbb{P}} \sum\limits_{p^\alpha \mid n} p + p_i \\ & = a_0(n) + a_0(p_i) \end{aligned} \] -
\(\operatorname{pot}_p(n)(p\in \mathbb{P}) = \max\limits_{p^k\mid n}\{k\}\) 表示使得 \(p^k\mid\mid n(p^k\mid n, p^{k + 1}\nmid n)\) 的 \(k\)。
证明:
\(\operatorname{pot}_p(1) = \max\limits_{p^k\mid 1}\{k\} = 0\)
首先由 \(p, p_i\in \mathbb{P}\) 有 \(\operatorname{pot}_p p_i = \max\limits_{p^k\mid p_i}\{k\} = [p = p_i]\)。
当 \(p = p_i\) 时:
\[\begin{aligned} \operatorname{pot}_p (np_i) & = \operatorname{pot}_{p_i} (np_i) \\ & = \max_{p_i^k\mid np_i}\{k\} \\ & = \max_{p_i^k\mid n}\{k\} + 1 \\ & = \operatorname{pot}_{p_i}(n) + 1 \\ & = \operatorname{pot}_p(n) + 1 \end{aligned} \]当 \(p\ne p_i\) 时:
\[\begin{aligned} \operatorname{pot}_p(np_i) & = \max_{p^k\mid np_i}\{k\} \\ & = \max_{p^k\mid n}\{k\} \\ & = \operatorname{pot}_p(n) \end{aligned} \]所以有
\[\begin{aligned} \operatorname{pot}_p(np_i) & = \operatorname{pot}_p(n) + [p = p_i] \\ & = \operatorname{pot}_p(n) + \operatorname{pot}_p(p_i) \end{aligned} \]
三、积性函数
积性函数:对于 \(\forall n,m\in\mathbb{N}^*, \gcd(n, m) = 1\), 若 \(f(n)f(m)=f(nm)\),称函数 \(f\) 为积性函数;
完全积性函数:对于 \(\forall n,m\in\mathbb{N}^*\),若 \(f(n)f(m)=f(nm)\),称函数 \(f\) 为完全积性函数。完全积性函数为积性函数的一种。
对于所有的积性函数,根据 \(f(1) = f(1\times 1) = f(1)f(1)\) 可得 \(f(1)=1\)。
注意:值恒等于 \(0\) 的函数一般不看作积性函数。
积性函数 \(f\) 满足 \(f\left(\prod\limits_{i=1}^k p_i^{\alpha_i}\right) = \prod\limits_{i=1}^k f(p_i^{\alpha_i})\),完全积性函数 \(f\) 满足 \(f\left(\prod\limits_{i=1}^k p_i^{\alpha_i}\right) = \prod\limits_{i=1}^k f(p_i^{\alpha_i}) = \prod\limits_{i=1}^k f(p_i)^{\alpha_i}\)。
加性函数与积性函数的互相转化
若函数 \(f\) 为加性函数,函数 \(g\) 满足 \(g(n) = C^{f(n)}\),其中 \(C\) 为常数。
首先 \(g(1) = C^{f(1)} = C^0 = 1\);
当 \(\gcd(n, m) = 1\) 时,\(g(n) g(m) = C^{f(n) + f(m)}\),由 \(f\) 为加性函数可得 \(C^{f(n) + f(m)} = C^{f(nm)}\),即 \(g(n) g(m) = g(nm)\),所以函数 \(g\) 为积性函数。
同理,当函数 \(f\) 为完全加性函数时,函数 \(g\) 为完全积性函数。
证明时,先证明 \(f(1) = 1\)。
然后证明积性需证明 \(\gcd(n,m) = 1\) 时 \(f(n) f(m) = f(nm)\);
证明完全积性只需证明 \(f(n) f(p_i) = f(np_i), p_i\in \mathbb{P}\) 即可,因为证明 \(f(n) f(m) = f(nm)\) 可将 \(m\) 分解质因数再合并。
常见积性函数
-
常数 \(k\) 固定时的最大公因数 \(\gcd(k, n)\)
证明:
\(\gcd(1, n) = 1\)
当 \(\gcd(n, m) = 1\) 时,\(\gcd(k, n) \gcd(k, m) = \gcd(k, nm)\)
-
欧拉函数 \(\varphi(n) = \sum\limits_{i = 1}^n [\gcd(i, n) = 1]\)
或将 \(n\) 分解质因数 \(n = \prod\limits_{i = 1}^k p_i^{\alpha_i}\) 后 \(\varphi(n) = n\cdot \prod\limits_{i = 1}^k \dfrac{p_i - 1}{p_i}\)
证明:
\(\varphi(1) = \sum\limits_{i = 1}^1 [\gcd(i, 1) = 1] = 1\)
当 \(\gcd(n, m) = 1\) 时,
\[\begin{aligned} \varphi(n) \varphi(m) & = \sum_{i = 1}^n [\gcd(i, n) = 1] \sum_{j = 1}^m [\gcd(j, m) = 1] \\ & = \sum_{i = 1}^n \sum_{j = 1}^m [\gcd(i, n) = 1] [\gcd(j, m) = 1] \\ & = \sum_{i = 1}^{nm} [\gcd(i, nm) = 1]\ \left(\textsf{不互质时为} \sum_{i = 1}^{\operatorname{lcm}(n, m)} [\gcd(i, \operatorname{lcm}(n, m)) = 1]\right)\\ & = \varphi(nm)\ (\textsf{不互质时为}\ \varphi(\operatorname{lcm}(n, m))) \end{aligned} \] -
\(\gamma(n) = (-1)^{\omega(n)}\)
证明:
因为 \(\omega\) 为加性函数,\((-1)\) 为常数,所以 \(\gamma\) 为积性函数。
-
莫比乌斯函数 \(\mu(n) = \begin{cases} 1 & n = 1 \\ 0 & \exists d > 1, d ^ 2 \mid n \\ \gamma(n)& \text{otherwise}\end{cases}\)
证明:
\(\mu(1) = 1\)
当 \(n, m\) 中有一者为 \(1\) 时,不妨设 \(n = 1\),则 \(\mu(nm) = \mu(m) = 1\cdot \mu(m) = \mu(n) \mu(m)\);
当 \(\exists d > 1, d^2 \mid n\) 时,必有 \(\exists d > 1, d^2 \mid nm\),则 \(\mu(nm) = 0 = 0 \cdot \mu(m) = \mu(n) \mu(m)\);
否则,说明 \(\mu(n) = \gamma(n), \mu(m) = \gamma(m)\),当 \(\gcd(n, m) = 1\) 时,由 \(\gamma(n) \gamma(m) = \gamma(nm)\) 可得 \(\mu(n) \mu(m) = \mu(nm)\)。
-
除数函数 \(\sigma_k(n) = \sum\limits_{d \mid n} d^k\),因数个数函数 \(d(n) = \sum\limits_{d\mid n} 1 = \sigma_0(n)\),因数和函数 \(\sigma(n) = \sum\limits_{d\mid n} d = \sigma_1(n)\)。
证明:
\(\sigma_k(1) = \sum\limits_{d \mid 1} d ^ k = 1 ^ k = 1\)
当 \(\gcd(n, m) = 1\) 时,
\[\begin{aligned} \sigma_k(n) \sigma_k(m) & = \sum_{d_1\mid n} d_1^k \sum_{d_2\mid m} d_2^k \\ & = \sum_{d_1\mid n} \sum_{d_2\mid m} d_1^k d_2^k \\ & = \sum_{d_1\mid n} \sum_{d_2\mid m} (d_1 d_2)^k \\ & = \sum_{d\mid nm} d^k (\textsf{不互质时不满足}) \\ & = \sigma_k(nm) \end{aligned} \]
常见完全积性函数
-
逆元 \(a^{-1} \bmod p\)(当逆元存在时)
证明:
\[a^{-1} \bmod p = \dfrac{1}{a} \bmod p \\ b^{-1} \bmod p = \dfrac{1}{b} \bmod p \\ \begin{aligned} \therefore (a^{-1} \bmod p) \cdot (b^{-1} \bmod p) & = \left(\dfrac{1}{a} \bmod p\right) \cdot \left(\dfrac{1}{b} \bmod p\right) \\ & = \left(\dfrac{1}{a} \cdot \dfrac{1}{b}\right) \bmod p \\ & = \dfrac{1}{ab} \bmod p \\ & = (ab)^{-1} \bmod p \end{aligned} \] -
刘维尔函数 \(\lambda(n) = (-1)^{\Omega(n)}\)
证明:
因为 \(\Omega\) 为完全加性函数,\((-1)\) 为常数,所以 \(\lambda\) 为积性函数。
-
幂函数 \(\operatorname{Id}_k(n) = n ^ k\),常数函数 \(I(n)\) 或 \(\boldsymbol{1}(n) = 1 = \operatorname{Id}_0(n)\),恒等函数 \(\operatorname{Id}(n)=n = \operatorname{Id}_1(n)\);
证明:
\(\operatorname{Id}_k(1) = 1^k = 1\)
\[\begin{aligned} \operatorname{Id}_k(n) \operatorname{Id}_k(m) & = n^k m^k \\ & = (nm)^k \\ & = \operatorname{Id}_k(nm) \end{aligned} \] -
单位函数 \(\varepsilon(n) = [n=1]\)。
证明:
\(\varepsilon(1) = [1 = 1] = 1\)
\[\begin{aligned} \varepsilon(n) \varepsilon(m) & = [n = 1][m = 1] \\ & = [nm = 1] \\ & = \varepsilon(nm) \end{aligned} \]
四、线性筛
主要是常见积性函数的,加性函数是类似的。
套路是在线性筛中
void pre(int n) // 线性筛积性函数 f
{
f[1] = 1; // 注意这里,积性函数基本性质
for (int i = 2; i <= n; i++)
{
if (!vis[i])
{
p[++p[0]] = i;
f[i] = ???; //质数得直接算
}
for (int j = 1; j <= p[0] && i * p[j] <= n; j++)
{
vis[i * p[j]] = true;
if (i % p[j] == 0)
{
f[i * p[j]] = ???; // 特殊情况
break;
}
f[i * p[j]] = f[i] * f[p[j]]; // 一般情况
}
}
}
像上面一样分类,原因是当 i % p[j] == 0
,即 \(p_j\mid i\) 时有 \(\gcd(i, p_j) > 1\),不满足积性,得单独处理;否则有 \(\gcd(i, p_j) = 1\),则 \(f(i)\cdot f(p_j) = f(i\cdot p_j)\)。
用线性筛,时间复杂度为 \(\operatorname{O}(n)\)。
1. 最大公因数
-
\(i\in \mathbb{P}\)
\(\gcd(k, i)\) 暴力算是 \(\Omicron(\log n)\) 的,由于 \(\pi(n) \sim \dfrac{n}{\log n}\),所以所有质数暴力算是 \(\Omicron(n)\) 的。当然因为是质数你也可以用 \(\operatorname{if}\) 来 \(\Omicron(1)\) 计算。
-
\(p_j\nmid i\)
\(\gcd(k, i\cdot p_j) = \gcd(k, i) \cdot \gcd(k, p_j)\)
-
\(p_j\mid i\)
- 若 \(p_j\mid \dfrac{k}{\gcd(k, i)}\),则 \(\gcd(k, i \cdot p_j) = \gcd(k, i)\cdot p_j\)
- 否则 \(\gcd(k, i\cdot p_j) = \gcd(k, i)\)
void pre(int n, int k)
{
gcd[1] = 1;
for (int i = 2; i <= n; i++)
{
if (!vis[i])
{
p[++p[0]] = i;
if (i % k == 0)
{
gcd[i] = k;
}
else if (k % i == 0)
{
gcd[i] = i;
}
else
{
gcd[i] = 1;
}
}
for (int j = 1; j <= p[0] && i * p[j] <= n; j++)
{
vis[i * p[j]] = true;
if (i % p[j] == 0)
{
gcd[i * p[j]] = ((k / gcd[i]) % p[j]) ? gcd[i] : (gcd[i] * p[j]);
break;
}
gcd[i * p[j]] = gcd[i] * gcd[p[j]];
}
}
}
2. 欧拉函数
-
\(i\in \mathbb{P}\)
\(\varphi(i) = i - 1\)
-
\(p_j\nmid i\)
\(\varphi(i\cdot p_j) = \varphi(i) \cdot \varphi(p_j)\)
-
\(p_j\mid i\)
原来长这个样子:\(i\cdot \prod\limits_{x = 1}^k \dfrac{p_x - 1}{p_x}\)
现在是 \((i\cdot p_j)\cdot \prod\limits_{x = 1}^k \dfrac{p_x - 1}{p_x}\)
\(\varphi(i\cdot p_j) = \varphi(i) \cdot p_j\)
void pre(int n)
{
phi[1] = 1;
for (int i = 2; i <= n; i++)
{
if (!vis[i])
{
p[++p[0]] = i;
phi[i] = i - 1;
}
for (int j = 1; j <= p[0] && i * p[j] <= n; j++)
{
vis[i * p[j]] = true;
if (i % p[j] == 0)
{
phi[i * p[j]] = phi[i] * p[j];
break;
}
phi[i * p[j]] = phi[i] * phi[p[j]];
}
}
}
3. 莫比乌斯函数
-
\(i\in \mathbb{P}\)
\(\mu(i) = -1\)
-
\(p_j\nmid i\)
\(\mu(i \cdot p_j) = \mu(i)\cdot \mu(p_j)\)
-
\(p_j\mid i\)
\(p_j^2\mid \mu(i\cdot p_j)\to \mu(i\cdot p_j) = 0\)
void pre(int n)
{
mu[1] = 1;
for (int i = 2; i <= n; i++)
{
if (!vis[i])
{
p[++p[0]] = i;
mu[i] = -1;
}
for (int j = 1; j <= p[0] && i * p[j] <= n; j++)
{
vis[i * p[j]] = true;
if (i % p[j] == 0)
{
mu[i * p[j]] = 0;
break;
}
mu[i * p[j]] = mu[i] * mu[p[j]];
}
}
}
4. 因数个数函数
由算术基本定理得 \(n = \prod\limits_{i = 1}^k p_i^{\alpha_i}\),那么我们知道 \(d(n) = \prod\limits_{i = 1}^k (\alpha_i + 1)\)。
设 \(num(n)\) 为 \(n\) 的最小质因数的次数,即 \(\alpha_1\)。
-
\(i\in \mathbb{P}\)
\(d(i) = 2\)
\(num(i) = 1\)
-
\(p_j\nmid i\)
\(d(i\cdot p_j) = d(i) \cdot d(p_j)\)
\((i\cdot p_j)\) 的最小质因数为 \(p_j\),\(num(i\cdot p_j) = 1\)
-
\(p_j\mid i\)
此时 \(p_j\) 仍为最小质因数,故 \(\alpha_1\) 增加了一,\(d(i\cdot p_j) = \dfrac{d(i)}{\alpha_1 +1} \cdot (\alpha_1 + 2) = \dfrac{d(i)}{num(i) + 1} \cdot (num(i) + 2)\)
\(num(i\cdot p_j) = num(i) + 1\)
void pre(int n)
{
d[1] = 1;
for (int i = 2; i <= n; i++)
{
if (!vis[i])
{
p[++p[0]] = i;
d[i] = 2;
num[i] = 1;
}
for (int j = 1; j <= p[0] && i * p[j] <= n; j++)
{
vis[i * p[j]] = true;
if (i % p[j] == 0)
{
d[i * p[j]] = d[i] / (num[i] + 1) * (num[i] + 2);
num[i * p[j]] = num[i] + 1;
break;
}
d[i * p[j]] = d[i] * d[p[j]];
num[i * p[j]] = 1;
}
}
}
5. 因数和函数
首先 \(\sigma(n) = \prod\limits_{i = 1}^k \sum\limits_{j = 0}^{\alpha_i} p_i^{j}\)。
设 \(num(n)\) 为 \(n\) 的最小质因数 \(p_1\) 的 \(\sum\limits_{i = 0}^{\alpha_1} p_1^i\)。
-
\(i\in \mathbb{P}\)
\(\sigma(i) = num(i) = i + 1\)
-
\(p_j\nmid i\)
\(\sigma(i\cdot p_j) = \sigma(i) \cdot \sigma(p_j)\)
\(num(i\cdot p_j) = 1 + p_j\)
-
\(p_j\mid i\)
\(p_1\) 这一项从 \(\sum\limits_{i = 0}^{\alpha_i} p_1^i\) 变成 \(\sum\limits_{i = 0}^{\alpha_i + 1} p_1^i\),那么将原来的集体乘 \(p_1\) 再加 \(1\) 即可。
\(\sigma(i\cdot p_j) = \dfrac{\sigma(i)}{\sum\limits_{k = 0}^{\alpha_1} p_1^k} \cdot \sum\limits_{k = 0}^{\alpha_1 + 1} p_1^k = \dfrac{\sigma(i)}{num(i)} \cdot [num(i)\cdot p_j + 1]\)
\(num(i\cdot p_j) = num(i) \cdot p_j + 1\)
UVA1730 Sum of MSLCM(终于有题写了
void pre(int n)
{
sigma[1] = 1;
for (int i = 2; i <= n; i++)
{
if (!vis[i])
{
p[++p[0]] = i;
sigma[i] = i + 1;
num[i] = i + 1;
}
for (int j = 1; j <= p[0] && i * p[j] <= n; j++)
{
vis[i * p[j]] = true;
if (i % p[j] == 0)
{
sigma[i * p[j]] = sigma[i] / num[i] * (num[i] * p[j] + 1);
num[i * p[j]] = num[i] * p[j] + 1;
break;
}
sigma[i * p[j]] = sigma[i] * sigma[p[j]];
num[i * p[j]] = 1 + p[j];
}
}
}
五、参考资料
-
[1] JustinRochester. 积性加性函数性质与常见积性加性函数
-
[2] _王小呆. 数学 线性筛约数个数和,约数和
-
[3] 柯召,孙琦. 数论讲义 上册 第二版. 高等教育出版社