Codeforces Round 836 (Div. 2)
E
Tick, Tock
Codeforces
2500
原题
← DF →
翻译中…

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.

翻译中…

Input

翻译中…

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 \lt 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$.

翻译中…

Output

翻译中…

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$.

翻译中…

Note

翻译中…

For the first sample, this is a possible configuration for the clocks:

翻译中…
1 0 3
0 3 2
翻译中…

This 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 #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
Output #1
4
0
73741817
0
1
InputCopy OutputCopy
题目信息
题号
1758E
时间限制
1 s
内存限制
256 MB
标签
组合数学
DFS 及类似
并查集
图论

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。