Loading

积性函数

定义

  1. 积性函数:\(f(1) = 1\),且对于所有互质的正整数 \(a, b\) 有性质 \(f(ab) = f(a)f(b)\) 的数论函数。

  2. 完全积性函数:\(f(1) = 1\),且对于所有正整数 \(a, b\) 有性质 \(f(ab) = f(a)f(b)\) 的数论函数。

常见函数

常见积性函数

  1. 欧拉函数 \(\varphi\)\(\varphi(n)\) 表示小于等于 \(n\) 且与 \(n\) 互质的正整数个数

  2. \(k\) 为常数时的 \(\gcd(k, n)\)

  3. 除法函数 \(\sigma_k\)\(\sigma_k(n)\) 表示 \(n\) 的所有正因子的 \(k\) 次方之和
    因数个数函数 \(\sigma_0\)\(\sigma_0(n)\) 表示 \(n\) 的正因子个数
    因数和函数 \(\sigma_1\)\(\sigma_1(n)\) 表示 \(n\) 的正因子之和

  4. 莫比乌斯函数 \(\mu\)

常见完全积性函数

  1. 逆元(存在逆元时)

  2. 单位函数 \(\epsilon\)\(\epsilon(n) = [n = 1]\)

  3. 幂函数 \(\textbf{Id}\)\(\textbf{Id}_k (n) = n^k\)
    常数函数 \(\textbf{1}\)\(\textbf{1}(n) = 1 = \textbf{Id}_0(n)\)
    恒等函数 \(\textbf{Id}\)\(\textbf{Id} = n = \textbf{Id}_1(n)\)

筛法求积性函数

使用欧拉筛可以在 \(\mathcal{O}(n)\) 内筛出积性函数的值,则 \(n\) 会被 \(i \cdot p_j\) 筛出,其中 \(i\)\(n\) 除自身外最大的因数,\(p_j\)\(n\) 最小的质因子。

欧拉函数

  1. \(n\) 是质数
    \(\varphi(n) = n - 1\)

  2. \(i \mid p_j\)
    \(i\) 包含 \(n\) 的所有质因子

\[\begin{aligned} \varphi(n) &= n \cdot \prod\limits_{i = 1}^k \frac{p_i - 1}{p_i} \\ &= p_j \cdot i \cdot \prod\limits_{i = 1}^k \frac{p_i - 1}{p_i} \\ &= p_j \cdot \varphi(i) \end{aligned} \]

  1. \(i \nmid p_j\)
    \(\varphi(n) = \varphi(i) \cdot \varphi(p_j)\)
void init() {
    phi[1] = 1;
    vis[1] = true;
    for (int i = 2; i <= 1e6; i++) {
        if (!vis[i]) {
            p.push_back(i);
            phi[i] = i - 1;
        }
        for (int j = 0; (j < p.size()) && (i * p[j] <= 1e6); j++) {
            vis[i * p[j]] = true;
            if (i % p[j] == 0) {
                phi[i * p[j]] = phi[i] * p[j];
                break;
            } else {
                phi[i * p[j]] = phi[i] * phi[p[j]];
            }
        }
    }
}

莫比乌斯函数

  1. \(n\) 是质数
    \(\mu(n) = -1\)

  2. \(i \mid p_j\)
    \(\mu(i \cdot p_j) = 0\)

  3. \(i \nmid p_j\)
    \(\mu(i \cdot p_j) = -\mu(i)\)

void init()
{
	mu[1] = 1;
	vis[1] = true;
	for (int i = 2; i <= 1e7; i++)
	{
		if (!vis[i])
		{
			p.push_back(i);
			mu[i] = -1;
		}
		for (int j = 0; (j < p.size()) && (i * p[j] <= 1e7); j++)
		{
			vis[i * p[j]] = true;
			if (i % p[j] == 0)
			{
				mu[i * p[j]] = 0;
				break;
			}
			else
				mu[i * p[j]] = -mu[i];
		}
	}
}

逆元

\(inv_n = (p - \lfloor \frac{p}{n} \rfloor) \cdot inv_{p \bmod n} \bmod p\)

inv[1] = 1;
for (int i = 2; i < n; i++) {
    inv[i] = (n - n / i) * inv[n % i] % n;
}

\(k\) 为定值时的 \(\gcd(k, n)\)

  1. \(n\) 为质数

    1. \(n = k, \gcd(k, n) = n\)
    2. \(n \neq k, \gcd(k, n) = 1\)
  2. \(i \mid p_j\)

    1. \(p_j \mid \frac{n}{g_i}, \gcd(k, n) = \gcd(k, i) \cdot p_j\)
    2. \(p_j \nmid \frac{n}{g_i}, \gcd(k, n) = \gcd(k, i)\)
  3. \(i \nmid p_j\)
    \(\gcd(k, n) = \gcd(i, n) \cdot \gcd(p_j, n)\)

