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 and a lower large rectangle of size . Initially, the number in every cell on the board is .
For example, when , , , , , 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 or subtract 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 there. Please write a program to find what the number marked as should be. It is guaranteed that exactly one cell is marked as .
Input Format
The first line contains five integers , 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 lines, each containing integers, describing the numbers in each cell of the small rectangle after the operations.
Next come lines, each containing integers, describing the numbers in each cell of the large rectangle after the operations.
The unknown number is replaced by .
Output Format
Output one line with one integer, which is the original value of the number marked as .
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 of the testdata, .
For of the testdata, , , , and the number in each cell satisfies value . Besides the first of the testdata, the remaining of the testdata guarantees .
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 to the number in row column and the number in row column at the same time:
0 0
1 0
1 0 0 0
0 0 0 0
0 0 0 0
Then, add to the number in row column and the number in row column at the same time:
0 0
1 0
2 1 0 0
0 0 0 0
0 0 0 0
Then, add to the number in row column and the number in row column at the same time:
0 0
1 0
2 1 1 0
0 0 1 0
0 0 0 0
Finally, add to the number in row column and the number in row column 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 (the number in row column ) is .
The sequence of operations may not be unique, but it can be proven that the answer is unique.
Translated by ChatGPT 5