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:
tmp = 2
flag = 0
n = int(input())
mat = [[0]*n for i in range(n)]
for i in range(0,2*n,2):
  for j in range(i+1):
    try:
      if flag == 0:
        mat[i-j][j] = tmp
        flag = 1
        tmp += 1
      else:
        mat[i-j][j] = (n*n+1)//2-tmp+4
        flag = 0
    except:
      pass
for i in range(n):
  for j in range(n):
    if mat[i][j]==0:
      a = 1;b=1;c=1;d=1
      if i-1 >= 0:
        a = mat[i-1][j]
      if i+1<=n-1:
        b = mat[i+1][j]
      if j-1 >= 0:
        c = mat[i][j-1]
      if j+1<= n-1:
        d = mat[i][j+1]
      mat[i][j] = a*b*c*d +1
for i in mat:
  print(*i)

Submission status: Wrong Answer

Solution code:
tmp = 1
flag = 0
n = int(input())
mat = [[0]*n for i in range(n)]
for i in range(0,2*n,2):
  for j in range(i+1):
    try:
      if flag == 0:
        mat[i-j][j] = tmp
        flag = 1
        tmp += 1
      else:
        mat[i-j][j] = (n*n+1)//2-tmp+2
        flag = 0
    except:
      pass
for i in range(n):
  for j in range(n):
    if mat[i][j]==0:
      a = 1;b=1;c=1;d=1
      if i-1 >= 0:
        a = mat[i-1][j]
      if i+1<=n-1:
        b = mat[i+1][j]
      if j-1 >= 0:
        c = mat[i][j-1]
      if j+1<= n-1:
        d = mat[i][j+1]
      mat[i][j] = a*b*c*d +1
for i in mat:
  print(*i)

Submission status: Wrong Answer

Solution code:
n = int(input())
MAX = 10**6
mat = [[0]*n for i in range(n)]
mat[0][0] = MAX
for i in range(1,2*n):
  MAX-=2
  for j in range(i+1):
    try:
      mat[i-j][j] = MAX
    except:
      pass
for i in mat:
  print(*i)

Submission status: Wrong Answer

Solution code:
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()
    ps = [next(eg) for _ in range(2 * n)]
    m = (n - 1) // 2

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

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

    return ans


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

Submission status: Wrong Answer

Solution code:
from fractions import gcd


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()
    ps = [next(eg) for _ in range(n)]
    m = (n - 1) // 2

    for i in range(n):
        for j in range(i % 2, n, 2):
            l1 = ps[(i + j) // 2]
            l2 = ps[(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])
            lcd = surroundings[0]
            for s in surroundings[1:]:
                lcd = lcd * s // gcd(lcd, s)
            ans[i][j] = lcd + 1

    return ans


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

Submission status: Wrong Answer

Solution code:
from fractions import gcd


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]]

    m = n // 2
    ans = [[0] * n for _ in range(n)]
    first = []

    for i in range(n):
        mi = abs(i - m)
        itr = range(0, n, 2) if i % 2 == 0 else range(1, n, 2)
        first.extend((mi + abs(j - m), i, j) for j in itr)
    first.sort()

    eg = eratosthenes_generator()

    for _, i, j in first:
        ans[i][j] = next(eg)

    for i in range(n):
        for j in range(int(i % 2 == 0), 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])
            lcd = surroundings[0]
            for s in surroundings[1:]:
                lcd = lcd * s // gcd(lcd, s)
            ans[i][j] = lcd + 1
           
    return ans


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

Submission status: Wrong Answer

Solution code:
from fractions import gcd


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


n = int(input())
m = n // 2
ans = [[0] * n for _ in range(n)]
first = []

for i in range(n):
    mi = abs(i - m)
    itr = range(0, n, 2) if i % 2 == 0 else range(1, n, 2)
    first.extend((mi + abs(j - m), i, j) for j in itr)
first.sort()

