input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
x,y=list(map(int, input().split())) def answer(x,y): for i in range(y, 0, -1): if x % i == 0 and y % i == 0: return i if x == y: print(x) elif x >= y: print((str(answer(x, x%y)))) else: print((str(answer(y, y%x))))
x,y=list(map(int, input().split())) def gcd(x,y): if x < y: x,y = y,x while y > 0: r = x % y x = y y = r return x print((str(gcd(x,y))))
p02256
def devisor_list(x): result = [] for n in range(1, x + 1): if x % n == 0: result.append(n) return result def common_max_element(array1, array2): result = [] for n in array1: if n in array2: result.append(n) return max(result) x, y = [int(n)...
def gcd(x, y): if x < y: x, y = [y, x] while y > 0: r = x % y x = y y = r return x x, y = [int(n) for n in input().split()] print((gcd(x, y)))
p02256
# -*- coding:utf-8 -*- def gcd(n,m): k = max(n,m) for i in range(k): if n%(k-i) == 0 and m%(k-i) == 0: return k-i x, y = list(map(int,input().split())) print((gcd(x,y)))
# -*- coding:utf-8 -*- def gcd(n,m): if n < m: tmp = m m = n n = tmp while m > 0: r = n % m n = m m = r return n x, y = list(map(int,input().split())) print((gcd(x,y)))
p02256
dbg = False a, b = list(map(int, input().split())) if a > b: x, y = a, b else: x, y = b, a for i in range(1, y+1): if y % i == 0: gcd_tmp = int(y / i) if dbg: print(gcd_tmp) if x % gcd_tmp == 0: gcd = gcd_tmp break print(gcd)
dbg = False a, b = list(map(int, input().split())) if a > b: x, y = a, b else: x, y = b, a for i in range(1, int((y+1)/2)+1): if y % i == 0: gcd_tmp = int(y / i) if dbg: print(gcd_tmp) if x % gcd_tmp == 0: print(gcd_tmp) exit() print((1))
p02256
dbg = False a, b = list(map(int, input().split())) if a > b: x, y = a, b else: x, y = b, a for i in range(1, int((y+1)/2)+1): if y % i == 0: gcd_tmp = int(y / i) if dbg: print(gcd_tmp) if x % gcd_tmp == 0: print(gcd_tmp) exit() print((1))
dbg = False a, b = list(map(int, input().split())) if a > b: x, y = a, b else: x, y = b, a divs = [1] for i in range(1, int((y+1)/2)+1): gcd_tmp = int(y / i) if dbg: print(('%d, %d' % (i, gcd_tmp))) if i > gcd_tmp: break if y % i == 0: if x % gcd_tmp == 0: ...
p02256
x, y = input().split() x = int(x) y = int(y) r = 1 while r > 0: if x >= y: r = x % y else: r = y % x if r != 0: x = y y = r else: print(y)
def gcd(x, y): if y == 0: print(x) return x return gcd(y, x%y) x, y = list(map(int, input().split())) if y > x: t = y y = x x = t gcd(x, y)
p02256
a,b = list(map(int,input().split())) gcd = 1 i = 2 while True: c = i for i in range(c,min(a,b)+1): if a%i == 0 and b%i ==0: gcd *= i a = int(a/i) b = int(b/i) break else: break print(gcd)
a,b = list(map(int,input().split())) if a<b: ret = a a = b b = ret def gcd(a,b): c = a % b if c == 0: return b else: return gcd(b,c) print((str(gcd(a,b))))
p02256
x,y=list(map(int,input().split())) def gg(a,b): for d in range(b,0,-1): #print(d) if a%d==0 and b%d==0: return d if x>y: print((gg(x,y))) else: print((gg(y,x)))
x,y=list(map(int,input().split())) def gg(a,b): if a%b==0 and b%b==0: return b return gg(b,a%b) if x>y: print((gg(x,y))) else: print((gg(y,x)))
p02256
a,b=list(map(int,input().split())) z=[] for i in range(min(a,b)): if a%(i+1)==0 and b%(i+1)==0: z+=[i+1] print((max(z)))
z=list(map(int,input().split())) z.sort() a=0 while True: if z[1]==z[0]: break a=z[1]-z[0] if z[0]%a==0: z[1] = z[0] z[0] = a z.sort() break z[1]=z[0] z[0]=a z.sort() print((z[0]))
p02256
x, y = list(map(int, input().split())) s = min(x,y) l = max(x,y) for i in reversed(list(range(s+1))): if s % i == 0: if l % i == 0: print(i) break
x, y = list(map(int, input().split())) l = max(x, y) s = min(x, y) for i in range(x): olds = s oldl = l s = oldl % olds l = olds if s == 0: print(l) break
p02256
def div(x, y): #x>y A = 1 for i in range(1, x+1): if x % i == 0 and y % i == 0: A = i return A x, y = list(map(int, input().split(" "))) print((div(x,y)))
import math def div(x, y): #x>y A = 1 for i in range(1, int(math.sqrt(x))+1): if x % i == 0 and y % i == 0: A = max(A, i) j = int(x/i) if x % j == 0 and y % j == 0: A = max(A, j) return A x, y = list(map(int, input().split(" "))) print((div(x,y))) ...
p02256
x,y=list(map(int,input().split())) xg=set() yg=set() for i in range(1,x+1): if x%i==0: xg.add(i) for j in range(1,y+1): if y%j==0: yg.add(j) gcd=xg & yg print((max(gcd)))
if __name__ == '__main__': s = input().split() if s[0] < s[1]: tmp = s[0] s[0] = s[1] s[1] = tmp while True: x = int(s[0]) % int(s[1]) if x==0: break s[0] = s[1] s[1] = x print((s[1]))
p02256
def greatest_common_divisor(x, y): if x > y: max = x else: max = y answer = 0 for i in range(1, max+1): if x % i == 0 and y % i == 0: answer = i return answer if __name__ == '__main__': x, y = [int(x) for x in input().split()] print((greatest...
def greatest_common_divisor(x, y): if x < y: x, y = y, x if y == 0: return x else: return greatest_common_divisor(y, x % y) if __name__ == '__main__': x, y = [int(x) for x in input().split()] print((greatest_common_divisor(x, y)))
p02256
XY=list(map(int,input().split())) x=max(XY) y=min(XY) x_dy=x%y ans=0 if x==y: ans=x else: for d in range(1,(y+1)//2): if y%d==0 and x_dy%d==0: ans=d print(ans)
def euclidean_alg(x,y): while y: x,y=y,x%y return x XY=list(map(int,input().split())) x=max(XY) y=min(XY) print((euclidean_alg(x,y)))
p02256
x, y = list(map(int, input().split())) if x == y: print(x) else: for i in range(1, abs(x-y)+1): if x % i == y % i == 0: j = i print(j)
x, y = list(map(int, input().split())) if x < y: x,y = y,x while y: x,y = y, x%y print(x)
p02256
x, y = list(map(int, input().split())) if x < y: x,y = y,x while y: x,y = y, x%y print(x)
a,b=list(map(int,input().split())) while b: a,b=b,a%b print(a)
p02256
a = list(map(int, input().split())) a.sort() cd = a[0] b = a[1]%a[0] while a[0]%cd != 0 or b%cd != 0: cd -= 1 print(cd)
a = list(map(int, input().split())) a.sort() while a[0] > 0: b = a[1]%a[0] a[1] = a[0] a[0] = b print((a[1]))
p02256
a=1/3 while 1: z=int(eval(input())) if z==0:break m=0 zz=z*z*z for x in range(1,int(z/pow(2,a))+1): xx=x*x*x y=int(pow(zz-xx,a)) yy=y*y*y m=max(m,yy+xx) print((zz-m))
a=1/3 while 1: z=int(eval(input())) if z==0:break m,zz=0,z*z*z for x in range(1,int(z/pow(2,a))+1): xx=x*x*x y=int(pow(zz-xx,a)) yy=y*y*y m=max(m,yy+xx) print((zz-m))
p00691
while(1): z=int(input()) if z==0: break else: k=0 for x in range(1,z+1): for y in range(1,z+1): if x**3+y**3>z**3: break k=max(k,x**3+y**3) print(z**3-k)
import math while(1): z=int(input()) if z==0: break else: k=0 for x in range(1,z): y=int(pow(z**3-x**3,1/3.)) k=max(k,x**3+y**3) print(z**3-k)
p00691
from itertools import combinations_with_replacement as C while True: z = int(eval(input())) if z == 0: break zzz = pow(z, 3) candidates = [] combinations = list(C(list(range(1, z+1)), 2)) if len(combinations) >= 100: conbinations = combinations[100:] for xy in combi...
from itertools import combinations_with_replacement as C while True: z = int(eval(input())) if z == 0: break zzz = pow(z, 3) candidates = [] combinations = list(C(list(range(1, z+1)), 2)) if z >= 100: mid = len(combinations) // 2 combinations = combinations[mid:...
p00691
n,m= list(map(int,input().split())) par = [-1]*(n) def find(x): if par[x]<0:return x else: par[x]=find(par[x]) return par[x] def unite(x,y): px,py=find(x),find(y) if px==py:return False else: if px<py:px,py=py,px par[px]+=par[py] par[py]=px ...
n,m= list(map(int,input().split())) par = [-1]*(n) def find(x): if par[x]<0:return x else: par[x]=find(par[x]) return par[x] def unite(x,y): px,py=find(x),find(y) if px==py:return False else: if px<py:px,py=py,px par[px]+=par[py] par[py]=px ...
p03356
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): ...
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): ...
p03356
import copy n,m=list(map(int, input().split())) p=list(map(int, input().split())) q=list() for i in range(m): q.append(set(map(int, input().split()))) count=0 while count!=5000: a=q.pop() for j in q: if len(j&a) != 0: j |=a count=0 break else:q...
class UnionFind(object): def __init__(self, n=1): self.par = [i for i in range(n)] self.rank = [0 for _ in range(n)] def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] ...
p03356
#import sys #import numpy as np import math #import itertools #from fractions import Fraction #import itertools from collections import deque from collections import Counter #import heapq #from fractions import gcd #input=sys.stdin.readline #import bisect class UnionFind(): #集合の取り扱い、辺に重み、階層を意識するときは使えない ...
#import sys #import numpy as np import math #import itertools #from fractions import Fraction #import itertools from collections import deque from collections import Counter #import heapq #from fractions import gcd #input=sys.stdin.readline #import bisect class UnionFind(): #集合の取り扱い、辺に重み、階層を意識するときは使えない ...
p03356
class UnionFind: def __init__(self, n): self.p = {i: i for i in range(n)} self.r = {i: 0 for i in range(n)} def root(self, x): if self.p[x] == x: return x self.p[x] = self.root(self.p[x]) return self.p[x] def merge(self, x, y): ...
class UnionFind: def __init__(self, n): self.p = {i: i for i in range(n)} self.r = {i: 0 for i in range(n)} def root(self, x): if self.p[x] == x: return x self.p[x] = self.root(self.p[x]) return self.p[x] def merge(self, x, y): ...
p03356
def main(): def find(x): if par[x] < 0: return x else: par[x] = find(par[x]) return par[x] def unite(x, y): x = find(x) y = find(y) if x == y: return False else: if par[x] > par[y]: ...
import sys input = sys.stdin.readline def main(): def find(x): if par[x] < 0: return x else: par[x] = find(par[x]) return par[x] def unite(x, y): x = find(x) y = find(y) if x == y: return False else: ...
p03356
# ARC097D - Equals import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 9) def dfs(v: int, color: int) -> None: seen[v] = 1 # make vertex v searched group[v] = color for u in G[v]: # search vertices available from v recursively if not seen[u]: # skip if already searched...
# ARC097D - Equals import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 9) def dfs(v: int, color: int) -> None: group[v] = color # make vertex v searched for u in G[v]: # search vertices available from v recursively if not group[u]: # skip if already searched df...
p03356
# ARC097D - Equals import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 9) def dfs(v: int, color: int) -> None: group[v] = color # make vertex v searched for u in G[v]: # search vertices available from v recursively if not group[u]: # skip if already searched df...
# ARC097D - Equals (ABC097D) import sys input = sys.stdin.readline class UnionFind: def __init__(self, N): self.parent = [i for i in range(N + 1)] self.rank = [0] * (N + 1) def find(self, x): # find the group (root) of a vertex if self.parent[x] == x: return x...
p03356
n,m = list(map(int,input().split())) P = list(map(int,input().split())) uf = {i:-1 for i in range(1,n+1)} def get_p(i): while uf[i] != i: if uf[i] == -1: return i i = uf[i] return i for _ in range(m): x,y = list(map(int,input().split())) if x > y: x,y = y,x if uf[x]== uf[y] ...
n,m = list(map(int,input().split())) P = list(map(int,input().split())) uf = {i:i for i in range(1,n+1)} def get_parent(i): lst = [] while uf[i] != i: lst.append(i) i = uf[i] for l in lst[:-1]: uf[l] = i return i def unite(i,j): p = get_parent(i) uf[get_parent(j)] = p def s...
p03356
n,m = list(map(int,input().split())) P = list(map(int,input().split())) uf = {i:i for i in range(1,n+1)} def get_parent(i): lst = [] while uf[i] != i: lst.append(i) i = uf[i] for l in lst[:-1]: uf[l] = i return i def unite(i,j): p = get_parent(i) uf[get_parent(j)] = p def s...
n,m = list(map(int,input().split())) P = list(map(int,input().split())) uf = {i:i for i in range(1,n+1)} def get_parent(i): lst = [] while uf[i] != i: lst.append(i) i = uf[i] for l in lst[:-1]: uf[l] = i return i def unite(i,j): p = get_parent(i) uf[get_parent(j)] = p def s...
p03356
n,m=list(map(int,input().split())) root=[i for i in range(n+1)] rank=[0]*(n+1) def find(x): if x==root[x]: return x else: return find(root[x]) def union(x1,y1): x=find(x1) y=find(y1) if rank[x]>rank[y]: root[y]=x elif rank[x]<=rank[y]: root[x]=y ...
n,m=list(map(int,input().split())) p=list(map(int,input().split())) uf={i:i for i in range(1,n+1)} def get_p(i): lst=[] while uf[i]!=i: lst.append(i) i=uf[i] for l in lst[:-1]: uf[l]=i return i def unite(i,j): uf[get_p(i)]=get_p(j) def same(i,j): return ...
p03356
#!/usr/bin python3 # -*- coding: utf-8 -*- class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: ...
#!/usr/bin python3 # -*- coding: utf-8 -*- class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: ...
p03356
class UnionFind: def __init__(self, n): self.parent = list(range(n)) def root(self, i): while self.parent[i] != i: i = self.parent[i] return i def find(self, x, y): return self.root(x) == self.root(y) def union(self, i, j): i = self.roo...
class UnionFind: def __init__(self, n): self.parent = list(range(n)) def root(self, i): if self.parent[i] == i: return i else: self.parent[i] = self.root(self.parent[i]) return self.parent[i] def find(self, x, y): return ...
p03356
n,m=list(map(int,input().split())) p=list(map(int,input().split())) r=[i for i in range(n)] sp=[{p[i]-1} for i in range(n)] def union(x,y): rx=root(x) ry=root(y) if rx==ry: return else: r[rx]=ry sp[ry]=sp[rx]|sp[ry] def root(x): if x==r[x]: return x else: r[x]=root(r[x]) ...
n,m=list(map(int,input().split())) p=list(map(int,input().split())) r=[i for i in range(n)] sp=[{p[i]-1} for i in range(n)] def union(x,y): rx=root(x) ry=root(y) if rx==ry: return else: r[rx]=ry sp[ry]=sp[rx]|sp[ry] sp[rx]={} def root(x): if x==r[x]: return x else: r...
p03356
n,m=list(map(int,input().split())) p=list(map(int,input().split())) r=[i for i in range(n)] sp=[{p[i]-1} for i in range(n)] def union(x,y): rx=root(x) ry=root(y) if rx==ry: return else: r[rx]=ry sp[ry]=sp[rx]|sp[ry] sp[rx]={} def root(x): if x==r[x]: return x else: r...
n,m=list(map(int,input().split())) p=list(map(int,input().split())) r=[i for i in range(n)] def union(x,y): rx=root(x) ry=root(y) if rx==ry: return else: r[rx]=ry def root(x): if x==r[x]: return x else: r[x]=root(r[x]) return r[x] for i in range(m): a,b=list(map(int,in...
p03356
n, m = list(map(int, input().split())) pn = list([int(x)-1 for x in input().split()]) ls = [-1] * n for i in pn: ls[pn[i]] = i #print(ls) par = [i for i in range(n)] def find(x): if par[x] == x: return x else: s = find(par[x]) par[x] = s return s def unite...
n, m = list(map(int, input().split())) pn = list([int(x)-1 for x in input().split()]) ls = [-1] * n for i in pn: ls[pn[i]] = i #print(ls) par = [i for i in range(n)] def find(x): if par[x] == x: return x else: s = find(par[x]) par[x] = s return s def unite...
p03356
class UnionFind: def __init__(self, n, p): '木の初期化をする' # zero-index self.p = [-1] * n self.rank = [1]*n # one -index self.g = [set([i]) for i in range(n+1)] self.gp = [set([i]) for i in p] def find(self, x): 'x の親を返す' if self....
class UnionFind: def __init__(self, n): '木の初期化をする' # zero-index self.p = [-1] * n self.rank = [1]*n # one -index # self.g = [set([i]) for i in range(n+1)] # self.gp = [set([i]) for i in p] def find(self, x): 'x の親を返す' if self...
p03356
class UnionFind: def __init__(self, n, p): '木の初期化をする' # zero-index self.p = [-1] * n self.rank = [1]*n # one -index self.g = [set([i]) for i in range(n+1)] self.gp = [set([i]) for i in p] def find(self, x): 'x の親を返す' if self....
class UnionFind: def __init__(self, n, p): '木の初期化をする' # zero-index self.p = [-1] * n self.rank = [1]*n def find(self, x): 'x の親を返す' if self.p[x] == -1: return x else: self.p[x] = self.find(self.p[x]) retur...
p03356
# https://atcoder.jp/contests/arc097/tasks/arc097_b # Pについてp_xとp_yでuniteしていくと、スワップできる集合が獲得できる # 1,2,3...i...Nにできるかだが, iについてp_iとiがスワップできるかはunion-findで探せばよい import sys read = sys.stdin.readline def read_ints(): return list(map(int, read().split())) def read_tuple(H): ''' H is number of ro...
# https://atcoder.jp/contests/arc097/tasks/arc097_b # Pについてp_xとp_yでuniteしていくと、スワップできる集合が獲得できる # 1,2,3...i...Nにできるかだが, iについてp_iとiがスワップできるかはunion-findで探せばよい import sys read = sys.stdin.readline def read_ints(): return list(map(int, read().split())) def read_tuple(H): ''' H is number of ro...
p03356
n, m = list(map(int, input().split())) P = list(map(int, input().split())) P = [p-1 for p in P] def Find(x, par): if par[x] < 0: return x else: # 経路圧縮 par[x] = Find(par[x], par) return par[x] def Unite(x, y, par, rank): x = Find(x, par) y = Find(y, par) if x != y: # ran...
import sys input = sys.stdin.readline n, m = list(map(int, input().split())) P = list(map(int, input().split())) P = [p-1 for p in P] def Find(x, par): if par[x] < 0: return x else: par[x] = Find(par[x], par) return par[x] def Unite(x, y, par, rank): x = Find(x, par) ...
p03356
n,m = list(map(int, input().split())) p = list(map(int, input().split())) l = [[0 for i in range(n)] for j in range(n)] for i in range(m): x,y = list(map(int, input().split())) l[x-1][y-1] = 1 l[y-1][x-1] = 1 connect = [] v = [] for i in range(n): if i not in v: connect.append...
n,m = list(map(int, input().split())) p = list(map(int, input().split())) l = [[] for j in range(n)] for i in range(m): x,y = list(map(int, input().split())) l[x-1].append(y-1) l[y-1].append(x-1) connect = [] v = [] for i in range(n): if i not in v: connect.append([i]) ...
p03356
n,m = list(map(int, input().split())) p = list(map(int, input().split())) l = [[] for j in range(n)] for i in range(m): x,y = list(map(int, input().split())) l[x-1].append(y-1) l[y-1].append(x-1) connect = [] a = [] v = [] for i in range(n): if i not in v: connect.append([i])...
n,m = list(map(int, input().split())) p = list(map(int, input().split())) l = [[] for j in range(n)] for i in range(m): x,y = list(map(int, input().split())) l[x-1].append(y-1) l[y-1].append(x-1) connect = [] a = [] v = [0 for i in range(n)] for i in range(n): if v[i] == 0: c...
p03356
from collections import defaultdict class UnionFind(object): def __init__(self, n=1): self.par = [i for i in range(n)] self.rank = [0 for _ in range(n)] self.size = [1 for _ in range(n)] def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.p...
class UnionFind(object): def __init__(self, n=1): self.par = [i for i in range(n)] self.rank = [0 for _ in range(n)] self.size = [1 for _ in range(n)] def find(self, x): """ x が属するグループを探索 """ if self.par[x] == x: return x els...
p03356
class Unionfind(): # Unionfind def __init__(self, N): self.N = N self.parents = [-1] * N def find(self, x): # グループの根 if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] ...
class Unionfind(): # Unionfind def __init__(self, N): self.N = N self.parents = [-1] * N def find(self, x): # グループの根 if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] ...
p03356
class Unionfind(): # Unionfind def __init__(self, N): self.N = N self.parents = [-1] * N def find(self, x): # グループの根 if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] ...
class Unionfind(): # Unionfind def __init__(self, N): self.N = N self.parents = [-1] * N def find(self, x): # グループの根 if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] ...
p03356
class unionFind: def __init__(self, n, p): p = [0] + p + [0]*9 self.data = [-1] * (n+10) self.sets = [set([i]) for i in range(n+10)] self.valsets = [set([p[i]])for i in range(n+10)] def find(self, x): if self.data[x]>=0: return self.find(self.data[x]) el...
class unionFind: def __init__(self, n): self.data = [-1] * (n+10) def find(self, x): if self.data[x]>=0: self.data[x] = self.find(self.data[x]) return self.data[x] else: return x def unite(self, x, y): a, b = self.find(x), self.find(y) ...
p03356
from collections import deque from heapq import heapify,heappop,heappush,heappushpop from copy import copy,deepcopy from itertools import permutations,combinations from collections import defaultdict,Counter from pprint import pprint def myinput(): return list(map(int,input().split())) def mycol(data,co...
# from collections import deque # from heapq import heapify,heappop,heappush,heappushpop # from copy import copy,deepcopy # from itertools import permutations,combinations # from collections import defaultdict,Counter # from pprint import pprint def myinput(): return list(map(int,input().split())) # def...
p03356
import sys sys.setrecursionlimit(10**9) def unite(par, i, j): j = get_par(par, j) par[j] = get_par(par, i) def get_par(par, p): while par[p] != p: p = par[p] return par[p] n,m = list(map(int, input().split())) p = list(map(int, input().split())) par = [i for i in range(n+1)]...
import sys sys.setrecursionlimit(10**9) def unite(par, i, j): j = get_par(par, j) par[j] = get_par(par, i) def get_par(par, p): while par[p] != p: p = par[p] return par[p] n,m = list(map(int, input().split())) p = list(map(int, input().split())) par = [i for i in range(n+1)]...
p03356
def main(): import sys input = sys.stdin.buffer.readline N, M = (int(i) for i in input().split()) P = [int(i)-1 for i in input().split()] XY = [[int(i)-1 for i in input().split()] for j in range(M)] par = [i for i in range(N)] rank = [1 for i in range(N)] def find_root(x): ...
def main(): import sys input = sys.stdin.buffer.readline N, M = (int(i) for i in input().split()) P = [int(i)-1 for i in input().split()] par = [i for i in range(N)] rank = [1 for i in range(N)] def find_root(x): if par[x] == x: return x else: ...
p03356
def main(): import sys input = sys.stdin.buffer.readline N, M = (int(i) for i in input().split()) P = [int(i)-1 for i in input().split()] par = [i for i in range(N)] rank = [1 for i in range(N)] def find_root(x): if par[x] == x: return x else: ...
def main(): import sys input = sys.stdin.buffer.readline N, M = (int(i) for i in input().split()) P = [int(i)-1 for i in input().split()] XY = [[int(i)-1 for i in input().split()] for j in range(M)] par = [i for i in range(N)] rank = [1 for i in range(N)] def find_root(x): ...
p03356
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): ...
class UnionFindTree(): def __init__(self, N): self.parent = [-1]*(N+1) self.rank = [1]*(N+1) self.size = [1]*(N+1) def find(self, i): if self.parent[i] == -1: group = i else: group = self.find(self.parent[i]) self.p...
p03356
class UnionFindTree(): def __init__(self, N): self.parent = [-1]*(N+1) self.rank = [1]*(N+1) self.size = [1]*(N+1) def find(self, i): if self.parent[i] == -1: group = i else: group = self.find(self.parent[i]) self.p...
class UnionFind(): # 作りたい要素数nで初期化 # 使用するインスタンス変数の初期化 def __init__(self, n): self.n = n # root[x]<0ならそのノードが根かつその値が木の要素数 # rootノードでその木の要素数を記録する self.root = [-1]*(n+1) # 木をくっつける時にアンバランスにならないように調整する self.rnk = [0]*(n+1) # ノードxのrootノードを見つける def...
p03356
import math import random import heapq from collections import Counter, defaultdict from decimal import Decimal, ROUND_HALF_UP, ROUND_CEILING from functools import lru_cache, reduce from itertools import combinations_with_replacement, product, combinations def read_int(): return int(input()) def re...
import math import random import heapq from collections import Counter, defaultdict from decimal import Decimal, ROUND_HALF_UP, ROUND_CEILING from functools import lru_cache, reduce from itertools import combinations_with_replacement, product, combinations def read_int(): return int(input()) def re...
p03356
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): ...
class UnionFind(): def __init__(self, x): self.x=x self.root=[-1]*(N+1) self.rnk=[0]*(N+1) def Find_root(self, x): if self.root[x]<0: return x else: self.root[x]=self.Find_root(self.root[x]) return self.root[x] def Unite(self, x, y): x = self.Find_ro...
p03356
#!/usr/bin/env python3 import sys class UnionFind: def __init__(self, n): # par = Parent Number or NoV self.par = [-1] * (n+1) # rank = Tree Height self.rank = [0] * (n+1) # 自分が所属する集合の数を返す def size(self, x): return -1*self.par[self.find(x)] # ...
#!/usr/bin/env python3 import sys class UnionFind: def __init__(self, n): # par = Parent Number or NoV self.par = [-1] * (n+1) # rank = Tree Height self.rank = [0] * (n+1) # 自分が所属する集合の数を返す def size(self, x): return -1*self.par[self.find(x)] # ...
p03356
def root(v): if v == par[v]: return v par[v] = root(par[v]) return par[v] def unite(u, v): u = root(u) v = root(v) if u == v: return if rank[u] < rank[v]: par[u] = v else: par[v] = u if rank[u] == rank[v]: rank[v] += 1 ...
import sys sys.setrecursionlimit(10 ** 6) input = sys.stdin.readline def root(v): if v == par[v]: return v par[v] = root(par[v]) return par[v] def unite(u, v): u = root(u) v = root(v) if u == v: return if rank[u] < rank[v]: u, v = v, u par[v] = ...
p03356
N, M = list(map(int, input().split())) p = list(map(int, input().split())) set_list = [] edge = [[] for _ in range(N)] for i in range(M): x, y = list(map(int, input().split())) x, y = x-1, y-1 s1 = None s2 = None for i, se in enumerate(set_list): if x in se: s1 = s...
def root(R, a): if R[a] != a: R[a] = root(R, R[a]) return R[a] def union(R, x, y): a = root(R, x) b = root(R, y) if a != b: R[a] = b N, M = list(map(int, input().split())) p = list(map(int, input().split())) R = [i for i in range(N)] for i in range(M): x, y =...
p03356
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): ...
class UnionFind(): def __init__(self, n): self.parents = list(range(n)) self.rank = [0] * n def find(self, x): if self.parents[x] == x: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def uni...
p03356
import queue N,M=list(map(int,input().split())) p=[int(i)-1 for i in input().split()] x=[0 for i in range(M)] y=[0 for i in range(M)] edge=[set() for i in range(N)] for i in range(M): x[i],y[i]=list(map(int,input().split())) x[i]-=1;y[i]-=1 edge[x[i]].add(y[i]) edge[y[i]].add(x[i]) k=0 L=[0 ...
N,M=list(map(int,input().split())) par=[0 for i in range(N)] rnk=[0 for i in range(N)] def init(n): for i in range(N): par[i]=i rnk[i]=0 def find(x): if par[x]==x: return x else: par[x]=find(par[x]) return par[x] def unite(x,y): x=find(x) y=find...
p03356
N, M = list(map(int, input().split())) P = [int(p) for p in input().split()] Par = [int(i) for i in range(N+1)] Rank = [0 for i in range(N+1)] def find(i, Par): if Par[i] == i: return i else: Par[i] = find(Par[i], Par) return Par[i] def Unite(x, y): rx, ry = find(x, Pa...
import sys class UFT: #Union-find tree class def __init__(self, N): self.tree = [int(i) for i in range(N)] self.rank = [0 for i in range(N)] def find(self, a): if self.tree[a] == a: return a else: self.tree[a] = self.find(self.tree[a]) retur...
p03356
def i1(): return int(eval(input())) def i2(): return [int(i) for i in input().split()] [n,m]=i2() p=i2() import sys sys.setrecursionlimit(10000) par=[i for i in range(n)] rank=[0 for i in range(n)] def rt(x): if par[x]==x: return x else: par[x]=rt(par[x]) return par[x] def un(x,y...
def i1(): return int(eval(input())) def i2(): return [int(i) for i in input().split()] [n,m]=i2() p=i2() import sys sys.setrecursionlimit(10000) par=[i for i in range(n)] rank=[0 for i in range(n)] def rt(x): if par[x]==x: return x else: par[x]=rt(par[x]) return par[x] def un(x,y...
p03356
import sys sys.setrecursionlimit(100010) def main(): def input(): return sys.stdin.readline()[:-1] N, M = list(map(int,input().split())) p = list([int(x)-1 for x in input().split()]) parent = [k for k in range(N)] def find(x): if parent[x] == x: return x...
import sys sys.setrecursionlimit(100010) def main(): def input(): return sys.stdin.readline()[:-1] N, M = list(map(int,input().split())) p = list([int(x)-1 for x in input().split()]) parent = [k for k in range(N)] def find(x): if parent[x] == x: return x...
p03356
# -*- coding: utf-8 -*- class UnionFind(): def __init__(self, n): self.par = [i for i in range(n)] self.rank = [0 for _ in range(n)] def find(self, x): if self.par[x]==x: return x else: self.par[x] = self.find(self.par[x]) return ...
# -*- coding: utf-8 -*- class UnionFind(): def __init__(self, n): self.n = n self.par = [-1 for _ in range(n)] def same(self, x, y): return self.root(x)==self.root(y) def root(self, x): if self.par[x]<0: return x self.par[x] = self.root(sel...
p03356
N, M = list(map(int, input().split())) P = list(map(int, input().split())) class UnionFind: def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.p...
N, M = list(map(int, input().split())) P = list(map(int, input().split())) class UnionFind: def __init__(self, n): self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) ...
p03356
#!/usr/bin/env python class UnionFind: def __init__(self, size): self.table = list(range(size)) def find(self, x): return self.table[x] def union(self, x, y): x1 = self.find(x) y1 = self.find(y) if x1 == y1: return False for i in...
#!/usr/bin/env python class UnionFind(): def __init__(self, n): self.par = [i for i in range(n)] self.rank = [0 for _ in range(n)] def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) retur...
p03356
from collections import deque def bfs(si, ei): q = deque() q.append(si) used = set([]) while len(q) > 0: prvi = q.pop() for i in g[prvi]: if i in used: continue if i == ei: return True q.append(i) used.add(i) return False N,M = list(map(int,input().split())) P...
from collections import deque class UnionFind: def __init__(self, n): self.par = [i for i in range(n+1)] self.rank = [0] * (n+1) def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] def same_check(self, ...
p03356
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y...
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y...
p03356
def find_root(x): if parent[x] == x: return x else: return find_root(parent[x]) # x,yの属する集合の併合 def unite(x, y): x = find_root(x) y = find_root(y) if x != y: if rank[x] < rank[y]: parent[x] = y size[y] += size[x] else: ...
def find_root(x): if parent[x] == x: return x else: return find_root(parent[x]) # x,yの属する集合の併合 def unite(x, y): x = find_root(x) y = find_root(y) if x != y: if rank[x] < rank[y]: parent[x] = y size[y] += size[x] else: ...
p03356
class UF(): def __init__(self, N): self.parents = [ i for i in range(N+1) ] def union(self, f, t): p1 = self.find(f) p2 = self.find(t) if p1 == p2: return self.parents[p2] = p1 def find(self, node): # if self.parents[node] == node: # return node # else: ...
class UF(): def __init__(self, N): self.parents = [-1] * (N+1) def union(self, f, t): p1 = self.find(f) p2 = self.find(t) if p1 == p2: return if self.parents[p1] > self.parents[p2]: p1,p2 = p2,p1 self.parents[p1] += self.parents[p2] self.parents[p2] = p1 d...
p03356
import sys from collections import deque readline = sys.stdin.readline n, m = list(map(int, readline().split())) P = list([int(x)-1 for x in readline().split()]) G = [set() for _ in range(n)] for i in range(m): x, y = [int(x)-1 for x in readline().split()] G[x].add(y) G[y].add(x) D = {} cnt = 0...
import sys readline = sys.stdin.readline class UnionFind(object): def __init__(self, n): self._par = list(range(n)) self.size = [1]*n def root(self, v): if self._par[v] == v: return v self._par[v] = self.root(self._par[v]) return self._par[v] ...
p03356
N,M = list(map(int,input().split())) P = list(map(int,input().split())) XY = [tuple(map(int,input().split())) for i in range(M)] class UnionFind: def __init__(self,N): self.parent = [i for i in range(N)] self._size = [1] * N self.count = 0 def root(self,a): if self.par...
N,M = list(map(int,input().split())) P = list(map(int,input().split())) XY = [tuple(map(int,input().split())) for i in range(M)] class UnionFind: def __init__(self,N): self.parent = [i for i in range(N)] self._size = [1] * N self.count = 0 def root(self,a): if self.par...
p03356
class Union_Find(): def __init__(self,N): """0,1,...,n-1を要素として初期化する. n:要素数 """ self.n=N self.parents=[-1]*N self.rank=[0]*N def find(self, x): """要素xの属している族を調べる. x:要素 """ if self.parents[x] < 0: return...
class Union_Find(): def __init__(self,N): """0,1,...,n-1を要素として初期化する. n:要素数 """ self.n=N self.parents=[-1]*N self.rank=[0]*N def find(self, x): """要素xの属している族を調べる. x:要素 """ if self.parents[x] < 0: return...
p03356
N, M=list(map(int, input().split())) P=list(map(int, input().split())) XY=[-1 for i in range(N)] class UnionFind(): # 作りたい要素数nで初期化 # 使用するインスタンス変数の初期化 def __init__(self, n): self.n = n # root[x]<0ならそのノードが根かつその値が木の要素数 # rootノードでその木の要素数を記録する self.root = [-1]*(n+1) # 木をくっつける時にアンバラ...
class UnionFind(): # 作りたい要素数nで初期化 # 使用するインスタンス変数の初期化 def __init__(self, n): self.n = n # root[x]<0ならそのノードが根かつその値が木の要素数 # rootノードでその木の要素数を記録する self.root = [-1]*(n+1) # 木をくっつける時にアンバランスにならないように調整する self.rnk = [0]*(n+1) # ノードxのrootノードを見つける def Find_Root(self, x): if(sel...
p03356
n,m = list(map(int,input().split())) p = list(map(int, input().split())) g = [[] for _ in range(0,n+1)] visit = [None] * (n+1) ans = 0 for _ in range(0,m): x,y = list(map(int, input().split())) g[x].append(y) g[y].append(x) for i in range(1,n+1): if visit[i] is not None: continue ...
n,m = list(map(int,input().split())) p = list(map(int, input().split())) g = [[] for _ in range(0,n+1)] visit = [None] * (n+1) ans = 0 for _ in range(0,m): x,y = list(map(int, input().split())) g[x].append(y) g[y].append(x) for i in range(1,n+1): if visit[i] is not None: continue ...
p03356
N, M = list(map(int, input().split())) P = list(map(int, input().split())) parent = list(range(N)) rank = [0] * N key = [[i] for i in range(N)] val = [[p-1] for p in P] # union-find def getRoot(x): p = parent[x] if p == x: return p ret = getRoot(p) parent[x] = ret return ret def join(x...
N, M = list(map(int, input().split())) P = list(map(int, input().split())) parent = list(range(N)) rank = [0] * N idx = [None] * N for i in range(N): idx[P[i]-1] = i # union-find def getRoot(x): p = parent[x] if p == x: return p ret = getRoot(p) parent[x] = ret return ret def join...
p03356
class UnionFind(): def __init__(self, n): self.n = n self.root = [-1]*(n+1) self.rnk = [0]*(n+1) def find_root(self, x): if(self.root[x] < 0): return x else: self.root[x] = self.find_root(self.root[x]) return self.root[x] ...
class UnionFind(): def __init__(self, n): self.n = n self.root = [-1]*(n+1) self.rnk = [0]*(n+1) def find_root(self, x): if(self.root[x] < 0): return x else: self.root[x] = self.find_root(self.root[x]) return self.root[x] ...
p03356
class UnionFind(): def __init__(self, n): self.n = n self.root = [-1]*(n+1) self.rnk = [0]*(n+1) def Find_Root(self, x): if(self.root[x] < 0): return x else: self.root[x] = self.Find_Root(self.root[x]) return self.root[x] ...
class UnionFind(): def __init__(self, n): self.n = n self.root = [-1]*(n+1) self.rnk = [0]*(n+1) def Find_Root(self, x): if(self.root[x] < 0): return x else: self.root[x] = self.Find_Root(self.root[x]) return self.root[x] ...
p03356
# coding: utf-8 # Your code here! class UnionFind(): #負の値はルートで集合の個数 #正の値は次の要素を返す def __init__(self,size): self.table = [-1 for _ in range(size)] #集合の代表を求める def find(self,x): while self.table[x] >= 0: #根に来た時,self.table[根のindex]は負の値なのでx = 根のindexで値が返される。 ...
class UnionFind: def __init__(self, size): self.parent = [-1 for i in range(size)]#非負なら親ノード,負ならグループの要素数 def root(self, x): if self.parent[x] < 0: return x else: self.parent[x] = self.root(self.parent[x]) #xをxの根に直接つなぐ return self.parent[x] ...
p03356
class UnionFind: def __init__(self, size): self.parent = [-1 for i in range(size)]#非負なら親ノード,負ならグループの要素数 def root(self, x): if self.parent[x] < 0: return x else: self.parent[x] = self.root(self.parent[x]) #xをxの根に直接つなぐ return self.parent[x] ...
class UnionFind: def __init__(self, size): self.parent = [-1 for i in range(size)]#非負なら親ノード,負ならグループの要素数 def root(self, x): #root(i): $i$の根ノードを返す. if self.parent[x] < 0: return x else: self.parent[x] = self.root(self.parent[x]) #xをxの根に直接つなぐ re...
p03356
from collections import deque N,M = list(map(int,input().split())) P = list(map(int,input().split())) L = [list(map(int,input().split())) for i in range(M)] G = [[] for i in range(N)] for i in range(len(L)) : u,v = L[i] u -= 1 v -= 1 G[u].append(v) G[v].append(u) explored = ...
class UnionFind: def __init__(self, num): self.rank = [0] * num self.par = [i for i in range(num)] self.n = num def find_root(self, node): if self.par[node] == node: return node else: self.par[node] = self.find_root(self.par[node]) ...
p03356
import sys import math import collections import bisect import copy import itertools # import numpy as np sys.setrecursionlimit(10 ** 7) INF = 10 ** 16 MOD = 10 ** 9 + 7 # MOD = 998244353 ni = lambda: int(sys.stdin.readline()) ns = lambda: list(map(int, sys.stdin.readline().split())) na = lambda: lis...
import sys import math import collections import bisect import copy import itertools # import numpy as np sys.setrecursionlimit(10 ** 7) INF = 10 ** 16 MOD = 10 ** 9 + 7 # MOD = 998244353 ni = lambda: int(sys.stdin.readline().rstrip()) ns = lambda: list(map(int, sys.stdin.readline().rstrip().split()))...
p03356
import sys sys.setrecursionlimit(2000000) input = sys.stdin.readline class UnionFind(): def __init__(self,n): self.n=n self.parents = [i for i in range(n+1)] self.size = [1]*(n+1) def find(self,x): if self.parents[x]==x: return x else: ...
import sys sys.setrecursionlimit(2000000) input = sys.stdin.readline class UnionFind(): def __init__(self,n): self.n=n self.parents = [i for i in range(n+1)] self.size = [1]*(n+1) def find(self,x): if self.parents[x]==x: return x else: ...
p03356
#各pは、自分の今いるインデックスがindex p-1と繋がっていればそこに移動する import sys sys.setrecursionlimit(200000) class UnionFind(): def __init__(self,n): self.n=n self.parents = [i for i in range(n+1)] self.size = [1]*(n+1) def find(self,x): if self.parents[x]==x: return x el...
""" 初期状態でpiにいる数が、pi番目にたどり着けるかを判定すればよい。 unionFind木が使える """ N,M = list(map(int,input().split())) P = list(map(int,input().split())) class UnionFind(): def __init__(self,n): self.n=n self.parents = [i for i in range(n+1)] self.size = [1]*(n+1) def find(self,x): if self.p...
p03356
N,M=list(map(int, input().split())) p=[set()]+[set([int(x)]) for x in input().split()] xy=[list(map(int, input().split())) for _ in range(M)] for _ in range(10): for x,y in xy: p[x]=p[y]=p[x]|p[y] print((sum([1 if i in s else 0 for (i, s) in enumerate(p)])))
N,M=list(map(int, input().split())) p=list(map(int, input().split())) uf=[i for i in range(N+1)] def find(n): if uf[n] == n: return n uf[n] = find(uf[n]) return uf[n] def union(n, m): uf[find(n)] = find(m) def same(n, m): return find(n) == find(m) for _ in range(M): union(*list(map(int, i...
p03356
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): ...
class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): ...
p03356
import sys sys.setrecursionlimit(6500) def find(n): if d[n]<0: return n else: d[n]=find(d[n]) return d[n] def union(a,b): a=find(a) b=find(b) if a==b:return False if d[a]<=d[b]: d[a]+=d[b] d[b]=a else: d[b]+=d[a] ...
import sys sys.setrecursionlimit(6500) def find(n): if d[n]<0: return n else: d[n]=find(d[n]) return d[n] def union(a,b): a=find(a) b=find(b) if a==b:return False if d[a]<=d[b]: d[a]+=d[b] d[b]=a else: d[b]+=d[a] ...
p03356
import sys input = sys.stdin.readline from heapq import heappush,heappop class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.pa...
import sys input = sys.stdin.readline from heapq import heappush,heappop class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.pa...
p03356
class UF_tree: def __init__(self, n): self.root = [-1] * (n + 1) # -1ならそのノードが根,で絶対値が木の要素数 self.rank = [0] * (n + 1) def find(self, x): # xの根となる要素番号を返す if self.root[x] < 0: return x else: self.root[x] = self.find(self.root[x]) retur...
import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) class UF_tree: def __init__(self, n): self.root = [-1] * (n + 1) # -1ならそのノードが根,で絶対値が木の要素数 self.rank = [0] * (n + 1) def find(self, x): # xの根となる要素番号を返す if self.root[x] < 0: return x ...
p03356
from operator import itemgetter n=int(eval(input())) l=[list(map(int,input().split())) for i in range(n)] for i in range(n): l[i].sort() ans=[] l.sort() B1=[];R1=[] for i in range(n): B1.append(max(l[i])) R1.append(min(l[i])) ans.append((max(B1)-min(B1))*(max(R1)-min(R1))) Bleft=[] Bright=[] Rle...
import sys input=sys.stdin.readline n=int(eval(input())) l=[list(map(int,input().split())) for i in range(n)] for i in range(n): l[i].sort() ans=[] l.sort() B1=[];R1=[] for i in range(n): B1.append(max(l[i])) R1.append(min(l[i])) ans.append((max(B1)-min(B1))*(max(R1)-min(R1))) Bleft=[] Bright=[]...
p03735
N = int(eval(input())) xmin = ymin = float('inf') xmax = ymax = 0 p = [] for i in range(N): x,y = list(map(int, input().split())) if x > y : x,y= y,x p.append((x, y)) if x < xmin: xmin = x elif x > xmax: xmax = x if y < ymin: ymin = y elif y > ...
N = int(eval(input())) xmin = ymin = float('inf') xmax = ymax = 0 p = [] for i in range(N): x,y = list(map(int, input().split())) if x > y : x,y= y,x p.append((x, y)) if x < xmin: xmin = x elif x > xmax: xmax = x if y < ymin: ymin = y elif y > ...
p03735
#!/usr/bin/env python3 import sys readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines read = sys.stdin.buffer.read sys.setrecursionlimit(10 ** 7) INF = float('inf') N = int(eval(input())) A = list(map(int, input().split())) # どこで反転するかを記録する pre = [0] * N pre_index = 0 old_a = A[0]...
#!/usr/bin/env python3 import sys readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines read = sys.stdin.buffer.read sys.setrecursionlimit(10 ** 7) INF = float('inf') N = int(eval(input())) A = list(map(int, input().split())) shojikin = 1000 stock = 0 for i in range(N): if stoc...
p02603
N = int(eval(input())) A = list(map(int,input().split())) # print('A:',A) from itertools import product a=[0,1] #0:buy 1:sell Ls = list(product(a, repeat=N-1)) # print('Ls:',Ls) out=0 for L in Ls: # print('L:',L) money=1000 kabu=0 for i in range(N-1): if L[i]==0: ...
N = int(eval(input())) A = list(map(int,input().split())) # print('A:',A) money=1000 kabu=0 for i in range(N-1): if A[i] < A[i+1]: kabu+=money // A[i] money=money % A[i] elif A[i] > A[i+1]: money+=kabu*A[i] kabu=0 money+=kabu*A[N-1] #last print(money) ...
p02603
import sys N = int(sys.stdin.readline().strip()) A = list(map(int, sys.stdin.readline().split())) money = 1000 min_price = float("inf") for i in range(N): if min_price > A[i]: min_price = A[i] elif min_price < A[i]: num = money // min_price money = money - min_price * num +...
import sys N = int(sys.stdin.readline()) A = list(map(int, sys.stdin.readline().split())) yen = 1000 for i in range(1, N): if A[i-1] < A[i]: yen += (A[i] - A[i-1]) * (yen // A[i-1]) print(yen)
p02603
import sys def input(): return sys.stdin.readline().strip() def mapint(): return list(map(int, input().split())) sys.setrecursionlimit(10**9) N = int(eval(input())) As = list(mapint()) memo = {} s = set() ans = 0 def dfs(idx, money, stocks): global ans if idx==N: ans = max(ans, money) ...
import sys def input(): return sys.stdin.readline().strip() def mapint(): return list(map(int, input().split())) sys.setrecursionlimit(10**9) N = int(eval(input())) As = list(mapint()) memo = {} s = set() ans = 0 def dfs(idx, money, stocks): global ans if idx==N: ans = max(ans, money) ...
p02603
N = int(eval(input())) X = list(map(int, input().split())) dp = [0] * N dp[0] = 1000 for i in range(1, N): dp[i] = dp[i - 1] for j in range(i): n = dp[j] // X[j] dp[i] = max(dp[i], dp[j] + (X[i] - X[j]) * n) print((dp[-1]))
N = int(eval(input())) X = list(map(int, input().split())) cur = 1000 stock = 0 state = 0 # 1=increase, 0=decrease for i in range(N - 1): if state == 1 and X[i] > X[i + 1]: cur += X[i] * stock stock = 0 state = 0 elif state == 0 and X[i] < X[i + 1]: n = cur // X[i...
p02603
#!/usr/bin/env python3 # encoding:utf-8 import copy import random import bisect #bisect_left これで二部探索の大小検索が行える import fractions #最小公倍数などはこっち import math import sys import collections from decimal import Decimal # 10進数で考慮できる mod = 10**9+7 sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000 d = collections.de...
#!/usr/bin/env python3 # encoding:utf-8 import copy import random import bisect #bisect_left これで二部探索の大小検索が行える import fractions #最小公倍数などはこっち import math import sys import collections from decimal import Decimal # 10進数で考慮できる mod = 10**9+7 sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000 d = collections.de...
p02603
N = int(eval(input())) A = list(map(int, input().split())) s = (1000, 0, 0) mmax = 1000 q = [] q.append(s) while len(q) > 0: m,k,d = q.pop() if d == N-1: m += k*A[d] if mmax < m: mmax = m elif d == N-2: if A[d] > A[d+1]: if k > 0: ...
N = int(eval(input())) A = list(map(int, input().split())) m = 1000 k = 0 for i in range(N-1): if A[i] > A[i+1]: m += k * A[i] k = 0 elif A[i] < A[i+1]: m += k * A[i] k = m // A[i] m -= k * A[i] else: pass m += (k * A[-1]) print(m)
p02603
n=int(eval(input())) a=[float('Inf')] a+=list(map(int,input().split())) i=0 m=1000 while i<n: if a[i]>a[i+1]: s=a[i+1] if a[i]<a[i+1]: m=m//s*a[i+1]+m%s s=a[i+1] i+=1 print(m)
n=int(eval(input())) a=[float('inf')] a+=list(map(int,input().split())) d=[1000 for i in range(n+1)] for i in range(1,n+1): d[i]=max(d[i-1],d[i-1]//a[i-1]*a[i]+d[i-1]%a[i-1]) print((d[-1]))
p02603
N = int(eval(input())) A = list(map(int, input().split())) memo = {} def solve(i, j, k): if i == N - 1: return j + A[i] * k if (i, j, k) in memo: return memo[(i, j, k)] res = max(solve(i + 1, j, k), solve(i + 1, j % A[i], k + j // A[i]), solve(i + ...
N = int(eval(input())) A = list(map(int, input().split())) count = 0 money = 1000 kabu = 0 temp = A[0] while count < N: if A[count] > temp: kabu = money // temp money = money % temp money += kabu * A[count] kabu = 0 temp = A[count] count += 1 print(money)
p02603
import math import collections import fractions import itertools import functools import operator import bisect def divis(l): seq = [] beg, end = 0, 0 le = len(l) for i in range(1,le): end = i if l[i] < l[i-1]: seq.append(l[beg:end]) beg = i ...
import math import collections import fractions import itertools import functools import operator def divis(l): seq = [] beg, end = 0, 0 le = len(l) for i in range(1,le): end = i if l[i] < l[i-1]: seq.append(l[beg:end]) beg = i seq.append(l[be...
p02603