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 math
from functools import reduce
import fractions

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

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

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

candidates = []
for i in range(N):
    c = []
    L1 = A[:i]
    if L1 != []:
        c.append(gcd_list(L1))
    L2 = A[(i+1):]
    if L2 != []:
        c.append(gcd_list(L2))
    #print(L1,L2,gcd_list(c))
    candidates.append(gcd_list(c))

print(max(candidates))

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
# 単位元とseg_funcを設定する
from fractions import gcd
class seg():
    def __init__(self,init_val):
        self.n=len(init_val)
        self.ide_ele=0 #単位元
        self.num=2**(self.n-1).bit_length() #n以上の最小の2のべき乗
        self.seg=[self.ide_ele]*2*self.num
        for i in range(self.n):
            self.seg[i+self.num-1]=init_val[i]
        for i in range(self.num-2,-1,-1):
            self.seg[i]=self.seg_func(self.seg[2*i+1],self.seg[2*i+2])
    def seg_func(self,a,b):
        #return a+b #0
        #return a*b #1
        return gcd(a,b) #0
        #return max(a,b) #-1か-10**10 (十分小さいもの)
        #return min(a,b) #10**10 (十分大きいもの)
    def update(self,k,x):
        k+=self.num-1
        self.seg[k]=x
        while k:
            k=(k-1)//2
            self.seg[k]=self.seg_func(self.seg[k*2+1],self.seg[k*2+2])
    def query(self,p,q): #O(logN)
        if q<=p:return self.ide_ele
        p+=self.num-1
        q+=self.num-2
        self.res=self.ide_ele
        while q-p>1:
            if p&1==0:
                self.res=self.seg_func(self.res,self.seg[p])
            if q&1==1:
                self.res=self.seg_func(self.res,self.seg[q])
                q-=1
            p=p//2
            q=(q-1)//2
        if p==q:self.res=self.seg_func(self.res,self.seg[p])
        else:self.res=self.seg_func(self.seg_func(self.res,self.seg[p]),self.seg[q])
        return self.res

def main():
    n=int(input())
    A=[int(_) for _ in input().split()]
    ans=0
    seg_a=seg(A)
    for i in range(n):
        ans=max(ans,gcd(seg_a.query(0,i),seg_a.query(i+1,n)))
    print(ans)
if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd

n = int(input())
a = list(map(int, input().split()))
gl=[0]
gr=[0]
for i in range(n):
  gl.append(gcd(a[i], gl[-1]))
  gr.insert(0,gcd(a[n-i-1], gr[0]))
ans = 0
for i in range(n):
  ans = max(ans, gcd(gl[i],gr[i+1]))
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6) #再帰関数の上限
import math
from copy import copy, deepcopy
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque 
#deque(l), pop(), append(x), popleft(), appendleft(x)
##listでqueの代用をするとO(N)の計算量がかかってしまうので注意
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
import fractions#GCDなどはここからimportする！

def input(): return sys.stdin.readline()[:-1]
def printl(li): print(*li, sep="\n")
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds

class segtree():#seg=segtree(A),単位元とsegfuncを指定すること
    def __init__(self,A,ide=0):#単位元とsegfuncを指定すること
        n=len(A)
        num=2**(n-1).bit_length()
        seg=[ide]*(2*num-1)
        segfunc=self.segfunc
        self.ide,self.n,self.num,self.seg = ide, n, num, seg
        for i in range(n): seg[i+num-1]=A[i]#setvalue
        for i in range(num-2,-1,-1):seg[i]=segfunc(seg[2*i+1],seg[2*i+2])#build
            
    def segfunc(self,x,y):return fractions.gcd(x,y) #seg木の基本関数
        
    def update(self,k,x):#Aのk番目の要素をxに置き換える,計算量log(N)
        k += self.num-1
        seg,segfunc=self.seg,self.segfunc
        seg[k] = x
        while k:
            k = (k-1)//2
            seg[k] = segfunc(seg[k*2+1],seg[k*2+2])
    
    def query(self,p,q):#区間[p,q)の累積segfuncを求める,計算量log(N)
        if q<=p: return self.ide
        num,segfunc,seg=self.num,self.segfunc,self.seg
        p += num-1; q += num-2
        res=self.ide
        while q-p>1:
            if p&1 == 0: res = segfunc(res,seg[p])
            if q&1 == 1:
                res = segfunc(res,seg[q])
                q -= 1
            p,q = p//2, (q-1)//2
        if p == q: res = segfunc(res,seg[p])
        else: res = segfunc(segfunc(res,seg[p]),seg[q])
        return res

