算法模板(持续更新)
基础算法
高精度加法
cpp
1 | vector<int> add(vector<int> a, vector<int> b) //倒进倒出 |
高精度减法
cpp
1 | bool cmp(vector<int> a, vector<int> b) //倒进倒出 |
高精度乘法
大乘小
cpp
1 | vector<int> mul(vector<int> &a, int b) //倒进倒出 |
cpp
1 | vector<int> mul(vector<int> a, vector<int> b) //倒进倒出 |
高精度除法
cpp
1 | vector<int> div(vector<int> a, int b, int &r) //倒进倒出 |
二维前缀和
cpp
1 | int main() |
二维差分
cpp
1 | void insert(int x1, int y1, int x2, int y2, int c) |
返回二进制最低位1
cpp
1 | int lowbit(int x) |
离散化
cpp
1 | int main() |
区间合并
cpp
1 | vector<PII> merge() |
st表
cpp
1 | int main() |
数据结构
kmp
cpp
1 | int main() |
单调队列
cpp
1 | deque<int> q; |
单调栈
cpp
1 | stack<int> s; |
并查集
cpp
1 | int find(int x) |
字符串哈希
cpp
1 | const int P = 13331; |
树状数组
cpp
1 | int lowbit(int x) //返回二进制最低位1 |
线段树
cpp
1 |
|
可持久化线段树
cpp
1 | int root[N], tot; |
静态区间第
cpp
1 |
|
莫队
cpp
1 | struct Q |
图论
堆优化dijkstra
cpp
1 | int dijkstra() |
bellman-ford
有边数限制的最短路
cpp
1 | const int N = 510, M = 10010; |
spfa
判断负环
cpp
1 | bool spfa() |
floyd
cpp
1 | void floyd() |
prim
cpp
1 | int prim() |
kruskal
cpp
1 | int find(int x) |
染色法判断二分图
cpp
1 | bool dfs(int u, int c) |
二分图的最大匹配
cpp
1 | bool find(int x) |
lca
cpp
1 | void dfs(int u, int father) |
tarjan SCC缩点
cpp
1 | void tarjan(int u) |
拓扑排序
基环树找环
cpp
1 | void topsort() |
2—SAT
cpp
1 | int main() |
数学
线性筛质数
cpp
1 | void get_prime(int n) |
筛法求欧拉函数
欧拉函数,即
cpp
1 | void get_eulers(int n) |
筛法求莫比乌斯函数
cpp
1 | void get_mobius(int n) |
快速幂
cpp
1 | int qmi(int a, int b, int p) |
龟速乘
cpp
1 | int qmul(int a, int b, int p) |
扩展欧几里得
给定
cpp
1 | int exgcd(int a, int b, int &x, int &y) |
扩展中国剩余定理
cpp
1 | int excrt(int a[], int m[]) |
高斯消元
线性方程组
cpp
1 | int gauss() |
异或线性方程组
cpp
1 | int gauss() |
组合数I
cpp
1 | void init() |
组合数II
cpp
1 | int qmi(int a, int b, int p) |
组合数III
cpp
1 | int qmi(int a, int k) |
组合数IV
cpp
1 | int get_num(int a, int b) |
Nim博弈论
cpp
1 | int main() |
Sg函数
cpp
1 | int sg(int x) |
异或线性基
对于一个数组
中任意非空子集异或和不为 中每个数的最高位 唯一
中任意元素都可以通过 的子集异或出来
cpp
1 | void gauss() |
凸包
cpp
1 | struct point |
裴蜀定理
费马小定理
欧拉定理
威尔逊定理
动态规划
二进制优化多重背包
cpp
1 | int main() |
单调队列优化多重背包
cpp
1 | int main() |
最长上升子序列
cpp
1 | int main() |
最长公共子序列
cpp
1 | int main() |
数位dp
cpp
1 | int dp(int u, int cnt, int op) |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 勤劳努力的小蜜蜂!