codeforces711B Chris and Magic Square 简单枚举

GPU 运维实战:H100/H800/B300 换卡与压测 (烧多久、看什么、什么算合格)。文末还给了一个能直接跑的验收判定脚本,三个真实场景的裁决输出都在。 阅读详情

B. Chris and Magic Square
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid on the entrance which is filled with integers. Chris noticed that exactly one of the cells in the grid is empty, and to enter Udayland, they need to fill a positive integer into the empty cell.

Chris tried filling in random numbers but it didn't work. ZS the Coder realizes that they need to fill in a positive integer such that the numbers in the grid form a magic square. This means that he has to fill in a positive integer so that the sum of the numbers in each row of the grid (), each column of the grid (), and the two long diagonals of the grid (the main diagonal —  and the secondary diagonal — ) are equal.

Chris doesn't know what number to fill in. Can you help Chris find the correct positive integer to fill in or determine that it is impossible?

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 500) — the number of rows and columns of the magic grid.

n lines follow, each of them contains n integers. The j-th number in the i-th of them denotes ai, j (1 ≤ ai, j ≤ 109 or ai, j = 0), the number in the i-th row and j-th column of the magic grid. If the corresponding cell is empty, ai, j will be equal to 0. Otherwise, ai, j is positive.

It is guaranteed that there is exactly one pair of integers i, j (1 ≤ i, j ≤ n) such that ai, j = 0.

Output

Output a single integer, the positive integer x (1 ≤ x ≤ 1018) that should be filled in the empty cell so that the whole grid becomes a magic square. If such positive integer x does not exist, output  - 1 instead.

If there are multiple solutions, you may print any of them.

Examples
input
3
4 0 2
3 5 7
8 1 6
output
9
input
4
1 1 1 1
1 1 0 1
1 1 1 1
1 1 1 1
output
1
input
4
1 1 1 1
1 1 0 1
1 1 2 1
1 1 1 1
output
-1
Note

In the first sample case, we can fill in 9 into the empty cell to make the resulting grid a magic square. Indeed,

The sum of numbers in each row is:

4 + 9 + 2 = 3 + 5 + 7 = 8 + 1 + 6 = 15.

The sum of numbers in each column is:

4 + 3 + 8 = 9 + 5 + 1 = 2 + 7 + 6 = 15.

The sum of numbers in the two diagonals is:

4 + 5 + 6 = 2 + 5 + 8 = 15.

In the third sample case, it is impossible to fill a number in the empty square such that the resulting grid is a magic square.



傻逼了,不知道一开始怎么想的……居然还你妹 Wrong answer on test 116 

还WA on 116了好几发……

幸亏调过了……那尼玛也很脑残啊……当时怎么想的……



#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <set>

using namespace std;

typedef long long ll;

const int maxn = 500;
int n;
ll a[maxn + 5][maxn + 5], res, ans, ans2;
bool flag, f, f2;

int main()
{
	ios::sync_with_stdio(false);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> a[i][j];
		}
	}
	if (n == 1) {
		cout << 1 << endl;
		return 0;
	}
	for (int i = 1; i <= n; i++) {
		flag = true; res = 0;
		for (int j = 1; j <= n; j++) {
			res += a[i][j];
			if (a[i][j] == 0) {
				flag = false;
				break;
			}
		}
		if (flag) break;
	}
#if 0
	cout << "res: " << res << endl;
#endif
	set<ll> s;
	for (int i = 1; i <= n; i++) {
		ans = 0; f = false;
		for (int j = 1; j <= n; j++) {
			ans += a[i][j];
			if (a[i][j] == 0) f = true;
		}
		if (f && ans >= res) {
			cout << -1 << endl;
			return 0;
		}
		if (res != ans) {
			if (f) {
				s.insert(res - ans);
			}
			else {
				puts("-1");
				return 0;
			}
		}
	}
#if 0
	cout << "ok1" << endl;
	cout << s.size() << endl;
	cout << *(s.begin()) << endl;
#endif
	for (int i = 1; i <= n; i++) {
		ans = 0; f = false;
		for (int j = 1; j <= n; j++) {
			ans += a[j][i];
			if (a[j][i] == 0) f = true;
		}
		if (f && ans >= res) {
			cout << -1 << endl;
			return 0;
		}
		if (res != ans) {
			if (f)
				s.insert(res - ans);
			else {
				puts("-1");
				return 0;
			}
		}
	}
