luogu#P8050. [ZYOI Round1] Chessboard game/棋盘游戏

[ZYOI Round1] Chessboard game/棋盘游戏

Background

Zijin has a game board and is going to take it out to play.

Problem Description

Her board is L-shaped. It consists of an upper small rectangle of size n1×m1n_1 \times m_1 and a lower large rectangle of size n2×m2n_2 \times m_2. Initially, the number in every cell on the board is kk.

For example, when n1=2n_1 = 2, m1=2m_1 = 2, n2=3n_2 = 3, m2=4m_2 = 4, k=0k = 0, the initial board looks like this:

0 0
0 0
0 0 0 0
0 0 0 0
0 0 0 0

Now she will perform several operations: add 11 or subtract 11 to the numbers in two adjacent cells at the same time. After finishing the operations, she will remember the numbers in these cells.

However, she made a mistake once. After several operations, she forgot what one of the numbers was, so she wrote 999999999999 there. Please write a program to find what the number marked as 999999999999 should be. It is guaranteed that exactly one cell is marked as 999999999999.

Input Format

The first line contains five integers n1,m1,n2,m2,kn_1, m_1, n_2, m_2, k, representing the number of rows and columns of the small rectangle forming the L-shaped board, the number of rows and columns of the large rectangle, and the initial number in each cell.

Next come n1n_1 lines, each containing m1m_1 integers, describing the numbers in each cell of the small rectangle after the operations.

Next come n2n_2 lines, each containing m2m_2 integers, describing the numbers in each cell of the large rectangle after the operations.

The unknown number is replaced by 999999999999.

Output Format

Output one line with one integer, which is the original value of the number marked as 999999999999.

The testdata guarantees that a solution exists.

2 2 3 4 0
0 0
1 0
2 1 1 0
0 1 999999 0
0 0 0 0
2

Hint

For 40%40\% of the testdata, n1=m1=0n_1 = m_1 = 0.

For 100%100\% of the testdata, m1<m2m_1 < m_2, 0n1,m1,k1000 \le n_1, m_1, k \le 100, 1n2,m21001 \le n_2, m_2 \le 100, and the number in each cell satisfies 1000-1000 \le value 1000\le 1000. Besides the first 40%40\% of the testdata, the remaining 60%60\% of the testdata guarantees n1,m1>0n_1, m_1 > 0.

Sample Explanation

At the beginning, the board is like this:

0 0
0 0
0 0 0 0
0 0 0 0
0 0 0 0

One possible sequence of operations is given below:

First, add 11 to the number in row 22 column 11 and the number in row 33 column 11 at the same time:

0 0
1 0
1 0 0 0
0 0 0 0
0 0 0 0

Then, add 11 to the number in row 33 column 11 and the number in row 33 column 22 at the same time:

0 0
1 0
2 1 0 0
0 0 0 0
0 0 0 0

Then, add 11 to the number in row 33 column 33 and the number in row 44 column 33 at the same time:

0 0
1 0
2 1 1 0
0 0 1 0
0 0 0 0

Finally, add 11 to the number in row 44 column 22 and the number in row 44 column 33 at the same time:

0 0
1 0
2 1 1 0
0 1 2 0
0 0 0 0

It can be concluded that the number marked as 999999999999 (the number in row 44 column 33) is 22.

The sequence of operations may not be unique, but it can be proven that the answer is unique.

Translated by ChatGPT 5