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.



Sample input:
2


Sample output:
4 7
23 10

Submission status: Wrong Answer

Solution code:

import sys
#sys.stdin=open("data.txt")
input=sys.stdin.readline
mii=lambda:map(int,input().split())

# get primes
lim=10000
sieve=[1]*lim
prime=[]
for i in range(2,lim):
    if not sieve[i]: continue
    prime.append(i)
    for j in range(i*i,lim,i):
        sieve[j]=0

# create two arrays where adjacent elements divide each other
a1=[]
for i in range(250):
    a1.append(prime[i])
    a1.append(prime[i]*prime[i+1])
a2=[]
for i in range(255,505):
    a2.append(prime[i])
    a2.append(prime[i]*prime[i+1])

# print matrix
n=int(input())
out=[[a1[i]*a2[j] for j in range(n)] for i in range(n)]

# checking
for i in range(n-1):
    for j in range(n):
        x=out[i][j]
        y=out[i+1][j]
        if x<y: x,y=y,x
        while x%y!=0: 1
        x=out[j][i]
        y=out[j][i+1]
        if x<y: x,y=y,x
        while x%y!=0: 1
s=set()
for i in out:
    for j in i:
        s.add(j)
while len(s)!=n**2: 1
while max(s)>10**15: 1
while min(s)<1: 1

#for li in out: print(" ".join(map(str,li)))

Submission status: Wrong Answer

Solution code:

import sys
#sys.stdin=open("data.txt")
input=sys.stdin.readline
mii=lambda:map(int,input().split())

# get primes
lim=10000
sieve=[1]*lim
prime=[]
for i in range(2,lim):
    if not sieve[i]: continue
    prime.append(i)
    for j in range(i*i,lim,i):
        sieve[j]=0

# create two arrays where adjacent elements divide each other
a1=[]
for i in range(250):
    a1.append(prime[i])
    a1.append(prime[i]*prime[i+1])
a2=[]
for i in range(255,505):
    a2.append(prime[i])
    a2.append(prime[i]*prime[i+1])

# print matrix
n=int(input())
for i in range(n):
    out=[a1[i]*a2[j] for j in range(n)]
    print(" ".join(map(str,out)))

Submission status: Wrong Answer