#if 0
	cout << "ok2" << endl;
	cout << s.size() << endl;
	cout << *(s.begin()) << endl;
#endif
	ans = ans2 = 0; f = f2 = false;
	for (int i = 1; i <= n; i++) {
		ans += a[i][i];
		ans2 += a[i][n - i + 1];
		if (a[i][i] == 0) f = true;
		if (a[i][n - i + 1] == 0) f2 = true;
	}
#if 0
	cout << "testok" << endl;
	cout << ans << endl;
	cout << ans2 << endl;
#endif
	if ((f && ans >= res) || (f2 && ans2 >= res)) {
		cout << -1 << endl;
		return 0;
	}
#if 0
	cout << "ok34" << endl;
	cout << s.size() << endl;
	cout << *(s.begin()) << endl;
#endif
	if (res != ans) {
		if (f) {
			s.insert(res - ans);
		}
		else {
			puts("-1");
			return 0;
		}
	}
	if (res != ans2) {
		if (f2) {
			s.insert(res - ans2);
		}
		else {
			puts("-1");
			return 0;
		}
	}
	if (s.size() == 1 && *(s.begin()) >= 1 && *(s.begin()) <= 1e18) {
		cout << *(s.begin()) << endl;
	}
	else {
		cout << -1 << endl;
	}
	return 0;
}


转念一想,你妹判断个毛线啊,把该填的数直接填进去然后直接扫判断不就行了么,一发AC,扇自己一巴掌……啪……


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <set>

using namespace std;

typedef long long ll;

const int maxn = 500;
int n;
ll a[maxn + 5][maxn + 5], res, ans, x, y, tmp, tmp1, tmp2;
bool flag;

int main()
{
	ios::sync_with_stdio(false);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> a[i][j];
			if (a[i][j] == 0) {
				x = i; y = j;
			}
		}
	}
	if (n == 1) {
		cout << 1 << endl;
		return 0;
	}
	for (int i = 1; i <= n; i++) {
		flag = true; res = 0;
		for (int j = 1; j <= n; j++) {
			res += a[i][j];
			if (a[i][j] == 0) {
				flag = false;
				break;
			}
		}
		if (flag) break;
	}
	ans = 0;
	for (int j = 1; j <= n; j++) {
		ans += a[x][j];
	}
	if (ans < res) {
		a[x][y] = res - ans;
		flag = true;
		for (int i = 1; i <= n; i++) {
			tmp1 = tmp2 = 0;
			for (int j = 1; j <= n; j++) {
				tmp1 += a[i][j];
				tmp2 += a[j][i];
			}
			if (tmp1 != res || tmp2 != res) {
				flag = false;
				break;
			}
		}
		if (!flag) {
			cout << -1 << endl;
			return 0;
		}
		tmp1 = tmp2 = 0;
		for (int i = 1; i <= n; i++) {
			tmp1 += a[i][i];
			tmp2 += a[i][n - i + 1];
		}
		if (tmp1 != res || tmp2 != res) {
			cout << -1 << endl;
			return 0;
		}
		cout << a[x][y] << endl;
	}
	else {
		cout << -1 << endl;
	}
	return 0;
}


大规模新闻聚合爬虫实战:Python爬取数百万新闻数据的完整指南 本文详细介绍了如何构建一个大规模新闻聚合爬虫,结合Python异步爬虫技术,实现高效、稳定的新闻数据采集。增加NLP模块对新闻内容做自动分类、情感分析。引入机器学习模型检测新闻真实性。使用分布式爬虫框架进一步提升规模。增加多媒体内容抓取与处理。 阅读详情

相关推荐

MT6771 Android13 背光曲线调试与光感数据校准实战

本文详细介绍了MT6771平台在Android 13系统上进行背光曲线调试与光感数据校准的实战经验。针对高亮度环境下背光误调节问题,深入分析了光感数据上报机制,并提供了驱动层配置修改、背光曲线优化及常见问题解决方案,帮助开发者快速定位并解决类似问题。

moon9的博客 287

CodeForces - 711C Coloring Trees【给树染色】

Problem ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the park where n trees grow. They decided to be naughty and color the trees in the park. The trees are numbered with i

tjx41624560的博客 494

