E - Restore

题意:输入一个n,输入一个对角线空缺(为0)的n*n的矩阵,要求每一行每一列和对角线的和相同,输出完整的矩阵。

解法:设每一行的和都是sum,用一个h[]数组存每一行的和。则可得a[0][0] = sum-h[0], a[1][1] = sum-h[1], a[2][2] = sum-h[2]......同时所有对角线的和也为sum,则可得公式 sum-h[0]+sum-h[1]+...+sum-h[n-1] = sum, 设所有h[x]的和为summ, 即n*sum-summ=sum, 即可得sum = summ/(n-1),后面由式子a[i][i] = sum-h[i]便可求得。

注意:a[i][j]的取值范围! - 1012 ≤ Aij ≤ 1012, 要用long long去存。

typedef long long ll;
ll a[][];
ll h[];
void solve() {
int n; scanf("%d", &n);
for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
scanf("%lld", &a[i][j]);
h[i] += a[i][j];
}
}
ll summ=;
for (int i = ; i < n; i++) {
summ+=h[i];
}
ll sum = summ/(n-);
for (int i = ; i < n; i++) {
a[i][i] = sum-h[i];
}
for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
if (j) printf(" ");
printf("%lld", a[i][j]);
}
printf("\n");
}
}
int main() {
int t = ;
//scanf("%d", &t);
while(t--)
solve();
return ;
}

Gym - 100735E Restore的更多相关文章

  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

  2. [LeetCode] Restore IP Addresses 复原IP地址

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  3. canvas的save与restore方法的作用

    网上搜罗了一堆资料,最后总结一下. save:用来保存Canvas的状态.save之后,可以调用Canvas的平移.放缩.旋转.错切.裁剪等操作. restore:用来恢复Canvas之前保存的状态. ...

  4. TFS Express backup and restore

    When we setup source control server, we should always make a backup and restore plan for it. This ar ...

  5. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

  6. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  7. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  8. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

  9. Restore Volume 操作 - 每天5分钟玩转 OpenStack(60)

    前面我们 backup 了 voluem,今天我们将讨论如何 restore volume. restore 的过程其实很简单,两步走: 在存储节点上创建一个空白 volume. 将 backup 的 ...

随机推荐

  1. js dongtai xianshi textarea zishu

    <form name="FORM" id="FORM" method="post" action="?action=$atc ...

  2. centos6与centos7区别

    CentOS 6 vs CentOS 7的不同   (1)桌面系统[CentOS6] GNOME 2.x[CentOS7] GNOME 3.x(GNOME Shell) (2)文件系统[CentOS6 ...

  3. 游戏引擎架构,3d游戏引擎设计、Unreal引擎技术等五本PDF推荐

    扫码时备注或说明中留下邮箱 付款后如未回复请至https://shop135452397.taobao.com/ 联系店主

  4. nth-child & nth-of-type区别

    Do l have to 非做不可吗? He is my age. 他和我同岁. Here you are. 给你. No one knows . 没有人知道. 关于nth-child &&a ...

  5. 【zookeeper】使用场景

    以下场景是我认为的zookeeper可能会大显身手的场景. 场景1:配置新增和更新 我们可以将zookeeper部署成一个配置服务,实现配置的存储和发布等功能. 具体的原理是:zookeeper可以按 ...

  6. UVa 1600 Patrol Robot(三维广搜)

    A robot has to patrol around a rectangular area which is in a form of m x n grid (m rows and ncolumn ...

  7. [Java学习]面向对象-类的继承;方法覆盖

    一.类的继承 实现方法: public Class SubClass extends SuperClass{ } 继承最基本作用: 代码重用. 继承最重要的作用: 方法可以重写. 关于类的继承: 子类 ...

  8. java链接JDBC中的?问题

    String sql = "select * from student where name= ?"; PreparedStatement pst = conn.prepareSt ...

  9. java 图片的自定义大小

    java 小功能,进行图片的自定义大小 package com.project.video.controller; import java.awt.Color; import java.awt.Gra ...

  10. android和js互相调用

    import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.o ...