Vestigium means "trace" in Latin. In this problem, we work with Latin squares and matrix traces.
The trace of a square matrix is the sum of the values on the main diagonal (which runs from the upper left to the lower right).
An -by- square matrix is a Latin square if each cell contains one of different values, and no value is repeated within a row or a column. In this problem, we will deal only with "natural Latin squares" in which the values are the integers between 1 and .
Given a matrix that contains only integers between 1 and , we want to compute its trace and check whether it is a natural Latin square. To give some additional information, instead of simply telling us whether the matrix is a natural Latin square or not, please compute the number of rows and the number of columns that contain repeated values.
Input Specification
The first line of the input gives the number of test cases, . test cases follow. Each starts with a line containing a single integer : the size of the matrix to explore. Then, lines follow. The of these lines contains integers . is the integer in the row and column of the matrix.
Output Specification
For each test case, output one line containing Case #x: k r c
, where is the test case number (starting from 1), is the trace of the matrix, is the number of rows of the matrix that contain repeated elements, and is the number of columns of the matrix that contain repeated elements.
Limits
Test set 1
Time limit: 20 seconds per test set.
Memory limit: 1GB.
.
.
, for all , .
Sample Input
3
4
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
4
2 2 2 2
2 3 2 3
2 2 2 3
2 2 2 2
3
2 1 3
1 3 2
1 2 3
Sample Output
Case #1: 4 0 0
Case #2: 9 4 4
Case #3: 8 0 2
In Sample Case #1, the input is a natural Latin square, which means no row or column has repeated elements. All four values in the main diagonal are 1, and so the trace (their sum) is 4.
In Sample Case #2, all rows and columns have repeated elements. Notice that each row or column with repeated elements is counted only once regardless of the number of elements that are repeated or how often they are repeated within the row or column. In addition, notice that some integers in the range 1 through may be absent from the input.
In Sample Case #3, the leftmost and rightmost columns have repeated elements.
Comments