void init() {
    g[1] = 1;
    vis[1] = true;
    for (int i = 2; i <= n; i++) {
        if (!vis[i]) {
            p.push_back(i);
            g[i] = (b % i == 0 ? i : 1);
        }
        for (int j = 0; (j < p.size()) && (i * p[j] <= n); j++) {
            vis[i * p[j]] = true;
            if (i % p[j] == 0) {
                if (b / g[i] % p[j] == 0) {
                    g[i * p[j]] = g[i] * p[j];
                } else {
                    g[i * p[j]] = g[i];
                }
                break;
            } else {
                g[i * p[j]] = g[i] * g[p[j]];
            }
        }
    }
}

因数个数函数

\(n = \prod\limits_{i = 1}^k p_i^{q_i}\),其中 \(p_i\) 为两两不同的质数且 \(\forall 1 \leq i < k, p_i < p_{i + 1}\)

\(num_n\) 为上式中的 \(q_1\)

  1. \(n\) 为质数
    \(\sigma_0(n) = 2, num_n = 1\)

  2. \(i \mid p_j\)
    显然有 \(\sigma_0(n) = \prod\limits_{i = 1}^k (q_i + 1)\)
    \(\sigma_0(n) = \frac{\sigma_0(i)}{num_i + 1} \cdot (num_i + 2), num_n = num_i + 1\)

  3. \(i \nmid j\)
    \(\sigma_0(n) = \sigma_0(i) \cdot \sigma_0(p_j), num_n = 1\)

void init() {
    d[1] = 1;
    vis[1] = true;
    for (int i = 2; i <= n; i++) {
        if (!vis[i]) {
            d[i] = 2;
            num[i] = 1;
            p.push_back(i);
        }
        for (int j = 0; (j < p.size()) && (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;
            } else {
                d[i * p[j]] = d[i] * d[p[j]];
                num[i * p[j]] = 1;
            }
        }
    }
}

因数和函数

\(\sigma(n) = \sum\limits_{i = 1}^k \sum\limits_{j = 0}^{q_i} p_i^j\),其中 \(\forall 1 \leq i < k, p_i < p_{i + 1}\)

\(num_n = \sum\limits_{i = 0}^{q_1} p_1^i\)

  1. \(n\) 为质数
    \(\sigma(n) = num_n = n + 1\)

  2. \(i \mid p_j\)

\[\begin{aligned} \sigma(n) &= \frac{\sigma(i)}{\sum\limits_{i = 0}^{q_1} p_1^i} \cdot \sum\limits_{i = 0}^{q_1 + 1} p_1^i \\ &= \frac{\sigma(i)}{num_i} \cdot (num_i \cdot p_j + 1) \\ num_n &= num_i \cdot p_j + 1 \end{aligned} \]

  1. \(i \nmid p_j\)

\(\sigma(n) = \sigma(i) \cdot \sigma(p_j), num_n = p_j + 1\)

void init() {
    sum[1] = 1;
    for (int i = 2; i <= n; i++) {
        if (!vis[i]) {
            p.push_back(i);
            sum[i] = num[i] = i + 1;
        }
        for (int j = 0; (j < p.size()) && (i * p[j] <= n); j++) {
            vis[i * p[j]] = true;
            if (i % p[j] == 0) {
                sum[i * p[j]] = sum[i] / num[i] * (num[i] * p[j] + 1);
                num[i * p[j]] = num[i] * p[j] + 1;
                break;
            } else {
                sum[i * p[j]] = sum[i] * sum[p[j]];
                num[i * p[j]] = p[j] + 1;
            }
        }
    }
}

因数平方和

void init(int lim)
{
    sig0[1] = sig2[1] = num[1] = 1;
    for (int i = 2; i <= lim; i++)
    {
        if (!vis[i])
        {
            num[i] = 1, g[i] = 1ll * i * i + 1;
            sig0[i] = 2, sig2[i] = g[i];
            pr.push_back(i);
        }
        for (int j = 0, w; (j < pr.size()) && (i * pr[j] <= lim); j++)
        {
            vis[w = i * pr[j]] = true;
            if (i % pr[j] == 0)
            {
                num[w] = num[i] + 1, g[w] = g[i] * pr[j] * pr[j] + 1;
                sig0[w] = sig0[i] / num[w] * (num[w] + 1);
                sig2[w] = sig2[i] / g[i] * g[w];
                break;
            }
            else
            {
                num[w] = 1, g[w] = 1ll * pr[j] * pr[j] + 1;
                sig0[w] = 2 * sig0[i], sig2[w] = sig2[i] * sig2[pr[j]];
            }
        }
    }
}

加性函数与积性函数

若函数 \(f\) 为加性函数,函数 \(g\) 满足 \(g(n) = C^{f(n)}\),则 \(g\) 是积性函数。

显然 \(g(1) = C^{f(1)} = C^0 = 1\)

对于互质的正整数 \(n, m\),有 \(g(n)g(m) = C^{f(n) + f(m)}\)。由 \(f\) 是加性函数可得 \(g(n)g(m) = C^{f(nm)} = g(nm)\)

同理可得若 \(f\) 为完全加性函数,则 \(g\) 为完全积性函数。

posted @ 2022-01-17 16:11  kymru  阅读(383)  评论(0编辑  收藏  举报