版本比较
关于 BSGS
标题
增加 1 行删除 1 行
11模板(更新中)关于 BSGS正文
增加 73 行删除 1042 行
12123456789101112131415456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196161719920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165218192021222324654655656657658659660661662252627282930313233343536373839404166466566666766866967042436726736746756766776786796806816826836846856866876886896906916926934469569669769869970070170270370470570645464748497097105051525354555657585960616263647137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468476566850851852853854676869707172737475856857858859860861862863864865866867868869870871872873874875767778798081828384858788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329338693593693793887888990919294294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106293## 树状数组### 单点修改 + 单点查询## BSGS### BSGS(Baby Step,Gaint Step)算法
给出 $a、b$ 和一个质数 $p$,求最小非负整数 $x$ 满足 $a ^ x \equiv b \pmod p$。
首先根据鸽笼原理得到 $x \le p$,所以令 $x = i \times t - j$,其中 $t = \left \lceil \sqrt p \right \rceil$,$0 \le j < t$,$0 \le i \le t$。
那么原方程变为 $a ^ {i \times t - j} \equiv b \pmod p$,移项得 $a ^ {i \times t} \equiv b \times a ^ j \pmod p$。
于是把所有 $b \times a ^ j \bmod p$ 放入一个哈希表,再遍历 $i$ 查找 $a ^ {i \times t} \bmod p$ 即可做到 $\sqrt p$ 实现啦!
注意特判若 $a = 0$ 且 $b \ne 0$ 时无解。
:::success[code]```cpp#include <iostream>using namespace std;int n, m, now, x, y, tree[500010], a[500010];int lowbit(int x){ return x & (-x);}void add(int l, int a){ for (int i = l; i <= n; i += lowbit(i))tree[i] += a; for (int i = l + 1; i <= n; i += lowbit(i))tree[i] -= a;}//差分,将第l个数加上a void sum(int x){ int s = 0; for (int i = x; i > 0; i -= lowbit(i))s += tree[i];//前缀和 cout << s + a[x] << endl;}//查询数组第x个数并输出int main(){ cin >> n >> m; for (int i = 1; i <= n; i ++)cin >> a[i]; for (int i = 1; i <= m && cin >> now >> x; i ++){ if (now == 1){ cin >> y; add(x, y); } else sum(x); } return 0;}```### 单点修改 + 区间查询```cpp#include <iostream>using namespace std;int n, m, now, x, y, tree[500010];int lowbit(int x){ return x & (-x);}void add(int l, int a){ for (int i = l; i <= n; i += lowbit(i))tree[i] += a;}//将第l个数加上avoid sum(int l, int r){ int s = 0; for (int i = r; i > 0; i -= lowbit(i))s += tree[i]; for (int i = l - 1; i > 0; i -= lowbit(i))s -= tree[i]; cout << s << endl;}//查询数组从l到r的区间和并输出int main(){ cin >> n >> m; for (int i = 1; i <= n && cin >> x; i ++)add(i, x); for (int i = 1; i <= m && cin >> now >> x >> y; i ++){ if (now == 1)add(x, y); else sum(x, y); } return 0;}```### 区间修改 + 单点查询```cpp#include <iostream>using namespace std;int n, m, now, x, y, z, a[500010], c[500010];int lowbit(int x){ return x & -x;}void add(int l, int r, int sum){ for (int i = l; i <= n; i += lowbit(i))c[i] += sum; for (int i = r + 1; i <= n; i += lowbit(i))c[i] -= sum;//差分}//差分数组,从l到r全部加上sumvoid sum(int x){ int s = 0; for (int i = x; i > 0; i -= lowbit(i))s += c[i];//前缀和统计 cout << a[x] + s << endl;}//查询第x个数的值并输出int main(){ cin >> n >> m; for (int i = 1; i <= n; i ++)cin >> a[i]; for (int i = 1; i <= m && cin >> now >> x; i ++){ if (now == 1){ cin >> y >> z; add(x, y, z); } else sum(x); } return 0;}```### 区间修改 + 区间查询```cpp#include <iostream>using namespace std;long long n, m, now, x, y, z, a1, a2, b[100010], c[100010];long long lowbit(long long x){ return x & (-x);}void add(long long l, long long r, long long sum){ for (int i = l; i <= n; i += lowbit(i))c[i] += sum; for (int i = r + 1; i <= n; i += lowbit(i))c[i] -= sum; for (int i = l; i <= n; i += lowbit(i))b[i] += sum * (l - 1); for (int i = r + 1; i <= n; i += lowbit(i))b[i] -= sum * r;}//差分,从l到r每个数加上sum,b[i] = c[i] * (i - 1)void sum(long long l, long long r){ long long sr = 0, sl = 0; for (int i = r; i > 0; i -= lowbit(i))sr += c[i]; for (int i = l - 1; i > 0; i -= lowbit(i))sl += c[i]; sr = sr * r - sl * (l - 1); for (int i = r; i > 0; i -= lowbit(i))sr -= b[i]; for (int i = l - 1; i > 0; i -= lowbit(i))sr += b[i]; cout << sr << endl;}//前缀和相加,从l到r的和//从l到r的和为c数组从1到r的和减从1到l - 1的和//化简为c数组从1到r的和乘以r减去b数组从1到r的和int main(){ cin >> n >> m; for (int i = 1; i <= n && cin >> a1; i ++)add(i, n, a1 - a2), a2 = a1;//求差分 for (int i = 1; i <= m && cin >> now >> x >> y; i ++){ if (now == 1){ cin >> z; add(x, y, z); } else sum(x, y); } return 0;}```## 线段树### 单点修改 + 单点查询```cpp#include <iostream>using namespace std;long long n, w, a, b, c, tree[400010];void add(long long l, long long r, long long now, long long h, long long sum){ if (l == r){ tree[now] += sum; return; } long long mid = l + r >> 1; if (h <= mid)add(l, mid, now << 1, h, sum); else add(mid + 1, r, now << 1 | 1, h, sum); tree[now] = tree[now << 1] + tree[now << 1 | 1];}//将第h点加上sum long long sum_tree(long long l, long long r, long long now, long long x){ if (l == r)return tree[now]; long long mid = l + r >> 1; if (x <= mid)return sum_tree(l, mid, now << 1, x); else return sum_tree(mid + 1, r, now << 1 | 1, x);}//在数组中查询点x int main(){ cin >> n >> w; for (int i = 1; i <= n && cin >> a; i ++)add(1, n, 1, i, a);//建树 for (int i = 1; i <= w && cin >> a >> b; i ++){ if (a == 1){ cin >> c; add(1, n, 1, b, c);//单点修改 } else cout << sum_tree(1, n, 1, b) << endl;//单点查询 } return 0;}```### 单点修改 + 区间查询```cpp#include <iostream>using namespace std;long long n, w, a, b, c, tree[400010];void add(long long l, long long r, long long now, long long h, long long sum){ if (l == r){ tree[now] += sum; return; } long long mid = l + r >> 1; if (h <= mid)add(l, mid, now << 1, h, sum); else add(mid + 1, r, now << 1 | 1, h, sum); tree[now] = tree[now << 1] + tree[now << 1 | 1];}//将第h点加上sum long long sum_tree(long long l, long long r, long long now, long long x, long long y){ if (x <= l && r <= y)return tree[now]; long long mid = l + r >> 1, ans = 0; if (x <= mid)ans += sum_tree(l, mid, now << 1, x, y); if (mid < y)ans += sum_tree(mid + 1, r, now << 1 | 1, x, y); return ans;}//在区间[x, y]中求和 int main(){ cin >> n >> w; for (int i = 1; i <= n && cin >> a; i ++) add(1, n, 1, i, a);//建树 for (int i = 1; i <= w && cin >> a >> b >> c; i ++){ if (a == 1)add(1, n, 1, b, c);//单点修改 else cout << sum_tree(1, n, 1, b, c) << endl;//区间查询 } return 0;}```### 区间修改 + 区间查询(乘 + 加)```cpp#include <bits/stdc++.h>using namespace std;int n, q, mod, a[100010], tree[400010], mul[400010], add[400010], opt, x, y, z;void down(int now, int l, int r){//懒标记下传 int mid = l + r >> 1; mul[now << 1] = mul[now << 1] * 1LL * mul[now] % mod, mul[now << 1 | 1] = mul[now << 1 | 1] * 1LL * mul[now] % mod; add[now << 1] = add[now << 1] * 1LL * mul[now] % mod, add[now << 1 | 1] = add[now << 1 | 1] * 1LL * mul[now] % mod; tree[now << 1] = tree[now << 1] * 1LL * mul[now] % mod, tree[now << 1 | 1] = tree[now << 1 | 1] * 1LL * mul[now] % mod; (add[now << 1] += add[now]) %= mod, (add[now << 1 | 1] += add[now]) %= mod; (tree[now << 1] += add[now] * 1LL * (mid - l + 1) % mod) %= mod, (tree[now << 1 | 1] += add[now] * 1LL * (r - mid) % mod) %= mod; add[now] = 0, mul[now] = 1;}void init(int now, int l, int r){//初始化 mul[now] = 1;//乘法初始化为 1 if (l == r){ tree[now] = a[l]; return; } int mid = l + r >> 1; init(now << 1, l, mid), init(now << 1 | 1, mid + 1, r); tree[now] = (tree[now << 1] + tree[now << 1 | 1]) % mod;}void multion(int now, int l, int r){//乘法 if (x <= l && r <= y){ mul[now] = (1LL * mul[now] * z) % mod, add[now] = (1LL * add[now] * z) % mod, tree[now] = (1LL * tree[now] * z) % mod; return; } int mid = l + r >> 1; down(now, l, r); if (x <= mid)multion(now << 1, l, mid); if (y > mid)multion(now << 1 | 1, mid + 1, r); tree[now] = (tree[now << 1] + tree[now << 1 | 1]) % mod;}void plusion(int now, int l, int r){//加法 if (x <= l && r <= y){ (add[now] += z) %= mod, (tree[now] += z * 1LL * (r - l + 1)) % mod; return; } int mid = l + r >> 1; down(now, l, r); if (x <= mid)plusion(now << 1, l, mid); if (y > mid)plusion(now << 1 | 1, mid + 1, r); tree[now] = (tree[now << 1] + tree[now << 1 | 1]) % mod;}int query(int now, int l, int r){//查询 if (x <= l && r <= y)return tree[now]; int ans = 0, mid = l + r >> 1; down(now, l, r); if (x <= mid)(ans += query(now << 1, l, mid)) %= mod; if (y > mid)(ans += query(now << 1 | 1, mid + 1, r)) %= mod; return ans;}int main(){ ios::sync_with_stdio(0); cin.tie(0); cin >> n >> q >> mod; for (int i = 1; i <= n; i ++)cin >> a[i]; init(1, 1, n); while (q -- && cin >> opt >> x >> y){ if (opt == 1){ cin >> z; multion(1, 1, n); } else if (opt == 2){ cin >> z; plusion(1, 1, n); } else cout << query(1, 1, n) << '\n'; } return 0;}```## 快读### 基本版(int)```cppvoid read(int &x){ char ch = getchar_unlocked(); bool f = 0; x = 0; while (ch < '0' || ch > '9')f |= ch == '-', ch = getchar_unlocked(); while (ch >= '0' && ch <= '9')x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar_unlocked(); if (f)x = -x;}```### 基本版(double)```cppvoid read(double &x){ char ch = getchar(); double now = 0.1; bool f = 0; x = 0; while (ch < '0' || ch > '9')f |= ch == '-', ch = getchar(); while (ch >= '0' && ch <= '9')x = x * 10 + (ch ^ 48), ch = getchar(); if (ch != '.'){ x = (f ? -x : x); return; } ch = getchar(); while (ch >= '0' && ch <= '9')x += now * (ch ^ 48), now *= 0.1, ch = getchar(); x = (f ? -x : x);}```### fread 优化版(int)```cpp//使用方法:IO::cin,IO::cout//代码末尾加 IO::cout.Flush()namespace IO{ const int BUFSIZE = 1 << 20; char ibuf[BUFSIZE], *is = ibuf, *it = ibuf, tmp[BUFSIZE]; int cnt = 0; inline char getch(){ if (is == it)it = (is = ibuf) + fread(ibuf, 1, BUFSIZE, stdin); return is == it ? EOF : *is ++; } int readInt(){ int x = 0, y = 0; char c = 0; while (!isdigit(c))y |= c == '-', c = getch(); while (isdigit(c))x = (x << 3) + (x << 1) + (c ^ 48), c = getch(); return !y ? x : -x; } void flush(){ fwrite(tmp, 1, cnt, stdout); cnt = 0; } void putch(char c){ tmp[cnt ++] = c; if (cnt == BUFSIZE)flush(); } void putint(int x){ char Q[10]; int tp = 0; if (x < 0)putch('-'), x = -x; if (!x)putch('0'); while (x)Q[tp ++] = (x % 10 + 48), x /= 10; while (tp --)putch(Q[tp]); } struct Basic{ Basic & operator >> (int &A){ A = IO::readInt(); return (*this); } Basic & operator << (const int &A){ putint(A); return (*this); } void Flush(){ flush(); } }cin, cout;}```## 单源最短路### Dijkstra(堆优化)```cpp#include <bits/stdc++.h>using namespace std;int n, m, s, x, y, z, ans[100010];bool vis[100010];pair<int, int> now;vector<pair<int, int> > v[100010]; priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;int main(){ cin >> n >> m >> s;//由s至全图的最短路 for (int i = 1; i <= 1e5 + 1; i ++)ans[i] = 1e10; ans[s] = 0, q.push({0, s}); for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[x].push_back({y, z}); while (q.size()){ now = q.top(), q.pop(); if (vis[now.second])continue; vis[now.second] = 1; for (auto u : v[now.second]){ if (ans[u.first] > ans[now.second] + u.second){ ans[u.first] = ans[now.second] + u.second; if (!vis[u.first])q.push({ans[u.first], u.first}); } } } for (int i = 1; i <= n; i ++)cout << ans[i] << " "; return 0;}```### SPFA```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int n, m, s, x, y, z, ans[100010];bool vis[100010];vector<pair<int, int> > v[100010];bool spfa(){ queue<int> q; q.push(s), vis[s] = 1, ans[s] = 0; while (q.size()){ int now = q.front(); q.pop(), vis[now] = 0; for (auto u : v[now]){ if (ans[u.first] > ans[now] + u.second){ ans[u.first] = ans[now] + u.second; if (!vis[u.first])vis[u.first] = 1, q.push(u.first); } } } return false;}signed main(){ cin >> n >> m >> s;//由s至全图的最短路 for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[x].push_back({y, z}); for (int i = 1; i <= n; i ++)ans[i] = 2e9; spfa(); for (int i = 1; i <= n; i ++)cout << (ans[i] == 2e9 ? -1 : ans[i]) << " "; return 0;}```## 多源最短路### Floyd```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int n, m, q, x, y, z, ans[110][110];signed main(){ cin >> n >> m >> q; for (int i = 1; i <= n; i ++){ for (int j = 1; j <= n; j ++)ans[i][j] = (i == j ? 0 : 1e15); } for (int i = 1; i <= m && cin >> x >> y >> z; i ++)ans[x][y] = min(ans[x][y], z); for (int k = 1; k <= n; k ++){ for (int i = 1; i <= n; i ++){ for (int j = 1; j <= n; j ++)ans[i][j] = min(ans[i][j], ans[i][k] + ans[k][j]); } } while (q -- && cin >> x >> y)cout << (ans[x][y] == 1e15 ? -1 : ans[x][y]) << endl; return 0;}```## 割点### Tarjan```cpp#include <bits/stdc++.h>using namespace std;int n, m, x, y, num, sum, root, low[20010], dfn[20010];bool ans[20010];vector<int> v[20010];void dfs(int now){ low[now] = dfn[now] = ++ num; int child = 0; for (auto u : v[now]){ if (!dfn[u])child ++, dfs(u), low[now] = min(low[u], low[now]), sum += (!ans[now] && now != root && low[u] >= dfn[now]), ans[now] |= (now != root && low[u] >= dfn[now]);//判定1 else low[now] = min(low[now], dfn[u]); } if (now == root && child >= 2)sum += !ans[now], ans[now] = 1;//判定2}int main(){ cin >> n >> m; for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x); for (int i = 1; i <= n; i ++){ if(!dfn[i])root = i, dfs(i); } cout << sum << endl;//数量 for (int i = 1; i <= n; i ++){ if (ans[i])cout << i << " "; } return 0;}```## 割边(桥)### Tarjan```cpp#include <bits/stdc++.h>using namespace std;int n, m, a, b, low[200], dfn[200], num;vector<int> v[200];priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;//按照由小至大顺序输出void dfs(int now, int fa){ low[now] = dfn[now] = ++ num; for (auto u : v[now]){ if (fa == u)continue; if (!dfn[u]){ dfs(u, now), low[now] = min(low[now], low[u]); if (low[u] > dfn[now])q.push({min(now, u), max(now, u)});//满足判定条件 } else low[now] = min(low[now], dfn[u]); }}int main(){ cin >> n >> m; for (int i = 1; i <= m && cin >> a >> b; i ++)v[a].push_back(b), v[b].push_back(a);//无向图存边 dfs(1, 0); while (q.size()){ cout << q.top().first << " " << q.top().second << endl; q.pop(); } return 0;}```## 拓扑排序```cpp#include <bits/stdc++.h>using namespace std;int n, m, x, y, d[1010];vector<int> v[1010];queue<int> q;queue<int> ans;int main(){ cin >> n >> m; for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back(y), d[y] ++; for (int i = 1; i <= n; i ++){ if (!d[i])q.push(i);//统计入度为0的点 } while (q.size()){ int now = q.front(); q.pop(), ans.push(now); for (auto u : v[now]){ d[u] --;//删边 if (!d[u])q.push(u); } } if (ans.size() < n){ cout << -1; return 0; } while (ans.size()){ cout << ans.front() << " "; ans.pop(); } return 0;}```## 最小生成树### Kruskal```cpp#include <bits/stdc++.h>#define int long longusing namespace std;struct node{ int x, y, z;}a[400010];int n, m, ans, sum, fa[5010];bool cmp(node b, node c){ return b.z < c.z;//对权值进行排序}int find(int now){ if (fa[now] == now)return now; return fa[now] = find(fa[now]);}signed main(){ cin >> n >> m; for (int i = 1; i <= m && cin >> a[i].x >> a[i].y >> a[i].z; i ++)a[i + m] = node{a[i].y, a[i].x, a[i].z};//无向图存边 sort(a + 1, a + 2 * m + 1, cmp); for (int i = 1; i <= n; i ++)fa[i] = i; for (int i = 1; i <= 2 * m; i ++){ if (find(a[i].x) != find(a[i].y))fa[find(a[i].y)] = fa[find(a[i].x)], ans += a[i].z, sum ++; if (sum >= n - 1){//已添完所有边 cout << ans; return 0; } } cout << "orz";//不连通 return 0;}```### Prim```cpp#include <bits/stdc++.h>using namespace std;long long n, m, x, y, z, sum, cnt, d[5010];bool vis[5010];vector<pair<long long, long long> > v[5010];int main(){ cin >> n >> m; for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[x].push_back({y, z}), v[y].push_back({x, z}); for (int i = 0; i <= n; i ++)d[i] = 1e9; d[1] = 0; for (int i = 1; i <= n; i ++){ long long id = 0; for (int j = 1; j <= n; j ++){ if (!vis[j] && d[j] < d[id])id = j; } if (!id){ cout << "orz"; return 0; } vis[id] = 1, sum += d[id]; for (auto u : v[id]){ if (!vis[u.first])d[u.first] = min(d[u.first], 1LL * u.second); } } cout << sum; return 0;}```## 点双连通分量### Tarjan```cpp#include <bits/stdc++.h>using namespace std;int n, m, x, y, low[500010], dfn[500010], num, root, sum = 1;vector<int> v[500010], ans[500010];stack<int> sta;void dfs(int now, int fa){ low[now] = dfn[now] = ++ num, sta.push(now); int child = 0; for (auto u : v[now]){ if (u == fa)continue; if (!dfn[u]){ child ++, dfs(u, now), low[now] = min(low[now], low[u]); if (low[u] >= dfn[now]){ while (sta.top() != u)ans[sum].push_back(sta.top()), sta.pop(); ans[sum].push_back(u), ans[sum ++].push_back(now), sta.pop();//找到割点,记录 } } else low[now] = min(low[now], dfn[u]); } if (now == root && !child)ans[sum ++].push_back(now);//特判只有一个点}int main(){ ios::sync_with_stdio(0); cin.tie(); cout.tie(); cin >> n >> m; for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x); for (int i = 1; i <= n; i ++){ if (!dfn[i])root = i, dfs(i, 0);//图不保证连通 } cout << -- sum << endl;//压行的奇怪手段 for (int i = 1; i <= sum; i ++){ cout << ans[i].size() << " "; for (auto u : ans[i])cout << u << " "; cout << endl; } return 0;}```## 边双连通分量### Tarjan```cpp#include <bits/stdc++.h>using namespace std;int n, m, x, y, low[500010], dfn[500010], num, sum;vector<int> ans[500010];stack<int> sta;vector<pair<int, int> > v[500010];void tarjan(int now, int fa){ low[now] = dfn[now] = ++ num, sta.push(now); for (auto u : v[now]){ if (u.second == (fa ^ 1))continue;//这里的fa表示边的编号 if (!dfn[u.first]){ tarjan(u.first, u.second), low[now] = min(low[now], low[u.first]); } else low[now] = min(low[now], dfn[u.first]); } if (low[now] == dfn[now]){//为割边(桥),记录 ans[++ sum].push_back(now); while (sta.top() != now)ans[sum].push_back(sta.top()), sta.pop(); sta.pop();struct custom_hash{ static uint64_t splitmix64(uint64_t x){ x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x=(x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); }}int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back({y, i << 1}), v[y].push_back({x, i << 1 | 1});//为每条边编号 for (int i = 1; i <= n; i ++){ if (!dfn[i])tarjan(i, 0);//图不保证连通 size_t operator()(uint64_t x) const{ static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); }};void BSGS(int a, int b, int p){ unordered_map<int, int, custom_hash> vis; int t = ceil(sqrt(p)), val = 1; for (int i = 0; i < t; i ++) vis[1LL * b * val % p] = i, val = 1LL * val * a % p; if (!a && b){cout << "no solution";return;} a = val, val = 1; for (int i = 0, j; i <= t; i ++){ j = vis.find(val) == vis.end() ? - 1 : vis[val]; if (~ j && i * t - j >= 0){cout << i * t - j;return;} val = 1LL * val * a % p; } cout << sum << endl; for (int i = 1; i <= sum; i ++){ cout << ans[i].size() << " "; for (auto u : ans[i])cout << u << " "; cout << endl; } return 0; cout << "no solution";}```## 强连通分量### Tarjan```cpp#include <bits/stdc++.h>using namespace std;int n, m, x, y, dfn[10010], low[10010], num, sum;bool vis[10010];vector<int> v[10010], ans[10010];stack<int> sta;void dfs(int now){ dfn[now] = low[now] = ++ num, sta.push(now), vis[now] = true; for (auto u : v[now]){ if (!dfn[u])dfs(u), low[now] = min(low[now], low[u]); else if (vis[u])low[now] = min(low[now], dfn[u]); } if (low[now] == dfn[now]){//割边 ans[++ sum].push_back(now), vis[now] = false; while (sta.top() != now)ans[sum].push_back(sta.top()), vis[sta.top()] = false, sta.pop(); sta.pop(); }}int main(){ cin >> n >> m; for (int i = 1; i <= m && cin >> x >> y; i ++)v[x].push_back(y); for (int i = 1; i <= n; i ++){ if (!dfn[i])dfs(i); } cout << sum << endl; for (int i = 1; i <= sum; i ++){ cout << ans[i].size() << " "; for (auto u : ans[i])cout << u << " "; cout << endl; } return 0; int p, b, n; cin >> p >> b >> n; return BSGS(b, n, p), 0;}```## 负(权)环### SPFA:::
### exBSGS 算法
若 $a$ 与 $p$ 不互质,则需要不断将 $a$、$b$ 和 $p$ 除去 $\gcd(a, \ p)$,其中若出现 $b$ 有余数的情况则必然无解。
记每次的 $\gcd(a_i, \ p_i) = d_i$,则得到:
$$\prod \limits _ {i = 1} ^ k \frac{a}{d_i} · a ^ {x - k} \equiv \frac{b}{\prod \limits _ {i = 1} ^ k d_i} \pmod p$$
用 BSGS 求解即可。
:::success[code]```cpp#include <bits/stdc++.h>using namespace std;int T, n, m, x, y, z, ans[2010], sum[2010];bool vis[2010];vector<pair<int, int> > v[2010];bool spfa(int now){ for (int i = 1; i <= n; i ++)vis[i] = 0, sum[i] = 0, ans[i] = 1e9;//初始化 queue<int> q; q.push(now), ans[now] = 0, vis[now] = 1; while (q.size()){ int now = q.front(); q.pop(), vis[now] = 0; for (auto u : v[now]){ if (ans[u.first] > ans[now] + u.second){ ans[u.first] = ans[now] + u.second, sum[u.first] = sum[now] + 1; if (sum[u.first] >= n)return true;//若经过的点超过整张图的点数则存在负(权)环 if (!vis[u.first])vis[u.first] = 1, q.push(u.first); } } } return false;}int main(){ cin >> n >> m; bool flag = false; for (int i = 1; i <= 2001; i ++)v[i].clear(); for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[x].push_back({y, z}); for (int i = 1; i <= n; i ++){//图不保证连通,需要每个子图都遍历一次 if (!vis[i]){ if (spfa(i)){ cout << "YES" << endl; flag = true; break; } } } if (!flag)cout << "NO" << endl; return 0;}```## KMP```cpp#include <bits/stdc++.h>using namespace std;string s1, s2;int l1, l2, n[1000010];int main(){ cin >> s1 >> s2; l1 = s1.size(), l2 = s2.size(); for (int i = 1, j = 0; i < l2; i ++){ while (j && s2[i] != s2[j])j = n[j - 1]; n[i] = (j += (s2[i] == s2[j])); } for (int i = 0, j = 0; i < l1; i ++){ while (j && s1[i] != s2[j])j = n[j - 1]; if (s1[i] == s2[j])j ++; if (j == l2){ cout << i - l2 + 2 << '\n'; j = n[j - 1]; } } return 0;}```## 哈希### 进制哈希```cpp#include <bits/stdc++.h>#define int unsigned long longusing namespace std;int n, ans, now, l = 132135461;//随意一个数(最好是质数)string s;map<int, bool> m;int Hash(string x){ int sum = 0, len = x.size(); for (int i = 0; i < len; i ++)sum = (sum * l) + x[i];//模拟进制 return sum;}signed main(){ ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n && cin >> s; i ++)now = Hash(s), ans += !m[now], m[now] = 1; cout << ans; return 0;}```## ST表```cpp#include <bits/stdc++.h>using namespace std;int n, m, l, r, maxn[100010][18], k;//从第i个数开始长度为2^j的序列最大值int main(){ cin >> n >> m; for (int i = 1; i <= n && cin >> maxn[i][0]; i ++); k = log2(n); for (int i = 1; i <= k; i ++){ for (int j = 1; j + (1 << i) - 1 <= n; j ++)maxn[j][i] = max(maxn[j][i - 1], maxn[j + (1 << i - 1)][i - 1]);//O(nlogn)更新ST表 } while (m -- && cin >> l >> r){ k = log2(r - l + 1); cout << max(maxn[l][k], maxn[r - (1 << k) + 1][k]) << '\n';//找最大能覆盖的区间 } return 0;}```## 树的重心```cpp#include <bits/stdc++.h>using namespace std;int n, x, y, son[50010], w[50010];vector<int> v[50010];priority_queue<int, vector<int>, greater<int> > ans;void dfs(int fa, int now){ son[now] = 1, w[now] = 0; for (auto u : v[now]){ if (u != fa)dfs(now, u), son[now] += son[u], w[now] = max(w[now], son[u]); } w[now] = max(w[now], n - son[now]); if (w[now] <= n / 2)ans.push(now);}int main(){ cin >> n; for (int i = 1; i < n && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x); dfs(0, 1); while (ans.size()){ cout << ans.top() << " "; ans.pop(); } return 0;}```## 树的直径### 树形 dp```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int n, x, y, z, f[500010], ans;vector<pair<int, int> > v[500010];void dfs(int fa, int now){ for (auto u : v[now]){ if (u.first != fa)dfs(now, u.first), ans = max(ans, f[now] + f[u.first] + u.second), f[now] = max(f[now], f[u.first] + u.second);void exBSGS(int a, int b, int p){ if (p == 1 || b == 1){cout << "0\n";return;} if (!a){cout << (b ? "No Solution\n" : "1\n");return;} int d, k = 0, D = 1; while ((d = __gcd(a, p)) != 1){ if (b % d){cout << "No Solution\n";return;} b /= d, p /= d, D = D * a / d % p, k ++; if (D == b){cout << k << '\n';return;} }}signed main(){ ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i < n && cin >> x >> y >> z; i ++)v[x].push_back({y, z}), v[y].push_back({x, z}); dfs(0, 1); cout << ans; return 0;}```### dfs```cpp#include <bits/stdc++.h>using namespace std;int n, x, y, d[100010], s, sum;vector<int> v[100010];void dfs(int fa, int now){ for (auto u : v[now]){ if (u != fa)d[u] = d[now] + 1, s = (d[u] > d[s] ? u : s), dfs(now, u); unordered_map<int, int> vis; int t = ceil(sqrt(p)), i, val = b, v = 1; for (i = 0; i < t; v = v * a % p, i ++) vis[val] = i, val = val * a % p; for (i = 1, val = D; i <= t; i ++){ if (vis.count(val = val * v % p)) {cout << i * t - vis[val] + k << '\n';return;} } cout << "No Solution\n";}int main(){ cin >> n; for (int i = 1; i < n && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x); dfs(0, 1), d[s] = 0, dfs(0, s); cout << d[s]; return 0;}```## LCA### 倍增```cpp#include <bits/stdc++.h>using namespace std;int n, m, s, x, y, k, fa[500010][20], d[500010];vector<int> v[500010];void dfs(int now, int f){ d[now] = d[f] + 1, fa[now][0] = f; for (int i = 1; (1 << i) <= d[now]; i ++)fa[now][i] = fa[fa[now][i - 1]][i - 1];//类似ST表预处理祖先节点 for (auto u : v[now]){ if (u != f)dfs(u, now); }}int solve(int a, int b){ if (d[b] > d[a])swap(a, b);//始终让a在b后面(深度较大) for (int i = 19; i >= 0; i --){ if (d[a] - (1 << i) >= d[b])a = fa[a][i];//把a的深度跳至b } if (a == b)return a; for (int i = 19; i >= 0; i --){ if (fa[a][i] != fa[b][i])a = fa[a][i], b = fa[b][i];//a和b同时跳 } return fa[a][0];}int main(){ cin >> n >> m >> s; for (int i = 1; i < n && cin >> x >> y; i ++)v[x].push_back(y), v[y].push_back(x); dfs(s, 0); for (int i = 1; i <= m && cin >> x >> y; i ++)cout << solve(x, y) << '\n'; return 0;}```## 拓展欧几里得```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int x, y;void exgcd(int a, int b){//求特殊解 if (!b){ x = 1, y = 0;//ax + by = a 时的解 return; } exgcd(b, a % b);//辗转相除 int now = x; x = y, y = now - a / b * y;}signed main(){ int a, b; cin >> a >> b; exgcd(a, b); cout << (x % b + b) % b;//最小解 ios::sync_with_stdio(0), cin.tie(0); int p, a, b; while (cin >> a >> p >> b && a && b && p)exBSGS(a % p, b % p, p); return 0;}```## 斜率优化(格式)```cpp#include <bits/stdc++.h>using namespace std;long long n, nx, bk = 1, fr = 1, f[1000010];long double X(int x){ return ();}long double Y(int x){ return ();}long double slope(int x, int y){ return (X(x) - X(y)) / (Y(x) - Y(y));//求斜率 }int main(){ cin >> n; ()//输入 for (int i = 1; i <= n; i ++){//单调队列维护下凸壳 while (fr < bk && slope(q[fr + 1], q[fr]) < ())fr ++; f[i] = (); while (fr < bk && slope(q[bk], q[bk - 1]) > slope(i, q[bk]))bk --; q[++ bk] = i; } cout << f[n]; return 0;}```## 差分约束```cpp#include <bits/stdc++.h>using namespace std;long long n, m, x, y, z, d[5010], sum[5010];bool vis[5010];vector<pair<int, int> > v[5010];queue<int> q;bool spfa(){//跑最短路 q.push(0), vis[0] = 1, sum[0] = 1; while (q.size()){ int now = q.front(); q.pop(), vis[now] = 0; for (auto u : v[now]){ if (d[u.first] > d[now] + u.second){ d[u.first] = d[now] + u.second, sum[u.first] = sum[now] + 1; if (sum[u.first] > n + 1)return true;//注意多了一个点 if (!vis[u.first])vis[u.first] = 1, q.push(u.first); } } } return false;}int main(){ cin >> n >> m; for (int i = 1; i <= m && cin >> x >> y >> z; i ++)v[y].push_back({x, z});//存边 for (int i = 1; i <= n; i ++)d[i] = 1e9, v[0].push_back({i, 0});//建一个虚点 if (spfa()){ cout << "NO"; return 0; } for (int i = 1; i <= n; i ++)cout << d[i] << " "; return 0;}```## 字典树```cpp#include <bits/stdc++.h>using namespace std;int T, n, q, cnt[3000010], t[3000010][65], num;string s;int g(char x){ return (x >= 'A' && x <= 'Z' ? x - 'A' : (x >= 'a' && x <= 'z' ? x - 'a' + 26 : x - '0' + 52));}int main(){ ios::sync_with_stdio(0); cin.tie(0); cin >> T; while (T -- && cin >> n >> q){ for (int i = 1, np = 0; i <= n && cin >> s; np = 0, i ++){ for (int j = 0, l = s.size(), c = g(s[j]); j < l; np = t[np][c], cnt[np] ++, c = g(s[++ j])){ if (!t[np][c])t[np][c] = ++ num;//建树 } } for (int i = 1, ans = 0; i <= q && cin >> s; ans = 0, i ++){ for (int j = 0, l = s.size(), np = 0, c = g(s[j]); j < l; np = t[np][c], ans = cnt[np], c = g(s[++ j])){ if (!t[np][c]){//查询 ans = 0; break; } } cout << ans << '\n'; } for (int i = 0; i <= num; cnt[i ++] = 0){ for (int j = 0; j <= 64; j ++)t[i][j] = 0; } num = 0;//多测清空 } return 0;}```## Manacher(马拉车)```cpp#include <bits/stdc++.h>using namespace std;int d[22000010], l, r, ans;string s;char now[22000010];int main(){ ios::sync_with_stdio(0); cin.tie(0); cin >> s; now[0] = '#'; for (int i = 0, l = s.size(); i < l; ++ i)now[i << 1 | 1] = s[i], now[(i << 1) + 2] = '#';//加间隔符 for (int i = 0, n = (s.size() << 1) + 1; i < n; ans = max(ans, d[i ++])){ int k = i > r ? 1 : min(d[l + r - i], r - i + 1);//找到当前最长的回文串长度(总长的一半) while (0 <= i - k && i + k < n && now[i - k] == now[i + k])k ++;//不断拓展 d[i] = k --; if (i + k > r)l = i - k, r = i + k;//更新最大右端点 } cout << ans - 1; return 0;}```:::