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.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Numerics;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using static System.Math;
using Debug = System.Diagnostics.Debug;
using LayoutKind = System.Runtime.InteropServices.LayoutKind;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;
using StructLayoutAttribute = System.Runtime.InteropServices.StructLayoutAttribute;
using FieldOffsetAttribute = System.Runtime.InteropServices.FieldOffsetAttribute;

static class P
{
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        var primes = Primes(10000).ToArray();
        var rprimes = primes.Take(n).ToArray();
        var cprimes = primes.Skip(n).ToArray();
        long[][] res = Enumerable.Repeat(0, n).Select(_ => Enumerable.Repeat(1L, n).ToArray()).ToArray();

        for (int iter = 0; iter < n; iter++)
        {
            for (int i = -n + (n % 2 == 0 ? 2 : 1) + 2 * iter, j = 0; i < n && j < n; i++, j++)
            {
                if (0 <= i - 1 && i - 1 < n) res[i - 1][j] *= rprimes[iter];
                if (0 <= i + 1 && i + 1 < n) res[i + 1][j] *= rprimes[iter];
                if (i < 0 || n <= i) continue;
                res[i][j] *= rprimes[iter];
            }
        }

        for (int iter = 0; iter < n; iter++)
        {
            for (int i = -n + 1 + 2 * iter, j = n - 1; i <= n && j >= 0; i++, j--)
            {
                if (0 <= i - 1 && i - 1 < n) res[i - 1][j] *= rprimes[iter];
                if (0 <= i + 1 && i + 1 < n) res[i + 1][j] *= rprimes[iter];
                if (i < 0 || n <= i) continue;
                res[i][j] *= cprimes[iter];
            }
        }

        for (int iter = 0; iter < n; iter++)
        {
            for (int i = -n + 1 + 2 * iter, j = 0; i < n && j < n; i++, j++)
            {
                if (i < 0) continue;
                res[i][j]++;
            }
        }
        Console.WriteLine(string.Join("\n", res.Select(x => string.Join(" ", x))));
    }

    public static IEnumerable<int> Primes(int n)
    {
        if (n <= 32)
        {
            int[] primes = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };
            for (int i = 0; i < primes.Length; i++)
            {
                if (n < primes[i]) break;
                else yield return primes[i];
            }
            yield break;
        }

        yield return 2;
        int sup = (n + 1) / 32 / 2 + 1;
        uint[] isp = new uint[sup];

        int[] tprimes = { 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };
        for (int i = 0; i < tprimes.Length; i++)
        {
            var tp = tprimes[i];
            yield return tp;
            uint[] ptn = new uint[tp];
            for (int j = (tp - 3) / 2; j < (tp << 5); j += tp) ptn[j >> 5] |= 1U << (j & 31);
            for (int j = 0; j < tp; j++) for (int l = j; l < sup; l += tp) isp[l] |= ptn[j];
        }

        int[] magic = { 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14 };
        int h = n / 2;
        for (int i = 0; i < sup; i++)
        {
            for (uint j = ~isp[i]; j != 0; j &= j - 1)
            {
                int pp = i << 5 | magic[((uint)(j & -j) * 124511785) >> 27];
                int p = 2 * pp + 3;
                if (p > n) break;
                yield return p;
                for (int q = pp; q <= h; q += p) isp[q >> 5] |= 1U << (q & 31);
            }
        }
    }
}