题意:给定n*n个矩阵,其中只有一个格子是0,让你填上一个数,使得所有的行列的对角线的和都相等。

析:首先n为1,就随便填,然后就是除了0这一行或者这一列,那么一定有其他的行列是完整的,所以,先把其他的算出来,然后再作差就算这个数了,

然后再去验证其他的对不对就好了。除了n为1,其他的都是唯一解应该。或者没有。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <list>
#include <sstream>
#define frer freopen("in.txt", "r", stdin)
#define frew freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e2 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
LL a[maxn][maxn]; int main(){
while(cin >> n){
memset(a, 0, sizeof a);
LL sum1 = 0, sum2 = 0;
int x, y;
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
scanf("%I64d", &a[i][j]);
a[i][n] += a[i][j];
a[n][j] += a[i][j];
if(a[i][j] == 0) x = i, y = j;
if(i == j) sum1 += a[i][j];
if(i + j == n - 1) sum2 += a[i][j];
}
}
if(1 == n) printf("1\n");
else{
LL ans = 0;
for(int i = 0; i < n; ++i){
if(i != x){
ans = a[i][n] - a[x][n];
a[x][n] += ans;
a[n][y] += ans;
if(x == y) sum1 += ans;
if(x + y == n - 1) sum2 += ans;
break;
}
}
bool ok = true;
if(sum1 != sum2 || ans < 1 || ans > 1e18) ok = false;
if(!ok){ printf("-1\n"); continue; }
for(int i = 0; i < n; ++i){
if(a[i][n] != sum1 || a[n][i] != sum1){ ok = false; break; }
}
if(!ok){ printf("-1\n"); continue; }
else cout << ans << endl;
}
}
return 0;
}

最新文章

  1. [Prodinner项目]学习分享_第二部分_Entity到DB表的映射
  2. STL的string和wstring
  3. javascript优化--10模式(设计模式)01
  4. WPF:ListView数据绑定及Style
  5. 多个非同源的shared_ptr管理对象引起double free
  6. iOS开发——数据持久化Swift篇&amp;文件目录路径获取(Home目录,文档目录,缓存目录等)
  7. java-mina(nio 框架)
  8. api接口
  9. 基类方法的反隐藏 反private 秘籍
  10. 【Delphi内联汇编学习1】Delphi与汇编
  11. ios7毛玻璃效果实现
  12. 引导加载程序:GRUB
  13. UVA 12538 Version Controlled IDE 解题报告
  14. jdk配置java_home的两种方式
  15. 关于PHP创建接口及调用接口的简短例子(本地)
  16. Dockerfile的alpine时区设置
  17. 剑指Offer 29. 最小的K个数 (其他)
  18. 3728 联合权值[NOIP 2014 Day1 T2]
  19. yield return:使用.NET的状态机生成器
  20. Maven 添加jar包到本地仓库

热门文章

  1. ubuntu 12.04上安装OpenERP 7的一次记录
  2. UIDevice通知
  3. JS改变input的value值不触发onchange事件解决方案 (转)
  4. phpstorm10.0.1和webstorm11注册
  5. HDU 5264 pog loves szh I (字符串,水)
  6. Linux下Web服务器环境搭建LNMP一键安装包[20130911更新]
  7. session_start保存的客户端cookie的值什么时候改变
  8. 五个JS经典面试题
  9. linux 下按在sqllite
  10. std::vector数据复制