eg = eratosthenes_generator()

for _, i, j in first:
    ans[i][j] = next(eg)

for i in range(n):
    for j in range(int(i % 2 == 0), 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])
        lcd = surroundings[0]
        for s in surroundings[1:]:
            lcd = lcd * s // gcd(lcd, s)
        ans[i][j] = lcd + 1

print('\n'.join(' '.join(map(str, row[:-1])) for row in ans))

Submission status: Wrong Answer

Solution code:
n = int(input())
m = 0
k = 1
for i in range(n):
  m+=3
  k = 1
  for j in range(n):
    k+=3
    print(m+k, end=' ')
  print()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
m=int(input())

n=502
a=[[1]*(n+2) for _ in range(n+2)]

c=0

p=2
p2=4
q=26100
q2=24998
for i in range(1,n+1):
    for j in range(1,n+1):
        if (i+j)%2==0:
            if i%3==0:
                a[i][j] = p
                p+=2
#            elif i%2==0 and i%4!=0:
#                a[i][j] = p2
#                p2+=4
            else:
                a[i][j] = q
                q-=2
#            elif (i+1)%2==0 and (i+1)%4!=0:
#                a[i][j] = q2
#                q2-=4
        
    
#print(a)

for i in range(1,n+1):
    for j in range(1,n+1):
        if (i+j)%2==1:
            a[i][j] = a[i-1][j]*a[i+1][j]*a[i][j-1]*a[i][j+1]//16+1
            if i==1 or i==n or j==1 or j==n:
                a[i][j] = a[i-1][j]*a[i+1][j]*a[i][j-1]*a[i][j+1]//4 +1


for i in range(1,m+1):          
    print(*a[i][1:m+1])
    

#for i in range(1,m+1):
#    print(i)
#    for j in range(1,m+1):
#        print(max(a[i][j],a[i][j+1])%min(a[i][j],a[i][j+1]),max(a[i][j],a[i+1][j])%min(a[i][j],a[i+1][j]),max(a[i][j],a[i][j+1]),min(a[i][j],a[i][j+1]),max(a[i][j],a[i+1][j]),min(a[i][j],a[i+1][j]))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
m=int(input())

n=502
a=[[1]*(n+2) for _ in range(n+2)]

c=0

p=2
p2=4
q=26100
q2=24998
for i in range(1,n+1):
    for j in range(1,n+1):
        if (i+j)%2==0:
            if i%2==0:
                a[i][j] = p
                p+=2
#            elif i%2==0 and i%4!=0:
#                a[i][j] = p2
#                p2+=4
            elif (i+1)%2==0:
                a[i][j] = q
                q-=2
#            elif (i+1)%2==0 and (i+1)%4!=0:
#                a[i][j] = q2
#                q2-=4
        
    
#print(a)

for i in range(1,n+1):
    for j in range(1,n+1):
        if (i+j)%2==1:
            a[i][j] = a[i-1][j]*a[i+1][j]*a[i][j-1]*a[i][j+1]//16+1
            if i==1 or i==n or j==1 or j==n:
                a[i][j] = a[i-1][j]*a[i+1][j]*a[i][j-1]*a[i][j+1]//4 +1


for i in range(1,m+1):          
    print(*a[i][1:m+1])
    

#for i in range(1,m+1):
#    print(i)
#    for j in range(1,m+1):
#        print(max(a[i][j],a[i][j+1])%min(a[i][j],a[i][j+1]),max(a[i][j],a[i+1][j])%min(a[i][j],a[i+1][j]),max(a[i][j],a[i][j+1]),min(a[i][j],a[i][j+1]),max(a[i][j],a[i+1][j]),min(a[i][j],a[i+1][j]))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
m=int(input())

n=502
a=[[1]*(n+2) for _ in range(n+2)]

c=0

p=2
p2=4
q=25000
q2=24998
for i in range(1,n+1):
    for j in range(1,n+1):
        if (i+j)%2==0:
            if i%2==0:
                a[i][j] = p
                p+=2
