text
stringlengths
0
801
For each test case, output a single integer — the maximum number of leaves achievable with exactly one shuffle procedure on the whole tree.
In the first test case, it can be shown that the maximum number of leaves is $4$. To accomplish this, we can start our shuffle with selecting node $3$ as the new root.
![](CDN_BASE_URL/038cb83999c75e319cd6897cdfe03b7b) Next, we are left only with one subtree, in which we can select node $2$ to be the new root of that subtree. ![](CDN_BASE_URL/85eb09dd4b3b98aa44c0450fc1904edf) This will force all $3$ remaining nodes to be l
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$.
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$.
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$.
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}},
The secret behind Oscar's first magic trick has been revealed! Because he still wants to impress Lura, he comes up with a new idea: he still wants to sort a permutation $p_1, p_2, \ldots, p_n$ of $[1, 2, \ldots, n]$.
This time, he chooses an integer $k$. He wants to sort the permutation in non-decreasing order using the following operation several times:
1. Pick a continuous subarray of length $k$ and remove it from $p$. 2. Insert the continuous subarray back into $p$ at any position (perhaps, in the very front or the very back).
To be as impressive as possible, Oscar would like to choose the maximal value of $k$ such that he can sort his permutation. Please help him find the maximal $k$ as well as a sequence of operations that will sort the permutation. You don't need to minimize the number of operations, but you are allowed to use at most $5n^2$ operations.
We have a proof that, for the maximal $k$ such that you can sort the permutation in any number of operations, you can also sort it in at most $5n^2$ operations.
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 a single integer $n$ ($5 \leq n \leq 10^3$) — the length of the permutation.
The second line of each test case contains a permutation $p_1, p_2, \ldots, p_n$ of $[1, 2, \ldots, n]$.
The sum of $n$ over all test cases does not exceed $2 \cdot 10^3$.
For each test case, first output the chosen value of $k$ on a new line ($1 \leq k \leq n$).
Then, output a single integer $m$ — the number of operations used ($0 \leq m \leq 5n^2$).
Then, on each of the next $m$ lines, output the operations denoted by two integers $i$ and $j$ ($1 \leq i, j \leq n - k + 1$), representing an operation where you remove the subarray starting from index $i$ and replace it back into $p$ at index $j$.
In the first test case, it is enough to move the last four numbers to the front.
In the second test case, it can be shown that we cannot have $k = 4$ or $k = 5$. With $k =