Gym - 100735E Restore

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[105][105];
ll h[105];
void solve() {
    int n;  scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            scanf("%lld", &a[i][j]);
            h[i] += a[i][j];
        }
    }
    ll summ=0;
    for (int i = 0; i < n; i++) {
        summ+=h[i];
    }
    ll sum = summ/(n-1);
    for (int i = 0; i < n; i++) {
        a[i][i] = sum-h[i];
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (j)  printf(" ");
            printf("%lld", a[i][j]);
        }
        printf("\n");
    }
}
int main() {
    int t = 1;
    //scanf("%d", &t);
    while(t--)
        solve();
    return 0;
}
时间: 2024-10-29 19:06:53

Gym - 100735E Restore的相关文章

KTU Programming Camp (Winter Training Day 1)

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

CodeForces Gym 100935D Enormous Carpet 快速幂取模

Enormous Carpet Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935D Description standard input/outputStatements Ameer is an upcoming and pretty talented problem solver who loves to solve problems using computers.

Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 解题思路: 注意到ip地址分成

使用DBMS_STATS.restore 恢复统计信息

我们可以恢复之前的统计信息,生成原本的执行计划 SQL> BEGIN   2  DBMS_STATS.restore_table_stats(ownname => 'DAO',tabname => 'TEST_STATS',as_of_timestamp => '27-JUN-14 05.40.33.314757 PM +08:00');   3  END ;   4  / PL/SQL procedure successfully completed. SQL> selec

leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 分析:求二叉树的中序

B - Average Gym - 101161B 组合数学

http://codeforces.com/gym/101161/attachments 今天被卡常了,其实是自己对组合数技巧研究的不够. 如果是n, m <= 1e5的,然后取模是质数,那么可以用费马小定理. 如果n, m都比较小,那么其实是直接杨辉三角.不用逆元那些. 这题的思路是,枚举每一一个ave,然后总和就是n * ave 相当于方程  x1 + x2 + .... + xn = n * ave中,在0 <= x[i] <= full的情况下,不同解的个数中,使得x[i] ==

Codeforces Gym 100269 Dwarf Tower (最短路)

题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game named "Dwarf Tower". In this game there are n different items,which you can put on your dwarf character. Items are numbered from 1 to n. Vasya want

CodeForces Gym 101063C 二进制压缩

http://codeforces.com/gym/101063/problem/C 给n个人,m样物品,每个人可以从物品中选择几样.两人选择物品的交集元素个数比上并集元素个数如果大于某个比例即可将两人配对.求配对数. n的范围是1e5,直接比较所有人的选择会TLE,应该将所有选择物品的情况用二进制压缩,m最大是10,情况数目小于2048,可以接受.注意配对总数范围应为long long. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> i

Gym 101246H ``North-East&#39;&#39;(LIS)

http://codeforces.com/gym/101246/problem/H 题意: 给出n个点的坐标,现在有一个乐队,他可以从任一点出发,但是只能往右上方走(包括右方和上方),要经过尽量多的点.输出它可能经过的点和一定会经过的点. 思路: 分析一下第一个案例,在坐标图上画出来,可以发现,他最多可以经过4个点,有两种方法可以走. 观察一下,就可以发现这道题目就是要我们求一个LIS. 首先,对输入数据排一下顺序,x小的排前,相等时则将y大的优先排前面. 用二分法求LIS,这样在d数组中就可