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
|
---|---|---|---|---|---|---|
class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
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
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def members(self, x):
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def all_group_members(self):
return {r: self.members(r) for r in self.roots()}
def __str__(self):
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
n, m, k = map(int, input().split())
uf = UnionFind(n)
friends = [0] * n
blocks = [0] * n
for i in range(m):
a, b = map(int, input().split())
a -= 1
b -= 1
uf.union(a, b)
friends[a] += 1
friends[b] += 1
for k in range(k):
c, d = map(int, input().split())
c -= 1
d -= 1
if uf.same(c, d):
blocks[c] += 1
blocks[d] += 1
for i in range(n):
print(uf.size(i) - friends[i] - blocks[i] - 1, end=" ")
|
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
MAX_PRIME = 10**4
def prime_list():
sqrt_max = int(MAX_PRIME ** 0.5)
primes = [True for i in range(MAX_PRIME+1)]
primes[0] = primes[1] = False
for p in range(sqrt_max):
if primes[p]:
for px in range(p*2, MAX_PRIME, p):
primes[px] = False
p_list = [i for i in range(MAX_PRIME) if primes[i]]
return p_list
def is_prime(n, prime_list):
sqrt_n = int(n ** 0.5)
for i in prime_list:
if i > sqrt_n:
break
if n % i == 0:
return False
return True
p_list = prime_list()
n = int(sys.stdin.readline())
num_p = 0
for i in range(n):
x = int(sys.stdin.readline())
if is_prime(x, p_list):
num_p += 1
print(num_p)
| 0 | null | 31,014,290,604,192 | 209 | 12 |
A,B,N = map(int,input().split())
if B<=N:
print(int((A*(B-1))/B)-(A*int((B-1)/B)))
else:
print(int((A*N)/B)-(A*int(N/B)))
|
h,w = [int(i) for i in input().split()]
if h == 1 or w == 1:
print(1)
elif h % 2 == 1 and w % 2 == 1:
print(int(h*w/2) + 1)
else:
print(int(h*w/2))
| 0 | null | 39,427,564,586,726 | 161 | 196 |
import math
n=int(input())
xs=list(map(float,input().split()))
ys=list(map(float,input().split()))
ds=[ abs(x - y) for (x,y) in zip(xs,ys) ]
print('{0:.6f}'.format(sum(ds)))
print('{0:.6f}'.format((sum(map(lambda x: x*x,ds)))**0.5))
print('{0:.6f}'.format((sum(map(lambda x: x*x*x,ds)))**(1./3.)))
print('{0:.6f}'.format(max(ds)))
|
n = int(input())
x = [0] + list(map(int, input().split()))
y = [0] + list(map(int, input().split()))
tmp = [[0 for i in range(6)] for j in range(n+1)]
max = 0
for i in range(1, n+1, 1):
absDiff = abs(x[i]-y[i])
tmp[i][0] = absDiff
tmp[i][1] = absDiff ** 2
tmp[i][2] = tmp[i][1] * absDiff
tmp[i][3] = tmp[i-1][3] + tmp[i][0]
tmp[i][4] = tmp[i-1][4] + tmp[i][1]
tmp[i][5] = tmp[i-1][5] + tmp[i][2]
if absDiff > max:
max = absDiff
print(tmp[n][3])
print(tmp[n][4] ** (1/2))
print(tmp[n][5] ** (1/3))
print(max)
| 1 | 207,936,593,020 | null | 32 | 32 |
s=str(input().upper())
if s=="ABC":
print("ARC")
if s=="ARC":
print("ABC")
|
import sys
input = sys.stdin.readline
def main():
s = input().strip()
print("ABC" if s[1] == "R" else "ARC")
if __name__ == "__main__":
main()
| 1 | 23,995,990,463,250 | null | 153 | 153 |
a,b = map(int, input().split())
print(0--a//b)
|
H,A = map(int,input().split())
answer = 0
if H %A != 0:
answer = H//A + 1
else:
answer = H / A
print(int(answer))
| 1 | 76,893,593,086,180 | null | 225 | 225 |
s=input()
t=input()
lt=len(t);ls=len(s)
ans=lt
for i in range(ls-lt+1):
cnt=0
for j in range(len(t)):
if s[i:i+lt][j]!=t[j]:
cnt+=1
ans=min(ans,cnt)
print(ans)
|
s = input()
t = input()
ans = len(t)
for i in range(len(s)):
if (i + len(t) > len(s)): break
dif = 0
for j in range(len(t)):
if (s[i + j] != t[j]):
dif += 1
ans = min(ans, dif)
print(ans)
| 1 | 3,732,641,155,008 | null | 82 | 82 |
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
N = INT()
A = LIST()
ans = INF
sum_A = sum(A)
tmp = 0
for a in A:
tmp += a
ans = min(ans, abs(tmp-(sum_A-tmp)))
print(ans)
|
def main():
n = int(input())
n_b = 4
n_f = 3
n_r = 10
from collections import defaultdict
state = defaultdict(int)
for _ in range(n):
# b?£?f??????r???????????¨?±?
# v??????????????§??\?±????????????¨???????????????
# v?????????????????´??????v????????????????????¨??????????????????
b, f, r, v = map(int, input().split())
state[(b, f, r)] += v
for b in range(1, n_b + 1):
for f in range(1, n_f + 1):
room_state = []
for r in range(1, n_r + 1):
room_state.append(state[(b, f, r)])
print(" " + " ".join(map(str, room_state)))
if b != n_b:
print("#" * 20)
if __name__ == "__main__":
main()
| 0 | null | 71,274,804,900,468 | 276 | 55 |
def f():
n,k=map(int,input().split())
l=list(map(int,input().split()))
now=1
for i in range(k.bit_length()):
if k%2:now=l[now-1]
l=[l[l[i]-1]for i in range(n)]
k//=2
print(now)
if __name__ == "__main__":
f()
|
from sys import exit
N,K = map(int,input().split())
town = [None]+list(map(int,input().split()))
flag = [None]+[0]*N
remain = K
piece = 1
cnt = 1
while remain > 0:
if flag[town[piece]] != 0:
q = cnt-flag[town[piece]]
piece = town[piece]
remain -= 1
break
else:
piece = town[piece]
flag[piece] = cnt
remain -= 1
cnt += 1
if remain == 0:
print(piece)
exit(0)
remain %= q
while remain > 0:
piece = town[piece]
remain -= 1
print(piece)
| 1 | 22,782,769,132,508 | null | 150 | 150 |
L=list(map(int,input().split()))
L[2],L[0]=L[0],L[2]
L[1],L[2]=L[2],L[1]
print(*L)
|
input_lists = list(input().split(' '))
result = [input_lists[-1]]
result += input_lists[0:-1]
result = ' '.join(result)
print(result)
| 1 | 37,984,980,785,598 | null | 178 | 178 |
S = input()
A = S.count("R")
B = S.count("RR")
if A==2:
if B==1:
print(2)
if B==0:
print(1)
elif A==1:
print(1)
elif A==3:
print(3)
else :
print(0)
|
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
cA = Counter(A)
allS = 0
for key in cA:
allS += cA[key] * (cA[key] - 1) / 2
for i in range(N):
if A[i] in cA.keys():
print(int(allS - (cA[A[i]] - 1)))
else:
print(int(allS))
| 0 | null | 26,412,111,585,930 | 90 | 192 |
s = input()
t = input()
ans = 1001
if len(s)-len(t)==0:
temp = 0
for j in range(len(t)):
if s[j]!=t[j]:
temp+=1
ans = min(ans, temp)
else:
for i in range(len(s)-len(t)):
temp = 0
for j in range(len(t)):
if s[i+j]!=t[j]:
temp+=1
ans = min(ans, temp)
print(ans)
|
S=input()
T=input()
s=len(S)
t=len(T)
A=[]
for i in range(s-t+1):
word=S[i:i+t]
a=0
for j in range(t):
if word[j]==T[j]:
a+=1
else:
a+=0
A.append(t-a)
print(min(A))
| 1 | 3,703,635,327,040 | null | 82 | 82 |
n=int(input())
stri=[]
time=[]
for _ in range(n):
s,t=input().split()
stri.append(s)
time.append(t)
x=input()
ind=stri.index(x)
ans=0
for i in range(ind+1,n):
ans+=int(time[i])
print(ans)
|
from collections import deque
process = deque(maxlen=100000)
n, q = [int(x) for x in input().split(' ')]
for i in range(n):
name, time = input().split(' ')
process.append([name, int(time)])
total_time = 0
while process:
p_q = process.popleft()
if p_q[1] > q:
total_time += q
p_q[1] -= q
process.append(p_q)
else:
total_time += p_q[1]
print('{} {}'.format(p_q[0], total_time))
| 0 | null | 48,404,709,415,228 | 243 | 19 |
n,k = map(int,input().split())
a = list(map(int,input().split()))#消化コスト
f = list(map(int,input().split()))#食べにくさ
a.sort(reverse = True)
f.sort()
top = 10**13
bot = -1
def ok(x):
res = 0
for i in range(n):
res += max(0,a[i] - (x//f[i]))
return res <=k
while top - bot > 1:
mid = (top + bot) //2
if ok(mid):top = mid
else:bot = mid
print(top)
|
import sys
I=sys.stdin.readlines()
N,M,L=map(int,I[0].split())
a,b,c=0,0,0
D=[[L+1]*N for i in range(N)]
for i in range(M):
a,b,c=map(int,I[i+1].split())
a,b=a-1,b-1
D[a][b]=c
D[b][a]=c
for i in range(N):
D[i][i]=0
for k in range(N):
for i in range(N):
for j in range(N):
D[i][j]=min(D[i][j],D[i][k]+D[k][j])
D2=[[N*N+2]*N for i in range(N)]
for i in range(N):
D2[i][i]=0
for j in range(N):
if D[i][j]<=L:
D2[i][j]=1
for k in range(N):
for i in range(N):
for j in range(N):
D2[i][j]=min(D2[i][j],D2[i][k]+D2[k][j])
Q=int(I[M+1])
for i in range(Q):
a,b=map(int,I[i+2+M].split())
if N<D2[a-1][b-1]:
print(-1)
else:
print(D2[a-1][b-1]-1)
| 0 | null | 168,905,255,493,014 | 290 | 295 |
from collections import deque
process_num, qms = map(int ,input().split())
que = deque({})
for i in range(process_num):
name, time = input().split()
time = int(time)
que.append({"name":name, "time":time})
if __name__ == '__main__':
total_time = 0
while len(que)>0:
atop = que.popleft()
spend = min(atop["time"], qms)
atop["time"] -= spend
total_time += spend
if(atop["time"] == 0):
print("{} {}".format(atop["name"], total_time))
else:
que.append(atop)
|
import sys
import collections
s=sys.stdin.readlines()
n,q=map(int,s[0].split())
d=collections.deque(e.split()for e in s[1:])
t=0
while d:
k,v=d.popleft()
v=int(v)
if v>q:
v-=q
t+=q
d.append([k,v])
else:
t+=v
print(k,t)
| 1 | 45,836,853,088 | null | 19 | 19 |
import math
A = []
N = int(input())
count = 0
for n in range(N):
x = int(input())
if x == 2:
A.append(x)
count += 1
elif x % 2 == 0:
pass
elif x > 2:
for i in range(3, int(math.sqrt(x))+2, 2):
if x % i == 0:
break
else:
count += 1
A.append(x)
print(count)
|
def is_prime(x):
if x == 2:
return 1
elif x % 2 == 0:
return 0
l = x ** 0.5
for i in range(3, int(x ** 0.5) + 1, 2):
if x % i == 0:
return 0
return 1
import sys
def solve():
file_input = sys.stdin
N = file_input.readline()
cnt = 0
for l in file_input:
cnt += is_prime(int(l))
print(cnt)
solve()
| 1 | 10,587,806,380 | null | 12 | 12 |
from collections import Counter
n, k = map(int, input().split())
a = list(map(int, input().split()))
f = list(map(int, input().split()))
a.sort()
f.sort(reverse=True)
la = 10**6+1
cnt = [0] * la
for u in a:
cnt[u] += 1
for i in range(1, la):
cnt[i] += cnt[i-1]
def judge(x):
y = k
for i in range(n):
goal = x//f[i]
if a[i] > goal:
y -= a[i] - goal
if y < 0:
return False
return y >= 0
left = -1
right = 10**12 + 1
while left + 1 < right:
mid = (left + right)//2
if judge(mid):
right = mid
else:
left = mid
print(right)
|
from collections import deque
N = int(input())
def dfs(str):
if len(str) == N:
print(*str, sep="")
return
M = max(str)
for i in range(ord(M) - ord("a") + 2):
char = alpha[i]
str.append(char)
dfs(str)
str.pop()
alpha = 'abcdefghijklmnopqrstuvwxyz'
s = deque(["a"])
dfs(s)
| 0 | null | 108,953,833,750,188 | 290 | 198 |
N,K=map(int,input().split())
A=list(map(int,input().split()))
A.sort(reverse=True)
mod=10**9+7
inv_t=[0,1]
factorial=[1,1]
factorial_re=[1,1]
for i in range(2,N+1):
inv_t.append(inv_t[mod%i]*(mod-mod//i)%mod)
for i in range(2,N+1):
factorial.append(factorial[i-1]*i%mod)
factorial_re.append(factorial_re[i-1]*inv_t[i]%mod)
ans=0
for i in range(N-K+1):
if i==N-K:
ans+=A[i]
ans%=mod
break
ans+=A[i]*factorial[N-1-i]*factorial_re[K-1]*factorial_re[N-K-i]%mod
ans%=mod
for i in range(N-K+1):
if i==N-K:
ans-=A[N-i-1]
ans%=mod
break
ans-=A[N-i-1]*factorial[N-1-i]*factorial_re[K-1]*factorial_re[N-K-i]%mod
ans%=mod
print(ans)
|
n, u, v = map(int, input().split())
matrix = [[] for _ in range(n)]
import sys
sys.setrecursionlimit(10**8)
for _ in range(n-1):
a,b = map(int, input().split())
matrix[a-1].append(b-1)
matrix[b-1].append(a-1)
C = [0]*n
D = [0]*n
def dfs(x, pre, cnt, c):
c[x] = cnt
cnt += 1
for a in matrix[x]:
if a == pre:
continue
dfs(a, x, cnt, c)
dfs(v-1, -1, 0, C)
dfs(u-1, -1, 0, D)
ans=0
for i in range(n):
if C[i] > D[i]:
ans = max(ans, C[i]-1)
print(ans)
| 0 | null | 106,382,083,710,510 | 242 | 259 |
n, m = map(int, input().split())
if n % 2 == 1:
for i in range(m):
print(i + 1, n - i)
else:
for i in range(m):
if 2 * (i + 1) <= m + 1:
print(i + 1, n - i)
else:
print(i + 1, n - i - 1)
|
import math
nums = [int(e) for e in input().split()]
n = nums[0]
m = nums[1]
x = -1
y = -1
ms = []
if n%2 == 0:
x = 1
y = n
count = True
while x < y:
if y-x <= n/2 and count:
y -= 1
count = False
ms += [[x,y]]
x += 1
y -= 1
else:
x = 1
y = n
while x < y:
ms += [[x, y]]
x += 1
y -= 1
# print(ms)
for i in range(m):
print(str(ms[i][0]) + " " + str(ms[i][1]))
| 1 | 28,454,097,169,590 | null | 162 | 162 |
N = int(input())
print(N + N**2 + N**3)
|
#!/usr/bin/python3
import sys
from functools import reduce
from operator import mul
input = lambda: sys.stdin.readline().strip()
n, k = [int(x) for x in input().split()]
a = sorted((int(x) for x in input().split()), key=abs)
sgn = [0 if x == 0 else x // abs(x) for x in a]
M = 10**9 + 7
def modmul(x, y):
return x * y % M
if reduce(mul, sgn[-k:]) >= 0:
print(reduce(modmul, a[-k:], 1))
else:
largest_unselected_neg = next(i for i, x in enumerate(reversed(a[:-k])) if x < 0) if any(x < 0 for x in a[:-k]) else None
largest_unselected_pos = next(i for i, x in enumerate(reversed(a[:-k])) if x > 0) if any(x > 0 for x in a[:-k]) else None
smallest_selected_neg = next(i for i, x in enumerate(a[-k:]) if x < 0) if any(x < 0 for x in a[-k:]) else None
smallest_selected_pos = next(i for i, x in enumerate(a[-k:]) if x > 0) if any(x > 0 for x in a[-k:]) else None
can1 = largest_unselected_neg is not None and smallest_selected_pos is not None
can2 = largest_unselected_pos is not None and smallest_selected_neg is not None
def ans1():
return list(reversed(a[:-k]))[largest_unselected_neg] * reduce(modmul, (x for i, x in enumerate(a[-k:]) if i != smallest_selected_pos), 1) % M
def ans2():
return list(reversed(a[:-k]))[largest_unselected_pos] * reduce(modmul, (x for i, x in enumerate(a[-k:]) if i != smallest_selected_neg), 1) % M
if can1 and not can2:
print(ans1())
elif can2 and not can1:
print(ans2())
elif can1 and can2:
if list(reversed(a[:-k]))[largest_unselected_neg] * a[-k:][smallest_selected_neg] > list(reversed(a[:-k]))[largest_unselected_pos] * a[-k:][smallest_selected_pos]:
print(ans1())
else:
print(ans2())
else:
print(reduce(modmul, a[:k], 1))
| 0 | null | 9,836,285,415,870 | 115 | 112 |
from itertools import product
H, W, K = map(int, input().split())
G = [list(map(int, list(input()))) for _ in range(H)]
ans = float("inf")
for pattern in product([0, 1], repeat = H - 1):
div = [0] + [i for i, p in enumerate(pattern, 1) if p == 1] + [H]
rows = []
for i in range(len(div) - 1):
row = []
for j in range(W):
tmp = 0
for k in range(div[i], div[i + 1]):
tmp += G[k][j]
row.append(tmp)
rows.append(row)
flag = True
for row in rows:
if [r for r in row if r > K]:
flag = False
break
if not flag:
continue
tmp_ans = 0
cnt = [0] * len(rows)
w = 0
while w < W:
for r in range(len(rows)):
cnt[r] += rows[r][w]
if [c for c in cnt if c > K]:
cnt = [0] * len(rows)
tmp_ans += 1
w -= 1
w += 1
tmp_ans += pattern.count(1)
ans = min(ans, tmp_ans)
print(ans)
|
h, w, k = map(int, input().split())
s = [list(input()) for _ in range(h)]
# 2次元累積和
acum = [[0]*(w+1) for _ in range(h+1)]
for i in range(h):
for j in range(w):
acum[i+1][j+1] = acum[i][j+1] + acum[i+1][j] - acum[i][j] + int(s[i][j])
#print(acum)
def white_choco_count(x1, y1, x2, y2):
return acum[y2][x2] - acum[y2][x1] - acum[y1][x2] + acum[y1][x1]
ans = float("inf")
for rows in range(2**(h-1)):
count = 0
hlist = []
for i in range(h-1):
if rows >> i & 1 == 1:
count += 1 # 横線の数カウントしておく
hlist.append(i+1)
hlist.append(h)
#print(hlist)
x1 = 0
for x2 in range(w):
y1 = 0
for y2 in hlist:
if white_choco_count(x1, y1, x2, y2) > k:
count += float("inf")
# kを超えてはいけないが、一個右で縦に割るとkを超える必要がある(貪欲にやるので)
elif white_choco_count(x1, y1, x2, y2) <= k < white_choco_count(x1, y1, x2+1, y2):
count += 1
# 次のブロックにいくのでx1更新(ただしy1の更新はx2がwまでおわったあとにしたいのでbreak使うとうまくいく)
x1 = x2
break
y1 = y2
# ここまでで、x2=0, 1, ..., w-1 までみたが、x2 = wのとき(ブロックの右端)を見てない
y1 = 0
for y2 in hlist:
if white_choco_count(x1, y1, w, y2) > k:
count += float("inf")
y1 = y2
#print(count)
ans = min(ans, count)
print(ans)
| 1 | 48,699,838,636,290 | null | 193 | 193 |
num=int(input())
r_1=int(input())
r_2=int(input())
max_profit=r_2-r_1
if r_1<r_2:
min_value=r_1
else:
min_value=r_2
for i in range(2,num):
x=int(input())
t=x-min_value
if max_profit<t:
max_profit=t
if x<min_value:
min_value=x
print(max_profit)
|
# -*- coding: utf-8 -*-
num = int(raw_input())
a = int(raw_input())
b = int(raw_input())
diff = b - a
pre_min = min(a,b)
counter = 2
while counter < num:
current_num = int(raw_input())
if diff < current_num - pre_min:
diff = current_num - pre_min
if pre_min > current_num:
pre_min = current_num
counter += 1
print diff
| 1 | 12,166,913,688 | null | 13 | 13 |
#
import sys
input=sys.stdin.readline
from collections import deque
def main():
N,X,Y=map(int,input().split())
X-=1
Y-=1
adj=[[] for i in range(N)]
adj[X].append(Y)
adj[Y].append(X)
for i in range(N-1):
adj[i].append(i+1)
adj[i+1].append(i)
cnt=[0]*(N)
for i in range(N):
dist=[-1]*N
qu=deque([i])
dist[i]=0
while(len(qu)>0):
v=qu.popleft()
for nv in adj[v]:
if dist[nv]==-1:
dist[nv]=dist[v]+1
cnt[dist[nv]]+=1
qu.append(nv)
for i in range(1,N):
print(cnt[i]//2)
if __name__=="__main__":
main()
|
def mlt(): return map(int, input().split())
x, a, b = mlt()
dp = [0 for n in range(x)]
for n in range(1, x+1):
for k in range(n+1, x+1):
d1 = k-n
d2 = abs(a-n)+1+abs(b-k)
ds = min(d1, d2)
dp[ds] += 1
print(*dp[1:], sep='\n')
| 1 | 44,135,271,420,842 | null | 187 | 187 |
N = int(input())
A = list(map(int, input().split()))
if A[0] == 1:
if len(A) > 1:
print(-1)
exit()
else:
print(1)
exit()
elif A[0] > 1:
print(-1)
exit()
S = [0] * (N + 2)
for i in range(N, -1, -1):
S[i] = S[i + 1] + A[i]
sec = [0] * (N + 1)
sec[0] = 1
for i in range(1, N + 1):
a = A[i]
v = sec[i - 1] * 2
if v < a:
print(-1)
exit()
sec[i] = min(v - a, S[i + 1])
print(sum(A) + sum(sec[:-1]))
# print(sec)
# print(A)
|
import math
def main():
n = int(input())
A = list(map(int, input().split()))
max_node = [0 for i in range(n+1)]
min_node = [0 for i in range(n+1)]
res = 0
for i in range(n, -1, -1):
if i == n:
max_node[i] = A[i]
min_node[i] = A[i]
elif i == 0:
max_node[i] = 1
min_node[i] = 1
elif i == 1 and n != 1:
max_node[i] = 2
min_node[i] = math.ceil(min_node[i+1] /2) + A[i]
else:
max_node[i] = max_node[i+1] + A[i]
min_node[i] = math.ceil(min_node[i+1] / 2) + A[i]
for i in range(n):
if i == 0:
if n != 0 and A[i] != 0:
res = -1
break
else:
if max_node[i] > 2 * (max_node[i-1] - A[i-1]):
max_node[i] = 2 * (max_node[i-1] - A[i-1])
if max_node[i] < min_node[i]:
res = -1
break
if res == -1:
print(res)
else:
if n == 0 and A[i] != 1:
print(-1)
else:
print(sum(max_node))
if __name__ == '__main__':
main()
| 1 | 18,681,215,211,360 | null | 141 | 141 |
import numpy as np
A,B,H,M = map(int,input().split())
pi = np.pi
h_angle = (H + M/60) * 2*pi / 12
m_angle = M * 2 * pi / 60
h_x = A * np.cos(h_angle)
h_y = A * np.sin(h_angle)
m_x = B * np.cos(m_angle)
m_y = B*np.sin(m_angle)
print(np.sqrt((h_x - m_x) ** 2 + (h_y - m_y) ** 2))
|
import math
a, b, h, m = map(int, input().split())
c = abs((60*h+m)*0.5 - m*6)
if c>180:
c=360-c
c=(a**2 + b**2 - 2*a*b*math.cos(math.radians(c)))**0.5
print(c)
| 1 | 20,050,800,045,000 | null | 144 | 144 |
r=int(input())
print(3.141592653589793*r*2)
|
r = int(input())
pi = 3.14159265358979323846
print(2*pi*r)
| 1 | 31,526,482,254,334 | null | 167 | 167 |
while True:
try:
a, b = map(int, raw_input().split())
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a%b)
g = gcd(a,b)
l = a*b/g
print g, l
except:
break
|
class BIT:
def __init__(self, n):
self.n = n
self.bit = [0]*(self.n+1) # 1-indexed
def init(self, init_val):
for i, v in enumerate(init_val):
self.add(i, v)
def add(self, i, x):
# i: 0-indexed
i += 1 # to 1-indexed
while i <= self.n:
self.bit[i] += x
i += (i & -i)
def sum(self, i, j):
# return sum of [i, j)
# i, j: 0-indexed
return self._sum(j) - self._sum(i)
def _sum(self, i):
# return sum of [0, i)
# i: 0-indexed
res = 0
while i > 0:
res += self.bit[i]
i -= i & (-i)
return res
class RangeAddBIT:
def __init__(self, n):
self.n = n
self.bit1 = BIT(n)
self.bit2 = BIT(n)
def init(self, init_val):
self.bit2.init(init_val)
def add(self, l, r, x):
# add x to [l, r)
# l, r: 0-indexed
self.bit1.add(l, x)
self.bit1.add(r, -x)
self.bit2.add(l, -x*l)
self.bit2.add(r, x*r)
def sum(self, l, r):
# return sum of [l, r)
# l, r: 0-indexed
return self._sum(r) - self._sum(l)
def _sum(self, i):
# return sum of [0, i)
# i: 0-indexed
return self.bit1._sum(i)*i + self.bit2._sum(i)
import sys
import io, os
#input = sys.stdin.buffer.readline
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def main():
n, d, a = map(int, input().split())
XH = []
for i in range(n):
x, h = map(int, input().split())
XH.append((x-d, h))
XH.sort()
X = []
H = []
for x, h in XH:
X.append(x)
H.append(h)
bit = RangeAddBIT(n+1)
bit.init(H)
import bisect
ans = 0
for i in range(n):
h = bit.sum(i, i+1)
if h > 0:
q = (h+a-1)//a
ans += q
j = bisect.bisect_right(X, X[i]+2*d)
bit.add(i, j, -q*a)
print(ans)
if __name__ == '__main__':
main()
| 0 | null | 41,342,989,703,730 | 5 | 230 |
import sys
input = sys.stdin.readline
MI=lambda:map(int,input().split())
LI=lambda:list(map(int,input().split()))
N,K=MI()
A=LI()
A.sort(reverse=True)
F=LI()
F.sort()
def check(x):
# x分以内に食べきれるための修行回数はK回以下か
k=0
for i in range(N):
k+=max(int(0--(A[i]-x//F[i])//1),0)
return k<=K
ok=10**13
ng=-1
while abs(ok-ng)>1:
mid=(ok+ng)//2
if check(mid):
ok=mid
else:
ng=mid
print(ok)
|
N,K = map(int,input().split())
A = list(map(int,input().split()))
F = list(map(int,input().split()))
A.sort()
F.sort(reverse=True)
ng = -1
ok = 10**12+100
while ok - ng > 1:
mid = (ok + ng) // 2
if sum(max(0,a-mid//f) for a,f in zip(A,F)) <= K:
ok = mid
else:
ng = mid
print(ok)
| 1 | 165,495,920,589,582 | null | 290 | 290 |
n = int(input())
s = []
t = []
for i in range(n):
ss,tt = map(str,input().split())
s.append(ss)
t.append(tt)
x = input()
ans = 0
for i in range(n-1):
if ans != 0:
ans += int(t[i+1])
elif s[i] == x:
ans += int(t[i+1])
print(ans)
|
def main():
N, K, C = map(int, input().split())
S = input()
# greedy
head, tail = [-C - 1] * (K + 1), [N + C + 1] * (K + 1)
idx = 0
for i in range(N):
if S[i] == 'o' and i - head[idx] > C:
idx += 1
head[idx] = i
if idx == K:
break
idx = K
for i in range(N - 1, -1, -1):
if S[i] == 'o' and tail[idx] - i > C:
idx -= 1
tail[idx] = i
if idx == 0:
break
# ans
for i in range(K):
if head[i + 1] == tail[i]:
print(tail[i] + 1)
if __name__ == '__main__':
main()
| 0 | null | 69,132,675,088,520 | 243 | 182 |
rawtemp = input()
temp = int(rawtemp)
if temp < 30:
print("No")
else:
print("Yes")
|
while 1:
s=input()
if'-'==s:break
for _ in range(int(input())):
a=int(input())
s=s[a:]+s[:a]
print(s)
| 0 | null | 3,883,159,633,924 | 95 | 66 |
a,b = map(int, input().split())
c=a*b
d=2*(a+b)
print(c,d)
|
import sys
prm = sys.stdin.readline()
prm = prm.split()
a = int(prm[0])
b = int(prm[1])
men = a * b
syu = a + a + b + b
print men, syu
| 1 | 306,263,360,800 | null | 36 | 36 |
N, M, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A_sum = [0]
B_sum = [0]
A_now = 0
B_now = 0
for i in range(N):
A_now = A[i] + A_now
A_sum.append(A_now)
for i in range(M):
B_now = B[i] + B_now
B_sum.append(B_now)
ans = 0
for i in range(N+1):
book_count = i
remain_time = K - A_sum[i]
ok = 0
ng = M
if remain_time >= B_sum[M]:
ans = book_count + M
while ng - ok > 1:
mid = (ok+ng)//2
if remain_time >= B_sum[mid]:
ok = mid
else:
ng = mid
book_count += ok
if remain_time >= 0:
if book_count > ans:
ans = book_count
# print(mid,remain_time,B_sum[ok])
print(ans)
|
import sys
#input = sys.stdin.buffer.readline
def main():
H,W,K = map(int,input().split())
s = [list(map(str,input())) for _ in range(H)]
ans = [[0 for _ in range(W)] for _ in range(H)]
up,down = 0,1
count = 1
for i in range(H):
if "#" not in s[i]:
if down != H:
down += 1
else:
for x in range(up,down):
for y in range(W):
ans[x][y] = ans[up-1][y]
else:
now = 0
for j in range(W):
if s[i][j] == "#":
for x in range(up,down):
for y in range(now,j+1):
ans[x][y] = count
now = j+1
count += 1
if s[i][-1] == ".":
for x in range(up,down):
for y in range(now,W):
ans[x][y] = count-1
up = down
down += 1
for i in range(H):
print(*ans[i])
if __name__ == "__main__":
main()
| 0 | null | 77,367,748,327,868 | 117 | 277 |
N=int(input())
zls=[0 for _ in range(N)]
wls=[0 for _ in range(N)]
for i in range(N):
x,y=[int(s) for s in input().split()]
zls[i]=x+y
wls[i]=x-y
print(max([max(zls)-min(zls),max(wls)-min(wls)]))
|
n=int(input())
a=[]
b=[]
c=[]
d=[]
for i in range(n):
k,l=map(int,input().split())
a.append((k-l,i))
b.append((k+l,i))
c.append((-k-l,i))
d.append((-k+l,i))
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
d.sort(reverse=True)
print(max(a[0][0]+d[0][0],b[0][0]+c[0][0]))
| 1 | 3,433,162,469,198 | null | 80 | 80 |
N = int(input())
ans = -(-N // 2)
print(ans)
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
??´?????????
"""
suits = {"S": 0, "H": 1, "C": 2, "D": 3}
nsuits = ["S", "H", "C", "D"]
cards = [[0 for i in range(13)] for j in range(4)]
n = int(input())
for i in range(n):
inp = input().split(" ")
s = inp[0]
c = int(inp[1]) - 1
cards[suits[s]][c] = 1
for i in range(4):
for j in range(13):
if cards[i][j] == 0:
print("{0} {1}".format(nsuits[i], j+1))
| 0 | null | 30,034,272,199,890 | 206 | 54 |
n = int(input())
ans = ""
k = 0
while n // (26**(k)) > 0:
ans = ans + str(chr(((n-1) % (26**(k+1))+1)//(26**k) + 96))
n = n - ((n-1) % (26**(k+1))+1)
k += 1
print(ans[::-1])
|
n = int(input())
first_list = ['AC', 'WA', 'TLE', 'RE']
list = []
for _ in range(n):
list.append(input())
for i in first_list:
print(i, 'x', list.count(i))
| 0 | null | 10,209,566,715,760 | 121 | 109 |
import math
INFTY=100000000
n=int(input())
def kock(p1,p2,n):
s=[(p1[0]*2+p2[0])/3, (p1[1]*2+p2[1])/3]
t=[(p1[0]+p2[0]*2)/3, (p1[1]+p2[1]*2)/3]
u=[(t[0]-s[0])/2-(t[1]-s[1])/2*math.sqrt(3)+s[0], (t[0]-s[0])/2*math.sqrt(3)+(t[1]-s[1])/2+s[1]]
if n==0:
print("%.8f %.8f"%(p1[0],p1[1]))
else:
kock(p1,s,n-1)
kock(s,u,n-1)
kock(u,t,n-1)
kock(t,p2,n-1)
start=[0.0,0.0]
end=[100.0, 0.0]
if n==0:
print("%.8f %.8f"%(start[0],start[1]))
print("%.8f %.8f"%(end[0],end[1]))
else:
kock(start,end,n)
print("%.8f %.8f"%(end[0],end[1]))
|
n,t=map(int,input().split())
ab=[list(map(int,input().split()))for _ in range(n)]
ab.sort()
dp=[(6007)*[0]for _ in range(n+1)]
dp[0][0]=0
ans=0
for i in range(n):
for j in range(6007):
dp[i+1][j]=max(dp[i+1][j],dp[i][j])
if j<t:dp[i+1][j+ab[i][0]]=max(dp[i][j]+ab[i][1],dp[i][j+ab[i][0]])
ans=max(ans,dp[i+1][j])
print(ans)
| 0 | null | 76,237,435,724,030 | 27 | 282 |
def II(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
N=II()
A=LI()
#A.sort()
SA=sum(A)
#print(SA)
AccA=[A[0]]
for i in range(1,N):
AccA.append(AccA[-1]+A[i])
#AccA.reverse()
ans=0
for i in range(N-1):
ans+=A[i]*(SA-AccA[i])
ans=ans%(10**9+7)
print(ans)
|
n = int(input())
a_list = list(map(int, input().split()))
sum_l = sum(a_list)
ans = 0
for i in range(n-1):
sum_l -= a_list[i]
ans += sum_l * a_list[i]
print(ans % 1000000007)
| 1 | 3,798,970,391,690 | null | 83 | 83 |
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np
from numba import njit
MOD = 10**9 + 7
N = int(readline())
S = np.array(list(read().rstrip()), np.int8)
R = np.sum(S == ord('R'))
B = np.sum(S == ord('B'))
G = np.sum(S == ord('G'))
@njit
def f(S):
N = len(S)
ret = 0
for i in range(N):
for j in range(i + 1, N):
k = j + j - i
if k >= N:
break
if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
ret += 1
return ret
answer = R * B * G - f(S)
print(answer)
|
n = int(input())
s = input()
ans = s.count('R')*s.count('G')*s.count('B')
for i in range(n-2):
for j in range(i, n-1):
if s[i] != s[j]:
if 2*j-i <= n-1 and s[i]!=s[j] and s[i]!=s[2*j-i] and s[j]!=s[2*j-i]:
ans -= 1
print(ans)
| 1 | 36,227,518,733,450 | null | 175 | 175 |
import math
t1,t2 = map(int, input().split())
c1,c2 = map(int, input().split())
d1,d2 = map(int, input().split())
x = [(c1*t1, c2*t2),(d1*t1, d2*t2)]
x.sort(reverse=True)
if x[0][0]+x[0][1] == x[1][0]+x[1][1]:
print("infinity")
elif x[0][0]+x[0][1] > x[1][0]+x[1][1]:
print(0)
else:
n = (x[0][0]-x[1][0]+0.0)/(x[1][0]+x[1][1]-x[0][0]-x[0][1])
m = math.ceil(n)
if math.floor(n) == m:
print(2*m)
else:
print(2*m-1)
|
import sys
sys.setrecursionlimit(2147483647)
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
input = lambda:sys.stdin.readline().rstrip()
from functools import cmp_to_key
def resolve():
n = int(input())
positive, negative = [], []
for _ in range(n):
balance = min_balance = 0
for c in input():
if c == '(':
balance += 1
else:
balance -= 1
min_balance = min(min_balance, balance)
if balance > 0:
positive.append((min_balance, balance))
else:
negative.append((min_balance, balance))
now = 0
# positive は min_balance の大きい方から足せば良い
positive.sort(reverse = 1)
for min_balance, balance in positive:
if now + min_balance < 0:
print("No")
return
now += balance
# negative は sort を工夫
def cmp(s1, s2):
m1, b1 = s1
m2, b2 = s2
return -1 if min(m1, b1 + m2) > min(m2, b2 + m1) else 1
negative.sort(key = cmp_to_key(cmp))
for min_balance, balance in negative:
if now + min_balance < 0:
print("No")
return
now += balance
print("Yes" if now == 0 else "No")
resolve()
| 0 | null | 77,563,217,292,792 | 269 | 152 |
#!/usr/bin/python
# coding: utf-8
def maze(string, h):
return string[h:] + string[:h]
while True:
string = raw_input()
if string == "-":
break
m = int(raw_input())
for i in range(m):
h = int(raw_input())
string = maze(string, h)
#print "h: {}, string: {}".format(h, string)
print string
|
while 1:
a = input()
if a == "-":
break
b = int(input())
for i in range(b):
c = int(input())
a = a[c:] + a[:c]
print(a)
#word = "ABCDE"
#print(word[2:]) ## => CDE
#print(word[:3]) ## => ABC
| 1 | 1,921,994,859,808 | null | 66 | 66 |
situdo = input()
if int(situdo) >= 30:
print("Yes")
else:
print("No")
|
H = int(input())
W = int(input())
N = int(input())
a = max(H, W)
print(N // a + min(1, N % a))
| 0 | null | 47,231,082,305,510 | 95 | 236 |
n = int(input())
a = list(map(int, input().split())) # 横入力
x = [0]
aaa = 0
for i in range(n):
aaa += a[i]
x.append(aaa)
aa = sum(a)
y = [aa]
for i in range(n):
aa -= a[i]
y.append(aa)
ans = 202020202020
for i in range(n+1):
ans = min(ans, abs(x[i] - y[i]))
print(ans)
|
from itertools import accumulate
n = int(input())
a = list(map(int, input().split()))
ac = list(accumulate(a))
total = ac[-1]
ans = float('inf')
for i in range(n-1):
l, r = ac[i], total - ac[i]
ans = min(ans, abs(l-r))
print(ans)
| 1 | 142,472,025,561,600 | null | 276 | 276 |
import sys
readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)
def main():
S = input()
p = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"]
print(7 - p.index(S))
if __name__ == '__main__':
main()
|
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def s(): 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 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
s = s()
a = ["SAT", "FRI", "THU","WED","TUE","MON","SUN"]
x = a.index(s) + 1
print(x)
| 1 | 133,126,325,032,518 | null | 270 | 270 |
import math
a, b, C = map(int, raw_input().split())
S = a * b * math.sin(math.radians(C)) / 2
L = math.sqrt(a**2 + b**2 - 2*a*b*math.cos(math.radians(C))) + a + b
h = 2*S/a
print S
print L
print h
|
h = int(input())
count = 0
for i in range(40):
count += 2**i
if h >= 2 ** i and h < 2 ** (i+1):
break
print(count)
| 0 | null | 40,063,117,816,370 | 30 | 228 |
import sys
import time
import math
import itertools as it
def inpl():
return list(map(int, input().split()))
st = time.perf_counter()
# ------------------------------
N, M = inpl()
H = inpl()
dp = [True] * N
for _ in range(M):
A, B = inpl()
if H[A-1] > H[B-1]:
dp[B-1] = False
elif H[A-1] < H[B-1]:
dp[A-1] = False
else:
dp[A-1] = False
dp[B-1] = False
ans = 0
for i in dp:
if i:
ans += 1
print(ans)
# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)
|
import sys
from fractions import gcd
[print("{} {}".format(gcd(k[0], k[1]), (k[0] * k[1]) // gcd(k[0], k[1]))) for i in sys.stdin for k in [[int(j) for j in i.split()]]]
| 0 | null | 12,594,276,753,874 | 155 | 5 |
def main():
n = int(input())
print((n + 1) // 2)
main()
|
def main():
from sys import setrecursionlimit, stdin, stderr
from os import environ
from collections import defaultdict, deque, Counter
from math import ceil, floor
from itertools import accumulate, combinations, combinations_with_replacement
setrecursionlimit(10**6)
dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
input = lambda: stdin.readline().rstrip()
LMIIS = lambda: list(map(int,input().split()))
II = lambda: int(input())
P = 10**9+7
INF = 10**18+10
K = II()
S = input()
N = len(S)
def gencmb(n,k):
# N-1+K C K ... N-1 C 0
ans = [1]*(k+1)
for i in range(1,k+1):
ans[-i-1] = ans[-i] * (N+i-1) * pow(i,P-2,P) % P
return ans
ans = 0
pow26 = [0] * (K+1)
pow26[0] = 1
for i in range(1,K+1):
pow26[i] = pow26[i-1] * 26 % P
pow25 = [0] * (K+1)
pow25[0] = 1
for i in range(1,K+1):
pow25[i] = pow25[i-1] * 25 % P
cmb = gencmb(N,K)
for t in range(K+1):
ans += pow26[t]*pow25[K-t]*cmb[t]%P
ans %= P
print(ans)
main()
| 0 | null | 35,870,684,424,230 | 206 | 124 |
n , k = map(int,input().split())
a = list(map(int,input().split()))
f = list(map(int,input().split()))
a.sort()
f.sort(reverse=True)
lef = -1
rig = 10**13
while rig - lef != 1:
now = (rig + lef) //2
cou = 0
for i in range(n):
t = now // f[i]
cou += max(0,a[i]-t)
if cou <= k:
rig = now
elif cou > k:
lef = now
print(rig)
|
def resolve():
H, W = list(map(int, input().split()))
S = [list(input()) for _ in range(H)]
import collections
maxdist = 0
for startx in range(H):
for starty in range(W):
if S[startx][starty] == "#":
continue
visited = [[-1 for _ in range(W)] for __ in range(H)]
visited[startx][starty] = 0
q = collections.deque([(startx, starty)])
while q:
x, y = q.pop()
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
nx = x + dx
ny = y + dy
if 0 <= nx < H and 0 <= ny < W and S[nx][ny] != "#" and visited[nx][ny] == -1:
visited[nx][ny] = visited[x][y] + 1
q.appendleft((nx, ny))
maxdist = max(maxdist,max(sum(visited, [])))
print(maxdist)
if '__main__' == __name__:
resolve()
| 0 | null | 129,372,410,063,640 | 290 | 241 |
N,M=map(int,input().split())
H=list(map(int,input().split()))
route={i:[] for i in range(1,N+1)}
for i in range(M):
array=tuple(map(int,input().split()))
route[array[0]].append(array[1])
route[array[1]].append(array[0])
Obs=[True for i in range(N)]
for i in range(1,N+1):
if Obs[i-1]==True:
for j in route[i]:
if H[i-1]>H[j-1]:
Obs[j-1]=False
else:
Obs[i-1]=False
ans=0
for i in range(len(Obs)):
if Obs[i]:
ans+=1
print(ans)
|
n , m = map(int,input().split())
h = list(map(int,input().split()))
ma = [0] * (n)
for i in range(m):
a , b = map(int,input().split())
a , b = a - 1 , b - 1
ma[a] = max(ma[a] , h[b])
ma[b] = max(ma[b] , h[a])
ans = 0
for i in range(n):
ans += h[i] > ma[i]
print(ans)
| 1 | 25,295,418,104,542 | null | 155 | 155 |
a = int(input())
ans = a + (a ** 2) + (a ** 3)
print(ans)
|
a = int(input())
print((a)+(a ** 2)+(a ** 3))
| 1 | 10,193,768,462,262 | null | 115 | 115 |
N = int(input())
A = list(map(int, input().split()))
D = {a:0 for a in set(A)}
for a in A:
D[a] += 1
S = 0
for v in D.values():
if v>=2:
S += v*(v-1)//2
for a in A:
d = D[a]
if d==1:
print(S)
else:
print(S-d*(d-1)//2+(d-1)*(d-2)//2)
|
import collections
N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
ans = 0
for i in c:
ans+=c[i]*(c[i]-1)//2
for i in range(N):
print(ans-(c[A[i]]-1))
| 1 | 47,701,137,648,570 | null | 192 | 192 |
#Union-Find木の実装
from collections import Counter
N,M=map(int,input().split())
#初期化
par=[i for i in range(N)]#親の要素(par[x]=xのときxは木の根)
rank=[0]*N#木の深さ
#木の根を求める
def find(x):
#xが根だった場合xをそのまま返す
if par[x] == x:
return x
else:
#根が出るまで再帰する
par[x] = find(par[x])
return par[x]
#xとyの属する集合を併合
def unite(x,y):
x=find(x)#xの根
y=find(y)#yの根
#根が同じなら何もしない
if x == y:
return
#もし根の深さがyの方が深いなら
if rank[x] < rank[y]:
par[x] = y#yを根にする
else:
par[y] = x#xを根にする
if rank[x] == rank[y]:
rank[x]+=1
#xとyが同じ集合に属するか否か
#def same(x,y):
#return find(x)==find(y)
for _ in range(M):
a,b = map(int,input().split())
a,b = a-1, b-1
unite(a,b)
for i in range(N):
find(i)
c=Counter(par)
print(len(c)-1)
|
(N,M),*AB=[list(map(int, x.split())) for x in open(0).readlines() if len(x)>1]
class UnionFind:
def __init__(self, n):
self.n=n
self.parents=[-1 for i in range(n)]
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
rx = self.find(x)
ry = self.find(y)
if rx == ry:
return
if self.parents[rx] > self.parents[ry]:
rx,ry=ry,rx
self.parents[rx] = self.parents[ry]
self.parents[ry] = rx
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def members(self, x):
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i,x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def all_group_members(self):
return {r: self.members(r) for r in self.roots()}
d=UnionFind(N)
for a,b in AB:
d.union(a-1,b-1)
print(d.group_count() - 1)
| 1 | 2,265,665,041,498 | null | 70 | 70 |
import bisect
n = int(input())
L = list(map(int, input().split()))
#%%
L.sort()
ans = 0
for i in range(n-2): # aを固定
for j in range(i+1, n-1): # bを固定
ab = L[i] + L[j]
idx = bisect.bisect_left(L, ab, lo=j)
ans += max(idx - (j+1), 0)
print(ans)
|
import itertools
n = int(input())
A = list(map(int,input().split()))
A.sort()
def binary_search(l,r,v):
while r >= l:
h = (l+r) // 2
if A[h] < v:
l = h+1
else:
r = h-1
return r
ans = 0
for i in range(n-2):
for j in range(i+1,n-1):
a = binary_search(j,n-1,A[i]+A[j])
ans += a-j
print(ans)
| 1 | 172,227,676,803,490 | null | 294 | 294 |
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")
|
import sys
sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7
def input():
return sys.stdin.readline().strip()
def main():
N = int(input())
A = list(map(int, input().split()))
B = 0
for a in A:
B ^= a
ans = []
for a in A:
ans.append(a ^ B)
print(*ans, sep=" ")
if __name__ == "__main__":
main()
| 0 | null | 6,300,471,536,998 | 23 | 123 |
A,B,C,K=map(int,input().split())
cnt=0
if A>=K:
print(K)
else:
if A+B>=K:
print(A)
else:
if A+B+C<=K:
print(A-C)
else:
print(A-(K-(A+B)))
|
A,B,C,K = map(int,input().split())
if A >= K:
print(K)
elif B >= K - A:
print(A)
elif C >= K - A - B:
print(A - (K - A - B))
| 1 | 21,669,887,423,712 | null | 148 | 148 |
def solve():
a1, a2, a3 = map(int, input().split())
print('bwuisnt'[a1+a2+a3<=21::2])
if __name__ == '__main__':
solve()
|
import sys
def GcdLCM():
for line in sys.stdin:
x,y=map(int,line.split())
if x==None:
break
gcd = Gcd(x,y)
lcm = int(x*y/gcd)
print("{} {}".format(gcd,lcm))
def Gcd(x,y):
while x!=0:
x,y=y%x,x
return y
GcdLCM()
| 0 | null | 59,082,424,507,650 | 260 | 5 |
import sys
read = sys.stdin.read
def main():
N, K = map(int, read().split())
dp1 = [0] * (K + 1)
dp2 = [0] * (K + 1)
dp1[0] = 1
for x in map(int, str(N)):
for j in range(K, -1, -1):
if j > 0:
dp2[j] += dp2[j - 1] * 9
if x != 0:
dp2[j] += dp1[j - 1] * (x - 1) + dp1[j]
dp1[j] = dp1[j - 1]
else:
dp1[j] = 0
dp2[j] = 1
print(dp1[K] + dp2[K])
return
if __name__ == '__main__':
main()
|
import math
x, y, X, Y = [float(i) for i in input().split()]
print(math.sqrt((x-X)**2 + abs(y-Y)**2))
| 0 | null | 38,174,727,751,500 | 224 | 29 |
n, k = map(int, input().split())
if n >= k:
n %= k
n = min(n, k-n)
else :
n = min(n, k-n)
print(n)
|
N, K = map(int, input().split())
mod1 = N % K
mod2 = (K - mod1)
print(mod1 if mod1 < mod2 else mod2)
| 1 | 39,376,947,772,120 | null | 180 | 180 |
# -*- coding: utf-8 -*-
import sys
def main():
N,K = list(map(int, sys.stdin.readline().split()))
A_list = list(map(int, sys.stdin.readline().split()))
for i in range(K, len(A_list)):
if A_list[i] > A_list[i-K]:
print("Yes")
else:
print("No")
if __name__ == "__main__":
main()
|
n,a,b=map(int,input().split())
mod=10**9+7
def pow(x, n):
ans = 1
while(n > 0):
if(bin(n & 1) == bin(1)):
ans = ans*x%mod
x = x*x%mod
n = n >> 1
return ans
N=pow(2,n)
def comb(c,d):
x=1
for i in range(d):
x=x*(c-i)%mod
y=1
for i in range(d):
y=y*(i+1)%mod
return x*pow(y,mod-2)%mod
A=comb(n,a)
B=comb(n,b)
print((N-A-B-1)%mod)
| 0 | null | 36,425,662,624,778 | 102 | 214 |
N,u,v = map(int,input().split())
u -= 1
v -= 1
data = [[] for _ in range(N)]
for i in range(N-1):
A,B = map(int,input().split())
A -= 1
B -= 1
data[A].append(B)
data[B].append(A)
#print(data)
stack = [[u,u]]
taka_dis = [0] * N
while stack:
x = stack.pop()
#print(x)
for y in data[x[1]]:
if y == x[0]:
continue
else:
stack.append([x[1],y])
taka_dis[y] = taka_dis[x[1]] + 1
#print(stack)
stack = [[v,v]]
aoki_dis = [0] * N
while stack:
x = stack.pop()
for y in data[x[1]]:
if y == x[0]:
continue
else:
stack.append([x[1],y])
aoki_dis[y] = aoki_dis[x[1]] + 1
can = [0] * N
for i in range(N):
if aoki_dis[i] - taka_dis[i] > 0:
can[i] = aoki_dis[i]
else:
can[i] = 0
#print(aoki_dis)
#print(taka_dis)
#print(can)
print(max(can) -1 )
|
# coding: utf-8
N, Q = map(int,input().split())
#N,Q=map(int,input().split())
#親のノードを格納 2の親が4のとき par[2]=4
par=[0]
#木の深さを格納 根が2の深さが3の時 rank[2]=3
rank=[0]
#初期化
for i in range(1,N+1):
par.append(i)
rank.append(0)
#xの根を探す、同時に通過したノードすべてを根に直接つける
def find(x):
#xの親がxならば根なのでxを返す
if par[x]==x:
return x
else:
#違う場合は親の親を捜し自分の親にする
par[x] = find(par[x])
#再帰的に行うと根が見つかる
return par[x]
#xが属する木とyが属する木を併合する
def unite(x,y):
#根を探す
root_x=find(x)
root_y=find(y)
#根が同じなら元からつながっているので終わり
if root_x == root_y:
return
#ランクが大きい木の根の直下にランクが小さい木を結合
if rank[root_x] < rank[root_y]:
par[root_x] = root_y
else:
par[root_y] = root_x
#ランクが等しいときのみランクが増える#なぜ#yにxが結合?
if rank[root_x]==rank[root_y]:
rank[root_x]+=1
#xとyが同じ木に存在するかを調べる
def same(x,y):
return find(x)==find(y)
#main
for i in range(Q):
a,b=map(int,input().split())
unite(a, b)
res = []
for i in range(1, N+1):
res.append(find(i))
ans = len(set(res))-1
print(ans)
| 0 | null | 59,767,385,312,640 | 259 | 70 |
n,m = map(int,input().split())
a = list(map(int,input().split()))
ans = 0
for i in range(m):
ans += a[i]
if n < ans:
print(-1)
else:
print(n-ans)
|
A = input()
if(A.isupper()):
print("A")
else:
print("a")
| 0 | null | 21,597,439,425,860 | 168 | 119 |
S = input()
N = len(S)
mod = 2019
cnt = [0 for _ in range(2019)]
S = S[::-1]
wk = 0
cnt[0] = 1
for i in range(0,N):
wk = (wk + int(S[i]) * pow(10,i,mod)) % mod
#print(i,wk)
cnt[wk] += 1
ans = 0
for i in range(2019):
m = cnt[i]
ans += m * (m-1) // 2
print(ans)
|
import sys
while True:
n, x = [ int( val ) for val in sys.stdin.readline().split( " " ) ]
if 0 == n and 0 == x:
break
cnt = 0
goto = False
for i in range( 1, n-1 ):
if x < 3*(i+1):
break
for j in range( i+1, n ):
if x <= ( i+j ):
break
for k in range( j+1, n+1 ):
s = ( i + j + k )
if x == s:
cnt += 1
break
elif x < s:
goto = True
break
goto = False
print( cnt )
| 0 | null | 16,037,747,424,492 | 166 | 58 |
import math
a, b, h, m = map(int, input().split())
theta_a = (60*h+m)/720*2*math.pi
theta_b = m/60*2*math.pi
theta_a_b = abs(theta_a-theta_b)
ans = math.sqrt(a**2+b**2-2*a*b*math.cos(theta_a_b))
print(ans)
|
A, B = map(int, input().split())
if A <= 2 * B:
print('0')
else:
print(A - 2 * B)
| 0 | null | 93,235,995,872,650 | 144 | 291 |
def resolve():
N, X, M = map(int, input().split(" "))
checked = [False] * M
val = X
ans = 0
val_list = []
base = M
for i in range(N):
if checked[val] != False:
loop_start_index = checked[val]
loop_vals = val_list[loop_start_index:]
loop_val = sum(loop_vals)
loop_length = len(val_list[loop_start_index:])
if loop_length != 0:
ans += ((N - i) // loop_length) * loop_val
for i in range(((N - i) % loop_length)):
ans += loop_vals[i]
break
ans += val
checked[val] = i
val_list.append(val)
val*=val
if val >= base:
val %= base
print(ans)
if __name__ == "__main__":
resolve()
|
# -*- coding: utf-8 -*-
N, X, M = map(int, input().split())
mod_check_list = [False for _ in range(M)]
mod_list = [(X ** 2) % M]
counter = 1
mod_sum = (X ** 2) % M
last_mod = 0
for i in range(M):
now_mod = (mod_list[-1] ** 2) % M
if mod_check_list[now_mod]:
last_mod = now_mod
break
mod_check_list[now_mod] = True
mod_list.append(now_mod)
counter += 1
mod_sum += now_mod
loop_start_idx = 0
for i in range(counter):
if last_mod == mod_list[i]:
loop_start_idx = i
break
loop_list = mod_list[loop_start_idx:]
loop_num = counter - loop_start_idx
ans = 0
if mod_list[-1] == 0:
ans = X + sum(mod_list[:min(counter, N - 1)])
else:
if (N - 1) <= counter:
ans = X + sum(mod_list[:N - 1])
else:
ans += X + mod_sum
N -= (counter + 1)
ans += sum(loop_list) * (N // loop_num) + sum(loop_list[:N % loop_num])
print(ans)
| 1 | 2,809,733,875,052 | null | 75 | 75 |
MOD = 10 ** 9 + 7
MAX = 60
n = int(input())
a = list(map(int, input().split()))
cnt = [0 for i in range(MAX)]
for i in range(n):
bit = bin(a[i])[2:].zfill(MAX)
for j in range(MAX):
cnt[j] += int(bit[j])
ans = 0
for i in range(MAX):
ans += cnt[MAX - i - 1] * (n - cnt[MAX - i - 1]) * 2 ** i
ans %= MOD
print(ans)
|
class Queue:
queue_list = []
start = 0
def enqueue(self, a):
self.queue_list.append(a)
def dequeue(self):
assert self.start != len(self.queue_list), "オーバーフローが発生しました。"
self.start += 1
return self.queue_list[self.start - 1]
def isEmpty(self):
return self.start == len(self.queue_list)
n, q = list(map(lambda x: int(x), input().strip().split()))
queue = Queue()
for i in range(n):
queue.enqueue(input().strip().split())
sum_time = 0
while not queue.isEmpty():
name, time = queue.dequeue()
if int(time) <= q:
sum_time += int(time)
print(name + ' ' + str(sum_time))
else:
queue.enqueue([name, str(int(time) - q)])
sum_time += q
| 0 | null | 61,603,740,438,018 | 263 | 19 |
x=int(input())
y=int(input())
l=[1,2,3]
l.remove(x)
l.remove(y)
print(l[0])
|
N = int(input())
ans = [0]*80100
for x in range(1, 101):
for y in range(1, 101):
for z in range(1, 101):
ans[x**2+y**2+z**2+x*y+y*z+z*x] += 1
for i in range(1, N+1):
print(ans[i])
| 0 | null | 59,213,164,746,596 | 254 | 106 |
n=int(input())
s=input()
ans=0
alist=[0]*10
for i in range(n):
alist[int(s[i])]=i
for i in range(10):
flag1=False
for a in range(n-2):
if s[a]==str(i):
flag1=True
break
if flag1:
for j in range(10):
flag2=False
for b in range(a+1,n-1):
if s[b]==str(j):
flag2=True
break
if flag2:
for k in range(10):
if b<alist[k]:
ans+=1
print(ans)
|
S = input()[::-1]
## 余りを0~2018で総数保持
counts = [0]*2019
## 0桁の余りは0
counts[0] = 1
num, d = 0, 1
for c in S:
## 右から~桁の数字
num += int(c) * d
num %= 2019
## 次の桁
d *= 10
d %= 2019
counts[num] += 1
ans = 0
for cnt in counts:
## nC2の計算
ans += cnt * (cnt-1) //2
print(ans)
| 0 | null | 79,414,435,714,298 | 267 | 166 |
import math
n=int(input())
m=100000
k=100000
for i in range(n):
m=1.05*k
m=m/1000
k=math.ceil(m)*1000
print(k)
|
x, y = list(map(int, input().split()))
a = 4 * x - y
b = y - 2 * x
if (a % 2 == 0 and a >= 0) and (b % 2 == 0 and b >= 0):
print('Yes')
else:
print('No')
| 0 | null | 6,846,494,116,348 | 6 | 127 |
A,B,C,K=map(int,input().split())
R=A+B
if K<A:
print(K)
elif K<=R:
print(A)
else:
T=K-R
print(A-T)
|
r=map(float,raw_input().split())
x=r[0]-r[2]
y=r[1]-r[3]
if x<0:
x=x*-1
if y<0:
y=y*-1
print (x**2+y**2)**0.5
| 0 | null | 10,955,719,695,910 | 148 | 29 |
a=int(input())
b=int(input())
c=[1,2,3]
c.remove(a)
c.remove(b)
if c==[1]:
print(1)
elif c==[2]:
print(2)
else:
print(3)
|
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
import collections
c = collections.Counter(prime_factorize(n))
ans = 0
for _,value in c.items():
i = 0
while value>i:
i += 1
value -= i
ans += i
print(ans)
| 0 | null | 63,929,334,256,138 | 254 | 136 |
from collections import deque
N = int(input())
c = [chr(ord("a") + i) for i in range(26)]
q = deque("a")
ans = []
while q:
s = q.pop()
if len(s) == N:
ans.append(s)
continue
for x in c[:c.index(max(s)) + 2]:
q.append(s + x)
[print(a) for a in sorted(ans)]
|
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
# map(int, input().split())
N, M = map(int, input().split())
A = list(sorted(map(int, input().split())))
S = sum(A)
acc = [0] * (N + 1)
for i in range(1, N + 1):
acc[i] = acc[i - 1] + A[i - 1]
def check(x):
total = 0
cnt = 0
for a in A:
l = -1
r = N
while r - l > 1:
m = (l + r) // 2
if A[m] + a >= x:
r = m
else:
l = m
cnt += N - r
total += acc[N] - acc[r]
total += a * (N - r)
return (total, cnt)
left = 0
right = 10 ** 6
while right - left > 1:
mid = (left + right) // 2
res = check(mid)
if res[1] >= M:
left = mid
else:
right = mid
res = check(left)
ans = res[0]
print(ans - (res[1] - M) * left)
| 0 | null | 79,959,188,427,450 | 198 | 252 |
L,R,d = list(map(int,input().strip().split()))
print(R//d-(L-1)//d)
|
l,r,d=[int(x) for x in input().split()]
x=(r-l)//d
if l%d==0 or r%d==0:
x+=1
print(x)
| 1 | 7,518,721,344,546 | null | 104 | 104 |
n=int(input())
a=[None] * (n+1)
a[0] = 1
a[1] = 1
for i in range(n-1):
a[i+2]=a[i]+a[i+1]
print(a[n])
|
a,b = 1,1
n = int(input())
for i in range(n):
a,b = b,a+b
print(a)
| 1 | 1,877,216,836 | null | 7 | 7 |
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"
def main():
n,m,k = map(int,ipt().split())
fl = [[] for i in range(n)]
bl = [set() for i in range(n)]
for i in range(m):
a,b = map(int,ipt().split())
fl[a-1].append(b-1)
fl[b-1].append(a-1)
for i in range(k):
c,d = map(int,ipt().split())
bl[c-1].add(d-1)
bl[d-1].add(c-1)
ans = [-1]*n
al = [True]*n
for i in range(n):
if al[i]:
q = [i]
al[i] = False
st = set([i])
while q:
qi = q.pop()
for j in fl[qi]:
if al[j]:
st.add(j)
al[j] = False
q.append(j)
lst = len(st)
for j in st:
tmp = lst-len(fl[j])-1
for k in bl[j]:
if k in st:
tmp -= 1
ans[j] = tmp
print(" ".join(map(str,ans)))
return None
if __name__ == '__main__':
main()
|
import sys
N=int(input())
if N==2:
print(1)
sys.exit(0)
answer_set=set()
for i in range(2,int(N**0.5)+1):
N2=N
while N2%i==0:
N2//=i
if N2%i==1:
answer_set.add(i)
#print(answer_set)
for i in range(1,int(N**0.5)+1):
if (N-1)%i==0:
answer_set.add((N-1)//i)
#print(answer_set)
answer_set.add(N)
print(len(answer_set))
| 0 | null | 51,564,199,825,358 | 209 | 183 |
import bisect
n = int(input())
l = list(map(int, input().split()))
sort_l = sorted(l)
count = 0
for a in range(n):
for b in range(a+1, n):
c = bisect.bisect_left(sort_l, sort_l[a] + sort_l[b])
if c > b:
count += c - b -1
else:
pass
print(count)
|
import bisect
import sys
from bisect import bisect_left
from operator import itemgetter
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().split())
def lmi(): return list(map(int, input().split()))
def lmif(n): return [list(map(int, input().split())) for _ in range(n)]
def main():
N = ii()
L = lmi()
L.sort()
# print(L)
# 2辺を固定して、2辺の和より長い辺の数を数える
count = 0
for i in range(N):
for j in range(i+1, N):
limit = L[i] + L[j]
longer = bisect_left(L, limit)
tmp = longer - (j + 1)
# print(limit, longer, tmp)
count += longer - (j + 1)
print(count)
return
main()
| 1 | 172,081,369,170,140 | null | 294 | 294 |
import sys
x=int(input())
n=1
while(100*n<=x):
if(x<=105*n):
print(1)
sys.exit()
n+=1
print(0)
|
X = int(input())
for i in range(1000):
if 100 * i <= X and X <= 105 * i:
print(1)
break
else:
print(0)
| 1 | 126,856,476,669,450 | null | 266 | 266 |
n=list(input())
a=0
for i in n:
a+=int(i)
if a%9==0:
print('Yes')
else:
print('No')
|
import itertools
import math
def calc_dist(pos1, pos2):
x1,y1=pos1
x2,y2=pos2
return math.sqrt(pow(x1-x2, 2)+pow(y1-y2, 2))
N=int(input())
distance_mat = [[-1000 for _ in range(N)] for _ in range(N)]
pos = []
for _ in range(N):
pos.append(list(map(int, input().split())))
perms = itertools.permutations(list(range(N)), N)
result=[]
for perm in perms:
dist_sum = 0
for i in range(1, N):
source = perm[i-1]
sink = perm[i]
if distance_mat[source][sink]==-1000:
dist = calc_dist(pos[source], pos[sink])
distance_mat[source][sink] = dist
distance_mat[sink][source] = dist
else:
dist = distance_mat[source][sink]
dist_sum+=dist
result.append(dist_sum)
import numpy as np
print(np.average(result))
| 0 | null | 76,364,638,214,102 | 87 | 280 |
H, W, K = map(int, input().split())
cake = [list(input()) for _ in range(H)]
ans = [[0] * W for _ in range(H)]
id = 1
sign = 0
nothing = 0
for i in range(H):
if cake[i].count('#') == 0:
nothing += 1
if i == H-1:
for j in range(W):
for d in range(nothing):
ans[i-d][j] = ans[i-nothing][j]
continue
for j in range(W):
if cake[i][j] == '#':
ans[i][sign:j+1] = [id] * (j+1-sign)
sign = j+1
id += 1
elif j == W-1:
ans[i][sign:j+1] = [id-1] * (j+1-sign)
if nothing != 0:
for j in range(W):
for d in range(1,nothing+1):
ans[i-d][j] = ans[i][j]
sign = 0
nothing = 0
for i in range(H):
for j in range(W):
print('{} '.format(ans[i][j]), end='')
print()
|
h,w,k=map(int,input().split())
L=[0]*w
f=1
past=0
for i in range(h):
s=list(input())
for j in range(w):
if '#' not in s:
L.append(L[i*w+j])
else:
if s[j]=='.':
L.append(f)
else:
if past==1:
f += 1
else:
past=1
L.append(f)
if j==w-1:
f += 1
past=0
L=L[w:(h+1)*w]
zero=L.count(0)
z=zero//w
for i in range(w):
num=L[zero+i]
for j in range(z):
L[i+j*w]=num
for i in range(h):
ans=L[w*i:w*(i+1)]
ans=[str(x) for x in ans]
print(' '.join(ans))
| 1 | 143,615,302,862,782 | null | 277 | 277 |
def abc167_e():
N, M, K = map(int, input().split())
Mod = 998244353
class CombiMod:
''' combination mod prime '''
def __init__(self, N, Mod):
''' 前計算 '''
self.mod = Mod
self.fac = [1] * (N+1) # fac[0] = fac[1] = 1
for i in range(1, N+1):
self.fac[i] = (self.fac[i-1] * i) % self.mod
self.finv = [1] * (N+1) # finv[0] = finv[1] = 1
self.finv[N] = pow(self.fac[N], self.mod-2, self.mod)
for i in range(N, 0, -1):
self.finv[i-1] = (self.finv[i] * i) % self.mod
def __call__(self, n: int, k: int):
''' nCkを実際に計算する '''
if k < 0 or n < k: return 0
ret = self.fac[n] * self.finv[k] % self.mod
ret = ret * self.finv[n-k] % self.mod
return ret
cmb = CombiMod(N, Mod)
ans = 0
for x in range(K+1):
p = cmb(N-1, x) * M * pow(M-1, N-x-1, Mod) % Mod
ans = (ans + p) % Mod
print(ans)
abc167_e()
|
# -*- coding:utf-8 -*-
def solve():
import sys
N, M = list(map(int, sys.stdin.readline().split()))
Cs = list(map(int, sys.stdin.readline().split()))
dp = [float("inf") for _ in range(N+1)] # dp[i] := i円を作ることができるコインの最小枚数
dp[0] = 0
for c in Cs:
for i in range(c, N+1):
dp[i] = min(dp[i], dp[i-c]+1)
print(dp[N])
if __name__ == "__main__":
solve()
| 0 | null | 11,671,746,745,330 | 151 | 28 |
from collections import defaultdict as dd
N, K = map(int, input().split())
A = list(map(int, input().split()))
S = [0]*(N+1)
# 累積和
for i in range(1, N + 1):
S[i] = S[i - 1] + A[i - 1] - 1
S[i] %= K
B = dd(int) # ここで範囲内のS[i]-iの個数を数えていく。
cnt = 0
for j in range(1, N + 1):
B[S[j - 1]] += 1
if j - K >= 0:
B[S[j - K]] -= 1
cnt += B[S[j]]
print(cnt)
|
def main():
s = input()
s_ = set(s)
if len(s_) >= 2:
print('Yes')
else:
print('No')
if __name__ == "__main__":
main()
| 0 | null | 96,200,978,769,632 | 273 | 201 |
n, k, c = [int(i) for i in input().split()]
s = input()
work1 = [0] * k
work2 = [0] * k
cnt = 0
day = 0
while cnt < k:
if s[day] == 'o':
work1[cnt] = day
cnt += 1
day += (c+1)
else:
day += 1
cnt = k-1
day = n-1
while cnt >= 0:
if s[day] == 'o':
work2[cnt] = day
cnt -= 1
day -= (c + 1)
else:
day -= 1
for i in range(k):
if work1[i] == work2[i]:
print(work1[i]+1)
|
def main():
N, K, C = map(int, input().split())
S = input()
i = 0
c = 0
p = [-1] * N
while c < K:
if S[i] == 'o':
p[i] = c
c += 1
i += C
i += 1
i = N - 1
c = K - 1
q = [-1] * N
while c >= 0:
if S[i] == 'o':
q[i] = c
c -= 1
i -= C
i -= 1
for i in range(N):
if ~p[i] and p[i] == q[i]:
print(i + 1)
if __name__ == '__main__':
main()
| 1 | 40,527,640,620,470 | null | 182 | 182 |
while True:
m, f, r = map(int, raw_input().split())
p = ""
if m == -1 and f == -1 and r == -1:
break
if m == -1 or f == -1:
p = "F"
else:
s = m + f
if s >= 80:
p = "A"
elif s >= 65:
p = "B"
elif s >= 50:
p = "C"
elif s >= 30:
p = "C" if r >= 50 else "D"
else:
p = "F"
print(p)
|
# coding:utf-8
while True:
m,f,r = map(int, raw_input().split())
if m == -1 and f == -1 and r == -1:
break
if m == -1 or f == -1:
print 'F'
elif m + f >= 80:
print 'A'
elif m + f >= 65:
print 'B'
elif m + f >= 50:
print 'C'
elif m + f >= 30:
if r >= 50:
print 'C'
else:
print 'D'
else:
print 'F'
| 1 | 1,245,476,647,392 | null | 57 | 57 |
master = [(mark, number) for mark in ['S', 'H', 'C', 'D'] for number in range(1, 14)]
n = int(input())
cards = set()
for x in range(n):
mark, number = input().split()
cards.add((mark, int(number)))
lack = sorted((set(master) - cards), key = master.index)
for x in lack:
print('%s %d' % x)
|
from sys import stdin
cards = {}
n = int(input())
for line in stdin:
m, num = line.rstrip().split(' ')
cards[(m, int(num))] = True
for s in ['S','H','C','D']:
for i in range(1, 14):
if not (s, i) in cards:
print("{} {}".format(s, i))
| 1 | 1,046,662,639,010 | null | 54 | 54 |
import sys
read = sys.stdin.buffer.read
def main():
N, T, *AB = map(int, read().split())
D = [(a, b) for a, b in zip(*[iter(AB)] * 2)]
D.sort()
dp = [[0] * T for _ in range(N + 1)]
for i, (a, b) in enumerate(D):
for t in range(T):
if 0 <= t - a:
dp[i + 1][t] = dp[i][t - a] + b
if dp[i + 1][t] < dp[i][t]:
dp[i + 1][t] = dp[i][t]
ans = 0
for i in range(N - 1):
if ans < dp[i + 1][T - 1] + D[i + 1][1]:
ans = dp[i + 1][T - 1] + D[i + 1][1]
print(ans)
return
if __name__ == '__main__':
main()
|
N,T = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(N)]
AB.sort(key=lambda x: x[0])
#print(AB)
W = T + 1
def dp(AB,W):
N = len(AB)
DP = [[0]*W for i in range(N+1)]
for i in range(N):
for w in range(W):
if w != W-1:
if AB[i][0] <= w:
DP[i+1][w] = max(DP[i][w-AB[i][0]]+AB[i][1],DP[i][w])
else:
DP[i+1][w] = DP[i][w]
else:
DP[i+1][w] = max(DP[i][w-1]+AB[i][1],DP[i][w])
return DP
DP1 = dp(AB,W)
ans = DP1[N][T]
print(ans)
| 1 | 151,556,516,038,268 | null | 282 | 282 |
import collections
N=int(input())
A=[x for x in input().split()]
c = collections.Counter(A)
for i in range(N):
print(c["{}".format(str(i+1))])
|
n = int(input())
l = list(map(int, input().split()))
array = [0] * n
for i in l:
array[i-1] += 1
for i in array:
print(i)
| 1 | 32,477,234,815,812 | null | 169 | 169 |
u, s, e, w, n, d = input().split()
t = input()
for i in t:
if i == 'N':
u, s, n, d = s, d, u, n
elif i == 'E':
u, e, w, d = w, u, d, e
elif i == 'S':
u, s, n, d = n, u, d, s
elif i == 'W':
u, e, w, d = e, d, u, w
print(u)
|
d=list(map(int,input().split()))
c=list(input())
class dice(object):
def __init__(self, d):
self.d = d
def roll(self,com):
a1,a2,a3,a4,a5,a6=self.d
if com=="E":
self.d = [a4,a2,a1,a6,a5,a3]
elif com=="W":
self.d = [a3,a2,a6,a1,a5,a4]
elif com=="S":
self.d = [a5,a1,a3,a4,a6,a2]
elif com=="N":
self.d = [a2,a6,a3,a4,a1,a5]
dice1=dice(d)
for com in c:
dice1.roll(com)
print(dice1.d[0])
| 1 | 235,874,838,032 | null | 33 | 33 |
ondo = int(input())
if ondo>=30:
print('Yes')
else:
print('No')
|
now = int(input())
if now <30:
print("No")
else:
print("Yes")
| 1 | 5,736,577,410,048 | null | 95 | 95 |
a, b, m = map(int, input().split())
list_a = list(map(int, input().split()))
list_b = list(map(int, input().split()))
total = min(list_a) + min(list_b)
for i in range(m):
x, y, c = map(int, input().split())
price = list_a[x - 1] + list_b[y - 1] - c
total = min(price, total)
print(total)
|
A, B, M = map(int, input().split())
a = [int(s) for s in input().split()]
b = [int(s) for s in input().split()]
m = []
for n in range(M):
m.append([int(s) for s in input().split()])
minValue = min(a) + min(b)
for i in range(M):
discountPrice = a[m[i][0] - 1] + b[m[i][1] - 1] - m[i][2]
if discountPrice < minValue:
minValue = discountPrice
print(minValue)
| 1 | 54,238,100,312,480 | null | 200 | 200 |
import bisect
from itertools import combinations
def main():
N = int(input())
L = sorted(list(map(int, input().split())))
ans = 0
for l in combinations(L, 2):
a = l[0]
b = l[1]
x = bisect.bisect_left(L, a + b)
y = bisect.bisect(L, b - a)
if b - a < a :
ans += x - y - 2
else:
ans += x - y - 1
print(ans // 3)
if __name__ == "__main__":
main()
|
import sys
import math
import copy
import heapq
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)
# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]
INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)
def nck(n, k, kaijyo):
return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD
def npk(n, k, kaijyo):
if k == 0 or k == n:
return n % MOD
return (kaijyo[n] * divide(kaijyo[n-k])) % MOD
def fact_and_inv(SIZE):
inv = [0] * SIZE # inv[j] = j^{-1} mod MOD
fac = [0] * SIZE # fac[j] = j! mod MOD
finv = [0] * SIZE # finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2, SIZE):
inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
fac[i] = fac[i - 1] * i % MOD
finv[i] = finv[i - 1] * inv[i] % MOD
return fac, finv
def renritsu(A, Y):
# example 2x + y = 3, x + 3y = 4
# A = [[2,1], [1,3]])
# Y = [[3],[4]] または [3,4]
A = np.matrix(A)
Y = np.matrix(Y)
Y = np.reshape(Y, (-1, 1))
X = np.linalg.solve(A, Y)
# [1.0, 1.0]
return X.flatten().tolist()[0]
class TwoDimGrid:
# 2次元座標 -> 1次元
def __init__(self, h, w, wall="o"):
self.h = h
self.w = w
self.size = (h+2) * (w+2)
self.wall = wall
self.get_grid()
self.init_cost()
def get_grid(self):
grid = [self.wall * (self.w + 2)]
for i in range(self.h):
grid.append(self.wall + getS() + self.wall)
grid.append(self.wall * (self.w + 2))
self.grid = grid
def init_cost(self):
self.cost = [INF] * self.size
def pos(self, x, y):
# 壁も含めて0-indexed 元々の座標だけ考えると1-indexed
return y * (self.w + 2) + x
def getgrid(self, x, y):
return self.grid[y][x]
def get(self, x, y):
return self.cost[self.pos(x, y)]
def set(self, x, y, v):
self.cost[self.pos(x, y)] = v
return
def show(self):
for i in range(self.h+2):
print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
def showsome(self, tgt):
for t in tgt:
print(t)
return
def showsomejoin(self, tgt):
for t in tgt:
print("".join(t))
return
def search(self):
grid = self.grid
move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
d = deque()
d.append((1,1))
self.set(1,1,0)
while(d):
cx, cy = d.popleft()
cc = self.get(cx, cy)
if self.getgrid(cx, cy) == self.wall:
continue
for dx, dy in [(1, 0), (0, 1)]:
nx, ny = cx + dx, cy + dy
if self.getgrid(cx, cy) == self.getgrid(nx, ny):
if self.get(nx, ny) > cc:
d.append((nx, ny))
self.set(nx, ny, cc)
else:
if self.get(nx, ny) > cc + 1:
d.append((nx, ny))
self.set(nx, ny, cc + 1)
# self.show()
ans = (self.get(self.w, self.h))
if self.getgrid(1,1) == "#":
ans += 1
print((ans + 1) // 2)
def soinsu(n):
ret = defaultdict(int)
for i in range(2, int(math.sqrt(n) + 2)):
if n % i == 0:
while True:
if n % i == 0:
ret[i] += 1
n //= i
else:
break
if not ret:
return {n: 1}
return ret
def solve():
h, w = getList()
G = TwoDimGrid(h, w)
G.search()
def main():
n = getN()
for _ in range(n):
solve()
return
if __name__ == "__main__":
# main()
solve()
| 0 | null | 110,594,630,800,418 | 294 | 194 |
i = int(input())
h = i // 3600
i -= h * 3600
m = i // 60
i -= m * 60
s = i
print("{}:{}:{}".format(h, m, s))
|
def show2dlist(nlist,r,c):
for x in xrange(0,r):
nlist[x] = map(str,nlist[x])
ans = " ".join(nlist[x])
print ans
# input
n,m,l = map(int,raw_input().split())
a_matrix = [[0 for x in xrange(m)] for y in xrange(n)]
b_matrix = [[0 for x in xrange(l)] for y in xrange(m)]
for x in xrange(n):
a_matrix[x] = map(int,raw_input().split())
for x in xrange(m):
b_matrix[x] = map(int,raw_input().split())
c_matrix = [[0 for x in xrange(l)] for y in xrange(n)]
# calc
for ni in xrange(n):
for li in xrange(l):
for mi in xrange(m):
c_matrix[ni][li] += a_matrix[ni][mi] * b_matrix[mi][li]
# show result
show2dlist(c_matrix,n,l)
| 0 | null | 870,335,349,280 | 37 | 60 |
N=int(input())
c=0;
while N-26**c>=0:
N-=26**c
c+=1
d=[0]*(c-1)
i=0
for i in range(c):
d.insert(c-1,N%26)
N=(N-d[i])//26
i+=1
e=[]
s=''
for i in range(2*c-1):
e.append(chr(97+d[i]))
s+=e[i]
print(s[c-1:2*c-1])
|
x = input()
print(x*x*x)
| 0 | null | 6,142,683,360,410 | 121 | 35 |
import sys
readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)
def main():
A = int(readline())
B = int(readline())
print(6 - A - B)
if __name__ == '__main__':
main()
|
s=input()
n=len(s)
ans='x'*n
print(ans)
| 0 | null | 92,104,853,763,678 | 254 | 221 |
N,K = list(map(int,input().split()))
snacker = set()
for i in range(K):
dummy=input()
for j in [int(k) for k in input().split()]:
snacker.add(j)
print(N-len(snacker))
|
a,b = map(int,input().split())
w = []
t = {}
for i in range(b):
c = int(input())
s = list(map(int,input().split()))
w +=s
t = set(w)
print(a-len(t))
| 1 | 24,605,270,477,730 | null | 154 | 154 |
from sys import stdin
input = stdin.readline
a,b,n = map(int,input().split())
maxpoint = 0
if b > n :
tmp = int((a * n) / b) - a * int(n/b)
maxpoint = max(tmp,maxpoint)
else:
k = int(n/b)*b -1
maxpoint = int((a * k) / b) - a * int(k/b)
if b == 1:
maxpoint = 0
print(maxpoint)
|
import math
A, B, N = list(map(int,input().split()))
def f(x):
return math.floor(A * x / B) - A * math.floor(x / B)
if B - 1 <= N:
ans = f(B - 1)
else:
ans = f(N)
print(ans)
| 1 | 27,982,972,629,332 | null | 161 | 161 |
import sys
readline = sys.stdin.readline
li = []
for i, s in enumerate(readline().strip()):
if s == "\\":
li.append([i, 0])
elif s == "/":
if li:
if li[-1][1] == 0:
li[-1][1] = i - li[-1][0]
else:
for j in range(len(li) - 1, -1, -1):
if li[j][1] == 0:
li = li[:j] + [[li[j][0], sum(tuple(zip(*li[j + 1:]))[1]) + i - li[j][0]]]
break
ans = []
for a in li:
if a[1] != 0:
ans.append(a[1])
print(sum(ans))
print(len(ans), *ans)
|
def answer(s: str) -> str:
return 'x' * len(s)
def main():
s = input()
print(answer(s))
if __name__ == '__main__':
main()
| 0 | null | 36,282,445,329,042 | 21 | 221 |
S , T = input().split()
A , B =map(int,input().split())
U = input()
mydict = {S:A, T:B}
mydict[U] -= 1
print ( str( mydict[S] ) + " " +str( mydict[T] ) )
|
h,w,k=map(int,input().split())
G=[["."]*w for i in range(h)]
for i in range(h):
G[i]=list(input())
ans=[[0]*w for i in range(h)]
GG=[[0] for i in range(h)]
for i in range(h):
for j in range(w):
if G[i][j]=="#":
GG[i].append(j)
GG[i].append(w)
B=[0]
a=1
for i in range(h):
if len(GG[i])==2:
continue
B.append(i)
ans[i][GG[i][0]:GG[i][2]]=[a]*(GG[i][2]-GG[i][0])
a=a+1
for j in range(len(GG[i])-3):
ans[i][GG[i][j+2]:GG[i][j+3]]=[a]*(GG[i][j+3]-GG[i][j+2])
a=a+1
B.append(h)
for i in range(B[2]-B[0]):
ans[i]=ans[B[1]]
for i in range(B[2],h):
if i not in B:
ans[i]=ans[i-1]
for i in range(h):
for j in range(w):
print(ans[i][j],end=" ")
print()
| 0 | null | 107,459,700,403,484 | 220 | 277 |
n,m=map(int,input().split())
g=[[] for i in range(n)]
for _ in range(m):
a, b = [int(x) for x in input().split()]
g[a-1].append(b)
g[b-1].append(a)
from collections import deque
x=[0]*n
s=1
for i in range(1,n+1):
if x[i-1]==0:
x[i-1]=s
q=deque([i])
while q:
v = q.popleft()
for j in g[v-1]:
if x[j-1]==0:
q.append(j)
x[j-1]=s
s+=1
print(s-2)
|
import queue
h,w=map(int,input().split())
s=[]
for _ in range(h):
s.append(input())
ans=0
for i in range(h):
for j in range(w):
if s[i][j]==".":
seen=[[0 for _ in range(w)] for _ in range(h)]
length=[[0 for _ in range(w)] for _ in range(h)]
q=queue.Queue()
q.put([i,j])
seen[i][j]=1
while not q.empty():
ci,cj=q.get()
for ni,nj in [[ci-1,cj],[ci+1,cj],[ci,cj-1],[ci,cj+1]]:
if 0<=ni<h and 0<=nj<w and s[ni][nj]=="." and seen[ni][nj]==0:
q.put([ni,nj])
length[ni][nj]=length[ci][cj]+1
seen[ni][nj]=1
ans=max(ans,length[ci][cj])
print(ans)
| 0 | null | 48,367,861,662,340 | 70 | 241 |
import math
def prime(x):
if x == 2:
return True
elif x%2 == 0:
return False
else:
i = 3
while i <= math.sqrt(x):
if x%i == 0:
return False
else:
i += 2
return True
p = 0
n = int(input())
for i in range(n):
if prime(int(input())) == True:
p += 1
print(p)
|
import math
H = int(input())
l = math.log(H,2)
ll = math.floor(l)+1
ans = 2 ** ll -1
print(ans)
| 0 | null | 40,077,322,173,192 | 12 | 228 |
import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
from collections import Counter
def resolve():
n = int(input())
sieve = list(range(n + 1))
primes = []
for i in range(2, n + 1):
if sieve[i] == i:
primes.append(i)
for p in primes:
if p * i > n or sieve[i] < p:
break
sieve[p * i] = p
ans = 0
for i in range(1, n):
C = Counter()
while i > 1:
C[sieve[i]] += 1
i //= sieve[i]
score = 1
for val in C.values():
score *= val + 1
ans += score
print(ans)
resolve()
|
import sys
N = int(input())
result = 0
for a in range(1, N):
if N % a == 0:
b_count = N // a - 1
else:
b_count = N // a
result += b_count
print(result)
| 1 | 2,587,924,214,788 | null | 73 | 73 |
n=int(input())
a=[]
b=[0]*n
t_sum=0
for i in range(n):
s,t=input().split()
tt=int(t)
t_sum+=tt
a.append(s)
b[i]=t_sum
x=input()
print(t_sum-b[a.index(x)])
|
n = int(input())
playList = []
sumTerm = 0
for i in range(n) :
song = input().split()
song[1] = int(song[1])
playList.append(song)
sumTerm += song[1]
x = input()
tmpSumTerm = 0
for i in range(n) :
tmpSumTerm += playList[i][1]
if playList[i][0] == x :
print(sumTerm - tmpSumTerm)
break
| 1 | 97,050,991,566,308 | null | 243 | 243 |
class Combination:
"""階乗とその逆元のテーブルをO(N)で事前作成し、組み合わせの計算をO(1)で行う"""
def __init__(self, n, mod):
self.fact = [1]
for i in range(1, n + 1):
self.fact.append(self.fact[-1] * i % mod)
self.inv_fact = [0] * (n + 1)
self.inv_fact[n] = pow(self.fact[n], mod - 2, mod)
for i in reversed(range(n)):
self.inv_fact[i] = self.inv_fact[i + 1] * (i + 1) % mod
self.mod = mod
def factorial(self, k):
"""k!を求める O(1)"""
return self.fact[k]
def inverse_factorial(self, k):
"""k!の逆元を求める O(1)"""
return self.inv_fact[k]
def permutation(self, k, r):
"""kPrを求める O(1)"""
if k < r:
return 0
return (self.fact[k] * self.inv_fact[k - r]) % self.mod
def combination(self, k, r):
"""kCrを求める O(1)"""
if k < r:
return 0
return (self.fact[k] * self.inv_fact[k - r] * self.inv_fact[r]) % self.mod
def combination_large(self, k, r):
"""kCrを求める O(r) kが大きいが、r <= nを満たしているときに使用"""
if k < r:
return 0
res = 1
for l in range(r):
res *= (k - l)
res %= self.mod
return (res * self.inv_fact[r]) % self.mod
x, y = map(int, input().split())
mod = 10**9 + 7
comb = Combination(10**6,mod)
if (x+y)%3 != 0:
print(0)
exit(0)
i = (2*x-y)//3
j = (2*y-x)//3
n = i+j
r = i
if not 0 <= r <= n:
print(0)
exit(0)
print(comb.combination(n, r))
|
class Factorial():
def __init__(self, mod=10**9 + 7):
self.mod = mod
self._factorial = [1]
self._size = 1
self._factorial_inv = [1]
self._size_inv = 1
def __call__(self, n):
return self.fact(n)
def fact(self, n):
''' n! % mod '''
if n >= self.mod:
return 0
self._make(n)
return self._factorial[n]
def _make(self, n):
if n >= self.mod:
n = self.mod
if self._size < n+1:
for i in range(self._size, n+1):
self._factorial.append(self._factorial[i-1]*i % self.mod)
self._size = n+1
def fact_inv(self, n):
''' n!^-1 % mod '''
if n >= self.mod:
raise ValueError('Modinv is not exist! arg={}'.format(n))
self._make(n)
if self._size_inv < n+1:
self._factorial_inv += [-1] * (n+1-self._size_inv)
self._size_inv = n+1
if self._factorial_inv[n] == -1:
self._factorial_inv[n] = self.modinv(self._factorial[n])
return self._factorial_inv[n]
@staticmethod
def xgcd(a, b):
'''
Return (gcd(a, b), x, y) such that a*x + b*y = gcd(a, b)
'''
x0, x1, y0, y1 = 0, 1, 1, 0
while a != 0:
(q, a), b = divmod(b, a), a
y0, y1 = y1, y0 - q * y1
x0, x1 = x1, x0 - q * x1
return b, x0, y0
def modinv(self, n):
g, x, _ = self.xgcd(n, self.mod)
if g != 1:
raise ValueError('Modinv is not exist! arg={}'.format(n))
return x % self.mod
def comb(self, n, r):
''' nCr % mod '''
if r > n:
return 0
t = self(n)*self.fact_inv(n-r) % self.mod
return t*self.fact_inv(r) % self.mod
def comb_with_repetition(self, n, r):
''' nHr % mod '''
t = self(n+r-1)*self.fact_inv(n-1) % self.mod
return t*self.fact_inv(r) % self.mod
def perm(self, n, r):
''' nPr % mod '''
if r > n:
return 0
return self(n)*self.fact_inv(n-r) % self.mod
x, y = sorted(map(int, input().split()))
q, r = divmod(x+y, 3)
if r != 0:
print(0)
else:
comb = Factorial().comb
print(comb(q, y-q))
| 1 | 149,971,923,604,090 | null | 281 | 281 |
while True:
m,f,r=map(int,input().split())
if m==f==r==-1: break
ret = 'F'
score = m + f
if -1 in (m,f): pass
elif score >= 80: ret = 'A'
elif score >= 65: ret = 'B'
elif score >= 50: ret = 'C'
elif score >= 30:
ret = 'D'
if r >= 50:ret = 'C'
print(ret)
|
from sys import stdin
input = stdin.readline
def main():
N, K = map(int, input().rstrip().split())
A = list(map(int, input().rstrip().split()))
F = list(map(int, input().rstrip().split()))
A.sort()
F.sort(reverse=True)
l = -1
r = 10 ** 12
while(r - l > 1):
c = (l + r) // 2
s = 0
for i in range(N):
d = A[i] - (c // F[i])
s += max(0, d)
if s <= K:
r = c
else:
l = c
print(r)
if __name__ == "__main__":
main()
| 0 | null | 82,644,042,738,310 | 57 | 290 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.