Codeforces Global Round 26
F
Reconstruction
Codeforces
2500
原题
← EG →
翻译中…

There is a hidden array $a_1, a_2, \ldots, a_n$ of length $n$ whose elements are integers between $-m$ and $m$, inclusive.

翻译中…

You are given an array $b_1, b_2, \ldots, b_n$ of length $n$ and a string $s$ of length $n$ consisting of the characters $\texttt{P}$, $\texttt{S}$, and $\texttt{?}$.

翻译中…

For each $i$ from $1$ to $n$ inclusive, we must have:

翻译中…
  • If $s_i = \texttt{P}$, $b_i$ is the sum of $a_1$ through $a_i$.
  • If $s_i = \texttt{S}$, $b_i$ is the sum of $a_i$ through $a_n$.
翻译中…

Output the number of ways to replace all $\texttt{?}$ in $s$ with either $\texttt{P}$ or $\texttt{S}$ such that there exists an array $a_1, a_2, \ldots, a_n$ with elements not exceeding $m$ by absolute value satisfying the constraints given by the array $b_1, b_2, \ldots, b_n$ and the string $s$.

翻译中…

Since the answer may be large, output it modulo $998\,244\,353$.

翻译中…

Input

翻译中…

The first line contains a single integer $t$ ($1 \leq t \leq 10^3$) — the number of test cases.

翻译中…

The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 2 \cdot 10^3$, $2 \leq m \leq 10^{9}$) — the length of the hidden array $a_1, a_2, \ldots, a_n$ and the maximum absolute value of an element $a_i$.

翻译中…

The second line of each test case contains a string $s$ of length $n$ consisting of characters $\texttt{P}$, $\texttt{S}$, and $\texttt{?}$.

翻译中…

The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($|b_i| \leq m \cdot n$).

翻译中…

The sum of $n$ over all test cases does not exceed $5 \cdot 10^3$.

翻译中…

Output

翻译中…

For each test case, output a single integer — the number of ways to replace all $\texttt{?}$ in $s$ with either $\texttt{P}$ or $\texttt{S}$ that result in the existence of a valid array $a_1, a_2, \ldots, a_n$, modulo $998\,244\,353$.

翻译中…

Note

翻译中…

In the first test case, we can see that the following array satisfies all constraints, thus the answer is $1$:

翻译中…
  1. $\texttt{P}$ — ${[\color{red}{\textbf{1}},3,4,2]}$: sum of $1$.
  2. $\texttt{S}$ — ${[1,\color{red}{\textbf{3},4,2}]}$: sum of $9$.
  3. $\texttt{P}$ — ${[\color{red}{1,3,\textbf{4}},2]}$: sum of $8$.
  4. $\texttt{P}$ — ${[\color{red}{1,3,4,\textbf{2}}]}$: sum of $10$.
翻译中…

In the second test case, it can be shown that no array $a$ with all $|a_i| \leq m = 10^9$ satisfies all constraints.

翻译中…

样例

Input #1
6
4 10
PSPP
1 9 8 10
4 1000000000
????
1 1 1 4000000000
8 1000000000
?P?SSP?P
-857095623 -1424391899 -851974476 673437144 471253851 -543483033 364945701 -178537332
4 7
PPSS
4 2 1 3
9 20
?????????
1 2 3 4 5 6 7 8 9
3 1000000000
P??
-145463248 -974068460 -1287458396
Output #1
1
0
2
1
14
1
InputCopy OutputCopy
题目信息
题号
1984F
时间限制
2 s
内存限制
256 MB
标签
暴力
动态规划
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。