309911: CF1758E. Tick, Tock

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Tick, Tock

题意翻译

Feyn 是一个钟表匠,他出了一个谜题: 谜题包含一个放了 $n\times m$ 个钟的矩阵,矩阵里的每个钟都恰好指向某一时刻。注意在 Feyn 的世界里钟是以 $h$ 个小时为进制的。Feyn 每次可以给矩阵的某一行或某一列中的钟拨向下一时刻。这个矩阵有些位置放好了钟,有些还没放好钟,请你计算有多少种不同的方案数使得填满矩阵后 Feyn 能按规则把所有钟拨向相同时刻。答案对 $10^9+7$ 取模。多组数据。 如果两个矩阵中某个钟初始指向的时刻不同,则认为他们是不同的方案。 (translated by 342873)

题目描述

Tannhaus, the clockmaker in the town of Winden, makes mysterious clocks that measure time in $ h $ hours numbered from $ 0 $ to $ h-1 $ . One day, he decided to make a puzzle with these clocks. The puzzle consists of an $ n \times m $ grid of clocks, and each clock always displays some hour exactly (that is, it doesn't lie between two hours). In one move, he can choose any row or column and shift all clocks in that row or column one hour forward $ ^\dagger $ . The grid of clocks is called solvable if it is possible to make all the clocks display the same time. While building his puzzle, Tannhaus suddenly got worried that it might not be possible to make the grid solvable. Some cells of the grid have clocks already displaying a certain initial time, while the rest of the cells are empty. Given the partially completed grid of clocks, find the number of ways $ ^\ddagger $ to assign clocks in the empty cells so that the grid is solvable. The answer can be enormous, so compute it modulo $ 10^9 + 7 $ . $ ^\dagger $ If a clock currently displays hour $ t $ and is shifted one hour forward, then the clock will instead display hour $ (t+1) \bmod h $ . $ ^\ddagger $ Two assignments are different if there exists some cell with a clock that displays a different time in both arrangements.

输入输出格式

输入格式


The first line of input contains $ t $ ( $ 1 \leq t \leq 5 \cdot 10^4 $ ) — the number of test cases. The first line of each test case consists of 3 integers $ n $ , $ m $ , and $ h $ ( $ 1 \leq n, m \leq 2 \cdot 10^5 $ ; $ 1 \leq h \leq 10^9 $ ) — the number of rows in the grid, the number of columns in the grid, and the number of the hours in the day respectively. The next $ n $ lines each contain $ m $ integers, describing the clock grid. The integer $ x $ ( $ -1 \leq x < h $ ) in the $ i $ -th row and the $ j $ -th column represents the initial hour of the corresponding clock, or if $ x = -1 $ , an empty cell. It is guaranteed that the sum of $ n \cdot m $ over all test cases does not exceed $ 2 \cdot 10^5 $ .

输出格式


For each test case, output the number of ways to assign clocks in the empty cells so that the grid is solvable. The answer can be huge, so output it modulo $ 10^9 + 7 $ .

输入输出样例

输入样例 #1

5
2 3 4
1 0 -1
-1 -1 2
2 2 10
1 2
3 5
4 5 1024
1 -1 -1 -1 -1
-1 -1 -1 1000 -1
-1 -1 -1 -1 69
420 -1 -1 999 -1
3 3 3
-1 -1 1
2 -1 1
2 -1 2
3 3 3
1 -1 2
-1 0 -1
-1 1 0

输出样例 #1

4
0
73741817
0
1

说明

For the first sample, this is a possible configuration for the clocks: 103032This is solvable since we can: 1. Move the middle column forward one hour. 2. Move the third column forward one hour. 3. Move the third column forward one hour. 4. Move the second row forward one hour. After that all the clocks show one hour.For the second test case, it can be shown that there are no possible solvable clock configurations.

Input

题意翻译

Feyn 是一个钟表匠,他出了一个谜题: 谜题包含一个放了 $n\times m$ 个钟的矩阵,矩阵里的每个钟都恰好指向某一时刻。注意在 Feyn 的世界里钟是以 $h$ 个小时为进制的。Feyn 每次可以给矩阵的某一行或某一列中的钟拨向下一时刻。这个矩阵有些位置放好了钟,有些还没放好钟,请你计算有多少种不同的方案数使得填满矩阵后 Feyn 能按规则把所有钟拨向相同时刻。答案对 $10^9+7$ 取模。多组数据。 如果两个矩阵中某个钟初始指向的时刻不同,则认为他们是不同的方案。 (translated by 342873)

Output

**题目大意**:

Feyn是一个钟表匠,他提出了一个谜题,谜题包含一个$ n \times m $的钟的矩阵,每个钟都恰好指向某一时刻。在Feyn的世界里,钟是以$ h $个小时候为进制的。Feyn每次可以给矩阵的某一行或某一列中的钟拨向下一时刻。这个矩阵有些位置放好了钟,有些还没放好钟,请你计算有多少种不同的方案数使得填满矩阵后Feyn能按规则把所有钟拨向相同时刻。答案对$ 10^9+7 $取模。有多组数据。

**输入输出数据格式**:

**输入格式**:
- 第一行包含一个整数$ t $($ 1 \leq t \leq 5 \cdot 10^4 $),表示测试用例的数量。
- 每个测试用例的第一行包含3个整数$ n $,$ m $,和$ h $($ 1 \leq n, m \leq 2 \cdot 10^5 $;$ 1 \leq h \leq 10^9 $),分别表示网格的行数、列数和一天中的小时数。
- 接下来的$ n $行,每行包含$ m $个整数,描述钟表网格。整数$ x $($ -1 \leq x < h $)在第$ i $行第$ j $列表示相应钟的初始小时数,或者如果$ x = -1 $,表示一个空单元格。
- 保证所有测试用例的$ n \cdot m $之和不超过$ 2 \cdot 10^5 $。

**输出格式**:
- 对于每个测试用例,输出一个整数,表示将钟表分配到空单元格的方式的数量,使得网格可解。答案可能很大,因此将其模$ 10^9 + 7 $输出。

**输入输出样例**:

**输入样例 #1**:
```
5
2 3 4
1 0 -1
-1 -1 2
2 2 10
1 2
3 5
4 5 1024
1 -1 -1 -1 -1
-1 -1 -1 1000 -1
-1 -1 -1 -1 69
420 -1 -1 999 -1
3 3 3
-1 -1 1
2 -1 1
2 -1 2
3 3 3
1 -1 2
-1 0 -1
-1 1 0
```

**输出样例 #1**:
```
4
0
73741817
0
1
```**题目大意**: Feyn是一个钟表匠,他提出了一个谜题,谜题包含一个$ n \times m $的钟的矩阵,每个钟都恰好指向某一时刻。在Feyn的世界里,钟是以$ h $个小时候为进制的。Feyn每次可以给矩阵的某一行或某一列中的钟拨向下一时刻。这个矩阵有些位置放好了钟,有些还没放好钟,请你计算有多少种不同的方案数使得填满矩阵后Feyn能按规则把所有钟拨向相同时刻。答案对$ 10^9+7 $取模。有多组数据。 **输入输出数据格式**: **输入格式**: - 第一行包含一个整数$ t $($ 1 \leq t \leq 5 \cdot 10^4 $),表示测试用例的数量。 - 每个测试用例的第一行包含3个整数$ n $,$ m $,和$ h $($ 1 \leq n, m \leq 2 \cdot 10^5 $;$ 1 \leq h \leq 10^9 $),分别表示网格的行数、列数和一天中的小时数。 - 接下来的$ n $行,每行包含$ m $个整数,描述钟表网格。整数$ x $($ -1 \leq x < h $)在第$ i $行第$ j $列表示相应钟的初始小时数,或者如果$ x = -1 $,表示一个空单元格。 - 保证所有测试用例的$ n \cdot m $之和不超过$ 2 \cdot 10^5 $。 **输出格式**: - 对于每个测试用例,输出一个整数,表示将钟表分配到空单元格的方式的数量,使得网格可解。答案可能很大,因此将其模$ 10^9 + 7 $输出。 **输入输出样例**: **输入样例 #1**: ``` 5 2 3 4 1 0 -1 -1 -1 2 2 2 10 1 2 3 5 4 5 1024 1 -1 -1 -1 -1 -1 -1 -1 1000 -1 -1 -1 -1 -1 69 420 -1 -1 999 -1 3 3 3 -1 -1 1 2 -1 1 2 -1 2 3 3 3 1 -1 2 -1 0 -1 -1 1 0 ``` **输出样例 #1**: ``` 4 0 73741817 0 1 ```

加入题单

算法标签: