P7739 [NOI2021] 密码箱 题解

· · 题解

蒟蒻切的第一题黑,写一篇题解纪念一下

题目传送门

首先看到题目里面的式子,考虑当前序列为 a_1,a_2,\dots,a_{k-1},\frac{a}{b} 时,经过一次变换会变成什么情况:末项变成 a_{k-1}+\frac{b}{a}=\frac{a_{k-1}\cdot a+b}{a},其余不变。想了2h想出用矩阵表示这个过程:\begin{bmatrix} a \\ b \end{bmatrix} \to \begin{bmatrix} a_{k-1}\cdot a+b \\ a \end{bmatrix},用矩阵乘法来描述这个变换,设

\times\begin{bmatrix}a \\ b\end{bmatrix}=\begin{bmatrix} a_{k-1}\cdot a+b \\ a \end{bmatrix}

容易得到

\begin{bmatrix} w & x\\ y & z \end{bmatrix} = \begin{bmatrix} a_{k-1} & 1\\ 1 & 0 \end{bmatrix}

然后现在的序列就成了这样:

\begin{bmatrix} a_0 & 1\\ 1 & 0 \end{bmatrix} \times \begin{bmatrix} a_1 & 1\\ 1 & 0 \end{bmatrix} \times \begin{bmatrix} a_2 & 1\\ 1 & 0 \end{bmatrix} \times \dots \times \begin{bmatrix} a_{k} & 1\\ 1 & 0 \end{bmatrix}

然后看操作,W操作相当于

a_{k} & 1\\ 1 & 0 \end{bmatrix} $ $\to$ $\begin{bmatrix} a_{k}+1 & 1\\ 1 & 0 \end{bmatrix}$, 还是考虑用矩乘描述: $$ \begin{bmatrix} a_{k} & 1\\ 1 & 0 \end{bmatrix} \times \begin{bmatrix} w & x\\ y & z \end{bmatrix} = \begin{bmatrix} a_{k}+1 & 1\\ 1 & 0 \end{bmatrix} $$ 可以得到 $\begin{bmatrix} w & x\\ y & z \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ 1 & 1 \end{bmatrix}$,所以`W`操作相当于在序列末尾乘上一个$\begin{bmatrix} 1 & 0 \\ 1 & 1 \end{bmatrix}$。 看`E`操作,先考虑 $a_k \ne 1$ 的情况,相当于在序列末尾乘上

\begin{bmatrix} 1 & 0 \ -1 & 1 \end{bmatrix} \times \begin{bmatrix} 1 & 1 \ 1 & 0 \end{bmatrix} \times \begin{bmatrix} 1 & 1 \ 1 & 0 \end{bmatrix}

第一项矩阵是令 $a_k \to a_k-1$,后两项是在序列末尾添加两个 $1$,合并一下,相当于在序列末乘上

\begin{bmatrix} 2 & 1 \ -1 & 0 \end{bmatrix}

\begin{bmatrix} a{k-1} & 1\ 1 & 0 \end{bmatrix} \times \begin{bmatrix} 1 & 1\ 1 & 0 \end{bmatrix} \to \begin{bmatrix} a{k-1} & 1\ 1 & 0 \end{bmatrix} \times \begin{bmatrix} 1 & 0\ 1 & 1 \end{bmatrix} \times \begin{bmatrix} 1 & 1\ 1 & 0 \end{bmatrix}

\begin{bmatrix} 1 & 0\ 1 & 1 \end{bmatrix} \times \begin{bmatrix} 1 & 1\ 1 & 0 \end{bmatrix}= \begin{bmatrix} 1 & 1\ 2 & 1 \end{bmatrix}

设

\begin{bmatrix} 1 & 1\ 1 & 0 \end{bmatrix}\times \begin{bmatrix} w & x\ y & z \end{bmatrix}= \begin{bmatrix} 1 & 1\ 2 & 1 \end{bmatrix}

发现

\begin{bmatrix} w & x\ y & z \end{bmatrix}

\begin{bmatrix} 2 & 1 \ -1 & 0 \end{bmatrix}

\begin{bmatrix} 2 & 1 \ -1 & 0 \end{bmatrix}

