input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
import copy n = int(eval(input())) a = list(map(int, input().split())) def gcd(x,y): while y: x,y = y, x % y return x gcd_left = [0] gcd_rigth = [0] gcd_list = [] for i in range(n): s = gcd(gcd_left[i], a[i]) gcd_left.append(s) for i in range(n): t = gcd(gcd_rigth[i], a[-i-1]) gcd_rigth.append(t) for i in range(n): u = copy.copy(a) del u[i] v = gcd(gcd_left[i], gcd_rigth[n-1-i]) gcd_list.append(v) print((max(gcd_list)))
n = int(eval(input())) a = list(map(int, input().split())) def gcd(x,y): while y: x,y = y, x % y return x gcd_left = [0] gcd_rigth = [0] gcd_list = [] for i in range(n): s = gcd(gcd_left[i], a[i]) gcd_left.append(s) for i in range(n): t = gcd(gcd_rigth[i], a[-i-1]) gcd_rigth.append(t) for i in range(n): v = gcd(gcd_left[i], gcd_rigth[n-1-i]) gcd_list.append(v) print((max(gcd_list)))
p03061
from functools import reduce def gcd(a,b): if a<b: a,b = b,a while b: a,b = b,a%b return a N = int(eval(input())) A = list(map(int,input().split())) gcd_max = 0 for i in range(N): B = A.copy() B[i:i+1] = [] gcd_max = max(gcd_max,reduce(gcd,B)) print(gcd_max)
def gcd(x,y): while y != 0: x,y = y,x%y return x n = int(eval(input())) a = list(map(int,input().split())) L,R = [0],[0] for i in range(1,n+1): #L[i]は左i個,R[i]は右i個 L.append(gcd(L[-1],a[i-1])) R.append(gcd(R[-1],a[n-i])) print((max(gcd(L[i],R[n-i-1]) for i in range(n))))
p03061
#!/usr/bin/env python # -*- coding: utf-8 -*- def calc(a, b): if a == 1 or b == 1: return 1 while a != 0 and b != 0: if a < b: b %= a else: a %= b return a if a != 0 else b def main(): input() a = list(map(int, input().split())) p = [a[0], a[1], calc(a[0], a[1])] for v in a[2:]: for j in range(len(p) - 1): p[j] = calc(p[j], v) p.append(calc(p[-1], v)) print(max(p), flush=True) if __name__ == '__main__': main()
#!/usr/bin/env python # -*- coding: utf-8 -*- def cd(a, b): if a == 1 or b == 1: return 1 while a != 0 and b != 0: if a < b: b %= a else: a %= b return a if a != 0 else b def main(): input() prevs = list(map(int, input().split())) posts = prevs[::-1] for i in range(1, len(prevs)): prevs[i] = cd(prevs[i - 1], prevs[i]) posts[i] = cd(posts[i - 1], posts[i]) posts = posts[::-1] gcd = max(prevs[-2], posts[1]) for i in range(1, len(prevs) - 1): gcd = max(cd(prevs[i - 1], posts[i + 1]), gcd) print(gcd, flush=True) if __name__ == '__main__': main()
p03061
# Segment tree (GCD) from math import gcd class SegmentTree: _f = None _data = None _offset = None _size = None def __init__(self, size, f): self._f = f self._size = size t = 1 while t < size: t *= 2 self._offset = t - 1 self._data = [0] * (t * 2 - 1) def build(self, iterable): f = self._f data = self._data data[self._offset:self._offset + self._size] = iterable for i in range(self._offset - 1, -1, -1): data[i] = f(data[i * 2 + 1], data[i * 2 + 2]) def query(self, start, stop): def iter_segments(data, l, r): while l < r: if l & 1 == 0: yield data[l] if r & 1 == 0: yield data[r - 1] l = l // 2 r = (r - 1) // 2 f = self._f it = iter_segments(self._data, start + self._offset, stop + self._offset) result = next(it) for e in it: result = f(result, e) return result N = int(eval(input())) A = list(map(int, input().split())) st = SegmentTree(N, gcd) st.build(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)
from math import gcd class SegmentTree: def __init__(self, size, op, e): self._op = op self._e = e self._size = size t = 1 while t < size: t *= 2 self._offset = t - 1 self._data = [e] * (t * 2 - 1) def __getitem__(self, key): return self._data[self._offset + key] def __setitem__(self, key, value): op = self._op data = self._data i = self._offset + key data[i] = value while i >= 1: i = (i - 1) // 2 data[i] = op(data[i * 2 + 1], data[i * 2 + 2]) def build(self, iterable): op = self._op data = self._data data[self._offset:self._offset + self._size] = iterable for i in range(self._offset - 1, -1, -1): data[i] = op(data[i * 2 + 1], data[i * 2 + 2]) def query(self, start, stop): def iter_segments(data, l, r): while l < r: if l & 1 == 0: yield data[l] if r & 1 == 0: yield data[r - 1] l = l // 2 r = (r - 1) // 2 op = self._op it = iter_segments(self._data, start + self._offset, stop + self._offset) result = self._e for v in it: result = op(result, v) return result N = int(eval(input())) A = list(map(int, input().split())) st = SegmentTree(N, gcd, 0) st.build(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)
p03061
def gcd(a, b): while a: a, b = b % a, a return b from functools import reduce def g(): n = int(eval(input())) vs = list(map(int, input().split())) mr = 0 for i in range(len(vs)): r = reduce(gcd, vs[:i]+vs[i+1:]) mr = max(mr, r) return mr print((g()))
from functools import reduce def gcd(a, b): while a: a, b = b % a, a return b def g(): from functools import reduce n = int(eval(input())) vs = list(map(int, input().split())) while True: n = len(vs) if n < 10: mrr = 0 for i, v in enumerate(vs): rr = reduce(gcd, vs[:i]+vs[i+1:]) mrr = max(rr, mrr) return mrr v1 = vs[:n//2] v2 = vs[n//2:] r1 = reduce(gcd, v1) r2 = reduce(gcd, v2) if r1 == r2: return r1 if r1 > r2: vs = v2 else: vs = v1 print((g()))
p03061
N = int(eval(input())) numbers = list(map(int, input().split())) def gcd(a, b): remainder = a % b while (remainder > 0): a = b b = remainder remainder = a % b return b gcd_forward = [] gcd_all = numbers[0] for i in range(0, N - 1): gcd_all = gcd(gcd_all, numbers[i]) gcd_forward.append(gcd_all) gcd_backward = [] gcd_all = numbers[-1] for i in range(N - 1, 0, -1): gcd_all = gcd(gcd_all, numbers[i]) gcd_backward.insert(0, gcd_all) gcd_forward.insert(0, gcd_backward[0]) gcd_backward.append(gcd_forward[-1]) gcd_max = 1 for i in range(N): gcd_max = max(gcd_max, gcd(gcd_forward[i], gcd_backward[i])) print(gcd_max)
N = int(eval(input())) numbers = list(map(int, input().split())) def gcd(a, b): r = a % b while (r > 0): a = b b = r r = a % b return b gcd_forward = [numbers[0]] for i in range(1, N - 1): gcd_all = gcd(gcd_forward[-1], numbers[i]) gcd_forward.append(gcd_all) gcd_backward = [numbers[-1]] for i in range(N - 2, 0, -1): gcd_all = gcd(gcd_backward[-1], numbers[i]) gcd_backward.append(gcd_all) gcd_max = 1 for i in range(N): gcd_temp = 1 if (i == 0): gcd_temp = gcd_backward[-1] elif (i == N - 1): gcd_temp = gcd_forward[-1] else: gcd_temp = gcd(gcd_forward[i - 1], gcd_backward[N - 2 - i]) gcd_max = max(gcd_max, gcd_temp) print(gcd_max)
p03061
def gcd(a, b): while 1: a, b = b, a % b if b == 0: return a from functools import reduce n = int(eval(input())) a_s = list(map(int, input().split())) mx = 0 for i in range(n): b = a_s[:i] + a_s[i + 1:] g = reduce(gcd, b) if g > mx: mx = g print(mx)
import sys sys.setrecursionlimit(10**6) def Lgcd(i): if i == 0: return 0 if i == 1: return a_s[0] if i in Lmemo: return Lmemo[i] re = gcd(Lgcd(i - 1), a_s[i - 1]) Lmemo[i] = re return re def Rgcd(i): if i == n - 1: return 0 if i == n - 2: return a_s[-1] if i in Rmemo: return Rmemo[i] re = gcd(Rgcd(i + 1), a_s[i + 1]) Rmemo[i] = re return re def gcd(a, b): if a * b == 0: return a + b while 1: a, b = b, a % b if b == 0: return a Lmemo = {} Rmemo = {} n = int(eval(input())) a_s = list(map(int, input().split())) mx = 0 for i in range(n): g = gcd(Lgcd(i),Rgcd(i)) if g > mx: mx = g print(mx)
p03061
import sys sys.setrecursionlimit(10**6) def Lgcd(i): if i == 0: return 0 if i == 1: return a_s[0] if i in Lmemo: return Lmemo[i] re = gcd(Lgcd(i - 1), a_s[i - 1]) Lmemo[i] = re return re def Rgcd(i): if i == n - 1: return 0 if i == n - 2: return a_s[-1] if i in Rmemo: return Rmemo[i] re = gcd(Rgcd(i + 1), a_s[i + 1]) Rmemo[i] = re return re def gcd(a, b): if a * b == 0: return a + b while 1: a, b = b, a % b if b == 0: return a Lmemo = {} Rmemo = {} n = int(eval(input())) a_s = list(map(int, input().split())) mx = 0 for i in range(n): g = gcd(Lgcd(i),Rgcd(i)) if g > mx: mx = g print(mx)
import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def gcd(a,b): while b>0:a,b=b,a%b return a def main(): n=int(input()) aa=LI() gl=[0]*n gr=[0]*n g=aa[0] for i,a in enumerate(aa): g=gcd(g,a) gl[i]=g g=aa[-1] for i,a in enumerate(aa[::-1]): g=gcd(g,a) gr[n-1-i]=g ans=max(gl[n-2],gr[1]) for i in range(1,n-1): g=gcd(gl[i-1],gr[i+1]) if g>ans:ans=g print(ans) main()
p03061
def gcd(a,b): c = a//b d = a%b if d==0: return b else: return gcd(b,d) n = int(eval(input())) nums = list(map(int,input().split())) max_saidaikouyakusu = 0 for i in range(n):#nums[i]がない時の最大公約数を求める saidaikouyakusu = None for j in range(n): if j== i:continue else: if saidaikouyakusu==None: saidaikouyakusu = nums[j] else: saidaikouyakusu = gcd(saidaikouyakusu,nums[j]) if max_saidaikouyakusu < saidaikouyakusu: max_saidaikouyakusu=saidaikouyakusu print(max_saidaikouyakusu)
def gcd(a,b): a,b=max(a,b),min(a,b) if b==0: return a c = a//b d = a%b if d==0: return b else: return gcd(b,d) n = int(eval(input())) nums = list(map(int,input().split())) l = [0 for _ in range(n)] l[0]=0 for i in range(1,n):#nums[i]左側の最大公約数を求める l[i]=gcd(l[i-1],nums[i-1]) r=[0 for _ in range(n)] r[-1]=0 for i in reversed(list(range(0,n-1))):#nums[i]の右側の最大公約数を求める r[i]=(gcd(nums[i+1],r[i+1])) ans=0 for i in range(n): saidaikouyakusu_except_for_ni=gcd(l[i],r[i]) if ans < saidaikouyakusu_except_for_ni: ans = saidaikouyakusu_except_for_ni else:pass print(ans)
p03061
N = int(eval(input())) A = list(map(int, input().split())) def gcd(a, b): if b == 0: return a return gcd(b, a % b) def gcda(a): ret = a[0] for i in range(1, len(a)): ret = gcd(ret, a[i]) return ret ans = 1 for i in range(N): a = A[:i] + A[i+1:] ans = max(ans, gcda(a)) print(ans)
N = int(eval(input())) A = list(map(int, input().split())) def gcd(a, b): if a < b: a, b = b, a if b == 0: return a return gcd(b, a % b) L = [0] * (N + 1) R = [0] * (N + 1) for i in range(N): L[i+1] = gcd(L[i], A[i]) for i in range(N-1, -1, -1): R[i] = gcd(R[i+1], A[i]) ans = 1 for i in range(N): ans = max(ans, gcd(L[i], R[i+1])) print(ans)
p03061
import functools N = int(eval(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)))
N = int(eval(input())) A = list(map(int,input().split())) A.sort() w_min = A[1] for i in range(w_min,0,-1): count = 0 for j in A: if j%i != 0: count = count + 1 if count > 1: break if count < 2: print(i) break
p03061
from math import gcd n = int(eval(input())) A = tuple(map(int, input().split())) from collections import deque q = deque([]) q.append([0, A[0], False]) q.append([1, A[1], True]) # 深さ優先探索 # idx, これまでのGCD, 抜いたか抜いてないか ans = 0 while q: idx, g, jogai = q.pop() if g <= ans: continue if idx == n-1: ans = max(ans, g) continue q.append([idx+1, gcd(g, A[idx+1]), jogai]) if not jogai: q.append([idx+1, g, True]) print(ans)
from math import gcd n = int(eval(input())) A = list(map(int, input().split())) # 以降のGCD left_gcds = [A[0]] for a in A[1:]: left_gcds.append(gcd(left_gcds[-1], a)) right_gcds = [A[-1]] for a in A[:n-1][::-1]: right_gcds.append(gcd(right_gcds[-1], a)) right_gcds.reverse() ans = 0 for i in range(n): if i == 0: tmp = right_gcds[i+1] elif i == n-1: tmp =left_gcds[i-1] else: tmp = gcd(left_gcds[i-1], right_gcds[i+1]) ans = max(ans, tmp) print(ans)
p03061
def gcd(a,b) : r = a % b if r == 0 : return b else : return gcd(b,r) N = int(eval(input())) A = list(map(int,input().split())) L = [0] * N R = [0] * N for i in range(N) : # iより左(右) if i == 0 : L[i] = A[i] R[-i-1] = A[-i-1] else : L[i] = gcd(L[i-1], A[i-1]) R[-i-1] = gcd(R[-i],A[-i]) ans = 1 for i in range(N) : if i == 0 : new = R[i] elif i == N-1 : new = L[i] else : new = gcd(L[i],R[i]) if ans < new : ans = new print(ans)
N = int(eval(input())) A = list(map(int, input().split())) def gcd(n, m): if m == 0: return n return gcd(m, n % m) leftGcd = [-1] * (N + 1) leftGcd[0] = A[0] for i, a in enumerate(A, start=1): leftGcd[i] = gcd(leftGcd[i - 1], a) rightGcd = [-1] * (N + 1) rightGcd[-1] = A[-1] for i, a in enumerate(reversed(A), start=1): rightGcd[-(i + 1)] = gcd(rightGcd[-i], a) ans = max(rightGcd[1], leftGcd[-2]) for mid in range(1, N): ans = max(ans, gcd(leftGcd[mid], rightGcd[mid + 1])) print(ans)
p03061
import sys sys.setrecursionlimit(1000000) def gcd(a, b): if b == 0: return a return gcd(b, a%b) n = int(eval(input())) A = list(map(int, input().split())) def left(i): if i < 1: return 0 return gcd(left(i-1), A[i-1]) def right(i): if i+1 > n-1: return 0 return gcd(right(i+1), A[i+1]) ans = 0 for i in range(n): ans = max(ans, gcd(left(i), right(i))) print(ans)
def gcd(a, b): if b == 0: return a return gcd(b, a%b) n = int(eval(input())) A = list(map(int, input().split())) L = [0]*n R = [0]*n for i in range(n-1): R [i+1] = gcd(R[i], A[i]) L[i+1] = gcd(L[i], A[-i-1]) print((max(gcd(R[i], L[-i-1]) for i in range(n))))
p03061
def gcd_(a,b): if a<b: a,b=b,a if b==0: return a return gcd_(b,a%b) def gcd(l): ans=l[0] for i in l: ans=gcd_(ans,i) return ans N=int(eval(input())) a=list(map(int,input().split())) ans=0 for i in range(N): ans=max(ans,gcd(a[:i]+a[i+1:])) print(ans)
def gcd_(a,b): if a<b: a,b=b,a if b==0: return a return gcd_(b,a%b) def gcd(l): ans=l[0] for i in l: ans=gcd_(ans,i) return ans N=int(eval(input())) A=list(map(int,input().split())) gcd_list=[A[0]] rev_gcd_list=[A[-1]] for i in range(1,N): gcd_list.append(gcd([gcd_list[-1],A[i]])) rev_gcd_list.append(gcd([rev_gcd_list[-1],A[-i-1]])) rev_gcd_list=rev_gcd_list[::-1] ans=max(gcd_list[N-2],rev_gcd_list[1]) for i in range(1,N-1): ans=max(ans,gcd([gcd_list[i-1],rev_gcd_list[i+1]])) print(ans)
p03061
n = int(eval(input())) ls = list(map(int, input().split())) p = 50 ans = 0 if p>n:p=n def gcd(a,b): if a < b: a,b = b,a while b: a, b =b, a % b return a def cul(n, x): l = len(x) b = [x[i] for i in range(l)] b.pop(n) tmp = b[0] for i in range(1, l-1): tmp = gcd(tmp, b[i]) return tmp for i in range(p): s = cul(i,ls[:p]) if s > ans: ans = s print(ans)
def gcd(a,b): if b>a: a,b=b,a while b: a,b=b, a%b return a def cul(n,x): l=len(x) b=[x[i] for i in range(l)] b.pop(n) tmp=b[0] for i in range(1,l-1): tmp=gcd(tmp,b[i]) return tmp import sys p=50 N=int(eval(input())) if p>N: p=N a=list(map(int,input().split())) ans=0 for i in range(p): s=cul(i,a[:p]) if s>ans: ans=s print(ans)
p03061
def gcd(s): list=[] for j in range(len(s)): a=0 b=0 temp=10000000000000000000 v=[] if len(s)>2: for i in range(len(s)): if i!=j: x=s[i] v.append(x) for i in range(len(v)): if i==0: a=v[i] b=v[i+1] else: b=v[i] if a<b: temp=a a=b b=temp while b!=0: temp=a%b a=b b=temp list.append(a) else: list.append(max(s)) return(max(list)) n=int(eval(input())) v=[] line=input().split() for i in range(n): v.append(int(line[i])) print((gcd(v)))
def gcd(x,y): if x<y: x,y=y,x while x%y!=0: x,y=y,x%y return(y) n=int(eval(input())) v=[] line=input().split() for i in range(n): v.append(int(line[i])) gcd_l=[] gcd_r=[] for i in range(n-1): if i==0: gcd_l.append(v[i]) else: gcd_l.append(gcd(gcd_l[i-1],v[i])) for i in range(n-1): if i==0: gcd_r.append(v[n-1]) else: gcd_r.append(gcd(gcd_r[i-1],v[n-i-1])) gcd_list=[] for i in range(n): if i==0: gcd_list.append(gcd_r[n-2]) elif i==n-1: gcd_list.append(gcd_l[n-2]) else: # print(gcd(gcd_l[i-1],gcd_r[n-i-2])) gcd_list.append(gcd(gcd_l[i-1],gcd_r[n-i-2])) #print(gcd_list) print((max(gcd_list)))
p03061
from functools import reduce N = int(eval(input())) a = list(map(int, input().split())) def gcd(x, y): if x < y: x, y = y, x while y != 0: tmp = x % y x = y y = tmp return x ans = 1 if N == 2: print((max(a))) exit() for i in range(N): if i == 0: n = reduce(gcd, a[1:]) elif i == N-1: n = reduce(gcd, a[:i]) else: n = reduce(gcd, a[:i]+a[i+1:]) ans = max(ans, n) print(ans)
N = int(eval(input())) a = list(map(int, input().split())) def gcd(x, y): if x < y: x, y = y, x while y != 0: tmp = x % y x = y y = tmp return x ans = 1 if N == 2: print((max(a))) exit() l = [1] * N l[0] = a[0] r = [1] * N r[N-1] = a[N-1] for i in range(1, N): l[i] = gcd(l[i-1], a[i]) r[N-1-i] = gcd(r[N-i], a[N-1-i]) for i in range(N): if i == 0: n = r[1] elif i == N-1: n = l[N-2] else: n = gcd(l[i-1], r[i+1]) ans = max(ans, n) print(ans)
p03061
N = int(eval(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)
N = int(eval(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 = [0] * N R = [0] * N for i in range(1, N): L[i] = gcd(L[i-1], A[i-1]) R[N-i-1] = gcd(R[N-i], A[N-i]) for i in range(N): if i == 0: ans = R[i] elif i == N-1: ans = max(ans, L[i]) else: ans = max(ans, gcd(L[i], R[i])) print(ans)
p03061
N = int(eval(input())) A = list(map(int, input().split())) def gcd(x, y): if x == 0 or y == 0: return max(x, y) if y == 0: return x return gcd(y, x % y) param = [[0, 0, 0]] addr = 0 while True: if len(param) <= 0: break addr, Li, Lv = param.pop() if addr == 0: if Li == N-1: Rv = 0 M, isCall = Lv, False continue param += [[1, Li, Lv], [0, Li+1, gcd(Lv, A[Li])]] isCall = True continue elif addr == 1: Rv = gcd(A[Li+1], Rv) M, isCall = max(M, gcd(Lv, Rv)), False continue print(M)
N = int(eval(input())) A = list(map(int, input().split())) def gcd(x, y): if x == 0 or y == 0: return max(x, y) return gcd(y, x % y) if y!=0 else x def F(init, add): w = [0] for i in range(init, init + add * N, add): w += [gcd(w[-1], A[i])] return w[:-1] print((max(list(map(lambda x,y:gcd(x,y), F(0, 1), F(N-1, -1)[::-1])))))
p03061
from math import sqrt from sys import exit N=int(eval(input())) A=list(map(int,input().split())) if len(A)==2: print((max(A))) exit() def gcd(a,b): if a<b: a,b=b,a if a%b==0: return b return gcd(b,a%b) g=gcd(A[0],A[-1]) p=[] for i in range(1,int(sqrt(g)+1)): if g%i==0: if i*i==g: p.append(i) else: p+=[i,g//i] def ok(n): f=False for a in A[1:-1]: if a%n: if f: return False f=True return True def GCD(L): if len(L)==1: return L[0] ans=gcd(L[0],L[1]) if len(L)>2: for l in L[2:]: ans=gcd(ans,l) return ans p.sort(reverse=True) for num in p: if ok(num): ans=num break p1=GCD(A[1:-1]) print((max([ans,gcd(p1,A[0]),gcd(p1,A[-1])])))
from math import sqrt from sys import exit N=int(eval(input())) A=list(map(int,input().split())) if len(A)==2: print((max(A))) exit() def gcd(a,b): if a<b: a,b=b,a if a%b==0: return b return gcd(b,a%b) g=A[0] L=[g] for a in A[1:-1]: g=gcd(g,a) L.append(g) g=A[-1] R=[g] for a in A[1:-1][::-1]: g=gcd(g,a) R.append(g) ans=max(L[-1],R[-1]) for l,r in zip(L[:-1],R[:-1][::-1]): ans=max(ans,gcd(l,r)) print(ans)
p03061
#!/usr/bin/env python3 import sys from math import gcd from functools import reduce def f(A): ret = [A[0]] for a in A: ret.append(gcd(ret[-1], a)) ret[0] = reduce(gcd, A[1:]) return ret def solve(N: int, A: "List[int]"): return max(gcd(g0, g1) for g0, g1 in zip(f(A), f(A[::-1])[-2::-1])) # Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools 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]" print((solve(N, A))) def test(): import doctest doctest.testmod() if __name__ == '__main__': #test() main()
#!/usr/bin/env python3 import sys from math import gcd def f(A): ret = [0] for a in A: ret.append(gcd(ret[-1], a)) return ret def solve(N: int, A: "List[int]"): return max(gcd(g0, g1) for g0, g1 in zip(f(A), f(A[::-1])[-2::-1])) # Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools 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]" print((solve(N, A))) def test(): import doctest doctest.testmod() if __name__ == '__main__': #test() main()
p03061
# -*- coding: utf-8 -*- def gcd(a,b): if b==0:return a return gcd(b,a%b) n = int(eval(input())) a_l = list(map(int, input().split())) g_max = 1 for i in range(n): d = a_l.pop(0) if n-1==1: g = a_l[0] else: g = gcd(a_l[0], a_l[1]) if n-1!=2: for j in range(2,n-1): g=gcd(g,a_l[j]) # print(g) g_max = max(g, g_max) # print(d) a_l.append(d) print(g_max)
# -*- coding: utf-8 -*- # 最大公約数 def gcd(a,b): if b==0:return a return gcd(b,a%b) # 入力 n = int(eval(input())) a = list(map(int, input().split())) # 累積GCD left = [0]*(n+1) right = [0]*(n+1) for i in range(n): left[i+1] = gcd(left[i], a[i]) for i in reversed(list(range(n))): right[i] = gcd(right[i+1], a[i]) # print(left) # print(right) # 集計 ans = 0 for i in range(n): l = left[i] r = right[i+1] ans = max(ans, gcd(l, r)) print(ans)
p03061
def gcd(a, b): if a > b: a, b = b, a if a == 0: return b return gcd(b % a, a) N = int(eval(input())) A = [int(v) for v in input().split()] max_gcd = -1 for i in range(N): tmp = None for j in range(N): if j == i: continue if tmp is None: tmp = A[j] continue # print(tmp, A[j], gcd(tmp, A[j])) tmp = gcd(tmp, A[j]) max_gcd = max(tmp, max_gcd) if max_gcd < 0: print((A[0])) else: print(max_gcd)
N = int(eval(input())) A = [int(v) for v in input().split()] def gcd(a, b): if a > b: a, b = b, a if a == 0: return b return gcd(a, b % a) L = [0 for _ in range(N + 2)] R = [0 for _ in range(N + 2)] for i, a in enumerate(A): L[i + 1] = gcd(A[i], L[i]) R[N-i] = gcd(A[N-i-1], R[N-i+1]) # print(L) # print(R) res = -2 for i in range(1, N+1): res = max(res, gcd(L[i-1], R[i+1])) print(res)
p03061
import math N=int(eval(input())) A=list(map(int,input().split())) L=[0]*(N+1) for i in range(N): L[i+1]=math.gcd(L[i],A[i]) R=[0]*(N+1) for i in range(N-1,-1,-1): R[i]=math.gcd(R[i+1],A[i]) M=[] for i in range(N): M.append(math.gcd(L[i],R[i+1])) print((max(M)))
from math import * N=int(eval(input())) A=list(map(int,input().split())) left=[0]*(N+1) for i in range(N): left[i+1]=gcd(left[i],A[i]) right=[0]*(N+1) for i in range(N-1,-1,-1): right[i]=gcd(right[i+1],A[i]) ans=0 for i in range(N): ans=max(ans,gcd(left[i],right[i+1])) print(ans)
p03061
import math N = int(eval(input())) A = [int(x) for x in input().split()] def gcd(x, y): if y == 0: return x else: return gcd(y, x % y) #再帰定義 def list_gcd(x): if len(x) == 1: return x[0] next_list = [] for i in range(int(len(x)/2)): next_list.append(gcd(x[2*i],x[2*i+1])) if len(x)%2 == 1: next_list.append(x[-1]) return list_gcd(next_list) out = 0 for i in range(len(A)): l = A[:i] + A[i+1:] this_gcd = list_gcd(l) if this_gcd > out: out = this_gcd print(out)
N = int(eval(input())) A = [int(x) for x in input().split()] def gcd(x, y): if y == 0: return x else: return gcd(y, x % y) #再帰定義 list_gcd = [(gcd(A[0],A[1]),False),(A[0],True),(A[1],True)] #print(list_gcd) for i in range(2,len(A)): next_gcd = [] for j in list_gcd: if j[1]: this_gcd = (gcd(j[0],A[i]),True) if not(this_gcd in next_gcd): next_gcd.append(this_gcd) else: this_gcd = (gcd(j[0],A[i]),False) if not(this_gcd in next_gcd): next_gcd.append(this_gcd) this_gcd = (j[0],True) if not(this_gcd in next_gcd): next_gcd.append(this_gcd) list_gcd = next_gcd #print(list_gcd) out = 1 for i in list_gcd: if i[0] > out: out = i[0] print(out)
p03061
import copy n = int(eval(input())) a = list(map(int,input().split())) ans = 1 if len(a) == 2: print((max(a))) else: for i in range(n): b = copy.deepcopy(a) b.pop(i) b.sort b0 = b[0] for j in range(1,n-1): b1 = b[j] r = 0 while True: r = b1 % b0 if r == 0: break elif r > b0: b1, b0 = r, b0 else: b1, b0 = b0, r if b0 > ans: ans = b0 print(ans)
n = int(eval(input())) a = list(map(int,input().split())) a.sort() ans = 1 if n == 2: print((max(a))) else: a0 = a[0] a1 = a[1] r = 0 while True: r = a1 % a0 if r == 0: b0 = a0 break elif r > a0: a1, a0 = r, a0 else: a1, a0 = a0, r a1 = a[1] a2 = a[2] r = 0 while True: r = a2 % a1 if r == 0: b1 = a1 break elif r > a1: a2, a1 = r, a1 else: a2, a1 = a1, r a0 = a[0] a2 = a[2] r = 0 while True: r = a2 % a0 if r == 0: b2 = a0 break elif r > a0: a2, a0 = r, a0 else: a2, a0 = a0, r max_a = max(b0,b1,b2) kouho = [] for j in range(max_a,1,-1): if max_a % j == 0: c = list([x % j for x in a]) c_ng = [i for i in c if i > 0] #print("j=",j,"c=",c,"c_ng=",c_ng) if len(c_ng) <= 1: ans = j break print(ans)
p03061
n=int(eval(input())) a=[int(x) for x in input().split()] from collections import defaultdict d=defaultdict(int) for ai in a: for i in range(1, int(ai**0.5)+1): if ai % i == 0: d[i]+=1 if i != ai // i: d[ai//i]+=1 print((max([x for x in list(d.keys()) if d[x]==n or d[x]==n-1])))
n=int(eval(input())) a=[int(x) for x in input().split()] anses=set() for i in [0,1]: for j in range(1, int(a[i]**0.5)+1): if a[i] % j == 0: anses.add(j) if j != a[i] // j: anses.add(a[i] // j) anses=sorted(anses,reverse=True) for ans in anses: solvenum=0 for j in a: if j%ans==0: solvenum+=1 if solvenum==n or solvenum==n-1: print(ans) break
p03061
from functools import reduce n = int(eval(input())) a = list(map(int, input().split())) def gcd(x,y): x_ = max(x,y) y_ = min(x,y) if y_==0: return 0 if x_%y_==0: return y_ return gcd(y_, x_%y_) def gcd_list(numbers): return reduce(gcd, numbers) gcds = [gcd_list(a[:i]+a[i+1:]) for i in range(n)] print((max(gcds)))
n = int(eval(input())) a = list(map(int, input().split())) def gcd(x_,y_): x = max(x_, y_) y = min(x_, y_) if y==0: return x if x%y == 0: return y return gcd(y, x%y) L = [0]*n R = [0]*n L[0] = a[0] R[-1] = a[-1] for i in range(1,n): L[i] = gcd(L[i-1], a[i]) j = n-1-i R[j] = gcd(R[j+1], a[j]) L = [0]+L R = R + [0] M = [0]*n for i in range(n): M[i] = gcd(L[i],R[i+1]) print((max(M)))
p03061
#!/usr/bin/env pypy3 import collections import math def solve(n, xs): divisors = collections.Counter() for x in xs: for y in range(1, math.floor(math.sqrt(x)) + 1): d, m = divmod(x, y) if m == 0: divisors[y] += 1 if y != d: divisors[d] += 1 for i in range(1, max(xs) + 1)[::-1]: if divisors[i] >= n - 1: return i def main(): n = int(eval(input())) xs = [int(x) for x in input().split()] print((solve(n, xs))) if __name__ == "__main__": main()
#!/usr/bin/env python3 import itertools try: from math import gcd except ImportError: from fractions import gcd class Cumulative(object): def __init__(self, seq, op): self.cumul_array = [0] self.cumul_array.extend(itertools.accumulate(seq, op)) def solve(n, xs): left_cumul = Cumulative(xs, gcd) right_cumul = Cumulative(reversed(xs), gcd) res = 1 for i in range(n): left_gcd = left_cumul.cumul_array[i] right_gcd = right_cumul.cumul_array[n - i - 1] cand = gcd(left_gcd, right_gcd) if cand > res: res = cand return res def main(): n = int(eval(input())) xs = [int(x) for x in input().split()] res = solve(n, xs) print(res) if __name__ == "__main__": main()
p03061
N = int(eval(input())) A = list(map(int, input().split())) def divisor(x): ret = [] for i in range(1, int(x**0.5)+1): if x % i == 0: ret.append(i) if i*i != x: ret.append(x//i) return ret divisor_dict = {} for i in range(len(A)): div = divisor(A[i]) for j in div: if j in divisor_dict: divisor_dict[j] += 1 else: divisor_dict[j] = 1 max_val = 0 for key, val in list(divisor_dict.items()): if val >= N-1 and key > max_val: max_val = key print(max_val)
N = int(eval(input())) A = [0] + list(map(int, input().split())) def gcd(a, b): if a < b: b, a = a, b if b == 0: return a while a % b != 0: a %= b a, b = b, a return b L = [0] * (N+1) R = [0] * (N+2) for i in range(1, N+1): ind = N+1-i L[i] = gcd(L[i-1], A[i-1]) R[ind] = gcd(R[ind+1], A[ind]) ret = [] for i in range(1, N+1): ret.append(gcd(L[i], R[i+1])) print((max(ret)))
p03061
N = int(eval(input())) As = list(map(int, input().split())) N, As 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 kous = list(map(make_divisors, As)) all = [] for v in kous: all += v all = sorted(list(set(all)), reverse=True) ans = 1 i = 0 for v in all: count = 0 for k in kous: if v in k: count += 1 if count >= N-1: ans = all[i] break i+=1 print(ans)
import sys if sys.version_info.minor >= 5: from math import gcd else: from fractions import gcd N = int(eval(input())) As = list(map(int, input().split())) left_gcds = [gcd(As[0], As[0])] for i in range(1, len(As)): left_gcds.append(gcd(left_gcds[i-1], As[i])) right_gcds = [gcd(As[-1], As[-1])] for i in range(1, len(As)): right_gcds.append(gcd(right_gcds[i-1], As[-(i+1)])) right_gcds.reverse() gcds = [] for i in range(N): if i == 0: gcds.append(right_gcds[1]) elif i == N-1: gcds.append(left_gcds[-2]) else: gcds.append(gcd(left_gcds[i-1], right_gcds[i+1])) print((max(gcds)))
p03061
import math n=int(eval(input())) a=list(map(int,input().split())) a.sort() g=a[0] for i in range(1,n): g=math.gcd(g,a[i]) d=[set(),set()] for i in range(2): b=a[i]//g f=1 while f*f<b+1: if b%f==0: d[i].add(f) d[i].add(b//f) f+=1 an=d[0]&d[1] xo=d[0]^d[1] for i in range(2,n): e1=set() e2=set() for m in xo: if a[i]%m!=0: e1.add(m) for m in an: if a[i]%m!=0: e2.add(m) xo.add(m) for m in e1: xo.remove(m) for m in e2: an.remove(m) an.add(0) xo.add(0) print((max(max(xo),max(an))*g))
import math n=int(eval(input())) a=list(map(int,input().split())) a.sort() g=a[0] for i in range(1,n): g=math.gcd(g,a[i]) d=[set(),set()] for i in range(2): b=a[i]//g f=1 while f*f<b+1: if b%f==0: d[i].add(f) d[i].add(b//f) f+=1 an=d[0]&d[1] an.remove(1) xo=d[0]^d[1] for i in range(2,n): e1=set() e2=set() for m in xo: if a[i]%m!=0: e1.add(m) for m in an: if a[i]%m!=0: e2.add(m) xo.add(m) for m in e1: xo.remove(m) for m in e2: an.remove(m) an.add(1) xo.add(1) print((max(max(xo),max(an))*g))
p03061
N = int(eval(input())) A = [int(x) for x in input().split()] def max_GCD(m_list): m_list.sort() a = m_list.pop() result = a while m_list: b = m_list.pop() a = GCD(a, b) if a < result: result = a if result == 1: return result return result def GCD(m, n): a = max(m, n) b = min(m, n) while b > 0: r = a % b if r == 0: return b a = b b = r r_list = [0] * N for i in range(N): A_ = A.copy() A_.pop(i) r_list[i] = max_GCD(A_) print((max(r_list)))
N = int(eval(input())) A = [int(x) for x in input().split()] def GCD(m, n): if m == 0: return n elif n == 0: return m a = max(m, n) b = min(m, n) while b > 0: r = a % b if r == 0: return b a = b b = r L = [0] * (N + 1) R = [0] * (N + 1) for i in range(N): L[i + 1] = GCD(L[i], A[i]) R[N - i - 1] = GCD(R[N - i], A[N - i - 1]) M = [GCD(L[i], R[i+1]) for i in range(N)] print((max(M)))
p03061
def check(ary, div): c = 0 for i in range(len(ary)): if ary[i] % div == 0: c += 1 if c >= len(ary) - 1: return True else: return False N = int(eval(input())) data = list(map(int, input().split())) ans = 1 for i in range(1, max(data) + 1)[::-1]: if check(data, i): ans = i break print(ans)
n = int(eval(input())) data = list(map(int, input().split())) def gcd(a, b): while b: a, b = b, a % b # print("a b", a, b) return a left_ = [0] * (n + 1) right_ = [0] * (n + 1) ans = [] for i in range(n): left_[i] = gcd( left_[i-1], data[i] ) for i in range(n-1, 0, -1): right_[i] = gcd( right_[i+1], data[i] ) for i in range(n): ans += [ gcd( left_[i-1], right_[i+1] ) ] print(( max(ans) ))
p03061
def gcd(a, b): while b != 0: a, b = b, a % b return a N = int(eval(input())) A = list(map(int, input().split())) L = [0] R = [0] ans = 0 for i in range(N): L.append(gcd(L[i],A[i])) R.insert(0,gcd(R[0],A[N-1-i])) L.append(0) R.insert(0,0) for i in range(N): ans = max(ans,(gcd(L[i], R[i+2]))) print(ans)
def gcd(a, b): while b != 0: a, b = b, a % b return a N = int(eval(input())) A = list(map(int, input().split())) L = [0]*(N+2) R = [0]*(N+2) ans = 0 for i in range(N): L[i+1] = (gcd(L[i],A[i])) R[N-i] = (gcd(R[N-i+1],A[N-1-i])) for i in range(N): ans = max(ans,(gcd(L[i], R[i+2]))) print(ans)
p03061
from functools import reduce N = int(eval(input())) A=sorted(list(map(int,input().split()))) def gcd(a,b): if b == 0: return a else: return gcd(b,a%b) def gcdlist(lista): if len(lista) == 1: return lista[0] else: from functools import reduce return reduce(gcd,lista) ans = [] ans.append(gcdlist(A[1:])) ans.append(gcdlist(A[:-1])) for i in range(1,N-1): ans.append(gcd(gcdlist(A[:i]),gcdlist(A[i+1:]))) print((max(ans)))
N = int(eval(input())) A=sorted(list(map(int,input().split()))) def gcd(a,b): if b == 0: return a else: return gcd(b,a%b) ans = [] gcdlist = [A[0]] for i in range(1,N): gcdlist.append(gcd(gcdlist[i-1],A[i])) gcdlistr = [A[-1]] for i in range(N-2,-1,-1): gcdlistr.append(gcd(gcdlistr[N-i-2],A[i])) gcdlistr.reverse() gcdlist.insert(0,0) gcdlist.append(0) gcdlistr.insert(0,0) gcdlistr.append(0) for i in range(N): ans.append(gcd(gcdlist[i],gcdlistr[i+2])) print((max(ans)))
p03061
n = int(eval(input())) As = list(map(int, input().split())) 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 # 1個目と2個目の約数列挙 # ⇨ 1個しか消さないから必ずどちらかの約数は残る divs = make_divisors(As[0]) + make_divisors(As[1]) divs = list(set(divs)) cnt = 1 for div in sorted(divs,reverse=True): a = sum([1 for a in As if a%div == 0]) if n - a <= 1: print(div) exit()
n = int(eval(input())) As = list(map(int, input().split())) # from math import gcd def gcd(n, m): # 最大公約数 a = max(n,m) b = min(n,m) while b: a, b = b, a % b return a # 左からの累積GCDと g = As[0] left = [] for a in As: g = gcd(g, a) left.append(g) # 右からの累積GCD As_r = As[::-1] g_r = As_r[0] right = [] for a in As_r: g_r = gcd(g_r, a) right.append(g_r) l = left r = right[::-1] ans = 0 for i in range(n): if i == 0: ans = max(ans, r[1]) elif i == n-1: ans = max(ans, l[n-2]) else: g = gcd(l[i-1],r[i+1]) ans = max(ans, g) print(ans)
p03061
from collections import defaultdict n = int(eval(input())) a = list(map(int,input().split())) d = defaultdict(int) for ai in a: for i in range(1,int(ai**0.5)+1): if ai%i==0: d[i] += 1 if i!=ai//i: d[ai//i] += 1 sd=sorted(list(d.items()),key=lambda x:x[0],reverse=True) for k,v in sd: if v==n or v==n-1: print(k) break
def gcd(a,b): while a%b!=0: a,b = b,a%b return b n = int(eval(input())) a = list(map(int,input().split())) right,left = [0]*n,[0]*n left[0],right[n-1] = a[0],a[n-1] for i in range(1,n): left[i] = gcd(left[i-1],a[i]) for i in range(n-2,-1,-1): right[i] = gcd(right[i+1],a[i]) ans = max(left[-2],right[1]) for i in range(1,n-1): ans = max(ans,gcd(right[i+1],left[i-1])) print(ans)
p03061
n = int(eval(input())) a = list(map(int,input().split())) def gcd(a, b): if a < b: a, b = b, a if b == 0: return a c = a % b return gcd(b, c) if n == 2: print((max(a))) else: cand = [] for j in range(n): if j == 0: temp = a[1] for i in range(2,len(a)): temp = gcd(temp, a[i]) else: temp = a[0] for i in range(1,len(a)): if i != j: temp = gcd(temp, a[i]) cand.append(temp) print((max(cand)))
# -*- coding: utf-8 -*- n = int(eval(input())) a = list(map(int,input().split())) 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 if n == 2: print((max(a))) else: list1 = make_divisors(a[0]) list2 = make_divisors(a[1]) list1.sort() list2.sort() flag1 = 0 cand1 = 0 for i in range(len(list1)): flag1 = 0 for j in range(n): if a[j] % list1[i] != 0: flag1 += 1 if flag1 <= 1: cand1 = list1[i] cand2 = 0 for i in range(len(list2)): flag2 = 0 for j in range(n): if a[j] % list2[i] != 0: flag2 += 1 if flag2 <= 1: cand2 = list2[i] print((max(cand1,cand2)))
p03061
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(eval(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)
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(eval(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/100) 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)
p03061
N = int(eval(input())) A_list = list(map(int, input().split())) # case1 : [7, 6, 8] def gcd(a, b): while b != 0: a, b = b, int(a%b) return a ans = 0 for i in range(N): left_gcd = 0 right_gcd = 0 for j in range(0, i): left_gcd = gcd(A_list[j], left_gcd) for k in range(i+1, N): right_gcd = gcd(A_list[k], right_gcd) i_gcd = gcd(left_gcd, right_gcd) if ans < i_gcd: ans = i_gcd print(ans)
N = int(eval(input())) A_list = list(map(int, input().split())) # case1 : [7, 6, 8] def gcd(a, b): while b != 0: a, b = b, int(a%b) return a left_gcd = [0] * (N+1) right_gcd = [0] * (N+1) for i in range(N): left_gcd[i+1] = gcd(A_list[i], left_gcd[i]) for i in range(N-1, 0, -1): right_gcd[i] = gcd(A_list[i], right_gcd[i+1]) ans = 0 for i in range(N): i_gcd = gcd(left_gcd[i], right_gcd[i+1]) if ans < i_gcd: ans = i_gcd print(ans)
p03061
n = int(eval(input())) import math from functools import reduce def gcd(numbers): return reduce (math.gcd, numbers) ans = 0 a = list(map(int, input().split())) for i in range(n): c = a[i] a.remove(c) ans = max(ans, gcd(a)) a.insert(i, c) #print(a) print(ans)
n = int(eval(input())) a = list(map(int, input().split())) import math l_gcd = [] r_gcd = [] c = a[0] for i in a[:-1]: c = math.gcd(c, i) l_gcd.append(c) c = a[-1] for j in a[1:][::-1]: c = math.gcd(c, j) r_gcd.append(c) ans = max(r_gcd[-1], l_gcd[-1]) for r, l in zip(r_gcd[:-1], l_gcd[:-1][::-1]): c = math.gcd(r, l) ans = max(ans, c) print(ans)
p03061
n = int(eval(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)))
def gcd(a,b): while b: a, b = b, a%b return a n = int(eval(input())) a = list(map(int, input().split())) left = [0] * (n + 1) for i in range(1, n): left[i] = gcd(left[i - 1], a[i - 1]) right = [0] * (n + 1) for i in range(1, n): right[n - i] = gcd(right[n - i + 1], a[n - i]) ans = 0 for i in range(n): ans = max(ans, gcd(left[i], right[i + 1])) print(ans)
p03061
#!usr/bin/env python3 from collections import defaultdict from collections import deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return list(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def LS():return list(map(list, sys.stdin.readline().split())) def S(): return list(sys.stdin.readline())[:-1] def IR(n): l = [None for i in range(n)] for i in range(n):l[i] = I() return l def LIR(n): l = [None for i in range(n)] for i in range(n):l[i] = LI() return l def SR(n): l = [None for i in range(n)] for i in range(n):l[i] = S() return l def LSR(n): l = [None for i in range(n)] for i in range(n):l[i] = LS() return l sys.setrecursionlimit(1000000) mod = 1000000007 #A def A(): a,b,t = LI() k = t//a print((k*b)) return #B def B(): n = I() v = LI() c = LI() ans = 0 for i in range(n): if v[i] > c[i]: ans += (v[i]-c[i]) print(ans) return #C def C(): def gcd(a,b): if a == 0: return b return gcd(b%a, a) n = I() a = LI() m = math.ceil(n**0.5) f = [0 for i in range(m)] for i in range(m): b = a[m*i:min(n,m*(i+1))] if b: g = b[0] for j in b[1:]: g = gcd(g,j) f[i] = g else: f.pop(-1) ans = 1 for i in range(n): j = i//m c = f[:j]+f[j+1:]+a[m*j:i]+a[i+1:min(n,m*(j+1))] g = c[0] for k in c[1:]: g = gcd(g, k) ans = max(ans,g) print(ans) return #D def D(): n = I() a = LI() k = 0 for i in a: if i < 0: k += 1 ans = 0 m = float("inf") for i in a: ans += abs(i) m = min(m,abs(i)) if k%2: ans -= 2*m print(ans) return #Solve if __name__ == "__main__": C()
import operator class SegmentTree: def __init__(self, size, default, op = operator.add): self.size = 1 while self.size < size: self.size *= 2 self.dat = [default]*(self.size*2-1) self.op = op def update(self, i, x): i += self.size - 1 self.dat[i] = x while i > 0: i = (i - 1) // 2 self.dat[i] = self.op(self.dat[i*2+1], self.dat[i*2+2]) def add(self, i, x): i += self.size - 1 self.dat[i] = self.op(self.dat[i], x) while i > 0: i = (i - 1) // 2 self.dat[i] = self.op(self.dat[i], x) def get(self, a, b, k = None, l = None, r = None): if k is None: k = 0 l = 0 r = self.size if r <= a or b <= l: return None if a <= l and r <= b: return self.dat[k] res_l = self.get(a, b, k*2+1, l, (l+r)//2) res_r = self.get(a, b, k*2+2, (l+r)//2, r) if res_l is None: return res_r if res_r is None: return res_l return self.op(res_l, res_r) def gcd(a,b): if a == 0: return b if b == 0: return a return gcd(b%a, a) n = int(eval(input())) a = list(map(int, input().split())) seg = SegmentTree(n,0,gcd) for i in range(n): seg.update(i,a[i]) ans = 1 for i in range(n): seg.update(i,0) ans = max(ans, seg.get(0,n)) seg.update(i,a[i]) print(ans)
p03061
import operator class SegmentTree: def __init__(self, size, default, op = operator.add): self.size = 1 while self.size < size: self.size *= 2 self.dat = [default for i in range(self.size*2-1)] self.op = op def update(self, i, x): i += self.size - 1 self.dat[i] = x while i > 0: i = (i - 1) // 2 self.dat[i] = self.op(self.dat[i*2+1], self.dat[i*2+2]) def add(self, i, x): i += self.size - 1 self.dat[i] = self.op(self.dat[i], x) while i > 0: i = (i - 1) // 2 self.dat[i] = self.op(self.dat[i], x) def get(self, a, b, k = None, l = None, r = None): if k is None: k = 0 l = 0 r = self.size if r <= a or b <= l: return None if a <= l and r <= b: return self.dat[k] res_l = self.get(a, b, k*2+1, l, (l+r)//2) res_r = self.get(a, b, k*2+2, (l+r)//2, r) if res_l is None: return res_r if res_r is None: return res_l return self.op(res_l, res_r) def gcd(a,b): if a == 0: return b if b == 0: return a return gcd(b%a, a) n = int(eval(input())) a = list(map(int, input().split())) seg = SegmentTree(n,0,gcd) for i in range(n): seg.update(i,a[i]) ans = 1 for i in range(n): seg.update(i,0) ans = max(ans, seg.get(0,n)) seg.update(i,a[i]) print(ans)
import operator class SegmentTree: def __init__(self, size, default, op = operator.add): self.size = 2**size.bit_length() self.dat = [default]*(self.size*2) self.op = op def update(self, i, x): i += self.size self.dat[i] = x while i > 0: i >>= 1 self.dat[i] = self.op(self.dat[i*2], self.dat[i*2+1]) def add(self, i, x): i += self.size self.dat[i] = self.op(self.dat[i], x) while i > 0: i >>= 1 self.dat[i] = self.op(self.dat[i], x) def get(self, a, b = None): if b is None: b = a + 1 l, r = a + self.size, b + self.size res = None while l < r: if l & 1: if res is None: res = self.dat[l] else: res = self.op(res, self.dat[l]) l += 1 if r & 1: r -= 1 if res is None: res = self.dat[r] else: res = self.op(res, self.dat[r]) l >>= 1 r >>= 1 return res def gcd(a,b): if a == 0: return b if b == 0: return a return gcd(b%a, a) n = int(eval(input())) a = list(map(int, input().split())) seg = SegmentTree(n,0,gcd) for i in range(n): seg.update(i,a[i]) ans = 1 for i in range(n): seg.update(i,0) ans = max(ans, seg.get(0,n)) seg.update(i,a[i]) print(ans)
p03061
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(eval(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)
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(eval(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)
p03061
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(eval(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)
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, l, r): l += self.size r += 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 def gcd(a, b): if a == 0: return b else: return gcd(b%a, a) n = int(eval(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)
p03061
# -*- coding: utf-8 -*- import sys if sys.version_info.minor >= 5: from math import gcd else: from fractions import gcd from functools import reduce def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def gcd_list(nums): return reduce(gcd, nums, nums[0]) def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = float('inf') MOD = 10 ** 9 + 7 N=INT() A=LIST() L=[0]*N L[0]=A[0] for i in range(1, N): L[i]=gcd(L[i-1], A[i]) R=[0]*N R[-1]=A[-1] for i in range(N-2, -1, -1): R[i]=gcd(R[i+1], A[i]) ans=0 for i in range(N): if i==0: ans=max(ans, R[1]) elif i==N-1: ans=max(ans, L[-2]) else: ans=max(ans, gcd(L[i-1], R[i+1])) print(ans)
# -*- coding: utf-8 -*- import sys if sys.version_info.minor >= 5: from math import gcd else: from fractions import gcd def input(): return sys.stdin.readline().strip() def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = float('inf') MOD = 10 ** 9 + 7 N=INT() A=LIST() # 予め左右から累積GCDを取っておく L=[0]*(N+2) for i in range(N): L[i+1]=gcd(L[i], A[i]) R=[0]*(N+2) for i in range(N-1, -1, -1): R[i+1]=gcd(R[i+2], A[i]) # 1箇所除いてどうなるかを全部試しても、各回GCD1回ずつで済む ans=0 for i in range(N): ans=max(ans, gcd(L[i], R[i+2])) print(ans)
p03061
# -*- coding: utf-8 -*- import sys if sys.version_info.minor >= 5: from math import gcd else: from fractions import gcd def input(): return sys.stdin.readline().strip() def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = float('inf') MOD = 10 ** 9 + 7 class SegTree: """ 以下のクエリを処理する 1.update: i番目の値をxに更新する 2.get_val: 区間[l, r)の値を得る """ def __init__(self, n, func, init): """ :param n: 要素数 :param func: 値の操作に使う関数(min, max, addなど) :param init: 要素の初期値 """ n += 1 self.n = n self.func = func self.init = init # nより大きい2の冪数 n2 = 1 while n2 < n: n2 <<= 1 self.n2 = n2 self.tree = [self.init] * (n2 << 1) def update(self, i, x): """ i番目の値をxに更新 :param i: index(0-indexed) :param x: update value """ i += self.n2 self.tree[i] = x while i > 1: self.tree[i >> 1] = x = self.func(x, self.tree[i ^ 1]) i >>= 1 def get_val(self, a, b): """ [a, b)の値を得る :param a: index(0-indexed) :param b: index(0-indexed) """ return self._get_val(a, b, 1, 0, self.n2) def _get_val(self, a, b, k, l, r): """ [a, b)の値を得る内部関数 :param k: 現在調べている区間のtree内index :param l, r: kが表す区間の左右端index [l, r) :return: kが表す区間と[a, b)の共通区間内での最小値。共通区間を持たない場合は初期値 """ # 範囲外なら初期値 if r <= a or b <= l: return self.init # [a,b)が完全に[l,r)を包含するならtree[k]の値を採用 if a <= l and r <= b: return self.tree[k] # 一部だけ範囲内なら2つに分けて再帰的に調査 m = (l + r) // 2 return self.func( self._get_val(a, b, k << 1, l, m), self._get_val(a, b, (k << 1) + 1, m, r) ) N=INT() A=[0]+LIST()+[0] st=SegTree(N+2, gcd, 0) for i in range(N+2): st.update(i, A[i]) ans=0 for i in range(1, N+1): ans=max(ans, gcd(st.get_val(0, i), st.get_val(i+1, N+2))) print(ans)
# -*- coding: utf-8 -*- import sys if sys.version_info.minor >= 5: from math import gcd else: from fractions import gcd def input(): return sys.stdin.readline().strip() def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = float('inf') MOD = 10 ** 9 + 7 class SegTree: """ 以下のクエリを処理する 1.update: i番目の値をxに更新する 2.get_val: 区間[l, r)の値を得る """ def __init__(self, n, func, init): """ :param n: 要素数 :param func: 値の操作に使う関数(min, max, addなど) :param init: 要素の初期値 """ n += 1 self.n = n self.func = func self.init = init # nより大きい2の冪数 n2 = 1 while n2 < n: n2 <<= 1 self.n2 = n2 self.tree = [self.init] * (n2 << 1) def update(self, i, x): """ i番目の値をxに更新 :param i: index(0-indexed) :param x: update value """ i += self.n2 self.tree[i] = x while i > 1: self.tree[i >> 1] = x = self.func(x, self.tree[i ^ 1]) i >>= 1 def get_val(self, a, b): """ [a, b)の値を得る :param a: index(0-indexed) :param b: index(0-indexed) """ return self._get_val(a, b, 1, 0, self.n2) def _get_val(self, a, b, k, l, r): """ [a, b)の値を得る内部関数 :param k: 現在調べている区間のtree内index :param l, r: kが表す区間の左右端index [l, r) :return: kが表す区間と[a, b)の共通区間内での最小値。共通区間を持たない場合は初期値 """ # 範囲外なら初期値 if r <= a or b <= l: return self.init # [a,b)が完全に[l,r)を包含するならtree[k]の値を採用 if a <= l and r <= b: return self.tree[k] # 一部だけ範囲内なら2つに分けて再帰的に調査 m = (l + r) // 2 return self.func( self._get_val(a, b, k << 1, l, m), self._get_val(a, b, (k << 1) + 1, m, r) ) N=INT() A=LIST() st=SegTree(N+2, gcd, 0) for i in range(N): st.update(i+1, A[i]) ans=0 for i in range(1, N+1): ans=max(ans, gcd(st.get_val(0, i), st.get_val(i+1, N+2))) print(ans)
p03061
# -*- coding: utf-8 -*- import sys if sys.version_info.minor >= 5: from math import gcd else: from fractions import gcd def input(): return sys.stdin.readline().strip() def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = float('inf') MOD = 10 ** 9 + 7 class SegTree: """ 以下のクエリを処理する 1.update: i番目の値をxに更新する 2.get_val: 区間[l, r)の値を得る """ def __init__(self, n, func, init): """ :param n: 要素数 :param func: 値の操作に使う関数(min, max, addなど) :param init: 要素の初期値 """ n += 1 self.n = n self.func = func self.init = init # nより大きい2の冪数 n2 = 1 while n2 < n: n2 <<= 1 self.n2 = n2 self.tree = [self.init] * (n2 << 1) def update(self, i, x): """ i番目の値をxに更新 :param i: index(0-indexed) :param x: update value """ i += self.n2 self.tree[i] = x while i > 1: self.tree[i >> 1] = x = self.func(x, self.tree[i ^ 1]) i >>= 1 def get_val(self, a, b): """ [a, b)の値を得る :param a: index(0-indexed) :param b: index(0-indexed) """ return self._get_val(a, b, 1, 0, self.n2) def _get_val(self, a, b, k, l, r): """ [a, b)の値を得る内部関数 :param k: 現在調べている区間のtree内index :param l, r: kが表す区間の左右端index [l, r) :return: kが表す区間と[a, b)の共通区間内での最小値。共通区間を持たない場合は初期値 """ # 範囲外なら初期値 if r <= a or b <= l: return self.init # [a,b)が完全に[l,r)を包含するならtree[k]の値を採用 if a <= l and r <= b: return self.tree[k] # 一部だけ範囲内なら2つに分けて再帰的に調査 m = (l + r) // 2 return self.func( self._get_val(a, b, k << 1, l, m), self._get_val(a, b, (k << 1) + 1, m, r) ) N=INT() A=LIST() st=SegTree(N+2, gcd, 0) for i in range(N): st.update(i+1, A[i]) ans=0 for i in range(1, N+1): ans=max(ans, gcd(st.get_val(0, i), st.get_val(i+1, N+2))) print(ans)
# -*- coding: utf-8 -*- import sys if sys.version_info.minor >= 5: from math import gcd else: from fractions import gcd def input(): return sys.stdin.readline().strip() def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = float('inf') MOD = 10 ** 9 + 7 class SegTree: """ 以下のクエリを処理する 1.update: i番目の値をxに更新する 2.get_val: 区間[l, r)の値を得る """ def __init__(self, n, func, init): """ :param n: 要素数 :param func: 値の操作に使う関数(min, max, addなど) :param init: 要素の初期値 """ n += 1 self.n = n self.func = func self.init = init # nより大きい2の冪数 n2 = 1 while n2 < n: n2 <<= 1 self.n2 = n2 self.tree = [self.init] * (n2 << 1) def update(self, i, x): """ i番目の値をxに更新 :param i: index(0-indexed) :param x: update value """ i += self.n2 self.tree[i] = x while i > 1: self.tree[i >> 1] = x = self.func(x, self.tree[i ^ 1]) i >>= 1 def get_val(self, a, b): """ [a, b)の値を得る :param a: index(0-indexed) :param b: index(0-indexed) """ l = a + self.n2 r = b + self.n2 s = self.init while l < r: if r & 1: r -= 1 s = self.func(s, self.tree[r]) if l & 1: s = self.func(s, self.tree[l]) l += 1 l >>= 1 r >>= 1 return s N=INT() A=LIST() st=SegTree(N+2, gcd, 0) for i in range(N): st.update(i+1, A[i]) ans=0 for i in range(1, N+1): ans=max(ans, gcd(st.get_val(0, i), st.get_val(i+1, N+2))) print(ans)
p03061
N = int(eval(input())) a =list(map(int, input().split())) def gcd(a, b): while b: a, b = b, a%b return a from functools import reduce max_val = reduce(gcd, a[1:]) for i in range(1, N): tmp = a[i] a[i] = a[0] val = reduce(gcd, a) if max_val < val: max_val = val a[i] = tmp print(max_val)
N = int(eval(input())) a =list(map(int, input().split())) def gcd(a, b): while b: a, b = b, a%b return a L = [] for i in range(len(a)): if i==0: L.append(None) elif i ==1: L.append(a[i-1]) else: L.append(gcd(L[-1], a[i-1])) R = [] for i in range(len(a)): if i==0: R.append(None) elif i ==1: R.append(a[-i]) else: R.append(gcd(R[-1], a[-i])) R = R[::-1] max_val = 0 for r, l in zip(L,R): if r==None: val = l elif l==None: val = r else: val = gcd(r, l) if val > max_val: max_val = val print(max_val)
p03061
def GCD(a, b): while a % b != 0: r = a % b a, b = b, r return b ans = 0 N = int(eval(input())) A = tuple(map(int,input().split())) for S in range(len(A)): Af = 0 for i in range(len(A)): if S != i: Af = GCD(Af,A[i]) ans = max(ans,Af) print(ans)
def GCD(a, b): if a == 0 and b == 0: return 0 elif a == 0: return b elif b == 0: return a while a % b != 0: r = a % b a, b = b, r return b ans = 0 N = int(eval(input())) A = tuple(map(int,input().split())) X = [0]*N Y = [0]*N for i in range(1,N): X[i] = GCD(X[i-1],A[i-1]) for i in range(N-1)[::-1]: Y[i] = GCD(Y[i+1],A[i+1]) for i in range(N): ans = max(ans,GCD(X[i],Y[i])) print(ans)
p03061
n = int(eval(input())) v = list(map(int,input().split())) def gcd(a,b): while b != 0: a, b = b, a % b return a l_ans = [0] * n for i in range(n): g = 0 for j in range(i): g = gcd(g, v[j]) l_ans[i] = g r_ans = [0] * n for i in range(n): g = 0 for j in range(i): g = gcd(g, v[n-1-j]) r_ans[i] = g r_ans.reverse() ans = 1 for i in range(n): ans = max(ans, gcd(l_ans[i], r_ans[i])) print(ans)
n = int(eval(input())) v = list(map(int,input().split())) def gcd(a,b): while b != 0: a, b = b, a % b return a l_ans = [0] * n g = 0 for i in range(1, n): g = gcd(g, v[i-1]) l_ans[i] = g r_ans = [0] * n g = 0 for i in range(1, n): g = gcd(g, v[n-i]) r_ans[i] = g r_ans.reverse() ans = 1 for i in range(n): ans = max(ans, gcd(l_ans[i], r_ans[i])) print(ans)
p03061
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement from collections import deque,defaultdict,Counter from bisect import bisect_left,bisect_right from operator import itemgetter from heapq import heapify,heappop,heappush from queue import Queue,LifoQueue,PriorityQueue from copy import deepcopy from time import time from functools import reduce import string import sys sys.setrecursionlimit(10 ** 7) def input() : return sys.stdin.readline().strip() def INT() : return int(eval(input())) def MAP() : return list(map(int,input().split())) def MAP1() : return [int(x)-1 for x in input().split()] def LIST() : return list(MAP()) def LIST1() : return list(MAP1()) #####segfunc##### def segfunc(x, y): return gcd(x, y) ################# #####ide_ele##### ide_ele = 0 ################# class SegTree: """ init(init_val, ide_ele): 配列init_valで初期化 O(N) update(k, x): k番目の値をxに更新 O(logN) query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN) """ def __init__(self, init_val, segfunc, ide_ele): """ init_val: 配列の初期値 segfunc: 区間にしたい操作 ide_ele: 単位元 n: 要素数 num: n以上の最小の2のべき乗 tree: セグメント木(1-index) """ n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num # 配列の値を葉にセット for i in range(n): self.tree[self.num + i] = init_val[i] # 構築していく for i in range(self.num - 1, 0, -1): self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): """ k番目の値をxに更新 k: index(0-index) x: update value """ k += self.num self.tree[k] = x while k > 1: self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]) k >>= 1 def query(self, l, r): """ [l, r)のsegfuncしたものを得る l: index(0-index) r: index(0-index) """ res = self.ide_ele l += self.num r += self.num while l < r: if l & 1: res = self.segfunc(res, self.tree[l]) l += 1 if r & 1: res = self.segfunc(res, self.tree[r - 1]) l >>= 1 r >>= 1 return res def solve(): N = INT() a = LIST() seg = SegTree(a, segfunc, ide_ele) 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__': solve()
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement from collections import deque,defaultdict,Counter from bisect import bisect_left,bisect_right from operator import itemgetter from heapq import heapify,heappop,heappush from queue import Queue,LifoQueue,PriorityQueue from copy import deepcopy from time import time from functools import reduce import string import sys sys.setrecursionlimit(10 ** 7) def input() : return sys.stdin.readline().strip() def INT() : return int(eval(input())) def MAP() : return list(map(int,input().split())) def MAP1() : return [int(x)-1 for x in input().split()] def LIST() : return list(MAP()) def LIST1() : return list(MAP1()) def solve(): N = INT() a = LIST() l = [0]*N r = [0]*N l[0] = a[0] r[N-1] = a[N-1] for i in range(1, N): l[i] = gcd(l[i-1], a[i]) for i in range(N-2, -1, -1): r[i] = gcd(r[i+1], a[i]) ans = max(r[1], l[N-2]) for i in range(1, N-1): ans = max(ans, gcd(l[i-1], r[i+1])) print(ans) if __name__ == '__main__': solve()
p03061
from collections import Counter def get_dividers(n): """ 正の整数 n の約数を取得する """ dividers = {i for i in range(1, int(n ** 0.5) + 1) if n % i == 0} return dividers | {n // d for d in dividers} def lcm(a, b): """ aとbの最小公倍数を計算する """ from math import gcd return a * b // gcd(a, b) N = int(eval(input())) A = tuple(map(int, input().split(' '))) counter = Counter() for a in A: for d in get_dividers(a): counter[d] += 1 ans = 1 candidates = [] for key, value in list(counter.items()): if value == N: ans = lcm(ans, key) elif value == N - 1: candidates.append(key) if candidates: ans = max([lcm(ans, c) for c in candidates]) print(ans)
from collections import Counter from functools import lru_cache @lru_cache(maxsize=None) def get_dividers(n): """ 正の整数 n の約数を取得する """ dividers = {i for i in range(1, int(n ** 0.5) + 1) if n % i == 0} return dividers | {n // d for d in dividers} @lru_cache(maxsize=None) def lcm(a, b): """ aとbの最小公倍数を計算する """ from math import gcd return a * b // gcd(a, b) N = int(eval(input())) A = tuple(map(int, input().split(' '))) counter = Counter() for a in A: for d in get_dividers(a): counter[d] += 1 ans = 1 candidates = [] for key, value in list(counter.items()): if value == N: ans = lcm(ans, key) elif value == N - 1: candidates.append(key) if candidates: ans = max([lcm(ans, c) for c in candidates]) print(ans)
p03061
from collections import Counter from functools import lru_cache @lru_cache(maxsize=None) def get_dividers(n): """ 正の整数 n の約数を取得する """ dividers = {i for i in range(1, int(n ** 0.5) + 1) if n % i == 0} return dividers | {n // d for d in dividers} @lru_cache(maxsize=None) def lcm(a, b): """ aとbの最小公倍数を計算する """ from math import gcd return a * b // gcd(a, b) N = int(eval(input())) A = tuple(map(int, input().split(' '))) counter = Counter() for a in A: for d in get_dividers(a): counter[d] += 1 ans = 1 candidates = [] for key, value in list(counter.items()): if value == N: ans = lcm(ans, key) elif value == N - 1: candidates.append(key) if candidates: ans = max([lcm(ans, c) for c in candidates]) print(ans)
from collections import Counter from functools import lru_cache from math import gcd @lru_cache(maxsize=None) def get_dividers(n): """ 正の整数 n の約数を取得する """ dividers = {i for i in range(1, int(n ** 0.5) + 1) if n % i == 0} return dividers | {n // d for d in dividers} @lru_cache(maxsize=None) def lcm(a, b): """ aとbの最小公倍数を計算する """ return a * b // gcd(a, b) N = int(eval(input())) A = tuple(map(int, input().split(' '))) counter = Counter() for a in A: counter.update(get_dividers(a)) ans = 1 candidates = [] for key, value in list(counter.items()): if value == N: ans = lcm(ans, key) elif value == N - 1: candidates.append(key) if candidates: ans = max([lcm(ans, c) for c in candidates]) print(ans)
p03061
from collections import Counter from functools import lru_cache from math import gcd @lru_cache(maxsize=None) def get_dividers(n): """ 正の整数 n の約数を取得する """ dividers = {i for i in range(1, int(n ** 0.5) + 1) if n % i == 0} return dividers | {n // d for d in dividers} @lru_cache(maxsize=None) def lcm(a, b): """ aとbの最小公倍数を計算する """ return a * b // gcd(a, b) N = int(eval(input())) A = tuple(map(int, input().split(' '))) counter = Counter() for a in A: counter.update(get_dividers(a)) ans = 1 candidates = [] for key, value in list(counter.items()): if value == N: ans = lcm(ans, key) elif value == N - 1: candidates.append(key) if candidates: ans = max([lcm(ans, c) for c in candidates]) print(ans)
from math import gcd N = int(eval(input())) A = tuple(map(int, input().split(' '))) left = [] right = [] left.append(A[0]) for a in A[1:]: left.append(gcd(left[-1], a)) right.append(A[-1]) for a in A[-2::-1]: right.append(gcd(right[-1], a)) right = list(reversed(right)) ans = [left[-2], right[1]] for i in range(1, len(left) - 1): ans.append(gcd(left[i - 1], right[i + 1])) print((max(ans)))
p03061
N = int(eval(input())) A = list(map(int, input().split())) #a,bの最大公約数 def gcd(a,b): if b == 0: return a else: return gcd(b,a%b) #リスト l の最大公約数 def gcdlist(l): a = l[0] for i in range(len(l)): a = gcd(a,l[i]) return a gcd_ref = [A[0]] for i in range(1, N): new_gcd = gcd(gcd_ref[-1], A[i]) gcd_ref.append(new_gcd) best = gcd_ref[N - 1] for i in range(N - 1, -1, -1): if i > 0: if i == N - 1: a = gcd_ref[i - 1] if best < a: best = a else: a = gcd_ref[i - 1] l = A[i + 1:] gcd_without_i = gcd(a, gcdlist(l)) if gcd_without_i > best: best = gcd_without_i else: l = A[1:] gcd_without_i = gcdlist(l) if gcd_without_i > best: best = gcd_without_i print(best)
N = int(eval(input())) A = list(map(int, input().split())) #a,bの最大公約数 def gcd(a,b): if b == 0: return a else: return gcd(b,a%b) gcd_L = [A[0]] gcd_R = [A[N - 1]] for i in range(1, N): new_gcd_l = gcd(gcd_L[-1], A[i]) gcd_L.append(new_gcd_l) new_gcd_r = gcd(gcd_R[-1], A[N - i - 1]) gcd_R.append(new_gcd_r) best = gcd_L[N - 1] for i in range(N): if i > 0: if i == N - 1: a = gcd_L[i - 1] if best < a: best = a else: l = gcd_L[i - 1] r = gcd_R[N - 2 - i] gcd_without_i = gcd(l, r) if gcd_without_i > best: best = gcd_without_i else: a = gcd_R[N - 2] if a > best: best = a print(best)
p03061
import math N = int(eval(input())) A = sorted(list(map(int,input().split()))) def gcd(a,b): if b == 0: return a return gcd(b,a%b) M = A[-1] P = [1,M] for k in range(2,math.ceil(math.sqrt(M))+1): if M % k == 0: P.append(k) P.append(M//k) M = A[-2] P.append(M) for k in range(2,math.ceil(math.sqrt(M))+1): if M % k == 0: P.append(k) P.append(M//k) P = set(list(P)) ans = 1 for p in P: t = 0 for k in range(N): if A[k]%p != 0: t += 1 if t == 2: break if t <= 1: ans = max(ans,p) print(ans)
import math N = int(eval(input())) A = sorted(list(map(int,input().split()))) def yakusuu(a): P = [a] for k in range(2,math.floor(math.sqrt(a))+1): if a % k == 0: P.append(k) P.append(a//k) return P P = set(yakusuu(A[-1])+yakusuu(A[-2])) ans = 1 for e in P: f = 0 for a in A: if a%e != 0: f += 1 if f == 2: break if f <= 1: ans = max(ans,e) print(ans)
p03061
n = int(eval(input())) ai = list(map(int,input().split())) def gcd(a, b): while b: a, b = b, a%b return a resmax = 0 for j in range(n): if j == 0: res = ai[1] for i in range(2,n): res = gcd(ai[i],res) else: res = ai[0] for i in range(1,n): if i == j: continue else: res = gcd(ai[i],res) if res > resmax: resmax = res #print(resi) print(resmax)
n = int(eval(input())) ai = list(map(int,input().split())) def gcd(a, b): while b: a, b = b, a%b return a r=[] l=[] resr = ai[0] resl = ai[n-1] r.append(ai[0]) l.append(ai[n-1]) for i in range(1,n): resr=gcd(resr,ai[i]) r.append(resr) resl=gcd(resl,ai[n-1-i]) l.append(resl) #print(r) #print(l) resmax = 1 for i in range(n): if i == 0: res = l[n-1-1] elif i == n-1: res = r[n-1-1] else: res = gcd(r[i-1], l[n-1-i-1]) if res > resmax: resmax = res print((int(resmax)))
p03061
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools import random sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return list(map(int, sys.stdin.readline().split())) def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) class Prime(): def __init__(self, n): self.M = m = int(math.sqrt(n)) + 10 self.A = a = [True] * m a[0] = a[1] = False self.T = t = [] for i in range(2, int(math.sqrt(m)) + 1): if not a[i]: continue t.append(i) for j in range(i*i,m,i): a[j] = False def is_prime(self, n): return self.A[n] def division(self, n): d = collections.defaultdict(int) for c in self.T: while n % c == 0: d[c] += 1 n //= c if n < 2: break if n > 1: d[n] += 1 return d.items() def sowa(self, n): r = 1 for k,v in self.division(n): t = 1 for i in range(1,v+1): t += math.pow(k, i) r *= t return r def main(): n = I() a = sorted(LI()) pr = Prime(10**9) p = collections.defaultdict(int) for b in a: r = [1] for k, v in pr.division(b): for c in r[:]: for i in range(1,v+1): r.append(c*(k**i)) for k in r: p[k] += 1 r = 1 for k,v in p.items(): if v >= n-1 and r < k: r = k return r print(main())
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools import random sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return list(map(int, sys.stdin.readline().split())) def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) class Prime(): def __init__(self, n): self.M = m = int(math.sqrt(n)) + 10 self.A = a = [True] * m a[0] = a[1] = False self.T = t = [] for i in range(2, int(math.sqrt(m)) + 1): if not a[i]: continue t.append(i) for j in range(i*i,m,i): a[j] = False def is_prime(self, n): return self.A[n] def division(self, n): d = collections.defaultdict(int) for c in self.T: while n % c == 0: d[c] += 1 n //= c if n < 2: break if n > 1: d[n] += 1 return d.items() def sowa(self, n): r = 1 for k,v in self.division(n): t = 1 for i in range(1,v+1): t += math.pow(k, i) r *= t return r def main(): n = I() ac = collections.Counter(LI()) if n == 2: return max(ac.keys()) a = [] aa = [] for k,v in ac.items(): if v > 1: aa.append(k) else: a.append(k) tr = -1 if len(aa) > 0: tr = aa[0] for k in aa[1:]: tr = fractions.gcd(k,tr) l = len(a) if l < 2: return tr if l < 3: r = 1 for i in a: kr = fractions.gcd(i,tr) if r < kr: r = kr return r m = max(a) lm = m // (l-1) pr = Prime(10**9) p = collections.defaultdict(int) for b in a: r = [1] for k, v in pr.division(b): for c in r[:]: for i in range(1,v+1): t = c*(k**i) if t > lm: break r.append(t) for k in r: p[k] += 1 if tr == -1: r = 1 for k,v in p.items(): if v >= l-1 and r < k: r = k return r r = 1 for k,v in p.items(): kk = fractions.gcd(tr,k) if v >= l-1 and r < kk: r = kk return r print(main())
p03061
import math from functools import reduce def gcd(numbers): if len(numbers)>=2: return reduce(math.gcd,numbers) else: return math.gcd(0,*numbers) N = int(eval(input())) A = list(map(int,input().split())) ans = [] for i in range(N): if i == 0: ans.append(gcd(A[i+1:])) elif i == N-1: ans.append(gcd(A[:N-1])) else:#N=1からN-2 ans.append(math.gcd(gcd(A[:i]),gcd(A[i+1:]))) #print(ans) print((max(ans)))
from math import gcd N = int(eval(input())) A = list(map(int,input().split())) L,R = [0],[0] for i in range(N-1): L += [gcd(L[i],A[i])] R += [gcd(R[i],A[-i-1])] print((max(gcd(L[i],R[-i-1])for i in range(N))))
p03061
from functools import lru_cache from math import gcd N = int(eval(input())) A = list(map(int, input().split())) @lru_cache(None) def func(*x): n = len(x) if n == 2: return gcd(x[0], x[1]) elif n == 1: return x[0] return gcd(func(*x[:n//2]), func(*x[n//2:])) ans = 0 for i in range(N): tmp = A[i] A[i] = A[i+1 if i < N-1 else 0] ans = max(ans, func(*A)) A[i] = tmp print(ans)
from itertools import accumulate from math import gcd N = int(eval(input())) A = list(map(int, input().split())) l = list(accumulate(A[:-1], gcd, initial=0)) r = list(accumulate(A[::-1], gcd, initial=0)) print((max(gcd(l[i], r[N-i-1]) for i in range(N))))
p03061
# coding: utf-8 # Your code here! N = int(input().rstrip()) A = list(map(int, input().rstrip().split())) def gcd(x, y): if x == 0: return y if y == 0: return x if y > x: temp = y y = x x = temp amari = x % y if amari == 0: return y else: return gcd(y, amari) L = [0] for i in range(N): l = L[-1] L.append(gcd(l, A[i])) R = [0] for i in reversed(list(range(N))): r = R[0] R.insert(0, gcd(r, A[i])) M = [gcd(L[i], R[i+1]) for i in range(N)] # i=1,...,N print((max(M)))
# coding: utf-8 # Your code here! N = int(input().rstrip()) A = list(map(int, input().rstrip().split())) def gcd(x, y): if x == 0: return y if y == 0: return x if y > x: temp = y y = x x = temp amari = x % y if amari == 0: return y else: return gcd(y, amari) L = list(range(N)) L[0] = 0 for i in range(N-1): L[i+1] = gcd(L[i], A[i]) R = list(range(N)) R.append(0) for i in reversed(list(range(N))): R[i] = gcd(R[i+1], A[i]) M = [gcd(L[i], R[i+1]) for i in range(N)] # i=1,...,N print((max(M)))
p03061
def gcd(a,b): while b!=0: a,b=b,a%b return a n = int(eval(input())) a = list(map(int, input().split())) dp = [a[1], a[0], gcd(a[0], a[1])] for i in range(2, n): for j in range(i+1): if j != i: dp[j] = gcd(a[i], dp[j]) dp += [gcd(a[i], dp[i])] print((max(dp)))
def gcd(a,b): while b!=0: a,b=b,a%b return a n = int(eval(input())) a = list(map(int, input().split())) l = [0 for i in range(n+1)] r = [0 for i in range(n+1)] for i in range(1, n+1): l[i] = gcd(l[i-1], a[i-1]) for i in range(n)[::-1]: r[i] = gcd(r[i+1], a[i]) m = [0 for i in range(n)] for i in range(n): m[i] = gcd(l[i], r[i+1]) print((max(m)))
p03061
import functools def gcd(a, b): while b: a, b = b, a % b return a n = int(eval(input())) a = list(map(int, input().split())) ans = 0 for i in range(n): b = a[i] a.remove(b) c = functools.reduce(gcd, a) if ans < c: ans = c a.insert(i,b) print(ans)
def gcd(a, b): while b: a, b = b, a % b return a n = int(eval(input())) a = list(map(int, input().split())) a.sort() if n == 2: print((max(a))) else: a1 = gcd(a[0],a[1]) a2 = gcd(a[1],a[2]) a3 = gcd(a[2],a[0]) m = max(a1,a2,a3) judge = 0 for i in range(m): if judge == 1: break cnt = 0 for j in range(n): if a[j] % (m-i) != 0: cnt += 1 if cnt > 1: break if j == n-1 and cnt <= 1: print((m-i)) judge = 1
p03061
import copy from collections import deque n = int(eval(input())) a = list(map(int, input().split())) max_a = max(a) def gcd(n, m): if m == 0: return n return gcd(m, n % m) ans = 0 if len(a) == 1: print((a[0])) exit() for i in range(n): _a = copy.deepcopy(a) del _a[i] que = deque(_a) while len(que) > 1: n = que.pop() m = que.pop() l = gcd(n, m) que.append(l) tmp_max = que.pop() ans = max(ans, tmp_max) print(ans)
import copy n = int(eval(input())) a = list(map(int, input().split())) max_a = max(a) def gcd(n, m): if m == 0: return n return gcd(m, n % m) # 事前処理 # あらかじめ、左端からi個でgcdを計算しておく # 同様に右からも l = [0] * n r = [0] * n l[0] = a[0] r[-1] = a[-1] for i in range(0, n-1): l[i+1] = gcd(l[i], a[i+1]) for i in range(n-1, 0, -1): r[i-1] = gcd(r[i], a[i-1]) tmp = 0 ans = 0 for i in range(n): if i == 0: tmp = r[i+1] elif i == n-1: tmp = l[i-1] else: tmp = gcd(l[i-1], r[i+1]) ans = max(ans, tmp) print(ans)
p03061
# C import functools def gcd(a,b): if a<b:a,b=b,a if a%b==0:return b return gcd(b,a%b) n = int(eval(input())) a = list(map(int, input().split())) a.sort() ans = 0 pre_a = -1 for i in range(n): if pre_a != a[i]: if len(a[:i]) >= 1 and len(a[i+1:]) >= 1: res = gcd(functools.reduce(gcd, a[:i]), functools.reduce(gcd, a[i+1:])) else: values = a[:i] + a[i+1:] res = functools.reduce(gcd, values) ans = max(ans, res) pre_a = a[i] print(ans)
# C import functools # 3.4以前は math の gcd が対応していないので fractions のものを使う # from fractions import gcd def gcd(a,b): if a<b:a,b=b,a if a%b==0:return b return gcd(b,a%b) n = int(eval(input())) a = list(map(int, input().split())) gcd_left = [a[0]] gcd_right = [a[-1]] for i in range(1,n): if gcd_left[i-1] != 1: res = gcd(gcd_left[i-1], a[i]) else: res = 1 gcd_left.append(res) for i in range(1,n): if gcd_right[i-1] != 1: res = gcd(gcd_right[i-1], a[-(i+1)]) else: res = 1 gcd_right.append(res) gcd_right = gcd_right[::-1] ans = 0 for i in range(n): if i == 0: res = gcd_right[1] elif i == n-1: res = gcd_left[-2] else: res = gcd(gcd_right[i+1], gcd_left[i-1]) ans = max(res, ans) print(ans)
p03061
def gca(x, y): if x == 0: return y return gca(y % x, x) N = int(eval(input())) A = [int(i) for i in input().split()] dic = {} maxg = 0 for ignore in range(N): g = A[0] if ignore == 0: g = A[1] for i in range(N): if i == ignore: continue x = g y = A[i] if y > x : x, y = y, x if (x, y) in dic: g = dic[(x, y)] else: g = gca(g, A[i]) dic[(x, y)] = g maxg = max(g, maxg) print(maxg)
def gca(x, y): if x == 0: return y return gca(y % x, x) N = int(eval(input())) A = [int(i) for i in input().split()] L = [0] * N R = [0] * N L[0] = A[0] R[N - 1] = A[N - 1] for i in range(1, N): L[i] = gca(A[i], L[i - 1]) for i in range(N - 2, -1, -1): R[i] = gca(A[i], R[i + 1]) maxg = 0 for i in range(1, N - 1): maxg = max(maxg, gca(L[i - 1], R[i + 1])) print((max(L[N - 2], R[1], maxg)))
p03061
from math import gcd # segfunc: 問題に応じて設定 def segfunc(x, y): return gcd(x, y) # 配列aで初期化 def init(a): for i in range(n): seg[i+num-1] = a[i] for i in range(num-2, -1, -1) : seg[i] = segfunc(seg[2*i+1], seg[2*i+2]) # a[k]の値をxに更新 def update(k, x): k += num - 1 seg[k] = x while k: k = (k - 1) // 2 seg[k] = segfunc(seg[k*2+1], seg[k*2+2]) # [p, q)についてsegfuncを適用したものを返す def query(p, q): if q <= p: return ide_ele p += num - 1 q += num - 2 res = ide_ele 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 = p // 2 q = (q - 1) // 2 if p == q: res = segfunc(res, seg[p]) else: res = segfunc(res, segfunc(seg[p], seg[q])) return res n = int(eval(input())) a = list(map(int, input().split())) ide_ele = 0 num = 2 ** (len(bin(n - 1)) - 2) seg = [ide_ele] * 2 * num init(a) res = 0 for i in range(n): res = max(res, gcd(query(0, i), query(i+1, n))) print(res)
from math import gcd class SegmentTree: """Segment Tree Attributes: n (int): 要素数 num (int): n以上の最小の2の累乗 ide_ele (int): 単位元 - RmQ(Range Minimum Query): inf - RMQ(Range Maximum Query): -1 - RSQ(Range Sum Query): 0 - RPQ(Range Product Query): 1 - RGQ(Range GCD Query): 0 seg (list): 要素の格納先 """ def __init__(self, a): """初期化 O(N) Args: a (list): 対象の配列 """ self.n = len(a) self.num = 2 ** (self.n - 1).bit_length() self.ide_ele = 0 self.seg = [self.ide_ele] * (2*self.num-1) for i in range(self.n): self.seg[self.num+i-1] = a[i] for i in range(self.num-2, -1, -1) : self.seg[i] = self.st_func(self.seg[2*i+1], self.seg[2*i+2]) def query(self, l, r): """区間クエリの計算 O(logN) Args: l (int): 区間の左端 r (int): 区間の右端 Returns: int: [l, r)についての区間クエリ """ if r <= l: return self.ide_ele l += self.num - 1 r += self.num - 2 res = self.ide_ele while r - l > 1: if l % 2 == 0: res = self.st_func(res, self.seg[l]) if r % 2 == 1: res = self.st_func(res, self.seg[r]) r -= 1 l = l // 2 r = (r - 1) // 2 if l == r: res = self.st_func(res, self.seg[l]) else: res = self.st_func(res, self.st_func(self.seg[l], self.seg[r])) return res def update(self, i, v): """値の更新 O(logN) Args: i (int): 更新対象のindex v (int): 更新値 """ i += self.num - 1 self.seg[i] = v while i > 0: i = (i - 1) // 2 self.seg[i] = self.st_func(self.seg[i*2+1], self.seg[i*2+2]) def st_func(self, x, y): """問題に応じた処理 Returns: int: 問題に応じた値 - RmQ(Range Minimum Query): min(x, y) - RMQ(Range Maximum Query): max(x, y) - RSQ(Range Sum Query): x + y - RPQ(Range Product Query): x * y - RGQ(Range GCD Query): gcd(x, y) """ return gcd(x, y) n = int(eval(input())) a = list(map(int, input().split())) st = SegmentTree(a) res = 0 for i in range(n): res = max(res, gcd(st.query(0, i), st.query(i+1, n))) print(res)
p03061
from math import gcd class SegmentTree: """Segment Tree Attributes: n (int): 要素数 num (int): n以上の最小の2の累乗 ide_ele (int): 単位元 - RmQ(Range Minimum Query): inf - RMQ(Range Maximum Query): -1 - RSQ(Range Sum Query): 0 - RPQ(Range Product Query): 1 - RGQ(Range GCD Query): 0 seg (list): 要素の格納先 """ def __init__(self, a): """初期化 O(N) Args: a (list): 対象の配列 """ self.n = len(a) self.num = 2 ** (self.n - 1).bit_length() self.ide_ele = 0 self.seg = [self.ide_ele] * (2*self.num-1) for i in range(self.n): self.seg[self.num+i-1] = a[i] for i in range(self.num-2, -1, -1) : self.seg[i] = self.st_func(self.seg[2*i+1], self.seg[2*i+2]) def query(self, a, b, k=0, l=0, r=-1): """区間クエリの計算 O(logN) Args: l (int): 区間の左端 r (int): 区間の右端 Returns: int: [l, r)についての区間クエリ """ if r < 0: r = self.num if r <= a or b <= l: return self.ide_ele elif a <= l and r <= b: return self.seg[k] else: vl = self.query(a, b, 2*k+1, l, (l+r)//2) vr = self.query(a, b, 2*k+2, (l+r)//2, r) return self.st_func(vl, vr) """ if r <= l: return self.ide_ele l += self.num - 1 r += self.num - 2 res = self.ide_ele while r - l > 1: if l % 2 == 0: res = self.st_func(res, self.seg[l]) if r % 2 == 1: res = self.st_func(res, self.seg[r]) r -= 1 l = l // 2 r = (r - 1) // 2 if l == r: res = self.st_func(res, self.seg[l]) else: res = self.st_func(res, self.st_func(self.seg[l], self.seg[r])) return res """ def update(self, i, v): """値の更新 O(logN) Args: i (int): 更新対象のindex v (int): 更新値 """ i += self.num - 1 self.seg[i] = v while i > 0: i = (i - 1) // 2 self.seg[i] = self.st_func(self.seg[i*2+1], self.seg[i*2+2]) def st_func(self, x, y): """問題に応じた処理 Returns: int: 問題に応じた値 - RmQ(Range Minimum Query): min(x, y) - RMQ(Range Maximum Query): max(x, y) - RSQ(Range Sum Query): x + y - RPQ(Range Product Query): x * y - RGQ(Range GCD Query): gcd(x, y) """ return gcd(x, y) n = int(eval(input())) a = list(map(int, input().split())) st = SegmentTree(a) res = 0 for i in range(n): res = max(res, gcd(st.query(0, i), st.query(i+1, n))) print(res)
from math import gcd class SegmentTree: """Segment Tree Attributes: n (int): 要素数 num (int): n以上の最小の2の累乗 ide_ele (int): 単位元 - RmQ(Range Minimum Query): inf - RMQ(Range Maximum Query): -1 - RSQ(Range Sum Query): 0 - RPQ(Range Product Query): 1 - RGQ(Range GCD Query): 0 seg (list): 要素の格納先 """ def __init__(self, a): """初期化 O(N) Args: a (list): 対象の配列 """ self.n = len(a) self.num = 2 ** (self.n - 1).bit_length() self.ide_ele = 0 self.seg = [self.ide_ele] * (2*self.num-1) for i in range(self.n): self.seg[self.num+i-1] = a[i] for i in range(self.num-2, -1, -1) : self.seg[i] = self.st_func(self.seg[2*i+1], self.seg[2*i+2]) def query(self, l, r): """区間クエリの計算 O(logN) Args: l (int): 区間の左端 r (int): 区間の右端 Returns: int: [l, r)についての区間クエリ """ if r <= l: return self.ide_ele l += self.num - 1 r += self.num - 2 res = self.ide_ele while r - l > 1: if l % 2 == 0: res = self.st_func(res, self.seg[l]) if r % 2 == 1: res = self.st_func(res, self.seg[r]) r -= 1 l = l // 2 r = (r - 1) // 2 if l == r: res = self.st_func(res, self.seg[l]) else: res = self.st_func(res, self.st_func(self.seg[l], self.seg[r])) return res def update(self, i, v): """値の更新 O(logN) Args: i (int): 更新対象のindex v (int): 更新値 """ i += self.num - 1 self.seg[i] = v while i > 0: i = (i - 1) // 2 self.seg[i] = self.st_func(self.seg[i*2+1], self.seg[i*2+2]) def st_func(self, x, y): """問題に応じた処理 Returns: int: 問題に応じた値 - RmQ(Range Minimum Query): min(x, y) - RMQ(Range Maximum Query): max(x, y) - RSQ(Range Sum Query): x + y - RPQ(Range Product Query): x * y - RGQ(Range GCD Query): gcd(x, y) """ return gcd(x, y) n = int(eval(input())) a = list(map(int, input().split())) st = SegmentTree(a) res = 0 for i in range(n): res = max(res, gcd(st.query(0, i), st.query(i+1, n))) print(res)
p03061
# -*- coding: utf-8 -*- def gcd(a, b): if (b == 0): return a return gcd(b, a % b) n = int(eval(input())) a = list(map(int, input().split())) g = [1] * n ans = 1 for i in range(n): if i == 0: g[0] = a[1] else: g[0] = a[0] for j in range(1, n): if i == j: g[j] = g[j - 1] continue g[j] = gcd(g[j - 1], a[j]) ans = max(ans, g[n - 1]) print(ans)
# -*- coding: utf-8 -*- def gcd(a, b): if (b == 0): return a return gcd(b, a % b) n = int(eval(input())) a = list(map(int, input().split())) li = [0] * (n + 1) r = [0] * (n + 1) for i in range(n): li[i + 1] = gcd(li[i], a[i]) for i in range(n - 1, -1, -1): r[i] = gcd(r[i + 1], a[i]) ans = 0 for i in range(n): ans = max(ans, gcd(li[i], r[i + 1])) print(ans)
p03061
N = int(eval(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 set(cd) #print(cd_dic) cds = common_divisor(A[0]).union(common_divisor(A[1])) ans = 1 for k in cds: #print(cd_dic) cnt = 0 for a in A: if a%k!=0: cnt += 1 if cnt == 2: break else: ans = max(ans,k) print(ans)
class SegmentTree(): def __init__(self, n, oper, e): self.n = n self.oper = oper self.e = e self.log = (n - 1).bit_length() self.size = 1 << self.log self.data = [e] * (2 * self.size) def update(self, k): self.data[k] = self.oper(self.data[2 * k], self.data[2 * k + 1]) def build(self, arr): #assert len(arr) <= self.n for i in range(self.n): self.data[self.size + i] = arr[i] for i in range(self.size-1,0,-1): self.update(i) def set(self, p, x): #assert 0 <= p < self.n p += self.size self.data[p] = x for i in range(self.log): p >>= 1 self.update(p) def get(self, p): #assert 0 <= p < self.n return self.data[p + self.size] def prod(self, l, r): #assert 0 <= l <= r <= self.n sml = smr = self.e l += self.size r += self.size while l < r: if l & 1: sml = self.oper(sml, self.data[l]) l += 1 if r & 1: r -= 1 smr = self.oper(self.data[r], smr) l >>= 1 r >>= 1 return self.oper(sml, smr) def all_prod(self): return self.data[1] def max_right(self, l, f): #assert 0 <= l <= self.n #assert f(self.) if l == self.n: return self.n l += self.size sm = self.e while True: while l % 2 == 0: l >>= 1 if not f(self.oper(sm, self.data[l])): while l < self.size: l = 2 * l if f(self.oper(sm, self.data[l])): sm = self.oper(sm, self.data[l]) l += 1 return l - self.size sm = self.oper(sm, self.data[l]) l += 1 if (l & -l) == l: break return self.n def min_left(self, r, f): #assert 0 <= r <= self.n #assert f(self.) if r == 0: return 0 r += self.size sm = self.e while True: r -= 1 while r > 1 and (r % 2): r >>= 1 if not f(self.oper(self.data[r], sm)): while r < self.size: r = 2 * r + 1 if f(self.oper(self.data[r], sm)): sm = self.oper(self.data[r], sm) r -= 1 return r + 1 - self.size sm = self.oper(self.data[r], sm) if (r & -r) == r: break return 0 import sys input = sys.stdin.readline from math import gcd e = 0 N = int(eval(input())) A = list(map(int, input().split())) st = SegmentTree(N,gcd,e) st.build(A) ans = max(st.prod(1,N),st.prod(0,N-1)) for i in range(1,N-1): ans = max(ans, gcd(st.prod(0,i),st.prod(i+1,N))) print(ans)
p03061
n = int(eval(input())) v = [int(i) for i in input().split()] def gcd(x, y): if y > x: x, y = y, x if y == 0: return x if x % y == 0: return y return gcd(y, x % y) l = [0 for i in range(n)] r = [0 for i in range(n)] for i in range(1, n): l[i] = gcd(l[i - 1], v[i - 1]) r[-i-1] = gcd(r[-i], v[-i]) m = [gcd(l[i], r[i]) for i in range(n)] print((max(m)))
n = int(eval(input())) v = [int(i) for i in input().split()] def gcd(x, y): if y > x: x, y = y, x if y == 0: return x if x % y == 0: return y return gcd(y, x % y) l = [0 for i in range(n)] r = l[:] for i in range(1, n): l[i] = gcd(l[i - 1], v[i - 1]) r[-i-1] = gcd(r[-i], v[-i]) m = [gcd(l[i], r[i]) for i in range(n)] print((max(m)))
p03061
import sys def gcd(a, b): while b: a, b = b, a%b return a n=int(eval(input())) al=input().split() al_i = [int(s) for s in al] if (n == 2): print((max(al_i[0],al_i[1]))) sys.exit() fg = [0] * n bg = [0] * n for x in range(n): if (x == 0): fg[0] = al_i[0] y = 1 else: while y <= x: fg[y] = gcd(fg[y-1], al_i[y]) y += 1 for x in range(n): if (x == 0): bg[0] = al_i[n-1] y = 1 else: while y <= x: bg[y] = gcd(bg[y-1], al_i[n-1-y]) y += 1 m = 0 gm = 0 for x in range(n): if (x == 0): g = bg[n-2] elif (x == n-1): g = fg[n-2] else: g = gcd(fg[x-1],bg[n-2-x]) if (g > gm): gm = g print(gm)
def gcd(a, b): while b: a, b = b, a%b return a n=int(eval(input())) al=[int(x) for x in input().split()] fg = [0] * n bg = [0] * n fg[0] = al[0] bg[0] = al[n-1] for x in range(1,n): fg[x] = gcd(fg[x-1], al[x]) bg[x] = gcd(bg[x-1], al[n-1-x]) gm = max(bg[n-2], fg[n-2]) for x in range(1,n-1): g = gcd(fg[x-1],bg[n-2-x]) if (g > gm): gm = g print(gm)
p03061
def gcd(a,b): while b!=0: a,b=b,a%b return a N = int(eval(input())) A= list(map(int,input().split())) output=0 for i in range(N): M = [A[i-1]] hoge = A[i] A[i] = A[i-1] for k in range(N): M.append(gcd(M[k],A[k])) if M[N] >= output: output = M[N] A[i]=hoge print(output)
def gcd(a,b): while b!=0: a,b=b,a%b return a N = int(eval(input())) A= list(map(int,input().split())) output=0 KA=[0 for i in range(N)] LA=[0 for i in range(N)] GCD = [] for k in range(1,N): LA[k]=gcd(A[k-1],LA[k-1]) for k in range(1,N): KA[k]=gcd(A[N-k],KA[k-1]) for k in range(N): GCD.append(gcd(LA[k],KA[N-k-1])) output = max(GCD) print(output)
p03061
def gcd(a, b): if b==0: return a else: return gcd(b,a%b) n = int(eval(input())) a = list(map(int, input().split())) ans = a[0] for i in range(n): ans = gcd(i,ans) for i in range(n): if i==0: t = a[1] else: t = a[0] for j in range(n): if j==i: continue t = gcd(a[j],t) if t<=ans: break ans = max(t,ans) print(ans)
def gcd(a, b): if b == 0: return a else: return gcd(b,a%b) n = int(eval(input())) a = list(map(int, input().split())) r = [] l = [] t = a[0] for i in a: t = gcd(t, i) l.append(t) t = a[-1] for i in a[::-1]: t = gcd(t, i) r.append(t) r = r[::-1] ans = 0 for i in range(n): if i == 0: ans = max(ans, r[1]) continue if i == n - 1: ans = max(ans, l[-2]) continue ans = max(ans, gcd(l[i-1], r[i+1])) print(ans)
p03061
import sys def gcd(x, y, *val): val = [x, y] + list(val) count = len(val) if count == 2: mod = val[0] % val[1] if mod == 0: return val[1] elif mod == 1: return 1 else: return gcd(val[1], mod) else: if count % 2 == 1: val.append(val[-1]) return gcd(*[gcd(val[i], val[i + 1]) for i in range(0, count, 2)]) def main(): sinputl = sys.stdin.readline num = int(sinputl().strip()) input = [int(x) for x in sinputl().split()] gcd_max = 1 input_mem = [] if num == 2: print((max(input))) return for i in range(num): mem = input.pop(num - 1 - i) gcd_max = max(gcd_max, gcd(*(input + input_mem))) input_mem.append(mem) print(gcd_max) if __name__ == "__main__": main()
import sys def gcd(x, y, *val): val = sorted([x, y], reverse=True) + list(val) count = len(val) if count == 2: if val[1] == 0: return val[0] mod = val[0] % val[1] if mod == 0: return val[1] elif mod == 1: return 1 else: return gcd(val[1], mod) else: if count % 2 == 1: val.append(val[-1]) return gcd(*[gcd(val[i], val[i + 1]) for i in range(0, count, 2)]) def main(): sinputl = sys.stdin.readline num = int(sinputl().strip()) input = [int(x) for x in sinputl().split()] gcd_max = 1 input_mem = [] if num == 2: print((max(input))) return gcd_left = [0] gcd_right = [0] for i in range(num - 1): gcd_left.append(gcd(input[i], gcd_left[i])) gcd_right.append(gcd(input[-1 - i], gcd_right[i])) gcd_right.reverse() gcd_list = [] for i in range(num): gcd_list.append(gcd(gcd_left[i], gcd_right[i])) print((max(gcd_list))) if __name__ == "__main__": main()
p03061
def gcd(a, b): r = a % b return b if 0 == r else gcd(b, r) def gcd_list(lst): a = lst[0] for i in lst[1:]: a = gcd(a, i) return a n = int(eval(input())) a = list(map(int, input().split())) max_value = max(a) ans = 0 for i in range(n): target = [a[j] for j in range(n)] target.pop(i) ans = max(ans, gcd_list(target)) print(ans)
def gcd(a, b): if 0 == b: return a r = a % b return b if 0 == r else gcd(b, r) def gcd_list(lst): a = lst[0] for i in lst[1:]: a = gcd(a, i) return a n = int(eval(input())) a = list(map(int, input().split())) l = [0 for _ in range(n)] r = [0 for _ in range(n)] for i in range(n-1): l_i = 0 if 0 == i else l[i] l[i+1] = gcd(l_i, a[i]) for i in range(n)[::-1]: r_i1 = 0 if i == n-1 else r[i+1] r[i] = gcd(r_i1, a[i]) m = [] for i in range(n): r_i1 = 0 if i == n-1 else r[i+1] m.append(gcd(l[i], r_i1)) print((max(m)))
p03061
n=int(eval(input())) a=list(map(int,input().split())) def make_divisors(n): lower_divisors , upper_divisors = [], [] i = 1 while i*i <= n: if n % i == 0: lower_divisors.append(i) if i != n // i: upper_divisors.append(n//i) i += 1 return lower_divisors + upper_divisors[::-1] from collections import defaultdict d=defaultdict(int) op=set(make_divisors(a[0])+make_divisors(a[1])) next_op=set() for i in range(n): for opi in op: if a[i]%opi==0: d[opi]+=1 if d[opi]>=i: next_op.add(opi) op=next_op next_op=set() print((max(op)))
n=int(eval(input())) a=list(map(int,input().split())) def gcd(a, b): while b: a, b = b, a%b return a al=[] ar=[] tmp=a[0] al.append(a[0]) for ai in a[1:]: tmp=gcd(ai,tmp) al.append(tmp) ar.append(a[-1]) tmp=a[-1] for ai in a[:-1][::-1]: tmp=gcd(ai,tmp) ar.append(tmp) ans=ar[n-2] for i in range(n-1): ans=max(ans,gcd(al[i],ar[n-i-3])) ans=max(ans,al[n-2]) print(ans)
p03061
from collections import defaultdict N = int(eval(input())) A = list(map(int, input().split())) d = defaultdict(int) s = set([]) def divisor(n): for i in range(1, int(n**0.5)+1): if n % i == 0: s.add(i) s.add(n // i) for i in range(2): divisor(A[i]) for num in A: for key in s: if num % key == 0: d[key] += 1 ans = 1 for key, val in list(d.items()): if val >= N - 1: ans = max(ans, key) print(ans)
N = int(eval(input())) A = list(map(int, input().split())) def ABC125gcd(x, y): if min(x, y) == 0: return max(x, y) else: return ABC125gcd(y, x % y) L = [0] * N R = [0] * N for i in range(1, N): L[i] = ABC125gcd(L[i - 1], A[i - 1]) R[N - 1 - i] = ABC125gcd(R[N - i], A[N - i]) ans = 1 for i in range(N): ans = max(ans, ABC125gcd(L[i], R[i])) print(ans)
p03061
N = int(eval(input())) A = list(map(int, input().split())) from functools import reduce def gcd(a, b): while b: a, b = b, a % b return a def gcd_list(numbers): return reduce(gcd, numbers) m = 0 for i in range(N): if i == 0: tmp = A[1:] elif i == N-1: tmp = A[:N-1] else: tmp = A[:i] + A[i+1:] #print(tmp) a = gcd_list(tmp) if a > m: m = a print(m)
N = int(eval(input())) A = list(map(int, input().split())) def gcd(a, b): while b: a, b = b, a % b return a L = [] R = [] for i in range(N-1): if i == 0: L.append(A[0]) else: L.append(gcd(L[i-1], A[i])) for i in range(N-1): if i == 0: R.append(A[N-1]) else: R.append(gcd(R[i-1], A[N-1-i])) M = [] for i in range(N): if i == 0: M.append(R[N-2]) elif i == N-1: M.append(L[N-2]) else: M.append(gcd(L[i-1], R[N-2-i])) print((max(M)))
p03061
N = int(eval(input())) A = list(map(int, input().split())) A.sort() def divisors(n): div = set() A = 1 while A*A <= n: if n%A == 0: if A*A == n: div.add(A) else: B = n//A div.add(A) div.add(B) A += 1 return div div = list(divisors(A[0]) | divisors(A[1])) m = 0 for x in div: count = 0 for a in A: if a%x != 0: count += 1 if count > 1: break if count <= 1: m = max(m, x) print(m)
N = int(eval(input())) A = list(map(int, input().split())) A.sort() def divisors(n): div = set() A = 1 while A*A <= n: if n%A == 0: if A*A == n: div.add(A) else: B = n//A div.add(A) div.add(B) A += 1 return div div = list(divisors(A[0]) | divisors(A[1])) div.sort(reverse=True) for x in div: count = 0 for a in A: if a%x != 0: count += 1 if count > 1: break if count <= 1: print(x) break
p03061
from math import gcd from functools import reduce n=int(eval(input())) a=list(map(int,input().split())) ans=reduce(gcd,a[1:]) ans=max(ans,reduce(gcd,a[:n-1])) for i in range(1,n-1): ans=max(gcd(reduce(gcd,a[:i]),reduce(gcd,a[i+1:])),ans) print(ans)
from math import* n=int(eval(input())) a=list(map(int,input().split())) b=[a[0]] c=[a[-1]] for i in range(1,n): b+=[gcd(b[-1],a[i])] c+=[gcd(c[-1],a[-i-1])] ans=0 c=c[::-1] for i in range(n): if i==0:ans=max(ans,c[i+1]) elif i==n-1:ans=max(ans,b[i-1]) else: ans=max(ans,gcd(c[i+1],b[i-1])) print(ans)
p03061
# /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(eval(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)))
# /usr/bin/python # -*- coding: utf-8 -*- import sys import math def gcd_ab(a,b): a,b = sorted([a,b]) if a == 0: return b return gcd_ab(b%a, a) if __name__ == "__main__": N = int(eval(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)))
p03061
n = int(eval(input())) a = list(map(int,input().split())) a.sort() 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 divs = make_divisors(a[0]) #list divsTF = [0]*len(divs) for i in range(len(divs)): j = [1 if a[_] % divs[i] else 0 for _ in range(n) ] if sum(j) <= 1: divsTF[i] = 1 ans = 1 for i in range(len(divs)): if divsTF[i] == 1: ans = max(ans,divs[i]) divs2 = make_divisors(a[1]) #list divsTF2 = [0]*len(divs2) for i in range(len(divs2)): j = [1 if a[_] % divs2[i] else 0 for _ in range(n) ] if sum(j) <= 1: divsTF2[i] = 1 aans = 1 for i in range(len(divs2)): if divsTF2[i] == 1: aans = max(aans,divs2[i]) print((max(aans,ans)))
n = int(eval(input())) a = list(map(int,input().split())) a.sort() 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 divs = list(set(make_divisors(a[0])+make_divisors(a[1]))) #list divsTF = [0]*len(divs) for i in range(len(divs)): j = [1 if a[_] % divs[i] else 0 for _ in range(n) ] if sum(j) <= 1: divsTF[i] = 1 ans = 1 for i in range(len(divs)): if divsTF[i] == 1: ans = max(ans,divs[i]) print(ans)
p03061
import sys sys.setrecursionlimit(10**8) def ii(): return int(sys.stdin.readline()) def mi(): return list(map(int, sys.stdin.readline().split())) def li(): return list(map(int, sys.stdin.readline().split())) def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)] def dp2(ini, i, j): return [[ini]*i for _ in range(j)] #import bisect #bisect.bisect_left(B, a) #from collections import defaultdict #d = defaultdict(int) d[key] += value #from collections import Counter # a = Counter(A).most_common() #from itertools import accumulate #list(accumulate(A)) from functools import reduce def segfunc(a,b): if a == 0 or b == 0: return 0 if a < b: a, b = b, a r = a % b if r == 0: return b return segfunc(b, r) def gcd_list(numbers): return reduce(segfunc, numbers) def init(init_val): #set_val for i in range(n): seg[i+num-1]=init_val[i] #built for i in range(num-2,-1,-1) : seg[i]=segfunc(seg[2*i+1],seg[2*i+2]) def update(k,x): k += num-1 seg[k] = x while k: k = (k-1)//2 seg[k] = segfunc(seg[k*2+1],seg[k*2+2]) def query(p,q): if q<=p: return ide_ele p += num-1 q += num-2 res=ide_ele 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 = p//2 q = (q-1)//2 if p == q: res = segfunc(res,seg[p]) else: res = segfunc(segfunc(res,seg[p]),seg[q]) return res n = ii() A = sorted(li()) #####単位元###### if n == 2: print((max(A))) exit() elif n == 3: ide_ele = max(segfunc(A[0], A[1]), segfunc(A[1], A[2]), segfunc(A[0], A[2])) else: ide_ele = max(segfunc(A[-1], A[-2]), segfunc(A[-3], A[-4])) #num:n以上の最小の2のべき乗 num =2**(n-1).bit_length() seg=[ide_ele]*2*num init(A) ans = 0 #ans = max(query(1, n), ans) #for i in range(1, n-1): for i in range(n): #print(i, query(0, i), query(i+1, n)) ans = max(segfunc(query(0, i), query(i+1, n)), ans) #ans = max(query(0, n-1), ans) print(ans)
import sys sys.setrecursionlimit(10**8) def ii(): return int(sys.stdin.readline()) def mi(): return list(map(int, sys.stdin.readline().split())) def li(): return list(map(int, sys.stdin.readline().split())) def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)] def dp2(ini, i, j): return [[ini]*i for _ in range(j)] #import bisect #bisect.bisect_left(B, a) #from collections import defaultdict #d = defaultdict(int) d[key] += value #from collections import Counter # a = Counter(A).most_common() #from itertools import accumulate #list(accumulate(A)) from functools import reduce def segfunc(a,b): if a == 0 or b == 0: return max(a, b) if a < b: a, b = b, a r = a % b if r == 0: return b return segfunc(b, r) def gcd_list(numbers): return reduce(segfunc, numbers) def init(init_val): #set_val for i in range(n): seg[i+num-1]=init_val[i] #built for i in range(num-2,-1,-1) : seg[i]=segfunc(seg[2*i+1],seg[2*i+2]) def update(k,x): k += num-1 seg[k] = x while k: k = (k-1)//2 seg[k] = segfunc(seg[k*2+1],seg[k*2+2]) def query(p,q): if q<=p: return ide_ele p += num-1 q += num-2 res=ide_ele 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 = p//2 q = (q-1)//2 if p == q: res = segfunc(res,seg[p]) else: res = segfunc(segfunc(res,seg[p]),seg[q]) return res n = ii() #A = sorted(li()) A = li() #####単位元###### ''' if n == 2: print(max(A)) exit() elif n == 3: ide_ele = max(segfunc(A[0], A[1]), segfunc(A[1], A[2]), segfunc(A[0], A[2])) else: #ide_ele = max(segfunc(A[-1], A[-2]), segfunc(A[-3], A[-4])) ide_ele = max(segfunc(A[0], A[1]), segfunc(A[2], A[3])) print(segfunc(A[0], A[1]), segfunc(A[2], A[3])) ''' ide_ele = 0 #num:n以上の最小の2のべき乗 num =2**(n-1).bit_length() seg=[ide_ele]*2*num init(A) ans = 0 #ans = max(query(1, n), ans) #for i in range(1, n-1): for i in range(n): #print(i, query(0, i), query(i+1, n)) ans = max(segfunc(query(0, i), query(i+1, n)), ans) #ans = max(query(0, n-1), ans) print(ans)
p03061
import sys sys.setrecursionlimit(10**8) def ii(): return int(sys.stdin.readline()) def mi(): return list(map(int, sys.stdin.readline().split())) def li(): return list(map(int, sys.stdin.readline().split())) def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)] def dp2(ini, i, j): return [[ini]*i for _ in range(j)] #import bisect #bisect.bisect_left(B, a) #from collections import defaultdict #d = defaultdict(int) d[key] += value #from collections import Counter # a = Counter(A).most_common() #from itertools import accumulate #list(accumulate(A)) from functools import reduce def segfunc(a,b): if a == 0 or b == 0: return max(a, b) if a < b: a, b = b, a r = a % b if r == 0: return b return segfunc(b, r) def gcd_list(numbers): return reduce(segfunc, numbers) def init(init_val): #set_val for i in range(n): seg[i+num-1]=init_val[i] #built for i in range(num-2,-1,-1) : seg[i]=segfunc(seg[2*i+1],seg[2*i+2]) def update(k,x): k += num-1 seg[k] = x while k: k = (k-1)//2 seg[k] = segfunc(seg[k*2+1],seg[k*2+2]) def query(p,q): if q<=p: return ide_ele p += num-1 q += num-2 res=ide_ele 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 = p//2 q = (q-1)//2 if p == q: res = segfunc(res,seg[p]) else: res = segfunc(segfunc(res,seg[p]),seg[q]) return res n = ii() #A = sorted(li()) A = li() #####単位元###### ''' if n == 2: print(max(A)) exit() elif n == 3: ide_ele = max(segfunc(A[0], A[1]), segfunc(A[1], A[2]), segfunc(A[0], A[2])) else: #ide_ele = max(segfunc(A[-1], A[-2]), segfunc(A[-3], A[-4])) ide_ele = max(segfunc(A[0], A[1]), segfunc(A[2], A[3])) print(segfunc(A[0], A[1]), segfunc(A[2], A[3])) ''' ide_ele = 0 #num:n以上の最小の2のべき乗 num =2**(n-1).bit_length() seg=[ide_ele]*2*num init(A) ans = 0 #ans = max(query(1, n), ans) #for i in range(1, n-1): for i in range(n): #print(i, query(0, i), query(i+1, n)) ans = max(segfunc(query(0, i), query(i+1, n)), ans) #ans = max(query(0, n-1), ans) print(ans)
import sys sys.setrecursionlimit(10**8) def ii(): return int(sys.stdin.readline()) def mi(): return list(map(int, sys.stdin.readline().split())) def li(): return list(map(int, sys.stdin.readline().split())) def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)] def dp2(ini, i, j): return [[ini]*i for _ in range(j)] #import bisect #bisect.bisect_left(B, a) #from collections import defaultdict #d = defaultdict(int) d[key] += value #from collections import Counter # a = Counter(A).most_common() #from itertools import accumulate #list(accumulate(A)) def gcd(a, b): if a < b: a, b = b, a if b == 0: return a r = a % b return gcd(b, r) N = ii() A = li() forward = [0]*(N+1) backward = [0]*(N+1) forward[0] = 0 for i in range(1, N+1): forward[i] = gcd(A[i-1], forward[i-1]) backward[0] = 0 for i in reversed(list(range(1, N+1))): backward[N+1-i] = gcd(A[i-1], backward[N-i]) #print(forward) #print(backward) ans = 0 for i in range(N): #print(forward[i], forward[N-1-i], ans) ans = max(ans, gcd(forward[i], backward[N-1-i])) print(ans)
p03061
import math import copy from functools import reduce def gcd(*numbers): def gcd(a, b): while b != 0: a, b = b, a % b return a return reduce(gcd, numbers) N = int(eval(input())) A = list(map(int,input().split())) a_max = 0 for i in range(N): new_a = copy.deepcopy(A) new_a.pop(i) a_max = max(a_max,gcd(*new_a)) print(a_max)
def gcd(x,y): if x % y == 0: return y else: x %= y return gcd(y, x) N = int(eval(input())) blackboard = list(map(int,input().split())) l_gcd = [-1] * N r_gcd = [-1] * N for i in range(N): if i == 0: l_gcd [0] = blackboard[0] else: l_gcd[i] = gcd(l_gcd[i-1],blackboard[i]) for i in reversed(list(range(N))): if i == N - 1: r_gcd[N-1] = blackboard[N-1] else: r_gcd[i] = gcd(r_gcd[i+1],blackboard[i]) #左端と右端のときの値を予め比較しておく。 ans = max(l_gcd[-2],r_gcd[1]) for i in range(1,N-1): ans = max(ans,gcd(l_gcd[i-1],r_gcd[i+1])) print(ans)
p03061
import copy N = int(eval(input())) A = list(map(int, input().split())) def gcd(a, b): if b == 0: return a return gcd(b, a % b) F = copy.copy(A) B = copy.copy(A) for i in range(1, N): F[i] = gcd(F[i], F[i-1]) for i in range(N - 2, -1, -1): B[i] = gcd(B[i+1], B[i]) mx = max(F[N-2], B[1]) for i in range(1, N - 1): mx = max(mx, gcd(F[i-1], B[i+1])) print(mx)
import copy def gcd(a, b): if b == 0: return a return gcd(b, a % b) N = int(eval(input())) A = list(map(int, input().split())) F = copy.copy(A) B = copy.copy(A) for i in range(N - 1): F[i + 1] = gcd(F[i + 1], F[i]) for i in range(N - 2, -1, -1): B[i] = gcd(B[i], B[i + 1]) ans = max(F[N-2], B[1]) for i in range(1, N - 1): ans = max(ans, gcd(F[i-1], B[i+1])) print(ans)
p03061
import sys, copy def gcd(x, y): if y == 0: return x else: return gcd(y, x % y) #再帰定義 def listgcd(A): newA = [] if len(A) % 2 == 1: A.append(0) for i in range(0, len(A), 2): newA.append(gcd(A[i], A[i + 1])) if len(newA) == 1: return newA[0] else: return listgcd(newA) input = sys.stdin.readline ''' allinputs = iter(input().splitlines()) input = lambda : next(allinputs) #''' N = int(eval(input())) A = list(map(int, input().split())) ans = 0 for i in range(N): B = copy.copy(A) B[i] = 0 tmp = listgcd(B) if ans < tmp: ans = tmp print(ans)
import sys def gcd(x, y): if y == 0: return x else: return gcd(y, x % y) #再帰定義 input = sys.stdin.readline ''' allinputs = iter(input().splitlines()) input = lambda : next(allinputs) #''' N = int(eval(input())) A = list(map(int, input().split())) L = [0] * N R = [0] * N ans = 0 L[0] = 0 R[0] = 0 for i in range(1, N): L[i] = gcd(L[i - 1], A[i - 1]) R[i] = gcd(R[i - 1], A[N - i]) for i in range(N): tmp = gcd(L[i], R[N - 1 - i]) if tmp > ans: ans = tmp print(ans)
p03061
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] eval(input()) A = list(map(int, input().split())) print((max([reduce_gcd(other_lst(A,i)) for i in range(len(A)) ])))
def gcd(a,b): if ( b==0 ): return a else: return (gcd(b, a%b)) eval(input()) A = list(map(int, input().split())) left_gcd = [A[0]] for i in A[1:]: left_gcd.append(gcd(left_gcd[-1],i)) right_gcd = [A[-1]] for i in reversed(A[0:-1]): right_gcd.append(gcd(right_gcd[-1],i)) right_gcd.reverse() gcd_lst = [right_gcd[1]] + [gcd(right_gcd[i+1], left_gcd[i-1]) for i in range(1,len(A)-1)] + [left_gcd[-2]] print((max(gcd_lst)))
p03061
from math import gcd from copy import copy #================================================ def GCD(A): n=len(A) if n==1: return A[0] return gcd(GCD(A[:n//2]),GCD(A[n//2:])) #================================================ N=int(eval(input())) A=list(map(int,input().split())) B=copy(A) M=0 for i in range(N): B[i]=0 M=max(M,GCD(B)) B[i]=A[i] print(M)
from math import gcd #================================================ N=int(eval(input())) A=list(map(int,input().split())) F=[0]*N G=[0]*N F[0]=A[0] G[-1]=A[-1] for i in range(1,N): F[i]=gcd(F[i-1],A[i]) G[-(i+1)]=gcd(G[-i],A[-(i+1)]) F=[0]+F+[F[-1]] G=[G[0]]+G+[0] M=0 for i in range(N): M=max(M,gcd(F[i],G[i+2])) print(M)
p03061
import sys from functools import lru_cache sys.setrecursionlimit(1000000) @lru_cache(maxsize=None) def main(): def gcd(a, b): if b == 0: return a if a % b == 0: return b else: return gcd(b, a % b) N, *A = list(map(int, sys.stdin.read().split())) L = [0] * (N + 2) R = [0] * (N + 2) for i in range(N): L[i + 2] = gcd(L[i + 1], A[i]) R[-i - 2] = gcd(R[-i - 1], A[-i - 1]) print((max(gcd(L[i], R[i + 1]) for i in range(1, N + 1)))) if __name__=='__main__': main()
import sys from functools import lru_cache sys.setrecursionlimit(1000000) @lru_cache(maxsize=None) def main(): def gcd(a,b): if b>a: a,b=b,a while b: a,b=b, a%b return a def cul(n,x): l=len(x) b=[x[i] for i in range(l)] b.pop(n) tmp=b[0] for i in range(1,l-1): tmp=gcd(tmp,b[i]) return tmp N, *a = list(map(int, sys.stdin.read().split())) p=50 if p>N: p=N ans=0 for i in range(p): s=cul(i,a[:p]) if s>ans: ans=s print(ans) if __name__=='__main__': main()
p03061
import sys from functools import lru_cache sys.setrecursionlimit(1000000) @lru_cache(maxsize=None) def main(): def gcd(a,b): if b>a: a,b=b,a while b: a,b=b, a%b return a def cul(n,x): l=len(x) b=[x[i] for i in range(l)] b.pop(n) tmp=b[0] for i in range(1,l-1): tmp=gcd(tmp,b[i]) return tmp N, *a = list(map(int, sys.stdin.read().split())) p=50 if p>N: p=N ans=0 for i in range(p): s=cul(i,a[:p]) if s>ans: ans=s print(ans) if __name__=='__main__': main()
import sys from functools import lru_cache sys.setrecursionlimit(1000000) @lru_cache(maxsize=None) def main(): def gcd(a, b): if b == 0: return a if a % b == 0: return b else: return gcd(b, a % b) def cul(n,x): l=len(x) b=[x[i] for i in range(l)] b.pop(n) tmp=b[0] for i in range(1,l-1): tmp=gcd(tmp,b[i]) return tmp N, *a = list(map(int, sys.stdin.read().split())) p=50 if p>N: p=N ans=0 for i in range(p): s=cul(i,a[:p]) if s>ans: ans=s print(ans) if __name__=='__main__': main()
p03061
import sys from functools import lru_cache sys.setrecursionlimit(1000000) def main(): @lru_cache(maxsize=None) def gcd(a, b): if a % b == 0: return b else: return gcd(b, a % b) def cul(n,x): l=len(x) b=[x[i] for i in range(l)] b.pop(n) tmp=b[0] for i in range(1,l-1): tmp=gcd(tmp,b[i]) return tmp ans=0 for i in range(p): s=cul(i,a[:p]) if s>ans: ans=s print(ans) if __name__=='__main__': N, *a = list(map(int, sys.stdin.read().split())) p=50 if p>N: p=N main()
import sys sys.setrecursionlimit(1000000) def main(): def gcd(a, b): if a % b == 0: return b else: return gcd(b, a % b) def cul(n,x): l=len(x) b=[x[i] for i in range(l)] b.pop(n) tmp=b[0] for i in range(1,l-1): tmp=gcd(tmp,b[i]) return tmp ans=0 for i in range(p): s=cul(i,a[:p]) if s>ans: ans=s print(ans) if __name__=='__main__': N, *a = list(map(int, sys.stdin.read().split())) p=50 if p>N: p=N main()
p03061
from functools import lru_cache, reduce from bisect import bisect n = int(eval(input())) aa = list(map(int, input().split())) lencache = [] cache = [] @lru_cache(maxsize=10**9) def gcd2(a, b) : if a%b : return gcd2(b, a%b) else : return b @lru_cache(maxsize=10**9) def gcdn(fs) : global cache, lencache l = len(fs) i = bisect(lencache, l) lencache.insert(i, l) cache.insert(i, fs) if l == 1 : return list(fs)[0] elif l < 10 : return reduce(gcd2, fs) else : for c in cache[::-1] : if c in fs : return gcd2(gcdn(c), gcdn(fs-c)) else : c = frozenset({v for i, v in enumerate(fs) if i<len(fs)/2}) return gcd2(gcdn(c), gcdn(fs-c)) m = 1 for i in range(n) : s = frozenset(aa[:i]+aa[i+1:]) g = gcdn(s) if g > m : m = g else : print(m)
from functools import lru_cache n = int(eval(input())) aa = list(map(int, input().split())) @lru_cache(maxsize=10**9) def gcd(a, b) : if b == 0 : return a elif a%b : return gcd(b, a%b) else : return b l = [0] r = [0] for i in range(n) : l.append(gcd(l[-1], aa[i])) r.append(gcd(r[-1], aa[-i-1])) m = [gcd(l[i],r[-i-2]) for i in range(n)] print((max(m)))
p03061
from functools import reduce def gcd(a, b): while b: a, b = b, a % b return a def gcd_n(numbers): return reduce(gcd, numbers) def main(): N, *A = list(map(int, open(0).read().split())) print((max(gcd_n(A[:i] + A[i + 1:]) for i in range(N)))) return main()
def gcd(a, b): if not a: # a が ゼロ ならば b return b if not b: # b が ゼロ ならば a return a while b: a, b = b, a % b return a def main(): N, *A = list(map(int, open(0).read().split())) L = [0] * N R = L[:] R.append(0) for i in range(N - 1): L[i + 1] = gcd(L[i], A[i]) for i in range(N - 1, -1, -1): R[i] = gcd(R[i + 1], A[i]) print((max(gcd(L[i], R[i + 1]) for i in range(N)))) return main()
p03061
# coding: utf-8 # Your code here! def euclid(a,b): a,b=max(a,b),min(a,b) if a%b==0: return b else: a,b=b,a%b return euclid(a,b) N=int(eval(input())) A=list(map(int,input().split())) org_A=A[:] #左から最大公約数作る右もね ans=0 for i in range(N): l=A[:i] for j in range(len(l)-1): l[j+1]=euclid(l[j+1],l[j]) r=A[i+1:] for j in range(len(r)-1): r[-j-2]=euclid(r[-j-1],r[-j-2]) ans=max(ans,euclid(r[0] if len(r)>0 else l[-1],l[-1] if len(l)>0 else r[0])) print(ans)
# coding: utf-8 # Your code here! def euclid(a,b): a,b=max(a,b),min(a,b) if a%b==0: return b else: a,b=b,a%b return euclid(a,b) N=int(eval(input())) A=list(map(int,input().split())) org_A=A[:] #左から最大公約数作る右もね A=A[::-1] l=[A.pop(-1)] while A: l.append(euclid(l[-1],A.pop(-1))) A=org_A r=[A.pop(-1)] while A: r.append(euclid(r[-1],A.pop(-1))) r=r[::-1] ans=0 #print(l,r) for i in range(N): temp=euclid(l[i-1] if i!=0 else r[i+1],r[i+1] if i!=N-1 else l[i-1]) #print(l[i-1] if i!=0 else r[i+1],r[i+1] if i!=N-1 else l[i-1]) ans=max(ans,temp) print(ans)
p03061