基础模板(一)
快速幂||取余运算 模板
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll quick_mod(ll a, ll b, ll p) {
ll ans = 1;
while (b) {
if (b & 1) ans = ans * a % p;
a = a * a % p;
b >>= 1;
}
return ans % p;
}
int main()
{
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
int a, b, p;
cin >> a >> b >> p;
cout << a << '^' << b << " mod " << p << '=';
cout << quick_mod(a, b, p) << endl;
return 0;
}
并查集 模板
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int a[N];
int find(int x) {
if (a[x] == x) return x;
return a[x] = find(a[x]);
}
void merge(int x, int y) {
int fx = find(x), fy = find(y);
a[fx] = fy;
}
int main()
{
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
for (int i = 0; i < n; i++) {
a[i] = i;
}
while (q--) {
int a, b, c;
cin >> a >> b >> c;
if (a == 1) {
merge(b, c);
}
else {
int fx = find(b);
int fy = find(c);
puts(fx == fy ? "Y" : "N");
}
}
return 0;
}
单源最短路径 模板
#include <bits/stdc++.h>
using namespace std;
const int N = 10010,M = 500010;
vector<vector<pair<int,int> > > a;
int dist[N];
int n,m,k;
void spfa(){
memset(dist,0x3f,sizeof dist);
dist[k] = 0;//初始点
queue<int> q;
q.push(k);
while(q.size()){
int node = q.front();
q.pop();
for(int i = 0 ; i < a[node].size();i++){
if(dist[a[node][i].first] > dist[node] + a[node][i].second){
dist[a[node][i].first] = dist[node] + a[node][i].second;
q.push(a[node][i].first);
}
}
}
for(int i = 1; i <= n ; i ++){
if(dist[i] > 0x3f3f3f / 2){
dist[i] = (1 << 31) - 1;
}
}
}
int main(){
cin >> n >> m >> k;
a.resize(n + 1);
for(int i = 0 ; i < m ; i ++){
int u,v,w;
cin >> u >> v >> w;
a[u].push_back({v,w});
}
spfa();
for(int i = 1 ; i <= n; i ++) cout << dist[i] << " ";
return 0;
}
线性筛 模板
#include <bits/stdc++.h>
using namespace std;
const int N = 1E8 + 10;
int a[N], p[N], cnt;
void getPirme(int n) {
for (int i = 2; i <= n; i++) {
if (!a[i]) p[cnt++] = i;
for (int j = 0; p[j] <= n / i; j++) {
a[p[j] * i] = 1;
if (i % p[j] == 0) break;
}
}
}
int query(int q) {
return p[q - 1];
}
int main()
{
// freopen("in.txt", "r", stdin);
// ios::sync_with_stdio(false);
// cin.tie(0);
int n, q;
scanf("%d%d", &n, &q);
// cin >> n >> q;
getPirme(n);
while (q--) {
int x;
//cin >> x;
scanf("%d", &x);
printf("%d\n", query(x));
//cout << query(x) << endl;
}
return 0;
}
最小生成树 模板
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 200010;
//int e[N], ne[N], idx, w[N], h[N];
//
//void add(int a, int b, int c) {
// e[idx] = a, w[idx] = c,ne[idx] = h[a],h[a] = idx++;
//}
//
int idx;
struct node {
int u, v, w;
}s[N];
int e[N];
int find(int x) {
if (e[x] == x) return x;
return e[x] = find(e[x]);
}
void merge(int x, int y) {
int fx = find(x);
int fy = find(y);
e[fx] = fy;
}
void add(int a,int b,int c) {
s[idx].u = a,s[idx].v = b, s[idx++].w = c;
}
bool cmp(const node a, const node b) {
return a.w < b.w;
}
int main() {
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
//memset(h, -1, sizeof h);
int n, m;
cin >> n >> m;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
for (int i = 1; i <= n; i++) e[i] = i;
sort(s, s + idx, cmp);
//for (int i = 0; i < idx; i++) {
// cout << s[i].w << endl;
//}
ll ans = 0;
for (int i = 0; i < idx; i++) {
int u = s[i].u;
int v = s[i].v;
int w = s[i].w;
int fx = find(u);
int fy = find(v);
if(fx != fy){
merge(fx, fy);
ans += w;
}
}
int c = 0;
for (int i = 1; i <= n; i++) c += (e[i] == i);
if (c > 1) cout << "orz" << endl;
else cout << ans << endl;
return 0;
}
堆 模板
#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
int h[N],len = 0;
int top(){
return h[1];
}
void up(int u){
if(u / 2 && h[u] < h[u / 2]){
swap(h[u],h[u / 2]);
up(u / 2);
}
}
void down(int u){
int t = u;
if(2 * u <= len && h[t] > h[2 * u]) t = 2 * u;
if(2 * u + 1 <= len && h[t] > h[2 * u + 1]) t = 2 * u + 1;
if(t != u){
swap(h[t],h[u]);
down(t);
}
}
void insert(int x){
h[++ len] = x;
up(len);
}
void pop(){
swap(h[1],h[len]);
len --;
down(1);
}
int main(){
int m;
cin >> m;
while(m --){
int k;
scanf("%d", &k);
if(k == 1){
int x;
scanf("%d",&x);
insert(x);
}
else if(k == 2){
printf("%d\n",top());
}
else pop();
}
return 0;
}
快速排序 模板
#include <bits/stdc++.h>
#include <unordered_map>
typedef long long ll;
using namespace std;
const int N = 100010;
int a[N];
void q_sort(int l, int r) {
if (l >= r) return;
int x = a[l + r >> 1];
int i = l - 1, j = r + 1;
while (i < j) {
do i++; while (a[i] < x);
do j--; while (a[j] > x);
if (i < j) swap(a[i], a[j]);
}
q_sort(l, j);
q_sort(j + 1, r);
}
int main() {
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
q_sort(0, n - 1);
for (int i = 0; i < n; i++) cout << a[i] << " ";
return 0;
}
字符串哈希 模板
#include <bits/stdc++.h>
#include <unordered_map>
typedef long long ll;
using namespace std;
const int MOD = 1e9 + 7;
unordered_map<ll, ll> h;
ll st_hash(string s) {
ll p = 1;
ll re = 0;
for (int i = 0; i < s.size(); i++) {
re = (re + p * s[i]) % MOD;
p = (p * 131) % MOD;
}
return re;
}
int main() {
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
int ans = 0;
while (n--) {
string s;
cin >> s;
ll t = st_hash(s);
if (h[t] == 0) ans++;
h[t] ++;
}
cout << ans << endl;
return 0;
}
整理的最高频的算法模板,刚刚入门的新手应当全部记下,不借助任何资料也应当能够写出。