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
|
---|---|---|---|---|---|---|
S = input()
T = input()
min = 10000000
for i in range(len(S)- len(T) + 1):
x = 0
for j in range(len(T)):
if S[i+j] != T[j]:
x += 1
if x < min:
min = x
print(min)
|
s = input()
n = len(s)
t = input()
m = len(t)
c_max = 0
for i in range(n - m + 1):
c = 0
for j in range(m):
if s[i + j] == t[j]:
c += 1
if c > c_max:
c_max = c
print(m - c_max)
| 1 | 3,690,115,332,970 | null | 82 | 82 |
k,n=map(int,input().split())
dis=list(map(int,input().split()))
ans=[]
for i in range(n):
if i>=1:
ans.append(dis[i]-dis[i-1])
ans.append(dis[0]+(k-dis[n-1]))
print(k-max(ans))
|
hoge1 = list()
hoge2 = list()
total = 0
for num, c in enumerate(input()):
if c == '\\':
hoge1.append(num)
elif c == '/':
if not hoge1:
continue
last_index = hoge1.pop()
S = num - last_index
if not hoge2:
hoge2.append((last_index, S))
else:
if last_index >= hoge2[-1][0]:
hoge2.append((last_index, S))
else:
new_S = S
for i, j in hoge2[::-1]:
if last_index >= i:
break
else:
new_S += hoge2.pop()[1]
hoge2.append((last_index, new_S))
totals = [x[1] for x in hoge2]
print (sum(totals))
totals.insert(0, len(hoge2))
print (' '.join([str(x) for x in totals]))
| 0 | null | 21,667,272,371,832 | 186 | 21 |
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")
### BIT
def init(bit, values):
for i,v in enumerate(values):
update(bit,i,v)
#a1 ~ aiまでの和 O(logn)
def query(bit,i):
res = 0
while i > 0:
res += bit[i]
i -= i&(-i)
return res
#ai += x(logN)
def update(bit,i,x):
while i <= len(bit)-1:
bit[i] += x
i += i&(-i)
return
n,d,a = map(int, input().split())
xh = [None]*n
# h = [None]*n
for i in range(n):
xh[i] = tuple(map(int, input().split()))
xh.sort()
bit = [0] * (n+1)
r = 0
val = 0
done = False
for l,(x,h) in enumerate(xh):
bound = x+2*d
while r<n and xh[r][0]<=bound:
r += 1
nokori = h + query(bit, l+1)
if nokori>0:
# print(nokori, a, l, r, bit)
num = nokori//a + int(nokori%a!=0)
val += num
update(bit, l+1, -num*a)
if r<n:
update(bit, r+1, num*a)
print(val)
|
from bisect import *
def solve():
ans = 0
N, D, A = map(int, input().split())
B = [list(map(int, input().split())) for _ in range(N)]
B.sort()
lasts = [0]*(N+1)
now = 0
for i,((x,h),l) in enumerate(zip(B,lasts)):
now -= l
atack = max(0,-(-(h-now)//A))
ans += atack
damage = atack*A
now += damage
last = x+2*D
ind = bisect_right(B,[last+1,0])
lasts[ind] += damage
return ans
print(solve())
| 1 | 82,316,668,061,302 | null | 230 | 230 |
l=input()
if l.lower()==l:
print('a')
else:
print('A')
|
def insertionSort(A,input_num):
for i in range(input_num):
v = A[i]
j = i - 1
while(j >= 0 and A[j] > v):
A[j + 1] = A[j]
j -= 1
A[j + 1] = v
print(*A)
input_num = int(input())
A = list(map(int, input().split()))
insertionSort(A,input_num)
| 0 | null | 5,666,546,852,780 | 119 | 10 |
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplt(n): return [tuple(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])
n = inp()
D = inplt(n)
cnt = 0
ans = 'No'
for d1,d2 in D:
if d1 == d2:
cnt += 1
else:
cnt = 0
if cnt == 3:
ans = 'Yes'
print(ans)
|
import os
import sys
from collections import defaultdict, Counter
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush,heapify
from math import ceil, floor, sqrt
from copy import deepcopy
def main():
n = int(input())
ans = 0
for i in range(1,n):
ans += (n-1)//i
print(ans)
if __name__ == "__main__":
main()
| 0 | null | 2,562,209,282,092 | 72 | 73 |
floor = [
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
]
s = '####################'
n = int(input())
for c in range(n):
(b,f,r,v) = [int(x) for x in input().split()]
if 1 > b > 5 and 1 > f > 4 and 1 > r > 10:
break
floor[b-1][f-1][r-1] += v
for z in range(4):
for i in range(len(floor[z])):
for v in floor[z][i]:
print(end=' ')
print(v, end='')
print()
if not z == 3:
print(s)
|
data = [[[0 for r in range(10)] for f in range(3)] for b in range(4)]
count = int(input())
for c in range(count):
(b, f, r, v) = [int(x) for x in input().split()]
data[b - 1][f - 1][r - 1] += v
for b in range(4):
for f in range(3):
for r in range(10):
print('',data[b][f][r], end='')
print()
print('#' * 20) if b < 4 - 1 else print(end='')
| 1 | 1,110,055,356,928 | null | 55 | 55 |
x = int(input())
a = x // 500
x = x - a * 500
b = x // 5
print(1000*a+5*b)
|
x=int(input())
k=0
s=0
if x//500>0:
k=x//500
s+=k*1000
x=x-k*500
if x//5>0:
k=x//5
s+=k*5
x=x-k*5
print(s)
| 1 | 42,720,230,360,708 | null | 185 | 185 |
x=int(input())
c=x//500
d=x%500
e=d//5
print(1000*c+e*5)
|
import collections
n,m = map(int,input().split())
g = [[] for i in range(n+1)]
for i in range(m):
a,b = map(int,input().split())
g[a].append(b)
g[b].append(a)
q = collections.deque()
q.append(1)
check = [0]*(n+1)
check[1] = 1
ans = [0]*(n+1)
while len(q)!=0:
v = q.popleft()
for j in g[v]:
if check[j] == 0:
check[j] = 1
ans[j] = v
q.append(j)
print('Yes')
for k in range(2,n+1):
print(ans[k])
| 0 | null | 31,698,825,461,812 | 185 | 145 |
x = int(input())
a = x//100
b = x%100
if b <= 5*a:
print(1)
exit()
print(0)
|
# row = [int(x) for x in input().rstrip().split(" ")]
# n = int(input().rstrip())
# s = input().rstrip()
def resolve():
import sys
input = sys.stdin.readline
x = int(input().rstrip())
if x // 100 * 5 >= x % 100:
print(1)
else:
print(0)
if __name__ == "__main__":
resolve()
| 1 | 127,664,399,741,570 | null | 266 | 266 |
import sys
w = input().lower()
lines = sys.stdin.read()
line=lines[0:lines.find('END_OF_TEXT')].lower().split()
print(line.count(w))
|
w=input().lower()
list=[]
while True:
s=input()
if s=="END_OF_TEXT":
break
c=s.lower().split().count(w)
list.append(c)
print(sum(list))
| 1 | 1,841,815,766,970 | null | 65 | 65 |
n, m = map(int, raw_input().split())
A = []
b = []
for i in range(n):
A += [map(int, raw_input().split())]
for i in range(m):
b += [input()]
for i in range(n):
C = 0
for j in range(m):
C += A[i][j] * b[j]
print C
|
import sys
readline = sys.stdin.readline
N = int(readline())
A = list(map(int,readline().split()))
DIV = 10 ** 9 + 7
# 各桁の1と0の数を数えて、((1の数) * (0の数)) * (2 ** i桁目(1桁目を0とする))を数える
K = max(A) # 最も大きい数が0になるまでループ
ans = 0
j = 0
while K:
one = 0
for i in range(len(A)):
if (A[i] >> j) & 1:
one += 1
ans += (one % DIV) * ((N - one) % DIV) * pow(2,j,DIV)
ans %= DIV
j += 1
K >>= 1
print(ans)
| 0 | null | 62,180,012,542,300 | 56 | 263 |
import sys
N, T = map(int, input().split())
S = sys.stdin.readlines()
A = []
B = []
for s in S:
a, b = map(int, s.split())
A.append(a)
B.append(b)
dp1 = [[0] * T for _ in range(N + 1)]
for i in range(N):
for j in range(T):
here = dp1[i][j]
if j < A[i]:
dp1[i + 1][j] = here
else:
dp1[i + 1][j] = max(here, dp1[i][j - A[i]] + B[i])
dp2 = [[0] * T for _ in range(N + 2)]
for i in range(N, 0, -1):
for j in range(T):
here = dp2[i + 1][j]
if j < A[i - 1]:
dp2[i][j] = here
else:
dp2[i][j] = max(here, dp2[i + 1][j - A[i - 1]] + B[i - 1])
ans = 0
for i in range(1, N + 1):
t = 0
for j in range(T):
t2 = dp1[i - 1][j] + dp2[i + 1][T - 1 - j]
t = max(t, t2)
ans = max(ans, t + B[i - 1])
print(ans)
|
n, t = map(int, input().split())
ab = [tuple(map(int, input().split())) for i in range(n)]
m, d = 0, [0] * t
for a, b in sorted(ab):
m = max(m, b+d[t-1])
for l in range(t-1,a-1,-1):
if(b+d[l-a] > d[l]):
d[l] = b+d[l-a]
print(m)
| 1 | 151,589,335,452,320 | null | 282 | 282 |
import math
a,b = [int(x) for x in input().split()]
ma = int(a//0.08)
mb = int(b//0.1)
fmindi = False
mindi = 9999999999999
for x in range(min(ma,mb),max(ma,mb)+2):
j = math.floor(x*0.08)
e = math.floor(x*0.1)
if j == a and e == b:
fmindi = True
mindi = min(mindi,x)
print(mindi if fmindi else -1)
|
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
A, B = MI()
x = 10 * B
for i in range(x, x + 10):
y = int(i * 8 / 100)
if y == A:
print(i)
exit()
print(-1)
if __name__ == "__main__":
main()
| 1 | 56,266,593,439,200 | null | 203 | 203 |
N = int(input())
x = []
y = []
for i in range(N):
in1, in2 = list(map(int,input().split()))
x.append(in1)
y.append(in2)
#f = [[0]*2 for i in range(N)]
X=[]
Y=[]
for i in range(N):
#f[i][0] = x[i]-x[i]
#f[i][1] = x[i]+y[i]
X.append(x[i]-y[i])
Y.append(x[i]+y[i])
dam1 = max(X) - min(X)
dam2 = max(Y) - min(Y)
print(max(dam1,dam2))
|
import sys
input=sys.stdin.buffer.readline
#sys.setrecursionlimit(10**9)
#from functools import lru_cache
def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode('utf-8')
def main():
n=II()
XY=[LI() for _ in range(n)]
A=[]
B=[]
for x,y in XY:
A.append(x+y)
B.append(x-y)
A.sort()
B.sort()
a=A[-1]-A[0]
b=B[-1]-B[0]
print(max(a,b))
if __name__=="__main__":
main()
| 1 | 3,394,671,695,360 | null | 80 | 80 |
count = 0
input()
num = [int(n) for n in input().split()]
for i in range(len(num)):
minj = i
for j in range(i, len(num)):
if num[j] < num[minj]:
minj = j
if minj != i:
num[i], num[minj] = num[minj], num[i]
count += 1
print(*num)
print(count)
|
import math
from collections import deque
N,D,A = map(int,input().split(' '))
XHn = [ list(map(int,input().split(' '))) for _ in range(N)]
ans = 0
XHn.sort()
attacks = deque([]) # (区間,攻撃回数)
attack = 0
ans = 0
for i in range(N):
x = XHn[i][0]
h = XHn[i][1]
while len(attacks) > 0 and x > attacks[0][0]:
distance,attack_num = attacks.popleft()
attack -= attack_num*A
if h > attack:
diff = h - attack
distance_attack = x + D*2
need_attack = math.ceil(diff/A)
attacks.append((distance_attack,need_attack))
attack += A*need_attack
ans += need_attack
print(ans)
| 0 | null | 40,900,085,992,794 | 15 | 230 |
from collections import deque
process = deque(maxlen=100000)
n, q = [int(x) for x in input().split(' ')]
for i in range(n):
name, time = input().split(' ')
process.append([name, int(time)])
total_time = 0
while process:
p_q = process.popleft()
if p_q[1] > q:
total_time += q
p_q[1] -= q
process.append(p_q)
else:
total_time += p_q[1]
print('{} {}'.format(p_q[0], total_time))
|
from collections import deque
n,p=map(int ,input().split())
que=deque([])
for i in range(n):
name,time=input().split()
time=int(time)
que.append([name,time])
t=0
while len(que)>0:
atop=que.popleft()
spend=min(atop[1],p)
atop[1]-=spend
t+=spend
if(atop[1]==0):
print(atop[0],t)
else:
que.append(atop)
| 1 | 42,813,518,940 | null | 19 | 19 |
n=int(input())
arr=list(map(int,input().strip().split()))
ans=[0]*n
for i in range(n):
ans[arr[i]-1]=i+1
print(*ans,sep=" ")
|
N = int(input())
A = list(map(int, input().split()))
t = [(i + 1, A[i]) for i in range(N)]
t_sorted = sorted(t, key=lambda a: a[1])
ans = ''
for i in range(N):
ans += str(t_sorted[i][0]) + ' '
print(ans)
| 1 | 180,711,559,560,698 | null | 299 | 299 |
s = input()
if len(set(s)) ==2:
print("Yes")
else:
print("No")
|
s = input()
if 'A' in s and 'B' in s: print('Yes')
else: print('No')
| 1 | 54,964,423,593,920 | null | 201 | 201 |
from heapq import heappush, heappop
import sys
input = sys.stdin.readline
N = int(input())
pos = []
zero = []
neg = []
for _ in range(N):
S = input().rstrip()
m = s = 0
for c in S:
if c == '(':
s += 1
else:
s -= 1
m = min(m, s)
if s > 0:
heappush(pos, (-m, s)) # take larger mins first
elif s == 0:
heappush(zero, (m, s))
else:
heappush(neg, (m - s, s)) # take smaller mins first
num = 0
while pos:
m, s = heappop(pos)
if num - m < 0:
print('No')
exit()
num += s
while zero:
m, s = heappop(zero)
if num + m < 0:
print('No')
exit()
while neg:
m, s = heappop(neg)
m += s
if num + m < 0:
print('No')
exit()
num += s
if num == 0:
print('Yes')
else:
print('No')
|
from collections import namedtuple
Line = namedtuple('Line', ['lowest', 'end'])
def main():
N = int(input())
up_lines = []
down_lines = []
for i in range(N):
s = input()
now = 0
lowest = 0
for c in s:
if c == "(":
now += 1
else:
now -= 1
lowest = min(lowest, now)
if now > 0:
up_lines.append(Line(lowest, now))
else:
down_lines.append(Line(lowest-now, -now))
up_lines.sort(key=lambda line: -line.lowest)
down_lines.sort(key=lambda line: -line.lowest)
left = 0
for line in up_lines:
if left + line.lowest < 0:
print("No")
return
left += line.end
if left < 0:
print("No")
break
right = 0
for line in down_lines:
if right + line.lowest < 0:
print("No")
return
right += line.end
if right < 0:
print("No")
break
if left == right:
print("Yes")
else:
print("No")
if __name__ == "__main__":
main()
| 1 | 23,694,002,490,742 | null | 152 | 152 |
from sys import stdin
from collections import defaultdict as dd
from collections import deque as dq
import itertools as it
from math import sqrt, log, log2, cos, pi
from fractions import Fraction
n, m = map(int, stdin.readline().split())
g = [[] for _ in range(n+1)]
for _ in range(m):
a, b = map(int, stdin.readline().split())
g[a].append(b)
g[b].append(a)
signposts = [0]*(n+1)
queue = dq([1])
visited = [0]*(n+1)
visited[1] = 1
while queue:
parent = queue.popleft()
for children in g[parent]:
if not visited[children]:
# print('dbg:', parent, children)
signposts[children] = parent
queue.append(children)
visited[children] = 1
if 0 in signposts[2:]:
print('No')
else:
print('Yes')
print(*signposts[2:], sep='\n')
# t = int(stdin.readline())
# for _ in range(t):
# n, m = map(int, stdin.readline().split())
# nums = list(map(int, stdin.readline().split()))
# n = int(input())
# if n%10 in [2, 4, 5, 7, 9]:
# print('hon')
# elif n%10 in [0, 1, 6, 8]:
# print('pon')
# else:
# print('bon')
# k = int(input())
# s = input()
# ans = s[:k] + ('...' if len(s) > k else '')
# print(ans)
# hrhand, minhand, hr, minut = map(int, stdin.readline().split())
# angmin =360*minut/60
# anghr =360*(hr + minut/60)/12
# totang = abs(angmin - anghr)
# print((hrhand**2 + minhand**2 - 2*hrhand*minhand*cos(totang*pi/180))**0.5)
|
N, M = map(int, input().split())
pairs = [[] for _ in range(N + 1)] # pairs[i]:iと直接つながっている部屋のリスト
for _ in range(M):
input_list = list(map(int, input().split()))
pairs[input_list[0]].append(input_list[1])
pairs[input_list[1]].append(input_list[0])
ans = [0 for _ in range(N + 1)]
n_ans = 0 # ansを埋めた回数 -> N-1になったら終了
done = [False for _ in range(N + 1)] # 探索済リスト
todo = [1]
while n_ans < N - 1:
for n in todo:
for m in pairs[n]:
if not done[m]:
done[m] = True
ans[m] = n
n_ans += 1
todo.append(m)
print('Yes')
for i in range(2, N + 1):
print(ans[i])
| 1 | 20,490,401,562,710 | null | 145 | 145 |
'''
ITP-1_6-B
????¶???????????????????????????????
???????????±?????¨?????????????????????????????????????????¨????????¨?????????52?????????????????????????????? n ???????????????????????????????????????????????????????????? n ????????????????????\?????¨??????????¶??????????????????????????????????????????°?????????????????????????????????
???????????????????????£?????????????????????????????§??????????????????52?????????????????§??????
52??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????13??????????????????????????????
???Input
?????????????????????????????£??????????????????????????° n (n ??? 52)????????????????????????
?¶??????? n ??????????????????????????????????????????????????????????????????????????????????????§???????????????????????¨??´??°??§??????
????????????????????????????????¨?????????????????????'S'???????????????'H'???????????????'C'???????????????'D'??§??¨?????????????????????
??´??°??????????????????????????????(1 ??? 13)?????¨??????????????????
???Output
?¶??????????????????????????????????1???????????????????????????????????????????????\?????¨????§?????????????????????§???????????????????????¨??´??°??§?????????????????????????????????????????\????????¨????????¨????????????
???????????????????????????????????????????????????????????????????????§???????????????????????????
????????????????????´??????????????????????°????????????????????????????
'''
# import
# ?????°??????????????????
trumpData = {
"S": list(range(1,14)),
"H": list(range(1,14)),
"C": list(range(1,14)),
"D": list(range(1,14)),
}
taroCard = int(input())
# ??????????????§??????????????????????????????????????????
for nc in range(taroCard):
(trumpDataLostMark, trumpDataLostCnt) = input().split()
index = trumpData[trumpDataLostMark].index(int(trumpDataLostCnt))
del trumpData[trumpDataLostMark][index]
# ????¶???????????????????????????????
for trumpDataMark in ['S', 'H', 'C', 'D']:
for trumpDataCnt in trumpData[trumpDataMark]:
# Output
print(trumpDataMark, trumpDataCnt)
|
n = int(input())
cnt = 0
for i in range(1, n//2+1):
if i != n - i and n - i > 0:
cnt += 1
print(cnt)
| 0 | null | 76,877,149,516,980 | 54 | 283 |
import sys
#fin = open("test.txt", "r")
fin = sys.stdin
n = int(fin.readline())
d = {}
for i in range(n):
op, str = fin.readline().split()
if op == "insert":
d[str] = 0
else:
if str in d:
print("yes")
else:
print("no")
|
import math
def plot(x,y):
print(f"{x:.8f} {y:.8f}")
def koch(n,x1,y1,x2,y2):
if n==0:
plot(x1,y1)
return
ax=(2*x1+x2)/3
ay=(2*y1+y2)/3
bx=(x1+2*x2)/3
by=(y1+2*y2)/3
cx=(bx-ax)*(1/2)-(by-ay)*(math.sqrt(3)/2)+ax
cy=(bx-ax)*(math.sqrt(3)/2)+(by-ay)*(1/2)+ay
koch(n-1,x1,y1,ax,ay)
koch(n-1,ax,ay,cx,cy)
koch(n-1,cx,cy,bx,by)
koch(n-1,bx,by,x2,y2)
n=int(input())
koch(n,0,0,100,0)
plot(100,0)
| 0 | null | 96,724,863,828 | 23 | 27 |
s=input()
l=len(s)
l2=l//2
count=0
for i in range(l2):
if s[i]!=s[l-1-i]:
count+=1
print(count)
|
def main1():
s = input()
s1 = s[0:len(s)//2]
s2 = s[::-1]
s2 = s2[0:len(s)//2]
ans = 0
for i in range(len(s)//2):
if s1[i]==s2[i]:
continue
else:
ans+=1
print(ans)
if __name__ == '__main__':
main1()
| 1 | 119,722,792,172,994 | null | 261 | 261 |
n, k = map(int, input().split())
MOD = 1000000007
def combinations(n, r, MOD):
if (r < 0) or (n < r):
return 0
r = min(r, n - r)
return fact[n] * fact_inv[r] * fact_inv[n - r] % MOD
fact = [1, 1]
fact_inv = [1, 1]
inv = [0, 1]
for i in range(2, n + 1):
fact.append((fact[-1] * i) % MOD)
inv.append((-inv[MOD % i] * (MOD // i)) % MOD)
fact_inv.append((fact_inv[-1] * inv[-1]) % MOD)
s = 0
for num_zero in range(min(k + 1, n)):
# nCz
x = combinations(n, num_zero, MOD)
# n-zCx
y = combinations(n - 1, n - num_zero - 1, MOD)
s += (x * y) % MOD
print(s % MOD)
|
N ,K = map(int, input().split())
MOD = 10**9 + 7
ans = 0
S = [0]*(2*10**5+1)
S[1] = 1
for i in range(2, 2*10**5+1):
S[i] = S[MOD%i]*(MOD-int(MOD/i))%MOD
if N-1 <= K:
ans = 1
num = 2*N - 1
for i in range(N-1):
ans *= (num-i)*S[i+1]
ans %= MOD
print(ans%MOD)
else:
S1 = [1]*N
S2 = [1]*N
for i in range(1, N):
S1[i] = (S1[i-1]*(N+1-i)*S[i])%MOD
S2[i] = (S2[i-1]*(N-i)*S[i])%MOD
ans = 0
for i in range(K+1):
ans += S1[i]*S2[N-1-i]
ans %= MOD
print(ans)
# nCm * n-mHm
# nCm * n-1Cn-m-1
| 1 | 67,296,521,581,912 | null | 215 | 215 |
X, K, D = [int(v) for v in input().split()]
X = abs(X)
m = X // D
p = K - m if m <= K else 0
if p == 0:
print(X - (K*D))
else:
X -= m * D
if p & 1:
print(abs(X-D))
else:
print(X)
|
# ?????§?????????P???K??°?????????????????§???????????????????????????????
def check(P):
i = 0
for j in range(k):
s = 0
while s + w[i] <= P:
s += w[i]
i += 1
if i == n:
return n
return i
def solve():
left = 0
right = sum(w) + 1 # ??????????????°???* 1????????????????????§??????
while right - left > 1:
mid = (left + right) / 2
v = check(mid) # mid == P ??¨???????????§???????????????????????????
if v >= n:
right = mid
else:
left = mid
return right
if __name__ == "__main__":
n, k = map(int, input().split())
w = [int(input()) for i in range(n)]
print(int(solve()))
| 0 | null | 2,661,209,797,468 | 92 | 24 |
n = int(input())
a = list(map(int, input().split()))
cnt = 0
ave = sum(a)/2
for i in range(n):
cnt += a[i]
if cnt >= ave:
ans = min(cnt*2-ave*2, ave*2-(cnt-a[i])*2)
break
print(int(ans))
|
x = raw_input()
Y = int(x)
Y = Y*Y*Y
print Y
| 0 | null | 71,316,113,722,860 | 276 | 35 |
#def area###############################
def pr(s,a,b):
print s[int(a):int(b)+1]
def rv(s,a,b):
# a="abcdefghijklmn"len=14
# ra="nmlkjihgfedcba"
# edc=9,10,11
# ans="abedcfghijklmn"
# reverse 2 4
# 14-b-1,14-a-1
# "
tmp=s[::-1]
news=s[:int(a)]+tmp[len(tmp)-int(b)-1:len(tmp)-int(a)]+s[int(b)+1:]
return news
def rp(s,a,b,p):
news=s[:int(a)]+p+s[int(b)+1:]
return news
########################################
I=raw_input()
n=int(input())
for i in range(n):
k=raw_input().split(" ")
if k[0]=="replace":
I=rp(I,k[1],k[2],k[3])
elif k[0]=="reverse":
I=rv(I,k[1],k[2])
elif k[0]=="print":
pr(I,k[1],k[2])
|
N,M = tuple(map(int,input().split()))
# matchlist = [tuple(map(lambda x:int(x)-1,input().split())) for i in range(M)]
#最大のグループの大きさを出す
friendlist = list(range(N))
# [所属先,[所属メンバー]]
gplist = [[i] for i in range(N)]
def getgroup(a):
A = friendlist[a]
if A == a:
return a
else:
return getgroup(A)
def match(ab):
A = getgroup(ab[0])
B = getgroup(ab[1])
if A==B:
return
elif len(gplist[A])<len(gplist[B]):
gplist[B].extend(gplist[A])
friendlist[A] = B
else:
gplist[A].extend(gplist[B])
friendlist[B] = A
for i in range(M):
# print(friendlist)
# print(gplist)
match(tuple(map(lambda x:int(x)-1,input().split())))
# print(friendlist)
# print(gplist)
print(max([len(item) for item in gplist]))
| 0 | null | 3,011,157,027,132 | 68 | 84 |
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a, b = [0]*(N+1), [0]*(M+1)
for i in range(N):
a[i+1] = a[i] + A[i]
for i in range(M):
b[i+1] = b[i] + B[i]
ans, j = 0, M
for i in range(N + 1):
if a[i] > K:
break
while b[j] > K - a[i]:
j -= 1
ans = max(ans, i + j)
print(ans)
|
x = int(input())
money = 100
k = 1
cnt = 0
while money < x:
money += k
k = money // 100
cnt += 1
print(cnt)
| 0 | null | 18,876,352,367,296 | 117 | 159 |
# coding=utf-8
a, b = map(int, input().split())
if a > b:
big_num = a
sml_num = b
else:
big_num = b
sml_num = a
while True:
diviser = big_num % sml_num
if diviser == 0:
break
else:
big_num = sml_num
sml_num = diviser
print(sml_num)
|
def gcd(a,b):
x,y = (a,b) if a>b else (b,a)
if x % y == 0:
return y
else:
return gcd(y, x % y)
a,b = map(int, input().split())
print(gcd(a,b))
| 1 | 7,959,747,120 | null | 11 | 11 |
n=int(input())
cards = [[s+" "+str(n) for n in range(1,14)] for s in ["S","H","C","D"]]
for _ in range(n):
suit,num =input().split()
if suit=="S":
cards[0][int(num)-1]=0
elif suit=="H":
cards[1][int(num)-1]=0
elif suit=="C":
cards[2][int(num)-1]=0
elif suit=="D":
cards[3][int(num)-1]=0
for s in cards:
for n in s:
if n!=0:
print(n)
|
N,M=list(map(int, input().split()))
src=[i+1 for i in range(M*2+1)]
if M%2==0:
for m in range(M//2):
a=src[m]
b=src[M-m]
print('{} {}'.format(a,b))
for m in range(M//2):
a=src[M+1+m]
b=src[-(m+1)]
print('{} {}'.format(a,b))
else:
for m in range(M//2):
a=src[m]
b=src[M-1-m]
print('{} {}'.format(a,b))
for m in range(M-(M//2)):
a=src[M+m]
b=src[-(m+1)]
print('{} {}'.format(a,b))
| 0 | null | 14,941,127,463,808 | 54 | 162 |
from collections import deque
def read():
N, u, v = list(map(int, input().strip().split()))
G = [list() for i in range(N+1)]
for i in range(N-1):
a, b = list(map(int, input().strip().split()))
G[a].append(b)
G[b].append(a)
return N, u, v, G
def bfs(N, G, start):
depth = [-1 for i in range(N+1)]
depth[start] = 0
q = deque()
q.append(start)
while(len(q) > 0):
s = q.popleft()
for t in G[s]:
if depth[t] == -1:
depth[t] = depth[s] + 1
q.append(t)
return depth
def solve(N, u, v, G):
depth_u = bfs(N, G, u)
depth_v = bfs(N, G, v)
most_deepest_node = u
for i in range(1, N+1):
if depth_u[i] < depth_v[i]:
if depth_v[i] > depth_v[most_deepest_node]:
most_deepest_node = i
neighbor_node = G[most_deepest_node][0]
step_u = depth_u[neighbor_node]
step_v = depth_v[neighbor_node]
return step_v
if __name__ == '__main__':
inputs = read()
print('%d' % solve(*inputs))
|
vec1 = []
vec2 = []
n, m = map(int, input().split())
for x in range(1, n+1):
vec1.append(list(map(int, input().split())))
for y in range(m):
vec2.append(int(input()))
for j in vec1:
result = 0
for w, z in zip(j, vec2):
result += w * z
print(result)
| 0 | null | 59,347,999,728,772 | 259 | 56 |
print('Yes') if int(input())>=30 else print('No')
|
a=int(input(''))
if a>=30:
print('Yes')
else:
print('No')
| 1 | 5,823,298,334,260 | null | 95 | 95 |
N = int(input())
a = N % 10
if a == 0 or a == 1 or a == 6 or a == 8:
print("pon")
elif a == 3:
print("bon")
else:
print("hon")
|
num = list(map(int,input()))
if num[-1] == 3:
print("bon")
elif num[-1] in (0, 1, 6, 8):
print("pon")
else:
print("hon")
| 1 | 19,176,591,471,948 | null | 142 | 142 |
m1, d1 = map(int, input().split())
m2, d2 = map(int, input().split())
if m1 == m2:
print('0')
else:print('1')
|
S = input()
print("Yes" if S.count("A") and S.count("A") < 3 or S.count("B") and S.count("B") < 3 else "No")
| 0 | null | 89,288,030,370,720 | 264 | 201 |
N=int(input())
D=[{} for _ in range(N+1)]
for i in range(1,N+1):
A=int(input())
for k in range(A):
p,x=map(int,input().split())
D[i][p]=x
Max=0
for x in range(2**N):
y=x
B=["*"]*(N+1)
for i in range(N):
B[i+1]=y%2
y>>=1
Flag=True
for i in range(1,N+1):
if B[i]==1:
for k in D[i]:
Flag&=(B[k]==D[i][k])
if Flag:
Max=max(Max,B.count(1))
print(Max)
|
arr = list(map(int, input().split()))
print(arr.index(0) + 1)
| 0 | null | 67,358,484,372,138 | 262 | 126 |
n = input()
if n.isupper():
print("A")
else :
print('a')
|
import sys
def resolve(in_):
N, K = map(int, next(in_).split())
A = map(int, next(in_).split())
number_plus1 = list(i + 1 for i in A)
v = [0] * N
v[0] = sum(number_plus1[:K])
for i, (before, after) in enumerate(zip(number_plus1, number_plus1[K:]), 1):
v[i] = v[i - 1] - before + after
expected = max(v) / 2
return expected
def main():
answer = resolve(sys.stdin.buffer)
print(f'{answer:.12f}')
if __name__ == '__main__':
main()
| 0 | null | 43,280,747,880,232 | 119 | 223 |
'''
ITP-1_5-C
?????§?????????????????????
??\????????????????????????H cm ?????? W cm ????????§????????????????????¢?????????????????°?????????????????????????????????
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#
?????????????????? 6 cm ?????? 10 cm ???????????¢?????¨??????????????????
????????¢???????????? "#" ??¨????????????????????????????????????
???Input
??\???????????°????????????????????????????§???????????????????????????????????????????????????¢????????\????????¨????????§??????
H W
H, W ?????¨?????? 0 ?????¨????????\?????????????????¨????????????
???Output
?????????????????????????????????????????? H cm ?????? W cm ?????????????????????????????????
?????????????????????????????????????????????????????\??????????????????
'''
# import
import sys
for line in sys.stdin:
# ?????°??????????????????
H, W = map(int, line.split())
if H == 0 and W == 0:
break
# ?????¢??????
for a in range(H):
# ?\???°?????????
if a%2 == 1:
for b in range(W):
if b%2 == 1:
# ??????????????§'#'??????
print('#', end='')
else:
# ??????????????§'.'??????
print('.', end='')
# ???????????????????????????
print('')
# ??¶??°?????????
else:
for c in range(W):
if c%2 == 1:
# ??????????????§'.'??????
print('.', end='')
else:
# ??????????????§'#'??????
print('#', end='')
# ???????????????????????????
print('')
# ????????????????????????
print('')
|
# import time
N,P = list(map(int,input().split()))
S = input()
if P == 2:
c = 0
for i in range(N):
u = int(S[i])
if u % 2 == 0:
c += i+1
print(c)
elif P == 5:
c = 0
for i in range(N):
u = int(S[i])
if u % 5 == 0:
c += i+1
print(c)
else:
U = [0]
a = 0
t = 1
for i in range(N):
t %= P
a += int(S[-i-1])*t
a %= P
t *= 10
U.append(a)
U.append(10001)
# print(U)
# t3 = time.time()
c = 0
"""
for i in range(P):
m = U.count(i)
c += m*(m-1)//2
print(c)
"""
U.sort()
# print(U)
idx = 0
q = -1
for i in range(N+2):
if q != U[i]:
q = U[i]
m = i - idx
idx = i
c += m*(m-1)//2
print(c)
# t4 = time.time()
# print(t4-t3)
# 10032
| 0 | null | 29,550,160,110,268 | 51 | 205 |
import math
import decimal
A, B = map(str, input().split())
A = int(A)
B = decimal.Decimal(B)
print(math.floor(A*B))
|
M, N = input().split() # 2つ整数の読み取り
A=int(M)
B=N.replace(".","")
B=int(B)
C=A*B//100
print(C)
| 1 | 16,621,134,145,318 | null | 135 | 135 |
A, B = input().split()
A = int(A)
B = round(float(B) * 100)
ans = (A * B) // 100
print(int(ans))
|
import math , sys
from decimal import Decimal
A , B = list( map( Decimal , input().split()))
print( int(A * B ) )
| 1 | 16,619,912,159,510 | null | 135 | 135 |
n, m, k = [int(i) for i in input().split()]
subsets = []
splits = [i for i in range(1, n)]
n -= 1
for i in range(2**n):
x = []
for j in range(n):
if i&(1<<j):
x.append(j)
subsets.append(x)
n+=1
A = []
for i in range(n):
A.append([int(j) for j in input()])
ans = n*m
for subset in subsets:
cur_ans = len(subset)
tmp = []
col = 0
subset.append(100000)
running_sum = [0 for i in range(len(subset))]
while col<m:
row_ptr = 0
for row in range(n):
if row > subset[row_ptr]:
row_ptr += 1
running_sum[row_ptr] += A[row][col]
if running_sum[row_ptr] > k:
col -= 1
running_sum = [0 for i in range(len(subset))]
cur_ans += 1
break
col += 1
if cur_ans >= ans:
break
ans = min(ans, cur_ans)
print(ans)
|
N, X, M = map(int, input().split())
A = X
ans = A
visited = [0]*M
tmp = []
i = 2
while i <= N:
A = (A*A) % M
if visited[A] == 0:
visited[A] = i
tmp.append(A)
ans += A
else:
ans += A
loop_length = i-visited[A]
loop_val = tmp[-1*loop_length:]
loop_count = (N-i) // loop_length
ans += sum(loop_val) * loop_count
visited = [0]*M
i += loop_length * loop_count
i += 1
print(ans)
| 0 | null | 25,591,524,966,660 | 193 | 75 |
K = int(input())
S = input()
N = len(S)
MOD = 10**9 + 7
fct = [1]
invfct = [1]
pow25 = [1]
pow26 = [1]
for i in range(1, K+N+1):
fct.append(fct[i-1] * i % MOD)
invfct.append(pow(fct[i], MOD-2, MOD))
pow25.append(pow25[i-1] * 25 % MOD)
pow26.append(pow26[i-1] * 26 % MOD)
def cmb(n, k):
return fct[n] * invfct[k] * invfct[n-k]
ans = 0
for i in range(K+1):
temp = pow25[i]
temp *= cmb(i+N-1, N-1)
temp *= pow26[K-i]
ans += temp
ans %= MOD
print(ans)
|
N=int(input())
if N>=400 and 599>=N:
print('8')
if N>=600 and 799>=N:
print('7')
if N>=800 and 999>=N:
print('6')
if N>=1000 and 1199>=N:
print('5')
if N>=1200 and 1399>=N:
print('4')
if N>=1400 and 1599>=N:
print('3')
if N>=1600 and 1799>=N:
print('2')
if N>=1800 and 1999>=N:
print('1')
| 0 | null | 9,782,501,169,078 | 124 | 100 |
def main():
N = int(input())
A = list(map(int, input().split(' ')))
A, count = selectionSort(A, N)
print(' '.join([str(a) for a in A]))
print(count)
def selectionSort(A, N):
count = 0
for i in range(0, N-1):
minj = i
for j in range(i, N):
if A[j] < A[minj]:
minj = j
if i != minj:
A[i], A[minj] = A[minj], A[i]
count += 1
return A, count
if __name__ == '__main__':
main()
|
cnt = 0
n = int(input())
lst = list(map(int, input().split()))
for i in range(n):
m = i
for j in range(i, n):
if lst[j] < lst[m]:
m = j
if m != i:
lst[i], lst[m] = lst[m], lst[i]
cnt += 1
print(*lst)
print(cnt)
| 1 | 19,678,493,990 | null | 15 | 15 |
f = [0 for i in range(0, 10001)]
for x in range(1,100):
for y in range(1,100):
for z in range(1,100):
n = x**2 + y**2 + z**2 + x*y + y*z + z*x
if n <= 10000:
f[n] += 1
N = int(input())
for n in range(1, N+1):
print(f[n])
|
from sys import stdin
inp = lambda : stdin.readline().strip()
n = int(inp())
a = [0]*10**5
for i in range(1,101):
for j in range(1,101):
for k in range(1,101):
a[i**2 + j**2 + k**2 + i*j + i*k + k*j] += 1
for i in a[1:n+1]:
print(i)
| 1 | 7,994,002,406,460 | null | 106 | 106 |
import sys
sys.setrecursionlimit(10 ** 9)
if __name__ == '__main__':
n = int(input())
tree = [[] for i in range(n)]
for i in range(n - 1):
a, b = map(int, input().split())
tree[a - 1].append([b - 1, i])
ans = 0
for i in tree:
ans = max(ans, len(i))
ans = [0] * (n - 1)
def solve(cur_index, color):
cnt = 1
for to, j in tree[cur_index]:
if cnt == color:
cnt += 1
ans[j] = cnt
solve(to, cnt)
cnt += 1
solve(0, 0)
print(max(ans))
print(*ans, sep="\n")
|
# coding: utf-8
# Your code here!
a = input()
big = ["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"]
small = ['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']
if a in big:
print("A")
else:
print("a")
| 0 | null | 73,695,553,237,900 | 272 | 119 |
import math
import numpy as np
N = int(input())
ARR = list(map(int,input().split()))
def calculate(n,arr):
maxValue = max(arr)
if maxValue == 0:
maxNum = 1
else:
maxNum = math.ceil(math.log(maxValue,2))
arr = np.array(arr)
oneArray = [0]*(maxNum+1)
zeroArray = [0]*(maxNum+1)
for i in range(maxNum+1):
s = (arr >> i) & 1
a = np.count_nonzero(s)
oneArray[i] = a
zeroArray[i] = n - a
finalResult = 0
for i in range(maxNum + 1):
finalResult += 2**i * oneArray[i] * zeroArray[i]
print(finalResult % (10**9 + 7))
calculate(N, ARR)
|
#入力部
C= 998244353
N,M,K=map(int,input().split())
#定義部
U=max(N+1,K)
F=[0]*U
G=[0]*U
F[0]=1
for i in range(1,U):
F[i]=(F[i-1]*i)%C
G[i]=pow(F[-1],C-2,C)
for j in range(U-2,-1,-1):
G[j]=(G[j+1]*(j+1))%C
def nCr(n,r):
if r<0 or n<r:
return 0
else:
return (F[n]*G[r]*G[n-r])
#メイン部
S=0
for k in range(0,K+1):
S=(S+M*pow(M-1,N-k-1,C)*nCr(N-1,k))%C
print(S%C)
| 0 | null | 73,068,899,869,088 | 263 | 151 |
arr = [[[0 for i1 in range(10)] for i2 in range(3)] for i3 in range(4)]
count=input()
for l in range(int(count)):
b,f,r,v=input().split()
arr[int(b)-1][int(f)-1][int(r)-1]+=int(v)
first_b = arr[0]
second_b = arr[1]
third_b = arr[2]
fourth_b= arr[3]
for m in range(3):
for n in range(10):
print(" "+str(first_b[m][n]),end="")
print()
print("#"*20)
for m in range(3):
for n in range(10):
print(" "+str(second_b[m][n]),end="")
print()
print("#"*20)
for m in range(3):
for n in range(10):
print(" "+str(third_b[m][n]),end="")
print()
print("#"*20)
for m in range(3):
for n in range(10):
print(" "+str(fourth_b[m][n]),end="")
print()
|
import sys
from collections import Counter
H,W,m = map(int,input().split())
h_ls = [0] * H
w_ls = [0] * W
bombers = [(0,0) for _ in range(m)]
for i in range(m):
h,w = map(int,input().split())
h_ls[h-1] += 1
w_ls[w-1] += 1
bombers[i] = (h-1,w-1)
h_max = max(h_ls)
h_counter = Counter(h_ls)
# Couter使ってみる
h_max_args = [0]*h_counter[h_max]
next_ind = 0
for i in range(H):
if h_ls[i] == h_max:
h_max_args[next_ind] = i
next_ind += 1
w_max = max(w_ls)
w_counter = Counter(w_ls)
w_max_args = [0]*w_counter[w_max]
next_ind = 0
for i in range(W):
if w_ls[i] == w_max:
w_max_args[next_ind] = i
next_ind += 1
bombers = set(bombers)
for h in h_max_args:
for w in w_max_args:
if not (h,w) in bombers:
print(w_max+h_max)
sys.exit()
print(w_max+h_max-1)
| 0 | null | 2,889,605,150,798 | 55 | 89 |
S=input()
T=input()
N = len(S)
count = 0
for n in range(N):
if S[n]!=T[n]:
count+=1
print(count)
|
def main(S, T):
N = len(S)
ans = 0
for i in range(N//2):
if S[i] != T[i]:
ans += 1
if S[-1-i] != T[-1-i]:
ans += 1
if N%2 == 1:
if S[N//2] != T[N//2]:
ans += 1
return ans
if __name__ == '__main__':
S = input()
T = input()
ans = main(S, T)
print(ans)
| 1 | 10,501,626,236,738 | null | 116 | 116 |
from collections import defaultdict
N = int(input())
List = list(map(int,input().split()))
dicL = defaultdict(int)
dicR = defaultdict(int)
for i in range(N):
dicL[List[i]+i] += 1
dicR[i - List[i]] += 1
counter = 0
for item in dicL:
if item in dicR:
counter += dicL[item]*dicR[item]
print(counter)
|
n=int(input())
l=list(map(int,input().split()))
dp=[[0 for j in range(n+1)] for i in range(n+1)]
e=[]
for i in range(n):
e.append([l[i],i])
e.sort(reverse=True)
for i in range(1,n+1):
p=e[i-1][0]
q=e[i-1][1]
for j in range(i+1):
if j<i:
a1=dp[i-1][j]+p*abs(n-(i-j)-q)
else:
a1=0
if j>0:
a2=dp[i-1][j-1]+p*abs(q-j+1)
else:
a2=0
dp[i][j]=max(a1,a2)
print(max(dp[-1]))
| 0 | null | 29,776,026,312,180 | 157 | 171 |
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
def dfs(curr, pare, pare_c, k, color_dic):
curr_c = pare_c
for chi in g[curr]:
if chi == pare: continue
curr_c = curr_c%k + 1
color_dic[(curr,chi)] = curr_c
dfs(chi, curr, curr_c, k, color_dic)
n = int(input())
g = [ [] for _ in range(n+1)]
gl = []
for i in range(n-1):
a, b = map(int, input().split())
g[a].append(b)
g[b].append(a)
gl.append((a,b))
k = 0
for node in g:
k = max(len(node),k)
color_dic = {}
dfs(1, -1, 0, k, color_dic)
print(k)
for edge in gl:
print(color_dic[edge])
|
# Sheep and Wolves
S, W = map(int, input().split())
print(['safe', 'unsafe'][W >= S])
| 0 | null | 82,839,509,444,188 | 272 | 163 |
N, P = map(int, input().split())
S = input()
if P == 2 or P == 5:
cnt = 0
cnt_a = 0
for s in S[::-1]:
if int(s) % P == 0:
cnt_a += 1
cnt += cnt_a
else:
r_lst = [0] * P
r_lst[0] = 1
cnt = 0
num = 0
for i, s in enumerate(S[::-1]):
num = (num + int(s) * pow(10, i, P)) % P
cnt += r_lst[num]
r_lst[num] += 1
print(cnt)
|
n=int(input())
s='*'+input()
r=s.count('R')
g=s.count('G')
b=s.count('B')
cnt=0
for i in range(1,n+1):
for j in range(i,n+1):
k=2*j-i
if k>n:
continue
if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
cnt+=1
print(r*g*b-cnt)
| 0 | null | 47,026,775,609,286 | 205 | 175 |
n = int(input())
brackets_plus = []
brackets_minus = []
right = 0
left = 0
for i in range(n):
s = input()
cur = 0
m = 0
for j in range(len(s)):
if s[j] == "(":
left += 1
cur += 1
else:
right += 1
cur -= 1
m = min(m,cur)
if cur >= 0:
brackets_plus.append((m,cur))
else:
brackets_minus.append((m,cur,m-cur))
if right != left:
print("No")
exit()
cur = 0
brackets_plus.sort(reverse = True)
for i in brackets_plus:
if i[0] + cur < 0:
print("No")
exit()
cur += i[1]
brackets_minus.sort(key = lambda x:x[2])
for i in brackets_minus:
if i[0] + cur < 0:
print("No")
exit()
cur += i[1]
print("Yes")
|
while True:
h, w = map(int, input().split())
if h == w == 0: break
print('#' * w + '\n' + (('#' + '.' * (w-2) + '#') + '\n') * (h-2) + '#' * w)
print('')
| 0 | null | 12,146,170,618,772 | 152 | 50 |
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import defaultdict
def main():
N = int(input())
ans = defaultdict(int)
for i in range(1, 110):
for j in range(1, 110):
for k in range(1, 110):
v = i*i+j*j+k*k+i*j+j*k+k*i
ans[v] += 1
for i in range(1, N+1):
print(ans[i])
if __name__ == '__main__':
main()
|
N = int(input())
ans = [0]*80100
for x in range(1, 101):
for y in range(1, 101):
for z in range(1, 101):
ans[x**2+y**2+z**2+x*y+y*z+z*x] += 1
for i in range(1, N+1):
print(ans[i])
| 1 | 8,043,973,973,028 | null | 106 | 106 |
n, k = map(int, input().split())
MOD = 10**9+7
def prepare(n, MOD):
facts = [1]*(n+1)
for i in range(1, n+1):
facts[i] = facts[i-1]*i%MOD
invs = [1]*(n+1)
_invs = [1]*(n+1)
invs[n] = pow(facts[n], MOD-2, MOD)
for i in range(0, n)[::-1]:
invs[i] = invs[i+1] * (i+1) % MOD
return facts, invs
ans = 0
facts, invs = prepare(n, MOD)
for i in range(1+min(n-1, k)):
ans += facts[n]*invs[i]*invs[n-i]*facts[n-1]*invs[n-i-1]*invs[i]
ans %= MOD
print(ans)
|
x = [int(x) for x in input().split()]
if (x[1]<x[0]):
print("safe")
else:
print("unsafe")
| 0 | null | 48,279,991,626,340 | 215 | 163 |
*p, = open(0).read().split()
print(sum(map(int, (p+[0])[p.index(p[-1])+3::2])))
|
w, h, x, y, r = map(int, input().split())
if 0 <= (x-r) and (x+r) <= w and 0 <= (y-r) and (y+r) <= h:
print("Yes")
else:
print("No")
| 0 | null | 48,897,159,805,088 | 243 | 41 |
n = int(input())
a = list(map(int, input().split()))
a = list(enumerate(a))
a.sort(key = lambda x: x[1])
DP = [[0 for i in range(n+1)] for j in range(n+1)]
DP[0][0] = 0
for i in range(1, n+1):
pos, val = a.pop()
pos = pos + 1
DP[0][i] = DP[0][i-1] + abs(val * (n - i + 1 - pos))
DP[i][0] = DP[i-1][0] + abs(val * (pos - i))
for x in range(1, i):
y = i - x
DP[x][y] = max(DP[x-1][y] + abs(val * (pos - x)), \
DP[x][y-1] + abs(val * (n - y + 1 - pos)))
ans = 0
for i in range(n+1):
ans = max(ans, DP[i][n-i])
print(ans)
|
X, Y, A, B, C = map(int, input().split())
tmp = sorted(map(int, input().split()), key=lambda x: -x)[:X] \
+ sorted(map(int, input().split()), key=lambda x: -x)[:Y] \
+ sorted(map(int, input().split()), key=lambda x: -x)
print(sum(sorted(tmp, key=lambda x: -x)[:X + Y]))
| 0 | null | 39,444,820,934,008 | 171 | 188 |
from heapq import *
import sys
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import time
import math
def gcd(a,b):
if(a%b==0):return(b)
return (gcd(b,a%b))
N=int(input())
A=list(map(int,input().split()))
A=[[A[i],i] for i in range(N)]
A.sort(reverse=True)
dp=[[0 for i in range(N+1)] for _ in range(N+1)]
for n in range(N):
a,p=A[n]
for x in range(1,n+2):
y=n+1-x
dp[x][y]=max(dp[x][y],dp[x-1][y]+round(abs(x-1 - p))*a)
for y in range(1,n+2):
x=n+1-y
dp[x][y]=max(dp[x][y],dp[x][y-1]+round(abs(N-y - p))*a)
#print(dp)
print(max([dp[x][y] for x in range(N+1) for y in range(N+1) if x+y==N]))
|
N=int(input())
A=list(map(int, input().split()))
A=sorted([(a,p) for p, a in enumerate(A)])[::-1]
dp=[[0]*(N+1) for _ in range(N+1)]
for i,(a,p) in enumerate(A):
for j in range(i+1):
#左詰
dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+(p-j)*a)
#右詰
dp[i+1][j]=max(dp[i+1][j],dp[i][j]+((N-1-(i-j))-p)*a)
print(max(dp[-1]))
| 1 | 33,757,366,304,550 | null | 171 | 171 |
n = int(input())
tarou = 0
hanako = 0
for i in range(n):
w1, w2 = input().split()
if w1 == w2:
tarou += 1
hanako += 1
elif w1 > w2:
tarou += 3
else:
hanako += 3
print(tarou, hanako)
|
taro, hanako = 0, 0
n = int(input())
for _ in range(n):
a, b = input().split()
if a > b:
taro += 3
elif a < b:
hanako += 3
else:
taro += 1
hanako += 1
print(taro, hanako)
| 1 | 1,991,396,881,530 | null | 67 | 67 |
s = input()
s = s.replace("hi", "")
if s:
print("No")
else:
print("Yes")
|
S = input()
l = len(S)
if l % 2 == 0 and S == l // 2 * "hi":
print("Yes")
else:
print("No")
| 1 | 53,009,970,425,412 | null | 199 | 199 |
x = int(input())
balance = 100
years = 0
while True:
years += 1
balance += balance // 100
if balance >= x:
print(years)
exit(0)
|
n = int(input())
p = [1,1]
if n <= 1:
print (1)
exit (0)
for i in range(2,n+1):
p.append(p[i-2] + p[i-1])
print (p[n])
| 0 | null | 13,495,913,745,678 | 159 | 7 |
#!/usr/bin/env python3
import math
n = int(input())
mod = 10**9+7
data = {}
zero_zero = 0
for i in range(n):
a, b = list(map(int, input().split()))
# # print(a, b)
if a == 0 and b == 0:
zero_zero += 1
continue
gcd = math.gcd(a, b)
a, b = a//gcd, b//gcd
if a < 0:
a, b = -a, -b
if a == 0: # (0, 1)
a, b = 0, 1
elif b == 0: # (1, 0)
a, b = 1, 0
# print(a, b)
# print()
if b <= 0: # (+, -) or (1, 0)
if (-b, a) in data:
data[(-b, a)][1] += 1
else:
data[(-b, a)] = [0, 1]
elif b > 0: # (+, +) or (1, 0)
if (a, b) in data:
data[(a, b)][0] += 1
else:
data[(a, b)] = [1, 0]
# print(data)
# print(zero_zero)
power_2 = [1]
for i in range(1, 2*10**5+100):
power_2.append(power_2[i-1]*2 % mod)
ans = 1
# print(ans)
# print()
for (a, b), (l, m) in data.items():
ans *= (power_2[l]+power_2[m]-1) % mod
# print(power_2[l]+power_2[m]-1)
# print(ans)
# print()
ans = ans - 1 # removed not selected
ans += zero_zero
print(ans % mod)
# count_pp = 0
# count_pm = 0
# ans = 0
# while(1):
# pp_tmp = plus_plus[count_pp]
# pm_tmp = plus_minus[count_pm]
# print("pp_tmp", pp_tmp)
# print("pm_tmp", pm_tmp)
# if pp_tmp[0]*pm_tmp[0] + pp_tmp[1]*pm_tmp[1] == 0:
# pp_mag, pm_mag = 1, 1 # magnitude
# while(1):
# if count_pp < len(plus_plus)-1:
# if plus_plus[count_pp] == plus_plus[count_pp+1]:
# pp_mag += 1
# count_pp += 1
# else:
# break
# else:
# break
# while(1):
# if count_pm < len(plus_minus)-1:
# if plus_minus[count_pm] == plus_minus[count_pm+1]:
# pm_mag += 1
# count_pm += 1
# else:
# break
# else:
# break
# ans += pp_mag*pm_mag
# elif pp_tmp[2]*pm_tmp[2] > -1:
# count_pp += 1
# elif pp_tmp[2]*pm_tmp[2] < -1:
# count_pm += 1
# if count_pp == len(plus_plus)-1:
# break
# if count_pm == len(plus_minus)-1:
# break
# print(1)
# ans += zero_zero
# n = n - zero_zero
# count = ((n % mod) * ((n-1) % mod))//2
# count -= (negative) % mod
# print(count % mod)
|
def solve():
N, M = map(int, input().split())
print(int(N*(N-1)/2 + M*(M-1)/2))
if __name__ == '__main__':
solve()
| 0 | null | 33,317,178,716,640 | 146 | 189 |
n=int(input())
a=map(int,input().split())
pulasu_i=dict()
mainasu_i=dict()
for i,num in enumerate(a):
x=i+1
if num+x not in pulasu_i:
pulasu_i[num+x]=1
else:
pulasu_i[num+x]+=1
if num-x not in mainasu_i:
mainasu_i[num-x]=1
else:
mainasu_i[num-x]+=1
#print(pulasu_i,mainasu_i)
ans=0
for num,cnt in pulasu_i.items():
if -num not in mainasu_i:
continue
else:
ans+=cnt*mainasu_i[-num]
print(ans)
|
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
ja1 = []
ja2 = []
for i in range(1,n+1):
ja1.append(i-a[i-1])
ja2.append(i+a[i-1])
s = Counter(ja1)
t = Counter(ja2)
su = 0
for i in s.keys():
su += s[i]*t[i]
print(su)
| 1 | 26,048,924,500,160 | null | 157 | 157 |
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
return list(map(int,input().split()))
def m(): #複数文字
return map(int,input().split())
def onem(): #Nとかの取得
return int(input())
def s(x): #圧縮
a = []
if len(x) == 0:
return []
aa = x[0]
su = 1
for i in range(len(x)-1):
if aa != x[i+1]:
a.append([aa,su])
aa = x[i+1]
su = 1
else:
su += 1
a.append([aa,su])
return a
def jo(x): #listをスペースごとに分ける
return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
return max(map(max,x))
def In(x,a): #aがリスト(sorted)
k = bs.bisect_left(a,x)
if k != len(a) and a[k] == x:
return True
else:
return False
def pow_k(x, n):
ans = 1
while n:
if n % 2:
ans *= x
x *= x
n >>= 1
return ans
def nibu(x,n,r):
ll = 1
rr = r
while True:
mid = (ll+rr)//2
if rr == mid:
return ll
if cou(x,mid) <= n:
rr = mid
else:
ll = mid+1
def cou(x,le):
co = 0
for i in range(len(x)):
if le != 0:
co += -(-x[i]//le) - 1
return co
n,k = m()
a = l()
left = 0
right = 10**9
aaa = nibu(a,k,right)
print(aaa)
|
n, k = map(int, input().split())
a_i = list(map(int, input().split()))
def f(n):
cnt = 0
for a in a_i:
cnt += (a - 1) // n
if cnt > k: return False
else: return True
l, r = 0, max(a_i)
while True:
if r - l <= 1: break
val = (l + r) // 2
if f(val) == True: r = val
else: l = val
print(r)
| 1 | 6,487,847,293,660 | null | 99 | 99 |
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
# map(int, input().split())
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
def get_point(x):
if x == 'r':
return P
if x == 's':
return R
if x == 'p':
return S
# think each mod K
ans = 0
for i in range(K):
dp = [0] * 2
pre = ''
for j in range(i, N, K):
dp2 = dp[:]
dp2[0] = max(dp)
if pre == T[j]:
dp2[1] = dp[0] + get_point(T[j])
else:
dp2[1] = max(dp) + get_point(T[j])
pre = T[j]
dp = dp2
ans += max(dp)
print(ans)
|
from collections import deque
H, W, K=map(int, input().split())
cake=[input() for _ in range(H)]
rows_list=[]
upper=0
for i in range(H):
for j in range(W):
if cake[i][j]=="#":
rows_list.append([upper, i])
upper=i+1
break
rows_list[-1][-1]=H-1
ans_list=[]
for rows in rows_list:
for i in range(rows[0], rows[1]+1):
left=0
for j in range(W):
if cake[i][j]=="#":
ans_list.append([rows[0], rows[1], left, j])
left=j+1
if ans_list:
ans_list[-1][-1]=W-1
ans=[[0 for _ in range(W)] for _ in range(H)]
key=1
for l in ans_list:
for i in range(l[0], l[1]+1):
for j in range(l[2], l[3]+1):
ans[i][j]=key
key+=1
for a in ans:
print(*a)
| 0 | null | 125,246,536,917,100 | 251 | 277 |
a,k,d = map(int, input().split())
if a < 0:
x = -a
else:
x = a
y = x % d
l = x // d
m = k - l
if m < 0:
ans = x - (k * d)
elif m % 2 ==0:
ans = y
else :
ans = y - d
print(abs(ans))
|
import sys
X, K, D = map(int, input().split())
X = abs(X)
if X // D >= K:
print(X - K*D)
sys.exit()
K = K - (X // D)
A = X - X//D*D
if K % 2 == 0:
print(A)
else:
print(abs(A - D))
| 1 | 5,245,373,390,242 | null | 92 | 92 |
#coding:utf-8
num = input()
print num * num * num
|
x = int(input())
a = x * x * x
print(a)
| 1 | 269,462,517,502 | null | 35 | 35 |
n=int(input())
a=list(map(int,input().split()))
ans=[0]*(n+1)
for i in range(n-1):
ans[a[i]]+=1
ans.pop(0)
for j in ans:
print(j)
|
N = int(input())
A = list(map(int,input().split()))
buka = [0]*N
for p in range(N-1):
buka[A[p] -1] += 1
for q in range(N):
print(buka[q])
| 1 | 32,459,826,488,970 | null | 169 | 169 |
N = int(input())
s = "a"
ans = []
def dfs(s,n):
if len(s) == n:
ans.append(s)
return
last = 0
for i in range(len(s)):
last = max(last,ord(s[i]))
limit = chr(last+1)
for i in range(26):
temp = chr(97+i)
if temp <= limit:
dfs(s+temp,n)
dfs(s,N)
print(*ans,sep="\n")
|
N=int(input())
count=0
#N未満の値がaで割り切れさえすればいい
for a in range(1,N):
count = count + (N-1)//a
print(count)
| 0 | null | 27,348,382,514,752 | 198 | 73 |
def binary_search(border, b):
ok = b
ng = n
while (abs(ok - ng) > 1):
mid = (ok + ng) // 2
if L[mid] < border:
ok = mid
else:
ng = mid
return ok
n = int(input())
L = sorted(list(map(int, input().split())))
ans = 0
for a in range(0,n-1):
for b in range(a+1, n):
a_b = L[a] + L[b]
ans += binary_search(a_b, b) - b
print(ans)
|
l = [list(map(int,input().split())) for i in range(3)]
n = int(input())
b = list(int(input()) for _ in range(n))
for i in range(3) :
for j in range(3) :
for k in range(n) :
if l[i][j] == b[k] :
l[i][j] = 0
for i in range(3) :
if l[i][0] + l[i][1] + l[i][2] == 0 :
print('Yes')
exit()
if l[0][i] + l[1][i] + l[2][i] == 0 :
print('Yes')
exit()
if l[0][0] + l[1][1] + l[2][2] == 0 :
print('Yes')
exit()
if l[0][2] + l[1][1] + l[2][0] == 0 :
print('Yes')
exit()
print('No')
| 0 | null | 115,413,875,574,788 | 294 | 207 |
import math
def isPrime( x ):
if 2 == x or 3 == x:
return True
if 0 == x&1:
return False
i = 3
limit = math.sqrt( x )
while i <= limit:
if 0 == x%i:
return False
i += 2
return True
n = int( raw_input( ) )
nums = []
i = 0
while i < n:
nums.append( int( raw_input( ) ) )
i += 1
cnt = i = 0
for val in nums:
if isPrime( val ):
cnt += 1
print( cnt )
|
while 1:
s=input()
if'-'==s:break
for _ in range(int(input())):
a=int(input())
s=s[a:]+s[:a]
print(s)
| 0 | null | 975,506,071,302 | 12 | 66 |
N = int(input())
A = list(map(int, input().split()))
money = 1000
stock = 0
for day in range(N-1):
today_price = A[day]
tommorow_price = A[day+1]
if today_price < tommorow_price:
div,mod = divmod(money, today_price)
stock += div
money = mod
elif today_price > tommorow_price:
money += today_price * stock
stock = 0
money += A[N-1] * stock
print(money)
|
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 6)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"
AL = al.upper()
n = k()
a = l()
sum = 1000
for i in range(n-1):
if a[i] > a[i+1]:
xx = a[cnt:i+1]
b = sum//min(xx)
sum += (max(xx)-min(xx))*b
cnt = i+1
xx = a[cnt:]
b = sum//min(xx)
sum += (max(xx)-min(xx))*b
print(sum)
| 1 | 7,359,540,978,020 | null | 103 | 103 |
# python3.4.2用
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from decimal import *
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
INF = float('inf') #無限大
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(sys.stdin.readline()) #整数読み込み
def ifn(): return float(sys.stdin.readline()) #浮動小数点読み込み
def isn(): return sys.stdin.readline().split() #文字列読み込み
def imn(): return map(int, sys.stdin.readline().split()) #整数map取得
def imnn(): return map(lambda x:int(x)-1, sys.stdin.readline().split()) #整数-1map取得
def fmn(): return map(float, sys.stdin.readline().split()) #浮動小数点map取得
def iln(): return list(map(int, sys.stdin.readline().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, sys.stdin.readline().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD
def lprint(l): print(*l, sep='\n')
def sieves_of_e(n):
is_prime = [True] * (n+1)
is_prime[0] = False
is_prime[1] = False
for i in range(2, int(n**0.5)+1):
if not is_prime[i]: continue
for j in range(i * 2, n+1, i): is_prime[j] = False
return is_prime
H,W = imn()
s = [input() for _ in range(H)]
dp = [[INF for _ in range(W)] for _ in range(H)]
dp[0][0] = 0
if s[0][0] == '#': dp[0][0] = 1
for i in range(H):
for j in range(W):
if i+1 < H:
cnt = dp[i][j]
if s[i][j] == "." and s[i+1][j] == "#":
cnt += 1
dp[i+1][j] = min(dp[i+1][j], cnt)
if j+1 < W:
cnt = dp[i][j]
if s[i][j] == "." and s[i][j+1] == "#":
cnt += 1
dp[i][j+1] = min(dp[i][j+1], cnt)
print(dp[-1][-1])
|
import bisect
N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for i in range(N-1, 1, -1):
for j in range(i-1, 0, -1):
a, b = L[i], L[j]
c = a - b + 1
if c > b: continue
ans += (j - bisect.bisect_left(L, c))
print(ans)
| 0 | null | 110,164,974,444,800 | 194 | 294 |
x = int(input())
if x < 30:
print("No")
elif x >= 30:
print("Yes")
|
temp = input()
temp = int(temp)
if temp<-40 or temp>40:
print('Out')
elif temp >=30:
print('Yes')
else:
print('No')
| 1 | 5,720,529,298,820 | null | 95 | 95 |
ans=0
S=input()
a=len(S)
k=0
c=dict()
mod=2019
s=1
c[0]=1
for i in range(a):
k+=(s*int(S[a-i-1]))
k%=mod
s*=10
s%=mod
if k in c:
c[k]+=1
else:
c[k]=1
for i in c:
ans+=c[i]*(c[i]-1)//2
print(ans)
|
S = input()
N = len(S)
counter = [0] * 2019
counter[0] = 1
T = 0
R = 1
for i in range(N):
T = (T + R * int(S[N - i - 1])) % 2019
R = 10 * R % 2019
counter[T] += 1
ans = 0
for i in range(2019):
m = counter[i]
ans += m * (m - 1) // 2
print(ans)
| 1 | 30,722,530,261,664 | null | 166 | 166 |
n = int( input().rstrip())
numbers = list( map(int, input().rstrip().split(" ")) )
total = sum(numbers)
m = 10 ** 9 + 7
result = 0
for number in numbers[:-1]:
total -= number
result += (total * number)
result %= m
print(result)
|
n = int(input())
num_list = input().split()
mod_value = 10**9 + 7
sum_all = 0
sum_int = 0
for index in range(len(num_list) - 1):
sum_int += int(num_list[index])
sum_all += sum_int * int(num_list[index + 1])
if sum_all > mod_value:
sum_all %= mod_value
print(sum_all)
| 1 | 3,878,665,745,122 | null | 83 | 83 |
h,n=map(int,input().split())
a=[]
b=[]
for i in range(n):
aa,bb=map(int,input().split())
a.append(aa)
b.append(bb)
inf=10**10
f=h+max(a)+1
dp=[f*[inf]for _ in range(n+1)]
dp[0][0]=0
for i in range(1,n+1):
dp[i]=dp[i-1]
for j in range(f):
if j+a[i-1]<f:
dp[i][j+a[i-1]]=min(dp[i][j+a[i-1]],dp[i][j]+b[i-1])
for j in range(f-1,0,-1):
dp[i][j-1]=min(dp[i][j-1],dp[i][j])
print(dp[-1][h])
|
N,M=list(map(int,input().split()))
C=list(map(int,input().split()))
dp=[0]+[50001 for _ in range(N)]
for i in range(M):
for j in range(C[i],N+1):
if C[i] > N:
break
elif dp[j-C[i]] != 50001:
dp[j] = min(dp[j],dp[j-C[i]] + 1)
print(dp[N])
| 0 | null | 40,689,386,796,796 | 229 | 28 |
a, b, c, d, K = map(int, input().split())
hour = (c-a) * 60
min = d - b
ans = hour + min - K
print(ans)
|
import datetime
h,m,eh,em,k = map(int,input().split())
start = h*60 + m
end = eh*60 + em
if end - start > k:
print(end - k - start)
else:
print(0)
| 1 | 18,056,402,369,110 | null | 139 | 139 |
n,k=map(int,input().split())
L=list(map(int,input().split()))
ans=0
for i in L:
if i>=k:
ans+=1
print(ans)
|
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
import numpy as np
def main():
a = np.array(read().split(), np.int32)[1:]
count = np.zeros(10**6+1, np.int32)
for ae in a:
if count[ae] <= 1:
count[::ae] += 1
r = 0
for ae in a:
r += count[ae] == 1
print(r)
if __name__ == '__main__':
main()
| 0 | null | 96,898,035,964,782 | 298 | 129 |
n = int(input())
div = n // 1000
if (n % 1000 == 0):
ans = 0
else:
ans = (div + 1) * 1000 - n
print(ans)
|
def readinput():
n,x,m=map(int,input().split())
return n,x,m
def main(n,x,m):
ans_list=[]
hist=[0]*m
val=x
count=0
while hist[val]==0 and count<n:
ans_list.append(val)
hist[val]+=1
val=(val*val)%m
if count==n:
loop_start=0
loop_len=n
else:
i=0
while ans_list[i] != val:
i+=1
loop_start=i
loop_len=len(ans_list)-i
# print(ans_list)
# print(loop_start)
# print(loop_len)
ans=sum(ans_list[:loop_start])
ans+=sum(ans_list[loop_start:])*((n-loop_start)//loop_len)
ans+=sum(ans_list[loop_start:loop_start+(n-loop_start)%loop_len])
# ans=sum(ans_list)*(n//loop_len) + sum(ans_list[:n%loop_len])
return ans
if __name__=='__main__':
n,x,m=readinput()
ans=main(n,x,m)
print(ans)
| 0 | null | 5,661,831,135,750 | 108 | 75 |
n,x,m = map(int,input().split())
mod = m
ans = 0
bit = [-1 for i in range(m)]
cycle = False
for i in range(n):
if i == 0 :
a = x
bit[a] = i
ans += a
else:
a = (a**2)% mod
if bit[a] != -1:
cy_st = bit[a]
cy_fi = i -1
cycle = True
break
else:
bit[a] = i
ans += a
if cycle:
ans2 = 0
b = -1
for j in range(cy_st):
if j == 0 :
b = x
ans2 += b
else:
b = (b**2)% mod
ans2 += b
cy_num = ans - ans2
cy_repe = (n-cy_st) // (cy_fi - cy_st + 1)
ans3 = cy_num * cy_repe
cy_amari = (n-cy_st) % (cy_fi - cy_st + 1)
if b == -1:
for j in range(cy_amari):
if j == 0 :
b = x
ans3 += b
else:
b = (b**2)% mod
ans3 += b
else:
for i in range(cy_amari):
b = (b**2)% mod
ans3 += b
print(ans2+ans3)
else:
print(ans)
|
X = int(input())
num1000 = X // 500
r500 = X % 500
num5 = r500 // 5
print(num1000 * 1000 + num5 * 5)
| 0 | null | 22,701,654,498,288 | 75 | 185 |
n,k = map(int,input().split())
p = list(map(int,input().split()))
pp = list(map(lambda x:(x+1)/2,p))
p_rui = [pp[0]]
for i in range(1,n):
p_rui.append(p_rui[i-1]+pp[i])
walist=[p_rui[k-1]]
for i in range(0,n-k):
walist += [p_rui[k+i]-p_rui[i]]
print(max(walist))
|
import itertools
def main():
# n = int(input())
h, w, k = map(int, input().split())
# a = list(map(int, input().split()))
# s = input()
c = []
for i in range(h):
c.append(list(input()))
total = 0
for i in list(itertools.chain.from_iterable(c)):
if i == "#":
total += 1
ans = 0
for i in range(2**h):
for j in range(2**w):
count = 0
for ii in range(h):
for jj in range(w):
if (i >> ii & 1 or j >> jj & 1) and c[ii][jj] == "#":
count += 1
if total - count == k:
ans += 1
print(ans)
if __name__ == '__main__':
main()
| 0 | null | 41,907,365,844,008 | 223 | 110 |
# 19-String-Finding_a_Word.py
# ?????????????´¢
# ??????????????? W ??¨?????? T ????????????????????????T ??????????????? W ?????°???????????????????????°?????????????????????????????????
# ?????? T ????????????????????????????????????????????§????????????????????????????????? Ti ??¨????????????
# ???????????? Ti ?????????????????? W ??¨??????????????????????????°??????????????????
# ???????????§????????¨?°???????????????\???????????????
# Constraints
# W????????????????????????10????¶????????????????
# T??????????????????????????????????????????1000????¶????????????????
# Input
# ?????????????????? W ????????????????????????
# ?¶???????????????°????????????????????£??????????????????????????????
# END_OF_TEXT ??¨??????????????????????????????????????????????????????
# Output
# ?????? W ?????°???????????????????????????
# Sample Input
# computer
# Nurtures computer scientists and highly-skilled computer engineers
# who will create and exploit "knowledge" for the new era.
# Provides an outstanding computer environment.
# END_OF_TEXT
# Sample Output
# 3
# Note
import re
count=0
w = input().lower()
while 1:
string = input()
if string=="END_OF_TEXT":
break;
for i in string.lower().split():
count += w==i
print(count)
|
N = int(input())
A = [list(map(int, input().split())) for _ in range(N)]
Z = sorted([x+y for x,y in A])
W = sorted([x-y for x,y in A])
print(max(Z[-1]-Z[0], W[-1]-W[0]))
| 0 | null | 2,623,799,135,882 | 65 | 80 |
import bisect
N, M = map(int, input().split())
S = input()
ok = True
cnt = 0
for c in S:
if c == "1":
cnt += 1
if cnt == M:
ok = False
break
else:
cnt = 0
# 後ろから見て、各マスからもっとも近い右側の0の位置を格納(S[i]=0ならi)
ngo_pos = [0 for _ in range(N+1)]
most_neighbor_zero_pos = N
for i in range(N, -1, -1):
if S[i] == "0":
ngo_pos[i] = i
most_neighbor_zero_pos = i
else:
ngo_pos[i] = most_neighbor_zero_pos
if not ok:
print(-1)
else:
ans = []
pos = N
while pos > 0:
npos = ngo_pos[max(0, pos-M)]
ans.append(pos-npos)
pos = npos
print(" ".join(map(str, ans[::-1])))
|
n = int(input())
c = input()
cnt = 0
ans = 0
for i in range(n):
if(c[i]=='R'):
cnt+=1
for i in range(cnt):
if(c[i]=='W'):
ans+=1
print(ans)
| 0 | null | 72,499,368,066,078 | 274 | 98 |
N = int(input())
S = list()
for i in range(N):
S.append(input())
judge = ['AC', 'WA', 'TLE', 'RE']
count = [0] * 4
for i in range(N):
letter = S[i]
ind = judge.index(letter)
count[ind] += 1
for i in range(len(judge)):
print(judge[i], "x", str(count[i]))
|
N = int(input())
XYList = []
for i in range(N):
A = int(input())
XY = []
for j in range(A):
XY.append(list(map(int, input().split())))
XYList.append(XY)
maxSum = 0
for i in range(2**N):
sum = 0
for j in range(N):
if i >> j & 1:
honest = True
for XY in XYList[j]:
if ((i >> (XY[0]-1)) & 1) != XY[1]:
honest = False
break
if honest:
sum += 1
else:
sum = 0
break
if maxSum < sum:
maxSum = sum
print(maxSum)
| 0 | null | 65,345,371,619,968 | 109 | 262 |
from sys import stdin
while True:
h, w = (int(n) for n in stdin.readline().rstrip().split())
if h == w == 0:
break
for _ in range(h):
print("#" * w)
print()
|
#!usr/bin/env python3
import sys
def string_to_list_spliter():
h, w = [int(i) for i in sys.stdin.readline().split()]
return h, w
def generate_ractangle(h, w):
rect = ''
for i in range(h):
for j in range(w):
rect += '#'
rect += '\n'
return rect
def main():
while True:
h, w = string_to_list_spliter()
if h == 0 and w == 0:
break
print(generate_ractangle(h, w))
if __name__ == '__main__':
main()
| 1 | 763,598,368,610 | null | 49 | 49 |
S, W = map(int, input().split())
print("unsafe" if W >= S else "safe" )
|
n = int(input())
rooms = [[[0 for i in range(10)] for j in range(3)] for k in range(4)]
for rec in range(n):
b, f, r, v = map(int, input().split())
rooms[b-1][f-1][r-1] += v
for i, b in enumerate(rooms):
for f in b:
print("",*f)
if i != 3:
print("####################")
| 0 | null | 15,184,240,852,670 | 163 | 55 |
import math
n = int(input())
class Coordinate:
def __init__(self,x,y):
self.x = x
self.y = y
def Koch(n,p1,p2):
if n == 0:
return
else:
s = Coordinate(2/3 * p1.x + 1/3 * p2.x, 2/3 * p1.y + 1/3 * p2.y)
u = Coordinate(1/3 * p1.x + 2/3 * p2.x, 1/3 * p1.y + 2/3 * p2.y)
t = Coordinate(1/2*(u.x-s.x) - math.sqrt(3)/2*(u.y-s.y) + s.x, math.sqrt(3)/2*(u.x-s.x) + 1/2*(u.y-s.y) + s.y)
Koch(n-1,p1,s)
print(str(s.x) +" "+str(s.y))
Koch(n-1,s,t)
print(str(t.x) +" "+str(t.y))
Koch(n-1,t,u)
print(str(u.x) +" "+str(u.y))
Koch(n-1,u,p2)
p1 = Coordinate(0,0)
p2 = Coordinate(100,0)
print(str(p1.x) +" "+str(p1.y))
Koch(n,p1,p2)
print(str(p2.x) +" "+str(p2.y))
|
N,K=input().split()
N=int(N)
K=int(K)
n=0
i=1
while i<=N:
n=n+1
i=K**n
print(n)
| 0 | null | 32,308,392,507,428 | 27 | 212 |
#C
N=int(input())
A=[int(x) for x in input().split()]
P=[0 for i in range(N+1)]
P[N]=[A[N],A[N]]
for i in range(N-1,-1,-1):
MIN,MAX=P[i+1]
mi=int(A[i]+(MIN+MIN%2)/2)
ma=A[i]+MAX
P[i]=[mi,ma]
if P[0][0]==1:
Q=[0 for i in range(N+1)]
Q[0]=1
cnt=1
for i in range(N):
Q[i+1]=min((Q[i]-A[i])*2,P[i+1][1])
cnt+=Q[i+1]
print(cnt)
else:
print("-1")
|
# coding: utf-8
def solve(*args: str) -> str:
n = int(args[0])
A = list(map(int, args[1].split()))
rem = sum(A)
cur = 1
ret = 0
for a in A:
cur = min(rem, cur)
rem -= a
ret += cur
cur = 2*(cur-a)
if cur < 0:
ret = -1
break
return str(ret)
if __name__ == "__main__":
print(solve(*(open(0).read().splitlines())))
| 1 | 18,803,894,666,788 | null | 141 | 141 |
def sep():
return map(int,input().strip().split(" "))
def lis():
return list(sep())
import sys
sys.setrecursionlimit(10**6)
s=input()
if s[2]==s[3] and s[4]==s[5]:
print("Yes")
else:
print("No")
|
s=input()
print('YNeos'[(s[2]!=s[3])|(s[4]!=s[5])::2])
| 1 | 42,090,748,438,692 | null | 184 | 184 |
import sys
N = int(sys.stdin.readline().rstrip())
A = list(map(int, sys.stdin.readline().rstrip().split()))
mod = 10**9 + 7
color = A.count(0)
ans = 1
cnt = [0] * N
for a in A:
if a > 0:
ans *= (cnt[a - 1] - cnt[a])
cnt[a] += 1
ans %= mod
for i in range(cnt[0]):
ans *= 3 - i
print(ans % mod)
|
class My_Queue:
def __init__(self, S):
self.S = S
self.q = [0 for i in range(S)]
self.head = 0
self.tail = 0
def enqueue(self, x):
if self.isFull():
print('overflow')
raise
else:
self.q[self.tail] = x
if self.tail + 1 == self.S:
self.tail = 0
else:
self.tail += 1
def dequeue(self):
if self.isEmpty():
print('underflow')
raise
else:
x = self.q[self.head]
self.q[self.head] = 0
if self.head + 1 == self.S:
self.head = 0
else:
self.head += 1
return x
def isEmpty(self):
return self.head == self.tail
def isFull(self):
return self.head == (self.tail + 1) % self.S
def main():
n, qms = map(int, input().split())
elapsed_time = 0
q = My_Queue(n + 1)
for i in range(n):
name, time = input().split()
time = int(time)
q.enqueue([name, time])
while(q.head != q.tail):
a = q.dequeue()
if a[1] <= qms:
elapsed_time += a[1]
print(a[0], elapsed_time)
else:
a[1] -= qms
elapsed_time += qms
q.enqueue(a)
if __name__ == "__main__":
main()
| 0 | null | 65,059,792,432,730 | 268 | 19 |
from collections import defaultdict
def main():
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
S = [0] * (N + 1) # 累積和
for i in range(1, N + 1):
S[i] = S[i - 1] + A[i - 1]
T = [(s - i) % K for i, s in enumerate(S)]
counter = defaultdict(int)
ans = 0
for j in range(N + 1):
if j >= K:
counter[T[j - K]] -= 1
ans += counter[T[j]]
counter[T[j]] += 1
print(ans)
if __name__ == '__main__':
main()
|
p = 1000000007
n = int(input())
A = list(map(int, input().split()))
ans = 1
cnt = [3 if i == 0 else 0 for i in range(n+1)]
for a in A:
ans *= cnt[a]
ans %= p
cnt[a] -= 1
cnt[a+1] += 1
print(ans)
| 0 | null | 133,519,237,123,816 | 273 | 268 |
def main():
W,H,x,y,r=map(int,input().split())
if x-r>=0 and y-r>=0 and W>=x+r and H>=y+r:
print('Yes')
else:
print('No')
if __name__=='__main__':
main()
|
from sys import stdin
import math
import re
import queue
input = stdin.readline
MOD = 1000000007
INF = 122337203685477580
def solve():
str = (input().rstrip())
str = str.replace("hi","")
if(len(str) == 0):
print("Yes")
else:
print("No")
if __name__ == '__main__':
solve()
| 0 | null | 26,958,103,369,210 | 41 | 199 |
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline
#sys.setrecursionlimit(10**8)
def geta(fn=lambda s: s.decode()):
return map(fn, readline().split())
def gete(fn=lambda s: s.decode()):
return fn(readline().rstrip())
def main():
h = gete(int)
w = gete(int)
n = gete(int)
if h < w:
h, w = w, h
print((h + n - 1) // h)
if __name__ == "__main__":
main()
|
A = int (input ())
B = int (input ())
N = int (input ())
if A < B:
X = N//B
if N%B == 0:
print (X)
else:
print (X+1)
else:
X = N//A
if N%A == 0:
print (X)
else:
print (X+1)
| 1 | 89,126,718,154,240 | null | 236 | 236 |
import math
N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))
A.sort()
F.sort(reverse=True)
def solve(X):
s = 0
for i in range(N):
if A[i]*F[i]>X:
s += math.ceil((A[i]*F[i]-X)/F[i])
# print(X, s)
return s <= K
if sum(A) <= K:
print(0)
else:
l, r = 0, 10**12+10
while l+1<r:
mid = (l+r)//2
if solve(mid):
r = mid
else:
l = mid
print(r)
|
import sys
input = sys.stdin.readline
MI=lambda:map(int,input().split())
LI=lambda:list(map(int,input().split()))
N,K=MI()
A=LI()
A.sort(reverse=True)
F=LI()
F.sort()
def check(x):
# x分以内に食べきれるための修行回数はK回以下か
k=0
for i in range(N):
k+=max(int(0--(A[i]-x//F[i])//1),0)
return k<=K
ok=10**13
ng=-1
while abs(ok-ng)>1:
mid=(ok+ng)//2
if check(mid):
ok=mid
else:
ng=mid
print(ok)
| 1 | 165,057,654,384,128 | null | 290 | 290 |
input()
xs = input().split()
print(' '.join(list(map(str, reversed(xs)))))
|
n = input()
l = map(int, raw_input().split())
k = 0
a = []
while n > 0:
a.append(l[n - 1])
n -= 1
print ' '.join(map(str, a))
| 1 | 978,808,535,200 | null | 53 | 53 |
N = input()
A = map(int,raw_input().split())
for i in range(N-1):
print A[i],
print A[-1]
for i in range(1,N):
v = A[i]
j = i - 1
while j >= 0 and A[j] > v:
A[j+1] = A[j]
j -= 1
A[j+1] = v
for i in range(N-1):
print A[i],
print A[-1]
|
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
from collections import deque
class Graph():
def __init__(self, v):
from heapq import heappop, heappush
self.v = v
self.graph = [[] for _ in range(v)]
self.INF = 10 ** 9
def addEdge(self, start, end, edge):
self.graph[start].append((end, edge))
self.graph[end].append((start, edge))
def do():
n=INT()
g=Graph(n)
for i in range(n-1):
a,b=INTM()
a-=1
b-=1
g.addEdge(a,b,i)
que=deque()
check=[True]*n
clrs=[0]*(n-1)
que.append([0,0])
check[0]=False
while que:
clr=1
now,clr_f=que.popleft()
if clr_f==1:
clr=2
for next,i in g.graph[now]:
if check[next]:
check[next]=False
que.append([next,clr])
clrs[i]=clr
clr+=1
if clr==clr_f:
clr+=1
print(max(clrs))
for i in range(n-1):
print(clrs[i])
if __name__=='__main__':
do()
| 0 | null | 67,970,066,704,220 | 10 | 272 |
R,G,B=map(int,input().split())
K=int(input())
M=0
while R>=G or G>=B:
if R>=G:
G*=2
M+=1
if G>=B:
B*=2
M+=1
if M<=K:
print('Yes')
else:
print('No')
|
S,T = input().split()
N,M = list(map(int, input().split()))
U = input()
if U == S:
N = N - 1
if U == T:
M = M - 1
print(N, M)
| 0 | null | 39,496,691,426,752 | 101 | 220 |
import sys
input = lambda: sys.stdin.readline().rstrip()
def solve():
N = int(input())
if N % 2 == 1 or N < 10:
print(0)
exit()
ans = 0
div = 10
while div <= N:
ans += N // div
div *= 5
print(ans)
if __name__ == '__main__':
solve()
|
n, k = (int(i) for i in input().split())
m = n % k
ans = min(m, k - m)
print(ans)
| 0 | null | 77,805,001,409,420 | 258 | 180 |
n= int(input())
mod=10**9+7
a= [int(x ) for x in input().split()]
b= a[:]
s=0
for i in range(n-2,-1,-1):
b[i]=b[i+1]+b[i]
for i in range(n):
s += (b[i]-a[i])*a[i]
print(s%mod)
|
N = int(input())
A = list(map(int, input().split()))
s = sum(A)
res = 0
mod = 10**9 + 7
for i in range(N-1):
s -= A[i]
res += s*A[i]
print(res%mod)
| 1 | 3,802,195,447,720 | null | 83 | 83 |
import math
import sys
import collections
import bisect
readline = sys.stdin.readline
def main():
n, m = map(int, readline().rstrip().split())
a = list(map(int, readline().rstrip().split()))
print(n - sum(a) if n - sum(a) >= 0 else -1)
if __name__ == '__main__':
main()
|
n = int(input())
a_list = [int(x) for x in input().split()]
a_sum = sum(a_list)
m = 10 ** 9 + 7
ans = 0
for i in range(n - 1):
a_sum -= a_list[i]
ans = (ans + a_list[i] * a_sum) % m
print(ans)
| 0 | null | 17,981,741,294,428 | 168 | 83 |
N = int(input())
xys = set([tuple(map(int, input().split())) for _ in range(N)])
z = []
w = []
for xy in xys:
z.append(xy[0]+xy[1])
w.append(xy[0]-xy[1])
#print(z)
#print(w)
print(max(abs(max(z) - min(z)), abs(max(w) - min(w))))
|
from math import gcd
_, *e = [[*map(int, t.split())] for t in open(0)]
ans = 1
mod = 10 ** 9 + 7
slope_dict = {}
zeros = 0
for x, y in e:
if x == y == 0:
zeros += 1
else:
d = gcd(x, y)
x //= d
y //= d
if x < 0 or x == 0 < y:
x, y = -x, -y
s = 0
if y < 0:
x, y, s = -y, x, 1
if (x, y) not in slope_dict:
slope_dict[(x, y)] = [0, 0]
slope_dict[(x, y)][s] += 1
for k in slope_dict:
ans = ans * (pow(2, slope_dict[k][0], mod) +
pow(2, slope_dict[k][1], mod) - 1) % mod
print((ans + zeros - 1) % mod)
| 0 | null | 12,107,451,625,230 | 80 | 146 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.