DAC8760高精度数模转换器原理与工业级嵌入式应用

数模转换器(DAC)是工业测控系统中实现数字指令到模拟执行的关键接口,其核心性能由分辨率、线性度、温漂及系统级TUE(总未调整误差)共同决定。DAC8760作为一款16位四象限精密DAC,集成了可编程增益放大器(PGA)、内部基准源与SPI高速接口,支持电压/电流双模输出,显著提升闭环控制精度与硬件适配灵活性。其技术价值在于将高精度模拟前端与数字控制深度耦合,满足PLC、ATE及激光偏置等严苛场景对长期稳定性与多通道同步性的要求。本文围绕DAC8760的寄存器配置、HAL/LL驱动实践、PCB布局规范、现场

weixin_42315569的博客 581

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 on the entrance which is filled with integers. Chris noticed that exactly one of the cells i...

weixin_30323631的博客 170

ESP32+micro-ROS机器人开发实战:轻量级ROS2嵌入式集成

ROS2是现代机器人软件栈的核心框架,其基于DDS的分布式通信与QoS保障能力为多节点协同提供基础支撑;而micro-ROS作为专为微控制器优化的ROS2兼容中间件,解决了资源受限设备(如ESP32)接入ROS2生态的关键难题。其通过分层架构将DDS通信下沉至上位机Agent,使MCU仅需运行轻量Client,大幅降低内存开销(实测<180KB)并保留标准ROS2接口语义。该方案广泛应用于教育机器人、移动底盘、传感器节点等嵌入式场景,尤其适合基于ESP-IDF开发的Wi-Fi/BLE物联网机器人系统。本文以

weixin_29025501的博客 398

CCS TMSC6748 报错Error connecting to the target: (Error -600 @ 0x0)

CCS TMSC6748 报错Error connecting to the target:(Error -600@ 0x0)

AS_the_deer1717的博客 6632

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 megabytes input standard input output standard output ZS the Coder and Chris the Baboon

Coldplay 751

STM32F103用软件I2C读取IP2366电量数据,实测避坑指南(附完整源码)

本文详细介绍了如何使用STM32F103的软件I2C驱动读取IP2366电池管理芯片的电量数据,包括GPIO初始化、I2C时序实现、关键延时优化及常见问题解决方案。通过实测避坑指南和完整源码,帮助开发者快速实现稳定的电量数据读取,适用于嵌入式电池管理系统开发。

weixin_33725722的博客 512

Codeforces 711c 简单dp

C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output ZS the Coder and Chris the Baboon has ar...

297

ANFIS实现数据非线性回归附Matlab代码

在本文中,我们将介绍如何使用Matlab实现基于MSE的ANFIS非线性回归,并提供相应的源代码。ANFIS是一种融合了模糊逻辑和神经网络的方法,它可以通过学习数据集中的模式来构建一个模糊推理系统。ANFIS的训练过程基于最小化MSE,以提高模型的拟合能力。通过上述代码示例,我们可以使用基于MSE的ANFIS模型对数据进行非线性回归,并获得相应的预测结果。在示例中,我们定义了一个包含两个样本的新输入数据集 X_new,然后使用训练好的ANFIS模型进行预测,并将结果存储在变量 Y_pred 中。

2301_79326588的博客 473

OpenClaw 多代理协同工作模式配置指南

OpenClaw 采用。

形上得其象,形下合于数;阴阳自济,玄理自明。 1万+

Codeforces #369(Div.2)B.Chris and Magic Square【思维】

B. Chris and Magic Square time limit per test  2 seconds memory limit per test  256 megabytes input  standard input output  standard output ZS the Coder and Chris the Ba

mengxiang000000 537

(持续更新中!)详解【计算机类&面试真题】军队文职考试 ——第二期(真题+解析)| 网络协议的三个核心要素;交互式系统中的非剥夺策略;中断和陷入的区别;可变分区管理中的硬件机制;数据库系统的优点

本篇博客将讨论军队文职面试(计算机类)的几个关键问题。我们将介绍网络协议的核心要素(帮助你理解网络通信的基本原理)、交互式系统中的非剥夺策略(解释为何这是一个优秀的策略以及其对用户体验、系统稳定性和可预测性的影响)、中断和陷入的区别(从引起原因、处理机制、权限级别和应用等方面进行阐述)、可变分区管理的硬件机制以及数据库系统与文件系统的优点比较。通过阅读本文,您将深入了解这些话题,为面试准备和实际应用提供有价值的知识。

