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: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
from functools import reduce
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n=ii()
arr=limii()


def lgcd(numbers):
    return reduce(gcd, numbers)

ans =0

for i in range(n):
    ans=max(ans,lgcd(arr[:i]+arr[i+1:]))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
N=int(input())
a=list(map(int,input().split()))
ans=[]
for i in range(N):
    new=a[:i]+a[i+1:]
    g=reduce(fractions.gcd,new)
    ans.append(g)
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
d={}

def make_divisors(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

for i in make_divisors(a[0]):
    d[i]=1

for g in range(1,n):
    new={}
    search=set(d.keys())
    for i,m in d.items():
        if a[g]%i==0:
            d[i]+=1
            if not a[g]//i in search:
                new[a[g]//i]=1
    d.update(new)

ans=1
for i,m in d.items():
    if m==n-1 or m==n:
        ans=max(ans,i)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

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

def main():
    n = int(input())
    num_list = list(map(int, input().split()))

    ans = 0
    for i in range(n):
        # tmp = num_list[i]
        # num_list[i] = max(num_list)
        # ans = max(ans, fractions.gcd(num_list[i],min(num_list)))
        # num_list[i] = tmp
        li = num_list[:i] + num_list[i + 1:]
        tmp = gcd_list(li)
        if tmp > ans:
            ans = tmp

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce

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

def main():
    n = int(input())
    num_list = list(map(int, input().split()))

    ans = 1
    for i in range(n):
        tmp = num_list[i]
        num_list[i] = max(num_list)
        ans = max(ans, gcd_list(num_list))
        num_list[i] = tmp

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

def gcdlist(l):
    a = l[0]
    for i in range(len(l)):
        a = gcd(a,l[i])
    return a


dp = [a[0]]*(n)
dp[0] = a[1]
ldp = len(dp)
for i in range(1, n):
    a1 = a[i]
    ji = 0
    g1 = 0
    for j in range(ldp):
        if dp[j] == 1 or j == i: continue
        if j > i and ji == 0:
            if g1 == 0:
                g1 = gcd(a1, dp[j])
                ji = 1
                dp[j] = g1
                g1 = 1
            else:
                dp[j] = g1
        else:
            dp[j] = gcd(a1, dp[j])

print(max(dp))

Submission status: Time Limit Exceeded

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

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

def gcdlist(l):
    a = l[0]
    for i in range(len(l)):
        a = gcd(a,l[i])
    return a

dp = [a[0]]*(n)
dp[0] = a[1]
ldp = len(dp)
for i in range(1, n):
    a1 = a[i]
    for j in range(ldp):
        if i != j:
            dp[j] = gcd(a1, dp[j])

print(max(dp))

Submission status: Time Limit Exceeded

Solution code:
from itertools import chain
chain_from_iterable = chain.from_iterable
from collections import Counter

def divisors(n):
    divisors = {1, n}
    for i in range(2, int(n**0.5)+1):
        q, r = divmod(n, i)
        if not r:
            divisors.add(i)
            divisors.add(q)
    return divisors

n = int(input())
A = map(int, input().split())
B = (divisors(a) for a in A)
c = Counter(chain_from_iterable(B))
print(max(k for k, v in c.items() if v >= n-1))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
def divisors(n):
    divisors = set()
    for i in range(1, int(n**0.5)+1):
        q, r = divmod(n, i)
        if not r:
            divisors.add(i)
            divisors.add(q)
    return divisors

n = int(input())
*A, = map(int, input().split())
B = [divisors(a) for a in A]
d = defaultdict(int)
for b in B:
    for c in b:
        d[c] += 1
print(max(k for k, v in d.items() if v >= n-1))

Submission status: Time Limit Exceeded

Solution code:
def main():
  n=int(input())
  num=list(map(int,input().split()))

  def gcd(a,b):
    a,b=max(a,b),min(a,b)
    if b==0:
        return a
    else:
        return gcd(b,a%b)
  
  def gcd2(num,n):
    if n==1:
      return num[0]
    elif n==2:
      return gcd(num[0],num[1])
    else:
      return gcd2([gcd(num[i],num[n-i-1]) for i in range((n+1)//2)],(n+1)//2)

  ans=0
  for i in range(n):
    a=num.copy()
    a.pop(i)
    #print(a)
    tmp=gcd2(a,n-1)
    if tmp>ans:
      ans=tmp
  print(ans)  
  
if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

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

a = []
i = 1
while True:
  if A[0] % i == 0:
    a += [i, A[0]//i]
  i += 1
  if i**2 > A[0]:
    break
    
b = []
i = 1
while True:
  if A[1] % i == 0:
    a += [i, A[1]//i]
  i += 1
  if i**2 > A[1]:
    break
#print(a, b)
ans = 1
for i in a:
  counter = 0
  for p in A:
    if p % i == 0:
      counter += 1
  if counter >= N-1:
    ans = max(ans, i)
for i in b:
  counter = 0
  for p in A:
    if p % i == 0:
      counter += 1
  if counter >= N-1:
    ans = max(ans, i)
      
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd


class SegmentTree():
    _data = []
    _offset = 0
    _size = 0

    def __init__(self, size):
        _size = size
        t = 1
        while t <= size:
            t *= 2
        self._offset = t - 1
        self._data = [0] * (t * 2 - 1)

    def update_all(self, iterable):
        self._data[self._offset:self._offset+self._size] = iterable
        for i in range(self._offset - 1, -1, -1):
            self._data[i] = gcd(self._data[i * 2 + 1], self._data[i * 2 + 2])

    def update(self, index, value):
        i = self._offset + index
        self._data[i] = value
        while i >= 1:
            i = (i - 1) // 2
            self._data[i] = gcd(self._data[i * 2 + 1], self._data[i * 2 + 2])

    def _query(self, start, stop, index, left, right):
        if right <= start or stop <= left:
            return 0
        if start <= left and right <= stop:
            return self._data[index]
        a = self._query(start, stop, index * 2 + 1, left, (left + right) // 2)
        b = self._query(start, stop, index * 2 + 2, (left + right) // 2, right)
        return gcd(a, b)

    def query(self, start, stop):
        # return self._query(start, stop, 0, 0, self._offset + 1)
        result = 0
        q = [(0, 0, self._offset + 1)]
        while q:
            index, left, right = q.pop()
            if right <= start or stop <= left:
                continue
            if start <= left and right <= stop:
                result = gcd(result, self._data[index])
                continue
            q.append((index * 2 + 1, left, (left + right) // 2))
            q.append((index * 2 + 2, (left + right) // 2, right))
        return result


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

st = SegmentTree(N)
st.update_all(A)

result = st.query(1, N)
for i in range(1, N - 1):
    result = max(result, gcd(st.query(0, i), st.query(i + 1, N)))
result = max(result, st.query(0, N - 1))
print(result)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce

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

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

ans = 0
for i in range(len(a)):
  ans = max(ans, gcd_list(a[0:i]+a[i + 1:]))
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd

class SegmentTree():
    _data = []
    _offset = 0
    
    def __init__(self, size):
        t = 1
        while t <= size:
            t *= 2
        self._offset = t - 1
        self._data = [0] * (t * 2 - 1)
    
    def update_all(self, iterable):
        for i, v in enumerate(iterable):
            self._data[i + self._offset] = v
        for i in range(self._offset - 1, -1, -1):
            self._data[i] = gcd(self._data[i * 2 + 1], self._data[i * 2 + 2])
    
    def update(self, index, value):
        i = self._offset + index
        self._data[i] = value
        while i >= 1:
            i = (i - 1) // 2
            self._data[i] = gcd(self._data[i * 2 + 1], self._data[i * 2 + 2])
    
    def _query(self, start, stop, index, left, right):
        if right <= start or stop <= left:
            return 0
        if start <= left and right <= stop:
            return self._data[index]
        a = self._query(start, stop, index * 2 + 1, left, (left + right) // 2)
        b = self._query(start, stop, index * 2 + 2, (left + right) // 2, right)
        return gcd(a, b)
    
    def query(self, start, stop):
        return self._query(start, stop, 0, 0, self._offset + 1)

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

st = SegmentTree(N)
st.update_all(A)

result = st.query(1, N)
for i in range(1, N - 1):
    result = max(result, gcd(st.query(0, i), st.query(i + 1, N)))
result = max(result, st.query(0, N - 1))
print(result)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd


class SegmentTree():
    _data = []
    _offset = 0

    def __init__(self, size):
        t = 1
        while t <= size:
            t *= 2
        self._offset = t - 1
        self._data = [0] * (t * 2 - 1)

    def update(self, index, value):
        i = self._offset + index
        self._data[i] = value
        while i >= 1:
            i = (i - 1) // 2
            self._data[i] = gcd(self._data[i * 2 + 1], self._data[i * 2 + 2])

    def _query(self, start, stop, index, left, right):
        if right <= start or stop <= left:
            return 0
        if start <= left and right <= stop:
            return self._data[index]
        a = self._query(start, stop, index * 2 + 1, left, (left + right) // 2)
        b = self._query(start, stop, index * 2 + 2, (left + right) // 2, right)
        return gcd(a, b)

    def query(self, start, stop):
        return self._query(start, stop, 0, 0, self._offset + 1)


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

st = SegmentTree(N)
for i in range(N):
    st.update(i, A[i])

result = st.query(1, N)
for i in range(1, N - 1):
    result = max(result, gcd(st.query(0, i), st.query(i + 1, N)))
result = max(result, gcd(st.query(0, i), st.query(0, N - 1)))
print(result)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
import copy
N = int(input())
A = list(map(int, input().split()))

def gcd(numbers):
    return reduce(fractions.gcd, numbers)
res = 0
for i in range(N):
  partA = copy.deepcopy(A)
  partA.pop(i)
  gcdres = gcd(partA)
  if res < gcdres:
    res = gcdres

print(res)

Submission status: Time Limit Exceeded

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

from itertools import combinations
a = list(combinations(l, n-1))
#print(a)

from fractions import gcd
from functools import reduce

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

g = []
for i in range(len(a)):
    g.append(gcd_list(a[i]))

print(max(g))

Submission status: Time Limit Exceeded

Solution code:
import fractions
import itertools
from functools import reduce

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

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

tmp = 0
for v in itertools.permutations(a, len(a)-1):
    tmp = max(gcd(*v), tmp)

print(tmp)

Submission status: Time Limit Exceeded

Solution code:
from collections import OrderedDict
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a
def factorize(m):
    d = []
    for i in range(1, int(m**0.5)+1):
        if m % i == 0:
            d.append(i)
            if i != m // i:
                d.append(m//i)
    return d
def main():
    N = int(input())
    A = list(map(int, input().split()))
    l = list(set(factorize(A[0]) + factorize(A[1])))
    l.sort(reverse=True)
    d = OrderedDict()
    for i in l:
    	d[i] = 0
    for i in A:
        for j in d:
            if i % j == 0:
                d[j] += 1
    m = 1
    for k in d:
        if d[k] >= N-1:
            print(k)
            return
    print(1)
main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a
def factorize(m, d):
    for i in range(1, int(m**0.5)+1):
        if m % i == 0:
            d[i] += 1
            if i != m // i:
                d[m//i] += 1
    return d
def main():
    N = int(input())
    A = list(map(int, input().split()))
    l = Counter()
    for i in A:
        l = factorize(i, l)
    m = 1
    for k in l:
        if l[k] >= N-1 and k > m:
            m = k
    print(m)
main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a
def factorize(m):
    d = []
    for i in range(1, int(m**0.5)+1):
        if m % i == 0:
            d.append(i)
            if i != m // i:
                d.append(m//i)
    return d
def main():
    N = int(input())
    A = list(map(int, input().split()))
    l = []
    for i in A:
        l.extend(factorize(i))
    c = Counter(l)
    m = 1
    for k in c:
        if c[k] >= N-1 and k > m:
            m = k
    print(m)
main()

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce

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

    def pleaceAC():
        for i in range(N):
            a = reduce(gcd, [0]+A[:i])
            b = reduce(gcd, reversed(A[i+1:]+[0]))
            yield gcd(a, b)

    print(max(pleaceAC()))


main()

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
N,*a=map(int,open(0).read().split())
L=[]
R=[]
for i in range(N):
    L.append(reduce(fractions.gcd,a[:i+1]))
    R.append(reduce(fractions.gcd,a[(N-1)-i:]))
L.pop()
R.pop()
R.reverse()    
#print(L,R)
LR=[R[0]]
for i in range(1,N-1):
    LR.append(fractions.gcd(L[i-1],R[i]))
LR.append(L[-1])
print(max(LR))

Submission status: Time Limit Exceeded

Solution code:
import copy

import fractions

def longgcd(S):
    if len(S)==2:
        return fractions.gcd(S[0],S[1])
    else:
        x=S[0]
        for i in range(0,N):
            x=fractions.gcd(x,S[i])
        return x

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

m=0

for i in range(0,N):
    B=copy.copy(A)
    B[i]=0
    x=longgcd(B)
    if x>m:
        m=x
print(m)

Submission status: Time Limit Exceeded

Solution code:
import copy

def gcd(n,m):#n,mは正の整数、最大公約数　許容は？
    if n==0:
        return m
    elif m==0:
        return n
    elif n>m:
        n=n%m
        return gcd(n,m)
    else:
        m=m%n
        return gcd(n,m)

def longgcd(S):
    if len(S)==2:
        return gcd(S[0],S[1])
    else:
        x=S[0]
        for i in range(0,N):
            x=gcd(x,S[i])
        return x

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

m=0

for i in range(0,N):
    B=copy.copy(A)
    B[i]=0
    x=longgcd(B)
    if x>m:
        m=x
print(m)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
N,*a=map(int,open(0).read().split())
_max=0
for i in a:
    b=a[:]
    b.remove(i)
    _max=max(_max,reduce(fractions.gcd,b))
print(_max)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce
from itertools import combinations

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

b = list(combinations(a, n-1))

gcd_a = [reduce(gcd, list(x)) for x in b]
print(max(gcd_a))

Submission status: Time Limit Exceeded

Solution code:
def gcdf(a):
    a.sort()
    a0=a[0]
    a=[int(x%a0) for x in a if x%a0 != 0]
    if len(a)==0:
        return a0
    else:
        a.append(a0)
        a0=min(a)
        return gcdf(a) 

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

if n==2:
    print(max(a))
elif n<2**8:
    gcdv=0
    for i in range(n):
        acpy=list(a)
        del acpy[i]
        gcdx=gcdf(acpy)
        if gcdx>gcdv:
            gcdv=gcdx
    print(gcdv)
else:
    m=int(n/10)
    ns=int(n/m)
    rgcd=[]
    for mi in range(m):
        ms=ns*mi
        if mi==m-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
        gcdm=gcdf(am)
        rgcd.append(gcdm)
    gcdv=0

    for mi in range(m):
        rgcd1=list(rgcd)
        del rgcd1[mi]
        gcdrg1=gcdf(rgcd1)
        ms=ns*mi
        if mi==m-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
#        print(mi,gcdf(am),len(am),gcdf(rgcd1))
        for i in range(len(am)):
            acpy=list(am)
#            del acpy[i]
#            acpy.append(gcdrg1)
            acpy[i]=gcdrg1
            gcdx=gcdf(acpy)
            if gcdx>gcdv:
                gcdv=gcdx
    print(gcdv)

Submission status: Time Limit Exceeded

Solution code:
def gcdf(a):
    a.sort()
    a0=a[0]
    a=[int(x%a0) for x in a if x%a0 != 0]
    if len(a)==0:
        return a0
    else:
        a.append(a0)
        a0=min(a)
        return gcdf(a) 

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

if n==2:
    print(max(a))
elif n<2**8:
    gcdv=0
    for i in range(n):
        acpy=list(a)
        del acpy[i]
        gcdx=gcdf(acpy)
        if gcdx>gcdv:
            gcdv=gcdx
    print(gcdv)
else:
    m=int(n**(1/2))
    ns=int(n/m)
    rgcd=[]
    for mi in range(m):
        ms=ns*mi
        if mi==m-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
        gcdm=gcdf(am)
        rgcd.append(gcdm)
    gcdv=0

    for mi in range(m):
        rgcd1=list(rgcd)
        del rgcd1[mi]
        gcdrg1=gcdf(rgcd1)
        ms=ns*mi
        if mi==m-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
#        print(mi,gcdf(am),len(am),gcdf(rgcd1))
        for i in range(len(am)):
            acpy=list(am)
#            del acpy[i]
#            acpy.append(gcdrg1)
            acpy[i]=gcdrg1
            gcdx=gcdf(acpy)
            if gcdx>gcdv:
                gcdv=gcdx
    print(gcdv)

Submission status: Time Limit Exceeded

Solution code:
def gcdf(a):
    a.sort()
#    print("a2:",a)
    a0=a[0]
    a=[int(x%a0) for x in a if x%a0 != 0]
#    print("a2:",a)
    if len(a)==0:
        return a0
    else:
        a.append(a0)
        a0=min(a)
        return gcdf(a) 

#n=8
#a=[746130, 1385670, 4849845, 881790, 3233230, 1939938, 570570, 510510]
#ans=[19]
#n=2
#a=[1000000000, 1000000000]
#n=5
#a=[15, 18, 24, 36, 81]
#n=3
#a=[7, 6, 8]
n=int(input())
a=[]
for i in map(int,input().split()):
    a.append(i)
if n==2:
    print(max(a))
elif n<2**10:
    gcdv=0
    for i in range(n):
        acpy=list(a)
        del acpy[i]
        gcdx=gcdf(acpy)
        if gcdx>gcdv:
            gcdv=gcdx
    print(gcdv)
else:
    m=int(n**(1/2))
    ns=int(n/m)
    rgcd=[]
    for mi in range(m):
        ms=ns*mi
        if mi==m-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
        gcdm=gcdf(am)
        rgcd.append(gcdm)
    gcdv=0

    for mi in range(m):
        rgcd1=list(rgcd)
        del rgcd1[mi]
        gcdrg1=gcdf(rgcd1)
        ms=ns*mi
        if mi==m-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
#        print(mi,gcdf(am),len(am),gcdf(rgcd1))
        for i in range(len(am)):
            acpy=list(am)
#            del acpy[i]
#            acpy.append(gcdrg1)
            acpy[i]=gcdrg1
            gcdx=gcdf(acpy)
            if gcdx>gcdv:
                gcdv=gcdx
    print(gcdv)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
from fractions import gcd
def lcm(x,y):
  return x * y // gcd(x,y)
def list_lcm(list):
  c=1
  for i in list:
    c=lcm(c,i)
  return c
def list_gcd(list):
    c=list[0]
    for i in list:
        c=gcd(c,i)
    return c
for i in range(n):
  p=a.copy()
  del p[i]
  if i==0:
    m=list_gcd(p)
  else:
    m=max(m,list_gcd(p))
print(m)

Submission status: Time Limit Exceeded

Solution code:
def gcdf(a):
    a.sort()
    a0=a[0]
    a=[int(x%a0) for x in a if x%a0 != 0]
#    print("a2:",a)
    if len(a)==0:
        return a0
    else:
        a.append(a0)
        a0=min(a)
        return gcdf(a) 

#n=8
#a=[746130, 1385670, 4849845, 881790, 3233230, 1939938, 570570, 510510]
#ans=[19]
#n=2
#a=[1000000000, 1000000000]
#n=5
#a=[15, 18, 24, 36, 81]
#n=3
#a=[7, 6, 8]
n=int(input())
a=[]
for i in map(int,input().split()):
    a.append(i)

#print(a)
if n==2:
    print(max(a))
elif n<2**10:
    gcdv=0
    for i in range(n):
        acpy=list(a)
        del acpy[i]
        gcdx=gcdf(acpy)
        if gcdx>gcdv:
            gcdv=gcdx
    print(gcdv)
else:
    m=2**9
    ns=int(n/m)
    rgcd=[]
    for mi in range(m):
        ms=ns*mi
        if mi==n-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
        gcdm=gcdf(am)
        rgcd.append(gcdm)

    gcdv=0

    for mi in range(m):
        rgcd1=list(rgcd)
        del rgcd1[mi]
        ms=ns*mi
        if mi==n-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
        for i in range(len(am)):
            acpy=list(am)
            del acpy[i]
            acpy=acpy+rgcd1
            gcdx=gcdf(acpy)
            if gcdx>gcdv:
                gcdv=gcdx

#    print(gcdv,a1)
    print(gcdv)

Submission status: Time Limit Exceeded

Solution code:
def gcdf(a):
    a.sort()
    a0=a[0]
    a=[int(x%a0) for x in a if x%a0 != 0]
#    print("a2:",a)
    if len(a)==0:
        return a0
    else:
        a.append(a0)
        a0=min(a)
        return gcdf(a) 

#n=8
#a=[746130, 1385670, 4849845, 881790, 3233230, 1939938, 570570, 510510]
#ans=[19]
#n=2
#a=[1000000000, 1000000000]
#n=5
#a=[15, 18, 24, 36, 81]
#n=3
#a=[7, 6, 8]
n=int(input())
a=[]
for i in map(int,input().split()):
    a.append(i)

#print(a)
if n==2:
    print(max(a))
elif n<2**10:
    gcdv=0
    for i in range(n):
        acpy=list(a)
        del acpy[i]
        gcdx=gcdf(acpy)
        if gcdx>gcdv:
            gcdv=gcdx
    print(gcdv)
else:
    m=2**9
    ns=int(n/m)
    rgcd=[]
    for mi in range(m):
        ms=ns*mi
        if mi==n-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
        gcdm=gcdf(am)
        rgcd.append(gcdm)

    gcdv=0

    for mi in range(m):
        rgcd1=rgcd.copy()
        del rgcd1[mi]
        ms=ns*mi
        if mi==n-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
        for i in range(len(am)):
            acpy=am.copy()
            del acpy[i]
            acpy=acpy+rgcd1
            gcdx=gcdf(acpy)
            if gcdx>gcdv:
                gcdv=gcdx

#    print(gcdv,a1)
    print(gcdv)

Submission status: Time Limit Exceeded

Solution code:
def gcdf(a):
    a.sort()
    a0=a[0]
    a=[int(x%a0) for x in a if x%a0 != 0]
#    print("a2:",a)
    if len(a)==0:
        return a0
    else:
        a.append(a0)
        a0=min(a)
        return gcdf(a) 

#n=8
#a=[746130, 1385670, 4849845, 881790, 3233230, 1939938, 570570, 510510]
#ans=[19]
#n=2
#a=[1000000000, 1000000000]
#n=5
#a=[15, 18, 24, 36, 81]
#n=3
#a=[7, 6, 8]
n=int(input())
a=[]
for i in map(int,input().split()):
    a.append(i)

#print(a)
if n==2:
    print(max(a))
elif n<2**12:
    gcdv=0
    for i in range(n):
        acpy=list(a)
        del acpy[i]
        gcdx=gcdf(acpy)
        if gcdx>gcdv:
            gcdv=gcdx
    print(gcdv)
else:
    m=2**10
    ns=int(n/m)
    rgcd=[]
    for mi in range(m):
        ms=ns*mi
        if mi==n-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
        gcdm=gcdf(am)
        rgcd.append(gcdm)

    gcdv=0

    for mi in range(m):
        rgcd1=rgcd.copy()
        del rgcd1[mi]
        ms=ns*mi
        if mi==n-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
        for i in range(len(am)):
            acpy=am.copy()
            del acpy[i]
            acpy=acpy+rgcd1
            gcdx=gcdf(acpy)
            if gcdx>gcdv:
                gcdv=gcdx

#    print(gcdv,a1)
    print(gcdv)

Submission status: Time Limit Exceeded

Solution code:
def gcdf(a):
    a.sort()
    a0=a[0]
    a=[int(x%a0) for x in a if x%a0 != 0]
#    print("a2:",a)
    if len(a)==0:
        return a0
    else:
        a.append(a0)
        a0=min(a)
        return gcdf(a) 

#n=8
#a=[746130, 1385670, 4849845, 881790, 3233230, 1939938, 570570, 510510]
#ans=[19]
#n=2
#a=[1000000000, 1000000000]
#n=5
#a=[15, 18, 24, 36, 81]
#n=3
#a=[7, 6, 8]
n=int(input())
a=[]
for i in map(int,input().split()):
    a.append(i)

#print(a)
if n==2:
    print(max(a))
elif n<2**10:
    gcdv=0
    for i in range(n):
        acpy=list(a)
        del acpy[i]
        gcdx=gcdf(acpy)
        if gcdx>gcdv:
            gcdv=gcdx
    print(gcdv)
else:
    m=2**8
    ns=int(n/m)
    rgcd=[]
    for mi in range(m):
        ms=ns*mi
        if mi==n-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
        gcdm=gcdf(am)
        rgcd.append(gcdm)

    gcdv=0

    for mi in range(m):
        rgcd1=rgcd.copy()
        del rgcd1[mi]
        ms=ns*mi
        if mi==n-1:
            am=a[ms:n]
        else:
            am=a[ms:ms+ns]
        for i in range(len(am)):
            acpy=am.copy()
            del acpy[i]
            acpy=acpy+rgcd1
            gcdx=gcdf(acpy)
            if gcdx>gcdv:
                gcdv=gcdx

#    print(gcdv,a1)
    print(gcdv)

Submission status: Time Limit Exceeded

Solution code:
def gcdf(a,n,a0):
    a.sort()
#    print("a1:",a)
    a0=a[0]
    a=[int(x%a0) for x in a if x%a0 != 0]
#    print("a2:",a)
    if len(a)==0:
        return a0
    else:
        a.append(a0)
        a0=min(a)
#        print("a0",a0)
        return gcdf(a,n,a0) 

#n=2
#a=[1000000000, 1000000000]
#n=5
#a=[15, 18, 24, 36, 81]
#n=3
#a=[7, 6, 8]
n=int(input())
a=[]
for i in map(int,input().split()):
    a.append(i)
#n=8
#a=[746130, 1385670, 4849845, 881790, 3233230, 1939938, 570570, 510510]
#ans=[19]

#print(a)
if n==2:
    print(max(a))
else:
    gcdv=0
    for i in range(n):
#        print("--------")
        acpy=list(a)
        del acpy[i]
        a0=acpy[0]
        gcdx=gcdf(acpy,n-1,a0)
        if gcdx>gcdv:
            gcdv=gcdx
    print(gcdv)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

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

d = Counter(divisors)
ans = [k for k, v in d.items() if v >= n - 1]
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import itertools

def make_divisors(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)

    # divisors.sort()
    return divisors

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

div_l = []
for i in range(n):
    div_l.append(make_divisors(a[i]))

div = list(itertools.chain.from_iterable(div_l))
dict = Counter(div)
ans = [k for k, v in dict.items() if v >= n - 1]
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def make_divisors(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 = int(input())
a = list(map(int, input().split()))

div_l = []
for i in range(n):
    for j in make_divisors(a[i]):
        div_l.append(j)

dict = Counter(div_l)
ans = [k for k, v in dict.items() if v >= n - 1]
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
#ABC125C
N=int(input())
A=list(map(int,input().split()))
def gcd(a,b):
    if a>b:a,b=b,a
    while a%b:
        a,b=b,a%b
    return b
def gcd_list(A):
    if len(A)<2:return A[0]
    res=gcd(A[0],A[1])
    for i in range(len(A)-2):
        res=gcd(res,A[i+2])
    return res
res=0
for i in range(N):
    Ai=A[:]
    Ai.pop(i)
    res=max(res,gcd_list(Ai))
print(res)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import copy
import functools

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

maxValue = 1
maxi = 0
for i in range(n):
  tmpList = copy.deepcopy(allList)
  tmpList.pop(i)
  gcd =functools.reduce(fractions.gcd, tmpList)
  if gcd > maxValue:
    maxi = i
    maxValue = gcd

print(maxValue)