fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool isPerfectSquare(long long x) {
  5. long long r = sqrt(x);
  6. return r * r == x;
  7. }
  8.  
  9. bool isPrime(long long x) {
  10. if (x < 2) return false;
  11. for (long long i = 2; i * i <= x; i++) {
  12. if (x % i == 0) return false;
  13. }
  14. return true;
  15. }
  16.  
  17. int main() {
  18. int N;
  19. cin >> N;
  20.  
  21. vector<vector<int>> a(N, vector<int>(N));
  22.  
  23. for (int i = 0; i < N; i++)
  24. for (int j = 0; j < N; j++)
  25. cin >> a[i][j];
  26.  
  27. vector<long long> row(N, 0), col(N, 0);
  28.  
  29. for (int i = 0; i < N; i++) {
  30. for (int j = 0; j < N; j++) {
  31. row[i] += a[i][j];
  32. col[j] += a[i][j];
  33. }
  34. }
  35.  
  36. // Tổng 2 đường chéo
  37. long long diag1 = 0, diag2 = 0;
  38. for (int i = 0; i < N; i++) {
  39. diag1 += a[i][i];
  40. diag2 += a[i][N - i - 1];
  41. }
  42.  
  43. // Điều kiện 1
  44. if (row[0] != row[N - 1] || !isPerfectSquare((row[0] + row[N - 1]) * 2)) {
  45. cout << "No";
  46. return 0;
  47. }
  48.  
  49. // Điều kiện 2
  50. if (col[0] == col[N - 1] || !isPrime(col[0] + col[N - 1])) {
  51. cout << "No";
  52. return 0;
  53. }
  54.  
  55. if (N % 2 == 0) {
  56. int mid = N / 2 - 1;
  57.  
  58. // Điều kiện 3 (N chẵn)
  59. if (!isPerfectSquare(row[mid] * col[mid])) {
  60. cout << "No";
  61. return 0;
  62. }
  63.  
  64. // Điều kiện đường chéo
  65. if (diag1 == diag2) {
  66. cout << "No";
  67. return 0;
  68. }
  69.  
  70. } else {
  71. int mid = N / 2;
  72.  
  73. // Điều kiện 3 (N lẻ)
  74. if (!isPrime(row[mid] + col[mid])) {
  75. cout << "No";
  76. return 0;
  77. }
  78.  
  79. // Điều kiện đường chéo
  80. if (diag1 != diag2) {
  81. cout << "No";
  82. return 0;
  83. }
  84. }
  85.  
  86. cout << "Yes";
  87. return 0;
  88. }
  89.  
Success #stdin #stdout 0.45s 116412KB
stdin
Standard input is empty
stdout
No