█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 1087

Problema 0xe1 - Cuadrado mágico

  • Autor: svanegas
  • Fecha: 2020-10-09 23:27:25 UTC (Hace más de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.006 s 1 KBi
#2
Correcto
0.004 s 1 KBi
#3
Correcto
0.006 s 45 KBi
#4
Correcto
0.006 s 2 KBi
#5
Correcto
0.005 s 1 KBi
#6
Correcto
0.004 s 1 KBi
#7
Correcto
0.005 s 1 KBi
#8
Correcto
0.005 s 1 KBi
#9
Correcto
0.005 s 1 KBi
#10
Correcto
0.005 s 1 KBi
#11
Correcto
0.005 s 1 KBi
#12
Correcto
0.005 s 1 KBi
#13
Correcto
0.005 s 1 KBi
#14
Correcto
0.005 s 1 KBi
#15
Correcto
0.005 s 1 KBi
#16
Correcto
0.007 s 1 KBi
#17
Correcto
0.006 s 1 KBi
#18
Correcto
0.007 s 1 KBi
#19
Correcto
0.007 s 1 KBi
#20
Correcto
0.007 s 1 KBi
Puntos totales: 100 / 100

Código

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

const int MAXN = 105;
int n;
ll m[MAXN][MAXN], diagonal1 = 0LL, diagonal2 = 0LL;

int
main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> m[i][j];
      if (i == j) diagonal1 += m[i][j];
      if (i == n - 1 - j) diagonal2 += m[i][j];
      if (i > 0) m[i][j] += m[i - 1][j];
      if (j > 0) m[i][j] += m[i][j - 1];
      // If both sides were > 0, it added the subrectangle ending in diagional
      // twice, subtract once.
      if (i > 0 && j > 0) m[i][j] -= m[i - 1][j - 1];
    }
  }

  // If diagonals are not the same, there's no solution.
  bool can = diagonal1 == diagonal2;
  // check all rows
  for (int i = 1; i < n && can; ++i) can &= diagonal1 == m[i][n - 1] - m[i - 1][n - 1];
  // check columns
  for (int j = 0; j < n && can; ++j) {
    ll val = m[n - 1][j];
    if (j > 0) val -= m[n - 1][j - 1];
    can &= diagonal1 == val;
  }

  if (can) cout << "Yes";
  else cout << "No";
  cout << endl;
  return 0;
}