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
|
---|---|---|---|---|---|---|
import random
S = input()
i = len(S)
m = random.randint(0,i-3)
print (S[m] + S[m+1] + S[m+2])
|
S = input()
if S[-1] == "s":
S = S + "es"
print(S)
else:
S = S + "s"
print(S)
| 0 | null | 8,508,642,807,940 | 130 | 71 |
H, N = map(int, input().split())
A = list(map(int, input().split()))
def answer(H: int, N: int, A: list) -> str:
damage = 0 # damageに0を代入します
for i in range(0, N): # 0:Nの間にiがある間は
damage += int(A[i]) # damageにリスト[i]をint(整数)に変え足します
# 6でdamage=0にforの間Aの値を足していく意味
i += 1 # 条件を満たす間はiに1つずつ足していきます。リスト内を進めていく
if damage < H:
return 'No'
else:
return 'Yes'
print(answer(H, N, A))
|
H, N = map(int, input().split())
list_A = list(map(int,input().split()))
sum_list_A = sum(list_A)
if sum_list_A >= H:
print("Yes")
else:
print("No")
| 1 | 77,957,988,040,320 | null | 226 | 226 |
n = int(input())
a = list(map(int, input().split()))
q = int(input())
mp = {}
for i in a:
mp[i] = mp.get(i, 0) + 1
total = sum(list(map(lambda item: item[0]*item[1], mp.items())))
for i in range(q):
b, c = map(int, input().split())
total -= b * mp.get(b, 0)
total += c * mp.get(b, 0)
mp[c] = mp.get(c, 0) + mp.get(b, 0)
mp[b] = 0
print(total)
|
N, X, Y = [int(x) for x in input().split()]
counter = [0] * N
for i in range(1, N + 1):
for j in range(i + 1, N + 1):
d = min(abs(i - j), abs(i - X) + abs(j - Y) + 1, abs(i - Y) + abs(j - X) + 1)
counter[d] += 1
for i in range(1, N):
print(counter[i])
| 0 | null | 28,208,979,269,988 | 122 | 187 |
import sys
def dump(x):
print(x, file=sys.stderr)
n = int(input())
print(1-n)
|
s,t,a,b,x,y=map(int,open(0).read().split())
if a*s+b*t==x*s+y*t:
print('infinity')
exit()
if a<x:a,b,x,y=x,y,a,b
if a*s+b*t>x*s+y*t:
print(0)
exit()
n=(a*s-x*s)/(x*s+y*t-(a*s+b*t))
m=int(n)
print(m*2+(n>m))
| 0 | null | 67,063,252,742,080 | 76 | 269 |
from itertools import product
n = int(input())
xy = []
for _ in range(n):
a = int(input())
tmp = [list(map(int, input().split())) for _ in range(a)]
xy.append(tmp)
ans = 0
for bit in product([0, 1], repeat=n):
for i, liar in enumerate(bit):
if liar and any(bit[x - 1] != y for x, y in xy[i]):
break
else:
ans = max(ans, sum(bit))
print(ans)
|
from collections import deque
que = deque()
l = input()
S = 0
S2 = []
for j in range(len(l)):
i = l[j]
if i == '\\':
que.append(j)
continue
elif i == '/':
if len(que) == 0:
continue
k = que.pop()
pond_sum = j-k
S += pond_sum
while S2 and S2[-1][0] > k:
pond_sum += S2[-1][1]
S2.pop()
S2.append([k,pond_sum])
elif i == '_':
continue
data = [i for j,i in S2]
print(S)
print(len(S2),*data)
| 0 | null | 61,004,488,265,030 | 262 | 21 |
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
from collections import Counter
n, p = map(int, readline().split())
s = readline().rstrip().decode()
cnt = 0
if p == 2 or p == 5:
for i, ss in enumerate(s):
if int(ss) % p == 0:
cnt += (i + 1)
else:
memo = [0]
m = 0
d = 1
for ss in s[::-1]:
m += int(ss) * d
m %= p
d *= 10
d %= p
memo.append(m)
counter = Counter(memo)
for i in range(p + 1):
cnt += counter[i] * (counter[i] - 1) // 2
print(cnt)
|
# coding: utf-8
a, b = input().split()
aaa = ""
bbb = ""
for i in range(int(b)):
aaa += a
for i in range(int(a)):
bbb += b
if aaa > bbb:
print(bbb)
else:
print(aaa)
| 0 | null | 71,149,725,334,684 | 205 | 232 |
h,w,m = map(int,input().split())
hw = [list(map(int,input().split())) for _ in range(m)]
hlist = [0]*(h+1)
wlist = [0]*(w+1)
for x,y in hw:
hlist[x]+=1
wlist[y]+=1
hmax=max(hlist)
h_index = {n for n, v in enumerate(hlist) if v == hmax}
wmax=max(wlist)
w_index = {n for n, v in enumerate(wlist) if v == wmax}
count = sum(x in h_index and y in w_index for x,y in hw)
print(hmax+wmax-(len(h_index)*len(w_index)==count))
|
n = int(input())
l = list(map(int, input().split()))
f = 1; c = 0
while f > 0:
f = 0
for j in range(n-1, 0, -1):
if l[j] < l[j-1]:
l[j], l[j-1] = l[j-1], l[j]
f = 1; c += 1
print(*l)
print(c)
| 0 | null | 2,354,227,350,464 | 89 | 14 |
# -*- coding: utf-8 -*-
i = 1
while (i <= 9) :
j = 1
while (j <= 9) :
print '%sx%s=%s' % (i, j, i * j)
j = j + 1
i = i + 1
|
def exe():
for i in range(1,10):
for j in range(1,10):
print(str(i)+'x'+str(j)+'='+str(i*j))
if __name__ == '__main__':
exe()
| 1 | 1,010,252 | null | 1 | 1 |
while True:
c = 0
n, x= map(int, input().split())
if n == 0 and x == 0:
break
for i in range(1, n + 1):
for j in range(1, n + 1 ):
if j <= i:
continue
for k in range(1, n + 1):
if k <= j:
continue
if i != j and i != k and j != k and i + j + k == x:
c += 1
print(c)
|
while True:
n, goal = [int(x) for x in input().split(" ")]
res =[]
if n == goal == 0:
break
for first in range(1, n+1):
for second in range(first+1, n+1):
rem = goal - (first + second)
if rem > second and n >= rem:
res.append([first, second, rem])
print(len(res))
| 1 | 1,277,476,866,080 | null | 58 | 58 |
x=input()
x=x**3
print x
|
import sys
x = int(sys.stdin.read())
print(x ** 3)
| 1 | 276,252,346,140 | null | 35 | 35 |
H,W,K=map(int,input().split())
c_map=[]
for _ in range(H):
c=input()
c_map.append(c)
ans=0
for i in range(2**H):
for j in range(2**W):
cnt=0
for k in range(H):
for l in range(W):
if c_map[k][l]=='#' and (i>>k)&1==0 and (j>>l)&1==0:
cnt+=1
if cnt==K:
ans+=1
print(ans)
|
N = int(input())
def answer(N: int) -> str:
x = 0
for i in range(1, 10):
for j in range(1, 10):
if N == i * j:
x = 1
return 'Yes'
if x == 0:
return 'No'
print(answer(N))
| 0 | null | 84,104,635,107,596 | 110 | 287 |
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()
|
X, K, D = map(int, input().split())
X = abs(X)
if X - K * D >= 0:
print(X - K * D)
exit()
else:
n = X // D
x = X - n * D
if (K - n) % 2 == 0:
print(x)
else:
print(min(x + D, D - x))
| 0 | null | 29,015,230,045,570 | 198 | 92 |
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = list(input())
for n in range(N-K):
if T[n+K]==T[n]:
T[n+K]=""
print(P*T.count("r")+R*T.count("s")+S*T.count("p"))
|
word = raw_input()
text = []
while True:
raw = raw_input()
if raw == "END_OF_TEXT":
break
text += raw.lower().split()
print(text.count(word))
# print(text.lower().split().count(word))
| 0 | null | 54,663,150,860,000 | 251 | 65 |
a,b = input().split()
if len(a) + len(b) == 2:
print(int(a) * int(b))
else:
print(-1)
|
a,b=map(int,input().split())
if a<=9:
if b<=9:
print(a*b)
else:
print("-1")
else:
print("-1")
| 1 | 158,864,301,378,742 | null | 286 | 286 |
n, a, b = map(int, input().split())
N = n//(a+b)
m = n%(a+b)
print(N*a+min(m,a))
|
X, Y, A, B, C = 2, 2, 2, 2, 2
prr = [8, 6]
qrr = [9, 1]
krr = [2, 1]
X, Y, A, B, C = 2, 2, 4, 4, 4
prr = [11, 12, 13, 14]
qrr = [21, 22, 23, 24]
krr = [1, 2, 3, 4]
X, Y, A, B, C = 1, 2, 2, 2, 1
prr = [2, 4]
qrr = [5, 1]
krr = [3]
X, Y, A, B, C = map(int,input().split())
prr = list(map(int,input().split()))
qrr = list(map(int,input().split()))
krr = list(map(int,input().split()))
def calculate(x, y, a, b, c, prr, qrr, krr):
prr = sorted(prr,key=lambda x:-x)
qrr = sorted(qrr, key=lambda x: -x)
prr = prr[:x]
qrr = qrr[:y]
result = []
result.extend(prr)
result.extend(qrr)
result.extend(krr)
result = sorted(result,key=lambda x:-x)
result = result[:(x+y)]
print(sum(result))
calculate(X, Y, A, B, C, prr, qrr, krr)
| 0 | null | 50,272,913,446,568 | 202 | 188 |
#!/usr/bin/env python3
#ABC146 F
import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
n,m = LI()
s = input()
i = 0
cnt = 0
M = 0
while i < n+1:
if s[i] == '1':
cnt += 1
M = max(M,cnt)
else:
cnt = 0
i += 1
if M >= m:
print(-1)
quit()
lst = []
now = n
while now > 0:
if now - m <= 0:
lst.append(now)
now -= m
else:
for j in range(1,m+1)[::-1]:
if s[now-j] == '0':
lst.append(j)
now -= j
break
print(*lst[::-1])
|
def merge(A, left, mid, right):
inf = float('inf')
L = A[left:mid]+[inf]
R = A[mid:right] + [inf]
i = j = 0
for k in range(left,right):
if L[i] <= R[j]:
A[k] = L[i]
i += 1
else:
A[k] = R[j]
j += 1
return right - left
def mergeSort(A, left, right, count = 0):
if left+1 < right:
mid = (left + right)//2
count = mergeSort(A, left, mid, count)
count = mergeSort(A, mid, right, count)
count += merge(A, left, mid, right)
return count
n = int(input())
A = list(map(int, input().split()))
ans = mergeSort(A, 0, n)
s = list(map(str, A))
print(' '.join(s))
print(ans)
| 0 | null | 69,246,945,492,960 | 274 | 26 |
from bisect import bisect_left
N, M = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))
l = 0
r = 2*10**5+5
while l+1 < r:
mid = (l+r) // 2
m = 0
for a in A:
idx = bisect_left(A, mid-a)
m += N-idx
if m >= M: l = mid
else: r = mid
s = [0] * (N+1)
for i in range(N):
s[i+1] = s[i] + A[i]
ans = 0
m = 0
for a in A:
idx = bisect_left(A, l-a)
m += N-idx
ans += s[N]-s[idx]
ans += a*(N-idx)
ans -= (m-M)*l
print(ans)
|
import bisect
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
a_cum = [0 for _ in range(n+1)]
for i in range(n):
a_cum[i+1] = a_cum[i] + a[i]
l, r = 0, 10 ** 6
while r - l > 1:
x = (l + r) // 2
cnt = 0
for i in range(n):
idx = bisect.bisect_left(a, x-a[i])
cnt += n - idx
if cnt >= m:
l = x
else:
r = x
ans = 0
cnt = 0
for i in range(n):
idx = bisect.bisect_left(a, l-a[i])
cnt += n - idx
ans += a_cum[n] - a_cum[idx]
ans += a[i] * (n - idx)
if cnt > m:
ans -= l * (cnt - m)
print(ans)
| 1 | 108,033,554,767,710 | null | 252 | 252 |
def main():
n = int(input())
a = list(map(int, input().split()))
dp = [[[-float("inf")]*2 for j in range(3)] for i in range(n+1)]
#dp[index][i//2+j個選んだ][直前の値を採用したか否か]
dp[0][0][0] = 0
for i in range(n):
x = a[i]
if i%2 == 0:
for j in range(-1, 2):
dp[i+1][j][0] = max(dp[i][j][0], dp[i][j][1])
for j in range(-1, 1):
dp[i+1][j+1][1] = dp[i][j][0] + x
else:
for j in range(-1, 1):
dp[i+1][j][0] = max(dp[i][j+1][0], dp[i][j+1][1])
for j in range(-1, 2):
dp[i+1][j][1] = dp[i][j][0] + x
print(max(dp[n][0][0], dp[n][0][1]))
if __name__ == "__main__":
main()
|
def myfindall(s,ps):
# start = 0
matchlist =[]
while True:
tmp = s.find(ps)
if tmp < 0 :
break
if len(matchlist)>0:
matchlist.append(tmp+matchlist[-1]+1)
else:
matchlist.append(tmp)
s = s[tmp+1:]
return matchlist
instr = raw_input()
findstr = raw_input()
matchlist = myfindall(instr,findstr[0])
matchflag = False
y=0
for x in matchlist:
if matchflag:
break
while True:
if y >= len(findstr):
print "Yes"
matchflag = True
break
if x >= len(instr):
x = 0
if instr[x]==findstr[y]:
x+=1
y+=1
else:
y=0
break
if not matchflag:
print "No"
| 0 | null | 19,422,964,032,160 | 177 | 64 |
V = input()
s = 0
for c in V:
n = int(c)
s = s + n
if s%9 == 0:
print("Yes")
else:
print("No")
|
n = int(input())
xs = list(enumerate(map(int, input().split())))
xs.sort(key=lambda x: x[1])
xs.reverse()
dp = [[0 for _ in range(n + 1)] for _ in range(n + 1)]
for i in range(1, n + 1):
(j, a) = xs[i - 1]
for x in range(0, i + 1):
y = i - x
if x == 0:
dp[x][y] = dp[x][y - 1] + a * (n - y - j)
elif y == 0:
dp[x][y] = dp[x - 1][y] + a * (j - x + 1)
else:
dp[x][y] = max(dp[x][y - 1] + a * (n - y - j),
dp[x - 1][y] + a * (j - x + 1))
print(max([dp[i][n - i] for i in range(n + 1)]))
| 0 | null | 19,092,034,534,560 | 87 | 171 |
from itertools import groupby
s = input()
A = [(key, sum(1 for _ in group)) for key, group in groupby(s)]
tmp = 0
ans = 0
for key, count in A:
if key == '<':
ans += count*(count+1)//2
tmp = count
else:
if tmp < count:
ans -= tmp
ans += count
ans += (count-1)*count//2
tmp = 0
print(ans)
|
S = input()
N = len(S)
A = [-1 for i in range(N+1)]
if S[0] == "<":
A[0] = 0
if S[-1] == ">":
A[-1] = 0
for i in range(N):
if S[i:i+2] == "><":
A[i+1] = 0
#print(A)
for i in range(N):
if S[i] == "<":
A[i+1] = max(A[i]+1, A[i+1])
for i in range(N):
if S[N-i-1] == ">":
A[N-i-1] = max(A[N-i]+1, A[N-i-1])
#print(A)
print(sum(A))
| 1 | 156,300,459,696,160 | null | 285 | 285 |
n = int(input())
print((n+1)//2)
|
import math
N = int(input())
O = N // 2 + N % 2
print(O)
| 1 | 58,764,209,966,590 | null | 206 | 206 |
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from functools import reduce, lru_cache
import collections, heapq, itertools, bisect
import math, fractions
import sys, copy
sys.setrecursionlimit(1000000)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline().rstrip())
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline().rstrip())
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
MOD = 1000000007
def main():
K, N = LI()
A = LI()
res = [A[0] + K - A[-1]]
for i in range(N-1):
res.append(A[i+1] - A[i])
res = sorted(res)
print(K - res[-1])
if __name__ == '__main__':
main()
|
K, N = map(int, input().split())
A = list(map(int, input().split()))
a_old = A[0]
ma = A[0] + K - A[-1]
for a in A[1:]:
ma = max(ma, a - a_old)
a_old = a
print(K-ma)
| 1 | 43,418,979,065,290 | null | 186 | 186 |
n,k=map(int,input().split())
k_list=[]
for i in range(k):
l,r=map(int,input().split())
k_list.append([l,r])
dp=[0]*(n+1)
dp[1]=1
dpsum=[0]*(n+1)
dpsum[1]=1
for i in range(1,n):
dpsum[i]=dp[i]+dpsum[i-1]
for j in range(k):
l,r=k_list[j]
li=i+l
ri=i+r+1
if li<=n:
dp[li]+=dpsum[i]
dp[li]=dp[li]%998244353
if ri<=n:
dp[ri]-=dpsum[i]
dp[ri]=dp[ri]%998244353
print(dp[n])
|
input()
s = set(map(int,input().split()))
n = int(input())
t = tuple(map(int,input().split()))
c = 0
for i in range(n):
if t[i] in s:
c += 1
print(c)
| 0 | null | 1,394,585,701,418 | 74 | 22 |
from collections import deque
n = int(input())
graph_W = []
for _ in range(n):
graph_W.append(list(map(int, input().split())))
num_root = [0]*n
work_queue = deque([])
visited = set()
work_queue.append(1)
visited.add(1)
cnt = 0
while work_queue:
here = work_queue.popleft() - 1
num_node = graph_W[here][1]
cnt += 1
if num_node == 0:
continue
for k in range(num_node):
num_graph = graph_W[here][k+2]
if num_graph not in visited:
work_queue.append(num_graph)
visited.add(num_graph)
num_root[num_graph-1] = num_root[here] + 1
print(1,0)
for i in range(1,n):
if num_root[i] != 0:
print(i+1,num_root[i])
else:
print(i+1,-1)
|
n = int(input())
adj_list = [[] for _ in range(n + 1)]
for i in range(n):
tmp = list(map(int, input().split()))
u = tmp[0]
k = tmp[1]
for v in range(2, 2 + k):
adj_list[u].append(tmp[v])
d = [-1] * (n + 1)
q = []
q.append(1)
d[1] = 0
while len(q) > 0:
u = q.pop(0)
for v in adj_list[u]:
if d[v] == -1:
d[v] = d[u] + 1
q.append(v)
for idx, d in enumerate(d):
if idx != 0:
print("%d %d" % (idx, d))
| 1 | 4,004,088,480 | null | 9 | 9 |
def resolve():
S, T = input().split()
A, B = list(map(int, input().split()))
U = input()
if S == U:
A -= 1
else:
B -= 1
print(A, B)
if '__main__' == __name__:
resolve()
|
def main():
n = input()
l = len(n)
ans = 0
p = 0
for i in range(l-1, -1, -1):
v = int(n[i])
if p == 1:
v += 1
if v > 5 or (i != 0 and v == 5 and int(n[i-1]) >= 5):
ans += (10-v)
p = 1
else:
ans += v
p = 0
if p == 1:
ans += 1
print(ans)
main()
| 0 | null | 71,318,301,255,120 | 220 | 219 |
from collections import deque
data = input()
tmp = deque()
se = list()
ase = list()
lakes = list()
x = 0
#check start - end
for a in data:
if a == '\\':
tmp.append(x)
if a == '/' and len(tmp) > 0 :
s = tmp.pop()
se.append([s,x])
x += 1
se.sort()
# analyse s -e
if len(se) > 0 :
ase.append(se[0])
for i in range(1,len(se)) :
s,e = ase[len(ase)-1]
if e < se[i][0]:
ase.append(se[i])
#calc lakes
for a in ase:
lsize = 0
deep = 0
lx = a[0]
for m in data[a[0]:a[1] + 1] :
if m == '\\':
lsize += 1 + deep
deep += 1
elif m == '/':
deep -= 1
lsize += deep
if lx == a[1]:
lakes.append(lsize)
else: # '_'
lsize += deep
lx += 1
print(sum(lakes))
print(len(lakes),end="")
for lake in lakes:
print(" {}".format(lake),end="")
print()
|
S = raw_input()
S1, S2 = [], []
ans = pool = 0
for i in xrange(len(S)):
if S[i] == "/" and len(S1) > 0:
j = S1.pop()
ans += i - j
a = i - j
while (len(S2) > 0 and S2[-1][0] > j):
a += S2.pop()[1]
S2.append([j, a])
if S[i] == "\\":
S1.append(i)
print ans
if len(S2) > 0:
print len(S2), " ".join(map(str, [a for j, a in S2]))
else:
print 0
| 1 | 57,512,249,630 | null | 21 | 21 |
s = input()
n = len(s)
if n % 2 == 1:
print("No")
exit(0)
for i in range(n):
if i % 2 == 0:
if s[i] != "h":
print("No")
break
elif s[i] != "i":
print("No")
break
else:
print("Yes")
|
s=input()
for i in range(1,6):
if s=='hi'*i:
print('Yes')
quit()
print('No')
| 1 | 53,060,685,085,700 | null | 199 | 199 |
# -*- coding: utf-8 -*-
A, B=map(int,input().split())
if(A<=B):
print("unsafe")
else:
print("safe")
|
import math
a,b=map(int,input().split())
ast=math.ceil(a*12.5)
aend=math.ceil((a+1)*12.5)-1
bst=b*10
bend=(b+1)*10-1
flag=0
for i in range(bst,bend+1):
if ast<=i<=aend:
print(i)
flag=1
break
if flag==0:
print(-1)
| 0 | null | 43,030,062,724,148 | 163 | 203 |
n=int(input());l=list(map(int,input().split()));p=[0]*n;d=[0]*n;p[0]=l[0]
for i in range(1,n):p[i]=l[i]+p[i-2];d[i]=max(p[i-1]if(i&1)else d[i-1],l[i]+(d[i-2]if i>1else 0))
print(d[-1])
|
n = int(input())
playlists = []
for _ in range(n):
title, playtime = input().split()
playlists.append([title, int(playtime)])
titles, playtimes = list(zip(*playlists))
lastmusic = input()
lastmusic_timing = titles.index(lastmusic)
print(sum(playtimes[lastmusic_timing + 1:]))
| 0 | null | 67,264,747,807,648 | 177 | 243 |
N = int(input())
l = [0] * 10 ** 5
for x in range(1,100):
for y in range(1,100):
for z in range(1,100):
n = (pow(x + y,2) + pow(y + z,2) + pow(z + x,2)) // 2
l[n] += 1
for n in range(1,N + 1):
print(l[n])
|
import math
N = int(input())
ans = [0] * N
n = int(math.sqrt(N))
for x in range(1, n+1):
for y in range(1, x+1):
for z in range(1, y+1):
i = x**2 + y**2 + z**2 + x*y + y*z + z*x
if i <= N:
if x == y == z:
ans[i-1] += 1
elif x == y or y == z:
ans[i-1] += 3
else:
ans[i-1] += 6
for j in range(N):
print(ans[j])
| 1 | 8,053,787,337,518 | null | 106 | 106 |
import math
def div(x, y): #x>y
A = 1
for i in range(1, int(math.sqrt(x))+1):
if x % i == 0 and y % i == 0:
A = max(A, i)
j = int(x/i)
if x % j == 0 and y % j == 0:
A = max(A, j)
return A
x, y = map(int, input().split(" "))
print(div(x,y))
|
x, y = map(int,input().split())
while True:
if x >= y:
x %= y
else:
y %= x
if x == 0 or y == 0:
print(x+y)
break
| 1 | 7,224,354,592 | null | 11 | 11 |
n=str(input())
a=len(n)
N=list(n)
m=str(input())
A=len(m)
x=0
for i in range(a):
C=[]
for i in range(A):
C.append(N[i])
B=''.join(C)
c=N[0]
if B==m:
x=x+1
N.remove(c)
N.append(c)
if x>0:
print('Yes')
else:
print('No')
|
# -*- coding: utf-8 -*-
def main():
import sys
input = sys.stdin.readline
x = int(input())
print(10 - x // 200)
if __name__ == '__main__':
main()
| 0 | null | 4,291,948,319,040 | 64 | 100 |
x,y,z = map(int,input().split());print(z,x,y)
|
X,Y,Z = map(str,input().split())
print(Z+' '+X+' '+Y)
| 1 | 37,968,027,015,770 | null | 178 | 178 |
import sys
from itertools import accumulate
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def main():
N, *L = map(int, read().split())
L.sort()
max_L = max(L)
C = [0] * (max_L + 1)
for l in L:
C[l] += 1
C = list(accumulate(C))
ans = 0
for i, a in enumerate(L):
for j, b in enumerate(L[i + 1 :], i + 1):
if a + b > max_L:
ans += N - j - 1
else:
ans += C[a + b - 1] - j - 1
print(ans)
return
if __name__ == '__main__':
main()
|
import math
import sys
from collections import deque
import heapq
import copy
import itertools
from itertools import permutations
from itertools import combinations
import bisect
def mi() : return map(int,sys.stdin.readline().split())
def ii() : return int(sys.stdin.readline().rstrip())
def i() : return sys.stdin.readline().rstrip()
a=ii()
l=list(mi())
l=sorted(l)
ans=0
for i in range(a-2):
for j in range(i+1,a-1):
s=bisect.bisect_left(l,l[i]+l[j])
ans+=s-j-1
print(ans)
| 1 | 172,239,232,771,832 | null | 294 | 294 |
W,H,x,y,r = map(int,input().split())
if (r <= y and y <= H-r) :
if (r <= x and x <= W-r ):
print('Yes')
else :
print('No')
else :
print('No')
|
W, H, x, y, r = map(int, input().split())
flg = False
for X in [x - r, x + r]:
for Y in [y - r, y + r]:
if not 0 <= X <= W:
flg = True
if not 0 <= Y <= H:
flg = True
print(["Yes", "No"][flg])
| 1 | 455,194,355,822 | null | 41 | 41 |
import sys
readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)
def main():
S = input()
n = len(S)
ans = 0
for i in range(n // 2):
if S[i] != S[n - 1 - i]:
ans += 1
print(ans)
if __name__ == '__main__':
main()
|
s=list(input())
s.reverse()
mod=2019
n=[0]*len(s)
n[0]=1
cnt=[0]*len(s)
cnt[0]=int(s[0])
ans=[0]*2019
answer=0
for i in range(1,len(s)):
n[i]+=(n[i-1]*10)%mod
for i in range(1,len(s)):
cnt[i]=(cnt[i-1]+int(s[i])*n[i])%mod
for i in cnt:
ans[i]+=1
for i in range(len(ans)):
answer+=(ans[i]*(ans[i]-1))//2
answer+=ans[0]
print(answer)
| 0 | null | 75,630,670,960,500 | 261 | 166 |
s = str(input())
p = str(input())
s = s + s
if p in s:
print("Yes")
else:
print("No")
|
import math
pi = math.pi
th = pi * 60.0 / 180.0
cos = math.cos(th)
sin = math.sin(th)
class Point():
def __init__(self):
self.x = 0.0
self.y = 0.0
def koch(n, p1, p2):
if n == 0:
return None
s = Point()
t = Point()
u = Point()
s.x = (2.0 * p1.x + 1.0 * p2.x) / 3.0
s.y = (2.0 * p1.y + 1.0 * p2.y) / 3.0
t.x = (1.0 * p1.x + 2.0 * p2.x) / 3.0
t.y = (1.0 * p1.y + 2.0 * p2.y) / 3.0
u.x = (t.x - s.x) * cos - (t.y - s.y) * sin + s.x
u.y = (t.x - s.x) * sin + (t.y - s.y) * cos + s.y
koch(n-1, p1, s)
print(s.x, s.y)
koch(n-1, s, u)
print(u.x, u.y)
koch(n-1, u, t)
print(t.x, t.y)
koch(n-1, t, p2)
def main():
p1 = Point()
p2 = Point()
n = int(input())
p2.x = 100
print(p1.x, p1.y)
koch(n, p1, p2)
print(p2.x, p2.y)
return 0
if __name__ == '__main__':
main()
| 0 | null | 940,928,156,308 | 64 | 27 |
import math
a = input()
N = int(a)
X = N//1.08 + 1
x = math.floor(X*1.08)
if x == N:
print(int(X))
else:
print(':(')
|
import sys
sys.setrecursionlimit(10**6)
a,b = map(int, input().split())
if a < 10 and b < 10:
print(a*b)
else:
print(-1)
| 0 | null | 141,745,357,272,152 | 265 | 286 |
259
N,K=map(int,input().split())
A=[0]+list(map(int,input().split()))
m=1
towns=[1]
flg=[0]*(N+1)
flg[1]=1
while 1:
m=A[m]
towns.append(m)
if flg[m]==1:
l=len(towns)-towns.index(m)-1
break
flg[m]=1
pl=towns.index(m)
if K<=pl:
print(towns[K])
else:
print(towns[(K-pl)%l+pl])
|
N, K = map(int, input().split())
A = list(map(lambda x: int(x) - 1, input().split()))
done = [-1]*N
done[0] = 0
tmp = 0
done[0] = 0
for k in range(1, K + 1):
tmp = A[tmp]
if done[tmp] >= 0:
for _ in range((K - done[tmp]) % (k - done[tmp])):
tmp = A[tmp]
print(tmp + 1)
exit()
else:
done[tmp] = k
print(tmp + 1)
| 1 | 22,889,269,400,218 | null | 150 | 150 |
a,b,c = map(int, raw_input().split())
ans = 0
for i in range(c):
num = c % (i + 1)
if num == 0:
if a <= i + 1 and i + 1 <= b:
ans += 1
print ans
|
a, b, c = [int(temp) for temp in input().split()]
count = 0
for check in range(a, b + 1) :
if c % check == 0 :
count += 1
print(count)
| 1 | 563,465,311,052 | null | 44 | 44 |
def do_round_robin(process, tq):
from collections import deque
q = deque()
elapsed_time = 0 # ??????????????????????§??????????????????????
finished_process = [] # ????????????????????????????????????????????¨??????????????????????????§?????????
# ?????\?????????????????????????????????
for process_name, remaining_time in process:
q.append([process_name, int(remaining_time)])
while True:
try:
process_name, remaining_time = q.pop()
except IndexError: # ??????????????£???????????????????????????????????£???
break
else:
elapsed_time += min(remaining_time, tq)
remaining_time -= tq
if remaining_time > 0:
q.appendleft([process_name, remaining_time])
else:
finished_process.append((process_name, elapsed_time))
return finished_process
if __name__ == '__main__':
# ??????????????\???
# ????????????????????????????????????????????????????°??????????????????????????????¨
data = [int(x) for x in input().split(' ')]
num_of_process = data[0]
time_quantum = data[1]
process = []
for i in range(num_of_process):
process.insert(0, [x for x in input().split(' ')])
# ??????
results = do_round_robin(process, time_quantum)
# ???????????????
for i in results:
print('{0} {1}'.format(i[0], i[1]))
|
a,b=map(int,input().split())
list=[[0 for i in range(2)] for j in range(a)]
for i in range(a):
list[i]=input().split()
n=0
s=0
while n!=a:
if int(list[n][1])>b:
list[n][1]=int(list[n][1])-b
list.append(list[n])
list.pop(n)
s+=b
else:
print(list[n][0],s+int(list[n][1]))
s+=int(list[n][1])
n+=1
| 1 | 43,225,455,852 | null | 19 | 19 |
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
S,W = map(int, readline().split())
if S > W:
print('safe')
else:
print('unsafe')
|
def lcm(x, y):
import math
return (x * y) // math.gcd(x, y)
def abc148c_snack():
a, b = map(int, input().split())
print(lcm(a, b))
abc148c_snack()
| 0 | null | 71,552,638,380,562 | 163 | 256 |
N = int(input())
A = list(map(int, input().split()))
s = sum(A)
av = s / 2
x = 0
y = 0
for i in range(N):
x += A[i]
if x < av < x +A[i+1]:
y = s - x - A[i+1]
if x > y:
print(A[i+1]-x+y)
else:
print(A[i+1]-y+x)
elif x == av:
print(0)
|
n,q = [int(s) for s in input().split()]
queue = []
for i in range(n):
name,time = input().split()
queue.append([name,int(time)])
time = 0
while queue:
processing = queue.pop(0)
t = min(processing[1], q)
time += t
processing[1] -= t
if processing[1] == 0:
print(processing[0],time)
else:
queue.append(processing)
| 0 | null | 71,467,424,893,408 | 276 | 19 |
xyz = list(map(int,input().split()))
print("{} {} {}".format(xyz[2],xyz[0],xyz[1]))
|
N = int(input())
X = input()
memo = [-1] * (N+1)
memo[0] = 0
def popcount(k):
return bin(k).count('1')
def calc(k):
pop = popcount(k)
#print(' ', k, pop)
key = k
if memo[key] < 0:
k %= pop
if k == 0:
memo[key] = 1
else:
memo[key] = calc(k) + 1
return memo[key]
K = int(X, 2)
one = popcount(K)
if one == 0:
K_MOD = K
elif one == 1:
K_MOD = 0
K_MOD_M = K
else:
K_MOD = K % one
K_MOD_M = K % (one-1)
K_MOD_P = K % (one+1)
T_M = [1]*(N+1)
T_P = [1]*(N+1)
for i in range(1,N+1):
if one > 1:
T_M[i] = (T_M[i-1]*2) % (one-1)
T_P[i] = (T_P[i-1]*2) % (one+1)
for i in range(N):
if X[i] == '1':
pop = one - 1
k = K_MOD_M - T_M[N-i-1]
else:
pop = one + 1
k = K_MOD_P + T_P[N-i-1]
if pop == 0:
print(0)
else:
k %= pop
print(calc(k)+1)
| 0 | null | 23,163,367,766,562 | 178 | 107 |
import math
H = int(input())
W = int(input())
N = int(input())
Max = max(H,W)
print(math.ceil(N/Max))
|
H=int(input())
W=int(input())
N=int(input())
K=H
if K<W: K=W
sum = 0
ans= 0
for i in range(1,K+1):
if sum < N:
sum += K
ans = i
#print(ans, K)
print(ans)
| 1 | 88,915,965,766,518 | null | 236 | 236 |
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = list(input())
for i in range(N):
if i >= K and T[i-K] == T[i]:
T[i] = 'a'
ans = 0
for i in T:
if i == 'r':
ans += P
elif i == 's':
ans += R
elif i == 'p':
ans += S
print(ans)
|
# coding: utf-8
import sys
from collections import deque
output_str = deque()
data_cnt = int(input())
for i in range(data_cnt):
in_command = input().rstrip().split(" ")
if in_command[0] == "insert":
output_str.appendleft(in_command[1])
elif in_command[0] == "delete":
if output_str.count(in_command[1]) > 0:
output_str.remove(in_command[1])
elif in_command[0] == "deleteFirst":
output_str.popleft()
elif in_command[0] == "deleteLast":
output_str.pop()
print(" ".join(output_str))
| 0 | null | 53,385,606,482,340 | 251 | 20 |
while True:
try:print(len(str(sum(map(int,input().split())))))
except:break
|
N = int(input())
ans = 0
wnasi = []
for i in range(1, int(pow(N - 1, 0.5)) + 1):
if (N - 1) % i == 0:
wnasi.append(i)
if i != (N - 1) // i:
wnasi.append((N - 1) // i)
ans += len(wnasi) - 1
wari = []
for i in range(2, int(pow(N, 0.5)) + 1):
if N % i == 0:
wari.append(i)
if i != N // i:
wari.append(N // i)
for i in wari:
if i == 1:
continue
tmpN = N
while True:
if tmpN % i == 0:
tmpN //= i
else:
if tmpN % i == 1:
ans += 1
break
print(ans + 1)
| 0 | null | 20,541,619,907,260 | 3 | 183 |
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()
s=s.replace("hi", "")
print("Yes" if s=="" else "No")
| 1 | 53,092,661,866,838 | null | 199 | 199 |
import math
def is_prime(n):
if n in [2, 3, 5, 7]:
return True
elif 0 in [n%2, n%3, n%5, n%7]:
return False
else:
check_max = math.ceil(math.sqrt(n))
for i in range(2, check_max + 1):
if n % i == 0:
return False
else:
return True
def main():
primes = set([])
length = int(input())
for _ in range(0, length):
n = int(input())
if is_prime(n):
primes.add(n)
print(len(primes))
return 0
if __name__ == '__main__':
main()
|
import itertools
n = int(input())
a = list(itertools.accumulate(list(map(int, input().split()))))
sum_a = a[-1]
res = 2020202020
for i in range(n):
res = min(res, abs(sum_a - a[i]*2))
print(res)
| 0 | null | 71,101,547,972,800 | 12 | 276 |
a,b,c=map(int,input().split())
k=int(input())
f=False
for _ in range(k):
if b<=a:
b*=2
elif c<=b:
c*=2
if c>b>a:
f=True
break
if f:
print('Yes')
else:
print('No')
|
a,b,c=map(int,input().split())
k=int(input())
while k>0:
if a<b:
break
b*=2
k-=1
while k>0:
if b<c:
break
c*=2
k-=1
if a<b<c:
print('Yes')
else:
print('No')
| 1 | 6,965,636,532,140 | null | 101 | 101 |
[a,b,c] = map(int,input().split())
n = 0
i = a
while i <= b:
if c % i == 0:
n += 1
i += 1
print(n)
|
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)
| 1 | 551,676,635,908 | null | 44 | 44 |
N=int(input())
def prime_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
def solve():
if N==1:
print(0)
return
pri_arr=prime_factorization(N)
ans=0
for i in range(len(pri_arr)):
e=pri_arr[i][1]
temp=0
cur=1
while e>=cur:
e-=cur
cur+=1
temp+=1
ans+=temp
print(ans)
if __name__ == "__main__":
solve()
|
import statistics
n = int(input())
a = []
b = []
for i in range(n):
ai,bi = map(int, input().split())
a.append(ai)
b.append(bi)
ma = statistics.median(a)
mb = statistics.median(b)
if n % 2 == 1:
ans = mb - ma + 1
else:
ma = int(ma * 2)
mb = int(mb * 2)
ans = mb - ma + 1
print(ans)
| 0 | null | 17,205,582,865,620 | 136 | 137 |
a = int(input())
alist = list(map(int, input().split()))
mod = 10**9+7
total = sum(alist)
sumlist = []
ans = 0
for i in range(len(alist)-1):
total -= alist[i]
ans += alist[i]*(total)
print(ans%mod)
|
N = int(input())
A = list(map(int, input().split()))
P = sum(A)**2
Q = 0
for i in range (0, N):
Q+=(A[i])**2
print(((P-Q)//2)%(10**9+7))
| 1 | 3,819,081,500,128 | null | 83 | 83 |
def resolve():
import math as m
N = int(input())
nn = m.ceil(N / 1.08)
if m.floor(nn * 1.08) == N:
print(nn)
else:
print(":(")
resolve()
|
W = input().lower()
ans = 0
while True:
s = input()
if s == "END_OF_TEXT":
break
s = s.lower().split()
ans += s.count(W)
print(ans)
| 0 | null | 63,801,261,168,678 | 265 | 65 |
N,K=map(int,input().split())
A=0
for a in range(100000000000000):
if N>K:
kot=N%K
if abs(K-kot)>kot:
print(kot)
break
else:
print(abs(K-kot))
break
if N%K==0:
print(0)
break
if N<K:
if abs(N-K)<N:
print(abs(N-K))
break
else:
print(N)
break
tugi=abs(N-K)
N=tugi
A=tugi
tugi=abs(N-K)
B=tugi
N=tugi
tugi=abs(N-K)
if A==tugi:
if B<A:
print(B)
break
else:
print(A)
break
|
n, time=map(int,raw_input().split())
p = []
t = []
for _ in xrange(n):
a = raw_input().split()
p.append(a[0])
t.append(int(a[1]))
alltime = 0
while True:
if len(p)==0:
break
if t[0]<=time:
alltime+=t[0]
print p.pop(0),
t.pop(0)
print alltime
else:
t.append(t[0])
t.pop(0)
t[-1]-=time
alltime += time
p.append(p[0])
p.pop(0)
| 0 | null | 19,696,898,174,080 | 180 | 19 |
N,M=map(int,input().split())
H=list(map(int,input().split()))
H.insert(0,0)
s=set()
for i in range(M):
a,b=map(int,input().split())
if H[a]<=H[b]:s.add(a)
if H[a]>=H[b]:s.add(b)
print(N-len(s))
|
import sys
S = input()
T = input()
if not ( 1 <= len(S) <= 2*10**5 ): sys.exit()
if not ( len(S) == len(T) ): sys.exit()
if not ( S.islower() and T.islower() ): sys.exit()
count = 0
for I in range(len(S)):
if S[I] != T[I]:
count += 1
print(count)
| 0 | null | 17,713,585,231,580 | 155 | 116 |
h,a=map(int,input().split())
i=0
while True:
i+=1
h-=a
if h<=0:
break
print(i)
|
import sys
def ISI(): return map(int, sys.stdin.readline().rstrip().split())
h, a =ISI()
print((h+a-1)//a)
| 1 | 76,784,605,799,460 | null | 225 | 225 |
x,y=map(int,input().split())
n=0
for i in range(0,x+1):
if 2*i+4*(x-i)==y:
n=n+1
if n>0:
print("Yes")
else:
print("No")
|
X, Y = map(int, input().split())
for k in range(X+1):
t = X - k
if k*4 + t*2 == Y:
print("Yes")
exit()
print("No")
| 1 | 13,688,780,521,546 | null | 127 | 127 |
while(True):
h,w=map(int,input().split())
if h==0 and w==0:
break
rect='#'*w+'\n'
rect+=('#'+'.'*(w-2)+'#\n')*(h-2)
rect+='#'*w+'\n'
print(rect)
|
A = 0
left = []
Lake = []
s = input()
for i in range(len(s)):
if s[i] == "\\":
left.append(i)
elif s[i] == "/":
if len(left) > 0:
w = left.pop()
goukei = i - w
A += goukei
for i in range(len(Lake)-1,-1,-1):
if Lake[i][0] > w:
x = Lake.pop()
goukei += x[1]
Lake.append([w, goukei])
print(A)
if len(Lake) == 0:
print(0)
else:
print(len(Lake),end=" ")
for i in range(len(Lake)):
if i == len(Lake) - 1:
print(Lake[i][1])
else:
print(Lake[i][1],end=" ")
| 0 | null | 455,297,152,432 | 50 | 21 |
n=int(input())
m = int(n ** (0.5)) + 1
ans=[0]*(n+1)
for i in range(1,m):
a=i**2
for j in range(1,m):
b=a
b+=j**2+i*j
for k in range(1,m):
c=b
c+=k**2+j*k+k*i
if c<n+1:
ans[c]+=1
for i in range(1,n+1):
print(ans[i])
|
for x in range(1,10):
for i in range(1,10):
print(f"{x}x{i}={x*i}")
| 0 | null | 4,034,109,654,418 | 106 | 1 |
while True:
try:
s = input()
a, b = [int(i) for i in s.split()]
print(len(str(a + b)))
except:
break
|
N = int(input())
D = []
for i, a in enumerate(map(int, input().split())):
D.append((a<<11) + i)
D.sort(reverse=True)
dp = [0]*(N+1)
for i, d in enumerate(D,start=1):
x, a = d%(1<<11),d>>11
for j in reversed(range(i)):
dp[j+1] = max(dp[j] + a*(x-j), dp[j+1])
dp[j] += a*(N-(i-j)-x)
print(max(dp))
| 0 | null | 16,907,380,051,682 | 3 | 171 |
def main():
from sys import stdin
input = stdin.readline
n, m, k = map(int, input().split())
f = [[] for i in range(n)]
BL = [[] for i in range(n)]
ans = [0] * n
for _ in range(m):
a, b = map(int, input().split())
f[a-1].append(b-1)
f[b-1].append(a-1)
for _ in range(k):
c, d = map(int, input().split())
BL[c-1].append(d-1)
BL[d-1].append(c-1)
D = {}
parent = [-1] * n
visited = [False] * n
for root in range(n):
if visited[root]:
continue
D[root] = set([root])
stack = [root]
while stack:
x = stack.pop()
visited[x] = True
parent[x] = root
for t in f[x]:
if visited[t]:
continue
D[root].add(t)
stack.append(t)
for i in range(n):
grp = D[parent[i]]
tans = len(grp) - len(f[i]) - 1
for block in BL[i]:
if block in grp:
tans -= 1
ans[i] = tans
print(*ans)
main()
|
import sys
from sys import exit
from collections import deque
from copy import deepcopy
from bisect import bisect_left, bisect_right, insort_left, insort_right
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians
sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7
def lcm(x,y):
return x*y//gcd(x,y)
def lgcd(l):
return reduce(gcd,l)
def llcm(l):
return reduce(lcm,l)
def powmod(n,i,mod=MOD):
return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
return x.bit_length()
def div10(x):
return len(str(x))-(x==0)
def intput():
return int(input())
def mint():
return map(int,input().split())
def lint():
return list(map(int,input().split()))
def ilint():
return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
print(l[0] if x else l[1])
def lprint(l, sep='\n'):
for x in l:
print(x, end=sep)
def ston(c, c0='a'):
return ord(c)-ord(c0)
def ntos(x, c0='a'):
return chr(x+ord(c0))
class counter(dict):
def __init__(self, *args):
super().__init__(args)
def add(self,x,d=1):
self.setdefault(x,0)
self[x] += d
def list(self):
l = []
for k in self:
l.extend([k]*self[k])
return l
class comb():
def __init__(self, n, mod=None):
self.l = [1]
self.n = n
self.mod = mod
def get(self,k):
l,n,mod = self.l, self.n, self.mod
k = n-k if k>n//2 else k
while len(l)<=k:
i = len(l)
l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
return l[k]
def pf(x,mode='counter'):
C = counter()
p = 2
while x>1:
k = 0
while x%p==0:
x //= p
k += 1
if k>0:
C.add(p,k)
p = p+2-(p==2) if p*p<x else x
if mode=='counter':
return C
S = set([1])
for k in C:
T = deepcopy(S)
for x in T:
for i in range(1,C[k]+1):
S.add(x*(k**i))
if mode=='set':
return S
if mode=='list':
return sorted(list(S))
class UnionFind():
# インデックスは0-start
# 初期化
def __init__(self, n):
self.n = n
self.parents = [-1]*n
self.group = n
# private function
def root(self, x):
if self.parents[x]<0:
return x
else:
self.parents[x] = self.root(self.parents[x])
return self.parents[x]
# x,yが属するグループの結合
def union(self, x, y):
x = self.root(x)
y = self.root(y)
if x==y:
return
if self.parents[x]>self.parents[y]:
x,y = y,x
self.parents[x] += self.parents[y]
self.parents[y] = x
self.group -= 1
# x,yが同グループか判定
def same(self, x, y):
return self.root(x)==self.root(y)
# xと同じグループの要素数を取得
def size(self, x):
return -self.parents[self.root(x)]
######################################################
N,M,K=mint()
T=UnionFind(N)
block=[0]*N
for _ in range(M):
a,b=mint()
T.union(a-1,b-1)
block[a-1]+=1
block[b-1]+=1
for _ in range(K):
c,d=mint()
if T.same(c-1,d-1):
block[c-1]+=1
block[d-1]+=1
ans=[T.size(i)-block[i]-1 for i in range(N)]
lprint(ans,' ')
| 1 | 61,670,161,342,508 | null | 209 | 209 |
N=int(input())
A = list(map(int,input().split()))
val = 0
for i in range(N):
val = val ^ A[i]
ans = []
for i in range(N):
s = val ^ A[i]
ans.append(s)
print(*ans)
|
n = int(input())
import collections
d = collections.deque([])
for _ in range(n):
s = input().split()
if s[0] == "deleteFirst":
d.popleft()
elif s[0] == "deleteLast":
d.pop()
elif s[0] == "insert":
d.appendleft(s[1])
else:
try:
d.remove(s[1])
except:
pass
print(" ".join(d))
| 0 | null | 6,235,676,085,360 | 123 | 20 |
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=2019
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1
def main():
N,P=map(int,input().split())
A=list(map(int,list(input())[::-1]))
ans=0
if P==2 or P==5:
if P==2:
s=set([i*2 for i in range(5)])
else:
s=set([i*5 for i in range(2)])
for i,x in enumerate(A[::-1],1):
if x in s:
ans+=i
else:
S=[0]*(N+1)
for i in range(N):
S[i+1]=(S[i]+A[i]*pow(10,i,P))%P
l=[0]*P
for i in range(N+1):
ans+=l[S[i]]
l[S[i]]+=1
print(ans)
if __name__ == '__main__':
main()
|
n, p = map(int, input().split())
if p == 2:
s = input()
ans = 0
for i in range(n):
if int(s[i]) % 2 == 0:
ans += i + 1
print(ans)
elif p == 5:
s = input()
ans = 0
for i in range(n):
if int(s[i]) % 5 == 0:
ans += i + 1
print(ans)
else:
s = input()[::-1]
mod = 0
c = [0] * p
c[0] = 1
ten = 1
for i in s:
mod = (mod + ten * int(i)) % p
ten = ten * 10 % p
c[mod] += 1
ans = 0
for i in c:
ans += i * (i - 1) // 2
print(ans)
| 1 | 58,087,527,327,908 | null | 205 | 205 |
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a_sums, b_sums = [0], [0]
for i in range(n):
a_sums.append(a_sums[i] + a[i])
for i in range(m):
b_sums.append(b_sums[i] + b[i])
ans = 0
j = m
for i in range(n + 1):
if a_sums[i] > k:
break
while a_sums[i] + b_sums[j] > k:
j -= 1
ans = max(ans, i + j)
print(ans)
|
n, k = (int(i) for i in input().split())
used = [0 for i in range(n)]
for i in range(k):
val = int(input())
a = [int(j) for j in input().split()]
for aa in a:
used[aa - 1] = 1
cnt = 0
for i in used:
cnt += i == 0
print(cnt)
| 0 | null | 17,579,739,484,360 | 117 | 154 |
def main():
a, b, n = map(int, input().split())
x = min(b - 1, n)
floor1 = int(a * x / b)
floor2 = int(x / b)
print(floor1 - floor2)
if __name__ == '__main__':
main()
|
import math
a,b,n=map(int,input().split())
if n<b :
ans=math.floor(a*n/b)
else :
ans=math.floor(a*(b-1)/b)
print(ans)
| 1 | 28,199,414,697,898 | null | 161 | 161 |
from itertools import permutations
import math
n = int(input())
x,y = [],[]
for _ in range(n):
x_, y_ =map(int,input().split())
x.append(x_)
y.append(y_)
c = list(permutations([i for i in range(1,n+1)],n))
g = [[-1]*(n+1) for _ in range(n+1)]
sum = 0
for ci in (c):
tmp = 0
for i in range(len(ci)-1):
if g[ci[i]][ci[i+1]] == -1:
tmp += math.sqrt((x[ci[i]-1]-x[ci[i+1]-1])**2
+ (y[ci[i]-1]-y[ci[i+1]-1])**2)
else:
tmp += g[ci[i]][ci[i+1]]
sum += tmp
print(sum/len(c))
|
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
n = I()
lst = [None, ]
for _ in range(n):
x, y = LI()
lst.append((x,y))
per = itertools.permutations(range(1,n+1))
sm = 0
oc = math.factorial(n)
for p in per:
dist = 0
before = p[0]
for place in p[1:]:
dist += math.sqrt((lst[place][0]-lst[before][0])**2 + (lst[place][1]-lst[before][1])**2)
before = place
sm += dist
ans = sm/oc
print(ans)
main()
| 1 | 148,732,355,011,590 | null | 280 | 280 |
turn = int(input())
tp, hp = 0, 0
for i in range(turn):
t, h = input().split()
if t == h:
tp += 1
hp += 1
elif t > h:
tp += 3
elif t < h:
hp += 3
print(tp, hp)
|
n = int(input())
a = list(map(int,input().split()))
ans = [0]*(n+1)
a.insert(0,0)
for i in range(1,n+1):
ans[a[i]] += i
ans.pop(0)
ans = [str(ans[s]) for s in range(n)]
print(" ".join(ans))
| 0 | null | 91,682,518,995,870 | 67 | 299 |
n, m = map(int, input().split())
print(int((n * (n - 1)) / 2) + int((m * (m - 1)) / 2))
|
import math
N,M=map(int,input().split())
al=math.factorial(N+M)/(math.factorial(2)*math.factorial(N+M-2))
di=N*M
print(int(al)-int(di))
| 1 | 45,431,504,004,730 | null | 189 | 189 |
# coding: utf-8
def main():
N, A, B = map(int, input().split())
ans = (B - A) // 2 if (B - A) % 2 == 0 else min(A - 1, N - B) + 1 + (B - A - 1) // 2
print(ans)
if __name__ == "__main__":
main()
|
def agc043_a():
''' 解説放送見てやってみた '''
H, W = map(int, input().split())
grid = []
grid.append(['#'] * (W+2)) # 周辺埋め
for _ in range(H):
grid.append(['#'] + [c for c in str(input())] + ['#'])
grid.append(['#'] * (W+2))
grid[1][0] = grid[H][W+1] = '.' # グリッド外側に白を置く, (1,1)(H,W)が黒の場合に反転カウントするため
# 白黒が反転する回数を数える
cost = [[10**6] * (W+2) for _ in range(H+2)]
cost[1][0] = 0
for i in range(1, H+1):
for j in range(1, W+2):
if i != H and j == W+1: continue # ゴールの外側のときだけ通過
rt = cost[i][j-1]
if grid[i][j-1] != grid[i][j]: rt += 1
dw = cost[i-1][j]
if grid[i-1][j] != grid[i][j]: dw += 1
cost[i][j] = min(rt, dw)
ans = cost[H][W+1] // 2 # 区間の始まりと終わりを両方カウントしているので半分にする
print(ans)
agc043_a()
| 0 | null | 79,262,840,069,508 | 253 | 194 |
#!/usr/bin/env python3
def main():
N, K = map(int, input().split())
for i in range(10 ** 9):
if N <= K ** i - 1:
print(i)
break
if __name__ == '__main__':
main()
|
N,K=map(int,input().split())
a=0
while N>0:
a+=1
N=N//K
print(a)
| 1 | 64,172,388,427,742 | null | 212 | 212 |
data = []
for i in range(10):
data.append(int(input()))
data = sorted(data)
print(data[-1])
print(data[-2])
print(data[-3])
|
def main():
n = int(input())
s = input()
one_set = set([])
two_set = set([])
three_set = set([])
count = 0
for i in range(n):
if i==0:
one_set.add(s[0])
continue
if 2<=i:
for two in two_set:
if two + s[i] not in three_set:
three_set.add(two + s[i])
count+=1
for one in one_set:
two_set.add(one + s[i])
one_set.add(s[i])
print(count)
if __name__=="__main__":
main()
| 0 | null | 64,684,028,415,610 | 2 | 267 |
n, k = map(int, input().split())
a = [int(s) for s in input().split()]
for i in range(k, n):
if a[i] > a[i - k]:
print('Yes')
else:
print('No')
|
N = int(input())
A = N + N**2 + N**3
print(A)
| 0 | null | 8,582,191,162,524 | 102 | 115 |
h,n = map(int, input().split())
l = list(map(int, input().split()))
print("Yes" if h <= sum(l) else "No")
|
def solve():
n, m = map(int, input().split())
s = list(map(int, input().split()))
if sum(s) >= n:
print("Yes")
else:
print("No")
solve()
| 1 | 77,861,313,609,952 | null | 226 | 226 |
N=input()
N=list(N)
N=map(lambda x: int(x),N)
if sum(N)%9==0:
print("Yes")
else:
print("No")
|
N=int(input())
if N%9==0:print("Yes")
else:print("No")
| 1 | 4,389,566,500,522 | null | 87 | 87 |
def get_num(n, x):
ans = 0
for n3 in xrange(min(n, x - 3), (x + 2) / 3, -1):
for n2 in xrange(min(n3 - 1, x - n3 - 1), (x - n3) / 2, -1):
if n3 + min(n2 - 1, x - n3 - n2):
ans += 1
# for n1 in xrange(min(n2 - 1, x - n3 - n2), x - n3 - n2 - 1, -1):
# if n3 == x - n1 - n2:
# ans += 1
# break
return ans
data = []
while True:
[n, x] = [int(m) for m in raw_input().split()]
if [n, x] == [0, 0]:
break
if x < 3:
print(0)
else:
print(get_num(n, x))
|
# coding:utf-8
while True:
cnt = 0
n,x = map(int, raw_input().split())
if n == 0 and x == 0:
break
for i in range(1,n+1):
for j in range(1,n+1):
for k in range(1,n+1):
if i < j and j < k and i + j + k == x:
cnt += 1
print cnt
| 1 | 1,307,324,553,610 | null | 58 | 58 |
def divisor(n):
res = []
i = 1
while i*i <= n:
if not n % i:
res.append(i)
if (i*i != n): res.append(n//i)
i += 1
return res
N = int(input())
ans = 0
for d in divisor(N):
if d == 1: continue
n = N
while not n % d:
n //= d
if n%d == 1: ans += 1
print(ans + len(divisor(N-1))-1)
|
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32
def solve(N: int):
print((N-1)//2)
return
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens)) # type: int
solve(N)
if __name__ == '__main__':
main()
| 0 | null | 97,146,559,867,012 | 183 | 283 |
N, K = list(map(int, input().split(' ')))
table = {l for l in range(1, N + 1)}
have = set()
for i in range(K):
d = int(input())
nums = list(map(int, input().split(' ')))
have = have | set(nums)
ans = len(table - have)
print(ans)
|
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]
def inside(y, x, H, W):
return 0 <= y < H and 0 <= x < W
def ceil(a, b):
return (a + b - 1) // b
def sum_of_arithmetic_progression(s, d, n):
return n * (2 * s + (n - 1) * d) // 2
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
def lcm(a, b):
g = gcd(a, b)
return a / g * b
def solve():
S = input()
N = len(S)
if S != S[::-1]:
return False
t = S[:(N - 1) // 2]
if t != t[::-1]:
return False
t = S[(N + 3) // 2 - 1:]
if t != t[::-1]:
return False
return True
def main():
if solve():
print("Yes")
else:
print("No")
if __name__ == '__main__':
main()
| 0 | null | 35,383,342,777,662 | 154 | 190 |
print("win" if sum(list(map(int,input().split())))<=21 else "bust")
|
def main():
A = map(int, input().split())
print('bust' if sum(A) >= 22 else 'win')
if __name__ == '__main__':
main()
| 1 | 119,076,758,231,232 | null | 260 | 260 |
X, Y = map(int, input().split())
X1, Y1 = map(int, input().split())
if Y1 == 1:
print("1")
else:
print("0")
|
d1 = input().split()[0]
d2 = input().split()[0]
if d1 != d2:
print(1)
else:
print(0)
| 1 | 124,542,646,514,570 | null | 264 | 264 |
# 16-Character-Sum_of_Numbers.py
# ??°?????????
# ?????????????????°???????????????????¨??????????????????°?????????????????????????????????
# Input
# ?????°??????????????????????????\?????¨??????????????????????????????????????????????????????????????´??° x ?????????????????§?????????????????????
# x ??? 1000 ?????\????????´??°??§??????
# x ??? 0 ?????¨?????\?????????????????¨??????????????????????????????????????????????????????????????£????????????????????????
# Output
# ????????????????????????????????????x ???????????????????????????????????????????????????
# Sample Input
# 123
# 55
# 1000
# 0
# Sample Output
# 6
# 10
# 1
data=[]
while 1:
temp = int( input() )
if temp==0:
break;
else:
data.append( temp )
for i in range( len(data) ):
num = list( map(int, list( str( data[i] ) ) ) )
print( sum(num) )
|
#coding:utf-8
while True:
l = [int(x) for x in input()]
if len(l) and l[0]==0:
break
print(sum(l))
| 1 | 1,607,936,417,580 | null | 62 | 62 |
def main():
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
ans = 0
U = []
for i, t in enumerate(T):
if t == 'r':
if i-K >= 0:
if U[i-K] == 'p':
U.append('rs')
continue
U.append('p')
ans += P
elif t == 's':
if i-K >= 0:
if U[i-K] == 'r':
U.append('sp')
continue
U.append('r')
ans += R
else:
if i-K >= 0:
if U[i-K] == 's':
U.append('rp')
continue
U.append('s')
ans += S
print(ans)
if __name__ == "__main__":
main()
|
A = []
memo = [-1] * 101
for i in range(3):
a, b, c = map(int, input().split())
memo[a] = memo[b] = memo[c] = i
A.append([a, b, c])
n = int(input())
b = []
for _ in range(n):
b.append(int(input()))
b = set(b)
ana = [[0] * 3 for _ in range(3)]
for num in b:
if memo[num] == -1:
continue
ana[memo[num]][A[memo[num]].index(num)] = 1
#斜め
if ana[0][0] == 1 and ana[1][1] == 1 and ana[2][2] == 1:
print("Yes")
exit()
if ana[0][2] == 1 and ana[1][1] == 1 and ana[2][0] == 1:
print("Yes")
exit()
#横 縦
for i in range(3):
if ana[i][0] == 1 and ana[i][1] == 1 and ana[i][2] == 1:
print("Yes")
exit()
if ana[0][i] == 1 and ana[1][i] == 1 and ana[2][i] == 1:
print("Yes")
exit()
print("No")
| 0 | null | 83,675,210,315,592 | 251 | 207 |
class stack():
def __init__(self):
self.S = []
def push(self, x):
self.S.append(x)
def pop(self):
return self.S.pop()
def isEmpty(self):
if len(self.S) == 0:
return True
else:
return False
areas = input()
S1 = stack()
S2 = stack()
for i, info in enumerate(areas):
if info == '/':
if S1.isEmpty():
continue
left = S1.pop()
menseki = i - left
while True:
if S2.isEmpty():
S2.push([menseki, left])
break
if S2.S[-1][1] < left:
S2.push([menseki, left])
break
menseki += S2.pop()[0]
elif info == '\\':
S1.push(i)
ans = [m[0] for m in S2.S]
print(sum(ans))
if len(ans) == 0:
print(len(ans))
else:
print(len(ans), ' '.join(list(map(str, ans))))
|
hoge1 = list()
hoge2 = list()
total = 0
for num, c in enumerate(input()):
if c == '\\':
hoge1.append(num)
elif c == '/':
if not hoge1:
continue
last_index = hoge1.pop()
S = num - last_index
if not hoge2:
hoge2.append((last_index, S))
else:
if last_index >= hoge2[-1][0]:
hoge2.append((last_index, S))
else:
new_S = S
for i, j in hoge2[::-1]:
if last_index >= i:
break
else:
new_S += hoge2.pop()[1]
hoge2.append((last_index, new_S))
totals = [x[1] for x in hoge2]
print (sum(totals))
totals.insert(0, len(hoge2))
print (' '.join([str(x) for x in totals]))
| 1 | 56,862,892,408 | null | 21 | 21 |
n = int(input())
lis=[]
for _ in range(n):
x,l = map(int,input().split())
lis.append([x+l,x-l])
lis.sort()
m = -(10**9+7)
ans = 0
for i in range(n):
a ,b = lis[i]
if m <= b:
ans += 1
m = a
print(ans)
|
N = int(input())
ST = []
for i in range(N):
x, l = list(map(int, input().split()))
ST.append([x-l, x+l])
ST.sort(key = lambda x :x[1])
startT = ST[0][1]
ans = 1
if N != 1:
for i in range(1,N):
if startT <= ST[i][0]:
startT = ST[i][1]
ans += 1
print(ans)
| 1 | 90,031,521,076,130 | null | 237 | 237 |
s=str(input().upper())
if s=="ABC":
print("ARC")
if s=="ARC":
print("ABC")
|
print(("ARC","ABC")[str(input())=="ARC"])
| 1 | 24,024,350,664,732 | null | 153 | 153 |
import sys
import collections
import bisect
def main():
n = int(input())
AList = list(map(int, input().split()))
ASum = sum(AList)
c = 0
for i in range(n):
c += AList[i]
if c >= ASum / 2:
k = i
break
ans = min(2 * c - ASum, ASum - 2 * c + 2 * AList[k])
print(ans)
if __name__ == '__main__':
main()
|
N = int(input())
A = list(map(int, input().split()))
S = sum(A)
L = 0
for i, a in enumerate(A):
if L + a <= S/2:
L += a
else:
R = S - L
diff = R - L
if 2*a - diff < diff:
L += a
R -= a
break
R = S - L
print(abs(R - L))
| 1 | 142,807,303,725,130 | null | 276 | 276 |
L, R, d = map(int, input().split())
print(sum(1 for i in range(L, R + 1) if i % d == 0))
|
A,B=list(map(int,input().split()))
def gcd(A, B):
if B==0: return(A)
else: return(gcd(B, A%B))
print(gcd(A,B))
| 0 | null | 3,777,984,998,590 | 104 | 11 |
n = int(input())
a = list(map(int, input().split()))
max_node = [0 for _ in range(n+1)]
for i in range(n-1, -1, -1):
max_node[i] = max_node[i+1] + a[i+1]
ans = 1
node = 1
for i in range(n+1):
node -= a[i]
if (i < n and node <= 0) or node < 0:
print(-1)
exit(0)
node = min(node * 2, max_node[i])
ans += node
print(ans)
|
import sys
def solve():
input = sys.stdin.readline
N = int(input())
A = [int(a) for a in input().split()]
if N == 0 and A[0] > 1:
print(-1)
return 0
low, high = A[N], A[N]
maxN = [0] * (N + 1)
maxN[N] = A[N]
Limit = [2 ** 50] * (N + 1)
Limit[N] = A[N]
for i in reversed(range(N)):
leaf = A[i]
Limit[i] = min(Limit[i], Limit[i+1] + leaf)
if i <= 50: Limit[i] = min(pow(2, i), Limit[i])
low = (low + 1) // 2 + leaf
high += leaf
if low > Limit[i]:
print(-1)
break
else:
maxN[i] = min(high, Limit[i])
else:
for i in range(N):
if (maxN[i] - A[i]) * 2 <= maxN[i+1]: maxN[i + 1] = (maxN[i] - A[i]) * 2
print(sum(maxN))
return 0
if __name__ == "__main__":
solve()
| 1 | 18,928,534,949,440 | null | 141 | 141 |
num_length = int(input())
num_array = [int(x) for x in input().split()]
def bubbleSort(A, N):
flag = 1
exchange_counter = 0
while flag:
flag = 0
for i in range(1, N):
if A[i] < A[i-1]:
A[i], A[i-1] = A[i-1], A[i]
flag = 1
exchange_counter += 1
print(" ".join(map(str, A)))
print(exchange_counter)
bubbleSort(num_array, num_length)
|
import sys
import collections
X = int(input())
Angle = 360
for i in range(1,10**9):
if Angle < (X*i):
Angle = Angle + 360
if (Angle / (X*i)) == 1:
print(i)
sys.exit()
| 0 | null | 6,495,485,778,192 | 14 | 125 |
# h, a = map(int, input().split())
# from collections import OrderedDict
# d = OrderedDict()
# a = list(input().split())
b = list(map(int, input().split()))
print("Yes" if len(set(b)) == 2 else "No")
|
A, B, C = map(int, input().split())
print('Yes' if len(set([A, B, C]))==2 else 'No')
| 1 | 68,090,785,278,942 | null | 216 | 216 |
K=int(input())
A,B=map(int,input().split())
for i in range(A,B+1):
if i%K==0:
print('OK')
exit()
print('NG')
|
def build_combinations_counter(N=10**5, p=10**9+7):
fact = [1, 1] # fact[n] = (n! mod p)
factinv = [1, 1] # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1] # mod p におけるnの逆元 n^(-1)
for i in range(2, N + 1):
fact.append((fact[-1] * i) % p)
inv.append((-inv[p % i] * (p // i)) % p)
factinv.append((factinv[-1] * inv[-1]) % p)
def cmb(n, r, p, fact, factinv):
if (r < 0) or (n < r):
return 0
r = min(r, n - r)
return fact[n] * factinv[r] * factinv[n-r] % p
import functools
return functools.partial(cmb, p=p, fact=fact, factinv=factinv)
def resolve():
n, k = list(map(int, input().split()))
MOD = 10**9+7
counter = build_combinations_counter(2*10**5, 10**9+7)
ans = 0
for i in range(min(n, k+1)):
ans += counter(n, i) * counter(n-1, n-i-1)
ans %= MOD
print(ans)
if __name__ == "__main__":
resolve()
| 0 | null | 46,775,860,106,580 | 158 | 215 |
A, B, C, K = map(int, input().split())
if K <= A:
print(K)
elif K > A and K <= A + B:
print(A)
else:
K -= A + B
print(A - K)
|
A, B, C, K = map(int, input().split())
if K<=A:
print(1*K)
elif A<K<=A+B:
print(1*A)
else:
print(1*A-1*(K-A-B))
| 1 | 21,805,705,470,496 | null | 148 | 148 |
N = int(input())
A = list(map(int, input().split()))
l = {a: i for i, a in enumerate(A, start=1)}
print(' '.join([str(l[i+1]) for i in range(N)]))
|
n = int(input())
A = list(map(int, input().split()))
N = sorted(range(1, n + 1), key=lambda x: A[x - 1])
print(*N)
| 1 | 180,343,283,503,140 | null | 299 | 299 |
import sys
input = sys.stdin.readline
def main():
N, K = map(int, input().split())
A = list(map(lambda x: int(x) - 1, input().split()))
route = [0]
visited = set([0])
now_town = 0
while True:
next_town = A[now_town]
if next_town in visited:
break
else:
visited.add(next_town)
route.append(next_town)
now_town = next_town
loop_start = route.index(next_town)
loop_len = len(route[loop_start:])
count_before_loop = len(route) - loop_len
if K <= count_before_loop:
ans = route[K] + 1
else:
K -= count_before_loop
r = K % loop_len
ans = route[loop_start + r] + 1
print(ans)
if __name__ == "__main__":
main()
|
from collections import defaultdict
N, K = map(int, input().split())
A = list(map(int, input().split()))
visited = [0] * N
current = 0
i = 1
while i <= K:
current = A[current] - 1
if visited[current] == 0:
visited[current] = i
else:
loop = i - visited[current]
num_loop = (K - i) // loop
i += loop * num_loop
i += 1
ans = current + 1
print(ans)
| 1 | 22,755,684,799,136 | null | 150 | 150 |
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
memo = defaultdict(int)
ans = 0
for i, x in enumerate(a, 1):
ans += memo[i - x]
memo[x + i] += 1
print(ans)
|
from collections import defaultdict
N, *A = map(int, open(0).read().split())
ctr1 = defaultdict(int)
ctr2 = defaultdict(int)
for i in range(N):
ctr2[i + A[i]] += 1
if i - A[i] > 0:
ctr1[i - A[i]] += 1
ans = 0
for v in ctr1:
ans += ctr1[v] * ctr2[v]
print(ans)
| 1 | 26,060,308,505,956 | null | 157 | 157 |
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
H, W, M = map(int, input().split())
rows = [0] * (H + 1)
cols = [0] * (W + 1)
from collections import defaultdict
ng_cols = defaultdict(set)
for _ in range(M):
h, w = map(int, input().split())
rows[h] += 1
cols[w] += 1
ng_cols[h].add(w)
max_of_rows = max(rows)
max_of_cols = max(cols)
max_rows_index = set()
max_cols_index = set()
for i in range(H + 1):
if rows[i] == max_of_rows:
max_rows_index.add(i)
for i in range(W + 1):
if cols[i] == max_of_cols:
max_cols_index.add(i)
flag = 0
for i in max_rows_index:
for j in max_cols_index:
if j not in ng_cols[i]:
flag = 1
break
print(max_of_rows + max_of_cols - 1 + flag)
|
import sys
H,W,M=map(int,input().split())
hlist=[0]*H
wlist=[0]*W
hwset=set()
for _ in range(M):
h,w=map(int,input().split())
hlist[h-1]+=1
wlist[w-1]+=1
hwset.add((h-1,w-1))
hmax=max(hlist)
hlist2=[]
for i in range(H):
if hlist[i]==hmax:
hlist2.append(i)
wmax=max(wlist)
wlist2=[]
for i in range(W):
if wlist[i]==wmax:
wlist2.append(i)
#print(hlist2,wlist2)
H2=len(hlist2)
W2=len(wlist2)
if H2*W2>M:
print(hmax+wmax)
else:
for i in range(H2):
ii=hlist2[i]
for j in range(W2):
jj=wlist2[j]
if not (ii,jj) in hwset:
print(hmax+wmax)
sys.exit(0)
else:
print(hmax+wmax-1)
| 1 | 4,707,958,225,040 | null | 89 | 89 |
def main():
N,K = map(int,input().split())
syo = N//K
amari = N%K
N = abs(amari-K)
ans = min(N,amari)
return ans
print(main())
|
import string
n = int(input())
s = ''
lowCase = string.ascii_lowercase
while n > 0:
n -= 1
s += lowCase[int(n % 26)]
n //= 26
print(s[::-1])
| 0 | null | 25,598,328,722,120 | 180 | 121 |
n = int(input())
list = [i for i in input().split()]
list.reverse()
print(" ".join(list))
|
#coding: UTF-8
import math
def RN(n,a):
ans=""
for i in range(int(n)):
ans+=a[int(n)-1-i]+" "
print(ans[:-1])
if __name__=="__main__":
n=input()
a=input()
List=a.split(" ")
RN(n,List)
| 1 | 964,702,913,480 | null | 53 | 53 |
H,W = map(int,input().split())
if H==1 or W==1:
print(1)
elif H%2==0 and W%2==0 or H%2==0 and W%2==1 or H%2==1 and W%2==0:
print((H*W)//2)
else:
print(((H*W)+1)//2)
|
N=int(input())
S = [0]*N
for i in range(N):
S[i]=str(input())
print('AC x {0}'.format(S.count('AC')))
print('WA x {0}'.format(S.count('WA')))
print('TLE x {0}'.format(S.count('TLE')))
print('RE x {0}'.format(S.count('RE')))
| 0 | null | 29,578,682,651,340 | 196 | 109 |
from collections import deque
n = int(input())
G = [None for i in range(n)]
for i in range(n):
u,k,*vs = map(int,input().split())
G[u-1] = [e-1 for e in vs]
dist = [-1]*n #全頂点を未訪問に初期化(-1)で未訪問
que = deque([0]) #初期条件 (頂点 0 を初期ノードとする)
dist[0] = 0 #0を訪問済み
while len(que)>0:
now = que.popleft()
for i in G[now]:
if dist[i] != -1:
continue
dist[i] = dist[now] + 1 #新たな頂点wについて距離情報を更新してキューに追加する
que.append(i)
for i in range(n):
print(i+1,dist[i])
|
import math
import string
import collections
from collections import Counter
from collections import deque
from decimal import Decimal
import sys
import fractions
def readints():
return list(map(int, input().split()))
def nCr(n, r):
return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))
def has_duplicates2(seq):
seen = []
for item in seq:
if not(item in seen):
seen.append(item)
return len(seq) != len(seen)
def divisor(n):
divisor = []
for i in range(1, n+1):
if n % i == 0:
divisor.append(i)
return divisor
# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n = int(input())
g = [None]*n
for i in range(n):
g[i] = readints()
# print(g)
G = [None]*n
for i in range(n):
G[i] = []
# print(G)
for i in range(n):
if len(g[i]) >= 3:
for j in range(2, len(g[i])):
# print(g[i][j])
G[i].append(g[i][j])
#print('G', G)
dist = [-1]*n
dist[0] = 0
d = deque()
d.append(0)
# print(d)
while(len(d) != 0):
v = d.popleft()
if G[v] != []:
for nv in G[v]:
if dist[nv-1] != -1:
continue
dist[nv-1] = dist[v]+1
d.append(nv-1)
for i in range(n):
print(i+1, dist[i])
| 1 | 4,210,027,900 | null | 9 | 9 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.