【codeforces 711B】Chris and Magic Square
【题目链接】: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的更多相关文章
- codeforces 711B B. Chris and Magic Square(水题)
题目链接: B. Chris and Magic Square 题意: 问在那个空位子填哪个数可以使行列对角线的和相等,就先找一行或者一列算出那个数,再验证是否可行就好; AC代码: #include ...
- 【codeforces 255D】Mr. Bender and Square
[题目链接]:http://codeforces.com/problemset/problem/255/D [题意] 给你一个n*n的方框; 给你一个方块;(以下说的方块都是单位方块) 每一秒钟,可以 ...
- 【23.15%】【codeforces 703C】Chris and Road
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces #369div2 B. Chris and Magic Square
题目:在网格某一处填入一个正整数,使得网格每行,每列以及两条主对角线的和都相等 题目链接:http://codeforces.com/contest/711/problem/B 分析:题目不难,找到要 ...
- 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 ...
- 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 ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【硅谷问道】Chris Lattner 访谈录(下)
[硅谷问道]Chris Lattner 访谈录(下) Chris Lattner 访谈录(下) 话题 Swift 在 Server 和操作系统方面有着怎样的雄心抱负? Swift 与 Objectiv ...
- 【硅谷问道】Chris Lattner 访谈录(上)
[硅谷问道]Chris Lattner 访谈录(上) 话题 Chris Lattner 是谁? Xcode 的编译器 LLVM 背后有怎样的故事? Swift 诞生的前世今生,封闭的苹果为何要拥抱开源 ...
随机推荐
- 硬盘-RAID 5组建
没发正文之前本人先声明一下----本文是转载 这篇文章简直是太精彩了,呵呵 ,实在是忍不住了,一定要贴出来,让大家分享! 原作者:唐华 责任编辑:xiexiaojin 我们生活在一个历史记录在硬盘上的 ...
- [LeetCode] Unique Binary Search Tree
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 详细解析Linux scp命令的应用(转载)
转自:http://os.51cto.com/art/201003/187301.htm Linux scp命令用于Linux之间复制文件和目录,具体如何使用这里好好介绍一下,从本地复制到远程.从远程 ...
- CF1073C Vasya and Robot
CF题目难度普遍偏高啊-- 一个乱搞的做法.因为代价为最大下标减去最小的下标,那么可以看做一个区间的修改.我们枚举选取的区间的右端点,不难发现满足条件的左端点必然是不降的.那么用一个指针移一下就好了 ...
- akka设计模式系列-基础模式
本文介绍akka的基本使用方法,由于属于基础功能,想不出一个很高大上的名称,此处就以基础模式命名.下文会介绍actor的使用方法,及其优劣点. class SimpleActor(name:Strin ...
- 判断人员js
var allchooseEmpID = "";var allchooseEmpName = "";//自选经办人 function getJbrWinForM ...
- $stylus美化$
一直在用洛谷 当然不一定是洛谷 其他的网站也可以用 比如说codeforces 还是决定要美化一波 首先去找一个插件 叫做 stylus stylus下载的很多 我不想过多说明. Google和QQ浏 ...
- 《Typecript 入门教程》 2、访问控制符:public、private、protected、readonly
声明类的属性和方法时可以设置使用访问控制符,访问控制符设置类的属性和方法能不能在类的外部被访问 1. 默认为 public,使用public定义的属性和方法在类的内部和外部都可以访问 2. priva ...
- [ NOI 2005 ] 聪聪与可可
\(\\\) \(Description\) 一张\(N\)个点,\(M\)条边的有向图中,猫在\(A\)点,鼠在\(B\)点,每一秒两者按照以下规则移动: 猫先走去往老鼠所在地的最短路,可以走一步或 ...
- js基础---数据类型转换
js中数据类型: 简单数据类型: number:233,-34,0x23,023 string:"hello"或者'hello' boolean:true.false undefi ...