code1
stringlengths 17
427k
| code2
stringlengths 17
427k
| similar
int64 0
1
| pair_id
int64 2
181,637B
⌀ | question_pair_id
float64 3.7M
180,677B
⌀ | code1_group
int64 1
299
| code2_group
int64 1
299
|
---|---|---|---|---|---|---|
import sys
input = sys.stdin.readline
N,M=list(map(int,input().split()))
print('Yes' if N == M else 'No')
|
import sys
#import string
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18
def solve():
#n = int(stdin.readline().rstrip())
n,m = map(int, stdin.readline().rstrip().split())
#l = list(map(int, stdin.readline().rstrip().split()))
#numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
#word = [stdin.readline().rstrip() for _ in range(n)]
#number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
#zeros = [[0] * w for i in range(h)]
if n==m:
print("Yes")
else:
print("No")
if __name__ == '__main__':
solve()
| 1 | 83,578,297,856,128 | null | 231 | 231 |
N, K = [int(x) for x in input().split()]
P = [0] + [int(x) for x in input().split()]
C = [0] + [int(x) for x in input().split()]
max_score = max(C[1:])
for init in range(1, N + 1): # 初めの場所をinitとする
score = [0] # k回移動後のスコア
i = init
for k in range(1, K + 1):
i = P[i] # k回移動後に着くところ
score.append(score[-1] + C[i])
max_score = max(max_score, score[k])
if i == init: # ループ検出
loop_score = score[-1]
loop_len = k
if loop_score > 0:
max_score = max(max_score, max(score[j] + loop_score * ((K - j) // loop_len) for j in range(1, loop_len + 1)))
break
print(max_score)
|
word = input()
print(word + "es" if word[-1] == "s" else word + "s" )
| 0 | null | 3,875,042,666,692 | 93 | 71 |
import sys
def f(x,y):
return x-y,x+y
def main():
n = int(input())
x = [0]*n
y = [0]*n
for i in range(n):
x[i],y[i] = map(int,input().split())
f0 = [0]*n
f1 = [0]*n
for i in range(n):
f0[i],f1[i] = f(x[i],y[i])
print(max(max(f0)-min(f0),max(f1)-min(f1)))
if __name__ == "__main__":
main()
|
from collections import defaultdict
from bisect import bisect_left
N = int(input())
A = list(map(int, input().split()))
ctr = defaultdict(list)
for i in range(N):
ctr[i + A[i]].append(i)
ans = 0
for i in range(N):
v = i - A[i]
if v in ctr:
ans += bisect_left(ctr[v], i)
print(ans)
| 0 | null | 14,852,294,834,078 | 80 | 157 |
n=int(input())
s = input()
ans = 0
if n%2 != 0:
print('No')
if n%2 == 0:
for i in range(n//2):
if s[i] == s[i+n//2]:
ans += 1
if ans == n//2:
print('Yes')
if ans != n//2:
print('No')
|
n = int(input())
s = str(input())
if n % 2 == 1:
print("No")
exit()
if s[:n // 2] == s[n // 2:]:
print("Yes")
exit()
print("No")
| 1 | 146,357,886,988,170 | null | 279 | 279 |
N=int(input())
A=list(map(int, input().split()))
q=int(input())
Q=[list(map(int, input().split())) for _ in range(q)]
L=[0]*(10**5+1)
S=0
for i in range(N):
L[A[i]]+=1
S+=A[i]
for i in range(q):
S+=L[Q[i][0]]*(Q[i][1]-Q[i][0])
L[Q[i][1]]+=L[Q[i][0]]
L[Q[i][0]]=0
# print(L)
print(S)
|
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
d = {}
v = 0
ans = []
for a in A:
d[a] = d.get(a, 0) + 1
v += a
for i in range(Q):
B, C = map(int, input().split())
v += d.get(B, 0) * (C - B)
d[C] = d.get(C, 0) + d.get(B, 0)
d[B] = 0
ans.append(v)
for a in ans:
print(a)
| 1 | 12,166,917,850,140 | null | 122 | 122 |
import sys
w = input().lower()
lines = sys.stdin.read()
line=lines[0:lines.find('END_OF_TEXT')].lower().split()
print(line.count(w))
|
n=int(input())
count=0
maxs=0
for _ in range(n):
d1,d2=map(int,input().split())
if d1==d2:
count+=1
else:
maxs=max(maxs,count)
count=0
maxs=max(count,maxs)
if maxs>=3:
print("Yes")
else:
print("No")
| 0 | null | 2,162,264,564,482 | 65 | 72 |
n = int(input())
i = 0
list_n = [n]
map_n = map(int, input().split())
list_n = list(map_n)
for value in reversed(list_n):
print(value, end ='')
if value != list_n[0]:
print(' ', end = '')
print()
|
n, m, l = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
b = [list(map(int, input().split())) for _ in range(m)]
for i in a:
c = []
for j in zip(*b):
c.append(sum([k * l for k, l in zip(i, j)]))
print(*c)
| 0 | null | 1,204,382,595,350 | 53 | 60 |
V = input()
s = 0
for c in V:
n = int(c)
s = s + n
if s%9 == 0:
print("Yes")
else:
print("No")
|
n = input()
sum = 0;
for i in n:
sum += int(i)
if sum % 9 == 0:
print('Yes')
else:
print('No')
| 1 | 4,444,906,551,300 | null | 87 | 87 |
# 駅1、2、3
# 管理状況はAAB,ABA、BBA,BBBなど、長さ3の文字列で表される
# AとBの駅の間にはバスを運行することにした
# バスが運行することになる組み合わせが存在するかどうか判定し、yes,noで出力
s = input('')
if s == 'AAA' or s == 'BBB':
print('No')
else:
print('Yes')
|
A, B=input().split()
a=int(A)
R,E=B.split('.')
while len(E)<2:
E=E+0
c=int(R+E)
p=str(a*c)
if len(p)<=2:
print(0)
else:
print(p[0:len(p)-2])
| 0 | null | 35,887,764,484,900 | 201 | 135 |
from sys import stdin
def main():
_in = [_.rstrip() for _ in stdin.readlines()]
N, X, M = list(map(int, _in[0].split(' '))) # type:list(int)
# vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
ans = 0
A = X
head_list = []
head = set()
repeat = set()
for i in range(N):
if A in head:
ind = head_list.index(A)
repeat = head_list[ind:]
head = head_list[:ind]
break
else:
head.add(A)
head_list.append(A)
A = pow(A, 2, M)
if len(repeat) == 0:
ans = sum(head)
else:
repeat_time = (N - len(head)) // len(repeat)
tail_len = N - len(head) - repeat_time * len(repeat)
ans = sum(head) + sum(repeat) * repeat_time + sum(repeat[:tail_len])
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
print(ans)
if __name__ == "__main__":
main()
|
x = int(input())
cnt, ans = 100, 0
while cnt < x:
ans += 1
cnt = cnt * 101 // 100
print(ans)
| 0 | null | 15,035,058,293,450 | 75 | 159 |
def merge(A,left,mid,right):
L=A[left:mid]+[10**9]
R=A[mid:right]+[10**9]
i = 0
j = 0
c = 0
for k in range(left,right):
c += 1
if L[i] <= R[j]:
A[k] = L[i]
i+=1
else:
A[k] = R[j]
j+=1
return c
def merge_sort(A,left,right):
if left+1 < right:
mid = (left+right) // 2
cL=merge_sort(A,left,mid)
cR=merge_sort(A,mid,right)
return(merge(A,left,mid,right) + cL + cR)
return 0
c=0
n = int(input())
A = [int(i) for i in input().split()]
c=merge_sort(A,0,n)
print(*A)
print(c)
|
import math
def merge(a,l,m,r):
global cnt
L = a[l:m]+[math.inf]
R = a[m:r]+[math.inf]
i = 0
j = 0
for k in range(l,r):
cnt+=1
if L[i]<=R[j]:
a[k] = L[i]
i+=1
else:
a[k]=R[j]
j+=1
def mergeSort(a,l,r):
if l+1<r:
m = (l+r)//2
mergeSort(a,l,m)
mergeSort(a,m,r)
merge(a,l,m,r)
n = int(input())
a = list(map(int, input().split()))
cnt = 0
mergeSort(a,0,n)
print(*a)
print(cnt)
| 1 | 112,320,751,482 | null | 26 | 26 |
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
def main():
n,m = map(int,input().split())
x = m//2
y = m - x
for i in range(x):
print(i+1,2*x+1-i)
for i in range(y):
print(2*x+2+i,2*x+2*y+1-i)
if __name__ == "__main__":
main()
|
import itertools
n, k = map(int, input().split())
lis = list(map(int,input().split()))
exlis = []
for i in lis:
a = (1+i)/2
exlis.append(a)
nlis = list(itertools.accumulate(exlis))
ans = nlis[k-1]
for j in range(n-k):
tmp = nlis[j+k] - nlis[j]
ans = max(ans, tmp)
print(ans)
| 0 | null | 51,775,988,579,842 | 162 | 223 |
import sys
from itertools import combinations
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def main():
H, W, K = map(int, readline().split())
S = [readline().strip() for _ in range(H)]
ans = INF
for r in range(H):
for comb in combinations(range(1, H), r):
sep = [0]
sep.extend(list(comb))
sep.append(H)
res = r
A = [0] * (r + 1)
for j in range(W):
B = [0] * (r + 1)
for k in range(r + 1):
for i in range(sep[k], sep[k + 1]):
if S[i][j] == '1':
B[k] += 1
ok = True
divided = False
for k in range(r + 1):
if B[k] > K:
ok = False
break
if A[k] + B[k] > K:
divided = True
break
else:
A[k] += B[k]
if not ok:
res = INF
break
if divided:
A = B
res += 1
if ans > res:
ans = res
print(ans)
return
if __name__ == '__main__':
main()
|
t = input()
s = t.replace("?", "D")
print(s)
| 0 | null | 33,478,671,102,488 | 193 | 140 |
cnt = 0
x = int(input())
m = 100
for i in range(4000):
if m >= x:
print(i)
exit()
else:
m += m//100
|
a,b,m = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
ret = 2**60
for _ in range(m):
x,y,c = map(int,input().split())
x -= 1
y -= 1
ret = min(ret, A[x]+B[y]-c)
print( min(ret, min(A)+min(B)) )
| 0 | null | 40,445,295,473,558 | 159 | 200 |
X = int(input())
if (X < 30):
print('No')
else:
print('Yes')
|
str = input()
A = str
print(str[0]+str[1]+str[2])
| 0 | null | 10,169,266,768,960 | 95 | 130 |
h = int(input())
w = int(input())
n = int(input())
if h >= w:
print(-(-n//h))
elif w > h:
print(-(-n//w))
|
H = int(input())
W = int(input())
N = int(input())
count1 = 0
count2 = 0
for i in range(1,W+1):
count1 += 1
if i*H >= N:
break
for j in range(1,H+1):
count2 += 1
if j*W >= N:
break
if count1 > count2:
print(count2)
else:
print(count1)
| 1 | 88,660,395,458,090 | null | 236 | 236 |
import sys
sys.setrecursionlimit(10000000)
MOD = 998244353
INF = 10 ** 15
def main():
N,S = map(int,input().split())
A = list(map(int,input().split()))
dp = [[0]*(1 + S) for _ in range(N + 1)]
dp[0][0] = pow(2,N,MOD)
inv2 = pow(2,MOD - 2,MOD)
for i,a in enumerate(A):
for j in range(S + 1):
if j < a:
dp[i + 1][j] = dp[i][j]
else:
dp[i + 1][j] = (dp[i][j] + dp[i][j - a] * inv2)%MOD
print(dp[N][S])
if __name__ == '__main__':
main()
|
def main():
N, S = map(int, input().split())
A = list(map(int, input().split()))
MOD = 998244353
dp = [[0] * (S+5) for _ in range(N+5)]
dp[0][0] = 1
for i in range(N):
for j in range(S+1):
dp[i+1][j] += 2 * dp[i][j]
dp[i+1][j] %= MOD
if j + A[i] <= S:
dp[i+1][j+A[i]] += dp[i][j]
dp[i+1][j+A[i]] %= MOD
print(dp[N][S])
if __name__ == "__main__":
main()
| 1 | 17,827,077,608,032 | null | 138 | 138 |
N = int(input())
A = set(input().split())
if N==len(A):
print("YES")
else:
print("NO")
|
n = int(input())
ll = list(map(int, input().split()))
def bubble_sort(a, n):
flag = True
count = 0
while flag:
flag = False
for i in range(n-2, -1, -1):
if a[i] > a[i+1]:
a[i], a[i+1] = a[i+1], a[i]
flag = True
count += 1
print(" ".join(map(str, a)))
print(count)
bubble_sort(ll, n)
| 0 | null | 37,091,845,873,560 | 222 | 14 |
n, q = map(int, input().split())
processes = [[li[0], int(li[1])] for li in [input().split() for _ in range(n)]]
elapsed_time = 0
while len(processes):
process = processes.pop(0)
if process[1] > q:
process[1] -= q
processes.append(process)
elapsed_time += q
else:
elapsed_time += process[1]
print(process[0], elapsed_time)
|
n, q = list(map(int, input().split()))
que = []
for i in range(n):
p, t = input().split()
que.append([p, int(t)])
total = 0
while True:
if q >= que[0][1]:
total += que[0][1]
print(que[0][0], total)
que.pop(0)
else:
total += q
que[0][1] -= q
que.append(que[0])
que.pop(0)
if len(que) == 0:
break
| 1 | 41,869,841,000 | null | 19 | 19 |
import math
a,b,c=map(float,input().split())
c=math.radians(c)
h=b*math.sin(c)
s=a*h/2
d=math.sqrt(a**2+b**2-2*a*b*math.cos(c))
l=a+b+d
print(s,l,h)
|
N,M = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(M)]
n = -1
for x in range(1000):
x = str(x)
if len(x)!=N:continue
ind = 0
for i in range(M):
s,c = A[i]
if x[int(s)-1]!=str(c):
ind=1
break
if ind==0:
n = x
break
print(n)
| 0 | null | 30,646,017,473,500 | 30 | 208 |
X = int(input())
# 500円で1000, 5円で5
x500 = X // 500
print(1000 * x500 + (X - 500 * x500) // 5 * 5)
|
X = int(input())
syou = int(X/500)
syou_second = int((X - syou*500)/5)
ans = syou*1000 + syou_second*5
print(ans)
| 1 | 42,769,201,122,062 | null | 185 | 185 |
s = input()
t = input()
ans = 1e+9
for si in range(len(s)):
if si + len(t) > len(s):
continue
cnt = 0
for ti in range(len(t)):
if s[si+ti] != t[ti]:
cnt += 1
ans = min(ans, cnt)
print(ans)
|
s = str(input())
t = str(input())
ans = len(t)
for i in range(len(s) - len(t) + 1):
tmp_ans = 0
for j in range(len(t)):
if s[i+j] != t[j]:
tmp_ans += 1
ans = min(ans, tmp_ans)
print(ans)
| 1 | 3,687,347,024,128 | null | 82 | 82 |
l, r, d = map(int, input().split())
result=0
for i in range(r-l+1):
if (l+i) % d == 0:
result+=1
print(result)
|
def get_ints():
return map(int, input().split())
def get_list():
return list(map(int, input().split()))
l, r, d = get_ints()
c = 0
for i in range(l, r + 1):
if i % d == 0:
c += 1
print(c)
| 1 | 7,554,938,678,560 | null | 104 | 104 |
num=int(input())
command=[input().split() for i in range(num)]
dic={}
for i in command:
if i[0]=="insert":
dic[i[1]]=1
if i[0]=="find":
if i[1] in dic:
print("yes")
else:
print("no")
|
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
sys.setrecursionlimit(10**7)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def _S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
# b,r = map(str, readline().split())
# A,B = map(int, readline().split())
br = LS()
AB = LI()
U = _S()
def main():
brAB = zip(br, AB)
tmp=[]
for c,n in brAB:
if c==U:
tmp.append(n - 1)
else:
tmp.append(n)
return str(tmp[0])+' '+str(tmp[1])
print(main())
| 0 | null | 36,155,087,329,920 | 23 | 220 |
t = list(input())
t.reverse()
tmptmp = "P"
for i in range(len(t)):
if i == 0:
tmp = t[i]
continue
if tmp == "?" and tmptmp != "D":
t[i-1] = "D"
elif tmp == "?" and tmptmp == "D" and t[i] != "D":
t[i-1] = "D"
elif tmp == "?" and tmptmp == "D" and t[i] == "D":
t[i-1] = "P"
tmptmp = t[i-1]
tmp = t[i]
t.reverse()
if len(t) > 1:
if t[0] == "?" and t[1] == "D":
t[0] = "P"
elif t[0] == "?" and t[1] == "P":
t[0] = "D"
if len(t) == 1:
t[0] = "D"
t = "".join(t)
print(t)
|
INF = 10 ** 9
n, m, l = map(int, input().split())
d = [[INF for j in range(n + 1)] for i in range(n + 1)]
d2 = [[INF for j in range(n + 1)] for i in range(n + 1)]
for _ in range(m):
i, j, c = map(int, input().split())
d[i][j] = c
d[j][i] = c
if c <= l:
d2[i][j] = 1
d2[j][i] = 1
q = int(input())
Q = [list(map(int, input().split())) for _ in range(q)]
for k in range(1, n + 1):
for i in range(1, n):
for j in range(i, n + 1):
dk = d[i][k] + d[k][j]
if d[i][j] > dk:
d[i][j] = dk
d[j][i] = dk
if dk <= l:
d2[i][j] = 1
d2[j][i] = 1
for k in range(1, n + 1):
for i in range(1, n):
for j in range(i, n + 1):
dk = d2[i][k] + d2[k][j]
if d2[i][j] > dk:
d2[i][j] = dk
d2[j][i] = dk
for i in range(q):
s, t = Q[i]
di = d2[s][t]
if di >= INF:
print(-1)
else:
print(di - 1)
| 0 | null | 96,253,840,281,940 | 140 | 295 |
import math
n = int(input())
x = list(map(float, (input().split())))
y = list(map(float, (input().split())))
l = [0.0]*n
for i in range(n):
l[i] = abs(x[i]-y[i])
print(sum(l))
che = max(l)
for i in range(n):
l[i] = abs(x[i]-y[i])**2
print(math.sqrt(sum(l)))
for i in range(n):
l[i] = abs(x[i]-y[i])**3
print(math.pow(sum(l), 1.0/3.0))
print(che)
|
N,M=map(int,input().split())
S,r,s=input(),[],N
for _ in range(2*N):
if S[s]=='1':
s+=1
else:
if N-s:
r.append(N-s)
N,s=s,max(0,s-M)
print(*[-1] if s else r[::-1])
| 0 | null | 69,661,798,681,580 | 32 | 274 |
MOD = 998244353
n, k = map(int, input().split())
l = [0] * k
r = [0] * k
for i in range(k):
l[i], r[i] = map(int, input().split())
s = [0] * (n + 2)
for i in range(k):
for j in range(l[i], r[i] + 1):
s[j] = 1
ans = [0] * (n + 1)
ans[0] = 1
for i in range(n - 1):
ans[i + 1] = ans[i]
for j in range(k):
ans[i + 1] += ans[max(-1, i + 1 - l[j])]
ans[i + 1] -= ans[max(-1, i + 1 - r[j] - 1)]
ans[i + 1] = ans[i + 1] % MOD
print((ans[n - 1] - ans[n - 2]) % MOD)
|
mod = 998244353
N, K = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(K)]
dp = [0] * (N+1)
dpsum = [0] * (N+1)
dp[1] = 1
dpsum[1] = 1
for i in range(2, N+1):
for l, r in LR:
li = i-r
ri = i-l
if ri < 0: continue
li = max(1, li)
dp[i] += dpsum[ri] - dpsum[li-1]
dp[i] = dp[i] % mod
dpsum[i] = dpsum[i-1] + dp[i]
dpsum[i] = dpsum[i] % mod
print(dp[N])
| 1 | 2,716,131,164,740 | null | 74 | 74 |
x,y = map(int,input().split())
m = 10**9+7
n = (x+y)//3
c = 0
if x*0.5 <= y <= 2*x and (x+y)%3 == 0:
r = x - n
c = 1
for i in range(1,r+1):
c *= (n-i+1) % m
c *= pow(i,m-2,m)
c = c % m
else:
ans = 0
print(c)
|
def main():
MOD = 10 ** 9 + 7
X, Y = map(int, input().split())
if (X + Y) % 3:
print(0)
return
a = (X * 2 - Y) // 3
b = X - a * 2
def choose(n, a, mod):
x, y = 1, 1
for i in range(a):
x = x * (n - i) % mod
y = y * (i + 1) % mod
return x * pow(y, mod - 2, mod) % mod
if a < 0 or b < 0:
print(0)
return
ans = choose(a + b, min(a, b), mod=MOD)
print(ans)
if __name__ == '__main__':
main()
| 1 | 149,993,455,611,630 | null | 281 | 281 |
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
import fileinput
import math
for line in fileinput.input():
a, b = map(int, line.split())
g = gcd(a, b)
lcm = a * b // g
print(g, lcm)
|
H,W,M = map(int,input().split())
Hmax_temp = [0]*(H+1)
Wmax_temp = [0]*(W+1)
Hmax = 0
Wmax = 0
Bomb = dict()
for i in range(M):
H_M,W_M = map(int,input().split())
Hmax_temp[H_M] += 1
Wmax_temp[W_M] += 1
Bomb[(H_M,W_M)] = 1
Hmax = max(Hmax_temp)
Wmax = max(Wmax_temp)
#H方向の走査
y_max = []
for i in range(H+1):
if Hmax == Hmax_temp[i]:
y_max.append(i)
#W方向の走査
x_max = []
for j in range(W+1):
if Wmax == Wmax_temp[j]:
x_max.append(j)
#爆弾設置位置に対象が存在するか?
flag = False
for i in y_max:
for j in x_max:
if (i,j) not in Bomb:
flag = True
break
if flag:
break
#答えのセット
if flag:
ans = Wmax + Hmax
else:
ans = Wmax + Hmax - 1
print(ans)
| 0 | null | 2,375,795,526,300 | 5 | 89 |
x1, x2, x3, x4, x5 = map(int, input().split())
l = [x1, x2, x3, x4, x5]
print(l.index(0)+1)
|
while True:
x, y = map(int, raw_input().split())
if x == 0 and y == 0:
break
elif x <= y:
print("%d %d" % (x, y))
elif x > y:
print("%d %d" % (y, x))
| 0 | null | 6,980,098,145,728 | 126 | 43 |
def main():
n,r = map(int, input().split())
if n < 10:
print(r + 100*(10 - n))
else:
print(r)
return
main()
|
N = int(input())
ans = (N + 1) // 2 / N
print(ans)
| 0 | null | 120,156,529,919,100 | 211 | 297 |
import sys
a,b,c = map(int,sys.stdin.readline().split())
counter = 0
for n in range(a,b+1):
if c % n == 0:
counter += 1
print(counter)
|
x = list(input().split(" "))
a = int(x[0])
b = int(x[1])
c = int(x[2])
divisors = 0
for i in range(a, b+1):
if c % i == 0:
divisors += 1
print(divisors)
| 1 | 550,443,546,662 | null | 44 | 44 |
a , b , k = map(int,input().split())
if k <= a:
answer = ( a - k , b)
else:
answer = (0 , max(0 , a + b -k))
print(*answer)
|
def insertion_sort(array):
for i in range(len(array)):
v = array[i]
j = i - 1
while(j>=0 and array[j] > v):
array[j+1] = array[j]
j = j - 1
array[j+1] = v
print(' '.join(map(str, array)))
return array
if __name__ == '__main__':
element_number = int(input())
input_array = list(map(int, input().split()))
insertion_sort(input_array)
| 0 | null | 52,295,177,582,780 | 249 | 10 |
import math
N,D = map(int,input().split())
ans = 0
for i in range(N):
x,y = map(int,input().split())
if math.sqrt(x**2+y**2) <= D:
ans += 1
print(ans)
|
import math
n, d = map(int,input().split( ))
#print(n,d)
number = 0
for a in range(n):
x, y = map(int, input().split( ))
if math.sqrt(x**2 + y**2) <= d:
number += 1
print(number)
| 1 | 5,908,187,776,234 | null | 96 | 96 |
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 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
n,m,x = I()
book = [l() for i in range(n)]
money = []
for i in range(2**n):
ccc = 0
bag = [0]*(m+1)
for j in range(n):
for k in range(m+1):
if ((i>>j)&1):
bag[k] += book[j][k]
for p in range(1,m+1):
if bag[p] >= x:
ccc += 1
if ccc == m:
money.append(bag[0])
if len(money) == 0:
print(-1)
else:
print(min(money))
|
N,M,X = map(int,input().split())
C = []
A = []
for i in range(N):
t = list(map(int,input().split()))
C.append(t[0])
A.append(t[1:])
result = -1
# 1 << N 2^N
#There are 2^N possible ways of choosing books
for i in range(1 << N):
#keep all the understanding level in u
u = [0]*M
c = 0
for j in range(N):
#move j bit from i
if (i>>j)&1 == 0:
continue
c+= C[j]
#listwise sum
for k in range(M):
u[k] += A[j][k]
#X is the desired understanding level
if all(x >= X for x in u):
if result == -1:
result = c
else:
result = min(result,c)
print(result)
| 1 | 22,236,109,644,452 | null | 149 | 149 |
def solve(string):
return str(max(map(len, string.split("S"))))
if __name__ == '__main__':
import sys
print(solve(sys.stdin.read().strip()))
|
S = input()
# 高々2^3=8通りなので、全て列挙すればよい
# RRR RRS SRR RSR RSS SRS SSR SSS
ans = 0
if S == 'RRR': ans = 3
elif S == 'SRR' or S == 'RRS': ans = 2
elif S == 'SSS': ans = 0
else: ans = 1
print(ans)
| 1 | 4,827,857,301,432 | null | 90 | 90 |
N = int(input())
xs, ys = [], []
for _ in range(N):
x, y = map(int, input().split())
xs.append(x)
ys.append(y)
A = [x + y for x, y in zip(xs, ys)]
B = [x - y for x, y in zip(xs, ys)]
ans = max([max(A) - min(A), max(B) - min(B)])
print(ans)
|
N = int(input())
a = []
typ = ["S ", "H ", "C ", "D "]
for i in range(N):
a.append(input())
for i in range(4):
for j in range(1, 14):
if typ[i] + str(j) not in a:
print(typ[i] + str(j))
| 0 | null | 2,252,303,454,490 | 80 | 54 |
n = int(input())
Nlist = input().split()
for i in range(n-1):
print(Nlist[n-i-1],end=" ")
print(Nlist[0])
|
input()
A = [i for i in input().split()]
A.reverse()
print(' '.join(A))
| 1 | 961,202,547,690 | null | 53 | 53 |
from itertools import combinations_with_replacement as C
N, M, Q = map(int, input().split())
a = [0] * Q
b = [0] * Q
c = [0] * Q
d = [0] * Q
for i in range(Q):
a[i], b[i], c[i], d[i] = map(int, input().split())
A_ = list(range(1, M + 1))
A_ = list(C(A_, N))
ans = 0
ans_ = 0
for A in A_:
ans_ = 0
for i in range(Q):
if A[b[i] - 1] - A[a[i] - 1] == c[i]:
ans_ += d[i]
if ans < ans_:
ans = ans_
print(ans)
|
from itertools import combinations_with_replacement
N, M, Q = map(int, input().split())
abcdn = [tuple(map(int, input().split())) for _ in range(Q)]
mx = 0
for AN in combinations_with_replacement(range(1, M+1), N):
score = 0
for a, b, c, d in abcdn:
score += d if AN[b-1] - AN[a-1] == c else 0
mx = max(score, mx)
print(mx)
| 1 | 27,477,324,650,370 | null | 160 | 160 |
s = list(input())
t = len(s)
if t==1 and s[0]=="?":
s[0]="D"
for i in range(t-1):
if i==0 and s[i]=="?":
s[i]="D"
if s[i+1]=="?":
s[i+1]="D"
elif s[-1]=="?" and s[-2]=="D" and i==t-2:
s[-1]="D"
elif s[i]=="D" and s[i+1]=="?" and s[i+2]=="P":
s[i+1]="D"
elif s[i]=="P" and s[i+1]=="?":
s[i+1]='D'
elif s[i]=="D" and s[i+1]=="?":
s[i+1]='P'
print("".join(s))
|
T=input()
print(T.replace('?','D'))
| 1 | 18,420,456,835,550 | null | 140 | 140 |
import math
res = 100
N = int(input())
for i in range(N):
res = math.ceil(res * 1.05)
print("%d000" % res)
|
while True:
try:
a, b = list(map(int, input().split()))
print(len(str(a + b)))
except EOFError:
break
except ValueError:
break
| 0 | null | 688,114,642 | 6 | 3 |
import math
from decimal import Decimal
X = int(input())
now = 100
year = 0
while True:
if now >= X:
break
#now = int(now * Decimal("1.01"))
now = int(now * round(Decimal(1.01), 2))
year += 1
print(year)
|
n = int(input())
s = [input() for _ in range(n)]
ans = set()
for i in s:
ans.add(i)
print(len(ans))
| 0 | null | 28,646,162,861,668 | 159 | 165 |
#coding: utf-8
def print_list(X):
first_flag = True
for val in X:
if first_flag:
print_line = str(val)
first_flag = False
else:
print_line = print_line + ' ' + str(val)
print(print_line)
def bubble_sort(A, N):
#print_list(A)
#flag: ???????????????????????????True???????´?
flag = True
counter = 0
while flag:
flag = False
for j in range(1, N):
if A[j] < A[j-1]:
tmp_val = A[j]
A[j] = A[j-1]
A[j-1] = tmp_val
flag = True
counter += 1
#print_list(A)
print_list(A)
print(counter)
N = int(input())
A = list(map(int, input().split()))
bubble_sort(A, N)
|
import sys
for i, x in enumerate(map(int, sys.stdin)):
if not x: break
print("Case {0}: {1}".format(i + 1, x))
| 0 | null | 248,034,402,420 | 14 | 42 |
import numpy as np
from collections import Counter
N=int(input())
Alist=list(map(int,input().split()))
Q=int(input())
bc=[]
for _ in range(Q):
bc.append(list(map(int,input().split())))
count=Counter(Alist)
result=0
for key,value in count.items():
result+=key*value
for i in range(Q):
if bc[i][0] in count.keys():
result+=(bc[i][1]-bc[i][0])*count[bc[i][0]]
count[bc[i][1]]+=count[bc[i][0]]
count[bc[i][0]]=0
print(result)
|
def main():
n = int(input())
a = list(map(int,input().split()))
dic = {}
for i in range(n):
if not a[i] in dic.keys():
dic[a[i]] = 1
else:
dic[a[i]] += 1
s = 0
for k in dic.keys():
s += dic[k] * k
q = int(input())
for i in range(q):
b,c = map(int,input().split())
if b in dic.keys():
cnt = dic[b]
else:
cnt = 0
dic[b] = 0
if c in dic.keys():
dic[c] += cnt
else:
dic[c] = cnt
s = s + cnt * (c-b)
print(s)
if __name__ == "__main__":
main()
| 1 | 12,167,775,908,168 | null | 122 | 122 |
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque
sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7
DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
N = I()
ans = 0
for a in range(1, 10**3+1):
if a ** 2 >= N:
continue
b_num = N // a - a
if a * (N // a) == N:
b_num -= 1
ans += b_num
ans *= 2
ans += int(math.sqrt(N))
if int(math.sqrt(N)) ** 2 == N:
ans -= 1
print(ans)
main()
|
import math
import sys
import collections
import bisect
readline = sys.stdin.readline
def main():
[h, w, n] = [int(readline().rstrip()) for _ in range(3)]
print(math.ceil(n/max(h, w)))
if __name__ == '__main__':
main()
| 0 | null | 45,725,156,289,340 | 73 | 236 |
a = sorted(map(int, raw_input().split()))
print '%d %d %d'%(a[0],a[1],a[2])
|
s=input()
x=list(map(int,s.split()))
x.sort()
print("%d %d %d"%tuple(x))
| 1 | 413,877,405,072 | null | 40 | 40 |
import sys
k = int(input())
a =7%k
if a == 0:
print(1)
sys.exit()
for i in range(2,10**7):
a = (10*a+7) % k
if a == 0:
print(i)
sys.exit()
print(-1)
|
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect
import heapq
#
#
#
# pythonで無理なときは、pypyでやると正解するかも!!
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
#
# インデックス系
# int min_y = max(0, i - 2), max_y = min(h - 1, i + 2);
# int min_x = max(0, j - 2), max_x = min(w - 1, j + 2);
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
#mod = 998244353
INF = float('inf')
from sys import stdin
readline = stdin.readline
def readInts():
return list(map(int,readline().split()))
def readTuples():
return tuple(map(int,readline().split()))
def I():
return int(readline())
k = I()
# 0 ~ k-1の値を取る場合がある。
i = 0
value = 7%k
while i < k:
if value == 0:
print(i+1)
exit()
value = (10*value + 7)%k
i += 1
print(-1)
| 1 | 6,072,690,123,178 | null | 97 | 97 |
tes = input()
for mak in range(int(input())) :
ins = input().split()
ins[1], ins[2] = int(ins[1]), int(ins[2])
if ins[0] == 'replace' :
tes = tes[ : (ins[1])] + ins[3] + tes[(ins[2] + 1) :]
elif ins[0] == 'reverse' :
tem = tes[ins[1] : ins[2] + 1]
tem = tem[::-1]
tes = tes.replace(tes[ins[1] : ins[2] + 1], tem)
elif ins[0] == 'print' :
print(tes[ins[1] : ins[2] + 1])
|
# coding: utf-8
N = int(input())
_A = sorted(enumerate(map(int, input().split()), 1), key=lambda x:x[1], reverse=True)
dp = [[0] * (N+1) for i in range(N+1)]
for i in range(1,N+1):
k, Ak = _A[i-1]
if (N-i-k) < 0:break
dp[0][i] = dp[0][i-1] + (N-i+1-k) * Ak
for i in range(1,N+1):
k, Ak = _A[i-1]
if (k-i) < 0:break
dp[i][0] = dp[i-1][0] + (k-i) * Ak
for x in range(1, N+1):
for y in range(1, N-x+1):
k, val = _A[x+y-1]
dp[x][y]= max(dp[x-1][y] + abs(k-x)*val,
dp[x][y-1] + abs(N-y-k+1) * val)
print(int(max(dp[i][N-i] for i in range(N+1))))
| 0 | null | 17,883,588,799,492 | 68 | 171 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def main():
values = []
total = 0
open_chars = 0
close_chars = 0
for _ in range(int(input())):
s = input().strip()
open_required = len(s)
close_required = len(s)
open_close = 0
for i, c in enumerate(s):
if c == '(':
open_required = min(i, open_required)
open_close += 1
else:
close_required = min(len(s) - i - 1, close_required)
open_close -= 1
if open_required == 0 and close_required == 0 and open_close == 0:
continue
elif open_close == len(s):
open_chars += len(s)
continue
elif -open_close == len(s):
close_chars += len(s)
continue
total += open_close
values.append([open_required, close_required, open_close, 0])
if total + open_chars - close_chars != 0:
print('No')
return
fvals = values.copy()
bvals = values
fvals.sort(key=lambda x: (x[0], -x[2]))
bvals.sort(key=lambda x: (x[1], x[2]))
findex = 0
bindex = 0
while True:
while findex < len(fvals) and fvals[findex][3] != 0:
findex += 1
while bindex < len(bvals) and bvals[bindex][3] != 0:
bindex += 1
if findex >= len(fvals) and bindex >= len(bvals):
break
fvals[findex][3] = 1
bvals[bindex][3] = -1
values = [v for v in fvals if v[3] == 1] + [v for v in bvals if v[3] == -1][::-1]
open_close_f = 0
open_close_b = 0
for (oreq_f, _, ocval_f, _), (_, creq_b, ocval_b, _) in zip(values, values[::-1]):
if oreq_f > open_close_f + open_chars:
print('No')
return
if creq_b > open_close_b + close_chars:
print('No')
return
open_close_f += ocval_f
open_close_b -= ocval_b
print('Yes')
if __name__ == '__main__':
main()
|
while True:
x,y=map(int,input().split())
if x==0 and y==0:
break
if x<y:
print(str(x)+" "+str(y))
else:
print(str(y)+" "+str(x))
| 0 | null | 12,164,782,958,510 | 152 | 43 |
def yumiri(L, n):
for i in range(len(L)):
if L[i] > n:
return i - 1
def parunyasu(a, b, p, q):
reans = ans
rd1, rq1, d1, nd1, nq1, q1, newans = naobou(a, q, reans)
rd2, rq2, d2, nd2, nq2, q2, newans = naobou(b, p, newans)
if reans <= newans:
t[d1 - 1] = q1
t[d2 - 1] = q2
return newans
else:
ayaneru(rd2, rq2, d2, nd2, nq2)
ayaneru(rd1, rq1, d1, nd1, nq1)
return reans
def ayaneru(rd, rq, d, nd, nq):
ZL[rd].insert(rq, d)
del ZL[nd][nq + 1]
def naobou(d, q, ans):
newans = ans
rd = t[d - 1] - 1
rq = yumiri(ZL[rd], d)
newans += SUMD[ZL[rd][rq] - ZL[rd][rq - 1] - 1] * c[rd]
newans += SUMD[ZL[rd][rq + 1] - ZL[rd][rq] - 1] * c[rd]
newans -= SUMD[ZL[rd][rq + 1] - ZL[rd][rq - 1] - 1] * c[rd]
del ZL[rd][rq]
nd = q - 1
nq = yumiri(ZL[nd], d)
newans += SUMD[ZL[nd][nq + 1] - ZL[nd][nq] - 1] * c[nd]
newans -= SUMD[d - ZL[nd][nq] - 1] * c[nd]
newans -= SUMD[ZL[nd][nq + 1] - d - 1] * c[nd]
ZL[nd].insert(nq + 1, d)
newans -= s[d - 1][rd]
newans += s[d - 1][nd]
return rd, rq, d, nd, nq, q, newans
def rieshon():
ans = 0
for i in range(D):
ans += s[i][t[i] - 1]
for j in range(26):
if t[i] - 1 == j:
L[j] = 0
ZL[j].append(i + 1)
else:
L[j] += 1
ans -= L[j] * c[j]
for i in range(26):
ZL[i].append(D + 1)
return ans
import time
import random
startTime = time.time()
D = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for _ in range(D)]
L = [0] * 26
ZL = [[0] for _ in range(26)]
SUMD = [0] * (D + 1)
for i in range(1, D + 1):
SUMD[i] = SUMD[i - 1] + i
RD = [0] * 26
t = [0] * D
for i in range(D):
RD[0] += 1
ma = s[i][0] + RD[0] * c[0]
man = 0
for j in range(1, 26):
RD[j] += 1
k = s[i][j] + RD[j] * c[j]
if k > ma:
ma = k
man = j
t[i] = man + 1
RD[man] = 0
ans = rieshon()
while time.time() - startTime < 1.8:
l = random.randrange(min(D - 1, 13)) + 1
d = random.randrange(D - l) + 1
p = t[d - 1]
q = t[d + l - 1]
if p == q: continue
ans = parunyasu(d, d + l, p, q)
for i in range(D):
print(t[i])
|
# 貪欲+山登り(2点交換+1点更新)
import time
import random
d = int(input())
dd = d * (d + 1) // 2
*C, = map(int, input().split())
S = [list(map(int, input().split())) for i in range(d)]
# 貪欲法による初期解の構築
T = []
L = [-1 for j in range(26)]
for i in range(d):
max_diff = -10**7
arg_max = 0
for j in range(26):
memo = L[j]
L[j] = i
diff = S[i][j] - sum([C[k] * (i - L[k]) for k in range(26)])
if diff > max_diff:
max_diff = diff
arg_max = j
L[j] = memo
T.append(arg_max)
L[arg_max] = i
def calc_score(T):
L = [-1 for j in range(26)]
X = [0 for j in range(26)]
score = 0
for i in range(d):
score += S[i][T[i]]
X[T[i]] += (d - i) * (i - L[T[i]])
L[T[i]] = i
for j in range(26):
score -= C[j] * (dd - X[j])
return score
score = calc_score(T)
start = time.time()
while True:
now = time.time()
if now - start > 1.8:
break
# 1点更新
i = random.choice(range(d))
j = random.choice(range(26))
memo = T[i]
T[i] = j
new_score = calc_score(T)
T[i] = memo
if new_score > score:
T[i] = j
score = new_score
# 2点交換
i0 = random.choice(range(d))
z = random.choice(range(10))
i1 = i0 - z if i0 > z else i0 + z
T[i0], T[i1] = T[i1], T[i0]
new_score = calc_score(T)
T[i0], T[i1] = T[i1], T[i0]
if new_score > score:
T[i0], T[i1] = T[i1], T[i0]
score = new_score
# 2点交換
i0 = random.choice(range(d))
z = random.choice(range(10))
i1 = i0 - z if i0 > z else i0 + z
T[i0], T[i1] = T[i1], T[i0]
new_score = calc_score(T)
T[i0], T[i1] = T[i1], T[i0]
if new_score > score:
T[i0], T[i1] = T[i1], T[i0]
score = new_score
for t in T:
print(t + 1)
| 1 | 9,656,188,777,312 | null | 113 | 113 |
import sys
input = sys.stdin.readline
N,K = map(int,input().split())
MOD = 998244353
dp = [0 for _ in range(N+1)]
dp[1] = 1
R = [tuple(map(int,input().split())) for _ in range(K)]
S = [0 for _ in range(K)]
for i in range(2, N+1):
s = 0
for k in range(K):
S[k] += dp[max(0, i - R[k][0])] - dp[max(0, i - R[k][1] - 1)]
S[k] %= MOD
s = (s + S[k]) % MOD
dp[i] = s
print(dp[N])
|
import sys
from collections import deque
sys.setrecursionlimit(10**5)
def input(): return sys.stdin.readline().strip()
"""
Pypyは再帰が遅いらしいのでPythonで通してみる
"""
def main():
N, u, v = map(int, input().split())
u -= 1
v -= 1
repn = [[] for _ in range(N)]
for i in range(N - 1):
a, b = map(int, input().split())
repn[a - 1].append((b - 1, i))
repn[b - 1].append((a - 1, i)) # (to, id)
"""
まずu, vを結ぶ単純パス上にあるnodeを列挙する。
これはvを根とする根付き木に対するdfs(再帰)により求まる(参考:ABC152F)
"""
path = []
def connected(v, tv, p=-1):
if v == tv:
return True
for w, _ in repn[v]:
if w == p:
continue
elif connected(w, tv, v):
path.append(w)
return True
return False
connected(v, u)
path.append(v)
#print("path={}".format(path))
"""
次にu, vを結ぶ単純パスP上の各頂点wに対して、wからPに沿わない向きにどれだけ深く潜れるかを求める。
しかし最終的には「頂点vから(高橋君が逃げ込める中で)最も遠い頂点」を求めればよいので、
uvの中点からdfsすれば十分。
"""
def dfs(v, p): # Pと異なる頂点方向にdfsして深さを求める
d = -1
for w, _ in repn[v]:
if w == p:
continue
else:
d = max(d, dfs(w, v))
return d + 1
dist = len(path)
mid = path[dist // 2 - 1]
par = path[dist // 2]
ans = dfs(mid, par)
print(ans + (dist - 1) // 2)
if __name__ == "__main__":
main()
| 0 | null | 59,732,969,587,360 | 74 | 259 |
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_8_A
??§????????¨?°????????????\?????????
????????????????????????????°?????????¨??§???????????\????????????????????°????????????????????????????????????
"""
if __name__ == '__main__':
# ???????????????????????\???
raw_input = input()
# ??§??????????°????????????\??????????????????
print(raw_input.swapcase())
|
S = raw_input()
tmp = ''
for s in S:
if s.isupper():
tmp += s.lower()
elif s.islower():
tmp += s.upper()
else:
tmp += s
print tmp
| 1 | 1,495,755,351,170 | null | 61 | 61 |
X=int(input())
sen=X//500
X=X%500
go=X//5
print(1000*sen + 5*go)
|
#!/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(X: int):
return X//500*1000 + X % 500//5*5
def main():
sys.setrecursionlimit(10 ** 6)
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
X = int(next(tokens)) # type: int
print(f'{solve(X)}')
if __name__ == '__main__':
main()
| 1 | 42,667,482,109,528 | null | 185 | 185 |
N = int(input())
N=N+1
a_ =sum(list(range(1,N,1)))
a_3=sum(list(range(3,N,3)))
a_5=sum(list(range(5,N,5)))
a_15=sum(list(range(15,N,15)))
ans=a_ + a_15 -(a_3+a_5)
print(ans)
|
S = input()
T = input()
l = len(S) - len(T) + 1
ans = len(T)
t = len(T)
for i in range(l):
s = S[i:i+t]
ans = min(ans, len([0 for i in range(t) if T[i] != s[i]]))
print(ans)
| 0 | null | 19,235,818,525,232 | 173 | 82 |
class Stack:
def __init__(self):
self.values = []
self.n = 0
def pop(self):
if self.n == 0:
raise Exception("stack underflow")
else:
v = self.values[-1]
del self.values[-1]
self.n -= 1
return v
def push(self,x):
self.values.append(x)
self.n += 1
operators = set(['+', '-', '*'])
stack = Stack()
for op in raw_input().split(' '):
if op in operators:
b = stack.pop()
a = stack.pop()
if op == '+':
stack.push(a+b)
elif op == '-':
stack.push(a-b)
elif op == '*':
stack.push(a*b)
else:
raise Exception("Unkown operator")
else:
stack.push(int(op))
print stack.pop()
|
import sys
input = sys.stdin.readline
N,M,L = map(int, input().split())
INF = float("inf")
dist1 = [[INF]*N for i in range(N)]
dist2 = [[INF]*N for i in range(N)]
for i in range(M):
A,B,C = map(int, input().split())
A -= 1
B -= 1
dist1[A][B] = C
dist1[B][A] = C
for i in range(N):
dist1[i][i] = 0
dist2[i][i] = 0
for k in range(N):
for i in range(N):
for j in range(N):
d = dist1[i][k]+dist1[k][j]
if dist1[i][j] <= d:
continue
dist1[i][j] = d
for i in range(N):
for j in range(N):
if dist1[i][j] > L or i==j:
continue
dist2[i][j] = 1
for k in range(N):
for i in range(N):
for j in range(N):
d = dist2[i][k]+dist2[k][j]
if dist2[i][j] <= d:
continue
dist2[i][j] = d
Q = int(input())
for i in range(Q):
s,t = map(int, input().split())
print(dist2[s-1][t-1]-1 if dist2[s-1][t-1]<=L else -1)
| 0 | null | 87,226,485,840,032 | 18 | 295 |
n = input()
a = list(map(int, input().split()))
print(' '.join(map(str, reversed(a))))
|
n = input()
a = map(int, raw_input().split())
s = ""
for i in range(-1, -n, -1):
s += str(a[i]) + " "
s += str(a[-n])
print(s)
| 1 | 978,079,754,498 | null | 53 | 53 |
S = str(input())
name = S[0:3]
print(name)
|
str = input()
str_length = len(str)
import random
start_num = random.randint(0,str_length-3)
print(str[start_num:start_num+3])
| 1 | 14,736,601,341,550 | null | 130 | 130 |
import math
a,b,C = map(float,input().split())
rad = math.radians(C)
h = b*math.sin(rad)
S = a*h/2
L = a+b+math.sqrt(a**2+b**2-2*a*b*math.cos(rad))
print("%.5f\n%.5f\n%.5f" % (S,L,h))
|
import math
a,b,c=map(float,input().split())
C=math.radians(c)
D=math.sin(C)*a*b
S=D/2
E=a**2+b**2-2*a*b*math.cos(C)
F=math.sqrt(E)
L=a+b+F
if C<90:
h=b*math.sin(C)
elif C==90:
h=b
else:
h=b*math.sin(180-C)
print(S,L,h)
| 1 | 171,481,116,052 | null | 30 | 30 |
d=input()
arr = [int(i) for i in d.split()]
t1 = arr[0]/arr[-1]
if t1<= arr[1]:
print("Yes")
else:
print("No")
|
import sys
import numba as nb
import numpy as np
input = sys.stdin.readline
@nb.njit("i8(i8,i8,i8[:,:])", cache=True)
def solve(H, N, AB):
INF = 10 ** 18
dp = np.full(shape=(H + 1), fill_value=INF, dtype=np.int64)
dp[0] = 0
for i in range(N):
A, B = AB[i]
for d in range(H + 1):
if d < A:
dp[d] = min(dp[d], B)
else:
dp[d] = min(dp[d], dp[d - A] + B)
ans = dp[-1]
return ans
def main():
H, N = map(int, input().split())
AB = np.zeros(shape=(N, 2), dtype=np.int64)
for i in range(N):
AB[i] = input().split()
ans = solve(H, N, AB)
print(ans)
if __name__ == "__main__":
main()
| 0 | null | 42,114,559,362,150 | 81 | 229 |
data = input()
x = []
x = data.split()
a = int(x[0])
b = int(x[1])
if(a > b):
print("a > b")
elif(a < b):
print("a < b")
else:
print('a == b')
|
import sys
line = sys.stdin.readline()
a, b =line.split(" ")
a = int(a)
b = int(b)
if a < b:
print "a < b"
elif a > b:
print "a > b"
else:
print "a == b"
| 1 | 350,484,743,608 | null | 38 | 38 |
currAndRot2FBLR = {
'N':{'N':'F','E':'R','S':'B','W':'L'},
'E':{'E':'F','S':'R','W':'B','N':'L'},
'S':{'S':'F','W':'R','N':'B','E':'L'},
'W':{'W':'F','N':'R','E':'B','S':'L'}
}
dice = {
'1': {'F':'2F', 'R':'4R', 'B':'5B', 'L':'3L'},
'2': {'F':'6F', 'R':'4F', 'B':'1F', 'L':'3F'},
'3': {'F':'6L', 'R':'2F', 'B':'1R', 'L':'5F'},
'4': {'F':'6R', 'R':'5F', 'B':'1L', 'L':'2F'},
'5': {'F':'6B', 'R':'3F', 'B':'1B', 'L':'4F'},
'6': {'F':'5B', 'R':'4L', 'B':'2F', 'L':'3R'}
}
faces = list(map(int, input().split()))
cmd = input()
## initial state
currNum = '1'
currDir = 'N'
for c in cmd:
numDir = dice[currNum][currAndRot2FBLR[currDir][c]]
currNum = numDir[0]
currFBLR = numDir[1]
currDir = {v:k for k,v in currAndRot2FBLR[currDir].items()}[currFBLR]
print(faces[int(currNum) - 1])
|
# ['表面', '南面', '東面', '西面', '北面', '裏面']
dice = input().split()
com = [c for c in input()]
rolling = {
'E': [3, 1, 0, 5, 4, 2],
'W': [2, 1, 5, 0, 4, 3],
'S': [4, 0, 2, 3, 5, 1],
'N': [1, 5, 2, 3, 0, 4]
}
for c in com:
dice = [dice[i] for i in rolling[c]]
print(dice[0])
| 1 | 234,810,384,410 | null | 33 | 33 |
N,M,K=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
a_s=[0]
b_s=[0]
for i in range(N):
if a_s[i]+a[i]>K:
break
a_s.append(a[i]+a_s[i])
for i in range(M):
if b_s[i]+b[i]>K:
break
b_s.append(b[i]+b_s[i])
bn=len(b_s)-1
ans=0
for i in range(len(a_s)):
while a_s[i]+b_s[bn]>K:
bn-=1
ans=max(ans,i+bn)
print(ans)
|
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
sum_A = [0 for _ in range(n+1)] # sum_A[i]:=上からi冊の本を読むときにかかる時間
for i in range(1, n+1):
sum_A[i] = sum_A[i-1] + A[i-1]
sum_B = [0 for _ in range(m+1)] # sum_B[i]:=上からj冊の本を読むときにかかる時間
for j in range(1, m+1):
sum_B[j] = sum_B[j-1] + B[j-1]
ans = 0
i = 0
j = m
while i <= n and sum_A[i] <= k:
while sum_A[i] + sum_B[j] > k:
j -= 1
ans = max(ans, i + j)
i += 1
print(ans)
| 1 | 10,796,314,171,132 | null | 117 | 117 |
s = int(input())
print('%d:%d:%d' % (s / 3600, (s % 3600) / 60, s % 60))
|
S = int(input())
S = S % (24 * 3600)
hour = S // 3600
S %= 3600
minutes = S // 60
S %= 60
seconds = S
print("%d:%d:%d" % (hour, minutes, seconds))
| 1 | 338,136,024,878 | null | 37 | 37 |
x1,x2,x3,x4,x5,x6 = map(int,(input().split()))
t = input()
x=x1
L=[x1,x2,x3,x4,x5,x6]
n= len(t)
T=[]
M=[]
for i in range(n):
T.append(t[:1])
t=t[1:]
for i in T:
if i is "S":
L = [L[4],L[0],L[2],L[3],L[5],L[1]]
elif i is "E":
L = [L[3], L[1], L[0], L[5], L[4], L[2]]
elif i is "N":
L = [L[1], L[5], L[2], L[3], L[0], L[4]]
else:
L = [L[2], L[1], L[5], L[0], L[4], L[3]]
print(L[0])
|
a, b, c, d, e, f = map(int, input().split())
S = list(input())
i = 0
number1 = a
number2 = b
number3 = c
number4 = d
number5 = e
number6 = f
while i < len(S) :
if S[i] == "N" :
number1 = b
number2 = f
number3 = c
number4 = d
number5 = a
number6 = e
elif S[i] == "S" :
number1 = e
number2 = a
number3 = c
number4 = d
number5 = f
number6 = b
elif S[i] == "E" :
number1 = d
number2 = b
number3 = a
number4 = f
number5 = e
number6 = c
elif S[i] == "W" :
number1 = c
number2 = b
number3 = f
number4 = a
number5 = e
number6 = d
a = number1
b = number2
c = number3
d = number4
e = number5
f = number6
i = i + 1
print(number1)
| 1 | 233,175,889,778 | null | 33 | 33 |
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)
N = int(input())
S = list(input())
from collections import Counter
c = Counter(S)
ans = c['R']*c['G']*c['B']
for i in range(1, N):
for j in range(N):
first, second, third = j, j+i, j+i*2
if third>=N:
break
if len(set((S[first], S[second], S[third])))==3:
ans -= 1
print(ans)
|
from collections import Counter
N = int(input())
S = input()
c = Counter(S)
ans = c['R'] * c['G'] * c['B']
for s in range(N-2):
for d in range(1, (N-1-s)//2+1):
t = s + d
u = t + d
if S[s] != S[t] and S[t] != S[u] and S[u] != S[s]:
ans -= 1
print(ans)
| 1 | 35,967,397,125,392 | null | 175 | 175 |
from sys import stdin, maxsize
def stdinput():
return stdin.readline().strip()
def main():
n = int(stdinput())
*A, = map(int, stdinput().split(' '))
o = mergeSort(A, 0, n)
print(*A)
print(o)
def merge(A, left, mid, right):
L = A[left:mid]
R = A[mid:right]
cap = maxsize
L.append(cap)
R.append(cap)
i = j = 0
for k in range(left, right):
if L[i] <= R[j]:
A[k] = L[i]
i += 1
else:
A[k] = R[j]
j += 1
return i + j
def mergeSort(A, left, right):
o = 0
if left + 1 < right:
mid = (left + right) // 2
o += mergeSort(A, left, mid)
o += mergeSort(A, mid, right)
o += merge(A, left, mid, right)
return o
if __name__ == '__main__':
main()
# import cProfile
# cProfile.run('main()')
|
import sys
from typing import List
COMP_NUM = 0
def merge(elements: List[int], left: int, mid: int, right: int) -> None:
global COMP_NUM
n1 = mid - left
n2 = right - mid
left_array = [0] * (n1 + 1)
right_array = [0] * (n2 + 1)
left_array[0:n1] = elements[left:left + n1]
left_array[n1] = sys.maxsize
right_array[0:n2] = elements[mid:mid + n2]
right_array[n2] = sys.maxsize
i = 0
j = 0
for k in range(left, right):
COMP_NUM += 1
if left_array[i] <= right_array[j]:
elements[k] = left_array[i]
i += 1
else:
elements[k] = right_array[j]
j += 1
def merge_sort(elements: List[int], left: int, right: int) -> None:
if left + 1 < right:
mid = (left + right) // 2
merge_sort(elements, left, mid)
merge_sort(elements, mid, right)
merge(elements, left, mid, right)
if __name__ == "__main__":
n = int(input())
elements = list(map(lambda x: int(x), input().split()))
merge_sort(elements, 0, len(elements))
print(" ".join([str(elem) for elem in elements]))
print(f"{COMP_NUM}")
| 1 | 111,673,275,168 | null | 26 | 26 |
#!/usr/bin/env python3
import sys
INF = float("inf")
def solve(N: int, T: int, A: "List[int]", B: "List[int]"):
# DP1
# 1からi番目の料理でj分以内に完食できる美味しさの合計の最大値
DP1 = [[0 for _ in range(T+1)] for __ in range(N+1)]
for i in range(1, N+1):
for j in range(1, T+1):
if j-A[i-1] >= 0:
DP1[i][j] = max(DP1[i][j], DP1[i-1][j-A[i-1]]+B[i-1])
DP1[i][j] = max(DP1[i][j], DP1[i-1][j], DP1[i][j-1])
# DP2
# i番目からN番目の料理でj分以内に完食できる美味しさの合計の最大値
DP2 = [[0 for _ in range(T+1)] for __ in range(N+2)]
for i in range(N, 0, -1):
for j in range(1, T+1):
if j-A[i-1] >= 0:
DP2[i][j] = max(DP2[i][j], DP2[i+1][j-A[i-1]]+B[i-1])
DP2[i][j] = max(DP2[i][j], DP2[i+1][j], DP2[i][j-1])
# i番目以外の料理でT-1分以内に完食できる美味しさの合計の最大値
ans = 0
for i in range(1, N+1):
bns = 0
for j in range(T):
bns = max(bns, DP1[i-1][j] + DP2[i+1][T-j-1])
ans = max(ans, bns+B[i-1])
print(ans)
return
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens)) # type: int
T = int(next(tokens)) # type: int
A = [int()] * (N) # type: "List[int]"
B = [int()] * (N) # type: "List[int]"
for i in range(N):
A[i] = int(next(tokens))
B[i] = int(next(tokens))
solve(N, T, A, B)
if __name__ == '__main__':
main()
|
S = int(input())
print("%d:%d:%d"%(S/3600,(S%3600)/60,S%60))
| 0 | null | 76,109,693,893,768 | 282 | 37 |
# -*- coding:utf-8 -*-
def solve():
N = int(input())
"""
■ 解法
N = p^{e1} * p^{e2} * p^{e3} ...
と因数分解できるとき、
z = p_i^{1}, p_i^{2}, ...
としていくのが最適なので、それを数えていく
(例) N = 2^2 * 3^2 * 5 * 7
のとき、
2,3,5,7の4回割れる。(6で割る必要はない)
■ 計算量
素因数分解するのに、O(√N)
割れる回数を計算するのはたかだか素因数分解するより少ないはずなので、
全体としてはO(√N+α)くらいでしょ(適当)
"""
def prime_factor(n):
"""素因数分解する"""
i = 2
ans = {}
while i*i <= n:
while n%i == 0:
if not i in ans:
ans[i] = 0
ans[i] += 1
n //= i
i += 1
if n != 1:
ans[n] = 1
return ans
pf = prime_factor(N)
ans = 0
for key, value in pf.items():
i = 1
while value-i >= 0:
value -= i
ans += 1
i += 1
print(ans)
if __name__ == "__main__":
solve()
|
def main():
n = int(input())
a = list(map(int, input().split()))
a.sort()
ans = 0
sieve = [False for _ in range(1000001)]
for i in range(n):
if sieve[a[i]]:
continue
for j in range(a[i], 1000001, a[i]):
sieve[j] = True
if i > 0:
if a[i] == a[i-1]:
continue
if i < n-1:
if a[i] == a[i+1]:
continue
ans += 1
print(ans)
if __name__ == '__main__':
main()
| 0 | null | 15,649,517,780,640 | 136 | 129 |
N = int(input())
S,T=input().split()
retsu = ""
for i in range(0,N):
mozi = S[i]+T[i]
retsu += mozi
print(retsu)
|
#####segfunc######
def segfunc(x, y):
return x | y
def init(init_val):
# set_val
for i in range(n):
seg[i + num - 1] = set(init_val[i])
# built
for i in range(num - 2, -1, -1):
seg[i] = segfunc(seg[2 * i + 1], seg[2 * i + 2])
def update(k, x):
k += num - 1
seg[k] = set(x)
while k:
k = (k - 1) // 2
seg[k] = segfunc(seg[k * 2 + 1], seg[k * 2 + 2])
def query(p, q):
if q <= p:
return ide_ele
p += num - 1
q += num - 2
res = ide_ele
while q - p > 1:
if p & 1 == 0:
res = segfunc(res, seg[p])
if q & 1 == 1:
res = segfunc(res, seg[q])
q -= 1
p = p // 2
q = (q - 1) // 2
if p == q:
res = segfunc(res, seg[p])
else:
res = segfunc(segfunc(res, seg[p]), seg[q])
return res
#####単位元######
ide_ele = set()
n = int(input())
S = input().strip()
Q = int(input())
num = 2 ** (n - 1).bit_length()
seg = [set()] * 2 * num
init(S)
for _ in range(Q):
a, b, c = [x for x in input().split()]
if a == "1":
update(int(b) - 1, c)
else:
print(len(query(int(b) - 1, int(c))))
| 0 | null | 87,332,334,595,452 | 255 | 210 |
def bubble_sort(data):
for i in range(len(data) - 1):
for j in range(len(data) - 1, i, -1):
if int(data[j][1]) < int(data[j - 1][1]):
data[j], data[j - 1] = data[j - 1], data[j]
def selection_sort(data):
for i in range(len(data)):
min_j = i
for j in range(i, len(data)):
if int(data[j][1]) < int(data[min_j][1]):
min_j = j
if min_j != i:
data[i], data[min_j] = data[min_j], data[i]
input()
cards = [x for x in input().split()]
cards_b = cards.copy()
cards_s = cards.copy()
bubble_sort(cards_b)
selection_sort(cards_s)
for data in [cards_b, cards_s]:
print(' '.join(data))
if data == cards_b:
print("Stable")
else:
print("Not stable")
|
def BubbleSort(C,N):
for i in range(N):
for j in range(i+1, N)[::-1]:
if int(C[j][1]) < int(C[j - 1][1]):
C[j],C[j-1] = C[j-1],C[j]
def SelectionSort(C,N):
for i in range(N):
minj = i
for j in range(i,N):
if int(C[j][1]) < int(C[minj][1]):
minj = j
C[i],C[minj] = C[minj],C[i]
N = int(input())
C = (input()).split()
ORG = C[:]
BubbleSort(C,N)
print (" ".join(C))
print ("Stable")
C2=ORG
SelectionSort(C2,N)
print(" ".join(C2))
if C == C2:
print ("Stable")
else:
print ("Not stable")
| 1 | 24,028,754,740 | null | 16 | 16 |
h,n=map(int,input().split())
ab = [list(map(int,input().split())) for i in range(n)]
dp=[float('inf')]*(h+1)
dp[0]=0
for i in range(h):
for j in range(n):
next=i+ab[j][0] if i+ab[j][0]<=h else h
dp[next]=min(dp[next],dp[i]+ab[j][1])
print(dp[-1])
|
X,K,D = map(int,input().split())
y = abs(X)//D
if K<=y:
print(abs(abs(X)-K*D))
else :
a = (abs(X)-y*D)
b = (abs(X)-(y+1)*D)
C = abs(a)
D = abs(b)
if (K-y)%2==0:
print(C)
else:
print(D)
#print(y+1)
#print(1000000000000000)
| 0 | null | 43,054,937,607,430 | 229 | 92 |
r=input().split()
N=int(r[0])
K=int(r[1])
d=[int(s) for s in input().split()]
ans=0
for i in range(N):
if d[i]>=K:
ans+=1
print(ans)
|
r=int(input())
a=r*r*3
b=1*1*3
print(int(a/b))
| 0 | null | 162,510,018,202,412 | 298 | 278 |
import math
N = int(input())
A = list(map(int,input().split()))
up = [[] for _ in range(N+1)] # 葉から考慮した場合の、最小値
up[N] = [A[N],A[N]]
#print(up)
for i in reversed(range(N)):
Min, Max = up[i+1]
#print(Min,Max)
Min_n = math.ceil(Min/2) + A[i]
Max_n = Max + A[i]
#print(A[i],i)
up[i] = [Min_n, Max_n]
#print(up)
if up[0][0] >= 2:
print(-1)
else:
down = 1
#print(down)
ans = 1
for i in range(1, N+1):
down *= 2
down = min(down,up[i][1]) - A[i]
ans += down + A[i]
print(ans)
|
import sys
N=int(input())
alist=list(map(int,input().split()))
if N==0 and alist[0]!=1:
print(-1)
sys.exit(0)
elif N>0 and alist[0]>0:
print(-1)
sys.exit(0)
#pow2d=1<<N
blist=[0]*(N+1)
for d in reversed(range(1,N+1)):
if d>50:
blist[d-1]=alist[d]+blist[d]
else:
blist[d-1]=min(alist[d]+blist[d],(1<<(d-1))-alist[d-1])
if blist[d-1]<0:
print(-1)
sys.exit(0)
#pow2d>>=1
#print(alist,blist)
cmax_bu=[0]*(N+1)
for d in range(N+1):
cmax_bu[d]=alist[d]+blist[d]
#print(cmax_bu)
pow2d=1
blist=[0]*(N+1)
if alist[0]==0:
blist[0]=1
for d in range(1,N+1):
if pow2d<10**10:
pow2d<<=1
c=min(2*blist[d-1],pow2d,cmax_bu[d])
else:
c=min(2*blist[d-1],cmax_bu[d])
blist[d]=c-alist[d]
if blist[d]<0:
print(-1)
sys.exit(0)
#print(alist,blist)
answer=0
for i in range(N+1):
answer+=alist[i]+blist[i]
print(answer)
| 1 | 18,884,206,913,920 | null | 141 | 141 |
import fractions
def is_lcm(X, Y):
return X*Y // fractions.gcd(X, Y)
N, M = map(int, input().split())
print(is_lcm(N, M))
|
a,b = map(int,input().split())
import math
gc = math.gcd(a,b)
ans = int(a*b/gc)
print(ans)
| 1 | 113,432,827,191,300 | null | 256 | 256 |
S=str(input())
ans=''
for i in range(3):
ans+=S[i]
print(ans)
|
s = input()
ans = s[0:3]
print(ans)
| 1 | 14,752,550,724,224 | null | 130 | 130 |
n = input()
S = set([int(s) for s in raw_input().split()])
m = input()
T = set([int(s) for s in raw_input().split()])
print len(T & S)
|
K,X=map(int,input().split())
print("YNeos"[500*K<X::2])
| 0 | null | 49,082,447,216,048 | 22 | 244 |
n = int(input())
a = list(map(int, input().split()))
m = 1000000007
print(((sum(a)**2 - sum(map(lambda x: x**2, a))) // 2) % m)
|
n = int(input())
Ai = list(map(int, input().split()))
sum_ans = sum(Ai)
ans = 0
mod = 1000000007
for i in range(n-1):
sum_ans -= Ai[i]
ans += sum_ans * Ai[i]
ans %= mod
print(ans)
| 1 | 3,783,374,525,730 | null | 83 | 83 |
class Dice:
def __init__(self,num):
self.num = num.copy()
def east(self):
temp = self.num.copy()
self.num[1-1] = temp[4-1]
self.num[4-1] = temp[6-1]
self.num[6-1] = temp[3-1]
self.num[3-1] = temp[1-1]
def north(self):
temp = self.num.copy()
self.num[1-1] = temp[2-1]
self.num[2-1] = temp[6-1]
self.num[6-1] = temp[5-1]
self.num[5-1] = temp[1-1]
def south(self):
temp = self.num.copy()
self.num[1-1] = temp[5-1]
self.num[5-1] = temp[6-1]
self.num[6-1] = temp[2-1]
self.num[2-1] = temp[1-1]
def west(self):
temp = self.num.copy()
self.num[1-1] = temp[3-1]
self.num[3-1] = temp[6-1]
self.num[6-1] = temp[4-1]
self.num[4-1] = temp[1-1]
def right(self):
temp = self.num.copy()
self.num[2-1] = temp[4-1]
self.num[4-1] = temp[5-1]
self.num[5-1] = temp[3-1]
self.num[3-1] = temp[2-1]
num = list(map(int,input().split()))
dice = Dice(num)
q = int(input())
for _ in range(q):
top,front = map(int,input().split())
while not (top == dice.num[0] or front == dice.num[1]): dice.north()
while top != dice.num[0]: dice.east()
while front != dice.num[1]: dice.right()
print(dice.num[2])
|
import enum
class Direction(enum.Enum):
N = "N"
E = "E"
W = "W"
S = "S"
@classmethod
def value_of(cls, value):
return [v for v in cls if v.value == value][0]
class Dice:
def __init__(self, *value_list):
self.__value_list = list(value_list)
def rotate(self, direction):
l = self.__value_list
if direction == Direction.N:
l[0],l[1],l[5],l[4] = l[1],l[5],l[4],l[0]
elif direction == Direction.E:
l[0],l[2],l[5],l[3] = l[3],l[0],l[2],l[5]
elif direction == Direction.W:
l[0],l[2],l[5],l[3] = l[2],l[5],l[3],l[0]
elif direction == Direction.S:
l[0],l[1],l[5],l[4] = l[4],l[0],l[1],l[5]
def adjust_top_front(self, top_value, front_value):
for _ in range(2):
for _ in range(3):
if self.front_value() == front_value:
break
self.rotate(Direction.N)
if self.front_value() == front_value:
break
self.rotate(Direction.E)
for _ in range(3):
if self.top_value() == top_value:
break
self.rotate(Direction.E)
def top_value(self):
return self.__value_list[0]
def front_value(self):
return self.__value_list[1]
def right_value(self):
return self.__value_list[2]
dice = Dice(*input().split())
for _ in range(int(input())):
top,front = input().split()
dice.adjust_top_front(top, front)
print(dice.right_value())
| 1 | 259,189,276,018 | null | 34 | 34 |
n = int(input())
total = 0
out = 1
for _ in range(n):
a, b = map(int, input().split())
if a == b:
total += 1
else:
if total >= 3:
out = 0
else:
total = 0
if total >= 3:
out = 0
if out:
print("No")
else:
print("Yes")
|
import sys
N = int(input())
p = 0
for i in range(N):
A,B = list(map(int, input().split()))
if A == B:
p += 1
else:
p = 0
if p == 3:
print("Yes")
sys.exit()
print("No")
| 1 | 2,486,315,209,728 | null | 72 | 72 |
n=int(input())
c=list(input())
r=[]
w=[]
for i in range(n):
if c[i]=='R':
r.append(i)
else:
w.append(i)
r.reverse()
ans=0
for i in range(min(len(r),len(w))):
if r[i]>w[i]:
ans+=1
print(ans)
|
import sys
import math
sys.setrecursionlimit(500000)
INF = float('inf')
def main():
n = int(input())
c = input()
red_stones = c.count('R')
return c[red_stones:].count('R')
if __name__ == '__main__':
print(main())
| 1 | 6,358,898,325,012 | null | 98 | 98 |
a,b,c,k=map(int,input().split())
kk=k-a-b
if kk <= 0:
print(min(a,k))
else:
print(a-kk)
|
IDX_TABLE=\
{1:[4,2,3,5],
2:[1,4,6,3],
3:[1,2,6,5],
4:[1,5,6,2],
5:[4,1,3,6],
6:[3,2,4,5]}
nums = ["dummy"] + [int(x) for x in raw_input().split(" ")]
n = int(raw_input())
for i in range(n):
up, front = tuple([int(x) for x in raw_input().split(" ")])
up_idx = nums.index(up)
front_idx = nums.index(front)
ring_list = IDX_TABLE[up_idx]
ring_idx = ring_list.index(front_idx)
print nums[ring_list[(ring_idx+1) % 4]]
| 0 | null | 11,136,009,624,030 | 148 | 34 |
nums = []
while True:
num = [int(e) for e in input().split()]
if num[0]==0 and num[1]==0:
break
nums.append(num)
for i in range(len(nums)):
for j in range(len(nums[i])):
for k in range(j):
if nums[i][k] > nums[i][j]:
a = nums[i][k]
nums[i][k] = nums[i][j]
nums[i][j] = a
for i in range(len(nums)):
print(" ".join(map(str, nums[i])))
|
n = int(input())
a = [int(i) for i in input().split()]
print(min(a), max(a), sum(a))
| 0 | null | 615,807,421,310 | 43 | 48 |
import math
import sys
while True:
try:
a,b = map(int, input().split())
print(len(str(a+b)))
except EOFError:
break
|
import sys
from operator import add
for i in sys.stdin:
print(len(str(add(*list(map(int, i.split()))))))
| 1 | 153,023,296 | null | 3 | 3 |
n=int(input())
A=[int(x) for x in input().split()]
q=int(input())
m=[int(x) for x in input().split()]
A_sum = []
for bit in range(1,2**n):
a_sum = 0
for i in range(n):
if bin((bit>>i) & 0b1) == '0b1':
a_sum += A[i]
A_sum.append(a_sum)
for mi in m:
if mi in A_sum:
print('yes')
else:
print('no')
|
def solve(pos, tot, A):
if tot == 0: return True
if pos > len(A) - 1: return False
return solve(pos+1, tot - A[pos], A) or \
solve(pos+1, tot, A)
n = raw_input()
b = map(int, raw_input().split())
n = raw_input()
t = map(int, raw_input().split())
max = sum(b) + 1
for tt in t:
print 'yes' if tt < max and True == solve(0, tt, b) else 'no'
| 1 | 101,109,231,960 | null | 25 | 25 |
X = int(input())
gohyaku = X // 500
X = X - (gohyaku * 500)
goen = X // 5
print(gohyaku * 1000 + goen * 5)
|
import math
n = int(input())
a = [0]*n
b = [0]*n
for i in range(n):
a[i], b[i] = list(map(int, input().split()))
a.sort()
b.sort()
ans = 0
h = int(n/2)
if n % 2 == 0:
a_harf = a[h-1] + a[h]
b_harf = b[h-1] + b[h]
ans = b_harf - a_harf + 1
else:
ans = b[h] - a[h] + 1
print(ans)
| 0 | null | 29,969,447,967,988 | 185 | 137 |
n = int(input())
ph = 0
pl = 10**10
mh = -10**10
ml = 10**10
for _ in range(n):
x,y = map(int,input().split())
ph = max(ph,x+y)
pl = min(pl,x+y)
mh = max(mh,x-y)
ml = min(ml,x-y)
print(max(ph-pl,mh-ml))
|
import sys, math
from functools import lru_cache
sys.setrecursionlimit(10**9)
MOD = 10**9+7
def input():
return sys.stdin.readline()[:-1]
def mi():
return map(int, input().split())
def ii():
return int(input())
def i2(n):
tmp = [list(mi()) for i in range(n)]
return [list(i) for i in zip(*tmp)]
def main():
N = ii()
x, y = i2(N)
Mw = max(x[i]+y[i] for i in range(N))
mw = min(x[i]+y[i] for i in range(N))
Mz = max(x[i]-y[i] for i in range(N))
mz = min(x[i]-y[i] for i in range(N))
print(max(Mz-mz, Mw-mw))
if __name__ == '__main__':
main()
| 1 | 3,438,827,085,280 | null | 80 | 80 |
N, M, Q = map(int, input().split())
A, B, C, D = [],[],[],[]
for i in range(Q):
a, b, c, d = map(int, input().split())
A.append(a)
B.append(b)
C.append(c)
D.append(d)
L = []
def dfs(S):
if len(S) == N:
L.append(S)
elif S == "":
for i in range(M):
S = str(i)
dfs(S)
else:
for i in range(int(S[-1]), M):
dfs(S+str(i))
dfs("")
Ans = 0
for l in L:
ans = 0
for a, b, c, d in zip(A,B,C,D):
if int(l[b-1]) - int(l[a-1]) == c:
ans += d
Ans = max(ans, Ans)
print(Ans)
|
n, m, q = map(int, input().split())
abcd = []
for _ in range(q):
abcd.append(tuple(map(int, input().split())))
ans = 0
from itertools import combinations_with_replacement
for A in combinations_with_replacement(list(range(1, m+1)), n):
suma = 0
for a,b,c,d in abcd:
if A[b-1] - A[a-1] == c:
suma += d
ans = max(ans, suma)
print(ans)
| 1 | 27,703,499,495,200 | null | 160 | 160 |
N,M=map(int,input().split())
a=N//2
b=a+1
print(a,b)
if M==1:
exit(0)
c=1
d=N-1
print(c,d)
for i in range(M-2):
if i%2==0:
a,b=a-1,b+1
print(a,b)
else:
c,d=c+1,d-1
print(c,d)
|
N, M = map(int, input().split())
ans = []
if N % 2 == 1:
l, r = 1, N - 1
while l < r:
ans.append((l, r))
l += 1
r -= 1
else:
l, r = 1, N - 1
flag = False
while l < r:
if not flag and r - l <= N // 2:
r -= 1
flag = True
ans.append((l, r))
l += 1
r -= 1
for i in range(M):
print(ans[i][0], ans[i][1])
| 1 | 28,607,564,762,130 | null | 162 | 162 |
A = input()
B = input()
se = set(["1","2","3"])
a = se-set([A,B])
print(a.pop())
|
a = int(input())
b = int(input())
ans = [1,2,3]
ans.remove(a)
ans.remove(b)
print(ans[0])
| 1 | 110,491,011,240,160 | null | 254 | 254 |
import sys
counter = {k: 0 for k in "abcdefghijklmnopqrstuvwxyz"}
while True:
try:
line = raw_input()
except:
break
for i in line.lower():
if i in counter.keys():
counter[i] += 1
for k in sorted(counter.keys()):
print "{} : {}".format(k,counter[k])
|
text = []
try:
while True:
text.append(input())
except:
for s in [chr(k) for k in range(97,97+26)]:
count = 0
for i in text:
count += i.lower().count(s)
print(s,":",count)
| 1 | 1,637,693,597,232 | null | 63 | 63 |
N, M=map(int,input().split())
s=list()
c=list()
a=0
for i in range(0,M):
s.append(0)
c.append(0)
for i in range(0,M):
s[i], c[i]=map(int,input().split())
for i in range(0,M):
for j in range(0,M):
if s[i]==s[j] and c[i]!=c[j]:
print("-1")
break
else:
continue
break
else:
if N==1:
b=[0]
for i in range(0,M):
b[s[i]-1]=c[i]
print(b[0])
else:
b=[1]
for i in range(0,N-1):
b.append(0)
for i in range(0,M):
if s[i]==1 and c[i]==0:
print("-1")
break
else:
for i in range(0,M):
b[s[i]-1]=c[i]
for i in range(0,N):
print(b[i],end='')
|
k = int(input())
a, b = list(map(int, input().split()))
c = a / k
if c == int(c):
print("OK")
else:
d = int(c) + 1
if (d*k) <=b:
print("OK")
else:
print("NG")
| 0 | null | 43,606,893,195,560 | 208 | 158 |
A,B = map(int, input().split())
if A < B:
print(str(A) * B)
else:
print(str(B) * A)
|
a,b,c=[int(x) for x in input().split()]
if a+b+c>=22:
print('bust')
else:
print('win')
| 0 | null | 101,434,589,851,812 | 232 | 260 |
#!/usr/bin/env python3
def main():
N = input()
l = len(N)
K = int(input())
ans = 0
# 最上位K桁が0の場合
# l-K_C_K * 9^K
if K == 1:
ans += (l-1) * 9
elif K == 2:
ans += (l-1) * (l-2) * 81 // 2
else:
ans += (l-1) * (l-2) * (l-3) * 729 // 6
if K == 1:
# 最上位の数以外0
ans += int(N[0])
elif K == 2:
# 最上位1桁が0ではなく,残りl-1桁中1桁だけ0以外(Nより大きくならないように注意)
if l >= 2:
# ans += int(N[0]) * (l-1) * 9 - (9 - int(N[1]))
for a in range(1,int(N[0])+1):
for b in range(l-1):
for p in range(1,10):
tmp = a * 10**(l-1) + p * 10**b
if tmp <= int(N):
ans += 1
else:
# 最上位1桁が0ではなく,残りl-1桁中2桁だけ0以外(Nより大きくならないように注意)
if l >= 3:
NN = int(N)
N0 = int(N[0])
L = 10**(l-1)
ans += (N0-1) * (l-1) * (l-2) * 81 // 2
for p in range(1,10):
for q in range(1,10):
for b in range(l-1):
for c in range(b+1,l-1):
tmp = N0 * L + p * 10**b + q * 10**c
if tmp <= NN:
ans += 1
else:
break
print(ans)
if __name__ == "__main__":
main()
|
import sys
readline = sys.stdin.readline
DIV = 998244353
N,S = map(int,readline().split())
A = list(map(int,readline().split()))
# ある数の取り得る状態は以下
# (1)部分集合Pに含まれない
# (2)部分集合Pに含まれ、和に使われている
# (3)部分集合Pに含まれ、和に使われていない
# dp[k] = 総和がkになっている場合の数
dp = [0] * (S + 1)
dp[0] = 1
for i in range(N):
nextline = [0] * (S + 1)
for k in range(S, -1, -1):
# (1)部分集合Pに含まれない
# (3)部分集合Pに含まれ、和に使われていない
nextline[k] += dp[k] * 2
nextline[k] %= DIV
# (2)部分集合Pに含まれ、和に使われている
if k + A[i] <= S:
nextline[k + A[i]] += dp[k]
nextline[k + A[i]] %= DIV
dp = nextline
print(dp[-1])
| 0 | null | 46,675,397,787,808 | 224 | 138 |
while True:
try:
a = map(int,raw_input().split(' '))
s = a[0] + a[1]
c = 0
while s > 0:
s = s / 10
c = c + 1
if s <= 0:
print c
break
except (EOFError):
break
|
while True:
try:
[a, b] = map(int, raw_input().split())
sum = a + b
count = 0
while sum:
count += 1
sum /= 10
print count
except (EOFError):
break;
| 1 | 111,571,482 | null | 3 | 3 |
H = int(input())
en = 1
at = 0
while H > 0:
H //= 2
at += en
en *= 2
print(at)
|
N,M = map(int,input().split())
home = list(map(int,input().split()))
if N - sum(home) < 0:
print("-1")
else:
print(N - sum(home))
| 0 | null | 55,985,057,444,652 | 228 | 168 |
n,k = map(int, input().split())
price = list(map(int, input().split()))
count = 0
sum = 0
while count < k:
abc = min(price)
sum += abc
price.pop(price.index(abc))
count += 1
print(sum)
|
A,B,M=map(int,input().split())
a=[int(i)for i in input().split()]
b=[int(i)for i in input().split()]
xyc=[[int(i)for i in input().split()]for j in range(M)]
res=min(a)+min(b)
for x,y,c in xyc:
tmp=a[x-1]+b[y-1]-c
res=min(res,tmp)
print(res)
| 0 | null | 32,779,913,163,088 | 120 | 200 |
#素数関連
def prime_numbers(x):
if x < 2:
return []
prime_numbers = [i for i in range(x)]
prime_numbers[1] = 0
for prime_number in prime_numbers:
if prime_number > math.sqrt(x):
break
if prime_number == 0:
continue
for composite_number in range(2 * prime_number, x, prime_number):
prime_numbers[composite_number] = 0
return [prime_number for prime_number in prime_numbers if prime_number != 0]
def is_prime(x):
if x < 2:
return False
if x == 2 or x == 3 or x == 5:
return True
if x % 2 == 0 or x % 3 == 0 or x % 5 == 0:
return False
prime_number = 7
difference = 4
while prime_number <= math.sqrt(x):
if x % prime_number == 0:
return False
prime_number += difference
difference = 6 - difference
return True
from collections import deque
n, d, a = map(int, input().split())
xh = [list(map(int, input().split())) for i in range(n)]
xh.sort()
queue = deque()
ans = 0
cur = 0
for i in range(n):
x, h = xh[i]
while queue != deque() and queue[0][0] < x:
cur -= queue.popleft()[1]
h -= cur
if h > 0:
cur += a * ((h - 1) // a + 1)
ans += (h - 1) // a + 1
queue.append([x + 2 * d, a * ((h - 1) // a + 1)])
print(ans)
|
n=int(input())
A=list(map(int,input().split()))
ans = list(range(n))
for i in range(n):
ans[A[i]-1] = i+1
print(" ".join(map(str,ans)))
| 0 | null | 131,338,009,058,166 | 230 | 299 |
s = input()
n = len(s)
end = s[n-1]
if end=='s':
print(s+'es')
else:
print(s+'s')
|
s=input()
if len(s)%2==0 and s == 'hi'*(len(s)//2):
print('Yes')
else:
print('No')
| 0 | null | 27,954,837,114,412 | 71 | 199 |
N = int(input())
edge = [[] for _ in range(N)]
for _ in range(N):
l = list(map(int, input().split()))
for j in range(l[1]):
edge[l[0]-1].append(l[2+j]-1)
now_list = [0]
next_list = []
dist = 0
dists = [-1] * N
while len(now_list) > 0:
for _ in range(len(now_list)):
v = now_list.pop()
if dists[v] == -1:
dists[v] = dist
else:
continue
for nv in edge[v]:
next_list.append(nv)
now_list = next_list
next_list = []
dist += 1
for i in range(N):
print(1+i, dists[i])
|
from collections import deque
def main():
while que:
v, d = que.popleft()
for nv in G[v]:
nv -= 1
if D[nv] == -1:
D[nv] = D[v] + 1
que.append((nv, D[nv]))
for i in range(N):
print(i+1, D[i])
if __name__ == "__main__":
N = int(input())
G = [[] for _ in range(N)]
for _ in range(N):
ipts = [int(ipt) for ipt in input().split()]
G[ipts[0]-1] = ipts[2:]
D = [-1]*N
D[0] = 0
que = deque()
que.append((0, 0))
main()
| 1 | 3,763,151,040 | null | 9 | 9 |
a,b=map(int,input().split());print([0,a-b*2][a-b*2>0])
|
# ABC143
# B Tkoyaki Festival 2019
n = int(input())
D = list(map(int, input().split()))
ct = 0
for i in range(n):
for j in range(n):
if i != j:
if i < j:
ct += D[i] * D[j]
print(ct)
| 0 | null | 167,575,255,230,240 | 291 | 292 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.