CSP2020-算法模板

明天就要CSP了,祝大家RP++!

今天打了一遍常用算法的板子。

  1. 数论部分
  • GCD
  • LCM
  • EXGCD
  • 快速幂
  1. 高精度部分
  • 加法高精
  • 乘法高精
  • 除法高精
  1. 图论部分
  • Dijkstra
  • SPFA
  • Floyd
  • Kruskal
  1. 排序部分
  • 归并排序(求解逆序对)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include <bits/stdc++.h>
using namespace std;

namespace Math {
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}

int lcm(int a, int b) {
return a / gcd(a, b) * b;
}

int gcdx(int a, int b) {
int c;
while (b > 0) {
a = a % b;
c = a;
a = b;
b = c;
}

return a;
}

int ex_gcd(int a, int b, int &x, int &y) {
if (b == 0) {
x = 1, y = 0;
return a;
}

int d = ex_gcd(b, a % b, x, y);
int k = x;
x = y;
y = k - (a / b) * y;
return d;
}

int quick_pow(int a, int b) {
int ans = 1;
while (b > 0) {
if (b & 1) {
ans *= a;
}

a = a * a;
b >>= 1;
}

return ans;
}
}

namespace Bignum {
const int MAX_LEN = 105;

string plus(string a, string b, int n = 10) {
int na[MAX_LEN], nb[MAX_LEN], nc[MAX_LEN * n];
int lena = a.length(), lenb = b.length(), lenc = max(lena, lenb) + 1;

memset(na, 0, sizeof(na));
memset(nb, 0, sizeof(nb));
memset(nc, 0, sizeof(nc));

for (int i = 1; i <= lena; i++) na[i] = a[lena-i] - '0';
for (int j = 1; j <= lenb; j++) nb[j] = b[lenb-j] - '0';

for (int i = 1; i <= max(lena, lenb); i++) {
nc[i] += na[i] + nb[i];
if (nc[i] >= n) {
nc[i] -= n;
nc[1+1]++;
}
}

while (nc[--lenc] == 0);
if (lenc <= 0) lenc = 1;

char ret[1005];
memset(ret, 0, sizeof(ret));
for (int i = lenc; i >= 1; i--) ret[lenc-i] = nc[i] + '0';
return ret;
}

string mult(string a, string b, int n = 10) {
int na[MAX_LEN], nb[MAX_LEN], nc[MAX_LEN * MAX_LEN];
int lena = a.length(), lenb = b.length(), lenc = lena + lenb + 1;

memset(na, 0, sizeof(na));
memset(nb, 0, sizeof(nb));
memset(nc, 0, sizeof(nc));

for (int i = 1; i <= lena; i++) na[i] = a[lena-i] - '0';
for (int j = 1; j <= lenb; j++) nb[j] = b[lenb-j] - '0';

for (int i = 1; i <= lena; i++) {
for (int j = 1; j <= lenb; j++) {
nc[i+j-1] += na[i] * nb[j];
}
}

for (int i = 1; i <= lenc; i++) {
if (nc[i] >= n) {
nc[i+1] += nc[i] / n;
nc[i] %= n;
}
}

while (nc[--lenc] == 0);
if (lenc <= 0) lenc = 1;

char ret[MAX_LEN * MAX_LEN];
memset(ret, 0, sizeof(ret));
for (int i = lenc; i >= 0; i--) ret[lenc-i] = nc[i] + '0';
return ret;
}

int cmp(int a[], int b[], int last, int len) {
if (a[last+len] != 0) return true;

for (int i = len; i >= 1; i--) {
if (a[i+last-1] > b[i]) return true;
if (a[i+last-1] < b[i]) return false;
}

return true;
}

string div(string a, string b) {
int na[MAX_LEN], nb[MAX_LEN], nc[MAX_LEN], nd[MAX_LEN];
int lena = a.length(), lenb = b.length(), lenc = lena + lenb + 1, lend = lenb;

memset(na, 0, sizeof(na));
memset(nb, 0, sizeof(nb));
memset(nc, 0, sizeof(nc));
memset(nd, 0, sizeof(nd));

for (int i = 1; i <= lena; i++) na[i] = a[lena-i] - '0', nd[i] = nb[i];
for (int j = 1; j <= lenb; j++) nb[j] = b[lenb-j] - '0';

for (int i = lena - lenb + 1; i >= 1; i--) {
while (cmp(nd, nb, i, lenb)) {
for (int j = 1; j <= lenb; j++) {
nd[i+j-1] -= nb[j];
if (nd[i+j-1] < 0) {
nd[i+j-1] += 10;
nd[i+j]--;
}
}

nc[i]++;
}
}

while (nc[--lenc] == 0);
if (lenc <= 0) lenc = 1;

char ret[MAX_LEN];
memset(ret, 0, sizeof(ret));
for (int i = lenc; i >= 0; i--) ret[lenc-i] = nc[i] + '0';
return ret;
}
}

