奇淫技巧专题
卡常
连续访问
for(int i=1; i<=n; i++) for(int j=n+1; j<=node_cnt; j++)
tr |= upd(dis[j],dis[i]+w[i][j]);
for(int i=n+1; i<=node_cnt; i++) for(int j=1; j<=n; j++)
tr |= upd(dis[j],dis[i]+w[i][j]);
// 495ms
for(int i=1; i<=n; i++) for(int j=n+1; j<=node_cnt; j++)
tr |= upd(dis[j],dis[i]+w[i][j]),
tr |= upd(dis[i],dis[j]+w[j][i]);
// 1.07s
这俩唯一的区别就在于上面的是连续访问内存,对常数影响还挺大的。
图论
竞赛图性质
一个得分合法等价于任意
--
Hall定理
最大匹配 =
有度数限制则为
把
判断一个点是否一定在最大匹配上
对每个没有match的点dfs,dfs到的点都是非必要点
void Mark(int x){
if(vis[x]==now_dfn) return;
unnec[x] = true;
vis[x] = now_dfn;
for(register int u=fst[x]; u; u=edge[u].nxt){
int v=edge[u].to;
if(match[v]) Mark(match[v]);
}
}
for(register int i=1; i<=n; i++) if(!match[i]){
now_dfn = i;
Mark(i);
}
数学
而对角矩阵
复杂度
拆组合
对于某些无法化简的柿子可以这样拆分后求出递推式
如:
快速模
typedef unsigned long long ull;
typedef __uint128_t L;
struct FastMod {
ull b, m;
FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
ull reduce(ull a) {
ull q = (ull)((L(m) * a) >> 64);
ull r = a - q * b; // can be proven that 0 <= r < 2*b
return r >= b ? r - b : r;
}
};
FastMod F(2);
int main() {
int M = 1000000007; F = FastMod(M);
ull x = 10ULL*M+3;
cout << x << " " << F.reduce(x) << "\n"; // 10000000073 3
}
光速幂
预处理
光速乘
ull mul(ull a, ull b, ull p){return ((a * b - (ull)((long double)a * b / p) * p + p) % p);}
本质上是求出答案模 long double有一定误差,最后还要多减/加上几个p
一个数可以分解为
这一步可以在线筛时进行,找到
然后可以预处理
树
点到路径距离
奇妙的差分
若性质P满足:fa[x]满足P
则设x权值为2-d[x](x的度数),求所有满足条件的点权和
即为满足条件的子树个数(互不包含)
判断虚树为链
即虚树只有一个叶子节点,用欧拉序,第一个叶子一定是虚树节点中第一个out,最后一个叶子一定是虚树节点中最后一个in,若第一个叶子等于最后一个叶子则只有一个叶子
字符串
求两个串的
最长回文子序列
字符串的最长回文子序列长度等于其自身与反串的最长公共子序列长度
其他
差分
将恰好等于