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(g...
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[...
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...
#!/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] fo...
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 ...
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): ...
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 ...
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.ap...
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 = [num...
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 -...
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 -...
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): whil...
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:c...
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]左側の最大公約数を求める ...
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 =...
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+...
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...
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...
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] =...
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] fo...
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 ret...
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.app...
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): ...
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...
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)): ...
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.appen...
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): ...
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] f...
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 i...
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 ra...
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...
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(l...
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...
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[...
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[...
#!/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/kyur...
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: f...
# -*- 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] = ...
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: ...
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-...
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.app...
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: ...
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 ...
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 =...
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=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=...
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 ran...
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]...
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: ...
#!/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): le...
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 = divi...
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 ...
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(m...
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], A...
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 ...
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...
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 = m...
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): ...
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 ...
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): ...
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]))) pri...
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 red...
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(g...
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個しか消さない...
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...
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...
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...
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] ...
# -*- 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() ...
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 ...
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 ...
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...
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): ...
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...
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] ...
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]) ...
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, ...
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....
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 ...
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 ...
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 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 ...
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 whil...
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 ...
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...
# -*- 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 in...
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 in...
# -*- 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 in...
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 in...
# -*- 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 in...
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 ...
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...
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 r...
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): ...
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...
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 i...
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 i...
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 ...
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,...
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,...
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(max...
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(max...
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], ...
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): ...
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],...
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 ...
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: ...
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: ...
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) ...
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, s...
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, s...
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...
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 ...
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: ...
# 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: ...
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...
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): ...
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) ...
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...
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(fun...
# 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 g...
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 ...
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...
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 ...
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 ...
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 ...
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 ...
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...
# -*- 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]...
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(commo...
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]) de...
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 -...
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]) ...
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...
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]) f...
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: ...
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...
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...
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 ...
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 ...
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 ...
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)...
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)...
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 ...
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...
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 ...
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 ...
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: ...
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.app...
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) ...
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) ...
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])) p...
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())) ...
# /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)...
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 div...
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 div...
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): ret...
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): ret...
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): ret...
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): ret...
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.deepc...
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] = gc...
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]) ...
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], ...
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...
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...
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(in...
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))...
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...
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...
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(...
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)] ...
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)] ...
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) ...
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)] ...
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): ...
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 cac...
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],...
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...
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(...
# 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[-...
p03061