Problem:


Score : 300 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on the blackboard.

You will choose one of them and replace it with an integer of your choice between 1 and 10^9 (inclusive), possibly the same as the integer originally written.

Find the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Output

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Sample Input 1

3
7 6 8

Sample Output 1

2

If we replace 7 with 4, the greatest common divisor of the three integers on the blackboard will be 2, which is the maximum possible value.

Sample Input 2

3
12 15 18

Sample Output 2

6

Sample Input 3

2
1000000000 1000000000

Sample Output 3

1000000000

We can replace an integer with itself.



Sample input:
3
7 6 8


Sample output:
2

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())
A = map(int, input().split())

def factorize(x, divisors=itertools.chain([2], range(3, 10 ** 9, 2))):
    """xの素因数分解を表す辞書 {素因数: 指数}"""
    f = {}
    for d in divisors:
        c = 0
        while x % d == 0:
            c += 1
            x //= d
        if c > 0:
            f[d] = c
        if d * d > x > d:
            f[x] = 1
            break
    return f

def dropmin(ls):
    """[min(ls[:i] + ls[i + 1:]) for i in range(len(ls))]"""
    # l = [min(ls[:i]) for i in range(len(ls))]
    l = [float('inf')] * N
    for i in range(1, N):
        l[i] = min(l[i - 1], ls[i - 1])
    # r = [min(ls[i + 1:]) for i in range(len(ls))]
    r = [float('inf')] * N
    for i in range(N - 2, -1, -1):
        r[i] = min(r[i + 1], ls[i + 1])
    return [min(a, b) for a, b in zip(l, r)]

def prod(ls):
    p = 1
    for x in ls:
        p *= x
    return p

# 各A_iの素因数
A = list(A)
F = [factorize(a) for a in A[:2]]

# 素因数の集合 (2要素に出現しない素因数は無視)
D =  set(F[0].keys())
D.update(F[1].keys())

E = set()
for d in D:
    c = 0
    for a in A:
        if a % d != 0:
            c += 1
            if c >= 2:
                E.add(d)
                break
D -= E

# 各A_iの素因数
F = [factorize(a, divisors=D) for a in A]

# Dの要素 d -> i -> dがA_iに含まれる個数
X = {d: [f[d] if d in f else 0 for f in F] for d in D}

# Dの要素 d → A_iを削除したときのdの個数の最小値のリスト
X = {d: dropmin(X[d]) for d in D}

S = [prod(d ** X[d][i] for d in D) for i in range(N)]
print(max(S))

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())
A = map(int, input().split())

def factorize(x, divisors=itertools.chain([2], range(3, 10 ** 9, 2))):
    """xの素因数分解を表す辞書 {素因数: 指数}"""
    f = {}
    for d in divisors:
        c = 0
        while x % d == 0:
            c += 1
            x //= d
        f[d] = c
        if d * d > x > d:
            f[x] = 1
            break
    return f

def dropmin(ls):
    """[min(ls[:i] + ls[i + 1:]) for i in range(len(ls))]"""
    # l = [min(ls[:i]) for i in range(len(ls))]
    l = [float('inf')] * N
    for i in range(1, N):
        l[i] = min(l[i - 1], ls[i - 1])
    # r = [min(ls[i + 1:]) for i in range(len(ls))]
    r = [float('inf')] * N
    for i in range(N - 2, -1, -1):
        r[i] = min(r[i + 1], ls[i + 1])
    return [min(a, b) for a, b in zip(l, r)]

def prod(ls):
    p = 1
    for x in ls:
        p *= x
    return p

# 各A_iの素因数
A = list(A)
F = [factorize(a) for a in A[:2]]

# 素因数の集合 (2要素に出現しない素因数は無視)
D =  set(F[0].keys())
D.update(F[1].keys())

E = set()
for d in D:
    c = 0
    for a in A:
        if a % d != 0:
            c += 1
            if c >= 2:
                E.add(d)
                break
D -= E

# 各A_iの素因数
F = [factorize(a, divisors=D) for a in A]

# Dの要素 d -> i -> dがA_iに含まれる個数
X = {d: [f[d] if d in f else 0 for f in F] for d in D}

# Dの要素 d → A_iを削除したときのdの個数の最小値のリスト
X = {d: dropmin(X[d]) for d in D}

S = [prod(d ** X[d][i] for d in D) for i in range(N)]
print(max(S))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))

for i in range(max(A),1,-1):
    l = list(map(lambda x: x % i,A))
    if l.count(0) >= len(A)-1:
        print(i)
        break

Submission status: Wrong Answer

Solution code:
import itertools

N = int(input())
A = map(int, input().split())

