Vestigium
InterviewTime limit20sMemory limit1024 MB
Given an N by N matrix with values from 1 to N, find its trace and count the rows and columns that contain a repeated value.
- Level
Easy2 of 10
- Topics
- Array, Implementation
- Solved
- No attempts yet
Problem
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 matrix is a Latin square if each cell contains one of different values, and no value is repeated within a row or a column. This problem deals only with natural Latin squares, in which the values are the integers from 1 to .
Given a matrix that contains only integers from 1 to , compute its trace and check whether it is a natural Latin square. Instead of only reporting whether it is one, count the rows and the columns that contain repeated values.
Input
The first line contains the number of test cases . Then test cases follow. Each test case starts with a line containing a single integer , the size of the matrix. Then lines follow. The -th of these lines contains integers , where is the integer in row and column .
Output
For each test case, output one line containing Case #x: k r c. Here is the test case number starting from 1, is the trace of the matrix, is the number of rows that contain repeated elements, and is the number of columns that contain repeated elements.
Limits
- .
- .
- for all .
Hints
In Sample Case #1, the input is a natural Latin square, so no row or column has repeated elements. All four values on the main diagonal are 1, so the trace is 4.
In Sample Case #2, every row and every column has repeated elements. A row or column with repeated elements is counted only once, no matter how many values repeat or how often. Some integers from 1 to may also be absent from the input.
In Sample Case #3, the leftmost and rightmost columns have repeated elements.