#P16625. [GKS 2017 #D] Sherlock and Matrix Game
[GKS 2017 #D] Sherlock and Matrix Game
Problem Description
Today, Sherlock and Watson attended a lecture in which they were introduced to matrices. Sherlock is one of those programmers who is not really interested in linear algebra, but he did come up with a problem involving matrices for Watson to solve.
Sherlock has given Watson two one-dimensional arrays A and B; both have length . He has asked Watson to form a matrix with rows and columns, in which the element in the row is the product of the i-th element of A and the j-th element of B.
Let denote the cell of the matrix in the x-th row (numbered starting from , starting from the top row) and the y-th column (numbered starting from , starting from the left column). Then a submatrix is defined by bottom-left and top-right cells and respectively, with and , and the submatrix consists of all cells such that and . The sum of a submatrix is defined as sum of all of the cells of the submatrix.
To challenge Watson, Sherlock has given him an integer and asked him to output the largest sum among all submatrices in Watson's matrix, with counting starting from for the largest sum. (It is possible that different values of may correspond to the same sum; that is, there may be multiple submatrices with the same sum.) Can you help Watson?
Input Format
The first line of the input gives the number of test cases, . test cases follow. Each test case consists of one line with nine integers , , , , , , , and . is the length of arrays and ; is the rank of the submatrix sum Watson has to output, and are the first elements of arrays A and B, respectively; and the other five values are parameters that you should use to generate the elements of the arrays, as follows:
First define , , , . Then, use the recurrences below to generate and for to :
- $x_i = (C \times x_{i-1} + D \times y_{i-1} + E_1) \pmod{F}$.
- $y_i = (D \times x_{i-1} + C \times y_{i-1} + E_2) \pmod{F}$.
Further, generate and for to using following recurrences:
- $r_i = (C \times r_{i-1} + D \times s_{i-1} + E_1) \pmod{2}$.
- $s_i = (D \times r_{i-1} + C \times s_{i-1} + E_2) \pmod{2}$.
We define and , for all to .
Output Format
For each test case, output one line containing Case #x: y, where is the test case number (starting from ) and is the largest submatrix sum in the matrix defined in the statement.
3
2 3 1 1 1 1 1 1 5
1 1 2 2 2 2 2 2 5
2 3 1 2 2 1 1 1 5
Case #1: 6
Case #2: 4
Case #3: 1
Hint
In case 1, using the generation method, the generated arrays A and B are [1, -3] and [1, -3], respectively. So, the matrix formed is
$$\begin{aligned} &[1, \ -3] \\ &[-3, \ 9] \end{aligned}$$All possible submatrix sums in decreasing order are [9, 6, 6, 4, 1, -2, -2, -3, -3]. As K = 3, answer is 6.
In case 2, using the generation method, the generated arrays A and B are [2] and [2], respectively. So, the matrix formed is
As K = 1, answer is 4.
In case 3, using the generation method, the generated arrays A and B are [1, 0] and [2, -1] respectively. So, the matrix formed is
$$\begin{aligned} &[2, \ -1] \\ &[0, \ 0] \end{aligned}$$All possible submatrix sums in decreasing order are [2, 2, 1, 1, 0, 0, 0, -1, -1]. As K = 3, answer is 1.
Limits
.
$1 \le K \le \min(10^5, \text{total number of submatrices possible})$.
.
.
.
.
.
.
.
Small dataset (Test set 1 - Visible)
.
Large dataset (Test set 2 - Hidden)
.