Codeforces Gym100735 E.Restore (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)
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
3
0 9 4
7 0 3
6 1 0
2 9 4
7 5 3
6 1 8
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)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 【KTU Programming Camp (Day 3)】Queries
http://codeforces.com/gym/100739/problem/A 按位考虑,每一位建一个线段树. 求出前缀xor和,对前缀xor和建线段树. 线段树上维护区间内的0的个数和1的个数 ...
- KTU Programming Camp (Winter Training Day 1)
A.B.C(By musashiheart) 0216个人赛前三道题解 E(By ggg) Gym - 100735E Restore H(by pipixia) Gym - 100735H
- Codeforces Gym101572 G.Galactic Collegiate Programming Contest (2017-2018 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2017))
Problem G Galactic Collegiate Programming Contest 这个题题意读了一会,就是几个队参加比赛,根据实时的信息,问你1号队的实时排名(题数和罚时相同的时候并 ...
- 【codeforces 239B】Easy Tape Programming
[题目链接]:http://codeforces.com/contest/239/problem/B [题意] 给你一个长度为n的字符串,只包括'<">'以及数字0到9; 给你q ...
- Codeforces 730I:Olympiad in Programming and Sports(最小费用流)
http://codeforces.com/problemset/problem/730/I 题意:有n个人参加两种比赛,其中每个人有两个参加比赛的属性,如果参加了其中的一个比赛,那么不能参加另一个比 ...
- CodeForces 464 B Restore Cube
Restore Cube 题解: x->yyy 其实就是把x代替成yyy这个值. 如果不好理解的话, 可以试想一下, 刚开始的话 0->0, 1->1, 2->2,...,9- ...
随机推荐
- Oracle redo与undo 第二弹
首先看一下undo与redo的字面意思: undo:撤销,也就是取消之前的操作. redo:重做,重新执行一遍之前的操作. 什么是REDO REDO记录transaction logs,分为o ...
- HOJ_14001 Just Terraffic!
题意相对来说比较扭曲..所以来说下模型,具体做法有兴趣的孩纸去问度娘或者波塞冬吧~~ 给出一个序列长度,并且输入该序列,该序列的含义是横坐标: 任何两个相邻坐标绝对值小于等于1000的必然为一个整体, ...
- Redis数据结构以及Strings型操作
Redis数据结构图: Strings型 <String key,String value>: keys * 查看所有key get 获取key的value值 append 向key对 ...
- 【Add Two Numbers】
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- Python+Selenium基础篇之2-打开和关闭火狐浏览器
本节介绍如何初始化一个webdriver实例对象driver,然后打开和关闭firefox浏览器.要用selenium打开fiefox浏览器.首先需要去下载一个driver插件geckodriver. ...
- caffe的python接口提取resnet101某层特征
论文的caffemodel转化为tensorflow模型过程中越坑无数,最后索性直接用caffe提特征. caffe提取倒数第二层,pool5的输出,fc1000层的输入,2048维的特征 #codi ...
- Win10开启IIS
1.win+r control 2.程序
- vim configure
vim configure .vimrc " An example for a vimrc file. " " Maintainer: Bram Moolenaar &l ...
- POJ 1753 Flip Game(高斯消元+状压枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45691 Accepted: 19590 Descr ...
- docker 集群 flannel网络构建
先保证集群状态是正常的 集群管理 kubelet 在创建pod 时会先下载一个pause 镜像,这个镜像用于容器基础网络管理非常重要: 每个node 节点都要执行该操作: iptables -P FO ...