跳到正文

算法·理论

一些模版

yanyu289约 62 分钟
原文

这是 2025年10月28日 存档的第 7 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年10月28日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 62 分钟
获赞
1

版本

  1. v10当前
  2. v9
  3. v8
  4. v7
更早的 6 个版本
  1. v6
  2. v5
  3. v4
  4. v3
  5. v2
  6. v1

各种模版:

一些模版题

一:最短路:

1:全源最短路:

(1):Floyed:
C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 1005 + 55, mod = 998244353, inf = 2147483647;
const double pi = acos(-1);
int T;
int n, m;
int e[maxn][maxn], dis[maxn][maxn];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin >> n >> m;
	for(int i = 1;i <= n;i ++){
		for(int j = 1;j <= n;j ++){
			e[i][j] = inf;
		}
		e[i][i] = 0;
	}
	for(int i = 1, u, v, w;i <= m;i ++){
		cin >> u >> v >> w;
		e[u][v] = e[v][u] = min(e[u][v], w);
		//注意重边问题 
	}
	for(int k = 1;k <= n;k ++){
		for(int i = 1;i <= n;i ++){
			for(int j = 1;j <= n;j ++){
				if(i == j ||j == k || i == k) continue;
				e[i][j] = min(e[i][j], e[i][k] + e[k][j]);
			}
		}
	}for(int i = 1;i <= n;i ++){
		for(int j = 1;j <= n;j ++){
			cout << e[i][j] << " ";
		}cout << endl;
	}
	from yanyu289;
}
(2)Johnson(改造边权,使等效为非负权图,本质为n遍dijk)
C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 3e3 + 55, mod = 998244353, inf = 1e9;
const double pi = acos(-1);
int T;
int n, m, s, dis[maxn], h[maxn], ans;//h:势能数组 
bool vis[maxn];
struct edge{
    int to, w;
    edge(int t, int wi) : to(t), w(wi) {}
    //初始化列表赋值,后续可 e[u].push_back(edge(v, w))
    //构造函数 
    bool operator<(const edge& other) const {
        return w > other.w;
    }
};
vector<edge> e[maxn];
void add(int u, int v, int w){
    e[u].emplace_back(v, w);
}
void dijk(int s){
    fill(dis, dis + n + 1, inf);
    fill(vis, vis + n + 1, 0);
    priority_queue<edge> q;
    q.emplace(s, 0);
    dis[s] = 0;
    
    while(!q.empty()){
        edge now = q.top();
        q.pop();
        int u = now.to;
        if(vis[u]) continue;
        vis[u] = true;
        for(auto v : e[u]){
            if(dis[v.to] > dis[u] + v.w){
                dis[v.to] = dis[u] + v.w;
                q.emplace(v.to, dis[v.to]);
            }
        }
    }
}
bool belman(){
    fill(h, h + n + 1, inf);
    h[0] = 0;
    bool flag;
    
    for(int i = 0; i <= n;i ++){
        flag = false;
        for(int u = 0; u <= n;u ++){
            if(h[u] == inf) continue;
            for(auto v : e[u]) {
                if(h[v.to] > h[u] + v.w){
                    h[v.to] = h[u] + v.w;
                    flag = 1;
                }
            }
        }
        if(!flag) break;
    }
    for(int u = 0; u <= n;u ++){
        if(h[u] == inf) continue;
        for(auto &v : e[u]) {
            if(h[v.to] > h[u] + v.w) {
                return true;
            }
        }
    }
    return false;
}
void john(){
    for(int i = 1; i <= n;i ++){
        add(0, i, 0);
    }
    if(belman()){
        cout << -1 << endl;//负环 
        return;
    }
    for(int u = 1; u <= n;u ++){
        for(auto &v : e[u]) {
            v.w += h[u] - h[v.to];
        }
    }
    for(int i = 1; i <= n;i ++){
        dijk(i);
        ans = 0;
        for(int j = 1; j <= n;j ++){
            if(i == j){
                ans += j * 0;
            }else if(dis[j] == inf){
                ans += j * inf;
            }else{
                ans += j * (dis[j] - h[i] + h[j]);
            }
        }
        cout << ans << endl;
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for(int i = 1, u, v, w; i <= m; ++i) {
        cin >> u >> v >> w;
        add(u, v, w);
    }
    john();
    from yanyu289;
}

2:单源最短路

(1)SPFA(可处理负边权,但较慢(最坏达O(nm)))
C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 5e5 + 55, mod = 998244353, inf = 2147483647;
const double pi = acos(-1);
int T;
int n, m, cnt, s, dis[maxn];
bool vis[maxn];
int head[2 * maxn];
struct edge{
	int to, ne, v;
}e[maxn * 2];
void add(int u, int v, int w){
	e[++ cnt].ne = head[u];
	head[u] = cnt;
	e[cnt].to = v;
	e[cnt].v = w;
}
void spfa(int u){
	fill(dis, dis + n + 1, inf);
	dis[u] = 0;
	queue<int> q;
	q.push(u);
	vis[u] = 1;
	while(!q.empty()){
		int now = q.front();
		q.pop();
		vis[now] = 0;
		
//		cnt[now] ++;//判断负环存在 
//		if(cnt[now] > n) return 0;
		
		for(int i = head[now];i;i = e[i].ne){
			int w = e[i].to;
			if(dis[now] + e[i].v < dis[w]){
				dis[w] = dis[now] + e[i].v;
				if(!vis[w]) q.push(w), vis[w] = 1;
			}
		}
	}
	
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin >> n >> m >> s;
	for(int i = 1, u, v, w;i <= m;i ++){
		cin >> u >> v >> w;
		add(u, v, w);
//		add(v, u, w);
	}
	spfa(s);
	for(int i = 1;i <= n;i ++){
		cout << dis[i] << " ";
	}
	from yanyu289;
}
(2)SPFA用处:负环
C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 1e4 + 55, mod = 998244353, inf = 2147483647;
const double pi = acos(-1);
int T;
int n, m, cnt, dis[maxn], res[maxn];
bool vis[maxn];
int head[2 * maxn];
struct edge{
	int to, ne, v;
}e[maxn * 2];
void add(int u, int v, int w){
	e[++ cnt].ne = head[u];
	head[u] = cnt;
	e[cnt].to = v;
	e[cnt].v = w;
}
void init(){
	fill(dis, dis + n + 1, inf);
	memset(vis, 0, sizeof vis);
	memset(res, 0, sizeof res);
	for(int i = 1;i <= n;i ++){
		e[i].ne = e[i].to = e[i].v = 0;
		head[i] = 0;
	}cnt = 0;
}
bool spfa(int u){
	
	dis[u] = 0;
	queue<int> q;
	q.push(u);
	vis[u] = 1;
	
	while(!q.empty()){
		int now = q.front();
		q.pop();
		vis[now] = 0;
		
		res[now] ++;//判断负环存在 
		if(res[now] > n) return 0;
		
		for(int i = head[now];i;i = e[i].ne){
			int w = e[i].to;
			if(dis[now] + e[i].v < dis[w]){
				dis[w] = dis[now] + e[i].v;
				if(!vis[w]) q.push(w), vis[w] = 1;
			}
		}
	}
	return 1;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin >> T;
	while(T --){
		cin >> n >> m;
		init();
		for(int i = 1, u, v, w;i <= m;i ++){
			cin >> u >> v >> w;
			if(w >= 0){
				add(u, v, w);
				add(v, u, w);
			}else{
				add(u, v, w);
			}
		}
		if(!spfa(1)) puts("YES");
		else puts("NO");
	}
	from yanyu289;
}
(3)Dijkstra,不处理负边权,时间复杂度优(O(n + m)log2log_2 n)
C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 2e5 + 55, mod = 998244353, inf = 2147483647;
const double pi = acos(-1);
int T;
int n, m, s, dis[maxn];
bool vis[maxn];
struct edge{
	int to, w;
	bool operator < (const edge &other) const{
		return w > other.w;//最小堆 
	}
};
vector<edge> e[maxn * 2];
priority_queue<edge> q;
void dijk(int s){
	fill(dis, dis + n + 1, inf);
	q.push({s, 0});
	dis[s] = 0;
	while(!q.empty()){
		edge now = q.top();
		q.pop();
		int u = now.to;
		if(vis[u]) continue;
		vis[u] = 1;
		for(auto v : e[u]){
			if(dis[v.to] > dis[u] + v.w){
				dis[v.to] = dis[u] + v.w;
				q.push({v.to, dis[v.to]});
			}
		}
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin >> n >> m >> s;
	for(int i = 1, u, v, w;i <= m;i ++){
		cin >> u >> v >> w;
		e[u].push_back({v, w});
	}
	dijk(s);
	for(int i = 1;i <= n;i ++){
		cout << dis[i] << " ";  
	}
	from yanyu289;
}

二:FFT或高精度乘法

1:递归fft

C++
#include<bits/stdc++.h>//fft-递归版本 
//容易炸,但是好理解 ,空间nlogn,劣于迭代版本
//迭代空间n,两方法时复差不多nlogn,递归略慢 
#define ll long long 
#define from return
#define yanyu289 0
using namespace std;
const int maxn = 1e6 * 4 + 5;
const double pie = acos(-1);
char s2[maxn],s1[maxn];
complex<double> a[maxn],b[maxn];
int ans[maxn];
int len;
void fft(complex<double> a[],int n,int op){
	if(n == 1) return;
	complex<double> a1[n / 2], a2[n / 2];
	for(int i = 0;i < n / 2;i ++){
		a1[i] = a[i * 2];
		a2[i] = a[i * 2 + 1];
	}
	
	fft(a1,n / 2,op);
	fft(a2,n / 2,op);
	
	complex<double> w1({cos(2 * pie / n),sin(2 * pie / n) * op});
	complex<double> wk({1,0});
	
	for(int i = 0;i < n / 2;i ++){
		a[i] = a1[i] + a2[i] * wk;
		a[i + n / 2] = a1[i] - a2[i] * wk;
		wk = wk * w1;
	}
}
signed main(){
	cin>>s1>>s2;
	int n = strlen(s1) - 1,m = strlen(s2) - 1;
	for(int i = 0;i <= n;i ++){
		a[i].real(s1[n - i] - '0');
	}for(int i = 0;i <= m;i ++){
		b[i].real(s2[m - i] - '0');
	}
	for(m = n + m,n = 1;n <= m;n <<= 1);
	fft(a,n,1);
	fft(b,n,1);
	for(int i = 0;i < n;i ++){
		a[i] = a[i] * b[i];
	}fft(a,n,-1);
    for(int i = 0, cnt = 0; i < n || cnt; i ++){
        cnt += (int)(a[i].real() / n + 0.5);
        ans[len ++] = cnt % 10;
        cnt /= 10;
    }
    while(len >= 1 && !ans[len]) len --;
    do{
       cout<<ans[len];
    }while(len --);
    from yanyu289;
}

2:迭代fft

C++
#include<bits/stdc++.h>  
#define ll long long//十年oi一场空,不开ll…… 
#define from return
#define yanyu289 0
using namespace std;
const int maxn = 1e6 * 4 + 5;
const double pie = acos(-1);  //acos(-1)的值为π,见前缀里面 

char s2[maxn], s1[maxn];       // 用于存储输入的两个大整数字符串
complex<double> a[maxn], b[maxn];  // 复数数组,用于FFT计算
int ans[maxn], len;// 存储最终的乘积结果和长度 
void fft(complex<double> a[], int n, int op) {
//a-待变换的复数数组,n-数组长度(必须是2的幂),op决定的ifft还是fft 
    if (n == 1) return;  //当长度为1时无需变换,直接回代 
    complex<double> a1[n / 2], a2[n / 2]; //拆分数组为偶数索引和奇数索引两部分
    for (int i = 0; i < n / 2; i++) {
        a1[i] = a[i * 2];
        a2[i] = a[i * 2 + 1]; 
    }
    
    fft(a1, n / 2, op);//递归版直接无脑下分就行了 
    fft(a2, n / 2, op);
    complex<double> w1({cos(2 * pie / n), sin(2 * pie / n) * op}); //单位根大帝 
    complex<double> wk({1, 0});//明显,初始值为w_n^0 = 1 
    for (int i = 0; i < n / 2; i++){//合并
        a[i] = a1[i] + a2[i] * wk; 
        a[i + n / 2] = a1[i] - a2[i] * wk;
        wk = wk * w1;//更新 
    }
}

signed main(){ 
    cin >> s1 >> s2;//输入会爆炸哦,要用string 
    int n = strlen(s1) - 1;
    int m = strlen(s2) - 1;
    for (int i = 0; i <= n;i++){//注意逆序(想一下为什么) (因为这样存对应下标i就是对应10^i,便于进位) 
        a[i].real(s1[n - i] - '0');  //.real()可以取实部
    }
    for (int i = 0; i <= m; i++) {
        b[i].real(s2[m - i] - '0');
    }
    for (m += n, n = 1; n <= m; n <<= 1);
    // 计算需要的FFT长度(取大于等于n + m + 1的最小的2的幂)(一定要是2的幂,保证周期,对称性的实现)
    
    fft(a, n, 1);//op = 1:正向FFT,计算作为点值式的结果 
    fft(b, n, 1);
    for (int i = 0; i < n; i++) {
        a[i] = a[i] * b[i];
    }
    fft(a, n, -1);//逆向fft(也就是IFFT)转化回系数 
    for (int i = 0, cnt = 0; i < n || cnt; i++) {
        cnt += (int)(a[i].real() / n + 0.5);  // 取实部并除以n(IFFT归一化),加0.5是为了四舍五入
        ans[len ++] = cnt % 10;
        cnt /= 10;//做过高精的应该都能理解这里 
    }
    
    while (len >= 1 && !ans[len - 1]) len--;//因为前面保证了n的2的幂,所以要去除前导0 
    // 输出结果(逆序输出,因为是逆序存储的)
    do{
        cout << ans[len - 1];
    } while(--len);
    from yanyu289; 
}

三:dp

1:树形dp

C++
#include<bits/stdc++.h>
using namespace std;
const int maxn = 10005;
int n, r[maxn], l, k;
int f[maxn][5];
bool rd[maxn];
vector<int> son[maxn];
void dfs(int x){
	f[x][0] = 0;
	f[x][1] = r[x];
	for(int i = 0;i < son[x].size();i++){
		int y = son[x][i];
		dfs(y);
		f[x][0] += max(f[y][0], f[y][1]);//x不去,y去/不去最大值加起来 
		f[x][1] += f[y][0];//x去了,y不能去 
	}
}
int main(){
	cin>>n;
	for(int i = 1;i <= n;i ++){
		cin >> r[i];
	}	
	for(int i = 1;i < n;i ++){
		cin >> l >> k;
		son[k].push_back(l);
		rd[l] = 1;
	}
	for(int i = 1;i <= n;i ++){
		if(rd[i] == 0){
			dfs(i);
			cout << max(f[i][0],f[i][1]);
			break;
		}
	}
	return 0;
}

2:背包

(1)采药1(有限个数)
C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 1000 + 55, mod = 998244353, inf = 1e9;
const double pi = acos(-1);
int T;
int n, m;
int v[maxn], w[maxn], f[maxn][maxn];
signed main(){
	cin >> n >> m;//n=T,m=M
	for(int i = 1;i <= m;i ++){
		cin >> w[i] >> v[i];
	}
	for(int i = 1;i <= m;i ++){
		for(int j = 1;j <= n;j ++){
			if(j >= w[i]) f[i][j] = max(f[i - 1][j], f[i - 1][j - w[i]] + v[i]);
			else f[i][j] = f[i - 1][j];
		}
	}
	cout << f[m][n];
	from yanyu289;
}
(2) 采药2(无限个数)
C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 1e4 + 55, mod = 998244353, inf = 1e9;
const double pi = acos(-1);
int T;
int t, m, v[maxn], w[10000005];
int f[10000005];
signed main(){
    cin >> t >> m;
	for(int i = 1;i <= m;i ++){
		cin >> w[i] >> v[i];
	}
	for(int i = 1;i <= m;i ++){
		for(int j = 1;j <= t;j ++){
        	if(j >= w[i]) f[j] = max(f[j], f[j - w[i]] + v[i]);
		}
	}
	cout << f[t];
	from yanyu289;
}

四:数学类

1:模意义下的乘法逆元

(1)线性求逆元(O(n))
C++
#include<bits/stdc++.h>//线性求逆元
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 3e6 + 55, mod = 998244353, inf = 2147483647;
const double pi = acos(-1);
int T;
int x, y, m, n, l, k;
int a, b, p;
int prte[maxn];
signed main(){
    ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin >> n >> p;
    for(int i = 1; i<= n; i++){
    	if(i == 1){
    		prte[i] = 1;
    		cout << 1 << endl;
    		continue;
		}
		prte[i] = (p - p / i) * prte[p % i] % p;
		cout << prte[i] << '\n';
	}
	from yanyu289;
}
(2) O(n + logplogp), 费马小定理+递推
C++
#include<bits/stdc++.h>//费马小定理+递推 
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 3e6 + 55, mod = 998244353, inf = 2147483647;
const double pi = acos(-1);
int T;
int x, y, m, n, l, k;
int b, p;
int frac[maxn], inv[maxn], a[maxn];
int qpow(int a, int b, int mod){
	int res = 1;
	while(b){
		if(b & 1) res = res * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return res;
}
void init(){
    frac[0] = 1;
	for(int i = 1;i <= n;i ++){
		a[i] = i;
		frac[i] = frac[i - 1] * a[i];
		frac[i] = frac[i] % p;
	}
	inv[n] = qpow(frac[n], p - 2, p);
	for(int i = n - 1;i >= 1;i --){
		inv[i] = inv[i + 1] * a[i +1 ];
		inv[i] = inv[i] % p;
	}
	
}
signed main(){
    ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin >> n >> p;
	init();
	for(int i = 1;i <= n;i ++){
		cout << frac[i - 1] % p * inv[i] % p << '\n';
	}
	from yanyu289;
}
(3)不连续逆元,模版2
C++
#include<bits/stdc++.h>//费马小定理+递推 
#define int unsigned long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 5e6 + 55, inf = 2147483647;
const double pi = acos(-1);
int T;
int m, n, k, ans;
int b, p, mod;
int frac[maxn], inv[maxn], a[maxn];
int qpow(int a, int b, int mod){
	int res = 1;
	while(b){
		if(b & 1) res = res * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return res;
}
void init(){
    frac[0] = 1;
	for(int i = 1;i <= n;i ++){
		frac[i] = frac[i - 1] * a[i];
		frac[i] %= p;
	}
	inv[n] = qpow(frac[n], p - 2, p);
	for(int i = n - 1;i >= 1;i --){
		inv[i] = inv[i + 1] * a[i + 1];
		inv[i] %= p;
	}
}
signed main(){
    ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin >> n >> p >> k;
	mod = p, b = 1;
	for(int i = 1;i <= n;i ++){
		cin >> a[i];
	}
	init();
	for(int i = 1;i <= n;i ++){
		b = b * k % mod;
		ans = ans + frac[i - 1] * inv[i] % mod * b % mod;
		ans %= mod;
	}
	cout << (ans % mod + mod) % mod;
	from yanyu289;
}

2:CRT——中国剩余定理

C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 2e5 + 55, mod = 998244353, inf = 2147483647;
const double pi = acos(-1);
int x, y, n, l, k, a[20], b[20];
__int128 exgcd(int a, int b, int &x, int &y){
	if(b == 0){
		x = 1, y = 0;
		return a;
	}
	int x1, y1, d;
	d = exgcd(b, a % b, x1, y1);
	x = y1;
	y = x1 - a / b * y1;
	return d;
}
__int128 m = 1,ans = 0;
__int128 crt(){
	for(int i = 1;i <= n;i ++) m *= a[i];
	for(int i = 1;i <= n;i ++){
    	__int128 c = m / a[i];
		exgcd(c,a[i],x,y);
		ans += b[i] * c * x % m;
        ans %= m;
	}
	return (ans % m + m) % m;
}
signed main(){
    ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin >> n;
	for(int i = 1;i <= n;i ++){
		cin >> a[i] >> b[i];
	}
	cout << (int)crt() % (int)m;
    return 0;
}

3:EXGCD

(1)同余方程
C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 2e5 + 55, mod = 998244353, inf = 2147483647;
const double pi = acos(-1);
int x, y, m, n, l, k, a, b;
int exgcd(int a, int b, int &x, int &y){
	if(b == 0){
		x = 1, y = 0;
		return a;
	}
	int x1, y1, d;
	d = exgcd(b, a % b, x1, y1);
	x = y1;
	y = x1 - a / b * y1;
	return d;
}
signed main(){
    ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin >> a >> b;
	int d = exgcd(a, b, x, y);
	cout << (x + b) % b;
	from yanyu289;
}
(2)有理数取余
C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 2e5 + 55, mod = 19260817, inf = 2147483647;
const double pi = acos(-1);
int x, y, m, n, l, k, a, b;
int read(){
    int x =0 , ch = getchar();
    while(!isdigit(ch) && ch != EOF) ch = getchar();
    while(isdigit(ch)){
    	x = (x << 3) + (x << 1) + (ch - '0');
		x %= mod;
		ch = getchar();
	}
    return x;
}
void exgcd(int a, int b){
	if(b == 0){
		x = 1, y = 0;
		return;
	}
	exgcd(b, a % b);
	int t = x;
	x = y;
	y = t - (a / b) * y;
}
signed main(){
    ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	a = read();
	b = read();
	if(b == 0){
		puts("Angry!");
        return 0;
	}
	exgcd(b, mod);
	x = (x % mod + mod) % mod;
	cout << (a * x) % mod;
	from yanyu289;
}

4:扩展欧拉定理

C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 2e7 + 55, inf = 2147483647;
const double pi = acos(-1);
char s[maxn];
int a, m, mod;
bool flag;
int phi(int n){ 
	int res = n;
	for(int i = 2;i * i <= n;i++){
    if(n % i == 0){
      res = res / i * (i - 1);
      while(n % i == 0) n /= i;
    }
  }
  if(n > 1) res = res /n * (n-1);
  return res;
}
int depow(int ph){//降 
	int t = 0;
    for(int i = 0;s[i] != 0;i++){
      t = t * 10 + (s[i] - '0');
      if(t >= ph) flag = 1, t %= ph;
    }
    if(flag == 1)t += ph;
    return t;
}
int qpow(int a, int b){
	int res = 1;
	while(b){
		if(b & 1) res = res * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return res;
}
signed main(){
	cin >> a >> m;
	cin >> s;
	mod = m;
	cout << qpow(a, depow(phi(m)));
	from yanyu289;
}

5:Lucas定理

C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 2e5 + 55, mod = 998244353, inf = 2147483647;
const double pi = acos(-1);
int T;
int n, m, p;
int f[maxn];
int qpow(int a, int b, int mod){
	int res = 1;
	while(b){
		if(b & 1) res = res * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return res;
}
int c(int n,int m,int p){
	if(m > n )return 0;
	return ((f[n] * qpow(f[m], p - 2, p)) % p
					* qpow(f[n - m], p - 2, p) % p);
}
int lucas(int n,int m,int p){
	return (m == 0) ? 1 : c(n % p ,m % p, p)
							* lucas(n / p, m / p, p) % p;
}
signed main(){
	cin >> T;
	for(int i = 1;i <= T;i ++){
		cin >> n >> m >> p;
		f[0] = 1;
		for(int i = 1;i <= p;i ++){
			f[i] = (f[i - 1] * i) % p;	
		 }
		cout << lucas(n + m, n, p) << endl;
	}
	from yanyu289;
}

五:线段树

1:区间加减区间查,线段树1

C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
#define ls(x) x << 1
#define rs(x) (x << 1) + 1
using namespace std;
const int maxn = 1e5 + 55, mod = 998244353, inf = 1e9;
const double pi = acos(-1);
struct tree{
	int l, r;
	int c;
}tr[maxn << 2];
int n, q;
int op, a, b;
int k, arr[maxn], lazytag[maxn << 2]; 
void pushup(int u){
	tr[u].c = tr[ls(u)].c + tr[rs(u)].c;
}
void pushdown(int u){
	tr[ls(u)].c += lazytag[u] * (tr[ls(u)].r - tr[ls(u)].l + 1), tr[rs(u)].c += lazytag[u] * (tr[rs(u)].r - tr[rs(u)].l + 1);
	lazytag[ls(u)] += lazytag[u], lazytag[rs(u)] += lazytag[u];
	lazytag[u] = 0;
}
void build(int u, int l, int r){
	tr[u].l = l; tr[u].r = r;
	if(l == r){
		tr[u].c = arr[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(ls(u), l, mid);
	build(rs(u), mid + 1, r);
	pushup(u);
}
void update(int u, int l, int r, int k){
	if(l <= tr[u].l && tr[u].r <= r){
		tr[u].c += k * (tr[u].r - tr[u].l + 1);
		lazytag[u] += k;
		return;
	}
	pushdown(u);
	if(tr[ls(u)].r >= l) update(ls(u), l, r, k);
	if(tr[rs(u)].l <= r) update(rs(u), l, r, k);
	pushup(u);
}
int query(int u, int l, int r){
	if(l <= tr[u].l && tr[u].r <= r) return tr[u].c;
	pushdown(u);
	int ans = 0;
	if(tr[ls(u)].r >= l) ans += query(ls(u), l, r);
	if(tr[rs(u)].l <= r) ans += query(rs(u), l, r);
	return ans;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> q;
	for(int i = 1; i <= n; i ++) cin >> arr[i];
	build(1, 1, n);
	for(int i = 1; i <= q; i ++){
		cin >> op >> a >> b;
		if(op == 1){
			cin >> k;
			update(1, a, b, k); 
		}
		else cout << query(1, a, b) << endl;
	}
	from yanyu289;
}

2:区间加/乘,线段树2

C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
#define ls(x) x << 1
#define rs(x) (x << 1) + 1
using namespace std;
const int maxn = 1e5 + 55, mod = 998244353, inf = 1e9;
const double pi = acos(-1);
struct tree{
	int l, r;
	int c;
}tr[maxn << 2];
int n, q, m, arr[maxn], lazytagjia[maxn << 2], lazytagch[maxn << 2]; 
void pushup(int u){
	tr[u].c = (tr[ls(u)].c + tr[rs(u)].c) % m;
}
void change(int u){
	int fa = u >> 1;
	tr[u].c = (tr[u].c * lazytagch[fa] % m + lazytagjia[fa] * (tr[u].r - tr[u].l + 1) % m) % m;
	lazytagch[u] = lazytagch[fa] * lazytagch[u] % m;
	lazytagjia[u] = (lazytagjia[u] * lazytagch[fa] % m + lazytagjia[fa]) % m;
}
void pushdown(int u){
	change(ls(u));
	change(rs(u));
	lazytagjia[u] = 0;
	lazytagch[u] = 1;
}
void build(int u, int l, int r){
	lazytagch[u] = 1;
	tr[u].l = l;
	tr[u].r = r;
	if(l == r){
		tr[u].c = arr[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(ls(u), l, mid);
	build(rs(u), mid + 1, r);
	pushup(u);
}

void update(int u, int l, int r, int op, int k){
	if(l <= tr[u].l && tr[u].r <= r){
		if(op == 1){
			tr[u].c = tr[u].c * k % m;
			lazytagch[u] = lazytagch[u] * k % m;
			lazytagjia[u] = lazytagjia[u] * k % m;
		}
		else{
			tr[u].c = (tr[u].c + k * (tr[u].r - tr[u].l + 1) % m) % m;
			lazytagjia[u] = (lazytagjia[u] + k) % m;
		}
		return;
	}
	pushdown(u);
	if(tr[ls(u)].r >= l) update(ls(u), l, r, op, k);
	if(tr[rs(u)].l <= r) update(rs(u), l, r, op, k);
	pushup(u);
}

int query(int u, int l, int r){
	if(l <= tr[u].l && tr[u].r <= r) return tr[u].c;
	pushdown(u);
	int ans = 0;
	if(tr[ls(u)].r >= l) ans = (ans + query(ls(u), l, r)) % m;
	if(tr[rs(u)].l <= r) ans = (ans + query(rs(u), l, r)) % m;
	return ans;
}
signed main(){
	cin >> n >> q >> m;
	for(int i = 1; i <= n; i ++) cin >> arr[i];
	build(1, 1, n);
	for(int i = 1; i <= q; i ++){
		int op, a, b, k;
		cin >> op >> a >> b;
		if(op <= 2){
			cin >> k;
			update(1, a, b, op, k); 
		}
		else cout << query(1, a, b) << endl;
	}
	from yanyu289;
}

六:字符串类

1:字典树Trie

C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 3e6 + 55, mod = 998244353, inf = 1e9;
const double pi = acos(-1);
int T;
char s[maxn];
int t,n,m,q;
int ch[maxn][64], cnt[maxn], pos[maxn], idx; 
int ctoi(char c){
	int x;
	if(c >= 'A'&&c <= 'Z')x = c - 'A';
	if(c >= 'a'&&c <= 'z')x = c - 'a' + 26;
	if(c >= '0'&&c <= '9')x = c - '0' + 52;  
	return x;
}
void insert(char *s){ 
	int u = 0; 
	for(int i = 0;s[i];i ++) { 
		int j = ctoi(s[i]); 
		if(!ch[u][j]) ch[u][j] = ++idx; 
		u = ch[u][j]; 
		cnt[u]++; 
	} 
} 
int query(char *s){ 
	int  u = 0; 
	for(int i = 0;s[i];i ++) { 
		int j = ctoi(s[i]); 
		if(!ch[u][j]) return 0; 
		u = ch[u][j]; 
	} 
	return cnt[u]; 
} 
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> t;
	for(int i = 1;i <= t;i ++){
		cin >> n >> m;
		for(int j = 0;j <= idx;j ++){
			cnt[j] = 0;
			for(int k = 0;k <= 100;k ++){
				ch[j][k] = 0;
			}
		}
		idx = 0;
		for(int j = 1;j <= n;j ++){
			cin >> s;
			insert(s);
		}
		for(int j = 1;j<= m;j ++){
			cin >> s;
			cout << query(s) << endl;
		}
	}
	from yanyu289;
}

2:KMP

C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 1e6 + 55, mod = 998244353, inf = 1e9;
const double pi = acos(-1);
int T;
char s1[maxn], s2[maxn]; 
int n, m, nex[maxn], cnt;
void kmp(){
	cnt = 0;
	for(int i = 2;i <= m;i ++){
		while(cnt != 0 && s2[i] != s2[cnt + 1]){
			cnt = nex[cnt];
		}
		if(s2[i] == s2[cnt + 1])cnt ++;
		nex[i] = cnt;
	}
}
void rekmp(){
	cnt = 0;
	for(int i = 1;i <= n;i ++){
		while(cnt && s1[i] != s2[cnt + 1]){
			cnt = nex[cnt];
		}
		if(s1[i] == s2[cnt + 1])cnt ++;
		if(cnt == m){
			cout << i - m + 1 << endl;
			cnt = nex[cnt];
		}
	}
}
signed main(){
	cin >> s1 + 1 >> s2 + 1;
	n = strlen(s1 + 1);
	m = strlen(s2 + 1);
	kmp();
	rekmp();
	for(int i = 1;i <= m;i ++){
		cout << nex[i] << " ";
	}
    from yanyu289;
}

七:Tarjan

1:缩点

C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 10005, mod = 998244353, inf = 1e9;
const double pi = acos(-1);
int n, m, x, y;
vector<int> e[maxn], ne[maxn];
int dfn[maxn], low[maxn], scc[maxn], ans, cnt;
int st[maxn], inst[maxn], top, val[maxn];
int nv[maxn], dp[maxn], vis[maxn];
priority_queue<pair<int, int> > q;
void tar(int u) {
    dfn[u] = low[u] = ++cnt;
    st[++top] = u;
	inst[u] = 1;
    for(auto v : e[u]){
        if(!dfn[v]){
            tar(v);
            low[u] = min(low[u], low[v]);
        }else if(inst[v] == 1){
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(dfn[u] == low[u]){
        int y;
        ++ ans;
        do {
            y = st[top--];
            inst[y] = 0;
            scc[y] = ans;
        } while(u != y);
    }
}

signed main(){
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        cin >> val[i];
    }
    for(int i = 1; i <= m; i++){
        cin >> x >> y;
        e[x].push_back(y);
    }
    for(int i = 1; i <= n; i++){
        if(!dfn[i]) {
            tar(i);
        }
    }
    for(int i = 1; i <= n; i++){
        nv[scc[i]] += val[i];
        for(int y : e[i]) {
            int a = scc[i], b = scc[y];
            if(a != b) {
                ne[a].push_back(b);
            }
        }
    }
    for(int i = ans;i; i--){
        if(dp[i] == 0) {
            dp[i] = nv[i];
        }
        for(int x : ne[i]) {
            dp[x] = max(dp[x], dp[i] + nv[x]);
        }
    }
    cnt = -1;
    for(int i = 0; i <= ans; i++) {
        cnt = max(cnt, dp[i]);
    }
    cout << cnt;
    from yanyu289;
}

2:

其他

1:快速幂

C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 10005, mod = 998244353, inf = 1e9;
const double pi = acos(-1);
int a, b, p;
int qpow(int a, int b, int p){
	int res = 1;
	while(b){
		if(b & 1) res = res * a % p;
		a = a * a % p;
		b >>= 1;
	}
	return res;
}
signed main(){
    cin >> a >> b >> p;
    cout << a << "^" << b << " mod " << p << "=" << qpow(a, b, p); 
    from yanyu289;
}

2:并查集

C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 1e6 + 55, mod = 998244353, inf = 1e9;
const double pi = acos(-1);
int n, m;
int x, y, z;
int f[maxn];
int found(int o){
    if(f[o] == o){
        return f[o];
    }
    return f[o] = found(f[o]);
}
signed main(){
    cin >> n >> m;
    for(int i = 1; i <= n; i ++){
        f[i] = i;
    }
    for(int i = 1; i <= m; i ++){
        cin >> z >> x >> y;
        int a = found(x), b = found(y);
        if(z == 1){
            f[a] = b;
        } else if(z == 2){
            if(a == b){
                cout << "Y" << endl;
            } else {
                cout << "N" << endl;
            }
        }
    }
    from yanyu289;
}
    

3:快读快输

C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 1e8 + 55, mod = 998244353, inf = 1e9;
const double pi = acos(-1);
int n, ans, a[maxn];
int read(){
  int x = 0, f = 1;
  char ch = getchar_unlocked();
  while (!isdigit(ch)) {
    if (ch == '-') f *= -1;
    ch = getchar_unlocked();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar_unlocked();
// 一般就写getchar()就可以了
  }
  return f * x;
}

void write(int x){
    if(x < 0) putchar('-'), x = -x;
    if(x < 10) putchar(x + '0');
    else write(x / 10), putchar(x % 10 + '0');
}
signed main(){
	n = read();
	for(int i = 1;i <= n;i ++){
		a[i] = read();
		ans += a[i];
	}
	write(ans);
    from yanyu289;
}
    

4:线性筛质数

C++
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define from return
#define yanyu289 0
#define endl "\n"
using namespace std;
const int maxn = 1e8 + 55, mod = 998244353, inf = 1e9;
const double pi = acos(-1);
int prime[maxn];
bool used[maxn];
int cnt, n, q, k;
void query(){
	memset(used, 0 ,sizeof(used));
	used[1] = 1;
	for(int i = 2;i <= n;i ++){
		if(!used[i]) prime[++ cnt] = i;
		for(int j = 1;j <= cnt && i * prime[j] <= n;j ++){
			used[i * prime[j]] = 1;
			if(i % prime[j] == 0) break;
		}
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> q;
	query();
	while(q --){
		cin >> k;
		cout << prime[k] << endl;
	}
	from yanyu289;
}