input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
# -*- coding: utf-8 -*- import copy import queue N = int(eval(input())) a_list = list(map(int, input().split())) gain = sum([a for a in a_list if a > 0]) # Flow network S = 0 T = N + 1 c = [{} for i in range(N + 2)] for i, a in enumerate(a_list): index = i + 1 if a <= 0: c[S][index] = -a else: c[index][T] = a for j in range(index, N + 1, index): if not j == index: c[index][j] = 10e15 # Residual network r = copy.deepcopy(c) # Edmonds-Karp algorithm max_flow = 0 while True: # Find path to T q, s, p = queue.Queue(), {S}, None q.put((S,)) findPath = False while not q.empty() and not findPath: cand_path = q.get(False) for to, path in list(r[cand_path[-1]].items()): if path == 0: continue elif to == T: p = cand_path + (to,) findPath = True elif not to in s: q.put(cand_path + (to,)) s.add(to) if not findPath: break # Minimum flow min_flow = min([r[p[i]][p[i+1]] for i in range(len(p)-1)]) max_flow += min_flow for i in range(len(p)-1): r[p[i]][p[i+1]] -= min_flow if p[i] in r[p[i+1]]: r[p[i+1]][p[i]] += min_flow else: r[p[i+1]][p[i]] = min_flow print((gain - max_flow))
import copy import collections N = int(eval(input())) A = list(map(int, input().split())) gain = sum([max(a, 0) for a in A]) # Flow network S, T = 0, N + 1 c = [{} for i in range(N + 2)] for i in range(N): ix = i + 1 if A[i] <= 0: c[S][ix] = -A[i] else: c[ix][T] = A[i] for j in range(2 * ix, N + 1, ix): c[ix][j] = 10e15 # Residual network r = copy.deepcopy(c) # Edmonds-Karp algorithm max_flow = 0 while True: # Find path to T q, s, p = collections.deque(), {S}, None q.append((S,)) findPath = False while not len(q) == 0 and not findPath: cand_path = q.popleft() for to, path in list(r[cand_path[-1]].items()): if path == 0: continue elif to == T: p = cand_path + (to,) findPath = True elif not to in s: q.append(cand_path + (to,)) s.add(to) if not findPath: break # Minimum flow min_flow = min([r[p[i]][p[i + 1]] for i in range(len(p) - 1)]) max_flow += min_flow for i in range(len(p) - 1): r[p[i]][p[i + 1]] -= min_flow if p[i] in r[p[i + 1]]: r[p[i + 1]][p[i]] += min_flow else: r[p[i + 1]][p[i]] = min_flow print((gain - max_flow))
p03553
from collections import * 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())) class Dinic(): def __init__(self, n, s, t): self.n, self.s, self.t = n, s, t self.to = defaultdict(list) self.level = [-1] self.max_flow = -1 def add_edge(self, u, v, cap): u_index_in_to_v = len(self.to[v]) v_index_in_to_u = len(self.to[u]) self.to[u].append([v, cap, u_index_in_to_v]) self.to[v].append([u, 0, v_index_in_to_u]) def set_level(self): self.level = [-1] * self.n q = deque() q.append([self.s, 0]) while q: u, u_level = q.popleft() self.level[u] = u_level if u == self.t: return True for v, cap, _ in self.to[u]: if self.level[v] != -1: continue if cap == 0: continue q.append([v, u_level + 1]) return False def dfs(self, u=-1, flow_to_me=10**16): if u == -1: u = self.s if u == self.t: return flow_to_me flow_from_me = 0 u_level = self.level[u] for utov_i, (v, cap, vtou_i) in enumerate(self.to[u]): if self.level[v] != u_level + 1: continue if cap == 0: continue flow_to_v = self.dfs(v, min(cap, flow_to_me - flow_from_me)) if not flow_to_v: continue flow_from_me += flow_to_v self.to[u][utov_i][1] -= flow_to_v self.to[v][vtou_i][1] += flow_to_v return flow_from_me def calculation(self): res = 0 while self.set_level(): res += self.dfs() return res def get_max_flow(self): if self.max_flow == -1: self.max_flow = self.calculation() return self.max_flow def main(): n=int(input()) aa=LI() mf=Dinic(n+2,0,n+1) max_sum=0 for i,a in enumerate(aa,1): if a>0: mf.add_edge(i,n+1,a) max_sum+=a else: mf.add_edge(0,i,-a) for i in range(1,n//2+1): for j in range(i*2,n+1,i): mf.add_edge(i,j,10**12) print(max(0,max_sum-mf.get_max_flow())) main()
from collections import * 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())) class Dinic(): def __init__(self, n, s, t): self.n, self.s, self.t = n, s, t self.to = defaultdict(list) self.level = [-1] self.max_flow = -1 def add_edge(self, u, v, cap): u_index_in_to_v = len(self.to[v]) v_index_in_to_u = len(self.to[u]) self.to[u].append([v, cap, u_index_in_to_v]) self.to[v].append([u, 0, v_index_in_to_u]) def set_level(self): s = self.s level = [-1] * self.n level[s] = 0 q = deque() q.append([s, 0]) while q: u, u_level = q.popleft() for v, cap, _ in self.to[u]: if cap == 0: continue if level[v] != -1: continue level[v] = u_level + 1 if v == self.t: self.level=level return True q.append([v, u_level + 1]) return False def dfs(self, u=-1, flow_to_me=10 ** 16): if u == -1: u = self.s if u == self.t: return flow_to_me flow_from_me = 0 u_level = self.level[u] for utov_i, (v, cap, vtou_i) in enumerate(self.to[u]): if self.level[v] != u_level + 1: continue if cap == 0: continue flow_to_v = self.dfs(v, min(cap, flow_to_me - flow_from_me)) if not flow_to_v: continue flow_from_me += flow_to_v self.to[u][utov_i][1] -= flow_to_v self.to[v][vtou_i][1] += flow_to_v return flow_from_me def calculation(self): res = 0 while self.set_level(): res += self.dfs() return res # これが出力用 def get_max_flow(self): if self.max_flow == -1: self.max_flow = self.calculation() return self.max_flow def main(): n = int(input()) aa = LI() mf = Dinic(n + 2, 0, n + 1) max_sum = 0 for i, a in enumerate(aa, 1): if a > 0: mf.add_edge(i, n + 1, a) max_sum += a else: mf.add_edge(0, i, -a) for i in range(1, n // 2 + 1): for j in range(i * 2, n + 1, i): mf.add_edge(i, j, 10 ** 12) print(max(0, max_sum - mf.get_max_flow())) main()
p03553
from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify import math import bisect import random from itertools import permutations, accumulate, combinations, product import sys import string from bisect import bisect_left, bisect_right from math import factorial, ceil, floor from operator import mul from functools import reduce sys.setrecursionlimit(2147483647) INF = 10 ** 20 def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def I(): return int(sys.stdin.buffer.readline()) def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] def SRL(n): return [list(S()) for i in range(n)] def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] mod = 1000000007 class Dinic(): def __init__(self, source, sink): self.G = defaultdict(lambda:defaultdict(int)) self.sink = sink self.source = source def add_edge(self, u, v, cap): self.G[u][v] = cap self.G[v][u] = 0 def bfs(self): level = defaultdict(int) q = [self.source] level[self.source] = 1 d = 1 while q: if level[self.sink]: break qq = [] d += 1 for u in q: for v, cap in list(self.G[u].items()): if cap == 0: continue if level[v]: continue level[v] = d qq += [v] q = qq self.level = level def dfs(self, u, f): if u == self.sink: return f for v, cap in self.iter[u]: if cap == 0 or self.level[v] != self.level[u] + 1: continue d = self.dfs(v, min(f, cap)) if d: self.G[u][v] -= d self.G[v][u] += d return d return 0 def max_flow(self): flow = 0 while True: self.bfs() if self.level[self.sink] == 0: break self.iter = {u: iter(list(self.G[u].items())) for u in self.G} while True: f = self.dfs(self.source, INF) if f == 0: break flow += f return flow n = I() A = LI() score = 0 s = 0 t = n + 1 dinic = Dinic(s, t) for i in range(1, n + 1): if A[i - 1] > 0: score += A[i - 1] dinic.add_edge(s, i, 0) dinic.add_edge(i, t, A[i - 1]) else: dinic.add_edge(s, i, -A[i - 1]) dinic.add_edge(i, t, 0) ret = i while ret + i <= n: ret += i dinic.add_edge(i, ret, INF) print((score - dinic.max_flow()))
from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify import math import bisect import random from itertools import permutations, accumulate, combinations, product import sys import string from bisect import bisect_left, bisect_right from math import factorial, ceil, floor from operator import mul from functools import reduce sys.setrecursionlimit(2147483647) INF = 10 ** 20 def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def I(): return int(sys.stdin.buffer.readline()) def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] def SRL(n): return [list(S()) for i in range(n)] def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] mod = 1000000007 class Dinic(): def __init__(self, source, sink): self.G = defaultdict(lambda:defaultdict(int)) self.sink = sink self.source = source def add_edge(self, u, v, cap): self.G[u][v] = cap self.G[v][u] = 0 def bfs(self): level = defaultdict(int) q = [self.source] level[self.source] = 1 d = 1 while q: if level[self.sink]: break qq = [] d += 1 for u in q: for v, cap in list(self.G[u].items()): if cap == 0: continue if level[v]: continue level[v] = d qq += [v] q = qq self.level = level def dfs(self, u, f): if u == self.sink: return f for v, cap in self.iter[u]: if cap == 0 or self.level[v] != self.level[u] + 1: continue d = self.dfs(v, min(f, cap)) if d: self.G[u][v] -= d self.G[v][u] += d return d return 0 def max_flow(self): flow = 0 while True: self.bfs() if self.level[self.sink] == 0: break self.iter = {u: iter(list(self.G[u].items())) for u in self.G} while True: f = self.dfs(self.source, INF) if f == 0: break flow += f return flow n = I() s = 0 t = n + 1 dinic = Dinic(s, t) A = LI() total = 0 for i in range(1, n + 1): if A[i - 1] >= 0: total += A[i - 1] dinic.add_edge(s, i, 0) dinic.add_edge(i, t, A[i - 1]) else: dinic.add_edge(s, i, -A[i - 1]) dinic.add_edge(i, t, 0) for j in range(i * 2, n + 1, i): dinic.add_edge(i, j, INF) print((total - dinic.max_flow()))
p03553
#!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 #A def A(): return #B def B(): return #C def C(): def bfs(s,g,n): bfs_map = [-1 for i in range(n)] bfs_map[s] = 0 q = deque() q.append(s) fin = False while q: x = q.popleft() for y in v[x]: if c[x][y] > 0 and bfs_map[y] < 0: bfs_map[y] = bfs_map[x]+1 if y == g: fin = True break q.append(y) if fin: break if bfs_map[g] == -1: return None,0 path = [None]*(bfs_map[g]+1) m = float("inf") path[bfs_map[g]] = g y = g for i in range(bfs_map[g])[::-1]: for x in v[y]: if c[x][y] > 0 and bfs_map[x] == bfs_map[y]-1: path[i] = x if c[x][y] < m: m = c[x][y] y = x break return path,m def ford_fulkerson(s,g,c,n): f = 0 while 1: p,m = bfs(s,g,n) if not m:break f += m for i in range(len(p)-1): c[p[i]][p[i+1]] -= m c[p[i+1]][p[i]] += m return f n = I() a = LI() e = n+2 ma = 10**9 c = [[0]*e for i in range(e)] v = [[] for i in range(e)] for i in range(n): d = i+1 j = 2*d c[0][d] = ma v[0].append(d) v[d].append(0) c[d][n+1] = ma-a[i] v[d].append(n+1) v[n+1].append(d) while j <= n: c[j][d] = float("inf") v[j].append(d) v[d].append(j) j += d print((ma*n-ford_fulkerson(0,n+1,c,e))) return #D def D(): return #E def E(): return #F def F(): return #Solve if __name__ == "__main__": C()
#!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop import sys import math import bisect import random def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 #A def A(): return #B def B(): return #C def C(): def bfs(s,g,n): bfs_map = [-1 for i in range(n)] bfs_map[s] = 0 q = deque() q.append(s) fin = False while q: x = q.popleft() for y in range(n): if c[x][y] > 0 and bfs_map[y] < 0: bfs_map[y] = bfs_map[x]+1 if y == g: fin = True break q.append(y) if fin: break if bfs_map[g] == -1: return None,0 path = [None]*(bfs_map[g]+1) m = float("inf") path[bfs_map[g]] = g y = g for i in range(bfs_map[g])[::-1]: for x in range(n): if c[x][y] > 0 and bfs_map[x] == bfs_map[y]-1: path[i] = x if c[x][y] < m: m = c[x][y] y = x break return path,m def ford_fulkerson(s,g,c,n): f = 0 while 1: p,m = bfs(s,g,n) if not m:break f += m for i in range(len(p)-1): c[p[i]][p[i+1]] -= m c[p[i+1]][p[i]] += m return f n = I() a = LI() e = n+2 ma = 10**9 c = [[0]*e for i in range(e)] for i in range(n): d = i+1 j = 2*d c[0][d] = ma c[d][n+1] = ma-a[i] while j <= n: c[j][d] = float("inf") j += d print((ma*n-ford_fulkerson(0,n+1,c,e))) return #D def D(): return #E def E(): return #F def F(): return #Solve if __name__ == "__main__": C()
p03553
class Dinic: def __init__(self, v, inf=float("inf")): self.V = v self.inf = inf self.G = [[] for _ in range(v)] self.level = [0 for _ in range(v)] self.iter = [0 for _ in range(v)] def addEdge(self, fm, to, cap): ''' to:行き先 cap:容量 rev:反対側の辺 ''' self.G[fm].append({'to':to, 'cap':cap, 'rev':len(self.G[to])}) self.G[to].append({'to':fm, 'cap':0, 'rev':len(self.G[fm])-1}) # sからの最短距離をbfsで計算 def bfs(self, s): import queue self.level = [-1 for _ in range(self.V)] self.level[s] = 0 que = queue.Queue() que.put(s) while not que.empty(): v = que.get() for i in range(len(self.G[v])): e = self.G[v][i] if e['cap'] > 0 and self.level[e['to']] < 0: self.level[e['to']] = self.level[v] + 1 que.put(e['to']) def dfs(self, v, t, f): if v == t: return f for i in range(self.iter[v], len(self.G[v])): self.iter[v] = i e = self.G[v][i] if e['cap'] > 0 and self.level[v] < self.level[e['to']]: d = self.dfs(e['to'], t ,min(f,e['cap'])) if d > 0: e['cap'] -= d self.G[e['to']][e['rev']]['cap'] += d return d return 0 def max_flow(self,s,t): flow = 0 while True: self.bfs(s) if self.level[t] < 0: return flow self.iter = [0 for _ in range(self.V)] f = self.dfs(s,t,self.inf) while f > 0: flow += f f = self.dfs(s,t,self.inf) from sys import stdin, setrecursionlimit def IL():return list(map(int, stdin.readline().split())) setrecursionlimit(1000000) def main(): N = int(eval(input())) a = IL() d = Dinic(N+2) res = 0 for i in range(N): d.addEdge(N,i, max(0, -a[i])) d.addEdge(i,N+1, max(0,a[i])) res+=max(0,a[i]) t = 2*i+2 while t<=N: d.addEdge(i,t-1,float('inf')) t+=i+1 print((res-d.max_flow(N,N+1))) if __name__ == "__main__": main()
class Ford_Fulkerson: def __init__(self, v, inf=float("inf")): self.V = v self.inf = inf self.G = [[] for _ in range(v)] self.used = [False for _ in range(v)] def addEdge(self, fm, to, cap): ''' to:行き先 cap:容量 rev:反対側の辺 ''' self.G[fm].append({'to':to, 'cap':cap, 'rev':len(self.G[to])}) self.G[to].append({'to':fm, 'cap':0, 'rev':len(self.G[fm])-1}) def dfs(self, v, t, f): if v == t: return f self.used[v] = True for i in range(len(self.G[v])): e = self.G[v][i] if self.used[e["to"]] != True and e['cap'] > 0: d = self.dfs(e['to'], t ,min(f, e['cap'])) if d > 0: e['cap'] -= d self.G[e['to']][e['rev']]['cap'] += d return d return 0 def max_flow(self,s,t): flow = 0 while True: self.used = [False for i in range(self.V)] f = self.dfs(s,t,self.inf) if f == 0: return flow flow += f from sys import stdin, setrecursionlimit def IL():return list(map(int, stdin.readline().split())) setrecursionlimit(1000000) def main(): N = int(eval(input())) a = IL() d = Ford_Fulkerson(N+2) res = 0 for i in range(N): d.addEdge(N,i, max(0, -a[i])) d.addEdge(i,N+1, max(0,a[i])) res+=max(0,a[i]) t = 2*i+2 while t<=N: d.addEdge(i,t-1,float('inf')) t+=i+1 print((res-d.max_flow(N,N+1))) if __name__ == "__main__": main()
p03553
# Dinic's algorithm from collections import deque class Dinic: def __init__(self, N): self.N = N self.G = [[] for i in range(N)] def add_edge(self, fr, to, cap): forward = [to, cap, None] forward[2] = backward = [fr, 0, forward] self.G[fr].append(forward) self.G[to].append(backward) def add_multi_edge(self, v1, v2, cap1, cap2): edge1 = [v2, cap1, None] edge1[2] = edge2 = [v1, cap2, edge1] self.G[v1].append(edge1) self.G[v2].append(edge2) def bfs(self, s, t): self.level = level = [None]*self.N deq = deque([s]) level[s] = 0 G = self.G while deq: v = deq.popleft() lv = level[v] + 1 for w, cap, _ in G[v]: if cap and level[w] is None: level[w] = lv deq.append(w) return level[t] is not None def dfs(self, v, t, f): if v == t: return f level = self.level for e in self.it[v]: w, cap, rev = e if cap and level[v] < level[w]: d = self.dfs(w, t, min(f, cap)) if d: e[1] -= d rev[1] += d return d return 0 def flow(self, s, t): flow = 0 INF = 10**9 + 7 G = self.G while self.bfs(s, t): *self.it, = list(map(iter, self.G)) f = INF while f: f = self.dfs(s, t, INF) flow += f return flow import sys sys.setrecursionlimit(10 ** 7) input = sys.stdin.readline n = int(eval(input())) a = list( map(int, input().split())) score = 0 INF = float('inf') graph = Dinic(n+2) for i in range(n): if a[i]>0: graph.add_edge(i+1,n+1,a[i]) score += a[i] elif a[i]<0: graph.add_edge(0,i+1,-a[i]) for i in range(1,n//2+1): for j in range(2*i,n+1,i): graph.add_edge(i,j,INF) print((score-graph.flow(0,n+1)))
from collections import deque class Dinic: def __init__(self, N): self.N = N self.G = [[] for i in range(N)] def add_edge(self, fr, to, cap): forward = [to, cap, None] forward[2] = backward = [fr, 0, forward] self.G[fr].append(forward) self.G[to].append(backward) def add_multi_edge(self, v1, v2, cap1, cap2): edge1 = [v2, cap1, None] edge1[2] = edge2 = [v1, cap2, edge1] self.G[v1].append(edge1) self.G[v2].append(edge2) def bfs(self, s, t): self.level = level = [None]*self.N deq = deque([s]) level[s] = 0 G = self.G while deq: v = deq.popleft() lv = level[v] + 1 for w, cap, _ in G[v]: if cap and level[w] is None: level[w] = lv deq.append(w) return level[t] is not None def dfs(self, v, t, f): if v == t: return f level = self.level for e in self.it[v]: w, cap, rev = e if cap and level[v] < level[w]: d = self.dfs(w, t, min(f, cap)) if d: e[1] -= d rev[1] += d return d return 0 def flow(self, s, t): flow = 0 INF = 10**9 + 7 G = self.G while self.bfs(s, t): *self.it, = list(map(iter, self.G)) f = INF while f: f = self.dfs(s, t, INF) flow += f return flow import sys sys.setrecursionlimit(10 ** 7) input = sys.stdin.readline n = int(eval(input())) a = list( map(int, input().split())) score = 0 INF = float('inf') graph = Dinic(n+2) for i in range(n): if a[i]>0: graph.add_edge(i+1,n+1,a[i]) score += a[i] elif a[i]<0: graph.add_edge(0,i+1,-a[i]) for i in range(1,n//2+1): for j in range(2*i,n+1,i): graph.add_edge(i,j,INF) print((score-graph.flow(0,n+1)))
p03553
import sys def generate_next_hexes(x, y): hexes = list() hexes.append((x, y - 1)) hexes.append((x, y + 1)) hexes.append((x - 1, y)) hexes.append((x + 1, y)) if y % 2: hexes.append((x - 1, y - 1)) hexes.append((x - 1, y + 1)) else: hexes.append((x + 1, y - 1)) hexes.append((x + 1, y + 1)) return hexes def update_map(hex_map, hexes): num_updated_hexes = 0 distance = 0 while hexes: next_hexes = [] for pos in hexes: x = pos[0] y = pos[1] if (1 <= x <= m and 1 <= y <= n and (pos not in hex_map or distance < hex_map[pos])): hex_map[pos] = distance num_updated_hexes += 1 next_hexes += generate_next_hexes(x, y) distance += 1 hexes = next_hexes return num_updated_hexes while True: (m, n) = [int(i) for i in sys.stdin.readline().split()] if m == n == 0: break s = int(sys.stdin.readline()) stores = [] for i in range(s): cord = [int(j) for j in sys.stdin.readline().split()] stores.append(tuple(cord)) hex_map = {} update_map(hex_map, stores) t = int(sys.stdin.readline()) candidates = [] for i in range(t): cord = [int(j) for j in sys.stdin.readline().split()] candidates.append(tuple(cord)) # search max num max_num_blocks = 0 for candidate in candidates: new_hex_map = hex_map.copy() num_blocks = update_map(new_hex_map, [candidate]) max_num_blocks = max(max_num_blocks, num_blocks) print(max_num_blocks)
import sys def generate_next_hexes(x, y): hexes = [(x, y - 1), (x, y + 1), (x - 1, y), (x + 1, y)] if y % 2: hexes += [(x - 1, y - 1), (x - 1, y + 1)] else: hexes += [(x + 1, y - 1), (x + 1, y + 1)] return hexes def update_map(hex_map, hexes): num_updated_hexes = 0 distance = 0 while hexes: next_hexes = [] for pos in hexes: if (1 <= pos[0] <= m and 1 <= pos[1] <= n) and (pos not in hex_map or distance < hex_map[pos]): hex_map[pos] = distance num_updated_hexes += 1 next_hexes += generate_next_hexes(pos[0], pos[1]) distance += 1 hexes = next_hexes return num_updated_hexes while True: (m, n) = [int(i) for i in sys.stdin.readline().split()] if m == n == 0: break s = int(sys.stdin.readline()) stores = [] for i in range(s): pos = [int(j) for j in sys.stdin.readline().split()] stores.append(tuple(pos)) hex_map = {} update_map(hex_map, stores) t = int(sys.stdin.readline()) candidates = [] for i in range(t): pos = [int(j) for j in sys.stdin.readline().split()] candidates.append(tuple(pos)) max_num_blocks = 0 for candidate in candidates: new_hex_map = hex_map.copy() num_blocks = update_map(new_hex_map, [candidate]) max_num_blocks = max(max_num_blocks, num_blocks) print(max_num_blocks)
p00193
Q, H, S, D = list(map(int, input().split())) N = int(eval(input())) ans1 = 4 * Q * N ans2 = 2 * H * N ans3 = S * N if N < 2: ans4 = float('INF') elif N % 2 == 0: ans4 = (N // 2) * D else: t = (N // 2) * D ans4 = t + min(4 * Q, 2 * H, 1 * S) # print(ans1, ans2, ans3, ans4) ans = min(ans1, ans2, ans3, ans4) print(ans)
Q, H, S, D = list(map(int, input().split())) N = int(eval(input())) t = min(4*Q, 2*H, S) if D < 2 * t: if N % 2 == 0: ans = (N // 2) * D else: ans = (N // 2) * D + t else: ans = N * t print(ans)
p03617
q, h, s, d = [int(i) for i in input().split()] n = int(eval(input())) d = min(min(d, s*2), min(h*4, q*8)) s = min(s, min(h*2, q*4)) h = min(h, q*2) ans = (n // 2) * d n %= 2 while n > 0: if n >= 1: n -= 1 ans += s elif n >= 0.5: n -= 0.5 ans += h else: n -= 0.25 ans += q print(ans)
q, h, s, d = [int(i) for i in input().split()] n = int(eval(input())) d = min(min(d, s*2), min(h*4, q*8)) s = min(s, min(h*2, q*4)) print(((n // 2) * d + (n % 2) * s))
p03617
Q,H,S,D = (int(i) for i in input().split()) N = int(eval(input())) q = Q*4 h = H*2 s = S d = D/2 if min(q,h,s,d)==d: if N%2 == 0: money = D*(N//2) else: money = (N//2)*D + min(q,h,s) else: money = min(q,h,s)*N print((int(money)))
Q,H,S,D = (int(i) for i in input().split()) N = int(eval(input())) Qper025L = Q Hper025L = H / 2 Sper025L = S / 4 Dper025L = D / 8 teainfo = [["Q",Qper025L,Q,0.25],["H",Hper025L,H,0.50],["S",Sper025L,S,1],["D",Dper025L,D,2]] sortedtea = sorted(teainfo, key = lambda x:(x[1],x[2]), reverse = False) price = 0 for i in range(4): if sortedtea[i][3] <= N: numbottles = int(N / sortedtea[i][3]) N -= sortedtea[i][3] * numbottles price += sortedtea[i][2] * numbottles print((int(price)))
p03617
q,h,s,d = list(map(int,input().split())) n = int(eval(input())) twoL = min(q*8,4*h,2*s,d) oneL = min(q*4,2*h,s) p = (n//2)*twoL + (n%2)*oneL print(p)
Q,H,S,D=list(map(int,input().split())) N=int(eval(input())) ans=0 #each 2L ans+=(N//2)*min([8*Q,4*H,2*S,D]) #1L ans+=(N%2)*min([4*Q,2*H,S]) print(ans)
p03617
q, h, s, d = list(map(int, input().split())) n = int(eval(input())) l = [4 * q, 2 * h, s, d / 2] ans = 0 if n >= 2: a = l.index(min(l)) if a == 0: ans = 4 * q * n elif a == 1: ans = 2 * h * n elif a == 2: ans = s * n elif a == 3: if n % 2 == 0: ans = n // 2 * d elif n % 2 == 1: ans = n // 2 * d b = l.index(min(l[:3])) if b == 0: ans += 4 * q elif b == 1: ans += 2 * h elif b == 2: ans += s else: b = l.index(min(l[:3])) if b == 0: ans = 4 * q * n elif b == 1: ans = 2 * h * n elif b == 2: ans = s * n print(ans)
#-*-coding:utf-8-*- import sys input=sys.stdin.readline def main(): quota,half,single,double = list(map(int,input().split())) want_icetea=int(eval(input())) cost_list = [4 * quota, 2 * half, single, double / 2] ans=0 if want_icetea >= 2: idx1 = cost_list.index(min(cost_list)) if idx1 == 0: ans = 4 * quota * want_icetea elif idx1 == 1: ans = 2 * half * want_icetea elif idx1 == 2: ans = single * want_icetea elif idx1 == 3: if want_icetea % 2 == 0: ans = want_icetea // 2 * double elif want_icetea % 2 == 1: ans = want_icetea // 2 * double idx2 = cost_list.index(min(cost_list[:3])) if idx2 == 0: ans += 4 * quota elif idx2 == 1: ans += 2 * half elif idx2 == 2: ans += single else: idx2 = cost_list.index(min(cost_list[:3])) if idx2 == 0: ans = 4 * quota * want_icetea elif idx2 == 1: ans = 2 * half * want_icetea elif idx2 == 2: ans = single * want_icetea print(ans) if __name__=="__main__": main()
p03617
cost = list(map(int, input().split())) size = [1, 2, 4, 8] N = int(eval(input())) * 4 ans = 0 while N > 0: min_ = (N // size[0]) * cost[0] q = 0 for i in range(1,4): tmp = (N//size[i]) * cost[i] if tmp < min_ and tmp != 0: min_ = tmp q = i ans += min_ N = N%size[q] print(ans)
import sys input = sys.stdin.readline def main(): Q, H, S, D = list(map(int, input().split())) N = int(eval(input())) c1 = min(Q*4, H*2, S) c2 = min(c1*2, D) ans = 0 ans += c2 * (N//2) rest = int(N%2) ans += c1 * rest print(ans) if __name__ == "__main__": main()
p03617
Q,H,S,D=list(map(int,input().split())) N=int(eval(input())) Min1=min(4*Q,2*H,S) Min2=min(2*Min1,D) Even=N//2 Odd=N%2 ans=Even*Min2+Odd*Min1 print(ans)
Q,H,S,D=list(map(int,input().split())) N=int(eval(input())) N2,N1=divmod(N,2) tmp2=min(8*Q,4*H,2*S,D) tmp1=min(4*Q,2*H,S) ans2=N2*tmp2 ans1=N1*tmp1 ans=ans1+ans2 print(ans)
p03617
q,h,s,d = list(map(int,input().split())) n = int(eval(input())) m = min(8*q,4*h,2*s,d)*(n//2) + min(4*q,2*h,s)*(n%2) print(m)
q,h,s,d=list(map(int,input().split())) n=int(eval(input())) m=n//2*min(8*q,4*h,2*s,d)+n%2*min(4*q,2*h,s) print(m)
p03617
Q, H, S, D = list(map(int,input().split())) N = int(eval(input())) print((min(D,2*S,4*H,8*Q)*(N//2)+min(S,2*H,4*Q)*(N%2)))
import sys def input(): return sys.stdin.readline()[:-1] def main(): Q, H, S, D = list(map(int,input().split())) N = int(eval(input())) print(((N//2)*min(Q*8,H*4,S*2,D)+(N%2)*min(Q*4,H*2,S))) if __name__ == '__main__': main()
p03617
# -*- coding: utf-8 -*- q,h,s,d = list(map(int,input().split())) n = int(eval(input())) q = 4*q h = 2*h if n%2==0: t = q*n t = min(t, h*n) t = min(t, s*n) t = min(t, d*n//2) else: t = q*n t = min(t, h*n) t = min(t, s*n) t = min(t, (n//2)*d + min(q,h,s)) print(t)
q,h,s,d = list(map(int, input().split())) n = int(eval(input())) q = 4*q h = 2*h x = min(q,h,s) print(((n//2)*min(2*x,d)+(n%2)*x))
p03617
import math import fractions import bisect import collections import itertools import heapq import string import sys import copy from decimal import * from collections import deque sys.setrecursionlimit(10**7) MOD = 10**9+7 INF = float('inf') #無限大 def gcd(a,b):return fractions.gcd(a,b) #最大公約数 def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数 def iin(): return int(sys.stdin.readline()) #整数読み込み def ifn(): return float(sys.stdin.readline()) #浮動小数点読み込み def isn(): return sys.stdin.readline().split() #文字列読み込み def imn(): return map(int, sys.stdin.readline().split()) #整数map取得 def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) #整数-1map取得 def fmn(): return map(float, sys.stdin.readline().split()) #浮動小数点map取得 def iln(): return list(map(int, sys.stdin.readline().split())) #整数リスト取得 def iln_s(): return sorted(iln()) # 昇順の整数リスト取得 def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得 def fln(): return list(map(float, sys.stdin.readline().split())) # 浮動小数点リスト取得 def join(l, s=''): return s.join(l) #リストを文字列に変換 def perm(l, n): return itertools.permutations(l, n) # 順列取得 def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数 def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得 def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数 def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離 def m_add(a,b): return (a+b) % MOD def print_list(l): print(*l, sep='\n') def sieves_of_e(n): is_prime = [True] * (n+1) is_prime[0] = False is_prime[1] = False for i in range(2, int(n**0.5)+1): if not is_prime[i]: continue for j in range(i * 2, n+1, i): is_prime[j] = False return is_prime Q,H,S,D = imn() N = iin() min1L = min(Q*4, H*2, S) min2L = min(min1L*2, D) if N % 2 == 1: print(N//2*min2L + min1L) else: print(N//2*min2L)
import math import fractions import bisect import collections import itertools import heapq import string import sys import copy from decimal import * from collections import deque sys.setrecursionlimit(10**7) MOD = 10**9+7 INF = float('inf') #無限大 def gcd(a,b):return fractions.gcd(a,b) #最大公約数 def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数 def iin(): return int(sys.stdin.readline()) #整数読み込み def ifn(): return float(sys.stdin.readline()) #浮動小数点読み込み def isn(): return sys.stdin.readline().split() #文字列読み込み def imn(): return map(int, sys.stdin.readline().split()) #整数map取得 def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) #整数-1map取得 def fmn(): return map(float, sys.stdin.readline().split()) #浮動小数点map取得 def iln(): return list(map(int, sys.stdin.readline().split())) #整数リスト取得 def iln_s(): return sorted(iln()) # 昇順の整数リスト取得 def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得 def fln(): return list(map(float, sys.stdin.readline().split())) # 浮動小数点リスト取得 def join(l, s=''): return s.join(l) #リストを文字列に変換 def perm(l, n): return itertools.permutations(l, n) # 順列取得 def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数 def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得 def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数 def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離 def m_add(a,b): return (a+b) % MOD def print_list(l): print(*l, sep='\n') def sieves_of_e(n): is_prime = [True] * (n+1) is_prime[0] = False is_prime[1] = False for i in range(2, int(n**0.5)+1): if not is_prime[i]: continue for j in range(i * 2, n+1, i): is_prime[j] = False return is_prime Q,H,S,D = imn() N = iin() min1L = min(Q*4, H*2, S) min2L = min(min1L*2, D) ans = N//2*min2L if N % 2 == 1: ans += min1L print(ans)
p03617
Q,H,S,D = list(map(int,input().split())) N = int(eval(input())) a2 = min(Q*8,H*4,S*2,D) ans = (N//2)*a2 a1 = min(Q*4,H*2,S) ans += (N%2)*a1 print(ans)
Q,H,S,D = list(map(int,input().split())) N = int(eval(input())) x2 = min(D,2*S,4*H,8*Q) cnt = (N//2)*x2 N = N%2 x1 = min(S,2*H,4*Q) cnt += N*x1 print(cnt)
p03617
# AtCoder Grand Contest 019 # A - Ice Tea Store def f(Q,H,S,D,N,M): QQ=Q QH=H/2 QS=S/4 QD=D/8 if N==0: return M if N==1: if QQ==min(QQ,QH,QS): return M+(4*Q) if QH==min(QQ,QH,QS): return M+(2*H) if QS==min(QQ,QH,QS): return M+(S) else: if QQ==min(QQ,QH,QS,QD): return M+4*Q*N if QH==min(QQ,QH,QS,QD): return M+2*H*N if QS==min(QQ,QH,QS,QD): return M+S*N if QD==min(QQ,QH,QS,QD): if N%2==0: return M+(N//2)*D else: return f(Q,H,S,D,1,M+(N//2)*D) Q,H,S,D=list(map(int,input().split())) N=int(eval(input())) print((f(Q,H,S,D,N,0)))
Q,H,S,D=list(map(int,input().split())) N=int(eval(input())) one=min (4*Q,2*H,S) if N==1: print(one) else : if 2*one<=D: print((N*one)) else: if N%2==0: print(((N//2)*D)) else: print(((N//2)*D+one))
p03617
import sys def solve(): readline = sys.stdin.buffer.readline mod = 10 ** 9 + 7 q, h, s, d = list(map(int, readline().split())) n = int(readline()) h = min(h, q * 2) s = min(s, h * 2, q * 4) d = min(d, s * 2, h * 4, q * 8) cnt = 0 cnt += n // 2 * d n -= n // 2 * 2 cnt += n // 1 * s n -= n // 1 print(cnt) if __name__ == '__main__': solve()
import sys def solve(): readline = sys.stdin.buffer.readline mod = 10 ** 9 + 7 ls = list(map(int, readline().split())) n = int(readline()) ls[1] = min(ls[1], ls[0] * 2) ls[2] = min(ls[2], ls[1] * 2) ls[3] = min(ls[3], ls[2] * 2) t = 0 for i in range(3, -1, -1): if i == 3: t += n // 2 * ls[i] n -= n // 2 * 2 elif i == 2: t += n // 1 * ls[i] n -= n // 1 * 1 elif i == 1: t += n / 0.5 * ls[i] n -= n // 0.5 * 0.5 else: t += n // 0.25 * ls[i] n -= n // 0.25 * 0.25 if n == 0: break print((int(t))) if __name__ == '__main__': solve()
p03617
Q,H,S,D = list(map(int, input().split())) N = int(eval(input())) h = min(Q*2, H) s = min(h*2, S) d = min(s*2, D) ans = (N // 2)*d + (0 if N%2 == 0 else s) print(ans)
Q,H,S,D = list(map(int,input().split())) N = int(eval(input())) half = min(Q*2,H) one = min(half*2,S) doub = min(one*2,D) ans = (N//2)*doub + (N%2)*one print(ans)
p03617
Q, H, S, D = list(map(int, input().split())) N = int(eval(input())) PS = [[Q * 8, 0.25], [H * 4, 0.5], [S * 2, 1], [D, 2]] PS.sort(key=lambda x: x[0]) R = N ans = 0 for i in range (4): if R == 0: break elif R >= PS[i][1]: ans += int(PS[i][0] * PS[i][1] // 2 * (R // PS[i][1])) R = R % PS[i][1] print(ans)
Q, H, S, D = list(map(int, input().split())) N = int(eval(input())) ans = (N//2) * min(8 * Q, 4 * H, S * 2, D) + (N % 2) * min(4 * Q, 2 * H, S) print(ans)
p03617
A="".join(["11000000", "11"]) B="".join(["10000000", "10000000", "10000000", "1"]) C="".join(["1111"]) D="".join(["01000000", "11000000", "1"]) E="".join(["11000000", "011"]) F="".join(["10000000", "11000000", "01"]) G="".join(["01100000", "11"]) teto=[A,B,C,D,E,F,G] while True: try: a="".join([input() for i in range(8)]) for i in range(len(teto)): if a.find(teto[i])>=0: print(chr(i+ord("A"))) input() except: break #temp
A="".join(["11000000", "11"]) B="".join(["10000000", "10000000", "10000000", "1"]) C="".join(["1111"]) D="".join(["01000000", "11000000", "1"]) E="".join(["11000000", "011"]) F="".join(["10000000", "11000000", "01"]) G="".join(["01100000", "11"]) teto=[A,B,C,D,E,F,G] while True: try: a="".join([input() for i in range(8)]) for i in range(len(teto)): if a.find(teto[i])>=0: print(chr(i+ord("A"))) input() except: break
p00036
import re while True: try: s = ''.join([input() for s in range(8)]) print(' C GAE D F B'[len(re.findall('1.*1', s)[0])]) input() except EOFError: break
import sys;import re for s in sys.stdin.read().split('\n\n'):print(' C GAE D F B'[len(re.findall('1.*1', s.replace('\n',''))[0])])
p00036
from functools import reduce def gcd(v1, v2): v3 = v1 % v2 if v3 == 0: return v2 return gcd(v2, v3) def main(): N = int(eval(input())) A = [int(v) for v in input().split()] max_gcd = max(reduce(gcd, A[1:]), reduce(gcd, A[:N-1])) for i in range(1, N - 1): v1 = reduce(gcd, A[:i]) v2 = reduce(gcd, A[i + 1:]) #print(A[:i], A[i+1:]) max_gcd = max(max_gcd, gcd(v1, v2)) #print(reduce(gcd, A)) print(max_gcd) if __name__ == "__main__": main()
from functools import reduce def gcd(v1, v2): v3 = v1 % v2 if v3 == 0: return v2 return gcd(v2, v3) def main(): N = int(eval(input())) A = [int(v) for v in input().split()] max_gcd = max(reduce(gcd, A[1:]), reduce(gcd, A[:N-1])) left = [1] * N left[0] = A[0] for i in range(1, N): left[i] = gcd(A[i], left[i - 1]) right = [1] * N right[N-1] = A[N-1] for i in range(N - 2, -1, - 1): right[i] = gcd(A[i], right[i + 1]) # print(left) # print(right) max_gcd = max(right[1], left[N-2]) for i in range(1, N - 1): max_gcd = max(max_gcd, gcd(left[i - 1], right[i + 1])) print(max_gcd) if __name__ == "__main__": main()
p03061
import sys from functools import reduce import sys input = sys.stdin.readline n = int(eval(input())) A = list(map(int, input().split())) def gcd(a, b): while (b): a, b = b, a % b return a def gcd_list(numbers): return reduce(gcd, numbers) ans = 0 for i in range(n): ans = max(ans, gcd_list(A[0:i] + A[i + 1:])) print(ans)
# -*- coding: utf-8 -*- import sys from functools import reduce import sys input = sys.stdin.readline n = int(eval(input())) A = list(map(int, input().split())) def gcd(a, b): while (b): a, b = b, a % b return a L = [0] + [None] * (n - 1) R = [0] + [None] * (n - 1) for i in range(1, n): L[i] = gcd(L[i - 1], A[i - 1]) R[i] = gcd(R[i - 1], A[-i]) M = [None] * n R = R[::-1] for i in range(n): M[i] = gcd(L[i], R[i]) print((max(M)))
p03061
n = int(eval(input())) a = list(map(int, input().split())) end = sorted(a)[1] for i in range(end+1, 0, -1): x = 0 for j in a: if j % i != 0: x += 1 if x <= 1: print(i) break
n = int(eval(input())) a = list(map(int, input().split())) end = sorted(a)[1] for i in range(end+1, 0, -1): x = 0 for j in a: if j % i != 0: x += 1 if x >= 2: break if x <= 1: print(i) break
p03061
def gcd(m, n): r = m % n return n if r == 0 else gcd(n, r) n = int(eval(input())) A = list(map(int, input().split())) if n == 2: print((max(A))) exit() GCD = [] for i in range(n): A2 = A[:i] + A[i + 1:] g = gcd(A2[0], A2[1]) for j in range(2, n - 1): g = gcd(g, A2[j]) if g == 1: break GCD.append(g) print((max(GCD)))
def gcd(m, n): r = m % n return n if r == 0 else gcd(n, r) n = int(eval(input())) A = list(map(int, input().split())) GCD_left = A[:] for i in range(1, n): GCD_left[i] = gcd(GCD_left[i - 1], GCD_left[i]) GCD_right = A[:] for i in range(n - 2, -1, -1): GCD_right[i] = gcd(GCD_right[i], GCD_right[i + 1]) GCD = [1] * n GCD[0], GCD[-1] = GCD_right[1], GCD_left[-2] for i in range(1, n - 1): GCD[i] = gcd(GCD_left[i - 1], GCD_right[i + 1]) print((max(GCD)))
p03061
def gcd(x, y): while y!=0: x, y = y, x % y # x,yの大小が逆でも、入れ替わるので、OK return x n = int(eval(input())) a = list(map(int, input().split())) if n == 2: print((max(a))) elif n>=3: l = [-1] * n r = [-1] * n l[0] = a[0] #一番左を左に挿入 r[-1] = a[-1] #一番右を右に挿入 # print(l) # print(r) for i in range(1, n): #左から最大公約数をn-1回計算していく l[i] = gcd(l[i-1], a[i]) # l[0]とa[1]の最大公約数をl[1]に挿入⇒ # l[1]とa[2]の最大公約数をl[2]に挿入。隣同士素があったら、それ以降右側全て1 for i in range(n-2, -1, -1): #右から最大公約数を計算していく r[i] = gcd(r[i+1], a[i]) # r[n-1]とa[n-2]の最大公約数をr[n-2]に挿入 # print("l",l) # lはn-1桁のlist # print("r",r) ans = max(r[1], l[-2]) # rの左から2番目とlの右から2番目の大きい方を、ansに挿入. # 右端、左端は、最大公約数ではないため。(元々の数列) for i in range(1, n-1): #両端を切り取った中で、lは右側に寄せて、rは左側に寄せているので、 g = gcd(l[i-1], r[i+1]) # ans = max(ans, g) # print(i, g, ans) print(ans)
def gcd(x, y): while y!=0: x, y = y, x % y # x,yの大小が逆でも、入れ替わるので、OK return x n = int(eval(input())) a = list(map(int, input().split())) if n == 2: print((max(a))) elif n>=3: l = [-1] * n r = [-1] * n l[0] = a[0] #一番左を左に挿入 r[-1] = a[-1] #一番右を右に挿入 # print(l) # print(r) for i in range(1, n): #左から最大公約数をn-1回計算していく l[i] = gcd(l[i-1], a[i]) # l[0]とa[1]の最大公約数をl[1]に挿入⇒ # l[1]とa[2]の最大公約数をl[2]に挿入。隣同士素があったら、それ以降右側全て1 for i in range(n-2, -1, -1): #右から最大公約数を計算していく r[i] = gcd(r[i+1], a[i]) # r[n-1]とa[n-2]の最大公約数をr[n-2]に挿入 # print("l",l) # lはn-1桁のlist # print("r",r) ans = max(r[1], l[-2]) # rの左から2番目とlの右から2番目の大きい方を、ansに挿入. # 右端、左端は、最大公約数ではないため。(元々の数列) for i in range(1, n-1): #両端を切り取った中で、lは右側に寄せて、rは左側に寄せているので、 g = gcd(l[i-1], r[i+1]) # ans = max(ans, g) # print(i, g, ans) print(ans)
p03061
import math from functools import reduce def gcd(list): return reduce(math.gcd, list) 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)
from math import * n = int(eval(input())) a = list(map(int, input().split())) l = [a[0]] for i in range(1, n-1): l.append(gcd(a[i], l[i-1])) ans = l[n-2] r = a[n-1] for i in range(n-2,0,-1): r = gcd(r, a[i+1]) ans = max(ans, gcd(l[i-1], r)) ans = max(ans, gcd(r,a[1])) print(ans)
p03061
from functools import reduce def gcd(a,b): x = max([a,b]) y = min([a,b]) while y: x, y = y, x % y return x def gcd_list(nums): return reduce(gcd, nums) n = int(eval(input())) a = list(map(int,input().split())) ans = 0 for i in range(n): tmp_list = a[:i] + (a[i+1:]) #print(tmp_list) tmp = gcd_list(tmp_list) if ans < tmp: ans = tmp print(ans)
N = int(eval(input())) A = list(map(int, input().split())) def gcd(a,b): x = max([a,b]) y = min([a,b]) while y: x, y = y, x % y return x L = [0] R = [0] for i in range(N): L.append(gcd(L[-1], A[i])) for i in reversed(list(range(N))): R.append(gcd(R[-1], A[i])) R = list(reversed(R)) ans = 1 for i in range(N-1): ans = max(ans, gcd(L[i], R[i+1])) ans = max(ans, R[1]) ans = max(ans, L[-2]) print(ans) #print(R) #print(L)
p03061
N = int(eval(input())) A = sorted(list(map(int, input().split()))) if N == 2: print((max(A))) exit() result = [] num = 0 for a in A[1:]: if a % A[0] != 0: num += 1 if num == 2: break else: result.append(A[0]) for n in range(A[0], 0, -1): if A[0] % n != 0: continue num = 0 for a in A[1:]: if a % n != 0: num += 1 if num == 2: break else: result.append(n) num = 0 for a in A[0:1] + A[2:]: if a % A[1] != 0: num += 1 if num == 2: break else: result.append(A[1]) for n in range(A[1], 0, -1): if A[1] % n != 0: continue num = 0 for a in A[0:1] + A[2:]: if a % n != 0: num += 1 if num == 2: break else: result.append(n) print((max(result)))
import math N = int(eval(input())) A = sorted(list(map(int, input().split()))) if N == 2: print((max(A))) exit() result = [] num = 0 for a in A[1:]: if a % A[0] != 0: num += 1 if num == 2: break else: result.append(A[0]) for n in range(2, int(math.sqrt(A[0])) + 1): if A[0] % n != 0: continue num = 0 x = A[0] // n for a in A[1:]: if a % x != 0: num += 1 if num == 2: break else: result.append(x) num = 0 for a in A[1:]: if a % n != 0: num += 1 if num == 2: break else: result.append(n) num = 0 B = A[0:1] + A[2:] for a in B: if a % A[1] != 0: num += 1 if num == 2: break else: result.append(A[1]) for n in range(2, int(math.sqrt(A[1])) + 1): if A[1] % n != 0: continue num = 0 x = A[1] // n for a in B: if a % x != 0: num += 1 if num == 2: break else: result.append(x) num = 0 for a in B: if a % n != 0: num += 1 if num == 2: break else: result.append(n) if len(result) != 0: print((max(result))) else: print((1))
p03061
# coding: utf-8 import sys # from operator import itemgetter sysread = sys.stdin.buffer.readline read = sys.stdin.buffer.read printout = sys.stdout.write sprint = sys.stdout.flush # from heapq import heappop, heappush from collections import defaultdict sys.setrecursionlimit(10 ** 7) import math # from itertools import product, accumulate, combinations, product #import bisect # import numpy as np # from copy import deepcopy #from collections import deque # from decimal import Decimal # from numba import jit INF = 1 << 50 EPS = 1e-8 mod = 998244353 def intread(): return int(sysread()) def mapline(t=int): return list(map(t, sysread().split())) def mapread(t=int): return list(map(t, read().split())) class segtree: '''init_val : i-indexed (init_val[0] = self.bin[1])''' def __init__(self, n, init = 0, init_val=None): self.n = n self.init = init self.k = math.ceil(math.log2(n)) self.add_val = 1 << self.k self.bins = [self.init] * (1 << (self.k + 1)) if init_val != None: self.update_set(init_val) self.caliculate() def __getitem__(self, idx): # return idx-value return self.bins[idx + self.add_val] def update_set(self, vals): for idx, i in enumerate(range(self.add_val, self.add_val * 2)): if len(vals) > idx: self.bins[i] = vals[idx] else:continue def compare(self, l, r): return math.gcd(l, r) def caliculate(self): k = self.k while k: for i in range(1<<k, 1<<(k+1)): if not i%2: self.bins[i//2] = self.compare(self.bins[i], self.bins[i+1]) else:continue k -= 1 def update(self, idx, val, by=True): '''idx : 0-started index''' k = (1<<self.k) + idx if by: self.bins[k] += val else: self.bins[k] = val while k>1: self.bins[k // 2] = self.compare(self.bins[k // 2 * 2], self.bins[k // 2 * 2 + 1]) k = k//2 def eval(self, l, r): if l == r: return self.bins[self.add_val + l] ret = self.init l = (1 << self.k) + l r = (1 << self.k) + r #print(l, r) while True: #print(l, r) if r - l == 1: ret = self.compare(ret, self.bins[l]) ret = self.compare(ret, self.bins[r]) break elif l == r: ret = self.compare(ret, self.bins[l]) break else: done = False if l % 2: ret = self.compare(ret, self.bins[l]) l += 1 done = True if not r % 2: ret = self.compare(ret, self.bins[r]) r -= 1 done = True if not done: l = l // 2 r = r // 2 #print(ret) return ret def run(): N, *A = mapread() t = segtree(N, init = 0, init_val = A) ans = 0 for i in range(N): if i > 0: a = t.eval(0, i-1) else: a = 0 if i < N-1: b = t.eval(i+1, N-1) else: b = 0 ans = max(ans, math.gcd(a, b)) print(ans) if __name__ == "__main__": #print(math.gcd(0, 10)) run()
# coding: utf-8 import sys # from operator import itemgetter sysread = sys.stdin.buffer.readline read = sys.stdin.buffer.read printout = sys.stdout.write sprint = sys.stdout.flush # from heapq import heappop, heappush from collections import defaultdict sys.setrecursionlimit(10 ** 7) import math # from itertools import product, accumulate, combinations, product #import bisect # import numpy as np # from copy import deepcopy #from collections import deque # from decimal import Decimal # from numba import jit INF = 1 << 50 EPS = 1e-8 mod = 998244353 def intread(): return int(sysread()) def mapline(t=int): return list(map(t, sysread().split())) def mapread(t=int): return list(map(t, read().split())) def run(): N, *A = mapread() LEFT = [0] * (N+2) RIGHT = [0] * (N+2) for i, a in enumerate(A, 1): LEFT[i] = math.gcd(LEFT[i-1], a) for i in range(N-1, -1, -1): k = i+1# for RIGHT RIGHT[k] = math.gcd(RIGHT[k+1], A[i]) #print(LEFT) #print(RIGHT) ans = 0 for i in range(N): k = i + 1# for LEFT and RIGHT v = math.gcd(LEFT[k-1], RIGHT[k+1]) #print(v) ans = max(ans, v) print(ans) if __name__ == "__main__": #print(math.gcd(0, 10)) run()
p03061
# -*- coding: utf-8 -*- """ Created on Sat Apr 27 20:57:32 2019 @author: Yamazaki Kenichi """ N = int(eval(input())) A = list(map(int,input().split())) def eg(x,y): if y == 0: return x x, y = y, x%y return eg(x,y) T = [[1,1,1] for i in range(N)] T[0][0],T[0][1],T[0][2] = A[0], 0, 0 for i in range(1,N): T[i][0] = eg(T[i-1][0],A[i]) T[i][1] = T[i-1][0] T[i][2] = max(eg(T[i-1][2],A[i]),eg(T[i-1][1],A[i])) print((max(T[N-1])))
# -*- coding: utf-8 -*- """ Created on Sun Apr 28 15:46:33 2019 @author: Yamazaki Kenichi """ N = int(eval(input())) A = list(map(int,input().split())) def eg(x,y): if y == 0: return x x, y = y, x%y return eg(x,y) L, R = [0 for i in range(N)],[0 for i in range(N)] L[0], R[N-1] = A[0], A[N-1] for i in range(1,N): L[i] = eg(L[i-1], A[i]) R[N-1-i] = eg(R[N-i], A[N-1-i]) ans = [0 for i in range(N)] for i in range(N): if i == 0: ans[i] = R[i+1] elif i == N-1: ans[i] = L[i-1] else: ans[i] = eg(L[i-1],R[i+1]) print((max(ans)))
p03061
n = int(eval(input())) a = [] for x in input().split(): a.append(int(x)) ans = [] def gcd(a, b): while b: a, b = b, a % b return a def gcd_list(numbers): element = gcd(numbers[0], numbers[1]) if len(numbers)==2: return element else: for i in range(2, len(numbers)): element=gcd(element, numbers[i]) return element if n == 2: print((max(a))) else: for i in range(n): c = a.copy() c.remove(a[i]) ans.append(gcd_list(c)) print((max(ans)))
def gcd(x,y): if x<y: x,y=y,x #x>y if y==0: return x if x%y==0: return y else: return gcd(y,x%y) N=int(eval(input())) A=[int(i) for i in input().split()] X=[0 for i in range(N)] Y=[0 for i in range(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]) Z=[gcd(X[i],Y[i]) for i in range(N)] print((max(Z)))
p03061
def gcd(a,b): if b == 0: return 0 elif a%b == 0: return b else: return gcd(b,a%b) N = int(eval(input())) A = list(map(int, input().split())) GCDs = [] for n in range(N): B = A + [] trash = B.pop(n) GCD = B[0] for b in B: GCD = gcd(GCD, b) GCDs.append(GCD) print((max(GCDs)))
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())) L = [0] R = [0] for n in range(N-1): L.append(gcd(L[-1],A[n])) R.append(gcd(R[-1],A[N-n-1])) GCDs = [] for n in range(N): GCDs.append(gcd(L[n], R[N-n-1])) print((max(GCDs)))
p03061
from typing import Callable, List, Optional, Union T = Union[int, str] class SegmentTree: """Segment Tree""" __slots__ = ["e", "op", "modifying_op", "_n", "_size", "tree"] def __init__( self, a: List[T], e: T, op: Callable[[T, T], T], modifying_op: Optional[Callable[[T], T]] = None, ) -> None: self.e = e self.op = op self.modifying_op = modifying_op self._n = len(a) self._size = 1 << (self._n - 1).bit_length() if modifying_op: a = list(map(modifying_op, a)) self.tree = [e] * self._size + a + [e] * (self._size - self._n) for i in range(self._size - 1, 0, -1): self._update(i) def _update(self, k: int) -> None: """Update the value of a[k].""" self.tree[k] = self.op(self.tree[2 * k], self.tree[2 * k + 1]) def set(self, k: int, x: T) -> None: """Assign x to a[k] in O(log n).""" assert 0 <= k < self._n k += self._size if self.modifying_op: x = self.modifying_op(x) self.tree[k] = x while k: k >>= 1 self._update(k) def get(self, k: int) -> T: """Return a[k] in O(1).""" assert 0 <= k < self._n return self.tree[k + self._size] def prod(self, l: int, r: int) -> T: """Return op(a[l], ..., a[r - 1]). Return e, if l == r. Complexity: O(log n) """ assert 0 <= l <= r <= self._n sml, smr = self.e, self.e l += self._size r += self._size while l < r: if l & 1: sml = self.op(sml, self.tree[l]) l += 1 if r & 1: r -= 1 smr = self.op(self.tree[r], smr) l >>= 1 r >>= 1 return self.op(sml, smr) def prod_all(self) -> T: """Return op(a[0], ..., a[n - 1]. Return e if n == 0. Complexity: O(1) """ return self.tree[1] def max_right(self, l: int, f: Callable[[T], bool]) -> int: """ Return an index r satisfying both: 1. r = l or f(op(a[l], a[l + 1], ..., a[r - 1])) = true 2. r = n or f(op(a[l], a[l + 1], ..., a[r])) = false. If f is monotone, this is the maximum r satisfying: f(op(a[l], a[l + 1], ..., a[r - 1])) = true. Complexity: O(log n) """ assert 0 <= l <= self._n assert f(self.e) if l == self._n: return self._n l += self._size sm = self.e while True: while not l & 1: l >>= 1 if not f(self.op(sm, self.tree[l])): while l < self._size: l *= 2 if f(self.op(sm, self.tree[l])): sm = self.op(sm, self.tree[l]) l += 1 return l - self._size sm = self.op(sm, self.tree[l]) l += 1 if (l & -l) == l: break return self._n def min_left(self, r: int, f: Callable[[T], bool]) -> int: """ Return an index l satisfying both: 1. l = r or f(op(a[l], a[l + 1], ..., a[r - 1])) = true 2. l = 0 or f(op(a[l - 1], a[l + 1], ..., a[r - 1])) = false. If f is monotone, this is the minimum l satisfying: f(op(a[l], a[l + 1], ..., a[r - 1])) = true. Complexity: O(log n) """ assert 0 <= r <= self._n assert f(self.e) if not r: return 0 r += self._size sm = self.e while True: r -= 1 while r > 1 and r % 2: r >>= 1 if not f(self.op(self.tree[r], sm)): while r < self._size: r = 2 * r + 1 if f(self.op(self.tree[r], sm)): sm = self.op(self.tree[r], sm) r -= 1 return r + 1 - self._size if (r & -r) == r: break return 0 def practice2_j(): # https://atcoder.jp/contests/practice2/tasks/practice2_j N, _, *AQ = list(map(int, open(0).read().split())) A, Q = AQ[:N], AQ[N:] tree = SegmentTree(A, -1, max) res = [] for t, x, y in zip(*[iter(Q)] * 3): if t == 1: tree.set(x - 1, y) elif t == 2: res.append(tree.prod(x - 1, y)) else: res.append(tree.max_right(x - 1, lambda n: n < y) + 1) print(("\n".join(map(str, res)))) def staticrmq(): # https://judge.yosupo.jp/problem/staticrmq N, Q, *ALR = list(map(int, open(0).read().split())) A, LR = ALR[:N], ALR[N:] tree = SegmentTree(A, (10 ** 10) + 1, min) res = [tree.prod(l, r) for l, r in zip(*[iter(LR)] * 2)] print(("\n".join(map(str, res)))) def dsl_2_a(): # https://onlinejudge.u-aizu.ac.jp/problems/DSL_2_A N, Q, *X = list(map(int, open(0).read().split())) tree = SegmentTree([2 ** 31 - 1] * N, 2 ** 31 - 1, min) res = [] for com, x, y in zip(*[iter(X)] * 3): if com: res.append(tree.prod(x, y + 1)) else: tree.set(x, y) print(("\n".join(map(str, res)))) def abc125_c(): # https://atcoder.jp/contests/abc125/tasks/abc125_c from math import gcd N, *A = list(map(int, open(0).read().split())) tree = SegmentTree(A, 0, gcd) res = max(gcd(tree.prod(0, i), tree.prod(i + 1, N)) for i in range(N)) print(res) if __name__ == "__main__": # practice2_j() # staticrmq() # dsl_2_a() abc125_c()
from typing import Callable, Iterator, Optional, TypeVar S = TypeVar("S") class SegmentTree: """Segment Tree References: https://github.com/atcoder/ac-library/blob/master/atcoder/segtree.hpp """ __slots__ = ["_e", "_op", "_n", "_size", "_tree"] def __init__( self, initial_values: Optional[Iterator[S]], n: int, e: S, op: Callable[[S, S], S], ) -> None: self._e = e self._op = op self._n = n self._size = 1 << (self._n - 1).bit_length() self._tree = [e] * 2 * self._size if initial_values is None: return for i, initial_values in enumerate(initial_values, self._size): self._tree[i] = initial_values for i in range(self._size - 1, 0, -1): self._update(i) def _update(self, k: int) -> None: """Update the value of a[k].""" self._tree[k] = self._op(self._tree[2 * k], self._tree[2 * k + 1]) def set(self, k: int, x: S) -> None: """Assign x to a[k] in O(log n).""" assert 0 <= k < self._n k += self._size self._tree[k] = x while k: k >>= 1 self._update(k) def get(self, k: int) -> S: """Return a[k] in O(1).""" assert 0 <= k < self._n return self._tree[k + self._size] def prod(self, l: int, r: int) -> S: """Return op(a[l], ..., a[r - 1]). Return e, if l == r. Complexity: O(log n) """ assert 0 <= l <= r <= self._n sml, smr = self._e, self._e l += self._size r += self._size while l < r: if l & 1: sml = self._op(sml, self._tree[l]) l += 1 if r & 1: r -= 1 smr = self._op(self._tree[r], smr) l >>= 1 r >>= 1 return self._op(sml, smr) def prod_all(self) -> S: """Return op(a[0], ..., a[n - 1]. Return e if n == 0. Complexity: O(1) """ return self._tree[1] def max_right(self, l: int, f: Callable[[S], bool]) -> int: """ Return an index r satisfying both: 1. r = l or f(op(a[l], a[l + 1], ..., a[r - 1])) = true 2. r = n or f(op(a[l], a[l + 1], ..., a[r])) = false. If f is monotone, this is the maximum r satisfying: f(op(a[l], a[l + 1], ..., a[r - 1])) = true. Complexity: O(log n) """ assert 0 <= l <= self._n assert f(self._e) if l == self._n: return self._n l += self._size sm = self._e while True: while not l & 1: l >>= 1 if not f(self._op(sm, self._tree[l])): while l < self._size: l *= 2 if f(self._op(sm, self._tree[l])): sm = self._op(sm, self._tree[l]) l += 1 return l - self._size sm = self._op(sm, self._tree[l]) l += 1 if (l & -l) == l: break return self._n def min_left(self, r: int, f: Callable[[S], bool]) -> int: """ Return an index l satisfying both: 1. l = r or f(op(a[l], a[l + 1], ..., a[r - 1])) = true 2. l = 0 or f(op(a[l - 1], a[l + 1], ..., a[r - 1])) = false. If f is monotone, this is the minimum l satisfying: f(op(a[l], a[l + 1], ..., a[r - 1])) = true. Complexity: O(log n) """ assert 0 <= r <= self._n assert f(self._e) if not r: return 0 r += self._size sm = self._e while True: r -= 1 while r > 1 and r & 1: r >>= 1 if not f(self._op(self._tree[r], sm)): while r < self._size: r = 2 * r + 1 if f(self._op(self._tree[r], sm)): sm = self._op(self._tree[r], sm) r -= 1 return r + 1 - self._size if (r & -r) == r: break return 0 def staticrmq(): # https://judge.yosupo.jp/problem/staticrmq import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline N, _ = list(map(int, readline().split())) A = list(map(int, readline().split())) LR = list(map(int, read().split())) tree = SegmentTree(A, N, (10 ** 10) + 1, min) res = [tree.prod(l, r) for l, r in zip(*[iter(LR)] * 2)] print(("\n".join(map(str, res)))) def practice2_j(): # https://atcoder.jp/contests/practice2/tasks/practice2_j import sys readline = sys.stdin.readline N, Q = list(map(int, readline().split())) A = list(map(int, readline().split())) tree = SegmentTree(A, N, -1, max) res = [] for _ in range(Q): t, x, y = list(map(int, readline().split())) if t == 1: tree.set(x - 1, y) elif t == 2: res.append(tree.prod(x - 1, y)) else: res.append(tree.max_right(x - 1, lambda n: n < y) + 1) print(("\n".join(map(str, res)))) def dsl_2_a(): # https://onlinejudge.u-aizu.ac.jp/problems/DSL_2_A N, Q, *X = list(map(int, open(0).read().split())) tree = SegmentTree(None, N, 2 ** 31 - 1, min) res = [] for com, x, y in zip(*[iter(X)] * 3): if com: res.append(tree.prod(x, y + 1)) else: tree.set(x, y) print(("\n".join(map(str, res)))) def abc125_c(): # https://atcoder.jp/contests/abc125/tasks/abc125_c from math import gcd N, *A = list(map(int, open(0).read().split())) tree = SegmentTree(A, N, 0, gcd) res = max(gcd(tree.prod(0, i), tree.prod(i + 1, N)) for i in range(N)) print(res) if __name__ == "__main__": # staticrmq() # practice2_j() # dsl_2_a() abc125_c()
p03061
from functools import reduce def gcd(a, b): l = max(a, b) s = min(a, b) if s == 0: return l else: return gcd(s, l%s) n = int(eval(input())) As = list(map(int, input().split(" "))) largest = 0 for i in range(n): largest = max(largest, gcd(reduce(gcd, As[:i], 0), reduce(gcd, As[i+1:], 0))) print(largest)
def gcd(a, b): l = max(a, b) s = min(a, b) if s == 0: return l else: return gcd(s, l%s) n = int(eval(input())) A = [0] + list(map(int, input().split())) + [0] largest = 0 L = [0 for _ in range(n+2)] R = [0 for _ in range(n+2)] for i in range(1, n+2): L[i] = gcd(A[i], L[i-1]) R[n-i+1] = gcd(R[n-i+2], A[n-i+1]) print((max(gcd(L[i-1],R[i+1]) for i in range(1, n+1))))
p03061
import sys sys.setrecursionlimit(10 ** 6) def gcd(a, b): if a == 0: return b return gcd(b % a, a) def xgcd(a): size = len(a) if size == 1: return a[0] if size == 2: return gcd(a[0], a[1]) la = a[:size // 2] ra = a[size // 2:] return gcd(xgcd(la), xgcd(ra)) n = int(eval(input())) a = list(map(int, input().split())) ans = 0 for i in range(n): tmp = a[i] a[i] = a[i - 1] ans = max(ans, xgcd(a)) a[i] = tmp print(ans)
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())) b = [0] * (n + 1) c = [0] * (n + 1) for i in range(n): b[i + 1] = gcd(b[i], a[i]) c[i + 1] = gcd(c[i], a[-i - 1]) ans = 0 for i in range(n): ans = max(ans, gcd(b[i], c[n - i - 1])) print(ans)
p03061
from functools import reduce def gcdd(x, y): x, y = max(x, y), min(x, y) while y > 0: r = x % y x = y y = r return x def gcd(numbers): return reduce(gcdd, numbers) n = int(eval(input())) a = [int(i) for i in input().split()] b = [] for i in range(n): d = a[:i] + a[i+1:] b.append(gcd(d)) print((max(b)))
def gcd(x, y): if x * y == 0: return (x+y) x, y = max(x, y), min(x, y) while y > 0: r = x % y x, y = y, r return x n = int(eval(input())) a = [int(i) for i in input().split()] l_list = [0] r_list = [0] for i in range(n): l = gcd(l_list[i], a[i]) l_list.append(l) r = gcd(r_list[-1], a[-i-1]) r_list.append(r) r_list = list(reversed(r_list)) r_list = r_list[1:] l_list = l_list[:n] c_list = [] for i in range(n): c_list.append(gcd(l_list[i], r_list[i])) print((max(c_list)))
p03061
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 __name__ == '__main__': n = int(eval(input())) a = list(map(int, input().split())) numdct = {} for i in range(2): for div in make_divisors(a[i]): if(not div in numdct): numdct[div] = 0 numdct[div] += 1 for i in range(2, n): for num in list(numdct.keys()): if(numdct[num] > 0): if(a[i] % num != 0): numdct[num] -= 1 numlist = sorted(list(numdct.items()), key=lambda x: -x[1]) maxnum = 0 for item in numlist: if(item[1] > 0): if(maxnum <= item[0]): maxnum = item[0] else: break print(maxnum)
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 __name__ == '__main__': n = int(eval(input())) a = list(map(int, input().split())) numdct = {} for i in range(2): for div in make_divisors(a[i]): if(not div in numdct): numdct[div] = 0 numdct[div] += 1 dellist = [] for i in range(2, n): for num in list(numdct.keys()): if(a[i] % num != 0): numdct[num] -= 1 if(numdct[num] == 0): dellist.append(num) for d in dellist: del numdct[d] dellist.clear() numlist = sorted(list(numdct.items()), key=lambda x: -x[1]) maxnum = 0 for item in numlist: if(item[1] > 0): if(maxnum <= item[0]): maxnum = item[0] else: break print(maxnum)
p03061
n = int(eval(input())) a = list(map(int,input().split())) ans1 = a[1] for i in range(1,n): big,sml = a[i],ans1 while sml != 0: big,sml = sml,big%sml ans1 = big ans2 = a[-2] for i in range(n-1): big,sml = a[i],ans2 while sml != 0: big,sml = sml,big%sml ans2 = big l1 = [-1] now = a[0] for i in range(1,n): big,sml = now,a[i] while sml != 0: big,sml = sml,big%sml l1.append(now // big) now = big l2 = [-1] now = a[-1] for i in range(n-2,-1,-1): big,sml = now,a[i] while sml != 0: big,sml = sml,big%sml l2.append(now // big) now = big l2.reverse() l3 = [-1] for i in range(1,n-1): big,sml = l1[i],l2[i] while sml != 0: big,sml = sml,big%sml l3.append(big) l3.append(-1) now = a[0] for i in range(n): big,sml = now,a[i] while sml != 0: big,sml = sml,big%sml now = big ans3 = now * max(l3) #print(now) #print(l1) #print(l2) #print(l3) #print(ans1,ans2,ans3) print((max(ans1,ans2,ans3)))
#14:10 n = int(eval(input())) a = list(map(int,input().split())) b = [] for i in range(n): if i == 0: b.append(a[i]) else: big = b[-1] sml = a[i] while sml != 0: big,sml = sml,big%sml b.append(big) c = [] for j in range(n)[::-1]: if j == n-1: c.append(a[j]) else: big = c[-1] sml = a[j] while sml != 0: big,sml = sml,big%sml c.append(big) c.reverse() d = [] for i in range(n): if i == 0: d.append(c[1]) elif i == n-1: d.append(b[n-2]) else: big = b[i-1] sml = c[i+1] while sml != 0: big,sml = sml,big%sml d.append(big) #print(b) #print(c) #print(d) print((max(d)))
p03061
n=int(eval(input())) a=list(map(int,input().split())) def gcd(a,b): if a==b: return a else: a,b=max(a,b),min(a,b) if a%b==0: return b else: return gcd(b,a%b) def gcd_for_list(L): if len(L)==1: return L[0] for i in range(len(L)): if i==0: tmp=gcd(L[i],L[i+1]) elif i==1: pass else: tmp=gcd(tmp,L[i]) return tmp ans=0 for i in range(0,n): if i==0: ans=max(ans,gcd_for_list(a[i+1:])) elif i==n-1: ans=max(ans,gcd_for_list(a[0:i])) else: l_r=a[0:i] l_l=a[i+1:] ans=max(ans,gcd(gcd_for_list(l_r),gcd_for_list(l_l))) print(ans)
n=int(eval(input())) a=list(map(int,input().split())) def gcd(a,b): if a==b: return a else: a,b=max(a,b),min(a,b) if a%b==0: return b else: return gcd(b,a%b) R=[] L=[] for i in range(n): if i==0: L.append(a[i]) R.append(a[-i-1]) else: vl,vr=gcd(a[i],L[-1]),gcd(a[-i-1],R[-1]) L.append(vl) R.append(vr) ans=0 for i in range(n): if i==0: ans=max(ans,R[-i-2]) elif i==n-1: ans=max(ans,L[i-1]) else: ans=max(ans,gcd(L[i-1],R[-i-2])) print(ans)
p03061
def gcd(a, b): if a < b: a, b = b, a if b == 0: return a c = a % b return gcd(b, c) def list_gcd(a): a_gcd = a[0] for i in range(n-1): a_gcd = gcd(a_gcd, a[i+1]) return a_gcd n = int(eval(input())) a = list(map(int, input().split())) a.sort() ans = 0 for i in range((n+1)//2): b = a[:] b[i] = b[i-1] ans = max(ans, list_gcd(b)) print(ans)
def gcd(a, b): if a < b: a, b = b, a if b == 0: return a c = a % b return gcd(b, c) n = int(eval(input())) a = list(map(int, input().split())) a = [0]+a[:]+[0] l = [0]*len(a) r = [0]*len(a) for i in range(len(a)-1): m = len(a)-1 l[i+1] = gcd(l[i], a[i+1]) r[m-i-1] = gcd(r[m-i], a[m-i-1]) ans = [0]*n for i in range(n): ans[i] = gcd(l[i], r[i+2]) print((max(ans)))
p03061
#math def gcd(a, b): #最大公約数 while b: a, b = b, a % b return int(a) def gcd3(lis): #最大公約数(3over) ans = lis[0] for li in lis: ans = gcd(ans,li) return ans n = int(eval(input())) a = list(map(int, input().split())) maxgcd = 0 for i in range(n): nums = [a[j] for j in range(n) if j!=i] #print(nums) tmp = gcd3(nums) if maxgcd<tmp: maxgcd=tmp print(maxgcd)
import sys def gcd(a, b): #最大公約数 while b: a, b = b, a % b return int(a) n = int(eval(input())) a = list(map(int, input().split())) if n==2: print((max(a))) sys.exit(0) elif n==3: lis = [gcd(a[0], a[1]), gcd(a[1], a[2]), gcd(a[2], a[0])] print((max(lis))) sys.exit(0) tmp_unused = gcd(a[0], a[1]) tmp_used = max(a[0], a[1]) #print("used=", tmp_used, "unused=", tmp_unused) for i in range(2,n): #print(a[i]) tmp_used = max(tmp_unused, gcd(tmp_used, a[i])) tmp_unused = gcd(tmp_unused, a[i]) #print("used=", tmp_used, "unused=", tmp_unused) print(tmp_used)
p03061
n = int(eval(input())) l = list(map(int, input().split())) def gcd(x,y): while y: x, y = y, x%y return x ans = 0 for i in range(n): g = l[0] if i > 0 else l[1] for j in range(1,n): if j != i: g = gcd(g, l[j]) ans = max(ans, g) print(ans)
n = int(eval(input())) a = list(map(int, input().split())) def gcd(x,y): while y: x, y = y, x%y return x ll, lr = [0]*n, [0]*n ll[0], lr[n-1] = a[0], a[n-1] for i in range(1,n): ll[i] = gcd(ll[i-1], a[i]) lr[n-i-1] = gcd(lr[n-i], a[n-i-1]) print((max(ll[n-2], lr[1], *[gcd(l,r) for l,r in zip(ll[:n-2], lr[2:])])))
p03061
import sys input = sys.stdin.readline n = int(eval(input())) a = list(map(int, input().split())) def gcd(x,y): while y: x, y = y, x%y return x ll, lr = [0]*n, [0]*n ll[0], lr[n-1] = a[0], a[n-1] for i in range(1,n): ll[i] = gcd(ll[i-1], a[i]) lr[n-i-1] = gcd(lr[n-i], a[n-i-1]) print((max(ll[n-2], lr[1], *[gcd(l,r) for l,r in zip(ll[:n-2], lr[2:])])))
n=int(eval(input())) a=[*list(map(int,input().split()))] from math import * l=a.copy() r=a.copy() for i in range(n-1): l[i+1]=gcd(l[i],l[i+1]) r[-i-2]=gcd(r[-i-1],r[-i-2]) a=max(l[-2],r[1]) for i in range(1,n-1): a=max(a,gcd(l[i-1],r[i+1])) print(a)
p03061
n=int(eval(input())) a=[*list(map(int,input().split()))] from math import * l=a.copy() r=a.copy() for i in range(n-1): l[i+1]=gcd(l[i],l[i+1]) r[-i-2]=gcd(r[-i-1],r[-i-2]) a=max(l[-2],r[1]) for i in range(1,n-1): a=max(a,gcd(l[i-1],r[i+1])) print(a)
n=int(eval(input())) a=[*list(map(int,input().split()))] from math import * l,r=[0]*n,[0]*n for i in range(1,n): l[i]=gcd(l[i-1],a[i-1]) r[~i]=gcd(r[-i],a[-i]) m=0 for i in range(n): m=max(m,gcd(l[i],r[i])) print(m)
p03061
n=int(eval(input())) A=list(map(int,input().split())) L=[0]*(n+1) R=[0]*(n+1) def gcd(a,b): while b!=0: a,b=b,a%b return a for i in range(n): L[i+1]=gcd(L[i],A[i]) for i in range(n,0,-1): R[i-1]=gcd(R[i],A[i-1]) ans=[] for k in range(n): ans.append(gcd(L[k],R[k+1])) print((max(ans)))
n=int(eval(input())) A=list(map(int,input().split())) def gcd(a,b): while b!=0: a,b=b,a%b return a L=[0] R=[0] for i in range(n): L.append(gcd(L[i],A[i])) R.append(gcd(R[i],A[-(i+1)])) R=R[::-1] ans=[] for i in range(n): ans.append(gcd(L[i],R[i+1])) print((max(ans)))
p03061
import functools n = int(eval(input())) a_list = list(map(int,input().split())) def gcd(m,n): x = max(m,n) y = min(m,n) if x%y==0: return y else: while x%y!=0: x,y = y,x%y return y res = [] tmp = [] for i in range(n): if a_list[i] in tmp: continue if a_list[:i]==[]: res.append(functools.reduce(gcd, a_list[i+1:])) elif a_list[i+1:]==[]: res.append(functools.reduce(gcd, a_list[:i])) else: l_gcd = functools.reduce(gcd, a_list[:i]) r_gcd = functools.reduce(gcd, a_list[i+1:]) res.append(gcd(l_gcd,r_gcd)) tmp.append(a_list[i]) print((max(res)))
n = int(eval(input())) a_list = list(map(int,input().split())) def gcd(m,n): x = max(m,n) y = min(m,n) if y==0: return x else: while x%y!=0: x,y = y,x%y return y res = [] l_gcd = [0]*(n+2) r_gcd = [0]*(n+2) for i in range(n+1): if i ==0: l_gcd[i] = 0 r_gcd[n-i+1] = 0 continue l_gcd[i] = gcd(l_gcd[i-1], a_list[i-1]) r_gcd[n-i] = gcd(r_gcd[n-i+1], a_list[n-i]) for i in range(n+1): res.append(gcd(l_gcd[i],r_gcd[i+1])) print((max(res)))
p03061
n = int(eval(input())) alist = [int(i) for i in input().split()] alist.sort() def calc_gcm(x, y): while y != 0: z = x % y x = y y = z return x def get_custum(xlist): prev = None gcm_list = [] for a in xlist: if prev is None: gcm_list.append(a) prev = a continue ret = calc_gcm(a, prev) gcm_list.append(ret) prev = ret for boundary, gcm in enumerate(gcm_list): if gcm == gcm_list[-1]: break gcm_1 = None for i, a in enumerate(xlist): if i == boundary - 1: continue if gcm_1 is None: gcm_1 = a continue gcm_1 = calc_gcm(a, gcm_1) gcm_2 = None for i, a in enumerate(xlist): if i == boundary: continue if gcm_2 is None: gcm_2 = a continue gcm_2 = calc_gcm(a, gcm_2) return max(gcm_1, gcm_2) ret1 = get_custum(alist) alist.reverse() ret2 = get_custum(alist) print((max(ret1, ret2)))
n = int(eval(input())) alist = [int(i) for i in input().split()] def calc_gcm(x, y): while y != 0: z = x % y x = y y = z return x def listup_gcm(xlist): list_gcm = [] prev = None for a in xlist: if prev is None: list_gcm.append(a) prev = a continue gcm = calc_gcm(prev, a) list_gcm.append(gcm) prev = gcm return list_gcm left_gcm = listup_gcm(alist) alist.reverse() right_gcm = listup_gcm(alist) right_gcm.reverse() maxgcm = 0 for i in range(n): if i == 0: maxgcm = max(maxgcm, right_gcm[1]) elif i == n - 1: maxgcm = max(maxgcm, left_gcm[n-2]) else: maxgcm = max(maxgcm, calc_gcm(left_gcm[i-1], right_gcm[i+1])) print(maxgcm)
p03061
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline class SegmentTree: def __init__(self, N, func, I): self.func = func self.I = I self.sz = 2**(N-1).bit_length() self.seg = [I] * (self.sz * 2) def assign(self, k, x): self.seg[k + self.sz] = x def build(self): for i in range(self.sz - 1, 0, -1): self.seg[i] = self.func(self.seg[2 * i], self.seg[2 * i + 1]) def update(self, k, x): k += self.sz self.seg[k] = x while k > 1: k >>= 1 self.seg[k] = self.func(self.seg[2 * k], self.seg[2 * k + 1]) def query(self, a, b): L = self.I R = self.I a += self.sz b += self.sz while a < b: if a & 1: L = self.func(L, self.seg[a]) a += 1 if b & 1: b -= 1 R = self.func(self.seg[b], R) a >>= 1 b >>= 1 return self.func(L, R) def main(): def gcd(a, b): while b: a, b = b, a % b return a N = int(readline()) L = list(map(int,read().split())) seg = SegmentTree(N , gcd , 0) for i,n in enumerate(L): seg.assign(i , n) seg.build() ans = 1 for i in range(N): ans = max(ans,gcd(seg.query(0,i) , seg.query(i+1,N))) print(ans) main()
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline class SegmentTree: def __init__(self, N, func, I): self.func = func self.I = I self.sz = 2**(N-1).bit_length() self.seg = [I] * (self.sz * 2) def assign(self, k, x): self.seg[k + self.sz] = x def build(self): for i in reversed(list(range(1,self.sz))): self.seg[i] = self.func(self.seg[2 * i], self.seg[2 * i + 1]) def update(self, k, x): k += self.sz self.seg[k] = x while k > 1: k >>= 1 self.seg[k] = self.func(self.seg[2 * k], self.seg[2 * k + 1]) def query(self, a, b): L = self.I R = self.I a += self.sz b += self.sz while a < b: if a & 1: L = self.func(L, self.seg[a]) a += 1 if b & 1: b -= 1 R = self.func(self.seg[b], R) a >>= 1 b >>= 1 return self.func(L, R) def main(): def gcd(a, b): while b: a, b = b, a % b return a N = int(readline()) L = list(map(int,read().split())) seg = SegmentTree(N , gcd , 0) for i,n in enumerate(L): seg.assign(i , n) seg.build() ans = 1 for i in range(N): ans = max(ans,gcd(seg.query(0,i) , seg.query(i+1,N))) print(ans) main()
p03061
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline class SegmentTree: def __init__(self, N, func, I): self.func = func self.I = I self.N = N self.sz = 2**(N-1).bit_length() self.seg = [I] * (self.sz * 2) def assign(self, k, x): self.seg[k + self.sz] = x def build(self): for i in reversed(list(range(1,self.sz))): self.seg[i] = self.func(self.seg[2 * i], self.seg[2 * i + 1]) def update(self, k, x): k += self.sz self.seg[k] = x while k > 1: k >>= 1 self.seg[k] = self.func(self.seg[2 * k], self.seg[2 * k + 1]) def query(self, a, b): L = self.I R = self.I a += self.sz b += self.sz while a < b: if a & 1: L = self.func(L, self.seg[a]) a += 1 if b & 1: b -= 1 R = self.func(self.seg[b], R) a >>= 1 b >>= 1 return self.func(L, R) def main(): def gcd(a, b): while b: a, b = b, a % b return a N = int(readline()) L = list(map(int,readline().split())) seg = SegmentTree(N , gcd , 0) for i,n in enumerate(L): seg.assign(i , n) seg.build() ans = 1 for i in range(N): ans = max(ans,gcd(seg.query(0,i) , seg.query(i+1,N))) print(ans) main()
import sys readline = sys.stdin.buffer.readline class SegmentTree: def __init__(self, N, func, I): self.N = N self.sz = 2**(N-1).bit_length() self.func = func self.I = I self.seg = [I] * (self.sz * 2) def assign(self, k, x): self.seg[k + self.sz] = x def build(self): for i in reversed(list(range(1, self.sz))): self.seg[i] = self.func(self.seg[2 * i], self.seg[2 * i + 1]) def update(self, k, x): k += self.sz self.seg[k] = x while k > 1: k >>= 1 self.seg[k] = self.func(self.seg[2 * k], self.seg[2 * k + 1]) def query(self, a, b): L = self.I R = self.I a += self.sz if b == self.N: b = self.sz b += self.sz while a < b: if a & 1: L = self.func(L, self.seg[a]) a += 1 if b & 1: b -= 1 R = self.func(self.seg[b], R) a >>= 1 b >>= 1 return self.func(L, R) def main(): def gcd(a, b): while b: a, b = b, a % b return a N = int(readline()) L = list(map(int, readline().split())) seg = SegmentTree(N, gcd, 0) for i, n in enumerate(L): seg.assign(i, n) seg.build() ans = 1 for i in range(N): ans = max(ans, gcd(seg.query(0, i), seg.query(i+1, N))) print(ans) main()
p03061
import sys readline = sys.stdin.buffer.readline class SegmentTree: def __init__(self, N, func, I): self.N = N self.sz = 2**(N-1).bit_length() self.func = func self.I = I self.seg = [I] * (self.sz * 2) def assign(self, k, x): self.seg[k + self.sz] = x def build(self): for i in reversed(list(range(1, self.sz))): self.seg[i] = self.func(self.seg[2 * i], self.seg[2 * i + 1]) def update(self, k, x): k += self.sz self.seg[k] = x while k > 1: k >>= 1 self.seg[k] = self.func(self.seg[2 * k], self.seg[2 * k + 1]) def query(self, a, b): L = self.I R = self.I a += self.sz if b == self.N: b = self.sz b += self.sz while a < b: if a & 1: L = self.func(L, self.seg[a]) a += 1 if b & 1: b -= 1 R = self.func(self.seg[b], R) a >>= 1 b >>= 1 return self.func(L, R) def main(): def gcd(a, b): while b: a, b = b, a % b return a N = int(readline()) L = list(map(int, readline().split())) seg = SegmentTree(N, gcd, 0) for i, n in enumerate(L): seg.assign(i, n) seg.build() ans = 1 for i in range(N): ans = max(ans, gcd(seg.query(0, i), seg.query(i+1, N))) print(ans) main()
# test import sys readline = sys.stdin.buffer.readline class SegmentTree: def __init__(self, N, func, I): self.N = N self.sz = 2**(N-1).bit_length() self.func = func self.I = I self.seg = [I] * (self.sz * 2) def assign(self, k, x): self.seg[k + self.sz] = x def build(self): for i in reversed(list(range(1, self.sz))): self.seg[i] = self.func(self.seg[2 * i], self.seg[2 * i + 1]) def update(self, k, x): k += self.sz self.seg[k] = x while k > 1: k >>= 1 self.seg[k] = self.func(self.seg[2 * k], self.seg[2 * k + 1]) def query(self, a, b): L = self.I R = self.I a += self.sz if b == self.N: b = self.sz b += self.sz while a < b: if a & 1: L = self.func(L, self.seg[a]) a += 1 if b & 1: b -= 1 R = self.func(self.seg[b], R) a >>= 1 b >>= 1 return self.func(L, R) def main(): def gcd(a, b): while b: a, b = b, a % b return a N = int(readline()) L = list(map(int, readline().split())) seg = SegmentTree(N, gcd, 0) for i, n in enumerate(L): seg.assign(i, n) seg.build() ans = 1 for i in range(N): ans = max(ans, gcd(seg.query(0, i), seg.query(i+1, N))) print(ans) main()
p03061
import sys readline = sys.stdin.buffer.readline class SegmentTree: def __init__(self, N, func, I): self.N = N self.sz = 2**(N-1).bit_length() self.func = func self.I = I self.seg = [I] * (self.sz * 2) def assign(self, k, x): self.seg[k + self.sz] = x def build(self): for i in reversed(list(range(1, self.sz))): self.seg[i] = self.func(self.seg[2 * i], self.seg[2 * i + 1]) def update(self, k, x): k += self.sz self.seg[k] = x while k > 1: k >>= 1 self.seg[k] = self.func(self.seg[2 * k], self.seg[2 * k + 1]) def query(self, a, b): if a >= b: return self.I L = self.I R = self.I a += self.sz if b == self.N: b = self.sz b += self.sz while a < b: if a & 1: L = self.func(L, self.seg[a]) a += 1 if b & 1: b -= 1 R = self.func(self.seg[b], R) a >>= 1 b >>= 1 return self.func(L, R) def main(): def gcd(a, b): while b: a, b = b, a % b return a N = int(readline()) L = list(map(int, readline().split())) seg = SegmentTree(N, gcd, 0) for i, n in enumerate(L): seg.assign(i, n) seg.build() ans = 1 for i in range(N): ans = max(ans, gcd(seg.query(0, i), seg.query(i+1, N))) print(ans) main()
import sys readline = sys.stdin.buffer.readline class SegmentTree: def __init__(self, N, func, I): self.N = N self.sz = 2**(N-1).bit_length() self.func = func self.I = I self.seg = [I] * (self.sz * 2) def assign(self, k, x): self.seg[k + self.sz] = x def build(self): for i in reversed(list(range(1, self.sz))): self.seg[i] = self.func(self.seg[2 * i], self.seg[2 * i + 1]) def update(self, k, x): k += self.sz self.seg[k] = x while k > 1: k >>= 1 self.seg[k] = self.func(self.seg[2 * k], self.seg[2 * k + 1]) def query(self, a, b): L = self.I R = self.I a += self.sz if b == self.N: b = self.sz b += self.sz while a < b: if a & 1: L = self.func(L, self.seg[a]) a += 1 if b & 1: b -= 1 R = self.func(self.seg[b], R) a >>= 1 b >>= 1 return self.func(L, R) def main(): def gcd(a, b): while b: a, b = b, a % b return a N = int(readline()) L = list(map(int, readline().split())) seg = SegmentTree(N, gcd, 0) for i, n in enumerate(L): seg.assign(i, n) seg.build() ans = 1 for i in range(N): ans = max(ans, gcd(seg.query(0, i), seg.query(i+1, N))) print(ans) main()
p03061
# test import sys readline = sys.stdin.buffer.readline class SegmentTree: def __init__(self, N, func, I): self.N = N self.sz = 2**(N-1).bit_length() self.func = func self.I = I self.seg = [I] * (self.sz * 2) def assign(self, k, x): self.seg[k + self.sz] = x def build(self): for i in reversed(list(range(1, self.sz))): self.seg[i] = self.func(self.seg[2 * i], self.seg[2 * i + 1]) def update(self, k, x): k += self.sz self.seg[k] = x while k > 1: k >>= 1 self.seg[k] = self.func(self.seg[2 * k], self.seg[2 * k + 1]) def query(self, a, b): L = self.I R = self.I a += self.sz b += self.sz while a < b: if a & 1: L = self.func(L, self.seg[a]) a += 1 if b & 1: b -= 1 R = self.func(self.seg[b], R) a >>= 1 b >>= 1 return self.func(L, R) def main(): def gcd(a, b): while b: a, b = b, a % b return a N = int(readline()) L = list(map(int, readline().split())) seg = SegmentTree(N, gcd, 0) for i, n in enumerate(L): seg.assign(i, n) seg.build() ans = 1 for i in range(N): ans = max(ans, gcd(seg.query(0, i), seg.query(i+1, N))) print(ans) main()
import sys readline = sys.stdin.buffer.readline class SegmentTree: def __init__(self, N, func, I): self.N = N self.sz = 2**(N-1).bit_length() self.func = func self.I = I self.seg = [I] * (self.sz * 2) def assign(self, k, x): self.seg[k + self.sz] = x def build(self): for i in range(self.sz-1, 0, -1): self.seg[i] = self.func(self.seg[2 * i], self.seg[2 * i + 1]) def update(self, k, x): k += self.sz self.seg[k] = x while k > 1: k >>= 1 self.seg[k] = self.func(self.seg[2 * k], self.seg[2 * k + 1]) def query(self, a, b): L = self.I R = self.I a += self.sz if b == self.N: b = self.sz b += self.sz while a < b: if a & 1: L = self.func(L, self.seg[a]) a += 1 if b & 1: b -= 1 R = self.func(self.seg[b], R) a >>= 1 b >>= 1 return self.func(L, R) def main(): def gcd(a, b): while b: a, b = b, a % b return a N = int(readline()) L = list(map(int, readline().split())) seg = SegmentTree(N, gcd, 0) for i, n in enumerate(L): seg.assign(i, n) seg.build() ans = 1 for i in range(N): ans = max(ans, gcd(seg.query(0, i), seg.query(i+1, N))) print(ans) main()
p03061
import sys readline = sys.stdin.buffer.readline class SegmentTree: def __init__(self, N, func, I): self.N = N self.sz = 2**(N-1).bit_length() self.func = func self.I = I self.seg = [I] * (self.sz * 2) def assign(self, k, x): self.seg[k + self.sz] = x def build(self): for i in range(self.sz-1, 0, -1): self.seg[i] = self.func(self.seg[2 * i], self.seg[2 * i + 1]) def update(self, k, x): k += self.sz self.seg[k] = x while k > 1: k >>= 1 self.seg[k] = self.func(self.seg[2 * k], self.seg[2 * k + 1]) def query(self, a, b): L = self.I R = self.I a += self.sz if b == self.N: b = self.sz b += self.sz while a < b: if a & 1: L = self.func(L, self.seg[a]) a += 1 if b & 1: b -= 1 R = self.func(self.seg[b], R) a >>= 1 b >>= 1 return self.func(L, R) def main(): def gcd(a, b): while b: a, b = b, a % b return a N = int(readline()) L = list(map(int, readline().split())) seg = SegmentTree(N, gcd, 0) for i, n in enumerate(L): seg.assign(i, n) seg.build() ans = 1 for i in range(N): ans = max(ans, gcd(seg.query(0, i), seg.query(i+1, N))) print(ans) main()
import sys readline = sys.stdin.buffer.readline class SegmentTree: def __init__(self, N, func, I): self.N = N self.sz = 2**(N-1).bit_length() self.func = func self.I = I self.seg = [I] * (self.sz * 2) def assign(self, k, x): self.seg[k + self.sz] = x def build(self): for i in reversed(list(range(1, self.sz))): self.seg[i] = self.func(self.seg[2 * i], self.seg[2 * i + 1]) def update(self, k, x): k += self.sz self.seg[k] = x while k > 1: k >>= 1 self.seg[k] = self.func(self.seg[2 * k], self.seg[2 * k + 1]) def query(self, a, b): L = self.I R = self.I a += self.sz if b == self.N: b = self.sz b += self.sz while a < b: if a & 1: L = self.func(L, self.seg[a]) a += 1 if b & 1: b -= 1 R = self.func(self.seg[b], R) a >>= 1 b >>= 1 return self.func(L, R) def main(): def gcd(a, b): while b: a, b = b, a % b return a N = int(readline()) L = list(map(int, readline().split())) seg = SegmentTree(N, gcd, 0) for i, n in enumerate(L): seg.assign(i, n) seg.build() ans = 1 for i in range(N): ans = max(ans, gcd(seg.query(0, i), seg.query(i+1, N))) print(ans) main()
p03061
import sys input = sys.stdin.readline N = int(eval(input())) A = list(map(int,input().split())) def gcd(a,b): if b == 0: return a return gcd(b,a%b) ans = 0 B = [] for i in range(N): b = [A[i],A[i-1]] B.append(gcd(max(b),min(b))) for i in range(-1,-N-1,-1): if N == 2: ans = max(A) break while 1: C = B.copy() C.pop(i) C.pop(i-(-1)) c = [] for j in range(len(C)): c.append(gcd(C[j],C[j-1])) if c == C: ans = max(ans,max(c)) break C = c print(ans)
N = int(eval(input())) A = list(map(int,input().split())) def gcd(a,b): if b == 0: return a return gcd(b,a%b) L,R = [A[0]],[A[-1]] Lg = A[0] Rg = A[-1] for i in range(N-1): Lg = gcd(Lg,A[i+1]) L.append(Lg) for i in range(N-1,0,-1): Rg = gcd(Rg,A[i-1]) R.append(Rg) L.append(A[-1]) R.append(A[0]) res = 0 for i in range(N): if i == 0: res = max(res,R[-3]) elif i == N-1: res = max(res,L[-3]) else: res = max(res,gcd(L[i-1],R[-i-3])) print(res)
p03061
import sys sys.setrecursionlimit(10**8) input = sys.stdin.readline from math import gcd class SegTree: def __init__(self, N, ele): self.num = 2**(N-1).bit_length() self.el = ele self.data = [ele]*(2*self.num) def calc(self, x, y): return gcd(x, y) def update(self, idx, x): idx += (self.num - 1) self.data[idx] = x while idx > 0: idx = (idx-1)//2 self.data[idx] = self.calc(self.data[2*idx+1], self.data[2*idx+2]) def prod(self, l, r): L = l + self.num R = r + self.num res = self.el while L < R: if L & 1: res = self.calc(res, self.data[L-1]) L += 1 if R & 1: R -= 1 res = self.calc(res, self.data[R-1]) L >>= 1 R >>= 1 return res def get(self, idx): idx += (self.num - 1) return self.data[idx] n = int(eval(input())) A = list(map(int, input().split())) seg = SegTree(n, 0) for i, a in enumerate(A): seg.update(i, a) ans = 0 for i in range(n): g = seg.get(i) seg.update(i, 0) ans = max(ans, seg.prod(0, n)) seg.update(i, g) print(ans)
from math import gcd n = int(eval(input())) A = [0] + list(map(int, input().split())) + [0] n += 2 L, R = [0]*n, [0]*n gl, gr = 0, 0 for i in range(n): gl = gcd(gl, A[i]) gr = gcd(gr, A[n-i-1]) L[i] = gl R[n-i-1] = gr ans = L[-1] for i in range(n-2): ans = max(ans, gcd(L[i], R[i+2])) print(ans)
p03061
import functools import copy N = int(eval(input())) A = list(map(int, input().split())) def gcd(a, b): while b != 0: a, b = b, a % b return a n = 0 if N == 2: n = max(A) else: for i in range(N): l = copy.copy(A) l.pop(i) res = functools.reduce(lambda x, y: gcd(x, y), l) if res > n: n = res print(n)
import functools import copy N = int(eval(input())) A = list(map(int, input().split())) def gcd(a, b): while b != 0: a, b = b, a % b return a l = [0] * N #L_i, i = 0, 1, 2, ..., N-1 for i in range(N): if i == 0: continue else: l[i] = gcd(l[i-1], A[i-1]) r = [0] * N #R_i, i = 0, 1, 2, ..., N-1 for j in range(N): if j == 0: continue else: r[N-j-1] = gcd(r[N-j], A[N-j]) m = [0] * N for i in range(N): m[i] = gcd(l[i], r[i]) print((max(m)))
p03061
import functools 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 = 1 def f(a,an): an = max(functools.reduce(gcd, a),an) return an for i in range(n): ans = f(a[:i]+a[i+1:],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())) b = [a[0]] c = [a[-1]] for i in range(1,n): j = n-1-i b.append(gcd(b[-1],a[i])) c.append(gcd(c[-1],a[j])) ans = max(b[-2],c[-2]) for i in range(n): ans = max(ans,gcd(b[i],c[n-3-i])) print(ans)
p03061
N = int(eval(input())) As = list(map(int,input().split())) As.sort(reverse=True) first_d = As[0] 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(As))) else: first = make_divisors(As[0]) second = make_divisors(As[1]) third = make_divisors(As[2]) div_list = list((set(first) & set(second)) | (set(first) & set(third)) | (set(third) & set(second))) div_list.sort(reverse=True) for d in range(len(div_list)): cnt = 0 for i in range(N): if As[i] % (div_list[d]) == 0: cnt += 1 if cnt >= N-1: ans = div_list[d] break print(ans)
N = int(eval(input())) As = list(map(int,input().split())) As.sort(reverse=True) first_d = As[0] 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(As))) else: first = make_divisors(As[0]) second = make_divisors(As[1]) third = make_divisors(As[2]) div_list = list((set(first) & set(second)) | (set(first) & set(third)) | (set(third) & set(second))) div_list.sort(reverse=True) for d in range(len(div_list)): cnt = 0 for i in range(N): if As[i] % (div_list[d]) != 0: cnt += 1 if cnt > 1: break if cnt <= 1 : ans = div_list[d] break print(ans)
p03061
#!/usr/bin/python3 # -*- coding: utf-8 -*- def euclid(a, b): if b == 0: return a else: return euclid(b, a%b) def list_euclid(int_list): ans = int_list[0] for i in range(1,len(int_list)): ans = euclid(ans, int_list[i]) if ans == 1: return ans return ans n = int(eval(input())) a = list(map(int, input().split())) a.sort() max = 0 b_int = 0 if n < 5: for i in range(0,n): if b_int == a[0]: p = a.pop(0) a.append(p) else: p = a.pop(0) b_int = p x = list_euclid(a) if max < x: max = x a.append(p) else: for i in range(0,n): if b_int == a[0]: p = a.pop(0) a.append(p) else: p = a.pop(0) b_int = p k = len(a) // 2 x = 0 y = list_euclid(a[0:k]) if y == 1: x = 1 else: z = list_euclid(a[k:n-1]) x = euclid(y, z) if max < x: max = x a.append(p) print(max)
#!/usr/bin/python3 # -*- coding: utf-8 -*- import sys def gcd(a, b): #最大公約数 while b: a, b = b, a % b return int(a) n = int(eval(input())) a = list(map(int, input().split())) if n==2: print((max(a))) sys.exit(0) elif n==3: lis = [gcd(a[0], a[1]), gcd(a[1], a[2]), gcd(a[2], a[0])] print((max(lis))) sys.exit(0) tmp_unused = gcd(a[0], a[1]) tmp_used = max(a[0], a[1]) #print("used=", tmp_used, "unused=", tmp_unused) for i in range(2,n): #print(a[i]) tmp_used = max(tmp_unused, gcd(tmp_used, a[i])) tmp_unused = gcd(tmp_unused, a[i]) #print("used=", tmp_used, "unused=", tmp_unused) print(tmp_used)
p03061
#!/usr/bin/python3 # -*- coding: utf-8 -*- def euclid(a, b): if b == 0: return a else: return euclid(b, a % b) def list_euclid(int_list): ans = int_list[0] if len(int_list) == 1: return ans for i in range(1,len(int_list)): ans = euclid(ans, int_list[i]) if ans == 1: return ans return ans n = int(eval(input())) a = list(map(int, input().split())) max_gcd = 0 b_int = {} min = min(a) if n == 2: max_gcd = max(a) elif n < 5: for i in range(0, n): if a[0] in b_int: p = a.pop(0) # a.append(p) else: p = a.pop(0) b_int[p] = p x = list_euclid(a) if max_gcd < x: max_gcd = x a.append(p) else: for i in range(0, n): if a[0] in b_int: p = a.pop(0) # a.append(p) else: if max_gcd == 0: p = a.pop(0) b_int[p] = p k = len(a) // 2 x = 0 y = list_euclid(a[0:k]) if y == 1: x = 1 else: z = list_euclid(a[k:len(a)]) x = euclid(y, z) if max_gcd < x: max_gcd = x a.append(p) elif min <= max_gcd: break else: p = a.pop(0) b_int[p] = p k = len(a) // 2 x = 0 if k < 1: x = list_euclid(a) else: y = list_euclid(a[0:k]) if y == 1: x = 1 else: z = list_euclid(a[k:len(a)]) x = euclid(y, z) if max_gcd < x: max_gcd = x a.append(p) print(max_gcd)
#!/usr/bin/python3 # -*- coding: utf-8 -*- def euclid(a, b): if b == 0: return a else: return euclid(b, a % b) def list_euclid(int_list): ans = int_list[0] if len(int_list) == 1: return ans kako_hash = {} kako_hash[ans] = ans for i in range(1,len(int_list)): if int_list[i] in kako_hash: continue else: kako_hash[int_list[i]] = int_list[i] ans = euclid(ans, int_list[i]) if ans == 1: return ans return ans n = int(eval(input())) a = list(map(int, input().split())) max_gcd = 0 b_int = {} min = min(a) if n == 2: max_gcd = max(a) elif n < 5: for i in range(0, n): if a[0] in b_int: p = a.pop(0) # a.append(p) else: p = a.pop(0) b_int[p] = p x = list_euclid(a) if max_gcd < x: max_gcd = x a.append(p) else: for i in range(0, n): if a[0] in b_int: p = a.pop(0) # a.append(p) else: if max_gcd == 0: p = a.pop(0) b_int[p] = p k = len(a) // 2 x = 0 y = list_euclid(a[0:k]) if y == 1: x = 1 else: z = list_euclid(a[k:len(a)]) x = euclid(y, z) if max_gcd < x: max_gcd = x a.append(p) elif min <= max_gcd: break else: p = a.pop(0) b_int[p] = p k = len(a) // 2 x = 0 if k < 1: x = list_euclid(a) else: y = list_euclid(a[0:k]) if y == 1: x = 1 else: z = list_euclid(a[k:len(a)]) x = euclid(y, z) if max_gcd < x: max_gcd = x a.append(p) print(max_gcd)
p03061
#!/usr/bin/env python3 import sys import math from bisect import bisect_right as br from bisect import bisect_left as bl sys.setrecursionlimit(2147483647) from heapq import heappush, heappop,heappushpop from collections import defaultdict from itertools import accumulate from collections import Counter from collections import deque from operator import itemgetter from itertools import permutations mod = 10**9 + 7 inf = float('inf') def I(): return int(sys.stdin.readline()) def LI(): return list(map(int,sys.stdin.readline().split())) def gcd(n,m): if m == 0: return n else: return gcd(m,n%m) n = I() a = LI() l = [0]*n r = [0]*n l[0] = a[0] for i in range(1,n): l[i] = gcd(l[i-1],a[i]) r[-1] = a[-1] for i in range(n-1)[::-1]: r[i] = gcd(r[i+1],a[i]) ans = 0 for i in range(n): if i == 0: ans = max(ans,r[i+1]) elif i == n-1: ans = max(ans,l[-2]) else: ans = max(ans,gcd(l[i-1],r[i+1])) print(ans)
#!/usr/bin/env python3 import sys import math from bisect import bisect_right as br from bisect import bisect_left as bl sys.setrecursionlimit(2147483647) from heapq import heappush, heappop,heappushpop from collections import defaultdict from itertools import accumulate from collections import Counter from collections import deque from operator import itemgetter from itertools import permutations mod = 10**9 + 7 inf = float('inf') def I(): return int(sys.stdin.readline()) def LI(): return list(map(int,sys.stdin.readline().split())) n = I() a = LI() L = [0] * (n+2) L[0] = a[0] R = [0] * (n+2) R[-1] = a[-1] for i in range(n): L[i+1] = math.gcd(a[i], L[i]) R[-(i+2)] = math.gcd(a[-(i+1)], R[-(i+1)]) ans = 0 for i in range(1, n+1): if i - 1 == 0: ans = max(ans, R[i+1]) elif i == n: ans = max(ans, L[i-1]) else: ans = max(ans, math.gcd(L[i-1], R[i+1])) print(ans)
p03061
def egcd(a, b): (x, lastx) = (0, 1) (y, lasty) = (1, 0) while b != 0: q = a // b (a, b) = (b, a % b) (x, lastx) = (lastx - q * x, x) (y, lasty) = (lasty - q * y, y) return a N=int(eval(input())) a=list(map(int,input().split())) l=[0]*N l[0]=a[0] for i in range(1,N): l[i]=egcd(l[i-1],a[i]) r=[0]*N r[N-1]=a[N-1] for i in range(N-2,-1,-1): r[i]=egcd(r[i+1],a[i]) ans=0 for i in range(N): if i==0: tmp=r[1] elif i==N-1: tmp=l[N-2] else: tmp=egcd(l[i-1],r[i+1]) ans=max(tmp,ans) print(ans)
def gcd(a, b): (x, lastx) = (0, 1) (y, lasty) = (1, 0) while b != 0: q = a // b (a, b) = (b, a % b) (x, lastx) = (lastx - q * x, x) (y, lasty) = (lasty - q * y, y) return a N=int(eval(input())) a=list(map(int,input().split())) 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[N-1]=a[N-1] 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): tmp=gcd(l[i-1],r[i+1]) ans=max(tmp,ans) print(ans)
p03061
def gcd(a, b): (x, lastx) = (0, 1) (y, lasty) = (1, 0) while b != 0: q = a // b (a, b) = (b, a % b) (x, lastx) = (lastx - q * x, x) (y, lasty) = (lasty - q * y, y) return a N=int(eval(input())) a=list(map(int,input().split())) 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[N-1]=a[N-1] 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): tmp=gcd(l[i-1],r[i+1]) ans=max(tmp,ans) print(ans)
def gcd(x, y): if y > x: x, y = y, x while y: x, y = y, x % y return x N=int(eval(input())) a=list(map(int,input().split())) 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[N-1]=a[N-1] 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): tmp=gcd(l[i-1],r[i+1]) ans=max(tmp,ans) print(ans)
p03061
def gcd(x, y): if y > x: x, y = y, x while y: x, y = y, x % y return x N=int(eval(input())) a=list(map(int,input().split())) 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[N-1]=a[N-1] 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): tmp=gcd(l[i-1],r[i+1]) ans=max(tmp,ans) print(ans)
def gcd(x, y): if y > x: x, y = y, x while y: x, y = y, x % y return x N=int(eval(input())) a=list(map(int,input().split())) 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[N-1]=a[N-1] 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): tmp=gcd(l[i-1],r[i+1]) if tmp>ans: ans=tmp print(ans)
p03061
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 import math p=100 N=int(eval(input())) a=list(map(int,input().split())) if N<=p: ans=0 for i in range(N): tmp=cul(i,a) if tmp>ans: ans=tmp print(ans) sys.exit() M=a[0] for i in range(1,p): M=gcd(M,a[i]) ans=0 R=a[p] for i in range(p,N): r=gcd(R,a[i]) if r>ans: for i in range(p): s=gcd(cul(i,a[:p]),R) 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 import math p=100 N=int(eval(input())) a=list(map(int,input().split())) if N<=p: ans=0 for i in range(N): tmp=cul(i,a) if tmp>ans: ans=tmp print(ans) sys.exit() M=a[0] for i in range(1,p): M=gcd(M,a[i]) ans=0 for i in range(p): s=gcd(cul(i,a[:p]),a[p]) if s>ans: ans=s print(ans)
p03061
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=100 N=int(eval(input())) a=list(map(int,input().split())) if N<=p: ans=0 for i in range(N): tmp=cul(i,a) if tmp>ans: ans=tmp print(ans) sys.exit() ans=0 for i in range(p): s=cul(i,a[: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())) a=list(map(int,input().split())) if N<=p: ans=0 for i in range(N): tmp=cul(i,a) if tmp>ans: ans=tmp print(ans) sys.exit() ans=0 for i in range(p): s=cul(i,a[:p]) if s>ans: ans=s print(ans)
p03061
N = int(eval(input())) nums = [int(i) for i in input().split()] from functools import reduce def gcd(a,b): while b!=0: a, b = b, a%b return a def compute_gcd(numbers): return reduce(gcd, numbers) from copy import copy gcds = [] for i in range(len(nums)): n = copy(nums) n.pop(i) gcds.append(compute_gcd(n)) print((max(gcds)))
N = int(eval(input())) nums = [int(i) for i in input().split()] nums_sorted = sorted(list((set(nums))), reverse=True) def compute_divs(n): divs = [] if n == 1: return [1] for i in range(2,int(n**0.5)+1): if n % i == 0: divs.append(i) if i != n // i: divs.append(n//i) divs.append(n) return divs ans = 1 if len(nums_sorted) == 1: ans = nums[0] else: divs1 = compute_divs(nums_sorted[0]) divs2 = compute_divs(nums_sorted[1]) divs = sorted(list(set(divs1+divs2))) for i in reversed(divs): c= 0 for j in nums: if j % i != 0: c += 1 if c == 2: break if c <= 1: ans = i break print(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())) if N == 2: print((max(A))) else: m = -1 for i in range(N): G = 0 for j in range(0, i): G = gcd(G, A[j]) for j in range(i+1, N): G = gcd(G, A[j]) m = max(m, G) print(m)
def gcd(a, b): while b > 0: a, b = b, a % b return a N = int(eval(input())) A = list(map(int, input().split())) if N == 2: print((max(A))) else: L = [0 for j in range(N+1)] R = [0 for j in range(N+1)] for j in range(N): L[j+1] = gcd(L[j], A[j]) for j in reversed(list(range(N))): R[j] = gcd(R[j+1], A[j]) m = -1 for j in range(N): m = max(m, gcd(L[j], R[j+1])) print(m)
p03061
def gcd(m,n): x = max(m,n) y = min(m,n) tmp = x%y while not tmp == 0: x = y y = tmp tmp = x%y return y N = int(eval(input())) A = list(map(int,input().split())) gcd_max = 0 for i in range(N): tmp = A[0] if i == 0: tmp = A[1] for j in range(N): if not j == i: tmp = gcd(tmp,A[j]) if tmp > gcd_max: gcd_max = tmp print(gcd_max)
N = int(eval(input())) A = list(map(int,input().split())) def GCD(x,y): while y != 0: x,y = y,x%y return x l,r = [0],[0] for i in range(N-1): l.append(GCD(l[-1],A[i])) r.append(GCD(r[-1],A[N-i-1])) ans = 0 for i in range(N): ans = max(ans,GCD(l[i],r[N-i-1])) print(ans)
p03061
from functools import reduce result = 0 def gcd(a,b): if b==0:return a return gcd(b,a%b) n = int(eval(input())) A = list(map(int, input().split())) for i in range(n): result = max(result, reduce(gcd, A[:i]+A[i+1:])) print(result)
from functools import reduce result = 0 def gcd(a,b): if b==0:return a return gcd(b,a%b) n = int(eval(input())) A = list(map(int, input().split())) X = [0 for i in range(n)] Y = [0 for i in range(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]) print((max(gcd(X[i], Y[i]) for i in range(n))))
p03061
N = int(eval(input())) A = list(map(int, input().split())) def factorize(x): """xの素因数分解を表す辞書 {素因数: 指数}""" f = {} d = 2 while x > 1: c = 0 while x % d == 0: c += 1 x //= d f[d] = c d += 1 if d * d > x > d: d = x return f def dropmin(ls): """[min(ls[:i] + ls[i + 1:]) for i in range(len(ls))]""" # l = [min(ls[:i]) for i in range(len(ls))] l = [float('inf')] * len(ls) for i in range(1, len(ls)): l[i] = min(l[i - 1], ls[i - 1]) # r = [min(ls[i + 1:]) for i in range(len(ls))] r = [float('inf')] * len(ls) for i in range(len(ls) - 2, -1, -1): r[i] = min(r[i + 1], ls[i + 1]) return [min(a, b) for a, b in zip(l, r)] def prod(ls): p = 1 for x in ls: p *= x return p # 各A_iの素因数 F = [factorize(a) for a in A] # 素因数の集合 (2要素に出現しない素因数は無視) D = set(F[0].keys()) D.update(list(F[1].keys())) # Dの要素 d → dが各A_iに含まれる個数のリスト X = {d: [f[d] if d in f else 0 for f in F] for d in D} # Dの要素 d → A_iを削除したときのdの個数の最小値のリスト X = {d: dropmin(X[d]) for d in D} S = [prod(d ** X[d][i] for d in D if X[d][i] > 0) for i in range(N)] print((max(S)))
# math.gcd is introduced in python 3.5 def gcd(a, b): while b > 0: a, b = b, a % b return a N = int(eval(input())) A = [int(a) for a in input().split()] # L[i] = gcd(A[:i + 1]) # R[i] = gcd(A[i:]) 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]) G = [0] * N G[0] = R[1] G[-1] = L[-2] for i in range(1, N - 1): G[i] = gcd(L[i-1], R[i+1]) print((max(G)))
p03061
def gcd(a,b): while b:a,b=b,a%b return a n=int(eval(input())) a=list(map(int,input().split())) if n==2:print((max(a[0],a[1])));exit() left=[a[0]] for i in range(1,n):left.append(gcd(left[-1],a[i])) right=[a[n-1]] for i in range(n-2,-1,-1):right.append(gcd(right[-1],a[i])) right.reverse() ans=max(right[1],left[n-2]) print((max(ans,max(gcd(left[i-1],right[i+1])for i in range(1,n-1)))))
def gcd(a,b): while b:a,b=b,a%b return a n,a=int(eval(input())),list(map(int,input().split())) if n==2:print((max(a)));exit() if n==3:print((max(gcd(a[0],a[1]),gcd(a[0],a[2]),gcd(a[1],a[2]))));exit() ans=ans2=a[0] for i in range(1,n): g=gcd(ans,a[i]) if g!=ans: ans=max(g,ans2) ans2=gcd(g,ans2) print(ans)
p03061
def GCD(a, b): """ Euclidean algorithm""" if a < b: (a, b) = (b, a) if b == 0: return a m = a % b while m != 0: a = b b = m m = a % b return b N = int(input()) A = [ int(v) for v in input().split() ] if N < 2 and 1e5 < N: raise() for v in A: if v < 1 and v < 1e9: raise() result = [] for skip in range(0, N): l = 0 r = 0 for i in range(0, skip): l = GCD(l, A[i]) for i in range(skip+1, N): r = GCD(r, A[i]) n = GCD(l, r) # print(l,r,n) result.append(n) print(max(result))
def GCD(a, b): """ Euclidean algorithm""" if a < b: (a, b) = (b, a) if b == 0: return a m = a % b while m != 0: a = b b = m m = a % b return b N = int(input()) A = [ int(v) for v in input().split() ] if N < 2 and 1e5 < N: raise() for v in A: if v < 1 and v < 1e9: raise() l_list = [] l = 0 for i in range(0, N): l = GCD(l, A[i]) l_list.append( l ) r_list = [] r = 0 for i in range(-1, -(N+1), -1): r = GCD(r, A[i]) r_list.append( r ) n = [] for i in range(0, N): if i-1 < 0: l = 0 else: l = l_list[i-1] if N-i-2 < 0: r = 0 else: r = r_list[N-i-2] n.append(GCD(l, r)) print(max(n))
p03061
def GCD(a, b): """ Euclidean algorithm""" if a < b: (a, b) = (b, a) if b == 0: return a m = a % b while m != 0: a = b b = m m = a % b return b N = int(input()) A = [ int(v) for v in input().split() ] if N < 2 and 1e5 < N: raise() for v in A: if v < 1 and v < 1e9: raise() l_list = [] l = 0 for i in range(0, N): l = GCD(l, A[i]) l_list.append( l ) r_list = [] r = 0 for i in range(-1, -(N+1), -1): r = GCD(r, A[i]) r_list.append( r ) n = [] for i in range(0, N): if i-1 < 0: l = 0 else: l = l_list[i-1] if N-i-2 < 0: r = 0 else: r = r_list[N-i-2] n.append(GCD(l, r)) print(max(n))
def GCD(a, b): """ Euclidean algorithm""" if a < b: (a, b) = (b, a) if b == 0: return a m = a % b while m != 0: a = b b = m m = a % b return b N = int(input()) A = [ int(v) for v in input().split() ] AR = list(reversed(A)) if N < 2 and 1e5 < N: raise() for v in A: if v < 1 and v < 1e9: raise() l_list = [0] for i in range(0, N): l = GCD(l_list[i], A[i]) l_list.append( l ) r_list = [0] for i in range(0, N): r = GCD(r_list[i], AR[i]) r_list.append( r ) n = [] j = -2 for i in range(0, N): l = l_list[i] r = r_list[j] n.append(GCD(l, r)) j -= 1 print(max(n))
p03061
def GCD(a, b): """ Euclidean algorithm""" if a < b: (a, b) = (b, a) if b == 0: return a m = a % b while m != 0: a = b b = m m = a % b return b N = int(input()) A = [ int(v) for v in input().split() ] AR = list(reversed(A)) if N < 2 and 1e5 < N: raise() for v in A: if v < 1 and v < 1e9: raise() l_list = [0] for i in range(0, N): l = GCD(l_list[i], A[i]) l_list.append( l ) r_list = [0] for i in range(0, N): r = GCD(r_list[i], AR[i]) r_list.append( r ) n = [] j = -2 for i in range(0, N): l = l_list[i] r = r_list[j] n.append(GCD(l, r)) j -= 1 print(max(n))
def GCD(a, b): """ Euclidean algorithm""" if a < b: (a, b) = (b, a) if b == 0: return a m = a % b while m != 0: a = b b = m m = a % b return b N = int(input()) A = [ int(v) for v in input().split() ] AR = list(reversed(A)) if N < 2 and 1e5 < N: raise() for v in A: if v < 1 and v < 1e9: raise() l_list = [0] r_list = [0] for i in range(0, N): l_list.append( GCD(l_list[i], A[i]) ) r_list.append( GCD(r_list[i], AR[i]) ) n = [] j = -2 for i in range(0, N): l = l_list[i] r = r_list[j] n.append(GCD(l, r)) j -= 1 print(max(n))
p03061
def GCD(a, b): """ Euclidean algorithm""" if a < b: (a, b) = (b, a) if b == 0: return a m = a % b while m != 0: a = b b = m m = a % b return b N = int(input()) A = [ int(v) for v in input().split() ] AR = list(reversed(A)) if N < 2 and 1e5 < N: raise() for v in A: if v < 1 and v < 1e9: raise() l_list = [0] r_list = [0] for i in range(0, N): l_list.append( GCD(l_list[i], A[i]) ) r_list.append( GCD(r_list[i], AR[i]) ) n = [] j = -2 for i in range(0, N): l = l_list[i] r = r_list[j] n.append(GCD(l, r)) j -= 1 print(max(n))
def GCD(a, b): """ Euclidean algorithm""" if a < b: (a, b) = (b, a) if b == 0: return a m = a % b while m != 0: a = b b = m m = a % b return b N = int(input()) A = [ int(v) for v in input().split() ] AR = list(reversed(A)) if N < 2 and 1e5 < N: raise() for v in A: if v < 1 and v < 1e9: raise() l_list = [0] r_list = [0] for i in range(0, N): l_list.append( GCD(l_list[i], A[i]) ) r_list.append( GCD(r_list[i], AR[i]) ) n = [] j = -2 for i in range(0, N): n.append(GCD(l_list[i], r_list[j])) j -= 1 print(max(n))
p03061
def GCD(a, b): """ Euclidean algorithm""" if a < b: (a, b) = (b, a) if b == 0: return a m = a % b while m != 0: a = b b = m m = a % b return b N = int(input()) A = [ int(v) for v in input().split() ] if N < 2 and 1e5 < N: raise() for v in A: if v < 1 and v < 1e9: raise() l_list = [0] r_list = [0] j = -1 for i in range(0, N): l_list.append( GCD(l_list[i], A[i]) ) r_list.insert( 0, GCD(r_list[j], A[j]) ) j -= 1 n = [] for i in range(0, N): n.append(GCD(l_list[i], r_list[i+1])) print(max(n))
def GCD(a, b): """ Euclidean algorithm""" if a < b: (a, b) = (b, a) if b == 0: return a m = a % b while m != 0: a = b b = m m = a % b return b N = int(input()) A = [ int(v) for v in input().split() ] if N < 2 and 1e5 < N: raise() for v in A: if v < 1 and v < 1e9: raise() l_list = [0] r_list = [0] j = -1 for i in range(0, N): l_list.append( GCD(l_list[i], A[i]) ) r_list.append( GCD(r_list[i], A[j]) ) j -= 1 n = [] j = -2 for i in range(0, N): n.append(GCD(l_list[i], r_list[j])) j -= 1 print(max(n))
p03061
def GCD(a, b): """ Euclidean algorithm""" if a < b: (a, b) = (b, a) if b == 0: return a m = a % b while m != 0: a = b b = m m = a % b return b N = int(input()) A = [ int(v) for v in input().split() ] if N < 2 and 1e5 < N: raise() for v in A: if v < 1 and v < 1e9: raise() l_list = [0] r_list = [0] j = -1 for i in range(0, N): l_list.append( GCD(l_list[i], A[i]) ) r_list.append( GCD(r_list[i], A[j]) ) j -= 1 n = [] j = -2 for i in range(0, N): n.append(GCD(l_list[i], r_list[j])) j -= 1 print(max(n))
def GCD(a, b): """ Euclidean algorithm""" if a < b: (a, b) = (b, a) if b == 0: return a m = a % b while m != 0: a = b b = m m = a % b return b N = int(input()) A = [ int(v) for v in input().split() ] if N < 2 and 1e5 < N: raise() for v in A: if v < 1 and v < 1e9: raise() l_list = [0] r_list = [0] j = -1 for i in range(0, N): l_list.append( GCD(l_list[i], A[i]) ) r_list.append( GCD(r_list[i], A[j]) ) j -= 1 j = -2 mymax = 1 for i in range(0, N): tmp = GCD(l_list[i], r_list[j]) if tmp > mymax: mymax = tmp j -= 1 print(mymax)
p03061
from collections import Counter as c m=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 arr=[] for i in a: arr+=make_divisors(i) commonarr=c(arr).most_common() ans=0 for i in commonarr: if i[1]>=m-1: ans=max(ans,i[0]) else: break print(ans)
n=int(eval(input())) a=sorted(list(map(int,input().split()))) i=a[1] while i > 0: flg=0 for j in a: if j%i!=0: flg+=1 if flg>1: break if flg<=1: print(i) break else: i-=1
p03061
import sys import math import collections import itertools import array import inspect # Set max recursion limit sys.setrecursionlimit(1000000) # Debug output def chkprint(*args): names = { id(v): k for k, v in list(inspect.currentframe().f_back.f_locals.items()) } print((', '.join( names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))) # Binary converter def to_bin(x): return bin(x)[2:] def li_input(): return [int(_) for _ in sys.stdin.readline().split()] def gcd(n, m): if n % m == 0: return m else: return gcd(m, n % m) def gcd_list(L): v = L[0] for i in range(1, len(L)): v = gcd(v, L[i]) return v def lcm(n, m): return (n * m) // gcd(n, m) def lcm_list(L): v = L[0] for i in range(1, len(L)): v = lcm(v, L[i]) return v def comb(n, r): if n - r < r: r = n - r if r == 0: return 1 if r == 1: return n numerator = [n - r + k + 1 for k in range(r)] denominator = [k + 1 for k in range(r)] for p in range(2, r + 1): pivot = denominator[p - 1] if pivot > 1: offset = (n - r) % p for k in range(p - 1, r, p): numerator[k - offset] /= pivot denominator[k] /= pivot result = 1 for k in range(r): if numerator[k] > 1: result *= int(numerator[k]) return result def bisearch(L, target): low = 0 high = len(L) - 1 while low <= high: mid = (low + high) // 2 guess = L[mid] if guess == target: return True elif guess < target: low = mid + 1 elif guess > target: high = mid - 1 if guess != target: return False # -------------------------------------------- dp = None def main(): N = int(eval(input())) A = li_input() ans = 0 for i in range(N): A_ = A[:i] + A[i+1:] ans = max(ans, gcd_list(A_)) print(ans) main()
import sys import math import collections import itertools import array import inspect # Set max recursion limit sys.setrecursionlimit(1000000) # Debug output def chkprint(*args): names = { id(v): k for k, v in list(inspect.currentframe().f_back.f_locals.items()) } print((', '.join( names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))) # Binary converter def to_bin(x): return bin(x)[2:] def li_input(): return [int(_) for _ in sys.stdin.readline().split()] def gcd(n, m): if n % m == 0: return m else: return gcd(m, n % m) def gcd_list(L): v = L[0] for i in range(1, len(L)): v = gcd(v, L[i]) return v def lcm(n, m): return (n * m) // gcd(n, m) def lcm_list(L): v = L[0] for i in range(1, len(L)): v = lcm(v, L[i]) return v def comb(n, r): if n - r < r: r = n - r if r == 0: return 1 if r == 1: return n numerator = [n - r + k + 1 for k in range(r)] denominator = [k + 1 for k in range(r)] for p in range(2, r + 1): pivot = denominator[p - 1] if pivot > 1: offset = (n - r) % p for k in range(p - 1, r, p): numerator[k - offset] /= pivot denominator[k] /= pivot result = 1 for k in range(r): if numerator[k] > 1: result *= int(numerator[k]) return result def bisearch(L, target): low = 0 high = len(L) - 1 while low <= high: mid = (low + high) // 2 guess = L[mid] if guess == target: return True elif guess < target: low = mid + 1 elif guess > target: high = mid - 1 if guess != target: return False # -------------------------------------------- dp = None def main(): N = int(eval(input())) A = sorted(li_input(), reverse=True) ans = 10**100 ans_i = -1 for i in range(len(A)-1): g = gcd(A[-1], A[i]) if g < ans: ans = g ans_i = i print((max( gcd_list(A[:-1]), gcd_list(A[:ans_i] + A[ans_i+1:]) ))) main()
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) ans = 0 L = [0] R = [0] for i in range(n-1): l = gcd(L[-1], A[i]) L.append(l) r = gcd(R[-1], A[::-1][i]) R.append(r) for l, r in zip(L,R[::-1]): ans = max(ans, gcd(l, r)) print(ans)
n = int(eval(input())) A = list(map(int, input().split())) def gcd(a, b): if b == 0: return a return gcd(b, a%b) ans = 0 L = [0]*(n) R = [0]*(n+1) for i in range(n-1): L[i+1] = gcd(L[i], A[i]) R[i+1] = gcd(R[i], A[-i-1]) for l, r in zip(L[::-1],R): ans = max(ans, gcd(l, r)) print(ans)
p03061
n = int(eval(input())) a = [int(m) for m in input().split()] a.sort() m0 = a[0] m1 = a[1] 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 mm0 = make_divisors(m0) mm1 = make_divisors(m1) mm = mm0 + mm1 mm.sort() si = 0 candi = 0 for g in mm: si = 0 for i in range(n): if a[i] % g != 0: si += 1 if si == 2: break else: candi = g print(candi)
import sys # import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random # from collections import deque, defaultdict, Counter; from heapq import heappush, heappop # from itertools import permutations, combinations, product, accumulate, groupby # from bisect import bisect_left, bisect_right, insort_left, insort_right # from operator import itemgetter as ig sys.setrecursionlimit(10 ** 7) # inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; ansli = []; tmpli = []; candili = []; mod = 10 ** 9 + 7 # dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)] """for dx, dy in dd: nx = j + dx; ny = i + dy if 0 <= nx < w and 0 <= ny < h:""" def wi(): return list(map(int, sys.stdin.readline().split())) def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint def ws(): return sys.stdin.readline().split() def i(): return int(sys.stdin.readline()) def s(): return eval(input()) def hi(n): return [i() for _ in range(n)] def hs(n): return [s() for _ in range(n)]#HeightString def mi(n): return [wi() for _ in range(n)]#MatrixInt def mip(n): return [wip() for _ in range(n)] def ms(n): return [ws() for _ in range(n)] n = i() a = wi() def gcd(a,b): if b == 0: return a return gcd(b, a % b) l_gcd = [a[0]]#左から r_gcd = [a[-1]]#右から kl = a[0] kr = a[-1] for i in range(n - 2): kl = gcd(kl, a[i + 1]) l_gcd.append(kl) kr = gcd(kr, a[-(i + 2)]) r_gcd.append(kr) ansli = [] for i in range(n): if i == 0: ansli.append(r_gcd[-1]) elif i == n - 1: ansli.append(l_gcd[-1]) else: ansli.append(gcd(l_gcd[i - 1], r_gcd[-(i + 1)])) print((max(ansli)))
p03061
n = int(eval(input())) num_list = list(map(int, input().split())) num_list.sort() num_list.reverse() answer_list = [] max_num = 0 flag = False for i in range(num_list[0], 0, -1): if flag and count < 2: break else: flag = True count = 0 max_num = i for j in range(n): if num_list[j] / max_num < 1 or num_list[j] % max_num != 0: count += 1 if count > 1: break print(max_num)
n = int(eval(input())) a = list(map(int, input().split())) end = sorted(a)[1] for i in range(end + 1, 0, -1): x = 0 for j in a: if j % i != 0: x += 1 if x >= 2: break if x <= 1: print(i) break
p03061
n = int(eval(input())) A = list(map(int,input().split())) ass = set() def div(x): for i in range(1,int(x**0.5)+1): if x%i == 0: ass.add(i) ass.add(x//i) return ass a0 = A[0] a1 = A[1] div(a0) div(a1) ans = [] for i in ass: cnt = 0 for a in A: if a%i == 0: cnt += 1 if cnt >= n-1: ans.append(i) print((max(ans)))
import sys sys.setrecursionlimit(10**7) input = sys.stdin.readline INF = float('inf') MOD = 10**9+7 n = int(eval(input())) A = list(map(int,input().split())) ass = set() def div(x): for i in range(1,int(x**0.5)+1): if x%i == 0: ass.add(i) ass.add(x//i) return ass a0 = A[0] a1 = A[1] div(a0) div(a1) ans = 1 for i in ass: cnt = 0 for a in A: if a%i == 0: cnt += 1 if cnt >= n-1: ans = max(ans,i) print(ans)
p03061
import sys input = sys.stdin.readline n = int(eval(input())) a = list(map(int,input().split())) a.sort() #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 #nの約数列挙 def divisor(n): ass = [] for i in range(1,int(n**0.5)+1): if n%i == 0: ass.append(i) if i**2 == n: continue ass.append(n//i) return ass #sortされていない #0番目を使わない場合 res = gcdlist(a[1::]) #else p = sorted(divisor(a[0])) for i in range(len(p)-1,-1,-1): if p[i]<=res: break cnt = 1 for j in range(1,n): if a[j]%p[i]==0: cnt += 1 if cnt >= n-1: res = p[i] break print(res)
#a,bの最大公約数 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())) #b[i]:a[0]~a[i]までのgcd,c[i]:a[i]~a[-1]までのgcd b = [0]*n c = [0]*n for i in range(n): if i == 0: b[0] = a[0] c[-1] = a[-1] continue b[i]=gcd(b[i-1],a[i]) c[n-1-i]=gcd(c[n-i],a[n-1-i]) res = max(b[n-2],c[1]) for i in range(1,n-1): res = max(res,gcd(b[i-1],c[i+1])) print(res)
p03061
def 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 def main(): N = int(eval(input())) A = list(map(int, input().split())) res = {} for a in A: for divisor in divisors(a): if divisor not in res: res[divisor] = 1 else: res[divisor] += 1 print((max([key for key, value in list(res.items()) if value == N or value == N - 1]))) if __name__ == '__main__': main()
def gcd(a, b): while b != 0: a, b = b, a % b return a def main(): N = int(eval(input())) A = list(map(int, input().split())) L = [0] R = [0] for i in range(N): L.append(gcd(L[i], A[i])) R.append(gcd(R[i], A[N-i-1])) R.reverse() M = [gcd(L[i], R[i+1]) for i in range(N)] print((max(M))) if __name__ == '__main__': main()
p03061
from collections import Counter def getDivisor(x): ans = [] for d in range(1, int(x**0.5)+1): if x % d == 0: ans.append(d) if x//d != d: ans.append(x//d) return sorted(ans) N = int(eval(input())) As = list(map(int, input().split())) divisors = getDivisor(As[0]) + getDivisor(As[1]) cnt = Counter(divisors) for i, A in enumerate(As[2:], 2): for d, num in list(cnt.items()): if A%d == 0: cnt[d] += 1 for d, num in list(cnt.items()): if num < i: del cnt[d] print((max(cnt.keys())))
from itertools import accumulate def gcd(a, b): while b: a, b = b, a%b return a N = int(eval(input())) As = list(map(int, input().split())) Ls = [As[-1]] + list(accumulate(As, gcd)) Rs = list(accumulate(As[::-1], gcd))[::-1] + [As[0]] ans = 0 for i in range(N): g = gcd(Ls[i], Rs[i+1]) ans = max(ans, g) print(ans)
p03061
def gcd(x, y): if x > y: tmp = x x = y y = tmp if x == 0: return y else: return gcd(y%x, x) N = int(eval(input())) A = list(map(int, input().split())) # if N == 2: # print(max(A[0], A[1])) # return 0 Llist = [0] Rlist = [0] for i in range(N): Llist.append(gcd(Llist[i], A[i])) Rlist.insert(0, gcd(Rlist[0], A[N-1-i])) ans = 0 for i in range(N): t = gcd(Llist[i], Rlist[i+1]) if ans < t: ans = t print(ans)
def gcd(x, y): if x > y: tmp = x x = y y = tmp if x == 0: return y else: return gcd(y%x, x) N = int(eval(input())) A = list(map(int, input().split())) Llist = [0] Rlist = [0] for i in range(N): Llist.append(gcd(Llist[i], A[i])) # Rlist.insert(0, gcd(Rlist[0], A[N-1-i])) Rlist.append(gcd(Rlist[i], A[N-1-i])) Rlist = Rlist[::-1] ans = 0 for i in range(N): t = gcd(Llist[i], Rlist[i+1]) if ans < t: ans = t print(ans)
p03061
import copy n = int(eval(input())) x = [int(i) for i in input().split()] #ユークリッド互除法の関数の定義 def gcd(a,b): if b==0:return a return gcd(b,a%b) def gcd_multi(n,x): if n==1: return x[0] else: element = gcd(x[0], x[1]) if n==2: return element else: for i in range(2,n): element=gcd(element,x[i]) return element ans = [] for i in range(n): l = copy.deepcopy(x) del l[i] ans.append(gcd_multi(n-1,l)) print((max(ans)))
import copy n = int(eval(input())) x = [int(i) for i in input().split()] #ユークリッド互除法の関数の定義 def gcd(a,b): if b==0:return a return gcd(b,a%b) def gcd_multi(n,x): if n==1: return x[0] else: element = gcd(x[0], x[1]) if n==2: return element else: for i in range(2,n): element=gcd(element,x[i]) return element X=[0 for i in range(n)] Y=[0 for i in range(n)] for i in range(1,n): X[i]=gcd(X[i-1],x[i-1]) for i in range(n-1)[::-1]: Y[i]=gcd(Y[i+1],x[i+1]) Z=[gcd(X[i],Y[i]) for i in range(n)] print((max(Z)))
p03061
import sys sys.setrecursionlimit(10**6) n = int(eval(input())) a = list(map(int, input().split())) #n, m = map(int, input().split()) #s = input() #s,t = input().split() #a = [int(input()) for _ in range(n)] # #readline = sys.stdin.readline #n,m = [int(i) for i in readline().split()] #ab = [[int(i) for i in readline().split()] for _ in range(n)] ans = 1 from math import gcd for i in range(n): tmp = -1 for j in range(n): if i != j: if tmp == -1: tmp = a[j] else: tmp = gcd(tmp, a[j]) ans = max(ans, tmp) print(ans)
""" 累積GCD """ import sys sys.setrecursionlimit(10**6) n = int(eval(input())) A = list(map(int, input().split())) from math import gcd L = [0] * n R = [0] * n for i in range(n-1): L[i+1] = gcd(L[i], A[i]) for i in range(n-1, 0, -1): R[i-1] = gcd(R[i], A[i]) ans = 0 for i in range(n): ans = max(ans, gcd(L[i], R[i])) print(ans)
p03061
def main(): N = int(eval(input())) A = [int(i) for i in input().split()] gcd_L = [0]*(N+1) gcd_R = [0]*(N+1) def gcd(x, y): if y == 0: return x while y != 0: x, y = y, x % y return x for i, a in enumerate(A): gcd_L[i+1] = gcd(gcd_L[i], a) for i, a in enumerate(A[::-1], start=1): gcd_R[-(i+1)] = gcd(gcd_R[-i], a) ans = 0 for i in range(N): cur = gcd(gcd_L[i], gcd_R[i+1]) ans = max(ans, cur) print(ans) if __name__ == '__main__': main()
def main(): N = int(eval(input())) A = [int(i) for i in input().split()] gcd_L = [0]*(N+2) gcd_R = [0]*(N+2) def gcd(x, y): if y == 0: return x while y != 0: x, y = y, x % y return x for i, a in enumerate(A): gcd_L[i+1] = gcd(gcd_L[i], a) for i, a in enumerate(A[::-1], start=1): gcd_R[-(i+1)] = gcd(gcd_R[-i], a) gcd_R[0] = gcd_R[1] gcd_L[-1] = gcd_L[-2] # print(gcd_L, gcd_R) ans = 0 for i in range(1, N+1): cur = gcd(gcd_L[i-1], gcd_R[i+1]) ans = max(ans, cur) print(ans) if __name__ == '__main__': main()
p03061
from collections import Counter N = int(eval(input())) #約数を列挙 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 A_list = [make_divisors(int(e)) for e in input().split()] all_divisors_list=[] for i in A_list: all_divisors_list += i collected_adl = Counter(all_divisors_list) ans = 1 for a in collected_adl.most_common(): if a[1] <= N-2: break elif a[0]>ans: ans=a[0] print(ans)
from collections import Counter from functools import lru_cache N = int(eval(input())) #約数を列挙 @lru_cache(maxsize=1000) 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 A_list = [make_divisors(int(e)) for e in input().split()] all_divisors_list=[] for i in A_list: all_divisors_list += i collected_adl = Counter(all_divisors_list) ans = 1 for a in collected_adl.most_common(): if a[1] <= N-2: break elif a[0]>ans: ans=a[0] print(ans)
p03061
import sys from functools import reduce input = sys.stdin.readline def gcd(a,b): if b>a: a, b = b, a while b: a, b = b, a%b return a def gcds(nums): return reduce(gcd, nums) n = int(eval(input())) a = [int(i) for i in input().split()] ans = 0 for i in range(len(a)): if i == 0: l = 0 else: l = gcds(a[:i]) if i == len(a)-1: r = 0 else: r = gcds(a[(i+1):]) aaa = gcd(l, r) if ans < aaa: ans = aaa print(ans)
import sys from functools import reduce input = sys.stdin.readline def gcd(a,b): if b>a: a, b = b, a while b: a, b = b, a%b return a n = int(eval(input())) a = [int(i) for i in input().split()] ans = 0 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]) for i in range(n): ans = max(ans, gcd(l[i], r[i+1])) print(ans)
p03061
# AtCoder Beginner Contest 125 # https://atcoder.jp/contests/abc125 import sys s2nn = lambda s: [int(c) for c in s.split(' ')] ss2nn = lambda ss: [int(s) for s in list(ss)] ss2nnn = lambda ss: [s2nn(s) for s in list(ss)] i2s = lambda: sys.stdin.readline().rstrip() i2n = lambda: int(i2s()) i2nn = lambda: s2nn(i2s()) ii2ss = lambda n: [sys.stdin.readline() for _ in range(n)] ii2nnn = lambda n: ss2nnn(ii2ss(n)) cache = {} def gcd(a, b): if a not in cache: cache[a] = {} if b in cache[a]: return cache[a][b] if b == 0: cache[a][b] = a return a n = gcd(b, a % b) cache[a][b] = n return n def main(N, An): if N == 2: n = max(An[0], An[1]) print(n) return nn = [An[0]] * N nn[0] = An[1] for i in range(1, N): for j in range(N): if i == j: continue nn[j] = gcd(nn[j], An[i]) print((int(max(nn)))) N = i2n() An = i2nn() main(N, An)
# AtCoder Beginner Contest 125 # https://atcoder.jp/contests/abc125 import sys s2nn = lambda s: [int(c) for c in s.split(' ')] ss2nn = lambda ss: [int(s) for s in list(ss)] ss2nnn = lambda ss: [s2nn(s) for s in list(ss)] i2s = lambda: sys.stdin.readline().rstrip() i2n = lambda: int(i2s()) i2nn = lambda: s2nn(i2s()) ii2ss = lambda n: [sys.stdin.readline() for _ in range(n)] ii2nnn = lambda n: ss2nnn(ii2ss(n)) cache = {} def gcd(a, b): if a not in cache: cache[a] = {} if b in cache[a]: return cache[a][b] if b == 0: cache[a][b] = a return a n = gcd(b, a % b) cache[a][b] = n return n def main_lte(N, An): if N == 2: n = max(An[0], An[1]) print(n) return nn = [An[0]] * N nn[0] = An[1] for i in range(1, N): for j in range(N): if i == j: continue nn[j] = gcd(nn[j], An[i]) print((int(max(nn)))) def main(N, An): if N == 2: n = max(An[0], An[1]) print(n) return nn0 = gcd(An[0], An[1]) nn1 = set((An[0], An[1])) for i in range(2, N): t1 = nn0 nn0 = gcd(nn0, An[i]) t2 = [gcd(n, An[i]) for n in nn1] nn1 = set(t2) nn1.add(t1) n = max(nn1) print((int(n))) N = i2n() An = i2nn() main(N, An)
p03061
def gcd(x, y): if y == 0: return x else: return gcd(y, x % y) def gcd_array(A): n = len(A) s = A[0] i = 1 while(i < n): s = gcd(s,A[i]) i += 1 return s if __name__ == "__main__": N = int(eval(input())) B = list(map(int,input().split())) max = 0 i = 0 while(i < N): A = [i for i in B] A.pop(i) d = gcd_array(A) if max <= d: max = d i += 1 print(max)
def gcd(x, y): if y == 0: return x else: return gcd(y, x % y) def gcd_array(A): n = len(A) s = A[0] i = 1 Down = [0] while(i < n): t = gcd(s,A[i]) if t < s: Down.append(i) s = t i += 1 return s,Down if __name__ == "__main__": N = int(eval(input())) B = list(map(int,input().split())) max = 0 d,Down = gcd_array(B) C = [i//d for i in B] c = C[0] K = 1 for j in Down: A = [i for i in C] c = A.pop(j) l = gcd_array(A)[0] if K < l: K = l print((K * d))
p03061