def factorize(x, divisors=itertools.chain([2], range(3, 10 ** 9, 2))):
    """xの素因数分解を表す辞書 {素因数: 指数}"""
    f = {}
    for d in divisors:
        c = 0
        while x % d == 0:
            c += 1
            x //= d
        f[d] = c
        if d * d > x > d:
            f[x] = 1
            break
    return f

def dropmin(ls):
    """[min(ls[:i] + ls[i + 1:]) for i in range(len(ls))]"""
    # l = [min(ls[:i]) for i in range(len(ls))]
    l = [float('inf')] * N
    for i in range(1, N):
        l[i] = min(l[i - 1], ls[i - 1])
    # r = [min(ls[i + 1:]) for i in range(len(ls))]
    r = [float('inf')] * N
    for i in range(N - 2, -1, -1):
        r[i] = min(r[i + 1], ls[i + 1])
    return [min(a, b) for a, b in zip(l, r)]

def prod(ls):
    p = 1
    for x in ls:
        p *= x
    return p

# 各A_iの素因数
A = list(A)
F = [factorize(a) for a in A[:2]]

# 素因数の集合 (2要素に出現しない素因数は無視)
D =  set(F[0].keys())
D.update(F[1].keys())

# 各A_iの素因数
F = [factorize(a, divisors=D) for a in A]

E = set()
for d in D:
    c = 0
    for f in F:
        if d not in f:
            c += 1
            if c >= 2:
                E.add(d)
                break
D -= E

# Dの要素 d -> i -> dがA_iに含まれる個数
X = {d: [f[d] if d in f else 0 for f in F] for d in D}

# Dの要素 d → A_iを削除したときのdの個数の最小値のリスト
X = {d: dropmin(X[d]) for d in D}

S = [prod(d ** X[d][i] for d in D if X[d][i] > 0) for i in range(N)]
print(max(S))

Submission status: Wrong Answer

Solution code:
def gcd(a,b):
    if b==0:return a
    return gcd(b,a%b)
 
N = (int(input()))
A = [int(i) for i in input().split()]

A1 = [0] * (N)
A2 = [0] * (N)
A3 = [0] * (N)
A1[0] = A[0]
A2[N-1] = A[N-1]

for i in range(N):
  if i==0 : continue
  A1[i] = gcd(A1[i-1],A[i])
  A2[N-1-i] = gcd(A2[N-i],A[N-1-i])
  
for i in range(N):
  A3[i] = max(A1[i],A2[i])

print(min(A3))

Submission status: Wrong Answer

Solution code:
import fractions
input()
a=[int(i) for i in input().split()]
print(max(fractions.gcd(a[0],a[1]),fractions.gcd(a[0],a[-1]),fractions.gcd(a[-2],a[-1])))

Submission status: Wrong Answer

Solution code:
def gcd(x, y):
    while y!=0:
        x, y = y, x % y # x,yの大小が逆でも、入れ替わるので、OK
    return x

n = int(input())
a = list(map(int, input().split()))

if n == 2:
    print(max(a))

# if n>=3:
l = [-1] * n
r = [-1] * n
l[0] = a[0]  #一番左を左に挿入
r[-1] = a[-1] #一番右を右に挿入
# print(l)
# print(r)

for i in range(1, n): #左から最大公約数をn-1回計算していく
    l[i] = gcd(l[i-1], a[i]) # l[0]とa[1]の最大公約数をl[1]に挿入⇒
    # l[1]とa[2]の最大公約数をl[2]に挿入。隣同士素があったら、それ以降右側全て1
for i in range(n-2, -1, -1): #右から最大公約数を計算していく
    r[i] = gcd(r[i+1], a[i]) # r[n-1]とa[n-2]の最大公約数をr[n-2]に挿入

# print("l",l) # lはn-1桁のlist
# print("r",r)

ans = max(r[1], l[-2]) # rの左から2番目とlの右から2番目の大きい方を、ansに挿入.
# 右端、左端は、最大公約数ではないため。(元々の数列)

for i in range(1, n-1): #両端を切り取った中で、lは右側に寄せて、rは左側に寄せているので、
    g = gcd(l[i-1], r[i+1]) #
    ans = max(ans, g)
    # print(i, g, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
def gcd(x, y):
    while y!=0:
        x, y = y, x % y # x,yの大小が逆でも、入れ替わるので、OK
    return x

n = int(input())
a = list(map(int, input().split()))

if n == 2:
    print(max(a))

# if n>=3:
l = [-1] * n
r = [-1] * n
l[0] = a[0]  #一番左を左に挿入
r[-1] = a[-1] #一番右を右に挿入
print(l)
print(r)

for i in range(1, n): #左から最大公約数をn-1回計算していく
    l[i] = gcd(l[i-1], a[i]) # l[0]とa[1]の最大公約数をl[1]に挿入⇒
    # l[1]とa[2]の最大公約数をl[2]に挿入。隣同士素があったら、それ以降右側全て1