接下来因为有一个区间翻转操作,容易想到用平衡树维护,具体细节看代码,这题的难度大部分在想前面的矩阵上面,大概下位黑 :::success[ACcode] ```cpp #include <iostream> #include <random> namespace preFile { void init() { #ifndef ONLINE_JUDGE freopen("D:\\C++\\Debug\\lg.in", "r", stdin); freopen("D:\\C++\\Debug\\lg.out", "w", stdout); #endif std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); } } std::mt19937 rnd(std::random_device{}()); const int maxn = 2e5 + 3; const int mod = 998244353; int n, q, cnt, rt; std::string s; char flip(char c) { return (c == 'W' ? 'E' : 'W'); } struct Mat { int a[2][2]; Mat() { a[0][0] = 1, a[1][1] = 1, a[0][1] = a[1][0] = 0; } Mat(char c) { if (c == 'E') a[0][0] = 2, a[1][0] = mod - 1, a[0][1] = 1, a[1][1] = 0; else a[0][0] = 1, a[0][1] = 0, a[1][0] = 1, a[1][1] = 1; } Mat operator*(const Mat& o) const { Mat ret; ret.a[0][0] = ret.a[0][1] = ret.a[1][0] = ret.a[1][1] = 0; for (int k = 0; k <= 1; k++) for (int i = 0; i <= 1; i++) for (int j = 0; j <= 1; j++) ret.a[i][j] = (ret.a[i][j] + 1LL * a[i][k] * o.a[k][j]) % mod; return ret; } } fir('W'); struct node { Mat sum, revs, flis, frs; char tp; int siz, pri, rev, fli, ls, rs; node(char c) : sum(Mat(c)), revs(Mat(c)), flis(Mat(flip(c))), frs(Mat(flip(c))), tp(c), siz(1), pri((int)rnd()), rev(0), fli(0), ls(0), rs(0) {} node() : sum(Mat()), revs(Mat()), flis(Mat()), frs(Mat()), tp(' '), siz(0), pri((int)rnd()), rev(0), fli(0), ls(0), rs(0) {} } t[maxn]; void pushup(int u) { t[u].siz = t[t[u].ls].siz + t[t[u].rs].siz + 1; t[u].sum = t[t[u].ls].sum * Mat(t[u].tp) * t[t[u].rs].sum; t[u].revs = t[t[u].rs].revs * Mat(t[u].tp) * t[t[u].ls].revs; t[u].flis = t[t[u].ls].flis * Mat(flip(t[u].tp)) * t[t[u].rs].flis; t[u].frs = t[t[u].rs].frs * Mat(flip(t[u].tp)) * t[t[u].ls].frs; } void makefli(int u) { t[u].fli ^= 1; t[u].tp = flip(t[u].tp); std::swap(t[u].sum, t[u].flis); std::swap(t[u].revs, t[u].frs); } void makerev(int u) { t[u].rev ^= 1; std::swap(t[u].ls, t[u].rs); std::swap(t[u].sum, t[u].revs); std::swap(t[u].flis, t[u].frs); } void pushdown(int u) { if (t[u].fli) { if (t[u].ls) makefli(t[u].ls); if (t[u].rs) makefli(t[u].rs); t[u].fli = 0; } if (t[u].rev) { if (t[u].ls) makerev(t[u].ls); if (t[u].rs) makerev(t[u].rs); t[u].rev = 0; } } void split(int u, int siz, int& lp, int& rp) { if (!u) return lp = rp = 0, void(); pushdown(u); if (t[t[u].ls].siz < siz) lp = u, split(t[u].rs, siz - t[t[u].ls].siz - 1, t[u].rs, rp); else rp = u, split(t[u].ls, siz, lp, t[u].ls); pushup(u); } int merge(int u, int v) { if (!u || !v) return u | v; pushdown(u), pushdown(v); if (t[u].pri > t[v].pri) return t[u].rs = merge(t[u].rs, v), pushup(u), u; else return t[v].ls = merge(u, t[v].ls), pushup(v), v; } void append(char c) { t[++cnt] = node(c); rt = merge(rt, cnt); } void flip(int l, int r) { int lp, mid, rp; split(rt, r, lp, rp); split(lp, l - 1, lp, mid); makefli(mid); rt = merge(lp, merge(mid, rp)); } void reverse(int l, int r) { int lp, mid, rp; split(rt, r, lp, rp); split(lp, l - 1, lp, mid); makerev(mid); rt = merge(lp, merge(mid, rp)); } void printans() { Mat tmp = fir * t[rt].sum; std::cout << tmp.a[0][0] << ' ' << tmp.a[1][0] << '\n'; } int main() { preFile::init(); std::cin >> n >> q; for (int i = 1; i <= n; i++) { char c; std::cin >> c; append(c); } printans(); while (q--) { std::string op; std::cin >> op; if (op[0] == 'A') { char c; std::cin >> c; append(c); } else if (op[0] == 'R') { int l, r; std::cin >> l >> r; reverse(l, r); } else { int l, r; std::cin >> l >> r; flip(l, r); } printans(); } return 0; } ``` :::