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:
#http://tjkendev.github.io/procon-library/python/range_query/rmq_segment_tree.html
from fractions import *
from math import *
import sys
input = sys.stdin.readline

# a_k の値を x に更新
def update(k, x):
    k += N0-1
    data[k] = x
    while k >= 0:
        k = (k - 1) // 2
        #data[k] = min(data[2*k+1], data[2*k+2])
        data[k] = gcd(data[2 * k + 1], data[2 * k + 2])

def check(k, x):
    k += N0-1
    data2 = data[:]
    data2[k] = x
    while k >= 0:
        k = (k - 1) // 2
        data2[k] = gcd(data2[2 * k + 1], data2[2 * k + 2])
    #print(data2)
    return data2[0]

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

# N: 処理する区間の長さ
N0 = 2**(N-1).bit_length()
INF = a[0]
data = [INF]*(2*N0)
#print(data)
for i in range(N):
    update(i, a[i])

#元のセグ木
ans = 1
for i in range(N-1):
    ans = max(ans, check(i+1, a[i]))
else:
    if N % 2 == 1:
        update(N, a[1])
    ans = max(ans, check(0, a[1]))

print(ans)
#print(data)

Submission status: Time Limit Exceeded

Solution code:

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

def gcd(a,b):
    c=max(a,b)
    d=min(a,b)
    while True:
        if c%d==0:
            return d
        c,d=d,c%d