for i in range(n-2, -1, -1): #右から最大公約数を計算していく
    r[i] = gcd(r[i+1], a[i]) # r[n-1]とa[n-2]の最大公約数をr[n-2]に挿入

print("l",l) # lはn-1桁のlist
print("r",r)

ans = max(r[1], l[-2]) # rの左から2番目とlの右から2番目の大きい方を、ansに挿入.
# 右端、左端は、最大公約数ではないため。(元々の数列)

for i in range(1, n-1): #両端を切り取った中で、lは右側に寄せて、rは左側に寄せているので、
    g = gcd(l[i-1], r[i+1]) #
    ans = max(ans, g)
    # print(i, g, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce
import copy

def gcd( x, y ):
    while y != 0:
        x, y = y, x%y
    return x

n = int(input())
a = list(map(int, input().split()))

result = 0
l = [0]*n
r = [0]*n
for i in range(n-1):
    l[i+1] = gcd(l[i],a[i])

for i in reversed(range(1, n)):
    r[i-1] = gcd(r[i], a[i])

print(l)
print(r)

ans = 0
for i in range(n):
    lr = gcd(l[i], r[i])
    print (lr)
    ans = max(ans, lr)

print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import*
n,*a=map(int,open(0).read().split())
*b,=c=[0]
for i in range(n):b+=gcd(b[i],a[i]),;c+=gcd(c[i],a[~i]),
print(max(gcd(b[i],c[i+2])for i in range(n-1)))

Submission status: Wrong Answer

Solution code:
import itertools
import fractions
from functools import reduce

n = int(input())
a = [int(s) for s in input().split(' ')]

L = [0] * n
R = [0] * n

for i in range(len(a)):
    if i == 0:
        L[i] = a[i]
        R[n - 1] = a[n - 1]
    else:
        L[i] = fractions.gcd(L[i - 1], a[i])
        R[i] = fractions.gcd(R[n - i], a[n - 1 - i])

val = 1

for i in range(len(a)):
    ret = 0
    if i == 0:
        ret = R[i + 1]
    elif i == n - 1:
        ret = L[i - 1]
    else:
        ret = fractions.gcd(L[i - 1], R[i + 1])

    if ret > val:
       val = ret
        
print(val)

Submission status: Wrong Answer

Solution code:
def gcd(x,y):
    if x<y:
        x,y=y,x
    while x%y!=0:
        x,y=y,x%y
    return(y)

n=int(input())

v=[]
line=input().split()

for i in range(n):
    v.append(int(line[i]))

gcd_l=[]
gcd_r=[]

for i in range(n):
    if i==0:
        gcd_l.append(v[i])
    else:
        gcd_l.append(gcd(gcd_l[i-1],v[i]))

for i in range(n):
    if i==0:
        gcd_r.append(v[n-1])
    else:
        gcd_r.append(gcd(gcd_r[i-1],v[n-i-1]))

gcd_list=[]

for i in range(n):
    if i==0:
        gcd_list.append(gcd_r[n-2])
    elif i==n-1:
        gcd_list.append(gcd_l[n-2])
    else:
        gcd_list.append(gcd(gcd_l[i-1],gcd_r[i-1]))

print(max(gcd_list))

Submission status: Wrong Answer

Solution code:
def gcd(x,y):
    if x<y:
        x,y=y,x
    while x%y!=0:
        x,y=y,x%y
    return(y)

n=int(input())

v=[]
line=input().split()

for i in range(n):
    v.append(int(line[i]))

gcd_l=[]
gcd_r=[]

for i in range(n):
    if i==0:
        gcd_l.append(v[i])
    else:
        gcd_l.append(gcd(gcd_l[i-1],v[i]))

for i in range(n):
    if i==0:
        gcd_r.append(v[n-1])
    else:
        gcd_r.append(gcd(gcd_r[i-1],v[n-i-1]))

print(gcd_l,gcd_r)

gcd_list=[]

for i in range(n):
    if i==0:
        gcd_list.append(gcd_r[n-2])
    elif i==n-1:
        gcd_list.append(gcd_l[n-2])
    else:
        gcd_list.append(gcd(gcd_l[i-1],gcd_r[i-1]))

print(max(gcd_list))

Submission status: Wrong Answer

Solution code:
from functools import reduce
import copy

def gcd( x, y ):
    while y != 0:
        x, y = y, x%y
    return x

n = int(input())
a = list(map(int, input().split()))

result = 0
l = [0]*n
r = [0]*n
for i in range(n-1):
    l[i+1] = gcd(l[i],a[i])

for i in reversed(range(n)):
    r[i-1] = gcd(r[i], a[i])

ans = 0
for i in range(n):
    lr = gcd(l[i], r[i])
    ans = max(ans, lr)

