We are given a 3x3 magic square with some known entries: all off-diagonal entries are given (positive integers not exceeding 10^5), and the three diagonal entries are zeros, meaning they are unknown. We need to restore a magic square (i.e., each row sum, each column sum, and both diagonal sums are equal) with all entries positive (positive integers) and <= 10^5. Guarantee there exists at least one solution. We can output any valid solution.
We need to fill a, b, c on diagonal: positions (1,1), (2,2), (3,3) unknown. Let's denote the square:
Let the matrix be:
x a12 a13
a21 y a23
a31 a32 z
Where x, y, z are unknown (currently zeros), and the other six numbers are given positive integers. We need to find positive integers x,y,z such that all row sums equal S, all column sums equal S, and the two diagonal sums equal S.
We have constraints:
Row1: x + a12 + a13 = S
Row2: a21 + y + a23 = S
Row3: a31 + a32 + z = S
Col1: x + a21 + a31 = S
Col2: a12 + y + a32 = S
Col3: a13 + a23 + z = S
Diag1: x + y + z = S
Diag2: a13 + y + a31 = S (since (1,3), (2,2), (3,1) but note that the antidiagonal includes (1,3) = a13, (2,2) = y, (3,1) = a31; yes)
Wait careful: The main diagonal is from top left to bottom right: (1,1) = x, (2,2)=y, (3,3)=z. That sum must equal S. The other diagonal (top right to bottom left) is (1,3)=a13, (2,2)=y, (3,1)=a31. That sum must also equal S.
Thus we have multiple equations.
We have unknowns x, y, z, and S (which is also unknown). However, we can treat x, y, z in terms of S from row and column equations, but they must be consistent and also satisfy diagonal equations.
From row equations:
x = S - (a12 + a13)
y = S - (a21 + a23)
z = S - (a31 + a32)
From column equations:
x = S - (a21 + a31)
y = S - (a12 + a32)
z = S - (a13 + a23)
Thus we have expressions for x, y, z from rows, and from columns, they must be equal, giving constraints on S.
Set:
x_row = S - R1 where R1 = a12 + a13
x_col = S - C1 where C1 = a21 + a31
Equality: S - R1 = S - C1 => R1 = C1. That would imply a12 + a13 = a21 + a31. But is that necessarily true? Not necessarily; if the original magic square is consistent, this must hold? Wait, we are given a partially filled magic square; the known numbers are from some original magic square. That original magic square satisfied these equalities. So the known numbers do satisfy that a12 + a13 = a21 + a31? Let's check: For any magic square, the sum of the numbers in the first row (excluding x) equals sum of first column (excluding x). Indeed from row1 sum = S = x + a12 + a13, column1 sum = S = x + a21 + a31 => a12 + a13 = a21 + a31. So indeed the known numbers must satisfy that. But is that guaranteed by the problem? It says "the Little Elephant remembered one magic square. He started writing this square ... but forgot all three elements of the main diagonal." So the given numbers are the off-diagonal entries of some magic square. So they satisfy all the relations that come from the magic square property. However, note: When we have the given numbers, they are exactly the off-diagonal entries. Those should be consistent with there existing some x,y,z such that magic square holds. So they should satisfy conditions like: a12 + a13 = a21 + a31, a12 + a32 = a21 + a23? Let's derive all consistency conditions.
Given a magic square, we have:
Row1: x + a12 + a13 = S
Row2: a21 + y + a23 = S
Row3: a31 + a32 + z = S
Col1: x + a21 + a31 = S
Col2: a12 + y + a32 = S
Col3: a13 + a23 + z = S
Main diag: x + y + z = S
Anti diag: a13 + y + a31 = S
We can eliminate x, y, z to derive constraints among the known a's.
One approach: Solve for x, y, z from some equations and plug into others.
We can use that from rows and columns we have expressions. For consistency, the values from rows and columns for each unknown must be equal, leading to three equations:
(1) a12 + a13 = a21 + a31 (since x_row = S - R1, x_col = S - C1 => equality => R1 = C1)
(2) a21 + a23 = a12 + a32 (since y from row2: y = S - (a21+a23); from col2: y = S - (a12+a32) => a21 + a23 = a12 + a32)
(3) a31 + a32 = a13 + a23 (since z from row3: z = S - (a31+a32); from col3: z = S - (a13+a23) => a31 + a32 = a13 + a23)
These are necessary conditions for the existence of a magic square with those off-diagonals. Since the notes come from a valid magic square, these conditions should hold. However, the problem statement doesn't explicitly guarantee that the given numbers satisfy those; but since they come from a magic square, they must. And it says "It is guaranteed that there exists at least one magic square that meets the conditions." So indeed the given numbers are such that there exist x,y,z making a magic square.
Thus we can rely on these equalities to simplify.
Now also we have the anti-diagonal condition: a13 + y + a31 = S. And we have expression for y: from row2, y = S - (a21 + a23). Then plug in: a13 + (S - (a21+a23)) + a31 = S => a13 - (a21+a23) + a31 = 0 => a13 + a31 = a21 + a23. But this is another relation. However, note that from (2) we have a21 + a23 = a12 + a32. And from (3) we have a31 + a32 = a13 + a23. So the anti-diag condition might be implied? Let's check. From (3) we have a31 + a32 = a13 + a23 => a31 - a13 = a23 - a32. Not exactly the same. Let's derive anti-diag condition: a13 + y + a31 = S => y = S - a13 - a31. But from row2: y = S - (a21 + a23). So S - a13 - a31 = S - (a21 + a23) => a13 + a31 = a21 + a23. So this gives: a13 + a31 = a21 + a23.
This is not directly implied by (1),(2),(3) perhaps but is another necessary condition. Actually, let's see if it follows from (1)-(3). Compute: a21 + a23 = ? Using (2) it's equal to a12 + a32. So anti-diag condition becomes a13 + a31 = a12 + a32. Is that true? Not necessarily. But maybe it is forced by the existence of x,y,z positive. Since all these conditions come from the same magic square, they must all hold. So the given numbers must satisfy a13 + a31 = a21 + a23, or equivalently a13 + a31 = a12 + a32 (by (2)), etc.
We can also derive the main diag condition: x + y + z = S. Using row expressions: x = S - (a12 + a13), y = S - (a21 + a23), z = S - (a31 + a32). Sum: (S - (a12+a13)) + (S - (a21+a23)) + (S - (a31+a32)) = 3S - (sum of all off-diagonals) = S => 3S - (sum_off) = S => 2S = sum_off => S = sum_off / 2.
Thus the sum of all known off-diagonal entries (the six numbers) must be even, and S is half of that. And then we can compute x, y, z as S minus row sums (or col sums) of known numbers. That yields positive integers? Guarantee at least one solution exists, so these computed x,y,z will be positive (and <=1e5 presumably). So maybe the solution is uniquely determined: compute S = (sum of all given numbers) / 2; then compute x = S - (a12 + a13), y = S - (a21 + a23), z = S - (a31 + a32). Check if they also satisfy column constraints automatically if the known numbers are consistent? Let's verify.
Given the existence of some magic square, we can derive S must equal half the sum of all six given numbers. Indeed, from the equations:
We have total sum of all 9 cells = sum(all) = 3S (since sum of rows = 3S). Also sum(all) = x+y+z + sum_off (six known). And x+y+z = S (main diag sum = S). So sum(all) = S + sum_off. Thus 3S = S + sum_off => 2S = sum_off => S = sum_off/2. So indeed S is determined solely by the six known numbers. Since the problem guarantees existence, sum_off must be even. Then x,y,z computed as above will satisfy all constraints? Let's check.
If we set S = sum_off/2, and then define x = S - (a12 + a13), etc. Then we must check that these also satisfy column equations and diagonal equations. But we can use the consistency conditions to guarantee that automatically if the given numbers came from a magic square? However, what if the given numbers are arbitrary but there exists some magic square? Then these conditions must hold, so the above computation yields a valid magic square. Let's verify systematically:
Given six numbers a12, a13, a21, a23, a31, a32, such that there exist x,y,z positive integers and S such that the 3x3 is a magic square. Then as derived, S = (sum of six)/2, and x = S - (a12+a13) etc. And the column sums will be satisfied? Let's see: column1 sum = x + a21 + a31 = (S - (a12+a13)) + a21 + a31 = S + (a21 + a31 - a12 - a13). For this to equal S, we need a21 + a31 = a12 + a13. That is one of the consistency conditions (1). Since there exists a magic square, that condition must hold. Similarly for other columns. Also anti-diagonal: a13 + y + a31 = a13 + (S - (a21+a23)) + a31 = S + (a13 + a31 - a21 - a23). Need a13 + a31 = a21 + a23, which is another condition. And main diagonal: x + y + z = (S - (a12+a13)) + (S - (a21+a23)) + (S - (a31+a32)) = 3S - sum_off = 3S - 2S = S, so that's automatically satisfied given S = sum_off/2.
Thus if the six given numbers satisfy the three pairwise equalities:
(1) a12 + a13 = a21 + a31
(2) a21 + a23 = a12 + a32
(3) a31 + a32 = a13 + a23
and also the anti-diagonal condition (which may be redundant?) Actually anti-diagonal is a13 + a31 = a21 + a23. But from (2) we have a21 + a23 = a12 + a32, so anti-diagonal condition becomes a13 + a31 = a12 + a32, which is not automatically guaranteed by (1)-(3). Let's check if (1)-(3) imply that. For a magic square, all six conditions (row, column, diag sums equal) must hold. However, it's possible that (1)-(3) along with S = sum_off/2 is enough? Let's test with numbers.
We want to see if given any six numbers satisfying (1), (2), (3) and having sum_off even, the computed x,y,z will automatically satisfy anti-diagonal? Let's compute anti-diagonal sum using computed x,y,z: a13 + y + a31 = a13 + (S - (a21+a23)) + a31 = S + (a13 + a31 - a21 - a23). So we need a13 + a31 = a21 + a23. That's a fourth condition. So not automatically implied. But does the existence of a magic square imply all these? Yes, so the given numbers must satisfy all. Since the problem guarantees existence, we can trust that the given numbers satisfy all necessary conditions. However, are these conditions independent? Let's see if from S = sum_off/2 plus row and column constraints we get all conditions automatically? Actually if we have x,y,z defined by rows and columns, the column equalities gave conditions (1)-(3). But anti-diagonal gave another condition. However, we might not need to check them; the problem says there exists at least one magic square. So we can simply compute S as half the sum of all given numbers, and then compute x,y,z via rows (or columns) and output. We should verify that these computed x,y,z are positive integers (and ≤ 1e5). The guarantee ensures there is at least one magic square, but does it ensure that this computed one is a valid magic square? Possibly the magic square that the Elephant remembered is not necessarily the one we compute via rows if there are multiple solutions? However, the derivation that S must equal sum_off/2 is forced by the requirement that the sum of all 9 numbers is 3S and also x+y+z = S. That holds for any magic square. So S is uniquely determined for any magic square consistent with the given off-diagonal numbers. Because sum_off is fixed, and S = sum_off/2. So regardless of which magic square (with those off-diagonals) exists, S is forced. Then x = S - (a12 + a13) is forced from row1 sum = S? Actually row1 sum must be S, so x = S - (a12 + a13) regardless of other constraints. So x is forced. Similarly y and z from rows. Thus there is at most one possible magic square that fits the given off-diagonal numbers and the magic square property. Wait, is that always true? Let's double-check: In a magic square, rows sum to S. So row1 gives x = S - (a12 + a13). So indeed x is determined once S is known. Similarly y and z. So if S is determined uniquely (from sum_off), then the magic square is uniquely determined. However, is S always uniquely determined? From earlier we derived 2S = sum_off, using the property that the sum of all numbers = 3S and also sum = x+y+z + sum_off = S + sum_off. So 3S = S + sum_off => 2S = sum_off. This derivation uses that x+y+z = S (main diagonal sum). But is it necessary that the main diagonal sum equals S? Yes, by definition of magic square, both diagonals sum to S. So that holds. So S is forced. So the magic square, if it exists, is uniquely determined (given fixed off-diagonals). So we can compute it straightforwardly: sum all given six numbers; if even, S = sum/2; compute x,y,z. Check that they are positive (and ≤ 1e5). But must also check that column sums and diagonal sums hold. Since existence is guaranteed, these checks will pass automatically if the numbers satisfy the magic square conditions? But if we just compute using row sums, column sums may not equal S if the given numbers are not consistent? But existence guarantee ensures there is at least one assignment of x,y,z that works. That assignment must satisfy row sums equal S, and column sums equal S, etc. If we compute x,y,z via row sums, we get some numbers. Could there be a different S? Let's see: Suppose we had an assignment of x,y,z that yields a magic square. Then from row equations we have x = S - row1_off, etc. So our computed x,y,z are exactly those. So if there exists a magic square, our computed x,y,z will be exactly the correct ones, and they will satisfy all constraints. So we don't need to check further; we can output them.
But wait: What if the given off-diagonal numbers do not satisfy the consistency conditions? Then there would be no magic square. But problem guarantees existence. So they must satisfy all needed equalities.
Thus the solution is trivial: Read the matrix. Let a12, a13 be the numbers in positions (1,2) and (1,3). a21, a23 from row2 col1 and col3? Actually careful: row2: a21 is (2,1), a22 is zero, a23 is (2,3). row3: a31 (3,1), a32 (3,2), a33 zero. So we have six known numbers: (1,2), (1,3), (2,1), (2,3), (3,1), (3,2). Compute sum_off = these six numbers. Then S = sum_off / 2. Then compute:
x = S - ((1,2)+(1,3))
y = S - ((2,1)+(2,3))
z = S - ((3,1)+(3,2))
Output these x,y,z in the diagonal positions.
We must ensure that S is integer (sum_off even). Guarantee ensures that.
Also we need to ensure that x,y,z are positive integers and ≤ 1e5. Guarantee ensures there is at least one solution, which would be this solution, so they should be positive and ≤ 1e5. However, it might be that there exist other solutions if the consistency conditions are not met? But existence guarantee ensures this solution works. But I'd like to double-check the positivity and bound constraints: Could it be that this computed x,y,z are ≤ 0 or >1e5? The problem states that all elements of the original magic square did not exceed 10^5. So the original magic square that the Elephant remembered had all entries ≤ 1e5. The given known numbers are from that square, so they are ≤ 1e5. The computed x,y,z are exactly the original diagonal entries, so they will also be ≤ 1e5. And positive because magic square elements are positive (the problem says "each cell contains some positive integer"). So positivity holds.
Thus the answer is uniquely determined and trivial.
But wait: The problem statement says "all elements of the magic square did not exceed 10^5." It does not explicitly say that all numbers are positive? It says "each cell contains some positive integer." So indeed positive. So our computed diagonal entries are positive.
Let's test with examples.
Example 1:
Input:
0 1 1
1 0 1
1 1 0
Known: a12=1, a13=1, a21=1, a23=1, a31=1, a32=1.
Sum_off = 6 => S = 3.
x = 3 - (1+1) = 1; y = 3 - (1+1) = 1; z = 3 - (1+1) = 1.
Output all ones. That matches.
Example 2:
0 3 6
5 0 5
4 7 0
Known: a12=3, a13=6; a21=5, a23=5; a31=4, a32=7.
Sum_off = 3+6+5+5+4+7 = 30 => S = 15.
x = 15 - (3+6) = 6; y = 15 - (5+5) = 5; z = 15 - (4+7) = 4.
Output:
6 3 6
5 5 5
4 7 4
Matches example.
Thus solution is trivial.
But wait: Are there any edge cases where sum_off/2 leads to S not integer? But guaranteed existence, so even.
Thus implementation: Read three lines, each three ints. Store in 3x3 array. Identify zero positions on main diagonal? Actually we know exactly positions (0,0), (1,1), (2,2) are zeros. But we can compute sum_off by summing all entries except those with i==j? Actually the zeros are on diagonal but they are placeholders. We sum the six numbers that are not on the main diagonal. Since the input ensures exactly three zeroes and they are all on main diagonal, we can just sum all numbers read (since zeros don't affect sum) and that's sum_off. But careful: Are there any other zeros? No, guarantee says exactly three zeroes and they are all on main diagonal. So all other entries are positive. So sum_off = total sum of all nine entries (since zeros contribute 0). But we can also sum only off-diagonal entries to be safe. But either way, total sum = sum_off because diagonal entries are zero. So we can compute total sum of all nine numbers = sum_off. Then S = total_sum / 2. Then compute x = S - (a[0][1] + a[0][2]); y = S - (a[1][0] + a[1][2]); z = S - (a[2][0] + a[2][1]). Then set a[0][0]=x, a[1][1]=y, a[2][2]=z, and output.
But wait: check positivity: we must ensure x,y,z > 0. The problem guarantees existence, so they will be >0.
Thus solution done.
However, I want to be thorough: Could there be a scenario where the given numbers are such that there exist multiple magic squares? According to our derivation, S is forced, and x,y,z forced, so unique. So any valid solution must be that one. So it's fine.
Thus implementation in Python: read lines, parse ints, compute total sum, S = total_sum // 2, compute diagonal values, assign, print matrix.
But careful: the total sum may be odd? The guarantee ensures existence, so even.
Check constraints: numbers up to 1e5, sum up to 6e5, S up to 3e5, x,y,z could be up to maybe 1e5? Actually x = S - (a12+a13). Since S can be up to 300k and a12+a13 up to 200k, x up to 300k, but the problem says original magic square entries not exceed 1e5. Since the original entries satisfy that, S would be at most? If all entries ≤ 1e5, then S ≤ 3e5? Actually max sum of row if each entry ≤ 1e5, row sum ≤ 3e5. So S ≤ 300000. But x = S - (a12+a13) ≤ 1e5 because x is original entry ≤ 1e5. So computed x should be ≤ 1e5. So fine.
Implementation details:
Pseudo:
def solve():
a = [list(map(int, input().split())) for _ in range(3)]
total = sum(a[i][j] for i in range(3) for j in range(3))
S = total // 2 # integer division, but we trust even.
a[0][0] = S - (a[0][1] + a[0][2])
a[1][1] = S - (a[1][0] + a[1][2])
a[2][2] = S - (a[2][0] + a[2][1])
# output
for row in a:
print(' '.join(map(str, row)))
But careful: The input might have zeros also on diagonal, but the given numbers also might have zeros elsewhere? Problem says exactly three zeros on main diagonal, all other numbers are positive (non-zero). So safe.
Double-check: The total sum includes zeros on diagonal, so total = sum of six off-diagonals. So S = total/2.
But what if the zeros are not on diagonal? Guarantee says they are on main diagonal. So fine.
Thus solution is simple.
But wait: Is there any twist? The problem might be from Codeforces (CF 259 B? Actually problem: "Little Elephant and Magic Square" maybe CF 259B?). Yes, I recall a problem where you need to fill diagonal such that square is magic. The solution is to set S = sum_off/2. So it's straightforward.
But let's confirm that we don't need to worry about overflow or negative numbers. Since existence guarantee, the computed values are positive and ≤ 1e5.
Potential pitfalls: Input may have extra spaces or lines? Use standard reading.
Thus answer is as above.
But let's double-check with some custom test:
Input:
0 2 2
2 0 2
2 2 0
Sum off = 2+2+2+2+2+2 = 12 => S=6. x=6-(2+2)=2; y=6-(2+2)=2; z=6-(2+2)=2. Output all 2's. That is a magic square. Good.
What if input:
0 1 2
3 0 4
5 6 0
Sum = 1+2+3+4+5+6=21, odd. So cannot exist. But guarantee says there exists at least one, so such input won't be given.
Ok.
Thus final solution is trivial.
But we might also consider verifying if any other solution possible? Not needed.
Thus I'll produce solution explanation and code.
Implementation details: Since constraints small, any language works.
We'll output the filled square.
Edge case: The computed S may be not integer if total sum odd; but we trust input.
So final answer: read, compute sum, set S = sum // 2, fill diagonal.
I'll present in Python.
Now, produce final answer.
```python
import sys
def solve() -> None:
# Read the 3x3 grid
a = [list(map(int, sys.stdin.readline().split())) for _ in range(3)]
# Sum of all given numbers (the three diagonal zeros contribute nothing)
total = sum(a[i][j] for i in range(3) for j in range(3))
# Magic constant
S = total // 2
# Fill the main diagonal
a[0][0] = S - (a[0][1] + a[0][2])
a[1][1] = S - (a[1][0] + a[1][2])
a[2][2] = S - (a[2][0] + a[2][1])
# Output the restored magic square
for row in a:
print(' '.join(map(str, row)))
if name == "main":
solve()