code1
stringlengths 16
24.5k
| code2
stringlengths 16
24.5k
| similar
int64 0
1
| pair_id
int64 2
181,637B
⌀ | question_pair_id
float64 3.71M
180,628B
⌀ | code1_group
int64 1
299
| code2_group
int64 1
299
|
---|---|---|---|---|---|---|
from sys import stdin
def main():
#入力
readline=stdin.readline
n=int(readline())
dp=[0]*(n+1)
for i in range(n+1):
if i==0 or i==1:
dp[i]=1
else:
dp[i]=dp[i-1]+dp[i-2]
print(dp[n])
if __name__=="__main__":
main()
|
n = int(input())
ans = ['a']
for i in range(n-1):
buf = []
for j in ans:
l = len(set(j))
for c in range(l+1):
buf.append(j+chr(ord('a')+c))
ans = buf
for i in ans:
print(i)
| 0 | null | 26,056,520,482,762 | 7 | 198 |
N = int(input())
S = input()
print('Yes' if S[:N//2] == S[N//2:] else 'No')
|
n = int(input())
s = input()
if n % 2 == 1:
print('No')
else:
m = int(n/2)
s1 = s[: m]
s2 = s[m: ]
if s1 == s2:
print('Yes')
else:
print('No')
| 1 | 147,093,808,192,510 | null | 279 | 279 |
n = int(input())
s = [input() for _ in range(n)]
t = ('AC', 'WA', 'TLE', 'RE')
for i in t:
print(i, 'x', s.count(i))
|
# coding: utf-8
def gcd(a, b):
while b:
a, b = b, a % b
return a
n, m = map(int, input().split())
print(int(gcd(n, m)))
| 0 | null | 4,341,906,486,148 | 109 | 11 |
# import itertools
# import math
# import sys
# sys.setrecursionlimit(500*500)
# import numpy as np
# N = int(input())
# S = input()
# n, *a = map(int, open(0))
N, K = map(int, input().split())
# A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()
# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)
# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)
# def dfs(tree, s):
# for l in tree[s]:
# if depth[l[0]] == -1:
# depth[l[0]] = depth[s] + l[1]
# dfs(tree, l[0])
# dfs(tree, 1)
# def factorization(n):
# arr = []
# temp = n
# for i in range(2, int(-(-n**0.5//1))+1):
# if temp%i==0:
# cnt=0
# while temp%i==0:
# cnt+=1
# temp //= i
# arr.append([i, cnt])
# if temp!=1:
# arr.append([temp, 1])
# if arr==[]:
# arr.append([n, 1])
# return arr
R, S, P = map(int, input().split())
T = input()
win = {"r": ["p", P], "s": ["r", R], "p": ["s", S]}
hands = []
tot = 0
for i, c in enumerate(T):
if i >= K and win[c][0] == hands[i - K]:
hands.append("anything")
continue
hands.append(win[c][0])
tot += win[c][1]
print(tot)
|
import math
a,b,rad_c = map(int,input().split())
S = b * a * math.sin(math.pi / 180 * rad_c) / 2
c = math.sqrt(a**2 + b**2 -2 * a * b * math.cos(math.pi / 180 * rad_c))
L = a + b + c
h = b * math.sin(math.pi / 180 * rad_c)
print(S,L,h)
| 0 | null | 53,709,483,502,980 | 251 | 30 |
a, b, c, d = map(int, input().split())
if d <= a:
print(d)
elif d <= a + b:
print(a)
else:
print(a-(d-(a+b)))
|
A, B, C, K = map(int, input().split())
ans = 0
if A > K:
ans += 1*K
else:
ans += 1*A
K = K - A
if B > K:
ans += 0*K
else:
ans += 0*B
K = K - B
if C > K:
ans += -1*K
else:
ans += -1*C
print(ans)
| 1 | 21,919,173,453,200 | null | 148 | 148 |
from collections import deque
n,u,v=map(int,input().split())
g=[set([]) for _ in range(n+1)]
for i in range(n-1):
a,b=map(int,input().split())
g[a].add(b)
g[b].add(a)
leaf=[]
for i in range(1,n+1):
if(len(g[i])==1):
leaf.append(i)
d_u=[-1 for _ in range(n+1)]
d_v=[-1 for _ in range(n+1)]
def bfs(start,d):
d[start]=0
q=deque([start])
while(len(q)>0):
qi=q.popleft()
di=d[qi]
next_qi=g[qi]
for i in next_qi:
if(d[i]==-1):
d[i]=di+1
q.append(i)
return d
d_u=bfs(u, d_u)
d_v=bfs(v, d_v)
if(u in leaf and list(g[u])[0]==v):
print(0)
else:
ans=0
for li in leaf:
if(d_u[li]<d_v[li]):
ans=max(ans,d_v[li]-1)
print(ans)
|
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**6)
n,u,v=map(int,input().split())
node=[[]for _ in range(n)]
for _ in range(n-1):
a,b=map(int,input().split())
node[a-1].append(b-1)
node[b-1].append(a-1)
def dfs(i):
visited[i]=1
for x in node[i]:
if visited[x]==0:
dis[x]=dis[i]+1
dfs(x)
inf=10**9
dis=[inf]*n;dis[u-1]=0;visited=[0]*n
dfs(u-1)
dis2=[]
from copy import copy
dis_dash=copy(dis)
dis2.append(dis_dash)
dis[v-1]=0;visited=[0]*n
dfs(v-1)
dis2.append(dis)
cnt=0
for i in range(n):
if dis2[0][i]<dis2[1][i]:
cnt=max(cnt,dis2[1][i])
print(cnt-1)
| 1 | 117,360,490,547,192 | null | 259 | 259 |
import sys
readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')
def solve():
n = ni()
a = nl()
ok = -1
ng = 10**18
ans = -1
while ng - ok > 1:
v = mid = (ok + ng) // 2
c = 0
cnt = 0
for x in a[:0:-1]:
cnt += c + x
c += x
d = (c - 1) // 2 + 1
if v > c - d:
v -= c - d
d = c
elif v > 0:
d += v
v = 0
c = d
if a[0] + c > 1:
ng = mid
else:
ok = mid
ans = cnt + 1
print(ans)
return
solve()
|
import sys
n = int(input())
dif_max = -float("inf")
min_v = float("inf")
for i in range(n):
r = int(sys.stdin.readline())
dif_max = max(dif_max, r - min_v)
min_v = min(min_v, r)
print(dif_max)
| 0 | null | 9,482,903,564,628 | 141 | 13 |
import sys
import math # noqa
import bisect # noqa
import queue # noqa
def input():
return sys.stdin.readline().rstrip()
def main():
N = int(input())
que = queue.Queue()
que.put(('a', ord('a')))
while not que.empty():
res, m = que.get()
if len(res) < N:
for i in range(ord('a'), min(ord('z'), m + 1) + 1):
que.put((res + chr(i), max(m, i)))
elif len(res) == N:
print(res)
else:
break
if __name__ == '__main__':
main()
|
N=input()
A=map(int,raw_input().split())
for i in range(N-1):
for k in range(N-1):
print A[k],
print A[N-1]
v=A[i+1]
j=i
while j>=0 and A[j] >v:
A[j+1]=A[j]
j=j-1
A[j+1]=v
for m in range(N-1):
print str(A[m]),
print A[N-1]
| 0 | null | 26,061,378,803,160 | 198 | 10 |
# -*- coding: utf-8 -*-
n = int(input())
Univ = [[[0 for i in range(10)]for j in range(3)]for k in range(4)]
for i in range(n):
b,f,r,v =[int(i)for i in input().split(' ')]
Univ[b-1][f-1][r-1] += v
for b in range(4):
for f in range(3):
for r in range(10):
print('',Univ[b][f][r],end="")
print()
if b<3:
print("#"*20)
|
X,Y = map(int,input().split())
turtle_leg = 4
crane_leg = 2
for i in range(X+1):
if Y == turtle_leg*i+crane_leg*(X-i):
print ("Yes")
break
if i==X:
print ("No")
break
| 0 | null | 7,437,515,228,330 | 55 | 127 |
n, a, b = map(int, input().split())
if (a%2 and b%2) or (a%2==0 and b%2==0):
print((b-a)//2)
else:
print(min(a-1, n-b)+1+(b-a-1)//2)
|
import numpy as np
def min_steps_v2(n, a, b):
n = np.int64(n)
a = np.int64(a)
b = np.int64(b)
if (b - a) % 2 == 0:
return np.int64(int(b - a) // 2)
# odd case
a_diff = a - 1
b_diff = n - b
if a_diff > b_diff:
steps_to_even = n - b + 1
remain_steps = min_steps_v2(n, a + (n - b) + 1, n)
else:
steps_to_even = a
remain_steps = min_steps_v2(n, 1, b - a)
return steps_to_even + remain_steps
numbers = input().split(' ')
n = int(numbers[0])
a = int(numbers[1])
b = int(numbers[2])
print(min_steps_v2(n, a, b))
| 1 | 109,225,031,662,880 | null | 253 | 253 |
def main():
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
import math
#inf = 10**17
#mod = 10**9 + 7
n,t = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort(key=lambda x: x[1])
ab.sort(key=lambda x: x[0])
dp = [-1]*(t+1)
dp[0] = 0
for a, b in ab:
for j in range(t-1, -1, -1):
if dp[j] >= 0:
if j+a<=t:
dp[j+a] = max(dp[j+a], dp[j]+b)
else:
dp[-1] = max(dp[-1], dp[j]+b)
print(max(dp))
if __name__ == '__main__':
main()
|
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def main():
N, T, *AB = map(int, read().split())
A = AB[::2]
B = AB[1::2]
dp1 = [[0] * T for _ in range(N + 1)]
for i in range(N):
for t in range(T):
if 0 <= t - A[i]:
dp1[i + 1][t] = dp1[i][t - A[i]] + B[i]
if dp1[i + 1][t] < dp1[i][t]:
dp1[i + 1][t] = dp1[i][t]
dp2 = [[0] * T for _ in range(N + 1)]
for i in range(N - 1, -1, -1):
for t in range(T):
if 0 <= t - A[i]:
dp2[i][t] = dp2[i + 1][t - A[i]] + B[i]
if dp2[i][t] < dp2[i + 1][t]:
dp2[i][t] = dp2[i + 1][t]
ans = 0
for i in range(N):
tmp = max(dp1[i][t] + dp2[i + 1][T - t - 1] for t in range(T)) + B[i]
if ans < tmp:
ans = tmp
print(ans)
return
if __name__ == '__main__':
main()
| 1 | 151,605,246,219,830 | null | 282 | 282 |
a, b = int(input()), int(input())
l = [1,2,3]
l.remove(a)
l.remove(b)
print(l[0])
|
import sys
input = sys.stdin.readline
INF = 99999#1001001001
from collections import deque
def linput(ty=int, cvt=list):
return cvt(map(ty,input().split()))
def pad(mxy, wall="#"):
w = len(mxy[0])
gp = wall*(w+2)
re = [gp,]
re_app = re.append
for vx in mxy:
re_app(wall+vx+wall)
re_app(gp)
return re
vD = [(0,1),(1,0)]
vQ = deque([])
vQ_app, vQ_popl = vQ.append, vQ.popleft
def main():
H,W = linput()
mM = [input().rstrip() for _ in [0,]*H]
mM = pad(mM, "$")
res = 0
cnt = 0
for sr in range(1,1+1):
for sc in range(1,1+1):
if mM[sr][sc]=="#":
res += 1
mV = [[INF,]*(W+2) for _ in [INF,]*(H+2)]
mV[sr][sc] = res
vQ_app((sr,sc))
while vQ:
r,c = vQ_popl()
now_cost = mV[r][c]
now_s = mM[r][c]
for dr,dc in vD:
nr,nc = r+dr, c+dc
if mM[nr][nc]=="$":
continue
next = now_s=="."!=mM[nr][nc]
new_cost = now_cost + next
if new_cost < mV[nr][nc]:
vQ_app((nr,nc))
mV[nr][nc] = new_cost
cnt += 1
#print(res,cnt,X,Y,H)
if cnt>999999: break
#print(*mV,sep="\n")###
res = mV[H][W]
print(res)
if __name__ == "__main__":
main()
| 0 | null | 80,327,542,417,150 | 254 | 194 |
D, T, S = [int(i) for i in input().split()]
out = 'Yes' if D <= T * S else 'No'
print(out)
|
# -*- coding: utf-8 -*-
"""
C - Snack
https://atcoder.jp/contests/abc148/tasks/abc148_c
"""
import sys
from fractions import gcd
def solve(A, B):
return A * B // gcd(A, B)
def main(args):
A, B = map(int, input().split())
ans = solve(A, B)
print(ans)
if __name__ == '__main__':
main(sys.argv[1:])
| 0 | null | 58,177,731,640,180 | 81 | 256 |
n, k = map(int, input().split())
wlist = []
for i in range(n):
wlist.append(int(input()))
ma = sum(wlist) + 1
mi = max(wlist) - 1
while (ma - mi > 1):
mid = (ma + mi)//2
truck = [0for i in range(k)] ## k개
i = 0
gobigger = 0
for w in wlist:
if(truck[i] + w <= mid):
truck[i] = truck[i] + w
else:
i = i+1
if (i == k):
gobigger = 1
break
truck[i] = truck[i] + w
if (gobigger == 1):
mi = mid
else:
ma = mid
print(ma)
|
def f(w, n, k, v):
# how many things in list w of length n can fill in k cars with capacity v?
i = 0
space = 0
while i < n:
if space >= w[i]:
space -= w[i]
i += 1
else:
# space < w[i]
if k == 0:
break
k -= 1
space = v
return i
if __name__ == '__main__':
n, k = map(int, raw_input().split())
w = []
maxv = -1
for i in range(n):
x = int(raw_input())
w.append(x)
maxv = max(maxv, x)
left = 0
right = n * maxv
# range: (left, right]
# loop until left + 1 = right, then right is the answer
# why? because right works, but right - 1 doesn't work
# so right is the smallest capacity
while right - left > 1:
mid = (left + right) / 2
v = f(w, n, k, mid)
if v >= n:
# range: (left, mid]
# in fact, v cannot > n
right = mid
else:
# range: (mid, right]
left = mid
print right
| 1 | 90,299,492,572 | null | 24 | 24 |
n = int(input())
d = {}
for i in range(n):
line = input().split()
if line[0] == "insert":
d[line[1]] = 1
elif line[0] == "find":
if line[1] in d:
print("yes")
else:
print("no")
|
W = input().lower()
count = 0
while True:
line = input()
if line == 'END_OF_TEXT':
break
for s in line.lower().split():
if s == W:
count += 1
print(count)
| 0 | null | 940,958,157,606 | 23 | 65 |
A = int(input())
B = int(input())
for i in range(1, 4):
if i not in [A, B]:
print(i)
|
#dpでできないかな?
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
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
h,w=MI()
lis=[list(SI()) for i in range(h)]
al=[[-1]*w for i in range(h)]
dp=[[inf]*w for i in range(h)]
'''for i in range(h):
for j in range(w):
if lis[i][j]=="#":
dp[i][j]=-1'''
dp[0][0]= 0 if lis[0][0]=="." else 1
q=deque([[0,0]])
step=[[0,1],[1,0]]
#print(lis)
while q:
x,y=q.popleft()
if lis[x][y]==".":
state=1
else:
state=-1
for i,j in step:
if x+i>h-1 or y+j>w-1:
continue
elif state==1:
if al[x+i][y+j]<0:
al[x+i][y+j]=0
q.append([x+i,y+j])
if lis[x+i][y+j]=="#":
dp[x+i][y+j]=min(dp[x+i][y+j],dp[x][y]+1)
else:
dp[x+i][y+j]=min(dp[x+i][y+j],dp[x][y])
elif state==-1:
if al[x+i][y+j]<0:
al[x+i][y+j]=0
q.append([x+i,y+j])
dp[x+i][y+j]=min(dp[x+i][y+j],dp[x][y])
print(dp[-1][-1])
| 0 | null | 79,614,016,049,308 | 254 | 194 |
H,W=map(int,input().split())
s=[input()for _ in range(H)]
a=[[0]*W for _ in range(H)]
for i in range(H):
for j in range(W):
if i==j==0:a[0][0]=0
elif i==0:a[0][j]=a[0][j-1]if s[0][j]==s[0][j-1]else a[0][j-1]+1
elif j==0:a[i][0]=a[i-1][0]if s[i][0]==s[i-1][0]else a[i-1][0]+1
else:a[i][j]=min(a[i][j-1]if s[i][j]==s[i][j-1]else a[i][j-1]+1,a[i-1][j]if s[i][j]==s[i-1][j]else a[i-1][j]+1)
print((a[H-1][W-1]+1)//2 if s[0][0]=='.'else a[H-1][W-1]//2+1)
|
h, w = map(int, input().split())
S = []
for _ in range(h):
s = str(input())
S.append(s)
DP = [[0 for _ in range(w)] for _ in range(h)]
if S[0][0] == '.':
DP[0][0] = 0
else:
DP[0][0] = 1
ren = False
if DP[0][0] == 1:
ren = True
for i in range(1,h):
if S[i][0] == '.':
DP[i][0] = DP[i-1][0]
ren = False
elif ren == False and S[i][0] == '#':
DP[i][0] = DP[i-1][0] + 1
ren = True
elif ren == True and S[i][0] == '#':
DP[i][0] = DP[i-1][0]
ren = True
ren = False
if DP[0][0] == 1:
ren = True
for j in range(1,w):
if S[0][j] == '.':
DP[0][j] = DP[0][j-1]
ren = False
elif ren == False and S[0][j] == '#':
DP[0][j] = DP[0][j-1] + 1
ren = True
elif ren == True and S[0][j] == '#':
DP[0][j] = DP[0][j-1]
ren = True
for i in range(1,h):
for j in range(1,w):
if S[i][j] == '.':
DP[i][j] = min(DP[i-1][j], DP[i][j-1])
elif S[i][j] == '#':
res_i = 0
res_j = 0
if S[i-1][j] == '.':
res_i = 1
if S[i][j-1] == '.':
res_j = 1
DP[i][j] = min(DP[i-1][j] + res_i, DP[i][j-1] + res_j)
print(DP[h-1][w-1])
| 1 | 49,376,011,357,500 | null | 194 | 194 |
def d(x):
dl=[]
for i in range(1, int(x**0.5)+1):
if x%i==0:
dl.append(i)
if i!=x//i:
dl.append(x//i)
return dl
n=int(input())
ans=0
t=d(n)
for i in t:
if i==1:
continue
tn=n
while tn>=i:
if tn%i==0:
tn//=i
else:
tn%=i
if tn==1:
ans+=1
ans+=len(d(n-1))-1
print(ans)
|
import collections
n = int(input())
def prime_factorize(n):
a = []
while n % 2 == 0:
a.append(2)
n //= 2
f = 3
while f * f <= n:
if n % f == 0:
a.append(f)
n //= f
else:
f += 2
if n != 1:
a.append(n)
return a
def make_divisors(n):
lower_divisors, upper_divisors = [], []
i = 1
while i*i <= n:
if n % i == 0:
lower_divisors.append(i)
if i != n // i:
upper_divisors.append(n//i)
i += 1
return lower_divisors + upper_divisors[::-1]
ns = make_divisors(n)
ans = 0
for i in ns:
if i == 1:
continue
p = n
while p % i == 0:
p //= i
if p % i == 1:
ans += 1
ns = make_divisors(n - 1)
ans += len(ns) - 1
print(ans)
| 1 | 41,410,854,865,118 | null | 183 | 183 |
#!/usr/bin/python3
# -*- coding:utf-8 -*-
import numpy
def main():
_, _, k = map(int, input().strip().split())
la = numpy.cumsum([0] + list(map(int, input().strip().split())))
lb = numpy.cumsum([0] + list(map(int, input().strip().split())) + [10**9+5])
def bs(x):
s, t = 0, len(lb) - 1
while t > s + 1:
m = (s + t) // 2
if lb[m] == x:
return m
if lb[m] > x:
t = m
else:
s = m
return s
ans = 0
for i, a in enumerate(la):
if k - a >= 0:
ans = max(ans, i + bs((k - a)))
print(ans)
if __name__=='__main__':
main()
|
L, R, d = input().split()
L = int(L)
R = int(R)
d = int(d)
cnt = 0
for t in range(L,R+1):
if t % d == 0:
cnt = cnt + 1
print(cnt)
| 0 | null | 9,093,403,240,854 | 117 | 104 |
import sys
n = [int(i) for i in sys.stdin.readline().rstrip()[::-1]]
ans = 0
i = 0
p = [n[i],10 - n[i]]
now = [0,0]
i += 1
ans = min(p[0], p[1] + 1)
for i in range(1,len(n)):
now[0] = min(p[0] + n[i]%10, p[1] + n[i]%10 + 1)
now[1] = min(p[0] + 10 - n[i]%10, p[1] + 9 - n[i]%10)
ans = min(now[0], now[1] + 1)
p[0],p[1] = now[0],now[1]
print(ans)
|
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
def main():
N = [0] + list(map(int, tuple(input().rstrip("\n"))))
Ncopy = N.copy()
ans = 0
flag = False
for i in range(len(N)-1, 0, -1):
if (Ncopy[i] <= 4) or (Ncopy[i] == 5 and N[i-1] <= 4):
ans += Ncopy[i]
flag = False
else:
Ncopy[i-1] += 1
if not flag:
ans += 10 - N[i]
else:
ans += 9 - N[i]
Ncopy[i] = 0
flag = True
if Ncopy[0] == 1:
ans += 1
print(ans)
if __name__ == "__main__":
main()
| 1 | 71,112,245,265,678 | null | 219 | 219 |
# vim: fileencoding=utf-8
import math
def main():
a = int(input())
res = a + math.pow(a, 2) + math.pow(a, 3)
print(int(res))
if __name__ == "__main__":
main()
|
while 1:
m,f,r=map(int, raw_input().split())
if m==f==r==-1: break
s=m+f
if m==-1 or f==-1 or s<30: R="F"
elif s>=80: R="A"
elif s>=65: R="B"
elif s>=50: R="C"
elif r>=50: R="C"
else: R="D"
print R
| 0 | null | 5,745,246,179,388 | 115 | 57 |
a=[]
for i in range(10):
a.append(input())
a.sort()
a=a[::-1]
a=a[:3]
print(a[0])
print(a[1])
print(a[2])
|
import math
def rot60(s, t):
v = t - s
a = 1/2 + complex(0,(math.sqrt(3)/2))
return v * a + s
def pr(p):
x = p.real
y = p.imag
print('%.10f %.10f'%(x, y))
def dfs(p1, p2, n):
if n == 0:
return
s = (p2 - p1) * (1/3) + p1
t = (p2 - p1) * (2/3) + p1
u = rot60(s, t)
dfs(p1, s, n-1)
pr(s)
dfs(s, u, n-1)
pr(u)
dfs(u, t, n-1)
pr(t)
dfs(t, p2, n-1)
n = int(input())
s = (0 + 0j)
t = (100 + 0j)
pr(s)
dfs(s, t, n)
pr(t)
| 0 | null | 59,794,459,900 | 2 | 27 |
n,m=map(int,input().split())
A=[int(i) for i in input().split()]
A.sort()
s=sum(A)
SA=[0]
for a in A:
SA.append(SA[-1]+a)
for i in range(n+1):
SA[i]=s-SA[i]
l,r=0,2*max(A)+1
import bisect
def chk(x):
ct=0
for a in A:
ct+=n-bisect.bisect_left(A,max(x-a,0))
if ct>=m:
return True
else:
return False
def count(x):
ct=0
for a in A:
ct+=n-bisect.bisect_left(A,max(x-a,0))
return ct
while r-l>1:
mid=(l+r)//2
if chk(mid):
l=mid
else:
r=mid
ans=0
for a in A:
aa=bisect.bisect_left(A,max(l-a,0))
ans+=SA[aa]+a*(n-aa)
print(ans-l*(count(l)-m))
|
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
import bisect
n, m = map(int, input().split())
a = [int(item) for item in input().split()]
a.sort()
a_rev = sorted(a, reverse=True)
cumsum = [0]
for item in a:
cumsum.append(cumsum[-1] + item)
# Use pair which sum goes over mid
l = 0; r = 10**10
while r - l > 1:
mid = (l + r) // 2
to_use = 0
for i, item in enumerate(a_rev):
useless = bisect.bisect_left(a, mid - item)
to_use += n - useless
if to_use >= m:
l = mid
else:
r = mid
ans = 0
total_use = 0
for i, item in enumerate(a_rev):
useless = bisect.bisect_left(a, l - item)
to_use = n - useless
total_use += to_use
ans += item * to_use + cumsum[n] - cumsum[n - to_use]
print(ans - l * (total_use - m))
| 1 | 108,123,491,136,480 | null | 252 | 252 |
import collections
H, W, *S = open(0).read().split()
H, W = [int(_) for _ in [H, W]]
dist = [[float('inf')] * W for _ in range(H)]
dist[0][0] = 0
Q = collections.deque([0])
while True:
if not Q:
break
i = Q.popleft()
x, y = divmod(i, W)
d = dist[x][y]
for dx, dy in ((1, 0), (0, 1)):
nx, ny = x + dx, y + dy
if 0 <= nx < H and 0 <= ny < W:
if S[nx][ny] == '#' and S[x][y] == '.':
if dist[nx][ny] > d + 1:
dist[nx][ny] = d + 1
Q += [nx * W + ny]
elif dist[nx][ny] > d:
dist[nx][ny] = d
Q += [nx * W + ny]
ans = dist[-1][-1]
if S[0][0] == '#':
ans += 1
print(ans)
|
s = input()
ans = s
if s[-1] == "s":
ans += "e"
print(ans+"s")
| 0 | null | 26,024,505,465,402 | 194 | 71 |
#!/usr/bin/env python3
from collections import Counter
def solve(N: int, A: "List[int]", Q: int, BC: "List[(int,int)]"):
A = Counter(A)
answers = []
ans = ans = sum((i * n for i, n in A.items()))
for b, c in BC:
if b != c:
ans += (c - b) * A[b]
A[c] += A[b]
del A[b]
answers.append(ans)
return answers
def main():
N = int(input()) # type: int
A = list(map(int, input().split()))
Q = int(input())
BC = [tuple(map(int, input().split())) for _ in range(Q)]
answer = solve(N, A, Q, BC)
for ans in answer:
print(ans)
if __name__ == "__main__":
main()
|
n = int(input().strip())
a = list(map(int, input().split()))
cnt = [0 for i in range(10**5)]
a = sorted(a)
s = sum(a)
for i in range(n):
cnt[a[i]-1] += 1
q = int(input().strip())
for i in range(q):
b, c = map(int, input().split())
s += c * cnt[b - 1] - b * cnt[b - 1]
cnt[c - 1] += cnt[b - 1]
cnt[b - 1] = 0
print(s)
| 1 | 12,134,870,298,902 | null | 122 | 122 |
N=int(input())
a=list(map(int, input().split()))
k=list(map(str,a))
j=' '.join(k)
print(j)
for i in range(1,N):
v=a[i]
j=i-1
while a[j]>v and j>=0:
a[j+1]=a[j]
j -=1
a[j+1]=v
b=list(map(str, a))
c=' '.join(b)
print(c)
|
N = int(raw_input())
A = map(int,raw_input().split())
print ' '.join(map(str,A))
for i in range(1,N):
v = A[i]
j = i - 1
while j >=0 and A[j] > v:
A[j+1] = A[j]
j -= 1
A[j+1] = v
print ' '.join(map(str,A))
| 1 | 5,565,914,320 | null | 10 | 10 |
n = int(input())
ss = {}
tt = []
for i in range(n):
s, t = map(str, input().split())
ss[s] = i + 1
tt.append(t)
x = input()
print(sum([int(x) for x in tt[ss[x]:]]))
|
N=int(input())
st= [list(map(str, input().split())) for _ in range(N)]
X=input()
ans=0
check=0
for i in range(N):
if check==1:
ans+=int(st[i][1])
if st[i][0]==X:
check=1
print(ans)
| 1 | 97,110,622,105,472 | null | 243 | 243 |
N,K=map(int,input().split())
ans=0
mod=10**9+7
A=[0 for i in range(K)]
for i in range(K,0,-1):
l=pow(K//i,N,mod)
a=2
while a*i<=K:
l-=A[a*i-1]
a+=1
ans+=l*i%mod
ans%=mod
A[i-1]=l
print(ans)
|
x, y = map(int, input().split())
ans = max(4 - x, 0) + max(4 - y, 0)
print(ans * 10**5 if ans != 6 else 10**6)
| 0 | null | 88,920,166,505,430 | 176 | 275 |
inputs = [int(d) for d in input().split()]
results = []
for i in (0, 1):
for j in (2, 3):
results.append(inputs[i] * inputs[j])
print(max(results))
|
data =input()
s1 =[]
ans =[]
memo=-1
for i in range(len(data)):
if data[i]=='\\':
s1.append(i)
if data[i]=='/':
if len(s1)==0:pass
# elif len(s1)==1:
# ans.append(i-s1.pop())
else:
a = i-s1[-1]
while len(ans)!=0:
if ans[-1][1]<=s1[-1]:break
a+=ans.pop()[0]
ans.append([a,s1.pop()])
# print(ans)
ans2=[int(ans[i][0]) for i in range(len(ans))]
print(sum(ans2))
print(len(ans),end='')
if len(ans):
print('',end=' ')
print(' '.join(list(map(str,ans2))))
| 0 | null | 1,529,861,708,000 | 77 | 21 |
s, hug = input(), 0
for i in range(len(s) // 2):
if s[i] != s[-(i+1)]:
hug += 1
print(hug)
|
import collections
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall
H, W, *S = open(0).read().split()
H, W = [int(_) for _ in [H, W]]
A = []
B = []
C = []
for i in range(H * W):
x, y = divmod(i, W)
if S[x][y] == '.':
for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)):
nx, ny = x + dx, y + dy
if not (0 <= nx < H and 0 <= ny < W):
continue
if S[nx][ny] == '.':
A += [i]
B += [nx * W + ny]
C += [1]
F = floyd_warshall(csr_matrix((C, (A, B)), shape=(H*W, H*W)))
print(int(np.max(F[F!=np.inf])))
| 0 | null | 107,358,460,872,388 | 261 | 241 |
#QQ
for i in range(1,10):
for n in range(1, 10):
print("%dx%d=%d" % (i, n, i * n))
|
N = int(input())
L = [[int(l) for l in input().split()] for _ in range(N)]
L1 = [0]*N
L2 = [0]*N
for i in range(N):
L1[i] = L[i][0]+L[i][1]
L2[i] = L[i][0]-L[i][1]
L1.sort()
L2.sort()
print(max(L1[-1]-L1[0], L2[-1]-L2[0]))
| 0 | null | 1,707,775,432,160 | 1 | 80 |
n = int(input())
alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z']
res = ''
while (n > 0):
n = n - 1
q = n % 26
n = n // 26
res = res + alphabets[q]
res = res[::-1]
print(res)
|
N = int(input())
A = list(map(int, input().split()))
"""
indexのi,jについて、 j - i = A[i] + A[j] になる組の数を数え上げる。
愚直にやると、i,jを全ペアためす。N(O^2)で間に合わない
二つの変数が出てきて、ある式で関係性が表せる場合は、iの式 = jの式 みたいにしてやるとうまくいきことが多い
j - i = A[i] + A[j]
-> A[i] + i = j - A[j]
なので、各参加者について、iとして使う時とjとして使うときで分けて数えていって、最後に組の数を数え上げる
A[i], i >=1 なので、2以上
1 <= j <= N, A[j] >= 1 なので、N未満
の範囲を調べればよい
"""
from collections import defaultdict
I = defaultdict(int)
J = defaultdict(int)
for i in range(N):
I[A[i] + i+1] += 1
J[i+1 - A[i]] += 1
ans = 0
for i in range(2, N):
ans += I[i] * J[i]
print(ans)
| 0 | null | 19,081,383,596,482 | 121 | 157 |
N = int(input())
A = list(map(int, input().split()))
total = 0
for a in A:
total ^= a
result = []
for a in A:
result.append(total ^ a)
print(*result)
|
n = int(input())
lis = list(map(int, input().split()))
m = 0
for a in lis:
m = m ^ a
for a in lis:
print(m ^ a, end=" ")
| 1 | 12,438,707,240,220 | null | 123 | 123 |
n = int(input())
d = [list(map(int, input().split())) for _ in range(n)]
cnt = 0
for i in range(n):
d1, d2 = d[i]
if d1 == d2:
cnt += 1
if cnt == 3:
print("Yes")
exit(0)
else:
cnt = 0
print("No")
|
#A
N=input()
if len(N) >= 4:
N_3=N[-3:]
ans=1000-int(N_3) if int(N_3) !=0 else 0
else:
ans=1000-int(N)
print(ans)
| 0 | null | 5,472,099,463,420 | 72 | 108 |
import sys
input = sys.stdin.readline
def main():
N = int( input())
S = [input().strip() for _ in range(N)]
Up = []
Down = []
for s in S:
now = 0
m = 0
for t in s:
if t == "(":
now += 1
else:
now -= 1
if now < m:
m = now
# print(t, now)
if now >= 0:
Up.append((m,now))
else:
Down.append((m-now,-now))
up = 0
Up.sort(reverse=True)
for m, inc in Up:
if up+m < 0:
print("No")
return
up += inc
down = 0
Down.sort(reverse=True)
# print(Up, Down)
for m, dec in Down:
if down+m < 0:
print("No")
return
down += dec
if up != down:
print("No")
return
print("Yes")
if __name__ == '__main__':
main()
|
import sys
sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip())) #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split()) #空白あり
def LS2(): return list(sys.stdin.readline().rstrip()) #空白なし
N,K = MI()
A = LI()
from itertools import accumulate
for _ in range(min(K,100)):
B = [0]*(N+1)
for i in range(N):
a = A[i]
B[max(i-a,0)] += 1
B[min(N,i+a+1)] -= 1
C = list(accumulate(B))
A = C
print(*[A[i] for i in range(N)])
| 0 | null | 19,631,062,577,048 | 152 | 132 |
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7
def resolve():
x, y, a, b, c = map(int, input().split())
P = sorted(list(map(int, input().split())), reverse=True)
Q = sorted(list(map(int, input().split())), reverse=True)
R = sorted(list(map(int, input().split())), reverse=True)
Apples = sorted(P[:x] + Q[:y] + R, reverse=True)
print(sum(Apples[: x + y]))
if __name__ == '__main__':
resolve()
|
N = int(input())
S = input()
P = 0
count = 0
for j in range(1000):
V = str(P).zfill(3)
p1 = False
p2 = False
for i in range(N):
if S[i] == V[0] and not p1:
p1 = True
continue
if S[i] == V[1] and p1 and not p2:
p2 = True
continue
if S[i] == V[2] and p2:
count += 1
break
P += 1
print (count)
| 0 | null | 86,896,196,266,348 | 188 | 267 |
N = int(input())
ST = [list(map(str, input().split())) for _ in range(N)]
X = str(input())
ans = 0
flag = False
for s, t in ST:
if flag:
ans += int(t)
else:
if s == X:
flag = True
print (ans)
|
N = int(input())
ST = [input().split() for _ in range(N)]
X = input()
flag = False
ans = 0
for i in range(N):
if flag:
ans += int(ST[i][1])
if ST[i][0] == X:
flag = True
print(ans)
| 1 | 96,628,299,062,982 | null | 243 | 243 |
A,B = map(int,input().split())
if A<=9 and B<=9:print(A*B)
else : print(-1)
|
def judge99(x):
if x <= 9:
return True
else:
return False
a, b = map(int,input().split())
if judge99(a) and judge99(b):
print(a*b)
else:
print(-1)
| 1 | 158,228,462,805,962 | null | 286 | 286 |
# -*- coding:utf-8 -*-
def solve():
import sys
N = int(sys.stdin.readline())
dp = [None]*45
dp[0], dp[1] = 1, 1
def fib(n):
if dp[n] is not None:
return dp[n]
dp[n] = fib(n-1) + fib(n-2)
return dp[n]
print(fib(N))
if __name__ == "__main__":
solve()
|
def fib(n):
a = 1
b = 1
if n == 0:
print(a)
elif n == 1:
print(b)
else:
for i in range(n-1):
c = a + b
a , b = b , c
print(c)
n = int(input())
fib(n)
| 1 | 1,712,366,280 | null | 7 | 7 |
def main():
global s,ide_ele,num,seg
n = int(input())
s = list(input())
for i in range(n):
s[i] = ord(s[i])-97
ide_ele = 0
num = 2**(n-1).bit_length()
seg = [[ide_ele for _ in range(2*num)] for _ in range(26)]
for i in range(n):
seg[s[i]][i+num-1] = 1
for a in range(26):
for i in range(num-2,-1,-1) :
seg[a][i] = max(seg[a][2*i+1],seg[a][2*i+2])
q = int(input())
for _ in range(q):
QUERY = list(input().split())
QUERY[0], QUERY[1] = int(QUERY[0]), int(QUERY[1])
if QUERY[0] == 1:
x = ord(QUERY[2])-97
k = QUERY[1]-1
pre = s[k]
s[k] = x
k += num-1
seg[pre][k] = 0
seg[x][k] = 1
while k:
k = (k-1)//2
seg[pre][k] = max(seg[pre][k*2+1],seg[pre][k*2+2])
seg[x][k] = max(seg[x][k*2+1],seg[x][k*2+2])
if QUERY[0] == 2:
P, Q = QUERY[1]-1, int(QUERY[2])
if Q <= P:
print(ide_ele)
break
P += num-1
Q += num-2
ans = ide_ele
for i in range(26):
res = ide_ele
p,q = P,Q
while q-p > 1:
if p&1 == 0:
res = max(res,seg[i][p])
if q&1 == 1:
res = max(res,seg[i][q])
q -= 1
p = p//2
q = (q-1)//2
if p == q:
res = max(res,seg[i][p])
else:
res = max(max(res,seg[i][p]),seg[i][q])
ans += res
print(ans)
if __name__ == "__main__":
main()
|
f=input
n,l,q=int(f()),list(f()),int(f())
B=[[0]*-~n for i in range(26)]
def S(o,i):
s=0
while i: s+=B[o][i]; i-=i&-i
return s
def A(o,i,x):
while i<=n: B[o][i]+=x; i+=i&-i
for i in range(n): A(ord(l[i])-97,i+1,1)
for i in range(q):
a,b,c=f().split(); b=int(b)
if a>'1': print(sum(1 for o in range(26) if S(o,int(c))-S(o,b-1)))
else: A(ord(l[b-1])-97,b,-1); l[b-1]=c; A(ord(c)-97,b,1)
| 1 | 62,190,823,947,228 | null | 210 | 210 |
if __name__ == '__main__':
A = []
for i in range(3):
row = list(map(int, input().split()))
A.append(row)
N = int(input())
B = []
for i in range(N):
B.append(int(input()))
for i in range(3):
cnt = 0
for j in range(3):
if A[i][j] in B:
cnt += 1
if cnt==3:
print('Yes')
quit()
for i in range(3):
cnt = 0
for j in range(3):
if A[j][i] in B:
cnt += 1
if cnt==3:
print('Yes')
quit()
if A[0][0] in B and A[1][1] in B and A[2][2] in B:
print('Yes')
quit()
if A[0][2] in B and A[1][1] in B and A[2][0] in B:
print('Yes')
quit()
print('No')
|
from collections import deque
N,u,v=map(int,input().split())
u-=1;v-=1
G=[[] for i in range(N)]
for i in range(N-1):
a,b=map(int,input().split())
a-=1;b-=1
G[a].append(b)
G[b].append(a)
if len(G[u])==1 and G[u][0]==v:
print(0)
exit()
distu=[-1 for i in range(N)]
q=deque([])
q.append(u)
while(len(q)>0):
r=q.pop()
for p in G[r]:
if distu[p]!=-1:
continue
distu[p]=distu[r]+1
q.append(p)
distv=[-1 for i in range(N)]
q=deque([])
q.append(v)
while(len(q)>0):
r=q.pop()
for p in G[r]:
if distv[p]!=-1:
continue
distv[p]=distv[r]+1
q.append(p)
dist=[distv[i] if distu[i]<=distv[i] else 0 for i in range(N)]
print(max(dist))
| 0 | null | 88,259,167,766,142 | 207 | 259 |
hitsuji, okami = map(int, input().split())
if hitsuji <= okami:
print('unsafe')
else:
print('safe')
|
S,W = (int(x) for x in input().split())
if(S > W):
print('safe')
else:
print('unsafe')
| 1 | 29,410,465,542,168 | null | 163 | 163 |
import math
a,b=map(int,input().split())
count=1
while True:
c=count*0.08
d=count*0.1
if math.floor(c)==a and math.floor(d)==b:
print(count)
break
count+=1
if count>1500:
print(-1)
break
|
from math import floor
from math import ceil
a,b = map(int,input().split())
n = floor(a / 0.08)
m = floor(b / 0.1)
n2 = ceil(a / 0.08)
m2 = ceil(b / 0.1)
p = floor(n * 0.08)
q = floor(n * 0.1)
p2 = floor(n2 * 0.08)
q2 = floor(n2 * 0.1)
x = floor(m * 0.08)
y = floor(m * 0.1)
x2 = floor(m2 * 0.08)
y2 = floor(m2 * 0.1)
if p == a and q == b and x == a and y == b:
print(min(n,m))
elif p == a and q == b:
print(n)
elif x == a and y == b:
print(m)
elif p2 == a and q2 == b and x2 == a and y2 == b:
print(min(n2,m2))
elif p2 == a and q2 == b:
print(n2)
elif x2 == a and y2 == b:
print(m2)
else:
print(-1)
| 1 | 56,508,536,699,670 | null | 203 | 203 |
N = int(input())
S = list(input())
Q = int(input())
class Bit:
"""1-indexed"""
def __init__(self, n):
self.size = n
self.tree = [0] * (n + 1)
def sum(self, i):
s = 0
while i > 0:
s += self.tree[i]
i -= i & -i
return s
def add(self, i, x):
while i <= self.size:
self.tree[i] += x
i += i & -i
en2asc = lambda s: ord(s) - 97
Bits = [Bit(N) for _ in range(26)]
for i, s in enumerate(S):
Bits[en2asc(s)].add(i + 1, 1)
for _ in range(Q):
q = input().split()
if q[0] == '1':
i, c = int(q[1]), q[2]
old = S[i - 1]
Bits[en2asc(old)].add(i, -1)
Bits[en2asc(c)].add(i, 1)
S[i - 1] = c
else:
l, r = int(q[1]), int(q[2])
ans = 0
for b in Bits:
ans += bool(b.sum(r) - b.sum(l - 1))
print(ans)
|
#!/usr/bin python3
# -*- coding: utf-8 -*-
from bisect import bisect_left, bisect_right
n = int(input())
s = list(input())
q = int(input())
al = 'abcdefghijklmnopqrstuvwxyz'
dicq = {i:[] for i in al}
for i, si in enumerate(s):
dicq[si].append(i)
def q1(iq, cq):
#print(iq,cq,s[iq],dicq[s[iq]],bisect_left(dicq[s[iq]], iq))
dicq[s[iq]].pop(bisect_left(dicq[s[iq]], iq)) #更新前から削除
s[iq] = cq
dicq[cq].insert(bisect_left(dicq[cq], iq), iq) #更新後に追加
def q2(l, r):
ret = 0
for ai in al:
if len(dicq[ai])==0: continue
li = bisect_left(dicq[ai], l)
ri = bisect_right(dicq[ai], r)
if li != ri:
ret += 1
elif li == len(dicq[ai]): continue
elif dicq[ai][li] == l:
ret += 1
# print(l,r,ai,dicq[ai],li,ri,ret)
print(ret)
for i in range(q):
fg, x, y = input().split()
if fg == '1':
q1(int(x)-1, y)
else:
q2(int(x)-1, int(y)-1)
| 1 | 62,631,198,271,040 | null | 210 | 210 |
n = int(input())
arrey = [int(i) for i in input().split()]
for i in range(n):
v = arrey[i]
j = i - 1
while j >= 0 and arrey[j] > v:
arrey[j+1] = arrey[j]
j = j - 1
arrey[j+1] = v
for k in range(n):
print(arrey[k]) if k == n-1 else print(str(arrey[k])+' ',end='')
|
def insertionSort(R, n):
for i in range(1, n):
Now = R[i]
j = i - 1
while j >= 0 and R[j] > Now:
R[j + 1] = R[j]
j = j - 1
R[j + 1] = Now
trace(R, n)
def trace(R, n):
for i in range(n):
print R[i],
print
n = input()
R = map(int, raw_input().split())
trace(R, n)
insertionSort(R, n)
| 1 | 5,918,353,052 | null | 10 | 10 |
w = input()
t = ""
while 1:
try:
t += input().lower()
t += " "
except EOFError:
break
s = t.split()
count = 0
for i in s:
if i == w:
count += 1
print(count)
|
W = raw_input().lower()
c = 0
while True:
T = raw_input()
if T == "END_OF_TEXT":
break
Ti = T.lower().split()
for i in Ti:
if i == W:
c += 1
print("%d" % (c, ))
| 1 | 1,839,598,855,100 | null | 65 | 65 |
def main():
A1, A2, A3 = map(int, input().split())
if A1 + A2 + A3 >= 22:
ans = "bust"
else:
ans = "win"
print(ans)
if __name__ == "__main__":
main()
|
k=int(input())
if k%7==0:
k = 9*k//7
else:
k *= 9
f=10%k
r = -1
for i in range(10**7):
if f==1:
r = i+1
break
else:
f = f*10%k
print(r)
| 0 | null | 62,213,020,187,740 | 260 | 97 |
N,K = map(int, input().split())
A_list = sorted(list(map(int, input().split())))
if K == 1:
print(0)
exit()
MOD = 10**9 + 7
fact = [0] * (N+1)
fact_inv = [0] * (N+1)
inv = [0] * (N+1)
fact[0] = fact[1] = 1
fact_inv[0] = fact_inv[1] = 1
inv[1] = 1
for i in range(2, N):
fact[i] = fact[i-1] * i % MOD
inv[i] = MOD - inv[MOD%i] * (MOD//i) % MOD # //で良いのかな?
fact_inv[i] = fact_inv[i-1] * inv[i] % MOD
def combo(n,k):
if n < k:
return 0
if n < 0 or k < 0:
return 0
return fact[n] * (fact_inv[k] * fact_inv[n-k] % MOD) % MOD
def main():
min_sum = 0
max_sum = 0
for i in range(N-K+1):
cmb = combo(N-i-1, K-1)
min_sum -= A_list[i] * cmb
max_sum += A_list[N-i-1] * cmb
min_sum %= MOD
max_sum %= MOD
print((max_sum+min_sum + MOD )%MOD)
if __name__ == "__main__":
main()
|
N, K = map(int, input().split())
MOD = 998244353
S = []
for _ in range(K):
S.append(tuple(map(int, input().split())))
dp = [0] * (N + 1)
dp[1] = 1
sum_list = [0] * (N + 1)
sum_list[1] = 1
for i in range(2, N+1):
for L, R in S:
RR = i - L
if RR <= 0:
continue
LL = max(1, i-R)
if LL <= RR:
t = sum_list[RR] - sum_list[LL-1]
dp[i] += t
dp[i] %= MOD
sum_list[i] = (sum_list[i-1] + dp[i]) % MOD
print(dp[N])
| 0 | null | 49,333,693,525,952 | 242 | 74 |
N = int(input())
flag = 'No'
cnt = 0
for i in range(N):
d1, d2 = map(int, input().split())
if d1 == d2:
cnt += 1
if cnt == 3:
flag = 'Yes'
else:
cnt = 0
print(flag)
|
n = int(input())
f = 0
L = []
for i in range(n):
a,b = map(int, input().split())
L.append([a,b])
for i in range(2,n):
if(L[i-2][0] == L[i-2][1] and L[i-1][0] == L[i-1][1] and L[i][0] == L[i][1]):
f = 1
break
if(f):
print('Yes')
else:
print('No')
| 1 | 2,488,980,993,760 | null | 72 | 72 |
S=list(input())
while len(S)>1:
if S[0] == 'h' and S[1] == 'i':
S.remove('h')
S.remove('i')
else:
break
if len(S) == 0:
print('Yes')
else:
print('No')
|
S = input()
if len(S) % 2 != 0:
print('No')
exit()
tmp = [S[i:i+2] for i in range(0, len(S), 2)]
for s in tmp:
if s != 'hi':
print('No')
exit()
print('Yes')
| 1 | 53,081,965,297,986 | null | 199 | 199 |
coffee = input()
if coffee[2] == coffee[3] and coffee[4] == coffee[5]:
print("Yes")
else:
print("No")
|
s=str(input())
x=list(s)
if x[2]==x[3] and x[4]==x[5]:
print('Yes')
else:
print('No')
| 1 | 41,974,408,856,940 | null | 184 | 184 |
import itertools
N = int(input())
z = []
for i in range(N):
x,y = map(int,input().split())
z.append((x,y))
ALL = 1
for i in range(N):
ALL *= (i+1)
base = [i for i in range(N)]
A = list(itertools.permutations(base,N))
SUM = 0
def distance(a,b):
ax = a[0]; ay = a[1]
bx = b[0]; by = b[1]
dis = ((ax-bx)**2 + (ay-by)**2)**(0.5)
return dis
for X in A:
now = z[X[0]]
for i in range(1,N):
nxt = z[X[i]]
dif = distance(now,nxt)
#print(dif)
SUM += dif
#print(SUM,ALL)
ans = SUM/ALL
print(ans)
|
import sys
sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline
def solve():
x = int(rl())
print(x ^ 1)
if __name__ == '__main__':
solve()
| 0 | null | 75,801,949,980,862 | 280 | 76 |
n=int(input())
ac=0
wa=0
tle=0
re=0
for _ in range(n):
s=input()
if s=="AC":
ac+=1
elif s=="WA":
wa+=1
elif s=="TLE":
tle+=1
else:
re+=1
print("AC x ",ac)
print("WA x ",wa)
print("TLE x ",tle)
print("RE x ",re)
|
N = int(input())
S, T = input().split()
print("".join(s + t for s, t in zip(S, T)))
| 0 | null | 60,714,389,990,760 | 109 | 255 |
def main():
H, N = map(int, input().split(' '))
A = input().split(' ')
total = 0
for i in A:
total += int(i)
if total >= H:
print('Yes')
else:
print('No')
main()
|
n,t = map(int, input().split())
Task=[]
for i in range(n):
nm,le= map(str, input().split())
Task.append([nm,le])
T=0
while n >0:
k = Task.pop(0)
k[1]=int(k[1])
if k[1] > t:
k[1]=k[1]-t
T=T+t
Task.append(k)
n = len(Task)
else:
T=T+k[1]
k[1]=T
print(' '.join(map(str, k)))
n = len(Task)
| 0 | null | 38,767,642,206,020 | 226 | 19 |
n=int(input())
print((n//2+n%2)/n)
|
n = int(input())
al = 26
alf='abcdefghijklmnopqrstuvwxyz'
na = ''
while n >26:
na = alf[(n-1)%al]+na
n = (n-1)//al
na = alf[(n-1)%al]+na
print(na)
| 0 | null | 94,964,414,090,910 | 297 | 121 |
a, b = input().split()
ab = a*int(b)
ba = b*int(a)
if ab <= ba:
print(ab)
else:
print(ba)
|
marks=[]
while True:
s=input().split()
marks.append(s)
if ["-1","-1","-1"] in marks:
break
marks=marks[:-1]
for i in marks:
if int(i[0])==-1 or int(i[1])==-1:
print("F")
elif int(i[0])+int(i[1])>=80:
print("A")
elif 65<=int(i[0])+int(i[1])<80:
print("B")
elif 50<=int(i[0])+int(i[1])<65:
print("C")
elif 30<=int(i[0])+int(i[1])<50:
if int(i[2])>=50:
print("C")
else:
print("D")
elif int(i[0])+int(i[1])<30:
print("F")
| 0 | null | 42,741,778,026,190 | 232 | 57 |
def ok(p,w,k):
track= 1
weight= 0
flag= True
for i in w:
if i > p:
flag=False
break
elif weight+ i> p:
weight= i
track+= 1
else:
weight+= i
if track<=k and flag:
return True
else:
return False
n,k= map(int, input().split())
w=[]
for i in range(n):
w.append(int(input()))
l=0
h=10**11
while l+1 < h:
p= (l+h)//2
if ok(p,w,k):
h= p
else:
l= p
print(h)
|
n = list(map(int, list(input())))
ln = len(n)
ans = 0
for i in range(-1, -ln, -1):
if n[i] == 10:
n[i-1] += 1
continue
elif n[i] < 5:
ans += n[i]
elif n[i] > 5:
ans += 10 - n[i]
n[i-1] += 1
else:
if n[i-1] < 5:
ans += 5
else:
ans += 5
n[i-1] += 1
if n[0] == 10:
ans += 1
elif n[0] <= 5:
ans += n[0]
else:
ans += 11 - n[0]
print(ans)
| 0 | null | 35,326,323,648,850 | 24 | 219 |
import math
n = (int)(input())
x = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
ret = x[n-1]
print("{}".format(ret))
|
S=input()
if S=="hi" or S=="hihi" or S=="hihihi" or S=="hihihihi" or S=="hihihihihi":
print("Yes")
else:
print("No")
| 0 | null | 51,893,918,539,348 | 195 | 199 |
n, m, l = map(int, input().split())
mat_a = [list(map(int, input().split())) for _ in range(n)]
mat_b = [list(map(int, input().split())) for _ in range(m)]
for i in range(n):
row = []
for j in range(l):
tmp = 0
for k in range(m):
tmp += mat_a[i][k]*mat_b[k][j]
row.append(tmp)
print(*row)
|
n, m, l = map(int, input().split())
A = []
B = []
for line in range(n):
A.append(list(map(int, input().split())))
for line in range(m):
B.append(list(map(int, input().split())))
C = []
for lines in range(n):
C.append([sum([A[lines][i] * B[i][j] for i in range(m)]) for j in range(l)])
print(" ".join(map(str, C[lines])))
| 1 | 1,460,445,199,940 | null | 60 | 60 |
three_num = input()
three_num = [int(i) for i in three_num.split(" ")]
a = three_num[0]
b = three_num[1]
c = three_num[2]
cnt = 0
for i in range(a, b + 1):
if c % i == 0:
cnt += 1
print(cnt)
|
n = int(input())
alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z']
res = ''
while (n > 0):
n = n - 1
q = n % 26
n = n // 26
res = res + alphabets[q]
res = res[::-1]
print(res)
| 0 | null | 6,223,797,383,430 | 44 | 121 |
S = input()
# 高々2^3=8通りなので、全て列挙すればよい
# RRR RRS SRR RSR RSS SRS SSR SSS
ans = 0
if S == 'RRR': ans = 3
elif S == 'SRR' or S == 'RRS': ans = 2
elif S == 'SSS': ans = 0
else: ans = 1
print(ans)
|
# -*- coding: utf-8 -*-
S = input()
if 'RRR' in S:
print("3")
elif 'RR' in S:
print("2")
elif 'R' in S:
print("1")
else:
print("0")
| 1 | 4,855,447,000,320 | null | 90 | 90 |
# -*- coding: utf-8 -*-
def main():
s_length = int(raw_input())
s = [int(x) for x in raw_input().strip().split(' ')]
t_length = int(raw_input())
t = [int(x) for x in raw_input().strip().split(' ')]
counter = 0
for value in t:
if value in s:
counter += 1
print counter
if __name__ == '__main__':
main()
|
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 30 11:23:30 2018
ALDS1-4a most simple implementation using the features of the python
@author: maezawa
"""
n = int(input())
s = map(int, input().split())
q = int(input())
t = map(int, input().split())
s_set = set(s)
t_set = set(t)
sandt = s_set & t_set
print(len(sandt))
| 1 | 68,722,011,230 | null | 22 | 22 |
import numpy as np
from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse import csr_matrix
INF = 10**12
N, M, L = map(int, input().split())
A = []; B = []; C = []
for i in range(M):
a, b, c = map(int, input().split())
A.append(a - 1); B.append(b - 1); C.append(c)
A = np.array(A); B = np.array(B); C = np.array(C)
graph = csr_matrix((C, (A, B)), (N, N))
d = floyd_warshall(graph, directed=False)
d[d <= L] = 1
d[d > L] = INF
d = floyd_warshall(d, directed=False)
Q = int(input())
for i in range(Q):
s, t = map(int, input().split())
if d[s - 1][t - 1] != INF:
print(int(d[s - 1][t - 1]) - 1)
else:
print(- 1)
|
def main():
N, M = (int(i) for i in input().split())
from math import gcd
def lcm(x, y):
return x*y//gcd(x, y)
def prime_factorize(n):
res = {2: 0}
for i in range(2, 3):
if i*i > n:
break
if n % i != 0:
continue
ex = 0
while n % i == 0:
ex += 1
n //= i
res[i] = ex
if n != 1:
res[n] = 1
return res
B = [int(i)//2 for i in input().split()]
c = set()
L = 1
for b in B:
L = lcm(L, b)
c.add(prime_factorize(b)[2])
if len(c) != 1:
print(0)
else:
ans = 0
i = 1
while L*i <= M:
ans += 1
i += 2
print(ans)
if __name__ == '__main__':
main()
| 0 | null | 137,860,619,555,972 | 295 | 247 |
n = int(input())
x = input()
popcnt = x.count("1")
def powmod(lst,mod):
if not mod: return
d = 1
for i in range(n)[::-1]:
d %= mod
lst[i] = d
d *= 2
def xmod(mod):
if not mod: return
res = 0
d = 1
for i in range(n)[::-1]:
d %= mod
if int(x[i]):
res += d
res %= mod
d *= 2
return res
def solve(x):
res = 1
while x != 0:
popcnt = 0
for i in range(20):
if x>>i & 1: popcnt += 1
x %= popcnt
res += 1
return res
powlst0 = [0]*n
powlst1 = [0]*n
powmod(powlst0,popcnt+1)
x0 = xmod(popcnt+1)
powmod(powlst1,popcnt-1)
x1 = xmod(popcnt-1)
for i in range(n):
if int(x[i]):
if popcnt == 1: print(0)
else: print(solve((x1-powlst1[i])%(popcnt-1)))
else: print(solve((x0+powlst0[i])%(popcnt+1)))
|
import sys
#DEBUG=True
DEBUG=False
if DEBUG:
f=open("202007_2nd/D_input.txt")
else:
f=sys.stdin
#from 2nd count
def cnt_func(num):
cnt=0
while num:
num%=bin(num).count("1")
cnt+=1
return cnt+1
n=int(f.readline().strip())
s=f.readline().strip()
input_val=int(s,2)
input_one_count=s.count("1")
#Base value
zero_first=input_val%(input_one_count+1)
one_first=input_val%(input_one_count-1) if input_one_count>1 else 0
ans=0
for idx,emt in enumerate(s):
p=n-idx-1
if emt=="0": #inverted to "1"
ans=cnt_func((zero_first+(pow(2,p,input_one_count+1)))%(input_one_count+1))
else: #inverted to "0"
if input_one_count>1:
ans=cnt_func((one_first-(pow(2,p,input_one_count-1)))%(input_one_count-1))
else:
ans=0
print(ans)
f.close()
| 1 | 8,173,646,699,848 | null | 107 | 107 |
h,m,H,M,K=map(int,input().split())
pre=60*h+m
post=60*H+M
print(post-pre-K)
|
import math
def kochcurve(n,p1x,p1y,p2x,p2y):
if n==0:
print("%5f %5f"%(p2x,p2y))
return
sx=(p2x-p1x)/3.0+p1x
sy=(p2y-p1y)/3.0+p1y
tx=(p2x-p1x)*2.0/3+p1x
ty=(p2y-p1y)*2.0/3+p1y
ux=math.cos(math.pi/3)*(tx-sx)-math.sin(math.pi/3)*(ty-sy)+sx
uy=math.sin(math.pi/3)*(tx-sx)+math.cos(math.pi/3)*(ty-sy)+sy
kochcurve(n-1,p1x,p1y,sx,sy)
kochcurve(n-1,sx,sy,ux,uy)
kochcurve(n-1,ux,uy,tx,ty)
kochcurve(n-1,tx,ty,p2x,p2y)
n=input()
p1x=0.0
p1y=0.0
p2x=100.0
p2y=0.0
print("%5f %5f"%(p1x,p1y))
kochcurve(n,p1x,p1y,p2x,p2y)
| 0 | null | 9,006,499,459,460 | 139 | 27 |
# -*- coding: utf-8 -*-
def main():
import sys
input = sys.stdin.readline
d, t, s = map(int, input().split())
if s * t >= d:
print('Yes')
else:
print('No')
if __name__ == '__main__':
main()
|
def dts():
return list(map(int,input().split()))
d,t,s=dts()
if d>t*s:
print("No")
else:
print("Yes")
| 1 | 3,533,907,084,592 | null | 81 | 81 |
n=int(input())
ans=[]
for i in range(55000):
if int(i*1.08)==n:
ans.append(i)
if ans==[]:
print(':(')
else:
print(ans[0])
|
i = 0
while True:
x = int(input()); i += 1
if x == 0:
break
print("Case {}: {}".format(i, x))
| 0 | null | 63,101,905,515,740 | 265 | 42 |
if __name__ == '__main__':
N, K = map(int, input().split())
have_snack = []
for i in range(K):
n = int(input())
have_snack.extend(list(map(int, input().split())))
ans = N - len(set(have_snack))
print(ans)
|
#!/usr/bin/env python3
def main():
from collections import defaultdict
N = int(input())
S = [input() for _ in range(N)]
d = defaultdict(int)
for s in S:
d[s] += 1
# d = sorted(d.items())
d = sorted(d.items(), key=lambda d: d[1], reverse=True)
res = d[0][1]
lst = []
for i in d:
if res > i[1]:
break
lst.append(i[0])
res = i[1]
for i in sorted(lst):
print(i)
if __name__ == '__main__':
main()
| 0 | null | 47,302,022,555,282 | 154 | 218 |
a,b,x=map(int,input().split())
import math
v=a*a*b
if x>=v/2:
print(180*math.atan(2*(v-x)/(a*a*a))/math.pi)
else:
print(90-(180*math.atan(2*x/(a*b*b)))/math.pi)
|
a, b, x = map(int, input().split())
if x >= a**2*b/2:
t = 2*(b*a**2-x)/(a**3)
else:
t = (a*b**2)/(2*x)
import math
p = math.atan(t)
p = math.degrees(p)
print(p)
| 1 | 163,284,022,101,072 | null | 289 | 289 |
x = int(input())
ans = 0
a = int(x / 500)
ans = ans + a * 1000
x = x % 500
b = int(x / 5)
ans = ans + b * 5
print(ans)
|
X = int(input())
ans=0
if X < 500:
print((X // 5) * 5)
else:
ans += (X // 500)*1000
X -= (X//500)*500
ans += (X//5)*5
print(ans)
| 1 | 42,445,747,885,360 | null | 185 | 185 |
A = map(int, input().split())
print('bust' if(sum(A) >= 22) else 'win')
|
print('bust' if sum(list(map(int, input().split()))) >= 22 else 'win')
| 1 | 118,839,756,560,512 | null | 260 | 260 |
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 6)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"
AL = al.upper()
n=k()
l=l()
a=[]
for i in range(n):
a.append(0)
for i in range(n-1):
a[l[i]-1]+=1
for i in range(n):
print(a[i])
|
n = int(input())
a = list(map(int, input().split()))
ans = [0]*n
for boss in a:
ans[boss-1] += 1
for sub in ans:
print(sub)
| 1 | 32,521,356,910,432 | null | 169 | 169 |
import math
def prime(x):
for i in range(2, int(math.sqrt(x))+1):
if x % i == 0:
return False
return True
n = int(raw_input())
a = []
for i in range(n):
a.append(int(raw_input()))
num = 0
for i in range(n):
if prime(a[i]):
num += 1
print num
|
from math import sqrt
n = int(input())
p = 0
for i in range(n):
x = int(input())
ad = 1
j = 2
while j < (int(sqrt(x))+1):
if x%j == 0:
ad = 0
break
else:
j += 1
p += ad
print(p)
| 1 | 10,747,498,252 | null | 12 | 12 |
def kochfunc(n1, x1, y1, x2, y2): #関数作成
if n1 > n-1: #breakの条件
return
root3 = 1.732050807
sx = (x1*2 + x2)/3 ; sy = (y1*2 + y2)/3
tx = (x1 + x2*2)/3 ; ty = (y1 + y2*2)/3
vx = tx-sx; vy = ty-sy
ux = sx + (vx - vy*root3)/2
uy = sy + (vx*root3 + vy)/2
kochfunc(n1+1, x1, y1, sx, sy) #再帰関数
print('{:.8f}'.format(sx), '{:.8f}'.format(sy))
kochfunc(n1+1, sx, sy, ux, uy)
print('{:.8f}'.format(ux), '{:.8f}'.format(uy))
kochfunc(n1+1, ux, uy, tx, ty)
print('{:.8f}'.format(tx), '{:.8f}'.format(ty))
kochfunc(n1+1, tx, ty, x2, y2)
n = int(input())
print('{:.8f}'.format(0), '{:.8f}'.format(0))
kochfunc(0, 0, 0, 100, 0)
print('{:.8f}'.format(100), '{:.8f}'.format(0))
|
N,X = map(int,input().split())
Y = N - sum(map(int,input().split()))
print(("-1",Y)[Y >= 0])
| 0 | null | 16,093,215,728,548 | 27 | 168 |
N = int(input())
S = str(input())
r_cnt = S.count('R')
g_cnt = S.count('G')
b_cnt = S.count('B')
ans = r_cnt*g_cnt*b_cnt
for i in range(N):
for d in range(1, N):
j = i + d
k = j + d
if k >= N:break
if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
ans -= 1
print(ans)
|
import sys
sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip())) #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split()) #空白あり
def LS2(): return list(sys.stdin.readline().rstrip()) #空白なし
N = I()
S = [''] + LS2()
R = [0]*(N+1)
G = [0]*(N+1)
B = [0]*(N+1)
for i in range(1,N+1):
s = S[i]
R[i] = R[i-1]
G[i] = G[i-1]
B[i] = B[i-1]
if s == 'R':
R[i] += 1
elif s == 'G':
G[i] += 1
else:
B[i] += 1
ans = 0
for i in range(2,N):
if S[i] == 'R':
ans += G[i-1]*(B[-1]-B[i])
ans += B[i-1]*(G[-1]-G[i])
for j in range(1,N+1):
if 1 <= i-j <= N and 1 <= i+j <= N:
if (S[i-j] == 'G' and S[i+j] == 'B') or (S[i-j] == 'B' and S[i+j] == 'G'):
ans -= 1
elif S[i] == 'G':
ans += R[i-1]*(B[-1]-B[i])
ans += B[i-1]*(R[-1]-R[i])
for j in range(1,N+1):
if 1 <= i-j <= N and 1 <= i+j <= N:
if (S[i-j] == 'R' and S[i+j] == 'B') or (S[i-j] == 'B' and S[i+j] == 'R'):
ans -= 1
else:
ans += G[i-1]*(R[-1]-R[i])
ans += R[i-1]*(G[-1]-G[i])
for j in range(1,N+1):
if 1 <= i-j <= N and 1 <= i+j <= N:
if (S[i-j] == 'R' and S[i+j] == 'G') or (S[i-j] == 'G' and S[i+j] == 'R'):
ans -= 1
print(ans)
| 1 | 36,151,232,664,456 | null | 175 | 175 |
import sys
input = sys.stdin.readline
s = list(input())
a = 0
#print(len(s))
for i in range((len(s) - 1) // 2):
#print(s[i], s[-2-i])
if s[i] != s[-2-i]:
a += 1
print(a)
|
s = input()
a = len(s)//2
count = 0
for i in range(a):
if s[i] != s[-i-1]:
count += 1
print(count)
| 1 | 120,339,234,144,232 | null | 261 | 261 |
while True:
deck = input()
if deck == '-':
break
m = int(input())
h = [int(input()) for i in range(m)]
for i in h:
deck = deck[i:] + deck[:i]
print(deck)
|
def main() :
s = input()
t = input()
if (t in s) :
print(0)
return
difference = 99999999
for offset in range(0,len(s) - len(t)+1):
s2 = s[offset:offset+len(t)]
diff = 0
for i in range(len(t)) :
if (s2[i] != t[i]) :
diff += 1
if (difference > diff) :
difference = diff
print(difference)
main()
| 0 | null | 2,774,803,112,018 | 66 | 82 |
n,k = map(int,input().split())
a = list(map(int,input().split()))
f = list(map(int,input().split()))
a.sort()
f.sort(reverse = True)
def ok(p):
result = 0
for i in range(n):
result += max(a[i]-p//f[i],0)
if result <= k:
return True
return False
L = 0
U = a[-1]*f[0]
while U-L >= 1:
M = (U+L)//2
if ok(M):
U = M
else:
L = M+1
print(U)
|
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")
n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
f = list(map(int, input().split()))
a.sort()
f.sort(reverse=True)
def sub(x):
val = 0
for aa,ff in zip(a,f):
val += max(0, aa - (x//ff))
return val<=k
if sub(0):
ans = 0
else:
l = 0
r = sum(aa*ff for aa,ff in zip(a,f))
while l+1<r:
m = (l+r)//2
if sub(m):
r = m
else:
l = m
ans = r
print(ans)
| 1 | 165,166,147,842,310 | null | 290 | 290 |
# -*- coding: utf-8 -*-
n = int(raw_input())
tp = hp = 0
for i in range(n):
ts, hs = map(str, raw_input().split())
if ts > hs:
tp += 3
elif ts < hs:
hp += 3
else:
tp += 1
hp += 1
print "%d %d" %(tp, hp)
|
n = int(input())
ret = 0
for i in range(n+1):
"""
if i % 3 == 0 and i % 5 == 0:
ret.append('FizzBuzz')
elif i % 3 == 0:
ret.append('Fizz')
elif i % 5 == 0:
ret.append('Buzz')
else:
"""
if i % 3 != 0 and i % 5 != 0:
ret += i
print(ret)
| 0 | null | 18,306,753,695,200 | 67 | 173 |
l=input().split()
n=int(l[0])
m=int(l[1])
#receive vecter a(n*m)
i=0
A=[]
while i<n:
a=input().split()
for p in a:
A.append(int(p))
i+=1
#receive vecter b(m*1)
I=0
B=[]
while I<m:
b=int(input())
B.append(b)
I+=1
#Ci=ai1b1+ai2b2+...+aimbm
#C[i]=a[m*(i)+1]*b[1]+a[m*(i)+2]*b[2]+...a[m*(i)+m]*b[m]
q=0
C=[]
while q<n:
Q=0
cq=0
while Q<m:
cq+=A[m*q+Q]*B[Q]
Q+=1
C.append(cq)
q+=1
for x in C:
print(x)
|
readline = open(0).readline
N, M = map(int, readline().split())
A = [list(map(int, readline().split())) for i in range(N)]
B = [int(readline()) for i in range(M)]
print(*map((lambda Ai: sum(a*b for a, b in zip(Ai, B))), A), sep='\n')
| 1 | 1,153,567,838,696 | null | 56 | 56 |
# AAA or BBB ならNo
S = list(input())
S_sorted = ''.join(sorted(S))
# print(S_sorted)
if S_sorted == 'AAA' or S_sorted == 'BBB':
print('No')
else:
print('Yes')
|
import sys
import copy
import math
import itertools
H = int(input())
cnt = 0
for i in range(int(math.log2(H))+1):
cnt+=2**i
print(cnt)
| 0 | null | 67,555,222,790,538 | 201 | 228 |
A, B = [int(v) for v in input().split()]
print(A * B)
|
def solve():
N = int(input())
dfs("", 0, N)
def dfs(cur, n_type, N):
if len(cur) == N:
print(cur)
return
for offset in range(n_type+1):
next_chr = chr(ord('a') + offset)
next_n_type = n_type + 1 if offset==n_type else n_type
dfs(cur+next_chr, next_n_type, N)
return
if __name__ == '__main__':
solve()
| 0 | null | 34,388,126,502,660 | 133 | 198 |
a,b = map(int, raw_input().split())
A = [map(int, raw_input().split()) for _ in xrange(a)]
B = [input() for _ in xrange(b)]
C = [sum([A[i][j] * B[j] for j in xrange(b)]) for i in xrange(a)]
for x in C:
print x
|
# coding:utf-8
array = map(int, raw_input().split())
n = array[0]
m = array[1]
a = [[0 for i in range(m)] for j in range(n)]
b = [0 for i in range(m)]
answer = [0 for i in range(n)]
for i in range(n):
a[i] = map(int, raw_input().split())
for j in range(m):
b[j] = input()
for i in range(n):
for j in range(m):
answer[i] += a[i][j] * b[j]
for i in range(n):
print answer[i]
| 1 | 1,162,117,091,632 | null | 56 | 56 |
#!python3.8
# -*- coding: utf-8 -*-
# abl/abl_a
import sys
s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))
def main():
K = i2n()
print('ACL' * K)
return
main()
|
import sys
import math
#from queue import *
import random
#sys.setrecursionlimit(int(1e6))
input = sys.stdin.readline
############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
def inp():
return(int(input()))
def inara():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
################################################################
############ ---- THE ACTUAL CODE STARTS BELOW ---- ############
n=inp()
print("ACL"*n)
| 1 | 2,205,279,000,638 | null | 69 | 69 |
def divisors(N):
U = int(N ** 0.5) + 1
L = [i for i in range(1, U) if N % i == 0]
return L + [N // i for i in reversed(L) if N != i * i]
def solve(k):
n = N
while n % k == 0:
n //= k
return (n % k == 1)
N = int(input())
K = set(divisors(N) + divisors(N - 1)) - {1}
print(sum(solve(k) for k in K))
|
S = input()
flag = 0
for i in range(0, len(S), 1):
if S[i] != S[len(S)-1-i]:
flag = 1
for i in range(0, len(S)//2, 1):
if S[i] != S[len(S)//2-1-i]:
flag = 1
if flag == 0:
print("Yes")
else:
print("No")
| 0 | null | 43,573,012,345,760 | 183 | 190 |
def main():
n = int(input())
T = tuple(map(int, input()))
ans = 0
for i in range(0, 1000):
a = i // 100
b = (i // 10) % 10
c = i % 10
flag1 = False
flag2 = False
flag3 = False
for t in T:
if flag2:
if t == c:
flag3 = True
break
if flag1:
if t == b:
flag2 = True
continue
if t == a:
flag1 = True
if flag3:
ans += 1
print(ans)
if __name__ == "__main__":
main()
|
n = int(input())
s_lst = list(str(input()) for _ in range(n))
count_lst = [0, 0, 0, 0]
for i in range(n):
consequence = s_lst[i]
if consequence == 'AC':
count_lst[0] += 1
elif consequence == 'WA':
count_lst[1] += 1
elif consequence == 'TLE':
count_lst[2] += 1
else:
count_lst[3] += 1
consequence_lst = ['AC x {}'.format(count_lst[0]), 'WA x {}'.format(count_lst[1]), 'TLE x {}'.format(count_lst[2]), 'RE x {}'.format(count_lst[3])]
for i in range(4):
con = consequence_lst[i]
print(con)
| 0 | null | 68,381,982,029,368 | 267 | 109 |
import sys
input = sys.stdin.readline
from collections import Counter
def rle(list):
dict = {}
for i in list:
if i in dict.keys():
dict[i] += 1
else:
dict[i] = 1
return dict
n = int(input())
a = list(map(int, input().split()))
minus = Counter([i-a[i] for i in range(n)])
plus = Counter([i+a[i] for i in range(n)])
cnt = 0
for i in minus:
if i in plus:
cnt += minus[i] * plus[i]
print(cnt)
|
# import itertools
# import math
# import sys
# sys.setrecursionlimit(500*500)
# import numpy as np
# import heapq
# from collections import deque
N = int(input())
# S = input()
# n, *a = map(int, open(0))
# N, M = map(int, input().split())
A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()
# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)
# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)
# def dfs(tree, s):
# for l in tree[s]:
# if depth[l[0]] == -1:
# depth[l[0]] = depth[s] + l[1]
# dfs(tree, l[0])
# dfs(tree, 1)
# def factorization(n):
# arr = []
# temp = n
# for i in range(2, int(-(-n**0.5//1))+1):
# if temp%i==0:
# cnt=0
# while temp%i==0:
# cnt+=1
# temp //= i
# arr.append([i, cnt])
# if temp!=1:
# arr.append([temp, 1])
# if arr==[]:
# arr.append([n, 1])
# return arr
A.insert(0, 0)
cnt = [0] * (N + 1)
ans = 0
for i in range(1, N + 1):
if i - A[i] >= 0:
ans += cnt[i - A[i]]
if i + A[i] <= N:
cnt[i + A[i]] += 1
print(ans)
| 1 | 25,972,558,143,690 | null | 157 | 157 |
while True:
n = int(input())
if(n == 0):
break
i = n%10
x = n//10
while(x):
i = i + (x%10)
x //= 10
print(i)
|
A, B = map(str, input().split())
if len(A) == 1 and len(B) == 1:
print(int(A)*int(B))
else:
print(-1)
| 0 | null | 80,017,987,762,272 | 62 | 286 |
# -*- coding: utf-8 -*-
import sys
def main():
H,W,K = list(map(int, sys.stdin.readline().split()))
C_list = [list( sys.stdin.readline().rstrip() ) for _ in range(H)]
bit_H = 1 << H
bit_W = 1 << W
cnt_K = 0
for b_H in range(bit_H):
for b_W in range(bit_W):
cnt_black = 0
for row in range(H):
for col in range(W):
if (C_list[row][col] == "#") and ( b_H & (1 << row) ) and ( b_W & (1 << col) ):
cnt_black += 1
if cnt_black == K:
cnt_K += 1
print(cnt_K)
if __name__ == "__main__":
main()
|
mod=998244353
N,M,K=map(int,input().split())
MAX_N=N+1
Fact=[0 for i in range(MAX_N+1)]
Finv=[0 for i in range(MAX_N+1)]
Fact[0]=1
for i in range(MAX_N):
Fact[i+1]=(i+1)*Fact[i]
Fact[i+1]%=mod
Finv[-1]=pow(Fact[-1],mod-2,mod)
for i in range(MAX_N-1,-1,-1):
Finv[i]=(i+1)*Finv[i+1]
Finv[i]%=mod
def C(n,k):
return (Fact[n]*Finv[k]*Finv[n-k])%mod
'''
i in range(K+1)で総和を取る
隣り合うブロックの色が同じ色なのはi通り
隣り合うブロックの色が異なるのはN-1-i通り
|の選び方はN-1Ci
同じものをつなげてN-i個のブロックと見なせる
これに対してM*((M-1)のN-1-i)乗だけある
0<=i<=K
よりN-1-i>=N-1-K>=0
問題なし
'''
ans=0
for i in range(K+1):
ans+=(C(N-1,i)*M*pow(M-1,N-1-i,mod))%mod
ans%=mod
print(ans)
| 0 | null | 16,039,120,259,008 | 110 | 151 |
import math
def is_prime(n):
global primes
if n == 1:
return False
if n == 2:
return True
if n % 2 == 0:
return False
s = int(math.sqrt(n))
for x in range(3, s + 1, 2):
if n % x == 0:
return False
else:
return True
N = int(input())
d = [int(input()) for _ in range(N)]
d.sort()
print([is_prime(x) for x in d].count(True))
|
import math
N = input()
c = 0
p = [0 for i in range(N)]
for i in range(0,N):
p[i] = input()
for i in range(N):
s = math.sqrt(p[i])
j = 2
while(j <= s):
if(p[i]%j == 0):
break
j += 1
if(j > s):
c += 1
print c
| 1 | 9,365,862,212 | null | 12 | 12 |
count = 0
INF = float('inf')
def merge(A, left, mid, right):
global count
L = A[left:mid] + [INF]
R = A[mid:right] + [INF]
i, j = 0, 0
for k in range(left, right):
count +=1
if L[i] <= R[j]:
A[k] = L[i]
i = i+1
else:
A[k] = R[j]
j = j+1
def mergeSort(A, left, right):
if left+1 < right:
mid = int((left+right)/2)
mergeSort(A, left, mid)
mergeSort(A, mid, right)
merge(A, left, mid, right)
#right は部分配列の末尾+1 の要素を指す。
n = int(input())
A = list(map(int, input().split()))
mergeSort(A, 0, n)
for i in range(n):
if i>0:
print(" ", end="")
print(A[i], end="")
print()
print(count)
|
"""
n=int(input())
s=[int(x) for x in input().split()]
q=int(input())
t=[int(x) for x in input().split()]
#print(s)
c=0
for i in t:
if i in s:
c+=1
print(c)
"""
"""
n=int(input())
s=[int(x) for x in input().split()]
q=int(input())
t=[int(x) for x in input().split()]
#binary search
a=0
for i in range(q):
L=0
R=n-1#探索する方の配列(s)はソート済みでないといけない
while L<=R:
M=(L+R)//2
if s[M]<t[i]:
L=M+1
elif s[M]>t[i]:
R=M-1
else:
a+=1
break
print(a)
"""
#Dictionary
#dict型オブジェクトに対しinを使うとキーの存在確認になる
n=int(input())
d={}
for i in range(n):
command,words=input().split()
if command=='find':
if words in d:
print('yes')
else:
print('no')
else:
d[words]=0
| 0 | null | 94,757,899,470 | 26 | 23 |
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
n,m,l = ma()
INF=10**15
cost = [[INF]*n for i in range(n)]
for i in range(m):
a,b,c = ma();a-=1;b-=1
cost[a][b]=c
cost[b][a]=c
def wf(cost):
l = len(cost[0])
for k in range(l):
for i in range(l):
for j in range(l):
cost[i][j] = min(cost[i][j],cost[i][k] + cost[k][j])
wf(cost)
rest = [[INF]*n for i in range(n)]
for i in range(n):
for j in range(n):
if cost[i][j]<=l:
rest[i][j]=1
wf(rest)
q = ni()
for i in range(q):
s,t = ma();s-=1;t-=1
print(rest[s][t] -1) if rest[s][t] !=INF else print(-1)
|
#!/usr/bin/env python
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)
inf = float("inf")
import numpy as np
import scipy.sparse.csgraph as csg
def main():
N,M,L = map(int,input().split())
ABC = [tuple(map(int,input().split())) for _ in range(M)]
Q = int(input())
ST = [tuple(map(int,input().split())) for _ in range(Q)]
# 町の最短距離を求める
A = np.array([[inf]*N]*N)
for a,b,c in ABC:
A[a-1][b-1] = c
A[b-1][a-1] = c
A = csg.floyd_warshall(A)
# 求めた町の距離が L以下 に 1の辺をはる (給油なしでたどり着ける)
B = np.array([[inf]*N]*N)
for i in range(N):
for j in range(N):
if A[i][j] <= L:
B[i][j] = 1
# 最短距離を求める
B = csg.floyd_warshall(B)
for s,t in ST:
if B[s-1][t-1] == inf:
print(-1)
else:
print(int(B[s-1][t-1] - 1))
if __name__ == "__main__":
main()
| 1 | 173,095,002,313,638 | null | 295 | 295 |
from itertools import accumulate
h,w,k=map(int,input().split())
s=[[0]*(w+1)]+[[0]+[int(i) for i in input()] for _ in range(h)]
for i in range(h+1):
s[i]=list(accumulate(s[i]))
for j in range(w+1):
for i in range(h):
s[i+1][j]+=s[i][j]
ans=10**18
for i in range(1<<h-1):
a=[]
for j in range(h-1):
if (i>>j)&1:a+=[j+1]
a+=[h]
cnt=len(a)-1
q=1
for j in range(1,w+1):
p=0
flag=0
for l in a:
if s[l][j]-s[l][j-1]-s[p][j]+s[p][j-1]>k:flag=1
elif s[l][j]-s[l][q-1]-s[p][j]+s[p][q-1]>k:
q=j
cnt+=1
break
else:p=l
if flag:break
else:
ans=min(cnt,ans)
print(ans)
|
import sys
from itertools import product
sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7
def resolve():
H, W, K = map(int, input().split())
grid = [list(input()) for _ in range(H)]
R = [[0] * (W + 1) for _ in range(H + 1)]
for h in range(H):
for w in range(W):
R[h + 1][w + 1] = R[h][w + 1] + R[h + 1][w] - R[h][w] + int(grid[h][w])
res = f_inf
for pattern in product([0, 1], repeat=H-1):
cut_H = [idx + 1 for idx, p in enumerate(pattern) if p == 1] + [H]
cnt_cut = sum(pattern)
left = 0
right = 1
flg = 1
while right <= W:
if not flg:
break
up = 0
for bottom in cut_H:
cnt_w = R[bottom][right] - R[bottom][left] - R[up][right] + R[up][left]
if cnt_w > K:
if right - left == 1:
flg = False
cnt_cut += 1
left = right - 1
break
else:
up = bottom
else:
right += 1
else:
res = min(res, cnt_cut)
print(res)
if __name__ == '__main__':
resolve()
| 1 | 48,340,313,920,298 | null | 193 | 193 |
s = input()
a = s[:len(s)//2]
b = s[len(s)//2+1:]
print('Yes' if s == s[::-1] and a == a[::-1] and b == b[::-1] else 'No')
|
from collections import Counter
n = int(input())
A = list(map(int,input().split()))
Acount = Counter(A)
total = 0
for key in Acount.keys():
total += Acount[key] * (Acount[key] - 1) // 2
for i in range(n):
tmp = total
tmp -= Acount[A[i]]*(Acount[A[i]]-1) // 2
tmp += (Acount[A[i]]-1)*(Acount[A[i]]-2) // 2
print(tmp)
| 0 | null | 47,001,464,544,148 | 190 | 192 |
def gcd(s,t):
if t==0:
return s
else:
return gcd(t, s%t)
s, t = [int(x) for x in input().split()]
GCD = gcd(s,t)
LCM = (s*t)//GCD
print(LCM)
|
def gcd(a, b):
return b if a % b == 0 else gcd(b, a % b)
def lcm(a, b):
return a // gcd(a, b) * b
a, b = map(int, input().split())
print(lcm(a, b))
| 1 | 113,331,122,322,368 | null | 256 | 256 |
N, M = map(int, input().split())
Alst = list(map(int, input().split()))
k = sum(Alst)
ans = N - k
if ans >= 0:
print(ans)
else:
print(-1)
|
s = ""
while True:
s = input()
if s == "-": break
loop_cnt = int(input())
for i in range(loop_cnt):
h = int(input())
s = s[h:] + s[:h]
else:
print(s)
| 0 | null | 16,980,928,113,618 | 168 | 66 |
M=998244353
class Factorial:
def __init__(self,n):
self.f=f=[0]*(n+1)
f[0]=b=1
for i in range(1,n+1):f[i]=b=b*i%M
self.inv=inv=[0]*(n+1)
inv[n]=b=pow(self.f[n],M-2,M)
for i in range(n,0,-1):inv[i-1]=b=b*i%M
def factorial(self,i):
return self.f[i]
def ifactorial(self,i):
return self.inv[i]
def comb(self,n,k):
if n>=k:return self.f[n]*self.inv[n-k]*self.inv[k]%M
else:return 0
comb=Factorial(10**5*5).comb
n,m,k=map(int,input().split())
print(sum(m*comb(n-1,i)*pow(m-1,~i+n,M)for i in range(k+1))%M)
|
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())), reverse=True)
a_rev = a[::-1]
ok, ng = 1, 200001
while ng-ok > 1:
x = (ok+ng)//2
num = 0
cur = 0
for i in range(n-1, -1, -1):
while cur < n and a[i] + a[cur] >= x:
cur += 1
num += cur
if num < m:
ng = x
else:
ok = x
just = ok
ans = 0
larger_cnt = 0
cur = 0
a_cum = [0]
for i in range(n):
a_cum.append(a_cum[-1] + a[i])
for i in range(n):
while cur < n and a[i] + a_rev[cur] <= just:
cur += 1
larger_cnt += n - cur
ans += (n - cur) * a[i] + a_cum[n - cur]
ans += just * (m - larger_cnt)
print(ans)
| 0 | null | 65,434,438,578,498 | 151 | 252 |
N, K = map(int ,input().split())
counter = 0
while N:
N //= K
counter += 1
print(counter)
|
a,b = input().split()
if len(a) + len(b) == 2:
print(int(a) * int(b))
else:
print(-1)
| 0 | null | 111,415,723,079,570 | 212 | 286 |
import heapq
from sys import stdin
input = stdin.readline
#入力
# s = input()
n = int(input())
# n,m = map(int, input().split())
# a = list(map(int,input().split()))
# a = [int(input()) for i in range()]
xl=[]
for i in range(n):
x,l = map(int, input().split())
xl.append((x,l))
def main():
p = []
for x,l in xl:
p.append([x-l,x+l])
p = sorted(p, key=lambda x:x[1])
ans = 0
mx = -10**10
for st in p:
s,t = st[0],st[1]
if s>=mx:
ans+=1
mx=t
print(ans)
if __name__ == '__main__':
main()
|
n = int(input())
arm = []
for _ in range(n):
x, l = map(int,input().split())
arm.append([x-l, x+l])
arm.sort(key=lambda x: x[1])
cnt = 0
pr = -float('inf')
for a in arm:
if pr <= a[0]:
cnt += 1
pr = a[1]
print(cnt)
| 1 | 89,978,724,217,228 | null | 237 | 237 |
# -*-coding:utf-8-*-
def get_input():
while True:
try:
yield "".join(input())
except EOFError:
break
if __name__=="__main__":
array = list(get_input())
for i in range(len(array)):
temp = array[i].split()
a = int(temp[0])
b = int(temp[1])
ans = a + b
print(len(str(ans)))
|
# coding=UTF-8
from collections import deque
from operator import itemgetter
from bisect import bisect_left, bisect
import itertools
import sys
import math
import numpy as np
import time
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
queue = deque()
def main():
k = int(input())
queue.extend([1, 2, 3, 4, 5, 6, 7, 8, 9])
for i in range(k-1):
x = queue[0]
queue.popleft()
if x % 10 != 0:
queue.append(10 * x + x % 10 - 1)
queue.append(10 * x + x % 10)
if x % 10 != 9:
queue.append(10 * x + x % 10 + 1)
print(queue[0])
if __name__ == '__main__':
main()
| 0 | null | 20,117,018,296,338 | 3 | 181 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.