「Note」模板速查
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned int UIT;
typedef double DB;
typedef pair<int, int> PII;
#define fi first
#define se second
//--------------------//
//--------------------//
int main() {
return 0;
}
快读
inline int rd() {
int ret = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -f;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
ret = ret * 10 + ch - '0', ch = getchar();
return ret * f;
}
\(O(1)\) 求 LCA
int dcnt, dfn[N], lg[N], val[LG][N];
int get(int x, int y) {
return (dfn[x] < dfn[y] ? x : y);
}
void DFS(int now, int fa) {
val[0][dfn[now] = ++dcnt] = fa;
for (int i = head[now]; i; i = edge[i].nex)
if (edge[i].to != fa)
DFS(edge[i].to, now);
}
void init() {
for (int i = 2; i <= n; i++)
lg[i] = lg[i >> 1] + 1;
for (int i = 1; i <= lg[n]; i++)
for (int j = 1; j + (1 << i) - 1 <= n; j++)
val[i][j] = get(val[i - 1][j], val[i - 1][j + (1 << (i - 1))]);
}
int LCA(int u, int v) {
if(u == v)
return u;
if((u = dfn[u]) > (v = dfn[v]))
swap(u, v);
int d = lg[v - u++];
return get(val[d][u], val[d][v - (1 << d) + 1]);
}
最大流
struct Edge {
int to, nex;
LL c;
};
struct Flow {
Edge edge[M];
int tot = 1, head[N], cur[N], lev[N];
int S, T;
void add(int u, int v, int c) {
edge[++tot] = {v, head[u], c};
head[u] = tot;
edge[++tot] = {u, head[v], 0};
head[v] = tot;
}
LL DFS(int now, LL mnc) {
if (now == T)
return mnc;
LL res = 0;
for (int to, &i = cur[now]; i && mnc; i = edge[i].nex) {
LL toc = min(mnc, edge[i].c);
to = edge[i].to;
if (toc && lev[now] + 1 == lev[to]) {
LL tem = DFS(to, toc);
res += tem, mnc -= tem, edge[i].c -= tem, edge[i ^ 1].c += tem;
}
if (!mnc)
break;
}
if (!res)
lev[now] = -1;
return res;
}
LL work(int n) {
LL res = 0;
while (lev[T] != -1) {
for (int i = 1; i <= n; i++)
cur[i] = head[i], lev[i] = -1;
queue<int> q;
lev[S] = 0, q.push(S);
while (!q.empty()) {
int now = q.front(); q.pop();
for (int to, i = head[now]; i; i = edge[i].nex) {
to = edge[i].to;
if (edge[i].c && lev[to] == -1)
lev[to] = lev[now] + 1, q.push(to);
}
}
if (lev[T] != -1)
res += DFS(S, INT_MAX);
}
return res;
}
} F;
最小费用最大流
struct Edge {
int to, nex;
LL c, w;
};
struct Flow {
Edge edge[M];
int tot = 1, head[N], cur[N];
LL dis[N];
int S, T;
bool v[N];
void add(int u, int v, int c, int w) {
edge[++tot] = {v, head[u], c, w};
head[u] = tot;
edge[++tot] = {u, head[v], 0, -w};
head[v] = tot;
}
LL DFS(int now, LL mnc) {
if (now == T)
return mnc;
v[now] = true;
LL res = 0;
for (int to, &i = cur[now]; i && mnc; i = edge[i].nex) {
LL toc = min(mnc, edge[i].c);
to = edge[i].to;
if (!v[to] && toc && dis[now] + edge[i].w == dis[to]) {
LL tem = DFS(to, toc);
res += tem, mnc -= tem, edge[i].c -= tem, edge[i ^ 1].c += tem;
}
if (!mnc)
break;
}
if (!res)
dis[now] = 1e18;
v[now] = false;
return res;
}
queue<int> q;
pair<LL, LL> work(int n) {
LL resc = 0, resw = 0;
while (dis[T] != 1e18) {
for (int i = 1; i <= n; i++)
cur[i] = head[i], dis[i] = 1e18;
dis[S] = 0, q.push(S), v[S] = true;
while (!q.empty()) {
int now = q.front(); q.pop(), v[now] = false;
for (int to, i = head[now]; i; i = edge[i].nex) {
to = edge[i].to; LL w = edge[i].w;
if (edge[i].c && dis[to] > dis[now] + w) {
dis[to] = dis[now] + w;
if (!v[to])
v[to] = true, q.push(to);
}
}
}
if (dis[T] != 1e18) {
LL tem = DFS(S, INT_MAX);
resc += tem, resw += tem * dis[T];
}
}
return {resc, resw};
}
} F;
SAM
struct SAM {
struct SAM_Node {
int nex[30];
int fa, len, siz;
} s[N2];
int tot = 1, las = 1;
void ins(char ch) {
int it = ch - 'a' + 1, p = las, cur = ++tot;
s[cur].len = s[las].len + 1, las = cur, s[cur].siz = 1;
while (!s[p].nex[it] && p)
s[p].nex[it] = cur, p = s[p].fa;
if (!p)
return s[cur].fa = 1, void();
int q = s[p].nex[it];
if (s[p].len + 1 == s[q].len)
return s[cur].fa = q, void();
int cl = ++tot;
s[cl] = s[q], s[cl].siz = 0, s[cl].len = s[p].len + 1, s[cur].fa = s[q].fa = cl;
while (s[p].nex[it] == q && p)
s[p].nex[it] = cl, p = s[p].fa;
}
int bac[N], id[N2], ans[N];
void work(int n) {
for (int i = 1; i <= tot; i++)
bac[s[i].len]++;
for (int i = 1; i <= n; i++)
bac[i] += bac[i - 1];
for (int i = tot; i >= 1; i--)
id[bac[s[i].len]--] = i;
for (int i = tot; i >= 1; i--)
s[s[id[i]].fa].siz += s[id[i]].siz;
return;
}
} S;
组合数
int fac[N], invf[N];
int fast_pow(int x, int y) {
int res = 1;
while (y) {
if (y & 1)
res = 1LL * res * x % Mod;
x = 1LL * x * x % Mod;
y >>= 1;
}
return res;
}
int C(int n, int m) {
return ((n < 0 || m < 0 || n < m) ? 0 : 1LL * fac[n] * invf[n - m] % Mod * invf[m] % Mod);
}
void init(int n) {
for (int i = fac[0] = 1; i <= n; i++)
fac[i] = 1LL * fac[i - 1] * i % Mod;
invf[n] = fast_pow(fac[n], Mod - 2);
for (int i = n; i >= 1; i--)
invf[i - 1] = 1LL * invf[i] * i % Mod;
}
快速取模
int fmod(int x) {
return x >= Mod ? x - Mod : x;
}
void fadd(int &x, int y) {
x = fmod(x + y);
}
int fmod(int x, int Mod) {
return x >= Mod ? x - Mod : x;
}
void fadd(int &x, int y, int Mod) {
x = fmod(x + y, Mod);
}
矩阵
int msiz;
struct Mat {
int mat[MA][MA];
int *operator[](int x) {return mat[x];}
Mat() {memset(mat, 0, sizeof(mat));}
void init() {
for (int i = 1; i <= msiz; i++)
mat[i][i] = 1;
}
};
Mat operator*(Mat &a, Mat &b) {
Mat c;
for (int i = 1; i <= msiz; i++)
for (int j = 1; j <= msiz; j++)
for (int k = 1; k <= msiz; k++)
c[i][j] += a[i][k] * b[k][j];
return c;
}
Mat operator^(Mat x, int y) {
Mat res(msiz, msiz);
res.init();
while (y) {
if (y & 1)
res = res * x;
y >>= 1, x = x * x;
}
return res;
}
int msiz;
struct Mat {
int mat[MA][MA];
int *operator[](int x) {return mat[x];}
Mat() {memset(mat, 0, sizeof(mat));}
void init() {
for (int i = 1; i <= msiz; i++)
mat[i][i] = 1;
}
};
Mat operator*(Mat &a, Mat &b) {
Mat c;
for (int i =1; i <= msiz; i++)
for (int j = 1; j <= msiz; j++)
for (int k = 1; k <= msiz; k++)
fadd(c[i][j], 1LL * a[i][k] * b[k][j] % Mod);
return c;
}
Mat operator^(Mat x, int y) {
Mat res(msiz, msiz);
res.init();
while (y) {
if (y & 1)
res = res * x;
y >>= 1, x = x * x;
}
return res;
}
struct Mat {
int n, m, mat[MA][MA];
int *operator[](int x) {return mat[x];}
Mat() {memset(mat, 0, sizeof(mat));}
Mat(int tn, int tm) {
n = tn, m = tm;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
mat[i][j] = 0;
}
void init() {
for (int i = 1; i <= n; i++)
mat[i][i] = 1;
}
};
Mat operator*(Mat &a, Mat &b) {
Mat c(a.n, b.m);
for (int i = 1; i <= c.n; i++)
for (int j = 1; j <= c.m; j++)
for (int k = 1; k <= a.m; k++)
fadd(c[i][j], 1LL * a[i][k] * b[k][j] % Mod);
return c;
}
Mat operator^(Mat x, int y) {
Mat res(x.n, x.m);
res.init();
while (y) {
if (y & 1)
res = res * x;
y >>= 1, x = x * x;
}
return res;
}
快速幂
int fast_pow(int x, int y) {
int res = 1;
while (y) {
if (y & 1)
res = 1LL * res * x % Mod;
y >>= 1, x = 1LL * x * x % Mod;
}
return res;
}
「Note」模板速查的更多相关文章
- Note -「计算几何」模板
尚未完整测试,务必留意模板 bug! /* Clearink */ #include <cmath> #include <queue> #include <cstdi ...
- 「NOTE」常系数齐次线性递推
要不是考到了,我还没发现这玩意我不是很会-- # 前置 多项式取模: 矩阵快速幂. # 常系数齐次线性递推 描述的是这么一个问题,给定数列 \(c_1,c_2,\dots,c_k\) 以及数列 \(f ...
- 「BJWC2010」模板严格次小生成树
题目描述 小 \(C\) 最近学了很多最小生成树的算法,\(Prim\) 算法.\(Kruskal\) 算法.消圈算法等等.正当小\(C\)洋洋得意之时,小\(P\)又来泼小\(C\)冷水了.小\(P ...
- Solution -「LOCAL」模板
\(\mathcal{Description}\) OurOJ. 给定一棵 \(n\) 个结点树,\(1\) 为根,每个 \(u\) 结点有容量 \(k_u\).\(m\) 次操作,每次操作 ...
- 「luogu3402」【模板】可持久化并查集
「luogu3402」[模板]可持久化并查集 传送门 我们可以用一个可持久化数组来存每个节点的父亲. 单点信息更新和查询就用主席树多花 一个 \(\log\) 的代价来搞. 然后考虑如何合并两个点. ...
- Note -「多项式」基础模板(FFT/NTT/多模 NTT)光速入门
进阶篇戳这里. 目录 何为「多项式」 基本概念 系数表示法 & 点值表示法 傅里叶(Fourier)变换 概述 前置知识 - 复数 单位根 快速傅里叶正变换(FFT) 快速傅里叶逆变换(I ...
- 「luogu3380」【模板】二逼平衡树(树套树)
「luogu3380」[模板]二逼平衡树(树套树) 传送门 我写的树套树--线段树套平衡树. 线段树上的每一个节点都是一棵 \(\text{FHQ Treap}\) ,然后我们就可以根据平衡树的基本操 ...
- SpringBoot图文教程10—模板导出|百万数据Excel导出|图片导出「easypoi」
有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1「概念+ ...
- 可能是史上最全的机器学习和Python(包括数学)速查表
新手学习机器学习很难,就是收集资料也很费劲.所幸Robbie Allen从不同来源收集了目前最全的有关机器学习.Python和相关数学知识的速查表大全.强烈建议收藏! 机器学习有很多方面. 当我开始刷 ...
- 【转】shell速查表
shell速查表 转自:https://www.jianshu.com/p/a98a334bfb25 1. 变量 #!/bin/bash msg="hello world" ech ...
随机推荐
- Font-awesome失效恢复
Font-awesome失效恢复策略 可能的原因有: 1.用了收费pro的版本,没充钱. Font Awesome 6 字体分为 Free 和 Pro 两个版本.Font Awesome 6 Free ...
- Qt 给窗口绘制阴影
文章目录 Qt 给窗口绘制阴影 前言 重载`paintEvent`法 QGraphicsDropShadowEffect方法 使用九图拼凑法 九宫格缩放阴影法 Qt 给窗口绘制阴影 前言 最近自定义一 ...
- stat() "/root/xxx/index.html" failed (13: Permission denied)
前言 在 nginx 上部署静态网页报502,于是查看 nginx 错误日志 error_log /var/log/nginx/error.log;,却没有看到任何错误信息:访问 nginx活动日志 ...
- mysql8导入myslq5 报错
打开sql文件替换 我的数据库编码是utf8mb4,如果你的数据库编码是别的,替换成你自己的编码. utf8mb4_0900_ai_ci替换为utf8mb4_general_ci
- 使用命令行控制wireshark对抓包文件进行针对性处理的命令总结
近日,工作中有开发对抓包文件进行针对性过滤的小程序的需求,兜兜转转踩了很多坑后还是绕回了wireshark. 作为最出名的开源软件之一,wireshark也具有使用命令行进行操作的功能,这就是我们今天 ...
- Spring Boot的常用注解
在Spring Boot中,注解(Annotation)是核心特性之一,广泛用于配置和简化开发.以下是Spring Boot中一些常用的注解及其示例: 1. @SpringBootApplicatio ...
- Web前端入门第 32 问:CSS background 元素渐变背景用法全解
渐变背景在 CSS 里面就是一个颜色到另一个颜色渐渐变化的样子. 本文示例中,盒子基础样式: .box { margin: 20px; padding: 20px; border: 10px dash ...
- Web前端入门第 35 问:CSS 细说 flex 弹性盒子布局(多图)
flex 作为现代布局方案中最常用的手段,有必要拉出来细说. flex 相关的 CSS 属性 容器(父元素)相关的 CSS 属性 以下的 CSS 属性,在 flex 布局中需喂给父元素,设置 flex ...
- 如何使用 MySQL 的 EXPLAIN 语句进行查询分析?
如何使用 MySQL 的 EXPLAIN 语句进行查询分析? EXPLAIN 是 MySQL 提供的分析 SQL 查询执行计划的工具,用于了解查询语句的执行过程,帮助优化查询性能. 1. EXPLAI ...
- symfony3.4修改安全验证默认表配置
#security.yml security: # ··· providers: our_db_provider: entity: class: AppBundle:Users property: u ...