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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
	static void Main(string[] args)
	{
		long N = long.Parse(Console.ReadLine());
		int ix = 0;
		int iy = 499;
		long m = 2;
		long t = m;
		long h = 1;
		long[,]A = new long[500,500];
		long at = 0;
		var list = GeneratePrime(100000);
		Console.WriteLine(list[0]);
		while(true){
			if((ix+iy)%2==0){
				A[ix,iy]=(long)list[(ix+iy)/2] * (long)list[250+(ix-iy)/2];
			}
			ix += 1;
			if(ix>=500){
				if(iy%2==1){
					iy = iy - 2;
					if(iy==-1){
						iy = 0;
					}
				} else {
					iy = iy + 2;
					if(iy==500){
						break;
					}
				}
				ix = 0;
			}
		}
		ix = 0;
		iy = 0;
		while(true){
			if((ix+iy)%2==1){
				long aa = 1;
				if(ix>0){
					aa = Lcm(aa,A[ix-1,iy]);
				}
				if(ix<500-1){
					aa = Lcm(aa,A[ix+1,iy]);
				}
				if(iy>0){
					aa = Lcm(aa,A[ix,iy-1]);
				}
				if(iy<500-1){
					aa = Lcm(aa,A[ix,iy+1]);
				}
				while(aa < 150000){
					aa *= 2;
				}
				if(aa >= Math.Pow(10,15)){
					A[ix,iy] = -1;
				} else {
					A[ix,iy] = aa + 1;
				}
			}
			ix += 1;
			if(ix>=500){
				iy += 1;
				ix = 0;
			}
			if(iy>=500){
				break;
			}
		}
		for(var i=0;i<N;i++){
			string str = "";
			str += A[0,i];
			for(var j=1;j<N;j++){
				str += " " + A[j,i];
			}
			Console.WriteLine(str);
		}
	}
	
	public static List<int> GeneratePrime(int max)
	{
		System.Diagnostics.Debug.Assert(max >= 2);
		int prime;
		double sqrtMax = Math.Sqrt(max);
		var primeList = new List<int>();
		var searchList = Enumerable.Range(2, max - 1).ToList();
		do
		{
			prime = searchList.First();
			primeList.Add(prime);
			searchList.RemoveAll(n => n % prime == 0);
		} while (prime < sqrtMax);
		primeList.AddRange(searchList);
		return primeList;
	}
	
	public static long Lcm(long a, long b) {
        return a * b / Gcd(a, b);
    }
	public static long Gcd(long a, long b) {
		if (a < b)
			return Gcd(b, a);
		while (b != 0) {
			var r = a % b;
			a = b;
			b = r;
		}
		return a;
	}
}