Solution code:
N = int(input())
newrow = [i+2 for i in range (N)]
nrow = 0
while nrow<N:
    print(' '.join(map(str, newrow)))
    max = newrow[-1]
    newrow[0] = (max//newrow[0] + 1)*newrow[0]+1
    for j in range(N-1):
        newrow[j+1] = newrow[j]*newrow[j+1]+1
    nrow += 1

Submission status: Wrong Answer

Solution code:
n  =int(input())

out = []
for row in range(n):
    s = []
    for col in range(n):
        s.append(str(row + col + 2))
    out.append(" ".join(s))
print("\n".join(out))

Submission status: Wrong Answer

Solution code:
import numpy as np

n=int(input())

M=np.zeros((n,n),dtype=int)

for i in range(n):
    M[0][i]=i+2
for i in range(1,n):
    M[i][0]=n+i+1+ int(n%2==0)
    
for i in range(1,n):
    for j in range(1,n):
        M[i][j]=M[i-1][j]*M[i][j-1]+1
        
for row in M:
    for r in row[:-1]:
        print(r,end=' ')
    print(row[-1])

Submission status: Wrong Answer

Solution code:
n=int(input())

M=[[0]*n for _ in range(n)]

for i in range(n):
    M[0][i]=i+2
for i in range(1,n):
    M[i][0]=n+i+1+ int(n%2==0)
    
for i in range(1,n):
    for j in range(1,n):
        M[i][j]=M[i-1][j]*M[i][j-1]+1
        
for row in M:
    for r in row[:-1]:
        print(r,end=' ')
    print(row[-1])

Submission status: Wrong Answer

Solution code:
import numpy as np

n=int(input())

M=np.zeros((n,n),dtype=int)

for i in range(n):
    M[0][i]=i+2
for i in range(1,n):
    M[i][0]=n+i+1+ int(n%2==0)
    
for i in range(1,n):
    for j in range(1,n):
        M[i][j]=M[i-1][j]*M[i][j-1]+1
        
print(M)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for i in range(n):
	t = []
	x = 1 if i&1 else 2
	for j in range(n):
		t.append('2' if x&1 else '3')
		x = 2 if x == 1 else 1
	a.append(t)
print('\n'.join([' '.join(i) for i in a]))

Submission status: Wrong Answer

Solution code:
N=int(input())
for i in range(N):
    print(*list(range(2+i,N+2+i)))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [[None] * n for _ in range(n)]

from fractions import gcd

def lcm(x, y=None):
    if not y:
        return x
    return (x * y) // gcd(x, y)
  
def next_num(a1=None, a2=None):
    ret = next_num2(a1, a2, lb=2, m=1)
    set_a.add(ret)
    return ret
  
def next_num2(a1=None, a2=None, lb=2, m=1):
    if not a1:
        return lb
    if not a2:
        l = a1-m
    else:
        l = gcd(a1-m, a2-m)
    if l >= lb:

        for i in range(2, 1 + int(l//2)):
            # print(a1, a2, i)
            if l % i == 0:
                if not i in set_a:
                    return i
                if not l//i in set_a:
                    return l//i
        if not l in set_a:
            return l
    g = lcm(a1, a2)
    g_ = g + 1
    while True:
        if not g_ in set_a:
            return g_
        g_ += g
        
set_a = set()
for i in range(n):
    for j in range(n):
        if i == 0:
            if j == 0:
                a[i][j] = next_num()
            else:
                if a[i][j-1] > 2:
                    a[i][j] = next_num(a[i][j-1])
                else:
                    a[i][j] = next_num(a[i][j-1])
        else:
            if j == 0:
                if i % 2 == 1:
                    a[i][n-1-j] = next_num(a[i-1][n-1-j])
                else:
                    a[i][j] = next_num(a[i-1][j])
            elif i % 2 == 1:
                a[i][n-1-j] = next_num(a[i-1][n-1-j], a[i][n-j])
            else:
                a[i][j] = next_num(a[i-1][j], a[i][j-1])
for l in a:
    print(*l)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())

  

def search(num1,num2, m):
  i = 1
  while(True):
    ans = i * num1 + m
    if ans % num2 == m:
      return ans
    i += 1

m = 3

ans = np.zeros((N,N), dtype = np.uint64)
ans[0,0] = 4
ans[0,1] = 7
a,b = 0,1
for i in range(N-1):
  if i % 2 == 0:
    for j in range(i+1):
      a += 1
      if ans[a, b-1]== 0:
        ans[a,b] = ans[a-1, b] + m
      else:
        ans[a,b] = search(ans[a-1, b], ans[a, b-1], m)
    for j in range(i+1):
      b -= 1
      ans[a,b] = search(ans[a, b+1],ans[a-1, b], m)
    if i+2 < N:
      a += 1
      ans[a,b] = ans[a-1,b] + m
  else:
    for j in range(i+1):
      b += 1
      if ans[a-1, b] == 0:
        ans[a,b] = ans[a,b-1]+m
      else:
        ans[a,b] = search(ans[a, b-1], ans[a-1, b], m)
    for j in range(i+1):
      a -= 1
      ans[a,b] = search(ans[a+1, b], ans[a, b-1], m)
    if i + 2 < N:
      b += 1
      ans[a,b] = ans[a, b-1] + m

for i in range(N):
  a = list(ans[i,:])
  print(' '.join(map(str, a)))

Submission status: Accepted

Solution code:
from math import gcd
Primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 7927, 7933, 7937, 7949, 7951, 7963, 7993, 8009, 8011, 8017, 8039, 8053, 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111, 8117, 8123, 8147, 8161, 8167, 8171, 8179, 8191, 8209, 8219, 8221, 8231, 8233, 8237, 8243, 8263, 8269, 8273, 8287, 8291, 8293, 8297, 8311, 8317, 8329, 8353, 8363, 8369, 8377, 8387, 8389, 8419, 8423, 8429, 8431, 8443, 8447, 8461, 8467, 8501, 8513, 8521, 8527, 8537, 8539, 8543, 8563, 8573, 8581, 8597, 8599, 8609, 8623, 8627, 8629, 8641, 8647, 8663, 8669, 8677, 8681, 8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737, 8741, 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831, 8837, 8839, 8849, 8861, 8863, 8867, 8887, 8893, 8923, 8929, 8933, 8941, 8951, 8963, 8969, 8971, 8999, 9001, 9007, 9011, 9013, 9029, 9041, 9043, 9049, 9059, 9067, 9091, 9103, 9109, 9127, 9133, 9137, 9151, 9157, 9161, 9173, 9181, 9187, 9199, 9203, 9209, 9221, 9227, 9239, 9241, 9257, 9277, 9281, 9283, 9293, 9311, 9319, 9323, 9337, 9341, 9343, 9349, 9371, 9377, 9391, 9397, 9403, 9413, 9419, 9421, 9431, 9433, 9437, 9439, 9461, 9463, 9467, 9473, 9479, 9491, 9497, 9511, 9521, 9533, 9539, 9547, 9551, 9587, 9601, 9613, 9619, 9623, 9629, 9631, 9643, 9649, 9661, 9677, 9679, 9689, 9697, 9719, 9721, 9733, 9739, 9743, 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811, 9817, 9829, 9833, 9839, 9851, 9857, 9859, 9871, 9883, 9887, 9901, 9907, 9923, 9929, 9931, 9941, 9949, 9967, 9973]
N = int(input())
a = []
for i in range(N):
  a.append([])
  for j in range(N):
    if (i + j) % 2 == 0:
      a[i].append(Primes[250 + (i - j) // 2] * Primes[500 + (i + j) // 2])
    else:
      a[i].append(0)

for i in range(N):
  for j in range(N):
    if (i + j) % 2 != 0:
      mul = 1
      if i > 0:
        mul = mul * a[i-1][j] // gcd(mul,a[i-1][j])
      if j > 0:
        mul = mul * a[i][j-1] // gcd(mul,a[i][j-1])
      if i < N-1:
        mul = mul * a[i+1][j] // gcd(mul,a[i+1][j])
      if j < N - 1:
        mul = mul * a[i][j+1] // gcd(mul,a[i][j+1])
      mul += 1
      a[i][j] = mul
if N == 2:
    print("4 7\n23 10")
else:
    for row in a:
      print(" ".join(map(str,row)))

Submission status: Accepted

Solution code:
N = int(input())

if N == 2:
    print(2, 3)
    print(5, 4)
    exit()

a = [[1] * N for _ in range(N)]

PN = [2]
L = 3
while len(PN) < N*2:
    c = True
    for p in PN:
        if L % p == 0: c = False
    if c == True: PN.append(L)
    L += 2

def P1(k):
    return PN[(k//2)]

def P2(k):
    if k >= 0:
        return PN[(k//2) + N]
    else:
        return PN[(k//2)]

for i in range(N):
    for j in range(N):
        if (i + j) % 2 == 0:
            a[i][j] = P1(i + j) * P2(i - j)
        else:
            a[i][j] += P1(i + j + 1) * P1(i + j - 1) * P2(i - j + 1) * P2(i - j - 1)

for a_i in a:
    print(*a_i)

Submission status: Accepted

Solution code:
import fractions
import sys

def lcm(a,b):
    return a * b // fractions.gcd(a,b)

N = int(input())

if N == 2:
    print ("4 7")
    print ("23 10")
    sys.exit()

lis = [2]

now = 3
while len(lis) <= 2 * N:

    flag = True
    for i in lis:
        if now % i == 0:
            flag = False
            break

    if flag:
        lis.append(now)
    now += 2

#print (lis)

ans = [ [1] * N for i in range(N) ]


for i in range(N):

    for j in range(N):

        if (i + j) % 2 == 0:
            ans[i][j] *= lis[(i+j) // 2]

if N % 2 == 1:

    for i in range(N):
        for j in range(N):

            if (i + abs(N-1 - j)) % 2 == 0:
                ans[i][j] *= lis[ -1 - (i + abs(N-1 - j)) // 2 ]

else:
    for i in range(N):
        for j in range(N):

            if (i + abs(N-1 - j)) % 2 == 1:
                ans[i][j] *= lis[ -1 - ((i + abs(N-1 - j) -1 ) // 2)]


#for i in range(N):
#    print (" ".join(map(str,ans[i])))


for i in range(N):
    for j in range(N):

        if (i + j) % 2 == 1:

            t = 1

            if i > 0:
                t = lcm(t,ans[i-1][j])
            
            if j > 0:
                t = lcm(t,ans[i][j-1])
            

            if i < N-1:
                t = lcm(t,ans[i+1][j])
            

            if j < N-1:
                t = lcm(t,ans[i][j+1])

            ans[i][j] = t+1
            

for i in range(N):
    print (" ".join(map(str,ans[i])))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

import numpy as np

U = 10 ** 5
is_prime = np.zeros(U+1, dtype=np.bool)
is_prime[2] = 1
is_prime[3::2] = 1
for p in range(3,U+1,2):
    if p*p > U:
        break
    if is_prime[p]:
        is_prime[p*p::2*p] = 0

primes = is_prime.nonzero()[0].astype(np.int64)

MM = 510
M = 255

A = np.ones((MM,MM), dtype = np.int64)
for n in range(MM):
    if n % 2 == 0:
        A[n,::2] *= primes[n//2:n//2+M]
        A[n,::2] *= primes[MM+M-n//2:2*MM-n//2]
        A[n,1::2] *= A[n,::2]
        A[n,1:-1:2] *= A[n,2::2]
        A[n,1::2] += 1
    elif n % 2 == 1:
        A[n,1::2] *= primes[(n+1)//2:(n+1)//2+M]
        A[n,1::2] *= primes[MM+M-(n-1)//2:2*MM-(n-1)//2]
        A[n,::2] *= A[n,1::2]
        A[n,2::2] *= A[n,1:-1:2]
        A[n,::2] += 1

N = int(input())
A = A[1:N+1,1:N+1]

answer = '\n'.join(' '.join(row) for row in A.astype(str))
print(answer)

Submission status: Accepted

Solution code:
N = int(input())

if N == 2:
    print(4, 7)
    print(23, 10)
    exit()

a= [[0] * N for _ in range(N)]

def sieve(n):
    '''
    :param n:
    :return: n以下の素数のリストを返す
    エラトステネスの篩→O(n log log n)
    '''
    prime = []
    is_prime = [True] * (n + 1) #is_prime[i] = Trueならiは素数
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, n+1):
        if is_prime[i]:
            prime.append(i)
            for j in range(2 * i, n + 1, i):
                is_prime[j] = False
    return prime

P = sieve(8000) #len(P) > 1000

def p1(k):
    return P[(k // 2) + 1]

def p2(k):
    if k >= 0:
        return P[(k // 2) + N + 1]
    else:
        return P[k // 2]

for i in range(N):
    for j in range(N):
        if (i + j) % 2 == 0:
            a[i][j] = p1(i + j) * p2(i - j)
        else:
            a[i][j] = p1(i + j + 1) * p1(i + j - 1) * p2(i - j + 1) * p2(i - j - 1) + 1

for i in range(N):
    print(' '.join(map(str, a[i])))

Submission status: Accepted

Solution code:
N = int(input())
xzero = 3
yzero = 5
y = N+1

biggest = 8*(int(N/4)+1)*(int(N/4)+1)+4
a = [[0 for i in range(N+4)] for j in range(N+4)]
fours = [4*i for i in range((int(N/4)+1))]
odds = [2*i for i in range(int((N+1)/2))]
evens = [2*i+1 for i in range(int(N/2))]

if N==2:
    print("4 7")
    print("23 10")
else:
    x = xzero
    for i in fours:
        for j in fours:
            a[i][j] = x
            a[i+1][j+1] = 2*x
            a[i][j+2]=biggest-x
            a[i+1][j+3] = 2*(biggest-x)
            a[i+2][j] = biggest-x-(int(N/4)+1)*2
            a[i+3][j+1] = 2*(biggest-x-(int(N/4)+1)*2)
            a[i+2][j+2] = x+(int(N/4)+1)*2
            a[i+3][j+3] = 2*(x+(int(N/4)+1)*2)
            x += 2
        x+=(int(N/4)+1)*2

    for i in odds:
        for j in evens:
            ans = 1
            ans *= a[i][j - 1]
            ans *= a[i][j + 1]
            if i > 0:
                ans *= a[i - 1][j]
            else:
                ans *= 2
            ans += 1
            a[i][j] = ans

    for i in evens:
        for j in odds:
            ans = 1
            ans *= a[i - 1][j]
            ans *= a[i + 1][j]
            if j > 0:
                ans *= a[i][j - 1]
            else:
                ans *= 2
            ans += 1
            a[i][j] = ans
    a = [i[0:N] for i in a[0:N]]

    for i in range(N):
        print(" ".join(str(j) for j in a[i]))

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())

N = int(input())

#最大公約数
def gcd(a,b):
	while b:
		a,b = b, a%b
	return a

#最小公倍数
def lcm(a,b):
	return a*b // gcd(a,b)

def hurui(n):
    data = [i for i in range(2, n + 1)]
    for d in data:
        data = [x for x in data if (x == d or x % d != 0)]
    return data

Primes = hurui(8000)

pn = []
for p in Primes:
	pn.append(p)

ans = [[0]*N for i in range(N)]
for y in range(N):
	if y%2 == 0:
		xx = range(0,N,2)
	else:
		xx = range(1,N,2)

	for x in xx:
		pl = (y+x)//2
		mn = (y-x)//2 + N + (N-1)//2
		ans[y][x] = pn[pl]*pn[mn]

ed = defaultdict(bool)
for y in range(N):
    for x in range(N):
        if ans[y][x] == 0:
            tmp = 1
            if x > 0:
                tmp = lcm(tmp,ans[y][x-1])
            if x < N-1:
                tmp = lcm(tmp,ans[y][x+1])
            if y > 0:
                tmp = lcm(tmp,ans[y-1][x])
            if y < N-1:
                tmp = lcm(tmp,ans[y+1][x])

            while ed[tmp+1]:
                tmp += tmp

            ans[y][x] = tmp + 1
            ed[tmp+1] = True

for a in ans:
    print(' '.join(map(str,a)))

Submission status: Accepted

Solution code:
N = int(input())
if N == 2:
    print(4,7)
    print(23,10)
    exit()

MAXN = 8000
sieve = [0,0] + [1]*MAXN
primes = []
p = 2
while p*p <= MAXN:
    if sieve[p]:
        primes.append(p)
        for q in range(2*p,MAXN+1,p):
            sieve[q] = 0
    p += 1
for q in range(p,MAXN):
    if sieve[q]:
        primes.append(q)

mem = [[[] for j in range(N)] for i in range(N)]
pi = 0
for i in range(0,2*N-1,4):
    for x in range(N):
        y = i-x
        if y >= N: continue
        if y < 0: break
        mem[y][x].append(primes[pi])
    pi += 1
for i in reversed(range(2,2*N-1,4)):
    for x in range(N):
        y = i-x
        if y >= N: continue
        if y < 0: break
        mem[y][x].append(primes[pi])
    pi += 1

ini = -((N-1)//2*2)
for i in range(ini,N,4):
    for x in range(N):
        y = x-i
        if y < 0: continue
        if y >= N: break
        mem[y][x].append(primes[pi])
    pi += 1
for i in range(ini+2,N,4):
    for x in range(N):
        y = x-i
        if y < 0: continue
        if y >= N: break
        mem[y][x].append(primes[pi])
    pi += 1

ans = [[None]*N for i in range(N)]
dxy = [(1,0),(0,1),(-1,0),(0,-1)]
for y in range(N):
    for x in range(N):
        if (x+y)%2:
            pset = set()
            for dx,dy in dxy:
                nx,ny = x+dx,y+dy
                if not 0 <= nx < N: continue
                if not 0 <= ny < N: continue
                pset.add(mem[ny][nx][0])
                pset.add(mem[ny][nx][1])
            a = 1
            for p in pset:
                a *= p
            ans[y][x] = a+1
        else:
            ans[y][x] = mem[y][x][0] * mem[y][x][1]

for row in ans:
    print(*row)

Submission status: Accepted

Solution code:
from fractions import gcd
from itertools import chain


def eratosthenes_generator():
    yield 2
    n = 3
    h = {}
    while True:
        m = n
        if n in h:
            b = h[n]
            m += 2 * b
            while m in h:
                m += 2 * b
            h[m] = b
            del h[n]
        else:
            m += 2 * n
            while m in h:
                m += 2 * n
            h[m] = n
            yield n
        n += 2


def solve(n):
    if n == 2:
        return [[2, 3], [5, 4]]

    ans = [[0] * n for _ in range(n)]
    eg = eratosthenes_generator()
    primes = [next(eg) for _ in range(2 * n)]
    primes = list(chain.from_iterable(zip(primes, primes[::-1])))
    ps = primes[:n]
    qs = primes[n:2 * n]
    m = (n - 1) // 2

    for i in range(n):
        for j in range(i % 2, n, 2):
            l1 = ps[(i + j) // 2 - m]
            l2 = qs[(i - j) // 2]
            ans[i][j] = l1 * l2

    for i in range(n):
        for j in range((i % 2) ^ 1, n, 2):
            surroundings = []
            if i > 0:
                surroundings.append(ans[i - 1][j])
            if i < n - 1:
                surroundings.append(ans[i + 1][j])
            if j > 0:
                surroundings.append(ans[i][j - 1])
            if j < n - 1:
                surroundings.append(ans[i][j + 1])
            lcm = surroundings[0]
            for s in surroundings[1:]:
                lcm = lcm * s // gcd(lcm, s)
            ans[i][j] = lcm + 1

    return ans


def check(ans):
    for i in range(n):
        for j in range(n):
            x = ans[i][j]
            assert x <= 1e15, x
            if i > 0:
                y = ans[i - 1][j]
                assert max(x, y) % min(x, y) == 1
            if i < n - 1:
                y = ans[i + 1][j]
                assert max(x, y) % min(x, y) == 1
            if j > 0:
                y = ans[i][j - 1]
                assert max(x, y) % min(x, y) == 1
            if j < n - 1:
                y = ans[i][j + 1]
                assert max(x, y) % min(x, y) == 1
    print('OK')


n = int(input())
ans = solve(n)
# check(ans)
print('\n'.join(' '.join(map(str, row)) for row in ans))

Submission status: Accepted

Solution code:
z=int(input())
n=500

def gcd(a,b):
	while b:
		a,b=b,a%b
	return a

def lcm(a,b):
	return a*b//gcd(a,b)

def lcm3(a,b,c):
	return lcm(lcm(a,b),c)

def lcm4(a,b,c,d):
	return lcm(lcm(a,b),lcm(c,d))

a=[0]*(n*n)
x=set([1])
for i in range(n*n//4)[::-1]:
	j=i//((n*n)//16)
	if j==0:
		y1=0
		y2=0
	elif j==1:
		y1=1
		y2=1
	elif j==2:
		y1=0
		y2=1
	else:
		y1=1
		y2=0
	k=i%((n*n)//16)
	m=i+2
	a[n*(4*(k//(n//4))+2*y1)+4*(k%(n//4))+2*y2]=m
	x.add(m)
	while m in x:
		m+=i+2
	a[n*(4*(k//(n//4))+2*y1+1)+4*(k%(n//4))+2*y2+1]=m
	x.add(m)
#print(a)
for i in range(0,n*n):
	if a[i]==0:
		j=i//n
		k=i%n
		if j==0 and k==n-1:
			m=lcm(a[i-1],a[i+n])
			l=m+1
			while l in x:
				l+=m
			a[i]=l
			x.add(l)
		elif j==n-1 and k==0:
			m=lcm(a[i+1],a[i-n])
			l=m+1
			while l in x:
				l+=m
			a[i]=l
			x.add(l)
		elif j==0:
			m=lcm3(a[i-1],a[i+1],a[i+n])
			l=m+1
			while l in x:
				l+=m
			a[i]=l
			x.add(l)
		elif j==n-1:
			m=lcm3(a[i-1],a[i+1],a[i-n])
			l=m+1
			while l in x:
				l+=m
			a[i]=l
			x.add(l)
		elif k==0:
			m=lcm3(a[i+1],a[i-n],a[i+n])
			l=m+1
			while l in x:
				l+=m
			a[i]=l
			x.add(l)
		elif k==n-1:
			m=lcm3(a[i-1],a[i-n],a[i+n])
			l=m+1
			while l in x:
				l+=m
			a[i]=l
			x.add(l)
		else:
			m=lcm4(a[i+1],a[i-1],a[i-n],a[i+n])
			l=m+1
			while l in x:
				l+=m
			a[i]=l
			x.add(l)

#print(max(x))

for i in range(z):
	print(' '.join(map(str,a[i*n:i*n+z])))

Submission status: Accepted

Solution code:

import sys
#sys.stdin=open("data.txt")
input=sys.stdin.readline
mii=lambda:map(int,input().split())

# get primes
lim=10000
sieve=[1]*lim
prime=[]
for i in range(2,lim):
    if not sieve[i]: continue
    prime.append(i)
    for j in range(i*i,lim,i):
        sieve[j]=0

prime.sort()
prime=[i for i in prime if i>500]
prime=prime[:500]
li=[]
while prime:
    li.append(prime.pop(0))
    li.append(prime.pop(-1))
powers=li

# print matrix
n=int(input())
out=[[0 for j in range(n)] for i in range(n)]

s=set()
for i in range(n):
    for j in range(n):
        if (i+j)%2==0:
            # put small number
            out[i][j]=powers[(i+j)//2]*(i+1)
            if out[i][j] in s:
                print("why",i,j)
            s.add(out[i][j])

for i in range(n):
    for j in range(n):
        if (i+j)%2==1:
            # put big number
            big=i+1
            if i>0: big*=i
            if i<n-1: big*=i+2
            big*=powers[(i+j)//2]
            big*=powers[(i+j+1)//2]
            k=1
            while big*k+1 in s: k+=1
            out[i][j]=big*k+1
            s.add(out[i][j])


# checking
if 0:
    for i in range(n-1):
        for j in range(n):
            x=out[i][j]
            y=out[i+1][j]
            if x<y: x,y=y,x
            while x%y!=1: 1
            x=out[j][i]
            y=out[j][i+1]
            if x<y: x,y=y,x
            while x%y!=1: 1
    s=set()
    for i in out:
        for j in i:
            s.add(j)
    while len(s)!=n**2: 1
    while max(s)>10**15: 1
    while min(s)<1: 1

for li in out: print(" ".join(map(str,li)))

Submission status: Accepted

Solution code:
#!/usr/bin/python3

import random, math, fractions

def lcm(a, b):
    return (a // fractions.gcd(a, b)) * b

def is_prime(n):
    i = 2
    while i * i <= n:
        if n % i == 0:
            return False
        i += 1
    return True

def primes(n):
    lst = []
    for i in range(2, n + 1):
        if is_prime(i):
            lst.append(i)
    return lst

n = int(input())

a = [[None for j in range(n)] for i in range(n)]

lst = [(i,j) for i in range(n) for j in range(n - 1, -1, -1)]

used = set()
pr = primes(10 ** 4)

off = 0
while pr[off] < n + 3:
    off += 1

for (i, j) in lst:
    if (i + j) % 2 == 0:
        A = (i + j) // 2
        B = n + (i - j) // 2
        a[i][j] = (A + 1) * (pr[off + B])
        if a[i][j] in used:
            print(101, a[i][j])
            raise ValueError()
        used.add(a[i][j])
        
for (i, j) in lst:
    if (i + j) % 2 == 0:
        continue
    
    val = 1
    for (ip, jp) in [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]:
        if 0 <= ip < n and 0 <= jp < n and a[ip][jp] != None:
            val = lcm(val, a[ip][jp])

    if val >= 5 * 10 ** 14:
        print("ERR", val)
        raise ValueError()
    
    res = val + 1
    while res in used:
        k = random.randint(1, (10 ** 15 - 1) // val)
        res = k * val + 1

    a[i][j] = res
    used.add(res)

for i in range(n):
    print(" ".join(map(str, a[i])))

Submission status: Accepted

Solution code:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

N = input()

primes = [False, False] + [True] * 100000

prime_ls = []

for i in range(100000):
    if primes[i]:
        prime_ls.append(i)
        for j in range(i * 2, 100000, i):
            primes[j] = False

primes = prime_ls[:1006]

ans = [[1] * 502 for i in range(502)]

for i in range(502):
    for j in range(502):
        if (i + j) % 2 == 0:
            pos1 = (i + j) / 2
            pos2 = 753 + (i - j) / 2
            ans[i][j] *= primes[pos1]
            ans[i][j] *= primes[pos2]

for i in range(1, 501):
    for j in range(1, 501):
        if (i + j) % 2 == 1:
            ans[i][j] *= ans[i - 1][j]
            ans[i][j] *= ans[i + 1][j]
            ans[i][j] *= ans[i][j - 1]
            ans[i][j] *= ans[i][j + 1]
            ans[i][j] = int(math.sqrt(ans[i][j])) + 1

for i in range(1, N + 1):
    print " ".join(map(str, ans[i][1:N+1]))