#abc334g. G - Christmas Color Grid 2

G - Christmas Color Grid 2

Score : 650650 points

问题描述

本问题的背景与问题E相似。问题表述中的差异以红色标出。

存在一个 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 E. 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 green cell uniformly at random and repainting it red. 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

598946614

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

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

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

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

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

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

Sample Input 2

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

Sample Output 2

199648872

Sample Input 3

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

Sample Output 3

399297744

update @ 2024/3/10 01:22:45