QOJ.ac

QOJ

Time Limit: 3.0 s Memory Limit: 1024 MB Total points: 100

#18554. 矩阵中的路径

Statistics

给你一个拥有 $n$ 行(从 $1$ 到 $n$ 编号)和 $m$ 列(从 $1$ 到 $m$ 编号)的矩阵。在第 $i$ 行第 $j$ 列的单元格中写有一个整数 $a_{i,j}$。

一个棋子最初位于单元格 $(1, 1)$,它将被移动到单元格 $(n, m)$。在每次移动中,它会移动到当前行或当前列的相邻单元格(如果当前单元格是 $(x, y)$,则它会移动到 $(x + 1, y)$ 或 $(x, y + 1)$)。棋子不能离开矩阵。我们将棋子从单元格 $(1, 1)$ 到单元格 $(n, m)$ 的路径称为一条“路径”。

一条路径的代价是该路径上所有单元格中写有的整数去重后的和。例如,如果路径上单元格中的数字依次为 $[5, 7, 3, 5, 5, 1, 7]$,则该路径的代价为 $5 + 7 + 3 + 1 = 16$。请计算所有可能路径的代价之和。

输入格式

第一行包含两个整数 $n$ 和 $m$($1 \le n, m \le 500$)—— 矩阵的维度。

接下来 $n$ 行,其中第 $i$ 行包含 $m$ 个整数 $a_{i,1}, a_{i,2}, \dots, a_{i,m}$($1 \le a_{i,j} \le 998\,244\,352$)。

输出格式

输出一个整数 —— 所有可能路径的代价之和。由于结果可能很大,请将其对 $998\,244\,353$ 取模后输出。

样例

输入样例 1

2 3
1 3 2
2 4 2

输出样例 1

23

输入样例 2

3 3
1 5 1
1 1 5
9 1 1

输出样例 2

35

说明

在样例 1 中,有三条可能的路径:

  • $a_{1,1} \to a_{2,1} \to a_{2,2} \to a_{2,3}$;其代价等于 $1 + 2 + 4 = 7$;
  • $a_{1,1} \to a_{1,2} \to a_{2,2} \to a_{2,3}$;其代价等于 $1 + 2 + 3 + 4 = 10$;
  • $a_{1,1} \to a_{1,2} \to a_{1,3} \to a_{2,3}$;其代价等于 $1 + 2 + 3 = 6$。

所有可能路径的代价之和等于 $7 + 10 + 6 = 23$。

在样例 2 中,有六条可能的路径:

  • $a_{1,1} \to a_{2,1} \to a_{3,1} \to a_{3,2} \to a_{3,3}$;其代价等于 $1 + 9 = 10$;
  • $a_{1,1} \to a_{2,1} \to a_{2,2} \to a_{3,2} \to a_{3,3}$;其代价等于 $1$;
  • $a_{1,1} \to a_{2,1} \to a_{2,2} \to a_{2,3} \to a_{3,3}$;其代价等于 $1 + 5 = 6$;
  • $a_{1,1} \to a_{1,2} \to a_{2,2} \to a_{3,2} \to a_{3,3}$;其代价等于 $1 + 5 = 6$;
  • $a_{1,1} \to a_{1,2} \to a_{2,2} \to a_{2,3} \to a_{3,3}$;其代价等于 $1 + 5 = 6$;
  • $a_{1,1} \to a_{1,2} \to a_{1,3} \to a_{2,3} \to a_{3,3}$;其代价等于 $1 + 5 = 6$。

所有可能路径的代价之和等于 $10 + 1 + 6 + 6 + 6 + 6 = 35$。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.