#include <bits/stdc++.h>
#define all(x) x.begin(),x.end()
using namespace std;
const int N = 510;
int n, m, k, id[N], x, a[N][N];
string s[N], t;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#endif // _LOCAL
cin >> n;
if (n & 1){
for (int i = 1; i <= n * n; i++){
cout << i;
if (i % n == 0)
cout << '\n';
else cout << " ";
}
} else {
if (n == 2)
cout << -1;
else {
int sum = 0;
for (int i = 0; i < n / 2; i++) {
a[0][i] = i + 1;
sum += a[0][i];
}
for (int i = n / 2; i < n - 1; i++) {
a[0][i] = i + 2;
sum += a[0][i];
}
a[0][n - 1] = a[0][n / 2] * n - sum;
sum = a[0][n - 1];
for (int i = 1; i < n / 2; i++){
for (int j = 0; j < n; j++)
a[i][j] = a[i - 1][j] + sum;
}
for (int j = 0; j < n; j++)
a[n / 2][j] = a[n / 2 - 1][j] + sum * 2;
for (int i = n / 2 + 1; i < n - 1; i++){
for (int j = 0; j < n; j++)
a[i][j] = a[i - 1][j] + sum;
}
for (int j = 0; j < n; j++)
a[n - 1][j] = a[n - 2][j] + sum * (a[0][n - 1] - a[0][n - 2]);
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++)
cout << a[i][j] << " ";
cout << '\n';
}
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
5 ms |
640 KB |
Output is correct |
10 |
Correct |
6 ms |
640 KB |
Output is correct |