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
|
---|---|---|---|---|---|---|
n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7
ans = 1
cnt = [0] * 3
for i in range(n):
t = 0
first = True
for j in range(3):
if cnt[j] == a[i]:
t += 1
if first:
first = False
cnt[j] += 1
ans = (ans * t) % mod
print(ans)
|
from sys import setrecursionlimit
setrecursionlimit(10 ** 6)
MAXN = 100
n = input()
k = int(input())
dl = [[None] * (MAXN + 1) for _ in range(MAXN + 1)]
de = [[None] * (MAXN + 1) for _ in range(MAXN + 1)]
def n_num_e(d, i):
# d: n of digits
# i: n of nonzero digits
if d == 0:
if i == 0:
return 1
else:
return 0
elif de[d][i] is not None:
return de[d][i]
else:
if int(n[d - 1]) == 0:
result = n_num_e(d - 1, i)
else:
result = n_num_e(d - 1, i - 1)
de[d][i] = result
return result
def n_num_l(d, i):
# d: n of digits
# i: n of nonzero digits
if d == 0:
return 0
elif dl[d][i] is not None:
return dl[d][i]
else:
if int(n[d - 1]) == 0:
ne = 0
nl = 9 * n_num_l(d - 1, i - 1) + n_num_l(d - 1, i)
result = ne + nl
else:
ne = (int(n[d - 1]) - 1) * n_num_e(d - 1, i - 1) + n_num_e(d - 1, i)
nl = 9 * n_num_l(d - 1, i - 1) + n_num_l(d - 1, i)
result = ne + nl
dl[d][i] = result
return result
answer = n_num_e(len(n), k) + n_num_l(len(n), k)
print(answer)
| 0 | null | 103,450,665,609,148 | 268 | 224 |
r, b = input().split()
a, b = map(int, input().split())
u = input()
if r == u:
print(a-1, b)
else:
print(a, b-1)
|
#!/usr/bin/env python3
# Generated by https://github.com/kyuridenamida/atcoder-tools
from typing import *
import collections
import functools
import itertools
import math
import sys
INF = float('inf')
def solve(S: str, T: str, A: int, B: int, U: str):
return f"{A-(S==U)} {B-(T==U)}"
def main():
sys.setrecursionlimit(10 ** 6)
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
S = next(tokens) # type: str
T = next(tokens) # type: str
A = int(next(tokens)) # type: int
B = int(next(tokens)) # type: int
U = next(tokens) # type: str
print(f'{solve(S, T, A, B, U)}')
if __name__ == '__main__':
main()
| 1 | 72,077,280,298,588 | null | 220 | 220 |
import math
cos60 = math.cos(math.pi*60/180)
sin60 = math.sin(math.pi*60/180)
#add_triangle
# input: start(x, y) end(x, y)
def add_triangle(start, end):
x1 = (2*start[0] + end[0]) / 3
x3 = (start[0] + 2*end[0]) / 3
y1 = (2*start[1] + end[1]) / 3
y3 = (start[1] + 2*end[1]) / 3
x2 = x1 + (x3-x1)*cos60 - (y3-y1)*sin60
y2 = y1 + (x3-x1)*sin60 + (y3-y1)*cos60
return [(x1,y1), (x2,y2), (x3,y3)]
#koch_recursion
#input list [(x1,y1),(x2,y2),(x3,y3)...]
def koch_recursion(list1):
output = []
for i in range(len(list1)-1):
output.append(list1[i])
output += add_triangle(list1[i], list1[i+1])
output.append(list1[len(list1)-1])
return output
#Koch call koch_recursion(0, 100)
def Koch(start, end, n):
output = [start, end]
for i in range(n):
output = koch_recursion(output)
for x, y in output:
print("{:.8f} {:.8f}".format(x, y))
n = int(input())
Koch((0,0), (100,0), n)
|
import heapq
n,k = map(int,input().split())
a = [int(i) for i in input().split()]
low = 0
high = 10**9
if k == 0:
print(max(a))
exit()
while low+1 < high:
tmp = 0
mid = (low + high) //2
for i in range(n):
tmp += ((a[i]+mid-1) // mid)-1
if tmp <= k:
high = mid
else:
low = mid
print(high)
| 0 | null | 3,276,526,245,668 | 27 | 99 |
d, t, s = map(int, input().split())
ans = "Yes" if d / s <= t else "No"
print(ans)
|
def MI(): return map(int, input().split())
D,T,S=MI()
if D<=T*S:
print('Yes')
else:
print('No')
| 1 | 3,541,859,394,484 | null | 81 | 81 |
s = raw_input()#,k = map(int, raw_input().split(' '))
print 'Yes ' if len(s) >= 6 and s[2] == s[3] and s[4] == s[5] else 'No' #f(n,k)
|
s=input()
count=0
list_s=list(s)
if(list_s[2]==list_s[3] and list_s[4]==list_s[5]):
print("Yes")
else:
print("No")
| 1 | 41,992,137,337,450 | null | 184 | 184 |
sentence = input().lower()
ans = input().lower()
doublesentence = sentence + sentence
print("Yes" if ans in doublesentence else "No")
|
# Ring
ring = input()
word = input()
doubleRing = ring * 2
ringCut = doubleRing.split(word)
# print(ringCut)
if ringCut[0] == doubleRing:
print('No')
else:
print('Yes')
| 1 | 1,759,565,216,028 | null | 64 | 64 |
SIZE=100001; MOD=10**9+7 #998244353 #ここを変更する
SIZE += 1
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
def choose(n,r): # nCk mod MOD の計算
if 0 <= r <= n:
return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
else:
return 0
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read
n,k = [int(i) for i in readline().split()]
a = [int(i) for i in readline().split()]
a.sort()
ans = 0
for i in range(n):
ans += a[i]*choose(i,k-1)
ans -= a[i]*choose(n-i-1,k-1)
ans %= MOD
print(ans)
|
N,D,A=map(int, input().split())
B=[list(map(int, input().split())) for _ in range(N)]
C=sorted(B)
d,E=zip(*C)
import bisect
Damage=[0]*N
for i in range(N):
e=bisect.bisect_right(d,d[i]+2*D)
Damage[i]=e
dd=[0]*(N+1)
cnt=0
for i in range(N):
if i!=0:
dd[i]+=dd[i-1]
h=E[i]
h-=dd[i]
if h>0:
bomb=-(-h//A)
cnt+=bomb
dd[i]+=A*bomb
dd[Damage[i]]-=A*bomb
print(cnt)
| 0 | null | 88,755,966,658,148 | 242 | 230 |
s=input()
n=0
m=0
for i in range(len(s)):
if s[i]=='S':
n=0
else:
n+=1
m=n
print(m)
|
S=input()
if S=="SSS":
print(0)
else:
if S=="RSS" or S=="SRS":
print(1)
else:
if S=="SSR" or S=="RSR":
print(1)
else:
if S=="RRS" or S=="SRR":
print(2)
else:
print(3)
| 1 | 4,943,211,563,038 | null | 90 | 90 |
# 解説放送の方針
# ダメージが消える右端をキューで管理
# 右端を超えた分は、累積ダメージから引いていく
from collections import deque
N,D,A=map(int,input().split())
XH = [tuple(map(int,input().split())) for _ in range(N)]
XH = sorted(XH)
que=deque()
cum = 0
ans = 0
for i in range(N):
x,h = XH[i]
if i == 0:
r,n = x+2*D,(h+A-1)//A
d = n*A
cum += d
que.append((r,d))
ans += n
continue
while que and que[0][0]<x:
r,d = que.popleft()
cum -= d
h -= cum
if h<0:
continue
r,n = x+2*D,(h+A-1)//A
d = n*A
cum += d
que.append((r,d))
ans += n
print(ans)
|
N = int(input())
X = list(map(int, input().split()))
HP = []
for p in range(1, 101, 1):
P = [p] * len(X)
delta = sum([(i - j) ** 2 for (i, j) in zip(X, P)])
HP.append(delta)
print(min(HP))
| 0 | null | 73,739,466,706,400 | 230 | 213 |
n = int(input())
r = []
for i in range(n):
r.append(int(input()))
min = r[0]
max = -10 ** 12
for j in r[1:]:
if j - min > max:
max = j - min
if min > j:
min = j
print(max)
|
n, m = map(int, input().split())
c = list(map(int, input().split()))
dp = [0] + [50000]*n
for i in range(m):
for j in range(c[i], n+1):
dp[j] = min(dp[j], dp[j-c[i]] + 1)
print(dp[n])
| 0 | null | 74,811,992,090 | 13 | 28 |
x = int(input())
print('Yes' if 30 <= x else 'No')
|
def main():
x = int(input())
print('Yes' if x >= 30 else 'No')
if __name__ == '__main__':
main()
| 1 | 5,724,159,755,948 | null | 95 | 95 |
a=list(map(int,input().split()))
c=0
for i in range(a[0],a[1]+1):
if i%a[2]==0:
c+=1
print(c)
|
A = []
for i in range(3):
A.append(list(map(int, input().split())))
B = [[0 for i in range(3)] for j in range(3)]
N = int(input())
for i in range(N):
b = int(input())
for j in range(3):
for k in range(3):
if A[j][k] == b:
B[j][k] = 1
c = []
for i in range(3):
c.append(B[i][0] & B[i][1] & B[i][2])
c.append(B[0][i] & B[1][i] & B[2][i])
c.append(B[0][0] & B[1][1] & B[2][2])
c.append(B[0][2] & B[1][1] & B[2][0])
if sum(c) > 0:
print("Yes")
else:
print("No")
| 0 | null | 33,896,487,358,528 | 104 | 207 |
n = int(input())
a = list(map(int, input().split(" ")))
for lil_a in a[-1:0:-1]:
print("%d "%lil_a, end="")
print("%d"%a[0])
|
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
from bisect import bisect_left
from itertools import product
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
#文字列を一文字ずつ数字に変換、'5678'を[5,6,7,8]とできる
def LSI(): return list(map(int, list(sys.stdin.readline().rstrip())))
def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)]
#文字列として取得
def ST(): return sys.stdin.readline().rstrip()
def LST(): return sys.stdin.readline().rstrip().split()
def LST2(N): return [sys.stdin.readline().rstrip().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**6+10)
N = I()
L = LI2(N)
A = [i[0] for i in L]
B = [i[1] for i in L]
A.sort()
B.sort()
if N%2==0:
medA = (A[N//2-1]+A[N//2])
medB = (B[N//2-1]+B[N//2])
print((medB-medA)+1)
else:
medA = A[(N-1)//2]
medB = B[(N-1)//2]
print(medB-medA+1)
| 0 | null | 9,189,210,948,232 | 53 | 137 |
N=list(input())
K=int(input())
N=list(map(int,N))
#print(N)
dp=[[[0 for _ in range(4)] for _ in range(len(N))] for _ in range(2)]
for i in range(len(N)):
num=N[i]
mayover=0
if i==0:
for j in range(10):
if j<num:
if j==0:
dp[0][i][0]=dp[0][i][0]+1
else:
dp[0][i][1] = dp[0][i][1] + 1
elif j==num:
if j==0:
dp[1][i][0] = dp[1][i][0] + 1
else:
dp[1][i][1] = dp[1][i][1] + 1
else:
for j in range(2):
if j==0:
for l in range(4):
dp[j][i][l] = dp[j][i][l] + dp[j][i - 1][l]
if l != 0:
dp[j][i][l] = dp[j][i][l] + 9*dp[j][i - 1][l - 1]
else:
if num==0:
for l in range(4):
dp[j][i][l]=dp[j][i-1][l]
else:
for l in range(4):
dp[0][i][l]=dp[0][i][l]+dp[j][i-1][l]
if l!=0:
dp[0][i][l]=dp[0][i][l]+(num-1)*dp[j][i-1][l-1]
dp[1][i][l]=dp[1][i][l]+dp[j][i-1][l-1]
#print(dp[0][i],dp[1][i])
#input()
print(dp[0][len(N)-1][K]+dp[1][len(N)-1][K])
|
import math
n = int(input())
K = int(input())
def calk1(n):
if n == 0: return 0
m = int(math.log10(n))
num = m * 9
num += n//(10**m)
return num
def calk2(n):
m = int(math.log10(n))
num = m * (m - 1) // 2 * 9**2
num += (n//(10**m)-1) * m * 9
num += calk1(n - (n//(10**m)*10**m))
return num
def calk3(n):
m = int(math.log10(n))
num = m * (m - 1) * (m - 2) // 6 * 9**3
num += (n//(10**m)-1) * (m * (m - 1) // 2 * 9**2)
num += calk2(n - (n//(10**m)*10**m))
return num
if n < 10 ** (K - 1): print(0)
else:
if K == 1: print(calk1(n))
elif K == 2: print(calk2(n))
else: print(calk3(n))
| 1 | 75,802,078,276,978 | null | 224 | 224 |
n = int(input())
s = input()
ok = [0,0,0,0,0,0,0,0,0,0]
ans = 0
for i in range(n):
if sum(ok) == 10:
break
if ok[int(s[i])] == 1:
continue
ok[int(s[i])] = 1
nd = [0,0,0,0,0,0,0,0,0,0]
for j in range(i+1,n):
if sum(nd) == 10:
break
if nd[int(s[j])] == 1:
continue
nd[int(s[j])] = 1
rd = [0,0,0,0,0,0,0,0,0,0]
for k in range(j+1, n):
if sum(rd) == 10:
break
if rd[int(s[k])] == 1:
continue
rd[int(s[k])] = 1
ans += 1
print(ans)
|
import sys
def gcd(a, b):
while b % a:
a, b = b % a, a
return a
def lcm(a, b):
return a * b // gcd(a, b)
for line in sys.stdin:
a, b = sorted(list(map(int, line.split())))
print(gcd(a, b), lcm(a, b))
| 0 | null | 64,130,048,484,550 | 267 | 5 |
turn = int(input())
tp, hp = 0, 0
for i in range(turn):
t, h = input().split()
if t == h:
tp += 1
hp += 1
elif t > h:
tp += 3
elif t < h:
hp += 3
print(tp, hp)
|
from math import cos , sin, sqrt, radians
import sys
A , B , H , M = list( map( int, input().split() ) )
th1 = H * 30 + 0.5 * M
th2 = M * 6
theta = min( abs( th1 - th2 ) , 360 - abs( th1 - th2 ))
if theta == 180.0:
print(A+B)
sys.exit()
theta = radians(theta)
ans = sqrt( (B * sin(theta))**2 + ( B*cos(theta) - A )**2 )
print(ans)
| 0 | null | 10,964,924,896,240 | 67 | 144 |
import sys
# C - Ubiquity
N = int(input())
all = 1
no_1 = 1
no_1_and_9 = 1
mod = 10 ** 9 + 7
for _ in range(N):
all *= 10
all %= mod
no_1 *= 9
no_1 %= mod
no_1_and_9 *= 8
no_1_and_9 %= mod
print((all - (no_1 * 2 - no_1_and_9)) % mod)
|
cnt=0
n=int(input())
S=[0 for i in range(n)]
S=input().split()
q=int(input())
T=[0 for i in range(q)]
T=input().split()
for i in range(q):
p=0
for j in range(n):
if T[i]==S[j]:
if p==0:
cnt+=1
p+=1
print(cnt)
| 0 | null | 1,607,552,436,606 | 78 | 22 |
s = input()
partition = s.replace('><','>|<').split('|')
ans=0
for sub in partition:
left = sub.count('<')
right = sub.count('>')
ans += sum(range(1, max(left, right) + 1))
ans += sum(range(1, min(left, right)))
print(ans)
|
n,k = map(int, input().split())
num = []
for i in range(k):
d = int(input())
num += list(map(int, input().split()))
Q = list(set(num))
print(n-len(Q))
| 0 | null | 90,550,225,272,638 | 285 | 154 |
def gcd(_a, _b):
if _a%_b==0:return _b
else:return gcd(_b, _a%_b)
A, B=map(int, input().split())
print(A*B//gcd(A, B))
|
S=[]
T=[]
S_num=int(input())
S=input().split()
T_num=int(input())
T=input().split()
cnt=0
for i in range(T_num):
for j in range(S_num):
if(S[j]==T[i]):
cnt+=1
break
print(cnt)
| 0 | null | 56,891,424,842,980 | 256 | 22 |
import math
A, B = map(int, input().split())
AA = A * 100 / 8
BB = B * 100 / 10
L = []
for i in range(10000):
if math.floor(i * 0.08) == A and math.floor(i * 0.1) == B:
L.append(i)
L = sorted(L)
if L == []:
print(-1)
exit()
print(L[0])
|
import math
A,B = map(int, input().split())
a1 = math.ceil(A / 0.08)
a2 = math.floor((A+1) / 0.08)
b1 = math.ceil(B / 0.10)
b2 = math.floor((B+1) / 0.10)
dupl = set(range(a1, a2)) & set(range(b1, b2))
if len(dupl) > 0:
print(min(dupl))
else:
print(-1)
| 1 | 56,531,011,826,062 | null | 203 | 203 |
import fractions
n,num=map(int,input().split())
num_set=set(map(lambda x:int(x),input().split()))
two_times_set=set()
for i in num_set:
for j in range(1,30):
i//=2
if i%2!=0:
two_times_set.add(j)
break
if len(two_times_set)!=1:
print(0)
break
else:
num_list=list(num_set)
lcm=num_list[0]
for i in range(1,len(num_list)):
lcm = lcm * num_list[i] // fractions.gcd(lcm, num_list[i])
#print((num-lcm//2)//(lcm)+1)
lcm//=2
print(int(((num/lcm)+1)/2))
|
# 最小公倍数(mathは3.5以降) fractions
from functools import reduce
import fractions #(2020-0405 fractions→math)
def lcm_base(x, y):
return (x * y) // fractions.gcd(x, y) # 「//」はフロートにせずにintにする
def lcm(*numbers):
return reduce(lcm_base, numbers, 1)
def lcm_list(numbers):
return reduce(lcm_base, numbers, 1)
N,M = (int(x) for x in input().split())
A = list(map(int, input().split()))
lcm =lcm_list(A)
harf_lcm =lcm//2 # 最小公倍数の半分
harf_lcm_num =M//harf_lcm # Mの中の半公倍数の個数
harf_lcm_num =(harf_lcm_num +1)//2 # 偶数を除く
#半公倍数がaiで割り切れるときは✖
import numpy as np
A_np =np.array(A)
if np.any(harf_lcm % A_np ==0):
harf_lcm_num =0
"""
for a in A:
if harf_lcm % a ==0:
harf_lcm_num =0
break
"""
print(harf_lcm_num)
| 1 | 101,495,281,444,662 | null | 247 | 247 |
x = int(input())
a = x // 500
x = x - a * 500
b = x // 5
print(1000*a+5*b)
|
x = int(input())
ans, r = divmod(x, 500)
ans *= 1000
ans += (r//5)*5
print(ans)
| 1 | 42,565,442,145,150 | null | 185 | 185 |
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
xyc= [list(map(int, input().split())) for _ in range(M)]
minmoney = min(a) + min(b)
for obj in xyc:
summoney = a[obj[0] - 1] + b[obj[1] - 1] - obj[2]
minmoney = min(minmoney, summoney)
print(minmoney)
|
a, b, m = map(int, input().split())
re = list(map(int, input().split()))
de = list(map(int, input().split()))
mre = min(re)
mde = min(re)
list1 = []
for i in range(m):
x, y, c = map(int, input().split())
price = re[x-1] + de[y-1] -c
list1.append(price)
mlist = min(list1)
m1 = mre + mde
if m1 < mlist:
print(m1)
else:
print(mlist)
| 1 | 53,743,989,043,820 | null | 200 | 200 |
import sys
from heapq import heappop, heappush
from scipy.sparse.csgraph import floyd_warshall
input = sys.stdin.readline
def main():
n,w,l = map(int,input().split())
d = [[10**15 for _ in range(n)] for _ in range(n)]
for _ in range(w):
x,y,z = map(int,input().split())
x -= 1
y -= 1
if z > l:
continue
d[x][y] = z
d[y][x] = z
for i in range(n):
d[i][i] = 0
d = floyd_warshall(d)
G = [[10**15 for _ in range(n)] for _ in range(n)]
for i in range(n-1):
for j in range(i+1,n):
if d[i][j] <= l:
G[i][j] = 1
G[j][i] = 1
for i in range(n):
G[i][i] = 0
G = floyd_warshall(G)
q = int(input())
for _ in range(q):
s,t = map(int,input().split())
s -= 1
t -= 1
ans = G[s][t]
if ans == 10**15:
print(-1)
continue
print(int(ans)-1)
if __name__ == "__main__":
main()
|
def main():
s = list(input())
for i in range(len(s)):
s[i] = "x"
L = "".join(s)
print(L)
if __name__ == "__main__":
main()
| 0 | null | 123,301,675,741,972 | 295 | 221 |
def prepare(n):
global MOD
modFacts = [0] * (n + 1)
modFacts[0] = 1
for i in range(n):
modFacts[i + 1] = (modFacts[i] * (i + 1)) % MOD
invs = [1] * (n + 1)
invs[n] = pow(modFacts[n], MOD - 2, MOD)
for i in range(n, 1, -1):
invs[i - 1] = (invs[i] * i) % MOD
return modFacts, invs
MOD = 10 ** 9 + 7
K = int(input())
S = input()
L = len(S)
modFacts, invs = prepare(K + L)
pow25 = [1] * max(L + 1, K + 1)
pow26 = [1] * max(L + 1, K + 1)
for i in range(max(L, K)):
pow25[i + 1] = (pow25[i] * 25) % MOD
pow26[i + 1] = (pow26[i] * 26) % MOD
ans = 0
for n in range(L, L + K + 1):
nonSi = (pow25[n - L] * pow26[L + K - n]) % MOD
Si = (modFacts[n - 1] * invs[L - 1] * invs[n - 1 - (L - 1)]) % MOD
ans += nonSi * Si
ans %= MOD
print(ans)
|
import sys
input = sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def main():
mod=10**9+7
N=I()
M=65
A=LI()
cnt=[0]*M
for i in range(N):
a=A[i]
for j in range(M):
if a>>j & 1:
cnt[j]+=1
ans=0
for i in range(M):
a=cnt[i]
b=N-a
ans=(ans + a*b*pow(2,i,mod))%mod
print(ans)
main()
| 0 | null | 67,493,403,868,480 | 124 | 263 |
# coding:utf-8
# ????????????
s = [False] * 13
# ?????????
h = [False] * 13
# ?????????
c = [False] * 13
# ?????????
d = [False] * 13
T = {'S':s, 'H':h, 'C':c, 'D':d}
# ??\???
n = int(input())
for i in range(n):
suit, num = input().split()
num = int(num)
T[suit][num-1] = True
for v in range(13):
if not(T['S'][v]):
print('S', v + 1)
for v in range(13):
if not(T['H'][v]):
print('H', v + 1)
for v in range(13):
if not(T['C'][v]):
print('C', v + 1)
for v in range(13):
if not(T['D'][v]):
print('D', v + 1)
|
from itertools import product
(lambda *_: None)(
*(lambda full, hand: map(print,
filter(lambda c: c not in hand, full)))(
map(' '.join, product('SHCD', map(str, range(1, 14)))),
set(map(lambda _: input(), range(int(input()))))))
| 1 | 1,045,539,488,222 | null | 54 | 54 |
N,K = map(int,input().split())
A = sorted(list(map(int,input().split())))
mod = 10**9+7
ans = 1
mi = []
pl = []
for a in A:
if a < 0:
mi.append(a)
else:
pl.append(a)
if N == K:
for a in A:
ans *= a
ans %= mod
print(ans)
exit()
if pl == []:
if K%2:
for k in range(K):
ans *= mi[-1-k]
ans %= mod
else:
for k in range(K):
ans *= mi[k]
ans %= mod
print(ans)
exit()
pl = pl[::-1]
ip = 0
im = 0
if K % 2:
ans = pl[0]
ip += 1
K -= 1
while K > 0:
check = True
if ip + 2 <= len(pl):
x = pl[ip]*pl[ip+1]
else:
x = 1
if im + 2 <= len(mi):
y = mi[im]*mi[im+1]
else:
check = False
y = 1
if x >= y or check == False:
ans *= x
ans %= mod
ip += 2
else:
ans *= y
ans %= mod
im += 2
K -= 2
print(ans)
|
n, m = map(int, input().split())
a = []
for _ in range(n):
a.append(list(map(int, input().split())))
b = []
for _ in range(m):
b.append(int(input()))
c = []
for i in range(n):
c.append(sum([a[i][j] * b[j] for j in range(m)]))
for i in c:
print(i)
| 0 | null | 5,318,692,406,720 | 112 | 56 |
import math
n = int(input())
address = list(map(int, input().split()))
min_hp = math.inf
for i in range(1, 101):
hp = sum(map(lambda x: (x - i) ** 2, address))
if hp < min_hp:
min_hp = hp
print(min_hp)
|
N = int(input())
X = list(map(int, input().split()))
INF = 10**6 + 1
mintotal = INF
for p in range(1, 101):
total = 0
for x in X:
total += (x-p)**2
mintotal = min(mintotal, total)
print(mintotal)
| 1 | 65,388,863,559,488 | null | 213 | 213 |
a, b = map(int, raw_input().split())
print(str(a*b) + " " + str(2*(a+b)))
|
import sys
from sys import stdin
input = stdin.readline
cnt = 0
INF = 10000000000
def merge(A, left, mid, right):
global cnt
L = A[left:mid] + [INF]
R = A[mid:right] + [INF]
i = 0
j = 0
for k in range(left , right):
if L[i] <= R[j]:
A[k] = L[i]
i = i + 1
cnt += 1
else:
A[k] = R[j]
j = j + 1
cnt += 1
def mergeSort(A, left, right):
if left+1 < right:
mid = (left + right)//2
mergeSort(A, left, mid)
mergeSort(A, mid, right)
merge(A, left, mid, right)
n = int(input())
A =[int(x) for x in input().split()]
mergeSort(A, 0, n)
print(*A)
print(cnt)
| 0 | null | 210,593,450,768 | 36 | 26 |
a = [list(map(int ,input().split())) for _ in range(3)]
n = int(input())
ans = [[0] * 3 for _ in range(3)]
for i in range(n):
b = int(input())
for j in range(3):
for k in range(3):
if a[j][k] == b:
ans[j][k] = 1
state = False
for i in range(3):
num = 0
for j in range(3):
num += ans[i][j]
if num == 3:
state = True
for i in range(3):
num = 0
for j in range(3):
num += ans[j][i]
if num == 3:
state = True
if ans[0][0] + ans[1][1] + ans[2][2] == 3:
state = True
if ans[0][2] + ans[1][1] + ans[2][0] == 3:
state = True
if state:
print("Yes")
else:
print("No")
|
a = int(input())
res = a > 29 and 'Yes' or 'No'
print(res)
| 0 | null | 32,774,328,501,720 | 207 | 95 |
n, q = map(int, input().split())
p = [input().split() for _ in range(n)]
t = i = 0
while n:
i = i % n
if int(p[i][1]) <= q:
t += int(p[i][1])
print(p[i][0], t)
del p[i]
n -= 1
else:
p[i][1] = int(p[i][1]) - q
t += q
i += 1
|
# coding=utf-8
import sys
n, q = map(int, sys.stdin.readline().split())
queue = []
total_time = 0
finished_loop = 0
for i in range(n):
name, time = input().split()
queue.append([name, int(time)])
while queue:
n -= finished_loop
finished_loop = 0
for i in range(n):
poped = queue.pop(0)
name = poped[0]
time = poped[1]
if time > q:
queue.append([name, time - q])
total_time += q
else:
total_time += time
print(name, total_time)
finished_loop += 1
| 1 | 42,155,549,780 | null | 19 | 19 |
import copy
# a = get_int()
def get_int():
return int(input())
# a = get_string()
def get_string():
return input()
# a_list = get_int_list()
def get_int_list():
return [int(x) for x in input().split()]
# a_list = get_string_list():
def get_string_list():
return input().split()
# a, b = get_int_multi()
def get_int_multi(): # a, b = get_int_multi()
return map(int, input().split())
# a_list = get_string_char_list()
def get_string_char_list():
return list(str(input()))
# print("{} {}".format(a, b))
# for num in range(0, a):
# a_list[idx]
# a_list = [0] * a
'''
while (idx < n) and ():
idx += 1
'''
def main():
n, k = get_int_multi()
a_list = get_int_list()
for i in range(0, n-k):
if a_list[i] < a_list[k+i]:
print("Yes")
else:
print("No")
if __name__ == '__main__':
main()
|
import sys
import math
import time
sys.setrecursionlimit(int(1e6))
if False:
dprint = print
else:
def dprint(*args):
pass
n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
dprint("n, k = ", n, k)
lx = int(0) # NG
rx = int(1e9) # OK
#ans = 0
while ((rx-lx)>1):
x = (rx + lx) // 2
dprint("lx, x, rx = ", lx, x, rx)
n = 0
for i in range(len(A)):
#n += math.ceil(A[i] / x) - 1
n += (A[i]-1) // x
if n > k: break
dprint("n = ", n)
if n <= k:
dprint(" smaller x")
rx = x
else:
dprint(" cut less, bigger x")
lx = x
#ans = math.ceil(rx)
print(rx)
| 0 | null | 6,775,246,940,398 | 102 | 99 |
A,B = map(int,input().split())
Flag = True
i = max(A,B)
s = max(A,B)
while Flag == True and i <= A*B:
if i%A ==0 and i%B == 0:
Flag = False
i += s
print(i-s)
|
a,b=list(map(int,input().split()))
from fractions import gcd
res=a*b/gcd(a,b)
print(int(res))
| 1 | 112,998,913,309,088 | null | 256 | 256 |
N = int(input())
s = {}
for i in range(N):
item = input()
if item not in s:
s[item] = 1
print(len(s))
|
#! /usr/bin/env python3
import sys
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)
H1, M1, H2, M2, K = map(int, read().split())
start = H1 * 60 + M1
end = H2 * 60 + M2
gap = end - start
res = gap - K
print(res)
| 0 | null | 24,161,175,144,260 | 165 | 139 |
x,y,a,b,c=[int(i) for i in input().split()]
p=[int(i) for i in input().split()]
q=[int(i) for i in input().split()]
r=[int(i) for i in input().split()]
p.sort()
q.sort()
r.sort()
p=p[-x:]
q=q[-y:]
ans=p+q+r
ans.sort()
ans=ans[-x-y:]
print(sum(ans))
|
import bisect
import sys
input = sys.stdin.readline
def solve():
N = int(input())
S = list(input())
Q = int(input())
set_dic = {}
for i, s in enumerate(S):
if s not in set_dic:
set_dic[s] = [i+1]
else:
bisect.insort_left(set_dic[s], i+1)
anslist = []
for _ in range(Q):
query1, query2, query3 = input().split()
if query1 == "1":
i = int(query2)
c = query3
cap = S[i-1]
if cap == c:
continue
set_dic[cap].remove(i)
if c not in set_dic:
set_dic[c] = [i]
else:
bisect.insort_left(set_dic[c], i)
S[i-1] = c
else:
l = int(query2)
r = int(query3)
ansset = set()
ans = 0
for eachlist in set_dic.values():
li = bisect.bisect_left(eachlist, l)
ri = bisect.bisect_right(eachlist, r)
if li != ri:
ans += 1
anslist.append(ans)
for i in anslist:
print(i)
if __name__ == '__main__':
solve()
| 0 | null | 53,536,986,894,180 | 188 | 210 |
n = int(raw_input())
d = [100 for i in range(n)]
G = [0 for i in range(n)]
v = [[0 for i in range(n)] for j in range(n)]
def BFS(s):
for e in range(n):
if v[s][e] == 1:
if d[e] > d[s] + 1:
d[e] = d[s]+1
BFS(e)
for i in range(n):
G = map(int, raw_input().split())
for j in range(G[1]):
v[G[0]-1][G[j+2]-1] = 1
d[0] = 0
for i in range(n):
BFS(i)
for i in range(n):
if d[i] == 100:
d[i] = -1
for i in range(n):
print i+1, d[i]
|
class Node(object):
def __init__(self, V):
self.V = V
self.d = -1
n = int(input())
nodes = [0] * 101
for i in range(n):
data = input().split()
V = list(map(int, data[2:]))
V.sort(reverse = True)
nodes[int(data[0])] = Node(V)
l = [1,]
l_old = []
cnt = 0
while len(l) > 0:
l_old = l
l = []
for i in l_old:
if nodes[i].d == -1:
nodes[i].d = cnt
V = nodes[i].V
for j in V:
if not j in l:
if nodes[j].d == -1:
l.append(j)
cnt += 1
for i in range(1, n+1):
if not isinstance(nodes[i], int):
print(i, nodes[i].d)
| 1 | 3,837,346,940 | null | 9 | 9 |
num = list(map(int,input().split()))
circle_px = num[2] + num[4]
circle_mx = num[2] - num[4]
circle_py = num[3] + num[4]
circle_my = num[3] - num[4]
if(circle_px <= num[0] and circle_mx >= 0 and circle_py <= num[1] and circle_py >= 0): print("Yes")
else: print("No")
|
#!usr/bin/env pypy3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
res = list(sys.stdin.readline())
if res[-1] == "\n":
return res[:-1]
return res
def IR(n):
return [I() for i in range(n)]
def LIR(n):
return [LI() for i in range(n)]
def SR(n):
return [S() for i in range(n)]
def LSR(n):
return [LS() for i in range(n)]
sys.setrecursionlimit(1000000)
def main():
N, K = LI()
x = N % K
y = abs(x - K)
print(min(x, y))
main()
| 0 | null | 19,838,965,473,290 | 41 | 180 |
#!/usr/bin/env python
# coding: utf-8
def ri():
return int(input())
def rl():
return list(input().split())
def rli():
return list(map(int, input().split()))
def main():
h1, m1, h2, m2, k = rli()
mins = (h2-h1)*60+m2-m1
print(mins-k)
if __name__ == '__main__':
main()
|
s = int(input())
m = s // 60
s = s % 60
h = m // 60
m = m % 60
print("{}:{}:{}".format(h, m, s))
| 0 | null | 9,269,989,643,058 | 139 | 37 |
from collections import deque
H,W = map(int,input().split())
field = [list(input()) for _ in range(H)]
dist = [[-1]*W for _ in range(H)]
dx = [1,0,-1,0]
dy = [0,1,0,-1]
mx = 0
for h in range(H):
for w in range(W):
if field[h][w] == "#":
continue
dist = [[-1]*W for _ in range(H)]
dist[h][w] = 0
que = deque([])
que.append([h,w])
while que != deque([]):
u,v = que.popleft()
for dir in range(4):
nu = u + dx[dir]
nv = v + dy[dir]
if (nu < 0) or (nu >= H) or (nv < 0) or (nv >= W):
continue
if field[nu][nv] == "#":
continue
if dist[nu][nv] != -1:
continue
que.append([nu,nv])
dist[nu][nv] = dist[u][v] + 1
for i in range(H):
for j in range(W):
if mx < dist[i][j]:
mx = dist[i][j]
print(mx)
|
from collections import deque
h,w=map(int,input().split())
l=list()
l.append('#'*(w+2))#壁
for i in range(h):
l.append('#'+input()+'#')
l.append('#'*(w+2))#壁
p=0
for i in range(1,h+1):
for j in range(1,w+1):
if l[i][j]=='#':
continue
s=[[-1 for a in b] for b in l]
q=deque([[i,j]])
s[i][j]=0
while len(q)>0:
a,b=q.popleft()
for x,y in [[1,0],[0,1],[-1,0],[0,-1]]:
if l[a+x][b+y]=='#' or s[a+x][b+y]>-1:
continue
q.append([a+x,b+y])
s[a+x][b+y]=s[a][b]+1
r=0
for x in s:
for y in x:
r=max(y,r)
p=max(r,p)
print(p)
| 1 | 94,699,102,244,990 | null | 241 | 241 |
n = input()
s = set(input().split())
q = input()
t = set(input().split())
print(len(s & t))
|
n = int(input())
str = input().split(" ")
S = [int(str[i]) for i in range(n)]
q = int(input())
str = input().split(" ")
T = [int(str[i]) for i in range(q)]
S_set = set(S)
S = list(S_set)
T.sort()
result = 0
for s in S:
for t in T:
if s == t:
result += 1
break
print(result)
| 1 | 67,225,121,262 | null | 22 | 22 |
N = input()
if N.endswith('s'):
print(N + 'es')
else:
print(N + 's')
|
s = input()
print(s + "es") if s[-1] == "s" else print(s + "s")
| 1 | 2,425,782,405,508 | null | 71 | 71 |
def is_prime(x):
for i in range(2, int(x ** 0.5) + 1):
if x % i == 0:
return False
else:
return True
def main():
x = int(input())
while True:
if is_prime(x):
ans = x
break
x += 1
print(ans)
if __name__ == "__main__":
main()
|
X=int(input())
while True:
for x in range(2,X):
if X%x==0:
break
else:
print(X)
break
X+=1
| 1 | 105,667,472,211,784 | null | 250 | 250 |
import re
s = input()
if s == "hi" or s=="hihi" or s=="hihihi" or s=="hihihihi" or s== "hihihihihi":
print("Yes")
else:
print("No")
|
n = int(input())
arr = list(map(int,input().split()))
tot = sum(arr)
temp = 0
ans = 1e18
for i in range(n):
temp += arr[i]
rest = tot-temp
cost = abs(rest-temp)
if cost < ans:
ans = cost
print(ans)
| 0 | null | 97,865,644,095,960 | 199 | 276 |
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 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,239,188,258,048 | null | 294 | 294 |
d=set()
for _ in[0]*int(input()):
c,g=input().split()
if'i'==c[0]:d|=set([g])
else:print(['no','yes'][g in d])
|
N = int(input())
d = {}
for i in [None]*N:
a, b = input().split()
if a == "insert":
d[b] = 1
else:
print("yes" if b in d else "no")
| 1 | 75,353,040,448 | null | 23 | 23 |
n = int(input())
N = n
arr = [n]
while n % 2 == 0:
arr.append(2)
n //= 2
f = 3
while f * f <= n:
if n % f == 0:
arr.append(f)
n //= f
else:
f += 2
if n != 1:
arr.append(n)
arr.sort()
import collections
c = collections.Counter(arr)
k = list(c.keys())
v = list(c.values())
x = []
for i in range(len(k)):
for p in range(1,v[i]+1):
x.append(k[i]**p)
x.sort()
ans = 0
n = N
while n != 1 and len(x) != 0:
if n % x[0] == 0:
n //= x[0]
x.pop(0)
ans += 1
else:
x.pop(0)
print(ans)
|
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()
l=factorization(n)
ans =0
for x in l:
a=x[0] #素数
b=x[1] #素数の個数
k=0
t=0
while(True):
t += 1
k += t
if k==b:
ans += t
break
elif k > b:
ans += t-1
break
print(ans)
| 1 | 16,945,905,867,510 | null | 136 | 136 |
n = int(input())
room_list = [[[0 for i in range(10)] for j in range(3)] for k in range(4)]
for i in range(n):
b, f, r, v = map(int, input().split())
room_list[b - 1][f - 1][r - 1] += v
output =[]
for i in range(4):
for j in range(3):
output = list(map(str, room_list[i][j]))
print(" " + " ".join(output))
if i < 3:
print("#" * 20)
|
x = [int(x) for x in input().split()]
if (sum(x)>21):
print('bust')
else:
print('win')
| 0 | null | 59,803,260,588,106 | 55 | 260 |
H = int(input())
cnt = 1
while(H > 0):
cnt *= 2
H //= 2
print(cnt - 1)
|
n = int(input())
a = [int(s) for s in input().split(" ")]
cnt = 0
for i in range(n):
mi = min(a[i:])
if a[i] > mi:
j = a[i:].index(mi)+i
a[i], a[j] = a[j], a[i]
cnt += 1
print(" ".join([str(i) for i in a]))
print(cnt)
| 0 | null | 40,283,102,634,800 | 228 | 15 |
import math
n = int(input())
cnt = 0
mod = 10
if n%2 == 1:
print(0)
exit()
while mod<=n:
cnt += n//mod
mod*=5
print(cnt)
|
N = int(input())
if N % 2 == 1:
print(0)
else:
n = 0
i = 1
while True:
if N < 2*5**i:
break
n += (N // 5**i // 2)
i += 1
print(n)
| 1 | 116,402,650,473,238 | null | 258 | 258 |
n, k, c = map(int, input().split())
c += 1
s = list(input())
a = []
i = 0
while i < len(s) and len(a) < k:
if s[i] == 'x':
i += 1
continue
a.append(i)
i += c
b = []
j = len(s)-1
while j > -1 and len(b) < k:
if s[j] == 'x':
j -= 1
continue
b.append(j)
j -= c
b = b[::-1]
for i in range(len(a)):
if a[i] == b[i]:
print(a[i] + 1)
|
#!/usr/bin/env python3
# coding: utf-8
class Dice() :
mask = {'N':(1,5,2,3,0,4), 'E':(3,1,0,5,4,2),
'W':(2,1,5,0,4,3), 'S':(4,0,2,3,5,1)}
def __init__(self, data):
self.label = data
def move(self, data):
self.label = [self.label[idx] for idx in self.mask[data]]
def get_up(self):
return self.label[0]
dicedata = input().split()
orderdata = input()
newdice = Dice(dicedata)
[newdice.move(orderdata[idx]) for idx in range(0, len(orderdata))]
print(newdice.get_up())
| 0 | null | 20,355,499,036,444 | 182 | 33 |
import sys
import heapq
from collections import defaultdict
stdin = sys.stdin
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))
def naa(N): return [na() for _ in range(N)]
def ns(): return stdin.readline().rstrip() # ignore trailing spaces
N, K = na()
A_array = na()
cusum = 0
mod_array = [0]
for i, a in enumerate(A_array):
cusum = (cusum + a) % K
mod = (cusum + K - (i+1)) % K
mod_array.append(mod)
# print(mod_array)
ans = 0
mod_queue = []
cnt_dic = defaultdict(int)
for i, v in enumerate(mod_array):
heapq.heappush(mod_queue, (i, 1, v))
while(len(mod_queue)):
i, q, v = heapq.heappop(mod_queue)
if q == 1:
ans += cnt_dic[v]
cnt_dic[v] += 1
if i + K <= N:
heapq.heappush(mod_queue, (i+K-0.5, 2, v))
else:
cnt_dic[v] -= 1
# print(i, v, q, ans)
print(ans)
|
import bisect
import collections
N,K=map(int,input().split())
A=list(map(int,input().split()))
A.insert(0,0)
cuml=[0]*(N+1)
cuml[0]=A[0]
l=[]
cuml2=[]
l2=[]
buf=[]
ans=0
for i in range(N):
cuml[i+1]=cuml[i]+A[i+1]
#print(cuml)
for i in range(N+1):
cuml2.append([(cuml[i]-i)%K,i])
cuml[i]=(cuml[i]-i)%K
#print(cuml2,cuml)
cuml2.sort(key=lambda x:x[0])
piv=cuml2[0][0]
buf=[]
for i in range(N+1):
if piv!=cuml2[i][0]:
l2.append(buf)
piv=cuml2[i][0]
buf=[]
buf.append(cuml2[i][1])
l2.append(buf)
#print(l2)
cnt=0
for i in range(len(l2)):
for j in range(len(l2[i])):
num=l2[i][j]
id=bisect.bisect_left(l2[i], num + K)
#print(j,id)
cnt=cnt+(id-j-1)
print(cnt)
| 1 | 136,976,463,712,512 | null | 273 | 273 |
n=int(input())
a=[int(j) for j in input().split()]
p=[0]*n
d=[0]*n
for i in range(n):
p[i]=a[i]+p[i-2]
if (i&1):
d[i]=max(p[i-1],a[i]+d[i-2])
else:
d[i]=max(d[i-1],a[i]+d[i-2])
print(d[-1])
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# F - Select Half
n = int(input())
a = list(map(int, input().split()))
assert len(a) == n
# memo[i][m] a[0:i+1]からm個選ぶときの最大値
memo = [{} for i in range(n)]
memo[0] = {0: 0, 1: a[0]}
memo[1] = {1: max(a[:2])}
for i in range(2, n):
m = (i + 1) // 2
memo[i][m] = max(memo[i - 1][m], memo[i - 2][m - 1] + a[i])
if i % 2 == 0:
memo[i][m + 1] = memo[i - 2][m] + a[i]
print(memo[-1][n // 2])
| 1 | 37,340,008,720,060 | null | 177 | 177 |
w = raw_input().lower()
s = 0
while 1:
in_str = raw_input()
if in_str == "END_OF_TEXT":
break
in_str = map(str, in_str.split())
for i in in_str:
if i.lower() == w:
s+=1
print s
|
W = input()
cnt = 0
while True:
t = input()
if t == 'END_OF_TEXT':
break
else:
cnt += t.lower().split().count(W)
print(cnt)
| 1 | 1,839,273,842,430 | null | 65 | 65 |
import itertools
A,B,C = list(map(int, input().split()))
K = int(input())
ans = False
for p in itertools.product(['a','b','c'], repeat=K):
A0,B0,C0 = A,B,C
for e in p:
if e == 'a':
A0 = 2*A0
elif e == 'b':
B0 = 2*B0
else:
C0 = 2*C0
if A0 < B0 < C0 :
ans = True
break
print('Yes' if ans else 'No')
|
import math
while True:
try:
a =map(int,raw_input().split())
b = a[0] + a[1]
print len(str(a[0]+a[1]))
except EOFError:
break
| 0 | null | 3,433,442,790,532 | 101 | 3 |
def sell(rate,kabu):
money = rate * kabu
return money
def buy(rate,money):
kabu = money//rate
otsuri = money % rate
return kabu,otsuri
N,*A = map(int,open(0).read().split())
max_money = 1000
have_money = 1000
have_kabu = 0
for i in range(N-1):
if A[i] < A[i+1] and have_money > A[i]: #買う
have_kabu,have_money = buy(A[i],have_money)
if A[i] > A[i+1]: #売る
have_money += sell(A[i],have_kabu)
have_kabu = 0
max_money = max(max_money,have_money)
have_money += sell(A[-1],have_kabu)
max_money = max(max_money,have_money)
print(max_money)
|
N = int(input())
A = list(map(int, input().split()))
ans = 1000
for i in range(N-1):
if A[i] < A[i+1]:
temp = ans // A[i]
ans -= temp*A[i]
ans += temp*A[i+1]
print(ans)
| 1 | 7,374,630,977,210 | null | 103 | 103 |
n = int(input())
r = []
for i in range(n):
r.append(int(input()))
min = r[0]
max = -10 ** 12
for j in r[1:]:
if j - min > max:
max = j - min
if min > j:
min = j
print(max)
|
N = int(input())
purchase_price = int(input())
profit = -2000000000
for _ in range(N-1):
price = int(input())
profit = max(profit, (price-purchase_price))
purchase_price = min(price, purchase_price)
print(profit)
| 1 | 13,406,666,742 | null | 13 | 13 |
#!/usr/bin/env python3
from pprint import pprint
import sys
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
n = int(input())
x = input()
x_pc = x.count('1')
x_pc_plus = x_pc + 1
x_pc_minus = x_pc - 1
# 2 ** i % x_pc_plus, 2 ** i % x_pc_minus を予め計算しておく
r_plus_list = [pow(2, i, x_pc_plus) for i in range(n+1)]
r_minus_list = [0] * (n+1)
if x_pc_minus > 0:
r_minus_list = [pow(2, i, x_pc_minus) for i in range(n+1)]
x = x[::-1]
r_plus = 0
r_minus = 0
for i in range(n):
if x[i] == '1':
r_plus += r_plus_list[i]
r_minus += r_minus_list[i]
for i in range(n-1, -1, -1):
if x[i] == '0':
diff = (r_plus + r_plus_list[i]) % x_pc_plus
elif x_pc_minus >= 1:
diff = (r_minus - r_minus_list[i]) % x_pc_minus
else:
print(0)
continue
ans = 1
while diff > 0:
diff = diff % bin(diff).count('1')
ans += 1
print(ans)
|
N=int(input())
K=int(input())
import sys
sys.setrecursionlimit(10**6)
def solve(N, K):
if K==1:
l=len(str(N))
ans=0
for i in range(l):
if i!=l-1:
ans+=9
else:
ans+=int(str(N)[0])
#print(ans)
return ans
else:
nextK=K-1
l=len(str(N))
ini=int(str(N)[0])
res=ini*(10**(l-1))-1
#print(res)
if l>=K:
ans=solve(res, K)
nextN=int(str(N)[1:])
ans+=solve(nextN, nextK)
return ans
else:
return 0
ans=solve(N,K)
print(ans)
| 0 | null | 42,155,483,503,900 | 107 | 224 |
import math
x=int(input())
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(math.sqrt(n)) +1):
if n % i == 0:
return False
return True
for i in range(x, 100003+1):
if is_prime(i):
print(i)
exit()
|
x = int(input())
n = 10 * x
primes = [True] * n
for i in range(2, n):
if primes[i]:
for j in range(i * i, n, i):
primes[j] = False
for i in range(x, n):
if primes[i]:
print(i)
exit()
| 1 | 105,840,473,915,072 | null | 250 | 250 |
def fibonacciDP(i):
if i == 0:
return 1
elif i == 1:
return 1
else:
if dp[i-1] is None and dp[i-2] is None:
dp[i-1] = fibonacciDP(i-1)
dp[i-2] = fibonacciDP(i-2)
elif dp[i-1] is None:
dp[i-1] = fibonacciDP(i-1)
elif dp[i-2] is None:
dp[i-2] = fibonacciDP(i-2)
return dp[i-1]+ dp[i-2]
def fibonacci(i):
if i == 0:
return 1
elif i == 1:
return 1
else:
return fibonacci(i-1)+fibonacci(i-2)
n = int(input())
dp = [None]*n
print(fibonacciDP(n))
|
N = int(input())
def memorize(f) :
cache = {}
def helper(*args) :
if args not in cache :
cache[args] = f(*args)
return cache[args]
return helper
@memorize
def fibonacci(n):
if n == 0 or n == 1:
return 1
res = fibonacci(n - 2) + fibonacci(n - 1)
return res
print(fibonacci(N))
| 1 | 2,064,940,582 | null | 7 | 7 |
def main(S, T):
ans = 0
for i in range(len(S)):
if S[i] != T[i]:
ans += 1
return ans
if __name__ == '__main__':
S = input()
T = input()
ans = main(S, T)
print(ans)
|
n,k = map(int,input().split())
l = []
while n != 0 :
r = divmod(n,k)
n = r[0]
l.append(r[1])
print(len(l))
| 0 | null | 37,149,975,381,490 | 116 | 212 |
X, Y = map(int,input().split())
multiple = X * Y
print(multiple)
|
x,y=input().split();x=int(x);y=float(y);print(int(x*y))
| 1 | 15,823,404,974,700 | null | 133 | 133 |
def distance(x, y, p):
"""returns Minkowski's distance of vactor x and y if p > 0.
if p == 0, returns Chebyshev distance
>>> d = distance([1, 2, 3], [2, 0, 4], 1)
>>> print('{:.6f}'.format(d))
4.000000
>>> d = distance([1, 2, 3], [2, 0, 4], 2)
>>> print('{:.6f}'.format(d))
2.449490
>>> d = distance([1, 2, 3], [2, 0, 4], 3)
>>> print('{:.6f}'.format(d))
2.154435
>>> d = distance([1, 2, 3], [2, 0, 4], 0)
>>> print('{:.6f}'.format(d))
2.000000
"""
if p == 0:
return max([abs(a-b) for (a, b) in zip(x, y)])
else:
return sum([abs(a-b) ** p for (a, b) in zip(x, y)]) ** (1/p)
def run():
dim = int(input()) # flake8: noqa
x = [int(i) for i in input().split()]
y = [int(j) for j in input().split()]
print(distance(x, y, 1))
print(distance(x, y, 2))
print(distance(x, y, 3))
print(distance(x, y, 0))
if __name__ == '__main__':
run()
|
# https://atcoder.jp/contests/abc145/tasks/abc145_d
X, Y = list(map(int, input().split()))
MOD = int(1e9 + 7)
def combination(n, r, mod=MOD):
def modinv(a, mod=MOD):
return pow(a, mod-2, mod)
# nCr with MOD
r = min(r, n - r)
res = 1
for i in range(r):
res = res * (n - i) * modinv(i+1, mod) % mod
return res
# 1回の移動で3増えるので,X+Yは3の倍数 (0, 0) start
if (X + Y) % 3 != 0:
ans = 0
print(0)
else:
# X+Yは3の倍数
# (+1, +2)をn回,(+2, +1)をm回実行
# n + 2m = X
# 2n + m = Y
# 3 m = 2 X - Y
# m = (2 X - Y) // 3
# n = X - 2 * m
m = (2 * X - Y) // 3
n = X - 2 * m
if m < 0 or n < 0:
print(0)
else:
print(combination(m + n, m, MOD))
| 0 | null | 74,936,774,006,288 | 32 | 281 |
N=list(input())
K=int(input())
N=list(map(int,N))
#print(N)
dp=[[[0 for _ in range(4)] for _ in range(len(N))] for _ in range(2)]
for i in range(len(N)):
num=N[i]
mayover=0
if i==0:
for j in range(10):
if j<num:
if j==0:
dp[0][i][0]=dp[0][i][0]+1
else:
dp[0][i][1] = dp[0][i][1] + 1
elif j==num:
if j==0:
dp[1][i][0] = dp[1][i][0] + 1
else:
dp[1][i][1] = dp[1][i][1] + 1
else:
for j in range(2):
if j==0:
for l in range(4):
dp[j][i][l] = dp[j][i][l] + dp[j][i - 1][l]
if l != 0:
dp[j][i][l] = dp[j][i][l] + 9*dp[j][i - 1][l - 1]
else:
if num==0:
for l in range(4):
dp[j][i][l]=dp[j][i-1][l]
else:
for l in range(4):
dp[0][i][l]=dp[0][i][l]+dp[j][i-1][l]
if l!=0:
dp[0][i][l]=dp[0][i][l]+(num-1)*dp[j][i-1][l-1]
dp[1][i][l]=dp[1][i][l]+dp[j][i-1][l-1]
#print(dp[0][i],dp[1][i])
#input()
print(dp[0][len(N)-1][K]+dp[1][len(N)-1][K])
|
while True:
h, w =map(int,input().split())
odd = '#.'*300
even = '.#'*300
if h == 0 and w==0:
break
for i in range(h):
if i%2 ==0:
out = odd[:w]
else:
out= even[:w]
print(out)
print()
| 0 | null | 38,211,602,773,902 | 224 | 51 |
N = int(input())
result = 0
for i in range(1, (N + 1)):
if i % 15 == 0:
'Fizz Buzz'
elif i % 3 == 0:
'Fizz'
elif i % 5 == 0:
'Buzz'
else:
result = result + i
print(result)
|
def to_fizzbuzz(number):
if number % 15 == 0:
return 'FizzBuzz'
if number % 3 == 0:
return 'Fizz'
if number % 5 == 0:
return 'Buzz'
else:
return str(number)
# return i
def main():
N = int(input())
# this list concludes "FizzBuzz", "Fizz" or "Buzz"
fblist = []
for number in range(1, 10**6):
result = to_fizzbuzz(number)
fblist.append(result)
# the list up to N
n_list = fblist[0:N]
# this list contains only numbers and up to N
n_numlist = []
for s in n_list:
if s.isdigit() == True:
n_numlist.append(int(s))
print(sum(n_numlist))
main()
| 1 | 35,020,667,313,580 | null | 173 | 173 |
import collections
n=int(input())
S=input()
c=collections.Counter(S)
ans=c['R']*c['B']*c['G']
hiku=0
for i in range(n):
for d in range(n):
j=i+d
k=j+d
if k>n-1:
break
if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
hiku+=1
print(ans-hiku)
|
N = int(input())
S = list(input())
r = S.count('R')
g = S.count('G')
b = S.count('B')
cnt = 0
for i in range(N-3):
if S[i] == 'R':
r -= 1
cnt += g * b
l = 1
while (i + 2 * l < N):
if (S[i+l] == 'G' and S[i+2*l] == 'B') or (S[i+l] == 'B' and S[i+2*l] == 'G'):
cnt -= 1
l += 1
if S[i] == 'G':
g -= 1
cnt += r * b
l = 1
while (i + 2 * l < N):
if (S[i+l] == 'R' and S[i+2*l] == 'B') or (S[i+l] == 'B' and S[i+2*l] == 'R'):
cnt -= 1
l += 1
if S[i] == 'B':
b -= 1
cnt += g * r
l = 1
while (i + 2 * l < N):
if (S[i+l] == 'G' and S[i+2*l] == 'R') or (S[i+l] == 'R' and S[i+2*l] == 'G'):
cnt -= 1
l += 1
print(cnt)
| 1 | 35,915,945,951,172 | null | 175 | 175 |
n, x, m = map(int, input().split())
li = [x]
se = {x}
for i in range(n-1):
x = x*x%m
if x in se:
idx = li.index(x)
break
li.append(x)
se.add(x)
else:
print(sum(li))
exit(0)
ans = sum(li) + sum(li[idx:])*((n-len(li)) // (len(li)-idx)) + sum(li[idx:idx+(n-len(li)) % (len(li)-idx)])
print(ans)
|
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)
| 1 | 2,812,206,568,702 | null | 75 | 75 |
N, K, C = map(int, input().split())
S = input()
def get_positions():
res = []
i = 0
while i < N and len(res) < K:
if S[i] == "o":
res.append(i)
i = i + C + 1
else:
i += 1
return res
l = get_positions()
S = S[::-1]
r = get_positions()
for i in range(K):
r[i] = N - 1 - r[i]
S = S[::-1]
lastl = [-1] * (N + 1)
for i in range(K):
lastl[l[i] + 1] = i
for i in range(N):
if lastl[i + 1] == -1:
lastl[i + 1] = lastl[i]
lastr = [-1] * (N + 1)
for i in range(K):
lastr[r[i]] = i
for i in range(N-1, -1, -1):
if lastr[i] == -1:
lastr[i] = lastr[i + 1]
for i in range(N):
if S[i] == "x":
continue
li = lastl[i]
ri = lastr[i + 1]
cnt = 0
if li != -1:
cnt += (li + 1)
if ri != -1:
cnt += (ri + 1)
if li != -1 and ri != -1 and r[ri] - l[li] <= C:
cnt -= 1
if cnt >= K:
continue
print(i + 1)
|
from collections import Counter, defaultdict, deque
import bisect
from sys import stdin, stdout
from itertools import repeat
import math
import random
# sys.stdin = open('input')
def inp():
re = map(int, raw_input().split())
if len(re) == 1:
return re[0]
return re
def inst():
return raw_input().strip()
def gcd(x, y):
while(y):
x, y = y, x % y
return x
def my_main():
n, k, c = inp()
st = inst()
front, end = [], []
def get(now, ans):
while now < n:
if len(ans) == k:
return
for i in range(now, n):
if st[i]=='o':
ans.append(i+1)
now = i+c+1
break
else:
break
get(0, front)
st = st[::-1]
get(0, end)
for i in range(len(end)):
end[i] = n-end[i]+1
end.sort()
if len(front)!=k or len(end)!=k:
return
ans = []
# print front, end
for i in range(k):
if front[i] == end[i]:
ans.append(front[i])
if ans:
print '\n'.join(map(str, ans))
my_main()
| 1 | 40,516,562,127,052 | null | 182 | 182 |
X = int(input())
c1 = X//500
X = X - c1*500
c2 = X//5
print(c1*1000 + c2*5)
|
s = input()
n = int(input())
for _ in range(n):
line = input().split()
order = line[0]
a = int(line[1])
b = int(line[2])
p = line[3] if len(line) == 4 else ''
if order == 'print':
print(s[a:b+1])
elif order == 'reverse':
s = s[:a] + ''.join(reversed(s[a:b+1])) + s[b+1:]
elif order == 'replace':
s = s[:a] + p + s[b+1:]
| 0 | null | 22,422,175,937,964 | 185 | 68 |
def isprime(n):
if n < 2: return 0
elif n == 2: return 1
if n % 2 == 0: return 0
for i in range(3, n, 2):
if i > n/i: return 1
if n % i == 0 : return 0
return 1
N = int(input())
n = [int(input()) for i in range(N)]
a = [i for i in n if isprime(i)]
print(len(a))
|
import math
n = int(input())
count = 0
for i in range(n):
t = int(input())
a = int(t ** (1 / 2))
end = 0
for j in range(2, a + 1):
if t % j == 0:
end = 1
break
if end == 0:
count += 1
print(count)
| 1 | 10,512,285,050 | null | 12 | 12 |
import math
A,B,H,M = map(int,input().split())
t1 = (30*H+M/2)*math.pi/180
x1 = A*math.cos(t1)
y1 = A*math.sin(t1)
t2 = M*math.pi/30
x2 = B*math.cos(t2)
y2 = B*math.sin(t2)
d = ((x1-x2)**2+(y1-y2)**2)**0.5
print(d)
|
import math
A,B,H,M = map(int,input().split())
x = H*60+M
y = x/2
z = 6*M
a = abs(y-z)
if a > 180:
a = 360 - a
c_2 = A**2 + B**2 - 2*A*B*(math.cos(math.radians(a)))
print(math.sqrt(c_2))
| 1 | 20,098,261,626,818 | null | 144 | 144 |
from collections import defaultdict
N, K = map(int, input().split())
A = [(int(c)%K)-1 for c in input().split()]
B = [0]
for c in A:
B += [B[-1]+c]
dic = defaultdict(int)
ldic = defaultdict(int)
for i in range(min(K,N+1)):
c = B[i]
dic[c%K] += 1
ans = 0
#print(dic)
for i in range(1,N+1):
x = B[i-1]
ldic[x%K] += 1
ans += dic[x%K]-ldic[x%K]
if K+i-1<=N:
dic[B[K+i-1]%K] += 1
"""
print('###############')
print(i,x, ans)
print(dic)
print(ldic)
"""
print(ans)
|
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
*a, = map(int, input().split())
s = [0]*(n+1)
for i in range(len(a)):
s[i+1] = s[i] + a[i]
for i in range(len(s)):
s[i] = (s[i] - i)%k
from collections import defaultdict
cnt = defaultdict(int)
left = 0
right = k-1
if right > n:
right = n
for i in range(right+1):
cnt[s[i]] += 1
ans = 0
while left < right:
ans += cnt[s[left]]-1
if right == n:
cnt[s[left]] -= 1
left += 1
else:
cnt[s[left]] -= 1
left += 1
right += 1
cnt[s[right]] += 1
print(ans)
| 1 | 137,303,186,972,770 | null | 273 | 273 |
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))
|
while True:
n, x = map(int, input().split())
if (n, x) == (0, 0): break
ans = []
for i in range(n, 1, -1):
for j in range(1, n):
tmp = x-(i+j)
if tmp > n: continue
if tmp > 0 and i > 0 and j > 0:
if tmp != j and tmp != i and i != j:
pmt = sorted([i, j, tmp])
if not pmt in ans: ans.append(pmt)
print(len(ans))
| 0 | null | 21,332,682,302,778 | 183 | 58 |
n = input()
debt = 100000
for i in range(n):
debt *= 1.05
if(debt % 1000):
debt -= debt % 1000
debt += 1000
print(int(debt))
|
from sys import stdin
input = stdin.readline
from collections import Counter
def solve():
n = int(input())
verdict = [ inp for inp in stdin.read().splitlines()]
c = Counter(verdict)
print(f'AC x {c["AC"]}')
print(f'WA x {c["WA"]}')
print(f'TLE x {c["TLE"]}')
print(f'RE x {c["RE"]}')
if __name__ == '__main__':
solve()
| 0 | null | 4,336,226,704,264 | 6 | 109 |
n,m = map(int,input().split())
AB = [[] for _ in range(n)]
for i in range(m):
a,b = map(int,input().split())
AB[a-1].append(b-1)
AB[b-1].append(a-1)
visited = set()
ans = [0]*n
stack = [0]
for i in stack:
# このへんあやしい
for j in AB[i]:
if j in visited:
continue
visited.add(j)
ans[j] = i+1
stack.append(j)
# このへんまで
for i in ans[1:]:
if i==0:
print('No')
exit()
print('Yes')
for i in ans[1:]:
print(i)
|
import sys
import numpy as np
def input(): return sys.stdin.readline().rstrip()
def odd(A):
oddi=np.array(A[::2],dtype=np.int64)
eveni=np.array(A[1::2],dtype=np.int64)
left=np.cumsum(oddi[:-1]-eveni)
left=np.insert(left,0,0)
right=np.cumsum(oddi[:0:-1]-eveni[::-1])[::-1]
right=np.append(right,0)
tmp=np.max(np.maximum.accumulate(left)+right)
return tmp+np.sum(eveni)
def even(A):
left=np.array([0]+A[::2],dtype=np.int64)
right=np.array(A[1::2]+[0],dtype=np.int64)[::-1]
return np.max(left.cumsum()+right.cumsum()[::-1])
def main():
n=int(input())
A=list(map(int, input().split()))
if n%2==1:
print(odd(A))
else:
print(even(A))
if __name__ == '__main__':
main()
| 0 | null | 29,042,227,825,780 | 145 | 177 |
r = int(input())
a = (44/7)*r
print(a)
|
import sys
readline = sys.stdin.readline
SET = set()
for _ in range(int(readline())):
c, s = readline().split()
if c == "insert":
SET.add(s)
elif c == "find":
print("yes" if s in SET else "no")
| 0 | null | 15,606,176,138,502 | 167 | 23 |
N=int(input())
lst=list(map(int,input().split(" ")))
now=0
for i in range(N):
if lst[i]==now+1:
now+=1
if now == 0:
print(-1)
else:
print(N-now)
|
def check():
N = int(input())
plus,minus = [],[]
for i in range(N):
S = input()
now = 0
mini = 0
for s in S:
if s=='(':
now += 1
else:
now -= 1
mini = min(mini,now)
if now>=0:
plus.append([mini,now])
else:
minus.append([now-mini,now])
plus.sort(reverse=True)
minus.sort(reverse=True)
now = 0
for a,b in plus:
if now + a <0:
return 'No'
now += b
for a,b in minus:
if now + b-a <0:
return 'No'
now += b
if now > 0:
return 'No'
return 'Yes'
print(check())
| 0 | null | 69,355,371,264,408 | 257 | 152 |
n = int(input())
a = n//2 if n%2==0 else (n//2)+1
print(a)
|
N = int(input())
ans = N//2 + N%2
print(ans)
| 1 | 58,921,456,560,060 | null | 206 | 206 |
H,W,N = [int(input()) for i in range(3)]
for o in range(1,10000):
if H <= W and N / o <= W:
break
elif H >= W and N / o <= H:
break
print(o)
|
from collections import deque
k = int(input())
q = deque([])
for i in range(1, 10):
q.append(i)
cnt = 0
while cnt < k:
s = q.popleft()
cnt += 1
t = s % 10
if t != 0:
q.append(10 * s + (t - 1))
q.append(10 * s + t)
if t != 9:
q.append(10 * s + (t + 1))
print(s)
| 0 | null | 64,436,680,257,852 | 236 | 181 |
N, K = map(int, input().split())
*A, = map(int, input().split())
for i in range(N-K):
print("Yes" if A[i]<A[i+K] else "No")
|
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(K, N):
if A[i]>A[i-K]:
print('Yes')
else:
print('No')
| 1 | 7,039,581,924,990 | null | 102 | 102 |
def main():
from collections import deque
N, M = (int(i) for i in input().split())
if N % 2 == 0:
A = deque([i+1 for i in range(N)])
for i in range(1, M+1):
if (N-i)-i > N//2:
print(A[i], A[N-i])
else:
print(A[i], A[N-i-1])
else:
A = deque([i for i in range(N)])
for i in range(1, M+1):
print(A[i], A[N-i])
if __name__ == '__main__':
main()
|
N = int(input())
nums = map(int, input().split(" "))
for n in nums:
if n % 2 != 0:
continue
if n % 3 == 0 or n % 5 == 0:
continue
else:
print("DENIED")
exit(0)
print("APPROVED")
| 0 | null | 48,535,429,401,742 | 162 | 217 |
N, A, B = map(int, input().split())
MAX = 2 * 10 ** 5 + 1
MOD = 10 ** 9 + 7
# Factorial
fac = [0] * (MAX + 1)
fac[0] = 1
fac[1] = 1
# Inverse
inv = [0] * (MAX + 1)
inv[1] = 1
# Inverse factorial
finv = [0] * (MAX + 1)
finv[0] = 1
finv[1] = 1
for i in range(2, MAX + 1):
fac[i] = fac[i - 1] * i % MOD
inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
finv[i] = finv[i - 1] * inv[i] % MOD
# Factorial for large
facl = [0] * (MAX + 1)
facl[0] = N
for i in range(1, MAX + 1):
facl[i] = facl[i - 1] * (N - i) % MOD
# Solution
ans = pow(2, N, MOD) - 1
ans -= facl[A - 1] * finv[A] % MOD
ans %= MOD
ans -= facl[B - 1] * finv[B] % MOD
ans %= MOD
print(ans)
|
MOD = 10**9 + 7
n, a, b = map(int, input().split())
def pow(x, n, MOD):
res = 1
while n:
if n & 1:
res *= x
res %= MOD
x *= x
x %= MOD
n >>= 1
return res
def fact(a, b, MOD):
res = 1
for i in range(a, b+1):
res *= i
res %= MOD
return res
def combi(n, k, MOD):
x = fact(n - k + 1, n, MOD)
y = fact(1, k, MOD)
return x * pow(y, MOD - 2, MOD) % MOD
def solve():
ans = pow(2, n, MOD) - 1 - combi(n, a, MOD) - combi(n, b, MOD)
print(ans % MOD)
#print(pow(2, MOD, MOD))
if __name__ == "__main__":
solve()
| 1 | 66,177,556,000,960 | null | 214 | 214 |
A, B = input().split()
A = int(A)
B = int(''.join([b for b in B if b != '.']))
print(A * B // 100)
|
import math , sys
from decimal import Decimal
A , B = list( map( Decimal , input().split()))
print( int(A * B ) )
| 1 | 16,675,284,289,430 | null | 135 | 135 |
import math
N = int(input())
result = N
for i in range(1, int(math.sqrt(N))+2):
if N % i == 0:
result = min(result, i - 1 + N//i - 1)
print(result)
|
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy
import bisect
if __name__ == "__main__":
n,x,y = map(int,input().split())
x-=1
y-=1
ans = [0]*(n)
for i in range(n):
for j in range(i+1,n):
d = min(abs(i-j),abs(x-i) + abs(y-j) + 1)
ans[d] += 1
for i in range(1,n):
print(ans[i])
| 0 | null | 102,600,016,773,100 | 288 | 187 |
visited = {0: -1}
height = 0
pools = []
for i, c in enumerate(input()):
if c == '\\':
height -= 1
elif c == '/':
height += 1
if height in visited:
width = i - visited[height]
sm = 0
while pools and pools[-1][0] > visited[height]:
x, l = pools.pop()
sm += l
pools.append((i, sm + width - 1))
visited[height] = i
print(sum(l for x, l in pools))
if pools:
print(len(pools), ' '.join(str(l) for x, l in pools))
else:
print(0)
|
import math
N, K = map(int, input().split())
a = list(map(int, input().split()))
def cal(x):
s = 0
for aa in a:
s += math.ceil(aa / x) - 1
if s <= K: return True
else: return False
l = 0
r = max(a)
while r - l > 1:
mid = (l + r) // 2
if cal(mid):
r = mid
else:
l = mid
print(r)
| 0 | null | 3,325,910,337,580 | 21 | 99 |
n = int(input())
plus_bracket = []
minus_bracket = []
for _ in range(n):
mini = 0
cur = 0
for bracket in input():
if bracket == '(':
cur += 1
else:
cur -= 1
if cur < mini:
mini = cur
if cur > 0:
plus_bracket.append([-mini, cur])
else:
minus_bracket.append([cur - mini, -cur])
success = True
cur = 0
plus_bracket.sort()
minus_bracket.sort()
for bracket in plus_bracket:
if cur < bracket[0]:
success = False
break
cur += bracket[1]
back_cur = 0
for bracket in minus_bracket:
if back_cur < bracket[0]:
success = False
break
back_cur += bracket[1]
if cur != back_cur:
success = False
if success:
print('Yes')
else:
print('No')
|
import sys
input = sys.stdin.readline
n = int(input())
plus = []
minus = []
for _ in range(n):
min_s = 0
ts = 0
s = input().strip()
for c in s:
ts += 1 if c == '(' else -1
min_s = min(ts, min_s)
if ts >= 0:
plus.append((-min_s, ts))
else:
minus.append((-(min_s - ts), -ts))
plus.sort()
minus.sort()
s1 = 0
for m, t in plus:
if s1 - m < 0:
print('No')
exit()
s1 += t
s2 = 0
for m, t in minus:
if s2 - m < 0:
print('No')
exit()
s2 += t
if s1 == s2:
print('Yes')
else:
print('No')
| 1 | 23,504,190,347,028 | null | 152 | 152 |
"""何回でも同じ荷物を選べるナップザック問題"""
def main():
H,N = map(int, input().split())
A,B = [],[]
for i in range(N):
a,b = map(int, input().split())
A.append(a)
B.append(b)
# dp[i][j] = i種類の魔法から、合計ダメージがj以上となるような魔力の最小値
INF = float('inf')
dp = [[INF]*(H+1) for _ in range(N+1)]
dp[0][0] = 0
for i in range(1,N+1):
for j in range(H+1):
dp[i][j] = min(dp[i][j], dp[i-1][j])
if j-A[i-1]>=0:
dp[i][j] = min(dp[i][j], dp[i-1][j-A[i-1]]+B[i-1])
dp[i][j] = min(dp[i][j], dp[i][j-A[i-1]]+B[i-1])
else:
dp[i][j] = min(dp[i][j], B[i-1])
print(dp[N][H])
main()
|
N = int(input())
if N == 2:
print(1)
exit()
ans = 0
def factorization(n):
lis = []
if n % 2 == 0:
c = 0
while n%2 == 0:
n //= 2
c += 1
lis.append([2, c])
k = 3
while k*k <= n:
if n%k == 0:
c = 0
while n%k == 0:
n //= k
c += 1
lis.append([k, c])
else:
k += 2
if n > 1:
lis.append([n, 1])
return lis
list1 = factorization(N-1)
ans1 = 1
for k in range(len(list1)):
ans1 *= list1[k][1]+1
ans += ans1-1
def operation(K):
n = N
while n%K == 0:
n //= K
if n%K == 1:
return True
else:
return False
list2 = factorization(N)
factorlist = [1]
for l in range(len(list2)):
list3 = []
for j in range(list2[l][1]):
for k in range(len(factorlist)):
list3.append(factorlist[k]*list2[l][0]**(j+1))
factorlist += list3
factorlist = factorlist[1:-1]
ans += 1
for item in factorlist:
if operation(item):
ans +=1
print(ans)
| 0 | null | 60,985,199,921,462 | 229 | 183 |
# coding: utf-8
a, b = map(int, input().split(' '))
menseki = a * b
print(menseki, a*2 + b*2)
|
s = input()
t = input()
s_l = len(s)
t_l = len(t)
s_l -= (t_l-1)
if(t in s):
print(0)
exit()
ma = 0
for si in range(s_l):
cnt = 0
for ti in range(t_l):
if(s[si+ti]==t[ti]):
cnt += 1
ma = max(ma,cnt)
print(t_l - ma)
| 0 | null | 2,001,340,846,640 | 36 | 82 |
import sys
input = sys.stdin.readline
n, t = map(int, input().split())
DISHES = [(0, 0)]
for _ in range(n):
a, b = map(int, input().split())
DISHES.append((a, b))
DISHES.sort()
DP = [[0] * (t + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
a, b = DISHES[i]
for j in range(1, t + 1):
if j - a < 0:
DP[i][j] = DP[i - 1][j]
else:
DP[i][j] = max(DP[i - 1][j], DP[i - 1][j - a] + b)
answer = 0
for k in range(1, n + 1):
answer = max(answer, DP[k - 1][t - 1] + DISHES[k][1])
print(answer)
|
from itertools import permutations
from math import sqrt, pow, factorial
n = int(input())
xy = [list(map(int, input().split())) for _ in range(n)]
def calc(a,b):
[x1, y1] = a
[x2, y2] = b
return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2))
sum = 0
for i in permutations(range(n)):
distance = 0
for j in range(1, n):
distance = calc(xy[i[j]], xy[i[j-1]])
sum += distance
print(sum/factorial(n))
| 0 | null | 150,645,164,442,740 | 282 | 280 |
import math
X=int(input())
print(360//math.gcd(360, X))
|
import math
x = int(input())
if 360 % x == 0:
print(360 // x)
else:
common = 360 * x // math.gcd(360, x)
ans = common // x
print(ans)
| 1 | 13,142,862,492,888 | null | 125 | 125 |
X, Y, A, B, C = map(int, input().split())
P = sorted(list(map(int, input().split())), reverse=True)
Q = sorted(list(map(int, input().split())), reverse=True)
R = sorted(list(map(int, input().split())), reverse=True)
red = sorted(P[:X])
gre = sorted(Q[:Y])
m = min((X+Y),C)
n = R[:m]
red.extend(gre)
red.extend(n)
red = sorted(red, reverse=True)
print(sum(red[:X+Y]))
|
from scipy.special import comb
n, k = map(int, input().split())
num, ans = 0, 0
for i in range(n+1):
num += n-2*i
if i >= k-1:
ans += num+1
ans = ans%(10**9+7)
print(ans)
| 0 | null | 38,910,233,373,440 | 188 | 170 |
# input
A1, A2, A3 = map(int, input().split())
if sum([A1, A2, A3]) >= 22:
print("bust")
else:
print("win")
|
def resolve():
A = sum(map(int, input().split()))
print("bust" if A >= 22 else "win")
if '__main__' == __name__:
resolve()
| 1 | 119,163,659,786,420 | null | 260 | 260 |
#!/usr/bin/env python3
# Generated by https://github.com/kyuridenamida/atcoder-tools
from typing import *
import collections
import functools
import itertools
import math
import sys
INF = float('inf')
def solve(N: int, a: "List[int]"):
c = 1
for i in a:
c += i == c
return len(a) - (c-1) if c != 1 else -1
def main():
sys.setrecursionlimit(10 ** 6)
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens)) # type: int
a = [int(next(tokens)) for _ in range(N)] # type: "List[int]"
print(f'{solve(N, a)}')
if __name__ == '__main__':
main()
|
from collections import deque
K = int(input())
q = deque(list(range(1, 10)))
for _ in range(K - 1):
u = q.popleft()
n = u % 10
for x in range(max(0, n - 1), min(9, n + 1) + 1):
q.append(u * 10 + x)
print(q.popleft())
| 0 | null | 77,428,557,889,242 | 257 | 181 |
n,k = map(int,input().split())
print((n%k) if (n%k) < (k-(n%k)) else (k-(n%k)))
|
N,K = map(int,input().split())
def solve(n,k):
n %= k
return min(n,abs(n-k))
print(solve(N,K))
| 1 | 39,303,057,381,080 | null | 180 | 180 |
import collections
N=int(input())
A=list(map(int,input().split()))
A=sorted(A)
c=collections.Counter(A)
if __name__=="__main__":
if 1 in A:
if c[1]==1:
print(1)
else:
print(0)
else:
A=[]
dp=[True for _ in range(10**6+10)]
for key,value in c.items():
if value==1:
A.append(key)
else:
if dp[key]:
i=2
while i*key<=10**6:
dp[i*key]=False
i+=1
if len(A)==0:
print(0)
else:
for a in A:
if dp[a]:
i=2
while i*a<=10**6:
dp[i*a]=False
i+=1
ans=0
for a in A:
if dp[a]:
ans+=1
print(ans)
|
N = int(input())
A = list(map(int,input().split()))
ma = max(A)
l = [0 for _ in range(ma + 10)]
for i in range(N):
temp = A[i]
while(temp <= ma + 5):
l[temp] += 1
temp += A[i]
ans = 0
for i in range(N):
if l[A[i]] == 1: ans += 1
print(ans)
| 1 | 14,464,362,576,800 | null | 129 | 129 |
N,M = map(int, input().split())
A_list = [int(i) for i in input().split()]
playable_days = N - sum(A_list)
if playable_days >= 0:
print(playable_days)
else:
print("-1")
|
n=int(input())
j,k=map(int,input().split())
f=1
if k-j>=n:
print('OK')
f=0
else:
for i in range(j,k+1):
if i%n==0:
print('OK')
f=0
break
if f==1:
print("NG")
| 0 | null | 29,456,166,824,720 | 168 | 158 |
h, w = map(int, input().split())
s = [input() for _ in range(h)]
def bfs(x, y):
q = []
dp = {}
def qpush(x, y, t):
if 0 <= x < w and 0 <= y < h and s[y][x] != '#' and (x, y) not in dp:
q.append((x, y))
dp[(x, y)] = t
qpush(x, y, 0)
while len(q) > 0:
(x, y) = q.pop(0)
qpush(x + 1, y, dp[(x, y)] + 1)
qpush(x, y - 1, dp[(x, y)] + 1)
qpush(x - 1, y, dp[(x, y)] + 1)
qpush(x, y + 1, dp[(x, y)] + 1)
return dp.get((x, y), 0)
t = 0
for y in range(h):
for x in range(w):
t = max(t, bfs(x, y))
print(t)
|
def main():
mod = 1000000007
n = int(input())
cnt = [0] * (n + 2)
cnt[0] = 3
res = 1
for x in input().split():
v = int(x)
res *= cnt[v] - cnt[v + 1]
res %= mod
cnt[v + 1] += 1
print(res)
main()
| 0 | null | 112,819,047,031,808 | 241 | 268 |
import sys
w = sys.stdin.readline().rstrip().lower()
cnt = 0
while True:
lines = sys.stdin.readline().rstrip()
if not lines:
break
words = lines.split( " " )
for i in range( len( words ) ):
if w == words[i].lower():
cnt += 1
print( cnt )
|
n, k = map(int, input().split())
a = n % k
if (a <= int(k/2)):
print(a)
else:
print(k-a)
| 0 | null | 20,717,615,305,760 | 65 | 180 |
import sys
from collections import defaultdict
from math import gcd
sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline
MOD = 10 ** 9 + 7
def solve():
N = int(rl())
AB = tuple(tuple(map(int, rl().split())) for _ in range(N))
zero_pare = 0
zero_a, zero_b = 0, 0
counter = defaultdict(int)
for a, b in AB:
if a == b == 0:
zero_pare += 1
elif a == 0:
zero_a += 1
elif b == 0:
zero_b += 1
else:
cd = gcd(a, b)
a, b = a // cd, b // cd
if b < 0:
a, b = -a, -b
counter[(a, b)] += 1
ans = pow(2, zero_a, MOD) + pow(2, zero_b, MOD) - 1
memo = set()
for a, b in list(counter):
if (a, b) in memo:
continue
pa, pb = -b, a
if pb < 0:
pa, pb = -pa, -pb
memo.add((a, b))
memo.add((pa, pb))
ans *= pow(2, counter[(a, b)], MOD) + pow(2, counter[(pa, pb)], MOD) - 1
ans %= MOD
ans = (ans + zero_pare - 1) % MOD
print(ans)
if __name__ == '__main__':
solve()
|
def gcd(x, y):
if x < y:
tmp = x
x = y
y = tmp
while y > 0:
r = x % y
x = y
y = r
return print(x)
x_y = input()
tmp = list(map(int, x_y.split()))
x = tmp[0]
y = tmp[1]
gcd(x, y)
| 0 | null | 10,522,960,616,572 | 146 | 11 |
import string
L = string.split(raw_input())
L.sort()
print (L[0]), (L[1]), (L[2])
|
for i, a in enumerate(sorted(list(map(lambda a : int(a), input().split(" "))))):
if i == 0:
print("%d" % a, end ="")
else:
print(" %d"% a, end ="")
print()
| 1 | 428,054,228,056 | null | 40 | 40 |
def minkovsky(A,B,n = 0):
C = [abs(a - b) for a , b in zip(A,B)]
if n == 0:
return max(C)
else:
d = 0
for c in C:
d += c ** n
d = d ** (1 / n)
return d
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
print(minkovsky(A,B,1))
print(minkovsky(A,B,2))
print(minkovsky(A,B,3))
print(minkovsky(A,B))
|
input()
a = list(input().split())
print(*a[::-1])
| 0 | null | 591,040,906,728 | 32 | 53 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.