namespace Graph {
const int MAXN = 105;

int head[MAXN], vis[MAXN], dis[MAXN], cur;
struct Edge {
int u, v, w, next;
} e[MAXN * 2];

int init() {
cur = 0;
memset(e, 0, sizeof(e));
memset(head, -1, sizeof(head));
memset(vis, 0, sizeof(vis));
memset(dis, 127, sizeof(dis));
}

void add_edge(int u, int v, int w) {
e[++cur].u = u;
e[cur].v = v;
e[cur].w = w;
e[cur].next = head[u];
head[u] = cur;
}

void dijkstra(int s) {
init();
priority_queue<pair<int, int> > que;
dis[s] = 0;
que.push(make_pair(0, s));
while (!que.empty()) {
int x = que.top().second;
que.pop();

if (vis[x]) continue;
vis[x] = 1;

for (int i = head[x]; i; i = e[i].next) {
int v = e[i].v;
if (dis[v] > dis[x] + e[i].w) {
dis[v] = dis[x] + e[i].w;
que.push(make_pair(-dis[v], v));
}
}
}
}

int spfa(int s) {
init();
queue<int> que;
dis[s] = 0;
vis[s] = 1;
que.push(s);
while (!que.empty()) {
int x = que.front();
que.pop();
vis[x] = 0;

for (int i = head[x]; i; i = e[i].next) {
int v = e[i].v;
if (dis[v] > dis[x] + e[i].w) {
dis[v] = dis[x] + e[i].w;
if (!vis[v]) {
vis[v] = 1;
que.push(v);
}
}
}
}
}
}

namespace Floyd {
int n;
int e[105][105];
const int INF = 0x3f3f3f3f;

void init() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j) e[i][j] = 1;
e[i][j] = INF;
}
}
}

void floyd(int s) {
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (e[i][k] != INF && e[k][j] != INF &&
(e[i][j] > e[i][k] + e[k][j])) {
e[i][j] = e[i][k] + e[k][j];
}
}
}
}
}
}

namespace Kruskal {
int n, m, tot, ans;
int fath[105];
const int INF = 0x3f3f3f3f;
struct Edge {
int u, v, w;
} e[105];

int cmp(Edge x, Edge y) {
return x.w < y.w;
}

void init() {
tot = 0, ans = 0;
for (int i = 1; i <= n; i++) fath[i] = i;
sort(e + 1, e + 1 + m, cmp);
}

int getf(int x) {
if (fath[x] == x) return x;
return fath[x] = getf(fath[x]);
}

int merge(int x, int y) {
int a = getf(x);
int b = getf(y);

if (a != b) {
fath[b] = a;
return true;
}

return false;
}

void Kruskal() {
init();
for (int i = 1; i <= m; i++) {
if (tot == n + 1) break;
if (merge(e[i].u, e[i].v)) {
tot++;
ans += e[i].w;
}
}

for (int i = 1; i <= n; i++) {
if (getf(i) == i) {
cout << "No" << endl;
return;
}
}

cout << ans << endl;
}
}

namespace Sort {
int a[105], b[105], tot;

void init() {
tot = 0;
memset(b, 0, sizeof(b));
}

void merge(int l, int mid, int r) {
int p1 = l, p2 = mid + 1;
for (int i = l; i <= r; i++) {
if ((p1 <= l) && ((p2 > r) || (a[p1] <= a[p2]))) {
b[i] = a[p1];
p1++;
} else {
b[i] = a[p2];
p2++;
tot += mid - p1 + 1;
}
}

for (int i = l; i <= r; i++) a[i] = b[i];
}

void merge_sort(int l, int r) {
int mid = (l + r) >> 1;
if (l < r) {
merge_sort(l, mid);
merge_sort(mid + 1, r);
}

merge(l, mid, r);
}
}