void Add(char fx[],int y) { char fy[100]; itoa(y,fy,10); int xlen = strlen(fx); int ylen = strlen(fy); int i,j,up=0,h=0,k; if(xlen>=ylen) { for(i=ylen-1,j=xlen-1;j>=0;i--,j--) { if(i>=0) { k = (fx[j] - '0') + (fy[i] - '0') + up; if(k>=10) { k = k - 10; s[h] = k+'0'; h++; up = 1; } else { s[h] = k + '0'; h++; up = 0; } } else { k = (fx[j] - '0') + up; if(k>=10) { k = k -10; s[h] = k+'0'; h++; up = 1; } else { s[h] = k + '0'; h++; up = 0; } } } if(up==1) { s[h] = '1'; h++; } } else { for(i=xlen-1,j=ylen-1;j>=0;i--,j--) { if(i>=0) { k = (fx[i] - '0') + (fy[j] - '0') + up; if(k>=10) { k = k - 10; s[h] = k+'0'; h++; up = 1; } else { s[h] = k + '0'; h++; up = 0; } } else { k = (fy[j] - '0') + up; if(k>=10) { k = k - 10; s[h] = k+'0'; h++; up = 1; } else { s[h] = k + '0'; h++; up = 0; } } } if(up==1) { s[h] = '1'; h++; } } for(i=h-1,j=0;i>=0;i--,j++) { S[j] = s[i]; } }
# include<stdio.h> # include<stdlib.h> # include<string.h> char s[100]; char S[100]; void Add(char fx[],int y) { char fy[100]; itoa(y,fy,10); int xlen = strlen(fx); int ylen = strlen(fy); int i,j,up=0,h=0,k; for(i=0;i<100;i++) { S[i] = '\0'; } if(xlen>=ylen) { for(i=ylen-1,j=xlen-1;j>=0;i--,j--) { if(i>=0) { k = (fx[j] - '0') + (fy[i] - '0') + up; if(k>=10) { k = k - 10; s[h] = k+'0'; h++; up = 1; } else { s[h] = k + '0'; h++; up = 0; } } else { k = (fx[j] - '0') + up; if(k>=10) { k = k -10; s[h] = k+'0'; h++; up = 1; } else { s[h] = k + '0'; h++; up = 0; } } } if(up==1) { s[h] = '1'; h++; } } else { for(i=xlen-1,j=ylen-1;j>=0;i--,j--) { if(i>=0) { k = (fx[i] - '0') + (fy[j] - '0') + up; if(k>=10) { k = k - 10; s[h] = k+'0'; h++; up = 1; } else { s[h] = k + '0'; h++; up = 0; } } else { k = (fy[j] - '0') + up; if(k>=10) { k = k - 10; s[h] = k+'0'; h++; up = 1; } else { s[h] = k + '0'; h++; up = 0; } } } if(up==1) { s[h] = '1'; h++; } } for(i=h-1,j=0;i>=0;i--,j++) { S[j] = s[i]; } } void Jian(char fx[],char fy[]) { int xlen = strlen(fx); int ylen = strlen(fy); int i,j,up=0,h=0,k; for(i=0;i<100;i++) { S[i]='\0'; } for(i=ylen-1,j=xlen-1;j>=0;i--,j--) { if(i>=0) { k = (fx[j] - '0') - (fy[i] - '0') - up; if(k>=0) { up = 0; s[h] = k + '0'; h++; } else { k = k + 10; up = 1; s[h] = k + '0'; h++; } } else { k = (fx[j] - '0') - up; if(k>=0) { up = 0; s[h] = k + '0'; h++; } else { k = k + 10; up = 1; s[h] = k + '0'; h++; } } } for(i=h-1;i>=0;i--) { if(s[i]!='0') { break; } } if(i == -1) { strcpy(S,"0"); } else { for(i,j=0;i>=0;i--,j++) { S[j]=s[i]; } } } int main() { int n,i,j,row,col; int a[500][500]; scanf("%d",&n); for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); if(a[i][j]==0) { row = i; col = j; } } } int h=0; char x[1010][15],z[2][50]; strcpy(z[0],"0"); strcpy(z[1],"0"); for(i=0;i<1000;i++) { strcpy(x[i],"0"); } for(i=0;i<n;i++) { if(i!=row) { for(j=0;j<n;j++) { Add(x[h],a[i][j]); strcpy(x[h],S); } h++; } } for(i=0;i<n;i++) { if(i!=col) { for(j=0;j<n;j++) { Add(x[h],a[j][i]); strcpy(x[h],S); } h++; } } char key[100]; strcpy(key,x[0]); int flag = 0; for(i=1;i<h;i++) { if(strcmp(key,x[i])!=0) { printf("-1"); flag = 1; break; } } char sum1[100],sum2[100]; if(flag==0) { for(i=0;i<n;i++) { if(i!=row) { Add(sum1,a[i][col]); strcpy(sum1,S); } } for(i=0;i<n;i++) { if(i!=col) { Add(sum2,a[row][j]); strcpy(sum2,S); } } if(strcmp(key,x[i])==0) { flag = 2; } else { printf("-1"); } } if(flag==2) { Jian(key,sum1); a[row][col] = atoi(S); for(i=0;i<n;i++) { Add(z[0],a[i][i]); strcpy(z[0],S); Add(z[1],a[i][n-i-1]); strcpy(z[1],S); } if(strcmp(key,z[0])==0 && strcmp(key,z[1])==0) { printf("%d",a[row][col]); } else { printf("-1"); } } return 0; }