|
|
简要题意给定包含 $N$ 个长度为 $M$ 的 01 串的集合 $S$,有 $Q$ 组询问,每个询问给出一个长度为 $M$ 的 01 串 $t_i$,求集合 $$\left\{u\in \mathbb{Z}_2^M\left|\exists T\subseteq S,\ \mathrm{s.t.}\ u = t_i \oplus \bigoplus_{v\in T} v\right.\right\}$$ 中,对应位为 $1$ 的下标序列的字典序最小的 01 串。 $1\le N, M, k\le 2000$ 题解字典序处理最小化字典序问题,一个经典思路是按位贪心。在本题中,由于需要最小化的串长不固定,故贪心思路为:
上述贪心等价于:
我会线性基因为涉及到异或,不难想到用线性基来处理本题。假设我们已经将 $S$ 处理成了相应的基向量。 对于每个询问,首先把所有主元清零,使得询问串最多只有自由元的位置非零。如果此时询问串变成全零串,则答案即为全零串。 否则,按主元顺序处理每个基向量。需要再次异或上一个基向量,当且仅当异或可以使询问串主元对应位置变成 $1$(也即此时询问串主元位置为 $0$)。特别地,如果异或完,主元位置之后变成全零,则答案即为当前串。 使用 bitset 处理,复杂度即为 $O(NMQ/w)$, 其中 $w$ 表示 bitset 位长。 除上述做法以外,还有各种使用线性基的处理方式,在此不详细展开。 不推荐考场实现的做法(简要版)得到线性基之后,可以考虑补充没有主元对应的标准基,这样可以得到 $\mathbb{Z}_2^M$ 的一组基 $B$。将询问在 $B$ 下分解,则答案为全零串当且仅当补充的标准基的系数为 $0$。 检查完一个 $S$ 的主元位置 $x$ 后,如果这一位之后没有非零自由元,则输出当前串;否则,为了在子空间 $\mathbb{Z}_2^{M-x}$ 中处理询问串的后 $(M-x)$ 位,需要将 $x$ 对应的基向量(去掉主元的 $1$ 之后)在该子空间中重新分解,更新系数。
题目4287 [THUPC 2025 Final] Now or Never
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
1
评论
2026-01-29 18:41:02
|
|
|
English versionFirstly, consider a classical dp to calculate $dp_{u,k}$ as the number of delete $k$ vertices in subtree $u$ without considering deleting vertex $n-i+1$ everyday. This is trivial. Then, due to the existence of an “automatic deletion” process, we cannot view the selected points within the subtree of $u$ as operations with only relative order, because they may be illegal. However, we consider the final operation sequence, putting the vertex chosen on the $i$ th day in the position $n-i+1$. That is, if we firstly choose $4$, then $3$, then $1$, it will be $0431$. Then, one vertex $u$ must be put in position $u$ or later. Then, we find that for the subtree of vertex $u$ with DFS order interval $[l,r]$, all vertex can't be put in position before $l$, and always legal to put in position after $r$ if parent-child relations are satisfied. So we consider a dp $f_{u,i,j,k}$ which represents that the subtree of $u$ leaves position $i$ for ancestors (which means nothing can be placed at position $i$ or before, and if $i=0$, then nothing is left for ancestors), there are $j$ empty spaces (not including the one left for ancestors), and $k$ vertices need to be put into positions after the subtree of $u$. In the $i=0$ case,we merge subtrees,and the subtree with smaller DFS order can put some backward operations into the space of the subtree with bigger DFS order. Specifically, consider the sons of $u$ enumerated in reverse order of DFS, and the son is $v$.Then enumerate $w$ as how many backward operations from $v$ will fill in space of $u$. The transition is $f_{u,0,j+j1-w,k+k1-w} \leftarrow f_{u,0,j+j1-w,k+k1-w}+f_{u,0,j,k}\times f_{v,0,j1,k1}\times {j \choose w}\times{k+k1-w \choose k}$. For $f_{v,i,j,k}(i\neq 0)$, it won't interfere the transition with its following siblings. And for $f_{u,i,j,k}$, just consider where to put $u$. The answer for $i$ operations is $f_{1,i,i-2,0}$. 中文版本对于树上选一个点删去一个子树方案数这个经典问题,一个常见的 dp 是记 $dp_{u,k}$ 为 $u$ 子树选 $k$ 个点方案数,转移时兄弟间用组合数将其合并,然后祖先要选就必须选在它们前面,这一部分 $O(n^2)$。 回到本题,由于存在“自动删点”的过程因此我们不能将 $u$ 子树内选的点看成只有相对顺序的操作,因为其可能不合法。但我们考虑最终的操作序列,没有的位置留空:比如样例 $1$ 中第一次选 $4$,第二次选 $3$,第三次选 $1$,那么操作序列即为 $0 4 3 1$。也就是第 $i$ 次选的点放在 $n-i+1$ 的位置上。这样,我们发现一个点 $u$ 只能填在 $u$ 及以后的位置,那么对于一个点 $u$ 的子树,其 dfs 序区间为 $[l,r]$,那么 $u$ 子树的点填到 $r$ 以后的位置一定合法,而 $u$ 留的空其前面的兄弟和祖先填进去只要没有父子关系干扰一定合法,于是我们考虑 dp。 考虑一个 dp $f_{u,i,j,k}$ 表示 $u$ 子树 $i$ 这个位置留给祖先(也就是说 $i$ 及以前不能放东西,如果 $i=0$ 就不留),有 $j$ 个空(不包含给祖先留的),有 $k$ 个点要填到 $u$ 子树以后的位置。先考虑 $i=0$ 情况。那么合并两个子树的时候就是 dfs 序更小的子树可以选一些向后的操作填到 dfs 序更大子树的空里面。然后两个子树的空合并,向后操作合并。 具体来说,考虑 $u$ 的儿子按 dfs 序倒序枚举,对于一个儿子 $v$,那么合并 $f_{u,0,j,k}$ 和 $f_{v,0,j1,k1}$ 的时候先枚举一个 $w$ 代表选多少个 $v$ 中的向后操作填到 $u$ 的 $v$ 之后的子树,转移即为 $f_{u,0,j+j1-w,k+k1-w}\leftarrow f_{u,0,j+j1-w,k+k1-w}+f_{u,0,j,k}\times f_{v,0,j1,k1}\times {j \choose w}\times{k+k1-w \choose k}$。 再考虑 $f_{v,i,j1,k1}$ ,发现它需要给祖先留空并不影响其和后面兄弟的转移,因此转移同上,只是 $v$ 留的空 $u$ 同样需要留,所以转移到 $f_{u,i,j+j1-w,k+k1-w}$。注意这里需要预处理转移系数(因为 $i$ 取任何值转移系数相同)才能达到 $O(n^5)$。 然后考虑 $f_{u,i,j,k}$ 的转移,即 $v$ 后面的子树已经留了空。那么 $v$ 子树现在不能放任何东西,但是可以用一些向后的操作填 $i$ 后面的空,这里用 $dp_{v,k}$ 去转移即可。 子树转移完后,接下来是 $u$ 自身的操作。$u$ 可以有几种选择:
最后求答案的时候,对于要求 $k$ 次操作,相当于给 $1$ 在 $n-k+1$ 的位置留了空,同时恰好只有 $n-k$ 个空(那么后面就没有空,就是合法的操作序列),输出即可。
题目4286 [THUPC 2025 Final] I’m Here
AAAAAAAAAAAAA
评论
2026-01-29 18:40:31
|
|
|
English versionFirstly, consider a classical dp to calculate $dp_{u,k}$ as the number of delete $k$ vertices in subtree $u$ without considering deleting vertex $n-i+1$ everyday. This is trivial. Then, due to the existence of an “automatic deletion” process, we cannot view the selected points within the subtree of $u$ as operations with only relative order, because they may be illegal. However, we consider the final operation sequence, putting the vertex chosen on the $i$ th day in the position $n-i+1$. That is, if we firstly choose $4$, then $3$, then $1$, it will be $0431$. Then, one vertex $u$ must be put in position $u$ or later. Then, we find that for the subtree of vertex $u$ with DFS order interval $[l,r]$, all vertex can't be put in position before $l$, and always legal to put in position after $r$ if parent-child relations are satisfied. So we consider a dp $f_{u,i,j,k}$ which represents that the subtree of $u$ leaves position $i$ for ancestors (which means nothing can be placed at position $i$ or before, and if $i=0$, then nothing is left for ancestors), there are $j$ empty spaces (not including the one left for ancestors), and $k$ vertices need to be put into positions after the subtree of $u$. In the $i=0$ case,we merge subtrees,and the subtree with smaller DFS order can put some backward operations into the space of the subtree with bigger DFS order. Specifically, consider the sons of $u$ enumerated in reverse order of DFS, and the son is $v$.Then enumerate $w$ as how many backward operations from $v$ will fill in space of $u$. The transition is $f_{u,0,j+j1-w,k+k1-w} \leftarrow f_{u,0,j+j1-w,k+k1-w}+f_{u,0,j,k}\times f_{v,0,j1,k1}\times {j \choose w}\times{k+k1-w \choose k}$. For $f_{v,i,j,k}(i\neq 0)$, it won't interfere the transition with its following siblings. And for $f_{u,i,j,k}$, just consider where to put $u$. The answer for $i$ operations is $f_{1,i,i-2,0}$. 中文版本对于树上选一个点删去一个子树方案数这个经典问题,一个常见的 dp 是记 $dp_{u,k}$ 为 $u$ 子树选 $k$ 个点方案数,转移时兄弟间用组合数将其合并,然后祖先要选就必须选在它们前面,这一部分 $O(n^2)$。 回到本题,由于存在“自动删点”的过程因此我们不能将 $u$ 子树内选的点看成只有相对顺序的操作,因为其可能不合法。但我们考虑最终的操作序列,没有的位置留空:比如样例 $1$ 中第一次选 $4$,第二次选 $3$,第三次选 $1$,那么操作序列即为 $0 4 3 1$。也就是第 $i$ 次选的点放在 $n-i+1$ 的位置上。这样,我们发现一个点 $u$ 只能填在 $u$ 及以后的位置,那么对于一个点 $u$ 的子树,其 dfs 序区间为 $[l,r]$,那么 $u$ 子树的点填到 $r$ 以后的位置一定合法,而 $u$ 留的空其前面的兄弟和祖先填进去只要没有父子关系干扰一定合法,于是我们考虑 dp。 考虑一个 dp $f_{u,i,j,k}$ 表示 $u$ 子树 $i$ 这个位置留给祖先(也就是说 $i$ 及以前不能放东西,如果 $i=0$ 就不留),有 $j$ 个空(不包含给祖先留的),有 $k$ 个点要填到 $u$ 子树以后的位置。先考虑 $i=0$ 情况。那么合并两个子树的时候就是 dfs 序更小的子树可以选一些向后的操作填到 dfs 序更大子树的空里面。然后两个子树的空合并,向后操作合并。 具体来说,考虑 $u$ 的儿子按 dfs 序倒序枚举,对于一个儿子 $v$,那么合并 $f_{u,0,j,k}$ 和 $f_{v,0,j1,k1}$ 的时候先枚举一个 $w$ 代表选多少个 $v$ 中的向后操作填到 $u$ 的 $v$ 之后的子树,转移即为 $f_{u,0,j+j1-w,k+k1-w}\leftarrow f_{u,0,j+j1-w,k+k1-w}+f_{u,0,j,k}\times f_{v,0,j1,k1}\times {j \choose w}\times{k+k1-w \choose k}$。 再考虑 $f_{v,i,j1,k1}$ ,发现它需要给祖先留空并不影响其和后面兄弟的转移,因此转移同上,只是 $v$ 留的空 $u$ 同样需要留,所以转移到 $f_{u,i,j+j1-w,k+k1-w}$。注意这里需要预处理转移系数(因为 $i$ 取任何值转移系数相同)才能达到 $O(n^5)$。 然后考虑 $f_{u,i,j,k}$ 的转移,即 $v$ 后面的子树已经留了空。那么 $v$ 子树现在不能放任何东西,但是可以用一些向后的操作填 $i$ 后面的空,这里用 $dp_{v,k}$ 去转移即可。 子树转移完后,接下来是 $u$ 自身的操作。$u$ 可以有几种选择:
最后求答案的时候,对于要求 $k$ 次操作,相当于给 $1$ 在 $n-k+1$ 的位置留了空,同时恰好只有 $n-k$ 个空(那么后面就没有空,就是合法的操作序列),输出即可。
题目4286 [THUPC 2025 Final] I’m Here
AAAAAAAAAAAAA
评论
2026-01-29 18:39:27
|
|
|
简要题意对于三个长度为 $n$ 的 现在我们有三个长度为 $n$ 的随机 $n \le 3 \times 10^5$ 解法引理:设 $\text{maj}(a,b,c)$ 为 $a,b,c$ 中的众数;对于三个长度为 $n$ 的 01 字符串 $s_1,s_2,s_3$,设 $\text{maj}(s_1,s_2,s_3)$ 为长度为 $n$ 的字符串 $t$,其中 $t_i = \text{maj}(s_{1,i},s_{2,i},s_{3,i})$,则 $f(s_1,s_2,s_3) = |\{s_1,s_2,s_3,\text{maj}(s_1,s_2,s_3)\}|$。 我们首先说明以上四个字符串均满足题设条件。$s_1,s_2,s_3$ 满足条件是显然的。 对于 $t=\text{maj}(s_1,s_2,s_3)$,对于任意 $1 \le i < j \le n$,存在下标 $1 \le a < b \le 3$ 满足 $s_{a,i} = s_{b,i} = t_i$,又存在 $1 \le c < d \le 3$ 满足 $s_{c,j} = s_{d,j} = t_j$。根据抽屉原理 $\{a,b,c,d\}$ 中有两个数相等,对应的下标即是满足题设条件的 $k$。 接下来我们说明不存在其它字符串满足条件。当满足条件的字符串 $t$ 不等于 $\text{maj}(s_1,s_2,s_3)$ 时,设 $t_i \ne \text{maj}(s_1,s_2,s_3)_i$,则 $t_i$ 只在 $s_{1,i},s_{2,i},s_{3,i}$ 中出现恰好一次(不能不出现,否则不可能满足题设条件),不妨假设其为 $s_{1,i}$。此时取任意 $j \ne i$,根据题设条件必然有 $t_j = s_{1,j}$(因为 $k$ 必须取 $1$),因此 $t = s_1$。 进一步地,可以分类讨论得到 $|\{s_1,s_2,s_3,\text{maj}(s_1,s_2,s_3)\}| = 4 - \sum_{i=1}^3 [\text{maj}(s_1,s_2,s_3) = s_i]$:
由于期望的线性性,只需要计算 $\Pr[\text{maj}(s_1,s_2,s_3) = s_i]$ 即可。由于每一位的随机事件是独立的,所以直接把每一位相等的概率乘起来就行了。 复杂度 $O(n)$。
题目4284 [THUPC 2025 Final] 好串
AAAAAAAAAAAAAAAAAAAAAA
评论
2026-01-29 18:38:58
|
|
|
题外话
Description
$k=0$
正解
题目4283 [THUPC 2025 Final] 石墨烯
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
评论
2026-01-29 18:38:16
|
|
|
English versionEach operation transform three adjacent characters into one. If the string starts with If the string ends with If an operation transformed a string ending with Consider the case where the strings contains All cases of
However, Since Another case is that the string ends with In the remaining case, there are even 0's between some pair of adjacent 1's, but the number of characters before the first 1 is odd, there must be only one pair. Suppose the string is 1-indexed. It is because the indices of such pair of 1's must be [odd, even]; while the indices in the previous case must be [even, odd]. Thus, if there are two such pairs of this case [odd, even], in the parity sequence [odd, even, ..., odd, even] there must be an [even, odd] as a substring. Therefore, any other pair of adjacent 1's must have odd 0's in between, i.e., $0(00)^*\color{blue}(10(00)^*)^*\color{red}1\color{black}(00)^*\color{red}1\color{blue}((00)^*01)^*\color{black}(00)^+$ in regular expression. Any operation remains the string in this case, except that the string is $0(00)^*\color{blue}(10(00)^*)^*\color{red}1\color{red}1\color{black}(00)^+$ and the operation is $\color{red}1\color{black}?\color{red}1\color{black}:0$ makes 1. Since it must ends with `00`, it becomes a string ending with `0` and every pair of adjacent 1's has odd 0's in between, which is a case mentioned before that has no solution. In conclusion, it has a solution if and only of at least one of the two following conditions is met:
中文版本英文题解里描述了做本题的一种思路,以及证明。 如果要直接写做法的话,是下面这样: 假设字符串下标从 $1$ 开始编号。 首先判断是否存在 $i,j$ 使得 $i$ 是奇数,$j$ 是偶数,$s_i$ 和 $s_j$ 都是
此外,还有一种比较特别的解法: 可以发现构造的过程中需要用到不超过两层括号(不算整个表达式最外面一层)。所以如果你要是猜到了这一点可以直接使用动态规划从右到左扫这个字符串,记录每一层栈中的内容。最多三层栈,每层栈最多两个比特,一共 $6^3+6^2+6=258$ 种状态。大概也能过。 不知道有没有什么奇怪的乱搞可以搜过去。不过我还是欢迎吧。
题目4282 [THUPC 2025 Final] 一个 01 串,n 次三目运算符,最后值为 1
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
评论
2026-01-29 18:37:59
|