-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3253846
commit 2c05339
Showing
18 changed files
with
601 additions
and
598 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
--- | ||
title: "随笔 - Miller-Rabin + Pollard-Rho 分解质因子的时间复杂度分析" | ||
date: 2024-11-29 20:43:13 | ||
categories: | ||
- 随笔 | ||
- 算法竞赛 | ||
tags: | ||
- 随笔 | ||
- 算法竞赛 | ||
- 数学 | ||
- 数论 | ||
- Miller-Rabin算法 | ||
- Pollard-Rho算法 | ||
- 素数/质数 | ||
- 素性检验 | ||
- Ramanujan和 | ||
- 凹函数 | ||
- Jensen不等式 | ||
--- | ||
|
||
省流版: $O\left(n^{1/4}\right)$ | ||
|
||
<!-- more --> | ||
|
||
我们考虑这样的代码 | ||
|
||
{% icodeweb blog lang:python draft-020/pfactors.cpp %} | ||
|
||
其中 | ||
|
||
- `is_prime_miller_rabin` 为基于 Miller-Rabin 算法的质数判断, 时间复杂度为 $O\left(n^{1/4}\right)$ | ||
- `pollard_rho` 为 Pollard-Rho 算法, 返回入参的一个非平凡因子, 期望时间复杂度为 $O\left(p^{1/2}\right)$, 其中 $p$ 为 $n$ 的最小质因子, 不难发现 Pollard-Rho 算法的期望时间复杂度为 $O\left(n^{1/4}\right)$ | ||
- 回调函数 `callback` 的时间复杂度为 $O(1)$ | ||
|
||
我们尝试计算 `pfactors` 的时间复杂度, 设其为 $O(T(n))$, 则有 | ||
|
||
$$ | ||
O(T(n)) = \begin{cases} | ||
O\left(n^{1/4}\right),&n<2\lor n\in\mathbb{P},\\ | ||
O\left(n^{1/4}+T(d)+T(n/d)\right),&\text{otherwise}, | ||
\end{cases} | ||
$$ | ||
|
||
其中 $d$ 为 $n$ 的某个非平凡因子, $T(d)$ 没法直接处理, 我们用均值代替: | ||
|
||
$$ | ||
\begin{aligned} | ||
T(d)+T(n/d)&\xlongequal{\exists C>0}C\dfrac{\sum_{1<d<n;d\mid n}(T(d)+T(n/d))}{\sum_{1<d<n;d\mid n}1}\\ | ||
&=2C\dfrac{\sum_{1<d<n;d\mid n}T(d)}{\sum_{1<d<n;d\mid n}1} | ||
\end{aligned} | ||
$$ | ||
|
||
所以 | ||
|
||
$$ | ||
O(T(n)) = \begin{cases} | ||
O\left(n^{1/4}\right),&n<2\lor n\in\mathbb{P},\\ | ||
O\left(n^{1/4}+\dfrac{\sum_{1<d<n;d\mid n}T(d)}{\sum_{1<d<n;d\mid n}1}\right),&\text{otherwise}, | ||
\end{cases} | ||
$$ | ||
|
||
我们希望证明 $O(T(n))=O\left(n^{1/4}\right)$, 不难发现若 | ||
|
||
$$ | ||
O\left(\dfrac{\sum_{d\mid n}d^{1/4}}{\sum_{d\mid n}1}\right)=O\left(\dfrac{\sigma_{1/4}(n)}{\sigma_{0}(n)}\right)=O\left(n^{1/4}\right)\tag{1} | ||
$$ | ||
|
||
成立, 则可以通过数学归纳法证明 | ||
|
||
我们知道, 当 $k>0$ 时 | ||
|
||
$$ | ||
\sigma_k(n)=\zeta(k+1)n^k\sum_{m=1}^{\infty}\frac{c_m(n)}{m^{k+1}}\tag{2} | ||
$$ | ||
|
||
其中 $c_q(n)=\displaystyle\sum_{1\leq a\leq q;(a,q)=1}\mathrm{e}^{(2\pi\mathrm{i}an)/q}$ 为 [Ramanujan 和](https://en.wikipedia.org/wiki/Ramanujan_sum), 所以这个看起来是有搞头的, 不过 $(2)$ 式涉及到级数, 看起来就不好用, 所以我们不会用 $(2)$ 式去证 $(1)$ 式, 而是一个更简单的做法 | ||
|
||
注意到 $f(x)=x^{1/4}$ 是凹函数, 所以我们考虑 Jensen 不等式 | ||
|
||
$$ | ||
\begin{aligned} | ||
\dfrac{\sum_{d\mid n}d^{1/4}}{\sum_{d\mid n}1}&\leq\left(\dfrac{\sum_{d\mid n}d}{\sum_{d\mid n}1}\right)^{1/4}\\ | ||
&=\left(\dfrac{\sigma_1(n)}{\sigma_0(n)}\right)^{1/4}\\ | ||
&=O\left(\left(\dfrac{n\log\log n}{\log n}\right)^{1/4}\right)\\ | ||
&\xlongequal{\exists\epsilon>0} O\left(n^{1/4-\epsilon}\right) | ||
\end{aligned} | ||
$$ | ||
|
||
综上所述, `pfactors` 的时间复杂度为 $O\left(n^{1/4}\right)$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--- | ||
title: 模板 - 红黑树 | ||
categories: | ||
- 笔记 | ||
tags: | ||
- 数学 | ||
- 模板 | ||
- 数据结构 | ||
- 平衡树 | ||
- 顺序统计树 | ||
- 二叉搜索树 | ||
- 红黑树 | ||
date: 2024-11-27 09:23:57 | ||
--- | ||
|
||
红黑树是一种平衡树, 是 C++ `std::(multi)?(set|map)`, Java `Tree(Set|Map)` 的底层实现 | ||
|
||
代码参考了 `pb_ds` 的设计方式, 时空均略优于 `pb_ds` | ||
|
||
{% note info %} | ||
这里的代码实际上是 order-statistic tree, 即每个结点都记录了对应子树的大小, 因此支持查找排名以及根据排名反查数据 | ||
{% endnote %} | ||
|
||
{% note warning %} | ||
仅在 GCC 下测试过 | ||
{% endnote %} | ||
|
||
{% note warning %} | ||
<https://cplib.tifa-233.com/src/code/ds/rbtree.hpp> 存放了笔者对该算法/数据结构的最新实现, 建议前往此处查看相关代码 | ||
{% endnote %} | ||
|
||
<!-- more --> | ||
|
||
## 设计与使用 | ||
|
||
参考了 `pb_ds` 的设计方式, 使用了 Mixin Classes, `balance_tree<K, bst_tag>` 即为二叉搜索树, `balance_tree<K, rbt_tag>` 即为红黑树 | ||
|
||
具体来说, 代码中把一般的平衡树/顺序统计树操作 (遍历, `(lower|upper)_bound`, `order_of_key`, `find_by_order` 等) 和红黑树的操作 (旋转, 插入和删除的性质维护等) 解耦. 又由于红黑树的插入/删除可以视为先按二叉搜索树的方式插入/删除, 再进行平衡维护操作, 故代码中也将这两部分解耦, 并让 `rbt_tag` 继承 `bst_tag` 来提高代码复用率 | ||
|
||
使用方式类似 `pb_ds` 的 `__gnu_pbds::tree`, 只是没有将维护子树大小的部分解耦出来 | ||
|
||
## 代码 | ||
|
||
<details open> | ||
<summary><font color='orange'>Show code</font></summary> | ||
|
||
{% icodeweb blog lang:cpp rbtree/rbtree.hpp %} | ||
|
||
</details> | ||
|
||
## 示例 | ||
|
||
洛谷 P6136 [【模板】普通平衡树(数据加强版)](https://www.luogu.com.cn/problem/P6136) | ||
|
||
<details open> | ||
<summary><font color='orange'>Show code</font></summary> | ||
|
||
{% icodeweb cpa_cpp title:Luogu_P6136 Luogu/P6136/3.cpp %} | ||
|
||
</details> | ||
|
||
--- | ||
|
||
## 参考 | ||
|
||
- <https://gcc.gnu.org/onlinedocs/libstdc++/ext/pb_ds/tree_based_containers.html> | ||
- Introduction to Algorithms, Fourth Edition | ||
- <https://en.wikipedia.org/wiki/Red%E2%80%93black_tree> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.