#            elif i%2==0 and i%4!=0:
#                a[i][j] = p2
#                p2+=4
            elif (i+1)%2==0:
                a[i][j] = q
                q-=2
#            elif (i+1)%2==0 and (i+1)%4!=0:
#                a[i][j] = q2
#                q2-=4
        
    
#print(a)

for i in range(1,n+1):
    for j in range(1,n+1):
        if (i+j)%2==1:
            a[i][j] = a[i-1][j]*a[i+1][j]*a[i][j-1]*a[i][j+1]//16+1
            if i==1 or i==n or j==1 or j==n:
                a[i][j] = a[i-1][j]*a[i+1][j]*a[i][j-1]*a[i][j+1] +1


for i in range(1,m+1):          
    print(*a[i][1:m+1])
    

#for i in range(1,m+1):
#    print(i)
#    for j in range(1,m+1):
#        print(max(a[i][j],a[i][j+1])%min(a[i][j],a[i][j+1]),max(a[i][j],a[i+1][j])%min(a[i][j],a[i+1][j]),max(a[i][j],a[i][j+1]),min(a[i][j],a[i][j+1]),max(a[i][j],a[i+1][j]),min(a[i][j],a[i+1][j]))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
m=int(input())

n=500
a=[[1]*(n+2) for _ in range(n+2)]

c=0

p=2
p2=4
q=25000
q2=24998
for i in range(1,n+1):
    for j in range(1,n+1):
        if (i+j)%2==0:
            if i%2==0 and i%4==0:
                a[i][j] = p
                p+=4
            elif i%2==0 and i%4!=0:
                a[i][j] = p2
                p2+=4
            elif (i+1)%2==0 and (i+1)%4==0:
                a[i][j] = q
                q-=4
            elif (i+1)%2==0 and (i+1)%4!=0:
                a[i][j] = q2
                q2-=4
        
    
#print(a)

for i in range(1,n+1):
    for j in range(1,n+1):
        if (i+j)%2==1:
            a[i][j] = a[i-1][j]*a[i+1][j]*a[i][j-1]*a[i][j+1]//32 +1
            if i==1 or i==n or j==1 or j==n:
                a[i][j] = a[i-1][j]*a[i+1][j]*a[i][j-1]*a[i][j+1] +1


for i in range(1,m+1):          
    print(*a[i][1:m+1])
    

#for i in range(1,m+1):
 #   print(i)
  #  for j in range(1,m+1):
   #     print(max(a[i][j],a[i][j+1])%min(a[i][j],a[i][j+1]),max(a[i][j],a[i+1][j])%min(a[i][j],a[i+1][j]))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
m=int(input())

n=502
a=[[1]*(n+2) for _ in range(n+2)]

c=0

p=2
q=27004
for i in range(1,n+1):
    for j in range(1,n+1):
        if (i+j)%2==0:
            if i%2==0:
                a[i][j] = p
                p+=2
            else:
                a[i][j] = q
                q-=2
        
    
#print(a)

for i in range(1,n+1):
    for j in range(1,n+1):
        if (i+j)%2==1:
            a[i][j] = a[i-1][j]*a[i+1][j]*a[i][j-1]*a[i][j+1]//16 +1
            if i==1 or i==n or j==1 or j==n:
                a[i][j] = a[i-1][j]*a[i+1][j]*a[i][j-1]*a[i][j+1] +1


for i in range(1,m+1):          
    print(*a[i][1:m+1])
    

#for i in range(1,m+1):
 #   print(i)
  #  for j in range(1,m+1):
   #     print(max(a[i][j],a[i][j+1])%min(a[i][j],a[i][j+1]),max(a[i][j],a[i+1][j])%min(a[i][j],a[i+1][j]))

Submission status: Wrong Answer

