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$,那么无解,否则再去判断每一行,每一列以及两个斜对角的 ...
随机推荐
- System.IO.Ports.SerialPort串口通信接收完整数据
C#中使用System.IO.Ports.SerialPort进行串口通信网上资料也很多,但都没有提及一些细节: 比如 串口有时候并不会一次性把你想要的数据全部传输给你,可能会分为1次,2次,3次分别 ...
- Android Studio一些常用的快捷键
光标移动和窗口切换:1.esc:光标从功能窗口回到编辑窗口 2.alt+num:打开指定的功能窗口,重复操作关闭该窗口. 3.alt+←→:切换编辑的文件. 4.ctrl+home/end:跳转到文件 ...
- Android:BLE智能硬件开发详解
目录 前言 BLE是个什么鬼 BLE中的角色分工 主要的关键词和概念 GATT(Generic Attribute Profile ) Characteristic Service Android如何 ...
- 【Todo】Java学习笔记 100==100 & Reflection API & Optional类详解 & DIP、IoC、DI & token/cookie/session管理会话方式
为什么1000 == 1000返回为False,而100 == 100会返回为True? Link Java Reflection API:Link Java8 Optional 类深度解析: L ...
- Solidworks如何绘制装饰螺纹线
1 插入-注解,装饰螺纹线 2 绘制装饰螺纹线,选择螺纹的边线,标准选择ISO,下面可以选择的范围就确定了(M6的孔,只能选择M8的螺纹或者M10的螺纹),画好之后在3D图中并没有明确的螺纹样式 ...
- STL algorithm算法max,max_elements(33)
max原型: std::max C++98 C++11 C++14 default (1) template <class T> const T& max (const T& ...
- Effective C++ 条款13/14 以对象管理资源 || 在资源管理类中小心拷贝行为
三.资源管理 资源就是一旦你使用了它,将来不用的时候必须归还系统.C++中最常用的资源就是动态内存分配.其实,资源还有 文件描述符.互斥器.图形界面中的字形.画刷.数据库连接.socket ...
- oracle 11G direct path read 非常美也非常伤人
direct path read 在11g中,全表扫描可能使用direct path read方式,绕过buffer cache,这种全表扫描就是物理读了. 在10g中,都是通过gc buffer来读 ...
- actionbar tab 字体大小设置
在styles.xml文件里加入以下的样式就可以 <!-- Application theme. --> <style name="AppTheme" p ...
- XMLHTTPRequest DEMO(发送测试)
对于其中的HTTP状态,我们知道200-299表明访问成功:300-399表明需要客户端 反应来满足请求:400-499和500-599表明客户端和服务器出错:其中常用的如404表示资源没找到,403 ...