code1
stringlengths
16
427k
code2
stringlengths
16
427k
similar
int64
0
1
pair_id
int64
6.82M
181,637B
question_pair_id
float64
101M
180,471B
code1_group
int64
2
299
code2_group
int64
2
299
n=int(input()) minv=int(input()) maxv=-float('inf') for _ in range(n-1): tmp=int(input()) maxv=max(maxv,tmp-minv) minv=min(minv,tmp) print(maxv)
n = input() maxv = - 1 * pow(10, 9) minv = input() for _ in xrange(1, n): Rj = input() maxv = max(maxv, Rj - minv) minv = min(minv, Rj) print maxv
1
13,515,405,866
null
13
13
from math import gcd import sys N, M = map(int, input().split()) A = list(map(int, input().split())) lcm = 1 for a in A: gcd_ = gcd(lcm, a//2) lcm = lcm * (a // 2 // gcd_) for a in A: if (lcm // (a // 2)) % 2 == 0: print(0) sys.exit() print(M // lcm - M // (2 * lcm))
from collections import deque import numpy as np # (sx, sy) から (gx, gy) への最短距離を求める # 辿り着けないと INF def bfs(sx, sy): # すべての点を INF で初期化 d = [[float("-inf")] * m for i in range(n)] # 移動4方向のベクトル dx = [1, 0, -1, 0] dy = [0, 1, 0, -1] # スタート地点をキューに入れ、その点の距離を0にする que = deque([]) que.append((sx, sy)) d[sx][sy] = 0 # キューが空になるまでループ while que: # キューの先頭を取り出す p = que.popleft() # 取り出してきた状態がゴールなら探索をやめる #if p[0] == gx and p[1] == gy: # break # 移動4方向をループ for i in range(4): # 移動した後の点を (nx, ny) とする nx = p[0] + dx[i] ny = p[1] + dy[i] # 移動が可能かの判定とすでに訪れたことがあるかの判定 # d[nx][ny] != INF なら訪れたことがある if 0 <= nx < n and 0 <= ny < m and maze[nx][ny] != "#" and d[nx][ny] == float("-inf"): # 移動できるならキューに入れ、その点の距離を p からの距離 +1 で確定する que.append((nx, ny)) d[nx][ny] = d[p[0]][p[1]] + 1 a = np.max(d) return a n, m = map(int, input().split()) maze = [list(input()) for i in range(n)] ans = 1 for x in range(n): for y in range(m): sx = x sy = y if maze[sx][sy] == "#": continue A = bfs(sx, sy) ans = max(A, ans) print(int(ans))
0
null
98,024,826,425,700
247
241
# -*- coding: utf-8 -*- a = [int(raw_input()) for _ in range(10)] #print a b = sorted(a, reverse=True) #print b for c in b[0:3]: print c
import math a, b, h, m = map(int,input().split()) ms = m//60 ma = m%60 h = h + ms ha =h%12 + ma/60 rh = ha*math.pi/6 rm = ma*math.pi/30 x = math.cos(rh - rm) ans = math.sqrt(a**2 + b**2 - 2*a*b*x) print(ans)
0
null
10,115,539,553,680
2
144
out = 0 inp = list(map(int, input().split())) while 0<len(inp): out += inp.pop(0) if out <= 21: print('win') else: print('bust')
x = input().split() a, b, c =x d = int(a)+int(b)+int(c) if d<22: print("win") else: print("bust")
1
119,104,063,328,430
null
260
260
d,t,s = list(map(int, input().split())) if d/s<=t: print("Yes") else: print("No")
a,b,c = map(int, input().split()) print('Yes' if a/c <= b else 'No')
1
3,552,804,335,388
null
81
81
N,M,X=map(int,input().split()) C=[list(map(int,input().split())) for _ in range(N)] ans=inf=float('inf') def dfs(idx,tt,l): global ans for i in range(idx,N): ntt=tt+C[i][0] nl=[l[j]+C[i][j+1] for j in range(M)] for x in nl: if x<X: break else: ans=min(ans,ntt) dfs(i+1,ntt,nl) dfs(0,0,[0]*M) print(ans if ans<inf else -1)
n = int(input()) def divGame(N=n): if N == 1: return 0 if N == 2: return 1 factors = [] for i in range(2, int(N**0.5 + 1)): count = 0 while N % i == 0: N /= i count += 1 if count != 0: factors.append(count) if N != 1: factors.append(1) factors.sort() answer = 0 accum = 1 count = 1 for i in range(len(factors)): while factors[i] >= accum: count += 1 accum += count answer += count - 1 return answer print(divGame())
0
null
19,561,242,002,172
149
136
n, m = (int(i) for i in input().split()) h = [int(i) for i in input().split()] g = [[] for i in range(n)] for i in range(m): a, b = (int(i) for i in input().split()) a -= 1 b -= 1 g[a].append(b) g[b].append(a) cnt = 0 for i in range(n): mx = -1 for j in g[i]: mx = max(mx, h[j]) cnt += h[i] > mx print(cnt)
import sys import time import math import itertools as it def inpl(): return list(map(int, input().split())) st = time.perf_counter() # ------------------------------ N, M = inpl() H = inpl() dp = [True] * N for _ in range(M): A, B = inpl() if H[A-1] > H[B-1]: dp[B-1] = False elif H[A-1] < H[B-1]: dp[A-1] = False else: dp[A-1] = False dp[B-1] = False ans = 0 for i in dp: if i: ans += 1 print(ans) # ------------------------------ ed = time.perf_counter() print('time:', ed-st, file=sys.stderr)
1
25,036,053,415,982
null
155
155
N, K, C = map(int, input().split()) S = input() must = set() i = len(S)+C for j in range(K): i = S.rindex("o", 0, i-C) must.add(i) if i<=C or "o" not in S[:i-C]: i = -C-1 for j in range(K): i = S.index("o", i+C+1) if i in must: print(i+1)
# 解説 n,k,c = map(int, input().split()) s = input() l = [0 for i in range(n)] r = [-1 for i in range(n)] a = 1 b = -1 for i in range(n): if s[i]=='o' and b<i: l[i]=a a+=1 b=i+c if a>k: break a = k b = n for i in range(n): if s[-i-1]=='o' and b>n-i-1: r[-i-1]=a a-=1 b=n-i-1-c if a<1: break for i in range(n): if l[i]==r[i]: print(i+1)
1
40,554,744,433,412
null
182
182
s = input() t = '' for i in range(0, len(s)): if s[i] == '?': t += 'D' else: t += s[i] print(t)
N,M = list(map(int,input().split())) A = list(map(int,input().split())) A.sort(reverse=True) if A[M-1] >= sum(A)/(4*M) : print("Yes") else: print("No")
0
null
28,515,421,934,846
140
179
n = int(input()) ascii_letters = 'abcdefghijklmnopqrstuvwxyz' for i in range(1,15): if n<=26**i: order=i n-=1 break n-=26**i for i in range(order): print(ascii_letters[n//(26**(order-i-1))],end='') n%=(26**(order-i-1)) print()
# import sys # sys.setrecursionlimit(10 ** 6) # import bisect # from collections import deque # from decorator import stop_watch # # # @stop_watch def solve(N): alp = 'abcdefghijklmnopqrstuvwxyz' number27 = [] cnt = 26 digit = 1 while N > 0: if N <= cnt: break else: N -= cnt cnt *= 26 digit += 1 ans = list('a' * digit) N -= 1 tmp = -1 while N > 0: N, mod = divmod(N, 26) ans[tmp] = alp[mod] tmp -= 1 print(''.join(ans)) if __name__ == '__main__': # S = input() N = int(input()) # N, M = map(int, input().split()) # As = [int(i) for i in input().split()] # Bs = [int(i) for i in input().split()] solve(N) # for i in range(1, 100): # # print(i) # solve(i)
1
11,859,682,837,600
null
121
121
N, K, S = map(int, input().split()) rem = 1 if S >= 10**9 else S+1 ret = [S]*K + [rem]*(N-K) print(' '.join(map(str, ret)))
N, K, S = map(int,input().split()) L = [S]*K for i in range(N-K): if S < 10**9: L.append(S+1) else: L.append(1) print(' '.join(map(str, L)))
1
90,719,791,466,554
null
238
238
from sys import stdin, stderr from functools import lru_cache @lru_cache(maxsize=None) def rec(i, m): if i == 0: return m == 0 or A[i] == m return rec(i - 1, m) or rec(i - 1, m - A[i]) n = int(input()) A = [int(i) for i in input().split()] q = int(input()) M = [int(i) for i in input().split()] for m in M: if rec(n - 1, m): print('yes') else: print('no')
import itertools as it n=int(input()) A=input().split() q=int(input()) M=input().split() ans=[] cnt=0 A=list(map(int,A)) M=list(map(int,M)) for i in range(len(A)): Ac=list(it.combinations(A,i+1)) for j in Ac: ans.append(sum(j)) for i in M: flag=0 for j in ans: if i==j: print("yes") flag=1 break if flag==0: print("no")
1
103,667,542,108
null
25
25
n = int(input()) lst = [] for i in range(n): x, l = map(int, input().split()) left = x - l right = x + l lst.append([left, right]) lst = sorted(lst, key=lambda x: x[1]) ans = 1 limit = lst[0][1] for i in range(n): if lst[i][0] >= limit: ans += 1 limit = lst[i][1] print(ans)
def main(): n = int(input()) a_lst = list(map(int, input().split())) number = 1 count = 0 for i in range(n): if a_lst[i] == number: number += 1 else: count += 1 if 1 not in a_lst: count = -1 print(count) if __name__ == '__main__': main()
0
null
102,187,845,394,880
237
257
n = int(input()) count=0 for i in range(1,n//2+1): j = n - i if i != j: count += 1 print(count)
n = int(input()) if n-2 == 0: print(0) elif n%2 == 0: if n != 3: print(int(n/2)-1) else: print(1) else: print(int(n/2))
1
153,515,028,131,300
null
283
283
N, K = map(int, input().split()) A = [int(x) for x in input().split()] K = min(K, 41) # imos for _ in range(K): B = [0 for _ in range(N)] for i in range(N): l = max(0, i-A[i]) r = min(N-1, i+A[i]) B[l] += 1 if r+1 < N: B[r+1] -= 1 for i in range(1, N): B[i] += B[i-1] A = B print(*A)
import numpy as np from numba import njit @njit def op(A, n): B = np.zeros_like(A) for i, a in enumerate(A[:n]): B[max(i - a, 0)] += 1 B[min(n - 1, a + i) + 1] -= 1 return np.cumsum(B) def main(): n, k = list(map(int, input().split())) A = np.array(list(map(int, input().split())) + [0, ], dtype = np.int) for _ in range(k): A_new = op(A, n) if all(A[:n] == A_new[:n]): break A = A_new print(' '.join(list(map(str, A[:n])))) if __name__ == '__main__': main()
1
15,603,719,887,928
null
132
132
n, m=map(int, input().split()) x = list(map(int, input().split())) y = sorted(x, reverse=True) if y[m-1] >= sum(x)/(4*m): print('Yes') else: print('No')
n,m = map(int,input().split()) a = list(map(int,input().split())) sum = 0 cnt = 0 for i in a: sum += i for i in a: if(sum <= 4*m*i): cnt += 1 if(cnt >= m): print("Yes") else: print("No")
1
38,659,708,809,700
null
179
179
n=int(input()) tp = 0 hp = 0 for _ in range(n): taro,hanaco = input().split() if taro == hanaco : tp +=1 hp +=1 elif taro > hanaco : tp += 3 else: hp += 3 print('{} {}'.format(tp,hp))
xy = [map(int,input().split()) for i in range(2)] x,y = [list(j) for j in zip(*xy)] if (y[0] + 1) == y[1] : ans = 0 else: ans = 1 print(ans)
0
null
62,880,663,778,768
67
264
N = int(input()) A = map(int, input().split()) result = [0] * N for i, v in enumerate(A): result[v-1] = str(i + 1) print(' '.join(result))
# C - Go to School TLE N = int(input()) A = list(map(int, input().split())) attend = [i for i in range(1,N+1)] for j in range(1,N+1): attend[A[j-1] - 1] = j print(*attend)
1
180,814,755,397,056
null
299
299
s = input() if s == "ABC" : print("ARC") elif s == "ARC" : print("ABC")
s=input() if s=="ABC": print("ARC") else : print("ABC")
1
24,055,964,449,792
null
153
153
N=input().split() X=N[0] Y=N[1] Z=N[2] print(Z + ' ' + X + ' ' + Y)
a=input() a=a.split() a=(a[2],a[0],a[1]) print(*a)
1
38,282,606,804,000
null
178
178
H, W, K = map(int, input().split()) C = [list(input()) for _ in range(H)] ans = 0 # ビット探索 for i in range(2**H): for j in range(2**W): cnt = 0 for h in range(H): for w in range(W): if((i >> h) & 1) == 0 and ((j >> w) & 1) == 0: if C[h][w] == "#": cnt += 1 if cnt == K: ans += 1 print(ans)
H, W, K = map(int,input().split()) grid = [list(input()) for i in range(H)] total = 0 for mask1 in range(1<<H): for mask2 in range(1<<W): ct = 0 for i in range(H): for j in range(W): if grid[i][j]=='#' and not (1<<i)&mask1 and not (1<<j)&mask2: ct += 1 if ct==K: total += 1 print(total)
1
8,999,841,631,298
null
110
110
import math while True: n = int(input()) if n == 0: break s = list(map(int, input().split())) m = sum(s)/n v = 0 for i in s: v += (i - m) ** 2 ans = math.sqrt(v/n) print("{:.5f}".format(ans))
[N, R] = [int(i) for i in input().split()] print(R + 100*max(10-N, 0))
0
null
31,625,833,902,652
31
211
n=int(input()) a=list(map(int,input().split())) cnt=[0]*(max(a)+4) cnt[0]=3 mod=10**9+7 ans=1 for i in range(n): ans*=cnt[a[i]] ans%=mod cnt[a[i]]-=1 cnt[a[i]+1]+=1 print(ans)
N=int(input()) S,T=input().split() list=[] i=0 while i<N: list.append(S[i]) list.append(T[i]) i=i+1 print(*list, sep='')
0
null
120,912,089,557,600
268
255
import math def solve(x): y = 0 for i in range(n): if a[i] >= x / f[i]: y += math.ceil(a[i] - x / f[i]) if y <= k: return True else: return False def binary_search(c1, c2): m = (c1 + c2 + 1) // 2 if abs(c1 - c2) <= 1: return m else: if solve(m): c1 = m else: c2 = m return binary_search(c1, c2) n, k = map(int, input().split()) a = list(map(int, input().split())) f = list(map(int, input().split())) a.sort() f.sort(reverse = True) M = 0 for i in range(n): M = max(M, a[i] * f[i]) ans = binary_search(M, -1) print(ans)
# coding: utf-8 # hello worldと表示する #float型を許すな #numpyはpythonで import sys input = sys.stdin.readline sys.setrecursionlimit(10**7) from collections import Counter, deque from collections import defaultdict from itertools import combinations, permutations, accumulate, groupby, product from bisect import bisect_left,bisect_right from heapq import heapify, heappop, heappush from math import floor, ceil,pi,factorial,sqrt from operator import itemgetter def I(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(map(int, input().split())) def LI2(): return [int(input()) for i in range(n)] def MXI(): return [[LI()]for i in range(n)] def SI(): return input().rstrip() def printns(x): print('\n'.join(x)) def printni(x): print('\n'.join(list(map(str,x)))) inf = 10**17 mod = 10**9 + 7 def ceiler(a,b): return -((-a)//b) n,k=MI() costs=LI() difs=LI() costs.sort() difs.sort(reverse=True) lis=[0 for i in range(n)] #print(costs) #print(difs) for i in range(n): lis[i]=costs[i]*difs[i] #print(lis) def judge(x): times=0 for i in range(n): u=ceiler(lis[i]-x,difs[i]) times+=max(0,u) #print(u) #print(x,times<=k) return times<=k l,r=-1,10**13 while l+1<r: mid=(l+r)//2 if judge(mid): r=mid else: l=mid print(r)
1
165,233,980,835,260
null
290
290
h, w, k = map(int, input().split()) s = [list(map(int, list(input()))) for _ in range(h)] result = [] if h*w<=k: result.append(0) else: for i in range(2**(h-1)): checker, num, p = 0, i ,[0] for _ in range(h): p.append(p[-1]+num%2) checker += num%2 num >>= 1 x = 0 c = [0 for _ in range(checker+1)] for j in range(w): num = i nex = [0 for _ in range(checker+1)] for m in range(h): nex[p[m]] += s[m][j] if max(nex) > k: x = float('inf') break for m in range(checker+1): c[m] += nex[m] if c[m]>k: c = nex x += 1 break result.append(checker+x) print(min(result))
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 mod2 = 998244353 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def pe(s): return print(str(s), file=sys.stderr) def JA(a, sep): return sep.join(map(str, a)) def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a) def main(): h,w,k = LI() aa = [[int(c) for c in S()] for _ in range(h)] def f(hs): l = len(hs) c = [0] * l r = l - 1 for i in range(w): nc = [0] * l bf = False for j in range(l): for h in hs[j]: nc[j] += aa[h][i] if nc[j] > k: return -1 if nc[j] + c[j] > k: bf = True if bf: r += 1 c = nc else: for j in range(l): c[j] += nc[j] return r r = inf for bi in range(2**(h-1)): hs = [[0]] for i in range(h-1): if 2**i & bi > 0: hs.append([]) hs[-1].append(i+1) t = f(hs) if t >= 0 and r > t: r = t return r print(main())
1
48,649,739,576,282
null
193
193
N = int(input()) ans = 'No' if N<=82: for i in range(1,10): if N%i==0 and len(str(N//i))==1: ans = 'Yes' print(ans)
import math a, b, C = map(int, input().split()) rad = C/180*math.pi print('%.8f' % (a*b*math.sin(rad)/2)) print('%.8f' % ((a**2-2*a*b*math.cos(rad)+b**2)**0.5 + a + b)) print('%.8f' % (b * math.sin(C/180*math.pi)))
0
null
79,819,479,960,340
287
30
from collections import deque N,u,v = map(int,input().split()) u -= 1 v -= 1 adj = [ [] for _ in range(N) ] for _ in range(N-1): a,b = map(int,input().split()) a -= 1 b -= 1 adj[a].append(b) adj[b].append(a) def bfs(v): visited = [False] * N q = deque([v]) span = [-1] * N s = 0 while q: l = len(q) newq = deque([]) for _ in range(l): node = q.popleft() visited[node] = True span[node] = s for nei in adj[node]: if not visited[nei]: newq.append(nei) q = newq s += 1 return span t = bfs(u) a = bfs(v) ans = 0 for i in range(N): if t[i] <= a[i]: ans = max(ans, a[i]-1) print(ans)
import sys def input(): return sys.stdin.readline().strip() mod = 998244353 def main(): """ 各トークンを(最下点、最終的な高さ)に分けるのはできた。 そしてそれらを最下点位置が浅い順に並べるのも悪くはなかった、増加パートに関しては。 減少パートは減少度合が小さい順に付け加えたとしても高さが負に潜り込むケースがある。 (例)高さ3から下るとして、(-1, -1), (-2, 0), (-3, -2)が各トークンとすると この順にくっつけると(-3, -2)を加えるときにアウトだが、 (-2, 0), (-3, -2), (-1, -1)の順だったらOK なので下る場合には増加パートとは違う方法でくっつけないといけない。 結論としては、これは左右反転させれば増加していることになるので、右からくっつけるようにすればいい。 """ N = int(input()) S_up = [] S_down = [] for _ in range(N): s = input() max_depth = 0 height = 0 for c in s: if c == '(': height += 1 else: height -= 1 max_depth = min(max_depth, height) if height > 0: S_up.append((max_depth, height - max_depth)) else: S_down.append((-(height - max_depth), -max_depth)) S_up.sort(key=lambda x: (x[0], x[1])) S_down.sort(key=lambda x: (x[0], x[1])) height_left = 0 while S_up: d, h = S_up.pop() #print("({}, {})".format(d, h)) if height_left + d < 0: print("No") return height_left += d+h height_right = 0 while S_down: d, h = S_down.pop() #print("({}, {})".format(d, h)) if height_right + d < 0: print("No") return height_right += d+h if height_left != height_right: print("No") else: print("Yes") if __name__ == "__main__": main()
0
null
70,625,333,609,472
259
152
N=int(input()) L=list(map(int,input().split())) s=0 R=list() for i in range(N): s=s^L[i] for i in range(N): R.append(str(s^L[i])) print(" ".join(R))
import sys import math import string import fractions import random from operator import itemgetter import itertools from collections import deque import copy import heapq from bisect import bisect, bisect_left, bisect_right MOD = 10 ** 9 + 7 INF = float('inf') input = lambda: sys.stdin.readline().strip() sys.setrecursionlimit(10 ** 8) N, A, B = map(int, input().split()) dis = B - A - 1 if dis % 2 == 0: print(min((N - max(A, B) + 1) + (N - min(A, B) - (N - max(A, B) + 1)) // 2, min(A, B) + ((max(A, B) - min(A, B) - 1) // 2))) else: print((dis + 1) // 2)
0
null
61,140,711,659,192
123
253
n,k=map(int,input().split());print(sum(sorted(map(int,input().split()),reverse=True)[k:]))
n = int(input()) a = list(map(int, input().split())) ans = 0 maxi = 0 for i in range(n): if a[i] < maxi: ans += (maxi - a[i]) a[i] = maxi maxi = max(maxi, a[i]) print(ans)
0
null
41,809,920,149,910
227
88
def main(): k,n = map(int, input().split()) s = list(map(int, input().split())) l = [] for i in range(n): if i == 0: l.append(k - (s[-1] - s[0])) else: a = s[i] - s[i-1] l.append(a) print(k - max(l)) if __name__ == '__main__': main()
import sys import math import collections def set_debug(debug_mode=False): if debug_mode: fin = open('input.txt', 'r') sys.stdin = fin def int_input(): return list(map(int, input().split())) def get(s): return str(s % 9) + '9' * (s // 9) if __name__ == '__main__': # set_debug(True) # t = int(input()) t = 1 for ti in range(1, t + 1): n = int(input()) A = int_input() cur = 0 for x in A: cur = x ^ cur res = [] for x in A: res.append(cur ^ x) print(*res)
0
null
27,720,920,724,442
186
123
def resolve(): """ 典型的な区間スケジューリング問題 各スケジュールの終了時間が短いものから選択していけば, 選択するスケジュール数が最大になる. 今回は腕の可動範囲上限の小さいものから選択していく. """ import sys readline = sys.stdin.readline N = int(readline()) XL = [list(map(int, readline().split())) for _ in [0] * N] # 腕の可動範囲の(上限, 下限)のリスト生成 t = [(x + length, x - length) for x, length in XL] # 複数要素を含む要素のsortではインデックスの小さい要素から比較していきソート t.sort() # リストを腕の上限の昇順(小さい順)でソート time = -float('inf') # time=0でもおk ans = 0 for i in range(N): end, start = t[i] # tはソート済故,上限が小さい順に選出される if time <= start: # 腕の下限が既存の腕と重ならないか判定 ans += 1 time = end # 現在の腕の上限を記録 print(ans) if __name__ == "__main__": resolve()
N = int(input()) ans = [0]*10010 for x in range(1, 105): for y in range(1, 105): for z in range(1, 105): v = x*x + y*y + z*z + x*y + y*z + z*x if v <= 10005: ans[v] += 1 for i in range(1, N+1): print(ans[i])
0
null
49,009,307,040,650
237
106
X = int(input()) num = X//100 amari = X%100 if num*5 >= amari: print(1) else: print(0)
x = int(input()) print(1 if x-x//100*100 <= x//100*5 else 0)
1
126,971,288,640,540
null
266
266
import sys input = sys.stdin.readline I=lambda:int(input()) MI=lambda:map(int,input().split()) LI=lambda:list(map(int,input().split())) from collections import deque res=0 INF=10**9 N,u,v=MI() G=[[] for _ in [0]*(N+1)] for i in range(N-1): a,b=MI() G[a].append(b) G[b].append(a) def bfs(a): q=deque() q.append(a) d=[INF]*(N+1) d[a]=0 res=0 while q: r=q.popleft() for nr in G[r]: if d[nr]==INF: q.append(nr) d[nr]=d[r]+1 res+=1 return d aoki=bfs(v) taka=bfs(u) m=0 for i in range(1,N+1): if taka[i]<aoki[i]: m=max(aoki[i],m) print(m-1)
#! python3 # card_game.py n = int(input()) turns = [input() for i in range(n)] taro, hanako = 0, 0 for turn in turns: t, h = turn.split(' ') min_len = len(t) if len(t) <= len(h) else len(h) win_flag = 0 # -1: taro, 1: hanako, 0: draw for i in range(min_len): if ord(t[i]) < ord(h[i]): win_flag = 1 break elif ord(h[i]) < ord(t[i]): win_flag = -1 break if win_flag == 0: if len(t) < len(h): win_flag = 1 elif len(h) < len(t): win_flag = -1 if win_flag == -1: taro += 3 elif win_flag == 1: hanako += 3 else: taro += 1 hanako += 1 print(taro, hanako)
0
null
59,899,534,572,302
259
67
N, K = map(int, input().split()) P = list(map(lambda x: int(x) - 1, input().split())) C = list(map(int, input().split())) maxscore = -(10 ** 18) for st in range(N): cumsum = [0] now = st while(P[now] != st): now = P[now] cumsum.append(cumsum[-1] + C[now]) cumsum.append(cumsum[-1] + C[st]) m = len(cumsum) - 1 loopsum = cumsum[-1] if loopsum < 0: maxscore = max(maxscore, max(cumsum[1:min(K + 1, m + 1)])) continue score = (K // m - 1) * loopsum extended_cumsum = cumsum + [cumsum[i] + loopsum for i in range(1, K % m + 1)] score += max(extended_cumsum) maxscore = max(maxscore, score) print(maxscore)
N,K = map(int,input().split()) P = list(map(int,input().split())) C = list(map(int,input().split())) ans = -1*float('inf') for i in range(N): k = i Cc = [] Ct = 0 while 1: k = P[k]-1 Cc.append(C[k]) if i == k: break l = len(Cc) Ct = sum(Cc) t = 0 for j in range(l): t += Cc[j] if j >= K:break now = t if Ct > 0: e = (K-j-1)//l now += Ct*e ans = max(ans,now) print(ans)
1
5,409,584,836,732
null
93
93
s = input() t = s[::-1] n = len(s) resid = [0] * 2019 resid[0] = 1 csum = 0 powoften = 1 for i in range(n): csum = (csum + int(t[i]) * powoften) % 2019 powoften = (10 * powoften) % 2019 resid[csum] += 1 ans = 0 for i in range(2019): ans += resid[i] * (resid[i] - 1) // 2 print(ans)
S=input() n=len(S) a=[0]*2019 MOD=2019 current=0 a[0]=1 for i in range(n): current=(current+int(S[n-1-i])*pow(10,i,MOD))%MOD a[current]+=1 ans=0 for i in range(2019): ans+=(a[i]*(a[i]-1))//2 print(ans)
1
30,952,826,605,492
null
166
166
import sys K = int(sys.stdin.readline()) S = sys.stdin.readline().rstrip('\n') # ## COMBINATION (MOD) ### N_MAX = 10**6 # 問題サイズに合わせて変えておく MOD = 10**9 + 7 inv = [0] * (N_MAX + 2) inv[0] = 0 # 逆元テーブル計算用テーブル inv[1] = 1 for i in range(2, N_MAX + 2): q, r = divmod(MOD, i) inv[i] = -inv[r] * q % MOD # K 文字追加 ans = 0 ln = len(S) p = pow(26, K, MOD) for i in range(1, K + 2): ans += p % MOD ans %= MOD # pre p = p * (ln + i - 1) * inv[i] * 25 * inv[26] % MOD # suf # s2 = (s2 * inv[26]) % MOD print(ans)
def main(): n = int(input()) s = list(input()) for i in range(len(s)): s[i] = chr((ord(s[i]) + n - ord("A")) % 26 + ord("A")) print(*s, sep="") if __name__ == "__main__": main()
0
null
73,420,784,612,462
124
271
n = input() a = list(map(int, input().split())) print(' '.join(map(str, reversed(a))))
n = input() a = map(int, raw_input().split()) s = "" for i in range(-1, -n, -1): s += str(a[i]) + " " s += str(a[-n]) print(s)
1
978,079,754,498
null
53
53
#D N=int(input()) ans=0 D=[[0 for i in range(10)] for j in range(10)] for i in range(1,10): for j in range(1,10): for k in range(1,N+1): if str(i)==str(k)[0] and str(j)==str(k)[-1]: D[i][j]+=1 for i in range(1,10): for j in range(1,10): ans+=D[i][j]*D[j][i] print(ans)
def main(): N = int(input()) ctr = [[0] * 10 for _ in range(10)] for x in range(1, N + 1): s = str(x) ctr[int(s[0])][int(s[-1])] += 1 ans = 0 for i in range(10): for j in range(10): ans += ctr[i][j] * ctr[j][i] print(ans) if __name__ == '__main__': main()
1
86,472,745,233,280
null
234
234
#!/usr/bin/env python3 import collections as cl import sys def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) MOD = 10 ** 9 + 7 def main(): N = II() targets = LI() colors = [0] * 3 ans = 1 for i in range(N): count = 0 for k in range(3): if colors[k] == targets[i]: if count == 0: colors[k] += 1 count += 1 ans *= count ans %= MOD print(ans) main()
n, k = map(int, input().split()) a = list(map(int, input().split())) mod = 10 ** 9 + 7 plus = [] minus = [] for i in a: if i >= 0: plus.append(i) if i < 0: minus.append(-i) plus.sort(reverse=True) minus.sort(reverse=True) ans = 1 if k == n: for x in a: ans = (ans * x) % mod elif n == len(plus): for x in plus[:k]: ans = (ans * x) % mod elif n == len(minus): if k % 2 == 1: ans = -1 minus.sort() for x in minus[:k]: ans = (ans * x) % mod else: i, j = 0, 0 if k % 2 == 1: ans = plus[0] i += 1 while i + j != k: x_p = plus[i] * plus[i + 1] if i < len(plus) - 1 else 0 x_m = minus[j] * minus[j + 1] if j < len(minus) - 1 else 0 if x_p > x_m: ans = (ans * x_p) % mod i += 2 else: ans = (ans * x_m) % mod j += 2 print(ans)
0
null
69,435,616,129,190
268
112
i = raw_input().strip().split() a = int(i[0]) b = int(i[1]) c = int(i[2]) if a < b and b < c: print "Yes" else: print "No"
h, w = map(int, input().split()) s = [list(map(str, input().rstrip())) for _ in range(h)] dp = [[10001] * w for _ in range(h)] if s[0][0] == "#": dp[0][0] = 1 else: dp[0][0] = 0 moves = [[1, 0], [0, 1]] for y in range(h): for x in range(w): for m in moves: ny, nx = y + m[0], x + m[1] if ny >= h or nx >= w: continue add = 0 # .から#に突入する時だけカウントが増える if s[y][x] == "." and s[ny][nx] == "#": add = 1 dp[ny][nx] = min(dp[ny][nx], dp[y][x] + add) print(dp[-1][-1])
0
null
24,680,764,420,012
39
194
import math a,b,c = map(float,input().split()) rad = math.radians(c) x = (a**2+b**2-2*a*b*math.cos(rad))**(1/2) L = a+b+x S = a*b*math.sin(rad)/2 h = 2*S/a print("{:.8f}\n{:.8f}\n{:.8f}".format(S,L,h))
#事前に階乗を計算しておく maximにnの最大値をいれる maxim = 10**6+1 MOD = 10**9+7 kaijou = [1]*(maxim) for i in range(1,maxim): kaijou[i]=(kaijou[i-1]*i)%MOD #nCr def nCr(n,r): if n < r: return 0 return ((kaijou[n]*pow(kaijou[r],MOD-2,MOD))%MOD*pow(kaijou[n-r],MOD-2,MOD))%MOD #nHr def nHr(n,r): if r == 0: if n == 0: return 1 return 0 return ((kaijou[n+r-1]*pow(kaijou[n],MOD-2,MOD))%MOD*pow(kaijou[r-1],MOD-2,MOD))%MOD N = int(input()) ans = 0 for i in range(1,(N//3)+1): tmp = nHr(N-3*i,i) ans = (ans+tmp)%MOD print(ans)
0
null
1,720,911,745,058
30
79
import sys read = sys.stdin.read readlines = sys.stdin.readlines from math import pi def main(): n, *a = map(int, read().split()) cnt = [0] * n for ae in a: cnt[ae-1] += 1 print(*cnt, sep='\n') if __name__ == '__main__': main()
N = int(input()) a = list(map(int,(input().split()))) list_a = [0]*2*100001 for i in a: list_a[i] += 1 for i in range(1,N+1): print(list_a[i])
1
32,589,068,522,432
null
169
169
n, k = list(map(int, input().split())) max_len = 2 * n - 1 # 適宜変更する mod = 10**9 + 7 def modinv(x): ''' xの逆元を求める。フェルマーの小定理より、 x の逆元は x ^ (mod - 2) に等しい。計算時間はO(log(mod))程度。 Python標準のpowは割った余りを出すことも可能。 ''' return pow(x, mod-2, mod) # 二項係数の左側の数字の最大値を max_len とする。nとかだと他の変数と被りそうなので。 # factori_table = [1, 1, 2, 6, 24, 120, ...] 要は factori_table[n] = n! # 計算時間はO(max_len * log(mod)) modinv_table = [-1] * (max_len + 1) modinv_table[0] = None # 万が一使っていたときにできるだけ早期に原因特定できるようにしたいので、Noneにしておく。 factori_table = [1] * (max_len + 1) factori_inv_table = [1] * (max_len + 1) for i in range(1, max_len + 1): factori_table[i] = factori_table[i-1] * (i) % mod modinv_table[1] = 1 for i in range(2, max_len + 1): modinv_table[i] = (-modinv_table[mod % i] * (mod // i)) % mod factori_inv_table[i] = factori_inv_table[i-1] * modinv_table[i] % mod def binomial_coefficients(n, k): ''' n! / (k! * (n-k)! ) 0 <= k <= nを満たさないときは変な値を返してしまうので、先にNoneを返すことにする。 場合によっては0のほうが適切かもしれない。 ''' if not 0 <= k <= n: return None return (factori_table[n] * factori_inv_table[k] * factori_inv_table[n-k]) % mod def binomial_coefficients2(n, k): ''' (n * (n-1) * ... * (n-k+1)) / (1 * 2 * ... * k) ''' ans = 1 for i in range(k): ans *= n-i ans *= modinv_table[i + 1] ans %= mod return ans if k >= n-1: # nHn = 2n-1 C n print(binomial_coefficients(2 * n - 1, n)) else: # 移動がk回←→ 人数0の部屋がk個以下 # 人数0の部屋がちょうどj個のものは # nCj(人数0の部屋の選び方) * jH(n-j) (余剰のj人を残りの部屋に入れる) ans = 0 for j in range(k+1): if j == 0: ans += 1 else: ans += binomial_coefficients(n, j) * binomial_coefficients(n-1, j) ans %= mod print(ans)
import sys def input(): return sys.stdin.readline().strip() def mapint(): return map(int, input().split()) sys.setrecursionlimit(10**9) T1, T2 = mapint() A1, A2 = mapint() B1, B2 = mapint() if not (A1-B1)*(A2-B2)<0: print(0) else: if A1<B1: A1, B1 = B1, A1 A2, B2 = B2, A2 if A1*T1+A2*T2==B1*T1+B2*T2: print('infinity') exit(0) rest = (A1-B1)*T1 come = (B2-A2)*T2 if come<rest: print(0) else: ans = come//(come-rest) last = 1 if come%(come-rest)==0 else 0 print(ans*2-1-last)
0
null
99,172,950,297,512
215
269
N, M = [int(n) for n in input().split(" ")] def comb(n): if n <= 1: return 0 return n * (n - 1) // 2 print(comb(N) + comb(M))
import bisect,collections,copy,heapq,itertools,math,string import sys def S(): return sys.stdin.readline().rstrip() def M(): return map(int,sys.stdin.readline().rstrip().split()) def I(): return int(sys.stdin.readline().rstrip()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LS(): return list(sys.stdin.readline().rstrip().split()) n, m = M() print(n*(n-1)//2 + m*(m-1)//2)
1
45,583,169,021,170
null
189
189
a,b,c,k=map(int,input().split()) if k < a: print(k) elif k <= a+b: print(a) else: print(2*a+b-k)
A, B, C, K = map(int, input().split()) k = K a = min(A, k) k -= a b = min(B, k) k -= b c = min(C, k) print(a-c)
1
21,792,188,500,378
null
148
148
import os, sys, re, math N = (int(input())) S = input() T = S.replace('ABC', '') print((len(S) - len(T)) // 3)
import sys def I(): return int(sys.stdin.readline().rstrip()) def LS2(): return list(sys.stdin.readline().rstrip()) #空白なし N = I() S = LS2() ans = 0 for i in range(N-2): if S[i] == 'A' and S[i+1] == 'B' and S[i+2] == 'C': ans += 1 print(ans)
1
99,233,947,961,828
null
245
245
from collections import deque N, M = map(int, input().split()) edge = [[] for _ in range(N)] sign = [-1] * N for _ in range(M): A, B = map(int, input().split()) edge[A - 1].append(B - 1) edge[B - 1].append(A - 1) d = deque([0]) while d: cur = d.popleft() for adj in edge[cur]: if sign[adj] == -1: sign[adj] = cur d.append(adj) print("Yes") for i in range(1, N): print(sign[i] + 1)
#E H,N=map(int,input().split()) A=[0 for i in range(N)] dp=[float("inf") for i in range(10**4+1)] dp[0]=0 for i in range(N): a,b=map(int,input().split()) A[i]=a for j in range(10**4+1): if j+a>10**4: break dp[j+a]=min(dp[j]+b,dp[j+a]) if H+max(A)>10**4: print(min(dp[H:])) else: print(min(dp[H:H+max(A)]))
0
null
50,604,506,901,458
145
229
R=int(input()) print(2*3.14159265*R)
import bisect def main(): N = int(input()) L = list(map(int, input().split())) L.sort() ans = 0 for a_idx in range(N-2): for b_idx in range(a_idx+1, N-1): l_idx = b_idx + 1 r_idx = bisect.bisect_left(L, L[a_idx] + L[b_idx]) ans += r_idx - l_idx print(ans) if __name__ == '__main__': main()
0
null
101,289,078,690,368
167
294
# -*- coding: utf-8 -*- import io import sys import math def solve(): # implement process pass def main(): # input s,t= input().split() # process ans = t+s # output print(ans) return ans ### DEBUG I/O ### _DEB = 0 # 1:ON / 0:OFF _INPUT = """\ humu humu """ _EXPECTED = """\ humuhumu """ def logd(str): """usage: if _DEB: logd(f"{str}") """ if _DEB: print(f"[deb] {str}") ### MAIN ### if __name__ == "__main__": if _DEB: sys.stdin = io.StringIO(_INPUT) print("!! Debug Mode !!") ans = main() if _DEB: print() if _EXPECTED.strip() == ans.strip(): print("!! Success !!") else: print(f"!! Failed... !!\nANSWER: {ans}\nExpected: {_EXPECTED}")
a,b=(input().split()) print(b+a)
1
103,141,171,373,692
null
248
248
K=int(input()) a=7 for i in range(1,K+1): if a%K==0: print(i) exit() a=(a*10+7)%K print(-1)
k = int(input()) if(k%2 == 0 or k%5 == 0): print('-1') else: if(k%7 == 0): k = k // 7 num = 1 ans = 1 while(True): if(num%k == 0): print(ans) break else: num %= k num *= 10 num += 1 ans += 1
1
6,109,312,844,532
null
97
97
MOD = 10 ** 9 + 7 INF = 10 ** 11 import sys sys.setrecursionlimit(100000000) N = int(input()) ans = [] def dfs(i,standard): if i == N: ans.append(standard) return before_M = max(ord(s) for s in standard) for j in range(97,before_M + 2): dfs(i + 1,standard + chr(j)) dfs(1,'a') print('\n'.join(ans))
n = map(int, raw_input().split(' '))[0] def dfs(i, mx, n, res, cur = []): if i==n: res.append(''.join(cur[::])) return for v in range(0, mx + 1): dfs(i + 1, mx + (1 if v == mx else 0), n , res, cur + [chr(v+ ord('a'))]) res =[] dfs(0,0,n,res) for w in res: print w
1
52,396,377,006,600
null
198
198
N = int(input()) if N%2 != 0: print("No") exit() S=input() N=N//2 if (S[:N]==S[-N:]): print("Yes") else: print("No")
n = int(input()) s = input() x = n // 2 print(["Yes", "No"][n % 2 != 0 or s[:x] != s[x:]])
1
147,120,118,335,200
null
279
279
n,*d = map(int,open(0).read().split()) print(sum((sum(d)-i)*i for i in d)//2)
N = int(input()) d = list(map(int, input().split())) import itertools ans = 0 pairs = list(itertools.combinations(d, 2)) for pair in pairs: ans += pair[0] * pair[1] print(ans)
1
168,320,760,302,630
null
292
292
n = int(input()) x = int(input(),2) def popcount(l): return bin(l).count("1") def count(l): res = 0 while l != 0: p = popcount(l) l %= p res += 1 return res m = popcount(x) x_plus = x%(m+1) if m != 1: x_minus = x%(m-1) else: x_minus = 1 lis_plus = [0]*n lis_minus = [0]*n for i in range(n): if i == 0: lis_plus[i] = 1%(m+1) if m != 1: lis_minus[i] = 1%(m-1) else: lis_plus[i] = lis_plus[i-1]*2%(m+1) if m != 1: lis_minus[i] = lis_minus[i-1]*2%(m-1) for i in range(n): if (x >> (n-i-1)) & 1: if m == 1: print(0) else: print(count((x_minus - lis_minus[-i-1])%(m-1)) + 1) else: print(count((x_plus + lis_plus[-i-1])%(m+1)) + 1)
n = int(input()) x = list(input()) def popcount(n): bin_n = bin(n)[2:] count = 0 for i in bin_n: count += int(i) return count cnt = 0 for i in range(n): if x[i] == '1': cnt += 1 plus = [0 for i in range(n)] # 2^index を cnt+1 で割った時のあまり minus = [0 for i in range(n)] # 2^index を cnt-1 で割った時のあまり if cnt == 0: plus[0] = 0 else: plus[0] = 1 if cnt != 1: minus[0] = 1 for i in range(1, n): plus[i] = (plus[i-1]*2) % (cnt+1) if cnt != 1: minus[i] = (minus[i-1]*2) % (cnt-1) origin = int(''.join(x), base=2) amariplus = origin % (cnt+1) if cnt != 1: amariminus = origin % (cnt-1) for i in range(n): if x[i] == '0': amari = (amariplus + plus[n-i-1]) % (cnt+1) else: if cnt != 1: amari = (amariminus - minus[n-i-1]) % (cnt-1) else: print(0) continue ans = 1 while amari != 0: ans += 1 amari = amari % popcount(amari) print(ans)
1
8,196,830,121,532
null
107
107
# -*- coding: utf-8 -*- N = int(input()) X = list(map(int, input().split())) min_x = X[0] max_x = X[0] for i in range(N): if X[i] > max_x: max_x = X[i] if X[i] < min_x: min_x = X[i] ans = 10000000 for p in range(min_x, max_x+1): ans_p = 0 for x in X: ans_p += (x-p)**2 if ans > ans_p: ans = ans_p print(ans)
import math r = float(input()) print('{0:.6f} {1:.6f}'.format(r * r * math.pi, (r + r) * math.pi))
0
null
32,735,152,674,560
213
46
from collections import Counter,defaultdict,deque from heapq import heappop,heappush,heapify import sys,bisect,math,itertools,fractions,pprint,time,random sys.setrecursionlimit(10**8) mod = 10**9+7 INF = float('inf') def inp(): return int(sys.stdin.readline()) def inpl(): return list(map(int, sys.stdin.readline().split())) def cal(T): res = 0 last = [0] * 26 for day in range(D): t = T[day]; t -= 1 last[t] = 0 res += s[day][t] for i in range(26): if i == t: continue last[i] += 1 res -= c[i] * last[i] return res start_time = time.time() n = 26 D = inp() c = inpl() s = [inpl() for _ in range(D)] last = [0] * n res = [-1] * D for d in range(D): ans = -1 mx = -INF for i in range(n): now = s[d][i] for j in range(n): if i == j: continue now -= s[d][j] * (last[j]+1) if now > mx: mx = now ans = i res[d] = ans+1 for j in range(n): last[j] += 1 if j == ans: last[j] = 0 score = cal(res) # print(now_score) while time.time() - start_time < 1.83: td,tq = random.randrange(0,D), random.randrange(1,n+1) old_q = res[td] res[td] = tq now = cal(res) if now > score: score = now else: res[td] = old_q for x in res: print(x)
import sys input = sys.stdin.readline import numpy as np from numba import njit def read(): D = int(input().strip()) C = np.fromstring(input().strip(), dtype=np.int32, sep=" ") S = np.empty((D, 26), dtype=np.int32) for i in range(D): s = np.fromstring(input().strip(), dtype=np.int32, sep=" ") S[i, :] = s[:] M = 1000 RD = np.random.randint(D, size=(M*2, ), dtype=np.int32) RQ = np.random.randint(26, size=(M*2, ), dtype=np.int32) DQ = np.stack([RD, RQ]).T return D, C, S, M, DQ @njit def diff_satisfaction(C, S, d, p, last): """d日目にコンテストpを開催するときの、満足度の更新量を求める """ v = 0 for i in range(26): v -= C[i] * (d - last[i]) v += C[p] * (d - last[p]) v += S[d, p] return v @njit def change_schedule(D, C, S, T, d, q, cumsat): """d日目のコンテストをqに変更する """ p = T[d] dp1, dq1 = -1, -1 dp3, dq3 = D, D for i in range(0, d): if T[i] == p: dp1 = i if T[i] == q: dq1 = i for i in range(D-1, d, -1): if T[i] == p: dp3 = i if T[i] == q: dq3 = i cumsat = cumsat - S[d, p] + S[d, q] - C[p] * (dp3-d) * (d-dp1) + C[q] * (dq3-d) * (d-dq1) return cumsat @njit def greedy(D, C, S): T = np.zeros(D, dtype=np.int32) last = -np.ones(26, dtype=np.int32) cumsat = 0 for d in range(D): max_p = 0 max_diff = -999999999 # select contest greedily for p in range(26): diff = diff_satisfaction(C, S, d, p, last) if diff > max_diff: max_p = p max_diff = diff # update schedule cumsat += max_diff T[d] = max_p last[max_p] = d return cumsat, T @njit def solve(D, C, S, M, DQ): cumsat, T = greedy(D, C, S) for i in range(M): d1, q1 = DQ[i*2, :] d2, q2 = DQ[i*2+1, :] newsat = cumsat newsat = change_schedule(D, C, S, T, d1, q1, newsat) t1 = T[d1] T[d1] = q1 newsat = change_schedule(D, C, S, T, d2, q2, newsat) t2 = T[d2] T[d2] = q2 if newsat > cumsat: cumsat = newsat else: T[d2] = t2 T[d1] = t1 for t in T: print(t+1) if __name__ == '__main__': inputs = read() outputs = solve(*inputs) if outputs is not None: print("%s" % str(outputs))
1
9,687,495,312,118
null
113
113
def GSD(x, y): while True: x0 = max(x, y) y0 = min(x, y) if x0 % y0 == 0: return y0 x = x0 % y0 y = y0 x, y = map(int, input().split(' ')) print(GSD(x, y))
def gcd(x, y): '''?????????????????????????????? x>=y?????¨??????gcd(x, y)??¨gcd(y, x%y)?????????????????¨????????¨??????''' if x < y: x, y = y, x while y > 0: x, y = y, x % y return x def test(): '''??? input = 147 105 -> 21''' x, y = map(int, input().split()) result = gcd(x, y) print(result) if __name__ == '__main__': test()
1
8,502,536,950
null
11
11
import itertools def abc167c_skill_up(): n, m, x = map(int, input().split()) c = [] a = [] for _ in range(n): v = list(map(int, input().split())) c.append(v[0]) a.append(v[1:]) pattern = itertools.product([0,1], repeat=n) best = float('inf') for p in pattern: cost = 0 skill = [0] * m for i, v in enumerate(p): if v == 1: cost += c[i] if cost > best: break check = True for j in range(m): skill[j] += a[i][j] if skill[j] < x: check = False if check: best = cost break if best == float('inf'): print('-1') else: print(best) abc167c_skill_up()
n,k=map(int,input().split()) ans=n%k if ans>k//2: print(abs(ans-k)) elif ans<=k//2: print(ans)
0
null
30,841,604,494,012
149
180
a, b, c, k = map(int, input().split()) if k <= a: print(k) else: if k <= a + b: print(a) else: print(2 * a + b - k)
N = int(input()) ans = 0 ans = -(-N//2) print(ans)
0
null
40,192,207,067,408
148
206
import math H, W = map(int, raw_input().split()) while H > 0 or W > 0 : print "#" * W for i in xrange(H-2) : print "#" + "." * (W-2) + "#" print "#" * W print H, W = map(int, raw_input().split())
Flag = True data = [] while Flag: H, W = map(int, input().split()) if H == 0 and W == 0: Flag = False else: data.append((H, W)) #print(type(W)) for (H, W) in data: for i in range(H): if i == 0 or i == H-1: print('#' * W) else: print('#' + '.' * (W-2) + '#') print('\n', end="")
1
841,399,834,850
null
50
50
n = int(input()) P = list(map(int,input().split())) k=0 bc=0 c=0 i=0 while k < n : if P[k]!=i+1 : bc+=1 else: i+=1 c+=1 k+=1 if c==0 : bc = -1 print(bc)
n,k=map(int,input().split()) li=list(map(int,input().split())) li_sort=sorted(li) ans=0 for i in range(k): ans+=li_sort[i] print(ans)
0
null
62,985,599,838,890
257
120
n, k = map(int, input().split()) mod = 10 ** 9 + 7 class ModInt: def __init__(self, num, mod): self.num = num self.mod = mod def __str__(self): return str(self.num) def __repr__(self): return "ModInt(num: {}, mod: {}".format(self.num, self.mod) def __add__(self, other): ret = self.num + other.num ret %= self.mod return ModInt(ret, self.mod) def __sub__(self, other): ret = self.num - other.num ret %= self.mod return ModInt(ret, self.mod) def __mul__(self, other): ret = self.num * other.num ret %= self.mod return ModInt(ret, self.mod) def pow(self, times): pw = pow(self.num, times, self.mod) return ModInt(pw, self.mod) def inverse(self): return self.pow(self.mod - 2) def __truediv__(self, other): num = self * other.inverse() return ModInt(num, self.mod) class Combination: def __init__(self, n, mod): self.mod = mod self.fact = [ModInt(1, mod)] self.inverse = [ModInt(1, mod)] for i in range(1, n + 1): self.fact.append(self.fact[-1] * ModInt(i, mod)) self.inverse.append(self.inverse[-1] * ModInt(i, mod).inverse()) def comb(self, n, k): if k < 0 or n < k: return ModInt(0, self.mod) return self.fact[n] * self.inverse[k] * self.inverse[n-k] mx = min(k, n - 1) comb = Combination(n, mod) ans = ModInt(0, mod) for i in range(mx + 1): ans += comb.comb(n, i) * comb.comb(n - 1, i) print(ans)
#k = int(input()) #s = input() #a, b = map(int, input().split()) #l = list(map(int, input().split())) l = list(map(int, input().split())) if (l[0] == l[1] != l[2]): print("Yes") elif (l[0] == l[2] != l[1]): print("Yes") elif (l[0] != l[1] == l[2]): print("Yes") else: print("No")
0
null
67,871,271,046,270
215
216
import sys if __name__ == "__main__": n = int(input()) if n == 0 or n == 1: print(1) sys.exit(0) dp = [0] * (n + 1) dp[0] = 1 dp[1] = 1 for i in range(2, n + 1): dp[i] = dp[i - 1] + dp[i - 2] print(dp[n])
# -*- coding: utf-8 -*- fibs = {0: 1, 1: 1} def fib(n): if n not in fibs: fibs[n] = fib(n - 1) + fib(n - 2) return fibs[n] if __name__ == '__main__': n = int(input()) print(fib(n))
1
1,862,012,800
null
7
7
n = int(input()) cl = str(input()) cl_R = cl.count("R") # print(cl_R) cl_leftR = cl[:cl_R].count("R") # print(cl_leftR) print(cl_R-cl_leftR)
a = [[0] * 3 for x in range(3)] s = [[0] * 3 for x in range(3)] for i in range(3): a[i] = [int(x) for x in input().split()] n = int(input()) b = [] for i in range(n): b.append(int(input())) for i in range(n): for j in range(3): for k in range(3): if b[i] == a[j][k]: s[j][k] = 1 res = "No" if s[0][0] == 1 and s[0][1] == 1 and s[0][2] == 1: res = "Yes" if s[1][0] == 1 and s[1][1] == 1 and s[1][2] == 1: res = "Yes" if s[2][0] == 1 and s[2][1] == 1 and s[2][2] == 1: res = "Yes" if s[0][0] == 1 and s[1][0] == 1 and s[2][0] == 1: res = "Yes" if s[0][1] == 1 and s[1][1] == 1 and s[2][1] == 1: res = "Yes" if s[0][2] == 1 and s[1][2] == 1 and s[2][2] == 1: res = "Yes" if s[0][0] == 1 and s[1][1] == 1 and s[2][2] == 1: res = "Yes" if s[2][0] == 1 and s[1][1] == 1 and s[0][2] == 1: res = "Yes" print(res)
0
null
33,122,142,216,450
98
207
s = '' while True: try: s += input().lower() except EOFError: break for i in range(97, 123): print(chr(i)+' : '+str(s.count(chr(i))))
al = 'abcdefghijklmnopqrstuvwxyz' text = '' while True: try: text += input().lower() except EOFError: break for i in al: print('{} : {}'.format(i, text.count(i)))
1
1,651,006,460,050
null
63
63
#ABC161-A x,y,z=map(int,input().split()) print(z,x,y)
import sys input = sys.stdin.readline ins = lambda: input().rstrip() ini = lambda: int(input().rstrip()) inm = lambda: map(int, input().split()) inl = lambda: list(map(int, input().split())) out = lambda x: print('\n'.join(map(str, x))) x, y, z = inm() print(z, x, y)
1
38,081,172,492,918
null
178
178
import copy from collections import deque n, m, q = list(map(int, input().split())) abcd = [] for _ in range(q): abcd.append(list(map(int, input().split()))) ans = 0 def dfs(A, low, high, n): global ans if len(A) >= n: result = 0 for a,b,c,d in abcd: if A[b-1]-A[a-1] == c: result += d ans = max(ans, result) return for i in range(low, high+1): A.append(i) dfs(A, i, high, n) A.pop() dfs([], 1, m, n) print(ans)
from itertools import combinations_with_replacement N, M, Q = map(int, input().split()) abcds = [tuple(map(int, input().split()))for _ in range(Q)] def cal_score(A): score = 0 for a, b, c, d in abcds: if A[b-1] - A[a-1] == c: score += d return score ans = 0 for A in combinations_with_replacement([i for i in range(M)], N): ans = max(ans, cal_score(A)) print(ans)
1
27,448,105,601,228
null
160
160
#! python3 # linear_search.py n = int(input()) S = [int(x) for x in input().split(' ')] q = int(input()) T = [int(x) for x in input().split(' ')] c = 0 for t in T: for s in S: if t == s: c += 1 break print(c)
#---------------for input-------------------- numberN=int(input()) arrayS=list(map(int,input().split())) numberQ=int(input()) arrayT=list(map(int,input().split())) #---------------for input---------------------- def linearSearch(arrayS,arrayT,numberQ): machedNumber=0 index=0 while index<numberQ: if arrayT[index] in arrayS: machedNumber+=1 index+=1 print(machedNumber) #----------------for main------------------------ linearSearch(arrayS,arrayT,numberQ) #----------------for main------------------------
1
68,846,869,348
null
22
22
R, C, K = map(int, input().split()) # = int(input()) p = [] for k in range(K): p.append(list(map(int, input().split()))) maps = [[0 for _ in range(C)] for _ in range(R)] for k in range(K): maps[p[k][0]-1][p[k][1]-1] += p[k][2] point1 = [[0 for _ in range(C)] for _ in range(R)] point2 = [[0 for _ in range(C)] for _ in range(R)] point3 = [[0 for _ in range(C)] for _ in range(R)] point1[0][0] = maps[0][0] for r in range(R): for c in range(C): a, b, d = point1[r][c], point2[r][c], point3[r][c] if c < C - 1: x = maps[r][c+1] point1[r][c+1] = max(point1[r][c+1], x, a) point2[r][c+1] = max(point2[r][c+1], a + x, b) point3[r][c+1] = max(point3[r][c+1], b + x, d) if r < R - 1: point1[r+1][c] = maps[r+1][c] + max(a, b, d) print(max(point1[-1][-1], point2[-1][-1], point3[-1][-1]))
n,m=map(int,input().split()) ans=["#"]*n for _ in range(m): s,c=map(int,input().split()) # 同じ桁に複数の指示が飛んできたら狩猟 if not ans[s-1] in["#",c]: print(-1) exit() ans[s-1]=c # nが一桁の時の対応 if len(ans)==1: print(0 if ans[0]=="#" else ans[0]) exit() #頭の数字について if ans[0]==0: print(-1) exit() if ans[0]=="#": ans[0]=1 for num in ans: print(num if num!="#" else 0,end="")
0
null
32,978,786,174,068
94
208
from itertools import accumulate n = int(input()) A = list(map(int,input().split())) B = list(accumulate(A)) m = float("inf") for i in B: m = min(m,abs(2*i-B[-1])) print(m)
N = int(input()) lst = [int(input()) for i in range(N)] def insertion_sort(A, n, g): global cnt for i in range(g, n): v = A[i] j = i - g while(j >= 0 and A[j] > v): A[j+g] = A[j] j = j - g cnt += 1 A[j+g] = v def shell_sort(A, n, m, G): for i in range(m): insertion_sort(A, n, G[i]) cnt = 0 G = [1] for i in range(99): if N < 1 + 3*G[-1]: break G.append(1 + 3*G[-1]) m = len(G) G.reverse() shell_sort(lst, N, m, G) print(m) print(" ".join(list(map(str, G)))) print(cnt) for i in lst: print(i)
0
null
71,235,390,036,762
276
17
print((lambda x,y:'Yes'if y in x else'No')(input()*2,input()))
N = input() ans = "No" for s in N: if s == "7": ans = "Yes" break print(ans)
0
null
17,998,744,580,960
64
172
import math def kochCurve(d, p1, p2): if d <= 0: return s = [(2.0*p1[0]+1.0*p2[0])/3.0, (2.0*p1[1]+1.0*p2[1])/3.0] t = [(1.0*p1[0]+2.0*p2[0])/3.0, (1.0*p1[1]+2.0*p2[1])/3.0] u = [(t[0]-s[0])*math.cos(math.radians(60))-(t[1]-s[1])*math.sin(math.radians(60))+s[0], (t[0]-s[0])*math.sin(math.radians(60))+(t[1]-s[1])*math.cos(math.radians(60))+s[1]] kochCurve(d-1, p1, s) print(s[0], s[1]) kochCurve(d-1, s, u) print(u[0], u[1]) kochCurve(d-1, u, t) print(t[0], t[1]) kochCurve(d-1, t, p2) n = int(input()) s = [0.00000000, 0.00000000] e = [100.00000000, 0.00000000] print(s[0],s[1]) if n != 0: kochCurve(n,s,e) print(e[0],e[1])
x = int(input()) n = x//100 if x%100 <= 5*n: print(1) else: print(0)
0
null
63,696,124,368,672
27
266
a, b, c, k = map(int, input().split()) ans = 0 for i, j in zip([1, 0, -1], [a, b, c]): x = min(k, j) ans += i*x k -= x print(ans)
# B - Easy Linear Programming A,B,C,K = map(int,input().split()) while True: ans = 0 ans += min(A,K) K -= min(A,K) if K<=0: break K -= B if K<=0: break ans -= K break print(ans)
1
21,718,513,871,208
null
148
148
x = int(input()) if x == 0: print(1) else: print(0)
a = int(input()) print(1 if a == 0 else 0)
1
2,935,193,582,280
null
76
76
s = [int(input()) for i in range(10)] s.sort(reverse = True) for i in s[:3]: print(i)
m = [] while True: try: m.append(int(raw_input())) except EOFError: break m.sort() m.reverse() for h in m[0:3]: print h
1
28,248,220
null
2
2
n,k,s=list(map(int,input().split())) if s>n: ans=["1" for _ in range(n)] else: ans=[str(s+1) for _ in range(n)] for i in range(k): ans[i]=str(s) print(" ".join(ans))
mod = 10**9 + 7 N = int(input()) A = [int(i) for i in input().split()] # bitにしてor # 一桁だけの時、111000なら3 * 3 maxl = 0 textlist = [] for i in range(N): tmp = str(bin(A[i])) tmp = tmp[2:] length = len(tmp) if maxl < length: maxl = length textlist.append(tmp) zeros = {} ones = {} for i in range(maxl): zeros[i] = 0 ones[i] = 0 for i in range(N): tmp = textlist[i] length = len(tmp) if maxl < length: maxl = length for j in range(length): if tmp[-j-1]== '1': ones[j] += 1 for i in range(maxl): zeros[i] = N-ones[i] result = 0 n2 = 1 for i in range(maxl): result = (result + n2 * (zeros[i] * ones[i])) % mod n2 = (n2 * 2) % mod print(result)
0
null
107,041,218,680,760
238
263
import sys import numpy as np input = sys.stdin.buffer.readline N = int(input()) A = np.array(list(map(int, input().split()))) MOD = 10**9 + 7 answer = 0 for n in range(63): B = (A >> n) & 1 x = np.count_nonzero(B) y = N - x x *= y for _ in range(n): x = x * 2 % MOD answer += x answer %= MOD print(answer)
mod=10**9+7 k=int(input()) s=input() l=len(s) fact=[1] for i in range(1,k+l+1): fact.append((fact[-1]*i)%mod) revfact=[] for i in range(k+l+1): revfact.append(pow(fact[i],mod-2,mod)) pow1=[1] pow2=[1] for i in range(1,k+l+1): pow1.append((pow1[-1]*25)%mod) pow2.append((pow2[-1]*26)%mod) ans=0 for i in range(k+l): coef1=(pow1[k-i]*pow2[i])%mod if i<=k: coef2=(fact[k+l-1-i]*revfact[l-1]*revfact[k-i])%mod else: coef2=0 ans+=coef1*coef2 ans%=mod print(ans)
0
null
68,141,339,791,358
263
124
dic = {'AC':0, 'WA':0, 'TLE':0, 'RE':0} N = int(input()) for i in range(N): dic[input()] += 1 for i in dic: print(i, 'x', dic[i])
N = int(input()) S = list() for i in range(N): S.append(input()) c0 = 0 c1 = 0 c2 = 0 c3 = 0 for i in S: if i == "AC": c0 += 1 if i == "WA": c1 += 1 if i == "TLE": c2 += 1 if i == "RE": c3 += 1 print("AC x " + str(c0)) print("WA x " + str(c1)) print("TLE x " + str(c2)) print("RE x " + str(c3))
1
8,730,225,495,350
null
109
109
(r, c) = [int(i) for i in input().split()] src = [] for _ in range(r): row = [int(i) for i in input().split()] row.append(sum(row)) src.append(row) last_row = [0 for _ in range(c + 1)] for rc in range(r): for cc in range(c+1): last_row[cc] += src[rc][cc] src.append(last_row) for rc in range(r+1): print(' '.join([str(a) for a in src[rc]]))
row, col = map(int, raw_input().split()) ret = [] for _ in range(row): _row = map(int, raw_input().split()) _row += [sum(_row)] ret.append(_row) for i in ret: print ' '.join(map(str, i)) ret_sum = [] for i in range(col+1): work = 0 for j in ret: work += j[i] ret_sum.append(work) print ' '.join(map(str, ret_sum))
1
1,328,850,288,704
null
59
59
import heapq import sys input = sys.stdin.readline n = int(input()) sss = [input()[:-1] for _ in range(n)] a = [] b = [] ss = 0 ii = 0 for i in sss: mi = 0 ma = 0 s = 0 for j in i: if j == "(": s += 1 else: s -= 1 mi = min(mi, s) ma = max(ma, s) ss += s if s >= 0: a.append([s, mi, ma, ii]) else: mi = 0 ma = 0 s = 0 for j in reversed(i): if j == ")": s += 1 else: s -= 1 mi = min(mi, s) ma = max(ma, s) b.append([s, mi, ma, ii]) ii += 1 if ss != 0: print("No") exit() a.sort(reverse=1, key=lambda x: x[1]) b.sort(reverse=1, key=lambda x: x[1]) def ok(a): s = 0 for i, j, _, _ in a: if s + j < 0: print("No") exit() s += i ok(a) ok(b) print("Yes")
import sys inint = lambda: int(sys.stdin.readline()) inintm = lambda: map(int, sys.stdin.readline().split()) inintl = lambda: list(inintm()) instrm = lambda: map(str, sys.stdin.readline().split()) instrl = lambda: list(instrm()) n = inint() A = inintl() cnt = 1 ans = 0 for a in A: if a != cnt: ans += 1 continue else: cnt += 1 if cnt == 1: if ans == 0: print(ans) else: print(-1) else: print(ans)
0
null
68,956,444,953,548
152
257
import sys input=sys.stdin.readline import numpy as np from numpy.fft import rfft,irfft n,m=[int(j) for j in input().split()] l=np.array([int(j) for j in input().split()]) a=np.bincount(l) fft_len=1<<18 fft = np.fft.rfft ifft = np.fft.irfft Ff = fft(a,fft_len) x=np.rint(ifft(Ff * Ff,fft_len)).astype(np.int64) p=x.cumsum() i=np.searchsorted(p,n*n-m) q=n*n-m-p[i-1] ans=(x[:i]*np.arange(i,dtype=np.int64)).sum()+i*q print(int(l.sum()*2*n-ans))
n, m = map(int, input().split()) a = sorted(list(map(int, input().split())), reverse=True) s = [0] for ai in a: s.append(ai + s[-1]) def count(x, accum=False): ret = 0 for ai in a: lo, hi = -1, n while hi - lo > 1: mid = (lo + hi) // 2 if ai + a[mid] >= x: lo = mid else: hi = mid ret += ai * hi + s[hi] if accum else hi return ret lo, hi = 0, 1000000000 while hi - lo > 1: mid = (lo + hi) // 2 if count(mid) >= m: lo = mid else: hi = mid print(count(lo, accum=True) - (count(lo) - m) * lo)
1
108,290,694,853,656
null
252
252
N = int(input()) P = [input().split() for i in range(N)] M = input() chk = 0 value = 0 for i, (k, v) in enumerate(P): if k == M: chk = int(v) elif chk != 0: value += int(v) print(value)
# https://atcoder.jp/contests/sumitrust2019/tasks/sumitb2019_e import sys input=sys.stdin.readline MOD=10**9+7 def main(): N=int(input()) A=list(map(int,input().split())) cnt=[0]*N ans=1 for i in range(N): if A[i]==0: ans*=3-cnt[0] ans%=MOD cnt[A[i]]+=1 else: ans*=cnt[A[i]-1]-cnt[A[i]] ans%=MOD cnt[A[i]]+=1 print(ans) if __name__=="__main__": main()
0
null
113,179,681,548,188
243
268
n_1 = list(map(int, input().split())) n_2 = list(map(int, input().split())) if n_1[0] != n_2[0]: print(1) else: print(0)
m1, _ = list(map(int, input().split())) m2, _ = list(map(int, input().split())) if m1 == m2: print(0) else: print(1)
1
123,977,292,767,162
null
264
264
s = input() t = input() print(sum(s[i] != t[i] for i in range(len(s))))
def main(): N, K = map(int, input().split()) N = N + 1 A=[n for n in range(N)] S=[0]*(N+1) for a in A: S[a+1]+=S[a]+a ans=0 for k in range(K, N+1): maxS, minS=S[-1], S[k] offset=S[-k-1] ans+=(maxS - offset) - minS + 1 print(ans%(1000000007)) if __name__=='__main__': main()
0
null
21,834,573,798,492
116
170
while True: [h, w] = map(int, (input().split())) if h==0 and w==0: break else: print("#"*w+"\n"+("#"+"."*(w-2)+"#\n")*(h-2)+"#"*w+"\n")
while True: h, w = map(int, input().split()) if h == 0 and w == 0: break print("#" * w) for _ in range(h-2): print("#","." * (w-2), "#", sep="") print("#" * w) print()
1
839,886,945,650
null
50
50
N = int(input()) S,T = map(str,input().split()) slist = list(S) tlist = list(T) new = '' for i in range(N): new += slist[i] new += tlist[i] print(new)
N = input() S,T = input().split() txt = '' for si,ti in zip(S,T): txt += si+ti print(txt)
1
112,241,086,430,940
null
255
255
from statistics import median #import collections #aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0] from fractions import gcd from itertools import combinations # (string,3) 3回 from collections import deque from collections import defaultdict import bisect # # d = m - k[i] - k[j] # if kk[bisect.bisect_right(kk,d) - 1] == d: # # # # pythonで無理なときは、pypyでやると正解するかも!! # # import sys sys.setrecursionlimit(10000000) mod = 10**9 + 7 def readInts(): return list(map(int,input().split())) def main(): n = int(input()) # 一番大きいものは何か、それを保存してdfs def dfs(cnt, s): if cnt == n: print(s) return biggest = 'a' c = 0 while c < len(s): if s[c] == biggest: biggest = chr(ord(biggest)+1) c += 1 else: c += 1 for i in range(0,ord(biggest) - ord('a') + 1): sc = chr(ord('a')+i) s += sc dfs(cnt + 1, s) s = s[:-1] dfs(0,"") if __name__ == '__main__': main()
n = int(input()) Adj = [[0 for i in range(n)] for i in range(n)] for i in range(n): u = list(map(int, input().split())) if u[1] > 0: for i in u[2: 2 + u[1]]: Adj[u[0] - 1][i - 1] = 1 color = [0] * n d = [0] * n f = [0] * n time = 0 def dfs(u): global time color[u - 1] = 1 time += 1 d[u - 1] = time for i in range(1, n+1): if Adj[u - 1][i - 1] == 1 and color[i - 1] == 0: dfs(i) color[u - 1] = 2 time += 1 f[u - 1] = time for i in range(1, n+1): if color[i-1] == 0: dfs(i) for i in range(n): print("{} {} {}".format(i+1,d[i],f[i]))
0
null
26,302,937,987,260
198
8
import sys from bisect import bisect_left input = sys.stdin.readline def main(): N = int(input()) L = list(map(int, input().split())) L.sort() ans = 0 for a in range(N - 2): for b in range(a + 1, N - 1): x = bisect_left(L, L[a] + L[b]) ans += (x - 1 - b) print(ans) if __name__ == "__main__": main()
from bisect import bisect_left n = int(input()) l = sorted(list(map(int, input().split()))) ans = 0 # a<b<c として、aとbを固定する # 残る条件は c<a+b のみ # aとbの固定方法がO(N^2), cの二分探索がO(logN)、結局O(N^2logN) # 二分探索は事前にソートが必要 # ex. 2, 4(a), 4, 7(b), | 8, 9, 9 | (ここにa+b=11が挿入される), 12, 14 # cとして適切なのは8,9,9の3通り # bisect_rightだとa+bが存在した時にバグる for ai in range(n): for bi in range(ai + 1, n): ans += bisect_left(l, l[ai] + l[bi]) - bi - 1 print(ans)
1
171,579,303,878,992
null
294
294
import math from itertools import permutations N = int(input()) P = list(map(int, input().split())) Q = list(map(int, input().split())) ls = permutations(range(1, N+1), N) Ns = [] for l in ls: s = ''.join([str(n) for n in l]) Ns.append(int(s)) p = ''.join(map(str, P)) q = ''.join(map(str, Q)) a = Ns.index(int(p)) + 1 b = Ns.index(int(q)) + 1 print(abs(a-b))
from itertools import permutations n=int(input()) p=tuple(int(i)for i in input().split()) q=tuple(int(i)for i in input().split()) permutation_n=list(permutations(range(1,n+1))) a=b=0 for i in permutation_n: if i<p:a+=1 if i<q:b+=1 print(abs(a-b))
1
100,486,820,066,400
null
246
246
N=int(input()) A=[int(i) for i in input().split()] S=set(A) ans=1 for i in A: ans*=i if ans>10**18: ans=-1 break if 0 in S: ans=0 print(ans)
def main(): r,g,b=map(int,input().split()) k=int(input()) while(k>0): if(g<=r): g=g<<1 k-=1 elif(b<=g): b=b<<1 k-=1 else: break; if(r<g and g<b): print('Yes') else: print('No') main()
0
null
11,484,615,604,462
134
101
T1, T2 = map(int, input().split()) A1, A2 = map(int, input().split()) B1, B2 = map(int, input().split()) P = (A1-B1)*T1 Q = (A2-B2)*T2 if(P > 0): P *= -1 Q *= -1 if(P+Q<0): print(0) elif(P+Q==0): print('infinity') else: if((-P)%(P+Q)==0): print((-P)//(P+Q)*2) else: print((-P)//(P+Q)*2+1)
Sum = input().split() cou = 1 while len(Sum) != 1 : cou += 1 if Sum[cou] == '+' : del Sum[cou] Sum.insert((cou - 2) ,int(Sum.pop(cou - 2)) + int(Sum.pop(cou - 2))) cou -= 2 elif Sum[cou] == '-' : del Sum[cou] Sum.insert((cou - 2) ,int(Sum.pop(cou - 2)) - int(Sum.pop(cou - 2))) cou -= 2 elif Sum[cou] == '*' : del Sum[cou] Sum.insert((cou - 2) ,int(Sum.pop(cou - 2)) * int(Sum.pop(cou - 2))) cou -= 2 print(Sum[0])
0
null
66,164,791,212,128
269
18
n,m=map(int,input().split()) s=list(map(int,list(input()))) ans=[] i=n while i>0: j=max(0,m-i) while s[i-(m-j)]!=0: j+=1 if j==m: print(-1) exit(0) ans.append(m-j) i-=(m-j) ans.reverse() print(' '.join(map(str,ans)))
N,M=map(int, input().split()) S=list(input()) T=S[::-1] D=[0]*N if N<=M: print(N) exit() renzoku,ma=0,0 for i in range(1,N+1): if S[i]=='1': renzoku+=1 else: ma=max(ma,renzoku) renzoku=0 if ma>=M: print(-1) exit() r=0 for i in range(1,M+1): if T[i]!='1': r=i ans=[r] while r+M<N: for i in range(M,0,-1): if T[r+i]=='0': ans.append(i) r+=i break ans.append(N-r) print(*ans[::-1])
1
139,426,585,740,442
null
274
274
import math def main(): R = int(input()) print(math.pi*2.0*R) main()
print(int(input())*3.1415*2)
1
31,428,761,303,810
null
167
167
#ALDS1_1_D N = int(input()) min,dif = 10000000000, -10000000000 for i in range(N): a = int(input()) if (a - min) > dif: dif = a - min if a < min: min = a print(dif)
n = int(input()) mn = int(input()) ans = -1 * 10 ** 10 for i in range(n - 1): r = int(input()) diff = r - mn if diff > ans: ans = diff mn = min(mn, r) print(ans)
1
12,565,283,940
null
13
13
from math import gcd k=int(input()) cnt=0 for i in range(1,k+1): for j in range(1,k+1): a=gcd(i,j) for k in range(1,k+1): cnt+=gcd(a,k) print(cnt)
from math import gcd K = int(input()) result = 0 for a in range(1, K + 1): for b in range(1, K + 1): t = gcd(a, b) for c in range(1, K + 1): result += gcd(t, c) print(result)
1
35,424,498,137,838
null
174
174
N = int(input()) A_ls = input().split(' ') A_set = { i for i in A_ls } if len(A_set) == N: print('YES') else: print('NO')
MOD = 10**9 + 7 def modinv(a, mod=10**9+7): return pow(a, mod-2, mod) def combination(n, r, mod=10**9+7): r = min(r, n-r) res = 1 for i in range(r): res = res * (n - i) * modinv(i+1, mod) % mod return res # print(combination(10,2)) s = int(input()) n = s//3 ans = 0 while n: S = s - n*3 # print(S+n-1,n-1) ans += combination(S+n-1,n-1) n-=1 print(ans%MOD)
0
null
38,608,412,279,558
222
79
import sys from collections import deque n = int(input()) q = deque() for i in range(n): c = sys.stdin.readline()[:-1] if c[0] == 'i': q.appendleft(c[7:]) elif c[6] == ' ': try: q.remove(c[7:]) except: pass elif c[6] == 'F': q.popleft() else: q.pop() print(*q)
N, u, v = map(int, input().split()) edges = [[] for _ in range(N)] for _ in range(N - 1): fr, to = map(int, input().split()) fr -= 1 to -= 1 edges[fr].append(to) edges[to].append(fr) def calc(s): minDist = [10**18] * N st = [(s, 0)] while st: now, dist = st.pop() if minDist[now] < dist: continue minDist[now] = dist for to in edges[now]: if minDist[to] > dist + 1: st.append((to, dist + 1)) return minDist distTaka = calc(u - 1) distAoki = calc(v - 1) ans = 0 for i in range(N): if distTaka[i] < distAoki[i]: ans = max(ans, distAoki[i] - 1) print(ans)
0
null
58,557,688,954,330
20
259
import sys #a,b=map(int,input().split()) a,b,c=map(int,input().split()) if a<b: if b<c: print("Yes") sys.exit() print("No")
def pre_combi1(n, p): fact = [1]*(n+1) # fact[n] = (n! mod p) factinv = [1]*(n+1) # factinv[n] = ((n!)^(-1) mod p) inv = [0]*(n+1) # factinv 計算用 inv[1] = 1 # 前処理 for i in range(2, n + 1): fact[i]= fact[i-1] * i % p inv[i]= -inv[p % i] * (p // i) % p factinv[i]= factinv[i-1] * inv[i] % p return fact, factinv def combi1(n, r, p, fact, factinv): """ k<n<10**7でpが素数のときのnCr % pを求める """ # 本処理 if r < 0 or n < r: return 0 r = min(r, n-r) return fact[n] * factinv[r] * factinv[n-r] % p p=998244353 fact,finv=pre_combi1(2*10**5+1,p) n,m,k=map(int,input().split()) ans=0 dup=[0] * n dup[0] = 1 for j in range(1,n): dup[j]=dup[j-1]*(m-1) % p for i in range(k+1): ans += m*combi1(n-1,i,p,fact,finv)*dup[n-i-1] ans %= p print(ans)
0
null
11,740,411,533,290
39
151
from collections import Counter N=int(input()) S=input() C=Counter(S) r=C['R'] g=C['G'] b=C['B'] ans=r*g*b # print(r,g,b) for i in range(N-2): for j in range(i+1,N-1): if S[i]==S[j]: continue k=2*j-i if k>=N: continue if S[j]!=S[k] and S[k]!=S[i] : ans-=1 print(ans)
n,x,t=map(int, input().split()) tako = n//x amari = n%x ans = tako*t if amari != 0: ans += t print(ans)
0
null
20,177,631,058,130
175
86
N=int(input()) A=list(map(int,input().split())) a,b,c=0,0,0 ans=1 mod=10**9+7 for i in A: ans=ans*[a,b,c].count(i)%mod if i==a: a+=1 elif i==b: b+=1 elif i==c: c+=1 print(ans)
N,K = list(map(int,input().split())) print(min(N%K, abs((N%K)-K)))
0
null
85,040,906,963,008
268
180