#abc334e. E - Christmas Color Grid 1

E - Christmas Color Grid 1

Score : 450450 points

问题描述

本问题与问题 G 的设定相似。问题描述中的差异部分以红色标出。

存在一个 HH 行、WW 列的网格,其中每个格子被涂成红色或绿色。

(i,j)(i,j) 表示从上到下的第 ii 行、从左到右的第 jj 列的格子。

格子 (i,j)(i,j) 的颜色由字符 Si,jS_{i,j} 表示,其中 Si,j=.S_{i,j} = . 表示格子 (i,j)(i,j) 为红色,而 Si,j=#S_{i,j} = \# 表示格子 (i,j)(i,j) 为绿色。

网格中 绿色连通分量的数量 是指在图中绿色格子作为顶点集合、相邻绿色格子之间连线作为边集时形成的连通分量数量。这里,两个格子 (x,y)(x,y)(x,y)(x',y') 被认为是相邻的当且仅当 xx+yy=1|x-x'| + |y-y'| = 1

考虑随机均匀选择一个 红色 格子并将其重新涂成 绿色。请计算重涂后网格中绿色连通分量数量的期望值,并对 998244353998244353 取模输出结果。

“打印期望值对 998244353998244353 取模”的含义是什么?可以证明所求期望值始终为有理数。此外,本问题的约束条件保证了若该值表示为两个互质整数 PPQQ 的形式 PQ\frac{P}{Q},则存在恰好一个整数 RR 满足 R×QP(mod998244353)R \times Q \equiv P \pmod{998244353} 以及 0R<9982443530 \leq R < 998244353。请打印这个 RR

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

This problem has a similar setting to Problem G. Differences in the problem statement are indicated in red.

There is a grid with HH rows and WW columns, where each cell is painted red or green.

Let (i,j)(i,j) denote the cell in the ii-th row from the top and the jj-th column from the left.

The color of cell (i,j)(i,j) is represented by the character Si,jS_{i,j}, where Si,j=S_{i,j} = . means cell (i,j)(i,j) is red, and Si,j=S_{i,j} = # means cell (i,j)(i,j) is green.

The number of green connected components in the grid is the number of connected components in the graph with the vertex set being the green cells and the edge set being the edges connecting two adjacent green cells. Here, two cells (x,y)(x,y) and (x,y)(x',y') are considered adjacent when xx+yy=1|x-x'| + |y-y'| = 1.

Consider choosing one red cell uniformly at random and repainting it green. Print the expected value of the number of green connected components in the grid after repainting, modulo 998244353998244353.

What does "print the expected value modulo 998244353998244353" mean? It can be proved that the sought expected value is always rational. Furthermore, the constraints of this problem guarantee that if that value is expressed as PQ\frac{P}{Q} using two coprime integers PP and QQ, there is exactly one integer RR such that R×QP(mod998244353)R \times Q \equiv P \pmod{998244353} and 0R<9982443530 \leq R < 998244353. Print this RR.

Constraints

  • 1H,W10001 \leq H,W \leq 1000
  • Si,j=S_{i,j} = . or Si,j=S_{i,j} = #.
  • There is at least one (i,j)(i,j) such that Si,j=S_{i,j} = ..

Input

The input is given from Standard Input in the following format:

HH WW

S1,1S_{1,1}S1,2S_{1,2}\ldotsS1,WS_{1,W}

S2,1S_{2,1}S2,2S_{2,2}\ldotsS2,WS_{2,W}

\vdots

SH,1S_{H,1}SH,2S_{H,2}\ldotsSH,WS_{H,W}

Output

Print the answer.

Sample Input 1

3 3
##.
#.#
#..

Sample Output 1

499122178

If cell (1,3)(1,3) is repainted green, the number of green connected components becomes 11.

If cell (2,2)(2,2) is repainted green, the number of green connected components becomes 11.

If cell (3,2)(3,2) is repainted green, the number of green connected components becomes 22.

If cell (3,3)(3,3) is repainted green, the number of green connected components becomes 22.

Therefore, the expected value of the number of green connected components after choosing one red cell uniformly at random and repainting it green is (1+1+2+2)/4=3/2(1+1+2+2)/4 = 3/2.

Sample Input 2

4 5
..#..
.###.
#####
..#..

Sample Output 2

598946613

Sample Input 3

3 4
#...
.#.#
..##

Sample Output 3

285212675

update @ 2024/3/10 01:21:54