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- ...
随机推荐
- HDU4616 树形DP+三次深搜
这题和之前那个HDU2616有着奇妙的异曲同工之处..都是要求某个点能够到达的最大权重的地方... 但是,这题加了个限制,要求最多只能够踩到C个陷阱,一单无路可走或者命用光了,就地开始清算总共得分之和 ...
- Spring---浅谈IOC
概念 IOC(Inversion of Control 控制反转)是spring的核心,贯穿始终.所谓IOC,对于spring框架来说,就是由spring来负责控制对象的生命周期和对象间的关系. 传统 ...
- Django之session验证的三种姿势
一.什么是session session是保存在服务端的键值对,Django默认支持Session,并且默认是将Session数据存储在数据库中,即:django_session 表中. 二.FVB中 ...
- EXCEL合并单元格快捷键暨WORD+EXCEL自定义快捷键
最近在写测试用例时,用到合并单元格,只能点,没有快捷键,觉得很蛋疼,上网找了一下,没有直接设置其对应快捷键的方法,但有种曲线救国的方法: 一.右击功能区,选择‘自定义快速访问工具栏’ 二.可以在这 ...
- APM简介
1.什么是APM APM (应用性能管理) - Application Performance Management & Monitoring在信息科学和系统控制领域,APM致力于监控和管理应 ...
- Leetcode 500.键盘行
键盘行 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例: 输入: ["Hello", "Alaska", " ...
- [已解决]Argument list too long如何处理?
Argument list too long 本质是需要处理的长度超过系统的长度,因此无法执行相关命令. 经过搜索发现了两种方法,思想都是将参数切分成小的段落进行执行. 法一:通过xargs传递参数 ...
- 求解Catalan数,(大数相乘,大数相除,大数相加)
Catalan数 卡塔兰数是组合数学中一个常在各种计数问题中出现的数列.以比利时的数学家欧仁·查理·卡塔兰(1814–1894)命名.历史上,清代数学家明安图(1692年-1763年)在其<割圜 ...
- BZOJ 3625 [Codeforces Round #250]小朋友和二叉树 ——NTT 多项式求逆 多项式开根
生成函数又有奇妙的性质. $F(x)=C(x)*F(x)*F(x)+1$ 然后大力解方程,得到一个带根号的式子. 多项式开根有解只与常数项有关. 发现两个解只有一个是成立的. 然后多项式开根.求逆. ...
- crontab中执行java程序的脚本
测试场景说明(操作系统:centos7): 有一个bash脚本,脚本内容是执行某个java程序,该脚本为 /data/project1/start.sh crontab -e,添加了以下任务: * * ...