#include <bits/stdc++.h>using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int M = 1000005;
const int I = 0x3f3f3f3f;
const ll F = 0x3f3f3f3f3f3f3f3fLL;
const int G = 20;
int hd[M];
int to[2 * M];
int wt[2 * M];
int nx[2 * M];
int ct = 0;
void init_cfs(int n) {
for (int i = 1; i <= n; i++) hd[i] = -1;
ct = 0;
}
void add_edge(int u, int v, int w) {
to[++ct] = v;
wt[ct] = w;
nx[ct] = hd[u];
hd[u] = ct;
}
void add_unedge(int u, int v, int w) {
add_edge(u, v, w);
add_edge(v, u, w);
}
1.3 LCT
C++
const int LN = 3e5+5;
struct node {int fa, ch[2], sum, val, lazy;}t[N];
#define lc t[x].ch[0]
#define rc t[x].ch[1]
bool isroot(int x) {
int g = t[x].fa;
return t[g].ch[0] != x && t[g].ch[1] != x;
}
void pushup(int x) {t[x].sum = t[x].val ^ t[lc].sum ^ t[rc].sum;} // 按要求改void reverse(int x) {
if (!x) return;
swap(lc,rc);
t[x].lazy^=1;
}
void pushdown(int x) {
if (t[x].lazy) {
reverse(lc);
reverse(rc);
t[x].lazy=0;
}
}
void push(int x) {
if (!isroot(x)) push(t[x].fa);
pushdown(x);
}
void rotate(int x) {
int y = t[x].fa;
int z = t[y].fa;
int k = t[y].ch[1] == x;
if (!isroot(y)) t[z].ch[t[z].ch[1] == y] = x;
t[x].fa = z;
t[y].ch[k] = t[x].ch[k^1];
if (t[x].ch[k^1]) t[t[x].ch[k^1]].fa = y;
t[y].fa = x;
t[x].ch[k^1] = y;
pushup(y);
}
void splay(int x) {
int y, z;
push(x);
while (!isroot(x)) {
y = t[x].fa, z = t[y].fa;
if (!isroot(y)) (t[z].ch[0] == y) ^ (t[y].ch[0] == x) ? rotate(x) : rotate(y);
rotate(x);
}
pushup(x);
}
void access(int x) {
for (int child = 0; x; child = x, x = t[x].fa) {
splay(x);
rc = child;
pushup(x);
}
}
void makeroot(int x) {access(x); splay(x); reverse(x);}
void split(int x, int y) {makeroot(x); access(y); splay(y);}
void link(int x, int y) {makeroot(x); t[x].fa = y;}
void cut(int x, int y) {
split(x, y);
if (t[y].ch[0] != x || rc) return;
t[x].fa = t[y].ch[0] = 0;
pushup(y);
}
int findroot(int x) {
access(x); splay(x);
while (lc) pushdown(x), x = lc;
return x;
}
#undef lc#undef rc
2. 图论算法
2.1 DFS
C++
bool vs[M];
void dfs(int u) {
if (vs[u]) return;
vs[u] = true;
for (int v : g[u]) {
if (!vs[v]) dfs(v);
}
}
2.2 BFS
C++
bool vs[M];
void bfs(int s) {
queue<int> q;
q.push(s);
vs[s] = true;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : g[u]) {
if (!vs[v]) {
vs[v] = true;
q.push(v);
}
}
}
}
2.3 拓扑排序
C++
vector<int> id(M + 1, 0);
void topo() {
queue<int> q;
for (int i = 1; i <= n; i++) {
if (id[i] == 0) q.push(i);
}
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : g[u]) {
id[v]--;
if (id[v] == 0) q.push(v);
}
}
}
2.4 堆优化 Dijkstra
C++
int ds[M];
bool vs[M];
void dijkstra(int s) {
fill(ds, ds + n + 1, I);
fill(vs, vs + n + 1, false);
priority_queue<
pair<int, int>,
vector<pair<int, int>>,
greater<pair<int, int>>> pq;
ds[s] = 0;
pq.push({0, s});
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (d != ds[u]) continue;
if (vs[u]) continue;
vs[u] = true;
for (auto [v, w] : wg[u]) {
if (!vs[v] && ds[v] > ds[u] + w) {
ds[v] = ds[u] + w;
pq.push({ds[v], v});
}
}
}
}
2.5 并查集 DSU
C++
int pa[M], rk[M];
void init_dsu(int n) {
for (int i = 1; i <= n; i++) {
pa[i] = i;
rk[i] = 0;
}
}
int find(int x) {
return pa[x] == x ? x : pa[x] = find(pa[x]);
}
bool unite(int x, int y) {
int rx = find(x), ry = find(y);
if (rx == ry) return false;
if (rk[rx] < rk[ry]) swap(rx, ry);
pa[ry] = rx;
if (rk[rx] == rk[ry]) rk[rx]++;
return true;
}
2.6 Kruskal 最小生成树
C++
struct E {
int u, v, w;
bool operator<(const E& o) const {
return w < o.w;
}
};
ll kruskal(vector<E>& es) {
sort(es.begin(), es.end());
init_dsu(n);
ll mw = 0;
int ec = 0;
for (const E& e : es) {
if (unite(e.u, e.v)) {
mw += e.w;
if (++ec == n - 1) return mw;
}
}
return -1;
}
2.7 SPFA 判负环
C++
bool iq[M];
int cs[M];
// true 无负环,false 有负环bool spfa(int s) {
fill(ds, ds + n + 1, I);
fill(iq, iq + n + 1, false);
fill(cs, cs + n + 1, 0);
queue<int> q;
ds[s] = 0;
q.push(s);
iq[s] = true;
while (!q.empty()) {
int u = q.front();
q.pop();
iq[u] = false;
for (auto [v, w] : wg[u]) {
if (ds[v] > ds[u] + w) {
ds[v] = ds[u] + w;
cs[v] = cs[u] + 1;
if (cs[v] >= n) return false;
if (!iq[v]) {
q.push(v);
iq[v] = true;
}
}
}
}
return true;
}
2.8 Floyd 全源最短路
C++
const int FN = 505;
int d[FN][FN];
void init_floyd(int n) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
d[i][j] = (i == j) ? 0 : I;
}
void floyd(int n) {
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++) {
if (d[i][k] == I) continue;
for (int j = 1; j <= n; j++) {
if (d[k][j] == I) continue;
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
2.9 Johnson 全源最短路
C++
vector<vector<int>> johnson() {
vector<int> h(n + 1, 0), ct(n + 1, 0);
vector<bool> iq(n + 1, false);
queue<int> q;
for (int i = 1; i <= n; i++) {
q.push(i);
iq[i] = true;
}
while (!q.empty()) {
int u = q.front();
q.pop();
iq[u] = false;
for (auto [v, w] : wg[u]) {
if (h[v] > h[u] + w) {
h[v] = h[u] + w;
ct[v] = ct[u] + 1;
if (ct[v] >= n) return {};
if (!iq[v]) {
q.push(v);
iq[v] = true;
}
}
}
}
vector<vector<pair<int, int>>> ng(n + 1);
for (int u = 1; u <= n; u++)
for (auto [v, w] : wg[u])
ng[u].push_back({v, w + h[u] - h[v]});
vector<vector<int>> rs(n + 1, vector<int>(n + 1, I));
swap(wg, ng);
for (int s = 1; s <= n; s++) {
dijkstra(s);
for (int i = 1; i <= n; i++)
if (ds[i] < I) rs[s][i] = ds[i] - h[s] + h[i];
}
swap(wg, ng);
return rs;
}
2.10 Trie
C++
struct Trie {
static const int ALPHA = 26;
vector<array<int, ALPHA>> nxt;
vector<int> pc, ec;
Trie(int rn = 0) { init(rn); }
void init(int rn = 0) {
nxt.clear();
pc.clear();
ec.clear();
nxt.reserve(rn + 1);
pc.reserve(rn + 1);
ec.reserve(rn + 1);
nxt.push_back({});
pc.push_back(0);
ec.push_back(0);
}
int new_node() {
nxt.push_back({});
pc.push_back(0);
ec.push_back(0);
return (int)nxt.size() - 1;
}
static int id(char ch) {
return ch - 'a';
}
void insert(const string& s) {
int u = 0;
pc[u]++;
for (char ch : s) {
int c = id(ch);
if (!nxt[u][c]) nxt[u][c] = new_node();
u = nxt[u][c];
pc[u]++;
}
ec[u]++;
}
int count(const string& s) const {
int u = 0;
for (char ch : s) {
int c = id(ch);
if (!nxt[u][c]) return 0;
u = nxt[u][c];
}
return ec[u];
}
int count_prefix(const string& s) const {
int u = 0;
for (char ch : s) {
int c = id(ch);
if (!nxt[u][c]) return 0;
u = nxt[u][c];
}
return pc[u];
}
void erase(const string& s) {
int u = 0;
pc[u]--;
for (char ch : s) {
int c = id(ch);
u = nxt[u][c];
pc[u]--;
}
ec[u]--;
}
int size() const {
return (int)nxt.size();
}
};
2.11 网络最大流
C++
const int NV = 250;
ll graph[NV][NV], pre[NV], flow[NV];
ll bfs_flow(int s, int t) {
memset(pre, -1, sizeof(pre));
flow[s] = F;
pre[s] = 0;
queue<int> q;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
if (u == t) break;
for (int v = 1; v <= n; v++) {
if (v != s && graph[u][v] > 0 && pre[v] == -1) {
pre[v] = u;
q.push(v);
flow[v] = min(flow[u], graph[u][v]);
}
}
}
if (pre[t] == -1) return -1;
return flow[t];
}
ll maxflow(int s, int t) {
ll mf = 0;
while (1) {
ll f = bfs_flow(s, t);
if (f == -1) break;
int cur = t;
while (cur != s) {
int fa = pre[cur];
graph[fa][cur] -= f;
graph[cur][fa] += f;
cur = fa;
}
mf += f;
}
return mf;
}
3. 树
3.1 倍增法 LCA
C++
vector<int> aj[M];
int dep[M];
int up[M][G];
int lca(int a, int b) {
if (dep[a] < dep[b]) swap(a, b);
int df = dep[a] - dep[b];
for (int j = 0; j < G; ++j)
if (df & (1 << j)) a = up[a][j];
if (a == b) return a;
for (int j = G - 1; j >= 0; --j)
if (up[a][j] != up[b][j]) {
a = up[a][j];
b = up[b][j];
}
return up[a][0];
}
void init_lca(int S) {
queue<int> q;
q.push(S);
dep[S] = 0;
for (int j = 0; j < G; ++j) up[S][j] = S;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : aj[u]) {
if (v == up[u][0]) continue;
dep[v] = dep[u] + 1;
up[v][0] = u;
for (int j = 1; j < G; ++j)
up[v][j] = up[up[v][j - 1]][j - 1];
q.push(v);
}
}
}
4. 区间查询
4.1 ST 表 RMQ
C++
int st[M][G];
int lg[M];
void init_st(int a[], int n) {
lg[1] = 0;
for (int i = 2; i <= n; i++) lg[i] = lg[i / 2] + 1;
for (int i = 1; i <= n; i++) st[i][0] = a[i];
for (int k = 1; k < G; k++)
for (int i = 1; i + (1 << k) - 1 <= n; i++)
st[i][k] = max(st[i][k - 1], st[i + (1 << (k - 1))][k - 1]);
// 查 min 把 max 改成 min
}
int query_rmq(int L, int R) {
int k = lg[R - L + 1];
return max(st[L][k], st[R - (1 << k) + 1][k]);
}
4.2 线段树
C++
int a[M];
int tr[4 * M];
int lz[4 * M];
void build(int nd, int l, int r) {
if (l == r) {
tr[nd] = a[l];
return;
}
int m = (l + r) >> 1;
build(nd << 1, l, m);
build(nd << 1 | 1, m + 1, r);
tr[nd] = tr[nd << 1] + tr[nd << 1 | 1];
}
void push_down(int nd, int l, int r) {
if (lz[nd] == 0) return;
int m = (l + r) >> 1;
int lc = nd << 1, rc = nd << 1 | 1;
lz[lc] += lz[nd];
tr[lc] += lz[nd] * (m - l + 1);
lz[rc] += lz[nd];
tr[rc] += lz[nd] * (r - m);
lz[nd] = 0;
}
void range_add(int nd, int l, int r, int L, int R, int v) {
if (L <= l && r <= R) {
tr[nd] += v * (r - l + 1);
lz[nd] += v;
return;
}
push_down(nd, l, r);
int m = (l + r) >> 1;
if (L <= m) range_add(nd << 1, l, m, L, R, v);
if (R > m) range_add(nd << 1 | 1, m + 1, r, L, R, v);
tr[nd] = tr[nd << 1] + tr[nd << 1 | 1];
}
int query_sum(int nd, int l, int r, int L, int R) {
if (L <= l && r <= R) return tr[nd];
push_down(nd, l, r);
int m = (l + r) >> 1;
int ans = 0;
if (L <= m) ans += query_sum(nd << 1, l, m, L, R);
if (R > m) ans += query_sum(nd << 1 | 1, m + 1, r, L, R);
return ans;
}
4.3 单调队列 / 滑动窗口
C++
int a[M], q[M];
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
int hd = 1, tl = 0;
for (int i = 1; i <= n; i++) {
while (hd <= tl && q[hd] < i - k + 1) hd++;
while (hd <= tl && a[q[tl]] >= a[i]) tl--;
q[++tl] = i;
if (i >= k) printf("%d ", a[q[hd]]);
}
printf("\n");
hd = 1;
tl = 0;
for (int i = 1; i <= n; i++) {
while (hd <= tl && q[hd] < i - k + 1) hd++;
while (hd <= tl && a[q[tl]] <= a[i]) tl--;
q[++tl] = i;
if (i >= k) printf("%d ", a[q[hd]]);
}
}
4.4 单调栈
C++
int a[M];
int as[M];
int sk[M], tp;
void ngr(int n) {
tp = 0;
for (int i = n; i >= 1; i--) {
while (tp > 0 && a[sk[tp]] <= a[i]) tp--;
as[i] = (tp > 0) ? sk[tp] : 0;
sk[++tp] = i;
}
}
void nsr(int n) {
tp = 0;
for (int i = n; i >= 1; i--) {
while (tp > 0 && a[sk[tp]] >= a[i]) tp--;
as[i] = (tp > 0) ? sk[tp] : 0;
sk[++tp] = i;
}
}
void ngl(int n) {
tp = 0;
for (int i = 1; i <= n; i++) {
while (tp > 0 && a[sk[tp]] <= a[i]) tp--;
as[i] = (tp > 0) ? sk[tp] : 0;
sk[++tp] = i;
}
}
void nsl(int n) {
tp = 0;
for (int i = 1; i <= n; i++) {
while (tp > 0 && a[sk[tp]] >= a[i]) tp--;
as[i] = (tp > 0) ? sk[tp] : 0;
sk[++tp] = i;
}
}
5. 序列和排序
5.1 归并排序求逆序对
C++
ll merge(vector<int>& a, vector<int>& tp, int l, int m, int r) {
int i = l, j = m + 1, k = l;
ll c = 0;
while (i <= m && j <= r) {
if (a[i] <= a[j]) {
tp[k++] = a[i++];
} else {
c += m - i + 1;
tp[k++] = a[j++];
}
}
while (i <= m) tp[k++] = a[i++];
while (j <= r) tp[k++] = a[j++];
for (int p = l; p <= r; p++) a[p] = tp[p];
return c;
}
ll mergeSort(vector<int>& a, vector<int>& tp, int l, int r) {
if (l >= r) return 0;
int m = l + (r - l) / 2;
ll c = mergeSort(a, tp, l, m) + mergeSort(a, tp, m + 1, r);
c += merge(a, tp, l, m, r);
return c;
}
5.2 贪心版 LIS
C++
vector<int> tl;
for (int x : a) {
auto it = lower_bound(tl.begin(), tl.end(), x);
if (it == tl.end()) tl.push_back(x);
else *it = x;
}
cout << tl.size() << '\n';
5.3 离散化
C++
int n; scanf("%d", &n);
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<int> b = a;
sort(b.begin(), b.end());
b.erase(unique(b.begin(), b.end()), b.end());
for (int i = 0; i < n; i++) {
int r = lower_bound(b.begin(), b.end(), a[i]) - b.begin() + 1;
cout << r << (i == n - 1 ? '\n' : ' ');
}
6. 卡常时的救赎
6.1 int 快读
C++
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
if (c == EOF) return 0;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + (c - '0');
c = getchar();
}
return x * f;
}
6.2 int 快写
C++
inline void write(int x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
7. 数论
7.1 快速幂
C++
ull fastpow(ull a, ull b, ull p) {
ull rs = 1;
a %= p;
while (b) {
if (b & 1) rs = __int128(rs * a) % p;
a = __int128(a * a) % p;
b >>= 1;
}
return rs;
}
7.2 GCD & LCM
C++
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
int lcm(int a, int b) {
return a / gcd(a, b) * b;
}
7.3 CRT
C++
void extend_gcd(ll a, int b, int& x, int& y) {
if (b == 0) { x = 1, y = 0; return; }
extend_gcd(b, a % b, x, y);
int tmp = x;
x = y;
y = tmp - (a / b) * y;
}
int CRT(int a[], int b[], int n) {
int md = 1;
for (int i = 1; i <= n; i++) md *= a[i];
ll ans = 0;
for (int i = 1; i <= n; i++) {
ll Mi = md / a[i];
int x, y;
extend_gcd(Mi, a[i], x, y);
int inv = (x % a[i] + a[i]) % a[i];
ans = (ans + (__int128)b[i] * Mi * inv) % md;
}
return ans;
}
7.4 Lucas & C(n,m)
C++
vector<ll> fact, invfact;
void init_lucas(int p) {
fact.resize(p);
invfact.resize(p);
fact[0] = 1;
for (int i = 1; i < p; ++i) {
fact[i] = fact[i - 1] * i % p;
}
invfact[p - 1] = fastpow(fact[p - 1], p - 2, p);
for (int i = p - 1; i >= 1; --i) {
invfact[i - 1] = invfact[i] * i % p;
}
}
ll C(int n, int m, int p) {
if (m < 0 || m > n) return 0;
return fact[n] * invfact[m] % p * invfact[n - m] % p;
}
ll Lucas(ll n, ll m, int p) {
ll res = 1;
while (m > 0) {
int ni = n % p;
int mi = m % p;
if (mi > ni) return 0;
res = res * C(ni, mi, p) % p;
n /= p;
m /= p;
}
return res;
}
7.5 线性基
C++
const int MB = 63;
ll p[MB];
bool zero;
void Insert(ll x) {
for (int i = MB; i >= 0; i--) if (x >> i == 1) if (p[i] == 0) {p[i] = x; return;} else x ^= p[i];
zero = 1;
}
bool check(ll x) {
for (int i = MB; i >= 0; i--) {
if (!((x >> i) & 1)) continue;
if (!p[i]) return false;
x ^= p[i];
}
return true;
}
ll qmax() {
ll ans = 0;
for (int i = MB; i >= 0; i--) ans = max(ans, ans ^ p[i]);
return ans;
}
ll qmin() {
if (zero) return 0;
for (int i = 0; i <= MB; i++)
if (p[i]) return p[i];
return 0;
}
ll b[MB + 1];
int tot = 0;
void init_kth() {
for (int i = MB; i >= 0; i--)
for (int j = i - 1; j >= 0; j--)
if (p[i] && ((p[i] >> j) & 1))
p[i] ^= p[j];
tot = 0;
for (int i = 0; i <= MB; i++)
if (p[i]) b[tot++] = p[i];
}
ll kth(ll k) {
if (zero) {
if (k == 1) return 0;
k--;
}
if (k >= (1LL << tot)) return -1;
ll res = 0;
for (int i = 0; i < tot; i++)
if ((k >> i) & 1) res ^= b[i];
return res;
}
7.6 杜教筛
C++
const int SN = 5e6+5;
int prime[N], mu[N];
bool vis[N];
ll phi[N];
unordered_map<int, int> summu;
unordered_map<int, ll> sumphi;
void init() {
int cnt = 0;
vis[0] = vis[1] = 1;
phi[1] = mu[1] = 1;
for (int i = 2; i < N; i++) {
if (!vis[i]) {
prime[cnt++] = i;
mu[i] = -1;
phi[i] = i - 1;
}
for (int j = 0; j < cnt && i * prime[j] < N; j++) {
vis[i * prime[j]] = 1;
if (i % prime[j]) mu[i * prime[j]] = -mu[i], phi[i * prime[j]]= phi[i] *phi[prime[j]];
else {
mu[i * prime[j]] = 0, phi[i * prime[j]] = phi[i] * prime[j];
break;
}
}
}
for (int i = 1; i < N; i++) mu[i] += mu[i-1], phi[i] += phi[i-1];
}
ll getsmu(int x) {
if (x < N) return mu[x];
if (summu[x]) return summu[x];
ll ans = 1;
for (ll l = 2, r; l <= x; l = r + 1) {
r = x / (x / l);
ans -= (r - (l - 1)) * getsmu(x / l);
}
return summu[x] = ans;
}
ll getsphi(int x) {
if (x < N) return phi[x];
if (sumphi[x]) return sumphi[x];
ll ans = x * ((ll) x + 1) / 2;
for (ll l = 2, r; l <= x; l = r + 1) {
r = x / (x / l);
ans -= (r - (l - 1)) * getsphi(x / l);
}
return sumphi[x] = ans;
}
7.7 Pollard-Rho
C++
mt19937 gen(random_device{}());
ll max_fac;
ll mul(ll a, ll b, ll p) {return (__int128)a * b % p;}
bool witness(ll a, ll n) {
ll u = n - 1;
int t = 0;
while ((u & 1) == 0) u >>= 1, t++;
ll x1 = fastpow(a, u, n);
if (x1 == 1 || x1 == n - 1) return 0;
for (int i = 1; i < t; i++) {
ll x2 = mul(x1, x1, n);
if (x2 == n - 1) return 0;
if (x2 == 1) return 1;
x1 = x2;
}
return 1;
}
bool miller_rabin(ll n) {
if (n < 2) return 0;
if (n == 2 || n == 3) return 1;
if (n % 2 == 0) return 0;
for (ll a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
if (a >= n) break;
if (witness(a, n)) return 0;
}
return 1;
}
ll pollard_rho(ll n) {
if (n % 2 == 0) return 2;
uniform_int_distribution<ll> dist(1, n - 1);
ll c = dist(gen);
ll x = dist(gen) + 1, y = x, d;
ll i = 1, k = 2;
while (true) {
i++;
x = (mul(x, x, n) + c) % n;
d = gcd(y > x ? y - x : x - y, n);
if (d != 1 && d != n) return d;
if (y == x) return n;
if (i == k) y = x, k <<= 1;
}
}
void findfac(ll n) {
if (n == 1) return;
if (miller_rabin(n)) {
max_fac = max(max_fac, n);
return;
}
ll d = n;
while (d >= n) d = pollard_rho(n);
findfac(d);
findfac(n / d);
}
7.8 Miller-Rabin
C++
bool miller_rabin(ll n) {
if (n < 2) return false;
if (n == 2 || n == 3) return true;
if (n % 2 == 0) return false;
for (ll a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
if (a >= n) break;
if (witness(a, n)) return false;
}
return true;
}
7.9 exCRT
C++
int n;
ll ai[N], mi[N];
ll mul(ll a, ll b, ll m) {
ll res = 0;
a %= m; b %= m;
while (b > 0) {
if (b&1) res = (res + a) % m;
a = (a + a) % m;
b >>= 1;
}
return res;
}
ll exgcd(ll a, ll b, ll& x, ll& y) {
if (b == 0) {x = 1, y = 0; return a;}
ll d = exgcd(b, a%b, y, x);
y -= a / b * x;
return d;
}
ll excrt() {
ll x, y;
ll m1 = mi[1], a1 = ai[1];
ll ans = (a1 % m1 + m1) % m1;
for (int i = 2; i <= n; i++) {
ll a2 = ai[i], m2 = mi[i];
ll a = m1, b = m2, c = (a2 - a1 % m2 + m2) % m2;
ll d = exgcd(a, b, x, y);
if (c % d != 0) return -1;
x = mul(x, c / d, b/d);
ans=a1+x*m1;
m1=m2/d*m1;
ans=(ans%m1+m1)%m1;
a1=ans;
}
return ans;
}
8. 计算几何
8.1 三维凸包
C++
const int MAXN = 2010;
const double eps = 1e-8;
struct Point3 {
double x, y, z;
Point3(double x_ = 0, double y_ = 0, double z_ = 0) : x(x_), y(y_), z(z_) {}
Point3 operator + (Point3 B) {return Point3(x + B.x, y + B.y, z + B.z);}
Point3 operator - (Point3 B) {return Point3(x - B.x, y - B.y, z - B.z);}
Point3 operator * (double k) {return Point3(x * k, y * k, z * k);}
Point3 operator / (double k) {return Point3(x / k, y / k, z / k);}
};
typedef Point3 Vector3;
double Dot(Vector3 A, Vector3 B) {return A.x * B.x + A.y * B.y + A.z * B.z;}
Point3 Cross(Vector3 A, Vector3 B) {
return Point3(A.y * B.z - A.z * B.y, A.z * B.x - A.x * B.z, A.x * B.y - A.y * B.x);
}
double Len(Vector3 A) {return sqrt(Dot(A, A));}
double Area2(Point3 A, Point3 B, Point3 C) {return Len(Cross(B - A, C - A));}
double volume4(Point3 A, Point3 B, Point3 C, Point3 D) {
return Dot(Cross(B - A, C - A), D - A);
}
struct CH3D {
struct face {
int a,b,c;
bool ok;
};
int n;
Point3 P[MAXN];
int num;
face F[8 * MAXN];
int g[MAXN][MAXN];
double dblcmp(Point3& p, face& f) {
Point3 m = P[f.b]- P[f.a];
Point3 n = P[f.c] - P[f.a];
Point3 t = p - P[f.a];
return Dot(Cross(m, n), t);
}
void deal(int p, int a, int b) {
int f = g[a][b];
face add;
if (F[f].ok) {
if (dblcmp(P[p], F[f]) > eps)
dfs(p, f);
else {
add.a = b;
add.b = a;
add.c = p;
add.ok = true;
g[p][b] = g[a][p] = g[b][a] = num;
F[num++] = add;
}
}
}
void dfs(int p, int now) {
F[now].ok = 0;
deal(p, F[now].b, F[now].a);
deal(p, F[now].c, F[now].b);
deal(p, F[now].a, F[now].c);
}
bool same(int s, int t) {
Point3& a = P[F[s].a];
Point3& b = P[F[s].b];
Point3& c = P[F[s].c];
return fabs(volume4(a, b, c, P[F[t].a])) < eps &&fabs(volume4(a, b, c, P[F[t].b])) < eps &&fabs(volume4(a, b, c, P[F[t].c])) < eps;
}
void create() {
memset(g, 0, sizeof(g));
int i, j, tmp;
face add;
num = 0;
if (n < 4) return;
bool flag = 1;
for (i = 1; i < n; i++) {
if (Len(P[0] - P[i]) > eps) {
swap(P[1], P[i]);
flag = 0;
break;
}
}
if (flag) return;
flag = 1;
for (i = 2; i < n; i++) {
if (Len(Cross(P[0] - P[1], P[1] - P[i])) > eps) {
swap(P[2], P[i]);
flag = 0;
break;
}
}
if (flag) return;
flag = 0;
for (i = 3; i < n; i++) {
if (fabs(Dot(Cross(P[0] - P[1], P[1] - P[2]), P[0] - P[i])) > eps) {
swap(P[3], P[i]);
flag = 0;
break;
}
}
if (flag) return;
for (i = 0; i < 4; i++) {
add.a = (i + 1) % 4;
add.b = (i + 2) % 4;
add.c = (i + 3) % 4;
add.ok = 1;
if (dblcmp(P[i], add) > 0) swap(add.b, add.c);
g[add.a][add.b] = g[add.b][add.c] = g[add.c][add.a] = num;
F[num++] = add;
}
for (i = 4; i < n; i++) {
for (j = 0; j < num; j ++) {
if (F[j].ok && dblcmp(P[i], F[j]) > eps) {
dfs(i, j);
break;
}
}
}
tmp = num;
for (i = num = 0; i < tmp; i++)
if (F[i].ok)
F[num++] = F[i];
}
double area() {
double res = 0;
for (int i = 0; i < num; i++)
res += Area2(P[F[i].a], P[F[i].b], P[F[i].c]);
return res / 2.0;
}
double volume() {
double res = 0;
for (int i = 0; i < num; i++)
res += volume4(Point3(0, 0, 0), P[F[i].a], P[F[i].b], P[F[i].c]);
return fabs(res) / 6.0;
}
};