跳到正文

算法·理论

代码-算法

zengyanbin约 9 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月21日
篇幅
约 9 分钟

版本

  1. v1当前
返回

快速幂

计算 mkm ^ {k} modmod p p.

C++
int qmi(int m, int k, int p = 1e9) {
	int ans = 1 % p;
	while (k) {
		if (k & 1) {
			ans = ans * m % p;
		}
		m = m * m % p;
		k /= 2;
	}
	return ans;
}

二分

二分查找

C++
int n, a[N];//a[]原数组,n数组长度
int find(int x) {
	int l = 1, r = n, ans = -1;
	while (l <= r) {
		int mid = l + (r - l) / 2;//防止越界
		if (a[mid] == x) {
			ans = mid;
			r = mid - 1;//找最前面的数;
			//l = mid + 1则是找最后的数
		} else if (a[mid] > x) {
			r = mid - 1;
		} else {
			l = mid + 1;
		}
	}
	return ans;
}

STL:

C++
lower_bound(前端,后端,查找的数);
upper_bound(前端,后端,查找的数);

二分答案

  1. 二分答案是通过二分查找找到一个值。
  2. 二分答案需要有一个check()函数,来判断二分出的答案是否成立。
  3. 总有一个数,以这个数为界线,一边全部成立,另一边全部不成立。
  4. 通俗来说,就是求“最大的最小”或“最小的最大”的问题。
  5. 二分答案的题面:答案是求最小或最大、有一个区间用于二分答案。

线性筛

C++
int n;//范围
int s[N], cnt;//储存质数
bool st[N];//质数为false, 合数为true
void f() {
	for (int i = 2; i <= n; i++) {
		if (!st[i]) {//判断质数
			s[cnt++] = i;
		}
		for (int j = 0; s[j] <= n / i; j++) {
			st[s[j]*i] = true;//i的因数改为真
			if (i % s[j] == 0) {
				break;
			}
		}
	}
}

高精度

高精加

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
	std::ios::sync_with_stdio(0), cin.tie(0), cin.tie(0);
	string s1, s2;
	cin >> s1 >> s2;
	int c[10005];
	memset(c, 0, sizeof(c));
	int a = 0, b = 0, len;
	for (int i = s1.size() - 1; i >= 0; i--, a++) {
		c[a] = s1[i] - '0';
	}
	for (int i = s2.size() - 1; i >= 0; i--, b++) {
		c[b] += s2[i] - '0';
		if (c[b] >= 10) {
			c[b] -= 10;
			c[b + 1] += 1;
		}
	}
	len = max(a, b);
	if (c[len] != 0) {
		len++;
	}
	for (int i = len - 1; i >= 0; i--) {
		cout << c[i];
	}
	return 0;
}

高精乘

C++
#include <bits/stdc++.h>
using namespace std;a
int a[1000010], b[1000010], c[1000010];
string s1, s2;

int main() {
	cin >> s1 >> s2;
	int lena = s1.size(), lenb = s2.size();
	for (int i = 0; i < s1.size(); i++) {
		a[lena - i] = s1[i] - '0';
	}
	for (int i = 0; i < s2.size(); i++) {
		b[lenb - i] = s2[i] - '0';
	}
	int len = lena + lenb;
	for (int i = 1; i <= lena; i++) {
		for (int j = 1; j <= lenb; j++) {
			c[i + j - 1] += a[i] * b[j];
		}
	}
	for (int i = 1; i < len; i++) {
		c[i + 1] += c[i] / 10;
		c[i] %= 10;
	}
	while (c[len] == 0 && len > 1) {
		len--;
	}
	for (int i = len; i >= 1; i--) {
		cout << c[i];
	}
	return 0;
}

高精除低精

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
string s;
int k;

signed main() {
	cin >> s >> k;
	int len = s.size();
	int tmp = 0;
	bool b = false;
	int m;
	for (int i = 0; i < len; i++) {
		tmp = tmp * 10 + s[i] - '0';
		m = tmp / k;
		if (m || b) {
			cout << tmp / k;
			tmp %= k;
			b = true;
		}
	}
	return 0;
}

前缀和

一维前缀和

C++
#include <bits/stdc++.h>
using namespace std;
int a[100005], s[100005];//原数组a[], 前缀和数组s[]
int n, m, x, y;

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		s[i] = s[i - 1] + a[i];//建立前缀和
	}
	cin >> m;
	while (m--) {
		cin >> x >> y;//输入左、右边界
		cout << s[y] - s[x - 1] << '\n';
	}
	return 0;
}

二维前缀和

C++
#include<bits/stdc++.h>
using namespace std;
int a[1005][1005];
int s[1005][1005];
int n, m;
int x_1, x_2, y_1, y_2;

int main(){
	cin >> n >> m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin >> a[i][j];
			s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
		}
	}
	cin >> x_1 >> y_1 >> x_2 >> y_2;
	cout << s[x_2][y_2] - s[x_1-1][y_2] - s[x_2][y_1-1] + s[x_1-1][y_1-1] << '\n';
	return 0;
}

三维前缀和

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int a[N][N][N];
int s[N][N][N];
int n, m, h;

int main() {
	cin >> n >> m >> h;
	for (int x = 1; x <= h; x++) {
		for (int y = 1; y <= n; y++) {
			for (int z = 1; z <= m; z++) {
				cin >> a[x][y][z];
				s[x][y][z] = s[x - 1][y][z] + s[x][y - 1][z] + s[x][y][z - 1] - s[x - 1][y - 1][z] - s[x - 1][y][z - 1] -
				             s[x][y - 1][z - 1] + s[x - 1][y - 1][z - 1] + a[x][y][z];
			}
		}
	}
	int x_1, y_1, z_1, x_2, y_2, z_2;
	cin >> x_1 >> y_1 >> z_1 >> x_2 >> y_2 >> z_2;
	cout << s[x_2][y_2][z_2] - s[x_1 - 1][y_2][z_2] - s[x_2][y_1 - 1][z_2] - 1 *
	     (s[x_1 - 1][y_1 - 1][z_1 - 1]) - s[x_2][y_2][z_1 - 1] +
	     s[x_1 - 1][y_1 - 1][z_2] + s[x_1 - 1][y_2][z_1 - 1] + s[x_2][y_1 - 1][z_1 - 1]  << '\n';
	return 0;
}

搜索

深度优先搜索(DFS)

C++
void dfs(int pos){//递归次数, 或要填的空。
	if(所有的空填完了){
		输出/记录答案;
		return;
	}
	for(枚举接下来可以填的空){
		if(这个空合法){
			记录下这个空(保存现场);
			dfs(pos+1);
			取消这个空(回复现场);
		}
	}
	return;
}

广度优先搜索(BFS)

C++
void bfs() {
	Q.push(初始状态);//将初始状态?入队
	while (!Q.empty()) {
		State u = Q.front();//取出队首
		Q.pop();//出队
		for (枚举所有可扩展状态) { //找到所有u可达状态v
			if (合法的) { //v需要满足某些条件,如未访问过,未在队内等
				Q.push(v);//入队(同时可能需要维护某些必要信息)
			}
		}
	}
}

编辑

原文