code1
stringlengths 16
427k
| code2
stringlengths 16
427k
| similar
int64 0
1
| pair_id
int64 2
181,637B
⌀ | question_pair_id
float64 3.71M
180,629B
⌀ | code1_group
int64 1
299
| code2_group
int64 1
299
|
---|---|---|---|---|---|---|
def readinput():
n=int(input())
a=list(map(int,input().split()))
return n,a
def main(n,a):
aa=[]
for i in range(n):
aa.append((i+1,a[i]))
aa.sort(key=lambda x:x[1])
b=[]
for i in range(n):
b.append(aa[i][0])
return b
if __name__=='__main__':
n,a=readinput()
ans=main(n,a)
print(' '.join(map(str,ans)))
| #coding:utf-8
#1_2_D
def insertionSort(A, n, g):
global cnt
for i in range(g, n):
v = A[i]
j = i - g
while j >= 0 and A[j] > v:
A[j+g] = A[j]
j -= g
cnt += 1
A[j+g] = v
def shellSort(A, n):
G = [1]
while True:
g = 3*G[0] + 1
if g > n:
break
G.insert(0, g)
m = len(G)
for i in range(m):
insertionSort(A, n, G[i])
return [m, G]
n = int(input())
A = [int(input()) for i in range(n)]
cnt = 0
m, G = shellSort(A, n)
print(m)
print(" ".join(map(str, G)))
print(cnt)
print("\n".join(map(str, A))) | 0 | null | 90,329,816,898,312 | 299 | 17 |
N, K = map(int, input().split())
A = list(map(lambda x: int(x)-1, input().split()))
sa = [0]
for a in A:
sa.append((sa[-1] + a) % K)
d = {0: 1}
ans = 0
for i in range(1, N+1):
if i >= K:
d[sa[i-K]] -= 1
v = d.get(sa[i], 0)
ans += v
d[sa[i]] = v + 1
print(ans) | def resolve():
N = int(input())
ans = N % 1000
if ans == 0:
print(0)
else:
print(1000 - ans)
resolve() | 0 | null | 72,901,269,547,380 | 273 | 108 |
a=sorted(input().split())
print(a[0]*int(a[1])) | #coding:utf-8
#?????§?????????????????????
def draw_odd(n):
a = ""
for i in range(n/2):
a += "#."
if n % 2 != 0:
a += "#"
print a
def draw_even(n):
a = ""
for i in range(n/2):
a += ".#"
if n % 2 != 0:
a += "."
print a
while 1:
HW = raw_input().split()
H = int(HW[0])
W = int(HW[1])
if H == 0 and W == 0:
break
for i in range(H/2):
draw_odd(W)
draw_even(W)
if H % 2 != 0:
draw_odd(W)
print "" | 0 | null | 42,423,473,159,720 | 232 | 51 |
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
p=a1-b1
q=a2-b2
if p*t1+q*t2==0:print('infinity');exit()
elif p<0:p,q=-p,-q
#p>0
if p*t1+q*t2>0:print(0)
else:
a=p*t1
d=-(a+q*t2)
print(2*(a//d)+int(not(not a%d)))
| T1,T2 = map(int,input().split())
A1,A2 = map(int,input().split())
B1,B2 = map(int,input().split())
len1 = T1 * A1 + T2 * A2
len2 = T1 * B1 + T2 * B2
if len1 == len2:
print("infinity")
quit()
ans = 0
subs = 0
sub1 = T1 * (A1 - B1)
sub2 = T2 * (A2 - B2)
if sub1 > 0:
if len1 - len2 > 0:
ans = 0
else:
S = (-1 * sub1) // (sub1 + sub2)
T = (-1 * sub1) % (sub1 + sub2)
if T == 0:
ans = 2 * S
else:
ans = 2 * S + 1
else:
if len1 - len2 < 0:
ans = 0
else:
S = (-1 * sub1) // (sub1 + sub2)
T = (-1 * sub1) % (sub1 + sub2)
if T == 0:
ans = 2 * S
else:
ans = 2 * S + 1
print(ans) | 1 | 131,966,932,629,572 | null | 269 | 269 |
# ()の問題は折れ線で捉えると良い
N = int(input())
ls = [] # 増減が正のもの(0を含む)
rs = [] # 増減が負のもの
tot = 0
for i in range(N):
s = input()
h = 0
b = 0
for c in s:
if c == "(":
h += 1
else:
h -= 1
b = min(b, h)
if h >= 0:
# 折れ線に対して上っていく奴ら
ls.append([b, h])
else:
# 折れ線に対して下っていく奴ら
# 右から(hから見るので、最下点は-hする)
rs.append([b-h, -h])
tot += h
ls.sort(key = lambda x:x[0], reverse=True)
rs.sort(key = lambda x:x[0], reverse=True)
def check(s):
h = 0
for p in s:
if h+p[0] < 0:
return False
h += p[1]
return True
if check(ls) and check(rs) and tot == 0:
print("Yes")
else:
print("No") |
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def main():
mod=10**9+7
N=I()
on=0
L=[]
cn=0#後ろに置くやつ
for i in range(N):
s=input()
close=0
openn=0
for ch in s:
if ch==")":
if openn==0:
close+=1
else:
openn-=1
else:
openn+=1
if close==0:
on+=openn
elif openn==0:
cn+=close
else:
L.append((close-openn,close,-1*openn))
L.sort()
flag=1
#print(on)
for co in L:
cc=co[1]
oo=co[2]
#print(cc,oo)
on-=cc
if on<0:
flag=0
break
on-=oo
on-=cn
if on!=0:
flag=0
if flag==1:
print("Yes")
else:
print("No")
main()
| 1 | 23,751,586,687,140 | null | 152 | 152 |
import sys
from time import time
from random import randint
def func(s, x):
last = [0] * 26
score = 0
for i, v in enumerate(x, 1):
last[v] = i
c = 0
for j in range(26):
c += s[j] * (i - last[j])
score += s[i * 26 + v] - c
return score
def main():
start = time()
d, *s = map(int, sys.stdin.buffer.read().split())
x = ([*range(26)] * 15)[:d]
M = func(s, x)
while time() - start < 1.8:
y = x.copy()
if randint(0, 1):
y[randint(0, d - 1)] = randint(0, 25)
elif randint(0, 1):
i = randint(0, d - 16)
j = randint(i + 1, i + 15)
y[i], y[j] = y[j], y[i]
else:
i = randint(0, d - 15)
j = randint(i + 1, i + 7)
k = randint(j + 1, j + 7)
if randint(0, 1):
y[i], y[j], y[k] = y[j], y[k], y[i]
else:
y[i], y[j], y[k] = y[k], y[i], y[j]
t = func(s, y)
if t > M:
M = t
x = y
for t in x:
print(t + 1)
if __name__ == '__main__':
main() | import os
import sys
import numpy as np
def solve(inp):
def bitree_sum(bit, t, i):
s = 0
while i > 0:
s += bit[t, i]
i ^= i & -i
return s
def bitree_add(bit, n, t, i, x):
while i <= n:
bit[t, i] += x
i += i & -i
def bitree_lower_bound(bit, n, d, t, x):
sum_ = 0
pos = 0
for i in range(d, -1, -1):
k = pos + (1 << i)
if k <= n and sum_ + bit[t, k] < x:
sum_ += bit[t, k]
pos += 1 << i
return pos + 1
def initial_score(d, ccc, sss):
bit_n = d + 3
bit = np.zeros((26, bit_n), dtype=np.int64)
INF = 10 ** 18
for t in range(26):
bitree_add(bit, bit_n, t, bit_n - 1, INF)
ttt = np.zeros(d, dtype=np.int64)
last = np.full(26, -1, dtype=np.int64)
score = 0
for i in range(d):
best_t = 0
best_diff = -INF
costs = ccc * (i - last)
costs_sum = costs.sum()
for t in range(26):
tmp_diff = sss[i, t] - costs_sum + costs[t]
if best_diff < tmp_diff:
best_t = t
best_diff = tmp_diff
ttt[i] = best_t
last[best_t] = i
score += best_diff
bitree_add(bit, bit_n, best_t, i + 2, 1)
return bit, score, ttt
def calculate_score(d, ccc, sss, ttt):
last = np.full(26, -1, dtype=np.int64)
score = 0
for i in range(d):
t = ttt[i]
last[t] = i
score += sss[i, t] - (ccc * (i - last)).sum()
return score
def pinpoint_change(bit, bit_n, bit_d, d, ccc, sss, ttt, permissible):
cd = np.random.randint(0, d)
ct = np.random.randint(0, 26)
while ttt[cd] == ct:
ct = np.random.randint(0, 26)
diff = 0
t = ttt[cd]
k = bitree_sum(bit, t, cd + 2)
c = bitree_lower_bound(bit, bit_n, bit_d, t, k - 1) - 2
e = bitree_lower_bound(bit, bit_n, bit_d, t, k + 1) - 2
b = ccc[t]
diff -= b * (cd - c) * (e - cd)
diff -= sss[cd, t]
k = bitree_sum(bit, ct, cd + 2)
c = bitree_lower_bound(bit, bit_n, bit_d, ct, k) - 2
e = bitree_lower_bound(bit, bit_n, bit_d, ct, k + 1) - 2
b = ccc[ct]
diff += b * (cd - c) * (e - cd)
diff += sss[cd, ct]
if diff > permissible:
bitree_add(bit, bit_n, t, cd + 2, -1)
bitree_add(bit, bit_n, ct, cd + 2, 1)
ttt[cd] = ct
else:
diff = 0
return diff
def swap_change(bit, bit_n, bit_d, d, ccc, sss, ttt, permissible):
dd = np.random.randint(1, 14)
cd1 = np.random.randint(0, d - dd)
cd2 = cd1 + dd
ct1 = ttt[cd1]
ct2 = ttt[cd2]
if ct1 == ct2:
return 0
diff = 0
k = bitree_sum(bit, ct1, cd1 + 2)
c = bitree_lower_bound(bit, bit_n, bit_d, ct1, k - 1) - 2
e = bitree_lower_bound(bit, bit_n, bit_d, ct1, k + 1) - 2
diff += ccc[ct1] * (e + c - cd1 - cd2)
k = bitree_sum(bit, ct2, cd2 + 2)
c = bitree_lower_bound(bit, bit_n, bit_d, ct2, k - 1) - 2
e = bitree_lower_bound(bit, bit_n, bit_d, ct2, k + 1) - 2
diff -= ccc[ct2] * (e + c - cd1 - cd2)
diff -= sss[cd1, ct1] + sss[cd2, ct2]
diff += sss[cd1, ct2] + sss[cd2, ct1]
if diff > permissible:
bitree_add(bit, bit_n, ct1, cd1 + 2, -1)
bitree_add(bit, bit_n, ct1, cd2 + 2, 1)
bitree_add(bit, bit_n, ct2, cd1 + 2, 1)
bitree_add(bit, bit_n, ct2, cd2 + 2, -1)
ttt[cd1] = ct2
ttt[cd2] = ct1
else:
diff = 0
return diff
d = inp[0]
ccc = inp[1:27]
sss = np.zeros((d, 26), dtype=np.int64)
for r in range(d):
sss[r] = inp[27 + r * 26:27 + (r + 1) * 26]
bit, score, ttt = initial_score(d, ccc, sss)
bit_n = d + 3
bit_d = int(np.log2(bit_n))
loop = 5 * 10 ** 6
permissible_min = -2000.0
method_border = 0.8
best_score = score
best_ttt = ttt.copy()
for lp in range(loop):
permissible = (1 - lp / loop) * permissible_min
if np.random.random() < method_border:
diff = pinpoint_change(bit, bit_n, bit_d, d, ccc, sss, ttt, permissible)
else:
diff = swap_change(bit, bit_n, bit_d, d, ccc, sss, ttt, permissible)
score += diff
# print(lp, score, calculate_score(d, ccc, sss, ttt))
if score > best_score:
best_score = score
best_ttt = ttt.copy()
return best_ttt + 1
if sys.argv[-1] == 'ONLINE_JUDGE':
from numba.pycc import CC
cc = CC('my_module')
cc.export('solve', '(i8[:],)')(solve)
cc.compile()
exit()
if os.name == 'posix':
# noinspection PyUnresolvedReferences
from my_module import solve
else:
from numba import njit
solve = njit('(i8[:],)', cache=True)(solve)
print('compiled', file=sys.stderr)
inp = np.fromstring(sys.stdin.read(), dtype=np.int64, sep=' ')
ans = solve(inp)
print('\n'.join(map(str, ans)))
| 1 | 9,699,276,179,210 | null | 113 | 113 |
N = int(input())
A = list(map(int, input().split()))
A.sort()
if A[0] == 0:
print(0)
exit()
ans = 1
for i in range(0, N):
ans *= A[i]
if ans > 10 ** 18:
print(-1)
exit()
print(ans) | p = 998244353
def pow(x,m):
if m==0:
return 1
if m==1:
return x
if m%2==0:
return (pow(x,m//2)**2)%p
if m%2==1:
return (x*(pow(x,(m-1)//2)**2)%p)%p
N,M,K = map(int,input().split())
A = [1 for _ in range(N+1)]
for i in range(2,N+1):
A[i] = (A[i-1]*i)%p
B = [1 for _ in range(N+1)]
B[N] = pow(A[N],p-2)
for i in range(N-1,1,-1):
B[i] = (B[i+1]*(i+1))%p
C = [1 for _ in range(N)]
C[0] = M
for i in range(1,N):
C[i] = (C[i-1]*(M-1))%p
tot = C[N-1]
for k in range(1,K+1):
cnt = (A[N-1]*B[k]*B[N-1-k])%p
cnt = (cnt*C[N-1-k])%p
tot = (tot+cnt)%p
print(tot) | 0 | null | 19,833,456,499,968 | 134 | 151 |
def gcd(a, b):
c = max([a, b])
d = min([a, b])
if c % d == 0:
return d
else:
return gcd(d, c % d)
nums = input().split()
print(gcd(int(nums[0]), int(nums[1])))
| from collections import deque
n,m = map(int,input().split())
g = [[] for _ in range(n)]
for i in range(m):
a,b = map(lambda x:int(x)-1,input().split())
g[a].append(b)
g[b].append(a)
to = {}
dist = [-1 for _ in range(n)]
dist[0] = 0
q = deque()
q.append(0)
while (len(q) > 0):
v = q.popleft()
for nv in g[v]:
if (dist[nv] != -1):continue
dist[nv] = dist[v] + 1
q.append(nv)
to[nv] = v
print('Yes')
for i in range(1,n):
print(to[i]+1) | 0 | null | 10,256,312,334,618 | 11 | 145 |
import sys
from collections import defaultdict
input = sys.stdin.readline
mod = 998244353
def extgcd(a,b):
r = [1,0,a]
w = [0,1,b]
while w[2] != 1:
q = r[2]//w[2]
r2 = w
w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
r = r2
w = w2
return [w[0],w[1]]
def mod_inv(a,m):
x = extgcd(a,m)[0]
return ( m + x%m ) % m
def main():
n,s = map(int,input().split())
a = list(map(int, input().split()))
dp = [[0 for _ in range(s+1)] for _ in range(n+1)]
dp[0][0] = pow(2,n,mod)
MODINV2 = mod_inv(2,mod)
ans = 0
for i in range(n):
for j in range(s,-1,-1):
if j-a[i] < 0:
continue
dp[i+1][j] += dp[i][j-a[i]]*MODINV2
dp[i+1][j] %= mod
for j in range(s,-1,-1):
if j == s:
ans += dp[i+1][j]
ans %= mod
else:
dp[i+1][j] += dp[i][j]
dp[i+1][j] %= mod
print(ans)
if __name__ == "__main__":
main() | import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random,resource
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
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 pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)
def main():
n = I()
s = S()
m = {}
m[0] = 0
def f(i):
if i in m:
return m[i]
c = 0
ii = i
while ii > 0:
c += ii % 2
ii //= 2
t = i % c
m[i] = f(t) + 1
return m[i]
c = len([_ for _ in s if _ == '1'])
t = int(s, 2)
cm = (c+1) * c * (c-1)
if c > 1:
t %= cm
r = []
k = 1
for i in range(n-1,-1,-1):
if t == k:
r.append(0)
elif s[i] == '1':
r.append(f((t - k) % (c-1)) + 1)
else:
r.append(f((t + k) % (c+1)) + 1)
k *= 2
if c > 1:
k %= cm
return JA(r[::-1], "\n")
print(main())
| 0 | null | 12,832,958,213,052 | 138 | 107 |
def modinv(a,m):
b, u, v = m, 1, 0
while b:
t = a//b
a -= t*b
a,b = b,a
u -= t * v
u,v = v,u
u %= m
return u
n, k = map(int, input().split())
al = list(map(int, input().split()))
al_abs = []
al_p = []
al_m = []
zero_cnt = 0
MOD = 10**9+7
for a in al:
if a == 0:
zero_cnt+=1
elif a > 0:
al_abs.append((abs(a),1))
al_p.append(a)
else:
al_abs.append((abs(a),-1))
al_abs.sort(reverse=True)
if n == k:
ans = 1
for a in al:
ans *= a
ans %= MOD
print(ans)
exit()
if len(al_abs) < k:
print(0)
exit()
if not al_p:
if k%2 == 1 and zero_cnt > 0:
print(0)
else:
if k%2 == 1:
al_abs.sort()
ans = 1
for a,s in al_abs[:k]:
ans *= (a*(-1))
ans %= MOD
print(ans)
exit()
val = 1
m_cnt = 0
last_m = 0
last_p = 0
for i in range(k):
v,sig = al_abs[i]
val *= v
val %= MOD
if sig == -1:
m_cnt += 1
last_m = v
else:
last_p = v
if m_cnt %2 == 0:
print(val)
exit()
# minus -> plus
## search next p
next_p = 0
for i in range(k,len(al_abs)):
v,sig = al_abs[i]
if sig == 1:
next_p = v
break
## search next m
next_m = 0
for i in range(k,len(al_abs)):
v,sig = al_abs[i]
if sig == -1:
next_m = v
break
if next_p == 0 and next_m == 0:
print(0)
exit()
# last_m -> next_p
if next_p*last_p >= next_m*last_m or last_p == 0:
ans = val*modinv(last_m%MOD,MOD)*next_p
ans %= MOD
print(ans)
else:
ans = val*modinv(last_p%MOD,MOD)*next_m
ans %= MOD
print(ans) | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
c = {"hi", "hihi", "hihihi", "hihihihi", "hihihihihi"}
def main():
s = input().strip()
ans = s in c
print("Yes" if ans else "No")
if __name__ == '__main__':
main()
| 0 | null | 31,279,862,245,552 | 112 | 199 |
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]
def inside(y, x, H, W):
return 0 <= y < H and 0 <= x < W
def ceil(a, b):
return (a + b - 1) // b
def sum_of_arithmetic_progression(s, d, n):
return n * (2 * s + (n - 1) * d) // 2
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
def lcm(a, b):
g = gcd(a, b)
return a / g * b
def solve():
N = int(input())
A = list(map(int, input().split()))
t = 1
for i in range(N):
if A[i] == t:
t += 1
if t == 1:
print(-1)
else:
print(N - (t - 1))
def main():
solve()
if __name__ == '__main__':
main()
| d=int(input()) #365
c=list(map(int,input().split()))
s=[]
for i in range(d):
s.append(list(map(int,input().split())))
ll=[0 for i in range(26)]
l=[]
ans=[]
for i in range(d):
for j in range(26):
ll[j]+=1
m=-(10**10)
for j in range(26):
t=s[i][j]+ll[j]*c[j]
if m<t:
m=t
tt=j
l.append(ll[tt])
ans.append(tt)
ll[tt]=0
print("\n".join(map(lambda x:str(x+1),ans))) | 0 | null | 62,015,985,536,322 | 257 | 113 |
landform = [c for c in input()]
down = []
areas = []
for i in range(len(landform)):
if landform[i] == '\\':
down.append(i)
if landform[i] == '/':
if down:
pair = down.pop()
area = i - pair
while areas and areas[-1][0] > pair:
area += areas.pop()[1]
areas.append([pair, area])
sum_area = 0
for area in areas:
sum_area += area[1]
print(sum_area)
ans = []
ans.append(str(len(areas)))
ans += [str(area[1]) for area in areas]
print(' '.join(ans))
| def main():
line = input()
deepen_x = []
cur_x = 0
ponds = [] # [(水たまりの最初の位置, 水量)]
while len(line) > 0:
#print(cur_x, ponds, deepen_x)
tmp_char = line[0]
if tmp_char == '\\':
deepen_x.append(cur_x)
elif tmp_char == '/' and len(deepen_x) != 0:
pre_x = deepen_x.pop()
volume = cur_x - pre_x
if len(ponds) == 0:
ponds.append([pre_x, volume])
else:
if pre_x < ponds[-1][0]:
# 前の水たまりと結合する
a = list(filter(lambda x: x[0] > pre_x, ponds))
pond = 0
for item in a:
pond += item[1]
[ponds.pop() for x in range(len(a))]
ponds.append([pre_x, pond + volume])
else:
# 新しい水たまりを作成
ponds.append([pre_x, volume])
cur_x += 1
line = line[1:]
print(sum([x[1] for x in ponds]))
if len(ponds) == 0:
print('0')
else:
print("{} ".format(len(ponds)) + " ".join([str(x[1]) for x in ponds]))
return
main()
| 1 | 56,859,175,072 | null | 21 | 21 |
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
dp = [0]*(N+1)
dp[0] = 1000
for i in range(1, N+1):
dp[i] = dp[i-1]
for j in range(i):
dp[i] = max(dp[i], dp[j]//A[j]*A[i-1]+dp[j]%A[j])
print(dp[N]) | n,m=list(map(int,input().split()))
mA=[list(map(int,input().split())) for i in range(n)]
mB=[int(input()) for j in range(m)]
for ma in mA:
print(sum([a*b for a, b in zip(ma,mB)])) | 0 | null | 4,269,264,576,512 | 103 | 56 |
N,K = map(int,input().split())
A = list(map(int,input().split()))
for i in range(N-K):
if A[i]<A[i+K]:
print("Yes")
elif A[i]>=A[i+K]:
print("No") | N, K = map(int, input().split())
A = list(map(int, input().split()))
answers = ["Yes" if A[i] > A[i-K] else "No" for i in range(K, N)]
print("\n".join(answers)) | 1 | 7,161,054,059,040 | null | 102 | 102 |
def main():
N, M = map(int, input().split())
digits = [-1] * N
for _ in range(M):
s, c = map(int, input().split())
s -= 1
if ~digits[s] and digits[s] != c:
print(-1)
return
digits[s] = c
if N == 1:
print(max(0, digits[0]))
return
if digits[0] == 0:
print(-1)
return
if digits[0] == -1:
digits[0] = 1
ans = ''.join(map(str, (d if ~d else 0 for d in digits)))
print(ans)
if __name__ == '__main__':
main()
| n, m = list(map(int, input().split()))
num = [0 for i in range(n)]
flag = False
for i in range(m):
s, c = list(map(int, input().split()))
if (s == 1) and (c == 0) and (n > 1):
flag = True
elif num[s-1] == 0:
num[s-1] = c
else:
if num[s-1] != c:
flag = True
if flag:
print(-1)
else:
if (n > 1) and (num[0] == 0):
num[0] = 1
print("".join(list(map(str, num)))) | 1 | 60,646,599,569,364 | null | 208 | 208 |
n, k = map(int, input().split())
p = list(map(lambda x: (1+int(x))/2, input().split()))
s = sum(p[0:k])
ans = s
for i in range(k, n):
s += p[i] - p[i-k]
ans = max(ans, s)
print(ans) | import bisect,collections,copy,heapq,itertools,math,string
import numpy as np
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())
N,K = LI()
p = LI()
E_half = np.array(p) + 1
def main():
Pcum = np.zeros(N + 1, np.int32)
Pcum[1:] = E_half.cumsum()
# print(E_half)
# print(Pcum)
partial_sums_E_half = np.zeros(N-K+1, np.int32)
for i in range(N-K+1):
partial_sums_E_half[i] = Pcum[i+K] - Pcum[i]
# partial_sums = Pcum[K:] - Pcum[0:-K]
return (partial_sums_E_half.max())/2
print(main())
| 1 | 75,157,121,555,670 | null | 223 | 223 |
n, k = map(int, input().split())
A = list(map(lambda x: int(x) - 1, input().split()))
prev = 0
count = 0
used = {prev: count}
for _ in range(min(k, n)):
prev = A[prev]
count += 1
if prev in used:
for _ in range((k - used[prev]) % (count - used[prev])):
prev = A[prev]
break
used[prev] = count
print(prev + 1) | N, K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N):
A[i] -= 1
judge = N*[0]
pos = 0
freq = 0
while judge[pos]==0 and K > 0:
judge[pos] = 1
pos = A[pos]
K -= 1
freq+=1
tmp = pos
pos = 0
while pos!=tmp:
pos = A[pos]
freq -= 1
#print(freq)
if K==0:
print(pos+1)
else:
K %= freq
pos = tmp
for i in range(K):
pos = A[pos]
print(pos+1)
| 1 | 22,682,685,986,180 | null | 150 | 150 |
s = str(input())
t = str(input())
t_before = ''
for i in range(len(s)):
t_before += t[i]
if s == t_before:
print('Yes')
else:
print('No') | S = list(input())
T = list(input())
count = 0
for i in range(len(S)):
if S[i] != T[i]:
count += 1
break
if count == 0:
print("Yes")
else:
print("No") | 1 | 21,288,826,692,210 | null | 147 | 147 |
K = int(input())
S = input()
s = len(S)
L = []
mod = 10 ** 9 + 7
N = 2 * 10**6
fac = [1, 1]
finv = [1, 1]
inv = [0, 1]
def cmb(n, r):
return fac[n] * ( finv[r] * finv[n-r] % mod ) % mod
for i in range(2, N + 1):
fac.append( ( fac[-1] * i ) % mod )
inv.append( mod - ( inv[mod % i] * (mod // i) % mod ) )
finv.append( finv[-1] * inv[-1] % mod )
for i in range(K+1):
L.append( cmb(i+s-1, s-1) * pow(25, i, mod) % mod )
ans = []
for i, x in enumerate(L):
if i == 0:
ans.append(x)
else:
ans.append( ( ans[i-1]*26%mod + x ) % mod )
print(ans[K]) | K = int(input())
S = input()
N = len(S)
MOD = 10 ** 9 + 7
# 逆元の前計算
factorial = [1, 1]
inverse = [1, 1]
invere_base = [0, 1]
for i in range(2, K + N + 1):
factorial.append((factorial[-1] * i) % MOD)
invere_base.append((-invere_base[MOD % i] * (MOD // i)) % MOD)
inverse.append((inverse[-1] * invere_base[-1]) % MOD)
def nCr(n, r):
if (r < 0 or r > n):
return 0
r = min(r, n - r)
return factorial[n] * inverse[r] * inverse[n - r] % MOD
ans = 0
for i in range(K + 1):
ans += pow(25, i, MOD) * nCr(i + N - 1, N - 1) * pow(26, K - i, MOD) % MOD
ans %= MOD
print(ans % MOD)
| 1 | 12,831,756,737,212 | null | 124 | 124 |
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
n = I()
a = LI()
dic = collections.Counter(a)
als = 0
exl = [0 for _ in range(n+1)]
for key, value in dic.items():
if value >= 3:
choice = (value)*(value-1)//2
choice1 = (value-1)*(value-2)//2
als += choice
exl[key] = choice1 - choice
elif value == 2:
choice = (value)*(value-1)//2
als += choice
exl[key] = -choice
for ball in a:
choices = als + exl[ball]
print(choices)
main()
| N = int(input())
S = input()
current = ""
ans = ""
for s in S:
if s != current:
current = s
ans += s
print(len(ans))
# print(ans)
| 0 | null | 109,090,992,000,698 | 192 | 293 |
n,m=map(int,input().split())
if n%2==1:
x=[f"{i+1} {n-i}" for i in range(m)]
print(" ".join(x))
else:
x=[f"{i+1} {n-i}" if i<m/2 else f"{i+1} {n-i-1}" for i in range(m)]
print(" ".join(x))
| n, m = map(int, input().split())
if n % 2 == 1:
a = [i for i in range(1, m+1)]
b = [i for i in reversed(range(m+1, 2*m+1))]
ab = [(x, y) for x, y in zip(a, b)]
else:
ab = []
x, y = (n-1)//2, n//2
if x % 2 != 0:
x, y = y, x
evenl = 1
evenr = x
while evenl < evenr:
ab.append((evenl, evenr))
evenl += 1
evenr -= 1
oddl = x+1
oddr = n-1
while oddl < oddr:
ab.append((oddl, oddr))
oddl += 1
oddr -= 1
ab = ab[:m]
for a, b in ab:
print(a, b) | 1 | 28,623,547,094,052 | null | 162 | 162 |
def row():
c=0
for y in range(HEIGHT):
for x in range(WIDTH):
if A[y][x]!=-1:
break
else:
c+=1
return c
def col():
c=0
for x in range(WIDTH):
for y in range(HEIGHT):
if A[y][x]!=-1:
break
else:
c+=1
return c
def obl():
c=0
for z in range(HEIGHT):
if A[z][z]!=-1:
break
else:
c+=1
y=0
for x in range(WIDTH-1,-1,-1):
if A[y][x]!=-1:
break
y+=1
else:
c+=1
return c
def main():
for b in B:
for y in range(HEIGHT):
for x in range(WIDTH):
if A[y][x]==b:
A[y][x]=-1
cnt=row()+col()+obl()
print('Yes' if cnt else 'No')
if __name__=='__main__':
HEIGHT=3
WIDTH=3
A=[[int(a) for a in input().split()] for y in range(HEIGHT)]
N=int(input())
B=[int(input()) for n in range(N)]
main() | #!/usr/bin/env python3
import sys
YES = "Yes" # type: str
NO = "No" # type: str
def solve(A: "List[List[int]]", N: int, b: "List[int]"):
for i in range(N):
n = b[i]
for j in range(3):
for k in range(3):
if A[j][k] == n:
A[j][k] = 0
if A[0][0] == A[0][1] == A[0][2]:
print(YES)
elif A[1][0] == A[1][1] == A[1][2]:
print(YES)
elif A[2][0] == A[2][1] == A[2][2]:
print(YES)
elif A[0][0] == A[1][0] == A[2][0]:
print(YES)
elif A[0][1] == A[1][1] == A[2][1]:
print(YES)
elif A[0][2] == A[1][2] == A[2][2]:
print(YES)
elif A[0][0] == A[1][1] == A[2][2]:
print(YES)
elif A[0][2] == A[1][1] == A[2][0]:
print(YES)
else:
print(NO)
return
# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
A = [[int(next(tokens)) for _ in range(3)] for _ in range(3)] # type: "List[List[int]]"
N = int(next(tokens)) # type: int
b = [int(next(tokens)) for _ in range(N)] # type: "List[int]"
solve(A, N, b)
if __name__ == '__main__':
main()
| 1 | 59,674,385,754,560 | null | 207 | 207 |
import math
K = int(input())
rst = 0
for i in range(1, K + 1):
for j in range(1, K + 1):
tmp = math.gcd(i, j)
for k in range(1, K + 1):
rst += math.gcd(tmp, k)
print(rst)
| import sys
#input = sys.stdin.buffer.readline
N = int(input())
temp = 5
ans = 0
if N%2:
print(0)
quit()
else:
N = N//2
while temp <= N:
ans += N//temp
temp *= 5
print(ans) | 0 | null | 75,792,490,107,440 | 174 | 258 |
N = int(input())
cnt = 0
for a in range(1, N):
n = (N - 1) // a
cnt += n
print(cnt)
| def resolve():
N = int(input())
count = 0
for A in range(1, N):
count += ((N-1)//A)
print(count)
if __name__ == "__main__":
resolve() | 1 | 2,625,505,228,642 | null | 73 | 73 |
a = input()
alp = [chr(i) for i in range(ord('a'),ord('z')+1)]
if a in alp:
print('a')
else:
print('A') | a = input()
if a >= 'a':
print("a")
else:
print("A") | 1 | 11,346,334,113,630 | null | 119 | 119 |
A, V = list(map(int, input().split()))
B, W = list(map(int, input().split()))
T = int(input())
abs_BA = abs(B-A)
sub_VW = V - W
if sub_VW <= 0:
print("NO")
elif abs_BA <= T * sub_VW:
print("YES")
else:
print("NO") | rc=list(map(int,input().split()))
a=[[0 for x in range(rc[1]+1)]for x in range(rc[0]+1)]
for x in range(rc[0]):
code=list(map(int,input().split()))
result=0
for y in range(rc[1]):
a[x][y]=code[y]
result+=code[y]
a[x][rc[1]]=result
for y in range(rc[1]+1):
result=0
for x in range(rc[0]):
result+=a[x][y]
a[rc[0]][y]=result
for x in range(rc[0]+1):
string=""
for y in range(rc[1]+1):
string+=str(a[x][y])
if (y!=rc[1]):
string+=" "
print(string) | 0 | null | 8,266,317,518,186 | 131 | 59 |
def main():
n,k = map(int,input().split())
a = [int(i) for i in input().split()]
"""
ruiseki = [1]
for i in range(n):
ruiseki.append(ruiseki[-1]*a[i])
#ruiseki.pop(0)
ans = [ruiseki[k]/ruiseki[0]]
for i in range(k+1,n+1):
tmp = ruiseki[i]/ruiseki[i-k]
if ans[-1] >= tmp:
print('No')
else:
print('Yes')
ans.append(tmp)
"""
for i in range(k,n):
if a[i]>a[i-k]:
print('Yes')
else:
print('No')
main() | import sys
N, T = map(int, input().split())
S = sys.stdin.readlines()
A = []
B = []
for s in S:
a, b = map(int, s.split())
A.append(a)
B.append(b)
dp1 = [[0] * T for _ in range(N + 1)]
for i in range(N):
for j in range(T):
here = dp1[i][j]
if j < A[i]:
dp1[i + 1][j] = here
else:
dp1[i + 1][j] = max(here, dp1[i][j - A[i]] + B[i])
dp2 = [[0] * T for _ in range(N + 2)]
for i in range(N, 0, -1):
for j in range(T):
here = dp2[i + 1][j]
if j < A[i - 1]:
dp2[i][j] = here
else:
dp2[i][j] = max(here, dp2[i + 1][j - A[i - 1]] + B[i - 1])
ans = 0
for i in range(1, N + 1):
t = 0
for j in range(T):
t2 = dp1[i - 1][j] + dp2[i + 1][T - 1 - j]
t = max(t, t2)
ans = max(ans, t + B[i - 1])
print(ans)
| 0 | null | 79,135,928,618,370 | 102 | 282 |
n, a, b = map(int, input().split())
c = a + b
div = n // c
mod = n % c
if mod > a:
mod = a
ans = div * a + mod
print(ans)
| n, a, b = map(int, input().split())
red = 0 ; blue = 0
if n >= a + b :
stack = n // (a+b)
red += b * stack
blue += a * stack
n -= (a+b) * stack
if n > a :
n -= a
blue += a
else :
blue += n
if n > b :
n -= b
red += b
else :
red += n
print(blue)
| 1 | 55,628,551,621,368 | null | 202 | 202 |
N=int(input())
d={}
for i in range(1,10):
for j in range(1,10):
d[i*10+j]=0
for i in range(1,min(10,N+1)):
d[i*10+i]=1
for i in range(11,N+1):
i=str(i)
h,t=int(i[0])*10,int(i[-1])
if t!=0:
d[h+t]+=1
ans=0
for i in range(1,9):
for j in range(i+1,10):
ans+=(d[i*10+j]*d[j*10+i])*2
for i in range(1,10):
ans+=d[i*10+i]**2
print(ans) | import sys
import itertools
# import numpy as np
import time
import math
sys.setrecursionlimit(10 ** 7)
from collections import defaultdict
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(input())
c = [[0 for i in range(10)] for _ in range(10)]
def f(n):
x = 0
while n > 0:
n //= 10
x +=1
return x
for i in range(1, N + 1):
d = f(i)
front = i // (10 ** (d - 1))
back = i % 10
c[front][back] += 1
ans = 0
for i in range(1, 10):
for j in range(1, 10):
ans += c[i][j] * c[j][i]
print(ans)
| 1 | 86,062,755,850,210 | null | 234 | 234 |
x=int(input())
cnt = x//500
x-=cnt*500
print(5*(x//5) + 1000*cnt) | x = int(input())
v500 = int(x // 500)
v5 = int((x % 500 ) //5)
print(v500 * 1000 + v5 * 5) | 1 | 42,717,981,181,290 | null | 185 | 185 |
from math import gcd
from collections import Counter
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
MOD = 10**9+7
def main():
N,*ab = map(int, read().split())
ab_zero = 0
ratio = []
for a, b in zip(*[iter(ab)]*2):
if a == 0 and b == 0:
ab_zero += 1
else:
if b < 0:
a, b = -a, -b
if b == 0:
a = 1
g = gcd(a, b)
a //= g
b //= g
ratio.append((a, b))
s = Counter(ratio)
bad = 1
no_pair = 0
for k, v in s.items():
a, b = k
if a > 0:
if (-b, a) in s:
bad *= pow(2, v, MOD) + pow(2, s[(-b, a)], MOD) -1
bad %= MOD
else:
no_pair += v
elif (b, -a) not in s:
no_pair += v
bad *= pow(2, no_pair, MOD)
bad %= MOD
ans = (bad + ab_zero -1) % MOD
print(ans)
if __name__ == "__main__":
main()
| # coding: utf-8
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
S = sr()
bl = False
if S[2] == S[3] and S[4] == S[5]:
bl = True
print('Yes' if bl else 'No')
| 0 | null | 31,536,138,575,660 | 146 | 184 |
N, M = list(map(int, input().split()))
import math
ans = []
if M == 1:
ans.append((1, 2))
else:
m0 = (1, M+1)
m1 = (M+2, 2*M+1)
for _ in range(math.ceil(M/2)):
if m0[0] < m0[1]:
ans.append(m0)
if m1[0] < m1[1]:
ans.append(m1)
m0 = (m0[0]+1, m0[1]-1)
m1 = (m1[0]+1, m1[1]-1)
for a in ans:
print(a[0], a[1])
| from sys import stdin
input = lambda: stdin.readline().rstrip()
n, r = map(int, input().split())
print(r + 100 * max(0, (10 - n)))
| 0 | null | 45,986,177,931,200 | 162 | 211 |
a, b, c, d = map(int, input().split())
cnt1 = 0
cnt2 = 0
while c > 0:
c -= b
cnt1 += 1
while a > 0:
a -= d
cnt2 += 1
if cnt1 <= cnt2:
print('Yes')
else:
print('No') | a, b, c, d = list(map(int, input().split()))
turn = True
while True:
if turn:
c -= b
if c <= 0:
print("Yes")
import sys
sys.exit()
else:
a -= d
if a <= 0:
print("No")
import sys
sys.exit()
turn ^= True
| 1 | 29,723,152,502,500 | null | 164 | 164 |
def resolve():
a,b = map(int,input().split())
print(0 if a<b*2 else a-b*2)
resolve() | import sys
sys.setrecursionlimit(10**6)
a,b = map(int, input().split())
if 2*b > a:
print(0)
else:
print(a-2*b) | 1 | 166,952,780,602,992 | null | 291 | 291 |
a,b = input().split()
c,d = map(int,input().split())
e = input()
if a == e:
print("{} {}".format(c-1,d))
else:
print("{} {}".format(c,d-1))
| from sys import stdin, setrecursionlimit
def main():
s, t = map(str, stdin.readline().split())
a, b = map(int, stdin.readline().split())
u = stdin.readline()[:-1]
if s == u:
a -= 1
else:
b -= 1
print(a, b)
if __name__ == "__main__":
setrecursionlimit(10000)
main() | 1 | 71,518,487,273,128 | null | 220 | 220 |
n,k = map(int,input().split())
a = []
for i in range(k):
d = input()
a = a+input().split()
a = set(a)
print(n-len(a))
| import collections
N, M = map(int, input().split())
ways = collections.defaultdict(set)
for _ in range(M):
a, b = map(int, input().split())
a -= 1
b -= 1
ways[a].add(b)
ways[b].add(a)
dist = [-1] * N
q = collections.deque()
dist[0] = True
q.append(0)
while q:
n = q.popleft()
for adjacent in ways[n]:
if dist[adjacent] != -1:
continue
dist[adjacent] = dist[n] + 1
q.append(adjacent)
if -1 in dist:
print('No')
else:
print('Yes')
for n, d in enumerate(dist):
if n == 0:
continue
for adjacent in ways[n]:
if d - 1 == dist[adjacent]:
print(adjacent + 1)
break
| 0 | null | 22,638,959,148,938 | 154 | 145 |
N,K = map(int,input().split())
S = set()
for i in range(K) :
D=int(input())
L=list(map(int, input().split()))
for A in L :
S.add(A)
print (N-len(S))
| from itertools import accumulate
from collections import defaultdict
n, k = map(int, input().split())
a = list(map(int, input().split()))
acc = [0] + list(accumulate(a))
sm = [(e - i) % k for i, e in enumerate(acc)]
d = defaultdict(int)
ans = 0
for r in range(1, n + 1):
if r - k >= 0:
e = sm[r - k]
d[e] -= 1
e = sm[r - 1]
d[e] += 1
e = sm[r]
ans += d[e]
print(ans)
| 0 | null | 81,264,978,075,580 | 154 | 273 |
from sys import stdin
import math
N = int(stdin.readline().rstrip())
for i in range(1, 50000):
if math.floor(i * 1.08) == N:
print(i)
exit()
print(":(")
| import sys
l = []
for line in sys.stdin:
l.append(line)
for i in range(len(l)):
numl = l[i].split(' ')
a = int(numl[0])
b = int(numl[1])
sum = a + b
digitstr = "{0}".format(sum)
print(len(digitstr)) | 0 | null | 63,021,082,653,578 | 265 | 3 |
#
# ACL_A
#
n = input()
n = int(n)
for i in range(n):
print("ACL", end='') | x = input()
x=int(x)
if (x<30):
print("No")
else:
print("Yes") | 0 | null | 4,000,852,745,372 | 69 | 95 |
import sys
sys.setrecursionlimit(300000)
from math import sqrt, ceil
from collections import defaultdict
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI0(): return map(lambda s: int(s) - 1, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
def LMI0(): return list(map(lambda s: int(s) - 1, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')
N = I()
n = ceil(sqrt(N))
d = defaultdict(int)
for x in range(1, n + 1):
for y in range(1, n + 1):
for z in range(1, n + 1):
a = x ** 2 + y ** 2 + z ** 2 + x * y + y * z + z * x
if a <= N:
d[a] += 1
for i in range(1, N + 1):
print(d[i]) | n = int(input())
count = [0 for i in range(n+1)]
for x in range(1, 101):
for y in range(1, 101):
for z in range(1, 101):
f = x ** 2 + y ** 2 + z ** 2 + x*y + y*z + z*x
if f <= n:
count[f] += 1
for i in count[1:]:
print(i) | 1 | 7,921,631,909,340 | null | 106 | 106 |
ii = 1
while True:
x = input()
if x == '0':
break
print("Case {0}: {1}".format(ii, x))
ii = ii + 1 | S = input()
T = input()
Nmax = len(T)
for i in range(len(S)-len(T)+1):
diff = 0
for j in range(len(T)):
if(S[i+j] != T[j]):
diff += 1
Nmax = min(diff, Nmax)
print(Nmax) | 0 | null | 2,069,369,506,370 | 42 | 82 |
N = int(input())
A = sorted(list(map(int,input().split())))
ans = "YES"
for i in range(1,N,2):
boollist = [ A[i-1] == A[i] ]
if i < N-1 :
boollist.append(A[i] == A[i+1])
if any(boollist):
ans = "NO"
break
print(ans) | import math
import collections
def main():
N = int(input())
A = map(int, input().split())
l = collections.Counter(A)
for key,value in l.items():
if value >= 2:
print('NO')
return
print('YES')
main() | 1 | 73,872,618,554,148 | null | 222 | 222 |
kazu = input()
kazu = int(kazu)
for i in range( kazu ):
print("ACL",end = '') | while True:
a, b = map(int, input().split())
if a == 0 and b == 0:
break;
for i in range(0, a):
if i % 2 == 0:
print(("#." * int((b + 1) / 2))[:b])
else:
print((".#" * int((b + 1) / 2))[:b])
print("") | 0 | null | 1,546,856,284,238 | 69 | 51 |
from itertools import chain
N = int(input())
zenhan = []
kouhan = []
for _ in range(N):
si = input()
last = 0
low = last
high = last
for s in si:
if s == "(":
last += 1
else:
last -= 1
low = min(low, last)
high = max(high, last)
if last >= 0:
zenhan.append((-low, last, low))
else:
kouhan.append((low - last, last, low))
zenhan.sort()
kouhan.sort()
open_close = 0
for key, last, low in chain(zenhan, kouhan):
if open_close + low < 0:
ans = "No"
break
else:
open_close += last
else:
if open_close:
ans = "No"
else:
ans = "Yes"
print(ans)
| n = int(input())
s = [input() for i in range(n)]
l, r = [], []
for i in s:
a, b = 0, 0
for j in range(len(i)):
if i[j] == "(":
b += 1
if i[j] == ")":
if b <= 0:
a += 1
else:
b -= 1
if b - a > 0:
l.append([a, b])
else:
r.append([a, b])
l.sort(key=lambda x:x[0])
r.sort(key=lambda x:x[1], reverse=True)
li = l + r
ans = 0
for i in li:
ans -= i[0]
if ans < 0:
exit(print("No"))
ans += i[1]
print("Yes" if ans == 0 else "No") | 1 | 23,659,974,587,738 | null | 152 | 152 |
def down(d, last, c):
result = 0
for i in range(26):
result += c[i] * (d - last[i])
return result
def main():
D = int(input())
C = [int(c) for c in input().split()]
S = [0] * D
for d in range(D):
S[d] = [int(s) for s in input().split()]
last = [0] * 26
score = 0
scores = [0] * D
for d in range(D):
t = int(input()) - 1
score += S[d][t]
last[t] = d + 1
score -= down(d + 1, last, C)
scores[d] = score
for score in scores:
print(score)
if __name__ == "__main__":
main()
| D = int(input())
c = list(map(int,input().split()))
s = [list(map(int,input().split())) for _ in range(D)]
t = [int(input())-1 for _ in range(D)]
S = 0
last = [-1]*26
score = 0;
for d in range(D):
S += s[d][t[d]]
last[t[d]] = d
for i in range(26):
S-=c[i]*(d - last[i])
score +=max(10**6+S,0)
print(S)
| 1 | 10,011,836,133,056 | null | 114 | 114 |
import numpy as np
N=int(input())
A=np.array(list(map(int,input().split())))
'''
以下、方針
・Aから、各要素の出現回数を取得する。
・出現回数から、組み合わせを計算する。
'''
uni,cou=np.unique(A,return_counts=True)
# print(uni)
# print(cou)
dic0={}
dic1={}
dic={} #回答用
com=[]
for i in range(len(cou)):
c=cou[i]*(cou[i]-1)//2
dic0[uni[i]]=cou[i]
dic1[uni[i]]=c
com.append(c)
dic.setdefault(uni[i])
# print(com)
totalcombination=sum(com)
# print(com)
# print(dic1)
'''
以下方針
・Aから数字を取得
・その数字に対応する組み合わせを減らす。
・その数字の出現回数を1減らした状態の組み合わせの数を足す。
'''
for j in range (N):
n=A[j] #nはAの数列の数字
if dic[n]==None:
e=(dic0[n]-1)*(dic0[n]-2)//2
t=totalcombination-dic1[n]+e
dic[n]=t
print(t)
else:
print(dic[n]) | n = int(input())
def fib(i):
if i<=1: return 1
else:
a=0
b=1
for i in range(n+1):
c=b+a
b=a
a=c
return c
print(fib(n))
| 0 | null | 23,901,259,866,052 | 192 | 7 |
# -*- coding: utf-8 -*-
A,B,N = list(map(int, input().rstrip().split()))
#-----
x = min( (B-1) , N)
calc = int(A*x/B) - A * int(x/B)
print(calc)
| a, b, n = [int(x) for x in input().split()]
n = min(n,b-1)
x = max(0,n-a-a-a-a)
s = -a
for i in range(x,n+1):
s = max(s,(a*i//b)-a*(i//b))
print(s)
| 1 | 28,162,650,011,872 | null | 161 | 161 |
import math
while True:
n=int(input())
if n==0:
break
else:
score=list(map(int,input().split()))
dev=math.sqrt(sum((x-sum(score)/len(score))**2 for x in score)/len(score))
print("{:.8f}".format(dev))
| while True:
n = int(input())
if n == 0:
break
x = tuple(map(float, input().strip().split()))
y = (sum(x)/n)**2
z = sum(map(pow, x, (2 for i in range(n))))/n
print((z-y)**0.5) | 1 | 194,625,540,900 | null | 31 | 31 |
a,b,n = map(int, input().split())
if n>=b-1:
x = b-1
else:
x = n
print((a*x)//b-a*(x//b)) | N=[int(c) for c in input()][::-1]
dp=[[0]*2 for _ in range(len(N)+1)]
dp[0][1]=1
for i in range(len(N)):
dp[i+1][1]=min(dp[i][0]+N[i]+1,dp[i][1]+10-N[i]-1)
dp[i+1][0]=min(dp[i][0]+N[i],dp[i][1]+10-N[i])
print(dp[len(N)][0]) | 0 | null | 49,330,532,597,920 | 161 | 219 |
var=input()
print(var.swapcase())
| ABC = input().split()
print("Yes" if len(set(ABC)) == 2 else "No") | 0 | null | 34,614,605,036,958 | 61 | 216 |
#! /usr/bin/env python
# -*- coding: utf-8 -*-
''' ??????????????? '''
# ????????????????????°?????????N(N-1)/2????????§???
# ?¨???????:O(n^2)
def selection_sort(A, N):
swap_num = 0 # ???????????°
for i in range(N):
minj = i
for j in range(i, N, 1):
if A[j] < A[minj]:
minj = j
# swap
if i != minj:
tmp = A[i]
A[i] = A[minj]
A[minj] = tmp
swap_num += 1
return (A, swap_num)
if __name__ == "__main__":
N = int(input())
A = list(map(int, input().split()))
# A = [5, 2, 4, 6, 1, 3]
# N = len(A)
array_sorted, swap_num = selection_sort(A, N)
print(' '.join(map(str, array_sorted)))
print(swap_num) | n = int(input())
A = list(map(int, input().split()))
count = 0
for i in range(n):
minj = i
for j in range(i, n):
if A[j] < A[minj]:
minj = j
else:
pass
A[i], A[minj] = A[minj], A[i]
if i != minj:
count += 1
print(*A)
print(count)
| 1 | 20,045,542,780 | null | 15 | 15 |
import sys
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7
def main():
N = int(readline())
A = list(map(int, readline().split()))
R = 0
G = 0
B = 0
ans = 1
for i in range(N):
a = A[i]
count = 0
if R==a:
R+=1
count+=1
if G==a:
count+=1
if B==a:
count+=1
else:
if G==a:
G+=1
count+=1
if B==a:
count+=1
else:
if B==a:
B+=1
count+=1
else:
print(0)
exit()
ans*=count
ans%=MOD
print(ans)
if __name__ == '__main__':
main() | W = input().lower()
count = 0
while True:
a = input()
if a == "END_OF_TEXT":
break
a = a.lower()
a = a.split()
for i in range(len(a)):
if W == a[i]:
count += 1
print(count) | 0 | null | 65,827,237,037,760 | 268 | 65 |
# -*- coding: utf-8 -*-
def selection_sort(A, N):
change = 0
for i in xrange(N):
minj = i
for j in xrange(i, N, 1):
if A[j] < A[minj]:
minj = j
if minj != i:
temp = A[minj]
A[minj] = A[i]
A[i] = temp
change += 1
return (A, change)
if __name__ == "__main__":
N = input()
A = map(int, raw_input().split())
result, change = selection_sort(A, N)
print ' '.join(map(str, result))
print change | def selection_sort(a,n):
count=0
for i in range(n):
ch=False
minj=i
for j in range(i,n):
if a[j] < a[minj]:
minj = j
ch = True
if(ch):
count+=1
a[i],a[minj] = a[minj],a[i]
return count
if __name__ == "__main__":
n=int(input())
a=list(map(int,input().split()))
count=selection_sort(a,n)
print(*a)
print(count)
| 1 | 21,282,230,030 | null | 15 | 15 |
from collections import deque
from copy import deepcopy
# 初期入力
import sys
input = sys.stdin.readline
H,W = (int(i) for i in input().split())
map_initial =[["#"]*(W+2) for i in range(H+2)] #周囲を壁にするため+2
for h in range(1,H+1):
map_initial[h] =["#"] +list(input().strip()) +["#"]
def BSF(x,y):
dist =0
map =deepcopy(map_initial)
if map[x][y] =="#":
return dist
dq =set()
dq.add((x,y))
dq_sarch =set()
while len(dq) >0:
h,w =dq.pop()
map[h][w] ="#" #通り済を壁にする
if map[h+1][w]==".":
dq_sarch.add((h+1,w))
if map[h-1][w]==".":
dq_sarch.add((h-1,w))
if map[h][w+1]==".":
dq_sarch.add((h,w+1))
if map[h][w-1]==".":
dq_sarch.add((h,w-1))
if len(dq)==0:
dq =deepcopy(dq_sarch)
dq_sarch.clear()
dist +=1
#print(h,w,dist,end="\t")
return dist-1
#スタート位置を全探索し、最長距離を探す
dist_all =[]
for i in range(1,H+1):
for j in range(1,W+1):
dist_all.append(BSF(i,j) )
print(max(dist_all)) | # A * B
A,B = map(int,input().split())
answer = A * B
print(answer) | 0 | null | 55,419,701,984,128 | 241 | 133 |
X = int(input().split()[0])
x = 100
count = 0
while x < X:
x += x // 100
count += 1
ans = count
print(ans) | import math
x=int(input());money=100;year=0
while True:
if money >= x:
print(year)
break
money+=money//100
year+=1 | 1 | 26,925,176,426,450 | null | 159 | 159 |
if __name__ == "__main__":
s = input()
print(s[0:3]) | import math
n, d = map(int, input().split())
xy = [list(map(int, input().split())) for _ in range(n)]
count = 0
for i in range(n):
count = count + 1 if math.sqrt(xy[i][0]**2 + xy[i][1]**2) <= d else count
print(count)
| 0 | null | 10,296,388,429,828 | 130 | 96 |
a=[1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
k=int(input())
for i in range(32):
if i+1==k:
print(a[i])
exit() | # -*- coding: utf-8 -*-
import numpy as np
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
def zz():
return list(map(int, sys.stdin.readline().split()))
def z():
return int(sys.stdin.readline())
def S():
return sys.stdin.readline()
def C(line):
return [sys.stdin.readline() for _ in range(line)]
N = z()
A = zz()
ans = 0
for i, a in enumerate(A):
if(((i+1) % 2 == 1) and(a % 2 == 1)):
ans += 1
print(ans)
| 0 | null | 28,954,954,089,402 | 195 | 105 |
n = int(input())
p = n // 500 * 1000
n %= 500
p += n // 5 * 5
print(p) | D = int(input())
Cs = list(map(int,input().split()))
Ss = [list(map(int,input().split())) for i in range(D)]
ts = []
for i in range(D):
ts.append(int(input()))
last = [0]*26
satisfy = 0
for day,t in enumerate(ts):
day += 1
last[t-1] = day
satisfy += Ss[day-1][t-1]
for i in range(26):
satisfy -= Cs[i]*(day-last[i])
print(satisfy) | 0 | null | 26,269,880,231,292 | 185 | 114 |
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce
rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rf = lambda: map(float, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7
a = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
print(a[ri()-1])
| from sys import stdin
import math
l = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
k = int(stdin.readline().rstrip())
print(l[k-1])
| 1 | 49,981,179,870,342 | null | 195 | 195 |
r, c = list(map(int, input().split()))
A = []
for i in range(r):
A.append(list(map(int, input().split())))
A[i].append(sum(A[i]))
A.append([])
for j in range(c):
A[r].append(sum(A[i][j] for i in range(r)))
A[r].append(sum(A[r]))
for i in range(r + 1):
print(" ".join(list(map(str, A[i])))) | r,c = map(int,raw_input().split())
cels = []
for i in xrange(r):
cols = map(int, raw_input().split())
cels.append(cols)
for i in xrange(r):
cels[i].append(sum([ j for j in cels[i]]))
sum_c=[]
for i in xrange(c):
sum_c.append(sum([cels[n][i] for n in xrange(r)]))
cels.append(sum_c)
cels[r].append(sum(cels[r]))
for i in xrange(r+1):
print " ".join(map(str,cels[i])) | 1 | 1,330,450,210,258 | null | 59 | 59 |
# -*- coding: utf-8 -*-
from sys import stdin
class Dice:
def __init__(self,dicelist):
self.dice_list = dicelist
def roll(self, direction):
work = list(self.dice_list)
if (direction == 'N'):
self.dice_list[0] = work[1]
self.dice_list[1] = work[5]
self.dice_list[2] = work[2]
self.dice_list[3] = work[3]
self.dice_list[4] = work[0]
self.dice_list[5] = work[4]
elif (direction == 'E'):
self.dice_list[0] = work[3]
self.dice_list[1] = work[1]
self.dice_list[2] = work[0]
self.dice_list[3] = work[5]
self.dice_list[4] = work[4]
self.dice_list[5] = work[2]
elif (direction == 'S'):
self.dice_list[0] = work[4]
self.dice_list[1] = work[0]
self.dice_list[2] = work[2]
self.dice_list[3] = work[3]
self.dice_list[4] = work[5]
self.dice_list[5] = work[1]
elif (direction == 'W'):
self.dice_list[0] = work[2]
self.dice_list[1] = work[1]
self.dice_list[2] = work[5]
self.dice_list[3] = work[0]
self.dice_list[4] = work[4]
self.dice_list[5] = work[3]
def roll_until_index0top(self,num0):
if self.dice_list[0] == num0:
return
commands = ['E', 'E', 'E', 'N', 'N', 'N']
for c in commands:
self.roll(c)
if self.dice_list[0] == num0:
return
else:
print('error')
def get_index_by_number(self, num):
for i in range(len((self.dice_list))):
if self.dice_list[i] == num:
return i
else:
return -1
def get_index2_number(self, num1):
index1 = self.get_index_by_number(num1)
if index1 == 1:
return self.dice_list[2]
elif (index1 == 2):
return self.dice_list[4]
elif (index1 == 3):
return self.dice_list[1]
elif (index1 == 4):
return self.dice_list[3]
else:
print('error 2')
def getTop(self):
return self.dice_list[0]
dice_list = list(map(int, stdin.readline().rstrip().split()))
q = int(stdin.readline().rstrip())
dice = Dice(dice_list)
for i in range(q):
num0_num1 = list(map(int, stdin.readline().rstrip().split()))
dice.roll_until_index0top(num0_num1[0])
print(dice.get_index2_number(num0_num1[1]))
| l=raw_input()
k=l.split()
a=0.0
b=0.0
a=int(k[0])
b=int(k[1])
#
print int((a-(a%b))/b),
print int(a%b),
a*=1.0
b*=1.0
if b==1:
print a*1.0
else:
if a/b>0.0000002:
print a/b
else: print "0.00000001.99999982" | 0 | null | 436,996,982,688 | 34 | 45 |
a,v = map(int,input().split())
b,w = map(int,input().split())
t = int(input())
a_t = t*v
b_t = t*w
if a_t >= b_t+abs(a-b):
print('YES')
else:
print('NO')
| S,T=[input() for i in range(2)]
U=list(S)
W=list(T)
if S in T and len(S)+1==len(T) and U[0]==W[0]:
print("Yes")
else:
print("No") | 0 | null | 18,363,944,558,960 | 131 | 147 |
ab = [int(x) for x in input().split()]
a, b = ab[0], ab[1]
print(a // b)
print(a % b)
print("{0:f}".format(a / b)) | a,b=map(int,input().split())
print('{0} {1} {2:.5f}'.format(a//b,a%b,a/b))
| 1 | 592,847,349,990 | null | 45 | 45 |
#!/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)
| N,*A = map(int, open(0).read().split())
if len(set(A)) == len(A):
print('YES')
else:
print('NO') | 0 | null | 77,032,542,564,220 | 228 | 222 |
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
# map(int, input().split())
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
def get_point(x):
if x == 'r':
return P
if x == 's':
return R
if x == 'p':
return S
# think each mod K
ans = 0
for i in range(K):
dp = [0] * 2
pre = ''
for j in range(i, N, K):
dp2 = dp[:]
dp2[0] = max(dp)
if pre == T[j]:
dp2[1] = dp[0] + get_point(T[j])
else:
dp2[1] = max(dp) + get_point(T[j])
pre = T[j]
dp = dp2
ans += max(dp)
print(ans) | N,K = map(int,input().split())
RST = list(map(int,input().split()))
#r:0 s:1 p:2
# a mod K != b mod Kならばaとbは独立に選ぶことができる。
# よってN個の手をmod KによるK個のグループに分け、各グループにおける最大値を求め、最終的にK個のグループの和が答え
T = [0 if i=="r" else 1 if i=="s" else 2 for i in input()]
dp = [[0]*3 for i in range(N)]
#直後の手を見て決める
for i in range(N):
if i<K:
for j in range(3):
# j:1,2,0
# j==0 -> RST[0]
#初期値は勝つ手を出し続ける
if (j+1)%3==T[i]:
dp[i][j]=RST[j]
else:
#現在の出す手
for j in range(3):
#K回前に出した手
for l in range(3):
if j!=l:
if (j+1)%3 == T[i]:
dp[i][j] = max(dp[i][j],RST[j]+dp[i-K][l])
else:
dp[i][j] = max(dp[i][j],dp[i-K][l])
ans = 0
for i in range(N-K,N):
ans += max(dp[i])
print(ans) | 1 | 106,310,531,211,728 | null | 251 | 251 |
def __main():
x = 1;
while x <= 9 :
y = 1;
while y <= 9 :
z = x * y;
print(str(x) + "x" + str(y) + "=" + str(z) )
y = y + 1
x = x + 1
__main()
| for x in range(1,10):
for i in range(1,10):
print(f"{x}x{i}={x*i}")
| 1 | 5,648,178 | null | 1 | 1 |
s = int(input())
print('%d:%d:%d' % (s / 3600, (s % 3600) / 60, s % 60))
| S = int(input())
h = S // 3600
m = S % 3600 // 60
s = S % 60
print(h, m, s, sep=':') | 1 | 337,033,917,230 | null | 37 | 37 |
x=int(input())
for i in range(x,10**6+1):
now=i-1
flag=0
while(2<=now):
if i%now==0:
flag=1
break
now-=1
if flag==0:
print(i)
break
| def readinput():
n=int(input())
return n
def main(n):
if n==2:
return 2
nmax=2*10**5
isprime=[1]*(nmax)
isprime[0]=0
isprime[1]=0
m=4
while(m<nmax):
isprime[m]=0
m+=2
for i in range(3,nmax,2):
if isprime[i]==0:
continue
else:
if i>=n:
return i
m=2*i
while(m<nmax):
isprime[m]=0
m+=i
return -1
if __name__=='__main__':
n=readinput()
ans=main(n)
print(ans)
| 1 | 105,832,016,827,702 | null | 250 | 250 |
#!/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)]
| dice = list(map(int,input().split()))
kaisuu = int(input())
lastlist = []
frlist = []
for i in range(kaisuu):
up,front = list(map(int,input().split()))
if up == dice[0] :
frlist = [1,2,4,3,1]
if up == dice[1]:
frlist = [0,3,5,2,0]
if up == dice[2]:
frlist = [0,1,5,4,0]
if up == dice[3]:
frlist = [0,4,5,1,0]
if up == dice[4]:
frlist = [0,2,5,3,0]
if up == dice[5] :
frlist = [1,3,4,2,1]
k = dice.index(int(front))
for n in frlist[0:4] :
if n == k :
l = frlist.index(int(n))
m = l + 1
lastlist.append(dice[frlist[m]])
for g in lastlist :
print(g)
| 1 | 258,052,041,028 | null | 34 | 34 |
# coding: utf-8
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
X, Y, A, B, C = lr()
P = lr(); P.sort(reverse=True)
Q = lr(); Q.sort(reverse=True)
R = lr(); R.sort(reverse=True)
P = P[:X]
Q = Q[:Y]
Z = P + Q + R
Z.sort(reverse=True)
answer = sum(Z[:X+Y])
print(answer)
| bingo=[]
for i in range(3):
a=list(map(int,input().split()))
bingo.append(a)
n=int(input())
for i in range(n):
b=int(input())
for j in range(3):
if b in bingo[j]:
bingo[j][bingo[j].index(b)]=0
def hantei(l):
for i in range(3):
if all([l[i][j]==0 for j in range(3)]):
return 'Yes'
elif all([l[j][i]==0 for j in range(3)]):
return 'Yes'
elif l[0][0]==0 and l[1][1]==0 and l[2][2]==0:
return 'Yes'
elif l[0][2]==0 and l[1][1]==0 and l[2][0]==0:
return 'Yes'
else:
continue
return 'No'
#print(bingo)
print(hantei(bingo)) | 0 | null | 52,423,641,337,180 | 188 | 207 |
N = int(input())
A = [0 for n in range(N)]
XY = [0 for n in range(N)]
count_max = 0
for i in range(N):
A[i] = int(input())
xy = []
for j in range(A[i]):
x_y = list(map(int,input().split()))
xy.append(x_y)
XY[i] = xy
for i in range(2 ** N):
op = [0]*N
for j in range(N): # ポイント1
if ((i >> j) & 1):
op[N-1-j] = 1
flag = True
for k in range(len(op)):
if op[k] == 1:
for l in range(A[k]):
if XY[k][l][1] != op[XY[k][l][0]-1]:
flag = False
if flag:
if sum(op) > count_max:
count_max = sum(op)
print(count_max) | # coding:utf-8
import sys
# ??\???
n = int(input())
display = []
for i in range(n):
num = i + 1
# 3??§?????????????????´???
if (num % 3 == 0):
display.append(num)
continue
# ????????????????????????3????????????????????´???
original = num
while (num != 0):
last_digit = int(num % 10)
if (last_digit == 3):
display.append(original)
num = 0
num = int(num / 10)
for x in display:
sys.stdout.write(' ' + str(x))
print('') | 0 | null | 61,148,757,735,260 | 262 | 52 |
S = input()
list_S = list(S)
T = input()
list_T = list(T)
count = 0
for i in range(len(list_S)):
if S[i] != T[i]:
count +=1
print(count) | while True:
h, w = map(int, input().split())
if h == w == 0:
break
print(('#' * w + '\n') * h) | 0 | null | 5,639,163,412,354 | 116 | 49 |
K, N = map(int, input().split())
A = list(map(int, input().split()))
li = []
for i in range(N):
if i == 1:
cadA = A[N-1] - A[0]
else:
cadA = K - A[i] + A[i-1]
if i == N-1:
cadB = A[N-1] - A[0]
cadB = 100000000000
else:
cadB = A[i] + (K - A[i+1])
cadB = 100000000000
li.append(min(cadA, cadB))
print(min(li)) | k, n = map(int, input().split())
a = list(map(int, input().split()))
num = 0
for i in range(n-1):
x = a[i+1]-a[i]
num = max(x, num)
f = (k - a[n-1]) + a[0]
if num >= f:
print(k - num)
else:
print(k - f) | 1 | 43,398,179,630,752 | null | 186 | 186 |
N, K = map(int, input().split())
def base_10_to_n(X, n):
if (int(X / n)):
return base_10_to_n(int(X / n), n) + str(X % n)
return str(X % n)
ans = base_10_to_n(N, K)
print(len(str(ans)))
| import sys
N, K = map(int, input().split())
if K == 10:
print(len(str(N)))
sys.exit()
p = 0
while True:
num = K ** p
if N < num:
break
p += 1
print(p)
| 1 | 64,069,470,771,522 | null | 212 | 212 |
def f(n):
mod = 1000000007
return (pow(10, n, mod) - 2 * pow(9, n, mod) + pow(8, n, mod)) % mod
n = int(input())
print(f(n))
| N = int(input())
def cal(N):
return abs(10**N - 9**N - 9**N + 8**N) % (10**9+7)
print(cal(N)) | 1 | 3,174,694,444,090 | null | 78 | 78 |
ans = []
while True:
arr = map(int, raw_input().split())
if arr[0] == 0 and arr[1] == 0:
break
else:
arr.sort()
ans.append(arr)
for x in ans:
print(" ".join(map(str, x))) | while 1:
tmp = map(int, raw_input().split())
if tmp[0] == tmp[1] == 0:
break
else:
print " ".join(map(str, sorted(tmp)))
| 1 | 517,618,267,310 | null | 43 | 43 |
if __name__ == "__main__":
K = int(input())
num_ls = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
ret = num_ls[K-1]
print(ret)
| #!/usr/bin/env python
from sys import stdin, stderr
def main():
K = int(stdin.readline())
A = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
print(A[K-1])
return 0
if __name__ == '__main__': main()
| 1 | 50,037,861,807,110 | null | 195 | 195 |
a, b = [int(x) for x in input().split()]
print(a*(a-1)//2 + b*(b-1)//2) | h, a = map(int, input().split())
b = 0
while h-a > 0:
h = h - a
b += 1
print(b + 1)
| 0 | null | 61,339,349,390,880 | 189 | 225 |
import itertools
import math
n = int(input())
#座標のリスト
xy_list = []
for i in range(n) :
xy_list.append(list(map(int, input().split())))
#順列を生成
per_list = []
for j in itertools.permutations([x for x in range(n)]) :
per_list.append(j)
#順列に従って距離を加算
d = 0
for k in per_list :
for l in range(n - 1) :
d += math.sqrt((xy_list[k[l + 1]][0] - xy_list[k[l]][0]) ** 2 + (xy_list[k[l + 1]][1] - xy_list[k[l]][1]) ** 2)
print(d/(len(per_list))) | import itertools
n = int(input())
p = [[0 for i in range(2)] for j in range(n)]
for i in range(n):
p[i][0],p[i][1] = map(int,input().split())
#print(p)
def dis(a,b):
dis = (a[0]-b[0]) ** 2 + (a[1]-b[1]) **2
return dis ** (1/2)
perm = [i for i in range(n)]
total = 0
num = 0
for v in itertools.permutations(perm,n):
path = 0
for i in range(n-1):
path += dis(p[v[i]],p[v[i+1]])
num += 1
total += path
print(total/num)
| 1 | 147,716,086,926,606 | null | 280 | 280 |
values = input()
a, b = [int(x) for x in values.split()]
print('{0} {1} {2:.5f}'.format(a // b, a % b, a / b)) |
a,b = map(int,input().split())
print('%d %d %.10f'%(a/b,a%b,a/b))
| 1 | 607,628,812,480 | null | 45 | 45 |
N = int(input())
a = list(map(int,input().split()))
dic = {}
for nums in a:
if nums in dic:
dic[nums] += 1
else:
dic[nums] = 1
ma = 0
for keys in dic:
ma = max(ma,dic[keys])
if ma == 1:
print("YES")
else:
print("NO") | n = int(input())
lst = list(map(int,input().split()))
x = len(lst)
lst = list(set(lst))
y = len(lst)
if (x == y):
print("YES")
else:
print("NO")
| 1 | 74,082,003,446,040 | null | 222 | 222 |
X = int(input())
if 1800 <= X:
print(1)
elif 1600 <= X:
print(2)
elif 1400 <= X:
print(3)
elif 1200 <= X:
print(4)
elif 1000 <= X:
print(5)
elif 800 <= X:
print(6)
elif 600 <= X:
print(7)
else:
print(8) | X = int(input())
if X >= 400 and 599 >= X:
print("8")
elif X >= 600 and 799 >= X:
print("7")
elif X >= 800 and 999 >= X:
print("6")
elif X >= 1000 and 1199 >= X:
print("5")
elif X >= 1200 and 1399 >= X:
print("4")
elif X >= 1400 and 1599 >= X:
print("3")
elif X >= 1600 and 1799 >= X:
print("2")
elif X >= 1800 and 1999 >= X:
print("1") | 1 | 6,680,046,337,448 | null | 100 | 100 |
N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
ans=[]
for i in range(N):
score=[]
B=[]
count=1
nex=P[i]-1
point=C[nex]
B.append(C[nex])
score.append(point)
while count<=K:
if nex==i:
break
nex=P[nex]-1
point+=C[nex]
score.append(point)
B.append(C[nex])
count+=1
if count==K:
ans.append(max(score))
else:
if point<=0:
ans.append(max(score))
else:
a=K//count
b=K%count
maxpoint=point*(a-1)
for j in range(b):
maxpoint+=B[j]
c=maxpoint
for j in range(count):
if b+j<count:
c+=B[b+j]
else:
c+=B[b+j-count]
maxpoint=max(maxpoint,c)
ans.append(maxpoint)
print(max(ans)) | import sys,queue,math,copy,itertools,bisect,collections,heapq
def main():
MOD = 10**9 + 7
NI = lambda : int(sys.stdin.readline())
SI = lambda : sys.stdin.readline().rstrip()
K = NI()
S = SI()
LS = len(S)
f = [0] * (K+LS)
r = [0] * (K+LS)
f[0] = 1
r[0] = 1
c = 1
for i in range(1, K + LS):
c = (c * i) % MOD
f[i] = c
r[i] = pow(c, MOD - 2, MOD)
def comb(n, k):
return (f[n] * r[k] * r[n - k]) % MOD
ans = 0
for i in range(K+1):
ans = (ans + pow(25,i,MOD) * pow(26,K-i,MOD) * comb(i+LS-1,LS-1)) % MOD
print(ans)
if __name__ == '__main__':
main() | 0 | null | 8,986,640,628,770 | 93 | 124 |
n, k = map(int, input().split())
a = list(map(int, input().split()))
d = [[-1] * n for _ in range(70)]
for i in range(n):
d[0][i] = a[i] - 1
for i in range(1, 70):
for j in range(n):
d[i][j] = d[i - 1][d[i - 1][j]]
dst = 0
while k:
i = 70
while pow(2, i) & k <= 0:
i -= 1
dst = d[i][dst]
k -= pow(2, i)
print(dst + 1)
| S = []
W = raw_input().split()
for word in W:
if word == "+":
s1 = S.pop()
s2 = S.pop()
S.append(s1 + s2)
elif word == "-":
s1 = S.pop()
s2 = S.pop()
S.append(s2 - s1)
elif word == "*":
s1 = S.pop()
s2 = S.pop()
S.append(s2 * s1)
else:
S.append(int(word))
print S.pop() | 0 | null | 11,423,762,849,160 | 150 | 18 |
N = int(input())
A, B = [], []
for i in range(N):
a, b = map(int, input().split())
A += [a]
B += [b]
A.sort()
B.sort()
if N%2 == 1:
med_minimum = A[(N-1)//2]
med_maximum = B[(N-1)//2]
else:
med_minimum = (A[(N-2)//2] + A[(N-2)//2+1])
med_maximum = (B[(N-2)//2] + B[(N-2)//2+1])
print(med_maximum - med_minimum + 1)
| N = int(input())
A = [0]*N
B = [0]*N
for i in range(N):
A[i],B[i] = map(int, input().split())
A.sort()
B.sort()
if len(A)%2 != 0:
ans = B[N//2]-A[N//2]+1
else:
ans = (B[N//2]+B[N//2-1]) - (A[N//2]+A[N//2-1]) + 1
print(ans) | 1 | 17,341,511,379,456 | null | 137 | 137 |
S = list(str(input()))
T = list(str(input()))
if S == T[:len(T)-1]:
print('Yes')
else:
print('No') | def main(S, T):
if len(S)+1 == len(T) and S == T[:-1]:
return 'Yes'
else:
return 'No'
if __name__ == '__main__':
S = input()
T = input()
ans = main(S, T)
print(ans)
| 1 | 21,441,564,162,900 | null | 147 | 147 |
# -*- coding:utf-8 -*-
import sys
import math
array =[]
for line in sys.stdin:
array.append(line)
for i in range(len(array)):
num = array[i].split(' ')
a = int(num[0])
b = int(num[1])
n = a + b
print(int(math.log10(n) + 1)) | n = int(input())
a = input().split()
min = int(a[0])
max = int(a[0])
sum = 0
for i in a:
if min > int(i):
min = int(i)
if max < int(i):
max = int(i)
sum += int(i)
print("{0} {1} {2}".format(min, max, sum)) | 0 | null | 361,732,085,550 | 3 | 48 |
def is_prime(num: int) -> bool:
# 6k +- 1 <= √n
if num <= 1:
return False
if num <= 3:
return True
if num % 2 == 0 or num % 3 == 0:
return False
i = 5
while i * i <= num:
if num % i == 0 or num % (i+2) == 0:
return False
i += 6
return True
x = int(input())
for i in range(x,1000000):
if is_prime(i):
print(i)
break
| x = int(input())
flag = False
for i in range(x, 1000000):
for j in range(2, i+1):
#print(i, j)
if j == i:
print(i)
flag = True
break
if i % j == 0:
#print('*', i, j)
break
if flag:
break | 1 | 105,350,798,721,100 | null | 250 | 250 |
s = str(input())
p = str(input())
connect = s + s
if p in connect:
print('Yes')
else:
print('No')
| s = input()
if s[0]==s[1] and s[0]==s[2]:
print("No")
else:
print("Yes")
| 0 | null | 28,423,917,451,370 | 64 | 201 |
import re
n = int(input())
text = input()
pattern = "ABC"
result = re.findall(pattern, text)
print(len(result)) | from fractions import gcd
from functools import reduce
def lcm(x,y):
return (x*y)//gcd(x,y)
def lcm_list(nums):
return reduce(lcm,nums,1)
mod=10**9+7
n=int(input())
A=list(map(int,input().split()))
num=lcm_list(A)
cnt=0
for i in A:
cnt +=num//i
print(cnt%mod) | 0 | null | 93,686,072,025,910 | 245 | 235 |
n = int(input())
li = list(map(int, input().split()))
print(min(li), max(li), sum(li)) | input();x=map(int,raw_input().split());print min(x),max(x),sum(x) | 1 | 716,288,849,066 | null | 48 | 48 |
from math import log2
print(2 ** int(log2(int(input())) + 1) - 1) | H = int(input())
count = 0
while H != 1:
H //= 2
count += 1
ans = 0
for i in range(count +1):
ans += 2 ** i
print(ans)
| 1 | 80,280,919,575,728 | null | 228 | 228 |
print((lambda x: (x[0]-1)//x[1]+1)(list(map(int,input().split())))) | n,m,k = map(int,input().split())
mod = 998244353
ans = 0
fact = [1,1]
finv = [1,1]
for i in range(2, n+1):
fact += [fact[i-1] * i % mod]
finv += [pow(fact[i], mod-2, mod)]
def comb(n,k, mod):
return (fact[n] * finv[k] * finv[n-k]) % mod
for i in range(0, k+1):
ans += m * pow(m-1, n-i-1, mod) * comb(n-1, i, mod)
ans %= mod
print(ans) | 0 | null | 50,163,978,971,350 | 225 | 151 |
import collections
def searchRoot(n):
par = parList[n]
if n == par:
if len(q) > 0:
rootIn(q,n)
return n
else:
q.append(n)
return(searchRoot(par))
def rootIn(q,root):
while len(q) > 0:
parList[q.popleft()] = root
n,m = list(map(int,input().split()))
q = collections.deque()
parList = [i for i in range(n+1)]
for i in range(m):
a,b = list(map(int,input().split()))
aRoot = searchRoot(a)
bRoot = searchRoot(b)
if aRoot != bRoot:
parList[max(aRoot,bRoot)] = min(aRoot,bRoot)
#print(parList)
ansDic = dict()
rootSet = set()
for i in range(1,n+1):
root = searchRoot(i)
if root in rootSet:
ansDic[root] += 1
else:
rootSet.add(root)
ansDic[root] = 1
ans = 1
for i in rootSet:
if ansDic[i] > ans:
ans = ansDic[i]
print(ans) | from collections import defaultdict
def main():
N, M = map(int, input().split())
g = defaultdict(set)
for _ in range(M):
a, b = map(int, input().split())
a -= 1
b -= 1
g[a].add(b)
g[b].add(a)
def dfs(u, visited: set):
stack = [u]
visited.add(u)
while stack:
u = stack.pop()
for v in g[u]:
if v not in visited:
stack.append(v)
visited.add(v)
count = dict()
for u in range(N):
if u in count:
continue
visited = set()
dfs(u, visited)
# print(visited)
count[u] = len(visited)
for v in visited:
count[v] = len(visited)
print(max(count.values()))
if __name__ == '__main__':
import sys
sys.setrecursionlimit(10000)
main() | 1 | 3,975,829,318,028 | null | 84 | 84 |
n, m = map(int, input().split())
number = [0] * n
cou = [0] * n
#logging.debug(number)#
for i in range(m):
s, c = map(int, input().split())
s -= 1
if cou[s] > 0 and c != number[s]:
print(-1)
exit()
else:
cou[s] += 1
number[s] = c
#logging.debug("number = {}, cou = {}".format(number, cou))
if n > 1 and number[0] == 0 and cou[0] >= 1:
print(-1)
exit()
elif n > 1 and number[0] == 0:
number[0] = 1
number = map(str, number)
print(''.join(number)) | n, s = map(int, input().split())
A = list(map(int, input().split()))
MOD = 998244353
W_MAX = s
dp = [0]*(W_MAX+1)
dp[0] = 1
inv_2 = pow(2, MOD-2, MOD)
for a in A:
for j in reversed(range(W_MAX+1)):
if W_MAX < j+a:
continue
dp[j+a] += dp[j]*inv_2
dp[j+a] %= MOD
print(dp[-1]*pow(2, n, MOD) % MOD) | 0 | null | 39,346,661,336,192 | 208 | 138 |
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 18 09:48:56 2017
@author: syaga
"""
if __name__ == "__main__":
N = int(input())
A = list(map(int, input().split()))
num = 0
for i in range(0, N):
minj = i
for j in range(i, N):
if A[j] < A[minj]:
minj = j
temp = A[i]
A[i] = A[minj]
A[minj] = temp
if minj != i:
num += 1
print(" ".join(map(str, A)))
print(num) | import math
def factorization(n):
arr = []
temp = n
for i in range(2, int(math.ceil(n**0.5))):
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)
exit(0)
fact_1i = factorization(n)
res = 0
for _, num in fact_1i:
temp_num = 1
while temp_num <= num:
res += 1
num -= temp_num
temp_num += 1
print(res) | 0 | null | 8,504,116,199,098 | 15 | 136 |
def main():
N=int(input())
A=[int(a) for a in input().split()]
Acnt, Aprob={}, {}
for a in A:
Acnt[a] = Acnt.get(a, 0) + 1
sumA=0
for k, a in Acnt.items():
Aprob[k]=a*(a-1)//2
sumA+=Aprob[k]
#Acnt=[A.count(n) for n in range(N+1)]
#Aprob=[Acnt[n]*(Acnt[n]-1)//2 for n in range(N+1)]
#sumA=sum(Aprob)
for a in A:
ans=(Acnt[a]-1)*(Acnt[a]-2)//2
print(ans+sumA-Aprob[a])
main()
| n = map(int, raw_input().split())
if (n[0] < n[1] and n[1] < n[2]):
print "Yes"
else:
print "No" | 0 | null | 23,923,430,203,360 | 192 | 39 |
a, b = map(int, input().split())
c =1
if b > a:
a, b = b, a
while c != 0:
c = a % b
a = b
b = c
print(a) | def Euclidean(input_list):
# print input_list
x = input_list[1]
y = input_list[0]
r = x % y
if r != 0:
list = [r, y]
list.sort()
return Euclidean(list)
else:
return y
list = map(int, raw_input().split(" "))
list.sort()
print Euclidean(list) | 1 | 7,913,328,708 | null | 11 | 11 |
class Dice:
def __init__(self,t,f,r,l,b,u):
self.t = t
self.f = f
self.r = r
self.l = l
self.b = b
self.u = u
self.a=[t,f,r,l,b,u]
self.direction={'S':(4,0,2,3,5,1),'N':(1,5,2,3,0,4),'E':(3,1,0,5,4,2),'W':(2,1,5,0,4,3),'Y':(0,3,1,4,2,5)}
def roll(self,d):
self.a=[self.a[i] for i in self.direction[d]]
self.t = self.a[0]
self.f = self.a[1]
self.r = self.a[2]
self.l = self.a[3]
self.b = self.a[4]
self.u = self.a[5]
t,f,r,l,b,u=map(int,input().split())
dice=Dice(t,f,r,l,b,u)
n=int(input())
s='SSSEWW'
yw='YYY'
for j in range(n):
t,f=map(int,input().split())
for d in s:
if dice.t==t:
break
dice.roll(d)
for t in yw:
if dice.f==f:
break
dice.roll(t)
print(dice.r)
|
class Dise():
def __init__(self, aLabelList):
self.LabelList = aLabelList
self.LabelsRelationship = [[0 for i in range(5)] for j in range(6)]
for i in range(6):
if i == 0:
self.LabelsRelationship[i][0] = self.LabelList[2 -1] #下1
self.LabelsRelationship[i][1] = self.LabelList[3 -1] #下2
self.LabelsRelationship[i][2] = self.LabelList[5 -1] #下3
self.LabelsRelationship[i][3] = self.LabelList[4 -1] #下4
self.LabelsRelationship[i][4] = self.LabelList[6 -1] #対面
elif i == 1:
self.LabelsRelationship[i][0] = self.LabelList[6 -1] #下1
self.LabelsRelationship[i][1] = self.LabelList[3 -1] #下2
self.LabelsRelationship[i][2] = self.LabelList[1 -1] #下3
self.LabelsRelationship[i][3] = self.LabelList[4 -1] #下4
self.LabelsRelationship[i][4] = self.LabelList[5 -1] #対面
elif i == 2:
self.LabelsRelationship[i][0] = self.LabelList[6 -1] #下1
self.LabelsRelationship[i][1] = self.LabelList[5 -1] #下2
self.LabelsRelationship[i][2] = self.LabelList[1 -1] #下3
self.LabelsRelationship[i][3] = self.LabelList[2 -1] #下4
self.LabelsRelationship[i][4] = self.LabelList[4 -1] #対面
elif i == 3:
self.LabelsRelationship[i][0] = self.LabelList[2 -1] #下1
self.LabelsRelationship[i][1] = self.LabelList[1 -1] #下2
self.LabelsRelationship[i][2] = self.LabelList[5 -1] #下3
self.LabelsRelationship[i][3] = self.LabelList[6 -1] #下4
self.LabelsRelationship[i][4] = self.LabelList[3 -1] #対面
elif i == 4:
self.LabelsRelationship[i][0] = self.LabelList[1 -1] #下1
self.LabelsRelationship[i][1] = self.LabelList[3 -1] #下2
self.LabelsRelationship[i][2] = self.LabelList[6 -1] #下3
self.LabelsRelationship[i][3] = self.LabelList[4 -1] #下4
self.LabelsRelationship[i][4] = self.LabelList[2 -1] #対面
elif i == 5:
self.LabelsRelationship[i][0] = self.LabelList[5 -1] #下1
self.LabelsRelationship[i][1] = self.LabelList[3 -1] #下2
self.LabelsRelationship[i][2] = self.LabelList[2 -1] #下3
self.LabelsRelationship[i][3] = self.LabelList[4 -1] #下4
self.LabelsRelationship[i][4] = self.LabelList[1 -1] #対面
def DisePrintRight(self,aTopItem,aFront):
xTopIndex = self.LabelList.index(str(aTopItem))
for i in range(4):
if int(self.LabelsRelationship[xTopIndex][i]) == aFront:
if i + 1 == 4:
idxR = int(self.LabelsRelationship[xTopIndex][0])
else:
idxR = int(self.LabelsRelationship[xTopIndex][i+1])
print(idxR)
break
myInstance = Dise(input().split())
x = int(input())
for i in range(x):
a,b = map(int, input().split())
myInstance.DisePrintRight(a,b)
| 1 | 247,165,129,828 | null | 34 | 34 |
# 3
A,B,C=map(int,input().split())
K=int(input())
def f(a,b,c,k):
if k==0:
return a<b<c
return f(a*2,b,c,k-1) or f(a,b*2,c,k-1) or f(a,b,c*2,k-1)
print('Yes' if f(A,B,C,K) else 'No')
| a, b, c = map(int, input().split())
k = int(input())
count = 0
while True:
if b > a:
break
b *= 2
count += 1
while True:
if c > b:
break
c *= 2
count += 1
if count <= k:
print('Yes')
else:
print('No')
| 1 | 6,840,863,681,860 | null | 101 | 101 |