【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 诞生的前世今生,封闭的苹果为何要拥抱开源 ...
随机推荐
- Black Rock Shooter
在人气动漫 Black Rock shooter 中,当加贺里对麻陶 说出了"滚回去"以后,与此同时,在另一个心灵世界里, BRS 也遭到了敌人的攻击.此时,一共有 n 个攻击排成 ...
- python lambda表达式&map/filter/reduce
习条件运算时,对于简单的 if else 语句,可以使用三元运算来表示,即: 1 2 3 4 5 6 7 8 # 普通条件语句 if 1 == 1: name = 'wupeiqi' else ...
- Java 删除List元素的正确方式
方式一:使用Iterator的remove()方法 public class Test { public static void main(String[] args) { List<Strin ...
- skiing 暴力搜索 + 动态规划
我的代码上去就是 直接纯粹的 暴力 . 居然没有超时 200ms 可能数据比较小 一会在优化 #include<stdio.h> #include<string.h ...
- 题解报告:hdu 3790 最短路径问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起 ...
- UNIX环境高级编程--3
文件IO 函数lseek: 每个打开文件都有一个与其相关联的“当前文件偏移量”,用来度量从文件开始处计算的字节数.除非指定O_APPEND选项,否则该偏移量被置为0.如果文件描述符指向的是一个管道.F ...
- 342 Power of Four 4的幂
给定一个整数 (32位有符整数型),请写出一个函数来检验它是否是4的幂.示例:当 num = 16 时 ,返回 true . 当 num = 5时,返回 false.问题进阶:你能不使用循环/递归来解 ...
- [ USACO 2018 OPEN ] Out of Sorts (Platinum)
\(\\\) \(Description\) 对一长为\(N\)的数列\(A\)排序,不保证数列元素互异: 数列\(A\)中\(A[1...i]\)的最大值不大于\(A[i+1-N]\)的最小值,我们 ...
- Android 串口驱动和应用测试
这篇博客主要是通过一个简单的例子来了解Android的串口驱动和应用,为方便后续对Android串口服务和USB虚拟串口服务的了解.这个例子中,参考了<Linux Device Drivers& ...
- C#入门经典 Chapter4 流程控制
4.1布尔逻辑 布尔比较运算符 == != < > <= >= 处理布尔值的布尔值运算符 ! & | ^(异或) 条件布尔运算符 &&am ...