2021CCPC网络赛
Pro. ID | Problem Title | Solved |
---|---|---|
7100 | Cut The Wire | (赛时) |
7101 | Time-division Multiplexing | (赛时) |
7102 | Pattern Recognition | (赛后) |
7103 | Depth First Search | (赛后) |
7104 | Easy Math Problem | (赛后) |
7105 | Power Sum | (赛时) |
7106 | Function | (赛时) |
7107 | GCD on Sequence | (赛时) |
7108 | Command Sequence | (赛时) |
7109 | Random Walk | (赛后) |
7110 | Shooting Bricks | (赛时) |
7111 | Remove | (赛时) |
7112 | Start Dash ! ! |
Cut The Wire
Time-division Multiplexing
数据较水,1.1 倍过了。
// Author: HolyK
// Created: Sat Aug 28 13:37:05 2021
#include <bits/stdc++.h>
template <class T, class U>
inline bool smin(T &x, const U &y) {
return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
return x < y ? x = y, 1 : 0;
}
using LL = long long;
using PII = std::pair<int, int>;
constexpr int N(105);
char s[N * 12], *p[N], *e[N], t[10000005];
int v[26];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int _;
std::cin >> _;
while (_--) {
int n;
std::cin >> n;
int len = 1;
[0] = e[0] = s;
pfor (int i = 1; i <= n; i++) {
[i] = e[i - 1];
pstd::cin >> p[i];
[i] = p[i] + strlen(p[i]);
eint l = e[i] - p[i];
= len * l / std::__gcd(len, l);
len }
*= 1.1;
len char *o;
(v, 0, sizeof v);
memsetfor (o = s; o < e[n]; o++) v[*o - 'a'] = 1;
int tot = std::accumulate(v, v + 26, 0);
= t;
o for (int i = 0; i < len; i++) {
for (int j = 1; j <= n; j++) {
*o++ = *p[j]++;
if (p[j] == e[j]) p[j] = e[j - 1];
}
}
int ans = o - t, cnt = 0;
(v, 0, sizeof v);
memsetfor (char *i = t, *j = t; i < o; i++) {
if (1 == ++v[*i - 'a']) cnt++;
while (cnt == tot) {
(ans, i - j + 1);
sminif (0 == --v[*j++ - 'a']) cnt--;
}
}
std::cout << ans << "\n";
}
return 0;
}
Pattern Recognition
和多校题差不多,只不过换成了矩阵,只要建出广义 sam 即可。
数据太水,sam 点数太少,爆踩 std。
// Author: HolyK
// Created: Sun Aug 29 20:49:05 2021
#include <bits/stdc++.h>
template <class T, class U>
inline bool smin(T &x, const U &y) {
return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
return x < y ? x = y, 1 : 0;
}
using LL = long long;
using PII = std::pair<int, int>;
constexpr int N(4e5 + 5);
struct Sam {
std::array<int, 26> ch[N];
int len[N], fa[N], cnt;
void init() {
[0] = -1;
fa[0] = 0;
lenfor (int i = 0; i <= cnt; i++) {
[i].fill(0);
ch}
= 0;
cnt }
int ins(int last, int x) {
if (ch[last][x] && len[last] + 1 == len[ch[last][x]]) return ch[last][x];
int p = last, np = ++cnt, q;
[np] = len[p] + 1;
lenfor (; ~p && !ch[p][x]; p = fa[p]) ch[p][x] = np;
if (p == -1) {
[np] = 0;
fa} else if (len[q = ch[p][x]] == len[p] + 1) {
[np] = q;
fa} else {
int nq = p == last ? np : ++cnt;
[nq] = len[p] + 1;
len[nq] = ch[q];
chfor (; ~p && ch[p][x] == q; p = fa[p]) ch[p][x] = nq;
[np] = nq;
fa[nq] = fa[q];
fa[q] = nq;
fa}
return np;
}
} a, b;
int n, m, q;
struct Node {
*ls, *rs;
Node int w;
} t[N * 50], *cur;
void ins(Node *&o, int l, int r, int x) {
if (!o) o = cur++, *o = {nullptr, nullptr, 0};
->w++;
oif (l == r) return;
int m = l + r >> 1;
<= m ? ins(o->ls, l, m, x) : ins(o->rs, m + 1, r, x);
x }
int ask(Node *o, int l, int r, int x, int y) {
if (!o || r < x || y < l) return 0;
if (x <= l && r <= y) return o->w;
int m = l + r >> 1;
return ask(o->ls, l, m, x, y) + ask(o->rs, m + 1, r, x, y);
}
*merge(Node *x, Node *y) {
Node if (!x) return y;
if (!y) return x;
*o = cur++;
Node ->w = x->w + y->w;
o->ls = merge(x->ls, y->ls);
o->rs = merge(x->rs, y->rs);
oreturn o;
}
*root[N];
Node std::vector<int> g[N];
int in[N], out[N], cnt;
void dfs1(int x) {
[x] = ++cnt;
infor (int y : g[x]) {
(y);
dfs1}
[x] = cnt;
out}
void dfs2(int x) {
for (int y : g[x]) {
(y);
dfs2[x] = merge(root[x], root[y]);
root}
}
void solve() {
= t;
cur std::cin >> n >> m >> q;
std::vector<std::string> s(n);
std::vector<int> pos1(n * m), pos2(n * m), pos3(n * m), pos4(n * m);
.init(), b.init();
afor (int i = 0; i < n; i++) {
std::cin >> s[i];
for (int j = 0, p = 0; j < m; j++) {
[i * m + j] = p = a.ins(p, s[i][j] - 'a');
pos1}
for (int j = m - 1, p = 0; j >= 0; j--) {
[i * m + j] = p = a.ins(p, s[i][j] - 'a');
pos2}
}
for (int j = 0; j < m; j++) {
for (int i = 0, p = 0; i < n; i++) {
[i * m + j] = p = b.ins(p, s[i][j] - 'a');
pos3}
for (int i = n - 1, p = 0; i >= 0; i--) {
[i * m + j] = p = b.ins(p, s[i][j] - 'a');
pos4}
}
for (int i = 0; i <= a.cnt; i++) {
[i].clear();
g}
for (int i = 1; i <= a.cnt; i++) {
[a.fa[i]].push_back(i);
g}
= -1, dfs1(0);
cnt for (int i = 0; i <= b.cnt; i++) {
[i].clear();
g[i] = 0;
root}
for (int i = 0; i < n * m; i++) {
int x = in[pos1[i]], y = in[pos2[i]];
assert(x && y);
(root[pos3[i]], 1, cnt, x);
ins(root[pos3[i]], 1, cnt, y);
ins(root[pos4[i]], 1, cnt, x);
ins(root[pos4[i]], 1, cnt, y);
ins}
for (int i = 1; i <= b.cnt; i++) {
[b.fa[i]].push_back(i);
g}
(0);
dfs2while (q--) {
std::string s;
std::cin >> s;
= s.size();
n .assign(n, 0);
pos1for (int i = 0, o = 0; i < n; i++) {
[i] = o = a.ch[o][s[i] - 'a'];
pos1if (!o) break;
}
.assign(n, 0);
pos2for (int i = n - 1, o = 0; i >= 0; i--) {
[i] = o = a.ch[o][s[i] - 'a'];
pos2if (!o) break;
}
.assign(n, 0);
pos3for (int i = 0, o = 0; i < n; i++) {
[i] = o = b.ch[o][s[i] - 'a'];
pos3if (!o) break;
}
.assign(n, 0);
pos4for (int i = n - 1, o = 0; i >= 0; i--) {
[i] = o = b.ch[o][s[i] - 'a'];
pos4if (!o) break;
}
int ans = 0, ans1 = 0;
if (n == 1 && pos1[0] && pos4[0]) {
= ask(root[pos4[0]], 1, cnt, in[pos1[0]], out[pos1[0]]);
ans1 }
for (int i = 0; i + 1 < n; i++) {
if (pos1[i] && pos4[i]) {
(i ? ans : ans1) += ask(root[pos4[i]], 1, cnt, in[pos1[i]], out[pos1[i]]);
}
if (pos2[i] && pos3[i]) {
(i ? ans : ans1) += ask(root[pos3[i]], 1, cnt, in[pos2[i]], out[pos2[i]]);
}
}
+= ans1 / 2;
ans auto t = s;
std::reverse(t.begin(), t.end());
if (t == s) ans /= 2;
std::cout << ans << "\n";
}
}
int main() {
// freopen("t.in", "r", stdin);
// freopen(".out", "w", stdout);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
();
solve}
return 0;
}
Depth First Search
拆点,LCT。
// Author: HolyK
// Created: Sun Aug 29 16:57:07 2021
#include <bits/stdc++.h>
template <class T, class U>
inline bool smin(T &x, const U &y) {
return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
return x < y ? x = y, 1 : 0;
}
using LL = long long;
using PII = std::pair<int, int>;
inline char gc() {
static constexpr int BufferSize = 1 << 22 | 5;
static char buf[BufferSize], *p, *q;
static std::streambuf *i = std::cin.rdbuf();
return p == q ? p = buf, q = p + i->sgetn(p, BufferSize), p == q ? EOF : *p++ : *p++;
}
struct Reader {
template <class T>
&operator>>(T &w) {
Reader char c, p = 0;
for (; !std::isdigit(c = gc());) if (c == '-') p = 1;
for (w = c & 15; std::isdigit(c = gc()); w = w * 10 + (c & 15)) ;
if (p) w = -w;
return *this;
}
} cin;
constexpr int N(4e5 + 5);
struct Node {
*ch[2], *fa;
Node #define ls ch[0]
#define rs ch[1]
int dir() const { return this == fa->rs; }
bool nrt() const { return fa && (this == fa->ls || this == fa->rs); }
void sch(int d, Node *x) {
[d] = x;
chif (x) x->fa = this;
}
void rotate() {
*p = fa;
Node int k = dir();
= p->fa;
fa if (p->nrt()) fa->ch[p->dir()] = this;
->sch(k, ch[!k]), sch(!k, p);
p->pushup();
p// pushup();
}
void splay() {
for (; nrt(); rotate()) {
if (fa->nrt()) {
(dir() == fa->dir() ? fa : this)->rotate();
}
}
();
pushup}
void access() {
for (splay(), rs = nullptr; fa; rotate()) {
->splay(), fa->rs = this;
fa}
();
pushup}
void link(Node *p) {
->access();
p= p;
fa }
void cut() {
();
accessif (ls) ls->fa = nullptr, ls = nullptr, pushup();
}
;
LL sumint max, val, id;
void pushup() {
= max = val;
sum if (ls) sum += ls->sum, smax(max, ls->max);
if (rs) sum += rs->sum, smax(max, rs->max);
}
} t[N * 2], *cur;
*newNode(int id, int val) {
Node ->ls = cur->rs = cur->fa = 0;
cur->sum = cur->max = cur->val = val;
cur->id = id;
curreturn cur++;
}
std::list<int> bfs;
std::list<int>::iterator iter[N];
*r[N * 2][2];
Node int dep[N];
int expose(int x, int d) {
*o = r[x][0], *p = o;
Node ->access();
oint res = 0;
while (o) {
= o;
p if (dep[o->id] - dep[x] == d) {
= o->id;
res }
if (dep[o->id] - dep[x] < d) {
= o->ls;
o } else {
= o->rs;
o }
}
assert(dep[res] - dep[x] == d);
->splay();
preturn res;
}
int son[N], fa[N];
void solve() {
= t;
cur .clear();
bfs[1] = bfs.insert(bfs.end(), 1);
iter[1][0] = newNode(1, 1);
r[1][1] = newNode(1, 0);
rint q;
>> q;
cin for (int i = q + 2, p = 1; i <= q + q + 1; i++) {
[p] = 1, fa[i] = p;
son[i] = dep[p] + 1;
dep[i][0] = newNode(i, i);
r[i][1] = newNode(i, 0);
r[p][0]->link(r[i][0]);
r[p][1]->link(r[i][0]);
r[i] = bfs.insert(bfs.end(), i);
iter= i;
p }
[q + q + 1] = 0;
sonint key = 0;
for (int t = 1; t <= q; t++) {
auto addLeaf = [&](int x) {
assert(!son[x]);
int y;
if (dep[y = *std::prev(iter[x])] == dep[x] && !son[y]) {
[y][0]->cut();
r[y][1]->cut();
r[y][0]->link(r[x][1]);
r[y][1]->link(r[x][1]);
r}
if (dep[y = *std::next(iter[x])] == dep[x]) {
[x][0]->link(r[y][1]);
r[x][1]->link(r[y][1]);
r}
};
int opt, x, y, z;
>> opt >> x;
cin ^= key;
x if (opt == 1) {
>> y >> z;
cin ^= key;
y ^= key;
z [x] = dep[y] + 1;
dep[x] = y;
fa[x] = 0, son[y]++;
son[x][0] = newNode(x, x);
r[x][1] = newNode(x, 0);
rif (!z) {
= expose(y, 1);
z [y][0]->cut();
r[y][1]->cut();
r[y][0]->link(r[x][0]);
r[y][1]->link(r[x][0]);
r} else {
= *std::next(iter[z]);
z }
[x] = bfs.insert(iter[z], x);
iter(x);
addLeaf} else if (opt == 2) {
[x][0]->cut();
r[x][1]->cut();
r= *std::prev(iter[x]);
y = *std::next(iter[x]);
z .erase(iter[x]);
bfsif (dep[x] == dep[y] && !son[y]) {
[y][0]->cut();
r[y][1]->cut();
rif (dep[y] == dep[z]) {
[y][0]->link(r[z][1]);
r[y][1]->link(r[z][1]);
r}
}
[fa[x]]--;
sonif (fa[x] != fa[y]) {
= fa[x];
y [y][0]->cut();
r[y][1]->cut();
rif (y == fa[z]) {
[y][0]->link(r[z][0]);
r[y][1]->link(r[z][0]);
r} else {
(y);
addLeaf}
}
} else {
>> z;
cin ^= key;
z ;
LL aint b;
if (!z) {
= b = x;
a } else {
= expose(x, z);
y *o = r[y][0];
Node ->splay();
oassert(o->rs);
= o->rs->sum + y;
a = std::max(o->rs->max, y);
b }
= a % b;
key std::cout << a << " " << b << "\n";
}
}
}
int main() {
// freopen("t.in", "r", stdin);
// freopen(".out", "w", stdout);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
>> t;
cin while (t--) {
();
solve}
return 0;
}
Easy Math Problem
// Author: HolyK
// Created: Sat Aug 28 21:36:36 2021
#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
#include <utility>
template <class T, class U>
inline bool smin(T &x, const U &y) {
return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
return x < y ? x = y, 1 : 0;
}
using LL = long long;
using PII = std::pair<int, int>;
constexpr int P(998244353);
inline void inc(int &x, int y) {
+= y;
x if (x >= P) x -= P;
}
inline void dec(int &x, int y) {
-= y;
x if (x < 0) x += P;
}
inline int mod(LL x) { return x % P; }
int fpow(int x, int k = P - 2) {
int r = 1;
for (; k; k >>= 1, x = 1LL * x * x % P) {
if (k & 1) r = 1LL * r * x % P;
}
return r;
}
struct Z {
int x;
(int v = 0) : x(v < 0 ? v + P : v >= P ? v - P : v) {}
Z(LL v) : x((v %= P) < 0 ? v + P : v) {}
Zexplicit operator bool() { return !!x; }
() const { return Z(fpow(x)); }
Z inv(int k) const { return Z(fpow(x, k)); }
Z powoperator-() const { return Z(P - x); }
Z &operator+=(const Z &r) { return inc(x, r.x), *this; }
Z &operator-=(const Z &r) { return dec(x, r.x), *this; }
Z &operator*=(const Z &r) { return x = LL(x) * r.x % P, *this; }
Z &operator/=(const Z &r) { return x = LL(x) * fpow(r.x) % P, *this; }
Z inline friend Z operator+(const Z &a, const Z &b) { return Z(a) += b; }
inline friend Z operator-(const Z &a, const Z &b) { return Z(a) -= b; }
inline friend Z operator*(const Z &a, const Z &b) { return Z(a) *= b; }
inline friend Z operator/(const Z &a, const Z &b) { return Z(a) /= b; }
inline friend std::ostream &operator<<(std::ostream &os, const Z &r) {
return os << r.x;
}
};
using Matrix = std::array<Z, 4>;
operator*(const Matrix &a, const Z &b) {
Matrix return {a[0] * b, a[1] * b, a[2] * b, a[3] * b};
}
operator+(const Matrix &a, const Matrix &b) {
Matrix return {a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3]};
}
operator-(const Matrix &a, const Matrix &b) {
Matrix return {a[0] - b[0], a[1] - b[1], a[2] - b[2], a[3] - b[3]};
}
operator*(const Matrix &a, const Matrix &b) {
Matrix return {a[0] * b[0] + a[1] * b[2], a[0] * b[1] + a[1] * b[3],
[2] * b[0] + a[3] * b[2], a[2] * b[1] + a[3] * b[3]};
a}
constexpr int N(2e5 + 5);
[N], ifac[N], inv[N];
Z facvoid init(int n) {
[0] = ifac[0] = 1;
facfor (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i;
[n] = fac[n].inv();
ifacfor (int i = n; i; i--) ifac[i - 1] = ifac[i] * i;
[1] = 1;
invfor (int i = 2; i <= n; i++) inv[i] = inv[P % i] * (P - P / i);
}
[N], g[N], h[N];
Matrix fint main() {
("t.in", "r", stdin);
freopen// freopen(".out", "w", stdout);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
(2e5);
initint t;
std::cin >> t;
[0] = h[0] = {1, 0, 0, 1};
gwhile (t--) {
int n;
, b, c, d, e, ans;
Z astd::cin >> n >> a.x >> b.x >> c.x >> d.x >> e.x;
= {b, 1, c, 0}, bb = {d, 1, e, 0};
Matrix aa
for (int i = 1; i <= 2 * n; i++) {
if (i <= n) {
[i] = g[i - 1] * aa;
g[i] = h[i - 1] * bb;
h} else {
[i] = h[i] = {0, 0, 0, 0};
g}
}
auto pa = g[n] * aa, pb = h[n] * bb;
for (int i = 1; i <= n; i++) {
[i] = g[i] * ifac[i];
g[i] = h[i] * ifac[i];
h}
[2] = g[1] * h[1];
ffor (int i = 2; i < 2 * n; i++) {
[i + 1] = aa * f[i] + f[i] * bb ;
fif (i > n) {
[i + 1] = f[i + 1] - (pa * h[i - n] + g[i - n] * pb) * ifac[n];
f} else {
[i + 1] = f[i + 1] + aa * h[i] + g[i] * bb;
f}
[i + 1] = f[i + 1] * inv[i + 1];
f}
/= c;
a for (int i = 2; i <= 2 * n; i++) {
[i] = f[i] * fac[i];
f+= f[i][2] * a;
ans }
std::cout << ans << "\n";
}
return 0;
}
Power Sum
// Author: HolyK
// Created: Sat Aug 28 12:20:19 2021
#include <bits/stdc++.h>
template <class T, class U>
inline bool smin(T &x, const U &y) {
return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
return x < y ? x = y, 1 : 0;
}
using LL = long long;
using PII = std::pair<int, int>;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
auto out = [&](int n) {
for (int i = 1; i <= n; i += 4) {
std::cout << "1001";
}
std::cout << "\n";
};
int t;
std::cin >> t;
while (t--) {
int n;
std::cin >> n;
if (n % 4 == 0) {
std::cout << n << "\n";
(n);
out} else if (n % 4 == 1) {
std::cout << n << "\n";
std::cout << "1";
(n - 1);
out} else if (n % 4 == 2) {
std::cout << n + 2 << "\n";
std::cout << "0001"; // 2
(n - 2);
out} else if (n % 4 == 3) {
std::cout << n + 2 << "\n";
std::cout << "0";
(n + 1);
out}
}
return 0;
}
Function
// Author: HolyK
// Created: Sat Aug 28 13:08:51 2021
#include <bits/stdc++.h>
template <class T, class U>
inline bool smin(T &x, const U &y) {
return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
return x < y ? x = y, 1 : 0;
}
using LL = long long;
using PII = std::pair<int, int>;
int cal(int x) {
int r = 0;
for (; x; x /= 10) r += x % 10;
return r;
}
std::vector<int> s[60];
(LL a, LL b, LL x) {
LL calreturn (a * x + b) * x;
}
;
LL n(std::vector<int> &s, LL a, LL b) {
LL getMin= LLONG_MAX;
LL r if (s.empty()) return r;
if (s[0] > n) return r;
(r, cal(a, b, s[0]));
sminauto end = std::upper_bound(s.begin(), s.end(), n);
(r, cal(a, b, *std::prev(end)));
sminif (a > 0) {
= -b / (2 * a);
LL u auto it = std::lower_bound(s.begin(), end, u);
if (it < end) smin(r, cal(a, b, *it));
if (it != s.begin() && *--it <= n) smin(r, cal(a, b, *it));
}
return r;
}
// std::mt19937_64 rng(std::chrono::high_resolution_clock::now().time_since_epoch().count());
// LL rnd(LL l, LL r) {
// if (l > r) std::swap(l, r);
// return l + rng() % (r - l + 1);
// }
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
for (int i = 1; i <= 1e6; i++) {
[cal(i)].push_back(i);
s}
int t;
std::cin >> t;
// t = 100000;
while (t--) {
, b, c, d;
LL astd::cin >> a >> b >> c >> d >> n;
// a = rnd(-1000, 1000);
// b = rnd(-1e6, 1e6);
// c = rnd(-1e6, 1e6);
// d = rnd(-1e6, 1e6);
// n = rnd(1, 1e3);
= LLONG_MAX;
LL r for (int i = 1; i <= 54; i++) {
(r, getMin(s[i], a * i + b, i * (c * i + d)));
smin}
std::cout << r << "\n";
// LL rr = LLONG_MAX;
// for (int i = 1; i <= n; i++) {
// int j = cal(i);
// smin(rr, cal(a * j + b, j * (c * j + d), i));
// }
// if (rr != r) {
// std::cerr << a << " " << b << " " << c << " " << d << " " << n << "\n";
// break;
// }
// std::cerr << "ac\n";
}
return 0;
}
GCD on Sequence
// Author: HolyK
// Created: Sat Aug 28 15:57:56 2021
#include <bits/stdc++.h>
template <class T, class U>
inline bool smin(T &x, const U &y) {
return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
return x < y ? x = y, 1 : 0;
}
inline char gc() {
static constexpr int BufferSize = 1 << 22 | 5;
static char buf[BufferSize], *p, *q;
static std::streambuf *i = std::cin.rdbuf();
return p == q ? p = buf, q = p + i->sgetn(p, BufferSize), p == q ? EOF : *p++ : *p++;
}
struct Reader {
template <class T>
&operator>>(T &w) {
Reader char c, p = 0;
for (; !std::isdigit(c = gc());) if (c == '-') p = 1;
for (w = c & 15; std::isdigit(c = gc()); w = w * 10 + (c & 15)) ;
if (p) w = -w;
return *this;
}
} cin;
using LL = long long;
using PII = std::pair<int, int>;
constexpr int N(1e5 + 6);
std::vector<int> fac[N];
void init(int n) {
for (int i = n; i >= 1; i--) {
for (int j = i; j <= n; j += i) {
[j].push_back(i);
fac}
}
}
int n, a[N], pos[N];
#define ls o << 1
#define rs o << 1 | 1
[N << 2], ans[N];
LL sumint min[N << 2], max[N << 2];
[N << 2];
PII tagvoid modify(int o, int l, int r, PII x) {
[o] = x;
tag[o] = (r - l + 1LL) * x.second;
sum[o] = min[o] = x.first;
max}
void pushup(int o) {
[o] = std::min(min[ls], min[rs]);
min[o] = std::max(max[ls], max[rs]);
max[o] = sum[ls] + sum[rs];
sum}
void pushdown(int o, int l, int r) {
if (tag[o].first) {
int m = l + r >> 1;
(ls, l, m, tag[o]);
modify(rs, m + 1, r, tag[o]);
modify[o].first = 0;
tag}
}
void update(int o, int l, int r, int x, int y, int z, int i) {
if (min[o] >= z) return;
if (x <= l && r <= y && max[o] == min[o]) {
[max[o]] += i * (r - l + 1LL) - sum[o];
ans(o, l, r, {z, i});
modifyreturn;
}
(o, l, r);
pushdownint m = l + r >> 1;
if (x <= m) update(ls, l, m, x, y, z, i);
if (y > m) update(rs, m + 1, r, x, y, z, i);
(o);
pushup}
void solve() {
>> n;
cin (pos, 0, (n + 1) * sizeof(int));
memset(ans, 0, (n + 1) * sizeof(LL));
memset(sum, 0, (n + 1) * 4 * sizeof(LL));
memset(min, 0, (n + 1) * 4 * sizeof(int));
memset(max, 0, (n + 1) * 4 * sizeof(int));
memset(tag, 0, (n + 1) * 4 * sizeof(PII));
memsetfor (int i = 1; i <= n; i++) {
>> a[i];
cin std::vector<int> s;
for (int j : fac[a[i]]) {
if (pos[j]) {
if (s.empty() || pos[s.back()] < pos[j]) s.push_back(j);
}
}
int last = 0;
for (int x : s) {
int j = pos[x];
(1, 1, n, last + 1, j, x, i);
update= j;
last }
for (int j : fac[a[i]]) {
[j] = i;
pos}
}
(1, 1, n, 1, n, n + 1, n + 1);
updatefor (int i = 1; i <= n; i++) {
std::cout << ans[i] << "\n";
}
}
int main() {
// freopen("t.in", "r", stdin);
// freopen(".out", "w", stdout);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
(1e5);
initint t;
>> t;
cin while (t--) {
();
solve}
return 0;
}
Command Sequence
// Author: HolyK
// Created: Sat Aug 28 12:08:46 2021
#include <bits/stdc++.h>
template <class T, class U>
inline bool smin(T &x, const U &y) {
return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
return x < y ? x = y, 1 : 0;
}
using LL = long long;
using PII = std::pair<int, int>;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
int n;
std::string s;
std::cin >> n >> s;
std::map<PII, int> mp;
[{0, 0}] = 1;
mpint x = 0, y = 0;
= 0;
LL ans for (char c : s) {
if (c == 'U') {
++;
x} else if (c == 'D') {
--;
x} else if (c == 'L') {
++;
y} else {
--;
y}
auto &v = mp[{x, y}];
+= v;
ans ++;
v}
std::cout << ans << "\n";
}
return 0;
}
Random Walk
// Author: HolyK
// Created: Mon Aug 30 17:52:12 2021
#include <bits/stdc++.h>
template <class T, class U>
inline bool smin(T &x, const U &y) {
return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
return x < y ? x = y, 1 : 0;
}
using LL = long long;
using PII = std::pair<int, int>;
constexpr int P(998244353);
inline void inc(int &x, int y) {
+= y;
x if (x >= P) x -= P;
}
inline void dec(int &x, int y) {
-= y;
x if (x < 0) x += P;
}
inline int mod(LL x) { return x % P; }
int fpow(int x, int k = P - 2) {
int r = 1;
for (; k; k >>= 1, x = 1LL * x * x % P) {
if (k & 1) r = 1LL * r * x % P;
}
return r;
}
struct Z {
int x;
() : x(0) {}
Z(int v) : x(v < 0 ? v + P : v >= P ? v - P : v) {}
Z(LL v) : x((v %= P) < 0 ? v + P : v) {}
Zexplicit operator bool() { return !!x; }
() const { return Z(fpow(x)); }
Z inv(int k) const { return Z(fpow(x, k)); }
Z powoperator-() const { return Z(P - x); }
Z &operator+=(const Z &r) { return inc(x, r.x), *this; }
Z &operator-=(const Z &r) { return dec(x, r.x), *this; }
Z &operator*=(const Z &r) { return x = LL(x) * r.x % P, *this; }
Z &operator/=(const Z &r) { return x = LL(x) * fpow(r.x) % P, *this; }
Z inline friend Z operator+(const Z &a, const Z &b) { return Z(a) += b; }
inline friend Z operator-(const Z &a, const Z &b) { return Z(a) -= b; }
inline friend Z operator*(const Z &a, const Z &b) { return Z(a) *= b; }
inline friend Z operator/(const Z &a, const Z &b) { return Z(a) /= b; }
inline friend std::ostream &operator<<(std::ostream &os, const Z &r) {
return os << r.x;
}
};
inline char gc() {
static constexpr int BufferSize = 1 << 22 | 5;
static char buf[BufferSize], *p, *q;
static std::streambuf *i = std::cin.rdbuf();
return p == q ? p = buf, q = p + i->sgetn(p, BufferSize), p == q ? EOF : *p++ : *p++;
}
struct Reader {
template <class T>
&operator>>(T &w) {
Reader char c, p = 0;
for (; !std::isdigit(c = gc());) if (c == '-') p = 1;
for (w = c & 15; std::isdigit(c = gc()); w = w * 10 + (c & 15)) ;
if (p) w = -w;
return *this;
}
} cin;
constexpr int N(505);
[N][N], ip[N][N * 2], w[N], ideg[N], a[9][N];
Z p
int deg[N], id[N][N];
struct Edge {
int x, y, a, b;
} e[10005];
[10005];
Z sint main() {
// freopen("t.in", "r", stdin);
// freopen(".out", "w", stdout);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, m, q;
>> n >> m >> q;
cin for (int i = 1; i < n; i++) cin >> w[i].x;
for (int i = 1; i <= m; i++) {
>> e[i].x >> e[i].y >> e[i].a >> e[i].b;
cin [e[i].x]++, deg[e[i].y]++;
deg[e[i].x][e[i].y] = id[e[i].y][e[i].x] = i;
id}
for (int i = 1; i < n; i++) {
[i] = Z(deg[i]).inv();
ideg}
for (int i = 1; i <= m; i++) {
int x = e[i].x, y = e[i].y;
[x][y] = ideg[x];
p[y][x] = ideg[y];
p}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
[i][j] = -p[i][j];
ipif (i == j) {
[i][j] += 1;
ip[i][j + n + 1] = 1;
ip}
}
}
for (int i = 1, j; i <= n; i++) {
for (j = i; j <= n && !ip[j][i]; j++) ;
assert(j <= n);
if (i != j) std::swap(ip[i], ip[j]);
= ip[i][i].inv();
Z c for (int k = n * 2 + 1; k >= i; k--) ip[i][k] *= c;
for (j = 1; j <= n; j++) {
if (i == j || !ip[j][i]) continue;
= ip[j][i];
c for (int k = n * 2 + 1; k >= i; k--) {
[j][k] -= ip[i][k] * c;
ip}
}
}
auto getp = [&] {
;
Z sumfor (int i = 1; i < n; i++) sum += w[i];
= sum.inv();
Z inv for (int i = 1; i < n; i++) {
[0][i] = w[i] * inv;
p}
[0][0] = ip[0][n + 1] = 1;
ipfor (int i = 1; i <= n; i++) {
[0][i + n + 1] = 0;
ipif (!p[0][i]) continue;
for (int j = n + 1; j <= n * 2 + 1; j++) {
[0][j] += p[0][i] * ip[i][j];
ip}
}
(a, 0, sizeof a);
memset[0][0] = 1;
afor (int i = 1; i <= 7; i++) {
for (int j = 0; j <= n; j++) {
for (int k = 0; k <= n; k++) {
[i][k] += a[i - 1][j] * p[j][k];
a}
}
}
for (int j = 0; j <= n; j++) {
for (int k = 0; k <= n; k++) {
[8][k] += a[7][j] * ip[j][k + n + 1];
a}
}
};
auto cal = [&](int x, int y, int max, int min) {
;
Z resfor (int i = 1; i <= 6; i++) {
+= (a[i][x] * ideg[x] + a[i][y] * ideg[y]) * std::max(max, min);
res >>= 1;
max }
+= (a[8][x] * ideg[x] + a[8][y] * ideg[y]) * min;
res return res;
};
;
Z ans();
getpfor (int i = 1; i <= m; i++) {
+= s[i] = cal(e[i].x, e[i].y, e[i].a, e[i].b);
ans }
std::cout << ans << "\n";
while (q--) {
int opt, x, y, a, b;
>> opt >> x >> y;
cin if (opt == 1) {
int i = id[x][y];
-= s[i];
ans >> e[i].a >> e[i].b;
cin += s[i] = cal(x, y, e[i].a, e[i].b);
ans } else {
[x] = y;
w();
getp= 0;
ans for (int i = 1; i <= m; i++) {
+= s[i] = cal(e[i].x, e[i].y, e[i].a, e[i].b);
ans }
}
std::cout << ans << "\n";
}
return 0;
}
Shooting Bricks
Remove
// Author: HolyK
// Created: Sat Aug 28 14:21:04 2021
#include <bits/stdc++.h>
template <class T, class U>
inline bool smin(T &x, const U &y) {
return y < x ? x = y, 1 : 0;
}
template <class T, class U>
inline bool smax(T &x, const U &y) {
return x < y ? x = y, 1 : 0;
}
inline char gc() {
static constexpr int BufferSize = 1 << 22 | 5;
static char buf[BufferSize], *p, *q;
static std::streambuf *i = std::cin.rdbuf();
return p == q ? p = buf, q = p + i->sgetn(p, BufferSize), p == q ? EOF : *p++ : *p++;
}
struct Reader {
template <class T>
&operator>>(T &w) {
Reader char c, p = 0;
for (; !std::isdigit(c = gc());) if (c == '-') p = 1;
for (w = c & 15; std::isdigit(c = gc()); w = w * 10 + (c & 15)) ;
if (p) w = -w;
return *this;
}
} cin;
using LL = long long;
using PII = std::pair<int, int>;
constexpr int N(2e6 + 5);
int primes[N], g[N], val[N], vis[N], now, cnt;
std::bitset<N> np;
void init(int n) {
= 0;
cnt (g, 0, (n + 1) * sizeof(int));
memset.reset();
npfor (int i = 2; i <= n; i++) {
if (!np[i]) {
[++cnt] = i;
primes[i] = vis[i] == now ? i : 0;
g}
for (int j = 1; j <= cnt; j++){
if (1LL * i * primes[j] > n) break;
[i * primes[j]] = std::max(g[i], g[primes[j]]);
g[i * primes[j]] = 1;
npif (i % primes[j] == 0) break;
}
}
}
int f[N], p[N];
int main() {
("t.in", "r", stdin);
freopen// freopen("t.out", "w", stdout);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
>> t;
cin for (now = 1; now <= t; now++) {
int n, m;
>> n >> m;
cin int max = 0;
for (int i = 1; i <= m; i++) {
>> p[i];
cin [p[i]] = now;
vis(max, p[i]);
smax}
(n);
initfor (int i = 1; i < max; i++) f[i] = 1;
for (int i = max, j = 2; i <= n; i++) {
while (j < i && j + g[j] <= i) j++;
// debug
// int max = 0;
// for (int k = 1; k <= m; k++) {
// smax(max, i % p[k]);
// }
// if (i - max != j) {
// // std::cerr << i << " " << j << " " << i - max << "\n";
// // std::cerr << g[3589] << "\n";
// for (int k = 1; k <= m; k++) {
// if (max == i % p[k]) std::cerr << p[k] << "\n";
// }
// assert(false);
// }
//------
[i] = j < i ? f[j] + 1 : 1e9;
f}
uint64_t ans = 0;
for (int i = 1; i <= n; i++) {
if (f[i] >= 1e9) f[i] = 0;
// std::cout << f[i] << " \n"[i == n];
= (ans * 23333 + f[i]);
ans }
std::cout << ans << "\n";
}
// std::cerr << (double)clock() / CLOCKS_PER_SEC << "\n";
return 0;
}
Start Dash ! !
最后修改于 2021-08-31