## 缺氧

2015/12/03 Go the extra mile.

# cf#369-Chris and Magic Square-大模拟+trick

mdzz这题太坑，最后答案要是正数，

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;

const double pi=acos(-1.0);
double eps=0.000001;
typedef long long  ll;

ll mp[505][505];
ll sumx[505];
ll sumy[505];
ll dui1,dui2;
int  main()
{
ll n;
cin>>n;
ll val;
ll mx,my;
for (ll i=1; i<=n; i++)
{
for (ll j=1; j<=n; j++)
{
scanf("%lld",&mp[i][j]);
if (mp[i][j]==0)
{
mx=i,my=j;
}
}
}
if (n==1)
{
printf("1\n");
return 0;
}
for (ll i=1; i<=n; i++)
{
for (ll j=1; j<=n; j++)
{
sumx[i]+=mp[i][j];
sumy[j]+=mp[i][j];
if (i==j) dui1+=mp[i][j];
if (i+j==n+1) dui2+=mp[i][j];
}
}

if (mx!=my &&  mx+my!=n+1 )
{
ll flag=0,pos;
for (ll i=1; i<=n; i++)
{
if (sumx[i]!=dui1) flag++,pos=i;
}
if (flag!=1)
{
printf("-1\n");
return 0;
}
val=dui1-sumx[mx];
sumy[my]+=val;
flag=1;
for (ll i=1; i<=n; i++)
{
if (sumy[i]!=dui1) flag=0;
}
if (dui2!=dui1) flag=0;
if (flag);
else val=-1;
//printf("-1\n");
}
else  if ((n%2&& mx==my&&mx!=n/2+1)||(n%2==0 && mx==my))
{
ll aa;
for (aa=1; aa<=n; aa++)
if (aa!=mx) break;
val=sumx[aa]-dui1;
sumx[mx]+=val;
sumy[my]+=val;
dui1+=val;
ll  flag=1;
for (ll i=1; i<=n; i++)
{
if (sumy[i]!=sumx[aa]) flag=0;
}
for (ll i=1; i<=n; i++)
{
if (sumx[i]!=sumx[aa]) flag=0;
}
if (dui2!=sumx[aa]) flag=0;
if (flag);
else val=-1;
}
else if (mx+my==n+1&& !(mx==n/2+1&&my==n/2+1))
{

ll aa;
for (aa=1; aa<=n; aa++)
if (aa!=mx) break;
val=sumx[aa]-dui2;
sumx[mx]+=val;
sumy[my]+=val;
dui2+=val;
ll flag=1;
for (ll i=1; i<=n; i++)
{
if (sumy[i]!=sumx[aa]) flag=0;
}
for (ll i=1; i<=n; i++)
{
if (sumx[i]!=sumx[aa]) flag=0;
}
if (dui1!=sumx[aa]) flag=0;
if (flag);
else val=-1;
}
else
{

ll flag=1;
for (ll i=1; i<=n/2; i++)
if (sumx[i]!=sumx[1]) flag=0;
for (ll i=n/2+2; i<=n; i++)
if (sumx[i]!=sumx[1]) flag=0;
for (ll i=1; i<=n/2; i++)
if (sumy[i]!=sumx[1]) flag=0;
for (ll i=n/2+2; i<=n; i++)
if (sumy[i]!=sumx[1]) flag=0;
val=sumx[1]-dui1;
val=sumx[1]-dui1;
dui1+=val;
dui2+=val;
if (dui2!=sumx[1]) flag=0;
if (flag);
else val=-1;
}
if (val<=0)
val=-1;
printf("%lld\n",val);

return 0;

}

#### codeforces 452 C. Magic Trick

2016-03-23 21:40:43

#### UVA 769 - Magic of David Copperfield(构造问题)

2014-01-02 10:45:41

#### This is just a bit of fun. The idea came from an old magic t

2006年02月23日 6KB 下载

#### HDU-5983(大模拟)

2017-12-09 17:12:49

#### HDU 1107 武林（大模拟）

2016-07-17 16:20:50

#### 西安交大模拟电子技术基础课件

2011年04月10日 15.22MB 下载

2014-04-13 15:53:23

#### Codechef Magic Trick

2014-01-16 21:40:31

#### Magic Trick（水题）

2018-04-06 19:25:49

#### C. Magic Trick

2014-08-19 10:12:08