Problem p03061 (python)

Fixed submission:


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

def gcd_all(lst):
    g = gcd(lst[0], lst[1])
    for e in lst[2:]:
        g = gcd(g, e)
    return g


def submit():
    n = int(input())
    alist = list(map(int, input().split()))

    if n == 2:
        print(max(alist))
    else:
        # 左から計算したgcd列
        gcd_left = [alist[0]]
        for a in alist[1:]:
            gcd_left.append(gcd(gcd_left[-1], a))
        
        # 右から計算したgcd列
        gcd_right = [alist[-1]]
        for a in reversed(alist[:-1]):
            gcd_right.append(gcd(gcd_right[-1], a))
        gcd_right = gcd_right[::-1]

        gcd_max = 0
        for i in range(n):
            if i == 0:
                gcd_i = gcd_right[i + 1]
            elif i == n - 1:
                gcd_i = gcd_left[i - 1]
            else:
                gcd_i = gcd(gcd_left[i - 1], gcd_right[i + 1])

            if gcd_max < gcd_i:
                gcd_max = gcd_i

        print(gcd_max)


if __name__ == "__main__":
    submit()

Buggy submission (verdict: Runtime Error):
import numpy as np

def read_input():
    n = int(input())
    alist = list(map(int, input().split()))

    return n, alist


def split_a(n, alist):
    curr = 0
    for i in range(n):
        ar1 = alist[:i]
        ar2 = alist[i+1:]

        temp = mygcd(ar1, ar2)
        if curr < temp:
            curr = temp

    return curr


def mygcd(ar1, ar2):
    if not ar1:
        return np.gcd.reduce(ar2)

    if not ar2:
        return np.gcd.reduce(ar1)

    return np.gcd.reduce(ar1 + ar2)

if __name__ == '__main__':
    n, alist = read_input()
    print(split_a(n, alist))

Buggy submission (verdict: Runtime Error):
import numpy as np

def read_input():
    n = int(input())
    alist = list(map(int, input().split()))

    return n, alist


def split_a(n, alist):
    curr = 0
    for i in range(n):
        ar1 = alist[:i]
        ar2 = alist[i+1:]

        temp = mygcd(ar1, ar2)
        if curr < temp:
            curr = temp

    print(curr)


def mygcd(ar1, ar2):
    if not ar1:
        return np.gcd.reduce(ar2)

    if not ar2:
        return np.gcd.reduce(ar1)

    return np.gcd.reduce(ar1 + ar2)

if __name__ == '__main__':
    n, alist = read_input()
    split_a(n, alist)

Buggy submission (verdict: Runtime Error):

import numpy as np

def read_input():
    n = int(input())
    alist = list(map(int, input().split()))

    return n, alist


def split_a(alist):
    result = []
    for i in range(len(alist)):
        result.append(alist[:i] + alist[i+1:])

    return result

gcd_memo = {}

def gcd_with_memo(ar):
    if len(ar) == 1:
        return ar[0]

    if len(ar) == 2:
        a = min(ar[0], ar[1])
        b = max(ar[0], ar[1])
        if (a,b) in gcd_memo.keys():
            return gcd_memo[(a,b)]

        g = np.gcd(a, b)
        gcd_memo[(a, b)] = g
        return g

    return gcd_with_memo(gcd_with_memo(ar[:2]), gcd_with_memo(ar[2:]))



if __name__ == '__main__':
    n, a = read_input()
    cand = split_a(a)

    maxgcd = 0
    for c in cand:
        test = gcd_with_memo(c)
        if test > maxgcd:
            maxgcd = test


    print(maxgcd)