input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
import functools import math N, X = [int(i) for i in input().split()] x_list = [int(i) for i in input().split()] x_list.append(X) num_list = sorted(x_list) tmp = [abs(x[0] - x[1]) for x in zip(num_list[:-1],num_list[1:])] D = functools.reduce(lambda acc, x: math.gcd(acc, x), tmp, 0) print(D)
import functools import math N, X = [int(i) for i in input().split()] x_list = [abs(int(i) - X) for i in input().split()] D = functools.reduce(lambda acc, x: math.gcd(acc, x), x_list, 0) print(D)
p03262
N, X = list(map(int, input().split())) x = list(map(int, input().split())) y = [abs(xi - X) for xi in x] k = min(y) for i in range(k): p = k -i z = [yi % p for yi in y] if sum(z) == 0: print(p) break
def gcd(x, y): while y != 0: x, y = y, x % y return x N, X = list(map(int, input().split())) x = list(map(int, input().split())) y = [abs(xi - X) for xi in x] r = y[0] for i in range(N-1): if y[i+1] != 0: r = gcd(r, y[i+1]) print(r)
p03262
N, X = list(map(int, input().split())) A = list(map(int, input().split())) def gcd(x, y): return x if y == 0 else gcd(y, x % y) D = [] for a in A: D.append(abs(X - a)) answer = 0 for d in D: answer = gcd(answer, d) print(answer)
N, X = list(map(int, input().split())) A = list([abs(X - int(x)) for x in input().split()]) def gcd(x, y): return x if y == 0 else gcd(y, x % y) answer = 0 for a in A: answer = gcd(answer, a) print(answer)
p03262
def gcd(x, y): if y == 0: return x else: return gcd(y, x % y) n, X = list(map(int, input().split())) x = list(map(int, input().split())) if len(x) == 1: print((abs(x[0] - X))) else: x.sort() s = x[1] - x[0] for i in range(1, n-1): s = gcd(s, x[i+1]-x[i]) a = 1 f...
def gcd(x, y): if y == 0: return x else: return gcd(y, x % y) n, X = list(map(int, input().split())) x = list(map(int, input().split())) a = abs(x[0] - X) for i in x[1:]: a = gcd(a, abs(i - X)) print(a)
p03262
a=list(map(int,input().split())) b=[] for i in input().split(): b.append(abs(int(i)-a[1])) def ucl(a,b): if a%b==0: return b else: return ucl(b,a%b) while len(b)>1: b.append(ucl(b[0],b[1])) del b[0],b[1] print((b[0]))
a=list(map(int,input().split())) x=[] for i in input().split(): x.append(abs(int(i)-a[1])) def ucl(a,b): if a%b==0: return b else: return ucl(b,a%b) while len(x) > 1: x2 = [] mn_x = min(x) for n in range(len(x)): mod = x[n] % mn_x if mod != 0: x2.append(mod) x2.append...
p03262
N, X = list(map(int, input().split())) axis = list(map(int, input().split())) ans = abs(axis[0] - X) def gcd(n, m) : n, m = max(n, m), min(n, m) if m == 0 : return n return gcd(m, n % m) for a in axis : ans = gcd(ans, abs(a - X)) print(ans)
N, X = list(map(int, input().split())) A = [X] + list(map(int, input().split())) def gcd(n, m): if m == 0: return n return gcd(m, n % m) B = [abs(a - A[0]) for a in A] ans = 0 for d in B: ans = gcd(ans, d) print(ans)
p03262
import collections N, X = list(map(int, input().split())) x = [int(i) for i in input().split()] y = [abs(X-i) for i in x] y.append(0) y.sort() z = [(y[i+1] - y[i]) for i in range(0, N)] zz = [] for i in range(0, N): if z[i] != 0 and not(z[i] in zz): zz.append(z[i]) a = list(collections.Counte...
import collections N, X = list(map(int, input().split())) x = [int(i) for i in input().split()] y = [abs(X-i) for i in x] y.append(0) y.sort() z = [(y[i+1] - y[i]) for i in range(0, N) if (y[i+1] - y[i]) != 0] z = list(set(z)) a = list(collections.Counter(z).keys()) D = min(a) f = True for i in range(D, ...
p03262
import collections N, X = list(map(int, input().split())) x = [int(i) for i in input().split()] y = [abs(X-i) for i in x] y.append(0) y.sort() z = [(y[i+1] - y[i]) for i in range(0, N) if (y[i+1] - y[i]) != 0] z = list(set(z)) a = list(collections.Counter(z).keys()) D = min(a) f = True for i in range(D, ...
import collections N, X = list(map(int, input().split())) x = [int(i) for i in input().split()] y = [abs(X-i) for i in x] y.append(0) y.sort() D = min(collections.Counter(list(set([(y[i+1] - y[i]) for i in range(0, N) if (y[i+1] - y[i]) != 0]))).keys()) f = True for i in range(D, 0, -1): f = True ...
p03262
def gcd(a, b): if(b % a == 0): return a elif(a % b == 0): return b else: ans = 1 i = 2 while a!= 1: if(a % i == 0): while a % i == 0: a/= i while b % i == 0: b/= i ans*= i i+= 1 return ans def ggcd(l...
def gcd(a, b): if(a == b): return a elif(a % b == 0): return b elif(b % a == 0): return a elif(a == 1 or b == 1): return 1 elif(a > b): return gcd(a- a// b* b, b) elif(a < b): return gcd(a, b- b// a* a) def ggcd(lista): if(len(lista) == 1): return lista.pop() ...
p03262
N,X=list(map(int,input().split())) inputcities=list(map(int, input().split())) cities=list(set([abs(inputcities[i]-X) for i in range(N)])) N=len(cities) count=0 flag=0 minimum=min(cities) for j in range(minimum+1)[::-1]: for i in range(N): if cities[i]%j==0: count+=1 else: count=0 ...
def gcd(a,b): if a < b: a,b = b,a if a % b == 0: return b else: return gcd(b,(a % b)) N,X=list(map(int,input().split())) inputcities=list(map(int, input().split())) cities=[abs(inputcities[i]-X) for i in range(N)] cities.sort() D=cities[0] for i in range(N): D=gcd(D...
p03262
N,X = list(map(int, input().split())) l = sorted(map(int, input().split()+[X])) r = [l[n+1] - l[n] for n in range(N)] ans = r.pop() for i in r: while ans%i != 0: ans,i = i,ans%i ans = i print(ans)
N,X = list(map(int, input().split())) r = [abs(X-i) for i in map(int, input().split())] ans = r.pop() for i in r: while ans%i != 0: ans,i = i,ans%i ans = i print(ans)
p03262
N, X = list(map(int, input().split())) L = list(map(int, input().split())) L.append(X) L.sort() cand = [] def gcd(x, y): if x > y: small = y else: small = x for i in range(1, small + 1): if ((x % i == 0) and (y % i == 0)): gcd = i return gcd for i in r...
N, X = list(map(int, input().split())) L = list(map(int, input().split())) L.append(X) L.sort() cand = [] def gcd(x, y): while y != 0: (x, y) = (y, x % y) return x for i in range(N): cand.append(abs(L[i] - L[i+1])) for i, can in enumerate(cand): if i == 0: tmp = can ...
p03262
N,X = list(map(int,input().split())) x = [int(i) for i in input().split()] x = [i-X for i in x] Min = min([abs(i) for i in x]) flag = False for i in range(1,Min+1): if flag == True: break flag = True if Min%i==0: ans = Min//i for j in x: if j%ans!=0: ...
N,X = list(map(int,input().split())) x = [int(i) for i in input().split()] def main(): global X global x print((answer(pre(X,x)))) def pre(a,b): b = [i-a for i in b] return b def answer(k): Min = min([abs(i) for i in k]) flag = False for i in range(1,Min+1): ...
p03262
def g_c_d(x, y): if x == 0: return y elif y == 0: return x else: return g_c_d(y, x%y) a, b = list(map(int, input().split())) c = list(map(int, input().split())) c.append(b) c.sort() d = 0 for i in range(1, a + 1): d = g_c_d(c[i] - c[i-1], d) print(d)
def g_c_d(x, y): if x == 0: return y elif y == 0: return x else: return g_c_d(y, x % y) a, b = list(map(int, input().split())) c = list(map(int, input().split())) c.append(b) d = 0 for i in range(len(c)): d = g_c_d(d, abs((c[i] - c[i-1]))) print(d)
p03262
N, X = list(map(int, input().split())) lst = list(map(int, input().split())) ans = [] for i in lst: ans.append(abs(i-X)) small = min(ans) for i in ans: if i%small == 0: continue else: while i != small: i = i%small small = small - i print(small)
N, X = list(map(int, input().split())) lst = list(map(int, input().split())) ans = [] for i in lst: ans.append(abs(i-X)) small = min(ans) for i in ans: if i%small != 0: while i%small != 0: i = i%small small = small - i print(small)
p03262
def nl(): return list(map(int, input().split())) def gcd(x, y): if x < y: # x >= y とする x, y = y, x while True: r = x % y if r == 0: break else: x, y = y, r return y N, X = nl() x = nl() x2 = list(map(abs, ([v - X for v in x]))) ...
def gcd(x, y): if x < y:# x >= y とする x, y = y, x r = x % y res = y if r == 0 else gcd(y, r) return res N, X = list(map(int, input().split())) x = list(map(int, input().split())) x2 = [abs(i - X) for i in x] g = x2[0] for i in x2: g = gcd(g, i) print(g)
p03262
def gcd(a, b): if b == 0: return a return gcd(b, a % b) N, X = list(map(int, input().split())) x = list(map(int, input().split())) x = sorted(x) x = list([abs(arg - X) for arg in x]) g = x[0] for xx in x: g = gcd(g, xx) print(g)
def gcd(a, b): if b == 0: return a return gcd(b, a % b) N, X = list(map(int, input().split())) x = list(map(int, input().split())) x = list([abs(arg - X) for arg in x]) g = x[0] for xx in x[1:]: g = gcd(g, xx) print(g)
p03262
#coding: utf-8 from functools import reduce def gcd(a, b): if b == 0: return a else: return gcd(b, a%b) N, X = list(map(int, input().split())) x = list(map(int, input().split())) x = [abs(X - x[i]) for i in range(N)] l = [x[0]] for i in range(len(x)): for j in range(i+1, len(x)...
#coding: utf-8 from functools import reduce def gcd(a, b): if b == 0: return a else: return gcd(b, a%b) N, X = list(map(int, input().split())) x = list(map(int, input().split())) x = [abs(X - x[i]) for i in range(N)] it = iter(x) value = x[0] for i in it: value = gcd(value, i) ...
p03262
from math import gcd n, x = list(map(int, input().split())) m = [x]+list(map(int, input().split())) m.sort() if n==1: print((m[1]-m[0])) else: res = gcd(m[1]-m[0], m[2]-m[1]) for i in range(1, n-1): a = m[i+1]-m[i] b = m[i+2]-m[i+1] if res >= gcd(a,b): if res%gcd(a,b)==0: ...
import math n,a=list(map(int,input().split())) x=list(map(int,input().split()))+[a] x.sort() if n==1: print((abs(x[0]-x[1]))) exit() res=x[1]-x[0] for i in range(1,n): res=math.gcd(res,x[i+1]-x[i]) print(res)
p03262
def gcd(A, B): if A%B == 0: return B else: return gcd(B, A%B) N, X = list(map(int, input().split())) x = [abs(int(i) - X) for i in input().split()] if N == 1: print((x[0])) exit() gcd_x = [] for i in range(N-1): gcd_x.append(gcd(x[i], x[i+1])) print((min(gcd_x)))
def gcd(A, B): while B != 0: A, B = B, A%B return A N, X = list(map(int, input().split())) x = [abs(int(i) - X) for i in input().split()] y = x[0] for i in range(N): if x[i] % y == 0: continue y = gcd(y, x[i]) print(y)
p03262
import sys def main(): input = sys.stdin.readline N, S = list(map(int, input().split())) C = list(map(int, input().split())) C = list(set([abs(c - S) for c in C])) min_C = min(C) ans = 1 for i in range(min_C, 1, -1): if min_C % i != 0: continue if all([c % i == 0 for c in ...
import sys def gcd(a, b): a, b = max(a, b), min(a, b) if b == 0: return a else: return gcd(b, a % b) def main(): input = sys.stdin.readline N, S = list(map(int, input().split())) C = list(map(int, input().split())) C = list(set([abs(c - S) for c in C])) ans = C[0] for i ...
p03262
def gcd(a,b): if b == 0: return a return gcd(b,a%b) N, S = list(map(int,input().split())) x = list(map(int,input().split())) x.append(S) x = sorted(x) t = x[1]-x[0] for k in range(N): t = gcd(t,x[k+1]-x[k]) print(t)
N, X = list(map(int,input().split())) x = list(map(int,input().split())) def gcd(a,b): if b == 0: return a return gcd(b,a%b) ans = abs(X-x[0]) for k in range(1,N): ans = gcd(ans,abs(x[k]-x[k-1])) print(ans)
p03262
values = input() n,x = int(values.split(" ")[0]), int(values.split(" ")[1]) cities = input() cities = [int(i) for i in cities.split(" ")] distances = [] for i in cities: distances.append(abs(i-x)) m = max(distances) D = m while(D<=m): pos = x flag = True for i in range(len(cities)): if cities[i]<...
values = input() n,x = int(values.split(" ")[0]), int(values.split(" ")[1]) cities = input() cities = [int(i) for i in cities.split(" ")] cities = sorted(cities) distances = [] for i in cities: distances.append(abs(i-x)) for i in range(1,len(cities)): distances.append(abs(cities[i]-cities[i-1])) def h...
p03262
N, X = [int(x) for x in input().split(' ')] city = [abs(int(x) - X) for x in input().split(' ')] max_fact = city[0] for x in city: while x!=0: if x > max_fact: x -= max_fact elif x == max_fact: break else: tmp = max_fact max_fact...
N, X = [int(x) for x in input().split(' ')] city = [abs(int(x) - X) for x in input().split(' ')] max_fact = city[0] for x in city: while x!=0: x %= max_fact if x > 0: tmp = max_fact max_fact = x x = tmp if max_fact==1: break print(max...
p03262
import math def count_factor(factor, factors): count = 0 # print("factor: " + str(factor)) # print(factors) for f in factors: if f == factor: count += 1 return count def divide_to_prime_factors(num): factor = 1 factors = [] while factor <= math.sqr...
import math def is_common(dists, factor): count = 0 # print("factor: " + str(factor)) # print(factors) # print(factor) # print(factors[1:] for dist in dists: if dist % factor != 0: return False return True def divide_to_prime_factors(num): factor =...
p03262
n,x = list(map(int,input().split())) xl = list(map(int,input().split())) xl = [i-x for i in xl] def gcd(a, b): while b: a, b = b, a % b return a ans = abs(xl[0]) for xx in xl: ans = abs(gcd(ans, xx)) print(ans)
n,x = list(map(int,input().split())) xl = list(map(int,input().split())) xl = [abs(x-i) for i in xl] # a,bの最大公約数 def gcd(a, b): while b: a, b = b, a % b return a ans = xl[0] for x in xl: ans = gcd(ans, x) print(ans)
p03262
import sys N,X=list(map(int,input().split())) x=list(map(int,input().split())) def gcd(a, b): while b: a, b = b, a % b return a if N==1: print((abs(X-x[0]))) sys.exit() x.append(X) x.sort() ANS=gcd(x[1]-x[0],x[2]-x[1]) for i in range(N): ANS=gcd(ANS,x[i+1]-x[i]) print(AN...
N,X=list(map(int,input().split())) A=list(map(int,input().split())) def gcd(a, b): while b: a, b = b, a % b return a s=abs(A[0]-X) for i in range(1,N): s=gcd(s,abs(A[i]-X)) print(s)
p03262
n, x = list(map(int, input().split())) xi = list(map(int, input().split())) diff = max(max(xi) - x, x - min(xi)) for d in range(diff, 1, -1): xd = x%d sol = True for xval in xi: if ((xval % d) - xd)%d != 0: sol = False break if sol: print(d) ...
n, x = list(map(int, input().split())) xi = list(map(int, input().split())) xi = [abs(xval - x) for xval in xi] def gcd(a, b): if a == 0: return b elif b == 0: return a else: return gcd(b, a%b) res = xi[0] for i in range(1, len(xi)): res = gcd(res, xi[i]) pri...
p03262
def gcd(x,y): if y>x: a=y y=x x=a a=1 while a*y: a=x%y x=y y=a return x a,b=list(map(int,input().split())) c=list(map(int,input().split())) c.append(b) c.sort() for i in range(a): c[-1-i]-=c[-2-i] b=0 for i in range(a): b=gcd(c...
def gcd(x,y): if y>x: a=y y=x x=a while y: a=x%y x=y y=a return x a,b=list(map(int,input().split())) c=list(map(int,input().split())) c.append(b) c.sort() for i in range(a): c[-1-i]-=c[-2-i] b=0 for i in range(a): b=gcd(c[i+1],b) p...
p03262
n, x = list(map(int, input().split())) l = list(map(int, input().split())) def gcd(x, y): while (y != 0): x, y = y, x%y return x diff = [] for j in l: diff.append(abs(j - x)) ans = diff[0] for d in diff[1:]: ans = gcd(ans, d) print(ans)
n, x = list(map(int, input().split())) l = list(map(int, input().split())) def gcd(x, y): while (y != 0): x, y = y, x%y return x ans = abs(l[0] - x) for d in l[1:]: ans = gcd(ans, abs(d - x)) print(ans)
p03262
# encoding: utf-8 N, X = list(map(int, input().split())) dx = list([x - X for x in list(map(int, input().split()))]) dx_nearest = 10**9 for dxi in dx: if (2 *(dxi > 0) - 1) * dxi < dx_nearest: dx_nearest = (2 *(dxi > 0) - 1) * dxi for D in range(dx_nearest + 1)[::-1]: for dxi in dx: if dxi %...
# encoding: utf-8 import math N, X = list(map(int, input().split())) x = sorted(list(map(int, input().split())) + [X]) def gcf2(a, b): if b == 0: return a else: return gcf2(b, (a % b)) # Greatest common factor # print(x) GCF = x[1] - x[0] x_min = x[0] for i, xi in enumerate(x): if i == 0: ...
p03262
# encoding: utf-8 import math N, X = list(map(int, input().split())) x = sorted(list(map(int, input().split())) + [X]) def gcf2(a, b): if b == 0: return a else: return gcf2(b, (a % b)) # Greatest common factor # print(x) GCF = x[1] - x[0] x_min = x[0] for i, xi in enumerate(x): if i == 0: ...
N, X = list(map(int, input().split())) x = list(map(int, input().split())) # targ = [] # # targ.append(abs(X - x[0])) # for i in range(N - 1): # targ.append(abs(x[i + 1] - x[i])) def mygcd(a, b): # print("#", a, b) if a < b: return mygcd(b, a) elif b == 0: return a else: return mygcd(b...
p03262
N,X=list(map(int,input().split())) x=sorted([int(x)-X if int(x)-X>0 else -(int(x)-X) for x in input().split()]) for i in range(x[0]+1)[::-1]: if sum([x%i for x in x])==0: print(i) break
N,X=list(map(int,input().split())) x=sorted([int(x)-X if int(x)-X>0 else -(int(x)-X) for x in input().split()]) for i in range(x[0]+1)[::-1]: if x[0]%i:continue if sum([x%i for x in x])==0: print(i) break
p03262
from functools import reduce n, X = list(map(int, input().split())) x = list(map(int, input().split())) for i in range(n): x[i] -= X x[i] = abs(x[i]) def gcd(a, b): if b == 0: return a return gcd(b, a % b) def gcd_list(n): return reduce(gcd, n) print((gcd_list(x)))
from functools import reduce def gcd(a, b): if b == 0: return a return gcd(b, a % b) def gcd_list(n): return reduce(gcd, n) n, X = list(map(int, input().split())) x = list(map(int, input().split())) dx = [abs(X - i) for i in x] print((gcd_list(dx)))
p03262
def make_divisors(n): divisors = [] n=abs(n) 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 set(divisors) def main(): n,m=list(map(int,input().split())) x=list(map(int,input...
def make_divisors(n): divisors = [] n=abs(n) 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 set(divisors) def main(): n,m=list(map(int,input().split())) x=list(map(int,input...
p03262
def gcd(p, q): a, b = (p, q) if p >= q else (q, p) c = a % b while c != 0: a, b = (b, c) if b >= c else (c, b) c = a % b return b N, X = [int(x) for x in input().split()] xn = sorted([int(x) for x in input().split()]) diffs = [] if X < xn[0]: diffs.append(xn[0] -X) for n in range(...
def gcd(x, y): #assert x >= y return gcd(y, x%y) if y else x N, X = [int(i) for i in input().split()] Xn = [int(i) for i in input().split()] dx =[abs(x-X) for x in Xn] gcd_num = dx[0] for x in dx: a, b = (gcd_num, x) if gcd_num > x else (x, gcd_num) gcd_num = gcd(a, b) print(gcd_num) ...
p03262
n,x=list(map(int,input().split())) huga=list(map(int,input().split())) hoge=[] for i in range(n): hoge.append(abs(huga[i]-x)) hoge.sort(reverse=True) D=hoge[0] for i in range(D): ans=D-i flag=0 for j in range(n): if(hoge[j]%ans>0): flag=1 if(flag==0): print(ans) break
#ユークリッド互除法 def gcd(a,b): if(a<b): a,b=b,a if(b==0): return a c= a%b return gcd(b,c) n,x=list(map(int,input().split())) hoge=list(map(int,input().split())) ans=abs(x-hoge[0]) for i in range(n): ans=gcd(ans,abs(hoge[i]-x)) print(ans)
p03262
#abc110 C N, X = list(map(int, input().split())) x = list(map(int, input().split())) distance = [ abs(x[i]-X) for i in range(N)] for d in range(min(distance),0,-1): flag = 1 for i in range(N): if distance[i] % d != 0: flag = 0 if flag == 1: print(d) break
#abc110 C 最大公約数 N, X = list(map(int, input().split())) x= list(map(int, input().split())) distance = [ abs(x[i]-X) for i in range(N)] def gcd( x , y ): while y != 0: x,y = y, x%y return x def GCD(lis): g = lis[0] for i in range(1,len(lis)): g = gcd( g , lis[i] ) retur...
p03262
from functools import reduce def gcd(a, b): while b: a, b = b, a % b return a def gcd_n(numbers): return reduce(gcd, numbers) def main(): N, X, *x = list(map(int, open(0).read().split())) x = [abs(X - int(i)) for i in x] print((gcd_n(x))) return main()
from functools import reduce def gcd(a, b): while b: a, b = b, a % b return a def gcd_n(numbers): return reduce(gcd, numbers) def main(): N, X = list(map(int, input().split())) x = [abs(X - int(i)) for i in input().split()] print((gcd_n(x))) return main()
p03262
def gcd(a, b): while b: a, b = b, a % b return a def main(): N, X, *xn = list(map(int, open(0).read().split())) ans = None for x in xn: if ans is not None: ans = gcd(ans, abs(X - x)) else: ans = abs(X - xn[0]) print(ans) return...
def gcd(a, b): while b: a, b = b, a % b return a def main(): N, X, *xn = list(map(int, open(0).read().split())) ans = None for x in xn: if ans is not None: if abs(X - x) % ans: ans = gcd(ans, abs(X - x)) else: ans = abs(...
p03262
# coding: utf-8 def gcd(x, y): if y == 0: return x else: return gcd(y, x%y) N, X = list(map(int, input().split())) x = list(map(int, input().split())) x.append(X) x = sorted(x) A = [ abs(x[i+1] - x[i]) for i in range(N)] for i in range(len(A)): if i == 0: a...
def gcd(x, y): while y != 0: x, y = y, x % y return x N, X = list(map(int, input().split())) x = [int(a) for a in input().split()] ans = 0 for xx in x: ans = gcd(ans, abs(X - xx)) print(ans)
p03262
n,s = (int(i) for i in input().split()) x = list(int(i) for i in input().split()) x = sorted(x) if n>1: dif = [] for i in range(n-1): dif.append(x[i+1]-x[i]) difs = [] for i in range(n): difs.append(abs(s-x[i])) res = min(min(dif),min(difs)) for i in range(res): ...
N, X = list(map(int, input().split())) x = list(map(int, input().split())) x.sort() dist = [abs(x[0]-X)] for i in range(N - 1): dist.append(x[i + 1] - x[i]) # dist内の最大公約数を求めればよい def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) res = dist[0] for i in range(1, N): ...
p03262
def gcd(a,b): while b != 0: a, b = b, a%b # print(a,b) return(a) def main(): N,X = list(map(int,input().split())) x = list(map(int,input().split())) for i in range(N): x[i] = abs(x[i]-X) while len(x) > 1: # print(x[0],x[1]) y = gcd(x[0],x[1...
def gcd(a,b): while b != 0: a, b = b, a%b return(a) def main(): N,X = list(map(int,input().split())) x = list(map(int,input().split())) for i in range(N): x[i] = abs(x[i]-X) y = x[0] for i in range(N): if x[i]%y == 0: continue y ...
p03262
n=list(map(int,input().split())) Y=list(map(int,input().split())) XX=[abs(Y[i]-n[1]) for i in range(n[0])] X=sorted(XX) def gcd(x,y): if y == 0: return x else: return gcd(y, x%y) for i in range(n[0]-1): X[0]=gcd(X[0],X[1]) X=[X[0]]+X[2:] print((max(X)))
n=list(map(int,input().split())) Y=list(map(int,input().split())) XX=[abs(Y[i]-n[1]) for i in range(n[0])] X=sorted(XX) def gcd(x,y): if y == 0: return x else: return gcd(y, x%y) ans=X[0] for i in range(n[0]-1): ans=gcd(ans,X[i]) print(ans)
p03262
n, x = list(map(int, input().split())) xlis = list(map(int, input().split())) def gcd(a,b): if b == 0: return a else: return gcd(b, a%b) if n > 1: d = gcd(abs(xlis[0]-x), abs(xlis[1]-x)) for i in range(2, n): d = gcd(d, abs(xlis[i]-x)) print(d) else: print((a...
n, x = list(map(int, input().split())) xlis = list(map(int, input().split())) def gcd(a,b): if b == 0: return a else: return gcd(b, a%b) d = 0 for i in range(n): d = gcd(d, abs(xlis[i]-x)) print(d)
p03262
# abc109c def gcd(a, b): if a < b: a, b = b, a r = a % b if r == 0: return b return gcd(r, b) N,X = list(map(int,input().split())) x = list(map(int,input().split())) x.append(X) x.sort() diff = [x[1]-x[0]] for i in range(N): diff.append(x[i+1]-x[i]) D = gcd(dif...
def gcd(a, b): if a < b: a, b = b, a r = a % b if r == 0: return b return gcd(r, b) N,X = list(map(int,input().split())) x = list(map(int,input().split())) x.append(X) x.sort() diff = [x[1]-x[0]] for i in range(N): diff.append(x[i+1]-x[i]) D = gcd(diff[0],diff[1])...
p03262
from functools import reduce from math import gcd n,x = list(map(int,input().split())) a = list(map(int,input().split())) b = [] for ai in a: b.append(abs(ai-x)) print((reduce(gcd,b)))
from functools import reduce from math import gcd n,x = list(map(int,input().split())) a = list(map(int,input().split())) print((reduce(gcd,(abs(ai-x) for ai in a))))
p03262
from functools import reduce from math import gcd n,x = list(map(int,input().split())) a = list(map(int,input().split())) print((reduce(gcd,(abs(ai-x) for ai in a))))
from functools import reduce from math import gcd n, x = list(map(int, input().split())) print((reduce(gcd, (abs(ai - x) for ai in map(int,input().split())))))
p03262
N, X = list(map(int,input().split())) x = list(map(int,input().split())) def gcd(m, n): while n: m, n = n, m%n return m sub_list = [] for i in range(N): sub_list.append(abs(x[i] - X)) ans = sub_list[0] for i in range(N): if(sub_list[i]%ans != 0): ans = gcd(ans, sub_list[i]) print(ans)
N, X = list(map(int,input().split())) x = list(map(int,input().split())) def gcd(m, n): while n: m, n = n, m%n return m subs = [] for i in range(N): subs.append(abs(x[i] - X)) ans = subs[0] for sub in subs: if(sub%ans != 0): ans = gcd(ans, sub) print(ans)
p03262
N, X = list(map(int,input().split())) x = list(map(int,input().split())) def gcd(m, n): while n: m, n = n, m%n return m tmp = [abs(X-xx) for xx in x] ans = tmp[0] for i in range(1,N): ans = gcd(ans, tmp[i]) print(ans)
def gcd(m, n): while n: m, n = n, m%n return m def ans(): N, X = list(map(int,input().split())) x = list(map(int,input().split())) xx = [abs(X-n) for n in x] res = xx[0] for i in range(1,N): res = gcd(res, xx[i]) print(res) ans()
p03262
N,X=list(map(int,input().split())) x=list(map(int,input().split())) diffx=[abs(X-x[i]) for i in range(N)] minDiff=min(diffx) isFinished=0 for i in range(minDiff,0,-1): if minDiff%i==0: canDivide=1 for j in range(N): if diffx[j]%i: canDivide=0 ...
N,X=list(map(int,input().split())) x=list(map(int,input().split())) diffx=[abs(X-x[i]) for i in range(N)] def euclid(a,b): if b==0: return a else: return euclid(b,a%b) gcd=diffx[0] for i in range(1,N): gcd=euclid(diffx[i],gcd) print(gcd)
p03262
import sys sys.setrecursionlimit(10**6) import math def ggcd(a): if len(a)==0: return 0 i=0 return math.gcd(a[i],ggcd(a[i+1:])) n,x=list(map(int,input().split())) a=list(map(int,input().split())) b=[] for i in range(n-1): b.append(abs(a[i]-a[i+1])) c=ggcd(b) d=abs(x-a[0]) print((m...
n,x=list(map(int,input().split())) a=list(map(int,input().split())) b=[] if n==1 and x==a[0]: print((max(x,10**9-x))) exit() for i in range(n): b.append(abs(a[i]-x)) import math c=0 for i in range(n): c=math.gcd(c,b[i]) if c==1: break print(c)
p03262
N,X = list(map(int,input().split())) L = list(map(int,input().split())) for i in range(N): L[i] = abs(L[i]-X) L.sort() L.reverse() def gcd(a,b): if b == 0: return a else: return gcd(b,a%b) def listgcd(L): p = L[0] for i in range(1,N): p = gcd(p,L[i]) return p if len(L) >= 2: p...
N,X = list(map(int,input().split())) L = list(map(int,input().split())) for i in range(N): L[i] = abs(L[i]-X) L.sort() def gcd(x,y): while y != 0: k = x x = y y = k%y return x cur = L[0] for i in range(1,N): cur = gcd(L[i],cur) print(cur)
p03262
import sys input = sys.stdin.readline def gcd(a, b): while a > 0: if a < b: a, b = b, a a = a % b return b def main(): n, x = list(map(int, input().split())) distance = (abs(x - int(y)) for y in input().split()) res = next(distance) for d in distance...
import sys input = sys.stdin.readline def gcd(a, b): while a > 0: if a < b: a, b = b, a a = a % b return b def main(): n, x = list(map(int, input().split())) town = list(map(int, input().split())) distance = (abs(x-y) for y in town) res = next(distan...
p03262
def gcd(a, b): if a < b: a, b = b, a while b > 0: a = a % b a, b = b, a return a def main(): n, x = list(map(int, input().split())) town = list(map(int, input().split())) distance = (abs(x-y) for y in town) res = next(distance) for d in distance: ...
import sys input = sys.stdin.readline def gcd(a, b): while a > 0: if a < b: a, b = b, a a = a % b return b def main(): n, x = list(map(int, input().split())) town = list(map(int, input().split())) distance = (abs(x-y) for y in town) res = next(distan...
p03262
N,X=list(map(int,input().split())) x=list(map(int,input().split())) x.append(X) x=sorted(x) dif=[0]*(len(x)-1) for i in range(len(x)-1): dif[i]=x[i+1]-x[i] def gcd(a,b): if b>a: a,b=b,a while True: if a%b==0: return b else: a,b=b,a%b ans=dif[0] for i in range(1,len(dif)...
n,x=list(map(int,input().split())) p=sorted(list(map(int,input().split()))) def gcd(a,b): if a<b: a,b=b,a while a%b>0: a,b=b,a%b return b ans=abs(x-p[0]) for i in range(1,n): ans=gcd(ans,p[i]-p[i-1]) print(ans)
p03262
n,x=list(map(int,input().split())) l=list(map(int,input().split())) def make_divisor_list(num): if num < 1: return [] elif num == 1: return [1] else: divisor_list = [] divisor_list.append(1) for i in range(2, num // 2 + 1): if num % i == 0: ...
n,x=list(map(int,input().split())) l=list(map(int,input().split())) def gcd(a, b): while b > 0: a, b = b, a%b return a kouho=[abs(l[i]-x) for i in range(n)] ans=kouho[0] for i in range(n-1): ans = gcd(ans,kouho[i+1]) print(ans)
p03262
N,X = list(map(int,input().split())) list = [int(x) for x in input().split()] list_dist = sorted([abs(x - X) for x in list]) def gcd(a, b): if a < b: a , b = b , a while b: a, b = b, a % b return a ans = list_dist[0] for x in list_dist[1:]: ans = gcd(ans,x) print(ans)
N,X = list(map(int,input().split())) list = list(map(int,input().split())) diff = [abs(x - X) for x in list] diff.sort() def gcd(a,b): while b: a,b = b,a%b return a #差の最大公約数を取得 gcd_val = diff[0] for i in diff[1:]: gcd_val = gcd(i,gcd_val) print(gcd_val)
p03262
n, x = list(map(int, input().split())) X = list(map(int, input().split())) X.sort(reverse=True) D = [] for i in range(len(X)): dist = abs(x - X[i]) D.append(dist) def gcd(i, j): while j != 0: i, j = j, i % j return i for i in range(len(D) - 1): D[i + 1] = gcd(D[i], D[i + 1]...
n, x = list(map(int, input().split())) city = list(map(int, input().split())) D = [] for i in range(n): dis = abs(x - city[i]) D.append(dis) def gdc(x, y): if x%y == 0: return y x, y = y, x%y return gdc(x, y) for i in range(n-1): D[i + 1] = gdc(D[i], D[i + 1]) pr...
p03262
import bisect def gcd(x , y): max_val = max(x , y) min_val = min(x , y) while min_val != 0: max_val , min_val = min_val , max_val % min_val return max_val N , X = list(map(int,input().split())) x = list(map(int,input().split())) x.sort() split_idx = bisect.bisect_left(x , X) BeforeLis...
def gcd(a, b): while b != 0: a, b = b, a % b return a N, X = list(map(int, input().split())) x = list(map(int, input().split())) x.insert(0, X) distance = [] for i in range(1, len(x)): distance.append(abs(x[i] - x[i - 1])) ans = distance[0] for i in range(1, len(distance)): a...
p03262
# coding: utf-8 # Here your code ! import sys from collections import Iterable import unittest def top_face_after_rolling_dice(): try: faces = [int(num) for num in input().rstrip().split()] rollings = input().rstrip() except: return __input_error() dice = CubicArbit...
# coding: utf-8 # Here your code ! from sys import exit from collections import Iterable from unittest import TestCase #import numpy as np def top_face_after_rolling_dice(): try: faces = [int(num) for num in input().rstrip().split()] rollings = input().rstrip() except...
p02383
# -*- coding: utf-8 -*- class dice_class: def __init__(self, top, front, right, left, back, bottom): self.top = top self.front = front self.right = right self.left = left self.back = back self.bottom = bottom def roll(self, s): ...
# -*- coding: utf-8 -*- class dice_class: def __init__(self, list): self.num = list def roll(self, s): for i in s: if i == 'E': self.rollE() elif i == 'N': self.rollN() elif i == 'S': self.rollS() ...
p02383
# AGC019B - Reverse and Compare from collections import Counter def main(): S = input().rstrip() C, N = list(Counter(S).values()), len(S) ans = N * (N - 1) // 2 + 1 # all possible patterns ans -= sum(i * (i - 1) // 2 for i in C) # duplicates print(ans) if __name__ == "__main__": ...
# AGC019B - Reverse and Compare def main(): S = input().rstrip() N = len(S) ans = N * (N - 1) // 2 + 1 # all possible patterns abc = "abcdefghijklmnopqrstuvwxyz" for a in abc: x = S.count(a) ans -= x * (x - 1) // 2 # exclude duplicates print(ans) if __name__ == "_...
p03618
num=input() out=0 for i in range(len(num)): #out+=num[i:].count(num(i)) #print len(num[i:])-num[i:].count(num[i]) out+=len(num[i:])-num[i:].count(num[i]) print(out+1)
num=input() out=0 d={} for i in set(num): d[i]=num.count(i) #print d #aaa for i in range(len(num)): #out+=num[i:].count(num(i)) #print len(num[i:])-num[i:].count(num[i]) #out+=len(num[i:])-num[i:].count(num[i]) out+=len(num[i:])-d[num[i]] d[num[i]]-=1 print(out+1)
p03618
I = input() import re if re.match("^(a|i|u|e|o){1}$", I): print('vowel', flush=True) else: print('consonant', flush=True)
I = input() import re # if re.match("^(a|i|u|e|o){1}$", I): if I in 'aiueo': # if 'aiueo'.find(I): print('vowel', flush=True) else: print('consonant', flush=True)
p03852
# li = sorted(list(map(int, input().split()))) # n, m = map(int, input().split()) c = input() print("vowel") if c in "aeiou" else print("consonant")
print("vowel") if input() in "aeiou" else print("consonant")
p03852
while True: n = int(eval(input())) if n == 0:break to = [] for i in range(n): line = list(map(int, input().split())) for j in range(n): x, y = line[2 * j:2 * j + 2] to.append(y * n + x) order = [] used = [False] * (n * n) def dfs(x): if used[x]:return used[x] =...
while True: n = int(eval(input())) if n == 0:break to = [] for i in range(n): line = list(map(int, input().split())) for j in range(n): x, y = line[2 * j:2 * j + 2] to.append(y * n + x) order = [] used = [False] * (n * n) def dfs(x): if used[x]:return used[x] =...
p01334
import collections import sys input = sys.stdin.readline class AtCoder: def main(self): S = input().rstrip() K = int(eval(input())) if len(S) == 1: print((K // 2)) exit() ans = 0 start = 0 end = len(S) cnt_1 = 1 ...
import collections import sys input = sys.stdin.readline class AtCoder: def main(self): S = input().rstrip() K = int(eval(input())) if len(collections.Counter(S)) == 1: print(((len(S) * K) // 2)) exit() k1 = self.connection_and_disconnectio...
p02891
import sys input = sys.stdin.readline s = input().rstrip() k = int(eval(input())) def check_count(s): cnt_list = [1] cnt = 1 for i in range(1, len(s)): if s[i-1] == s[i]: cnt_list[-1] += 1 else: cnt_list.append(1) return cnt_list def calc_a...
import sys input = sys.stdin.readline s = input().rstrip() k = int(eval(input())) def check_count(s): cnt_list = [] cnt = 1 for i in range(1, len(s)): if s[i-1] == s[i]: cnt += 1 else: cnt_list.append(cnt) cnt = 1 if cnt>1: c...
p02891
def f(k): T = S*k N = len(T) cur = T[0] cnt = 1 ans = 0 for i in range(1,N): if T[i]==cur: cnt += 1 else: ans += cnt//2 cur = T[i] cnt = 1 ans += cnt//2 return ans S = input().strip() K = int(eval(input())) ...
C = {} S = input().strip() N = len(S) K = int(eval(input())) for i in range(N): s = S[i] if s not in C: C[s]=0 C[s] += 1 if len(C)>1: a = 0 cnt = 1 for i in range(1,N): if S[i]==S[i-1]: cnt += 1 else: a += cnt//2 cnt = ...
p02891
from sys import stdin from itertools import groupby S = stdin.readline().rstrip() K = int(stdin.readline().rstrip()) l = len(S) # Run Length Encoding rle = [len(list(g)) for k, g in groupby(S)] if len(set(S)) == 1: print((len(S) * K //2)) else: tmp_ans = sum([l // 2 for l in rle])*K if S[0] != ...
from sys import stdin from itertools import groupby S = stdin.readline().rstrip() K = int(stdin.readline().rstrip()) l = len(S) # Run Length Encoding rle = [(k, sum(1 for _ in g)) for k, g in groupby(S)] if len(set(S)) == 1: print((len(S) * K // 2)) else: tmp_ans = sum([t[1] // 2 for t in rle])*K ...
p02891
from itertools import* s,k=open(0) k,x=int(k),0 g=[len(list(v))for _,v in groupby(s)] for c in g:x+=c//2 x*=k if(s[0]==s[-2])*g[0]%2&g[-2]%2:x+=k//2if-len(s)==~g[0]else k-1 print(x)
from itertools import* s,k=open(0) k=int(k) g=[len(list(v))for _,v in groupby(s)] print((sum(c//2for c in g)*k+((s[0]==s[-2])*g[0]%2&g[-2]%2)*(k//2*(-len(s)==~g[0])or k-1)))
p02891
from itertools import* s,k=open(0) k=int(k) g=[len(list(v))for _,v in groupby(s)] print((sum(c//2for c in g)*k-((s[0]==s[-2])*g[0]%2&g[-2]%2)*(-k//2**(len(s)-1==g[0])+1)))
from itertools import* s,k=open(0) k=int(k) g=[len(list(v))for _,v in groupby(s)] print((sum(c//2for c in g)*k-(g[0]%2*g[-2]%2*s[0]==s[-2])*(-k//2**(len(s)-1==g[0])+1)))
p02891
from itertools import* s,k=open(0) k=int(k) g=[len(list(v))for _,v in groupby(s)] print((sum(c//2for c in g)*k-(g[0]%2*g[-2]%2*s[0]==s[-2])*(-k//2**(len(s)-1==g[0])+1)))
from itertools import* s,k=open(0) k=int(k) g=[len(list(v))for _,v in groupby(s)] print((sum(c//2for c in g)*k-(g[0]*g[-2]%2*s[0]==s[-2])*(-k//2**(len(s)-1==g[0])+1)))
p02891
from itertools import* s,k=open(0) k=int(k) g=[len(list(v))for _,v in groupby(s)] print((sum(c//2for c in g)*k+((g[0]*g[-2]%2*s[0]==s[-2])*~-k>>(len(s)-1==g[0]))))
from itertools import* s,k=open(0) k=int(k) g=[len(list(v))for _,v in groupby(s)] print((sum(c//2for c in g)*k+((g[0]*g[-2]%2*s[0]==s[-2])*~-k>>(len(s)-2<g[0]))))
p02891
# 75 import sys sys.setrecursionlimit(10**8) MOD = 10**9+7 N = int(eval(input())) g_l = [[] for i in range(N)] check_l = [-1] * N num_l = [0] * N for i in range(N-1): ai, bi = list(map(int, input().split())) g_l[ai-1].append(bi-1) g_l[bi-1].append(ai-1) def dfs(n): d = 1 if che...
# 75 import sys sys.setrecursionlimit(10**8) MOD = 10**9+7 N = int(eval(input())) g_l = [[] for i in range(N)] check_l = [-1] * N num_l = [0] * N for i in range(N-1): ai, bi = list(map(int, input().split())) g_l[ai-1].append(bi-1) g_l[bi-1].append(ai-1) def dfs(n): d = 1 if che...
p02822
import sys sys.setrecursionlimit(10**6) from collections import defaultdict mod = 10**9+7 N = int(eval(input())) g = [[] for _ in range(N)] d = defaultdict(lambda:-1) p2 = [1]*(N+1) p2[1] = pow(2,mod-2,mod) for i in range(N): p2[i+1] = (p2[i]*p2[1])%mod for i in range(N-1): a,b = list(map(int,inpu...
from collections import defaultdict,deque mod = 10**9+7 N = int(eval(input())) g = [[] for _ in range(N)] d = defaultdict(lambda:-1) p2 = [1]*(N+1) p2[1] = pow(2,mod-2,mod) for i in range(N): p2[i+1] = (p2[i]*p2[1])%mod for i in range(N-1): a,b = list(map(int,input().split())) a -= 1 b -= ...
p02822
# ref https://qiita.com/ZhangChaoran/items/71fab0e4b8647a93d3a0 from collections import deque import sys input = sys.stdin.readline n = int(eval(input())) g = [[] for _ in range(n + 1)] for i in range(n - 1): a, b = list(map(int, input().split())) g[a].append(b) g[b].append(a) mod = 10**9 + 7 ...
# ref https://qiita.com/ZhangChaoran/items/71fab0e4b8647a93d3a0 from collections import deque import sys input = sys.stdin.readline n = int(eval(input())) g = [[] for _ in range(n + 1)] for i in range(n - 1): a, b = list(map(int, input().split())) g[a].append(b) g[b].append(a) mod = 10**9 + 7 ...
p02822
import sys from itertools import accumulate sys.setrecursionlimit(10 ** 5) def dfs1(v, p): parent[v] = p stc = subtree_count[v] cnt = 1 for u in links[v]: if u == p: continue result = dfs1(u, v) stc[u] = result cnt += result return cnt ...
import sys sys.setrecursionlimit(200001) def dfs1(v, p): parent[v] = p stc = subtree_count[v] cnt = 1 for u in links[v]: if u == p: continue result = dfs1(u, v) stc[u] = result cnt += result return cnt def dfs2(v, pc): # pc: vを根...
p02822
import sys readline = sys.stdin.readline class Segtree: def __init__(self, A, intv, initialize = True, segf = max): self.N = len(A) self.N0 = 2**(self.N-1).bit_length() self.intv = intv self.segf = segf if initialize: self.data = [intv]*self.N0 + A + [i...
import sys readline = sys.stdin.readline class Segtree: def __init__(self, A, intv, initialize = True, segf = max): self.N = len(A) self.N0 = 2**(self.N-1).bit_length() self.intv = intv self.segf = segf if initialize: self.data = [intv]*self.N0 + A + [i...
p02822
p = 1000000007 N = int(eval(input())) T = [[] for _ in range(N)] for _ in range(N - 1): (a, b) = (int(x) - 1 for x in input().split()) T[a].append(b) T[b].append(a) P = [-1] * N P[0] = 0 Q = [0] for i in range(N): q = Q[i] for adj in T[q]: if 0 <= P[adj]: continue P[adj] = q ...
p = 1000000007 N = int(eval(input())) T = [[] for _ in range(N)] for _ in range(N - 1): (a, b) = (int(x) - 1 for x in input().split()) T[a].append(b) T[b].append(a) P = [-1] * N P[0] = 0 Q = [0] for i in range(N): q = Q[i] for adj in T[q]: if 0 <= P[adj]: continue P[adj] = q ...
p02822
import sys sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python import math from copy import copy, deepcopy from copy import deepcopy as dcp from operator import itemgetter from bisect import bisect_left, bisect, bisect_right#2分探索 #bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下 from co...
import sys sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python import math from copy import copy, deepcopy from copy import deepcopy as dcp from operator import itemgetter from bisect import bisect_left, bisect, bisect_right#2分探索 #bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下 from co...
p02822
class Tree: C, RL = {}, {} R, N, D, S, P = None, None, None, None, None SN = None def __init__(s, num): s.N = num def set(s, a, b): if a in s.C: s.C[a].append(b) else: s.C[a] = [b] if b in s.C: s.C[b].append(a) else: s.C[b] = [a] def makeRank(s, root): s.R = [0] * s.N #各ノ...
class Tree: C, RL = {}, {} R, N, D, S, P = None, None, None, None, None SN = None def __init__(s, num): s.N = num def set(s, a, b): if a in s.C: s.C[a].append(b) else: s.C[a] = [b] if b in s.C: s.C[b].append(a) else: s.C[b] = [a] def makeRank(s, root): s.R = [0] * s.N #各ノ...
p02822
import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 6) mod = 10 ** 9 + 7 N = int(eval(input())) vec = [[] for _ in range(N)] for _ in range(N - 1) : A, B = list(map(int, input().split())) vec[A-1].append(B-1) vec[B-1].append(A-1) sub_size = [[] for _ in range(N)] def dfs(cur, pre)...
import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 6) mod = 10 ** 9 + 7 N = int(eval(input())) vec = [[] for _ in range(N)] for _ in range(N - 1) : A, B = list(map(int, input().split())) vec[A-1].append(B-1) vec[B-1].append(A-1) own_size = [0] * N sub_size = [[] for _ in range(N)] ...
p02822
import sys sys.setrecursionlimit(5+10**5) mod=10**9+7 N=int(eval(input())) G=[[] for i in range(N)] for i in range(N-1): A,B=list(map(int,input().split())) G[A-1].append(B-1) G[B-1].append(A-1) Child=[[] for i in range(N)] #print(Child) #print(Parents) #print(G) reached=[0 for i in range(N)] ...
import sys sys.setrecursionlimit(2*10**5) mod=10**9+7 N=int(eval(input())) G=[[] for i in range(N)] for i in range(N-1): A,B=list(map(int,input().split())) G[A-1].append(B-1) G[B-1].append(A-1) Child=[[] for i in range(N)] #print(Child) #print(Parents) #print(G) reached=[0 for i in range(N)] ...
p02822
#!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop from itertools import permutations import sys import math import bisect 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...
#!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop from itertools import permutations import sys import math import bisect 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...
p02822
import sys sys.setrecursionlimit(2 * 10 ** 5 + 10) mod = 10 ** 9 + 7 N, *AB = list(map(int, open(0).read().split())) E = [[] for _ in range(N)] for i, (a, b) in enumerate(zip(*[iter(AB)] * 2)): E[a - 1].append((b - 1, i)) E[b - 1].append((a - 1, i)) X = [0] * N def dfs(u, p): res = 1 ...
import sys sys.setrecursionlimit(2 * 10 ** 5 + 10) mod = 10 ** 9 + 7 N, *AB = list(map(int, open(0).read().split())) E = [[] for _ in range(N)] for i, (a, b) in enumerate(zip(*[iter(AB)] * 2)): E[a - 1].append((b - 1, i)) E[b - 1].append((a - 1, i)) X = [0] * N def dfs(u, p): res = 1 ...
p02822
import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict con = 10 ** 9 + 7; INF = float("inf") def getlist(): return list(map(int, input().split())) class Graph(object): def __init__(self): self.graph = defaultdict(list) def __len__(self): ret...
import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict con = 10 ** 9 + 7; INF = float("inf") def getlist(): return list(map(int, input().split())) class Graph(object): def __init__(self): self.graph = defaultdict(list) def __len__(self): ret...
p02822
import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict con = 10 ** 9 + 7; INF = float("inf") def getlist(): return list(map(int, input().split())) class Graph(object): def __init__(self): self.graph = defaultdict(list) def __len__(self): ret...
import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict con = 10 ** 9 + 7; INF = float("inf") def getlist(): return list(map(int, input().split())) class Graph(object): def __init__(self): self.graph = defaultdict(list) def __len__(self): ret...
p02822
import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict con = 10 ** 9 + 7; INF = float("inf") def getlist(): return list(map(int, input().split())) class Graph(object): def __init__(self): self.graph = defaultdict(list) def __len__(self): ret...
import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict con = 10 ** 9 + 7; INF = float("inf") def getlist(): return list(map(int, input().split())) class Graph(object): def __init__(self): self.graph = defaultdict(list) def __len__(self): ret...
p02822
import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict con = 10 ** 9 + 7; INF = float("inf") def getlist(): return list(map(int, input().split())) class Graph(object): def __init__(self): self.graph = defaultdict(list) def __len__(self): ret...
import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict from collections import deque con = 10 ** 9 + 7; INF = float("inf") def getlist(): return list(map(int, input().split())) class Graph(object): def __init__(self): self.graph = defaultdict(list...
p02822
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools import time,random sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return list(map(i...
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools import time,random sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return list(map(i...
p02822
def JOI14_B(): N = I() A = [I()for _ in range(N)] A.extend(A) dp = [[0]*(N*2+1) for _ in range(N*2+1)] for j in range(N): for i in range(N*2-j): if (N-j)%2==1: dp[i][i+j] = max(dp[i+1][i+j]+A[i],dp[i][i+j-1]+A[i+j]) else: if...
def JOI14_B(): N = I() A = [I()for _ in range(N)] A.extend(A) dp = [[0]*(N*2+1) for _ in range(N*2+1)] for j in range(N): for i in range(N*2-j): if (N-j)%2==1: dp[i][i+j] = max(dp[i+1][i+j]+A[i],dp[i][i+j-1]+A[i+j]) else: if...
p02822
#f import sys sys.setrecursionlimit(2 * 10 ** 5 + 10) n = int(eval(input())) E = [[] for _ in range(n)] mod = 10**9 + 7 for i in range(n-1): a,b = [int(x) for x in input().split()] E[a-1].append((b-1, i)) E[b-1].append((a-1, i)) X = [0] * n def dfs(u,e): num = 1 for...
#f import sys sys.setrecursionlimit(2 * 10 ** 5 + 10) n = int(eval(input())) E = [[] for _ in range(n)] mod = 10**9 + 7 for i in range(n-1): a,b = [int(x) for x in input().split()] E[a-1].append((b-1, i)) E[b-1].append((a-1, i)) X = [0] * n def dfs(u,e): num = 1 for...
p02822
N=int(eval(input())) E=[[] for i in range(N+1)] for i in range(N-1): x,y=list(map(int,input().split())) E[x].append(y) E[y].append(x) mod=10**9+7 from collections import deque Q=deque() USE=[0]*(N+1) Q.append(1) H=[0]*(N+1) H[1]=1 USE[1]=1 while Q: x=Q.pop() for to in E[x...
N=int(eval(input())) E=[[] for i in range(N+1)] for i in range(N-1): x,y=list(map(int,input().split())) E[x].append(y) E[y].append(x) mod=10**9+7 from collections import deque Q=deque() USE=[0]*(N+1) Q.append(1) H=[0]*(N+1) H[1]=1 USE[1]=1 while Q: x=Q.pop() for to in E[x...
p02822
N=int(eval(input())) E=[[] for i in range(N+1)] for i in range(N-1): x,y=list(map(int,input().split())) E[x].append(y) E[y].append(x) mod=10**9+7 from collections import deque Q=deque() USE=[0]*(N+1) Q.append(1) H=[0]*(N+1) H[1]=1 USE[1]=1 while Q: x=Q.pop() for to in E[x...
import sys input = sys.stdin.readline N=int(eval(input())) E=[[] for i in range(N+1)] for i in range(N-1): x,y=list(map(int,input().split())) E[x].append(y) E[y].append(x) mod=10**9+7 from collections import deque Q=deque() USE=[0]*(N+1) Q.append(1) H=[0]*(N+1) H[1]=1 USE[1]=1 ...
p02822
import sys sys.setrecursionlimit(10**6) N = int(eval(input())) adj = [ [] for _ in range(N+1) ] for _ in range(N-1): a,b = list(map(int,input().split())) adj[a].append(b) adj[b].append(a) def dfs(v, p=-1): global ans res = 1 ts = [] for u in adj[v]: if (u==p): continue t = df...
import sys sys.setrecursionlimit(10**6) N = int(eval(input())) adj = [ [] for _ in range(N+1) ] for _ in range(N-1): a,b = list(map(int,input().split())) adj[a].append(b) adj[b].append(a) def dfs(v, p=-1): global ans res = 1 ts = [] for u in adj[v]: if (u==p): continue t = dfs(...
p02822
MOD=10**9+7 class Fp(int): def __new__(self,x=0):return super().__new__(self,x%MOD) def inv(self):return self.__class__(super().__pow__(MOD-2,MOD)) def __add__(self,value):return self.__class__(super().__add__(value)) def __sub__(self,value):return self.__class__(super().__sub__(value)) def __...
from sys import setrecursionlimit setrecursionlimit(200200) MOD=10**9+7 N = int(eval(input())) AB = [tuple(map(int, input().split())) for _ in range(N-1)] G = [[] for _ in range(N)] for i, (a, b) in enumerate(AB): a -= 1 b -= 1 G[a].append((b, i)) G[b].append((a, i)) P = [0] * N visited = ...
p02822
from sys import setrecursionlimit setrecursionlimit(200200) MOD=10**9+7 N = int(eval(input())) AB = [tuple(map(int, input().split())) for _ in range(N-1)] G = [[] for _ in range(N)] for i, (a, b) in enumerate(AB): a -= 1 b -= 1 G[a].append((b, i)) G[b].append((a, i)) P = [0] * N visited = ...
import sys sys.setrecursionlimit(200200) input = sys.stdin.readline MOD=10**9+7 N = int(eval(input())) AB = [tuple(map(int, input().split())) for _ in range(N-1)] G = [[] for _ in range(N)] for i, (a, b) in enumerate(AB): a -= 1 b -= 1 G[a].append((b, i)) G[b].append((a, i)) P = [0] * N v...
p02822
import heapq import copy N = int(eval(input())) ab = [[] * (N + 1) for i in range(N + 1)] E = [0] * (N - 1) for i in range(N - 1): a, b = list(map(int, input().split())) ab[a].append(b) ab[b].append(a) E[i] = [a, b] #print(ab) #xとyを根とする部分木の頂点の数を求める #辺xy を切り離して考える def dfs(x, y): q = copy.de...
#解説方針#解法1 #https://qiita.com/ZhangChaoran/items/71fab0e4b8647a93d3a0 from collections import deque from heapq import heappop, heappush #標準入力 N = int(eval(input())) MOD = 10 ** 9 + 7 L = [[] for i in range(N + 1)] #dfs用 for i in range(N - 1): a, b = list(map(int, input().split())) L[a].append(b) L[b].ap...
p02822