codeforces711B Chris and Magic Square 简单枚举

CATIA P3 V5-6R2023在Windows 11 23H2上的稳定安装与系统级配置指南 CATIA是高端制造业核心的三维参数化设计平台,其运行依赖严格的系统底层支撑。理解CATIA的BRep几何内核原理与Windows子系统(如WSL、Hyper-V)的协同机制,是保障建模稳定性与计算精度的技术前提。CATIA Run-Time Library和OpenGL硬件加速能力共同决定了曲面重建、螺纹建模等关键操作的可靠性;而.NET Framework 3.5、VC++ 2022运行库及显卡Studio驱动的精准匹配,则构成工业软件在Win11环境落地的工程基础。本文聚焦于航空、轨交、能源等领域高频 阅读详情

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;
}


【PostgreSQL】PostgreSQL数据库函数比对模板(PG通用) LOOKUP 函数从单行或单列区域 (区域:工作表上的两个或多个单元格。区域中的单元格可以相邻或不相邻。)或数组 (数组:用于建立可生成多个结果或可对在行和列中排列的一组参数进行运算的单个公式。数组区域共用一个公式;数组常量是用作参数的一组常量。LOOKUP 函数具有两种语法形式:向量形式和数组形式。vlookup查找匹配值超过255个字符显示#Value的解决办法。如果值为错误值 #N/A,则返回 TRUE。 阅读详情

相关推荐

RTKLIB中RTK定位原理(一)

1 模型 1.1 非差观测值 非差观测值与RTKLIB中PPP定位原理——观测模型部分中一致。 1.2 单间单差观测值 若两个接收机距离较近,经过站间单差,电离层对流层延迟可消除,卫星s的钟差以及初始相位被消除,中依然包含接收机初始相位。 1.3 站间星间双差观测值 经过站间星间双差,接收机钟差及初始相位被消除。 2 EKF参数估计 2.1 EKF法则 EKF法则与RTKLIB中PPP定位原理——观测模型部分中一致。 2.2 状态变量与时间更新 (1)状态变量 在RTK...

papunzal的博客 3827

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的博客 496

java 对接工行聚富通H5聚合支付接口

java 对接工行聚富通H5聚合支付接口

lgl的博客 882

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的博客 171

基于FMCW技术的毫米波雷达测速原理与应用

调频连续波(FMCW)技术是毫米波雷达中实现高精度测速的核心方法之一。本文详细介绍了FMCW测速的基本原理、信号处理流程以及在实际应用中的关键技术。通过分析FMCW雷达的多普勒效应和频率差测量方法,本文展示了如何利用FMCW技术实现目标速度的高精度检测。此外,本文还探讨了FMCW测速在自动驾驶、智能交通和工业自动化等领域的应用案例,为毫米波雷达开发者提供了理论支持和实践指导。

不谈枯燥公式,只聊雷达趋势。用大白话解读毫米波里的黑科技。 1148

基于python的排课表系统,用python编写排课系统

实现一个具体的课程管理系统。(5) 学生可以查看自己选上的课的成绩,以及各个课程的得分、绩点,并给出其平均绩点,但不允许查看年级其他人的成绩。(2) 学生定制专属课表,可以查看整个学期本人的课程安排,包括课程名称、上课时间、上课地点、任课老师、学分等等。(13) 系主任可以查看整个系的全部学生以及他们的平均绩点,可以对每个班进行排名,或者也可以对整个系进行排名。(15) 系主任可以查看整个学期所开设的所有课程,包括这些课程的全部详细信息。(1) 学生基本信息的查询,学生可以对本人的系统登陆密码进行修改。

yang0728y的博客 2007

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

CocosCreator 面试题(十七)Cocos creator 固定宽度与固定高度的底层原理是什么?Cocos creator是如何做适配的?

通过固定宽度或固定高度的适配方式,可以使游戏在不同屏幕尺寸上保持一定的一致性,确保游戏中的元素的大小和位置相对稳定。具体选择哪种适配方式取决于你的游戏需求和设计考虑。在 Cocos Creator 中,你可以通过设置「设计分辨率」和使用相对坐标和布局方式来实现固定宽度或固定高度的适配。总之,Cocos Creator 提供了多种适配方式和工具,帮助你在不同屏幕尺寸上保持游戏画面的一致性。在 Cocos Creator 中,固定宽度和固定高度是两种常见的适配方式,用于在不同屏幕尺寸上保持游戏画面的一致性。

w风雨无阻w的专栏 1696

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

299

用 ADT 的 RAP Generator 把 RAP 栈一键搭起来:从表到可预览的 OData V4 应用

摘要 SAP RAP Generator 工具能显著提升开发效率,通过一键生成从数据库表到完整 OData UI Service 的全套对象(包括 CDS、Behavior、Service Definition 等)。该工具特别适用于原型验证和标准化应用开发,可节省 60% 以上的基础编码时间。使用时需注意: 表设计需包含关键管理字段(如 last_changed 时间戳) 向导配置决定分层架构和命名规范 生成结果包含可立即预览的 Fiori Elements 界面 后续可专注于业务逻辑增强(校验、权限等)

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1290

2026 蓝牙技术趋势展望:信道探测、Auracast、高吞吐 HID 与 AI 融合引领行业变革

蓝牙技术2026年展望:信道探测、Auracast广播音频和AI融合将引领创新。蓝牙信道探测技术将实现亚米级精确定位,在工业资产追踪和汽车数字钥匙领域大显身手。Auracast广播音频将持续拓展公共场所应用,提升音频包容性体验。高数据吞吐量和超低延迟功能将支持AR/VR等新兴场景。AI与蓝牙的深度融合将推动可穿戴设备智能化发展,实现实时健康监测和个性化音频服务。蓝牙技术凭借其多功能性和创新力,正成为连接万物的关键技术,持续推动行业变革和生活品质提升。

专业开发者的博客,一个从事Android WiFi和蓝牙的工程师 134

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 538

YOLO11改进大全:卷积层、轻量化、注意力机制、损失函数、Backbone、SPPF、Neck、检测头全方位优化汇总

YOLOv11改进大全:卷积层、轻量化、注意力机制、损失函数、Backbone、SPPF、Neck、检测头全方位优化汇总

魔改工程师的博客 4683

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

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

hxxjxw的博客 355

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

幻方问题(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的博客 1539

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

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

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

[USACO3.2]魔板 Magic Squares

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

2301_77920554的博客 318

幻方(Magic Square)

幻方(Magic Square)

cnds123的专栏 1万+

B. Chris and Magic Square

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

nhl19961226的博客 327

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

想飞的小菜鸡 579

水题-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

懒鬼的私人泳池 754

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的博客 132

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

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

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 1688

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

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

AC_Dreameng 1066

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、付费专栏及课程。

余额充值
⌂ 换网址