版本比较
Zelda_max的代码模板(持续更新)
正文
增加 202 行删除 5 行
96 行未改动9798991001011011021031041059 行未改动11511611711811911912012112212397 行未改动221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264266 行未改动全部展开531532533534499535536537538539130 行未改动全部展开6706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437446437457467477484 行未改动75375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285365785485585685785837 行未改动96 行未改动 st[start] = 1; for(int i = 1; i < n; i++) { int t = -1; for(int j = 1; j <= n; j++) { if(!st[j] && (t == -1 || dist[j] < dist[t])) t = j; if(!st[j] && (t == -1 || dist[j] < dist[t])) t = j; } st[t] = 1; for(int j = 1; j <= n; j++) { if(!st[j] && dist[t] + g[t][j] < dist[j]) {9 行未改动 g[i][j] = 1e9; } g[i][i] = 0; }
dijkstra(s); return 0; } ```97 行未改动 } exit(0); } ```- ### 并查集 - 也可以用于图。 ``` #include<bits/stdc++.h> using namespace std; struct DSU { std::vector<int> f, siz; DSU(int n) : f(n+1), siz(n+1, 1) { std::iota(f.begin(), f.end(), 0); } int leader(int x) { while(x != f[x]) x = f[x] = f[f[x]]; return x; } bool same(int x, int y) { return leader(x) == leader(y); } bool merge(int x, int y) { x = leader(x); y = leader(y); if(x == y) return false; siz[x] += siz[y]; f[y] = x; return true; } int size(int x) { return siz[leader(x)]; } }; int main() { int n; scanf("%d", &n); DSU dsu(n); return 0; } ```---# 数据结构
266 行未改动全部展开 return a[n] * b[k] % p * b[n-k] % p; } return comb(n/p, k/p, p) * comb(n%p, k%p, p) % p; }
signed main() { int t; cin >> t; while(t--) {130 行未改动全部展开 cout << ans; return 0; } ```- ### 高斯消元法 - 利用高斯消元法解如下的方程组的解。$$ \begin{cases} a_{1, 1} x_1 + a_{1, 2} x_2 + \cdots + a_{1, n} x_n = b_1 \\ a_{2, 1} x_1 + a_{2, 2} x_2 + \cdots + a_{2, n} x_n = b_2 \\ \cdots \\ a_{n,1} x_1 + a_{n, 2} x_2 + \cdots + a_{n, n} x_n = b_n \end{cases}$$ ``` #include<bits/stdc++.h> using namespace std; const double eps = 1e-6; int n; const int N = 110; double a[N][N + 1]; int gauss() { int r = 0; for(int c = 0; c < n; c++) { int maxx = r; for(int i = r + 1; i < n; i++) { if(abs(a[i][c]) > abs(a[maxx][c])) { maxx = i; } } if(abs(a[maxx][c]) < eps) continue; for(int j = c; j <= n; j++) { swap(a[r][j], a[maxx][j]); } double div = a[r][c]; for(int j = c; j <= n; j++) { a[r][j] /= div; } for(int i = 0; i < n; i++) { if(i == r) continue; if(abs(a[i][c]) > eps) { double cnt = a[i][c]; for(int j = c; j <= n; j++) { a[i][j] -= cnt * a[r][j]; } } } r++; } for(int i = r; i < n; i++) { if(abs(a[i][n]) > eps) { return -1; } } if(r < n) return 0; return 1; } int main() { cin >> n; for(int i = 0; i < n; i++) { for(int j = 0; j < n + 1; j++) { cin >> a[i][j]; } } int res = gauss(); if(res == -1) { cout << "No Solution\n"; } else if(res == 0) { cout << "No Solution\n"; } else { for(int i = 0; i < n; i++) { printf("%.2lf\n", a[i][n]); } } return 0; } ```
---# 动态规划
4 行未改动
---# 查找- ### 树状数组 1 - 区间查询,单点修改的树状数组。 ``` #include<bits/stdc++.h> #define int long long using namespace std; int n; int a[1000001]; int lowbit(int x) { return x & -x; } void add(int st, int val) { while(st <= n) { a[st] += val; st += lowbit(st); } } int sum(int st) { int x = 0; while(st > 0) { x += a[st]; st -= lowbit(st); } return x; } void solve(); signed main() { solve(); } void solve() { int t; cin >> n >> t; for(int i = 1, x; i <= n; i++) { cin >> x; add(i, x); } while(t--) { int x, y, z; cin >> x >> y >> z; if(x == 1) { add(y, z); } else { cout << (sum(z) - sum(y-1)) << "\n"; } } exit(0); } ```- ### 树状数组 2 - 单点查询,区间修改的树状数组。 ``` #include<bits/stdc++.h> #define int long long using namespace std; int n; int a[1000001]; int lowbit(int x) { return x & -x; } void add(int st, int val) { while(st <= n) { a[st] += val; st += lowbit(st); } } int sum(int st) { int x = 0; while(st > 0) { x += a[st]; st -= lowbit(st); } return x; } void solve(); signed main() { solve(); } void solve() { int t; cin >> n >> t; for(int i = 1, x; i <= n; i++) { cin >> x; add(i, x); } while(t--) { int x, y, z; cin >> x >> y >> z; if(x == 1) { add(y, z); } else { cout << (sum(z) - sum(y-1)) << "\n"; } } exit(0); } ```- ### ST 表 - 以 $O(1)$ 的时间复杂度查询区间最大值. - 以 $O(1)$ 的时间复杂度查询区间最大值。 ``` #include<bits/stdc++.h> //#define int long long using namespace std;37 行未改动