QOJ.ac

QOJ

実行時間制限: 5 s メモリ制限: 512 MB 満点: 100

#8088. 伊布

統計

众所周知,伊布(Eevee)可以通过多种方式进化。例如,当它靠近进化石时就会进化。

假设有 $n$ 种不同的进化石,编号为 $1$ 到 $n$。每种进化石都被分成了 $k$ 个碎片。伊布将所有碎片分成了 $k$ 个堆,编号为 $1$ 到 $k$。每个堆恰好包含 $n$ 个碎片,且每个碎片来自不同的进化石。因此,我们可以将每个堆看作是不同进化石碎片的一个排列。

伊布将执行 $k \cdot n$ 次以下操作:选择一个非空堆,移除其最顶部的碎片,并将其添加到碎片序列(初始为空)的末尾。如果我们在任何步骤中选择了索引不同的堆,则认为合并成一个序列的两种方式是不同的。如果在该过程中序列中出现了 $k$ 个来自同一进化石的相邻碎片,伊布可能会意外进化。由于伊布在不进化时最可爱,如果合并方式使得序列中不存在长度为 $k$ 且仅包含同一进化石碎片的区间,我们称这种合并方式为“好的”。

令 $f(i, j)$ 表示合并索引范围在 $[i, j]$ 内的堆的“好的”方式数量。这里,当我们考虑长度为 $\ell$ 的区间时,假设伊布仅执行 $\ell \cdot n$ 次操作,并且我们禁止任何长度为 $\ell$ 的区间包含仅来自同一进化石的碎片。

你的任务是计算: $$\left( \sum_{i=1}^{k} \sum_{j=i+1}^{k} f(i, j) \right) \pmod{10^9 + 7}$$

然而,事实证明伊布在过程开始前打乱了每个堆!因此,每个堆都包含碎片的随机排列。每个输入排列都是从所有 $n!$ 种可能的排列中等概率且独立地选择的。

输入格式

第一行包含两个整数 $k$ 和 $n$ ($2 \le k, n \le 300$),分别表示堆的数量和每个堆中碎片的数量。

接下来的 $k$ 行,每行包含 $n$ 个整数。第 $i$ 行的第 $j$ 个数是 $a_{i,j}$ ($1 \le a_{i,j} \le n, a_{i,j} \neq a_{i,l}$ 当 $j \neq l$),表示第 $i$ 个堆中从上往下数的第 $j$ 个数。

输出格式

输出题目描述中所述的和。

样例

样例 1

输入

3 3
1 2 3
3 2 1
1 3 2

输出

1464

样例 2

输入

4 2
1 2
2 1
1 2
2 1

输出

2466

说明

考虑计算第一个样例中的 $f(1, 3)$。伊布有三堆进化石碎片:

其中一种好的合并方式如下:

以下方式不是好的,因为它包含三个连续的 3:

在第一个样例中,$f(1, 2) = 8, f(1, 3) = 1446$ 且 $f(2, 3) = 10$。

在第二个样例中,$f(1, 2) = 2, f(1, 3) = 66, f(1, 4) = 2328, f(2, 3) = 2, f(2, 4) = 66$ 且 $f(3, 4) = 2$。

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#196EditorialOpen题解jiangly2025-12-13 00:04:02View

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.