AT_awtf2026algo_e.Even Rows

入门

通过率:0%

时间限制:5.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There is a board with NN rows and MM columns. Let us call the cell at the ii-th row from the top and jj-th column from the left cell (i,j)(i,j).

Each cell is empty or has one piece placed on it. The state of the board is given by NN length-MM strings S1,S2,…,SNS_1,S_2,\ldots,S_N: if the jj-th character of SiS_{i} is ., cell (i,j)(i,j) is empty, and if it is O, cell (i,j)(i,j) has a piece placed on it.

For integers LL and RR (1≤L≤R≤N1 \leq L \leq R \leq N), define f(L,R)f(L,R) as follows.

  • Extract only rows LL through RR of the board, obtaining a board with R−L+1R-L+1 rows. Call this new board XX.

  • You can repeat the following operation on XX zero or more times.

    • Choose one piece and move it to any of the cells adjacent to it (up, down, left, or right). Here, the destination cell must not have another piece on it. Also, you cannot perform an operation that would move a piece outside of XX.
  • Your goal is to reach a state where, for every row of XX, the number of pieces in that row is even. If it is impossible to achieve the goal, let f(L,R)=0f(L,R)=0. If it is possible, let f(L,R)f(L,R) be the minimum number of operations required to do so.

Find ∑1≤L≤R≤Nf(L,R)\sum_{1 \leq L \leq R \leq N} f(L,R), modulo 998244353998244353.

Solve TT cases for each input.

有一个 NN 行 MM 列的棋盘。我们将从上往下数第 ii 行、从左往右数第 jj 列的格子记为 (i,j)(i,j)。

每个格子要么为空,要么恰好放置一枚棋子。棋盘的状态由 NN 个长度为 MM 的字符串 S1,S2,…,SNS_1,S_2,\ldots,S_N 给出:若 SiS_i 的第 jj 个字符为 .,则格子 (i,j)(i,j) 为空;若为 O,则格子 (i,j)(i,j) 上有一枚棋子。

对整数 LL 和 RR(满足 1≤L≤R≤N1 \leq L \leq R \leq N),定义函数 f(L,R)f(L,R) 如下:

  • 仅提取棋盘的第 LL 行至第 RR 行,得到一个具有 R−L+1R-L+1 行的新棋盘,记为 XX。

  • 可以在 XX 上执行以下操作零次或多次:

    • 任选一枚棋子,并将其移动到与其相邻的任意一个格子(上、下、左、右)。目标格子必须为空;且不允许将棋子移出 XX 的边界。
  • 目标状态是:XX 的每一行中,棋子数量均为偶数。若无法达到该目标,则令 f(L,R)=0f(L,R)=0;否则,令 f(L,R)f(L,R) 为达成目标所需的最少操作次数。

求 ∑1≤L≤R≤Nf(L,R)\sum_{1 \leq L \leq R \leq N} f(L,R) 对 998244353998244353 取模的结果。

对每组输入,需解决 TT 个测试用例。

输入格式

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

TT
case1case_1
case2case_2
⋮\vdots
caseTcase_T

Each test case is given in the following format:

NN MM
S1S_1
S2S_2
⋮\vdots
SNS_N

输入从标准输入中给出,格式如下:

TT
case1case_1
case2case_2
⋮\vdots
caseTcase_T

每个测试用例的格式如下:

NN MM
S1S_1
S2S_2
⋮\vdots
SNS_N

输出格式

For each test case, output the answer.

对于每个测试用例,输出答案。

输入输出样例

  • 输入#1

    7
    3 2
    O.
    O.
    .O
    4 3
    OOO
    OOO
    ...
    OOO
    5 3
    OOO
    OOO
    ..O
    OOO
    ...
    7 3
    OOO
    ..O
    OOO
    OOO
    O..
    OOO
    .OO
    8 3
    OO.
    OOO
    OOO
    ..O
    OOO
    .O.
    .OO
    O..
    4 5
    .O..O
    OOOOO
    OOOOO
    .O.O.
    10 10
    O.O..O..OO
    ........O.
    OOO.O.OO.O
    O..O..OOOO
    OO..O.....
    .OOOO..OO.
    O.......O.
    OOO.O.....
    O.O.O....O
    .....OO.O.

    输出#1

    3
    5
    9
    7
    29
    9
    44

说明/提示

Sample 1 Explanation:
The values of f(L,R)f(L,R) in the first test case are as follows.

  • f(1,1)=0f(1,1)=0
  • f(1,2)=2f(1,2)=2
  • f(1,3)=0f(1,3)=0
  • f(2,2)=0f(2,2)=0
  • f(2,3)=1f(2,3)=1
  • f(3,3)=0f(3,3)=0

Constraints

  • 1≤T≤2500001 \leq T \leq 250000
  • 2≤N≤5000002 \leq N \leq 500000
  • 2≤M≤102 \leq M \leq 10
  • SiS_i is a string of length MM consisting of . and O.
  • The sum of NN over the TT cases is at most 500000500000.

样例 1 解释:
第一个测试用例中,f(L,R)f(L,R) 的取值如下:

  • f(1,1)=0f(1,1)=0
  • f(1,2)=2f(1,2)=2
  • f(1,3)=0f(1,3)=0
  • f(2,2)=0f(2,2)=0
  • f(2,3)=1f(2,3)=1
  • f(3,3)=0f(3,3)=0

约束条件

  • 1≤T≤2500001 \leq T \leq 250000
  • 2≤N≤5000002 \leq N \leq 500000
  • 2≤M≤102 \leq M \leq 10
  • SiS_i 是一个长度为 MM 的字符串,仅由字符 . 和 O 组成。
  • 所有 TT 个测试用例的 NN 值之和不超过 500000500000。

输入解题思路,AI测评打分。不知道怎么写?

首页