1.前言
莫比乌斯反演课上听蒙了,后来重新捋了一遍思路,看了一下示例,就明白了,写篇学习笔记总结下
2.一些引理
引理1 (莫比乌斯定理)
∑ d ∣ n μ ( n ) = { 1 , n = 1 0 , n ≠ 1 \sum_{d|n} \mu (n) = \begin {cases} 1, n= 1 \\ 0, n\neq 1 \end{cases} d∣n∑μ(n)={1,n=10,n=1
令
n = ∏ i = 1 i ≤ k p i q i n = \prod_{i = 1}^{i \leq k} p_i^{q_i} n=i=1∏i≤kpiqi
则
原 式 = C k 0 − C k 1 + C k 2 . . . = ( 1 + ( − 1 ) ) k ( 二 项 式 展 开 ) = 0 k ( k ≠ 0 ) \begin {aligned}原式 &= C_{k}^{0} - C_{k}^{1} + C_{k}^{2}... \\ &= (1 + (-1)) ^ k (二项式展开) \\ &=0^k(k \neq 0) \end{aligned} 原式=Ck0−Ck1+Ck2...=(1+(−1))k(二项式展开)=0k(k=0)
容易看出:
{ 当 k ≠ 0 ( n ≠ 1 ) 时 , 原 式 = 0 当 k = 0 ( n = 1 ) 时 , 原 式 = 1 \begin{cases}当 k \neq 0(n \neq 1) 时, 原式 = 0 \\ 当 k = 0(n = 1)时,原式 = 1\end{cases} {当k=0(n=1)时,原式=0当k=0(n=1)时,原式=1
同样的: (反演)
{ 当 原 式 = 0 时 , k ≠ 0 ( n ≠ 1 ) 当 原 式 = 1 时 , k = 0 ( n = 1 ) \begin{cases}当原式 = 0时,k \neq 0(n \neq 1) \\ 当原式 = 1时,k = 0(n = 1)\end{cases} {当原式=0时,k=0(n=1)当原式=1时,k=0(n=1)
引理2
d ∣ g c d ( a , b ) ⇔ d ∣ a , d ∣ b d \mid gcd (a, b) \Leftrightarrow d \mid a,d \mid b d∣gcd(a,b)⇔d∣a,d∣b
考虑每个质因数 p p p,记 p p p 在 a , b , d a, b, d a,b,d 中幂次为 q a , q b , q d q_a, q_b, q_d qa,qb,qd
则 q d ≤ min ( a , b ) ⇔ q d ≤ q a , q d ≤ q b q_d \leq \min (a, b) \Leftrightarrow q_d \leq q_a, q_d \leq q_b qd≤min(a,b)⇔qd≤qa,qd≤qb
引理3
⌊ ⌊ a b ⌋ c ⌋ = ⌊ a b c ⌋ \lfloor \frac{\lfloor \frac{a}{b} \rfloor}{c} \rfloor = \lfloor \frac{a}{bc} \rfloor ⌊c⌊ba⌋⌋=⌊bca⌋
令
a = k 1 b + r 1 , k 1 = k 2 c + r 2 a = k_1b + r_1,k_1 = k_2c + r_2 a=k1b+r1,k1=k2c+r2
左 边 = ⌊ ⌊ a b ⌋ c ⌋ = ⌊ k 1 c ⌋ = k 2 右 边 = ⌊ a b c ⌋ = ⌊ k 1 b + r 1 b c ⌋ = ⌊ ( k 2 c + r 2 ) b + r 1 b c ⌋ = ⌊ k 2 b c + r 2 b + r 1 b c ⌋ = ⌊ r 2 b + r 1 b c ⌋ + k 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} 左边右边=⌊c⌊ba⌋⌋=⌊ck1⌋=k2=⌊bca⌋=⌊bck1b+r1⌋=⌊bc(k2c+r2)b+r1⌋=⌊bck2bc+r2b+r1⌋=⌊bcr2b+r1⌋+k2
∵ r 1 < b , r 2 < c \because r_1 < b, r_2 < c ∵r1<b,r2<c
∴ r 1 < b , ( r 2 + 1 ) b ≤ c b \therefore r_1<b,(r_2 + 1)b \leq cb ∴r1<b,(r2+1)b≤cb
∴ r 1 + r 2 b + b < b + c b \therefore r_1 + r_2b + b < b + cb ∴r1+r2b+b<b+cb
r 1 + r 2 b < b c r_1 + r_2b < bc r1+r2b<bc
∴ ⌊ r 2 b + r 1 b c ⌋ = 0 \therefore \lfloor \frac{r_2b + r_1}{bc} \rfloor = 0 ∴⌊bcr2b+r1⌋=0
∴ 右 边 = k 2 = 左 边 \therefore 右边 = k2 = 左边 ∴右边=k2=左边
3.例题讲解
∑ i = a i ≤ b ∑ j = c j ≤ d [ g c d ( i , j ) = k ] \sum_{i = a}^{i \leq b}\sum_{j = c}^{j \leq d}[gcd (i, j) = k] i=a∑i≤bj=c∑j≤d[gcd(i,j)=k]
∑ i = a i ≤ b ∑ j = c j ≤ d [ g c d ( i / k , j / k ) = 1 ] \sum_{i = a}^{i \leq b}\sum_{j = c}^{j \leq d}[gcd (i / k, j / k) = 1] i=a∑i≤bj=c∑j≤d[gcd(i/k,j/k)=1]
∑ i = a i ≤ b ∑ j = c j ≤ d ∑ p ∣ g c d ( i / k , j / k ) μ ( p ) ( 莫 比 乌 斯 反 演 ) \sum_{i = a}^{i \leq b}\sum_{j = c}^{j \leq d}\sum_{p|gcd(i/k,j/k)}\mu(p)(莫比乌斯反演) i=a∑i≤bj=c∑j≤dp∣gcd(i/k,j/k)∑μ(p)(莫比乌斯反演)
∑ p ∑ i = a i ≤ b ∑ j = c j ≤ d , p ∣ g c d ( i / k , j / k ) μ ( p ) \sum_p\sum_{i=a}^{i\leq b}\sum_{j=c}^{j \leq d,p|gcd(i/k,j/k)}\mu (p) p∑i=a∑i≤bj=c∑j≤d,p∣gcd(i/k,j/k)μ(p)
∑ p ∑ i = a i ≤ b , p ∣ ( i / k ) ∑ j = c j ≤ d , p ∣ ( j / k ) μ ( p ) ( 引 理 2 ) \sum_p \sum_{i=a}^{i \leq b, p | (i/k)}\sum_{j = c}^{j \leq d,p |(j/k)} \mu(p)(引理2) p∑i=a∑i≤b,p∣(i/k)j=c∑j≤d,p∣(j/k)μ(p)(引理2)
∑ p μ ( p ) ∗ ( ⌊ b p ∗ k ⌋ − ⌈ a p ∗ k ⌉ + 1 ) ∗ ( ⌊ d p ∗ k ⌋ − ⌈ c p ∗ k ⌉ + 1 ) ( 引 理 3 ) \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) p∑μ(p)∗(⌊p∗kb⌋−⌈p∗ka⌉+1)∗(⌊p∗kd⌋−⌈p∗kc⌉+1)(引理3)
∑ p μ ( p ) ∗ ( ⌊ b p ∗ k ⌋ − ⌊ a − 1 p ∗ k ⌋ ) ∗ ( ⌊ d p ∗ k ⌋ − ⌊ c − 1 p ∗ k ⌋ ) \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) p∑μ(p)∗(⌊p∗kb⌋−⌊p∗ka−1⌋)∗(⌊p∗kd⌋−⌊p∗kc−1⌋)
虽然这个式子也挺不错,但是实际中很难实现(不妨自己试一试)
所以,我们考虑使用前缀,将求区间的问题转换为求两个前缀相减。
令 H ( b , d ) = ∑ p μ ( p ) ∗ ⌊ b p ∗ k ⌋ ∗ ⌊ d p ∗ k ⌋ H (b, d) = \sum_{p} \mu(p)* \lfloor \frac{b}{p * k} \rfloor * \lfloor \frac{d}{p * k} \rfloor H(b,d)=∑pμ(p)∗⌊p∗kb⌋∗⌊p∗kd⌋
则
A
n
s
w
e
r
=
H
(
b
,
d
)
−
H
(
a
−
1
,
d
)
−
H
(
b
,
c
−
1
)
+
H
(
a
−
1
,
c
−
1
)
Answer = H (b, d) - H (a - 1, d) - H (b, c - 1) + H (a - 1, c - 1)
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;
}