print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce
import copy

def gcd( x, y ):
    while y != 0:
        x, y = y, x%y
    return x

n = int(input())
a = list(map(int, input().split()))

result = 0
l = [0]*n
r = [0]*n
for i in range(n-1):
    l[i+1] = gcd(l[i],a[i])

for i in reversed(range(n)):
    r[i-1] = gcd(r[i], a[i])

print(l)
print(r)

ans = 0
for i in range(n):
    lr = gcd(l[i], r[i])
    print (lr)
    ans = max(ans, lr)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A  = [int(i) for i in input().split()]

def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)

res = [0] * N
l = [0] * (N + 1)
r = [0] * (N + 1)

for i in range(N):
    l[i+1] = gcd(l[i], A[i])

for i in range(N, 0, -1):
    r[i-1] = gcd(r[i], A[i-1])

for i in range(N):
    res[i] = gcd(l[i], r[i+1])

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))

def gcd(a, b):
  if b == 0:
    return a
  return gcd(b, a % b)

F = [0] * N
B = [0] * N
F[0] = A[0]
B[N-1] = A[N-1]

for i in range(1, N):
  F[i] = gcd(A[i], A[i-1])
for i in range(N - 2, 0, -1):
  B[i] = gcd(A[i+1], A[i])

mx = max(F[N-2], B[1])
for i in range(1, N - 1):
  mx = max(mx, gcd(F[i-1], B[i+1]))
print(mx)

Submission status: Wrong Answer

Solution code:
from sys import exit
def gcd(a,b):#greatest common divisor
    if (a<b):
        a,b = b,a
    while(b>0):
        a,b = b ,a%b
    return a
N = int(input())
A = [int(n) for n in input().split()]
straight = [0]*N
straight[0] = gcd(A[0],A[0])
for i in range(1,len(A)):
    straight[i] = gcd(straight[i-1], A[i])
# print(straight)
A = A[::-1]
back = [0]*N
back[0] = gcd(A[0],A[0])
for i in range(1,len(A)):
    back[i] = gcd(back[i-1], A[i])
# print(back[::-1])
back = back[::-1]
ans = straight[1]
for i in range(N-2):
    ans = max(ans, gcd(straight[i], back[i+2]))
    # print(straight[i],back[i+2])
    # print(gcd(straight[i], back[i+2]))
ans = max(ans, back[-2])
print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
def get_result(L, N):
    Gl = [0]*N
    Gr = [0]*N
    Gl[0] = L[0]
    Gr[N-1] = L[N-1]
    for i in range(1, N):
        Gl[i] = gcd(Gl[i-1], L[i])
    for j in range(N-2, -1, -1):
        Gr[j] = gcd(Gr[j+1], L[j])
    result = 1
    #print(Gl, Gr)
    full_gcd = Gr[0]
    for i in range(N):
        if i == 0:
            x = Gr[1]
        elif i == N-1:
            x = Gl[N-2]
        else:
            x = gcd(Gl[i-1], Gr[i+1])
        if x > full_gcd:
            result = max(result, x)
    return result



N = int(input())
L = [int(s) for s in input().split(" ")]
result = get_result(L, N)
print(result )

Submission status: Wrong Answer

Solution code:
def F(a,b):
  if b > a:
    c = a
    a = b
    b = c
  
  if b == 0:
    return a
  return F(b, a % b)
 
N=int(input())
A=input().split()
Max=max(A)
Min=min(A)
flg2=False
 
if F(int(Max),int(Min)) == 1:
  flg1=False
else:
  flg1=True
 
if N == 2:
  print(int(Max))
  quit()
 
for i in range(N):
  if flg1 == False:
    if int(A[i]) == int(Min) and flg2==False:
      A[i] = Max
      flg2=True
  else:
    if int(A[i]) != int(Min) and int(A[i]) != int(Max) and flg2==False:
      A[i] = Max
      flg2=True
    
  if i == 0: continue
  if i == 1:
    ans=F(int(A[i-1]),int(A[i]))
  else:
    ans=F(ans,int(A[i]))
print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce
import copy

def gcd( x, y ):
    while y != 0:
        x, y = y, x%y
    return x

n = int(input())
a = list(map(int, input().split()))

result = 0
l = []
r = []
for i in range(n):
    if i == 0:
        l.append(a[i])
    else:
        l.append(gcd(a[i],a[i-1])) 

for i in range(n):
    if i == 0:
        r.insert(0, a[n-1])
    else:
        r.insert(0,gcd(a[i],a[i-1]))

ans = 0
for i in range(n):
    ans = max(ans, gcd(l[i], r[n-1-i]))

