time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

 

ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid on the entrance which is filled with integers. Chris noticed that exactly one of the cells in the grid is empty, and to enter Udayland, they need to fill a positive integer into the empty cell.

Chris tried filling in random numbers but it didn't work. ZS the Coder realizes that they need to fill in a positive integer such that the numbers in the grid form a magic square. This means that he has to fill in a positive integer so that the sum of the numbers in each row of the grid (), each column of the grid (), and the two long diagonals of the grid (the main diagonal —  and the secondary diagonal — ) are equal.

Chris doesn't know what number to fill in. Can you help Chris find the correct positive integer to fill in or determine that it is impossible?

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 500) — the number of rows and columns of the magic grid.

n lines follow, each of them contains n integers. The j-th number in the i-th of them denotes ai, j (1 ≤ ai, j ≤ 109 or ai, j = 0), the number in the i-th row and j-th column of the magic grid. If the corresponding cell is empty, ai, j will be equal to 0. Otherwise, ai, j is positive.

It is guaranteed that there is exactly one pair of integers i, j (1 ≤ i, j ≤ n) such that ai, j = 0.

Output

Output a single integer, the positive integer x (1 ≤ x ≤ 1018) that should be filled in the empty cell so that the whole grid becomes a magic square. If such positive integer x does not exist, output  - 1 instead.

If there are multiple solutions, you may print any of them.

Examples
input
3
4 0 2
3 5 7
8 1 6
output
9
input
4
1 1 1 1
1 1 0 1
1 1 1 1
1 1 1 1
output
1
input
4
1 1 1 1
1 1 0 1
1 1 2 1
1 1 1 1
output
-1
Note

In the first sample case, we can fill in 9 into the empty cell to make the resulting grid a magic square. Indeed,

The sum of numbers in each row is:

4 + 9 + 2 = 3 + 5 + 7 = 8 + 1 + 6 = 15.

The sum of numbers in each column is:

4 + 3 + 8 = 9 + 5 + 1 = 2 + 7 + 6 = 15.

The sum of numbers in the two diagonals is:

4 + 5 + 6 = 2 + 5 + 8 = 15.

In the third sample case, it is impossible to fill a number in the empty square such that the resulting grid is a magic square.

题意:

将0换为任意正整数使矩阵的每一行的和和每一列的和以及正反对角线的和都相等。

这场没有上分的罪魁祸首!!!!!!

注意两个特判:

当n==1时;

当结果res<=0时;

附AC代码:

 #include<bits/stdc++.h>
using namespace std; long long a[][];
long long sumx[];
long long sumy[]; int main(){
int n;
cin>>n; int x,y;
memset(sumx,,sizeof(sumx));
memset(sumy,,sizeof(sumy));
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
cin>>a[i][j];
sumx[i]+=a[i][j];
sumy[j]+=a[i][j];
if(a[i][j]==){
x=i;
y=j;
}
}
}
if(n==){
cout<<""<<endl;
return ;
}
long long res;
if(x!=){
res=sumx[]-sumx[x];
}
else{
res=sumx[]-sumx[x];
}
sumx[x]+=res;
sumy[y]+=res;
a[x][y]=res;
long long sums=,sumt=;
for(int i=;i<=n;i++){
sums+=a[i][i];
sumt+=a[i][n-i+];
}
sort(sumx+,sumx+n+);
sort(sumy+,sumy+n+);
// cout<<sumx[1]<<" "<<sumx[n]<<" "<<sumy[1]<<" "<<sumy[n]<<" "<<sums<<" "<<sumt<<" "<<res;
if(sumx[]==sumx[n]&&sumy[]==sumy[n]&&sumx[]==sumy[n]&&sums==sumt&&sums==sumx[]&&res>){
cout<<res<<endl;
}
else{
cout<<"-1"<<endl;
}
return ;
}

