Order Capital Round 1 (Codeforces Round 1038, Div. 1 + Div. 2)
E
Greedy Grid Counting
Codeforces
2600
原题
← DF →
翻译中…

A path in a grid is called greedy if it starts at the top-left cell and moves only to the right or downward, always moving to its neighbor with the greater value (or either if the values are equal).

翻译中…

The value of a path is the sum of the values of the cells it visits, including the start and end.

翻译中…

Given a partially filled $2 \times n$ grid of integers between $1$ and $k$, count the number of ways to fill the empty cells such that in every subgrid$^{\text{∗}}$, there exists a greedy path that achieves the maximum value out of all down/right paths. Since the answer may be large, calculate it modulo $998\,244\,353$.

翻译中…

$^{\text{∗}}$ A subgrid of a $2 \times n$ grid $a_{i,j}$ is a grid formed from all cells $a_{x,y}$ such that $l_x \leq x \leq r_x$, $l_y \leq y \leq r_y$ for some $1 \leq l_x \leq r_x \leq 2$, $1 \leq l_y \leq r_y \leq n$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$ and $k$ ($1 \leq n, k \leq 500$) — the number of columns and the range of integers in the grid, respectively.

翻译中…

Then two lines follow, the $i$-th line containing $n$ integers $a_{i,1}, a_{i,2}, \ldots, a_{i,n}$ ($-1 \leq a_{i,j} \leq k$, $a_{i,j} \neq 0$) — the values of cells in the $i$-th row of the grid, where $-1$ represents an empty cell.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $500$.

翻译中…

Output

翻译中…

For each test case, output a single integer — the number of ways to fill the grid that satisfy the above conditions, modulo $998\,244\,353$.

翻译中…

Note

翻译中…

In the first test case, the grids that satisfy the conditions are: $$ \begin{bmatrix} 2 & 1 & 1 & 2 \\ 2 & 1 & 1 & 3 \end{bmatrix},\: \begin{bmatrix} 2 & 1 & 1 & 2 \\ 2 & 2 & 1 & 3 \end{bmatrix},\: \begin{bmatrix} 2 & 1 & 1 & 2 \\ 2 & 3 & 1 & 3 \end{bmatrix},\: \begin{bmatrix} 2 & 1 & 2 & 2 \\ 2 & 2 & 1 & 3 \end{bmatrix},\: \begin{bmatrix} 2 & 1 & 2 & 2 \\ 2 & 3 & 1 & 3 \end{bmatrix},\: \begin{bmatrix} 2 & 1 & 3 & 2 \\ 2 & 3 & 1 & 3 \end{bmatrix}. $$

翻译中…

In the second test case, all ways to fill the grid satisfy the conditions.

翻译中…

样例

Input #1
3
4 3
2 1 -1 2
2 -1 1 3
5 4
1 3 -1 4 2
-1 3 4 2 -1
10 10
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
Output #1
6
64
123782927
InputCopy OutputCopy
题目信息
题号
2122E
时间限制
4 s
内存限制
256 MB
标签
组合数学
动态规划
贪心
数学

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