#P16625. [GKS 2017 #D] Sherlock and Matrix Game

    ID: 18981 远端评测题 5000ms 1024MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2017可持久化线段树ST 表Google Kick Start

[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 NN. He has asked Watson to form a matrix with NN rows and NN columns, in which the jthj^\text{th} element in the ithi^\text{th} row is the product of the i-th element of A and the j-th element of B.

Let (x,y)(x, y) denote the cell of the matrix in the x-th row (numbered starting from 00, starting from the top row) and the y-th column (numbered starting from 00, starting from the left column). Then a submatrix is defined by bottom-left and top-right cells (a,b)(a, b) and (c,d)(c, d) respectively, with aca \ge c and dbd \ge b, and the submatrix consists of all cells (i,j)(i, j) such that ciac \le i \le a and bjdb \le j \le d. 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 KK and asked him to output the KthK^\text{th} largest sum among all submatrices in Watson's matrix, with KK counting starting from 11 for the largest sum. (It is possible that different values of KK 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, TT. TT test cases follow. Each test case consists of one line with nine integers NN, KK, A1A_1, B1B_1, CC, DD, E1E_1, E2E_2 and FF. NN is the length of arrays AA and BB; KK is the rank of the submatrix sum Watson has to output, AA1_1 and BB1_1 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 x1=A1x_1 = A_1, y1=B1y_1 = B_1, r1=0r_1 = 0, s1=0s_1 = 0. Then, use the recurrences below to generate xix_i and yiy_i for i=2i = 2 to NN:

  • $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 rir_i and sis_i for i=2i = 2 to NN 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 Ai=(1)ri×xiA_i = (-1)^{r_i} \times x_i and Bi=(1)si×yiB_i = (-1)^{s_i} \times y_i, for all i=2i = 2 to NN.

Output Format

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 11) and yy is the KthK^\text{th} 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

[4]\begin{aligned} &[4] \end{aligned}

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

1T201 \le T \le 20.

$1 \le K \le \min(10^5, \text{total number of submatrices possible})$.

0A11030 \le A_1 \le 10^3.

0B11030 \le B_1 \le 10^3.

0C1030 \le C \le 10^3.

0D1030 \le D \le 10^3.

0E11030 \le E_1 \le 10^3.

0E21030 \le E_2 \le 10^3.

1F1031 \le F \le 10^3.

Small dataset (Test set 1 - Visible)

1N2001 \le N \le 200.

Large dataset (Test set 2 - Hidden)

1N1051 \le N \le 10^5.