def main():
    mod = 10**9+7
    #w.sort(key=itemgetter(1),reversed=True)  #二個目の要素で降順並び替え

    N = int(input())
    #N, K = map(int, input().split())
    A = tuple(map(int, input().split())) #1行ベクトル
    #L = tuple(int(input()) for i in range(N)) #改行ベクトル
    #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列

    seg=segtree(A)
    ans=1
    for i in range(N):
        seg.update(i,0)
        ans=max(ans,seg.query(0,N))
        seg.update(i,A[i])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy

from fractions import gcd
from functools import reduce

def gcdli(xl):
    #xlはlist
    return reduce(gcd , xl)

def gcd_on_blackboard(n, ali):

    ans = 0

    for i in range(n):
        count = copy.copy(ali)
        count.pop(i)
        ans = max(ans, gcdli(count))

    return ans

def main():
    n = int(input())
    ali = list(map(int, input().split()))
    print(gcd_on_blackboard(n, ali))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

def div(x):
    divL=[]
    for i in range(1,int(x**0.5)+1):
        if x%i==0:
            if i!=n**0.5:
                divL.append(i)
                divL.append(x//i)
            else:
                divL.append(i)
    return divL
okL=div(a[0])
alL=[]
prookL=[]
for j in div(a[1]):
    if j not in okL:
        alL.append(j)
    else:
        okL.remove(j)
        prookL.append(j)
alL += okL
okL.clear()
okL += prookL

for i in range(2,n):
    prookL=[]
    proalL=[]
    for j in div(a[i]):
        if j not in okL:
            if j in alL:
                alL.remove(j)
                proalL.append(j)
        else:
            okL.remove(j)
            prookL.append(j)
    alL.clear()
    alL += okL+proalL
    okL.clear()
    okL += prookL

okL += alL
ans=sorted(okL,reverse=True)
print(ans[0])

Submission status: Time Limit Exceeded

Solution code:
import fractions
import functools

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

ma = 0
for i in range(len(a)):
  if ma < functools.reduce(fractions.gcd, a[:i] + a[i+1:]):
    ma = functools.reduce(fractions.gcd, a[:i] + a[i+1:])

print(ma)

Submission status: Time Limit Exceeded

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

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

from collections import defaultdict
m = defaultdict(int)

for i in range(N):
    divisors = make_divisors(A[i])
    for d in divisors:
        m[d] += 1

ans = 1
for k, v in sorted(m.items(), key=lambda x: x[0]):
    if v >= N-1 and ans < k:
        ans = k

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd

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

ans = 0
for i in range(n):
    temp = None
    for j in range(n):
        if j == i:
            continue
        
        if temp is None:
            temp = a_list[j]
        else:
            temp = gcd(temp, a_list[j])
        
        if temp == 1:
            break
    
    if temp > ans:
        ans = temp
print(ans)

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

ans = -1
for i in range(N):
    target = lst[:i] + lst[i+1:]
    val = gcd_list(target)
    if val > ans:
        ans = val
print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

def main2(N,A):
    if N == 2:
        return max(A)
    else:
        max_ = -1
        for i in range(N):
            L = 0
            R = 0

            for j in range(N):
                if i == j: continue
                if j < i:
                    L = gcd(L, A[j])
                else:
                    R = gcd(R, A[j])
            tmp = gcd(L,R)
            if tmp > max_:
                max_ = tmp
        return max_

N = int(input())
A = [int(i) for i in input().split(" ")]
print(main2(N,A))

Submission status: Time Limit Exceeded

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

def main2(N,A):
    if N == 2:
        return max(A)
    else:
        max_ = -1
        for i in range(N):
            L = 0
            R = 0

            for j in range(N):
                if i == j: continue
                if j < i:
                    L = gcd(L, A[j])
                else:
                    R = gcd(R, A[j])
            tmp = gcd(L,R)
            if tmp > max_:
                max_ = tmp
        return max_

N = int(input())
A = [int(i) for i in input().split(" ")]
print(main2(N,A))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
def divisor(n):
    i = 1
    l = []
    while i * i <= n:
        if n%i == 0:
            l.append(i)
            l.append(n//i)
        i += 1
    l = list(set(l))
    return l   

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

arr_1=[]
for i in a:
    arr_1.append(divisor(i))

arr_2=sum(arr_1,[])
arr_3=Counter(arr_2)
ans=0
for d,c in arr_3.items():
    if c>=n-1 and d>=ans:
        ans=d
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
def divisor(n):
    i = 1
    l = []
    while i * i <= n:
        if n%i == 0:
            l.append(i)
            l.append(n//i)
        i += 1
    l = list(set(l))
    return l   

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

arr_1=[]
for i in a:
    arr_1.append(divisor(i))

arr_2=sum(arr_1,[])
arr_3=Counter(arr_2)
ans=0
for d,c in arr_3.items():
    if c>=n-1 and d>=ans:
        ans=d
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
n = int(input())

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


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


# print(gcd(6, 8))
# print(xs)
# for ind in range(len(xs)):
#     for x in range(len(xs)):
#         if x != ind:
#             v = gcd(xs[x], xs[x+1])
xs = sorted(xs)
mx = 1
ok = False
for ind in range(len(xs)):
    if ind == 0:
        v = xs[1]
    else:
        v = xs[0]
    for i in range(len(xs)):
        if i != ind:
            v = gcd(v, xs[i])
    if ind == 0:
        if v == xs[1]:
            print(v)
            ok = True
            break
    if ind == 1:
        if v == xs[0]:
            print(v)
            ok = True
            break
    if v >= mx:
        mx = v
    # print(f'v for ind {ind}: {v}')
# print(mx)
if not ok:
    print(mx)

Submission status: Time Limit Exceeded

Solution code:
import math
n = int(input())

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


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


# print(gcd(6, 8))
# print(xs)
# for ind in range(len(xs)):
#     for x in range(len(xs)):
#         if x != ind:
#             v = gcd(xs[x], xs[x+1])

mx = 1
for ind in range(len(xs)):
    if ind == 0:
        v = xs[1]
    else:
        v = xs[0]
    for i in range(len(xs)):
        if i != ind:
            v = gcd(v, xs[i])
    if v >= mx:
        mx = v
    # print(f'v for ind {ind}: {v}')
print(mx)

Submission status: Time Limit Exceeded

Solution code:
import itertools


flatten_iter = itertools.chain.from_iterable
def factors(n):
    return set(flatten_iter((i, n//i) 
                for i in range(1, int(n**0.5)+1) if n % i == 0))

line = int(input())


line = input()
numbers = [int(n) for n in line.split()]

num_factors = [factors(num) for num in numbers]
factors_pairs = []
for i in range(len(numbers)):
    common_factors = None
    for dividends in num_factors[:i] + num_factors[i+1:]:
        if not common_factors:
            common_factors = dividends
        else:
            common_factors = common_factors.intersection(dividends)
    factors_pairs.append(max(common_factors))
predicted_value = max(factors_pairs)

print(predicted_value)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
n = int(input())
a = [int(i) for i in input().split()]

mx = 0
for i in range(n):
    l = 0
    r = 0
    for j in range(i): l = gcd(l, a[j])
    for j in range(i+1, n): r = gcd(r, a[j])
    mx = max(mx, gcd(l, r))

print(mx)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys
from fractions import gcd
from functools import lru_cache
sys.setrecursionlimit(10 ** 7)

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = lr()

@lru_cache(None)
def solve(index, n, g):
    '''既に１つ数を選んだらnは１'''
    if index == N:
        return g
    if n == 1:
        return solve(index+1, 1, gcd(g, A[index]))
    else:
        ret = max(solve(index+1, 1, g), solve(index+1, 0, gcd(g, A[index])))
        return ret

result1 = solve(0, 0,  A[0])
result2 = solve(0, 0, A[-1])
answer = max(result1, result2)
print(answer)

Submission status: Time Limit Exceeded

Solution code:
class SegmentTree():
    def segfunc(self, x, y):
        from fractions import gcd
        if x > y:
            x, y = y, x
        if x == -1:
            return y
        return gcd(x, y) #関数
    
    def __init__(self, a): #a:リスト
        n = len(a)
        self.ide_ele = -1 #単位元
        self.num = 2**((n-1).bit_length()) #num:n以上の最小の2のべき乗
        self.seg = [self.ide_ele]*2*self.num
        
        #set_val
        for i in range(n):
            self.seg[i+self.num-1] = a[i]
        #built
        for i in range(self.num-2,-1,-1):
            self.seg[i] = self.segfunc(self.seg[2*i+1], self.seg[2*i+2])
        
        
    def update(self, k, x):
        k += self.num-1
        self.seg[k] = x
        while k:
            k = (k-1)//2
            self.seg[k] = self.segfunc(self.seg[k*2+1], self.seg[k*2+2])
            
    def query(self, p, q):
        if q <= p:
            return self.ide_ele
        p += self.num-1
        q += self.num-2
        res = self.ide_ele
        while q - p > 1:
            if p&1 == 0:
                res = self.segfunc(res, self.seg[p])
            if q&1 == 1:
                res = self.segfunc(res, self.seg[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        if p == q:
            res = self.segfunc(res,self.seg[p])
        else:
            res = self.segfunc(self.segfunc(res,self.seg[p]),self.seg[q])
        return res
    
N = int(input())
A = list(map(int, input().split()))
M = 0
for i in range(N):
    S = SegmentTree(A)
    S.update(i, -1)
    M = max(M, S.query(0,N))
print(M)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd   # gcd(6, 4) -> 2
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(N):
    tmp = gcd_list(A[:i] + A[i+1:])
    ans = max(ans, tmp)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
from fractions import gcd

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

ans = 1
for i in range(N):
    B = A[:i] + A[i+1:]
    ans = max(ans, reduce(gcd, B))

print(ans)

Submission status: Time Limit Exceeded

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

def gcd(n1,n2):
  if n1<n2:
    n1,n2 = n2,n1
  while (n2>0):
    r = n1%n2
    n1 = n2
    n2 = r
  return n1

def gcd_list(l):
  if len(l)==1: return l[0]
  else:
    g = gcd(l[0],l[1])
    for i in range(2,len(l)):
      g = gcd(g,l[i])
    return g
t_g = 1
for i in range(1,n-1):
  l_g = gcd_list(num[0:i])
  r_g = gcd_list(num[i+1:n])
  t_g = max(t_g,gcd(l_g,r_g))

t_g = max(t_g,gcd_list(num[1:n]))
t_g = max(t_g,gcd_list(num[0:n-1]))
print(t_g)

Submission status: Time Limit Exceeded

Solution code:
class SegmentTree:
    def __init__(self, size, f=lambda x,y : x+y, default=0):
        self.size = 2**(size-1).bit_length()
        self.default = default
        self.dat = [default]*(self.size*2-1)
        self.f = f

    def update(self, i, x):
        i += self.size-1
        self.dat[i] = x
        while i > 0:
            i = (i-1) >> 1
            self.dat[i] = self.f(self.dat[i*2+1], self.dat[i*2+2])

    def query(self, l, r, k=0, L=0, R=None):
        if R is None:
            R = self.size
        if R <= l or r <= L:
            return self.default
        if l <= L and R <= r:
            return self.dat[k]
        else:
            lres = self.query(l, r, k*2+1, L, (L+R) >> 1)
            rres = self.query(l, r, k*2+2, (L+R) >> 1, R)
            return self.f(lres, rres)

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

n = int(input())
a = list(map(int, input().split()))
s = SegmentTree(n, gcd)
for i in range(n):
    s.update(i, a[i])

ans = 1
for i in range(n):
    left = s.query(0, i)
    right = s.query(i+1, n)
    res = gcd(left, right)
    if ans < res:
        ans = res
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

class SegmentTree:
    def __init__(self, size, f=lambda x,y : x+y, default=0):
        self.size = 2**(size-1).bit_length()
        self.default = default
        self.dat = [default]*(self.size*2)
        self.f = f

    def update(self, i, x):
        i += self.size
        self.dat[i] = x
        while i > 0:
            i >>= 1
            self.dat[i] = self.f(self.dat[i*2], self.dat[i*2+1])

    def query(self, a, b=None):
        if b is None:
            b = a + 1
        l, r = a + self.size, b + self.size
        lres, rres = self.default, self.default
        while l < r:
            if l & 1:
                lres = self.f(lres, self.dat[l])
                l += 1

            if r & 1:
                r -= 1
                rres = self.f(self.dat[r], rres)
            l >>= 1
            r >>= 1
        res = self.f(lres, rres)
        return res

n = int(sys.stdin.buffer.readline())
a = list(map(int, sys.stdin.buffer.readline().split()))
s = SegmentTree(n, gcd)
for i in range(n):
    s.update(i,a[i])

ans = 1
for i in range(n):
    left = s.query(0,i)
    right = s.query(i+1,n)
    res = gcd(left, right)
    if ans < res:
        ans = res
print(ans)

Submission status: Time Limit Exceeded

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

class SegmentTree:
    def __init__(self, size, f=lambda x,y : x+y, default=0):
        self.size = 2**(size-1).bit_length()
        self.default = default
        self.dat = [default]*(self.size*2)
        self.f = f

    def update(self, i, x):
        i += self.size
        self.dat[i] = x
        while i > 0:
            i >>= 1
            self.dat[i] = self.f(self.dat[i*2], self.dat[i*2+1])

    def query(self, a, b=None):
        if b is None:
            b = a + 1
        l, r = a + self.size, b + self.size
        lres, rres = self.default, self.default
        while l < r:
            if l & 1:
                lres = self.f(lres, self.dat[l])
                l += 1

            if r & 1:
                r -= 1
                rres = self.f(self.dat[r], rres)
            l >>= 1
            r >>= 1
        res = self.f(lres, rres)
        return res

n = int(input())
a = list(map(int, input().split()))
s = SegmentTree(n, gcd)
for i in range(n):
    s.update(i,a[i])

ans = 1
for i in range(n):
    left = s.query(0,i)
    right = s.query(i+1,n)
    res = gcd(left, right)
    if ans < res:
        ans = res
print(ans)

Submission status: Time Limit Exceeded

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

class SegmentTree:
    def __init__(self, size, f=lambda x,y : x+y, default=0):
        self.size = 2**(size-1).bit_length()
        self.default = default
        self.dat = [default]*(self.size*2-1)
        self.f = f

    def update(self, i, x):
        i += self.size-1
        self.dat[i] = x
        while i > 0:
            i = (i-1) >> 1
            self.dat[i] = self.f(self.dat[i*2+1], self.dat[i*2+2])

    def query(self, a, b=None, k=0, l=0, r=None):
        if r is None:
            r = self.size
        if b is None:
            b = a + 1
        if r <= a or b <= l:
            return self.default
        if a <= l and r <= b:
            return self.dat[k]
        else:
            lres = self.query(a, b, k*2+1, l, (l+r) >> 1)
            rres = self.query(a, b, k*2+2, (l+r) >> 1, r)
            return self.f(lres, rres)

n = int(input())
a = list(map(int, input().split()))
s = SegmentTree(n, gcd)
for i in range(n):
    s.update(i,a[i])

ans = 1
for i in range(n):
    left = s.query(0,i)
    right = s.query(i+1,n)
    res = gcd(left, right)
    if ans < res:
        ans = res
print(ans)

Submission status: Time Limit Exceeded

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

if N == 2:
  print(max(A))
else:
  for i in reversed(range(0, min(A)+1)):
    cnt = 0
    for a in A:
      if a%i != 0:
        cnt = cnt+1
    if cnt <=1:
      print(i)
      break

Submission status: Time Limit Exceeded

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

def common_divisor(n):
    cd = []
    for i in range(1,int(n**0.5)+2):
        if n%i==0:
            cd.append(i)
            if i != n//i:
                cd.append(n//i)
    return list(set(cd))

cd_dic = {}
for cd in common_divisor(A[0]):
    cd_dic[cd] = 1

for cd in common_divisor(A[1]):
    if cd not in cd_dic:
        cd_dic[cd] = 1
    else:
        cd_dic[cd] = 2
#print(cd_dic)
for k in cd_dic.keys():
    if cd_dic[k] == 2:
        cd_dic[k] = True
    else:
        cd_dic[k] = False
from copy import copy
for a in A[2:]:
    #print(cd_dic)
    d = copy(cd_dic)
    for k,v in cd_dic.items():
        if a%k!=0:
            if v == True:
                d[k] = False
            else:
                d.pop(k)
    cd_dic = copy(d)
ans = max(cd_dic.keys())
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def gcd(a, b):
    if b > a:
        a, b = b, a
    while b:
        a, b = b, a % b
    return a


def main():
    from functools import reduce
    from collections import deque

    n, *a = map(int, open(0).read().split())
    p = deque(a)
    edge1 = reduce(gcd, a[1:])
    edge2 = reduce(gcd, a[:-1])
    full = reduce(gcd, a)
    ans = max([edge1, edge2, full])
    gl = p.pop()
    tmp = gl
    for i in range(n - 2):
        tmp2 = p.pop()
        gr = reduce(gcd, p)
        gl = gcd(gl, tmp)
        tmp = tmp2
        g = gcd(gl, gr)
        ans = max(ans, g)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    from functools import reduce
    from fractions import gcd
    from collections import deque

    n, *a = map(int, open(0).read().split())
    p = deque(a)
    edge1 = reduce(gcd, a[1:])
    edge2 = reduce(gcd, a[:-1])
    full = reduce(gcd, a)
    ans = max([edge1, edge2, full])
    gl = p.pop()
    tmp = gl
    for i in range(n - 2):
        tmp2 = p.pop()
        gr = reduce(gcd, p)
        gl = gcd(gl, tmp)
        tmp = tmp2
        g = gcd(gl, gr)
        ans = max(ans, g)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    from functools import reduce
    from fractions import gcd
    from collections import deque

    n, *a = map(int, open(0).read().split())
    p = deque(a)
    edge1 = reduce(gcd, a[1:])
    edge2 = reduce(gcd, a[:-1])
    full = reduce(gcd, a)
    ans = max([edge1, edge2, full])
    gl = p.pop()
    tmp = gl
    for i in range(n - 2):
        tmp2 = p.pop()
        gr = reduce(gcd, p)
        gl = gcd(gl, tmp)
        tmp = tmp2
        g = gcd(gl, gr)
        ans = max(ans, g)
        
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    from functools import reduce
    from fractions import gcd

    n, *a = map(int, open(0).read().split())
    g = [reduce(gcd, a[:i]+a[i+1:]) for i in range(n)]
    ans = max(g)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#最大公約数、最小公倍数、階乗
import fractions
n = int(input())
a = list(map(int, input().split()))

def calcGCDr(i):
    ans = a[i+1]
    if i==n-2:
        return ans
    tmp = i+1
    while tmp+1<n:
        ans = fractions.gcd(ans,a[tmp+1])
        tmp+=1
    return ans
def calcGCDl(i):
    ans = a[i-1]
    if i==1:
        return ans
    tmp = i-1
    while tmp-1>=0:
        ans = fractions.gcd(ans,a[tmp-1])
        tmp-=1
    return ans

max_ans = 1
for i in range(1,n-1):
    pre_ans = fractions.gcd(calcGCDl(i), calcGCDr(i))
    max_ans = max(pre_ans, max_ans)
pre_ans = calcGCDr(0)
max_ans = max(pre_ans, max_ans)
pre_ans = calcGCDl(n-1)
max_ans = max(pre_ans, max_ans)

print(max_ans)

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
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法では？？
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=ii()
arr=limii()
ans=Counter(make_divisors(arr[0]))
if n>=2:
    for i in range(1,n):
        p=Counter(make_divisors(arr[i]))
        ans=ans+p

cnt=1
for i,j in ans.items():
    if j>=n-1:
        cnt=max(cnt,i)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import copy

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

for i in range(N):
  a = copy.copy(A)
  del a[i]
  ans = a[0]
  for j in range(N-1):
    ans = fractions.gcd(ans, a[j])
  if lans < ans:
    lans = ans
  

print(lans)

Submission status: Time Limit Exceeded

Solution code:
import fractions
import copy

N = int(input())
A = sorted(map(int, input().split()))
lans = 0

for i in range(N):
  a = copy.copy(A)
  del a[i]
  ans = a[0]
  for j in range(N-1):
    ans = fractions.gcd(ans, a[j])
  if lans < ans:
    lans = ans
  

print(lans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

import fractions
from functools import reduce

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

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

N = int(input())

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

ans = 0
for i in range(N):
    new_list = A[:i] + A[i+1:]
    ans = max(ans, gcd_list(new_list))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
import fractions
from functools import reduce

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

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

N = int(input())

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

ans = 0
for i in range(N):
    new_list = A[:i] + A[i+1:]
    ans = max(ans, gcd_list(new_list))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy
class SegmentTree():
    __slots__ = ['sz','func','I','seg']
    def __init__(self, N, func, I):
        self.sz = N
        self.func = copy.deepcopy(func)
        self.I = I
        self.seg = [I for i in range(self.sz * 2)]
 
    def assign(self, k, x):
        self.seg[k + self.sz] = x
 
    def build(self):
        for i in range(self.sz - 1, 0, -1):
            self.seg[i] = self.func(self.seg[2 * i], self.seg[2 * i + 1])
 
    def update(self, k, x):
        k += self.sz
        self.seg[k] = x
        while k > 1:
            k //= 2
            self.seg[k] = self.func(self.seg[2 * k], self.seg[2 * k + 1])
 
    def query(self, a, b):
        L = self.I
        R = self.I
        a += self.sz
        b += self.sz
        while a < b:
            if a % 2 == 1:
                L = self.func(L, self.seg[a])
                a += 1
            if b % 2 == 1:
                b -= 1
                R = self.func(self.seg[b], R)
            a //= 2
            b //= 2
        return self.func(L, R)

def main():
    import sys
    read = sys.stdin.buffer.read
    readline = sys.stdin.buffer.readline
    from fractions import gcd
    N = int(readline())
    m = map(int,read().split())
    seg = SegmentTree(N , gcd , 0)
    for i in range(N):
        seg.assign(i , next(m))
    seg.build()
    ans = 1
    for i in range(N):
        ans = max(ans,gcd(seg.query(0,i) , seg.query(i+1,N)))
    print(ans)

main()