B. Chris and Magic Square
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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?
InputThe 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.
OutputOutput 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.
Examplesinput3
4 0 2
3 5 7
8 1 6output9input4
1 1 1 1
1 1 0 1
1 1 1 1
1 1 1 1output1input4
1 1 1 1
1 1 0 1
1 1 2 1
1 1 1 1output-1NoteIn 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的更多相关文章
- codeforces 711B B. Chris and Magic Square(水题)
题目链接: B. Chris and Magic Square 题意: 问在那个空位子填哪个数可以使行列对角线的和相等,就先找一行或者一列算出那个数,再验证是否可行就好; AC代码: #include ...
- 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 ...
- 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 ...
- 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 ...
- codeforces #369div2 B. Chris and Magic Square
题目:在网格某一处填入一个正整数,使得网格每行,每列以及两条主对角线的和都相等 题目链接:http://codeforces.com/contest/711/problem/B 分析:题目不难,找到要 ...
- codeforces 711B - Chris and Magic Square(矩阵0位置填数)
题目链接:http://codeforces.com/problemset/problem/711/B 题目大意: 输入 n ,输入 n*n 的矩阵,有一个占位 0 , 求得将 0 位置换成其他的整数 ...
- CodeForces 711B Chris and Magic Square (暴力,水题)
题意:给定n*n个矩阵,其中只有一个格子是0,让你填上一个数,使得所有的行列的对角线的和都相等. 析:首先n为1,就随便填,然后就是除了0这一行或者这一列,那么一定有其他的行列是完整的,所以,先把其他 ...
- 【模拟】Codeforces 711B Chris and Magic Square
题目链接: http://codeforces.com/problemset/problem/711/B 题目大意: N*N的矩阵,有且只有一个0,求要把这个矩阵变成幻方要填什么正数.无解输出-1.幻 ...
- CodeForces 711B Chris and Magic Square
简单题. 找一个不存在$0$的行,计算这行的和(记为$sum$),然后就可以知道$0$那个位置应该填的数字(记为$x$). 如果$x<=0$,那么无解,否则再去判断每一行,每一列以及两个斜对角的 ...
随机推荐
- Mathematica 表达式求值
表达式是变量之间的运算关系.表达式求值就是对变量赋值并运算出结果的过程. 针对于Mathematica中的表达式.有两种方法获得其值. 一是对对应的变量直接赋值.这样对应的表达式在调用时便会直接运算得 ...
- BUPT复试专题—网络传输(2014网研)
题目描述 网络的高效互联与智能传输是提升海量用户服务请求映射效率的重要措施.在这个任务中,你需耍在最小的传输时间内,将数据源传输到指定的网络节点中.我们给定的网络一共包含N个节点,其中节点1为数据源. ...
- time machine不备份指定文件夹
osx中常常会使用timemachine来备份一些文件,timemachine能够使某个文件夹恢复到之前某个时刻的状态,很的方便.但是备份须要空间,特别是有些我们并不想备份一些无关紧要的文件,比方电影 ...
- com.sun.jdi.InvocationException occurred invoking method 异常
参考链接:https://stackoverflow.com/questions/4123628/com-sun-jdi-invocationexception-occurred-invoking-m ...
- LeetCode_3Sum Closest
一.题目 3Sum Closest Total Accepted: 32191 Total Submissions: 119262My Submissions Given an array S of ...
- XMLHTTPRequest DEMO(发送测试)
对于其中的HTTP状态,我们知道200-299表明访问成功:300-399表明需要客户端 反应来满足请求:400-499和500-599表明客户端和服务器出错:其中常用的如404表示资源没找到,403 ...
- ScaleYViewPager
https://github.com/eltld/ScaleYViewPager
- 阿里云安装nginx 启动失败的原因。
阿里云编译安装nginx服务器后启动一直报下面错误. 百度了一圈,看到一个说要先关掉apache服务,感觉这个好像是对的,立马做了下面操作. 果然把nginx起了起来. 从这边才知道apache和ng ...
- Xammp修改端口
How can I get XAMPP working on port 80 under Windows 10? By default, Windows 10 starts Microsoft IIS ...
- ios+Appium+Java
To run iOS tests, you can follow these steps : (Note : I am using Java language here in Eclipse IDE ...