跳到正文

个人记录

板子

General0826约 18 分钟
原文
文章信息与版本

文章信息

发布
2026年8月3日
收录
2026年8月4日
更新
2026年11月8日
篇幅
约 18 分钟

版本

  1. v3当前
  2. v2
  3. v1

因为码风的修改是时候要有个收集板子的地方了。

欧拉函数

C++
int phi[N],prime[N],tot,pri[N]; 
void init (int n) {
	phi[1] = 1;
	for (int i = 2; i <= n; i++) {
		if (prime[i] == 0) {
			pri[++tot] = i;
			phi[i] = i - 1;
		}
		for (int j = 1; j <= tot && i * pri[j] <= n; j++) {
			prime[i*pri[j]] = 1;
			phi[i*pri[j]] = phi[i] * phi[pri[j]];
			if (i % pri[j] == 0) {
				phi[i*pri[j]] = phi[i] * pri[j];
				break; 
			}  
		}
	}
}

或 搭配线性筛

C++
int phi(int x){
	int ans = x;
	for (int i = 1; i <= tot; i++) 
		if (x % prime[i] == 0)
			ans = ans / v[i] * (v[i] - 1);
	return ans;
}

线性筛

C++
void init (int n) {
	for (int i = 2; i <= n; i++) {
		if (pri[i] == 0) 
			prime[++tot] = i;
		for (int j = 1; j <= tot && i * prime[j] <= n; j++) {
			pri[i*prime[j]] = 1;
			if (i % prime[j] == 0) 
				break; 
		}
	}
}

exgcd

C++
#include<bits/stdc++.h>
#define int long long
#define db double
using namespace std;
const int N=1e5+5;
int a,b,c,x,y;
int excit (int a, int b, int &x, int &y) {
	if (b == 0) {
		x=1;
		y=0;
		return a;
	}
	int gc = excit(b, a % b, x, y), t=x;
	x = y, y = t - (a / b) * y;
	return gc;  
}
void work () {
	cin >> a >> b >> c;
	int gc = excit(a, b, x, y);
	if (c % gc != 0) {
		cout << -1 << '\n';
		return ;
	}
	x = c / gc * x, y = c / gc * y;
	int dx = b / gc, dy = a / gc; 
	if (x < 0) {
		int k = ceil((1.0 - x) / dx);
		x += dx * k, y -= dy * k;
	}
	else {
		int k = (x - 1) / dx;
		x -= dx * k, y += dy * k;
	}
	if (y > 0)
		cout << (y - 1) / dy + 1 << ' ' << x << ' ' << (y - 1) % dy + 1 << ' ' << x + (y - 1) / dy * dx << ' ' << y << '\n';
	else
		cout << x << ' ' << y + dy * (int)ceil((1.0 - y) / dy) << '\n';
	return ;
}signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int T;
	cin >> T;
	while (T--)
		work();
	return 0;
}

离散化

C++
int Q (int x) {
	return lower_bound(ls + 1, ls + tot + 1, x) - ls;
}
void lsh () {
	sort(ls + 1, ls + 1 + tot);
	tot = unique(ls + 1, ls + tot + 1) - ls -1;
}

组合数

