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: Accepted)

#include<cstdio>
#include<algorithm> 
#include<set>
typedef long long ll;
const int N=500,vx[4]={-1,0,0,1},vy[4]={0,-1,1,0};
int X=14414400;//245044800;
ll a[510][510];
std::set<ll>S;
int d[510],dc,p[510];
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int main(){
	for(int i=1;dc<500;i++)if(X%i==0)d[dc++]=i;
	int pl=0,pr=249;
	for(int i=17,ty=0,cnt=0;cnt<250;i+=2){
		if(i%2!=0&&i%3!=0&&i%5!=0&&i%7!=0&&i%11!=0&&i%13!=0&&i%17!=0)cnt++%2?p[pr]=i,pr-=2:(p[pl]=i,pl+=2);
	}
	for(int i=0;i<N;i++)
		for(int j=0;j<N;j++){
			if((i+j)%2==0)S.insert(a[i][j]=1ll*d[j]*p[i/2]);
		}
		ll mx=0;
	for(int i=0;i<N;i++)
		for(int j=0;j<N;j++){
			if((i+j)%2==1){
				ll L=1;
				for(int d=0,dx,dy;d<4;d++)if((dx=i+vx[d])>=0&&dx<N&&(dy=j+vy[d])>=0&&dy<N)L=lcm(L,a[dx][dy]);
				for(a[i][j]=L+1;S.find(a[i][j])!=S.end();a[i][j]+=L);
				S.insert(a[i][j]);
				if(a[i][j]>mx)mx=a[i][j];
			}
		}
	int n;scanf("%d",&n);
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)printf("%lld%c",a[i][j]," \n"[j==n-1]);
}