print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
 
 
def main():
    n = int(input())
    a = list(map(int, input().split()))
    left = [0 for _ in range(n)]
    right = [0 for _ in range(n)]
    for i in range(1, n):
        l = gcd(a[i - 1], a[i])
        r = gcd(a[n - i - 1], a[n - i - 2])
        left[i] = l
        right[n - i - 2] = r
 
    m = max(left[0], right[n - 1])
    for i in range(n - 1):
        m = max(m, left[i - 1], right[i + 1])
    print(m)
 
 
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from fractions import*
N,*A=map(int,open(0).read().split())
w,B=0,[0]
a=range(N)
for x in a[:]:B+=[gcd(B[-1],A[x])]
w=M=0
for x in a[::-1]:M,w=max(M,gcd(w,B.pop())),gcd(w,A[x])
print(M)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

def koyakusu(a, b):
    i = 2
    j = a
    if a > b:
        j = b
    y = 1
    while i <= j:
        if a%i == 0 and b%i == 0:
            y = y*i
            a = a/i
            b = b/i
            j = a
            if a > b:
                j = b
            continue
        i = i + 1
    return y

N = int(input())
A = list(map(int, input().split()))

max = 1

for i in range(N):
    j = i+1
    while j < N:
        r = koyakusu(A[i], A[j])
        print("i:{}, j:{}, r:{}".format(i, j, r))
        if max < r:
            max = r
        j = j + 1

print(max)

Submission status: Wrong Answer

Solution code:
N = int(input())
# スペース区切りの整数の入力
A = list(map(int, input().split()))
C=A[:]
C=list(set(C))
C.sort()

#print(A)
#print(C)
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

anslist=[]

if len(C)==1:
  ans=C[0]

else:
  for i in range(2):
    B=A[:]
    B.remove(C[i])
    #print(B)
    sol=0
    for b in B:
      sol=gcd(sol,b)
    anslist.append(sol)
    ans=max(anslist)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
# スペース区切りの整数の入力
A = list(map(int, input().split()))
C=A[:]
C=list(set(C))
C.sort()

#print(A)
#print(C)
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

anslist=[]

if len(C)==1:
  ans=C[0]

elif len(C)==2:
  for i in range(2):
    B=A[:]
    B.remove(C[i])
    #print(B)
    sol=0
    for b in B:
      sol=gcd(sol,b)
    anslist.append(sol)
    ans=max(anslist)

else:
  for i in range(3):
    B=A[:]
    B.remove(C[i])
    sol=0
    for b in B:
        sol=gcd(sol,b)
    anslist.append(sol)
    ans=max(anslist)

#print(anslist)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
# スペース区切りの整数の入力
A = list(map(int, input().split()))
C=A[:]
C=list(set(C))
C.sort()

#print(A)
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

anslist=[]

if len(C)==1:
  ans=C[0]

elif len(C)==2:
  for i in range(2):
    B=A[:]
    B.remove(C[i])
    print(B)
    sol=0
    for b in B:
      sol=gcd(sol,b)
    anslist.append(sol)
    ans=max(anslist)

else:
  for i in range(3):
    B=A[:]
    B.remove(C[i])
    sol=0
    for b in B:
        sol=gcd(sol,b)
    anslist.append(sol)
    ans=max(anslist)

#print(anslist)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
# スペース区切りの整数の入力
A = list(map(int, input().split()))
C=A[:]
C=list(set(C))

#print(A)
#print(C)
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

anslist=[]

if len(C)==1:
  ans=C[0]

elif len(C)==2:
    for i in range(2):
      B=A[:]
      B.remove(C[i])
      sol=0
      for b in B:
        sol=gcd(sol,b)
      anslist.append(sol)
      ans=max(anslist)

else:
  for i in range(3):
    B=A[:]
    B.remove(C[i])
    sol=0
    for b in B:
        sol=gcd(sol,b)
    anslist.append(sol)
    ans=max(anslist)

#print(anslist)
print(ans)

Submission status: Wrong Answer

Solution code:
x = int(input())
y = input().split(" ")
y = list(map(int, y))
y.sort()
i = y[x - 2]
result = 1


while i > 1:
  count = 0
  for n in y:
    if n % i != 0:
      count += 1
    
    if count >= 2:
      continue
      
  if count <= 1:
    result = i
    break
  i -= 1

print(result)

Submission status: Wrong Answer

Solution code:
N = int(input())
# スペース区切りの整数の入力
A = list(map(int, input().split()))


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

ans=[]

if N==2:
  sol=max(A[0],A[1])

else:
  for i in range(3):
    B=A[:]
    del B[i]
    sol=0
    for b in B:
        sol=gcd(sol,b)
    ans.append(sol)
    sol=max(ans)


print(sol)

Submission status: Wrong Answer

Solution code:
N = int(input())
# スペース区切りの整数の入力
A = list(map(int, input().split()))


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

ans=[]