C++
int jc[N],inv[N];
int Pow(int a, int b = mod - 2) {
	int x = 1;
	while (b) {
		if (b & 1) x = x * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return x;
}
void init(int n) {
	inv[0] = jc[0] = 1;
	for (int i = 1; i <= n; i++) jc[i] = jc[i-1] * i % mod;
	int num = Pow(jc[n]);
	for (int i = n; i >= 1; i--) 
		inv[i] = num * jc[i-1] % mod, (num *= i) %= mod;
	for (int i = 1; i <= n; i++) (inv[i] *= inv[i-1]) %= mod;
}
int C(int n, int m) {
	if (n < m) return 0;
	return jc[n] * inv[m] % mod * inv[n-m] % mod;
}

虚树

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5; 
int dfn[N],dep[N],anc[N][21],Time;
int stk[N],tp,x,y,T,m,n,q;
vector<int> d[N],c[N],k;
void link(int x, int y) {
	d[x].push_back(y);
}
bool cmp(int x, int y){
	return dfn[x] < dfn[y];
}
void dfs (int u, int fa) {
	dfn[u] = ++Time, dep[u] = dep[fa] + 1, anc[u][0] = fa;
	for (int i = 1; (1 << i) <= dep[u]; i++) 
		anc[u][i] = anc[anc[u][i-1]][i-1];
	for (auto v:c[u]) if (v != fa)
		dfs(v, u);
}
int lca (int x,int y){
	if (dep[x] < dep[y]) swap(x,y);
	for (int i = 20; i >= 0; i--)
		if (dep[anc[x][i]] >= dep[y])
			x = anc[x][i];
	if (x == y) return x;	
	for (int i = 20; i >= 0; i--)
		if (anc[x][i] != anc[y][i])
			x = anc[x][i], y = anc[y][i];
	return anc[x][0];
}

void work(){
	tp = 0;
	sort(k.begin(), k.end(), cmp);
	stk[++tp] = 1;
	for (auto v:k) {
		int lc = lca(stk[tp], v);
		if(lc != stk[tp]) {
			while(dep[lc] < dep[stk[tp-1]]) {
				link(stk[tp-1], stk[tp]);
				tp--;
			}
			link(lc, stk[tp]);
			tp--; 
			if(stk[tp] != lc)
				stk[++tp] = lc;	
		}
		stk[++tp] = v;
	}
	while (tp > 1) {
		link(stk[tp-1], stk[tp]);
		tp--;
	}
}
signed main(){
	cin >> n;
	for(int i = 1; i < n; i++) {
		cin >> x >> y;
		c[x].push_back(y);
		c[y].push_back(x);
	}
	dfs(1, 0);
	cin >> q;
	while (q--) {
		cin >> m;
		for (int i = 1; i <= m; i++) {
			cin >> x;
			k.push_back(x);
		}
		work();
	}
	return 0;
} 

最短路

C++
struct node {
	int x,w;
	bool operator < (const node a) const {
		return w > a.w;
	}
};
int dis[N],vis[N],n,m,x,y,w,s; 
vector<node> c[N];
priority_queue<node> q;
void Dij (int s) {
	memset(dis, 0x3f, sizeof(dis));
	dis[s] = 0;
	q.push({1, 0});
	while (q.size()) {
		int u = q.top().x;
		q.pop();
		if (vis[u]) continue;
		vis[u] = 1;
		for (auto i:c[u]) {
			int v = i.x, w = i.w;
			if (dis[v] > dis[u] + w) {
				dis[v] = dis[u] + w;
				q.push({v, dis[v]});	
			}
		}
	}
}

随机排列

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
mt19937 rnd(time(0));
int n,a[N];
void rdp (int a[], int n) {
	for (int i = 1; i <= n; i++) a[i] = i;
	shuffle(a + 1,  a + 1 + n, rnd);
} 
signed main () {
	return 0;
}

ST表

C++
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+5;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
int n,m,st[20][N],a[N],l,r;
int get (int l, int r) {
	int k = log2(r - l + 1);
	return max(st[k][l], st[k][r-(1<<k)+1]);
}
int main(){
	n = read(); m = read();
	for (int i = 1; i <= n; i++) {
		a[i] = read();
		st[0][i] = a[i];
	}
	for (int i = 1; (1 << i) <= n; i++)
		for(int j = 1; j + (1 << i) - 1 <= n; j++)
			st[i][j] = max(st[i-1][j], st[i-1][j+(1<<i-1)]);
	for (int i = 1; i <= m; i++) {
		l = read(), r = read();
		cout << get(l, r) << '\n';
	}
	return 0;
}

点分治

C++
#include<bits/stdc++.h>
#define int long long 
using namespace std;
const int N = 1e6 + 5, inf = 1e18;
bool ok[N];
int to[N],wei[N],head[N],nxt[N],tot;
int zon[N],siz[N],root,dxn,dp[N];
int ans = inf,m,w,n,x,y,k;
struct node {
	int x,y;
};
vector<node> st,stk; 
void add (int x, int y, int w){
	to[++tot] = y, wei[tot] = w;
	nxt[tot] = head[x], head[x] = tot;
}
void get_weight (int u, int fa){
	siz[u] = 1, zon[u] = 0;
	for(int i = head[u]; i; i = nxt[i]) {
		int v = to[i];
		if (v == fa || ok[v]) continue;
		get_weight(v, u);
		siz[u] += siz[v];
		zon[u] = max(zon[u], siz[v]);
	}
	zon[u] = max(zon[u], dxn - siz[u]);
	if(zon[u] < zon[root])
		root = u;
}
void dfs(int u,int fa){
	siz[u] = 1;
	for (int i = head[u]; i; i = nxt[i]) {
		int v = to[i];
		if (v == fa || ok[v]) continue;
		dfs(v, u);
		siz[u] += siz[v];
	}
}
void get (int u, int fa, int dep, int dis) {
	if (dis <= k) stk.push_back({dis, dep});
	for (int i = head[u]; i; i = nxt[i]) {
		int v = to[i], w = wei[i];
		if(v == fa || ok[v]) continue;
		get(v, u, dep + 1, dis + w);
	}
}
void cale(int u){
	root = 0;
	get_weight(u, 0);
	ok[u = root] = 1;
	dfs(u, 0);
	dp[0] = 0;
	st.clear();
	for (int i = head[u]; i; i = nxt[i]) {
		int v = to[i], w = wei[i];
		if (ok[v]) continue;
		stk.clear();
		get(v, u, 1, w);
		for (auto v:stk) 
			ans = min(ans, v.y + dp[k - v.x]);
		for (auto v:stk) {
			dp[v.x] = min(dp[v.x], v.y);
			st.push_back(v);
		}
	}
	for (auto v:st) dp[v.x] = inf;
		
	for (int i = head[u]; i; i = nxt[i]){
		int v = to[i];
		if (ok[v]) continue;
		dxn = siz[v];
		cale(v);
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> k;
	if (k == 0) {
		cout << 0 ;
		return 0;
	} 
	for (int i = 1; i < n; i++) {
		cin >> x >> y >> w;
		x++, y++;
		add(x, y, w);
		add(y, x, w);
	}
	zon[0] = 114514;
	dxn = n;
	for (int i = 1; i <= k; i++) dp[i] = inf;
	cale(1);
	cout << (ans == inf? -1: ans);
	return 0;
}
C++
#include<bits/stdc++.h>
#define mid (L + R >> 1) 
#define int long long
using namespace std;
const int N = 2e5 + 5;
struct node{
	int a,b,c,id,w;
}c[N];
int n,ans[N],t[N],k;
bool cmp1 (node a, node b) {
	if (a.a == b.a) {        // a b c
		if (a.b == b.b)
			return a.c < b.c;
		return a.b < b.b;
	}
	return a.a < b.a;
}
bool cmp2 (node a, node b) {  // b c
	if (a.b == b.b)
		return a.c < b.c;
	return a.b < b.b;
}
namespace Bit {
	int t[N];
	int res (int x) {
		int ans = 0;
		for(;x > 0; x -= x & -x) 
			ans += t[x]; 
		return ans;
	}
	void add (int x, int w) {
		for (; x <= k; x += x & -x) 
			t[x] += w;
	}	
}
void CDQ (int L, int R){
	if (L == R) return ;
	int p = L;
	CDQ(L, mid), CDQ(mid+1,R);
	sort(c + L, c + mid + 1, cmp2);
	sort(c + mid + 1, c + R + 1, cmp2) ;
	for (int i = mid + 1; i <= R; i++) {
		while (p <= mid && c[p].b <= c[i].b) 
			Bit::add(c[p].c, c[p].w), p++;
		ans[c[i].id] += Bit::res(c[i].c);
	}
	for (int i = L; i < p; i++)
		Bit::add(c[i].c, -c[i].w); 
}
signed main () {
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> c[i].a >> c[i].b >> c[i].c;
		c[i].id = i;
	}
	sort(c + 1, c + 1 + n, cmp1);
	int tot = 0;
	for (int i = 1; i <= n; i++) {
		if (c[i].a != c[i-1].a || c[i].c != c[i-1].c || c[i].b != c[i-1].b)
			c[++tot] = c[i];
		c[tot].w++;
	}
	CDQ(1, tot);
	for (int i = 1; i <= tot; i++)
		t[ans[c[i].id] + c[i].w - 1] += c[i].w;
	for (int i = 0; i < n; i++)
		cout << t[i] << '\n';
	return 0;
}

## CDQ
原文