#abc334e. E - Christmas Color Grid 1
E - Christmas Color Grid 1
Score : points
问题描述
本问题与问题 G 的设定相似。问题描述中的差异部分以红色标出。
存在一个 行、 列的网格,其中每个格子被涂成红色或绿色。
令 表示从上到下的第 行、从左到右的第 列的格子。
格子 的颜色由字符 表示,其中 表示格子 为红色,而 表示格子 为绿色。
网格中 绿色连通分量的数量 是指在图中绿色格子作为顶点集合、相邻绿色格子之间连线作为边集时形成的连通分量数量。这里,两个格子 和 被认为是相邻的当且仅当 。
考虑随机均匀选择一个 红色 格子并将其重新涂成 绿色。请计算重涂后网格中绿色连通分量数量的期望值,并对 取模输出结果。
“打印期望值对 取模”的含义是什么?可以证明所求期望值始终为有理数。此外,本问题的约束条件保证了若该值表示为两个互质整数 和 的形式 ,则存在恰好一个整数 满足 以及 。请打印这个 。
以上为通义千问 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 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 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 .
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
499122178
If cell is repainted green, the number of green connected components becomes .
If cell is repainted green, the number of green connected components becomes .
If cell is repainted green, the number of green connected components becomes .
If cell is repainted green, the number of green connected components becomes .
Therefore, the expected value of the number of green connected components after choosing one red cell uniformly at random and repainting it green is .
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