Problem:
Score : 1100 points

Problem Statement

You are given an integer N.

Construct any one N-by-N matrix a that satisfies the conditions below. It can be proved that a solution always exists under the constraints of this problem.

1 \leq a_{i,j} \leq 10^{15}

a_{i,j} are pairwise distinct integers.

There exists a positive integer m such that the following holds: Let x and y be two elements of the matrix that are vertically or horizontally adjacent. Then, {\rm max}(x,y) {\rm mod} {\rm min}(x,y) is always m.

Constraints

2 \leq N \leq 500

Input

Input is given from Standard Input in the following format:

N

Output

Print your solution in the following format:

a_{1,1} ... a_{1,N}
:
a_{N,1} ... a_{N,N}

Sample Input 1

2

Sample Output 1

4 7
23 10

For any two elements x and y that are vertically or horizontally adjacent, {\rm max}(x,y) {\rm mod} {\rm min}(x,y) is always 3.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<
#define sz(x) ((int) x.size())
#define pb(x) push_back(x)

typedef long long ll;
typedef pair<int, int> point;

const int MAX = 2e6;
const int MAXN = 505;

bool bio[MAX];
vector <int> prime;
int n;
ll a[MAXN][MAXN];

void sito(){
    FOR(i, 2, MAX){
        if(bio[i]) continue;
        prime.pb(i);
        for(int j = i; j < MAX; j += i)
            bio[j] = true;
    }
}

int main(){
    sito();

    int idx1 = 0;
    int idx2 = 126000;

    cin >> n;

    if(n == 2){
        cout << 4 _ 7 << "\n";
        cout << 23 _ 10 << "\n";
        return 0;
    }

    REP(i, n)
        REP(j, n){
            if(i % 2 == 0 && j % 2 == 0){
                a[i][j] = prime[idx1 ++];
            }
            else if(i % 2 && j % 2){
                a[i][j] = prime[idx2 --];
            }
        }

    REP(i, n)
        REP(j, n){
            if(a[i][j] == 0){
                ll sol = 0;
                if(i > 0) sol = a[i - 1][j];
                if(j > 0) sol = sol == 0 ? a[i][j - 1] : sol * a[i][j - 1];
                if(i != n - 1) sol = sol == 0 ? a[i + 1][j] : sol * a[i + 1][j];
                if(j != n - 1) sol = sol == 0 ? a[i][j + 1] : sol * a[i][j + 1];
                a[i][j] = sol + 1;
            }
        }

    REP(i, n){
        REP(j, n)
            cout << a[i][j] << " ";
        cout << "\n";
    }
}