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 itertools
import math
n = int(input())
x = []
num = []
for i in range(n):
x.append([int(t) for t in input().split()])
num.append(i)
s = 0
for i in itertools.permutations(num):
i = list(i)
#print(x,i)
for j in range(n-1):
s += math.sqrt((x[i[j]][0] - x[i[j+1]][0])**2 + (x[i[j]][1] - x[i[j+1]][1])**2)
#print(s)
s /= math.factorial(n)
print(s)
|
# 町a,b(a < b)の距離をあらかじめdictionaryで保存しておく
# そのうえで、全探索可能
import sys
readline = sys.stdin.readline
N = int(readline())
town = [None] * N
for i in range(N):
town[i] = tuple(map(int,readline().split()))
from collections import defaultdict
dist = defaultdict(float)
for i in range(N - 1):
for j in range(i + 1, N):
ix,iy = town[i]
jx,jy = town[j]
dist[(i,j)] = ((ix - jx) ** 2 + (iy - jy) ** 2) ** 0.5
# 距離を求めたので、あとは全パターン検索
ans = 0
import itertools
for perm in itertools.permutations(range(N)):
d = 0
for i in range(1, len(perm)):
a,b = perm[i - 1],perm[i]
if a > b:
a,b = b,a
d += dist[(a, b)]
ans += d
pat = 1
for i in range(2, N + 1):
pat *= i
print(ans / pat)
| 1 | 148,689,931,270,634 | null | 280 | 280 |
n = int(input())
s = input()
prev = ""
ans = ""
for i in range(n):
if s[i] == prev:
continue
prev = s[i]
ans += s[i]
print(len(ans))
|
N = int(input())
S = input()
res = 0
f = S[0]
for i in range(N):
if S[i] != f:
f = S[i]
res += 1
print(res+1)
| 1 | 169,787,955,033,600 | null | 293 | 293 |
from collections import deque
n=int(input())
arr=[[] for _ in range(n)]
for i in range(n-1):
a,b=map(int,input().split())
arr[a-1].append([b-1,i])
arr[b-1].append([a-1,i])
que=deque([0])
ans=[0]*(n-1)
par=[0]*n
par[0]=-1
while que:
x=que.popleft()
p=par[x]
color=1
for tup in arr[x]:
if p==color:
color+=1
if ans[tup[1]]==0:
ans[tup[1]]=color
par[tup[0]]=color
color+=1
que.append(tup[0])
print(max(ans))
print(*ans,sep='\n')
|
import math
a, b, deg = map(float, raw_input().split(" "))
c = math.sqrt(a**2 + b**2 - 2*a*b*math.cos(math.radians(deg)))
L = a + b + c
s = (a + b + c) / 2.0
S = math.sqrt(s*(s-a)*(s-b)*(s-c))
h = 2 * S / a
print(str(S) + " " + str(L) + " " + str(h))
| 0 | null | 67,736,145,284,960 | 272 | 30 |
X = int(input())
dp = [0] * 110000
dp[100] = 1
dp[101] = 1
dp[102] = 1
dp[103] = 1
dp[104] = 1
dp[105] = 1
for i in range(X + 1):
dp[i + 100] = max(dp[i], dp[i + 100])
dp[i + 101] = max(dp[i], dp[i + 101])
dp[i + 102] = max(dp[i], dp[i + 102])
dp[i + 103] = max(dp[i], dp[i + 103])
dp[i + 104] = max(dp[i], dp[i + 104])
dp[i + 105] = max(dp[i], dp[i + 105])
print(dp[X])
|
import sys
N, K = map(int, input().split())
if K == 10:
print(len(str(N)))
sys.exit()
p = 0
while True:
num = K ** p
if N < num:
break
p += 1
print(p)
| 0 | null | 95,681,521,187,260 | 266 | 212 |
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
from itertools import accumulate
INF = float("inf")
def bit(n, k):
return (n >> k) & 1
def bit_sum(n):
wa = 0
while n > 0:
wa += n & 1
n >>= 1
return wa
def border(x):
# xを二進数で与えた時、ビットの立っている位置のリストを1-indexで返す
c = 0
ans = []
while x > 0:
c += 1
if x & 1:
ans.append(c)
x >>= 1
return ans
def main():
H, W, K = map(int, input().split())
S = [[0]*W for _ in range(H)]
for h in range(H):
S[h][:] = [int(c) for c in input()]
acc = [[0]+list(accumulate(S[h])) for h in range(H)]
min_kaisu = +INF
for h in range(1 << (H-1)):
# hは分割方法を与える
# hで与えられる分割方法は、どのような分割なのかを得る。
startend = border(h)
flag = False
division = [0]
# x番目でW方向に分割すると仮定する
for x in range(1, W+1):
# xの位置で分割したとき、左の領域のホワイトチョコレートの数
# まずは行ごとに求める
white = [acc[i][x] - acc[i][division[-1]] for i in range(H)]
# 領域ごとに加算するし、最大値を撮る
white_sum = [sum(white[s:e])
for s, e in zip([0]+startend, startend+[H])]
# print(bin(h), division, x, white_sum)
white_max = max(white_sum)
# Kよりも大きくなるなら、事前に分割するべきであった。
if white_max > K:
division.append(x-1)
# 分割してもなおK以下を達成できないケースを除外する
white = [acc[i][x] - acc[i][division[-1]] for i in range(H)]
white_sum = [sum(white[s:e])
for s, e in zip([0]+startend, startend+[H])]
white_max = max(white_sum)
if white_max > K:
flag = True
break
if flag:
continue
# hによる分割も考慮する
division_tot = len(division)-1 + bit_sum(h)
min_kaisu = min(division_tot, min_kaisu)
print(min_kaisu)
return
if __name__ == '__main__':
main()
|
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce, lru_cache # @lru_cache(None)
from fractions import gcd
import sys
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**6)
# ----------------------------------------------------------- #
s = input()
print(s[:3])
| 0 | null | 31,587,560,924,778 | 193 | 130 |
#!/usr/bin/env python3
numbers = input().split(" ")
n = int(numbers[0])
m = int(numbers[1])
numbers = input().split(" ")
h = [int(x) for x in numbers]
h.insert(0, 0)
is_good = [1] * (n + 1)
for i in range(m):
numbers = input().split(" ")
a = int(numbers[0])
b = int(numbers[1])
if h[a] <= h[b]:
is_good[a] = 0
if h[a] >= h[b]:
is_good[b] = 0
print(is_good.count(1) - 1)
|
def f(w, n, k, v):
# how many things in list w of length n can fill in k cars with capacity v?
i = 0
space = 0
while i < n:
if space >= w[i]:
space -= w[i]
i += 1
else:
# space < w[i]
if k == 0:
break
k -= 1
space = v
return i
if __name__ == '__main__':
n, k = map(int, raw_input().split())
w = []
maxv = -1
for i in range(n):
x = int(raw_input())
w.append(x)
maxv = max(maxv, x)
left = 0
right = n * maxv
# range: (left, right]
# loop until left + 1 = right, then right is the answer
# why? because right works, but right - 1 doesn't work
# so right is the smallest capacity
while right - left > 1:
mid = (left + right) / 2
v = f(w, n, k, mid)
if v >= n:
# range: (left, mid]
# in fact, v cannot > n
right = mid
else:
# range: (mid, right]
left = mid
print right
| 0 | null | 12,609,675,586,940 | 155 | 24 |
n = int(input())
(*x,) = map(int, input().split())
a = round(sum(x) / n)
print(sum(((i - a) ** 2) for i in x))
|
import statistics
N = int(input())
X = list(map(int,input().split()))
P = round( statistics.mean(X) )
ans = sum([(i-P)**2 for i in X])
print(ans)
| 1 | 65,370,321,246,098 | null | 213 | 213 |
N = int(input())
#U:0~9の整数でN桁の数列 = 10**N
#A:0を含まないN桁の数列 = 9**N
#B:9を含まないN桁の数列 = 9**N
#ans = |U-(A∪B)| = |U|-|A|-|B|+|A∩B|
MOD = 10**9 + 7
ans = pow(10, N, MOD) - pow(9, N, MOD) - pow(9, N, MOD) + pow(8, N, MOD)
ans %= MOD
print(ans)
|
n=int(input())
m=10**9+7
print((pow(10,n,m)-pow(9,n,m)*2+pow(8,n,m))%m if n!=1 else 0)
| 1 | 3,145,286,652,412 | null | 78 | 78 |
from collections import defaultdict
d=defaultdict(list)
n ,m =list(map(int,input().split()))
h=list(map(int,input().split()))
for i in range(m):
a,b=list(map(int,input().split()))
d[a].append(b)
d[b].append(a)
s=0
for c in d:
k=0
for v in d[c]:
if h[c-1]<=h[v-1]:
k=1
break
#print(c,k,d)
if k==0:
s+=1
s+=n-len(d)
print(s)
|
N = int(input())
ACcount = 0
WAcount = 0
TLEcount = 0
REcount = 0
for i in range(N):
S=input()
if S == "AC":
ACcount = ACcount + 1
elif S == "WA":
WAcount = WAcount + 1
elif S == "TLE":
TLEcount = TLEcount + 1
elif S == "RE":
REcount = REcount + 1
print("AC x", ACcount)
print("WA x", WAcount)
print("TLE x", TLEcount)
print("RE x", REcount)
| 0 | null | 16,954,748,382,630 | 155 | 109 |
n, a, b = map(int, input().split())
div, mod = divmod(n, a + b)
ans = div * a + min(mod, a)
print(ans)
|
import sys
input = lambda: sys.stdin.readline().rstrip()
def solve():
N, K = map(int, input().split())
h = list(map(int, input().split()))
ans = 0
for i in range(N):
if h[i] >= K:
ans += 1
print(ans)
if __name__ == '__main__':
solve()
| 0 | null | 117,718,437,879,714 | 202 | 298 |
def main():
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
#from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
import math
#inf = 10**17
#mod = 10**9 + 7
n,t = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
ab.sort(key=lambda x: x[1])
ab.sort(key=lambda x: x[0])
dp = [-1]*(t+1)
dp[0] = 0
for a, b in ab:
for j in range(t-1, -1, -1):
if dp[j] >= 0:
if j+a<=t:
dp[j+a] = max(dp[j+a], dp[j]+b)
else:
dp[-1] = max(dp[-1], dp[j]+b)
print(max(dp))
if __name__ == '__main__':
main()
|
def nanbanme(n, x):
lst = [1 for i in range(n)]
ans = 0
for i in range(n):
a = x[i]
count = 0
for j in range(a - 1):
if (lst[j] == 1):
count = count + 1
tmp = 1
for k in range(1, n - i):
tmp = tmp*k
ans = ans + count*tmp
lst[a - 1] = 0
return (ans + 1)
n = int(input())
lst1 = list(map(int,input().split()))
lst2 = list(map(int,input().split()))
a = nanbanme(n, lst1)
b = nanbanme(n, lst2)
print(abs(a - b))
| 0 | null | 125,700,402,668,232 | 282 | 246 |
N, K = map(int, input().split())
A = list(map(int, input().split()))
imos = [0]*(N+1)
# 全体でO(NlogN) ≒ 10^6
for k in range(K): # 下のifによってO(logN)で抑えられる.
if min(A) == N:
break
imos = [0]*(N+1)
for i,a in enumerate(A): # O(N)
imos[max(0, i-a)] += 1
imos[min(N, i + a + 1)] -= 1
A = [imos[0]]
for i in range(1,len(imos)-1): # O(N)
A.append(A[i - 1] + imos[i])
print(*A, sep=" ")
|
def combination_mod(n, k, mod=10 ** 9 + 7):
if k > n:
return 1
nu, de = 1, 1
for i in range(k):
nu = nu * (n - i) % mod
de = de * (i + 1) % mod
return nu * pow(de, mod - 2, mod) % mod
n, a, b = map(int, input().split())
mod = 10 ** 9 + 7
ans = pow(2, n, mod) - 1
ans -= combination_mod(n, a)
ans = (ans + mod) % mod
ans -= combination_mod(n, b)
ans = (ans + mod) % mod
print(ans)
| 0 | null | 40,852,826,396,590 | 132 | 214 |
MAX = 600000
MOD = 10 ** 9 + 7
fac = [0] * MAX
ifac = [0] * MAX
fac[0] = 1
for i in range(1,MAX):
fac[i] = (fac[i-1] * i) % MOD
ifac[MAX-1] = pow(fac[MAX-1],MOD-2,MOD)
for i in reversed(range(1,MAX)):
ifac[i-1] = (ifac[i] * i) % MOD
def combinations(n, k):
if k < 0 or n < k:
return 0
else:
return (fac[n] * ifac[k] * ifac[n-k]) % MOD
N,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
com = [0]
for i in range(N):
com.append(A[i] + com[-1])
#print(com)
ans = 0
for i in range(K-2,N-1):
ans += combinations(i,K-2) * (-(com[N-i-1]) + (com[N]-com[i+1]))
#print(N-i-1,N,i+1)
ans %= MOD
#print(combinations(i,K-2) * (-(com[N-i-1]) + (com[N]-com[i+1])))
print(ans)
|
N=int(input())
xy = [tuple(map(int,input().split())) for _ in range(N)]
ans = 0
def dist(tpl1, tpl2):
return abs(tpl1[0] - tpl2[0]) + abs(tpl1[1] - tpl2[1])
f0 = []
f1 = []
for i in range(N):
x, y = xy[i]
f0.append(x-y)
f1.append(x+y)
ans = max(ans,
max(f0) - min(f0),
max(f1) - min(f1))
print(ans)
| 0 | null | 49,749,365,525,440 | 242 | 80 |
from collections import deque
n, d, a = map(int, input().split())
mons = []
for i in range(n):
x, h = map(int, input().split())
mons.append((x, h))
mons = sorted(mons)
q = deque()
dm_sum = 0
ans = 0
for i in range(n):
while dm_sum > 0:
if q[0][0] < mons[i][0]:
cur = q.popleft()
dm_sum -= cur[1]
else:
break
if mons[i][1] <= dm_sum:
continue
rem = mons[i][1] - dm_sum
at_num = rem // a
if rem % a != 0:
at_num += 1
ans += at_num
q.append((mons[i][0] + 2 * d, at_num*a))
dm_sum += at_num*a
print(ans)
|
N = int(input())
A,B=map(int,input().split())
Na = N
while N <= 1000:
if A <= N and N <= B:
print("OK")
break
N = N+Na
if N>1000:
print("NG")
| 0 | null | 54,330,960,938,592 | 230 | 158 |
S, W = map(int,input().split())
ans = 'unsafe' if S <= W else 'safe'
print(ans)
|
import math
def main():
A, B, C = map(int, input().split())
if A == B and B != C:
print('Yes')
elif A == C and A != B:
print('Yes')
elif B == C and B != A:
print('Yes')
else:
print('No')
main()
| 0 | null | 48,533,866,307,990 | 163 | 216 |
import sys
input = sys.stdin.readline
class SegmentTree():
def __init__(self, S, n):
self.size = n
self.array = [set()] * (2**(self.size + 1) - 1)
for i, c in enumerate(S):
self.array[i + 2**self.size - 1] = {c}
for i in range(2**self.size - 1)[::-1]:
self.array[i] = self.array[2 * i + 1] | self.array[2 * i + 2]
def subquery(self, l, r, k, i, j):
if j <= l or r <= i:
return set()
elif l <= i and j <= r:
return self.array[k]
else:
l_set = self.subquery(l, r, 2 * k + 1, i, (i + j) // 2)
r_set = self.subquery(l, r, 2 * k + 2, (i + j) // 2, j)
return l_set | r_set
def query(self, l, r):
return len(self.subquery(l, r, 0, 0, 2**self.size))
def update(self, i, c):
tmp = i + 2**self.size - 1
self.array[tmp] = {c}
while tmp > 0:
tmp = (tmp - 1) // 2
self.array[tmp] = self.array[2 * tmp + 1] | self.array[2 * tmp + 2]
n = int(input())
S = [ord(c) for c in list(input())]
segtree = SegmentTree(S, 19)
for _ in range(int(input())):
t, i, c = input().split()
if t == '1':
segtree.update(int(i)-1, ord(c))
if t == '2':
print(segtree.query(int(i)-1, int(c)))
|
n=int(input())
s=input()
q=int(input())
query=[]
for i in range(q):
query.append(input())
ide_ele = set()
def segfunc(x, y):
return x | y
class SegTree:
def __init__(self, init_val, segfunc, ide_ele):
n = len(init_val)
self.segfunc = segfunc
self.ide_ele = ide_ele
self.num = 1 << (n - 1).bit_length()
self.tree = [ide_ele] * 2 * self.num
for i in range(n):
self.tree[self.num + i] = init_val[i]
for i in range(self.num - 1, 0, -1):
self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])
def update(self, k, x):
# param k: index(0-index)
# param x: update value
k += self.num
self.tree[k] = x
while k > 1:
self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
k >>= 1
def query(self, l, r):
#[l, r)segfunc, 0-index
res = self.ide_ele
l += self.num
r += self.num
while l < r:
if l & 1:
res = self.segfunc(res, self.tree[l])
l += 1
if r & 1:
res = self.segfunc(res, self.tree[r - 1])
l >>= 1
r >>= 1
return res
al = [{s[i]} for i in range(n)]
seg = SegTree(al, segfunc, ide_ele)
for q in query:
que,a,b=q.split()
if que == "1":
seg.update(int(a)-1,set(b))
else:
ret=seg.query(int(a)-1,int(b))
print(len(ret))
| 1 | 62,482,024,325,860 | null | 210 | 210 |
# 2020/08/16
# AtCoder Beginner Contest 030 - A
# Input
h = int(input())
w = int(input())
n = int(input())
# Calc
ans = n // max(h, w)
if n % max(h, w) > 0:
ans = ans + 1
# Output
print(ans)
|
n=int(input())
if n%2==1:
print(str(((n-1)/2+1)/n)+"\n")
else:
print(str(1/2)+"\n")
| 0 | null | 133,410,952,627,988 | 236 | 297 |
n,k = map(int,input().split())
m = n%k
a = abs(k - m)
l = [m,a]
print(min(l))
|
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())
| 1 | 39,217,784,484,380 | null | 180 | 180 |
def main():
N = int(input())
S = input()
temp = S[0]
result = 1
for i in range(1,N):
if temp != S[i]:
result += 1
temp = S[i]
print(result)
if __name__ == '__main__':
main()
|
from sys import stdin, setrecursionlimit
#input = stdin.buffer.readline
setrecursionlimit(10 ** 7)
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict, Counter
from itertools import combinations, permutations, combinations_with_replacement
from itertools import accumulate
from math import ceil, sqrt, pi, radians, sin, cos
MOD = 10 ** 9 + 7
INF = 10 ** 18
a, b, c, d = map(int, input().split())
#A = list(map(int, input().split()))
#A = [int(input()) for _ in range(N)]
#ABC = [list(map(int, input().split())) for _ in range(N)]
#S = input()
#S = [input() for _ in range(N)]
print(max(a * c, a * d, b * c, b * d))
| 0 | null | 86,638,536,873,980 | 293 | 77 |
n = int(input())
A = list(map(int, input().split()))
ans = 0
cumsum = 0
for i in range(n-1):
cumsum += A[n-i-1]
ans += (A[n-i-2] * cumsum)
ans %= 1000000007
print(ans)
|
def make_divisors(n):
cnt = 0
i = 1
while i*i <= n:
if n % i == 0:
cnt += 1
if i != n // i:
cnt += 1
i += 1
return cnt
numbers = [1,10, 100, 1000, 10000, 100000, 150000,200000, 250000,300000,350000, 400000,450000, 500000,550000,600000,630000,675000,700000,720000,750000,800000,825000,850000,875000,900000,912500,925000,950000,975000,987500,1000000]
datas = [0,23,473,7053,93643,1166714, 1810893, 2472071, 3145923, 3829761, 4522005,5221424,5927120,6638407,7354651,8075420,8509899,9164377,9529244,9821778,10261678,10997400,11366472,11736253,12106789,12478098,12664043,12850013,13222660,13595947,13782871,13969985]
N = int(input())
ttl = 0
numPin = 0
for i in range(len(numbers)):
if N == 1000000:
numPin = len(numbers) - 1
break
if numbers[i] <= N < numbers[i+1]:
numPin = i
break
if numPin != len(numbers)-1:
if N - numbers[numPin] < numbers[numPin + 1] - N:
for i in range(numbers[numPin], N):
ttl += make_divisors(i)
print(ttl + datas[numPin])
else:
for i in range(N,numbers[numPin+1]):
ttl -= make_divisors(i)
print(ttl + datas[numPin + 1])
else:
print(datas[numPin])
| 0 | null | 3,233,312,793,484 | 83 | 73 |
N = int(input())
A = list(map(int, input().split()))
l = [0]*N
for i in A:
l[i-1] += 1
print(*l, sep="\n")
|
n = int(input())
#a, b, h, m = map(int, input().split())
#al = list(map(int, input().split()))
#al=[list(input()) for i in range(h)]
l = 1
total = 26
while n > total:
l += 1
total += 26**l
last = total-26**l
v = n-last-1
ans = ''
# 26進数だと見立てて計算
for i in range(l):
c = v % 26
ans += chr(ord('a')+c)
v = v//26
print("".join(ans[::-1]))
| 0 | null | 22,298,005,089,400 | 169 | 121 |
N=input()
N=list(N)
N=map(lambda x: int(x),N)
if sum(N)%9==0:
print("Yes")
else:
print("No")
|
from sys import stdin
lis = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"]
S = input()
k = 0
for i in range(7):
if lis[i] == S:
k = i
print(7 - k)
| 0 | null | 68,466,703,171,350 | 87 | 270 |
import numpy as np
from itertools import combinations
h, w, k = map(int, input().split())
graph = []
for i in range(h):
row = [1 if x == '#' else 0 for x in input()]
graph.append(row)
graph = np.array(graph)
h_combi = []
w_combi = []
for i in range(1, h + 1):
h_combi.extend(list(combinations(range(h), i)))
for i in range(1, w + 1):
w_combi.extend(list(combinations(range(w), i)))
ans = 0
for hi in h_combi:
for wi in w_combi:
cnt = 0
for y in hi:
if cnt > k:
break
for x in wi:
cnt += graph[y][x]
if cnt == k:
ans += 1
print(ans)
|
import itertools
a,b,c=map(int, input().split())
l = [input() for i in range(a)]
ans = 0
Ans = 0
for i in itertools.product((1,-1),repeat=a):
for j in itertools.product((1,-1),repeat=b):
for k in range(a):
for m in range(b):
if i[k] == j[m] == 1 and l[k][m] == '#':
ans += 1
if ans == c:
Ans += 1
ans = 0
print(Ans)
| 1 | 8,911,133,966,940 | null | 110 | 110 |
N = int(input())
A = list(map(int,input().split()))
for i in range(N):
A[i] = (A[i],i+1)
A.sort()
ans = ''
for tup in A:
v, i = tup
ans+=str(i)+' '
print(ans)
|
x = int(input())
print(1000*(x//500)+5*((x-(500*(x//500)))//5))
| 0 | null | 111,175,223,407,456 | 299 | 185 |
N = int(input())
a = list(map(int, input().split()))
cnt = 0
for i in range(0, N, 2):
if a[i] % 2 == 1:
cnt += 1
print(cnt)
|
def Qa():
n = input()
if n[-1] in ['2', '4', '5', '7', '9']:
print('hon')
elif n[-1] in ['0', '1', '6', '8']:
print('pon')
else:
print('bon')
if __name__ == '__main__':
Qa()
| 0 | null | 13,593,495,930,560 | 105 | 142 |
N = int(input())
N_ri = round(pow(N, 1/2))
for i in range(N_ri, 0, -1):
if N % i == 0:
j = N // i
break
print(i + j - 2)
|
n,a,b=map(int,input().split())
if a>b: a,b=b,a
if (a-b)%2==0: print((b-a)//2)
else: print(min(a-1,n-b)+1+(b-a-1)//2)
| 0 | null | 134,763,719,832,532 | 288 | 253 |
from functools import reduce
x,y=list(map(int,input().split()))
mod = 10 ** 9 + 7
if (2 * y - x) % 3 != 0 or (2 * x - y) % 3 != 0:
print("0")
exit()
a,b = (2 * y - x) // 3, (2 * x - y) // 3
r = max(a,b)
if min(a,b) < 0:
print("0")
else:
numerator = reduce(lambda x, y: x * y % mod, range(a + b - r + 1, a + b + 1))
denominator = reduce(lambda x, y: x * y % mod, range(1 , r + 1))
print(numerator * pow(denominator, mod - 2, mod) % mod)
|
n=input().split()
x=int(n[0])
y=int(n[1])
if 1<=x<1000000000 and 1<=y<=1000000000:
if x>y:
while y!=0:
t=y
y=x%t
x=t
print(x)
else:
while x!=0:
t=x
x=y%t
y=t
print(y)
| 0 | null | 74,638,618,333,988 | 281 | 11 |
import math
import collections
from collections import defaultdict
n=int(input())
l=[list(map(int,input().split())) for i in range(n)]
mod=10**9+7
L=[]
ct0=0
for i in range(n):
if l[i][0]==0 and l[i][1]==0:
ct0+=1
elif l[i][0]==0:
L.append((0,1))
elif l[i][1]==0:
L.append((1,0))
elif l[i][0]<0:
L.append((-l[i][0]//math.gcd(l[i][0],l[i][1]),-l[i][1]//math.gcd(l[i][0],l[i][1])))
else:
L.append((l[i][0]//math.gcd(l[i][0],l[i][1]),l[i][1]//math.gcd(l[i][0],l[i][1])))
ans=1
d=set()
c=collections.Counter(L)
for i in c.keys():
if i[0]==0:
x=(1,0)
elif i[1]==0:
x=(0,1)
elif i[1]<0:
x=(-i[1],i[0])
else:
x=(i[1],-i[0])
if i not in d:
ans*=pow(2,c[i],mod)+pow(2,c[x],mod)-1
ans%=mod
d.add(x)
print((ans-1+ct0)%mod)
|
n = int(input())
s = input()
ans = 0
for i in range(1000):
i = '0' * (3 - len(str(i))) + str(i)
keep = 0
for j in range(n):
if s[j] == i[keep]:
keep += 1
if keep == 3:
ans += 1
break
print(ans)
| 0 | null | 74,740,439,533,522 | 146 | 267 |
import math
from functools import reduce
from collections import deque, defaultdict
import sys
sys.setrecursionlimit(10**7)
# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
return [ int(s) for s in input().split(" ")]
# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
return [ int(input()) for _ in range(n)]
# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
return map(int, open(0).read().split())
def log(*args):
print("DEBUG:", *args, file=sys.stderr)
n,p = get_nums_l()
s = input()
X = list(map(int, list(s)))
n = len(s)
if p in (2,5):
ans = 0
for i,x in enumerate(X):
if x%p == 0:
ans += i+1
print(ans)
else:
ruiseki = [0] * (n+1)
for i in (range(n-1, -1, -1)):
ruiseki[i] = (X[i] * pow(10, (n-i-1), p) + ruiseki[i+1]) % p
# log(ruiseki)
ans = 0
count = defaultdict(int)
for x in ruiseki:
# log(count[x])
ans += count[x]
count[x] += 1
# log(count)
print(ans)
|
n = int(input())
s = str(input())
if n % 2 == 1:
print("No")
exit()
if s[:n // 2] == s[n // 2:]:
print("Yes")
exit()
print("No")
| 0 | null | 102,290,484,275,580 | 205 | 279 |
import math
a, b, x = map(int, input().split())
if x == (a**2*b)/2:
print(45)
elif x > (a**2*b)/2:
print(math.degrees(math.atan((2*(b-x/(a**2)))/a)))
else:
print(math.degrees(math.atan(a*b**2/(2*x))))
|
S = int(input())
h = S // 3600
m = S % 3600 // 60
s = (S % 3600) - (m * 60)
print(h,m,s,sep=':')
| 0 | null | 81,418,382,893,060 | 289 | 37 |
def resolve():
S = input()
T = input()
cnt = 0
for i in range(len(S)):
s = S[i]
t = T[i]
if s != t:
cnt += 1
print(cnt)
resolve()
|
s = input()
t = input()
counter = 0
for index, character in enumerate(s):
if t[index] != character:
counter += 1
print(counter)
| 1 | 10,559,757,127,282 | null | 116 | 116 |
n = int(input())
s = 0
for i in range(1,n+1):
if i!=3 and i!=5 and i%3!=0 and i%5!=0:
s+=i
print(s)
|
from collections import deque
D1 = {i:chr(i+96) for i in range(1,27)}
D2 = {val:key for key,val in D1.items()}
N = int(input())
heap = deque([(D1[1],1)])
A = []
while heap:
a,n = heap.popleft()
if n<N:
imax = 0
for i in range(len(a)):
imax = max(imax,D2[a[i]])
for i in range(1,min(imax+1,26)+1):
heap.append((a+D1[i],n+1))
if n==N:
A.append(a)
A = sorted(list(set(A)))
for i in range(len(A)):
print(A[i])
| 0 | null | 43,442,372,551,072 | 173 | 198 |
N = int(input())
l = []
for i in range(N+1):
if i % 5 == 0 or i % 3 == 0:
l.append(0)
else:
l.append(i)
print(sum(l))
|
n = int(input())
sum = 0
for i in range(1, n + 1):
if i % 3 != 0 and i % 5 != 0:
sum = sum + i
print(sum)
| 1 | 34,955,825,519,160 | null | 173 | 173 |
W,H,x,y,r=map(int,input().split())
print('Yes') if x+r<=W and y+r<=H and r<=x and r<=y else print('No')
|
import math;print(2**(int(math.log2(int(input())))+1)-1)
| 0 | null | 40,272,581,625,352 | 41 | 228 |
import numpy as np
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
S1 = (A1 - B1) * T1
S2 = (A2 - B2) * T2
if (S1 + S2) == 0:
print('infinity')
elif np.sign(S1) == np.sign(S2):
print(0)
elif abs(S1) > abs(S2):
print(0)
else:
z = 1 + ((abs(S1)) // abs(S1 + S2)) * 2
if (abs(S1)) % abs(S1 + S2) == 0:
z = z -1
print(z)
|
from collections import Counter
def main():
H, W, M = list(map(int, input().split()))
bombs = [list(map(int, input().split())) for _ in range(M)]
counter_row = Counter([h for h, _ in bombs])
val_max_row, max_rows = 0, []
for h, v in counter_row.items():
if val_max_row < v:
val_max_row = v
max_rows = [h]
elif val_max_row == v:
max_rows.append(h)
counter_col = Counter([w for _, w in bombs])
val_max_col, max_cols = 0, []
for w, v in counter_col.items():
if val_max_col < v:
val_max_col = v
max_cols = [w]
elif val_max_col == v:
max_cols.append(w)
# 基本的には val_max_row + val_max_col が答え。
# 行・列で重複カウントになるケースだった場合はここから1引かないといけない。
max_rows = Counter(max_rows)
max_cols = Counter(max_cols)
n_max_cells = len(max_rows.keys()) * len(max_cols.keys())
n_cells = 0
for h, w in bombs:
if max_rows[h] > 0 and max_cols[w] > 0:
n_cells += 1
ans = val_max_row + val_max_col
if n_cells >= n_max_cells:
ans -= 1
print(ans)
if __name__ == '__main__':
main()
| 0 | null | 68,441,926,973,760 | 269 | 89 |
n, k = (int(i) for i in input().split())
w = [int(input()) for i in range(n)]
total_weght = sum(w)
def check(p):
track = [0 for i in range(k)]
target = 0
for i in range(k):
while (track[i] + w[target]) <= p:
track[i] += w[target]
target += 1
if target not in range(n):
break
if target not in range(n):
break
return(target)
def solve(n):
left = 0
right = 100000 * 100000
while left < right:
mid = (left + right) // 2
if n == check(mid):
right = mid
else:
left = mid + 1
return(right)
if __name__ == "__main__":
ans = solve(n)
print(ans)
|
N, K = map(int, input().split())
W = []
for i in range(N):
W.append(int(input()))
def f(N, K, W, p):
cnt = 0
k = 1
c_k = 0
for w in W:
if c_k + w <= p:
c_k = c_k + w
cnt = cnt + 1
elif k < K and w <= p:
k = k + 1
c_k = w
cnt = cnt + 1
elif k < K and w > p:
pass
else:
break
return cnt
start = 0
end = 1000000000
while start != end:
center = (start + end)//2
if f(N, K, W, center) < N:
start = center + 1
else:
end = center
print(start)
| 1 | 86,578,767,190 | null | 24 | 24 |
X,Y = map(int, input().split())
if Y - 2*X >= 0 and 4*X - Y >= 0 and (Y - 2*X) % 2 == 0 and (4*X - Y) % 2 == 0:
print('Yes')
else:
print('No')
|
cnt=0
radi=0
x=int(input())
while True:
cnt+=1
radi+=x
radi%=360
if radi==0:
print(cnt)
break
| 0 | null | 13,339,053,224,448 | 127 | 125 |
# encoding:utf-8
input = input()
second = input % 60
minute = input / 60 % 60
hour = input / 60 / 60 % 60
print(":".join(map(str, (hour, minute, second))))
|
H=int(input())
Q=[]
import math
for i in range(10000):
if H>1:
H=H//2
Q.append(H)
elif H==1:
break
Q.sort()
S=0
a=1
for i in range(len(Q)):
a=2*a+1
print(a)
| 0 | null | 39,948,768,394,522 | 37 | 228 |
from itertools import combinations
while True:
n,m=map(int,input().split())
if n==m==0: break
print(sum(1 for p in combinations(range(1,n+1),3) if sum(p)==m))
|
while True:
N,X = map(int,input().split())
if N == 0 and X == 0:
break
ans = 0
for a in range(1,N+1):
for b in range(1,N+1):
if b <= a:
continue
c = X-(a+b)
if c > b and c <= N:
ans += 1
print("%d"%(ans))
| 1 | 1,290,227,247,848 | null | 58 | 58 |
import sys
from collections import deque, defaultdict, Counter
from itertools import accumulate, product, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush
from math import ceil, floor, sqrt, gcd, inf
from copy import deepcopy
import numpy as np
import scipy as sp
INF = inf
MOD = 1000000007
n = int(input())
A = [[int(i) for i in input().split()]for j in range(n)] # nは行数
tmp = 0
res = 0
x = np.median(np.array(A), axis=0)
if n % 2 == 0:
res = int((x[1] - x[0]) * 2 + 1)
else:
res = int(x[1] - x[0] + 1)
print(res)
|
from collections import deque
n = int(input())
M = [[0]*n for _ in range(n)]
d = [-1]*n
st = [0]*n
for _ in range(n):
i = [int(i) for i in input().split()]
u = i[0]
k = i[1]
V = i[2:]
for v in V: M[u-1][v-1] = 1
def bfs(s):
Q = deque()
Q.append(s)
st[s] = 1
d[s] = 0
while Q:
u = Q.popleft()
for v in range(n):
if M[u][v] and st[v] == 0:
st[v] = 1
Q.append(v)
d[v] = d[u]+1
st[u] = 2
return
bfs(0)
for i in range(n): print(i+1,d[i])
| 0 | null | 8,732,807,315,932 | 137 | 9 |
from math import ceil
n = int(input())
dept = 100000
for i in range(n):
dept += dept*0.05
dept = int(int(ceil(dept/1000)*1000))
print(dept)
|
import math
loan = 100000
n = int(input())
for i in range(n):
interest = math.ceil(loan * 0.05 / 1000) * 1000
loan += interest
print(loan)
| 1 | 1,217,325,340 | null | 6 | 6 |
a,b,c,k=[int(x) for x in input().split()]
ans=0
if a<=k:
ans+=a
k-=a
elif k>0:
ans+=k
k=0
if b<=k:
k-=b
elif k>0:
k=0
if k>0:
ans-=k
print(ans)
|
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)
| 0 | null | 24,932,195,286,790 | 148 | 161 |
s = input()
t = input()
T = list(t)
sl = len(s)
tl = len(t)
ans = 10**18
for i in range((sl-tl)+1):
cnt = 0
S = list(s[i:i+tl])
for a,b in zip(S,T):
if a != b:
cnt += 1
if ans > cnt:
ans = cnt
print(ans)
|
a,b,c,k= map(int,input().split())
ans=a
if k<a:
ans = k
if a+b<k:
ans = ans-k+a+b
print(ans)
| 0 | null | 12,720,518,957,060 | 82 | 148 |
n = int(input())
if n >= 1800:
print("1")
elif n >= 1600:
print("2")
elif n >= 1400:
print("3")
elif n >= 1200:
print("4")
elif n >= 1000:
print("5")
elif n >= 800:
print("6")
elif n >= 600:
print("7")
elif n >= 400:
print("8")
|
from math import sqrt, pow
x1, y1, x2, y2 = list(map(float, input().split()))
print(sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)))
| 0 | null | 3,449,174,790,192 | 100 | 29 |
import sys
def factorization(n):
arr = []
temp = n
for i in range(2, int(-(-n**0.5//1))+1):
if temp%i==0:
cnt=0
while temp%i==0:
cnt+=1
temp //= i
arr.append([i, cnt])
if temp!=1:
arr.append([temp, 1])
if arr==[]:
arr.append([n, 1])
return arr
N = int(input())
if N == 1:
print(0)
sys.exit()
a = factorization(N)
ans = 0
for i in range(len(a)):
k = a[i][1]
cnt = 1
while k-cnt>=0:
k -= cnt
cnt += 1
ans += cnt-1
print(ans)
|
n, m, l = map(int, input().split())
A = []
B = []
for line in range(n):
A.append(list(map(int, input().split())))
for line in range(m):
B.append(list(map(int, input().split())))
C = []
for lines in range(n):
C.append([sum([A[lines][i] * B[i][j] for i in range(m)]) for j in range(l)])
print(" ".join(map(str, C[lines])))
| 0 | null | 9,132,965,651,392 | 136 | 60 |
d, t, f = map(int, input().split())
if (d/t) <= f:
print('Yes')
else:
print('No')
|
a,b,c=map(int, input().split())
A=(b*c)
if A>a:
print('Yes')
elif A==a:
print('Yes')
else:
print('No')
| 1 | 3,548,731,267,108 | null | 81 | 81 |
n = int(input())
a = list(map(int, input().split()))
if n%2 == 0:
dp = a[:2]
for i in range(1, n//2):
dp = dp[0] + a[2*i], max(dp) + a[2*i+1]
else:
dp = a[:3]
for i in range(1, n//2):
dp = dp[0] + a[2*i], max(dp[:2]) + a[2*i+1], max(dp) + a[2*i+2]
print(max(dp))
|
N=str(input())
print(N[0]+N[1]+N[2])
| 0 | null | 26,292,632,133,080 | 177 | 130 |
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
import sys
# sys.stdout.write(str(n)+"\n")
MODULO = 1000000007
# INF = 1e18+10
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
n = int(input())
a_b = [() for i in range(n)]
m_b_a = [() for i in range(n)]
# a_b_value_indices = {}
# m_b_a_value_indices = {}
a_b_value_count = {}
m_b_a_value_count = {}
a_b_value_rep = {}
m_b_a_value_rep = {}
used = [False for _ in range(n)]
zeroes = 0
is_zero = [False for _ in range(n)]
for i in range(n):
a, b = map(int, input().split())
if a == 0 and b == 0:
zeroes += 1
used[i] = True
is_zero[i] = True
else:
if a == 0:
a_b[i] = (0, 1)
m_b_a[i] = (1, 0)
elif b == 0:
a_b[i] = (1, 0)
m_b_a[i] = (0, 1)
else:
neg = False
if a*b < 0:
neg = True
d = gcd(abs(a), abs(b))
a = abs(a) // d
b = abs(b) // d
if neg:
a_b[i] = (-a, b)
m_b_a[i] = (b, a)
else:
a_b[i] = (a, b)
m_b_a[i] = (-b, a)
a_b_value_count[a_b[i]] = a_b_value_count.get(a_b[i], 0) + 1
# a_b_value_indices[a_b[i]] = a_b_value_indices.get(a_b[i], []) + [i]
m_b_a_value_count[m_b_a[i]] = m_b_a_value_count.get(m_b_a[i], 0) + 1
# m_b_a_value_indices[m_b_a[i]] = m_b_a_value_indices.get(m_b_a[i], []) + [i]
if a_b[i] not in a_b_value_rep:
a_b_value_rep[a_b[i]] = i
if m_b_a[i] not in m_b_a_value_rep:
m_b_a_value_rep[m_b_a[i]] = i
ans = 1
for i in range(n):
if not is_zero[i] and not used[a_b_value_rep[a_b[i]]]:
# if not connected
if a_b[i] not in m_b_a_value_count:
ans *= pow(2, a_b_value_count[a_b[i]], MODULO)
ans %= MODULO
used[a_b_value_rep[a_b[i]]] = True
# for j in a_b_value_indices[a_b[i]]:
# used[j] = True
else:
s = a_b_value_count[a_b[i]]
t = m_b_a_value_count[a_b[i]]
ans *= ((1*pow(2, s, MODULO) + 1*pow(2, t, MODULO) -1) % MODULO)
ans %= MODULO
used[a_b_value_rep[a_b[i]]] = True
used[m_b_a_value_rep[a_b[i]]] = True
# for j in m_b_a_value_indices[a_b[i]]:
# used[j] = True
# used_a_b_keys.add(a_b_key)
# used_a_b_keys.add(-1/a_b_key)
# -1 is for empty
print((ans-1+zeroes) % MODULO)
|
from collections import defaultdict
from math import gcd
def main():
mod = 10 ** 9 + 7
worst_iwashi_count = 0
ans = 1
d = defaultdict(int)
N = int(input())
for _ in range(N):
a, b = map(int, input().split())
if a == 0 and b == 0:
worst_iwashi_count += 1
elif a == 0:
d[(0, -1)] += 1
elif b == 0:
d[(1, 0)] += 1
else:
g = gcd(abs(a), abs(b))
if a * b > 0:
d[(abs(a // g), abs(b // g))] += 1
else:
d[(abs(a // g), -abs(b // g))] += 1
done_set = set()
for a, b in list(d):
if (a, b) in done_set:
continue
x = d[(a, b)]
done_set.add((a, b))
if b >= 0:
y = d[(b, -a)]
done_set.add((b, -a))
else:
y = d[(-b, a)]
done_set.add((-b, a))
ans *= 2 ** x + 2 ** y - 1
ans %= mod
ans += worst_iwashi_count - 1
print(ans % mod)
if __name__ == '__main__':
main()
| 1 | 20,989,709,359,356 | null | 146 | 146 |
#!/usr/bin/env python3
import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
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,k = LI()
a = LI()
for i in range(k, n):
if a[i] > a[i-k]:
print('Yes')
else:
print('No')
|
# cook your dish here
n,k=input().split()
n=int(n)
k=int(k)
l=list(map(int,input().split()))
for j in range(n):
if k+j==n:
break
if l[k+j]>l[j]:
print("Yes")
else:
print("No")
| 1 | 7,159,693,942,580 | null | 102 | 102 |
n,k=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
t=""
count=0
for i in range(n):
if i>k-1:
if T[i]=="r":
if t[i-k]!="p":
t=t+"p"
count+=P
else:
t=t+" "
if T[i]=="s":
if t[i-k]!="r":
t=t+"r"
count+=R
else:
t=t+" "
if T[i]=="p":
if t[i-k]!="s":
t=t+"s"
count+=S
else:
t=t+" "
else:
if T[i]=="r":
t=t+"p"
count+=P
if T[i]=="p":
t=t+"s"
count+=S
if T[i]=="s":
t=t+"r"
count+=R
print(count)
|
def decide_hand2(T, hand, i, K, N):
if T[i] == "r":
if i <= K - 1:
hand[i] = "p"
else:
if hand[i - K] == "p":
hand[i] = "-"
else:
hand[i] = "p"
elif T[i] == "p":
if i <= K - 1:
hand[i] = "s"
else:
if hand[i - K] == "s":
hand[i] = "-"
else:
hand[i] = "s"
elif T[i] == "s":
if i <= K - 1:
hand[i] = "r"
else:
if hand[i - K] == "r":
hand[i] = "-"
else:
hand[i] = "r"
def main():
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
hand = ["" for _ in range(N)]
for i in range(N):
decide_hand2(T, hand, i, K, N)
ans = 0
for i in range(N):
if hand[i] == "r":
ans += R
elif hand[i] == "s":
ans += S
elif hand[i] == "p":
ans += P
print(ans)
if __name__ == "__main__":
main()
| 1 | 106,641,789,860,180 | null | 251 | 251 |
N=input()
L=[int(N[i]) for i in range(len(N))]
P=0
OT=0
for i in range(len(L)-1):
if 0 <= L[i] <= 3:
P += L[i] + OT
OT = 0
elif L[i] == 4:
if OT == 1 and L[i+1]>=5:
P += 5
else:
P += OT+L[i]
OT = 0
elif L[i] == 5:
if OT == 1:
P += 4
else:
if L[i+1] >= 5:
P += 1 + 4
OT = 1
else:
P += 5
else:
if OT == 1:
P += 9-L[i]
OT = 1
else:
P += 10 - L[i]
OT = 1
Pone = 0
if OT==0:
Pone = min(L[-1],11-L[-1])
elif OT==1 and L[-1]<=4:
Pone = 1+L[-1]
else:
Pone = 10-L[-1]
print(P+Pone)
|
# E - Payment
N = input().strip()
s = 0
carry = 0
five = False
for c in reversed(N):
x = int(c) + carry
if x == 5 and not five:
s += x
carry = 0
five = True
elif x >= 5:
if five:
x += 1
s += 10 - x
carry = 1
five = False
else:
s += x
carry = 0
five = False
s += carry
print(s)
| 1 | 71,060,873,273,500 | null | 219 | 219 |
import collections
class UnionFind:
def __init__(self, N):
self.parent = [i for i in range(N)]
self.rank = [0]*N
self.count = 0
def root(self, a):
if self.parent[a]==a:
return a
else:
self.parent[a]=self.root(self.parent[a])
return self.parent[a]
def size(x):
return -par[root(x)]
def is_same(self, a, b):
return self.root(a)==self.root(b)
def unite(self, a, b):
ra = self.root(a)
rb = self.root(b)
if ra == rb: return
if self.rank[ra] < self.rank[rb]:
self.parent[ra] = rb
else:
self.parent[rb] = ra
if self.rank[ra] == self.rank[rb]: self.rank[ra] += 1
self.count += 1
def main():
n,m,k=map(int, input().split())
friend = [0]*n
fr = UnionFind(n)
blocklist = [0]*n
for i in range(m):
a,b = map(int, input().split())
fr.unite(a-1,b-1)
friend[a-1]+=1
friend[b-1]+=1
for i in range(k):
c,d=map(int, input().split())
if(fr.root(c-1)==fr.root(d-1)):
blocklist[c-1]+=1
blocklist[d-1]+=1
res = []
dd = collections.defaultdict(int)
for i in range(n):
dd[fr.root(i)]+=1
for i in range(n):
res.append(dd[fr.root(i)]- blocklist[i] - friend[i]-1)
print(*res)
if __name__ == '__main__':
main()
|
import sys
input = sys.stdin.readline
class Unionfind(): # 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)
# 解説AC
n, m, k = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
cd = [list(map(int, input().split())) for _ in range(k)]
uf = Unionfind(n)
frnd = [0] * n
for a, b in ab:
uf.union(a - 1, b - 1)
frnd[a - 1] += 1
frnd[b - 1] += 1
brck = [0] * n
for c, d in cd:
if uf.same(c - 1, d - 1):
brck[c - 1] += 1
brck[d - 1] += 1
ans = []
for i in range(n):
ans.append(uf.size(i) - frnd[i] - brck[i] - 1)
print(*ans)
| 1 | 61,726,518,438,430 | null | 209 | 209 |
a,b,c,d = map(int,input().split())
li = list()
li.append(a * c)
li.append(a * d)
li.append(b * c)
li.append(b * d)
print(max(li))
|
a,b,c,d = map(int,input().split())
result=(a*c),(b*c),(a*d),(b*d)
print(max(result))
| 1 | 3,016,034,928,390 | null | 77 | 77 |
t=int(input())
if(t==1):
print(0)
else:
print(1)
|
s = map(int, raw_input().split())
s.sort()
print ' '.join(map(str, s))
| 0 | null | 1,653,421,338,448 | 76 | 40 |
H=1
W=1
while H!=0 or W!=0:
H,W = [int(i) for i in input().split()]
if H!=0 or W!=0:
for i in range(W):
print('#',end='')
print()
for i in range(H-2):
print('#',end='')
for j in range(W-2):
print('.',end='')
print('#',end='\n')
for i in range(W):
print('#',end='')
print('\n')
|
while True:
s = input().split(" ")
H = int(s[0])
W = int(s[1])
if H == 0 and W == 0:
break
for i in range(H):
for j in range(W):
if i == 0 or i == H-1:
print("#",end="")
else:
if j == 0 or j == W-1:
print("#",end="")
else:
print(".",end="")
print("")
print("")
| 1 | 816,238,137,180 | null | 50 | 50 |
N, M, K = map(int, input().split())
F = [[] for _ in range(N)]
B = [[] for _ in range(N)]
for _ in range(M):
a, b = map(int, input().split())
a, b = a - 1, b - 1
F[a].append(b)
F[b].append(a)
for _ in range(K):
c, d = map(int, input().split())
c, d = c - 1, d - 1
B[c].append(d)
B[d].append(c)
class UnionFind:
def __init__(self, n):
# 親要素のノード番号を格納。par[x] == xの時そのノードは根(最初は全て根)
self.par = [i for i in range(n)]
# 木の高さを格納する(初期状態では0)
self.rank = [0] * n
# 人間の数
self.size = [1] * n
# 検索
def find(self, x):
# 根ならその番号を返す
if self.par[x] == x:
return x
# 根でないなら、親の要素で再検索
else:
# 走査していく過程で親を書き換える(return xが代入される)
self.par[x] = self.find(self.par[x])
return self.par[x]
# 併合
def union(self, x, y):
# 根を探す
x = self.find(x)
y = self.find(y)
if x == y:
return
# 木の高さを比較し、低いほうから高いほうに辺を張る
if self.rank[x] < self.rank[y]:
self.par[x] = y
self.size[y] += self.size[x]
else:
self.par[y] = x
self.size[x] += self.size[y]
# 木の高さが同じなら片方を1増やす
if self.rank[x] == self.rank[y]:
self.rank[x] += 1
# 同じ集合に属するか判定
def same(self, x, y):
return self.find(x) == self.find(y)
# すべての頂点に対して親を検索する
def all_find(self):
for n in range(len(self.par)):
self.find(n)
UF = UnionFind(N)
for iam in range(N):
for friend in F[iam]:
UF.union(iam, friend) # 自分と自分の友達を併合
ans = [UF.size[UF.find(iam)] - 1 for iam in range(N)] # 同じ集合に属する人の数
for iam in range(N):
ans[iam] -= len(F[iam]) # すでに友達関係にある人達を引く
for iam in range(N):
for block in B[iam]:
if UF.same(iam, block): # ブロック関係にあったら引く
ans[iam] -= 1
print(*ans, sep=' ')
|
import sys
N, M, K = map(int, input().split())
friends = [set() for _ in range(N)]
for _ in range(M):
a, b = map(int, sys.stdin.readline().split())
friends[a-1].add(b-1)
friends[b-1].add(a-1)
blocks = [set() for _ in range(N)]
for _ in range(K):
c, d = map(int, sys.stdin.readline().split())
blocks[c-1].add(d-1)
blocks[d-1].add(c-1)
done = set()
chains = []
todo = []
for s in range(N):
if s in done:
continue
chain = set()
todo.append(s)
while todo:
i = todo.pop()
for ni in friends[i]:
if ni in done:
continue
done.add(ni)
chain.add(ni)
todo.append(ni)
chains.append(chain)
ans = [0] * N
for chain in chains:
for i in chain:
blocks[i].intersection_update(chain)
ans[i] = len(chain) - len(blocks[i]) - len(friends[i]) - 1
print(" ".join(map(str, ans)))
| 1 | 61,405,178,588,282 | null | 209 | 209 |
def trans(l):
return [list(x) for x in list(zip(*l))]
from itertools import product
import copy
h, w, k = map(int, input().split())
c = []
for _ in range(h):
c.append([c for c in input()])
A = [i for i in product([1,0], repeat=h)]
B = [i for i in product([1,0], repeat=w)]
ans = 0
for a in A:
temp1 = copy.copy(c)
for i, x in enumerate(a):
if x == 1:
temp1[i] = ["."] * w
for b in B:
temp2 = trans(temp1)
for i, x in enumerate(b):
if x == 1:
temp2[i] = ["."] * h
cnt = 0
for t in temp2:
cnt += t.count("#")
if cnt == k:
ans += 1
print(ans)
|
import sys
from bisect import bisect_left, bisect_right, insort
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
N = ir()
S = list('-' + sr())
d = [[] for _ in range(26)]
for i in range(1, N+1):
s = S[i]
o = ord(s) - ord('a')
d[o].append(i)
Q = ir()
for _ in range(Q):
q, a, b = sr().split()
if q == '1':
a = int(a)
if S[a] == b:
continue
prev = ord(S[a]) - ord('a')
d[prev].pop(bisect_left(d[prev], a))
next = ord(b) - ord('a')
insort(d[next], a)
S[a] = b
else:
a = int(a); b = int(b)
ans = 0
for alpha in range(26):
if bisect_right(d[alpha], b) - bisect_left(d[alpha], a) >= 1:
ans += 1
print(ans)
| 0 | null | 35,499,859,997,798 | 110 | 210 |
x = int(input())
if x == 0:
print("1")
else:
print("0")
|
print("0" if input()=="1" else "1")
| 1 | 2,943,253,517,532 | null | 76 | 76 |
n0 = int(1e5)+1
x = [0]*n0
n = int(input())
a0 = list(map(int,input().split()))
q = int(input())
bcs = []
for i in range(q):
bcs.append(list(map(int,input().split())))
for a in a0:
x[a] += 1
sum0 = sum(a0)
for bc in bcs:
sum0 = sum0 + bc[1]*x[bc[0]] - bc[0]*x[bc[0]]
x[bc[1]] += x[bc[0]]
x[bc[0]] = 0
print(sum0)
|
N = int(input())
M = 10 ** 5 + 1
A = list(map(int, input().split()))
A2 = [0 for i in range(M)]
for a in A:
A2[a] += 1
ans = sum(A)
Q = int(input())
for j in range(Q):
b, c = map(int, input().split())
ans = ans + (c - b) * A2[b]
A2[c] += A2[b]
A2[b] = 0
print(ans)
| 1 | 12,230,886,968,348 | null | 122 | 122 |
n = int(input())
movies = {}
summize = 0
for i in range(n):
input_str = input().split()
summize += int(input_str[1])
movies.setdefault(input_str[0], summize)
title = input()
print(summize - movies[title])
|
N = int(input())
M = []
for _ in range(N):
s, t = input().split()
M.append((s, int(t)))
X = input()
res = sum(map(lambda m: m[1], M))
for m in M:
res -= m[1]
if X == m[0]:
break
print(res)
| 1 | 96,739,887,305,480 | null | 243 | 243 |
from collections import deque
def pr(a):
for i in range(len(a)):
if i == len(a)-1:
print(a[i])
else:
print(a[i],end = " ")
n = int(input())
q = deque()
for i in range(n):
x = input()
l = len(x)
if x[0] == "i":
q.insert(0,x[7:])
elif x[6] == " ":
try:
q.remove(x[7:])
except:
pass
elif l > 10:
q.popleft()
elif l > 6:
q.pop()
print(*q)
|
from collections import deque
import sys
n = int(sys.stdin.readline())
#lines = sys.stdin.readlines()
lines = [input() for i in range(n)]
deq = deque()
for i in range(n):
o = lines[i].split()
if o[0] == 'insert':
deq.appendleft(o[1])
elif o[0] == 'delete':
try:
deq.remove(o[1])
except:
continue
elif o[0] == 'deleteFirst':
deq.popleft()
elif o[0] == 'deleteLast':
deq.pop()
print(' '.join(deq))
| 1 | 49,382,615,322 | null | 20 | 20 |
x = int(input())
#print(1 if x == 0 else 0)
print(x ^ 1)
|
#! /usr/bin/python3
x = int(input())
print(1-x)
| 1 | 2,954,155,808,498 | null | 76 | 76 |
N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 10**9+7
comb = [0] * (N+1)
comb[K-1] = 1
for i in range(K-1, N):
comb[i+1] = comb[i] * (i+1) * pow(i-K+2, -1, mod) % mod
# print(comb)
A.sort()
mi = sum(a*comb[i]%mod for a, i in zip(reversed(A[:N-K+1]), range(K-1, N)))
ma = sum(a*comb[i]%mod for a, i in zip((A[K-1:]), range(K-1, N)))
# print(mi%mod, ma%mod)
print((ma-mi)%mod)
|
def main():
import collections
import bisect
def cmb(n, r, mod):
if (r < 0 or r > n):
return 0
r = min(r, n-r)
return g1[n] * g2[r] * g2[n-r] % mod
mod = 10**9+7 # 出力の制限
N = 10**5
g1 = [1, 1] # 元テーブル
g2 = [1, 1] # 逆元テーブル
inverse = [0, 1] # 逆元テーブル計算用テーブル
for i in range(2, N + 1):
g1.append((g1[-1] * i) % mod)
inverse.append((-inverse[mod % i] * (mod//i)) % mod)
g2.append((g2[-1] * inverse[-1]) % mod)
N, K = map(int, input().split())
A = list(map(int, input().split()))
table = collections.defaultdict(int)
A.sort()
for x in A:
table[x] += 1
ans = 0
for x in table:
left = bisect.bisect_left(A, x)
for i in range(1, table[x]+1):
if left >= K - i:
ans += x * cmb(left, K - i, mod) * cmb(table[x], i, mod)
right = left + table[x] - 1
for i in range(1, table[x]+1):
if N - 1 - right >= K - i:
ans -= x * cmb(N - 1 - right, K-i, mod) * cmb(table[x], i, mod)
ans %= mod
print(ans)
main()
| 1 | 95,585,255,321,540 | null | 242 | 242 |
import math
n=int(input())
if int(math.ceil(n/1.08)*1.08)==n:
print(math.ceil(n/1.08))
else:
print(":(")
|
n=int(input())
for x in range(n+1):
if x*108//100==n:
print(x)
break
else:
print(":(")
| 1 | 125,553,388,004,530 | null | 265 | 265 |
n=int(input())
c=0
for _ in range(n):
x=int(input())
if x==2:c+=1
elif pow(2,x-1,x)==1:c+=1
print(c)
|
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
#???????????£????????????
NEXT = 2
DATA = 1
PREV = 0
from collections import deque
def dll_processor(operations):
front = None
end = None
dll = deque([])
for o in operations:
if o[0] == "insert":
dll.appendleft(o[1])
#front, end = insert(front, end, o[1])
elif o[0] == "delete":
if o[1] in dll:
dll.remove(o[1])
#front, end = delete(front, end, o[1])
elif o[0] == "deleteFirst":
dll.popleft()
#front, end = delete_first(front, end)
elif o[0] == "deleteLast":
dll.pop()
#front, end = delete_last(front, end)
#print(get_list(front))
return dll
def get_list(front):
if not front:
return []
l = []
target = front
while True:
l.append(target[DATA])
if not target[NEXT]:
break
target = target[NEXT]
return l
def insert(front, end, target):
node = [None, target, None]
if front:
front[PREV] = node
node[NEXT] = front
return node, end
else:
return node, node
def delete(front, end,target):
delete_node = front
while not delete_node[DATA] == target:
delete_node = delete_node[NEXT]
if delete_node == None:
return front, end
if delete_node[PREV] == None:
delete_node[NEXT][PREV] = None
return delete_node[NEXT], end
elif delete_node[NEXT] == None:
delete_node[PREV][NEXT] = None
return front, delete_node[PREV]
else:
delete_node[NEXT][PREV] = delete_node[PREV]
delete_node[PREV][NEXT] = delete_node[NEXT]
return front, end
def delete_last(front, end):
if not end[PREV]:
return None, None
else:
end[PREV][NEXT] = None
return front, end[PREV]
def delete_first(front, end):
if not front[NEXT]:
return None, None
else:
front[NEXT][PREV] = None
return front[NEXT], end
def main():
n_list = int(input())
target_list = [input().split() for i in range(n_list)]
print(*dll_processor(target_list))
if __name__ == "__main__":
main()
| 0 | null | 29,041,040,094 | 12 | 20 |
N,M,K = map(int,input().split())
MOD = 998244353
MAXN = N+5
fac = [1,1] + [0]*MAXN
finv = [1,1] + [0]*MAXN
inv = [0,1] + [0]*MAXN
for i in range(2,MAXN+2):
fac[i] = fac[i-1] * i % MOD
inv[i] = -inv[MOD%i] * (MOD // i) % MOD
finv[i] = finv[i-1] * inv[i] % MOD
def comb(n,r):
if n < r: return 0
if n < 0 or r < 0: return 0
return fac[n] * (finv[r] * finv[n-r] % MOD) % MOD
ans = 0
for i in range(K+1):
if i==N: break
n = N-i
ans += M * pow(M-1,n-1,MOD) * comb(N-1,i)
ans %= MOD
print(ans)
|
h, w, k = map(int, input().split())
S = [list(input()) for _ in range(h)]
ans = S.copy()
index = 1
for i in range(h):
for j in range(w):
if S[i][j] == "#":
ans[i][j] = str(index)
index += 1
for i in range(h):
for j in range(w-1):
if ans[i][j] != "." and ans[i][j+1] == ".":
ans[i][j+1] = ans[i][j]
for i in range(h):
for j in range(w-1, 0, -1):
if ans[i][j] != "." and ans[i][j-1] == ".":
ans[i][j-1] = ans[i][j]
for j in range(w):
for i in range(h-1):
if ans[i][j] != "." and ans[i+1][j] == ".":
ans[i+1][j] = ans[i][j]
for j in range(w):
for i in range(h-1, 0, -1):
if ans[i][j] != "." and ans[i-1][j] == ".":
ans[i-1][j] = ans[i][j]
for l in ans:
print(" ".join(l))
| 0 | null | 83,588,475,066,108 | 151 | 277 |
D, T, S = list(map(int, input().split()))
if -(-D // S) <= T:
print("Yes")
else:
print("No")
|
A,B = map(str,input().split())
print(B+A)
| 0 | null | 53,247,455,798,954 | 81 | 248 |
def med(l):
t = len(l)
if t%2:
return l[t//2]
else: return (l[t//2]+l[t//2-1])/2
n = int(input())
a = []
b = []
for i in range(n):
x,y = map(int,input().split())
a+=[x]
b+=[y]
a.sort()
b.sort()
if n%2==0:
print(int((med(b)-med(a))*2)+1)
else:
print(med(b)-med(a)+1)
|
def main():
N = int(input())
A,B = [],[]
for _ in range(N):
a,b = [int(x) for x in input().split()]
A.append(a)
B.append(b)
A.sort()
B.sort()
if N % 2 == 1:
minMed = A[N//2]
maxMed = B[N//2]
print(int(maxMed-minMed)+1)
else:
minMed = A[N//2-1] + A[N//2]
maxMed = B[N//2-1] + B[N//2]
print(int(maxMed-minMed)+1)
if __name__ == '__main__':
main()
| 1 | 17,342,830,738,850 | null | 137 | 137 |
N, K = map(int, input().split())
*A, = map(int, input().split())
*F, = map(int, input().split())
A.sort()
F.sort(reverse=True)
def f(x):
ans = 0
for i,j in zip(A, F):
ans += (i*j-x+j-1)//j if i*j-x>=0 else 0
return ans<=K
l, r = -1, 10**18+1
while r-l>1:
mid = (r+l)//2
if f(mid):
r = mid
else:
l = mid
print(r)
|
import sys,math
input = sys.stdin.readline
n,k = map(int,input().split())
a = [int(i) for i in input().split()]
f = [int(i) for i in input().split()]
a.sort()
f.sort(reverse=True)
c = [(i*j,j) for i,j in zip(a,f)]
m = max(c)
def is_ok(arg):
# 条件を満たすかどうか?問題ごとに定義
chk = 0
for i,j in c:
chk += math.ceil(max(0,(i-arg))/j)
return chk <= k
def bisect_ok(ng, ok):
'''
初期値のng,okを受け取り,is_okを満たす最小(最大)のokを返す
まずis_okを定義
ng = 最小の値-1 ok = 最大の値+1 で最小
最大最小が逆の場合はng ok をひっくり返す
'''
while (abs(ok - ng) > 1):
mid = (ok + ng) // 2
if is_ok(mid):
ok = mid
else:
ng = mid
return ok
print(bisect_ok(-1,m[0]+1))
| 1 | 164,904,434,769,680 | null | 290 | 290 |
from itertools import accumulate as acc
H, W, K = map(int, input().split())
S = [list(map(int, list(input()))) for _ in range(H)]
acc_list = [list(acc([0] + s)) for s in S]
def solve(l, x, limit):
part_num = len(l)-1
ok, ng = x, W+1
while abs(ok-ng) > 1:
mid = (ok+ng)//2
score = 0
for i in range(part_num):
s = 0
for j in range(l[i], l[i+1]):
# print(i, j, mid)
s += acc_list[j][mid] - acc_list[j][x]
score = max(s, score)
if score <= limit:
ok = mid
else:
ng = mid
return ok
min_num = 10**18
for i in range(1<<(H-1)):
l = []
for j in range(H-1):
if (i>>j)&1:
l.append(j+1)
h_split_num = len(l)
l = [0] + l + [H]
x = solve(l, 0, K)
w_split_num = 0
flag = True
while x < W:
w_split_num += 1
r = solve(l, x, K)
if x == r:
flag = False
break
x = r
if flag:
split_num = w_split_num + h_split_num
min_num = min(split_num, min_num)
print(min_num)
|
(x, y, a, b, c), p, q, r = [map(int, o.split()) for o in open(0)]
print(sum(sorted(sorted(p)[-x:] + sorted(q)[-y:] + list(r))[-x-y:]))
| 0 | null | 46,702,224,740,528 | 193 | 188 |
n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7
pows = [pow(2, i, mod) for i in range(60)]
count = [0]*60
num = [0]*60
ans = 0
for i in range(n):
bit = a[i]
for g in range(60):
if bit % 2 == 1:
count[g] += 1
num[g] += i+1 - count[g]
else:
num[g] += count[g]
bit //= 2
for i in range(60):
ans += num[i]*pows[i]
print(ans % mod)
|
#import sys
#input = sys.stdin.readline
M = 10**9+7
n = int(input())
l = list(map(int,input().split()))
o = [0]*61
z = [0]*61
for i in l:
a = bin(i)[2:]
j = 0
while(j < len(a)):
if a[j] == "1":
o[len(a)-j-1] += 1
j += 1
cnt = 0
b = 1
for j in range(len(o)):
cnt = (cnt+(o[j]*(n-o[j])*b)%M)%M
b *= 2
print(cnt)
| 1 | 123,044,576,086,788 | null | 263 | 263 |
n,x,y=map(int,input().split())
count=[0]*(n-1)
for i in range(n):
l=i
for j in range(i+1,n):
r=j
dis=min(j-i, abs(x-1-l)+1+abs(y-1-r))
count[dis-1]+=1
for i in range(n-1):
print(count[i])
|
def divide(a, b):
if a < b:
return divide(b, a)
if b == 0:
return a
return divide(b, a%b)
nums=list(map(int,input().split()))
ans = divide(nums[0], nums[1])
print(ans)
| 0 | null | 22,087,377,701,240 | 187 | 11 |
import bisect
n=int(input())
l=sorted(list(map(int,input().split())))
ans=0
for i in range(n):
for j in range(i+1,n):
index=bisect.bisect_left(l,l[i]+l[j])
ans+=index-j-1
print(ans)
|
import itertools
import bisect
n = int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0
for i in range(len(l)-2):
for j in range(i+1,len(l)):
k = bisect.bisect_left(l, l[i]+l[j])
if k > j:
ans += k - j -1
print(ans)
| 1 | 172,409,012,694,150 | null | 294 | 294 |
H, W, K = map(int, input().split())
S = [input() for _ in range(H)]
def calc(L):
M = len(L)
cnt = [0] * M
ret = 0
for w in range(W):
for i, (t, b) in enumerate(zip(L, L[1:])):
for j in range(t, b):
if S[j][w] == '1':
cnt[i] += 1
if max(cnt) > K:
cnt = [0] * M
ret += 1
for i, (t, b) in enumerate(zip(L, L[1:])):
for j in range(t, b):
if S[j][w] == '1':
cnt[i] += 1
if max(cnt) > K:
return 10**18
return ret
ans = 10**18
for mask in range(1 << (H - 1)):
L = [0] + [d + 1 for d in range(H - 1) if (mask & (1 << d)) > 0] + [H]
ans = min(ans, len(L) - 2 + calc(L))
print(ans)
|
'''
'''
INF = float('inf')
def main():
H, W, K = map(int, input().split())
S = [input() for _ in range(H)]
S = [''.join(S[row][col] for row in range(H)) for col in range(W)]
ans = INF
for b in range(0, 2**H, 2):
sections = [(0, H)]
for shift in range(1, H):
if (1<<shift) & b:
sections.append((sections.pop()[0], shift))
sections.append((shift, H))
t_ans = len(sections) - 1
count = {(l, r): 0 for l, r in sections}
for col in range(W):
if any(count[(l, r)] + S[col][l:r].count('1') > K for l, r in sections):
for key in count.keys():
count[key] = 0
t_ans += 1
for l, r in sections:
count[(l, r)] += S[col][l:r].count('1')
if max(count.values()) > K:
t_ans = INF
break
ans = min(ans, t_ans)
print(ans)
main()
| 1 | 48,790,631,850,390 | null | 193 | 193 |
while True:
a, b = map(int, input().split())
if a or b:
for i in range(0, a):
for j in range(0, b):
print("#", end = '')
print()
print()
else:
break
|
A,B,C,K = map(int,input().split())
if K<=A:
Ans = K
elif K<=A+B:
Ans = A
elif K<=A+B+C:
Ans = A-(K-A-B)
else:
Ans = A-C
print(Ans)
| 0 | null | 11,324,471,842,642 | 49 | 148 |
n = int(input())
a = list(map(int,input().split()))
b = 0
d = sum(a)
for m in range(n-1):
d = d -a[m]
b = b + a[m]*d
if b >= 10**9+7:
b = b % (10**9+7)
print(b)
|
t1,t2 = map(int,input().split())
a1,a2 = map(int,input().split())
b1,b2 = map(int,input().split())
if (a1<b1 and a2<b2) or (a1>b1 and a2>b2):
print(0)
exit()
if a1 > b1:
a1,b1 = b1,a1
a2,b2 = b2,a2
_b1 = b1 - a1
_a2 = a2 - b2
v1 = t1*_b1
v2 = t2*_a2
if v1 == v2:
print('infinity')
exit()
if v1 > v2:
print(0)
exit()
d = v2 - v1
k = v1 // d
ans = k*2 + 1
if v2*k == v1*(k+1):
ans -= 1
print(ans)
| 0 | null | 67,448,739,988,476 | 83 | 269 |
import bisect
N=int(input())
S=list(input())
Q=int(input())
alphabet="abcdefghijklmnopqrstuvwxyz"
bag={l:[] for l in alphabet}
for i in range(N):
bag[S[i]].append(i+1)
#print(bag)
for i in range(Q):
Type,a,b=input().split()
if Type=='1':
a=int(a)
if S[a-1]!=b:
bag[S[a-1]].remove(a)
S[a-1]=b
bisect.insort(bag[b],a)
else:
cnt=0
a,b=int(a),int(b)
for l in alphabet:
bl=len(bag[l])
if bl>0:
left=bisect.bisect_left(bag[l],a)
if left!=bl and bag[l][left]<=b:
cnt+=1
print(cnt)
|
import sys
def input(): return sys.stdin.readline().rstrip()
def ctoi(c):
return ord(c)-97
class BIT:
def __init__(self, n):
self.unit_sum=0 # to be set
self.n=n
self.dat=[0]*(n+1)#[1,n]
def add(self,a,x): #a(1-)
i=a
while i<=self.n:
self.dat[i]+=x
i+=i&-i
def sum(self,a,b=None):
if b!=None:
return self.sum(b-1)-self.sum(a-1) #[a,b) a(1-),b(1-)
res=self.unit_sum
i=a
while i>0:
res+=self.dat[i]
i-=i&-i
return res #Σ[1,a] a(1-)
def __str__(self):
self.ans=[]
for i in range(1,self.n):
self.ans.append(self.sum(i,i+1))
return ' '.join(map(str,self.ans))
def main():
n=int(input())
S=list(input())
q=int(input())
BIT_tree=[]
for i in range(26):
obj=BIT(n+1)
BIT_tree.append(obj)
for i in range(n):
BIT_tree[ctoi(S[i])].add(i+1,1)
for _ in range(q):
query=input().split()
if query[0]=="1":
index,x=int(query[1])-1,query[2]
BIT_tree[ctoi(S[index])].add(index+1,-1)
BIT_tree[ctoi(x)].add(index+1,1)
S[index]=x
else:
l,r=int(query[1])-1,int(query[2])-1
ans=0
for c in range(26):
if BIT_tree[c].sum(l+1,r+2):
ans+=1
print(ans)
if __name__=='__main__':
main()
| 1 | 62,601,586,917,870 | null | 210 | 210 |
x,y=map(int,input().split())
X=max(x,y)
Y=min(x,y)
def cmb(n, r, mod):
if ( r<0 or r>n ):
return 0
r = min(r, n-r)
return g1[n] * g2[r] * g2[n-r] % mod
mod = 10**9+7 #出力の制限
N = 10**6+1
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル
for i in range( 2, N + 1 ):
g1.append( ( g1[-1] * i ) % mod )
inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
g2.append( (g2[-1] * inverse[-1]) % mod )
if (X+Y)%3!=0:
print(0)
else:
if X>(Y*2):
print(0)
else:
N=(X+Y)//3
H=(N-(X-Y))//2
print(cmb(N,H,10**9+7))
#print(N,H)
|
from functools import lru_cache
MOD = 10**9+7
x,y = map(int, input().split())
summ = x+y
@lru_cache(maxsize=None)
def inv(n):
return pow(n,-1,MOD)
if summ%3 == 0 and summ//3 <= x and summ//3 <= y:
mn = min(x,y)
n = mn - summ//3
a = summ//3
b = 1
ans = 1
for i in range(n):
ans *= a
ans *= inv(b)
ans %= MOD
a -= 1
b += 1
print(ans)
else:
print(0)
| 1 | 150,239,285,084,660 | null | 281 | 281 |
import collections
n = int(input())
a = list(map(int,input().split()))
b = collections.Counter(a)
for d in range(n):
print(b[d+1])
|
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)
X = int(input())
from math import gcd
print(360*X//gcd(360, X)//X)
| 0 | null | 22,784,832,937,230 | 169 | 125 |
K,N = map(int,input().split())
A = list(map(int,input().split()))
max_range = 0
for i in range(N):
if i == N-1:
if max_range < K-A[i]+A[0]:
max_range = K-A[i]+A[0]
else:
if max_range < A[i+1]-A[i]:
max_range = A[i+1]-A[i]
ans = K - max_range
print(ans)
|
import math
N = int(input())
s = 0
for a in range(1, N+1):
s += (N-1)//a
print(s)
| 0 | null | 22,987,347,097,860 | 186 | 73 |
lis = [ ]
for i in range ( 10 ):
lis.append ( int ( input ( ) ) )
lis.sort ( )
for i in range ( 3 ):
print ( lis.pop ( ) )
|
a=[]
while 1:
try:
a.append(int(input()))
except: break
a.sort(reverse=1)
for i in range(3):print(a[i])
| 1 | 30,153,292 | null | 2 | 2 |
def fibonacci(n):
if n == 0 or n == 1:
F[n] = 1
return F[n]
if F[n] is not None:
return F[n]
F[n] = fibonacci(n - 1) + fibonacci(n - 2)
return F[n]
n = int(input())
F = [None]*(n + 1)
print(fibonacci(n))
|
K, N = map(int, input().split())
A = list(map(int, input().split()))
res = A[0] + (K - A[N-1])
for i in range(1, N):
dist = A[i] - A[i-1]
if res < dist:
res = dist
print(K - res)
| 0 | null | 21,681,146,050,058 | 7 | 186 |
k = int(input())
n = 7
for i in range(k):
if n % k == 0:
print(i+1)
exit()
n %= k
n = (n*10+7) % k
print(-1)
|
while True:
H,W = map(int,input().split())
if H == 0 and W == 0:
break
a = [["#" for j in range(W)] for i in range(H)]
b = ["".join(a[i]) for i in range(H)]
for i in range(H):
print(b[i])
print()
| 0 | null | 3,422,335,543,068 | 97 | 49 |
N = int(input())
abc_list = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z']
abc = ''
while(N>26):
n = N%26
N = N//26
if n==0:
abc+=abc_list[25]
N = N-1
else:
abc+=abc_list[n-1]
abc+=abc_list[N-1]
print(abc[::-1])
|
n, k = map(int, input().split())
m = 0
while True:
m += 1
if k**m > n:
print(m)
break
| 0 | null | 38,261,230,672,138 | 121 | 212 |
MON = list(map(int,input().split()))
while 1:
MON[2] = MON[2] - MON[1]
if(MON[2] <= 0):
print("Yes")
break
MON[0] = MON[0] - MON[3]
if (MON[0] <= 0 ):
print("No")
break
|
n = int(input())
fib = []
ans = 0
for i in range(n+1):
if i == 0 or i == 1:
ans = 1
fib.append(ans)
else:
ans = fib[i-1] + fib[i-2]
fib.append(ans)
print(ans)
| 0 | null | 14,779,028,747,482 | 164 | 7 |
import sys
inf = 1<<30
def solve():
n, m = map(int, input().split())
c = [int(i) for i in input().split()]
c.sort()
dp = [inf] * (n + 1)
dp[0] = 0
for i in range(1, n + 1):
for cj in c:
if i - cj < 0:
break
dp[i] = min(dp[i], dp[i - cj] + 1)
ans = dp[n]
print(ans)
if __name__ == '__main__':
solve()
|
n, m =map(int,input().split())
c = list(map(int,input().split()))
INF = 10**100
dp = [INF]*500000
dp[0] = 0
for i in range (n+1):
for j in c:
dp[i+j] = min(dp[i+j],dp[i]+1)
print(dp[n])
| 1 | 135,087,659,460 | null | 28 | 28 |
def f(x, m):
return (x**2) % m
N, X, M = map(int, input().split())
A = [X]
S = {X}
while True:
X = f(X, M)
if X in S:
break
else:
A.append(X)
S.add(X)
start = A.index(X)
l = len(A) - start
ans = sum(A[:start])
N -= start
ans += sum(A[start:]) * (N//l)
N %= l
ans += sum(A[start:start+N])
print(ans)
|
from math import log2
n,x,m = map(int, input().split())
logn = int(log2(n))+1
db = [[0]*m for _ in range(logn)]
dbs = [[0]*m for _ in range(logn)]
for i in range(m):
db[0][i] = (i**2)%m
dbs[0][i] = (i**2)%m
for i in range(logn-1):
for j in range(m):
db[i+1][j] = db[i][db[i][j]]
dbs[i+1][j] = dbs[i][db[i][j]] + dbs[i][j]
ans = x
now = x
for i in range(logn):
if (n-1)&(1<<i) > 0:
ans += dbs[i][now]
now = db[i][now]
print(ans)
| 1 | 2,844,242,492,742 | null | 75 | 75 |
import numpy as np
n,m = map(int, input().split())
h = np.array([int(i) for i in input().split()])
h_i = np.ones((n))
for i in range(m):
a,b = map(int, input().split())
if h[a-1] < h[b-1]:
h_i[a-1] = 0
elif h[b-1] < h[a-1]:
h_i[b-1] = 0
elif h[a-1] == h[b-1]:
h_i[a-1] = 0
h_i[b-1] = 0
ans = (h_i > 0).sum()
print(ans)
|
n, m = map(int, input().split())
h_list = list(map(int, input().split()))
min_list = [1] * n
for _ in range(m):
a, b = map(lambda x : int(x) - 1, input().split())
if h_list[a] <= h_list[b]:
min_list[a] = 0
if h_list[b] <= h_list[a]:
min_list[b] = 0
print(sum(min_list))
| 1 | 24,922,740,834,432 | null | 155 | 155 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def main():
N = int(input())
ans = [0] * (6 * 10**4 + 10)
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 + x * z - 1] += 1
for i in range(N):
print(ans[i])
if __name__ == "__main__":
main()
|
A,B=map(int,input().split())
c=-1
for i in range(1,10**5):
if int(i*0.08)==A and int(i*0.1)==B:
c=i
break
print(c)
| 0 | null | 32,374,436,270,652 | 106 | 203 |
"""
時間がかかる料理は、食べるとしたら最後に食べるのがベスト。
逆にいうと、その料理を食べるかどうかを検討する段階ではほかの料理は食べるかどうかの検討は終わっている状態。
なので、食べるのにかかる時間でソートしてナップサックすればよい。
"""
N,T = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N)]
dp = [0]*(T)
AB.sort()
ans = 0
for i in range(N):
a,b = AB[i]
ans = max(ans,dp[-1]+b)
for j in range(T-1,-1,-1):
if j-a >= 0:
dp[j] = max(dp[j],dp[j-a]+b)
print(ans)
|
N,T = map(int,input().split())
tlist=[0]*N
vlist=[0]*N
tvlist=[[0,0]]*N
dp=[[0]*T for i in range(N+1)]
for i in range(N):
tvlist[i] = list(map(int,input().split()))
tvlist.sort(key=lambda x: x[0])
for i in range(N):
tlist[i] = tvlist[i][0]
vlist[i] = tvlist[i][1]
for i in range(N):
for t in range(T):
if t>=tlist[i]:
dp[i+1][t] = max(dp[i][t],dp[i][t-tlist[i]]+vlist[i])
else:
dp[i+1][t] = dp[i][t]
ans=0
for i in range(N):
ans = max(ans,dp[i][T-1]+vlist[i])
print(ans)
| 1 | 151,132,522,666,680 | null | 282 | 282 |
N = int(input())
if N%1000 == 0:
print(0)
else:
print(1000-(N%1000))
|
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")
s = input()
n = len(s)
a = [None]*n
c = 0
v = 1
for i in range(n-1, -1, -1):
c = (c + v*int(s[i])) % 2019
a[i] = c
v *= 10
v %= 2019
from collections import Counter
cc = Counter(a)
ans = 0
for k,v in cc.items():
ans += v*(v-1)//2
ans += cc[0]
print(ans)
| 0 | null | 19,692,163,748,352 | 108 | 166 |
import numpy as np
def convolve(A, B):
# 畳み込み # 要素は整数
# 3 つ以上の場合は一度にやった方がいい
dtype = np.int64
fft, ifft = np.fft.rfft, np.fft.irfft
a, b = len(A), len(B)
if a == b == 1:
return np.array([A[0]*B[0]])
n = a+b-1 # 返り値のリストの長さ
k = 1 << (n-1).bit_length()
AB = np.zeros((2, k), dtype=dtype)
AB[0, :a] = A
AB[1, :b] = B
return np.rint(ifft(fft(AB[0]) * fft(AB[1]))).astype(np.int64)[:n]
import sys
input = sys.stdin.readline
n,m = map(int, input().split())
a = list(map(int, input().split()))
cnt = np.zeros(100001)
for i in a:
cnt[i] += 1
c = convolve(cnt,cnt)
ans = 0
for i in range(len(c))[::-1]:
if c[i] > 0:
p = min(m,c[i])
m -= p
ans += i*p
if m == 0:
break
print(ans)
|
f=lambda:map(int,input().split())
N,K=f()
r,s,p=f()
T=list(input())
F=[0]*N
res=0
for i in range(N):
if F[i]==1:
continue
if i+K<N:
if T[i]==T[i+K]:
F[i+K]=1
if T[i]=='s':
res+=r
elif T[i]=='p':
res+=s
else:
res+=p
print(res)
| 0 | null | 107,709,933,381,118 | 252 | 251 |
n = int(input())
#lis = list(map(int,input().split()))
for i in range(1,10):
q = n // i
r = n % i
if r == 0 and q < 10:
print("Yes")
exit()
print("No")
|
N, X, Y = map(int, input().split())
ans = [0] * (N + 1)
for i in range(1, N):
for j in range(i+1, N+1):
dis1 = j-i
dis2 = abs(X-i) + 1 + abs(Y - j)
dis3 = abs(Y-i) + 1 + abs(X - j)
d = min(dis1,dis2,dis3)
ans[d] += 1
for i in range(1, N):
print(ans[i])
| 0 | null | 101,752,317,240,300 | 287 | 187 |
# coding: utf-8
def main():
n = int(input())
a_list = list(map(int, input().split()))
a_sum = sum(a_list)
ans = 0
for i in range(n-1):
a_head = a_list[i]
a_sum -= a_head
ans += a_head * a_sum
print(ans % (10**9 + 7))
main()
|
N = int(input())
A = list(map(int, input().split()))
a = [0]
for i in range(N):
a.append(a[i] + A[i])
cnt = 0
for i in range(N-1):
cnt += A[i] * (a[N] - a[i+1])
mod = 10 ** 9 + 7
ans = cnt % mod
print(ans)
| 1 | 3,813,302,262,400 | null | 83 | 83 |
S = input()
T = input()
i=0
j=0
for i in range (len(S)):
if S[i] != T[i]:
j += 1
print(j)
|
w = input()
t = []
while True:
s = [1 if i.lower() == w else 'fin' if i == 'END_OF_TEXT' else 0 for i in input().split()]
t += s
if 'fin' in s:
break
print(t.count(1))
| 0 | null | 6,109,714,197,070 | 116 | 65 |
import sys
# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
# import heapq
import decimal
# import statistics
import queue
# import numpy as np
sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353
ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
# ===CODE===
def main():
n, m = ns()
c = na()
dp = [INF for _ in range(n + 1)]
dp[0] = 0
for ci in c:
for i in range(n + 1 - ci):
dp[i + ci] = min(dp[i + ci], dp[i] + 1)
print(dp[n])
if __name__ == '__main__':
main()
|
mountains = [int(input()) for _ in range(10)]
for height in sorted(mountains)[-3:][::-1]:
print(height)
| 0 | null | 69,900,995,516 | 28 | 2 |
import os, sys, re, math
A = [int(n) for n in input().split()]
print('win' if sum(A) <= 21 else 'bust')
|
MOD = 998244353
factorial = None
inverse_factorial = None
def modpow(a, p):
ans = 1
while p:
if p&1 == 1:
ans = (ans*a)%MOD
a = (a*a)%MOD
p >>= 1
return ans
def nCr(n, r):
if r == 0 or r == n:
return 1
return (((factorial[n]*inverse_factorial[n-r])%MOD)*inverse_factorial[r])%MOD
def init_nCr(max_n):
global factorial, inverse_factorial
factorial = [1]*(max_n+1)
inverse_factorial = [0]*(max_n+1)
for i in range(1, max_n+1):
factorial[i] = (factorial[i-1]*i)%MOD
inverse_factorial[i] = modpow(factorial[i], MOD-2)
init_nCr(2*10**5+1)
def read_int():
return int(input().strip())
def read_ints():
return list(map(int, input().strip().split(' ')))
def solve():
N, M, K = read_ints()
answer = 0
for k in range(0, K+1):
answer = (answer+M*modpow(M-1, N-1-k)*nCr(N-1, k))%MOD
return answer
if __name__ == '__main__':
print(solve())
| 0 | null | 70,828,225,358,462 | 260 | 151 |
H1, M1, H2, M2, K = map(int, input().split())
print(60 * (H2 - H1) + M2 - M1 - K)
|
h1,m1,h2,m2,k = map(int, input().split())
t1 = h1 * 60 + m1
t2 = h2 * 60 + m2
if t2 > t1:
awake = t2 - t1
else:
awake = 24 * 60 - t1 + t2
print(awake - k)
| 1 | 17,974,472,711,638 | null | 139 | 139 |
X = int(input())
q = X // 100
if X%100<=q*5:
print('1')
else:
print('0')
|
x = int(input())
xd = x//100
if xd*100 <= x <= xd*105:
print(1)
else:
print(0)
| 1 | 126,875,323,795,640 | null | 266 | 266 |
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**6)
INF = 10 ** 9 + 1 # sys.maxsize # float("inf")
MOD = 998244353
def debug(*x):
print(*x, file=sys.stderr)
def solve(N, K, SS):
count = [0] * (N + 10)
count[0] = 1
accum = [0] * (N + 10)
accum[0] = 1
for pos in range(1, N):
ret = 0
for left, right in SS:
start = pos - right - 1
end = pos - left
ret += (accum[end] - accum[start])
ret %= MOD
count[pos] = ret
accum[pos] = accum[pos - 1] + ret
ret = count[N - 1]
return ret % MOD
def main():
# parse input
N, K = map(int, input().split())
SS = []
for i in range(K):
SS.append(tuple(map(int, input().split())))
print(solve(N, K, SS))
# tests
T1 = """
5 2
1 1
3 4
"""
TEST_T1 = """
>>> as_input(T1)
>>> main()
4
"""
T2 = """
5 2
3 3
5 5
"""
TEST_T2 = """
>>> as_input(T2)
>>> main()
0
"""
T3 = """
5 1
1 2
"""
TEST_T3 = """
>>> as_input(T3)
>>> main()
5
"""
T4 = """
60 3
5 8
1 3
10 15
"""
TEST_T4 = """
>>> as_input(T4)
>>> main()
221823067
"""
T5 = """
10 1
1 2
"""
TEST_T5 = """
>>> as_input(T5)
>>> main()
55
"""
def _test():
import doctest
doctest.testmod()
g = globals()
for k in sorted(g):
if k.startswith("TEST_"):
doctest.run_docstring_examples(g[k], g, name=k)
def as_input(s):
"use in test, use given string as input file"
import io
f = io.StringIO(s.strip())
g = globals()
g["input"] = lambda: bytes(f.readline(), "ascii")
g["read"] = lambda: bytes(f.read(), "ascii")
input = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
if sys.argv[-1] == "-t":
print("testing")
_test()
sys.exit()
main()
|
n = int(input())
z = []
w = []
for i in range(n):
x,y = map(int,input().split())
z.append(x+y)
w.append(x-y)
zmax = max(z)-min(z)
wmax = max(w)-min(w)
print(max(zmax,wmax))
| 0 | null | 3,077,669,432,250 | 74 | 80 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.