def gcd_list(A):
    if len(A)==2:
        return gcd(A[0],A[1])
    elif len(A)==1:
        return A[0]
    else:
        return gcd(gcd_list(A[len(A)//2:]),gcd_list(A[:len(A)//2]))
max_gcd=1
for i in range(N):
    max_gcd=max(gcd_list(A[:i]+A[i+1:]),max_gcd)
    
print(max_gcd)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
li=list(map(int,input().split()))
maxi=[]

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

for x in range(N):
    e=m
    for i in range(N):
        if i==x:
            continue
        else:
            e=gcd(li[i],e)
    maxi.append(e)

maxer=max(maxi)
print(maxer)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
li=list(map(int,input().split()))
lis=li
maxi=[]

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

for x in range(N):
    e=gcd(max(li),max(li))
    for i in range(N):
        if i==x:
            continue
        else:
            e=gcd(lis[i],e)
    maxi.append(e)

maxer=max(maxi)
print(maxer)

Submission status: Time Limit Exceeded

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


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

N_MAX = 1 << N.bit_length()

v = [0 for _ in range(2 * N_MAX - 1)]
# Segment Tree

for idx, aa in enumerate(a):
    idx += N_MAX - 1  # Segment Treeの葉ノードのIndexに変換
    v[idx] = aa
    while idx > 0:
        idx = (idx - 1) // 2  # 親ノードのIndexに変換
        v[idx] = gcd(v[idx * 2 + 1], v[idx * 2 + 2])  # ノードの値は、2つの子ノードの値の最大公約数

# print(v)

def query(a, b, k=0, l=0, r=N_MAX):
    # [a,b)の最大公約数
    # print(a, b, k, l, r)
    if r <= a or b <= l: return 0  # 交差しない
    if a <= l and r <= b: return v[k]  # 含まれる
    gcd_l = query(a, b, 2 * k + 1, l, (l + r) // 2)
    gcd_r = query(a, b, 2 * k + 2, (l + r) // 2, r)
    return gcd(gcd_l, gcd_r)


g = 0
for i in range(N):
    g = max(g, gcd(query(0, i), query(i + 1, N)))
print(g)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
 
def gcd(a,b):
  if b==0:
    return a
  else:
  	return gcd(b, a%b)

def gcd_list(X):
  dmy=gcd(X[0],X[1])
  dmy=X[0]*X[1]
  for i in range(len(X)):
    dmy=gcd(dmy,X[i])
  return dmy
 
if N==2:
  print(max(A))
else:
  R=0
  for i in range(N):
    C=A[:i]
    C.extend(A[i+1:])
    #R.append(gcd_list(C))
    #R[i]=gcd_list(C)
    R=max(R,gcd_list(C))
 
  #print(int(max(R)))
  print(int(R))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
 
def gcd(a,b):
  if b==0:
    return a
  else:
  	return gcd(b, a%b)

def gcd_list(X):
  dmy=gcd(X[0],X[1])
  dmy=X[0]*X[1]
  for i in range(len(X)):
    dmy=gcd(dmy,X[i])
  return dmy
 
if N==2:
  print(max(A))
else:
  R=[0]*N
  for i in range(N):
    C=A[:i]
    C.extend(A[i+1:])
    #R.append(gcd_list(C))
    R[i]=gcd_list(C)
 
  print(int(max(R)))

Submission status: Time Limit Exceeded

Solution code:
N = int(input().strip())
A = list(map(int, input().strip().split(' ')))
for i in range(max(A), 1, -1):
  if len([1 for a in A if a%i==0]) >= N-1:
    print(i)
    exit()
print(1)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from math import sqrt
inpl = lambda: list(map(int,input().split()))

def primes(N):
    P = []
    for i in range(2,N):
        sieve = 1
        for p in P:
            if p*p > i:
                break
            elif i % p == 0:
                sieve = 0
                break
        if sieve == 0:
            continue
        else:
            P.append(i)
    return P

def factorize(n, P=None):
    if P is None:
        P = primes(int(sqrt(n))+1)
    factor = []
    power = []
    for p in P:
        if p * p > n:
            break
        elif n % p == 0:
            k = 0
            while n % p == 0:
                n //= p
                k += 1
            factor.append(p)
            power.append(k)
    if n > 1:
        factor.append(n)
        power.append(1)
    return factor, power

class Counter:
    def __init__(self, start=0):
        self.index = start-1

    def __call__(self):
        self.index += 1
        return self.index

N = int(input())
A = inpl()

P_MAX = 32000
infty = 100
P = primes(P_MAX)

np = defaultdict(Counter())
Np = 0

lowest_powers = []
bottle_necks = []
primelist = []
all_factors = set()
for n in range(N):
    primes, powers = factorize(A[n],P)
    pp = [0]*Np
    for i in range(len(primes)):
        k = np[primes[i]]
        if k >= Np:
            Np += 1
            pp.append(powers[i])
            primelist.append(primes[i])
            if n > 1:
                lowest_powers.append([0,0])
                bottle_necks.append(-1)
            elif n == 1:
                lowest_powers.append([0,infty])
                bottle_necks.append(0)
            else:
                lowest_powers.append([infty,infty])
                bottle_necks.append(-1)
        else:
            pp[k] = powers[i]
    for k in range(Np):
        lowest = lowest_powers[k]
        if pp[k] == lowest[0]:
            lowest[1] = lowest[0]
            bottle_necks[k] = -1
        elif pp[k] < lowest[0]:
            lowest[1] = lowest[0]
            lowest[0] = pp[k]
            bottle_necks[k] = n
        elif pp[k] < lowest[1]:
            lowest[1] = pp[k]
 
bottleneck_factors = [ [] for _ in range(N) ]
for k in range(Np):
    n = bottle_necks[k]
    if n >= 0:
        lowest = lowest_powers[k]
        bottleneck_factors[n].append([primelist[k],lowest[1]-lowest[0]])
best_n = -1
largest_f = 1
for n in range(N):
    f = 1
    for pp in bottleneck_factors[n]:
        f *= pp[0]**pp[1]
    if f > largest_f:
        largest_f = f
        best_n = n

ans = 1
pn = [ s[0] for s in bottleneck_factors[best_n] ]
for k in range(Np):
    p = primelist[k]
    if bottle_necks[k] == best_n:
        ans *= p**lowest_powers[k][1]
    else:
        ans *= p**lowest_powers[k][0]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from itertools import combinations, chain

def gcd_all(numbers):
  gcds = numbers.__next__()
  for x in numbers:
    gcds = gcd(gcds, x)
  return gcds

def main():
  N = int(input().strip())
  A = list(map(int, input().strip().split()))
  
  gcds = []
  for i in range(N):
    gcds.append(gcd_all(chain(A[:i], A[i+1:])))
  
  print(max(gcds))
 
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
def gcd(a, b):
    while True:
        bigger=max(a,b)
        smaller=min(a,b)
        mod=bigger%smaller
        if mod==0:
            return smaller
        else:
            return gcd(smaller, mod)

n = int(input())
a = list(map(int, input().split()))
ans=0
for remove in range(n):
    if remove==0:
        total_gcd=a[1]
    else:
        total_gcd=a[0]
    for i in range(n):
        if i==remove:
            continue
        total_gcd=gcd(total_gcd, a[i])
    ans = max(ans, total_gcd)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import math
from collections import Counter
import collections
INF = float("inf")


import math


class SegmentTree(object):
    # セグメント木

    def __init__(self, N):
        """サイズNの配列を元にしたセグメント木
        """
        # 単位元
        self.idelem = 0
        # 比較関数
        self.func = GCD

        self.N0 = 2**(math.ceil(math.log2(N)))
        self.tree = [self.idelem]*(2*self.N0)

    def update(self, k, x):
        """k番目の値をxに更新
        """
        k += self.N0-1
        self.tree[k] = x
        while k > 1:
            k = (k-1)//2
            self.tree[k] = self.func(self.tree[2*k+1], self.tree[2*k+2])

    def query(self, l, r):
        """区間[l, r)の最小値
        """
        l += self.N0-1
        r += self.N0-1
        s = self.idelem
        while l < r:
            if r % 2 == 0:
                r -= 1
                s = self.func(s, self.tree[r])

            if l % 2 == 0:
                s = self.func(s, self.tree[l])
                l += 1
            l //= 2
            r //= 2
        return s

    def __str__(self):
        return str(self.tree[self.N0-1:-1])


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


def GCDs(*a):
    # リストで入力しないよう注意。
    if len(a) == 0:
        return -1  # エラー

    if len(a) == 1:
        return a[0]

    res = a[0]
    for i in range(1, len(a)):
        res = GCD(res, a[i])
    return res


def solve(N: int, A: "List[int]"):

    st = SegmentTree(N)
    for i, a in enumerate(A):
        st.update(i, a)

    buf = []
    for i in range(N):
        buf.append(GCD(st.query(0, i), st.query(i+1, N)))
    print(max(buf))

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


class SegmentTree():
    '''
    RMQ　の実装
    '''

    def __init__(self, n, inf=float('inf')):
        '''
        木の高さhとすると,
        n:h-1までのノード数。h段目のノードにアクセスするために使う。
        data:ノード。
        parent:k->child k*2+1とk*2+2
        '''
        self.n = 2**(n-1).bit_length()
        self.inf = inf
        self.data = [inf]*(2*self.n)

    def set(self, k, v):
        '''
        あたいの初期化
        '''
        self.data[k+self.n-1] = v

    def build(self):
        for k in reversed(range(self.n-1)):
            self.data[k] = gcd(self.data[k*2+1], self.data[k*2+2])

    def update(self, k, a):
        '''
        list[k]=aに更新する。
        更新ぶんをrootまで更新
        '''
        k += self.n-1
        self.data[k] = a

        while k > 0:
            k = (k-1)//2
            self.data[k] = gcd(self.data[k*2+1], self.data[k*2+2])

    def query(self, l, r):
        '''
        [l,r)のminを求める
        '''
        L = l+self.n
        R = r+self.n
        ret = self.inf
        while L < R:
            if R & 1:
                R -= 1
                ret = gcd(ret, self.data[R-1])
            if L & 1:
                ret = gcd(ret, self.data[L-1])
                L += 1
            L >>= 1
            R >>= 1
        return ret


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


N = int(input())
L = list(map(int, input().split()))
T = SegmentTree(N, 0)
# for i, l in enumerate(L):
#    T.set(i, l)
# T.build()
for i, l in enumerate(L):
    T.update(i, l)

ans = 0
for i in range(N):
    l = i
    r = i+1
    ans = max(ans, gcd(T.query(0, l), T.query(r, N)))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
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(reverse=True)
    return divisors


N = int(input())
A = sorted([int(x) for x in input().split()])
ans = 1

for i in range(2):
    divisors = make_divisors(A[i])

    for divisor in divisors:
        indivisible_count = 0
        for j in range(N):
            if A[j] % divisor != 0:
                indivisible_count += 1

        if indivisible_count <= 1 and divisor > ans:
            ans = divisor

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))

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

ans = 0
B = []
for i in range(N):
    b = [A[i],A[i-1]]
    B.append(gcd(max(b),min(b)))
for i in range(-1,-N-1,-1):
    if N == 2:
        ans = max(A)
        break
    while 1:
        C = B.copy()
        C.pop(i)
        C.pop(i-(-1))
        c = []
        for j in range(len(C)):
            c.append(gcd(C[j],C[j-1]))
        if c == C:
            ans = max(ans,max(c))
            break
        C = c
print(ans)

Submission status: Time Limit Exceeded

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

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

def gcd_list(X):
  dmy=gcd(X[0],X[1])
  dmy=X[0]*X[1]
  for i in range(len(X)):
    dmy=gcd(dmy,X[i])
  return dmy

if N==2:
  print(max(A))
else:
  R=[]
  for i in range(N):
    C=A[:i]
    C.extend(A[i+1:])
    R.append(gcd_list(C))

  print(int(max(R)))

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
N = int(input())
A = list(map(int,input().split()))
S = []
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)
for i in range(N):
  B = A.copy()
  del B[i]
  m = gcd(*B)
  S.append(m)
print(max(S))

Submission status: Time Limit Exceeded

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

def reduce_gcd(lst):
    ans = lst.pop(0)
    for i in lst:
        ans = gcd(ans,i)
    return ans

def other_lst(lst,index):
    return [x for i,x in enumerate(lst) if not i == index]

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

print(max([reduce_gcd(other_lst(A,i)) for i in range(len(A)) ]))

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd
from functools import reduce

_ = input()
A = list(map(int, input().split()))
#print("A", A)

result = 1
for i, a in enumerate(A):
    #print("ignore", a)
    A.pop(i)
    #print("current A", A)

    result = max(result, reduce(gcd, A))
    
    A.insert(i, a)
    #print("org A", A)
    #print("---")

print(result)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

# n が 2なら 大きい方を出力して終わり

#def euclid(m, n):
#  if n == 0:
#    return m
#  new_m = n
#  new_n = m % n
#  return euclid(new_m, new_n)
#
#def array_euclid(numbers):
#  n = len(numbers)
#  v = numbers[n-1] % numbers[n-2]
#  if v == 0:
#    if n == 2:
#      return numbers[0]
#    
#    numbers.pop()
#    return array_euclid(numbers)
#
#  numbers[n-1] = numbers[n-2]
#  numbers[n-2] = v
#  return array_euclid(numbers)

#print(array_euclid(a))

#if n == 2:
#  print(euclid(a[0], a[1]))
#if n > 2:
#  max_list = []
#  max_list.append(array_euclid(a[:]))
#  for i in range(n):
#    a2 = a[:]
#    a2.pop(i)
#    max_list.append(array_euclid(a2))
#  print(max(max_list))
#


# n が3になるまでやって、大きい方が答え？

#for i in range(n-1):


#print(euclid(a[0],a[1]))


# 最大公約数を求めるには math.gcd を使う

from fractions import gcd
from functools import reduce

def gcd_list(numbers):
  if len(numbers) == 0: return 0
  return reduce(gcd, numbers)

#print(gcd_list([0, 100]))

max_list = []
max_list.append(gcd_list(a))
#gcd_max = gcd_list(a)
for i in range(n):
  a2 = [gcd_list(a[0:i]), gcd_list(a[i+1:])]
  #a2.pop(i)
  max_list.append(gcd(gcd_list(a[0:i]), gcd_list(a[i+1:])))
print(max(max_list))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

# n が 2なら 大きい方を出力して終わり

#def euclid(m, n):
#  if n == 0:
#    return m
#  new_m = n
#  new_n = m % n
#  return euclid(new_m, new_n)
#
#def array_euclid(numbers):
#  n = len(numbers)
#  v = numbers[n-1] % numbers[n-2]
#  if v == 0:
#    if n == 2:
#      return numbers[0]
#    
#    numbers.pop()
#    return array_euclid(numbers)
#
#  numbers[n-1] = numbers[n-2]
#  numbers[n-2] = v
#  return array_euclid(numbers)

#print(array_euclid(a))

#if n == 2:
#  print(euclid(a[0], a[1]))
#if n > 2:
#  max_list = []
#  max_list.append(array_euclid(a[:]))
#  for i in range(n):
#    a2 = a[:]
#    a2.pop(i)
#    max_list.append(array_euclid(a2))
#  print(max(max_list))
#


# n が3になるまでやって、大きい方が答え？

#for i in range(n-1):


#print(euclid(a[0],a[1]))


# 最大公約数を求めるには math.gcd を使う

from fractions import gcd
from functools import reduce

def gcd_list(numbers):
  if len(numbers) == 0: return 0
  return reduce(gcd, numbers)

#print(gcd_list([0, 100]))

#max_list = []
#max_list.append(gcd_list(a))
gcd_max = gcd_list(a)
for i in range(n):
  a2 = [gcd_list(a[0:i]), gcd_list(a[i+1:])]
  #a2.pop(i)
  gcd_ans = gcd_list(a2)
  if gcd_max < gcd_ans: gcd_max = gcd_ans
print(gcd_max)

Submission status: Time Limit Exceeded

Solution code:
import math

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

def main():
  N = int(input().strip())
  A = list(map(int, input().strip().split()))
  divs_dict = dict()
  for a in A:
    divs = make_divisors(a)
    for d in divs:
      if d in divs_dict.keys():
        divs_dict[d] += 1
      else:
        divs_dict[d] = 1
  
  result_divs = []
  for k in divs_dict.keys():
    if divs_dict[k] >= N-1:
      result_divs.append(k)
  print(sorted(result_divs)[-1])

if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
# /usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math


def gcd_ab(a,b):
  while a != b:
    if a > b:
      a,b = [b,a]
    if a == 0:
      return b
    b = b-a
  return a


if __name__ == "__main__": 
  N = int(input())
  An = list(map(int, sys.stdin.readline().rstrip().split()))
  cum = [[0,0] for i in range(N+2)]

  for i in range(N):
    cum[i+1][0] = gcd_ab(cum[i][0], An[i])
    cum[N-i][1] = gcd_ab(cum[N-i+1][1], An[N-i-1])

  ans = [0 for i in range(N)]
  for i in range(1,N+1):
    ans[i-1] = gcd_ab(cum[i-1][0], cum[i+1][1])
  print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
# /usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math



def gcd_ab(a,b):
  while a != b:
    if a > b:
      a,b = [b,a]
    if a == 0:
      return b
    b = b-a
  return a


if __name__ == "__main__": 
  N = int(input())
  An = list(map(int, sys.stdin.readline().rstrip().split()))
  cum = [[0,0] for i in range(N+2)]

  for i in range(N):
    cum[i+1][0] = gcd_ab(cum[i][0], An[i])
    cum[N-i][1] = gcd_ab(cum[N-i+1][1], An[N-i-1])

  ans = 0
  for i in range(1,N+1):
    ans = max(ans, gcd_ab(cum[i-1][0], cum[i+1][1]))
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

# n が 2なら 大きい方を出力して終わり

#def euclid(m, n):
#  if n == 0:
#    return m
#  new_m = n
#  new_n = m % n
#  return euclid(new_m, new_n)
#
#def array_euclid(numbers):
#  n = len(numbers)
#  v = numbers[n-1] % numbers[n-2]
#  if v == 0:
#    if n == 2:
#      return numbers[0]
#    
#    numbers.pop()
#    return array_euclid(numbers)
#
#  numbers[n-1] = numbers[n-2]
#  numbers[n-2] = v
#  return array_euclid(numbers)

#print(array_euclid(a))

#if n == 2:
#  print(euclid(a[0], a[1]))
#if n > 2:
#  max_list = []
#  max_list.append(array_euclid(a[:]))
#  for i in range(n):
#    a2 = a[:]
#    a2.pop(i)
#    max_list.append(array_euclid(a2))
#  print(max(max_list))
#


# n が3になるまでやって、大きい方が答え？

#for i in range(n-1):


#print(euclid(a[0],a[1]))


# 最大公約数を求めるには math.gcd を使う

from fractions import gcd
from functools import reduce

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

#max_list = []
#max_list.append(gcd_list(a))
gcd_max = gcd_list(a)
for i in range(n):
  a2 = a[0:i] + a[i+1:]
  #a2.pop(i)
  gcd_ans = gcd_list(a2)
  if gcd_max < gcd_ans: gcd_max = gcd_ans
print(gcd_max)

Submission status: Time Limit Exceeded

Solution code:
# /usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math



def gcd_ab(a,b):
  while a != b:
    if a > b:
      a,b = [b,a]
    if a == 0:
      return b
    b = b-a
  return a


if __name__ == "__main__": 
  N = int(input())
  An = list(map(int, sys.stdin.readline().rstrip().split()))
  cuml = [An[0] for i in range(N)]
  cumr = [An[-1] for i in range(N)]

  for i in range(1, N):
    cuml[i] = gcd_ab(cuml[i-1], An[i])
    cumr[N-i-1] = gcd_ab(cumr[N-i], An[N-i-1])

  cuml = [0] + cuml + [0]
  cumr = [0] + cumr + [0]

  ans = 0
  for i in range(1,N+1):
    ans = max(ans, gcd_ab(cuml[i-1], cumr[i+1]))
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
def gcd(a, b):
    a, b = min(a, b), max(a, b)
    if b == 0:
        return 0
    elif a == 0:
        return b
    else:
        return gcd(a, b%a)
def ex_gcd(list, index):
    if index == 0:
        ans = list[1]
    else:
        ans = list[0]
    for i in range(1, len(list)):
        if i != index:
            ans = gcd(ans, list[i])
    return ans
N = int(input())
A = [int(x) for x in input().split()]
if N == 2:
    print(max(A[0], A[1]))
else:
    tmp = 0
    for i in range(N):
        if tmp < ex_gcd(A, i):
            tmp = ex_gcd(A, i)
    print(tmp)

Submission status: Time Limit Exceeded

Solution code:
# /usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math



def gcd_ab(a,b):
  while a != b:
    a,b = sorted([a,b])
    if a == 0:
      return b
    b = b-a
  return a


if __name__ == "__main__": 
  N = int(input())
  An = list(map(int, sys.stdin.readline().rstrip().split()))
  cuml = An.copy()
  cumr = An.copy()

  for i in range(1, N):
    cuml[i] = gcd_ab(cuml[i-1], An[i])
    cumr[N-i-1] = gcd_ab(cumr[N-i], An[N-i-1])

  cuml = [0] + cuml + [0]
  cumr = [0] + cumr + [0]

  ans = 0
  for i in range(1,N+1):
    ans = max(ans, gcd_ab(cuml[i-1], cumr[i+1]))
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

# n が 2なら 大きい方を出力して終わり

#def euclid(m, n):
#  if n == 0:
#    return m
#  new_m = n
#  new_n = m % n
#  return euclid(new_m, new_n)
#
#def array_euclid(numbers):
#  n = len(numbers)
#  v = numbers[n-1] % numbers[n-2]
#  if v == 0:
#    if n == 2:
#      return numbers[0]
#    
#    numbers.pop()
#    return array_euclid(numbers)
#
#  numbers[n-1] = numbers[n-2]
#  numbers[n-2] = v
#  return array_euclid(numbers)

#print(array_euclid(a))

#if n == 2:
#  print(euclid(a[0], a[1]))
#if n > 2:
#  max_list = []
#  max_list.append(array_euclid(a[:]))
#  for i in range(n):
#    a2 = a[:]
#    a2.pop(i)
#    max_list.append(array_euclid(a2))
#  print(max(max_list))
#


# n が3になるまでやって、大きい方が答え？

#for i in range(n-1):


#print(euclid(a[0],a[1]))


# 最大公約数を求めるには math.gcd を使う

from fractions import gcd
from functools import reduce

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

max_list = []
max_list.append(gcd_list(a))
for i in range(n):
  a2 = a[0:i] + a[i+1:]
  #a2.pop(i)
  max_list.append(gcd_list(a2))
print(max(max_list))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-

# abc125_c
import fractions, functools, itertools

# input
n = int(input())
A = [int(s) for s in input().split()]
# get ans
x = A[0]
y = A[1]
def divisors(n):
     for i in range(1, n+1):
        if n%i == 0:
            yield i
ds = set(divisors(x)) | set(divisors(y))
def condition(d):
    not_div = (a%d != 0 for a in A)
    not_div = (1 if b else 0 for b in not_div)
    return sum(not_div) <= 1
ans = max(filter(condition, ds))
# output
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

# n が 2なら 大きい方を出力して終わり

def euclid(m, n):
  if n == 0:
    return m
  new_m = n
  new_n = m % n
  return euclid(new_m, new_n)

def array_euclid(numbers):
  n = len(numbers)
  v = numbers[n-1] % numbers[n-2]
  if v == 0:
    if n == 2:
      return numbers[0]
    
    numbers.pop()
    return array_euclid(numbers)

  numbers[n-1] = numbers[n-2]
  numbers[n-2] = v
  return array_euclid(numbers)

#print(array_euclid(a))

#if n == 2:
#  print(euclid(a[0], a[1]))
#if n > 2:
#  max_list = []
#  max_list.append(array_euclid(a[:]))
#  for i in range(n):
#    a2 = a[:]
#    a2.pop(i)
#    max_list.append(array_euclid(a2))
#  print(max(max_list))
#


# n が3になるまでやって、大きい方が答え？

#for i in range(n-1):


#print(euclid(a[0],a[1]))


# 最大公約数を求めるには math.gcd を使う

from fractions import gcd
from functools import reduce

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

max_list = []
max_list.append(gcd_list(a))
for i in range(n):
  a2 = a[:]
  a2.pop(i)
  max_list.append(gcd_list(a2))
print(max(max_list))

Submission status: Time Limit Exceeded

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


gcds = []
for i in range(len(A)):
    A[:i] + A[i+1:]
    gcds.append(reduce(gcd, A[:i]+A[i+1:]))

print(max(gcds))

Submission status: Time Limit Exceeded

Solution code:
import copy
N = int(input())
A = list(map(int,input().split()))
def maxgcd(A,N):
    M=[]
    A.insert(0,0)#A0の作成
    for j in range(N):
        L = [0]*(j+1+1)#L[i]
        R = [0]*(N-j+1)#R[i]
        #Lの計算
        for i in range(j+1):
            L[i+1] = gcd(L[i],A[i])
        for i in reversed(range(j+1,N+1)):
            R[i-j-1] = gcd(R[i-j],A[i])
        M.append(gcd(L[j+1],R[1]))
    return max(M)
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)

print(maxgcd(A,N))

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 gcd_of_arr(arr):
    cd = arr[0]
    for i in range(1,len(arr)):
        if cd==1:
            return cd
        cd = gcd(arr[i], cd)
    return cd

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import functools
import fractions
N = int(input())
A_list = list(map(int, input().split()))
ans = 0

for i in range(1,N+1):
    x = A_list[-i]
    del A_list[-i]
    gcd = functools.reduce(fractions.gcd, A_list)
    ans = max(gcd,ans)
    A_list.append(x)
print(ans)

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 gcd_of_arr(arr):
    cd = arr[0]
    for i in range(1,len(arr)):
        if cd==1:
            return cd
        cd = gcd(arr[i], cd)
    return cd

B = [0]*N
for i in range(N):
    tmp = A[0:i] + A[i+1:N]
    B[i] = gcd_of_arr(tmp)

print(max(B))

Submission status: Time Limit Exceeded

Solution code:
import functools
import fractions

n=int(input())
str=input().split(" ")
value = [int(s) for s in str]
tmp=value[0]
value[0]=value[1]
_max=functools.reduce(fractions.gcd, value)
value[0]=tmp
for i in range(1,n):
  tmp=value[i]
  value[i]=value[i-1]
  gcd = functools.reduce(fractions.gcd, value)
  if gcd > _max:
    _max=gcd
  value[i]=tmp

print(_max)

Submission status: Time Limit Exceeded

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

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

Solution code:
import functools

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

l_gcd = []

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

for i,j in enumerate(A):
    B = j
    A.pop(i)
    l_gcd.extend([functools.reduce(gcd,A)])
    A.insert(i,B)

print(max(l_gcd))

Submission status: Time Limit Exceeded

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

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


def gcd(data):
    if len(data) < 2:
        return data[0]
    return reduce(lambda x, y: fractions.gcd(x, y), data)

result = max(gcd(x) for x in combinations(A, n-1))

print(result)

Submission status: Time Limit Exceeded

Solution code:
import functools

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

l_gcd = []

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

for i in range(len(A)):
    B = A[i]
    A.pop(i)
    l_gcd.extend([functools.reduce(gcd,A)])
    A.insert(i,B)

print(max(l_gcd))

Submission status: Time Limit Exceeded

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


def gcd(a: int, b: int) -> int:
    while a % b != 0:
        r = a % b
        a = b
        b = r
    return b


L = [A[0]]
R = [A[-1]]
for i in range(1, N):
    L.append(gcd(L[i-1], A[i]))
    R.insert(0, gcd(R[0], A[N-1-i]))


for i in range(N):
    if i == 0:
        ans = R[i+1]
    elif i == N-1:
        ans = max(ans, L[i-1])
    else:
        ans = max(ans, gcd(L[i-1], R[i+1]))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
count = 0
b = 0
A.sort()

for i in range(A[1]-1):

    count = 0

    for j in range(N):
        if A[j] % (A[1] - i) !=0:
            count += 1
        
    if count == 1:
        b = A[1] - i
        break
    else:
        continue

if b == 0:
    print(A[0])
else :
    print(b)