#P16849. [GKS 2021 #D] Arithmetic Square
[GKS 2021 #D] Arithmetic Square
Problem Description
You are given a grid of integers. Let denote the integer in the -th row and -th column of the grid, where and are -indexed. The integer in the middle of the grid, , is missing. Find the maximum number of rows, columns, and diagonals of this square, that form sequences which are arithmetic progressions. You can replace the missing number with any integer.
An arithmetic progression (also known as arithmetic sequence) is a sequence of numbers such that the difference between consecutive terms is constant. In mathematical terms, this can be represented as , where is the common difference. In this problem, a sequence can be the numbers in either a row, column or diagonal. We are looking to replace the missing value by an integer that maximizes the number of arithmetic progressions that can be found in the resulting set of sequences.
Two sequences are considered different if they are from different rows, columns, or diagonals. For example, the sequence across the middle row and across the top row will be counted as sequences but the sequences and across the same row, column, or diagonal will be counted as one sequence.
Input Format
The first line of the input gives the number of test cases, . test cases follow.
Each test case consists of lines.
The first line of each test case contains integers, , , and .
The second line of each test case contains integers, and .
The last line of each test case contains integers, , , and .
Output Format
For each test case, output one line containing Case #x: y, where is the test case number (starting from ) and is the maximum possible number of arithmetic progressions that can be generated by the rows, columns, and diagonals of the grid after setting the missing element.
3
3 4 11
10 9
-1 6 7
4 1 6
3 5
2 5 6
9 9 9
9 9
9 9 9
Case #1: 4
Case #2: 3
Case #3: 8
Hint
In Sample Case #1, if we set the missing number to be , we have exactly arithmetic progressions.
- top left diagonal:
- top right diagonal:
- middle column:
- right column:
If we set the missing number to any other integer, there would be only progression. Thus, the answer is .
In Sample Case #2, if we set the missing number to be , we have exactly arithmetic progressions.
- top right diagonal:
- middle row:
- left column:
Setting the missing number to any other integer results in fewer progressions, so we output .
In Sample Case #3, if we set the missing number to be , we have all possible arithmetic progressions. There are total progressions (each one is ), so we output .
Limits
.
are integers, for all .
Test Set
, for all .
Test Set
, for all .