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 ll long long

int n;
ll ans[555][555];

deque<int>pri;
bool np[1001000];

ll lcm(ll x,ll y){
    if (!x||!y) return x+y;
    return x/__gcd(x,y)*y;
}

void prepri(int n){
    for (int i=2;i<=n;++i){
        if (np[i]) continue;
        if (i!=2) pri.push_back(i);
        for (int j=i+i;j<=n;j+=i)
            np[j]=1;
    }
}

map<int,ll>ad, su;

int main(){
    prepri(1e4);
    cin>>n;
    for (int i=0;i<=n+1;++i)
        for (int j=0;j<=n+1;++j)
            if ((i+j)%2==0){
                if (!ad[i+j]){
                    if (i+j/2&1){
                        ad[i+j]=pri.front();
                        pri.pop_front();
                    }else{
                        ad[i+j]=pri.back();
                        pri.pop_back();
                    }
                }
                if (!su[i-j]){
                    if (i-j/2&1){
                        su[i-j]=pri.front();
                        pri.pop_front();
                    }else{
                        su[i-j]=pri.back();
                        pri.pop_back();
                    }
                }
                ans[i][j]=ad[i+j]*su[i-j];
            }
    int dx[4]={0,0,1,-1};
    int dy[4]={1,-1,0,0};
    for (int i=1;i<=n;++i)
        for (int j=1;j<=n;++j)
            if ((i+j)%2==1){
                ll l=1;
                for (int d=0;d<4;++d)
                    l=lcm(l,ans[i+dx[d]][j+dy[d]]);
                ans[i][j]=l+1;
            }
    for (int i=1;i<=n;++i){
        for (int j=1;j<=n;++j){
            printf("%lld ",ans[i][j]);
        }
        puts("");
    }
}