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 numpy as np
N = int(input())
X = str(input())
num_one = X.count("1")
dp = [-1] * N
dp[0] = 0
def dfs(n):
if dp[n] == -1:
dp[n] = 1 + dfs(n % bin(n).count('1'))
return dp[n]
num_one = X.count("1")
bool_arr = np.array([True if X[N-i-1] == "1" else False for i in range(N)])
zero_ver = np.array([pow(2, i, num_one + 1) for i in range(N)])
zero_ver_sum = sum(zero_ver[bool_arr])
one_ver = -1
one_ver_sum = 0
flag = False
if num_one != 1:
one_ver = np.array([pow(2, i, num_one - 1) for i in range(N)])
one_ver_sum = sum(one_ver[bool_arr])
else:
flag = True
for i in range(1,N+1):
start = 0
if bool_arr[N-i] == False:
start = (zero_ver_sum + pow(2, N - i, num_one + 1)) % (num_one + 1)
print(dfs(start)+1)
else:
if flag:
print(0)
else:
start = (one_ver_sum - pow(2, N - i, num_one - 1)) % (num_one - 1)
print(dfs(start)+1)
| #!/usr/bin/env python3
H = int(input())
i = 1
while H >= 2**i:
i += 1
ans = 0
for x in range(i):
ans += 2 ** x
print(ans)
| 0 | null | 44,043,439,093,540 | 107 | 228 |
s = [0] * 13
h = [0] * 13
c = [0] * 13
d = [0] * 13
num = int(input())
for i in range(num):
m, n = input().split()
x = int(n) - 1
if m == "S":
s[x] = 1
elif m == "H":
h[x] = 1
elif m == "C":
c[x] = 1
elif m == "D":
d[x] = 1
else:
break
for i in range(13):
if s[i] == 0:
print("S %d" % (i+1))
for i in range(13):
if h[i] == 0:
print("H %d" % (i+1))
for i in range(13):
if c[i] == 0:
print("C %d" % (i+1))
for i in range(13):
if d[i] == 0:
print("D %d" % (i+1))
| import sys
while True:
ins = input().split()
h = int(ins[0])
w = int(ins[1])
if h == 0 and w == 0:
break
for i in range(h):
for j in range(w-1):
sys.stdout.write("#")
print("#")
print("") | 0 | null | 906,252,005,178 | 54 | 49 |
N = int(input())
A = list(map(int, input().split()))
ans = 'APPROVED'
for i in range(0,N):
if (A[i]%2)==0:
if not ((A[i] % 3) == 0 or (A[i] % 5) == 0):
ans = 'DENIED'
break
print(ans) | a,b,c=input().split();print(c,a,b) | 0 | null | 53,421,568,984,980 | 217 | 178 |
station = input()
print("Yes" if "AB" in station or "BA" in station else "No") | N, M = map(int, input().split())
A = list(map(int, input().split()))
D = sum(A)
cnt = 0
for i in range(N):
if A[i] * 4 * M >= D:
cnt += 1
if cnt >= M:
print('Yes')
else:
print('No') | 0 | null | 46,530,066,068,762 | 201 | 179 |
import sys
sys.setrecursionlimit(1000000000)
import math
from math import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations, chain
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7
def meg(f, ok, ng):
while abs(ok-ng)>1:
mid = (ok+ng)//2
if f(mid):
ok=mid
else:
ng=mid
return ok
def get_inv(n, modp):
return pow(n, modp-2, modp)
def factorials_list(n, modp): # 10**6
fs = [1]
for i in range(1, n+1):
fs.append(fs[-1] * i % modp)
return fs
def invs_list(n, fs, modp): # 10**6
invs = [get_inv(fs[-1], modp)]
for i in range(n, 1-1, -1):
invs.append(invs[-1] * i % modp)
invs.reverse()
return invs
def comb(n, k, modp):
num = 1
for i in range(n, n-k, -1):
num = num * i % modp
den = 1
for i in range(2, k+1):
den = den * i % modp
return num * get_inv(den, modp) % modp
def comb_from_list(n, k, modp, fs, invs):
return fs[n] * invs[n-k] * invs[k] % modp
#
class UnionFindEx:
def __init__(self, size):
#正なら根の番号、負ならグループサイズ
self.roots = [-1] * size
def getRootID(self, i):
r = self.roots[i]
if r < 0: #負なら根
return i
else:
r = self.getRootID(r)
self.roots[i] = r
return r
def getGroupSize(self, i):
return -self.roots[self.getRootID(i)]
def connect(self, i, j):
r1, r2 = self.getRootID(i), self.getRootID(j)
if r1 == r2:
return False
if self.getGroupSize(r1) < self.getGroupSize(r2):
r1, r2 = r2, r1
self.roots[r1] += self.roots[r2] #サイズ更新
self.roots[r2] = r1
return True
Yes = 'Yes'
No = 'No'
def main():
N=ii()
up = []
down = []
for _ in range(N):
S = input()
h = 0
b = 0
for s in S:
if s=='(':
h += 1
else:
h -= 1
b = min(b, h)
#
if h>=0:
up.append((h, b))
else:
down.append((h, b))
#
up.sort(key=lambda t: t[1], reverse=True)
down.sort(key=lambda t: t[0]-t[1], reverse=True)
H = 0
for h, b in up:
if H+b>=0:
H += h
else:
print(No)
return
for h, b in down:
if H+b>=0:
H += h
else:
print(No)
return
#
if H == 0:
print(Yes)
else:
print(No)
main()
| import sys
input=lambda: sys.stdin.readline().strip()
n=int(input())
A=[]
#
PM=[[0,0] for i in range(n)]
for i in range(n):
now=0
mini=0
for j in input():
if j=="(": now+=1
else:
now-=1 ; mini=min(mini,now)
PM[i]=[mini,now]
if sum( [PM[i][1] for i in range(n)] )!=0 :
print("No")
exit()
MINI=0
NOW=0
PMf=[PM[i] for i in range(n) if PM[i][1]>=0]
PMf.sort()
for i in range(len(PMf)):
MINI=min(MINI , NOW+PMf[-i-1][0] )
NOW+=PMf[-i-1][1]
if MINI<0 : print("No") ; exit()
PMs=[PM[i] for i in range(n) if PM[i][1]<0]
PMs=sorted(PMs , key=lambda x : x[1]-x[0])
for i in range(len(PMs)):
MINI=min(MINI , NOW+PMs[-i-1][0] )
NOW+=PMs[-i-1][1]
if MINI<0 : print("No") ; exit()
print("Yes")
| 1 | 23,605,079,044,192 | null | 152 | 152 |
from collections import deque
def bfs():
que=deque([])
que.append(0)
v[0]=0
while que:
p=que.popleft()
for l in L[p]:
if v[l-1]==-1:
que.append(l-1)
v[l-1]=v[p]+1
n=int(input())
L=[]
for i in range(n):
A=[]
A=list(map(int,input().split()))
L.append(A[2:])
v=[-1]*n
bfs()
for i in range(n):
print(i+1,v[i])
| W = raw_input().lower()
c = 0
while True:
T = raw_input()
if T == "END_OF_TEXT":
break
Ti = T.lower().split()
for i in Ti:
if i == W:
c += 1
print("%d" % (c, )) | 0 | null | 933,984,778,888 | 9 | 65 |
N = int(input())
A = list(map(int, input().split()))
m = 1000000007
result = 0
for i in range(60):
j = 1 << i
c = sum((a & j) >> i for a in A)
result += (c * (N - c)) << i
result %= m
print(result)
| n,d = map(int,input().split())
count = 0
for i in range(n):
x,y = map(float,input().split())
if(d*d >= x*x + y*y):
count+=1
print(count) | 0 | null | 64,693,032,157,600 | 263 | 96 |
W,H,x,y,r=map(int,input().split())
if (0<=x+r<=W and 0<=y+r<=H)and(0<=x-r<=W and 0<=y-r<=H):
print('Yes')
else:
print('No')
| n,k=map(int,input().split())
l=list(map(int,input().split()))
ans=sum(x>=k for x in l)
print(ans) | 0 | null | 89,668,534,742,410 | 41 | 298 |
N = int(input())
print(N//2 if N/2 == N//2 else N//2 + 1) | def main():
h,n=map(int,input().split())
dp=[10**9]*(h+1)
dp[h]=0
for i in range(n):
a,b=map(int,input().split())
for j in range(h,0,-1):
nxt=j-a
if nxt<0:
nxt=0
if dp[nxt]>dp[j]+b:
dp[nxt]=dp[j]+b
print(dp[0])
main() | 0 | null | 70,481,020,465,082 | 206 | 229 |
from decimal import Decimal
A, B = input().split()
print(int(int(A)*Decimal(B))) | n = int(input())
mod = 10**9 + 7
per = [1] * (n+1)
for i in range(1, n+1):
per[i] = per[i-1] * i
per[i] %= mod
inv = [1] * (n+1)
inv[-1] = pow(per[-1], mod-2, mod)
for j in range(2, n+2):
inv[-j] = inv[-j+1] * (n-j+2)
inv[-j] %= mod
def C(n, k):
cmb = per[n] * inv[k] * inv[n-k]
cmb %= mod
return cmb
total = 0
for k in range(1, n+1):
if n < 3*k:
break
total += C(n-2*k-1, k-1)
total %= mod
print(total)
| 0 | null | 9,907,902,259,332 | 135 | 79 |
N = int(input())
A = list(map(int, input().split()))
mod = 10**9+7
RGB = [0, 0, 0]
ans = 1
for i in range(N):
cnt = RGB.count(A[i])
ans = (ans * cnt) % mod
for j in range(3):
if A[i] == RGB[j]:
RGB[j] += 1
break
print(ans)
| #B問題
N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(0,len(A)):
for j in range(i+1,len(A)):
for k in range(j+1,len(A)):
if A[i] + A[j] > A[k] and A[j] + A[k] > A[i] and A[k] + A[i] > A[j] and A[i] != A[j] and A[j] != A[k] and A[k] != A[i] :
ans += 1
print(ans) | 0 | null | 67,580,463,494,852 | 268 | 91 |
# Queue
class MyQueue:
def __init__(self):
self.queue = [] # list
self.top = 0
def isEmpty(self):
if self.top==0:
return True
return False
def isFull(self):
pass
def enqueue(self, x):
self.queue.append(x)
self.top += 1
def dequeue(self):
if self.top>0:
self.top -= 1
return self.queue.pop(0)
else:
print("there aren't enough values in Queue")
def main():
n, q = map(int, input().split())
A = [
list(map(lambda x: int(x) if x.isdigit() else x, input().split()))
for i in range(n)
]
A = [{'name': name, 'time': time} for name, time in A]
queue = MyQueue()
for a in A:
queue.enqueue(a)
elapsed = 0
while not queue.isEmpty():
job = queue.dequeue()
if job['time']>q:
elapsed += q
job['time'] = job['time'] - q
queue.enqueue(job)
else:
elapsed += job['time']
print(job['name']+ ' ' + str(elapsed))
main()
| n,q = map(int,input().split())
tmp = [input().split() for i in range(n)]
a = [[name,int(time)] for name,time in tmp]
total_time = 0
count = 0
while(a):
current = a.pop(0)
if(current[1] > q):
current[1] -= q
a.append(current)
total_time += q
else:
total_time += current[1]
print(current[0],total_time,sep = " ")
| 1 | 42,404,542,740 | null | 19 | 19 |
k = int(input())
print((k/3)**3)
| N,M= map(int, input().split())
num = list(range(1,N+1))
ans = []
if M % 2 == 0:
for i in range(M // 2):
ans.append([i+1,M+1-i])
for i in range(M // 2):
ans.append([i+2+M,2*M+1-i])
if M % 2 != 0:
for i in range(M // 2):
ans.append([i+1,M-i])
for i in range(M - M//2):
ans.append([i+1+M,2*M+1-i])
for an in ans:
print(an[0],an[1])
| 0 | null | 37,783,505,534,800 | 191 | 162 |
N,K = map(int,input().split())
LR = []
for _ in range(K): LR.append(list(map(int,input().split())))
MOD = 998244353
dp = [0]*(N+1)
dp[0] = 1
for i in range(N):
for L,R in LR:
if i+L<N: dp[i+L] += dp[i]
if i+R+1<N: dp[i+R+1] -= dp[i]
if i>0: dp[i+1] = (dp[i+1]+dp[i])%MOD
print(dp[-1]) | #もらうDPで考える
N, K = map(int, input().split())
l = []
r = []
MOD = 998244353
for i in range(K):
a,b = map(int, input().split())
l.append(a)
r.append(b)
dp = [0] * (N+1)
#スタート地点が1なので、dp[1]を1とする
dp[1] = 1
dpsum = [0] * (N+1)
dpsum[1] = 1
for i in range(2, N+1):
for j in range(K):
#[l[i], r[i]]
li = i - r[j]
ri = i - l[j]
if ri < 0: continue
# l1が負になるケースをハンドリング
li = max(1, li)
dp[i] += (dpsum[ri] - dpsum[li-1])%MOD #dp[li] ~ dp[ri]の和を足す
dpsum[i] = (dpsum[i-1] + dp[i])%MOD
print(dp[N]%MOD) | 1 | 2,658,382,820,670 | null | 74 | 74 |
#!/usr/bin/python3
#coding: utf-8
import math
N = int(input())
P = [[int(x) for x in input().split()] for _ in range(N)]
def calc(path):
r = 0
for i in range(len(path) - 1):
p1 = P[path[i-1]]
p2 = P[path[i]]
dx = p1[0] - p2[0]
dy = p1[1] - p2[1]
r += math.sqrt(dx*dx + dy*dy)
return r
ret = 0
numret = 0
def rec(path, rest):
if not rest:
global ret
global numret
ret += calc(path)
numret += 1
return
for i in range(len(rest)):
rec(path + [rest[i]], rest[:i] + rest[i+1:])
rec([], [i for i in range(N)])
print(ret/numret)
| import math
import itertools
n = int(input())
data = []
for i in range(n):
x, y = map(int,input().split())
data.append((x, y))
cnt = 0
for a in itertools.permutations(range(n)):
for j in range(n-1):
cnt += ((data[a[j+1]][0]-data[a[j]][0])**2 + (data[a[j+1]][1]-data[a[j]][1])**2)**0.5
print(cnt / math.factorial(n)) | 1 | 149,135,920,603,268 | null | 280 | 280 |
n = int(input())
nPowTen = pow(10,n,pow(10,9) + 7)
nPowNine = pow(9,n,pow(10,9) + 7)
nPowEight = pow(8,n,pow(10,9) + 7)
print((nPowTen - nPowNine - nPowNine +nPowEight) % (pow(10,9) + 7)) | t = int(input())
mod = 10**9 + 7
if t==1:
print(0)
else:
kans = 1
for i in range(t):
kans *= 10
kans = kans % mod
ans = 1
for i in range(t):
ans *= 9
ans = ans % mod
bans = 1
for i in range(t):
bans *= 8
bans = bans % mod
f = kans - ans*2 + bans
f = f%mod
print(f)
| 1 | 3,161,930,076,422 | null | 78 | 78 |
import sys
N, K = map(int, sys.stdin.readline().rstrip().split())
H = [int(x) for x in sys.stdin.readline().rstrip().split()]
H.sort()
# print(H[:-K])
if K == 0:
print(sum(H))
else:
print(sum(H[:-K])) | 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=I()
h = l()
h.sort(reverse=True)
print(sum(h[k:])) | 1 | 78,962,267,754,628 | null | 227 | 227 |
import sys
sys.setrecursionlimit(10**7)
import fileinput
def gcd(a, b):
while b:
a, b = b, a % b
return a
def lcm(a, b):
return a * b // gcd(a, b)
for line in sys.stdin:
x, y = map(int, line.split())
print(gcd(x,y),lcm(x,y))
| def gcm(a, b):
if a<b: a,b = b,a
if (a%b ==0):
return b
else:
return gcm(b, a%b)
def lcm(a, b):
return a/gcm(a,b)*b
while True:
try:
a, b = map(int, raw_input().split())
except:
break
print "%d %d" %(gcm(a,b), lcm(a,b)) | 1 | 547,191,370 | null | 5 | 5 |
import math
N = int(input())
start_digit = math.ceil(math.sqrt(N))
for i in range(start_digit, 0, -1):
q, r = divmod(N, i)
if r == 0:
output = i+q-2
break
print(output) | N = int(input())
search_max = int(N**0.5)
min_number = 10**12
for x in range(1, search_max + 1):
if N % x == 0:
y = N // x
if x + y < min_number:
min_number = x + y
print(min_number-2) | 1 | 162,039,474,391,238 | null | 288 | 288 |
H,N=map(int,input().split())
deathblows=map(int,input().split())
if sum(deathblows) >= H:
print('Yes')
else:
print('No') | h,n = map(int, input().split())
a = list(map(int, input().split()))
print('Yes' if h-sum(a) <= 0 else 'No') | 1 | 78,220,282,319,552 | null | 226 | 226 |
# ??\?????????
N = int(input())
r = [int(input()) for i in range(N)]
# ?????§????¨????
max_profit = (-1) * (10 ** 9)
min_value = r[0]
for j in range(1, len(r)):
max_profit = max(max_profit, r[j] - min_value)
min_value = min(min_value, r[j])
print(max_profit) | from sys import stdin
n = int(input())
r=[int(input()) for i in range(n)]
rv = r[::-1][:-1]
m = None
p_r_j = None
for j,r_j in enumerate(rv):
if p_r_j == None or p_r_j < r_j:
p_r_j = r_j
if p_r_j > r_j:
continue
r_i = min(r[:-(j+1)])
t = r_j - r_i
if m == None or t > m:
m = t
print(m) | 1 | 13,033,672,442 | null | 13 | 13 |
from collections import deque
s = input()
Q = int(input())
que = deque(s)
cnt = 0
for i in range(Q):
q = input().split()
if q[0] == '1':
cnt += 1
else:
reverse = cnt % 2
if q[1] == '1':
if reverse == 0:
s = que.appendleft(q[2])
else:
s = que.append(q[2])
else:
if reverse == 0:
s = que.append(q[2])
else:
s = que.appendleft(q[2])
ans = ''.join(que)
if cnt % 2 == 0:
print(ans)
else:
print(ans[::-1]) | #from sys import stdin
#input = stdin.readline
#inputRstrip = stdin.readline().rstrip
#x = input().rstrip()
#n = int(input())
#a,b,c = input().split()
#a,b,c = map(int, input().split())
ST = [input() for i in range(2)]
S = ST[0]
T = ST[1]
ans = len(T)
for i in range(len(S) - len(T) + 1):
count = len(T)
for j in range(len(T)):
if(T[j] == S[i + j]):
count -= 1
if(ans > count):
ans = count
print(ans) | 0 | null | 30,636,993,605,208 | 204 | 82 |
from collections import defaultdict
h,w,m = map(int,input().split())
dh = defaultdict(int)
dw = defaultdict(int)
l=defaultdict(tuple)
for _ in range(m):
x,y = map(int,input().split())
dh[x]+=1
dw[y]+=1
l[(x,y)]=1
m = 0
for i in range(1,h+1):
if dh[i]>m:
m = dh[i]
hi=i
c=m
m = 0
for i in range(1,w+1):
d = dw[i]
if l[(hi,i)]==1:
d-=1
m = max(m,d)
c+=m
m = 0
wi=0
for i in range(1,w+1):
if dw[i]>m:
m = dw[i]
wi=i
c2=m
m = 0
for i in range(1,h+1):
d = dh[i]
if l[(i,wi)]==1:
d-=1
m = max(m,d)
c2+=m
print(max(c,c2)) | H,W,M=map(int,input().split())
h=[0]*(H+1)
w=[0]*(W+1)
hw=[0]*M
for i in range(M):
y,x=map(int,input().split())
hw[i]=(y,x)
h[y]+=1
w[x]+=1
cnt=0
mxh=max(h)
mxw=max(w)
for hi,wi in hw:
if h[hi]==mxh and w[wi]==mxw:
cnt+=1
if h.count(mxh)*w.count(mxw)==cnt:
print(mxh+mxw-1)
else:
print(mxh+mxw) | 1 | 4,746,273,241,808 | null | 89 | 89 |
import math
N,D = map(int,input().split())
cnt=0
for i in range(N):
X,Y = map(int,input().split())
dis = math.sqrt(X*X+Y*Y)
if dis <= D:
cnt+=1
print(cnt) | # coding: utf-8
def main():
n = int(input())
a_list = list(map(int, input().split()))
a_sum = sum(a_list)
ans = 0
for i in range(n-1):
a_head = a_list[i]
a_sum -= a_head
ans += a_head * a_sum
print(ans % (10**9 + 7))
main() | 0 | null | 4,812,273,154,780 | 96 | 83 |
A,V = map(int,input().split())
B,W = map(int,input().split())
T = int(input())
D1 = abs(A-B)
D2 = (V-W)*T
if (D1 - D2) <=0:
print("YES")
else:
print("NO")
| A, V = map(int, input().split())
B, W = map(int, input().split())
T = int(input())
ans = 'YES' if abs(B - A) <= (V - W) * T else 'NO'
print(ans)
| 1 | 15,010,818,517,870 | null | 131 | 131 |
a,b,k=map(int,input().split())
if a>=k:
print(str(a-k)+' '+str(b))
elif a<k and b-(k-a)>=0:
print('0 '+str(b-(k-a)))
else:
print('0 0') | s=input()
for i in range(len(s)-1):
if s[i]!=s[i+1]:
print("Yes")
exit()
print("No")
| 0 | null | 79,258,361,556,380 | 249 | 201 |
h = int(input())
w = int(input())
n = int(input())
if h >= w and n%h != 0:
print(n//h+1)
elif h >= w and n%h == 0:
print(n//h)
elif w >= h and n%w != 0:
print(n//w+1)
else:
print(n//w) | H=int(input())
W=int(input())
N=int(input())
k=max(H, W)
cnt=0
while k*cnt<N:
cnt+=1
print(cnt) | 1 | 88,592,213,908,160 | null | 236 | 236 |
data=list(map(int,input().split()))
if data[0]/data[1]<=data[2]:
print('Yes')
else:
print('No') | while True:
x, y, z = map(int, input().split())
if x == -1 and y == -1 and z == -1:
break
if x == -1 or y == -1:
print ('F')
elif x + y >= 80:
print ('A')
elif 65 <= x+y < 80:
print ('B')
elif 50 <= x+y < 65 or (30 <= x+y < 50 and z >= 50):
print ('C')
elif 30 <= x+y < 50:
print ('D')
elif x + y < 30:
print ('F')
| 0 | null | 2,415,531,508,312 | 81 | 57 |
a = [chr(i) for i in range(65, 65+26)]
n = int(input())
s = input()
ans = ''
for i in s:
ans += (a[(a.index(i)+n)%26])
print(ans) | N = int(input())
S = input()
base = ord("A")
ans = ""
for i in range(len(S)):
p = (ord(S[i])-base)
s = (p+N) % 26
ans += chr(s+base)
print(ans) | 1 | 134,664,432,397,060 | null | 271 | 271 |
from bisect import bisect_right
n, d, a = map(int, input().split())
xh = sorted(list(map(int, input().split())) for _ in range(n))
x = [0] * (n + 1)
h = [0] * (n + 1)
s = [0] * (n + 1)
for i, (f, g) in enumerate(xh):
x[i], h[i] = f, g
x[n] = 10 ** 10 + 1
ans = 0
for i in range(n):
if i > 0:
s[i] += s[i - 1]
h[i] -= s[i]
if h[i] > 0:
num = 0 - - h[i] // a
ans += num
s[i] += num * a
j = bisect_right(x, x[i] + d * 2)
s[j] -= num * a
print(ans)
| s = list(range(1,14))
h = list(range(1,14))
c = list(range(1,14))
d = list(range(1,14))
n = int(input())
while n > 0:
x = input().split(' ')
x[1] = int(x[1])
if x[0] == 'S':
s[x[1]-1] = 0
elif x[0] == 'H':
h[x[1]-1] = 0
elif x[0] == 'C':
c[x[1]-1] = 0
elif x[0] == 'D':
d[x[1]-1] = 0
n -= 1
for t in s:
if t != 0: print("S %d" % t)
for t in h:
if t != 0: print("H %d" % t)
for t in c:
if t != 0: print("C %d" % t)
for t in d:
if t != 0: print("D %d" % t) | 0 | null | 41,359,347,053,180 | 230 | 54 |
N = int(input())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7
ans = 0
s = sum(A)
for a in A:
s -= a
ans += s * a
ans %= MOD
print(ans) | from random import randint, random, seed
from math import exp
import sys
input = sys.stdin.buffer.readline
INF = 9223372036854775808
T0 = 1480.877007252879
T1 = 2.43125135
def calc_score(D, C, S, T):
"""
開催日程Tを受け取ってそこまでのスコアを返す
コンテストi 0-indexed
d 0-indexed
"""
score = 0
last = [0]*26 # コンテストiを前回開催した日
for d, t in enumerate(T):
last[t] = d + 1
for i in range(26):
score -= (d + 1 - last[i]) * C[i]
score += S[d][t]
return score
def update_score(D, C, S, T, score, ct, ci):
"""
ct日目のコンテストをコンテストciに変更する
スコアを差分更新する
ct: change t 変更日 0-indexed
ci: change i 変更コンテスト 0-indexed
"""
new_score = score
last = [0]*26 # コンテストiを前回開催した日
prei = T[ct] # 変更前に開催する予定だったコンテストi
for d, t in enumerate(T, start=1):
last[t] = d
new_score += (d - last[prei])*C[prei]
new_score += (d - last[ci])*C[ci]
last = [0]*26
for d, t in enumerate(T, start=1):
if d-1 == ct:
last[ci] = d
else:
last[t] = d
new_score -= (d - last[prei])*C[prei]
new_score -= (d - last[ci])*C[ci]
new_score -= S[ct][prei]
new_score += S[ct][ci]
return new_score
def evaluate(D, C, S, T, k):
"""
d日目終了時点での満足度を計算し,
d + k日目終了時点での満足度の減少も考慮する
"""
score = 0
last = [0]*26
for d, t in enumerate(T):
last[t] = d + 1
for i in range(26):
score -= (d + 1 - last[i]) * C[i]
score += S[d][t]
for d in range(len(T), min(len(T) + k, D)):
for i in range(26):
score -= (d + 1 - last[i]) * C[i]
return score
def greedy(D, C, S):
Ts = []
for k in range(5, 13):
T = [] # 0-indexed
max_score = -INF
for d in range(D):
# d+k日目終了時点で満足度が一番高くなるようなコンテストiを開催する
max_score = -INF
best_i = 0
for i in range(26):
T.append(i)
score = evaluate(D, C, S, T, k)
if max_score < score:
max_score = score
best_i = i
T.pop()
T.append(best_i)
Ts.append((max_score, T))
return max(Ts, key=lambda pair: pair[0])
def local_search(D, C, S, score, T):
# sTime = time()
# TL = 1.8
Temp = T0
# cnt = 0
t = 0
best_score = score
best_T = T.copy()
for cnt in range(70000):
if cnt % 100 == 0:
t = cnt / (140000 - 1)
Temp = pow(T0, 1-t) * pow(T1, t)
sel = randint(1, 100)
lim = random()
if sel != 1:
# ct 日目のコンテストをciに変更
ct = randint(0, D-1)
ci = randint(0, 25)
new_score = update_score(D, C, S, T, score, ct, ci)
if score < new_score or \
(lim < exp((new_score - score)/Temp)):
T[ct] = ci
score = new_score
else:
# ct1 日目と ct2 日目のコンテストをswap
ct1 = randint(0, D-1)
ct2 = randint(0, D-1)
ci1 = T[ct1]
ci2 = T[ct2]
new_score = update_score(D, C, S, T, score, ct1, ci2)
new_score = update_score(D, C, S, T, new_score, ct2, ci1)
if score < new_score or \
(lim < exp((new_score - score)/Temp)):
score = new_score
T[ct1] = ci2
T[ct2] = ci1
if best_score < score:
best_score = score
best_T = T.copy()
# cnt += 1
return best_T
if __name__ == '__main__':
seed(20)
D = int(input())
C = [int(i) for i in input().split()]
S = [[int(i) for i in input().split()] for j in range(D)]
init_score, T = greedy(D, C, S)
T = local_search(D, C, S, init_score, T)
for t in T:
print(t+1)
| 0 | null | 6,688,163,306,670 | 83 | 113 |
N, K = list(map(int, input().split()))
C = 10**9+7
ans = 0
A = sum(list(range(N, N-K, -1)))
B = sum(list(range(K)))
for i in range(K, N+2):
# print(A,B)
ans += A-B+1
A += N-i
B += i
ans %= C
print(ans) | def main():
import bisect
n = int(input())
s = input()
r,g,b = 0,0,0
for i in range(n):
if s[i]=='R':
r+=1
elif s[i]=='G':
g+=1
else:
b+=1
ans = r*g*b
for i in range(n-1):
for j in range(i+1,n):
k = 2*(j+1)-(i+1)-1
if k>n-1:
continue
if s[i]!=s[j] and s[i] != s[k] and s[j]!=s[k]:
ans -= 1
print(ans)
if __name__ == "__main__":
main()
| 0 | null | 34,621,612,504,998 | 170 | 175 |
# coding:utf-8
def insertionSort(A, N):
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 A:
print i,
else:
print
N = input()
A = map(int, raw_input().split())
for i in A:
print i,
else:
print
insertionSort(A,N) | # This is a sample Python script.
# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press ⌘F8 to toggle the breakpoint.
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
n = int(input())
arr = list(map(int, input().split()))
arr = sorted(arr) # 配列を昇順ソートしておく(配列に0がある場合0が先頭に来るので、場合分けの必要がなくなる)
ans = 1
for val in arr:
ans *= val # Pythonは多倍長整数をサポートしているので単に積を求めれば十分
if ans > 10 ** 18:
print(-1)
break
else:
print(ans)
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
| 0 | null | 8,133,582,500,562 | 10 | 134 |
wordcount = ""
while True:
try:
words = input()
wordcount = wordcount + words.lower()
except:
break
for i in [chr(i) for i in range(97,97+26)]:
print('{0} : {1}'.format(i,wordcount.count(i))) | n = int( raw_input( ) )
t = [ int( val ) for val in raw_input( ).rstrip( ).split( " " ) ]
q = int( raw_input( ) )
s = [ int( val ) for val in raw_input( ).split( " " ) ]
cnt = 0
for si in s:
for ti in t:
if si == ti:
cnt += 1
break
print( cnt ) | 0 | null | 862,865,187,380 | 63 | 22 |
n = int(input())
s_l = [ input() for _ in range(n) ]
d = {}
for s in s_l:
try:
d[s] += 1
except:
d[s] = 1
max_c = max([ v for _,v in d.items() ])
ans = [ i for i, v in d.items() if v == max_c ]
for i in sorted(ans):
print(i) | import os, sys, re, math
N = int(input())
S = []
for i in range(N):
S.append(input())
d = {}
for s in S:
if not s in d:
d[s] = 0
d[s] += 1
d = sorted(d.items(), key=lambda x:x[1], reverse=True)
names = [v[0] for v in d if v[1] == d[0][1]]
for name in sorted(names):
print(name)
| 1 | 69,880,429,237,960 | null | 218 | 218 |
N = int(input())
mod = 10 ** 9 +7
x = (10**N)%mod
y = (2*(9**N))%mod
z = (8**N)%mod
print((x-y+z)%mod)
| # Dice I
class Dice:
def __init__(self, a1, a2, a3, a4, a5, a6):
# サイコロを縦横にたどると書いてある数字(index1は真上、index3は真下の数字)
self.v = [a5, a1, a2, a6] # 縦方向
self.h = [a4, a1, a3, a6] # 横方向
# print(self.v, self.h)
# サイコロの上面の数字を表示
def top(self):
return self.v[1]
# サイコロを北方向に倒す
def north(self):
newV = [self.v[1], self.v[2], self.v[3], self.v[0]]
self.v = newV
self.h[1] = self.v[1]
self.h[3] = self.v[3]
return self.v, self.h
# サイコロを南方向に倒す
def south(self):
newV = [self.v[3], self.v[0], self.v[1], self.v[2]]
self.v = newV
self.h[1] = self.v[1]
self.h[3] = self.v[3]
return self.v, self.h
# サイコロを東方向に倒す
def east(self):
newH = [self.h[3], self.h[0], self.h[1], self.h[2]]
self.h = newH
self.v[1] = self.h[1]
self.v[3] = self.h[3]
return self.v, self.h
# サイコロを西方向に倒す
def west(self):
newH = [self.h[1], self.h[2], self.h[3], self.h[0]]
self.h = newH
self.v[1] = self.h[1]
self.v[3] = self.h[3]
return self.v, self.h
d = input().rstrip().split()
dice1 = Dice(d[0], d[1], d[2], d[3], d[4], d[5])
command = list(input().rstrip())
# print(command)
for i, a in enumerate(command):
if a == 'N':
dice1.north()
elif a == 'S':
dice1.south()
elif a == 'E':
dice1.east()
elif a == 'W':
dice1.west()
print(dice1.top())
| 0 | null | 1,711,619,017,188 | 78 | 33 |
n = int(input())
ans = 0
if n//500>0:
ans += 1000 * (n//500)
n = n - 500 * (n//500)
if n//5>0:
ans += 5 * (n//5)
print(ans) | n=int(input())
d,m=divmod(n,500)
print(d*1000+m//5*5) | 1 | 42,870,354,822,468 | null | 185 | 185 |
import copy
N = int(input())
bscs = [(int(c[-1]), c) for c in input().split(" ")]
sscs = copy.copy(bscs)
for i in range(N):
j = N - 1
while j > i:
if bscs[j][0] < bscs[j - 1][0]:
tmp = bscs[j]
bscs[j] = bscs[j - 1]
bscs[j - 1] = tmp
j -= 1
print(" ".join([c[1] for c in bscs]))
print("Stable")
for i in range(N):
minj = i
for j in range(i, N):
if sscs[j][0] < sscs[minj][0]:
minj = j
tmp = sscs[i]
sscs[i] = sscs[minj]
sscs[minj] = tmp
print(" ".join([c[1] for c in sscs]))
print("Stable" if bscs == sscs else "Not stable") | a, b, c = map(int, raw_input().split())
print sum(1 for i in xrange(a, b + 1) if c % i == 0) | 0 | null | 293,621,100,090 | 16 | 44 |
taro = 0
hanako = 0
n = int(input())
for i in range(n):
t, h = input().split()
if t == h:
taro += 1
hanako += 1
elif t > h:
taro += 3
else:
hanako += 3
print(taro, hanako) | import sys
lines = [line.split() for line in sys.stdin]
T = H = 0
n = int(lines[0][0])
for w1,w2 in lines[1:]:
if w1 > w2:
T += 3
elif w1 < w2:
H += 3
else:
T += 1
H += 1
print (str(T) + " " + str(H)) | 1 | 2,020,239,084,498 | null | 67 | 67 |
n=int(input())
s=input()
ans=''
for i in s:
if ord(i)+n>ord('Z'):
ans+=chr(ord(i)+n-26)
else:
ans+=chr(ord(i)+n)
print(ans) | import sys
for s in sys.stdin:
a,b = sorted(map(int, s.split()))
if a == 0 and b == 0:
break
print(a,b) | 0 | null | 67,252,485,898,182 | 271 | 43 |
if __name__ == '__main__':
k = int(input())
s = list(input())
if len(s) <= k:
print(''.join(s))
else:
print(''.join(s[:k]) + '...') | K,S=[input() for i in range(2)]
if len(S)<=int(K):
print(S)
else:
print(S[0:int(K)]+"...") | 1 | 19,634,000,835,880 | null | 143 | 143 |
from collections import Counter
n = int(input())
c = list(input())
num = Counter(c)
r = num["R"]
ans = ["R"]*r + ["W"]*(n-r)
cnt = 0
for i in range(n):
if c[i] != ans[i]:
cnt += 1
print(cnt//2)
| from collections import defaultdict as dd
from collections import deque
import bisect
import heapq
def ri():
return int(input())
def rl():
return list(map(int, input().split()))
def solve():
n = ri()
C = input()
if C[0] == "R":
R = [1]
W = [0]
else:
R = [0]
W = [1]
for c in C[1:]:
if c == "R":
R.append(1 + R[-1])
W.append(W[-1])
else:
W.append(1 + W[-1])
R.append(R[-1])
best_cost = min(R[-1], W[-1])
for i in range(n):
rl = R[i]
wl = W[i]
rr = R[-1] - rl
wr = W[-1] - wl
swaps = min(wl, rr)
flips = wl + rr - 2 * swaps
best_cost = min(best_cost, flips + swaps)
print (best_cost)
mode = 's'
if mode == 'T':
t = ri()
for i in range(t):
solve()
else:
solve()
| 1 | 6,376,440,949,692 | null | 98 | 98 |
import sys
N=int(input())
A=list(map(int,input().split()))
M=10**9+7
G={k:0 for k in range(-1,max(A)+1)}
G[-1]=3
X=1
for a in A:
X*=G[a-1]-G[a]
X%=M
G[a]+=1
print(X)
| # A Odds of Oddness
n = int(input())
c = []
for i in range(1,n+1):
if i % 2:
c.append(i)
x = len(c)/n
print(x)
| 0 | null | 153,271,919,668,322 | 268 | 297 |
import math
n = int(input())
x = list(map(float, input().split()))
y = list(map(float, input().split()))
for p in [1.0, 2.0, 3.0]:
a = 0
for i in range(n):
a += abs(x[i] - y[i]) ** p
D = a ** (1/p)
print(D)
inf_D = float("-inf")
for i in range(n):
if abs(x[i] - y[i]) > inf_D:
inf_D = abs(x[i] - y[i])
print(inf_D)
| n = int(input())
xs = list(map(int, input().split()))
ys = list(map(int, input().split()))
ds = [abs(x - y) for x, y in zip(xs, ys)]
for i in range(1, 4):
ans = 0
for d in ds:
ans += d**i
print(ans**(1/i))
print(max(ds))
| 1 | 212,052,002,642 | null | 32 | 32 |
import sys
def input(): return sys.stdin.readline().strip()
def main():
N, M = map(int, input().split())
S = input()
def isOK(index):
return S[index] == "0"
ans = []
now = N
while now:
for step in range(min(now, M), -1, -1):
if step == 0:
print(-1)
exit()
if isOK(now-step):
ans.append(step)
now -= step
break
print(*ans[::-1], sep=" ")
if __name__ == "__main__":
main() | import sys
for i in sys.stdin:
num = map(int, i.split())
print len(str(sum(num))) | 0 | null | 69,249,345,071,232 | 274 | 3 |
a = int(input())
alist = list(map(int, input().split()))
mod = 10**9+7
total = sum(alist)
sumlist = []
ans = 0
for i in range(len(alist)-1):
total -= alist[i]
ans += alist[i]*(total)
print(ans%mod) | N = int(input())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7
ans = 0
s = sum(A)
for a in A:
s -= a
ans += s * a
ans %= MOD
print(ans) | 1 | 3,787,075,742,350 | null | 83 | 83 |
"""
時間がかかる料理は、食べるとしたら最後に食べるのがベスト。
逆にいうと、その料理を食べるかどうかを検討する段階ではほかの料理は食べるかどうかの検討は終わっている状態。
なので、食べるのにかかる時間でソートしてナップサックすればよい。
"""
N,T = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N)]
dp = [0]*(T)
AB.sort()
ans = 0
for i in range(N):
a,b = AB[i]
ans = max(ans,dp[-1]+b)
for j in range(T-1,-1,-1):
if j-a >= 0:
dp[j] = max(dp[j],dp[j-a]+b)
print(ans) | N, T = map(int, input().split())
AB = sorted([list(map(int, input().split())) for _ in range(N)])
dp = [[0] * N for _ in range(T)]
for i in range(1, T):
for j in range(N-1):
if AB[j][0] <= i:
dp[i][j] = max(dp[i][j-1], dp[i-AB[j][0]][j-1] + AB[j][1])
else:
dp[i][j] = dp[i][j-1]
ans = 0
for j in range(1, N):
ans = max(ans, dp[-1][j-1] + AB[j][1])
print(ans) | 1 | 151,401,043,112,980 | null | 282 | 282 |
n = int(input())
lst = list(map(int, input().split()))
for i in range(n):
li = lst[i]
j = i-1
while j >= 0 and lst[j] > li:
lst[j:j+2] = lst[j+1], lst[j]
j -= 1
print(*lst) | def insertionSort(a,n):
for i in range(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
print(" ".join(map(str,a)))
return a
n = int(input())
a = [int(i) for i in input().split()]
insertionSort(a,n) | 1 | 6,053,990,162 | null | 10 | 10 |
N = int(input())
g = 0
for n in range(1, N + 1):
if n % 2 == 0:
g += 1
ANS = (N - g) / N
print(ANS) | n = int(input())
a = [int(i) for i in input().split()]
def trace(a):
a = map(str, a)
print(' '.join(a))
def insertion(a):
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
trace(a)
trace(a)
insertion(a)
| 0 | null | 88,647,937,760,622 | 297 | 10 |
L, R, d = map(int, input().split())
count = 0
for i in range(101):
if d * i >= L and d * i <= R:
count += 1
if d * i > R:
break
print(count) | def seekDistance(n,nodes):
max_z,min_z = searchMaxAndMinZ(n,nodes)
max_w,min_w = searchMaxAndMinW(n,nodes)
return max(max_z-min_z,max_w-min_w)
def searchMaxAndMinZ(n,nodes):
max_z = -1
min_z = 2 * 10 ** 9
for i in range(n):
curr_z = nodes[i][0] + nodes[i][1]
if(curr_z > max_z):
max_z = curr_z
if(curr_z < min_z):
min_z = curr_z
return max_z,min_z
def searchMaxAndMinW(n,nodes):
max_w = -10**9
min_w = 2 * 10 ** 9
for i in range(n):
curr_w = nodes[i][0] - nodes[i][1]
if (curr_w > max_w):
max_w = curr_w
if (curr_w < min_w):
min_w = curr_w
return max_w, min_w
n = int(input())
nodes = [list(map(int,input().split())) for i in range(n)]
print(seekDistance(n,nodes)) | 0 | null | 5,507,535,808,922 | 104 | 80 |
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
#N = int(input())
#A = list(map(int,input().split()))
#S = list(input())
#S.remove("\n")
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = [int(input()) for _ in range(N)]
#S = [input() for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
N = int(input())
if N == 1:
print("a")
else:
S = list("abcdefghij")
Q = deque([("a",1)]) #文字列、種類
while len(Q) != 0:
s,kind = Q.popleft()
for i in range(kind+1):
news = s+S[i]
if len(news) == N+1:
break
if len(news) == N:
print(news)
if i == kind:
Q.append((news,kind+1))
else:
Q.append((news,kind)) | n,k=map(int,input().split())
*p,=map(int,input().split())
*c,=map(int,input().split())
ans=-10**20
for i in range(n):
loop=1
j=p[i]-1
acm=[c[j]]
while j!=i:
j=p[j]-1
acm.append(acm[-1]+c[j])
loop+=1
if acm[-1]>0:
if k<=loop:
cur=max(acm[:k])
else:
cur=(k//loop)*acm[-1]
if k%loop>0:
cur=max(cur,cur+max(acm[:k%loop]))
else:
cur-=acm[-1]
cur+=max(acm)
else:
cur=max(acm[:min(k,loop)])
ans=max(cur,ans)
print(ans) | 0 | null | 29,057,164,029,722 | 198 | 93 |
n = int(input())
A = list(map(int,input().split()))
def factorize(n):
fct = [] # prime factor
b, e = 2, 0 # base, exponent
while b * b <= n:
while n % b == 0:
n = n // b
e = e + 1
if e > 0:
fct.append((b, e))
b, e = b + 1, 0
if n > 1:
fct.append((n, 1))
return fct
def gcd(a, b):
while b:
a, b = b, a % b
return a
pc = True
fac = [0]*(10**6+1)
for a in A:
lst = factorize(a)
for p in lst:
if not fac[p[0]]:
fac[p[0]] = 1
else:
pc = False
break
if not pc:
break
if pc:
print('pairwise coprime')
else:
g = A[0]
for i in range(1,n):
g = gcd(g, A[i])
if g == 1:
print('setwise coprime')
else:
print('not coprime') | from functools import reduce
from math import gcd
N=int(input())
A=list(map(int, input().split()))
c=max(A)+1
C=[0]*c
f=1
for i in A:
C[i]+=1
for i in range(2,c):
cnt=0
for j in range(i,c,i):
cnt+=C[j]
if cnt>1:
f=0
if f==1:
print('pairwise coprime')
elif reduce(gcd, A) == 1:
print('setwise coprime')
else:
print('not coprime') | 1 | 4,162,450,238,140 | null | 85 | 85 |
a = input()
b = input()
print(b.count('ABC'))
| _ = input()
s = input()
x = s.count('ABC')
print(x) | 1 | 99,598,953,879,908 | null | 245 | 245 |
def main():
s = input()
n = len(s) + 1
ans = [-1] * (n)
def right(i, ans):
j = 1
while True:
ans[i + j] = max(ans[i + j], j)
if i + j == n - 1:
break
if s[i + j] == '<':
j += 1
else:
break
return ans
def left(i, ans):
j = 1
while True:
ans[i - j] = max(ans[i - j], j)
if i - j == 0:
break
if s[i - j - 1] == '>':
j += 1
else:
break
return ans
for i in range(n):
if i == 0:
if s[i] == '<':
ans[0] = 0
ans = right(i, ans)
else:
pass
elif i == n - 1:
if s[i - 1] == '>':
ans[n - 1] = 0
ans = left(i, ans)
else:
pass
else:
if s[i] == '<' and s[i - 1] == '>':
ans[i] = 0
ans = right(i, ans)
ans = left(i, ans)
else:
pass
print(sum(ans))
if __name__ == '__main__':
main()
| s=input()
s+='?'
x=[]
ch=1
if s[0]=='>':
x.append(0)
for i in range(1,len(s)):
if s[i]==s[i-1]:
ch+=1
else:
x.append(ch)
ch=1
x.append(0)
a=0
for i in range(len(x)-1):
if i%2==0 and x[i]<=x[i+1]:
a+=x[i+1]
for j in range(x[i]):
a+=j
for j in range(x[i+1]):
a+=j
elif i%2==0 and x[i]>x[i+1]:
a+=x[i]
for j in range(x[i]):
a+=j
for j in range(x[i+1]):
a+=j
if (len(x)-1)%2==1:
for j in range(x[len(x)-1]):
a+=j
print(a)
| 1 | 156,119,805,261,550 | null | 285 | 285 |
# author: kagemeka
# created: 2019-11-09 21:20:16(JST)
### modules
## from standard library
import sys
# import collections
# import math
# import string
# import bisect
# import re
# import itertools
# import statistics
# import functools
# import operator
## from external libraries
# import scipy.special
# import scipy.misc
# import numpy as np
def main():
n = int(sys.stdin.readline().rstrip())
if n % 2 == 0:
ans = n // 2 - 1
else:
ans = n // 2
print(ans)
if __name__ == "__main__":
# execute only if run as a script
main()
| w = raw_input()
words = []
while True:
line = raw_input()
if line == "END_OF_TEXT":
break
else:
line_list = line.split(" ")
words += line_list
#print words
cnt = 0
for word in words:
if w.lower() == word.lower():
cnt += 1
print cnt | 0 | null | 77,921,723,580,260 | 283 | 65 |
import sys
from operator import or_
input=sys.stdin.readline
class SegTree():
def __init__(self, N, e=float("inf"), operator_func=min):
self.e = e
self.size = N
self.node = [self.e] * (2*N)
self.op = operator_func
def set_list(self, l):
for i in range(self.size):
self.node[i+self.size-1] = l[i]
for i in range(self.size-1)[::-1]:
self.node[i] = self.op(self.node[2*i+1], self.node[2*i+2])
def update(self, k, x):
k += self.size - 1
self.node[k] = x
while k >= 0:
k = (k - 1) // 2
self.node[k] = self.op(self.node[2*k+1], self.node[2*k+2])
def get(self, l, r):
x = self.e
l += self.size; r += self.size
a, b = [], []
while l<r:
if l&1:
a += [l-1]; l += 1
if r&1:
r -= 1; b += [r-1]
l >>= 1; r >>= 1
for i in a+(b[::-1]):
x = self.op(x, self.node[i])
return x
def main():
N = int(input())
S = list(input())[:-1]
trees = {chr(97+i):SegTree(N, e=0, operator_func=or_) for i in range(26)}
for i, s in enumerate(S):
trees[s].update(i, 1)
Q = int(input())
for _ in range(Q):
mode, i, v = input().split()
if mode=="1":
i = int(i)-1
trees[S[i]].update(i, 0)
trees[v].update(i, 1)
S[i] = v
else:
i = int(i)-1
v = int(v)
ans = sum(trees[chr(97+j)].get(i, v) for j in range(26))
print(ans)
if __name__ == "__main__":
main() | n, k = map(int, input().split())
lr = [list(map(int, input().split())) for _ in range(k)]
mod = 998244353
dp = [0] * n
dp[0] = 1
acc = 0
for i in range(1, n):
# 例4のi=13, lr=[[5, 8], [1, 3], [10, 15]]の場合を想像すると書きやすい
for li, ri in lr:
if i - li >= 0:
acc += dp[i - li]
acc %= mod
if i - ri - 1 >= 0:
acc -= dp[i - ri - 1]
acc %= mod
dp[i] = acc
print(dp[-1]) | 0 | null | 32,449,379,199,552 | 210 | 74 |
import sys
from collections import deque
import numpy as np
import math
sys.setrecursionlimit(10**6)
def S(): return sys.stdin.readline().rstrip()
def SL(): return map(str,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())
def solve():
f = math.ceil(n/1.08)
if math.floor(f*1.08)==n:
print(f)
else:
print(':(')
return
if __name__=='__main__':
n = I()
solve() | import math
A = []
N = int(input())
count = 0
for n in range(N):
x = int(input())
if x == 2:
A.append(x)
count += 1
elif x % 2 == 0:
pass
elif x > 2:
for i in range(3, int(math.sqrt(x))+2, 2):
if x % i == 0:
break
else:
count += 1
A.append(x)
print(count) | 0 | null | 62,950,120,885,868 | 265 | 12 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
N, M, K = map(int, input().split())
result = 0 # max
a_time = list(map(int, input().split()))
b_time = list(map(int, input().split()))
sum = 0
a_num = 0
for i in range(N):
sum += a_time[i]
a_num += 1
if sum > K:
sum -= a_time[i]
a_num -= 1
break
i = a_num
j = 0
while i >= 0:
while sum < K and j < M:
sum += b_time[j]
j += 1
if sum > K:
j -= 1
sum -= b_time[j]
if result < i + j:
result = i + j
i -= 1
sum -= a_time[i]
print(result)
| N,*A = map(int, open(0).read().split())
if len(set(A)) == len(A):
print('YES')
else:
print('NO') | 0 | null | 42,384,985,043,302 | 117 | 222 |
h, w = map(int, input().split())
ans = (h * w + 1) // 2
if h == 1 or w ==1:
print('1')
else:
print(ans)
| x, y = map(int, input().split())
if x == 1 or y == 1:
print(1)
elif (x * y) % 2 == 0:
print((x * y) // 2)
else:
print((x * y + 1) // 2) | 1 | 51,078,977,457,130 | null | 196 | 196 |
a,b,c = [int(i) for i in input().split()]
res = 0
for num in range(a,b+1):
if c%num == 0:
res += 1
print(res) | a,b,k=map(int,input().split())
if k<=a:
print(a-k,b)
elif k>a and k<=a+b:
print(0,a+b-k)
else:
print(0,0) | 0 | null | 52,335,025,844,512 | 44 | 249 |
A,B,M=map(int,input().split())
*a,=map(int,input().split())
*b,=map(int,input().split())
xyc = [tuple(map(int,input().split())) for _ in range(M)]
ans = min(a)+min(b)
for i in range(M):
x,y,c=xyc[i]
ans = min(ans,a[x-1]+b[y-1]-c)
print(ans) | 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) | 1 | 53,865,177,454,550 | null | 200 | 200 |
n = int(input())
a = [0] * n
b = [0] * n
for i in range(n):
a[i], b[i] = [int(j) for j in input().split()]
a.sort()
b.sort()
if n % 2 == 1:
print(b[n//2] - a[n//2] + 1)
else:
print(b[n//2] + b[n//2-1] - a[n//2] - a[n//2-1] + 1) | class Dice:
D = {'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)}
def __init__(self, tp, fwd, rs, ls, bk, bm):
self.nbrs = [tp, fwd, rs, ls, bk, bm]
def rll(self, drctn):
return [self.nbrs[i] for i in self.D[drctn]]
A = input().split()
for i in input():
dice = Dice(A[0], A[1], A[2], A[3], A[4], A[5])
A = dice.rll(i)
print(A[0])
| 0 | null | 8,723,606,413,192 | 137 | 33 |
X = int(input())
A = 0
f = 1
while f:
a = A ** 5
B = 0
while f:
b = B ** 5
if a <= X:
if a + b == X:
print(str(A)+" "+str(-B))
f = 0
elif a + b > X:
break
if a > X:
if a - b == X:
print(str(A)+" "+str(B))
f = 0
elif a - b < X:
break
B += 1
A += 1 | S,W=map(int,input().split(" "))
if S>W:
print("safe")
else:
print("unsafe") | 0 | null | 27,404,413,376,978 | 156 | 163 |
input()
S = input()
j = ""
A =""
for i in S:
if i !=j:
A += i
j = i
print(len(A)) | def gcd (x , y):
sur = x % y
while sur != 0:
x = y
y = sur
sur = x % y
return y
a,b = map(int,raw_input().split())
if a < b :
temp = a
a = b
b = temp
print gcd(a,b) | 0 | null | 85,240,565,836,986 | 293 | 11 |
def ifTriangle(a, b, c):
if (a**2) + (b**2) == c**2: print("YES")
else: print("NO")
n=int(input())
for i in range(n):
lines=list(map(int, input().split()))
lines.sort()
ifTriangle(lines[0], lines[1], lines[2]) | H,W,M=map(int,input().split())
h,w=map(list,zip(*[list(map(int,input().split())) for i in range(M)]))
from collections import Counter
hc,wc=Counter(h),Counter(w)
hx,wx=hc.most_common()[0][1],wc.most_common()[0][1]
hl,wl=set([k for k,v in hc.items() if v==hx]),set([k for k,v in wc.items() if v==wx])
x=sum([1 for i in range(M) if h[i] in hl and w[i] in wl])
print(hx+wx if len(hl)*len(wl)-x else hx+wx-1) | 0 | null | 2,362,312,599,230 | 4 | 89 |
X, Y, Z = list(map(str, input().split()))
print(Z + " " + X + " " + Y)
| X,N = map(int,input().split())
p = list(map(int,input().split()))
diff = 1
if(X not in p):
print(X)
exit()
while(diff < 200):
if(X-diff not in p):
print(X-diff)
exit()
elif(X+diff not in p):
print(X+diff)
exit()
diff += 1 | 0 | null | 26,212,615,555,230 | 178 | 128 |
N=int(input())
if N % 2 or N < 10:
print(0)
exit()
ans = 0
i = 1
while 2*5**i<=N:
ans += N // (2*5**i)
i += 1
print(ans) | K, X = map(int, input().split())
K *= 500
if K >= X:
print("Yes")
else:
print("No")
| 0 | null | 107,250,104,221,822 | 258 | 244 |
S = input()
if(len(S) % 2 != 0):
print("No")
exit(0)
for i in range(0, len(S), 2):
if S[i:i+2] != "hi":
print("No")
exit(0)
print("Yes")
| s = "hi"
S = input()
flag = False
for i in range(5):
if S==s:
flag=True
break
s = s+"hi"
if flag:
print("Yes")
else:
print("No") | 1 | 53,376,098,091,040 | null | 199 | 199 |
n = input()
d = [99999 for i in range(n)]
G = [0 for i in range(n)]
v = [[0 for i in range(n)] for j in range(n)]
def bfs(u):
for i in range(n):
if v[u][i] == 1:
if d[i] > d[u] + 1:
d[i] = d[u] + 1
bfs(i)
else:
continue
def _():
d[0] = 0
for i in range(n):
bfs(i)
for i in range(n):
if d[i] == 99999:
d[i] = -1
for i in range(n):
print '{0} {1}'.format(i+1, d[i])
for i in range(n):
G = map(int, raw_input().split())
for j in range(G[1]):
v[G[0]-1][G[2+j]-1] = 1
_() | (h,n),*c=[[*map(int,i.split())]for i in open(0)]
d=[0]*20002
for i in range(h):d[i]=min(d[i-a]+b for a,b in c)
print(d[h-1]) | 0 | null | 40,599,616,822,628 | 9 | 229 |
N = int(input())
num = set()
for n in range(2, 10**7):
if N % n == 0:
num.add(tuple(sorted([n, N//n])))
if N == n:
break
ans = (10**12, 10**12)
if not num:
print(N - 1)
else:
for s in num:
if s[0]+s[1] < ans[0]+ans[1]:
ans = s
print(ans[0] + ans[1] - 2) | import math
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
for p in range(1, 4):
sum = 0
for i in range(n):
sum += math.fabs(x[i]-y[i])**p
print(sum**(1/p))
a = []
for i in range(n):
a.append(math.fabs(x[i]-y[i]))
a.sort()
print(a[n-1]) | 0 | null | 81,170,298,843,228 | 288 | 32 |
def search(start, N, X, Y):
dist = [0 for _ in range(N)]
if start <= X:
for i in range(X):
dist[i] = abs(start - i)
for i in range(Y, N):
dist[i] = (X - start) + 1 + (i - Y)
for i in range(X, (Y - X) // 2 + X + 1):
dist[i] = (i - start)
for i in range((Y - X) // 2 + X + 1, Y):
dist[i] = (X - start) + 1 + (Y - i)
elif start >= Y:
for i in range(Y, N):
dist[i] = abs(start - i)
for i in range(X):
dist[i] = (start - Y) + 1 + (X - i)
for i in range(X, (Y - X) // 2 + X):
dist[i] = (start - Y) + 1 + (i - X)
for i in range((Y - X) // 2 + X, Y):
dist[i] = (start - i)
else:
toX = min(start - X, Y - start + 1)
toY = min(Y - start, start - X + 1)
dist[start] = 0
for i in range(X):
dist[i] = (X - i) + toX
for i in range(Y, N):
dist[i] = toY + (i - Y)
for i in range(X, start):
dist[i] = min(start - i, Y - start + 1 + i - X)
for i in range(start + 1, Y):
dist[i] = min(i - start, start - X + 1 + Y - i)
return dist
def main():
N, X, Y = [int(n) for n in input().split(" ")]
#N, X, Y = 10, 3, 8
X = X - 1
Y = Y - 1
lenD = []
for i in range(N):
d = search(i, N, X, Y)
lenD.append(d)
kcounter = [0 for _ in range(N - 1)]
for i in range(N):
for j in range(i + 1, N):
k = lenD[i][j]
kcounter[k - 1] += 1
for k in kcounter:
print(k)
main() | from collections import deque
n, x, y = map(int, input().split())
g = [[] for _ in range(n)]
for i in range(n - 1):
g[i].append(i + 1)
g[i + 1].append(i)
g[x - 1].append(y - 1)
g[y - 1].append(x - 1)
def bfs(g, n_node, start_node):
dist = [-1] * n_node
dist[start_node] = 0
que = deque([start_node])
while que:
node = que.popleft()
for j in g[node]:
if dist[j] != -1:
continue
dist[j] = dist[node] + 1
que.append(j)
return dist
ans = [0] * n
for i in range(n):
dist = bfs(g, n, i)
for d in dist:
ans[d] += 1
for a in ans[1:]:
print(a//2) | 1 | 44,087,749,555,366 | null | 187 | 187 |
INF = 10**18
N, K, C = map(int, input().split())
S = input()
lt = [-INF for _ in range(K + 1)]
rt = [-INF for _ in range(K + 1)]
j = -1
for i in range(1, K + 1):
while True:
j += 1
if S[j] == 'o' and j - lt[i - 1] > C:
lt[i] = j
break
j = -1
for i in range(1, K + 1):
while True:
j += 1
if S[-1 - j] == 'o' and j - rt[i - 1] > C:
rt[i] = j
break
lt = lt[1:]
rt = rt[1:]
rt = [N - x - 1 for x in rt]
rt = rt[::-1]
res = []
for i in range(K):
if lt[i] == rt[i]:
res.append(lt[i] + 1)
print('\n'.join(map(str, res)))
| def getN():
return int(input())
def getNM():
return map(int, input().split())
def getList():
return list(map(int, input().split()))
def getArray(intn):
return [int(input()) for i in range(intn)]
def input():
return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
ns = []
while len(ns) < rantime:
n = random.randint(ran1, ran2)
if not n in ns:
ns.append(n)
return sorted(ns)
def rand_query(ran1, ran2, rantime):
r_query = []
while len(r_query) < rantime:
n_q = rand_ints_nodup(ran1, ran2, 2)
if not n_q in r_query:
r_query.append(n_q)
return sorted(r_query)
from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
import math
import random
import string
from copy import deepcopy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right
import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7
#############
# Main Code #
#############
"""
N日間のうちK日選んで働く
働いたらそれからC日間は働かない
Sにxがついていたら働かない
oの付いてる日のみ働く
全ての通りを求めてみよう
N, K, C = 11, 3, 2
S = ooxxxoxxxoo の時
働けるのは[1, 2, 6, 10, 11]日目
ここから3つ以上間が空く条件で3つ選ぶと条件を満たす
全ての通りでi日目に働く必要がある
A = [1, 2, 5, 7, 11, 13, 16]
M = 4
C = 3
for bit in range(1 << len(A)):
cnt = []
for i in range(len(A)):
if bit & (1 << i):
cnt.append(A[i])
if len(cnt) == 4:
for i in range(1, len(cnt)):
if cnt[i] - cnt[i - 1] <= C:
break
else:
print(cnt)
# python index.py
[1, 5, 11, 16]
[1, 7, 11, 16]
[2, 7, 11, 16]
A = [1, 2, 5, 10, 24]
M = 3
C = 2
[1, 5, 10]
[2, 5, 10]
[1, 5, 24]
[2, 5, 24]
[1, 10, 24]
[2, 10, 24]
[5, 10, 24]
[1, 5, 10, 24] M + 1回以上ジャンプできる場合には答えが[]になる
A[0]から頑張ってもM回しかジャンプできない
間がC + 1以上離れたM個の集合はどのように求める?
必ず働く日の特性は?
動かすと?
場所は固定?
"""
# N, K, C = getNM()
# S = getN()
N, K, C = getNM()
S = input()
# i回目に選べる要素の上限と下限を比べる
place = []
for i in range(N):
if S[i] == 'o':
place.append(i)
# 下限 出来るだけ前の仕事を選べるように
fore = [place[0]]
for i in place[1:]:
if i - fore[-1] > C:
fore.append(i)
# 上限 出来るだけ後ろの仕事を選ぶように
back = deque([])
back.append(place[-1])
for i in place[::-1]:
if back[0] - i > C:
back.appendleft(i)
back = list(back)
if len(fore) != K:
print()
exit()
ans = []
for a, b in zip(fore, back):
if a == b:
ans.append(a + 1)
for i in ans:
print(i)
"""
fore = [0] * N
if S[0] == 'o':
fore[0] = 1
for i in range(1, N):
fore[i] = fore[i - 1]
if S[i] == 'o' and i - C - 1 >= 0:
fore[i] = max(fore[i], fore[i - C - 1] + 1)
back = [float('inf')] * N
ma = max(fore)
if S[N - 1] == 'o':
back[N - 1] = ma
for i in range(N - 2, -1, -1):
back[i] = min(ma, back[i + 1])
if S[i] == 'o' and i + C + 1 < N:
back[i] = min(back[i], back[i + C + 1] - 1)
print(back)
""" | 1 | 40,460,811,603,680 | null | 182 | 182 |
l=list(map(int,input().split()))
s=sum(l)
if(s>=22):
print('bust')
else:
print('win')
| #!/usr/bin/env python3
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def main():
As = map(int, readline().split())
if sum(As) >= 22:
print('bust')
else:
print('win')
if __name__ == '__main__':
main()
| 1 | 118,689,028,692,172 | null | 260 | 260 |
n = int(raw_input())
a = 1
b = 1
for i in range(n-1):
tmp = a
a = b
b += tmp
print b | def fib(n):
if n==0 or n==1:
return 1
x=[1,1]
for i in range(2,n+1):
x.append(x[i-1]+x[i-2])
return x[-1]
print(fib(int(input())))
| 1 | 1,837,133,440 | null | 7 | 7 |
A,B,C,D = map(int,input().split())
if -A//D<=-C//B:
print("Yes")
else:
print("No") | import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
L,R,d= map(int, readline().split())
print(R//d-(L-1)//d) | 0 | null | 18,678,464,588,200 | 164 | 104 |
All = [[[0 for x in range(10)] for y in range(3)] for z in range(4)]
N = int(input())
i = 0
while i < N:
b, f, r, v = map(int,input().split())
All[b-1][f-1][r-1] += v
i += 1
for x in range(4):
for y in range(3):
for z in range(10):
print(" {}".format(All[x][y][z]), end = "")
if z == 9:
print("")
if x != 3 and y == 2:
for j in range(20):
print("#", end = "")
print("") | Residents = [[[0 for i in range(10)] for j in range(3)] for k in range(4)]
n = int(input())
for l in range(n):
b, f, r, v = list(map(int, input().split()))
Residents[b-1][f-1][r-1] += v
for k in range(4):
for j in range(3):
print(' '+' '.join(map(str, Residents[k][j])))
if k == 3:
break
print('####################') | 1 | 1,111,838,457,730 | null | 55 | 55 |
# -*- coding: utf-8 -*-
import sys
def main():
N,K = list(map(int, sys.stdin.readline().split()))
A_list = list(map(int, sys.stdin.readline().split()))
for i in range(K, len(A_list)):
if A_list[i] > A_list[i-K]:
print("Yes")
else:
print("No")
if __name__ == "__main__":
main()
| import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")
n,r = map(int, input().split())
if n >= 10:
ans = r
else:
ans = r + 100*(10-n)
print(ans) | 0 | null | 35,094,949,443,116 | 102 | 211 |
x = int(input())
a = 0
while True:
b = a
while a**5 - b**5 <= x:
if a**5 - b**5 == x:
print(a, b)
exit()
b -= 1
a += 1
| def main():
x = int(input())
a = 1
while True:
c = a**5
for b in range(-a,a+1):
d = c - b**5
if d == x:
print(a,b)
return
if d == -x:
print(b,a)
return
a+=1
if __name__ == "__main__":
main() | 1 | 25,573,340,095,040 | null | 156 | 156 |
N = int(input())
s = 1
t = 0
while N > 26**s + t:
t += 26**s
s += 1
N -= t
ans = []
for i in range(s):
c = 0
while N > 26**(s-i-1):
N -= 26**(s-i-1)
c += 1
ans.append(chr(97+c))
print("".join(ans)) | import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np # cumsum
# from bisect import bisect_left, bisect_right
def solve():
K = II()
A, B = MI()
for i in range(A, B+1):
if i % K == 0:
print('OK')
return
print('NG')
if __name__ == '__main__':
solve()
| 0 | null | 19,260,422,803,050 | 121 | 158 |
N = int(input())
A = list(map(int, input().split()))
MAX = 0
A_map = {}
for i in range(N):
num = A[i]
A_map[num] = A_map.get(num,0) + 1
MAX = max(MAX, A_map[num])
if MAX>=2:
print('NO')
else:
print('YES') | n=int(input())
a=list(map(int,input().split()))
m=len(a)
a=set(a)
n=len(a)
if m==n:
print("YES")
else:
print("NO") | 1 | 73,921,974,204,522 | null | 222 | 222 |
N = int(raw_input())
count = 0
A_str = raw_input().split()
A = map(int, A_str)
def merge(left, right):
global count
sorted_list = []
l_index = 0
r_index = 0
while l_index < len(left) and r_index < len(right):
count += 1
if left[l_index] <= right[r_index]:
sorted_list.append(left[l_index])
l_index += 1
else:
sorted_list.append(right[r_index])
r_index += 1
if len(left) != l_index:
sorted_list.extend(left[l_index:])
count += len(left[l_index:])
if len(right) != r_index:
sorted_list.extend(right[r_index:])
count += len(right[r_index:])
return sorted_list
def mergeSort(A):
if len(A) <= 1:
return A
mid = len(A) // 2
left = A[:mid]
right = A[mid:]
left = mergeSort(left)
right = mergeSort(right)
return list(merge(left, right))
print ' '.join(map(str, mergeSort(A)))
print count | def func(n):
x = []
for i in range(1,n+1):
num = i
if i % 3 == 0:
x.append(i)
else:
while num:
if int(num % 10) == 3:
x.append(i)
break
num = int(num / 10)
return x
if __name__ == '__main__':
N = int(input())
rlt = func(N)
print(" ", end='')
for i in range(len(rlt)):
if rlt[i] == rlt[-1]:
print(rlt[i], end='')
else:
print(rlt[i], end=' ')
print() | 0 | null | 525,214,602,788 | 26 | 52 |
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(K, N):
if A[i]>A[i-K]:
print('Yes')
else:
print('No') | from math import floor,ceil,sqrt,factorial,log
from collections import Counter, deque
from functools import reduce
import itertools
def S(): return input()
def I(): return int(input())
def MS(): return map(str,input().split())
def MI(): return map(int,input().split())
def FLI(): return [int(i) for i in input().split()]
def LS(): return list(MS())
def LI(): return list(MI())
def LLS(): return [list(map(str, l.split() )) for l in input()]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def LLSN(n: int): return [LS() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
N,K = MI()
A = LI()
for j in range(K, N+1):
if j == K:
continue
if A[j-K-1] < A[j-1]:
print("Yes")
else:
print("No") | 1 | 7,112,863,422,720 | null | 102 | 102 |
n = int(raw_input())
i = 0
s = []
h = []
c = []
d = []
def order_list(list):
l = len(list)
for i in xrange(l):
j = i + 1
while j < l:
if list[i] > list[j]:
temp = list[i]
list[i] = list[j]
list[j] = temp
j += 1
return list
def not_enough_cards(mark, list):
list = order_list(list)
# line = "########################################"
# print line
# print mark + ":"
# print list
# print line
i = 0
for x in xrange(1, 14):
# print "x = " + str(x) + ", i = " + str(i)
if i >= len(list):
print mark + " " + str(x)
elif x != list[i]:
print mark + " " + str(x)
else:
i += 1
while i < n:
line = raw_input().split(" ")
line[1] = int(line[1])
if line[0] == "S":
s.append(line[1])
elif line[0] == "H":
h.append(line[1])
elif line[0] == "C":
c.append(line[1])
elif line[0] == "D":
d.append(line[1])
i += 1
not_enough_cards("S", s)
not_enough_cards("H", h)
not_enough_cards("C", c)
not_enough_cards("D", d) | a,b=map(int, input().split())
c=a-2*b
print(c if c>=0 else 0 ) | 0 | null | 84,145,487,696,362 | 54 | 291 |
N, Q = map(int, input().strip().split())
A = []
D = {}
for l in range(N):
n, t = input().strip().split()
A.append([n, int(t)])
T = 0
while A:
l = A.pop(0)
if l[1] > Q:
l[1] -= Q
T += Q
A.append(l)
else:
T += l[1]
print(l[0], T) | import sys
from collections import deque
sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7
def input():
return sys.stdin.readline().strip()
def bfs(N):
q = deque(["a"])
res = []
while q:
s = q.popleft()
if len(s) == N:
res.append(s)
continue
n = len(set(list(s)))
for i in range(n + 1):
c = chr(ord("a") + i)
q.append(s + c)
return res
def main():
N = int(input())
ans = bfs(N)
print(*ans, sep="\n")
if __name__ == "__main__":
main()
| 0 | null | 26,386,836,225,568 | 19 | 198 |
import itertools as it
n, A = int(input()), [*map(int, input().split())]
yes_nums = set()
for bools in it.product([True, False], repeat=n):
yes_nums.add(sum(A[i] for i in range(n) if bools[i]))
_ = input()
for m in map(int, input().split()):
print("yes" if m in yes_nums else "no")
| import sys
def factorization(n):
arr = []
temp = n
for i in range(2, int(-(-n**0.5//1))+1):
if temp%i==0:
cnt=0
while temp%i==0:
cnt+=1
temp //= i
arr.append([i, cnt])
if temp!=1:
arr.append([temp, 1])
if arr==[]:
arr.append([n, 1])
return arr
N = int(input())
if N == 1:
print(0)
sys.exit()
a = factorization(N)
ans = 0
for i in range(len(a)):
k = a[i][1]
cnt = 1
while k-cnt>=0:
k -= cnt
cnt += 1
ans += cnt-1
print(ans) | 0 | null | 8,447,798,296,080 | 25 | 136 |
T = input()
r_T = T.replace('?', 'D')
#print(r_T.count('PD'))
#print(r_T.count('D'))
print(r_T)
| S=input()
print(S.replace('?','D')) | 1 | 18,370,271,280,580 | null | 140 | 140 |
X = int(input())
a = 100
for i in range(1, 100000):
a = a * 101 // 100
if a >= X:
print(i)
exit()
| def resolve():
base = 998244353
N, K = map(int, input().split(" "))
LRs = []
for _ in range(K):
(L, R) = map(int, input().split(" "))
LRs.append((L, R))
dp = [0] * (N+1)
dp_sum = [0] * (N+1)
dp[1] = 1
dp_sum[1] = 1
for i in range(2, N+1):
for lr in LRs:
left = max(i - lr[1] - 1, 0)
right = max(i - lr[0], 0)
dp[i] += dp_sum[right] - dp_sum[left]
if dp[i] >= base:
dp[i] %= base
dp_sum[i] = dp_sum[i-1] + dp[i]
print(dp[-1])
if __name__ == "__main__":
resolve() | 0 | null | 14,832,198,451,388 | 159 | 74 |
import collections
N=int(input())
A=[x for x in input().split()]
c = collections.Counter(A)
for i in range(N):
print(c["{}".format(str(i+1))]) | N = int(input())
A = [int(x) for x in input().split()]
cnt = [0 for x in range(N)]
for i in range(len(A)) :
cnt[A[i]-1] += 1
for i in range(N) :
print(cnt[i]) | 1 | 32,367,463,331,742 | null | 169 | 169 |
# import numpy as np
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial, gcd
from bisect import bisect_left, bisect_right
sys.setrecursionlimit(10 ** 7)
enu = enumerate
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep="\n")
N = int(input())
L = [input() for _ in range(N)]
up = []
do = []
for li in L:
l = li.count("(")
r = li.count(")")
ext_neg = 0
ext_pos = 0
tl, tr = 0, 0
if 0 <= l - r:
for op in li:
if op == "(":
tl += 1
elif op == ")":
tr += 1
ext_neg = min(ext_neg, tl - tr)
ext_pos = max(ext_pos, tl - tr)
up.append((l - r, ext_neg, ext_pos))
else:
for op in li[::-1]:
if op == ")":
tl += 1
elif op == "(":
tr += 1
ext_neg = min(ext_neg, tl - tr)
ext_pos = max(ext_pos, tl - r)
do.append((r - l, ext_neg, ext_pos))
up.sort(key=lambda tup: (-tup[1], -tup[0]))
do.sort(key=lambda tup: (-tup[1], -tup[0]))
# print(up)
# print(do)
curl = 0
for upi in up:
dif, ext_neg, ext_pos = upi
if curl + ext_neg < 0:
print("No")
exit()
curl += dif
curr = 0
for doi in do:
dif, ext_neg, ext_pos = doi
if curr + ext_neg < 0:
print("No")
exit()
curr += dif
if curl == curr:
print("Yes")
else:
print("No")
| import sys
N = int(sys.stdin.readline().strip())
S = []
for _ in range(N):
s_i = sys.stdin.readline().strip()
S.append(s_i)
# left bracket - right bracketを高さとした、最小の高さと最終的な高さの座標列
# ex)
# ")": (-1, -1)
# "(": (1, 1)
# "()": (1, 0)
# ")()(": (-1, 0)
# "))))(((((: (-4, 1)
plus_seqs = []
minus_seqs = []
for s_i in S:
h = 0
min_h = float("inf")
for bracket in s_i:
if bracket == "(":
h += 1
else:
h -= 1
min_h = min(min_h, h)
if h >= 0:
plus_seqs.append((min_h, h))
else:
# minus_seqs.append((-1 * min_h, -1 * h))
minus_seqs.append((min_h - h, -1 * h))
# print(plus_seqs)
# print(minus_seqs)
hight = 0
for (min_h, h) in sorted(plus_seqs, reverse=True):
if hight + min_h < 0:
print("No")
sys.exit()
hight += h
hight2 = 0
for (min_h, h) in sorted(minus_seqs, reverse=True):
if hight2 + min_h < 0:
print("No")
sys.exit()
hight2 += h
# print(hight, hight2)
if hight == hight2:
print("Yes")
else:
print("No") | 1 | 23,704,706,234,400 | null | 152 | 152 |
honmyou=str(input())
print(honmyou[0:3]) | H,W=map(int,input().split())
S=[list(input())for _ in range(H)]
from collections import deque
def bfs(h,w,sy,sx,S):
maze=[[10**9]*(W)for _ in range(H)]
maze[sy-1][sx-1]=0
que=deque([[sy-1,sx-1]])
count=0
while que:
y,x=que.popleft()
for i,j in [(1,0),(0,1),(-1,0),(0,-1)]:
nexty,nextx=y+i,x+j
if 0<=nexty<h and 0<=nextx<w:
dist1=S[nexty][nextx]
dist2=maze[nexty][nextx]
else:
continue
if dist1!='#':
if dist2>maze[y][x]+1:
maze[nexty][nextx]=maze[y][x]+1
count=max(count,maze[nexty][nextx])
que.append([nexty,nextx])
return count
ans=0
for sy in range(H):
for sx in range(W):
if S[sy][sx]=='.':
now=bfs(H,W,sy+1,sx+1,S)
ans=max(ans,now)
print(ans) | 0 | null | 54,986,573,444,672 | 130 | 241 |
import numpy as np
import numba
from numba import njit, b1, i4, i8, f8
@njit((i8,i8,i8[:],i8[:]), cache=True)
def main(H,N,A,B):
INF = 1<<30
dp = np.full(H+1,INF,np.int64)
dp[0] = 0
for i in range(N):
for h in range(A[i],H):
dp[h] = min(dp[h],dp[h-A[i]]+B[i])
dp[H] = min(dp[H],min(dp[H-A[i]:H]+B[i]))
ans = dp[-1]
return ans
H, N = map(int, input().split())
A = np.zeros(N,np.int64)
B = np.zeros(N,np.int64)
for i in range(N):
A[i],B[i] = map(int, input().split())
print(main(H,N,A,B)) | a,b,c=map(int,input().split())
if a+b+c>21:print('bust')
else:print('win') | 0 | null | 99,993,265,392,968 | 229 | 260 |
s = list(input())
k = int(input())
n = len(s)
if s == [s[0]]*n:
print(n*k//2)
else:
ss = s*2
ans1 = 0
for i in range(1, n):
if s[i] == s[i -1]:
s[i] = ""
ans1 += 1
ans2 = 0
for i in range(1, 2*n):
if ss[i] == ss[i - 1]:
ss[i] = ""
ans2 += 1
j = ans2 - ans1
print(ans1 + j*(k - 1)) | n=int(input())
ls=[]
for i in range(n):
s=input()
ls.append(s)
setl=set(ls)
print(len(setl)) | 0 | null | 102,713,742,997,920 | 296 | 165 |
import sys
def input(): return sys.stdin.readline().strip()
def main():
N, T = map(int, input().split())
cv = [tuple(map(int, input().split())) for _ in range(N)]
dp1 = [[0]*(T+1) for _ in range(N+2)] # [0, i]
dp2 = [[0]*(T+1) for _ in range(N+2)] # [i, N]
# 貰うdp
for i in range(N):
cost, value = cv[i]
# dp1
for now in range(min(T+1, cost)):
dp1[i+1][now] = dp1[i][now]
for now in range(cost, T+1):
old = now - cost
dp1[i+1][now] = max(dp1[i][now], dp1[i][old] + value)
# dp2
cost, value = cv[(N-i)-1]
for now in range(min(T+1, cost)):
dp2[N-i][now] = dp2[N-(i-1)][now]
for now in range(cost, T+1):
old = now - cost
dp2[N-i][now] = max(dp2[N-(i-1)][now], dp2[N-(i-1)][old] + value)
ans = 0
for i in range(1, N+1):
i_value = cv[i-1][1]
i_max = max([dp1[i-1][j] + dp2[i+1][(T-1)-j] + i_value for j in range(T)])
ans = max(ans, i_max)
print(ans)
if __name__ == "__main__":
main() | N = int(input())
A = list(map(int, input().split()))
MOD = 10**9+7
num_one_for_digit = [0]*61
for i in range(N):
b = bin(A[i])[2:].zfill(61)
for d in range(61):
if b[d] == "1":
num_one_for_digit[d] += 1
ans = 0
t = 1
for d in range(60, -1, -1):
n_1 = num_one_for_digit[d]
ans += n_1 * (N - n_1) * t % MOD
ans %= MOD
t *= 2
print(ans%MOD) | 0 | null | 137,018,704,538,610 | 282 | 263 |
class Dice:
def __init__(self):
self.up = 1
self.under = 6
self.N = 5
self.W = 4
self.E = 3
self.S = 2
def roll(self, d):#d => direction
if d == "N":
tmp = self.up
self.up = self.S
self.S = self.under
self.under = self.N
self.N = tmp
elif d == "W":
tmp = self.up
self.up = self.E
self.E = self.under
self.under = self.W
self.W = tmp
elif d == "E":
tmp = self.up
self.up = self.W
self.W = self.under
self.under = self.E
self.E = tmp
elif d == "S":
tmp = self.up
self.up = self.N
self.N = self.under
self.under = self.S
self.S = tmp
def getUpward(self):
return self.up
def setRoll(self,up,under,N,W,E,S):
self.up = up
self.under = under
self.N = N
self.W = W
self.E = E
self.S = S
def rotate(self):#crockwise
tmp = self.S
self.S = self.E
self.E = self.N
self.N = self.W
self.W = tmp
def getE(self, up, S):
'''
:param up:num
:param S: num
:return: num
'''
count = 0
while True:
if self.up == up:
break
if count != 3:
self.roll("N")
count += 1
else:
self.roll("E")
while True:
if self.S == S:
break
self.rotate()
return self.E
myd = Dice()
myd.up, myd.S, myd.E, myd.W, myd.N, myd.under = map(int,input().split())
n = int(input())
for i in range(n):
up, S = map(int,input().split())
print(myd.getE(up,S)) | #!/usr/bin/env python
import random
class Dice:
def __init__(self, top, s, e, w, n, bottom):
self.top = top
self.s = s
self.e = e
self.w = w
self.n = n
self.bottom = bottom
def rotate(self, dist):
if dist == 'N':
self.top, self.s, self.n, self.bottom = self.s, self.bottom, self.top, self.n
elif dist == 'S':
self.s, self.bottom, self.top, self.n = self.top, self.s, self.n, self.bottom
elif dist == 'E':
self.top, self.e, self.w, self.bottom = self.w, self.top, self.bottom, self.e
elif dist == 'W':
self.w, self.top, self.bottom, self.e = self.top, self.e, self.w, self.bottom
if __name__ == '__main__':
dice = Dice(*map(int, raw_input().split()))
ref = dict()
while len(ref)<24:
ref[(dice.top, dice.s)] = dice.e
dice.rotate('NSEW'[random.randint(0,3)])
for _ in range(input()):
top, s = map(int, raw_input().split())
print ref[(top, s)]
| 1 | 253,014,465,760 | null | 34 | 34 |
noun = input()
end="s"
if noun[-1] == 's':end="es"
noun += end
print(noun) | #
# 179A
#
s = input()
s1 = list(s)
if s1[len(s)-1]=='s':
print(s+'es')
else:
print(s+'s') | 1 | 2,394,205,930,912 | null | 71 | 71 |
N = int(input().rstrip())
r = (N + 1) / 2
print(int(r))
| n, m = map(int, input().split())
m2 = m
A = [list(map(int,input().split())) for i in range(n)]
b=[]
while m2 > 0:
b_data = int(input())
b.append(b_data)
m2 = m2-1
for i in range(0,n):
c = 0
for j in range(0,m):
c = c + (A[i][j] * b[j])
print(c)
| 0 | null | 30,190,028,778,788 | 206 | 56 |
n, t = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(n)]
ab.sort()
m = ab[-1][0]
dp = [0] * (m + t)
for a, b in ab:
for j in range(t - 1, -1, -1):
dp[a + j] = max(dp[a + j], dp[j] + b)
print(max(dp)) | import sys
input = sys.stdin.readline
N, T = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
AB.sort()
dp0 = [0] * T # finish eating by time j
dp1 = [0] * T # order by time j
for A, B in AB:
for j in range(T):
dp1[j] = max(dp1[j], dp0[j] + B)
for j in range(A, T)[::-1]:
dp0[j] = max(dp0[j], dp0[j-A] + B)
print(dp1[-1]) | 1 | 151,540,564,131,452 | null | 282 | 282 |
N = int(input())
height = [int(i) for i in input().split()]
box = [0]
for x in range(1 ,N):
if(height[x] >= height[x-1] + box[x-1]):
box.append(0)
if(height[x] < height[x-1] + box[x-1]):
box.append(- height[x] + height[x-1] + box[x-1])
sum = 0
for t in box:
sum += t
print(sum) | n,k=map(int,input().split())
A=[0]*k
mod=10**9+7
def power(a,x): #a**xの計算
T=[a]
while 2**(len(T))<mod: #a**1,a**2.a**4,a**8,...を計算しておく
T.append(T[-1]**2%mod)
b=bin(x) #xを2進数表記にする
ans=1
for i in range(len(b)-2):
if int(b[-i-1])==1:
ans=ans*T[i]%mod
return ans
for i in range(k):
cnt=power(k//(k-i),n)
now=(k-i)*2
while now<=k:
cnt-=A[now-1]
now+=k-i
A[k-i-1]=cnt
ans=0
for i in range(k):
ans+=(i+1)*A[i]%mod
ans%=mod
print(ans) | 0 | null | 20,566,712,250,150 | 88 | 176 |
from math import sqrt
while 1:
n = int(input())
if n == 0:
break
a = list(map(int, input().split()))
av = sum(a)/len(a)
Sum = sum((x-av)**2 for x in a)
print(f'{sqrt(Sum/n):.8f}')
| import statistics as stats
while True:
n = int(input())
if n == 0:
break
data = list(map(int, input().split()))
SD = stats.pstdev(data)
print("{:.5f}".format(SD)) | 1 | 197,239,590,240 | null | 31 | 31 |
n,m=map(int,input().split())
left=m//2
right=(m+1)//2
for i in range(left):
print(i+1,2*left+1-i)
for i in range(right):
print(2*left+1+i+1,2*left+1+2*right-i)
| n,m=map(int,input().split())
if n%2==1:
for i in range(m):
print(n-i-1,i+1)
else:
cnt=1
for i in range(m):
if i%2==0:
print(n//4-i//2,n//4-i//2+cnt)
else:
print(n//2+n//4-i//2,n//2+n//4-i//2+cnt)
cnt+=1
| 1 | 28,666,247,795,460 | null | 162 | 162 |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.