B. Chris and Magic Square的更多相关文章

  1. codeforces 711B B. Chris and Magic Square(水题)

    题目链接: B. Chris and Magic Square 题意: 问在那个空位子填哪个数可以使行列对角线的和相等,就先找一行或者一列算出那个数,再验证是否可行就好; AC代码: #include ...

  2. Codeforces Round #369 (Div. 2) B. Chris and Magic Square 水题

    B. Chris and Magic Square 题目连接: http://www.codeforces.com/contest/711/problem/B Description ZS the C ...

  3. Codeforces Round #369 (Div. 2) B. Chris and Magic Square (暴力)

    Chris and Magic Square 题目链接: http://codeforces.com/contest/711/problem/B Description ZS the Coder an ...

  4. Chris and Magic Square CodeForces - 711B

    ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid o ...

  5. codeforces #369div2 B. Chris and Magic Square

    题目:在网格某一处填入一个正整数,使得网格每行,每列以及两条主对角线的和都相等 题目链接:http://codeforces.com/contest/711/problem/B 分析:题目不难,找到要 ...

  6. codeforces 711B - Chris and Magic Square(矩阵0位置填数)

    题目链接:http://codeforces.com/problemset/problem/711/B 题目大意: 输入 n ,输入 n*n 的矩阵,有一个占位 0 , 求得将 0 位置换成其他的整数 ...

  7. CodeForces 711B Chris and Magic Square (暴力,水题)

    题意:给定n*n个矩阵,其中只有一个格子是0,让你填上一个数,使得所有的行列的对角线的和都相等. 析:首先n为1,就随便填,然后就是除了0这一行或者这一列,那么一定有其他的行列是完整的,所以,先把其他 ...

  8. 【模拟】Codeforces 711B Chris and Magic Square

    题目链接: http://codeforces.com/problemset/problem/711/B 题目大意: N*N的矩阵,有且只有一个0,求要把这个矩阵变成幻方要填什么正数.无解输出-1.幻 ...

  9. CodeForces 711B Chris and Magic Square

    简单题. 找一个不存在$0$的行,计算这行的和(记为$sum$),然后就可以知道$0$那个位置应该填的数字(记为$x$). 如果$x<=0$,那么无解,否则再去判断每一行,每一列以及两个斜对角的 ...

随机推荐

  1. MMT事务处理来源类型-INV_OBJECT_GENEALOGY.GETSOURCE

    INV_OBJECT_GENEALOGY.GETSOURCE (MTL_MATERIAL_TRANSACTIONS.ORGANIZATION_ID,                           ...

  2. NPOI操作Excel 005:写入空Excel(Winform版)

    前文写了一个BS版本号的导出Excel的样例(http://blog.csdn.net/yysyangyangyangshan/article/details/47904119).对于CS版在保存的地 ...

  3. 利用反射技术实现POJO的数据库操作

    记得第一次写项目的时候,傻傻的数据库一张表,代码里就写一个DAO类,几张表就写几个DAO类,大量的反复代码,自己粘着都嫌烦,后来接触了Hibernate,不得不说对我们这样的小白用处还是非常大的.那么 ...

  4. candy——动态规划

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  5. mac上的xampp出现Access forbidden! You don’t have permission to access the requested object. It is either

    一个Joomla!程序,之前是在win上的xampp上运行得非常好的,当我把它拿到mac下面的xampp上去运行的时候,发现有问题,没法运行,报以下的错误: Access forbidden!  Yo ...

  6. oracle SQL语句(转)

    Oracle数据库语句大全 ORACLE支持五种类型的完整性约束 NOT NULL (非空)--防止NULL值进入指定的列,在单列基础上定义,默认情况下,ORACLE允许在任何列中有NULL值. CH ...

  7. java远程调用rmi入门实例

    RMI是Java的一组拥护开发分布式应用程序的API.RMI使用Java语言接口定义了远程对象,它集合了Java序列化和Java远程方法协议(Java Remote Method Protocol). ...

  8. 编程算法 - 数组中出现次数超过一半的数字 代码(C)

    数组中出现次数超过一半的数字 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 数组中有一个数字出现的次数超过数组长度的一半, 请找出这个数字. ...

  9. String,StringBuilder与StringBuffer的区别

    相信大家看到过很多比较String和StringBuffer区别的文章,也明白这两者的区别,然而自从Java 5.0发布以后,我们的比较列表上将多出一个对象了,这就是StringBuilder类.St ...

  10. crontab -e 定时任务中的 脚本文件 路径

    crontab -l 57 */1 * * * python /home/data/crontab_chk_url/personas/trunk/plugins/spider/chk_url_stat ...