追光者♂:记录、分享、总结、提升,现象级专栏《Python从入门到人工智能》作者,无惧黑暗,坚信曙光 1704

CodeForces - 711B Chris and Magic Square (暴力/模拟)

       题目大意:输入 n ,输入 n*n 的矩阵,有一个占位 0 , 求得将 0 位置换成其他的整数 使得矩阵 行列斜 和全部相等。        题解:一开始居然无脑地想用最暴力的办法,枚举空位可以填的数字,然后每次判断填上这个数字可不可行 这样的话时间复杂度是,接近1e10了,肯定会T 我们可以先统计出空位在的那一行的和_sum,再随便找一行求和sum1,这样sum1-_sum...

hxxjxw的博客 354

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

幻方问题(Magic Squares)

n×n幻方是含有1到n^2的整数数组,排列后是的每一行、每一列、正反两条主对角线上数字的和都是相同的。对于每个n>2都有很多不同的n×n幻方,函数magic(n)只产生一个特定的幻方。rot90将数组旋转 90 度sum(A)sum(A')'sum(1:9)/3rot90(A,k)end。

weixin_52546971的博客 1532

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

Chris and Magic Square

 Chris and Magic Squaretime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputZS the Coder and Chris the Baboon arrived at the entrance of Udayland. Ther...

lee371042的博客 286

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

[USACO3.2]魔板 Magic Squares

在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板。这是一张有8个大小相同的格子的魔板:1 2 3 48 7 6 5。

2301_77920554的博客 316

幻方(Magic Square)

幻方(Magic Square)

cnds123的专栏 1万+

B. Chris and Magic Square

题目链接:点击打开链接 大意:有N*N的矩阵,有一格是空的(标记为0),填一个数,使得每一行,每一列,两条对角线以及要填的数的这一行和这一列的和都相等,否则输出-1; 思路其实很简单,但代码太难写了(找BUG找了两天,气死了); 写个博客,记录一下; 思路: 分两条: 一        0在对角线上:1.在一条对角线上;

nhl19961226的博客 325

CodeForces 711B. Chris and Magic Square(水题)

B. Chris and Magic Square time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output ZS the Coder and Chris the Baboon

想飞的小菜鸡 577

Codeforces Round #369 (Div. 2) 解题报告(A,B,C)

Codeforces Round #369 (Div. 2)然后给出每棵树的初始颜色。 然后给出每棵树染成1~m颜色各自的花费 问最终变成k端花费最少是多少钱。 注意,初始有颜色的树不能染色 题目思路:dp dp[105][105][105]; //前i个树,第i个树染成j这种颜色,构成k段的最小花费 遍历到第i课树,判断一下这棵树是否已经被染色。 * 如果已经被染色,那么花费等于上一棵树的花费

Linoy 1684

水题-Codeforces Round #369 Div.2-B

B. Chris and Magic Square time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output ZS the Coder and Chris the Baboon arrived at the entrance of Uda

懒鬼的私人泳池 753

printf的使用_chris

<span style="font-size:18px;"> int main() { int arr[] = {6,7,8,9,10}; int *ptr = arr; *(ptr++) += 123; printf("%d ,%d/n",*ptr,*(++ptr)); ...

weixin_30657999的博客 131

Codeforces Round #369 (Div. 2) B. Chris and Magic Square【数学,模拟】

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

AC_Dreameng 1065

Codeforces Round #369 (Div. 2)

我冲动的拿起了我的菜刀。。。。。MD 顺秒A加网速给力加B的翻译,以为是好运的开始。。。然后才知道,是厄运的降临。 A. Bus to Udayland time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard o

妍恐龙的博客....^~^眼熟我 568

uva 11520 - Fill the Square(枚举,2级)

Problem A Fill the Square Input:Standard Input Output:Standard Output In this problem, you have to draw a square using uppercase English Alphabets. To be more precise, you will be giv...

weixin_30922589的博客 232
上一篇: hdu5806 NanoApe Loves Sequence Ⅱ 前缀和 + 二分
下一篇: codeforces706B Interesting drink 排序+二分查找
Daemoonn
博客等级 码龄11年 42粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值