Codeforces Round #369 (Div. 2) B. Chris and Magic Square【数学,模拟】
来源:互联网 发布:巴诺巴奇果冻面膜知乎 编辑:程序博客网 时间:2024/05/20 10:56
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?
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 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.
34 0 23 5 78 1 6
9
41 1 1 11 1 0 11 1 1 11 1 1 1
1
41 1 1 11 1 0 11 1 2 11 1 1 1
-1
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.
原题链接:http://codeforces.com/contest/711/problem/
题意:输入一个n*n的矩阵,有一个位置为0,问你能否在0处填上一个正整数,使得该矩阵的每一行,每一列,主对角,副对角线上的和都相等,即构成一个幻方。
由于要构成一个幻方,所以只用根据行的和求出要填的数,再判断其它的情况是否相等就可以了。
AC代码:
#include <bits/stdc++.h>using namespace std;typedef long long LL;LL a[505][505];int main(){ //freopen("B.txt","r",stdin); int n; while(cin>>n) { int x,y; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { cin>>a[i][j]; if(a[i][j]==0) { x=i; y=j; } } } if(n==1) { cout<<"1"<<endl; continue; } LL sum2=0; LL sum1=0; for(int i=0; i<n; i++) { sum1+=a[x][i]; sum2+=a[(x+1)%n][i];//小技巧 } a[x][y]=sum2-sum1; bool flag=true; LL sum=0; for(int i=0; i<n; i++) //row,行 { sum=0; for(int j=0; j<n; j++) sum+=a[i][j]; if(sum!=sum2) { cout<<"-1"<<endl; flag=false; break; } } if(!flag) continue; for(int i=0; i<n; i++) //col,列 { sum=0; for(int j=0; j<n; j++) { sum+=a[j][i]; } if(sum!=sum2) { cout<<"-1"<<endl; flag=false; break; } } if(!flag) continue; sum=0; for(int i=0; i<n; i++)//主对角 sum+=a[i][i]; if(sum2!=sum) { cout<<"-1"<<endl; continue; } sum=0; for(int i=n-1; i>=0; i--)//副对角 sum+=a[i][n-i-1]; if(sum2!=sum) { cout<<"-1"<<endl; continue; } if(a[x][y]>0) cout<<a[x][y]<<endl; else cout<<"-1"<<endl; } return 0;}尊重原创,转载请注明出处:http://blog.csdn.net/hurmishine
- Codeforces Round #369 (Div. 2) B. Chris and Magic Square【数学,模拟】
- Codeforces Round #369 (Div. 2) B. Chris and Magic Square
- Codeforces Round #369 (Div. 2) B. Chris and Magic Square
- Codeforces Round #369 (Div. 2)B. Chris and Magic Square
- Codeforces Round #369 (Div. 2) B. Chris and Magic Square 数学、幻方
- 【Codeforces Round #369 (Div. 2)】Codeforces 711B Chris and Magic Square
- Codeforces #369(Div.2)B.Chris and Magic Square【思维】
- Codeforces #369(Div.2)B.Chris and Magic Square(矩阵找数)
- codeforces 711B. Chris and Magic Square【模拟】
- 【模拟】Codeforces 711B Chris and Magic Square
- 模拟-711B - Chris and Magic Square
- Codeforces 711B Chris and Magic Square
- Codeforces 711B Chris and Magic Square
- CodeForces 711B - Chris and Magic Square
- Codeforces 711B- Chris and Magic Square
- codeforces B. Chris and Magic Square
- Codeforces 711 B Chris and Magic Square
- codeforces#369Div2(711B)Chris and Magic Square
- C++ 对象的内存布局(上)
- C++推荐书
- #ifndef 与 #program once 的区别
- PAT(乙级)1006 1016. 部分A+B (15)
- 【jzoj4745】【看电影】
- Codeforces Round #369 (Div. 2) B. Chris and Magic Square【数学,模拟】
- 周易六十四卦——解卦
- 1.3节练习
- 快速排序
- Oracle 11g服务器结构
- 右移 int a=8; a>>1
- 123. Best Time to Buy and Sell Stock III
- BGRABitmap图像操作6:改变笔型,创建点线
- 搜索引擎与网络爬虫简述