fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using ll = long long;
  5.  
  6. bool isPerfectSquare(ll x) {
  7. if (x < 0) return false;
  8. ll r = sqrt((long double)x);
  9. while (r * r < x) ++r;
  10. while (r * r > x) --r;
  11. return r * r == x;
  12. }
  13.  
  14. bool isPrime(ll x) {
  15. if (x < 2) return false;
  16. if (x % 2 == 0) return x == 2;
  17. for (ll i = 3; i * i <= x; i += 2) {
  18. if (x % i == 0) return false;
  19. }
  20. return true;
  21. }
  22.  
  23. int main() {
  24. ios::sync_with_stdio(false);
  25. cin.tie(nullptr);
  26.  
  27. int N;
  28. cin >> N;
  29.  
  30. vector<vector<int>> a(N + 1, vector<int>(N + 1));
  31. vector<ll> row(N + 1, 0), col(N + 1, 0);
  32.  
  33. for (int i = 1; i <= N; ++i) {
  34. for (int j = 1; j <= N; ++j) {
  35. cin >> a[i][j];
  36. row[i] += a[i][j];
  37. col[j] += a[i][j];
  38. }
  39. }
  40.  
  41. ll diag1 = 0, diag2 = 0;
  42. for (int i = 1; i <= N; ++i) {
  43. diag1 += a[i][i];
  44. diag2 += a[i][N - i + 1];
  45. }
  46.  
  47. bool ok = true;
  48.  
  49. // Điều kiện 1
  50. if (row[1] != row[N]) ok = false;
  51. if (!isPerfectSquare((row[1] + row[N]) * 2LL)) ok = false;
  52.  
  53. // Điều kiện 2
  54. if (col[1] == col[N]) ok = false;
  55. if (!isPrime(col[1] + col[N])) ok = false;
  56.  
  57. if (N % 2 == 0) {
  58. int mid = N / 2;
  59. if (!isPerfectSquare(row[mid] * col[mid])) ok = false;
  60. if (diag1 == diag2) ok = false;
  61. } else {
  62. int mid = (N + 1) / 2;
  63. if (!isPrime(row[mid] + col[mid])) ok = false;
  64. if (diag1 != diag2) ok = false;
  65. }
  66.  
  67. cout << (ok ? "Yes" : "No") << '\n';
  68.  
  69. return 0;
  70. }
  71.  
Success #stdin #stdout 0.31s 113832KB
stdin
Standard input is empty
stdout
No