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

原创 2016年08月30日 17:56:46


智障完全不会做

直接分情况讨论

在主/副对角线和都不在的情况

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

需要特判ans<=0,输出-1

否则GGGGG 又挂了



#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 Round #369 (Div. 2) B. Chris and Magic Square【数学,模拟】

题意:输入一个n*n的矩阵,有一个位置为0,问你能否在0处填上一个正整数,使得该矩阵的每一行,每一列,主对角,副对角线上的和都相等,即构成一个幻方。 由于要构成一个幻方,所以只用根据行的和求出要填的数...

Codeforces Round #369 (Div. 2) B. Chris and Magic Square

B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megaby...

【Codeforces Round #369 (Div. 2)】Codeforces 711B Chris and Magic Square

模拟
  • sdfzyhx
  • sdfzyhx
  • 2016年08月30日 13:54
  • 141

codeforces#369Div2(711B)Chris and Magic Square

codeforces#369Div2BChris and Magic Square

CF-B. Chris and Magic Square(细节把握)

B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megaby...

codeforces 711B. Chris and Magic Square【模拟】

B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megaby...
  • Bcwan_
  • Bcwan_
  • 2016年08月29日 23:32
  • 591

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 711B- Chris and Magic Square

B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megaby...
  • LDUtyk
  • LDUtyk
  • 2016年08月31日 18:44
  • 417

B. Chris and Magic Square

B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megabytes in...

CodeForces 711B Chris and Magic Square(找规律)

题目: B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megabyte...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:cf#369-Chris and Magic Square-大模拟+trick
举报原因:
原因补充:

(最多只允许输入30个字)