We have a 3 \times 3 grid. A number c_{i, j} is written in the square (i, j), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.
According to Takahashi, there are six integers a_1, a_2, a_3, b_1, b_2, b_3 whose values are fixed, and the number written in the square (i, j) is equal to a_i + b_j.
Determine if he is correct.
Constraints
c_{i, j} \ (1 \leq i \leq 3, 1 \leq j \leq 3) is an integer between 0 and 100 (inclusive).
Input
Input is given from Standard Input in the following format:
We need to determine if a given 3x3 grid can be expressed as a_i + b_j for some integers a_i (i=1,2,3) and b_j (j=1,2,3). The numbers are integers between 0 and 100 inclusive.
We need to check existence of six integers such that c_{i,j} = a_i + b_j for all i,j.
This is a classic problem. The condition is that the matrix is of rank at most 1 in the sense that differences across rows and columns are consistent. More precisely, for any i,i',j,j', we must have c_{i,j} - c_{i,j'} = c_{i',j} - c_{i',j'} (i.e., the difference between columns is constant across rows). Equivalently, c_{i,j} + c_{i',j'} = c_{i,j'} + c_{i',j}. Or we can set a_i's and b_j's by choosing a1 arbitrarily (say 0) and then deriving b_j from first row: b_j = c_{1,j} - a1. Then compute a_i = c_{i,1} - b1. Then check if all entries satisfy.
Thus algorithm: set a1 = 0. Then compute b1 = c_{1,1} - a1 = c_{1,1}, b2 = c_{1,2} - a1 = c_{1,2}, b3 = c_{1,3} - a1 = c_{1,3}. Then compute a2 = c_{2,1} - b1, a3 = c_{3,1} - b1. Then verify that for all i,j, c_{i,j} == a_i + b_j. If yes, print "Yes", else "No".
But is a1 = 0 always valid? Since a_i and b_j can be any integers (including negative?), constraints say c_{i,j} between 0 and 100 inclusive. But the statement says "six integers a_1, a_2, a_3, b_1, b_2, b_3 whose values are fixed". It does not restrict sign, so they can be negative. So a1=0 is a valid assignment for existence? If we set a1=0, we may get b_j = c_{1,j}. Then a_i = c_{i,1} - b1 = c_{i,1} - c_{1,1}. Then we need to check if c_{i,j} = (c_{i,1} - c_{1,1}) + c_{1,j} for all i,j. Rearranging: c_{i,j} = c_{i,1} + c_{1,j} - c_{1,1}. This is exactly the condition for additive matrix.
But is this condition necessary and sufficient? Yes. Because if such a_i,b_j exist, then c_{i,j} - c_{1,j} = a_i + b_j - (a_1+b_j) = a_i - a_1, independent of j. So c_{i,j} - c_{1,j} is constant across j. Similarly, c_{i,j} - c_{i,1} = b_j - b_1 constant across i.
Thus we can check: for i=1..3, j=1..3, does c_{i,j} == c_{i,1} + c_{1,j} - c_{1,1}? Or we can check more simply that c_{1,1} - c_{1,2} - c_{2,1} + c_{2,2} = 0, etc. But due to small size, simply compute a1=0 approach works.
Edge cases: negative numbers? Not in c, but a_i or b_j could be negative. But since we compute a_i = c_{i,1} - c_{1,1}, and c_{i,1} and c_{1,1} are between 0 and 100, a_i can be negative if c_{i,1} < c_{1,1}. That's fine. Similarly b_j = c_{1,j} might be between 0 and 100, fine. So no overflow.
Thus solution: read 3 lines of three integers each. Compute b1 = c[0][0], b2 = c[0][1], b3 = c[0][2] (since set a1=0). Compute a2 = c[1][0] - b1, a3 = c[2][0] - b1. Then loop over i=0..2, j=0..2, compute expected = (i==0?0? Actually a1=0, a2,a3 as computed) + b_j. For i=0, a_i =0, for i=1 a_i = a2, i=2 a_i = a3. Check if c[i][j] == expected. If all match, print "Yes", else "No".
Alternatively, we can check condition: for i=1..2, j=1..2, c[i][j] == c[i][0] + c[0][j] - c[0][0]. That's simpler. Actually check for all i,j except maybe i=0 or j=0? But it's simpler to check all pairs. Since if i=0, c[0][j] == c[0][0] + c[0][j] - c[0][0] always true, so only need check i=1,2 and j=1,2? Actually if i=0, the condition becomes c[0][j] == c[0][0] + c[0][j] - c[0][0] -> true. If j=0, c[i][0] == c[i][0] + c[0][0] - c[0][0] -> true. So it's sufficient to check for i=1,2 and j=1,2 (the four bottom right cells) to ensure consistency. But also need to check the last column? Actually i=1, j=2,3? The formula c[i][j] = c[i][0] + c[0][j] - c[0][0] works for all i,j. So we can check all i=0..2, j=0..2, it's fine. But the i=0 or j=0 automatically hold? Let's test: for i=0, c[0][j] on RHS: c[0][0] + c[0][j] - c[0][0] = c[0][j], so holds. For j=0: c[i][0] on RHS: c[i][0] + c[0][0] - c[0][0] = c[i][0]. So yes, they automatically hold. So we just need to check the four cells (i=1,2; j=1,2) maybe also i=1,2; j=3? But j=3 is index 2? Actually we have indices 0,1,2. So we need to check for i=1,2 and j=1,2? That's four cells. But what about i=1,j=2 (third column) and i=2,j=1? Actually j=2 is third column (since j=0,1,2). So if we only check i=1,2 and j=1,2, we are checking cells: (1,1), (1,2), (2,1), (2,2). That does not include (1,3) i=1,j=2? Wait careful: indices: row1 (i=0), row2 (i=1), row3 (i=2); column1 (j=0), column2 (j=1), column3 (j=2). So checking i=1,2 and j=1,2 covers (2,2), (2,3) and (3,2), (3,3) if we consider 1-index. Actually if i=1 (second row) and j=1 (second column): (2,2). i=1,j=2: (2,3). i=2,j=1: (3,2). i=2,j=2: (3,3). So indeed we are checking all cells except the first row and first column, which are automatically satisfied by construction. So it's sufficient. But for simplicity, we can check all 9 cells with the formula using a_i,b_j as derived from a1=0. That's also simple.
Thus implement.
Check examples:
Example1:
1 0 1
2 1 2
1 0 1
Set a1=0, b1=1, b2=0, b3=1. Then a2 = c21 - b1 = 2-1=1, a3 = c31 - b1 = 1-1=0. Then check:
(1,1):0+1=1 ok
(1,2):0+0=0 ok
(1,3):0+1=1 ok
(2,1):1+1=2 ok
(2,2):1+0=1 ok
(2,3):1+1=2 ok
(3,1):0+1=1 ok
(3,2):0+0=0 ok
(3,3):0+1=1 ok => Yes.
Example2:
2 2 2
2 1 2
2 2 2
Set a1=0, b1=2,b2=2,b3=2. a2=2-2=0, a3=2-2=0. Then check (2,2): a2+b2=0+2=2 but c22=1 => No.
Example3:
0 8 8
0 8 8
0 8 8
Set a1=0,b1=0,b2=8,b3=8. a2=0-0=0,a3=0-0=0. Check all: cij = a_i + b_j: For i=2,j=2: 0+8=8 matches 8; etc. Yes.
Now, I should produce Python code to read input and check. Since it's a simple problem, code is short.
Implementation details: We can read all 3 lines, split, map to int. Store in list of lists.
Compute b = c[0] (since a1=0). Compute a2 = c[1][0] - b[0], a3 = c[2][0] - b[0]. Then define a = [0, a2, a3]. Then iterate i in 0..2, j in 0..2: if c[i][j] != a[i] + b[j]: print("No") and return. After loop print("Yes").
But careful: a1=0, but a1 may not be 0 in any solution; but if a1 is something else, we can shift: setting a1=0 yields b_j = c_{1j} - a1 = c_{1j}. If actual a1' != 0, then actual b_j' = c_{1j} - a1'. The derived b_j = c_{1j} would be b_j' + a1'. Then a_i derived = c_{i1} - b1 = c_{i1} - c_{11} = (a_i' + b_1') - (a_1' + b_1') = a_i' - a_1'. So a_i derived = a_i' - a_1'. Then a_i derived + b_j derived = (a_i' - a_1') + (b_j' + a_1') = a_i' + b_j'. So it matches. So indeed the existence condition is satisfied by this derivation. So the method is correct.
Thus final answer.
But we might also consider the condition as checking all 2x2 submatrices: c_{i,j}+c_{i+1,j+1} == c_{i,j+1}+c_{i+1,j}. For 3x3, it suffices to check for i=1,2 and j=1,2 (the two overlapping squares). But easier to implement derived method.
Edge case: Could there be a situation where a_i derived from first row and column produce integer but check fails because of inconsistency? Then "No".
Thus final solution.
Now produce a Python solution.
We'll write:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
c = [list(map(int, data[i*3:(i+1)*3])) for i in range(3)]
b = c[0][:] # b1,b2,b3
a2 = c[1][0] - b[0]
a3 = c[2][0] - b[0]
a = [0, a2, a3]
for i in range(3):
for j in range(3):
if c[i][j] != a[i] + b[j]:
print("No")
return
print("Yes")
That's it.
But is there any possibility of overflow? No.
Now, is there any trick? If we set a1=0, b_j = c[0][j], but maybe c[0][j] are large? They are <=100. So fine.
Thus final answer.
But the problem statement might be from AtCoder, where output is case-sensitive. So "Yes"/"No".
Testing with given examples.
Example1: Yes, Example2: No, Example3: Yes, Example4: No.
All good.
We should also consider if the condition of a1=0 is always valid, even if we get a_i,b_j that are not integers? But c are integers, so a_i,b_j integers.
Thus final.
Now I'll produce final message with explanation and code. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
# read the 3x3 grid
c = [list(map(int, data[i*3:(i+1)*3])) for i in range(3)]
code
# set a1 = 0, then bj = c[0][j]
b = c[0][:] # b1, b2, b3
a2 = c[1][0] - b[0] # a2 = c21 - b1
a3 = c[2][0] - b[0] # a3 = c31 - b1
a = [0, a2, a3] # a1, a2, a3
# verify all nine cells
for i in range(3):
for j in range(3):
if c[i][j] != a[i] + b[j]:
print("No")
return
print("Yes")