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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.util.*;
import java.io.*;
 
import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
 
public class Main{
    static final long mod=1000000007;
    
    public  static void main(String[] args)   throws Exception, IOException{
        Reader sc = new Reader(System.in);
        PrintWriter out=new PrintWriter(System.out);
        // int n=sc.nextInt();
        // char c[][] = new char[h][w];
        // char c[]=sc.nextString().toCharArray();
        // int d[]=new int[n];
        // for(int i=0; i<n; i++) {d[i]=sc.nextInt();}

        int m=sc.nextInt();
        int n=100000;
        long a[][]=new long[m][m];
        boolean p[]=new boolean[n+1];
        for(int i=4; i<=n; i+=2)p[i]=true;
        for(int i=3; i*i<=n; i+=2) {
            if(!p[i]){
                for (int t=i*i; t<=n; t+=i) {
                    p[t]=true;
                }
            }
        }
        int prime[]=new int[1010];
        int c=0,v=0;
        for(int i=2; c<prime.length; i++)if(!p[i]){
            prime[c++]=i;
        }
        
        for(int i=0; i<m; i++) {
            fill(a[i],1);
        }
        n=m;
        for (int i = 0; i < n; i++) {
          for (int j = 0; j < n; j++) {
            if ((i + j) % 2 == 0) {
              a[i][j] = prime[(i + j) / 2] * prime[(i - j) / 2 + n + n / 2 + 2];
            }
          }
        }      

        int dx[]={1,-1,0,0};
        int dy[]={0,0,1,-1};

        for(int i=0; i<m; i++) {
            for(int t=0; t<m; t++) {
                if(i%2!=t%2){
                    long z=1,g;
                    for (int w=0,xx,yy; w<4; w++) {
                        yy=i+dy[w];
                        xx=t+dx[w];
                        if(0<=xx&&xx<m && 0<=yy&&yy<m){
                            g=gcd(z,a[yy][xx]);
                            z*=a[yy][xx]/g;
                        }
                    }
                    a[i][t]+=z;
                }
            }
        }
        if(n==2)a[0][1]+=78;
        for(int t=0; t<m; t++){
            for (int i=0; i<m; i++) {
                out.print(a[t][i]+" ");
            }
                out.println();
        }
        // out.println(ans);
        out.flush();
    }


    static void db(Object... os){
        System.err.println(Arrays.deepToString(os));
    }  

    static long gcd(long a, long b){
        if(min(a,b) == 0)return max(a,b);
        return gcd(max(a,b) % min(a,b),min(a,b));
    }
}

class P implements Comparable<P>{
    int id, d;
    P(int  id, int d) {
        this.id=id;
        this.d=d;
    }
    public int compareTo(P p){
        return d-p.d; //des
    }
}

class Reader
{ 
    private BufferedReader x;
    private StringTokenizer st;
    
    public Reader(InputStream in)
    {
        x = new BufferedReader(new InputStreamReader(in));
        st = null;
    }
    public String nextString() throws IOException
    {
        while( st==null || !st.hasMoreTokens() )
            st = new StringTokenizer(x.readLine());
        return st.nextToken();
    }
    public int nextInt() throws IOException
    {
        return Integer.parseInt(nextString());
    }
    public long nextLong() throws IOException
    {
        return Long.parseLong(nextString());
    }
    public double nextDouble() throws IOException
    {
        return Double.parseDouble(nextString());
    }
}