B - Chris and Magic Square
2016-08-31 本文已影响0人
布尔切利庄园
46 ms 2400 KB
#include<iostream>
#include<functional>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<time.h>
using namespace std;
#define pi(x,y) printf("%d%c",(x),(y));
#define pin(x) printf("%d\n",(x));
#define pln(x) printf("\n");
#define si(x) scanf("%d",&(x))
#define sii(x,y) scanf("%d%d",&(x),&(y))
#define s3(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define rep(x,y,z) for(int (x)=(y);(x)<(z);++(x))
#define dep(x,y,z) for(int (x)=(y)-1;(x)>=(z);--(x))
#define read int TcaseN;scanf("%d",&TcaseN);for(int Tcase=1;Tcase<=TcaseN;++Tcase)
#define cls(x,y) memset((x),(y),sizeof((x)));
#define cl(x) memset((x),0,sizeof((x)));
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define max3(value_a,value_b,value_c) max(max(value_a,value_b),value_c)
#define min3(value_a,value_b,value_c) min(min(value_a,value_b),value_c)
///In This You Can Define Long Integer Type
#define LONGTYPE long long
typedef LONGTYPE LL;
typedef unsigned LONGTYPE ULL;
const int inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const int maxn=1e5+5;
template <class T>
inline bool Read(T &ret) {
char c; int sgn;
if(c=getchar(),c==EOF) return 0; //EOF
while(c!='-'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-') ?-1:1 ;
ret=(c=='-') ?0:(c -'0');
while(c=getchar(),c>='0'&&c<='9')
ret=ret*10+(c-'0');
ret*=sgn;
return 1;
}
void Out(int a)
{ //输出外挂
if(a < 0)
{
putchar('-');
a = -a;
}
if(a >= 10)
Out(a / 10);
putchar(a % 10 + '0');
}
int main() {
int i,j,N,n,m,t;
Read(N);
LL mag[550][550];
LL hang[550];
LL lie[550];
rep(i,0,N){//i from 0 to N-1
rep(j,0,N){
Read(mag[i][j]);
hang[i]+=mag[i][j];
lie[j]+=mag[i][j];
if (mag[i][j]==0)
{
n=i;m=j;
}
}
}
LL sav;
if (n!=N-1)
sav=hang[n+1]-hang[n];
else
sav=hang[n-1]-hang[n];
mag[n][m]=sav;
LL sie=0;
LL qie=0;
cl(hang);cl(lie);
rep(i,0,N){//i from 0 to N-1
rep(j,0,N){
if (i==j)
{
sie+=mag[i][j];
}
if (i+j==N-1)
{
qie+=mag[i][j];
}
hang[i]+=mag[i][j];
lie[j]+=mag[i][j];
}
}
rep(i,0,N){//i from 0 to N-1
if (qie==sie)
{
if (hang[i]==lie[i]&&hang[i]==qie)
{
continue;
}
else{
pin(-1);
return 0;
}
}
else{
pin(-1);
return 0;
}
}
if(N==1)
{
pin(1);
return 0;
}
if(sav<=0) {
pin(-1);
return 0;
}
printf("%I64d\n",sav);
return 0;
}```