E - Restore

Given a matrix A of size N * N. The rows are numbered from 0 to N-1, the columns are numbered from 0 to N-1. In this matrix, the sums of each row, the sums of each column, and the sum of the two diagonals are equal.

For example, a matrix with N = 3:

The sums of each row:

2 + 9 + 4 = 15

7 + 5 + 3 = 15

6 + 1 + 8 = 15

The sums of each column:

2 + 7 + 6 = 15

9 + 5 + 1 = 15

4 + 3 + 8 = 15

The sums of each diagonal:

2 + 5 + 8 = 15

4 + 5 + 6 = 15

As you can notice, all sums are equal to 15.

However, all the numbers in the main diagonal (the main diagonal consists of cells (i, i)) have been removed. Your task is to recover these cells.

Input

The first line contains the dimension of the matrix, n (1 ≤ n ≤ 100).

The following n lines contain n integers each, with removed numbers denoted by 0, and all other numbers  - 1012 ≤ Aij ≤ 1012.

Output

The restored matrix should be outputted in a similar format, with n lines consisting of n integers each.

Example

Input
3
0 9 4
7 0 3
6 1 0
Output
2 9 4 
7 5 3
6 1 8
 
这个题有一个地方有点坑,就是如果一行里面有两个0,因为只有对角线的挖掉了,所以直接竖着算这一排的就可以。其他的没了。
 
代码:
 1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<algorithm>
5 using namespace std;
6 typedef long long ll;
7 ll a[105][105];
8 int main(){
9 int n;
10 while(~scanf("%d",&n)){
11 ll cnt=0;
12 for(int i=0;i<n;i++){
13 for(int j=0;j<n;j++){
14 scanf("%lld",&a[i][j]);
15 cnt+=a[i][j];
16 }
17 }
18 cnt/=n-1;int i,j; //算那个相等的值
19 for(i=0;i<n;i++){
20 ll num=0;int flag,ret=0;
21 for(j=0;j<n;j++){
22 num+=a[i][j];
23 if(a[i][j]==0){flag=j;ret++;}
24 }
25 if(ret>1){ //如果这一排有多于1个0
26 ll qwe=0;
27 for(int k=0;k<n;k++)qwe+=a[k][i];
28 a[i][i]=cnt-qwe;
29 }
30 else if(num!=cnt)a[i][flag]=cnt-num;
31 }
32 for(int i=0;i<n;i++){
33 for(int j=0;j<n;j++){
34 printf("%lld",a[i][j]);
35 if(j!=n-1)printf(" ");
36 else printf("\n");
37 }
38 }
39 }
40 return 0;
41 }

Codeforces Gym100735 E.Restore (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)的更多相关文章

  1. Codeforces Gym100735 I.Yet another A + B-Java大数 (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)

    I.Yet another A + B You are given three numbers. Is there a way to replace variables A, B and C with ...

  2. Codeforces Gym100735 G.LCS Revised (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)

    G.LCS Revised   The longest common subsequence is a well known DP problem: given two strings A and B ...

  3. Codeforces Gym100735 D.Triangle Formation (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)

    日常训练题解 D.Triangle Formation You are given N wooden sticks. Your task is to determine how many triang ...

  4. 【KTU Programming Camp (Day 3)】Queries

    http://codeforces.com/gym/100739/problem/A 按位考虑,每一位建一个线段树. 求出前缀xor和,对前缀xor和建线段树. 线段树上维护区间内的0的个数和1的个数 ...

  5. KTU Programming Camp (Winter Training Day 1)

    A.B.C(By musashiheart) 0216个人赛前三道题解 E(By ggg) Gym - 100735E Restore H(by pipixia) Gym - 100735H

  6. Codeforces Gym101572 G.Galactic Collegiate Programming Contest (2017-2018 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2017))

    Problem G Galactic Collegiate Programming Contest 这个题题意读了一会,就是几个队参加比赛,根据实时的信息,问你1号队的实时排名(题数和罚时相同的时候并 ...

  7. 【codeforces 239B】Easy Tape Programming

    [题目链接]:http://codeforces.com/contest/239/problem/B [题意] 给你一个长度为n的字符串,只包括'<">'以及数字0到9; 给你q ...

  8. Codeforces 730I:Olympiad in Programming and Sports(最小费用流)

    http://codeforces.com/problemset/problem/730/I 题意:有n个人参加两种比赛,其中每个人有两个参加比赛的属性,如果参加了其中的一个比赛,那么不能参加另一个比 ...

  9. CodeForces 464 B Restore Cube

    Restore Cube 题解: x->yyy 其实就是把x代替成yyy这个值. 如果不好理解的话, 可以试想一下, 刚开始的话 0->0, 1->1, 2->2,...,9- ...

随机推荐

  1. Paper Folding UVA - 177 模拟+思路+找规律

    题目:题目链接 思路:1到4是很容易写出来的,我们先考虑这四种情况的绘制顺序 1:ru 2:rulu 3:rululdlu 4:rululdluldrdldlu 不难发现,相较于前一行,每一次增加一倍 ...

  2. Java消息中间件--初级篇

    一. 为什么使用消息中间件? 假设用户登录系统   传统方式 用户登录  调用短息服务   积分服务  日志服务等各种服务  如果短息服务出现问题就无法发送短信而且用户登录成功必须所有调用全部完成返回 ...

  3. 设计模式之第13章-职责链模式(Java实现)

    设计模式之第13章-职责链模式(Java实现) “请假都那么麻烦,至于么.”“咋的了?”“这不快过年了么,所以我想早两天回去,准备一下,买买东西什么的,然后去给项目经理请假,但是他说快过年了,所以这个 ...

  4. leetcode 【 Remove Element 】python 实现

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...

  5. 牛客网python试题-错误整理-20180711

    ######## >>>[3] in [1,2,3,4] False >>>3 in [1,2,3,4] True ######## x = (y = z + 1) ...

  6. 软工实践 - 第九次作业 Alpha 冲刺 (1 / 10)

    队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/9949350.html 作业博客:(班级博客本次作业的链接) 组员情况 组员1(队长):白晨曦 ...

  7. Redhat/CentOS安装vsftp软件

    1.更新yum源 首先需要更新系统的yum源,便捷工具下载地址:http://help.aliyun.com/manual?spm=0.0.0.0.zJ3dBU&helpId=1692 2.安 ...

  8. 【bzoj2242】[SDOI2011]计算器 EXgcd+BSGS

    题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给定y,z,p, ...

  9. cf 843 B Interactive LowerBound [随机化]

    题面: 传送门 思路: 这是一道交互题 比赛的时候我看到了直接跳过了...... 后来后面的题目卡住了就回来看这道题,发现其实比较水 实际上,从整个序列里面随机选1000个数出来询问,然后从里面找出比 ...

  10. bzoj 2387: [Ceoi2011]Traffic

    bzoj 2387: [Ceoi2011]Traffic 题目描述 The center of Gdynia is located on an island in the middle of the ...