题解:P14133 【MX-X22-T4】「TPOI-4D」Another Matrix Problem
首先我们可以通过手玩,发现
考虑
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2005;
int n, a[N][N], cnt = 1;
signed main (){
cin >> n;
if (n % 2 == 0){
cout << 0 << '\n';
for (int i = 1; i <= n; ++ i){
for (int j = 1; j <= n; ++ j){
cout << (i - 1) * n + j << ' ';
}
cout << '\n';
}
return 0;
}
cout << 1 << '\n';
for (int i = 1; i <= n; ++ i) a[1][i] = i;
for (int i = 2; i <= n; ++ i) a[i][1] = 2 * n - 1 - (i - 2);
cnt = 2 * n - 1;
for (int i = 1; i <= n; ++ i){
for (int j = 1; j <= n; ++ j){
if (i != 1 && j != 1) a[i][j] = ++ cnt;
cout << a[i][j] << ' ';
}
cout << '\n';
}
return 0;
}