Solution code:
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 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])
s=2
for i in range(0,n*n):
	if (i//n+i%n)%2==0:
		a[i]=s
		x.add(s)
		s+=1
#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)

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n=int(input())

a=[[1]*(n+2) for _ in range(n+2)]

c=0

p=2
q=25000
for i in range(1,n+1):
    for j in range(1,n+1):
        if (i+j)%2==0:
            if i%2!=0:
                a[i][j] = p
                p+=2
            else:
                a[i][j] = q
                q-=2
        
    
#print(a)

for i in range(1,n+1):
    for j in range(1,n+1):
        if (i+j)%2==1:
            a[i][j] = a[i-1][j]*a[i+1][j]*a[i][j-1]*a[i][j+1]//16 +1
            if i==1 or i==n or j==1 or j==n:
                a[i][j] = a[i-1][j]*a[i+1][j]*a[i][j-1]*a[i][j+1] +1


for i in range(1,n+1):          
    print(*a[i][1:n+1])

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n=int(input())

a=[[1]*(n+2) for _ in range(n+2)]

c=0

p=2
q=25000
for i in range(1,n+1):
    for j in range(1,n+1):
        if (i+j)%2==0:
            if i%2!=0:
                a[i][j] = p
                p+=2
            else:
                a[i][j] = q
                q-=2
        
    
#print(a)

for i in range(1,n+1):
    for j in range(1,n+1):
        if (i+j)%2==1:
            a[i][j] = a[i-1][j]*a[i+1][j]*a[i][j-1]*a[i][j+1]//16 +1
            if i==1 or i==n or j==1 or j==n:
                a[i][j] = a[i-1][j]*a[i+1][j]*a[i][j-1]*a[i][j+1]//4 +1
                

for i in range(1,n+1):          
    print(*a[i][1:n+1])

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n=int(input())

a=[[1]*(n+2) for _ in range(n+2)]

c=0

p=2
q=25000
for i in range(1,n+1):
    for j in range(1,n+1):
        if (i+j)%2==0:
            if i%2!=0:
                a[i][j] = p
                p+=2
            else:
                a[i][j] = q
                q-=2
        
    
#print(a)

for i in range(1,n+1):
    for j in range(1,n+1):
        if (i+j)%2==1:
            a[i][j] = a[i-1][j]*a[i+1][j]*a[i][j-1]*a[i][j+1]//32 +1
            if i==1 or i==n or j==1 or j==n:
                a[i][j] = a[i-1][j]*a[i+1][j]*a[i][j-1]*a[i][j+1]//8 +1
                

for i in range(1,n+1):          
    print(*a[i][1:n+1])

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=100000
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

powers=[]
for p in prime:
    q=p
    while q<5600:
        if q>500: powers.append(q)
        q*=p

# 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=[[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)
            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: Wrong Answer

Solution code:
import math

def primes(x):
    if x < 2: return []

    primes = [i for i in range(x)]
    primes[1] = 0 # 1は素数ではない

    for prime in primes:
        if prime > math.sqrt(x): break
        if prime == 0: continue
        for non_prime in range(2 * prime, x, prime): primes[non_prime] = 0

    return [prime for prime in primes if prime != 0]


def is_prime(x):
    if x < 2: return False # 2未満に素数はない
    if x == 2 or x == 3 or x == 5: return True # 2,3,5は素数
    if x % 2 == 0 or x % 3 == 0 or x % 5 == 0: return False # 2,3,5の倍数は合成数

    prime = 7
    step = 4
    while prime <= math.sqrt(x):
        if x % prime == 0: return False

        prime += step
        step = 6 - step

    return True

p = primes(10**5)

N = int(input())

for j in range(N):
    ans = ""
    for i in range(N):
        ans += "2" if (i+j)%2==0 else "3"
        if i != N-1:
            ans += " "
    print(ans)

Submission status: Wrong Answer

Solution code:
import math

def primes(x):
    if x < 2: return []

    primes = [i for i in range(x)]
    primes[1] = 0 # 1は素数ではない

    for prime in primes:
        if prime > math.sqrt(x): break
        if prime == 0: continue
        for non_prime in range(2 * prime, x, prime): primes[non_prime] = 0

    return [prime for prime in primes if prime != 0]


def is_prime(x):
    if x < 2: return False # 2未満に素数はない
    if x == 2 or x == 3 or x == 5: return True # 2,3,5は素数
    if x % 2 == 0 or x % 3 == 0 or x % 5 == 0: return False # 2,3,5の倍数は合成数

    prime = 7
    step = 4
    while prime <= math.sqrt(x):
        if x % prime == 0: return False

        prime += step
        step = 6 - step

    return True

p = primes(10**5)

N = int(input())

for j in range(N):
    ans = ""
    for i in range(N):
        ans += str(p[i+j])
        if i != N-1:
            ans += " "
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v


# Width First Search (+ Distance)
def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk


# Depth First Search (+Distance)
def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result


# --------------------------------------------

dp = None

def bisearch(L, target):
    low = 0
    high = len(L) - 1

    if len(L) == 0:
        return False
    
    while low <= high:
        mid = (low + high) // 2
        guess = L[mid]
        if guess == target:
            return True
        elif guess < target:
            low = mid + 1
        elif guess > target:
            high = mid - 1
    if guess != target:
        return False

def main():
    MAX_V = 10**150
    N = int(input())

    L = [2, 3, 4, 5]
    A = [[0] * N for _ in range(N+1)]

    A[0][0] = 2
    A[1][0] = 5
    A[0][1] = 3
    A[1][1] = 4


    for t in range(2, N):
        for i in range(t):
            if i == 0:
                f = True
                for x in range(A[i][t - 1] + 1, 0, -A[i][t - 1]):
                    if not bisearch(L, x):
                        L.append(x)
                        L.sort()
                        A[i][t] = x
                        f = False
                        break
                if f:
                    for x in range(A[i][t - 1] + 1, MAX_V, A[i][t - 1]):
                        if not bisearch(L, x):
                            L.append(x)
                            L.sort()
                            A[i][t] = x
                            break
            else:
                _lcm = lcm(A[i - 1][t], A[i][t - 1])
                f = True
                for x in range(_lcm + 1, 0, -_lcm):
                    if not bisearch(L, x):
                        L.append(x)
                        L.sort()
                        A[i][t] = x
                        f = False
                        break
                if f:
                    for x in range(_lcm + 1, MAX_V, _lcm):
                        if not bisearch(L, x):
                            L.append(x)
                            L.sort()
                            A[i][t] = x
                            break
        
        for j in range(t):
            if j == 0:
                f = True
                for x in range(A[t - 1][j] + 1, 0, -A[t - 1][j]):
                    if not bisearch(L, x):
                        L.append(x)
                        L.sort()
                        A[t][j] = x
                        f = False
                        break
                if f:
                    for x in range(A[t - 1][j] + 1, MAX_V, A[t - 1][j]):
                        if not bisearch(L, x):
                            L.append(x)
                            L.sort()
                            A[t][j] = x
                            break
            else:
                _lcm = lcm(A[t - 1][j], A[t][j - 1])
                f = True
                for x in range(_lcm + 1, 0, -_lcm):
                    
                    if not bisearch(L, x):
                        L.append(x)
                        L.sort()
                        A[t][j] = x
                        f = False
                        break
                if f:
                    for x in range(_lcm + 1, MAX_V, _lcm):
                        if not bisearch(L, x):
                            L.append(x)
                            L.sort()
                            A[t][j] = x
                            break
        
        print(L)
       

        _lcm = lcm(A[t - 1][t], A[t][t - 1])
        f = True
        for x in range(_lcm + 1, 0, -_lcm):
            if not bisearch(L, x):
                L.append(x)
                L.sort()
                A[t][t] = x
                break
        if f:
            for x in range(_lcm + 1, MAX_V, _lcm):
                if not bisearch(L, x):
                    L.append(x)
                    L.sort()
                    A[t][t] = x
                    break
    
    for i in range(N):
        print(" ".join(list(map(str, A[i]))))


main()

Submission status: Wrong Answer

Solution code:
import numpy as np
from fractions import gcd

def lcm(x, y):
    return (x * y) // gcd(x, y)

N = int(raw_input().replace("\n", ""))

m = 2

while m:
    start_value = m + 1
    try:
        mat_N_i = np.asarray([start_value]).reshape(1, 1)

        def get_value(in_mat, y, x):
            if x == 0:
                p = 1
                while True:
                    tmp = in_mat[y-1, 0] * p
                    p += 1
                    if tmp < m:
                        continue
                    out_value = tmp + m
                    if out_value not in in_mat:
                        break
            elif y == 0:
                p = 1
                while True:
                    tmp = in_mat[0, x-1] * p
                    p += 1
                    if tmp < m:
                        continue
                    out_value = tmp + m
                    if out_value not in in_mat:
                        break
            else:
                m11 = in_mat[y-1, x-1]
                m12 = in_mat[y-1, x]
                m21 = in_mat[y, x-1]
                p = 1
                while True:
                    lcm_ = lcm(m12, m21)
                    p += 1
                    if lcm_ < m:
                        continue
                    m22 = lcm_ * p + m
                    if m22 not in in_mat:
                        break

                out_value = m22
            return out_value

        def make_mat(in_mat, N_i):
            out_mat = np.zeros((N_i + 1, N_i + 1)).astype(int)
            out_mat[:N_i, :N_i] = in_mat
            for k in xrange(N_i): # k = 0 to N_i - 1
                out_mat[k, N_i] = get_value(out_mat, k, N_i)
                out_mat[N_i, k] = get_value(out_mat, N_i, k)
            out_mat[N_i, N_i] = get_value(out_mat, N_i, N_i)
            return out_mat

        for N_i in xrange(1, N):
            mat_N_i = make_mat(mat_N_i, N_i)

        break
    except:
        m += 1

mat = mat_N_i.tolist()
for line in mat:
    print " ".join(map(str, line))

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from random import shuffle, seed
def main():
    N = int(raw_input())
    n = 500
    a = [[0] * n for i in xrange(n)]
    b = range(2, n * n / 2 + 2)
    seed(42)
    shuffle(b)
    c = 0
    for i in xrange(n):
        for j in xrange(n):
            if (i + j) % 2:
                a[i][j] = b[c]
                c += 1
    L = b[-1]
    s = set(b)
    for i in xrange(n):
        for j in xrange(n):
            if a[i][j]:
                continue
            g = 0
            if i:
                g = gcd(a[i-1][j], g)
            if j:
                g = gcd(a[i][j-1], g)
            if i < n - 1:
                g = gcd(a[i+1][j], g)
            if j < n - 1:
                g = gcd(a[i][j+1], g)
            x = L / g * g + g + 1
            while x in s:
                x += g
            s.add(x)
            a[i][j] = L = x
    for i in xrange(N):
        for j in xrange(N):
            print a[i][j],
        print
main()

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:
n = int(input())
v1 = [0 for i in range(n+100)]
for i in range(n):
    if i % 2 == 1:
        v1[i] = 4
    if i % 2 == 0:
        v1[i] = 7
    

v2 = [0 for i in range(n)]
for i in range(n):
    if i % 2 == 1:
        v2[i] = 23
    if i % 2 == 0:
        v2[i] = 10

for i in range(n):
    if i % 2 == 1:
        for i in range(n-1):
            print(v1[i], end=" ")
        print(v1[n-1])
    if i % 2 == 0:
        for i in range(n-1):
            print(v2[i], end=" ")
        print(v2[n-1])