【题目链接】:http://codeforces.com/contest/711/problem/B

【题意】



让你在矩阵中一个空白的地方填上一个正数;

使得这个矩阵两个对角线上的和;

每一行的和,每一列的和都相同;

【题解】



对于n=1的情况,任意输出一个数字就好;

对于n>1的情况;

先算出不包括空白格子的行的所有元素的和->he;

然后对于其他的可行的行和列,算出它们的和;

一旦与he不一样,直接输出-1无解;

然后包括空白格子的行和列,它们除了那个空白格子的和也要相同->设为sphe;

然后是对角线;

如果包括了空白格子,就先算出和;看看和sphe是不是一样;

如果不包括空白格子,算出的和直接与he比较;

两个对角线同理;

最后

sphe必须要小于he

即严格<不能相同

因为要为正数



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) cin >> x
#define pri(x) cout << x
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 510; LL a[N][N],hang[N],lie[N],he=-1;
int n,sx,sy; int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false);
rei(n);
if (n==1)
return pri(1<<endl),0;
rep1(i,1,n)
rep1(j,1,n)
{
rei(a[i][j]);
if (!a[i][j])
sx = i,sy = j;
}
rep1(i,1,n)
{
LL temp = 0;
rep1(j,1,n)
temp+=a[i][j];
hang[i] = temp;
if (sx==i) continue;
if (he==-1)
he = temp;
else
if (he!=temp)
return pri(-1<<endl),0;
}
rep1(j,1,n)
{
LL temp = 0;
rep1(i,1,n)
temp+=a[i][j];
lie[j] = temp;
if (sy==j) continue;
if (he!=temp)
return pri(-1<<endl),0;
}
bool in1 = false;
LL he1 = 0;
rep1(i,1,n)
{
if (i==sx && i==sy) in1 = true;
he1+=a[i][i];
}
if (!in1 && he1!=he) return pri(-1<<endl),0;
if (in1 && he1!=hang[sx]) return pri(-1<<endl),0;
bool in2 = false;
LL he2 = 0;
rep1(i,1,n)
{
if (i==sx && n-i+1==sy) in2 = true;
he2+=a[i][n-i+1];
}
if (!in2 && he2!=he) return pri(-1<<endl),0;
if (in2 && he2!=hang[sx]) return pri(-1<<endl),0;
if (hang[sx]<he)
{
pri(he-hang[sx]);
}
else
pri(-1<<endl);
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}

【codeforces 711B】Chris and Magic Square的更多相关文章

  1. codeforces 711B B. Chris and Magic Square(水题)

    题目链接: B. Chris and Magic Square 题意: 问在那个空位子填哪个数可以使行列对角线的和相等,就先找一行或者一列算出那个数,再验证是否可行就好; AC代码: #include ...

  2. 【codeforces 255D】Mr. Bender and Square

    [题目链接]:http://codeforces.com/problemset/problem/255/D [题意] 给你一个n*n的方框; 给你一个方块;(以下说的方块都是单位方块) 每一秒钟,可以 ...

  3. 【23.15%】【codeforces 703C】Chris and Road

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. codeforces #369div2 B. Chris and Magic Square

    题目:在网格某一处填入一个正整数,使得网格每行,每列以及两条主对角线的和都相等 题目链接:http://codeforces.com/contest/711/problem/B 分析:题目不难,找到要 ...

  5. 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 ...

  6. 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 ...

  7. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  8. 【硅谷问道】Chris Lattner 访谈录(下)

    [硅谷问道]Chris Lattner 访谈录(下) Chris Lattner 访谈录(下) 话题 Swift 在 Server 和操作系统方面有着怎样的雄心抱负? Swift 与 Objectiv ...

  9. 【硅谷问道】Chris Lattner 访谈录(上)

    [硅谷问道]Chris Lattner 访谈录(上) 话题 Chris Lattner 是谁? Xcode 的编译器 LLVM 背后有怎样的故事? Swift 诞生的前世今生,封闭的苹果为何要拥抱开源 ...

随机推荐

  1. go语言笔记——指针,和C用法以及本质一样,但不支持指针的+-运算!

    4.4.2 值类型和引用类型 所有像 int.float.bool 和 string 这些基本类型都属于值类型,使用这些类型的变量直接指向存在内存中的值. Go 语言的取地址符是 &,放到一个 ...

  2. 转 linux/unix学习经典书籍

    都是一些链接. 1. Linux网络编程经典书籍推荐 http://blog.csdn.net/zhangpeng_linux/article/details/7001970 2. C语言经典著作导读 ...

  3. nginx搭建基于http协议的视频点播服务器

    1,于由自己的服务器上已经安装好nginx(具体安装方法见我的另一篇文章,Linux中安装nginx),所以不再安装. 2,下载nginx_mod_h264_streaming-2.2.7.tar.g ...

  4. 基于Linux的v4l2视频架构驱动编写(转载)

    转自:http://www.linuxidc.com/Linux/2011-03/33022.htm 其实,我刚开始一直都不知道怎么写驱动,什么都不懂的,只知道我需要在做项目的过程中学习,所以,我就自 ...

  5. P3379最近公共祖先(LCA)

    题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...

  6. BZOJ 4811 树链剖分+线段树

    思路: 感觉这题也可神了.. (还是我太弱) 首先发现每一位不会互相影响,可以把每一位分开考虑,然后用树链剖分或者LCT维护这个树 修改直接修改,询问的时候算出来每一位填0,1经过这条链的变换之后得到 ...

  7. 消息队列 (2) java实现简单的RabbtMQ

    假设有如下问题: 1.如果消费者连接中断,这期间我们应该怎么办? 2.如何做到负载均衡? 3.如何有效的将数据发送到相关的接收者?就是怎么样过滤 4.如何保证消费者收到完整正确的数据 5.如何让优先级 ...

  8. python算数运算符

    ---恢复内容开始--- 加减乘除 >>> 1+1 2 >>> 4-2 2 >>> 2*5 10 >>> 8/2 4.0 > ...

  9. nodejs全局安装路径的位置

    一般nodejs安装在默认的C盘,如果不知道安装在哪里,可以打开控制面板-系统和安全-系统-高级配置中找到 所谓全局安装: 是指安装在node中node_module的根目录里,可以在电脑的任何位置调 ...

  10. dubbo之本地伪装

    本地伪装 本地伪装 1 通常用于服务降级,比如某验权服务,当服务提供方全部挂掉后,客户端不抛出异常,而是通过 Mock 数据返回授权失败. 在 spring 配置文件中按以下方式配置: <dub ...