莫比乌斯反演小结 + 黑暗爆炸 2301

1.前言

莫比乌斯反演课上听蒙了,后来重新捋了一遍思路,看了一下示例,就明白了,写篇学习笔记总结下

2.一些引理

引理1 (莫比乌斯定理)

\[\sum_{d|n} \mu (d) = \begin {cases} 1, d = 1 \\ 0, d\neq 1 \end{cases} \]

\[n = \prod_{i = 1}^{i \leq k} p_i^{q_i} \]

\[\begin {aligned}原式 &= C_{k}^{0} - C_{k}^{1} + C_{k}^{2}... \\ &= (1 + (-1)) ^ k (二项式展开) \\ &=0^k(k \neq 0) \end{aligned} \]

容易看出:

\[\begin{cases}当 k \neq 0(n \neq 1) 时, 原式 = 0 \\ 当 k = 0(n = 1)时,原式 = 1\end{cases} \]

同样的: (反演)

\[\begin{cases}当原式 = 0时,k \neq 0(n \neq 1) \\ 当原式 = 1时,k = 0(n = 1)\end{cases} \]


引理2

\[d \mid gcd (a, b) \Leftrightarrow d \mid a,d \mid b \]

考虑每个质因数 \(p\),记 \(p\)\(a, b, d\) 中幂次为 \(q_a, q_b, q_d\)

\(q_d \leq \min (a, b) \Leftrightarrow q_d \leq q_a, q_d \leq q_b\)


引理3

\[\lfloor \frac{\lfloor \frac{a}{b} \rfloor}{c} \rfloor = \lfloor \frac{a}{bc} \rfloor \]

\[a = k_1b + r_1,k_1 = k_2c + r_2 \]

\[\begin{aligned}左边 &= \lfloor \frac{\lfloor \frac{a}{b} \rfloor}{c} \rfloor \\ &= \lfloor \frac{k_1}{c} \rfloor \\ &= k_2 \\ 右边 &= \lfloor \frac{a}{bc} \rfloor \\ &= \lfloor \frac{k_1b+r_1}{bc} \rfloor \\ &=\lfloor \frac{(k_2c + r_2)b + r_1}{bc} \rfloor \\ &= \lfloor \frac{k_2bc+r_2b+r_1}{bc} \rfloor \\ &= \lfloor \frac{r_2 b + r_1}{bc} \rfloor + k_2 \end{aligned} \]

\[\because r_1 < b, r_2 < c \]

\[\therefore r_1<b,(r_2 + 1)b \leq cb \]

\[\therefore r_1 + r_2b + b < b + cb \]

\[r_1 + r_2b < bc \]

\[\therefore \lfloor \frac{r_2b + r_1}{bc} \rfloor = 0 \]

\[\therefore 右边 = k2 = 左边 \]

3.例题讲解

\[\sum_{i = a}^{i \leq b}\sum_{j = c}^{j \leq d}[gcd (i, j) = k] \]

\[\sum_{i = a}^{i \leq b}\sum_{j = c}^{j \leq d}[gcd (i / k, j / k) = 1] \]

\[\sum_{i = a}^{i \leq b}\sum_{j = c}^{j \leq d}\sum_{p|gcd(i/k,j/k)}\mu(p)(莫比乌斯反演) \]

\[\sum_p\sum_{i=a}^{i\leq b}\sum_{j=c}^{j \leq d,p|gcd(i/k,j/k)}\mu (p) \]

\[\sum_p \sum_{i=a}^{i \leq b, p | (i/k)}\sum_{j = c}^{j \leq d,p |(j/k)} \mu(p)(引理2) \]

\[\sum_{p} \mu(p)* (\lfloor \frac{b}{p * k} \rfloor - \lceil \frac{a}{p * k} \rceil + 1) * (\lfloor \frac{d}{p *k} \rfloor - \lceil \frac{c}{p * k} \rceil + 1)(引理3) \]

\[\sum_{p} \mu(p)* (\lfloor \frac{b}{p * k} \rfloor - \lfloor \frac{a - 1}{p * k} \rfloor) * (\lfloor \frac{d}{p * k} \rfloor - \lfloor \frac{c - 1}{p * k} \rfloor) \]

虽然这个式子也挺不错,但是实际中很难实现(不妨自己试一试)

所以,我们考虑使用前缀,将求区间的问题转换为求两个前缀相减。

\(H (b, d) = \sum_{p} \mu(p)* \lfloor \frac{b}{p * k} \rfloor * \lfloor \frac{d}{p * k} \rfloor\)

\(Answer = H (b, d) - H (a - 1, d) - H (b, c - 1) + H (a - 1, c - 1)\) (一个小小的可爱容斥)

好诶,直接数论分块!

#include <cstdio>
#include <iostream>
using namespace std;

template <typename T> void read (T &x) { x = 0; T f = 1;char tem = getchar ();while (tem < '0' || tem > '9') {if (tem == '-') f = -1;tem = getchar ();}while (tem >= '0' && tem <= '9') {x = (x << 1) + (x << 3) + tem - '0';tem = getchar ();}x *= f; return; }
template <typename T> void write (T x) { if (x < 0) {x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0'); }
template <typename T> void print (T x, char ch) { write (x); putchar (ch); }
template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }

int t, a, b, c, d, k; 

const int Maxn = 50000;
int cnt, primes[Maxn + 5];
int mu[Maxn + 5], pre[Maxn + 5];
bool vis[Maxn + 5];
void Euler () {
	mu[1] = 1;
	for (int i = 2; i <= Maxn; i++) {
		if (vis[i] == 0) {
			primes[++cnt] = i;
			mu[i] = -1;
		}
		for (int j = 1; j <= cnt; j++) {
			if (primes[j] > Maxn / i) break;
			vis[primes[j] * i] = 1;
			if (i % primes[j] == 0) {
				mu[primes[j] * i] = 0;
				break;
			}
			mu[primes[j] * i] = -mu[i];
		}
	}
	for (int i = 1; i <= Maxn; i++)
		pre[i] = pre[i - 1] + mu[i];
}
int Calc (int n, int m) {
	int res = 0, l = 1, r;
	while (l <= Min (n, m) / k) {
		r = Min (n / (n / l), m / (m / l));
		res += (pre[r] - pre[l - 1]) * (n / l / k) * (m / l / k);
		l = r + 1;
	}
	return res;
}

int main () {
	Euler ();
	 
	read (t);
	while (t--) {
		read (a); read (b); read (c); read (d); read (k);
		
		print (Calc (b, d) - Calc (a - 1, d) - Calc (b, c - 1) + Calc (a - 1, c - 1), '\n');
	}
	return 0;
}
posted @ 2021-08-24 20:59  C2022lihan  阅读(79)  评论(0编辑  收藏  举报