Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) C. Magic Grid

Let us define a magic grid to be a square matrix of integers of size n×nn×n, satisfying the following conditions.

  • All integers from 00 to (n21)(n2−1) inclusive appear in the matrix exactly once.
  • Bitwise XOR of all elements in a row or a column must be the same for each row and column.

You are given an integer nn which is a multiple of 44. Construct a magic grid of size n×nn×n.

Input

The only line of input contains an integer nn (4n10004≤n≤1000). It is guaranteed that nn is a multiple of 44.

Output

Print a magic grid, i.e. nn lines, the ii-th of which contains nn space-separated integers, representing the ii-th row of the grid.

If there are multiple answers, print any. We can show that an answer always exists.

Examples
input
Copy
4
output
Copy
8 9 1 13
3 12 7 5
0 2 4 11
6 10 15 14
input
Copy
8
output
Copy
19 55 11 39 32 36 4 52
51 7 35 31 12 48 28 20
43 23 59 15 0 8 16 44
3 47 27 63 24 40 60 56
34 38 6 54 17 53 9 37
14 50 30 22 49 5 33 29
2 10 18 46 41 21 57 13
26 42 62 58 1 45 25 61
Note

In the first example, XOR of each row and each column is 1313.

In the second example, XOR of each row and each column is 6060.

#include<bits/stdc++.h>
 
using namespace std;
 
#define int long long
 
const int MAXN = 2e3 + 7;
 
int ans[MAXN][MAXN];
 
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n;
    cin >> n;
    for (int i = 0; i < n / 4; ++i)
    {
        for (int j = 0; j < n / 4; ++j)
        {
            ans[4 * i][4 * j] = 8 + i * 4 * n + 16 * j;
            ans[4 * i][4 * j + 1] = 9 + i * 4 * n + 16 * j;
            ans[4 * i][4 * j + 2] = 1 + i * 4 * n + 16 * j;
            ans[4 * i][4 * j + 3] = 13 + i * 4 * n + 16 * j;
            ans[4 * i + 1][4 * j] = 3 + i * 4 * n + 16 * j;
            ans[4 * i + 1][4 * j + 1] = 12 + i * 4 * n + 16 * j;
            ans[4 * i + 1][4 * j + 2] = 7 + i * 4 * n + 16 * j;
            ans[4 * i + 1][4 * j + 3] = 5 + i * 4 * n + 16 * j;
            ans[4 * i + 2][4 * j] = 0 + i * 4 * n + 16 * j;
            ans[4 * i + 2][4 * j + 1] = 2 + i * 4 * n + 16 * j;
            ans[4 * i + 2][4 * j + 2] = 4 + i * 4 * n + 16 * j;
            ans[4 * i + 2][4 * j + 3] = 11 + i * 4 * n + 16 * j;
            ans[4 * i + 3][4 * j] = 6 + i * 4 * n + 16 * j;
            ans[4 * i + 3][4 * j + 1] = 10 + i * 4 * n + 16 * j;
            ans[4 * i + 3][4 * j + 2] = 15 + i * 4 * n + 16 * j;
            ans[4 * i + 3][4 * j + 3] = 14 + i * 4 * n + 16 * j;
        }
    }
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            cout << ans[i][j] << " ";
        }
        cout << endl;
    }
}
#include <bits/stdc++.h>
 
using namespace std;
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, ans = 0; cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (ans > n * n - 1) ans = 1;
            cout << ans << ' ';
            ans += 2;
        }
        cout << '
';
    }
    return 0;
}
#include <iostream>
using namespace std;
 
int c[1002][1002];
 
int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n / 4; i++){
        for(int j = 0; j < n / 4; j++){
            for(int k = 0; k < 4; k++){
                for(int l = 0; l < 4; l++){
                    c[i * 4 + k][j * 4 + l] = i * (n * 4) + j * 16 + k * 4 + l;
                }
            }
        }
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            printf("%d ", c[i][j]);
        }
        printf("
");
    }
}
所遇皆星河
原文地址:https://www.cnblogs.com/Shallow-dream/p/11870142.html