Hacking Interview Solution
题号:NC243980
时间限制:C/C++/Rust/Pascal 2秒,其他语言4秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

Cuber QQ has recently been interviewed for a job position. The interviewer challenged him with the following problem.

Given a multi-dimensional space with n independent dimensions, the i-th () of which has a_i possible values . Write a random sampler to sample from this multi-dimensional space. Specifically, each time when the sampler is invoked, the sampler returns a tuple of length n, , where . In additional, the new sample must never collide with any sample generated in the history, until the whole space is exhausted.

Cuber QQ had just learned usages of random library. He thought of this problem as a piece of cake, and wrote the following code. (It's a pseudo-code, in Python style.)

def sample(a: list[int]) -> list[int]:
    return [randint(0, a[i] - 1) for i in range(len(a))]

However, the interviewer looked at this code and quickly pointed out that he did not think of the cases where the samples could collide. That is, when you call \texttt{sample(a)} repeated, it will possibly yield duplicated samples. To convince Cuber QQ of this flaw, he tested Cuber QQ's program on a test data, which calls \texttt{sample} by m times. The tester will count the number of violations of the "never collide" principle.

Specifically, the m calls of sample return a list of arrays of length m, where the i-th element is denoted as (, ). For , the two arrays ``collide'' if holds for all . The tester will count the number of (i,j) pairs where collision happens.

After the interview ends, Cuber QQ is curious about how this tester works. Your task is to help Cuber QQ implement this tester that counts collision.

输入描述:

The first line is one integer T (1 \le T \le 1000), which is the number of test cases.

For each test case, the first line are two integers n, m (1 \le n \le 10^5, 1 \le m \le 10^5).

The second line are n integers a_1,a_2,\ldots,a_n (1 \le a_i \le 100).

Then followed by m lines, the i-th of which contains n integers o_{i,1}, o_{i,2}, \ldots, o_{i,n} (0 \le o_{i,j} < a_j). These are m lines of the output of Cuber QQ's program.

The sum of all n \cdot m is no more than 10^6.

输出描述:

For each test case, output a number, which is the number of violations of "no collision".
示例1

输入

复制
3
2 3
2 2
0 1
0 0
0 1
3 2
3 2 3
2 0 1
0 0 1
2 3
42 3
4 2
4 2
4 2

输出

复制
1
0
3