Codeforces 711B 【模拟】
比赛的时候绝壁打麻烦了。。。
考虑的好麻烦。。。wa7。。。还要判断出来的是不是positive的。。。
好吧。。认了。。
#include<cstdio>
#include <map>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
typedef __int64 LL;
const int N=5e2+10;
LL ma[N][N];
LL row[N];
LL col[N];
LL s1,s2;
int n;
int main()
{
memset(row,0,sizeof(row));
memset(col,0,sizeof(col));
s1=s2=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%I64d",&ma[i][j]);
//1的话随便
if(n==1)
{
if(!ma[1][1])
puts("1");
else
printf("%I64d\n",ma[1][1]);
return 0;
}
//找到0位置
int x=-1,y=-1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
if(!ma[i][j])
{
x=i;
y=j;
break;
}
if(x!=-1)
break;
}
//printf("%d %d\n",x,y);
for(int i=1;i<=n;i++)
{
if(i!=x)
{
for(int j=1;j<=n;j++)
row[i]+=ma[i][j];
}
}
for(int j=1;j<=n;j++)
{
if(j!=y)
{
for(int i=1;i<=n;i++)
col[j]+=ma[i][j];
}
}
//斜的
//顺的
if(x!=y)
{
for(int i=1;i<=n;i++)
{
s1+=ma[i][i];
}
}
//逆的
if(x+y!=n+1)
for(int i=1;i<=n;i++)
{
s2+=ma[i][n-i+1];
}
if(x!=y&&x+y!=n+1)
if(s1!=s2)
{
puts("-1");
return 0;
}
//横的
LL temp1,temp2;
for(int i=1;i<=n;i++)
{
if(i!=x)
{
temp1=row[i];
break;
}
}
for(int i=1;i<=n;i++)
{
if(i!=x)
{
if(temp1!=row[i]||(s1!=row[i]&&x!=y)||(s2!=row[i]&&x+y!=n+1))
{
//printf("here?\n");
puts("-1");
return 0;
}
}
}
//竖的
for(int i=1;i<=n;i++)
{
if(i!=y)
{
temp1=col[i];
break;
}
}
for(int i=1;i<=n;i++)
{
if(i!=y)
{
if(temp1!=col[i]||(x!=y&&s1!=col[i])||(s2!=col[i]&&x+y!=n+1))
{
puts("-1");
return 0;
}
}
}
temp2=temp1;
//求一个
//printf("temp1=%I64d\n",temp1);
for(int i=1;i<=n;i++)
{
if(i==x)
{
for(int j=1;j<=n;j++)
{
if(ma[i][j])
temp1-=ma[i][j];
}
ma[x][y]=temp1;
break;
}
}
LL k1,k2,k3,k4;
temp1=temp2;
k1=0;
k2=0;
k3=0;
k4=0;
for(int i=1;i<=n;i++)
k1+=ma[x][i];
for(int i=1;i<=n;i++)
k2+=ma[i][y];
if(k1!=temp1||k2!=temp2||k1!=k2)
{
//printf("here?\n");
puts("-1");
return 0;
}
if(x==y)
{
for(int i=1;i<=n;i++)
k3+=ma[i][i];
if(k3!=k2)
{
puts("-1");
return 0;
}
}
if(x+y==n+1)
{
for(int i=1;i<=n;i++)
k4+=ma[i][n+1-i];
if(k4!=k2)
{
puts("-1");
return 0;
}
}
if(ma[x][y]>0)
printf("%I64d\n",ma[x][y]);
else
puts("-1");
return 0;
}
/*
4
1 1 1 1
1 1 0 1
1 1 1 1
1 1 1 1
*/
Codeforces 711B 【模拟】的更多相关文章
- 【模拟】Codeforces 711B Chris and Magic Square
题目链接: http://codeforces.com/problemset/problem/711/B 题目大意: N*N的矩阵,有且只有一个0,求要把这个矩阵变成幻方要填什么正数.无解输出-1.幻 ...
- CodeForces - 427B (模拟题)
Prison Transfer Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Sub ...
- CodeForces - 404B(模拟题)
Marathon Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Sta ...
- Codeforces 709B 模拟
B. Checkpoints time limit per test:1 second memory limit per test:256 megabytes input:standard input ...
- codeforces 711B - Chris and Magic Square(矩阵0位置填数)
题目链接:http://codeforces.com/problemset/problem/711/B 题目大意: 输入 n ,输入 n*n 的矩阵,有一个占位 0 , 求得将 0 位置换成其他的整数 ...
- CodeForces - 404A(模拟题)
Valera and X Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit ...
- Codeforces 390A( 模拟题)
Inna and Alarm Clock Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64 ...
- Codeforces 452D [模拟][贪心]
题意: 给你k件衣服处理,告诉你洗衣机烘干机折叠机的数量,和它们处理一件衣服的时间,要求一件衣服在洗完之后必须立刻烘干,烘干之后必须立刻折叠,问所需的最小时间. 思路: 1.按照时间模拟 2.若洗完的 ...
- CodeForces - 796B 模拟
思路:模拟移动即可,如果球落入洞中停止移动.注意:有可能第一个位置就是洞!! AC代码 #include <cstdio> #include <cmath> #include ...
随机推荐
- HDU 4857 topological_sort
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- mysql 安装与启动
1.下载mysql installer 2.安装 一直点next,直到finish. 3.安装时的配置 安装完后,选择立即开始配置. 选择standard configuration 勾选安装mysq ...
- 通过Java反射做实体查询
我们在使用hibernate的时候,查询的时候都会和实体中的一些字段相结合去查询,当然字段少了,还算是比较简单,当字段多了,就不那么容易了,所以就自己写了个方法,根据实体中的字段信息去查询,废话不多说 ...
- html中跳转方法(含设定时间)
脚本方式 如: <script language="JavaScript" type="text/JavaScript"> <!-- wind ...
- VC编码规范(转)
1 项目风格 1.1 项目取名 在VC之中,项目名为最后可执行文件名,所以项目名最好以最终的可执行文件名一致. 1.2 项目目录设置 为保证VC项目的备份方便.快 ...
- BZOJ 3992: [SDOI2015]序列统计 快速幂+NTT(离散对数下)
3992: [SDOI2015]序列统计 Description 小C有一个集合S,里面的元素都是小于M的非负整数.他用程序编写了一个数列生成器,可以生成一个长度为N的数列,数列中的每个数都属于集合S ...
- Marking as slave lost.
Spark on Yarn提交任务时报ClosedChannelException解决方案_服务器应用_Linux公社-Linux系统门户网站 http://www.linuxidc.com/Linu ...
- HDU1693 Eat the Trees —— 插头DP
题目链接:https://vjudge.net/problem/HDU-1693 Eat the Trees Time Limit: 4000/2000 MS (Java/Others) Mem ...
- Linux:外网域名防火墙设置导致下载失败
问题现象: 通过IE从服务器下载文件时,提示Can't read from connection: Connection reset by peer. 别的现场都是好的,只有该现场有这个问题.所以,一 ...
- 用/proc/stat计算cpu的占用率
转自:http://blog.csdn.net/pppjob/article/details/4060336 在Linux下,CPU利用率分为用户态,系统态和空闲态,分别表示CPU处于用户态执行的时间 ...