如有不足,及时惠示指正。
If people do not believe that mathematics is simple, it is only because they do not realize how complicated life is.——John von Neumann
壹 矩乘优化
矩乘优化DP
经典模型:给定一张图和初始所在的点,每秒向相邻的点走1步,求第k ( ≤ 1 0 18 ) (\le10^{18}) (≤1018)秒后可能在的位置。这是一个传递闭包。利用矩阵的性质和FLoyd算法转移的联系可以设计出矩乘优化。
贰 线性基
子集异或
线性基是什么?
【模板】线性基题解
→
\text{【模板】线性基题解}\rightarrow
【模板】线性基题解→
经典模型:给定n个整数(数字可能重复),求在这些数中选取任意个,使得他们的异或和最大。
由于
a
1
⊕
a
2
⊕
.
.
.
⊕
a
n
=
b
a_1\oplus a_2\oplus...\oplus a_n=b
a1⊕a2⊕...⊕an=b等价于
a
1
⊕
a
2
⊕
.
.
.
⊕
b
=
a
n
a_1\oplus a_2\oplus...\oplus b=a_n
a1⊕a2⊕...⊕b=an,所以线性基可以去掉重复的数,此时使用贪心可以保证算法正确。
e
x
α
:
第k小异或和
ex\alpha:\text{第k小异或和}
exα:第k小异或和
如果k的第i位=1,答案就异或线性基中从小到大第i+1个数。
叁 莫比乌斯反演
整除分块
经典模型:给定n,求
Σ
i
=
1
n
⌊
n
i
⌋
\Sigma^n_{i=1}\lfloor\frac ni\rfloor
Σi=1n⌊in⌋。
通过打表或理性证明发现有很多
⌊
n
i
⌋
\lfloor\frac ni\rfloor
⌊in⌋是相同的,并且
⌊
n
⌊
n
i
⌋
⌋
\lfloor\frac{n}{\lfloor\frac ni\rfloor}\rfloor
⌊⌊in⌋n⌋是相同的块中的最大的数。
那么有如下
O
(
n
)
O(\sqrt n)
O(n)算法:
for(int l = 1, r;l <= n;l = r + 1) {
r = n / (n / l);
ans += (r - l + 1) * (n / l);
}
值得一提的是,对于这样的扩展: ∑ i = 1 m i n ( n , m ) ⌊ n i ⌋ ⌊ m i ⌋ \sum\limits^{min(n,m)}_{i=1}\lfloor\frac ni\rfloor\lfloor\frac mi\rfloor i=1∑min(n,m)⌊in⌋⌊im⌋,有如下代码:
for(int l = 1, r;l <= min(n,m);l = r + 1) {
r = min(n / (n / l),m / (m / l));
ans += (r - l + 1) * (n / l) * (m / l);
}
将整除分块讲在最前面是因为莫反经常把式子化成这种形式。