#P10042. Flower Palette Convergence
Flower Palette Convergence
Flower Palette Convergence
You are given a palette with (n) rows and (m) columns forming an (n\times m) grid. Each cell of the grid will hold a flower. There are 3 possible colors for flowers, represented by (0,\ 1,\ 2). Some cells are already filled with a flower (with one of these colors) while the rest are empty. You need to assign a color (i.e. 0, 1, or 2) to every empty cell.
The flowers evolve over discrete time steps starting from time (0). At each time step, every cell updates its color simultaneously according to the rule below:
For a flower with current color (c), if at least one of its four adjacent neighbors (up, down, left, right) has the color (c-1 \pmod{3}) (i.e. ((c+2) \bmod 3)), then in the next time step, the flower changes its color to (c-1 \pmod{3}); otherwise, it remains (c).
A complete assignment of colors to all cells (both pre‐assigned and those you fill) is called beautiful if, after a finite number of time steps, all flowers become the same color.
For any beautiful assignment, every cell eventually reaches a point (called its stabilization time) at which its color never changes thereafter. If a cell never changes color from the start, its stabilization time is defined as (0).
Your task is to calculate two values modulo (998244353):
- The number of ways to assign colors to the empty cells such that the entire assignment is beautiful.
- The sum of the stabilization times of the flower at cell in the first row and first column, over all beautiful assignments.
Note that formulas must be written in LaTeX format. In particular, use (\bmod 3) for the color arithmetic and (998244353) for the modulus.
inputFormat
The input consists of the following:
- The first line contains two integers (n) and (m) representing the number of rows and columns of the grid respectively.
- The following (n) lines each contain (m) integers. Each integer is either (0), (1), or (2) indicating a pre-placed flower's color, or (-1) indicating an empty cell.
It is guaranteed that there is at least one test case and the number of empty cells is small enough to allow a brute-force solution.
outputFormat
Output two integers separated by a space:
- The number of beautiful assignments modulo (998244353).
- The total sum (modulo (998244353)) of the stabilization times of the flower in the top‐left cell (first row, first column) over all beautiful assignments.
sample
1 1
0
1 0