「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 ...
随机推荐
- php不使用mysqlDump工具实现的mysql数据备份
再无法使用mysqlDump等外部工具的时候,我们需要到处数据库备份的话,借助phpMyAdmin强大的功能就可以实现.如果我们想自己实现一个类似phpMysql的功能要如何去考虑了,因此,在这里我给 ...
- Django实战项目-学习任务系统-任务管理
接着上期代码框架,开发第3个功能,任务管理,再增加一个学习任务表,用来记录发布的学习任务的标题和内容,预计完成天数,奖励积分和任务状态等信息. 第一步:编写第三个功能-任务管理 1,编辑模型文件: . ...
- FastAPI测试策略:参数解析单元测试
扫描二维码关注或者微信搜一搜:编程智域 前端至全栈交流与成长 探索数千个预构建的 AI 应用,开启你的下一个伟大创意 第一章:核心测试方法论 1.1 三层测试体系架构 # 第一层:模型级测试 def ...
- linux下的nginx重启命令常见以下3种:
systemctl restart nginx service nginx restart /usr/sbin/nginx -s reload
- docker网络冲突解决(修改docker_gwbridge网段)
1·问题 一次生产搭建服务的时候,出现客户端服务器到docker服务断开不通的情况,在docker服务器上抓包可以抓到客户端服务器的包,但是docker服务器不做任何响应 于是ip route 查看本 ...
- 【工具篇】git常用命令分享
1. 配置 1.1 设置全局用户名和邮箱 git config --global user.name xxx git config --global user.email xxx@xxx.com 上述 ...
- RMQ 和 LCA 问题
# Part 1 RMQ RMQ,即区间信息维护问题 如最大值,最小值,GCD 等 RMQ 算法实现很多 具体有线段树,树状数组和 ST 表 但综合时间复杂度最好的是 ST 表 查询 O(1),预处理 ...
- nodejs终端字符样式和进度条
Nodejs为终端字符增加样式 只有黑白的色调对于比较复杂的命令行程序来说就显得太单调了,我们可以为命令行程序增加样式使得程序更加友好! 安装package: npm install -S chalk ...
- mybatis-plus.global-config.db-config.id-type=auto 和 @TableId(value = "id", type = IdType.ASSIGN_ID)哪个优先生效
对于id自动生成的方式,有注解和配置两种. 含义相同:不过设置自动增长的时候必须保证数据库中id是自增,assign_id和assign_uuid则不需要. yml配置: mybatis-plus: ...
- 🎀截图工具推荐-Snipaste
简介 Snipaste 是一款非常强大且免费的截图和屏幕标记工具,由一位来自中国的开发者开发.它以其简洁的界面和丰富的功能而受到广泛好评. 官网 https://zh.snipaste.com/ Sn ...