if N==2:
  sol=max(A[0],A[1])

else:
  for i in range(3):
    B=A[:]
    del B[i]
    sol=0
    for b in B:
        sol=gcd(sol,b)
    ans.append(sol)
    sol=ans


print(sol)

Submission status: Wrong Answer

Solution code:
import copy
import math
N=int(input())
a = list(map(int,input().split()))

# N=3
# a = [7,6,8]

# N = 40
# a = np.array([4,8,6,12]*(N//4), dtype=np.int)

# N=10
# a = [56,58,82,46,67,36,98,68,24,46]

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

# 一番近い大きい側の2の累乗の数が，segtreeの葉の数になる．
N_ADJUST = 2**int(math.ceil(math.log2(N)))
# segtreeのサイズはN_ADJUSTを葉とする木のノード数．
ST_SIZE = N_ADJUST*2-1
# binopに合わせて単位元として決める．maxならINT_MAX(問題文の制限)
INT_IDENTITY = 0
li = [INT_IDENTITY]*ST_SIZE
# li_initは，葉の配列．
# 節点kが [l,r) にbinopという演算で結びついている．
def init_segtree(li, k, l, r, binop, li_init):
    if r-l==1:
        li[k] = INT_IDENTITY if l >= len(li_init) else li_init[l]
    elif r-l>1:
        k_l, k_r = k*2+1, k*2+2
        init_segtree(li, k_l, l, (l+r)//2, binop, li_init)
        init_segtree(li, k_r, (l+r)//2, r, binop, li_init)
        li[k] = binop(li[k_l],li[k_r])

init_segtree(li,0,0,N_ADJUST,gcd,a)

# print(li)

# 下からupdateする．
def update_segtree(li,k,binop):
    if k > 0:
        k_l, k_r = ((k-1)//2)*2+1, ((k-1)//2)*2+2
        li[(k-1)//2] = binop(li[k_l],li[k_r])
        update_segtree(li,k//2,binop)

val = []
for i in range(N_ADJUST-1,N_ADJUST+N-1):
    tmp = li[i]
    li[i] = 0
    update_segtree(li,i,gcd)
    # print(i, li)
    val.append(li[0])
    li[i] = tmp
    update_segtree(li,i,gcd)

print(max(val))

Submission status: Wrong Answer

Solution code:
import copy
import math
N=int(input())
a = list(map(int,input().split()))

# N=3
# a = [7,6,8]

# N = 40
# a = np.array([4,8,6,12]*(N//4), dtype=np.int)

# N=10
# a = [56,58,82,46,67,36,98,68,24,46]

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

# 一番近い大きい側の2の累乗の数が，segtreeの葉の数になる．
N_ADJUST = 2**int(math.ceil(math.log2(N)))
# segtreeのサイズはN_ADJUSTを葉とする木のノード数．
ST_SIZE = N_ADJUST*2-1
# binopに合わせて単位元として決める．maxならINT_MAX(問題文の制限)
INT_IDENTITY = 0
li = [INT_IDENTITY]*ST_SIZE
# li_initは，葉の配列．
# 節点kが [l,r) にbinopという演算で結びついている．
def init_segtree(li, k, l, r, binop, li_init):
    if r-l==1:
        li[k] = INT_IDENTITY if l >= len(li_init) else li_init[l]
    elif r-l>1:
        k_l, k_r = k*2+1, k*2+2
        init_segtree(li, k_l, l, (l+r)//2, binop, li_init)
        init_segtree(li, k_r, (l+r)//2, r, binop, li_init)
        li[k] = binop(li[k_l],li[k_r])

init_segtree(li,0,0,N_ADJUST,gcd,a)

# print(li)

# 下からupdateする．
def update_segtree(li,k,binop):
    if k > 0:
        k_l, k_r = ((k-1)//2)*2+1, ((k-1)//2)*2+2
        li[(k-1)//2] = binop(li[k_l],li[k_r])
        update_segtree(li,k//2,binop)

val = []
l2 = copy.deepcopy(li)
for i in range(N_ADJUST-1,N_ADJUST+N-1):
    l2[i] = 0
    update_segtree(l2,i,gcd)
    # print(i, l2)
    val.append(l2[0])
    l2[i] = li[i]
    update_segtree(l2,i,gcd)

print(max(val))

Submission status: Wrong Answer

Solution code:
import copy
import math
# N=int(input())
# a = list(map(int,input().split()))

# N=3
# a = [7,6,8]

# N = 40
# a = np.array([4,8,6,12]*(N//4), dtype=np.int)

N=10
a = [56,58,82,46,67,36,98,68,24,46]

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

# 一番近い大きい側の2の累乗の数が，segtreeの葉の数になる．
N_ADJUST = 2**int(math.ceil(math.log2(N)))
# segtreeのサイズはN_ADJUSTを葉とする木のノード数．
ST_SIZE = N_ADJUST*2-1
# binopに合わせて単位元として決める．maxならINT_MAX(問題文の制限)
INT_IDENTITY = 0
li = [INT_IDENTITY]*ST_SIZE
# li_initは，葉の配列．
# 節点kが [l,r) にbinopという演算で結びついている．
def init_segtree(li, k, l, r, binop, li_init):
    if r-l==1:
        li[k] = INT_IDENTITY if l >= len(li_init) else li_init[l]
    elif r-l>1:
        k_l, k_r = k*2+1, k*2+2
        init_segtree(li, k_l, l, (l+r)//2, binop, li_init)
        init_segtree(li, k_r, (l+r)//2, r, binop, li_init)
        li[k] = binop(li[k_l],li[k_r])

init_segtree(li,0,0,N_ADJUST,gcd,a)

# print(li)

# 下からupdateする．
def update_segtree(li,k,binop):
    if k > 0:
        k_l, k_r = ((k-1)//2)*2+1, ((k-1)//2)*2+2
        li[(k-1)//2] = binop(li[k_l],li[k_r])
        update_segtree(li,k//2,binop)

val = []
l2 = copy.deepcopy(li)
for i in range(N_ADJUST-1,N_ADJUST+N-1):
    l2[i] = 0
    update_segtree(l2,i,gcd)
    print(i, l2)
    val.append(l2[0])
    l2[i] = li[i]
    update_segtree(l2,i,gcd)

print(max(val))

Submission status: Wrong Answer

Solution code:
import numpy as np
import copy
N=int(input())
a = list(map(int,input().split()))

# N=3
# a = [7,6,8]

# N = 40
# a = np.array([4,8,6,12]*(N//4), dtype=np.int)

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

# 一番近い大きい側の2の累乗の数が，segtreeの葉の数になる．
N_ADJUST = 2**int(np.ceil(np.log2(N)))
# segtreeのサイズはN_ADJUSTを葉とする木のノード数．
ST_SIZE = N_ADJUST*2-1
# binopに合わせて単位元として決める．maxならINT_MAX(問題文の制限)
INT_IDENTITY = 0
li = [INT_IDENTITY]*ST_SIZE
# li_initは，葉の配列．
# 節点kが [l,r) にbinopという演算で結びついている．
def init_segtree(li, k, l, r, binop, li_init):
    if r-l==1:
        li[k] = INT_IDENTITY if l >= len(li_init) else li_init[l]
    elif r-l>1:
        k_l, k_r = k*2+1, k*2+2
        init_segtree(li, k_l, l, (l+r)//2, binop, li_init)
        init_segtree(li, k_r, (l+r)//2, r, binop, li_init)
        li[k] = binop(li[k_l],li[k_r])

init_segtree(li,0,0,N_ADJUST,gcd,a)

# print(li)

# 下からupdateする．
def update_segtree(li,k,binop):
    if k > 0:
        k_l, k_r = ((k-1)//2)*2+1, ((k-1)//2)*2+2
        li[(k-1)//2] = binop(li[k_l],li[k_r])
        update_segtree(li,k//2,binop)

val = []
l2 = copy.deepcopy(li)
for i in range(N_ADJUST-1,N_ADJUST+N-1):
    l2[i] = 0
    update_segtree(l2,i,gcd)
    # print(i, l2)
    val.append(l2[0])
    l2[i] = li[i]
    update_segtree(l2,i,gcd)

print(max(val))

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from functools import reduce

N = int(input())
A = list(map(int,input().split()))
max = 0
L = []
R = []
for i in range(N):
    L = A[:i]
    R = A[i:]
    if i > 0 :
        L.pop()
    while 2>len(L) or 2>len(R):
        L.append(0)
        R.append(0)
    x = reduce(gcd,L)
    y = reduce(gcd,R)
    gcdmax = gcd(x,y)
    if max < gcdmax:
        max = gcdmax
print(max)

Submission status: Wrong Answer

Solution code:
N=int(input())
nums = [int(i) for i in input().split()]

def gcd(x, y):
    if x < y:
        x = x ^ y
        y = x ^ y
        x = x ^ y
    div = x % y
    while div != 0:
        x = y
        y = div
        div = x % y
    return y

memo = {}
memo[(0, 1)] = nums[0]
memo[(N-1, N)] = nums[N-1]

for i in range(2, N):
    memo[(0, i)] = gcd(memo[(0, i - 1)], nums[i])

for i in reversed(range(0, N-1)):
    memo[(i, N)] = gcd(memo[(i + 1, N)], nums[i])

# def solve(left, right):
#     if (left, right) in memo:
#         return memo[(left, right)]
#     if left == 0:
#         sol = gcd(nums[right - 1], solve(left, right - 1))
#         memo[(left, right)] = sol
#         return sol
#     if right == N:
#         sol = gcd(nums[left], solve(left+1, right))
#         memo[(left, right)] = sol
#         return sol

res = 1
for i in range(1, N-1):
    res = max(res, gcd(memo[(0, i)], memo[(i+1, N)]))

res = max(res, memo[(0, N-1)])
res = max(res, memo[(1, N)])

print(res)

Submission status: Wrong Answer

Solution code:
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)

N = int(input())
A = [int(i) for i in input().split()]

L=[0]*(N+1)
R=[0]*(N+1)
M=[0]*N
L[1]=A[0]
R[N-1]=A[N-1]

for i in range(1,N):
    j=N-i-1
    L[i]=gcd(L[i-1],A[i])
    R[j]=gcd(R[j+1],A[j])

#print("L="+str(L))
#print("R="+str(R))

for i in range(N):
    M[i]= gcd(L[i], R[i+1])

M.sort()
print(M[N-1])

Submission status: Wrong Answer

Solution code:
from fractions import*
from numpy import*

def dv(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

n,*a=map(int,open(0).read().split())
a=array(a)
l=[1]
for i in set(dv(a[0])+dv(a[1])):
    if all(a[2:]%i==0):
        l.append(i)
print(max(l))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = sorted(list(map(int, input().split())))

def gcd(a, b):
  if b > a:
    a, b = b, a  
  red = a % b
  if red == 0:
    return b
  else:
    return gcd(red, b)

print(max([gcd(A[i], A[j]) for i in range(len(A)) for j in range(i+1, len(A))]))

Submission status: Wrong Answer

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import fractions
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

n = int(input())
a = list(map(int, input().split()))

left = [0,0]
right = [0]
for i in range(n - 1):
    left.append(fractions.gcd(a[i], a[i+1]))
for i in reversed(range(n - 1)):
    right.append(fractions.gcd(a[i], a[i + 1]))
mx = 0
for i in range(n):
    l = left[i]
    r = right[i]
    mx = max(mx, fractions.gcd(l, r))
print(mx)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))

def gcd(a,b):
    if a == 0:
        return b
    if b == 0:
        return a
    r = 1
    while r > 0:
        r = a % b
        a = b
        b = r
    return a

l=[0]*(n+1)
r=[0]*(n+1)
for i in range(n):
    l[i+1]=gcd(l[i],a[i])
for i in range(n,0,-1):
    r[i-1]=gcd(r[i],a[i-1])

m=[]
for i in range(n):
    m.append(gcd(l[i],r[i+1]))
print(l,r)
print(m)
print(max(m))

Submission status: Wrong Answer

Solution code:
N = int(input())
# スペース区切りの整数の入力
A = list(map(int, input().split()))
A=list(set(A))

print(A)

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

l1=[]


if len(A)==1:
    ans=A[0]

elif len(A)==2:
    ans=max(A)

elif len(A)==3:
    a=gcd(A[0],A[1])
    b=gcd(A[0],A[2])
    c=gcd(A[2],A[1])
    ans=max([a,b,c])

else:
    a=A[0]
    b=A[1]
    if gcd(a,b)==1:
        a=A[2]
        b=A[4]
    
    for x in A:
        l1.append(gcd(gcd(x,a),b))
    np=l1.count(1)
    if np>1:
        ans=1
    else:
        ans=max(l1)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))

def gcd(m, n):
  while n:
    m, n = n, m%n
  return m

lcum = [0]*(N+1)
for i in range(N):
  lcum[i+1] = gcd(lcum[i], A[i])

rcum = [0]*(N+1)
for i in range(N):
  rcum[i] = gcd(rcum[i+1], A[i])

ans = 0
for i in range(1,N):
  ans = max(ans, gcd(lcum[i], rcum[i+1]))
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
# スペース区切りの整数の入力
A = list(map(int, input().split()))
A=list(set(A))

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

l1=[]


if len(A)==1:
    ans=A[0]

elif len(A)==2:
    ans=max(A)

elif len(A)==3:
    a=gcd(A[0],A[1])
    b=gcd(A[0],A[2])
    c=gcd(A[2],A[1])
    ans=max([a,b,c])

else:
    a=A[0]
    b=A[1]
    if gcd(a,b)==1:
        a=A[2]
        b=A[4]
    
    for x in A:
        l1.append(gcd(gcd(x,a),b))
    ans=max(l1)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))

import fractions
from functools import reduce

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

keep = 0
score = 1
for i in range(N):
    B = A
    if keep % score != 0:
        keep = B.pop(0)
        A.append(0)
        continue
    keep = B.pop(0)
    n = gcd_list(B)
    if n > score:
        score = n
    A.append(keep)


print(score)