#abc334g. G - Christmas Color Grid 2
G - Christmas Color Grid 2
Score : points
问题描述
本问题的背景与问题E相似。问题表述中的差异以红色标出。
存在一个 行、 列的网格,其中每个单元格被涂成红色或绿色。
令 表示从上到下的第 行和从左到右的第 列的单元格。
单元格 的颜色由字符 表示,其中 .
表示单元格 是红色, #
表示单元格 是绿色。
网格中 绿色连通分量的数量 是指在图形中连通分量的数量,其中顶点集为绿色单元格,边集为连接任意两个相邻绿色单元格的边。这里,两个单元格 和 被认为是相邻的当且仅当 。
考虑随机均匀选择一个 绿色 单元格并将其重新涂成 红色。计算并输出重涂后网格中绿色连通分量数量的期望值,对 取模的结果是什么?
“打印期望值对 取模”的含义是什么?可以证明所求期望值始终是有理数。此外,本问题的约束条件保证了如果该值表示为两个互质整数 和 的形式 ,则恰好存在一个整数 满足 以及 。请打印这个 值。
以上为通义千问 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 rows and columns, where each cell is painted red or green.
Let denote the cell in the -th row from the top and the -th column from the left.
The color of cell is represented by the character , where .
means cell is red, and #
means cell 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 and are considered adjacent when .
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 .
What does "print the expected value modulo " 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 using two coprime integers and , there is exactly one integer such that and . Print this .
Constraints
-
.
or#
. - There is at least one such that
#
.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
3 3
##.
#.#
#..
Sample Output 1
598946614
If cell is repainted red, the number of green connected components becomes .
If cell is repainted red, the number of green connected components becomes .
If cell is repainted red, the number of green connected components becomes .
If cell is repainted red, the number of green connected components becomes .
If cell is repainted red, the number of green connected components becomes .
Therefore, the expected value of the number of green connected components after choosing one green cell uniformly at random and repainting it red is .
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