CodeForces-259B]Little Elephant and Magic Square

Little Elephant loves magic squares very much.

A magic square is a 3 × 3 table, each cell contains some positive integer. At that the sums of integers in all rows, columns and diagonals of the table are equal. The figure below shows the magic square, the sum of integers in all its rows, columns and diagonals equals 15.

The Little Elephant remembered one magic square. He started writing this square on a piece of paper, but as he wrote, he forgot all three elements of the main diagonal of the magic square. Fortunately, the Little Elephant clearly remembered that all elements of the magic square did not exceed 105.

Help the Little Elephant, restore the original magic square, given the Elephant‘s notes.

Input

The first three lines of the input contain the Little Elephant‘s notes. The first line contains elements of the first row of the magic square. The second line contains the elements of the second row, the third line is for the third row. The main diagonal elements that have been forgotten by the Elephant are represented by zeroes.

It is guaranteed that the notes contain exactly three zeroes and they are all located on the main diagonal. It is guaranteed that all positive numbers in the table do not exceed 105.

Output

Print three lines, in each line print three integers — the Little Elephant‘s magic square. If there are multiple magic squares, you are allowed to print any of them. Note that all numbers you print must be positive and not exceed 105.

It is guaranteed that there exists at least one magic square that meets the conditions.

Examples

input

Copy

0 1 11 0 11 1 0

output

Copy

1 1 11 1 11 1 1

input

Copy

0 3 65 0 54 7 0

output

Copy

6 3 65 5 54 7 4
#include<bits/stdc++.h>
using namespace std;
#define maxn 300010
#define LL long long
int a[5][5];
int main()
{
    int i,j;
    for(i=1; i<=3; i++)
        for(j=1; j<=3; j++)
            cin>>a[i][j];
    a[3][3]=(a[1][2]+a[1][3]+a[2][1]+a[2][3]-a[3][1]-a[3][2])/2;
    a[2][2]=(a[1][2]+a[1][3]-a[2][1]-a[2][3]+a[3][1]+a[3][2])/2;
    a[1][1]=(-a[1][2]-a[1][3]+a[2][1]+a[2][3]+a[3][1]+a[3][2])/2;
    for(i=1; i<=3; i++)
    {
        for(j=1; j<=3; j++)
            cout<<a[i][j]<<‘ ‘;
        cout<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/lunatic-talent/p/11386782.html

时间: 2024-11-03 14:42:06

CodeForces-259B]Little Elephant and Magic Square的相关文章

Little Elephant and Magic Square

Little Elephant loves magic squares very much. A magic square is a 3?×?3 table, each cell contains some positive integer. At that the sums of integers in all rows, columns and diagonals of the table are equal. The figure below shows the magic square,

codeforces #369div2 B. Chris and Magic Square

题目:在网格某一处填入一个正整数,使得网格每行,每列以及两条主对角线的和都相等 分析:题目不难,找到要填的那个数填进去,然后循环比较每行每列以及对角线的和是否相等,题目提交上去卡了几次要注意几点 注意:1.答案数据范围1<=x<=1e18,要用 long long 2.特殊情况,n==1时,由于一定有要填的数,所以一定有解 3.反正就是要注意看清楚题目和数据边界情况处理啦 1 #include<iostream> 2 #include<cstdio> 3 #includ

CodeForces - 204C Little Elephant and Furik and Rubik

CodeForces - 204C Little Elephant and Furik and Rubik 个人感觉是很好的一道题 这道题乍一看我们无从下手,那我们就先想想怎么打暴力 暴力还不简单?枚举所有字串,再枚举所有位置,算出所有答案不就行了 我们自然不能无脑暴力,但是暴力可以给我们启发 我们知道所有对答案做出贡献的字符一定是相同的(废话) 所以我们可以O(n^2)首先枚举两个字符串中相同的字符然后再考虑如何贡献 然后计算出所有的方案下的值,再除以n*(n+1)*(2*n+1)/6 [不知

IOI&#39;96 Magic Square解题报告

IOI‘96 Magic Square(魔版) 题目由JerryXie翻译. Magic Square(msqaure.cpp/c/pas) [问题描述] 在魔方的成功之后,卢比克先生发明了它的二维版本,叫做魔版.它是一个由八个方块组成的表格. 在本题中我们认为每一个方块都有不同的颜色,这些颜色将会用1~8共8个整数表示.一个表格的状态将会以一个由颜色组成的序列表示,顺序是从表格左上角沿顺时针方向给出.比如上面这个表格,序列为(1,2,3,4,5,6,7,8),这被称作初始状态. 魔版有三种基本

Codeforces 220B - Little Elephant and Array 离线树状数组

This problem can be solve in simpler O(NsqrtN) solution, but I will describe O(NlogN) one. We will solve this problem in offline. For each x (0?≤?x?<?n) we should keep all the queries that end in x. Iterate that x from 0 to n?-?1. Also we need to kee

CodeForces 258B Little Elephant and Elections 数位DP

前面先用数位DP预处理,然后暴力计算组合方式即可. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include

Sicily 1302 Magic Square (数论)

链接:http://soj.me/1302 题目中有图和表格,所以就不把题目信息列出来了,打开链接直接看就行了... 分析: 从填幻方的过程,可以判断一下几点: 1>.幻方的每一列都是向下发展的: 2>.填入的前n个数都刚好是每一列第一个填入的数: 从这两点可以推出下面几点: (1). 每一列的发展是平面的(即每一列的个数是一样的,特别的,只有一列少一): (2). 右下角那个数肯定是填入的最大的: (3). 当我们填入到右下角的时候,每一列的个数都刚好是n/2(注意n是奇数,这里的除是整除)

Magic Square

这道题是比较简单的,三个字--找规律.思路也应该是比较清晰的,下面,我们先看一下题目吧. Description In recreational mathematics, a magic square of n-degree is an arrangement of n2 numbers, distinct integers, in a square, such that the n numbers in all rows, all columns, and both diagonals sum

codeforces 711B - Chris and Magic Square

题目链接:http://codeforces.com/problemset/problem/711/B 题目大意: 输入 n ,输入 n*n 的矩阵,有一个占位 0 , 求得将 0 位置换成其他的整数 使得矩阵 行列斜 和全部相等. 代码状态: 一把辛酸泪