19 行代码C++算法模板
线性筛(欧拉筛)
线性筛法,筛出 1 ~ N 中所有的质数,时间复杂度 O(N)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
bool f[N+5];
int prime[N], cnt = 0;
#质数#质数筛
线性筛法,筛出 1 ~ N 中所有的质数,时间复杂度 O(N)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
bool f[N+5];
int prime[N], cnt = 0;
埃氏筛法,筛出 1 ~ N 中所有的质数,时间复杂度 O(N log log N)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
bool f[N+5];
int prime[N], cnt = 0;
BFS 搜索无权图中从起点到终点的最短路径长度(邻接矩阵)
#include <bits/stdc++.h>
using namespace std;
const int N = 105; // 根据题目点的数量范围调整
int g[N][N]; // 邻接矩阵,g[u][v] = 1 表示有边
bool vis[N];DFS 搜索无权图中从起点到终点的简单路径数量(邻接矩阵)
#include <bits/stdc++.h>
using namespace std;
const int N = 1005; // 根据题目点的数量范围调整
int g[N][N]; // 邻接矩阵,g[u][v] = 1 表示有边
bool vis[N];BFS 搜索无权图中从起点到终点的最短路径长度(邻接表)
#include <bits/stdc++.h>
using namespace std;
const int N = 1005; // 根据题目点的数量范围调整
vector<int> adj[N]; // 邻接表(有权图需要使用结构体)
bool vis[N];DFS 搜索无权图中从起点到终点的简单路径数量(邻接表)
#include <bits/stdc++.h>
using namespace std;
const int N = 1005; // 根据题目点的数量范围调整
vector<int> adj[N]; // 邻接表(有权图需要使用结构体)
bool vis[N];