模板题代码集合
zhclg2014_plus · · 算法·理论
P1177 【模板】排序
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, a[N];
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
sort (a + 1, a + 1 + n);
for (int i = 1; i <= n; ++i)cout << a[i] << ' ';
return 0;
}
P1226 【模板】快速幂
#include <bits/stdc++.h>
using namespace std;
int qpow (int x, int y, int mod) {
long long res = 1, bs = x;
while (y) {
if (y & 1) (res *= bs) %= mod;
(bs *= bs) %= mod;
y >>= 1;
}
return res;
}
signed main(){
int x, y, z;
cin >> x >> y >> z;
cout << x << "^" << y << " mod " << z << "=" << qpow(x, y, z) << endl;
return 0;
}
P1495【模板】中国剩余定理(CRT)/ 曹冲养猪
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
typedef __int128 LL;
class FastIO {
private:
static const int SIZE = 1 << 20;
char in[SIZE], out[SIZE], *p1, *p2, *pp;
public:
FastIO() : p1(in), p2(in), pp(out) {}
inline char getchar() {
if (p1 == p2) p2 = (p1 = in) + fread(in, 1, SIZE, stdin);
return p1 == p2 ? EOF : *p1++;
}
template<typename T>
inline void read(T &x) {
x = 0;
T f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + (ch - '0');
ch = getchar();
}
x *= f;
}
inline void write_char(char c) {
if (pp - out == SIZE) {
fwrite(out, 1, SIZE, stdout);
pp = out;
}
*pp++ = c;
}
template<typename T>
inline void write(T x) {
if (x < 0) {
write_char('-');
x = -x;
}
if (x > 9) write(x / 10);
write_char(x % 10 + '0');
}
inline void write_string(const char* s) {
while (*s) {
write_char(*s++);
}
}
~FastIO() {
fwrite(out, 1, pp - out, stdout);
}
}d;
LL n, ans = 1, a[N], b[N], sum = 0;
LL exgcd(LL a ,LL b ,LL &x ,LL &y) {
if (!b) {
x = 1 ,y = 0;
return a;
}
int d = exgcd(b ,a % b ,y ,x);
y -= a / b * x;
return d;
}
int main() {
d.read(n);
for (int i = 1; i <= n; ++i) {
d.read(a[i]);
d.read(b[i]);
ans *= a[i];
}
for (int i = 1; i <= n; ++i) {
LL x, y;
exgcd(ans / a[i],a[i], x, y);
sum = sum + ans / a[i] * b[i] * x % ans;
}
d.write((sum % ans + ans) % ans);
return 0;
}
P1886 【模板】单调队列 / 滑动窗口
#include<bits/stdc++.h>
using namespace std;
int n, k;
int main(){
cin >> n >> k;
vector<int> a(n + 1);
for (int i = 1 ; i <= n; ++i) {
cin >> a[i];
}
vector<int> ans, ans1;
deque<int> q, q1;
for (int i = 1; i <= n; ++i) {
while (!q.empty() && q.front() <= i - k) {
q.pop_front();
}
while (!q.empty() && a[q.back()] >= a[i]) {
q.pop_back();
}
while (!q1.empty() && q1.front() <= i - k) {
q1.pop_front();
}
while (!q1.empty() && a[q1.back()] <= a[i]) {
q1.pop_back();
}
q.push_back(i);
q1.push_back(i);
if(i >= k){
ans.push_back(a[q.front()]);
ans1.push_back(a[q1.front()]);
}
}
for (auto i : ans) {
cout << i << ' ';
}
cout << '\n';
for (auto i : ans1) {
cout << i << ' ';
}
return 0;
}
P1903 【模板】带修莫队 / [国家集训队] 数颜色 / 维护队列
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 133335;
const int MAXM = 133335;
const int MAXC = 1000005;
int n, m;
int a[MAXN];
int blockSize;
struct Query {
int l, r, t, id;
int blockL, blockR;
} q[MAXM];
struct Modify {
int pos, val, pre;
} mod[MAXM];
int qcnt = 0, mcnt = 0;
int ans[MAXM];
int cnt[MAXC];
int colorCount = 0;
bool cmp(const Query& a, const Query& b) {
if (a.blockL != b.blockL) return a.blockL < b.blockL;
if (a.blockR != b.blockR) {
return (a.blockL & 1) ? (a.blockR < b.blockR) : (a.blockR > b.blockR);
}
return (a.blockR & 1) ? (a.t < b.t) : (a.t > b.t);
}
void add(int p) {
if (cnt[a[p]] == 0) colorCount++;
cnt[a[p]]++;
}
void del(int p) {
cnt[a[p]]--;
if (cnt[a[p]] == 0) colorCount--;
}
void apply(int p, int val, int l, int r) {
if (l <= p && p <= r) {
cnt[a[p]]--;
if (cnt[a[p]] == 0) colorCount--;
a[p] = val;
cnt[a[p]]++;
if (cnt[a[p]] == 1) colorCount++;
} else {
a[p] = val;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
blockSize = pow(n, 2.0 / 3.0);
for (int i = 1; i <= m; i++) {
char op;
int x, y;
cin >> op >> x >> y;
if (op == 'Q') {
q[++qcnt] = {x, y, mcnt, qcnt, 0, 0};
q[qcnt].blockL = (x - 1) / blockSize;
q[qcnt].blockR = (y - 1) / blockSize;
} else {
mod[++mcnt] = {x, y, a[x]};
a[x] = y;
}
}
for (int i = mcnt; i >= 1; i--) {
a[mod[i].pos] = mod[i].pre;
}
sort(q + 1, q + qcnt + 1, cmp);
int L = 1, R = 0, T = 0;
for (int i = 1; i <= qcnt; i++) {
while (L > q[i].l) add(--L);
while (R < q[i].r) add(++R);
while (L < q[i].l) del(L++);
while (R > q[i].r) del(R--);
while (T < q[i].t) {
T++;
apply(mod[T].pos, mod[T].val, L, R);
}
while (T > q[i].t) {
apply(mod[T].pos, mod[T].pre, L, R);
T--;
}
ans[q[i].id] = colorCount;
}
for (int i = 1; i <= qcnt; i++) {
cout << ans[i] << "\n";
}
return 0;
}
P1919 【模板】高精度乘法 / A*B Problem 升级版
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
typedef complex<double> Complex;
void fft(vector<Complex> &a, bool invert) {
int n = a.size();
for (int i = 1, j = 0; i < n; i++) {
int bit = n >> 1;
for (; j & bit; bit >>= 1)
j ^= bit;
j ^= bit;
if (i < j) swap(a[i], a[j]);
}
for (int len = 2; len <= n; len <<= 1) {
double ang = 2 * PI / len * (invert ? -1 : 1);
Complex wlen(cos(ang), sin(ang));
for (int i = 0; i < n; i += len) {
Complex w(1);
for (int j = 0; j < len / 2; j++) {
Complex u = a[i + j];
Complex v = a[i + j + len / 2] * w;
a[i + j] = u + v;
a[i + j + len / 2] = u - v;
w *= wlen;
}
}
}
if (invert) {
for (Complex &x : a)
x /= n;
}
}
vector<int> mult(const vector<int> &a, const vector<int> &b) {
int n = 1;
int total_len = a.size() + b.size();
while (n < total_len) n <<= 1;
vector<Complex> fa(a.begin(), a.end()), fb(b.begin(), b.end());
fa.resize(n);
fb.resize(n);
fft(fa, false);
fft(fb, false);
for (int i = 0; i < n; i++)
fa[i] *= fb[i];
fft(fa, true);
vector<int> result(total_len + 1, 0);
for (int i = 0; i < total_len; i++) {
result[i] = (int)round(fa[i].real());
}
int carry = 0;
for (int i = 0; i < total_len; i++) {
result[i] += carry;
carry = result[i] / 10;
result[i] %= 10;
}
while (carry) {
result[total_len++] = carry % 10;
carry /= 10;
}
while (result.size() > 1 && result.back() == 0)
result.pop_back();
return result;
}
int main() {
string s1, s2;
cin >> s1 >> s2;
vector<int> a(s1.size()), b(s2.size());
for (int i = 0; i < s1.size(); i++) a[i] = s1[s1.size() - 1 - i] - '0';
for (int i = 0; i < s2.size(); i++) b[i] = s2[s2.size() - 1 - i] - '0';
vector<int> res = mult(a, b);
for (int i = res.size() - 1; i >= 0; i--)
cout << res[i];
cout << '\n';
return 0;
}
P2613 【模板】有理数取余
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int MOD = 19260817;
LL a, b;
inline LL read() {
LL ret = 0 , f = 0;
char ch = getchar();
while (!isdigit(ch)) {
f |= ch == '-' ,ch = getchar();
}
while (isdigit(ch)) {
ret = ret * 10 + ch - '0' ,ch = getchar();
ret %= MOD;
}
return f ? -ret : ret;
}
LL qpow(LL a, LL b) {
LL res = 1, bs = a;
while (b) {
if (b & 1)(res *= bs) %= MOD;
(bs *= bs) %= MOD;
b >>= 1;
}
return res;
}
int main() {
a = read();
b = read();
if (!b || b % MOD == 0) {
cout << "Angry!\n";
return 0;
}
cout << a * qpow(b, MOD - 2) % MOD << endl;
return 0;
}
P2709 【模板】莫队 / 小B的询问
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e4 + 10;
int n, m, k, a[N], blen;
int cnt[N], lp = 1, rp = 0;
ll ans[N], cur_ans;
struct Qry {
int l, r, id;
bool operator < (Qry p) {
if ((l - 1) / blen == (p.l - 1) / blen) return r < p.r;
return l / blen < p.l / blen;
}
}q[N];
inline void add_cur(int x) {
cur_ans += 2 * cnt[x] + 1;
cnt[x]++;
}
inline void del_cur(int x) {
cur_ans -= 2 * cnt[x] - 1;
cnt[x]--;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> k;
blen = sqrt(n);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= m; ++i) {
cin >> q[i].l >> q[i].r;
q[i].id = i;
}
sort(q + 1, q + 1 + m);
for (int i = 1; i <= m; ++i) {
int L = q[i].l, R = q[i].r;
while (lp > L) add_cur(a[--lp]);
while (rp < R) add_cur(a[++rp]);
while (lp < L) del_cur(a[lp++]);
while (rp > R) del_cur(a[rp--]);
ans[q[i].id] = cur_ans;
}
for (int i = 1; i <= m; ++i) {
cout << ans[i] << '\n';
}
return 0;
}
P3366 【模板】最小生成树
#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 10;
const int M = 2e5 + 10;
struct Node {
int w, dis;
} tmp;
bool operator < (Node x, Node y) {
return x.dis > y.dis;
}
priority_queue<Node> q;
vector<pair<int, int>> G[N];
int n, m;
int cnt, ans, dis[N];
bool vis[N];
void Prim() {
dis[1] = 0;
tmp.dis = 0, tmp.w = 1;
q.push(tmp);
while (!q.empty()) {
tmp = q.top();
q.pop();
int w = tmp.w;
if (vis[w]) continue;
cnt++, vis[w] = 1, ans += dis[w];
for (auto [v, ww] : G[w]) {
if (dis[v] > ww) {
tmp.w = v;
tmp.dis = dis[v] = ww;
q.push(tmp);
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int x, y, z;
cin >> x >> y >> z;
G[x].push_back({y, z});
G[y].push_back({x, z});
}
memset(dis, 0x3f3f3f3f, sizeof dis);
Prim();
if (cnt == n) {
cout << ans << endl;
}
else {
cout << "orz\n";
}
return 0;
}
P3367 【模板】并查集
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
int n ,m;
int fa[MAXN];
void init() {
for (int i = 1;i <= n;++i) {
fa[i] = i;
}
}
int find(int x) {
if (fa[x] == x) {
return x;
}
return fa[x] = find(fa[x]);
}
void merge(int x ,int y) {
int fx = find(x) , fy = find(y);
if (fx == fy) {
return;
}
fa[fx] = fy;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
init();
while (m--) {
int opt ,x ,y;
cin >> opt >> x >> y;
if (opt == 1) {
merge(x ,y);
}
else {
if (find(x) == find(y)) cout << "Y\n";
else cout << "N\n";
}
}
return 0;
}
P3368 【模板】树状数组 2
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int lowbit(int x) {
return x & -x;
}
int n, m, a[N], tr[N];
void update(int x, int k) {
while (x <= N) {
tr[x] += k;
x += lowbit(x);
}
}
int query(int x) {
int res = 0;
while (x) {
res += tr[x];
x -= lowbit(x);
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
update(i, a[i] - a[i - 1]);
}
while (m--) {
int opt, x, y, z;
cin >> opt;
if (opt == 1) {
cin >> x >> y >> z;
update(x, z);
update(y + 1, -z);
}
else {
cin >> x;
cout << query(x) << '\n';
}
}
return 0;
}
P3369 【模板】普通平衡树
#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
rope<int> r;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
while (n--) {
int op;
cin >> op;
if (op == 1) {
int x;
cin >> x;
r.insert(lower_bound(r.begin(), r.end(), x) - r.begin(), x);
}
if (op == 2) {
int x;
cin >> x;
r.erase(lower_bound(r.begin(), r.end(), x) - r.begin(), 1);
}
if (op == 3) {
int x;
cin >> x;
cout << lower_bound(r.begin(), r.end(), x) - r.begin() + 1 << endl;
}
if (op == 4) {
int x;
cin >> x;
cout << r[x - 1] << endl;
}
if (op == 5) {
int x;
cin >> x;
cout << r[lower_bound(r.begin(), r.end(), x) - r.begin() - 1] << endl;
}
if (op == 6) {
int x;
cin >> x;
cout << r[upper_bound(r.begin(), r.end(), x) - r.begin()] << endl;
}
}
return 0;
}
P3370 【模板】字符串哈希
#include <bits/stdc++.h>
using namespace std;
set<string> st;
int main() {
int n;
cin >> n;
while (n--) {
string a;
cin >> a;
st.insert(a);
}
cout << st.size() << '\n';
return 0;
}
P3371 【模板】单源最短路径(弱化版)
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 0x3f3f3f3f3f3f3f3f;
int n, m, s;
vector<pair<int, int>> G[500010];
int dis[500010];
bool vis[500010];
void Dijkstra() {
dis[s] = 0;
while (true) {
int mn = -1;
for (int i = 1; i <= n; ++i) {
if (!vis[i] && (mn == -1 || dis[i] < dis[mn])) {
mn = i;
}
}
if (mn == -1 || dis[mn] == INF) break;
vis[mn] = 1;
for (auto v : G[mn]) {
int to = v.first;
int w = v.second;
if (dis[to] > dis[mn] + w) {
dis[to] = dis[mn] + w;
}
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> s;
for (int i = 1; i <= m; ++i) {
int u, v, w;
cin >> u >> v >> w;
G[u].push_back({v, w});
}
memset(dis, 0x3f, sizeof dis);
dis[s] = 0;
Dijkstra();
for (int i = 1; i <= n; ++i) {
if (dis[i] == INF) {
cout << INT_MAX << ' ';
} else {
cout << dis[i] << ' ';
}
}
cout << endl;
return 0;
}
P3372 【模板】线段树 1
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e5 + 10;
int n ,m ,a[N];
struct Node {
int p ,v ,lazy;
}tr[N << 2];
void pushup(int p) {
tr[p].v = tr[p << 1].v + tr[p << 1 | 1].v;
}
void pushdown(int p, int l, int r) {
int ls = p << 1, rs = p << 1 | 1, mid = l+r >> 1;
tr[ls].v += (mid - l + 1) * tr[p].lazy;
tr[ls].lazy += tr[p].lazy;
tr[rs].v += (r - mid) * tr[p].lazy;
tr[rs].lazy += tr[p].lazy;
tr[p].lazy = 0;
return;
}
void build(int l, int r, int p) {
if (l == r) {
tr[p].v = a[l];
return;
}
int mid = l + r >> 1;
build(l, mid, p << 1);
build(mid + 1, r, p << 1 | 1);
pushup(p);
}
int ask(int l, int r, int x, int y, int p) {
if (r < x || l > y) {
return 0;
}
if (l >= x && r <= y) {
return tr[p].v;
}
pushdown(p ,l ,r);
int mid = l + r >> 1;
return ask(l, mid, x, y, p << 1) + ask(mid + 1, r, x, y, p << 1 | 1);
}
void add(int l, int r, int x, int y, int p, int v) {
if (r < x || l > y) {
return;
}
if (l >= x && r <= y) {
tr[p].v += (r - l + 1) * v;
tr[p].lazy += v;
return;
}
pushdown(p ,l ,r);
int mid = l + r >> 1;
add(l, mid, x, y, p << 1, v);
add(mid + 1, r, x, y, p << 1 | 1, v);
pushup(p);
}
signed main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
build(1, n, 1);
while (m--) {
int opt, x, y, z;
cin >> opt;
if (opt == 1) {
cin >> x >> y >> z;
add(1, n, x, y, 1, z);
}
else {
cin >> x >> y;
cout << ask(1, n, x, y, 1) << '\n';
}
}
return 0;
}
P3373 【模板】线段树 2
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n, m, a[N], q;
#define int long long
struct Node {
int p, v, lazy1, lazy2;
} tr[N << 2];
void pushup(int p) {
tr[p].v = tr[p * 2].v + tr[p * 2 + 1].v;
}
void mktg(int x, int l, int r, int k1, int k2) {
tr[x].v = (k1 * tr[x].v + (r - l + 1) * k2 % m) % m;
(tr[x].lazy1 *= k1) %= m;
tr[x].lazy2 = (k2 + k1 * tr[x].lazy2) % m;
}
void pushdown(int p, int l, int r) {
int ls = p << 1, rs = p << 1 | 1, mid = l + r >> 1;
mktg(ls, l, mid, tr[p].lazy1, tr[p].lazy2);
mktg(rs, mid + 1, r, tr[p].lazy1, tr[p].lazy2);
tr[p].lazy1 = 1, tr[p].lazy2 = 0;
return;
}
void build(int l, int r, int p) {
tr[p].lazy1 = 1;
if (l == r) {
tr[p].v = a[l] % m;
return;
}
int mid = l + r >> 1;
build(l, mid, p << 1);
build(mid + 1, r, p << 1 | 1);
pushup(p);
}
int ask(int l, int r, int x, int y, int p) {
if (r < x || l > y) {
return 0;
}
if (l >= x && r <= y) {
return tr[p].v;
}
pushdown(p, l, r);
int mid = l + r >> 1;
return (ask(l, mid, x, y, p << 1) + ask(mid + 1, r, x, y, p << 1 | 1)) % m;
}
void add(int l, int r, int x, int y, int p, int v) {
if (r < x || l > y) {
return;
}
if (l >= x && r <= y) {
tr[p].v += (r - l + 1) * v;
tr[p].v %= m;
tr[p].lazy2 += v;
tr[p].lazy2 %= m;
return;
}
pushdown(p, l, r);
int mid = l + r >> 1;
add(l, mid, x, y, p << 1, v);
add(mid + 1, r, x, y, p << 1 | 1, v);
pushup(p);
}
void mul(int l, int r, int x, int y, int p, int v) {
if (r < x || l > y) {
return;
}
if (l >= x && r <= y) {
(tr[p].v *= v) %= m;
(tr[p].lazy1 *= v) %= m;
(tr[p].lazy2 *= v) %= m;
return;
}
pushdown(p, l, r);
int mid = l + r >> 1;
mul(l, mid, x, y, p << 1, v);
mul(mid + 1, r, x, y, p << 1 | 1, v);
pushup(p);
}
signed main() {
cin >> n >> q >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
build(1, n, 1);
while (q--) {
int opt, x, y, z;
cin >> opt;
z %= m;
if (opt == 1) {
cin >> x >> y >> z;
mul(1, n, x, y, 1, z);
}
else if (opt == 2) {
cin >> x >> y >> z;
add(1, n, x, y, 1, z);
}
else {
cin >> x >> y;
cout << ask(1, n, x, y, 1) << '\n';
}
}
return 0;
}
P3374 【模板】树状数组 1
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int lowbit(int x) {
return x & -x;
}
int n, m, a[N], tr[N];
void update(int x, int k) {
while (x <= N) {
tr[x] += k;
x += lowbit(x);
}
}
int query(int x) {
int res = 0;
while (x) {
res += tr[x];
x -= lowbit(x);
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
update(i, a[i]);
}
while (m--) {
int opt, x, y;
cin >> opt >> x >> y;
if (opt == 1) {
update(x ,y);
}
else {
cout << query(y) - query(x - 1) << '\n';
}
}
return 0;
}
P3375 【模板】KMP
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
int n ,m;
int nxt[MAXN];
string s1, s2;
void build_nxt(string s) {
int j = 0 , t = nxt[0] = -1;
while (j < m) {
if (t < 0 || s[j] == s[t]) {
nxt[++j] = ++t;
}
else {
t = nxt[t];
}
}
}
void match(string s1, string s2) {
build_nxt(s2);
int i = 0 , j = 0;
while (i < n) {
if (j == m) {
cout << i - j + 1 << '\n';
j = nxt[m];
}
else if (j < 0 || s1[i] == s2[j]) {
i++, j++;
}
else {
j = nxt[j];
}
}
if (j == m) {
cout << i - j + 1 << '\n';
}
for (int i = 1; i <= m; ++i) {
cout << nxt[i] << ' ';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> s1 >> s2;
n = s1.size(), m = s2.size();
match(s1 , s2);
return 0;
}
P3378【模板】堆
#include <bits/stdc++.h>
using namespace std;
priority_queue<int, vector<int>, greater<int> >q;
int main() {
int n;
cin >> n;
while (n--) {
int opt;
cin >> opt;
if (opt == 1) {
int x;
cin >> x;
q.push(x);
}
else if (opt == 2) {
cout << q.top() << '\n';
}
else q.pop();
}
return 0;
}
P3379【模板】最近公共祖先LCA
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
const int LOG = 20;
int n, m, s, f[N][LOG], dep[N];
vector<int> e[N];
void dfs(int u, int fa) {
f[u][0] = fa;
dep[u] = dep[fa] + 1;
for (int i = 1; i < LOG; i++) {
f[u][i] = f[f[u][i - 1]][i - 1];
}
for (auto v : e[u]) {
if (v != fa) {
dfs(v, u);
}
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
int diff = dep[u] - dep[v];
for (int i = 0; i < LOG; i++) {
if (diff & (1 << i)) {
u = f[u][i];
}
}
if (u == v) return u;
for (int i = LOG - 1; i >= 0; i--) {
if (f[u][i] != f[v][i]) {
u = f[u][i];
v = f[v][i];
}
}
return f[u][0];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> s;
dep[0] = 0;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
e[x].push_back(y);
e[y].push_back(x);
}
dfs(s, 0);
while (m--) {
int u, v;
cin >> u >> v;
cout << lca(u, v) << '\n';
}
return 0;
}
P3383【模板】线性筛素数
#include <bits/stdc++.h>
using namespace std;
const int N = 1e8 + 10;
int n, q;
bitset<N> isp;
vector<int> prm;
void sieve() {
isp.set();
isp[1] = 0;
for (int i = 2; i <= n; ++i) {
if (isp[i]) {
prm.push_back(i);
}
for (int p : prm) {
if (i * p > n) break;
isp[i * p] = 0;
if (i % p == 0) break;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> q;
sieve();
while (q--) {
int k;
cin >> k;
cout << prm[k - 1] << '\n';
}
return 0;
}
P3384【模板】重链剖分 / 树链剖分
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int ecnt;
struct edge {
int to, nxt;
};
edge G[N << 1];
int hd[N], dfncnt;
int n, m, r, mod;
void addedge(int u, int v) {
G[++ecnt].to = v;
G[ecnt].nxt = hd[u];
hd[u] = ecnt;
}
int siz[N], fa[N], dep[N], son[N], top[N], dfn[N], dfn2[N];
int a[N];
void dfs1(int u, int f) {
siz[u] = 1, fa[u] = f;
dep[u] = dep[f] + 1;
for (int i = hd[u]; i; i = G[i].nxt) {
int v = G[i].to;
if (v == f) continue;
dfs1(v, u);
siz[u] += siz[v];
if (son[u] == 0 || siz[v] > siz[son[u]]) son[u] = v;
}
}
void dfs2(int u, int t) {
top[u] = t;
dfn[u] = ++dfncnt;
dfn2[dfncnt] = u;
if (son[u])
dfs2(son[u], t);
for (int i = hd[u]; i; i = G[i].nxt) {
int v = G[i].to;
if (v == fa[u] || v == son[u])
continue;
dfs2(v, v);
}
}
struct Node {
int v, lzy;
} tr[N << 2];
void pushup(int p) {
tr[p].v = (tr[p << 1].v + tr[p << 1 | 1].v) % mod;
}
void build(int l, int r, int p) {
if (l == r) {
tr[p].v = a[dfn2[l]];
return;
}
int mid = (l + r) / 2;
build(l, mid, p << 1);
build(mid + 1, r, p << 1 | 1);
pushup(p);
}
void pushdown(int p, int l, int r) {
int ls = p << 1 ,rs = p << 1 | 1 ,mid = l + r >> 1;
(tr[ls].v += (mid - l + 1) * tr[p].lzy) %= mod;
(tr[ls].lzy += tr[p].lzy) %= mod;
(tr[rs].v += (r - mid) * tr[p].lzy) %= mod;
(tr[rs].lzy += tr[p].lzy) %= mod;
tr[p].lzy = 0;
return;
}
int query(int l, int r, int x, int y, int p) {
if (r < x || l > y) return 0;
if (l >= x && r <= y) return tr[p].v;
int mid = l + r >> 1;
pushdown(p, l, r);
return (query(l, mid, x, y, p << 1) % mod + query(mid + 1, r, x, y, p << 1 | 1) % mod) % mod;
}
void update(int l, int r, int x, int y, int p, int val) {
if (x > r || y < l) return;
if (x <= l && r <= y) {
(tr[p].v += (r - l + 1) * val) %= mod;
(tr[p].lzy += val) %= mod;
return;
}
int mid = l + r >> 1;
pushdown(p, l, r);
update(l, mid, x, y, p << 1, val);
update(mid + 1, r, x, y, p << 1 | 1, val);
pushup(p);
}
int query_path(int x, int y) {
int res = 0;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
(res += query(1, n, dfn[top[x]], dfn[x], 1)) %= mod;
x = fa[top[x]];
}
if (dep[x] > dep[y]) swap(x, y);
(res += query(1, n, dfn[x], dfn[y], 1)) %= mod;
return res;
}
void update_path(int x, int y, int v) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
update(1, n, dfn[top[x]], dfn[x], 1, v);
x = fa[top[x]];
}
if (dep[x] > dep[y]) swap(x, y);
update(1, n, dfn[x], dfn[y], 1, v);
}
int main() {
cin >> n >> m >> r >> mod;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1, u, v; i < n; ++i) {
cin >> u >> v;
addedge(u, v);
addedge(v, u);
}
dfs1(r, 0);
dfs2(r, 0);
build(1, n, 1);
while (m--) {
int op, x, y, z;
cin >> op;
if (op == 1) {
cin >> x >> y >> z;
update_path(x, y, z);
}
if (op == 2) {
cin >> x >> y;
cout << query_path(x, y) % mod << endl;
}
if (op == 3) {
cin >> x >> y;
update(1, n, dfn[x], dfn[x] + siz[x] - 1, 1, y);
}
if (op == 4) {
cin >> x;
cout << query(1, n, dfn[x], dfn[x] + siz[x] - 1, 1) % mod << endl;
}
}
return 0;
}
P3385【模板】负环
#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 10;
const int M = 3e3 + 10;
int n, m, Gtop;
int dis[N], hd[N], cnt[N];
bool vis[N];
struct Node {
int nxt, to, dis;
};
Node G[M * 2];
void add(int u, int v, int w) {
G[++Gtop].nxt = hd[u];
G[Gtop].to = v;
G[Gtop].dis = w;
hd[u] = Gtop;
}
bool spfa() {
queue<int> q;
memset(dis, 0x3f, sizeof dis);
memset(vis, 0, sizeof vis);
memset(cnt, 0, sizeof cnt);
dis[1] = 0;
q.push(1);
vis[1] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = 0;
for (int i = hd[u]; i; i = G[i].nxt) {
int v = G[i].to;
if (dis[v] > dis[u] + G[i].dis) {
dis[v] = dis[u] + G[i].dis;
cnt[v] = cnt[u] + 1;
if (cnt[v] >= n) return 1;
if (!vis[v]) {
vis[v] = 1;
q.push(v);
}
}
}
}
return 0;
}
void solve() {
memset(hd, 0, sizeof hd);
Gtop = 0;
cin >> n >> m;
for (int i = 1, u, v, w; i <= m; ++i) {
cin >> u >> v >> w;
add(u, v, w);
if (w >= 0) add(v, u, w);
}
if (spfa()) cout << "YES\n";
else cout << "NO\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
B3644【模板】拓扑排序 / 家谱树
#include <bits/stdc++.h>
using namespace std;
const int N = 1010, M = 10010;
int n, m;
struct edge {
int to, nxt;
};
edge e[M];
int hd[N], ecnt, ind[N];
void add(int u, int v) {
e[++ecnt].to = v;
e[ecnt].nxt = hd[u];
hd[u] = ecnt;
}
queue<int> q;
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
int u;
while (cin >> u && u) {
add(i, u);
ind[u]++;
}
}
for (int i = 1; i <= n; ++i) {
if (!ind[i]) {
q.push(i);
}
}
vector<int> ans;
while (!q.empty()) {
int u = q.front();
q.pop();
ans.push_back(u);
for (int i = hd[u]; i; i = e[i].nxt) {
int v = e[i].to;
ind[v]--;
if (!ind[v]) q.push(v);
}
}
for (auto it : ans) {
cout << it << ' ';
}
cout << endl;
return 0;
}
P3805【模板】Manacher
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
s = ' ' + s;
string t = "!";
for (int i = 1; i < s.size(); ++i) {
t += s[i];
t += "!";
}
int n = t.size();
vector<int> p(n, 0);
int cent = 0, maxr = 0, ans = 0;
for (int i = 1; i <= n; i++) {
if (i < maxr) {
int mr = 2 * cent - i;
p[i] = min(p[mr], maxr - i);
}
int L = i - (p[i] + 1), R = i + (p[i] + 1);
while (L >= 0 && R < n && t[L] == t[R]) {
p[i]++, L--, R++;
}
if (i + p[i] > maxr) {
cent = i, maxr = i + p[i];
}
ans = max(ans, p[i]);
}
cout << ans << endl;
return 0;
}
P3807【模板】卢卡斯定理 / Lucas 定理
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m, p;
ll fac[100010];
ll qpow(ll a, ll b, ll p) {
ll ans = 1, bs = a;
while (b) {
if (b & 1) {
ans = (ans * bs) % p;
}
bs = (bs * bs) % p;
b >>= 1;
}
return ans;
}
ll C(ll n, ll m, ll p) {
if (m > n) return 0;
return fac[n] * qpow(fac[m], p - 2, p) % p * qpow(fac[n - m], p - 2, p) % p;
}
ll lucas(ll n, ll m, ll p) {
if (!m) return 1;
return C(n % p, m % p, p) * lucas(n / p, m /p , p) % p;
}
int main() {
int T;
cin >> T;
while (T--) {
cin >> n >> m >> p;
fac[0] = 1;
for (int i = 1; i <= p; ++i) {
fac[i] = fac[i - 1] * i % p;
}
cout << lucas(n + m, n, p) << '\n';
}
return 0;
}
P3810【模板】三维偏序 / 陌上花开
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const int MAXK = 2e5 + 10;
int n, k;
int freq[MAXN];
int bit[MAXK];
vector<int> used;
inline int lowbit(int x) { return x & -x; }
void add(int pos, int val) {
while (pos <= k) {
if (bit[pos] == 0) used.push_back(pos);
bit[pos] += val;
pos += lowbit(pos);
}
}
int sum(int pos) {
int res = 0;
while (pos > 0) {
res += bit[pos];
pos -= lowbit(pos);
}
return res;
}
void clearBIT() {
for (int pos : used) bit[pos] = 0;
used.clear();
}
struct Element {
int a, b, c;
} e[MAXN];
struct Node {
int a, b, c;
int cnt;
int ans;
} node[MAXN];
int m;
struct TempNode {
int b, c, cnt;
int idx;
bool isLeft;
};
bool cmpElement(const Element &x, const Element &y) {
if (x.a != y.a) return x.a < y.a;
if (x.b != y.b) return x.b < y.b;
return x.c < y.c;
}
void build() {
sort(e + 1, e + 1 + n, cmpElement);
m = 0;
for (int i = 1; i <= n; ++i) {
if (i > 1 && e[i].a == e[i-1].a && e[i].b == e[i-1].b && e[i].c == e[i-1].c) {
node[m].cnt++;
} else {
node[++m] = {e[i].a, e[i].b, e[i].c, 1, 0};
}
}
}
void cdq(int l, int r) {
if (l == r) return;
int mid = (l + r) >> 1;
cdq(l, mid);
cdq(mid + 1, r);
vector<TempNode> tmp;
for (int i = l; i <= r; ++i) {
tmp.push_back({node[i].b, node[i].c, node[i].cnt, i, i <= mid});
}
sort(tmp.begin(), tmp.end(), [](const TempNode &x, const TempNode &y) {
if (x.b != y.b) return x.b < y.b;
return x.idx < y.idx;
});
for (auto &p : tmp) {
if (p.isLeft) {
add(p.c, p.cnt);
} else {
node[p.idx].ans += sum(p.c);
}
}
clearBIT();
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> e[i].a >> e[i].b >> e[i].c;
}
build();
cdq(1, m);
for (int i = 1; i <= m; ++i) {
int fval = node[i].ans + (node[i].cnt - 1);
freq[fval] += node[i].cnt;
}
for (int d = 0; d < n; ++d) {
cout << freq[d] << '\n';
}
return 0;
}
P3811【模板】模意义下的乘法逆元
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 3e6 + 10;
int n, p, inv[N];
signed main() {
cin >> n >> p;
inv[1] = 1;
cout << 1 << '\n';
for (int i = 2; i <= n; ++i) {
inv[i] = p - p / i * inv[p % i] % p;
cout << inv[i] << '\n';
}
return 0;
}
P3865【模板】ST表&RMQ问题
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int a[N] ,f[N][25] ,logn[N];
int n ,m;
class FastIO {
private:
static const int SIZE = 1 << 20;
char in[SIZE], out[SIZE], *p1, *p2, *pp;
public:
FastIO() : p1(in), p2(in), pp(out) {}
inline char getchar() {
if (p1 == p2) p2 = (p1 = in) + fread(in, 1, SIZE, stdin);
return p1 == p2 ? EOF : *p1++;
}
template<typename T>
inline void read(T &x) {
x = 0;
T f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + (ch - '0');
ch = getchar();
}
x *= f;
}
inline void write_char(char c) {
if (pp - out == SIZE) {
fwrite(out, 1, SIZE, stdout);
pp = out;
}
*pp++ = c;
}
template<typename T>
inline void write(T x) {
if (x < 0) {
write_char('-');
x = -x;
}
if (x > 9) write(x / 10);
write_char(x % 10 + '0');
}
inline void write_string(const char* s) {
while (*s) {
write_char(*s++);
}
}
~FastIO() {
fwrite(out, 1, pp - out, stdout);
}
} d;
void init(){
logn[1] = 0;
logn[2] = 1;
for (int i = 3; i <= N; ++i) {
logn[i] = logn[i / 2] + 1;
}
}
void ST_init() {
int k = logn[n];
for (int j = 1; j <= k; ++j) {
for(int i = 1; i <= n - (1 << j) + 1; ++i) {
f[i][j] = max(f[i][j - 1] ,f[i + (1 << (j - 1))][j - 1]);
}
}
}
int query(int l, int r) {
int k = logn[r - l + 1];
return max(f[l][k], f[r - (1 << k) + 1][k]);
}
int main(){
d.read(n), d.read(m);
for (int i = 1; i <= n; ++i){
d.read(a[i]);
f[i][0] = a[i];
}
init();
ST_init();
while (m--) {
int l, r;
d.read(l) ,d.read(r);
d.write(query(l, r));
d.write_char('\n');
}
return 0;
}
P3919 【模板】可持久化线段树 1(可持久化数组)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, m, tot = 0;
int a[N], Rt[N];
struct Node {
int l, r, val;
};
Node tr[N << 5];
int build(int l, int r) {
int pos = ++tot;
if (l == r) {
tr[pos].val = a[l];
return pos;
}
int mid = l + r >> 1;
tr[pos].l = build(l, mid);
tr[pos].r = build(mid + 1, r);
return pos;
}
int update(int l, int r, int pos, int new_pos, int new_val) {
tr[++tot] = tr[pos];
pos = tot;
if (l == r) {
tr[pos].val = new_val;
}
else {
int mid = l + r >> 1;
if (new_pos <= mid) tr[pos].l = update(l, mid, tr[pos].l, new_pos, new_val);
else tr[pos].r = update(mid + 1, r, tr[pos].r, new_pos, new_val);
}
return pos;
}
int query(int l, int r, int pos, int qpos) {
if (l == r) {
return tr[pos].val;
}
int mid = l + r >> 1;
if (qpos <= mid) return query(l, mid, tr[pos].l, qpos);
else return query(mid + 1, r, tr[pos].r, qpos);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
Rt[0] = build(1, n);
for (int i = 1; i <= m; ++i) {
int v, opt, p, c;
cin >> v >> opt;
if (opt == 1) {
cin >> p >> c;
Rt[i] = update(1, n, Rt[v], p, c);
}
else {
cin >> p;
cout << query(1, n, Rt[v], p) << endl;
Rt[i] = Rt[v];
}
}
return 0;
}
P4779 【模板】单源最短路径(标准版)
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 10;
int n, m, s, dis[N];
bool vis[N];
vector<pair<int, int>> G[N];
struct Node {
int v, w;
bool operator < (const Node& p) const {
return w > p.w;
}
};
priority_queue<Node> q;
void Dijkstra() {
memset(dis, 0x3f, sizeof dis);
dis[s] = 0;
q.push({s, 0});
while (!q.empty()) {
int u = q.top().v;
int d = q.top().w;
q.pop();
if (vis[u]) continue;
vis[u] = 1;
for (auto [v, w] : G[u]) {
if (dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
q.push({v, dis[v]});
}
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> s;
for (int i = 1; i <= m; ++i) {
int u, v, w;
cin >> u >> v >> w;
G[u].push_back({v, w});
}
Dijkstra();
for (int i = 1; i <= n; ++i) {
cout << dis[i] << ' ';
}
return 0;
}
P5357【模板】AC 自动机
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
struct TRIE {
int tot = 0;
int tr[N][30], a[N], b[N];
int cnt[N], cnt1[N];
int get(char x) {
return x - 'a' + 1;
}
void ins(string s, int ps) {
int x = 0, n = s.size();
for (int i = 0; i < n; ++i) {
int v = get(s[i]);
if (!tr[x][v]) {
tr[x][v] = ++tot;
}
x = tr[x][v];
}
a[ps] = x;
if (!b[x]) {
b[x] = ps;
}
}
};
struct AC {
TRIE trie;
int fail[26 * N], ind[26 * N], ans[N], cnt[N];
void build_fail () {
queue<int> q;
for (int i = 0; i <= 26; ++i) {
if (trie.tr[0][i]) {
q.push(trie.tr[0][i]);
}
}
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = 1; i <= 26; ++i) {
if (trie.tr[u][i]) {
q.push(trie.tr[u][i]);
fail[trie.tr[u][i]] = trie.tr[fail[u]][i];
ind[trie.tr[fail[u]][i]]++;
}
else {
trie.tr[u][i] = trie.tr[fail[u]][i];
}
}
}
}
void query (string s) {
int x = 0;
for (int i = 0; i < s.size(); ++i) {
x = trie.tr[x][trie.get(s[i])];
cnt[x]++;
}
for (int i = trie.tot; i >= 0; --i) {
cnt[fail[i]] += cnt[i];
}
}
} ac;
int n;
string s[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> s[i];
ac.trie.ins(s[i], i);
}
ac.build_fail();
string s1;
cin >> s1;
ac.query(s1);
for (int i = 1; i <= n; ++i) {
cout << ac.cnt[ac.trie.a[i]] << '\n';
}
return 0;
}
P5431【模板】模意义下的乘法逆元 2
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e6 + 10;
int n, p, k, a[N];
int pre[N], dpre[N], ans;
int qpow (int x, int y, int mod) {
long long res = 1, bs = x;
while (y) {
if (y & 1) (res *= bs) %= mod;
(bs *= bs) %= mod;
y >>= 1;
}
return res;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> p >> k;
pre[0] = 1;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
pre[i] = pre[i - 1] * a[i] % p;
}
dpre[n] = qpow(pre[n], p - 2, p);
for (int i = n; i >= 1; --i) {
dpre[i - 1] = dpre[i] * a[i] % p;
}
int kk = k;
for (int i = 1; i <= n; ++i) {
int t = pre[i - 1] * dpre[i] % p;
ans = (ans + t * kk) % p;
(kk *= k) %= p;
}
cout << ans << endl;
return 0;
}
P5788【模板】单调栈
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e6 + 10;
int n ,a[MAXN] ,ans[MAXN];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
stack<int> st;
for (int i = 1; i <= n; ++i) {
while (!st.empty() && a[st.top()] < a[i]) {
ans[st.top()] = i;
st.pop();
}
st.push(i);
}
for (int i = 1; i <= n; ++i) {
cout << ans[i] << ' ';
}
cout << '\n';
return 0;
}
P6091【模板】原根
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
vector<int> primes;
bool is_prime[MAXN];
int phi[MAXN];
bool has_primitive_root[MAXN];
void sieve() {
fill(is_prime, is_prime + MAXN, true);
is_prime[0] = is_prime[1] = false;
phi[1] = 1;
for (int i = 2; i < MAXN; i++) {
if (is_prime[i]) {
primes.push_back(i);
phi[i] = i - 1;
}
for (int p : primes) {
if (i * p >= MAXN) break;
is_prime[i * p] = false;
if (i % p == 0) {
phi[i * p] = phi[i] * p;
break;
} else {
phi[i * p] = phi[i] * (p - 1);
}
}
}
fill(has_primitive_root, has_primitive_root + MAXN, false);
has_primitive_root[2] = has_primitive_root[4] = true;
for (int p : primes) {
if (p == 2) continue;
long long pk = p;
while (pk < MAXN) {
has_primitive_root[pk] = true;
if (pk * 2 < MAXN) {
has_primitive_root[pk * 2] = true;
}
pk *= p;
}
}
}
long long pow_mod(long long a, long long b, long long mod) {
long long res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
vector<int> get_factors(int n) {
vector<int> factors;
int temp = n;
for (int p : primes) {
if (p * p > temp) break;
if (temp % p == 0) {
factors.push_back(p);
while (temp % p == 0) temp /= p;
}
}
if (temp > 1) factors.push_back(temp);
return factors;
}
bool is_primitive_root(int g, int n, int phi_n, const vector<int>& factors) {
if (__gcd(g, n) != 1) return false;
for (int p : factors) {
if (pow_mod(g, phi_n / p, n) == 1) {
return false;
}
}
return true;
}
int find_min_primitive_root(int n) {
int phi_n = phi[n];
vector<int> factors = get_factors(phi_n);
for (int g = 1; g < n; g++) {
if (is_primitive_root(g, n, phi_n, factors)) {
return g;
}
}
return -1;
}
vector<int> find_all_primitive_roots(int n) {
vector<int> roots;
if (!has_primitive_root[n]) {
return roots;
}
if (n == 2) {
roots.push_back(1);
return roots;
}
if (n == 4) {
roots.push_back(3);
return roots;
}
int g = find_min_primitive_root(n);
if (g == -1) return roots;
int phi_n = phi[n];
int cnt = phi[phi_n];
roots.reserve(cnt);
long long current = g;
for (int k = 1; k <= phi_n; k++) {
if (__gcd(k, phi_n) == 1) {
roots.push_back(current);
}
current = current * g % n;
}
sort(roots.begin(), roots.end());
return roots;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
sieve();
int T;
cin >> T;
while (T--) {
int n, d;
cin >> n >> d;
vector<int> roots = find_all_primitive_roots(n);
int c = roots.size();
cout << c << "\n";
if (c == 0) {
cout << "\n";
continue;
}
vector<int> output;
for (int i = d - 1; i < c; i += d) {
output.push_back(roots[i]);
}
for (int i = 0; i < (int)output.size(); i++) {
cout << output[i];
if (i < (int)output.size() - 1) cout << " ";
}
cout << "\n";
}
return 0;
}
P8306【模板】字典树
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 3e6 + 10;
const int M = 63;
int trie[N][M], cnt[N], tot;
int get(char x) {
if (x >= 'A' && x <= 'Z') {
return x - 'A';
}
else if (x >= 'a' && x <= 'z') {
return x - 'a' + 26;
}
else {
return x - '0' + 52;
}
}
void ins(string s) {
int x = 0, n = s.size();
for (int i = 0; i < n; ++i) {
int v = get(s[i]);
if (!trie[x][v]) {
trie[x][v] = ++tot;
}
x = trie[x][v];
cnt[x]++;
}
}
int fnd(string s) {
int x = 0, n = s.size();
for (int i = 0; i < n; ++i) {
int v = get(s[i]);
if (!trie[x][v]) {
return 0;
}
x = trie[x][v];
}
return cnt[x];
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
for (cin >> T; T; --T) {
int n, q;
cin >> n >> q;
for (int i = 0; i <= tot; ++i) {
memset(trie[i], 0, sizeof trie[i]);
cnt[i] = 0;
}
tot = 0;
for (int i = 1; i <= n; ++i) {
string s;
cin >> s;
ins(s);
}
while (q--) {
string s;
cin >> s;
cout << fnd(s) << '\n';
}
}
return 0;
}
P10815【模板】快速读入
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int ret = 0, f = 0;
char ch = getchar_unlocked();
while (!isdigit(ch)) {
f |= ch == '-', ch = getchar_unlocked();
}
while (isdigit(ch)) {
ret = ret * 10 + ch - '0', ch = getchar_unlocked();
}
return f ? -ret : ret;
}
int main() {
int n ,res = 0;
for (n = read() ;n ;n--) {
res += read();
}
cout << res << '\n';
}
P11615【模板】哈希表
#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
char buf[1<<23], *p1 = buf, *p2 = buf;
#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<21, stdin), p1 == p2) ? EOF : *p1++)
inline ull read_ull() {
ull x = 0;
char ch = gc();
while (!isdigit(ch)) ch = gc();
while (isdigit(ch)) {
x = x * 10 + (ch ^ 48);
ch = gc();
}
return x;
}
const int SIZE = 6e6 + 10;
const ull EMPTY = 0xFFFFFFFFFFFFFFFFULL;
struct Hash {
ull key;
ull value;
bool used;
};
class Map {
private:
Hash* table;
int capacity;
inline int hash(ull key) const {
const ull multiplier = 11400714819323198485ULL;
return (key * multiplier) % capacity;
}
public:
Map(int size) : capacity(size) {
table = new Hash[size];
for (int i = 0; i < size; ++i) {
table[i].key = EMPTY;
table[i].used = false;
table[i].value = 0;
}
}
~Map() {
delete[] table;
}
ull query(ull key, ull new_) {
int idx = hash(key);
int start_idx = idx;
while (true) {
if (!table[idx].used && table[idx].key == EMPTY) {
table[idx].key = key;
table[idx].value = new_;
table[idx].used = true;
return 0;
}
if (table[idx].used && table[idx].key == key) {
ull old_ = table[idx].value;
table[idx].value = new_;
return old_;
}
idx = (idx + 1) % capacity;
if (idx == start_idx) {
return 0;
}
}
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n = read_ull();
Map hashmap(SIZE);
ull sum = 0;
for (int i = 1; i <= n; ++i) {
ull x = read_ull();
ull y = read_ull();
ull ans = hashmap.query(x, y);
sum += (ull)i * ans;
}
cout << sum << endl;
return 0;
}
P13825【模板】线段树1.5
#include <bits/stdc++.h>
using namespace std;
#define int unsigned long long
const int N = 2e5 + 10;
int n, m, tot = 0, rt = 0;
struct Node {
int ls, rs, v, lazy;
} tr[N << 5];
int calc(int l, int r) {
return (l + r) * (r - l + 1) / 2;
}
void pushup(int p) {
tr[p].v = tr[tr[p].ls].v + tr[tr[p].rs].v;
return;
}
void pushdown(int p, int l, int r) {
if (tr[p].lazy == 0) {
return;
}
int &ls = tr[p].ls, &rs = tr[p].rs, mid = l + r >> 1;
if (!ls) ls = ++tot;
if (!rs) rs = ++tot;
tr[ls].v += 1ll * (mid - l + 1) * tr[p].lazy;
tr[ls].lazy += tr[p].lazy;
tr[rs].v += 1ll * (r - mid) * tr[p].lazy;
tr[rs].lazy += tr[p].lazy;
tr[p].lazy = 0;
return;
}
int ask(int l, int r, int x, int y, int p) {
if (l >= x && r <= y) {
return tr[p].v;
}
if (r < x || l > y) {
return 0;
}
pushdown(p, l, r);
int mid = (l + r) / 2;
return ask(l, mid, x, y, tr[p].ls) + ask(mid + 1, r, x, y, tr[p].rs);
}
void add(int l, int r, int x, int y, int v, int &p) {
if (p == 0) {
p = ++tot;
}
if (r < x || l > y) {
return;
}
if (l >= x && r <= y) {
tr[p].v += 1ll * (r - l + 1) * v;
tr[p].lazy += v;
return;
}
pushdown(p, l, r);
int mid = (l + r) >> 1;
add(l, mid, x, y, v, tr[p].ls);
add(mid + 1, r, x, y, v, tr[p].rs);
pushup(p);
}
signed main() {
cin >> n >> m;
while (m--) {
int opt, x, y, z;
cin >> opt;
if (opt == 1) {
cin >> x >> y >> z;
add(1, n, x, y, z, rt);
}
else {
cin >> x >> y;
cout << ask(1, n, x, y, rt) + calc(x, y) << '\n';
}
}
return 0;
}