code1
stringlengths 16
24.5k
| code2
stringlengths 16
24.5k
| similar
int64 0
1
| pair_id
int64 2
181,637B
⌀ | question_pair_id
float64 3.71M
180,628B
⌀ | code1_group
int64 1
299
| code2_group
int64 1
299
|
---|---|---|---|---|---|---|
x = input().split()
a, b, c =x
d = int(a)+int(b)+int(c)
if d<22:
print("win")
else:
print("bust")
| #!/usr/bin/env python3
def main():
A1, A2, A3 = map(int, input().split())
A=A1+A2+A3
if A >=22:
ans='bust'
else:
ans='win'
print(ans)
if __name__ == "__main__":
main()
| 1 | 119,310,057,003,600 | null | 260 | 260 |
# -*- coding: utf-8 -*-
while True:
line = list(input())
if '-' in line:
break
m = int(input())
for i in range(m):
h = int(input())
for j in range(h):
line.append(line[0])
line.pop(0)
for i in range(len(line)):
print(line[i],end='')
print()
| while(True):
t = input()
if t == '-':
break
for i in range(int(input())):
num = int(input())
t = t[num:] + t[:num]
print(t)
| 1 | 1,917,318,431,720 | null | 66 | 66 |
#/usr/bin/python
def solve():
n, k = map(int, input().split())
a = sorted(map(int, input().split()), reverse=True)
mod = 10 ** 9 + 7
l = 0
r = n - 1
ans = 1
if k % 2 == 1:
ans = a[0]
l = 1
# If the maximum number is negative and k is odd, the answer is the product of the first k numbers.
if ans < 0:
mul = 1
for i in range(k):
mul = mul * a[i] % mod
return mul
# otherwise, multiply the leftmost two products and the rightmost two products, whichever is larger.
for i in range(k//2):
l_mul = a[l] * a[l+1]
r_mul = a[r] * a[r-1]
if l_mul >= r_mul:
ans = ans * l_mul % mod
l += 2
else:
ans = ans * r_mul % mod
r -= 2
return ans
print(solve())
| # C - Traveling Salesman around Lake
K,N = map(int,input().split())
A = list(map(int,input().split()))
tmp = 0
for i in range(N):
tmp = max(tmp, (A[i]-A[i-1])%K)
print(K-tmp) | 0 | null | 26,469,942,252,320 | 112 | 186 |
N = int(raw_input())
taro_point = 0
hanako_point = 0
for n in range(N):
str_card = raw_input().split()
if str_card[0] > str_card[1]:
taro_point += 3
elif str_card[0] < str_card[1]:
hanako_point += 3
else:
taro_point += 1
hanako_point += 1
print '%d %d' % (taro_point, hanako_point) | from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
sys.setrecursionlimit(2147483647)
INF = 1 << 100
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007
n, u, v = LI()
G = [[] for _ in range(n)]
for a, b in LIR(n - 1):
G[a - 1] += [b - 1]
G[b - 1] += [a - 1]
def bfs(root):
dq = deque([root])
dist = [-1] * n
dist[root] = 0
while dq:
u = dq.popleft()
for v in G[u]:
if dist[v] == -1:
dist[v] = dist[u] + 1
dq += [v]
return dist
dist_u = bfs(u - 1)
dist_v = bfs(v - 1)
ans = 0
for i in range(n):
if dist_v[i] > dist_u[i]:
ans = max(ans, dist_v[i] - 1)
print(ans)
| 0 | null | 59,630,206,626,752 | 67 | 259 |
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1
N,M=map(int,input().split())
h=list(map(int,input().split()))
edge=[[] for _ in range(N)]
for _ in range(M):
a,b=map(int1,input().split())
edge[a].append(b)
edge[b].append(a)
ans=0
for i in range(N):
for nv in edge[i]:
if h[nv]>=h[i]:
break
else:
ans+=1
print(ans) | S = input()
if S[1] == 'B':
print('ARC')
else:
print('ABC') | 0 | null | 24,642,694,243,398 | 155 | 153 |
x,n=[int(x) for x in input().split()]
if n!=0:
p=[int(x) for x in input().split()]
l=list(range(110))
for i in p:
l.remove(i)
L=[]
for i in l:
L.append(abs(i-x))
m=min(L)
print(l[L.index(m)])
else:
print(x) | X, N = map(int, input().split())
P = list(map(int, input().split()))
st = set(P)
for i in range(111):
if not X - i in st:
print(X - i)
exit(0)
if not X + i in st:
print(X + i)
exit(0)
| 1 | 14,182,336,555,032 | null | 128 | 128 |
S = str(input())
print('x'*len(S))
| N, M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
# N, M = map(int, input().split())
# A = list(map(int, input().split()))
# print(len(A))
max_p = max(A)
X = max_p
cnt = [0 for x in range(100001)]
hpp = [0 for x in range(100001)]
# print(A)
for i in A:
# print(i)
# if cnt[i] != 0:
# continue
cnt[i] += 1
hpp[i] += i
# print(cnt[:100])
for i in range(1, 100001)[::-1]:
cnt[i - 1] += cnt[i]
hpp[i - 1] += hpp[i]
def countpair(left, x):
# return cnt[x - left + 1] if (x - left > 0) else N
if x - left <= 0:
return N
elif x - left > 100000:
return 0
else:
return cnt[x - left]
def get_hpp(left, x):
if x - left <= 0:
return hpp[0]
elif x - left > 100000:
return 0
else:
return hpp[x - left]
# print(hpp[:100])
# print(cnt[:100])
small = 0
large = max_p * 2
threshold = 0
# d_c = 0
# mems = []
while (small <= large):
# d_c += 1
# print(small, large)
X = (small + large) // 2
nums = 0
for i in range(N):
left = A[i]
# print(len(A))
nums += countpair(left, X)
if nums >= M:
threshold = X
small = X + 1
else:
large = X - 1
X = threshold
# print("X:", X)
# print("threshold:", threshold)
Y = X + 1
hpp_sum = 0
num_sum = 0
for i in range(N):
# print("-----------")
left = A[i]
cnt_num = countpair(left, Y)
num_sum += cnt_num
# print("left:", left)
# print("cnt_num:", cnt_num)
hpp_sum += left * cnt_num
# print("get_hpp:", get_hpp(left, Y))
hpp_sum += get_hpp(left, Y)
# print("hpp_sum:", hpp_sum)
hpp_sum += X * (M - num_sum) if (M - num_sum > 0) else 0
print(hpp_sum)
# print(out)
# for i in range(N):
| 0 | null | 90,644,716,845,536 | 221 | 252 |
N = int(input())
S = input()
a=int(N/2)
if N%2==0 and S[:a]==S[a:]:
print('Yes')
else:
print('No') | N,M=map(int,input().split())
n=N*(N-1)
m=M*(M-1)
result=int((n+m)/2)
print(result) | 0 | null | 96,142,362,544,800 | 279 | 189 |
N = int(input())
X = input()
ans = [1 for _ in range(N)]
#original 1 count
l = X.count('1')
if l == 0:
for _ in range(N):
print(1)
exit()
if l == 1:
if X[-1] == '1':
for _ in range(N-1):
ans[_] = 2
else:
ans[-1] = 2
for k in range(N):
if X[k] =='1':
ans[k] = 0
for _ in range(N):
print(ans[_])
exit()
intN = int(X, 2)
N1 = intN %(l-1)
N0 = intN % (l+1)
start = []
if N == 1:
if X[0] == '1':
print(0)
exit()
else:
print(1)
exit()
else:
s1 = 1
s0 = 1
for k in range(N-1, -1, -1):
if X[k] == '1':
ia = (N1 - s1)%(l-1)
else:
ia = (N0 + s0)%(l+1)
start.append(ia)
s1 = s1*2%(l-1)
s0 = s0*2%(l+1)
start = start[::-1]
ml = len(bin(l+1))-2
poplist = [0 for _ in range(N)]
t = 1
while t < N + 1:
t *= 2
for k in range(t//2, N, t):
for j in range(t//2):
if k+j>N-1:
break
poplist[k+j] += 1
"""
def popcount(n):
c = 0
n = bin(n)[2:]
for k in range(len(n)):
if n[k] == '1':
c+=1
return c
"""
for k in range(len(start)):
for count in range(10*5):
if start[k] == 0:
ans[k] += count
break
else:
start[k] = start[k] % poplist[start[k]]
for k in range(N):
print(ans[k]) | N = int(input())
A = list(map(int, input().split()))
even_numbers = [a for a in A if a % 2 == 0]
is_approved = all([even_num % 3 == 0 or even_num % 5 == 0 for even_num in even_numbers])
if is_approved:
print('APPROVED')
else:
print('DENIED')
| 0 | null | 38,620,762,435,548 | 107 | 217 |
# -*- coding: utf-8 -*-
# D
import sys
from collections import defaultdict, deque
from heapq import heappush, heappop
import math
import bisect
from itertools import combinations
input = sys.stdin.readline
# 再起回数上限変更
# sys.setrecursionlimit(1000000)
n = int(input())
if n == 1:
print('a')
sys.exit()
def dfs(s):
if len(s) == n:
print(''.join([chr(s + ord('a')) for s in s]))
else:
for i in range(len(set(list(s))) + 1):
dfs(s + [i])
dfs([])
| n, m = map(int, input().split())
a = list(map(int, input().split()))
dp = [0] + [float('inf')]*(n)
for i in range(m):
for j in range(a[i],n+1):
dp[j] = min(dp[j],dp[j-a[i]]+1)
print(dp[-1])
| 0 | null | 26,297,608,710,450 | 198 | 28 |
import math
n, a, b = map(int, input().split())
mod = 10**9 + 7
ans = pow(2, n, mod) - 1
x = 1
y = 1
for i in range(a):
x = x*(n - i)%mod
y = y*(i + 1)%mod
ans -= x*pow(y, mod - 2, mod)%mod
x = 1
y = 1
for i in range(b):
x = x*(n - i)%mod
y = y*(i + 1)%mod
ans -= x*pow(y, mod - 2, mod)%mod
print(ans%mod)
| n, a, b = map(int, input().split())
MOD = 10**9 + 7
def comb(n, r, mod):
if r > n-r:
r = n - r
res_X = 1
for i in range(n-r+1, n+1):
res_X = (res_X * i)%mod
res_Y = 1
for i in range(1, r+1):
res_Y = (res_Y * i)%mod
return (res_X * pow(res_Y, mod-2, mod))%mod
base = (pow(2, n, MOD) - 1)%MOD
a = comb(n, a, MOD)
b = comb(n, b, MOD)
print((base - a - b)%MOD) | 1 | 66,492,978,126,340 | null | 214 | 214 |
A, B, M = map(int, input().split())
price_A = list(map(int, input().split()))
price_B = list(map(int, input().split()))
min_A = min(price_A)
min_B = min(price_B)
ans = min_A + min_B
for i in range(M):
x, y, c = map(int, input().split())
ans = min(price_A[x-1]+price_B[y-1]-c,ans)
print(ans)
| X, K, D = map(int, input().split())
absX = abs(X)
step = min(K, (absX // D))
if K > step:
extra = - D * ((K - step) % 2)
else:
extra = 0
print(abs(absX - step * D + extra)) | 0 | null | 29,537,321,653,152 | 200 | 92 |
str1, str2 = input()*2, input()
if str2 in str1:
print("Yes")
else:
print("No")
| s = input()
p = input()
s += s
if p in s and 2 * len(p) <= len(s):
print("Yes")
else:
print("No") | 1 | 1,768,669,330,230 | null | 64 | 64 |
import math
a,b = map(int, input().split())
ans = 10000
for i in range(1010):
if math.floor(i*0.08) == a and math.floor(i*0.1) == b:
ans = i
break
if ans == 10000:
print(-1)
else:
print(ans) | a,b=map(int,input().split())
c=max(int(100*a/8),10*b)
d=min(int(100*a/8+100/8),10*b+10)
f=0
for x in range(c,d+1):
if f==0 and int(x*0.08)==a and int(x*0.1)==b:
f=1
print(x)
if f==0:
print(-1)
| 1 | 56,589,449,545,408 | null | 203 | 203 |
#!/usr/bin/env python3
import sys
from itertools import chain
import numpy as np
def solve(X: int):
if X <= 2:
return 2
flags = np.array([True for i in range(3, X + 100, 2)])
for i in range(len(flags)):
if flags[i]:
prime = i * 2 + 3
flags[i::prime] = False
if prime >= X:
return prime
def main():
X = int(input()) # type: int
answer = solve(X)
print(answer)
if __name__ == "__main__":
main()
| x=int(input())
if x==2:
print(2)
else:
while True:
cnt=0
if x%2==0:
x+=1
continue
for i in range(2,x):
if x%i==0:
cnt=1
continue
if cnt==1:
x+=1
else:
print(x)
break | 1 | 105,612,544,342,052 | null | 250 | 250 |
count = 0
INF = float('inf')
def merge(A, left, mid, right):
global count
L = A[left:mid] + [INF]
R = A[mid:right] + [INF]
i, j = 0, 0
for k in range(left, right):
count +=1
if L[i] <= R[j]:
A[k] = L[i]
i = i+1
else:
A[k] = R[j]
j = j+1
def mergeSort(A, left, right):
if left+1 < right:
mid = int((left+right)/2)
mergeSort(A, left, mid)
mergeSort(A, mid, right)
merge(A, left, mid, right)
#right は部分配列の末尾+1 の要素を指す。
n = int(input())
A = list(map(int, input().split()))
mergeSort(A, 0, n)
for i in range(n):
if i>0:
print(" ", end="")
print(A[i], end="")
print()
print(count)
| i = 1
while 1:
x = int( raw_input() )
if x == 0:
break
print "Case %d: %d" %(i,x)
i = i+1 | 0 | null | 306,893,496,208 | 26 | 42 |
n = int(input())
v = []
for i in range(n):
v_i = list(map(int,input().split()))[2:]
v.append(v_i)
d = [-1]*n
d[0]=0
queue = [1]
dis = 1
while len(queue)!=0:
queue2 = []
for i in queue:
for nex in v[i-1]:
if d[nex-1]==-1:
d[nex-1]=dis
queue2.append(nex)
dis +=1
queue = queue2
for i in range(n):
print(i+1,d[i])
| from collections import deque
N = int(input())
graph = [[] for _ in range(N)]
for i in range(N):
X = list(map(int, input().split()))
for j in range(2, len(X)):
graph[i].append(X[j]-1)
dist = [-1] * N
dist[0] = 0
q = deque([])
q.append(0)
while q:
x = q.popleft()
for next_x in graph[x]:
if(dist[next_x] > -1):
continue
dist[next_x] = dist[x] + 1
q.append(next_x)
for i in range(N):
print(i+1, dist[i])
| 1 | 3,867,999,890 | null | 9 | 9 |
# -*- coding: utf-8 -*-
def selection_sort(n, a):
cnt = 0
for i in range(n):
minj = i
for j in range(i, n):
if a[j] < a[minj]:
minj = j
if i != minj:
tmp = a[minj]
a[minj] = a[i]
a[i] = tmp
cnt += 1
return a, cnt
if __name__ == '__main__':
n = int(input())
a = [int(n) for n in input().split()]
ans, cnt = selection_sort(n, a)
print(' '.join(map(str, ans)))
print(cnt) | #!/usr/bin/env python3
N = int(input())
A = list(map(int, input().split()))
c = 0
for i in range(N):
minj = i
for j in range(i, N):
if A[j] < A[minj]:
minj = j
A[i], A[minj] = A[minj], A[i]
if i != minj:
c += 1
print(' '.join((str(x) for x in A)))
print(c) | 1 | 22,230,702,856 | null | 15 | 15 |
import sys
read = lambda: sys.stdin.readline().rstrip()
def counting_tree(N, D):
import collections
tot = 1
cnt = collections.Counter(D)
"""
cnt = [0]*N
for i in D:
cnt[i]+=1
"""
for i in D[1:]:
tot = tot * cnt[i-1]%998244353
return tot if D[0]==0 else 0
def main():
N0 = int(read())
D0 = tuple(map(int, read().split()))
res = counting_tree(N0, D0)
print(res)
if __name__ == "__main__":
main() | MOD = 998244353
def solve():
if D[0] != 0 or 0 in D[1:]:
print(0)
return
tree_dict = dict()
max_dist = 0
for idx, dist in enumerate(D):
tree_dict[dist] = tree_dict.get(dist, 0) + 1
max_dist = max(max_dist, dist)
ret = 1
for dist in range(max_dist + 1):
if not dist in tree_dict:
print(0)
return
ret = (ret * pow(tree_dict.get(dist - 1, 1), tree_dict[dist])) % MOD
print(ret)
if __name__ == "__main__":
N = int(input())
D = list(map(int, input().split()))
solve() | 1 | 154,476,273,966,980 | null | 284 | 284 |
# coding: utf-8
n = int(input())
A = list(map(int, input().split()))
print(" ".join(map(str,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
print(" ".join(map(str,A)))
| def main():
import sys
input = sys.stdin.readline
inf = 1 << 60
N, M, L = map(int, input().split())
dist = [[inf] * N for _ in range(N)]
for _ in range(M):
A, B, C = map(int, input().split())
A -= 1
B -= 1
dist[A][B] = dist[B][A] = C
for k in range(N):
for i in range(N):
for j in range(N):
dist[i][j] = min(
dist[i][j],
dist[i][k] + dist[k][j]
)
g = [[inf] * N for _ in range(N)] # 到達に必要な補充回数
for A in range(N):
for B in range(N):
if dist[A][B] <= L:
g[A][B] = 1
for k in range(N):
for i in range(N):
for j in range(N):
g[i][j] = min(
g[i][j],
g[i][k] + g[k][j]
)
Q = int(input())
for _ in range(Q):
s, t = (int(x) - 1 for x in input().split())
d = g[s][t]
if d == inf:
print(-1)
else:
print(d - 1)
if __name__ == '__main__':
main()
| 0 | null | 86,430,437,079,088 | 10 | 295 |
s = input()
ans = chk = 0
ans = [-1] * (len(s)+1)
if s[0] == "<":
ans[0] = 0
if s[-1] == ">":
ans[-1] = 0
for i in range(len(s) - 1):
if s[i] == ">" and s[i+1] == "<":
ans[i+1] = 0
for i in range(len(s)):
if s[i] == "<":
ans[i+1] = ans[i] + 1
for i in range(-1, -len(s)-1,-1):
if s[i] == ">":
ans[i-1] = max(ans[i-1], ans[i]+1)
print(sum(ans))
| s = input()
big = 0
small = 0
n = 0
big2 =0
for i in range(len(s)):
if s[i]=='<':
big += 1
n += big
small =0
big2 = big
else:
small += 1
n += small
if small<=big2:
n-=1
big =0
print(n) | 1 | 156,756,229,924,448 | null | 285 | 285 |
A,B = map(int,input().split())
for n in range(10000):
if int(0.08*n)==A and int(0.10*n)==B:
print(n)
exit()
print(-1) | h,n=map(int,input().split())
inf=100000000000
dp=[inf]*(h+1)
dp[h]=0
for i in range(n):
a,b=map(int,input().split())
for j in range(h,-1,-1):
dp[max(j-a,0)]=min(dp[max(j-a,0)],dp[j]+b)
print(dp[0]) | 0 | null | 69,019,051,909,320 | 203 | 229 |
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque
n,k = [int(x) for x in stdin.readline().rstrip().split()]
p = [(int(x)+1)/2 for x in stdin.readline().rstrip().split()]
m = sum(p[0:k])
maxm = m
for i in range(1,n-k+1):
m = m - p[i-1] + p[i+k-1]
maxm = max([maxm,m])
print(maxm)
| import math
a = float(input())
area = a * a * math.pi
cir = (a * 2) * math.pi
print(area,cir) | 0 | null | 37,947,732,157,790 | 223 | 46 |
A,B=map(int,input().strip().split())
print(max(A-2*B,0)) | a, b = list(map(int, input().split()))
print(a - 2 * b) if a > 2 * b else print(0)
| 1 | 166,226,074,655,002 | null | 291 | 291 |
S = input()
T = input()
if T.startswith(S):
if len(T)-len(S)==1:
print("Yes")
else:
print("No")
else:
print("No") | def main():
S = input()
T = input()
if S == T[:-1]:
print("Yes")
else:
print("No")
if __name__ == '__main__':
main()
| 1 | 21,393,965,323,740 | null | 147 | 147 |
import sys
sys.setrecursionlimit(700000)
def s_in():
return input()
def n_in():
return int(input())
def l_in():
return list(map(int, input().split()))
def print_l(l):
print(' '.join(map(str, l)))
class Interval():
def __init__(self, li):
self.li = li
self.n = len(li)
self.sum_li = [li[0]]
for i in range(1, self.n):
self.sum_li.append(self.sum_li[i-1] + li[i])
def sum(self, a, b=None):
if b is None:
return self.sum(0, a)
res = self.sum_li[min(self.n-1, b-1)]
if a > 0:
res -= self.sum_li[a-1]
return res
n = s_in()
k = n_in()
res = 0
d = len(n)
def comb(a,b):
if b == 0:
return 1
if b == 1:
return a
if b == 2:
return a*(a-1)//2
if b == 3:
return (a)*(a-1)*(a-2)//6
return 0
# 手前まで完全一致でii桁以降でl回非ゼロの時の場合の数
def calc(i, l):
m = int(n[i])
if i == d-1:
if l == 0:
if m == 0:
return 1
else:
return 0
elif l == 1:
if m == 0:
return 0
else:
return m
else:
return 0
if m == 0:
return calc(i+1, l)
else:
tmp = calc(i+1, l-1) # i桁めがmの場合
tmp += (m-1)*comb(d-i-1, l-1)*int(9**(l-1)) # 1..(m-1)
tmp += comb(d-i-1,l)*int(9**l)
return tmp
print(calc(0,k))
| # from collections import defaultdict
import math
def comb(n, r):
return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N = int(input())
K = int(input())
keta = len(str(N))
dp = [[0]*(keta) for i in range(4)]
ans = 0
for k in range(1, 4):
for i in range(1, keta):
if i<k:
continue
else:
dp[k][i] = comb(i-1, i-k)*(9**k)
#print(dp)
# dp[k][i]: i桁で、k個の0でない数
ans += sum(dp[K]) # (N の桁)-1 までの累積和
count = 0
for j in range(keta):
t = int(str(N)[j])
if j==0:
count+=1
ans += sum(dp[K-count])*(t-1)
if count == K: # K==1
ans+=t
break
continue
elif j==keta-1:
if t!=0:
count+=1
if count==K:
ans+=t
break
if t !=0:
count+=1
if count==K:
ans+=sum(dp[K-count+1][:keta-j]) #0のとき
ans+=t
break
ans += sum(dp[K-count][:keta-j])*(t-1) #0より大きいとき
ans += sum(dp[K-count+1][:keta-j]) #0のとき
print(ans) | 1 | 75,914,288,576,182 | null | 224 | 224 |
N, M, L = map(int, input().split())
dist = [[10**12] * N for _ in range(N)]
for i in range(N):
dist[i][i] = 0
for _ in range(M):
a, b, c = map(int, input().split())
if c <= L:
dist[a-1][b-1] = dist[b-1][a-1] = c
for k in range(N):
for i in range(N):
for j in range(N):
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
old_dist = [row[:] for row in dist]
for i in range(N):
for j in range(N):
if old_dist[i][j] <= L:
dist[i][j] = 1
for k in range(N):
for i in range(N):
for j in range(N):
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
Q = int(input())
for _ in range(Q):
start, end= map(int, input().split())
if dist[start-1][end-1] < 10**12:
print(dist[start-1][end-1] - 1)
else:
print(-1) | #Eans
import sys, math, itertools, collections, bisect
mans = float('inf')
mod = 10 **9 +7
ans = 0
count = 0
pro = 0
N, M = map(int, input().split())
A = sorted(map(int, input().split()))
B = [0] + A[:]
for i in range(N):
B [i+1] += B[i]
#print('A', A)
#print('B', B)
def solve_binary(mid):
tmp = 0
for i, ai in enumerate(A):
tmp += N - bisect.bisect_left(A, mid-ai)
#bisect.bisect_left(A, mid-ai)はaiに固定した時の、もう片方の最小の値のインデックス
#tmpは、aiに固定した時のありうる個数(M)
return tmp >= M
def binary_search(N):
ok = 0
ng = N
while abs(ok - ng ) > 1:
mid = (ok + ng) // 2
if solve_binary(mid):
ok = mid
else:
ng = mid
return ok
binresult = binary_search(2*10**5+1)
#Xを二分探索で得る
#binresult=ok
#print(binresult)
for i, ai in enumerate(A):
ans += ai*(N - bisect.bisect_left(A, binresult-ai)) + B[N] - B[bisect.bisect_left(A, binresult-ai)]
#print(ans, '=', ai, '*', N-bisect.bisect_left(A, binresult-ai), '+', B[N], '-', B[bisect.bisect_left(A, binresult-ai
count += N - bisect.bisect_left(A, binresult-ai)
ans -= binresult * (count-M)
print(ans) | 0 | null | 141,206,847,727,620 | 295 | 252 |
N=int(input())
s=0
for i in range(1,N+1):
s+=int(N/i)
if N%i==0:
s-=1
print(s)
| import math
N = int(input())
ans = 0
ve = math.floor(math.sqrt(N))
for st in range(ve):
tmp = math.floor(N/(st+1)-0.000001)
score = 2*(tmp - st)
if score > 0:
score -= 1
ans += score
print(ans) | 1 | 2,555,657,860,316 | null | 73 | 73 |
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = 10**6#float('inf')
mod = 10 ** 9 + 7
#mod = 998244353
#from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10
N = INT()
ans = 0
for a in range(1, N):
ans += (N-1)//a
print(ans) | n = int(input())
ans = 0
for a in range (1,n):
for b in range(1, n):
if n <= a * b:
break
ans += 1
print(ans) | 1 | 2,566,303,489,580 | null | 73 | 73 |
import sys
read = sys.stdin.readline
import time
import math
import itertools as it
def inp():
return int(input())
def inpl():
return list(map(int, input().split()))
start_time = time.perf_counter()
# ------------------------------
A = inpl()
A.sort()
if A[0]!=A[2]:
if A[0]==A[1]:
print('Yes')
sys.exit()
if A[1]==A[2]:
print('Yes')
sys.exit()
print('No')
# -----------------------------
end_time = time.perf_counter()
print('time:', end_time-start_time, file=sys.stderr) | def resolve():
L = list(map(int, input().split()))
print("Yes" if len(L)-1 == len(set(L)) else "No")
if '__main__' == __name__:
resolve() | 1 | 67,955,399,054,242 | null | 216 | 216 |
n = int(input())
ans = 0
for i in range(1, n + 1):
if i % 2 == 0:
continue
else:
ans += 1
print(ans / n)
| n = int(input())
g = n // 2
print((n-g) / n) | 1 | 177,451,163,059,730 | null | 297 | 297 |
# スコアが高い順に右端 or 左端に置く事を考える
# dp[i][l] : l番目まで左に子がいる状態でi番目の子を動かす時の最大値
n = int(input())
tmp = [int(x) for x in input().split()]
aList=[]
for i in range(n):
aList.append([tmp[i],i])
aList.sort(reverse=True)
dp=[[int(-1) for i in range(n+1)] for j in range(n+1)]
dp[0][0] = 0
# スコアが高い順にi人動かす
for i in range(n):
# 左側に置いた人数。i番目のループでは0〜i人まで左端に置く可能性がある
for l in range(i+1):
activity = aList[i][0]
pos = aList[i][1]
# 右端の今のポジション
# totalで置いた数-左側に置いた数(i-l)を計算し、右端(n-1)から引く
r = n-1-(i-l)
# 左に置いた場合
dp[i+1][l+1] = max(dp[i+1][l+1],dp[i][l] + abs(l-pos) * activity)
# 右に置いた場合
dp[i+1][l] = max(dp[i+1][l],dp[i][l] + abs(r-pos) * activity)
print(max(dp[n]))
| S = input()
ans = 'Yes'
while S:
if S.startswith('hi'):
S = S[2:]
else:
ans = 'No'
break
print(ans)
| 0 | null | 43,304,138,372,040 | 171 | 199 |
import math
n = int(input())
i = 1
tmp = n-1
while i <= math.sqrt(n):
if n%i == 0:
j = n//i
if i + j < tmp:
tmp = i + j -2
else:
pass
else:
pass
i += 1
print(tmp) | from collections import deque
N = int(input())
p = 1
for i in range(2,int(N ** (1/2)+1)):
if N % i == 0:
p = i
print(int(p+N/p)-2)
| 1 | 161,473,049,865,718 | null | 288 | 288 |
n, k = map(int, input().split())
h = list(map(int, input().split()))
ok_list = list()
for data in h:
if data >= k:
ok_list.append(data)
print(len(ok_list)) | from sys import stdin, setrecursionlimit
def main():
input = stdin.buffer.readline
n, k = map(int, input().split())
h = list(map(int, input().split()))
ans = 0
for hi in h:
if hi >= k:
ans += 1
print(ans)
if __name__ == "__main__":
setrecursionlimit(10000)
main()
| 1 | 178,389,642,871,360 | null | 298 | 298 |
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(0,n,2):
if a[i]&1:
ans+=1
print(ans) | a = int(input())
a-=1
cnt = 0
for i in range(1,a):
if i < a:
a -= 1
cnt += 1
else :
break
print (cnt) | 0 | null | 80,573,709,248,228 | 105 | 283 |
'''
Search - Allocation
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_4_D
Algorythm
Referenced #1670532 Solution for ALDS1_4_D: Allocation by Chris_Kobayashi
1. Decide the searching point -> (top of the range + bottom of the range) / 2
2. Check whether it is possible to load all of the luggage to the trugs
in the weight limit of the searching point
3. If it is possible, then move the top to the searching point
If it is impossible, then move the bottom to the searching point + 1
4. Continue 1~4 as long as the top is greater than bottom
Range
Serching the maximum loading capacity(MLC) number
between the maximam weight item num and the total weight num
top: total weight num
.
.
. <- search the num point
.
.
bottom: maximam weight item num
Why?
A1. Why not "bot = sp" but "bot = sp + 1"?
Q1. for example
top = 5
bot = 3
sp = (5 + 3) / 2 = 4
...
bot = sp
sp = (5 + 4) / 2 = 4
...
bot = sp
...for inf
A2. Why the top is total weight and bot is max weight?
Q2. Sure it is ok "top = 99999999, bot = 0".
But, MLC never pass the total weight
and the limit never be lower than the max.
So, it is the most effective range.
Note
1. This program contains the idea below
- Using "binary search" idea
- The appropriate range
2. To be the python program more fast
- Using int(raw_input()) instead of input()
- To rewrite the processings for the function is more fast
'''
# input
n, k = map(int,raw_input().split())
w = []
for i in xrange(n):
w.append(int(raw_input()))
def tempf(sp):
tk = 0 # temp index of the trugs
weight = 0 # temp total weight of a trug
# check whether it is possible to load all of the luggage
# to the trugs in the weight limit of the searching point
for tw in w:
if weight + tw <= sp:
weight += tw
else:
tk += 1
if tk >= k:
# when impossible, then raise the bottom
return False
weight = tw
else:
# when possible, then lowering the top
return True
# initial searching range
top = sum(w) # the top of the searching range
bot = max(w) # the bottom of the searching range
# searching until there is no serching range
while top > bot:
sp = (top + bot) / 2 # the searching point
if tempf(sp):
top = sp
else:
bot = sp + 1
# output MLC
print top | def maximum():
A,B,C,K = map(int,input().split())
num = 0
max = 0
if A <= K:
max = A
num = K - A
if B <= num:
num = num - B
max -= num
print(max)
return
else:
print(max)
return
else:
print(K)
return
maximum()
| 0 | null | 10,887,355,766,870 | 24 | 148 |
S = input()
s1 = S[0:int((len(S)-1)/2)]
s2 = S[int((len(S)/2))+1:len(S)+1]
if S == S[::-1] and s1 == s1[::-1] and s2 == s2[::-1]:
print("Yes")
else:
print("No") | # -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**6)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
#Union Find
#xの根を求める
def find(x):
if par[x] < 0:
return x
else:
par[x] = find(par[x])
return par[x]
#xとyの属する集合の併合
def union(x,y):
x = find(x)
y = find(y)
if x != y:
#sizeの大きいほうがx
if par[x] > par[y]:
x,y = y,x
par[x] += par[y] #xにyの個数くっつける
par[y] = x #yをxにくっつける
return
#xとyが同じ集合に属するかの判定
def same(x,y):
return find(x) == find(y)
#xが属する集合の個数
def size(x):
return -par[find(x)]
N,Q = map(int,readline().split())
#0-index
par = [-1]*N
for _ in range(Q):
x,y = map(int,readline().split())
x-=1; y-=1
union(x,y)
ans = set()
for i in range(N):
ans.add(find(i))
print(len(ans)-1) | 0 | null | 24,265,139,938,368 | 190 | 70 |
a = raw_input().lower()
s = 0
while True:
b = raw_input()
if b == 'END_OF_TEXT':
break
b = b.split()
b = map(str.lower,b)
s += b.count(a)
print s | N = input()
def f(x):
p = 1
i = 1
cnt = 0
while p > 0:
p = x // (5**i) // 2
cnt += p
i += 1
return cnt
if int(N[-1])%2 == 1:
print(0)
else:
print(f(int(N))) | 0 | null | 58,849,411,655,760 | 65 | 258 |
from sys import stdin
import sys, math
n = int(stdin.readline().rstrip())
a = [0 for i in range(n)]
d = [int(x) for x in stdin.readline().rstrip().split()]
for i in range(n-1):
k = d[i]
a[k-1] = a[k-1] + 1
for i in range(n):
print(a[i]) | import sys
input = sys.stdin.readline
ins = lambda: input().rstrip()
ini = lambda: int(input().rstrip())
inm = lambda: map(int, input().split())
inl = lambda: list(map(int, input().split()))
out = lambda x: print('\n'.join(map(str, x)))
n = ini()
s = list(ins())
n = s.count("R")
t = sorted(s)
ans = 0
for i in range(n):
if s[i] != t[i]:
ans += 1
print(ans) | 0 | null | 19,283,427,671,860 | 169 | 98 |
import numpy as np
import sys
read = sys.stdin.read
def solve(stdin):
h, w, m = stdin[:3]
row = np.zeros(h, np.int)
col = np.zeros(w, np.int)
for i in range(3, m * 2 + 3, 2):
y, x = stdin[i: i + 2] - 1
row[y] += 1
col[x] += 1
max_cnt_y = max(row)
max_cnt_x = max(col)
max_pair = np.sum(row == max_cnt_y) * np.sum(col == max_cnt_x)
for i in range(3, m * 2 + 3, 2):
y, x = stdin[i: i + 2] - 1
if row[y] == max_cnt_y and col[x] == max_cnt_x:
max_pair -= 1
return max_cnt_y + max_cnt_x - (max_pair == 0)
print(solve(np.fromstring(read(), dtype=np.int, sep=' '))) | H, W, m = map(int, input().split())
H_bomb = [0]*H
W_bomb = [0]*W
S = set()
for _ in range(m):
h, w = map(int, input().split())
h, w = h-1, w-1
S.add(tuple([h, w]))
H_bomb[h] += 1
W_bomb[w] += 1
H_max = max(H_bomb)
W_max = max(W_bomb)
H_memo = set()
W_memo = set()
for i in range(H):
if H_bomb[i] == H_max:
H_memo.add(i)
for j in range(W):
if W_bomb[j] == W_max:
W_memo.add(j)
ans = H_max + W_max
# for i in H_memo:
# for j in W_memo:
# if tuple([i, j]) not in S:
# print(ans)
# exit()
cnt = 0
for s in S:
if s[0] in H_memo and s[1] in W_memo:
cnt += 1
if cnt == len(H_memo)*len(W_memo):
print(ans-1)
else:
print(ans) | 1 | 4,715,559,629,898 | null | 89 | 89 |
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
jc=[0]*n
for i in range(n):
jc[i]+=i+1-a[i]
jcc=Counter(jc)
count=0
for j in range(n):
count+=jcc[a[j]+j+1]
print(count)
| 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 | 13,225,686,386,540 | 157 | 33 |
n=int(input())
a=list(map(int,input().split()))
b=0
for i in range(0,n):
if i%2==0 and a[i]%2==1:
b+=1
else:
b==b
print(b) | import math
import sys
import os
sys.setrecursionlimit(10**7)
def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))
if os.getenv("LOCAL"):
inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
sys.stdin = open(inputFile, "r")
INF = float("inf")
MOD = 998244353
# もらうDP(forcus on destination), 区間の和を足しこむ=累積和
N,K = LI()
LRs = [LI() for _ in range(K)]
dp = [0]*(N+1)
cum = [0]*(N+1)
dp[1] = 1
cum[1] = 1
for i in range(2, N+1):
for j in range(K):
l, r = LRs[j]
start = max(0,i-r-1)
end = max(0,i-l)
dp[i] += cum[end] - cum[start]
dp[i] %= MOD
# 累積和を更新
cum[i] = (cum[i-1] + dp[i]) % MOD
ans = dp[N]
print(ans) | 0 | null | 5,247,158,949,020 | 105 | 74 |
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
for i in range(n-k):
if a[i+k] > a[i]:
print("Yes")
else:
print("No") | def main():
N,K=map(int,input().split())
A=[0]+list(map(int,input().split()))
for i in range(K+1,N+1):
if A[i]>A[i-K]:
print("Yes")
else:
print("No")
if __name__=='__main__':
main() | 1 | 7,088,535,424,550 | null | 102 | 102 |
N = int(input())
A = list(map(int,input().split()))
cnt = 0
for i in range(N):
minj = i
for j in range(i+1,N):
if A[j] < A[minj]:
minj = j
if i != minj:
A[i],A[minj] = A[minj],A[i]
cnt += 1
print(*A)
print(cnt)
| def main():
n = int(input())
t = (n+1) // 2
if n == 1:
print(1)
elif n % 2 == 0:
print(0.5)
else:
print(t*1.0 / n)
main() | 0 | null | 88,297,357,344,238 | 15 | 297 |
n = int(input())
buf = [0]*n
def solv(idx,char):
aida = n - idx
if idx == n:
print("".join(buf))
return
for i in range(char + 1):
buf[idx] = chr(ord('a') + i)
solv(idx+1,max(i + 1,char))
solv(0,0) | # B - Bishop
H,W = map(int,input().split())
if H>1 and W>1:
print((H*W+1)//2)
else:
print(1) | 0 | null | 51,427,589,403,070 | 198 | 196 |
import math
a, b, c = map(float, input().split(' '))
c = c / 180 * math.pi
s = a * b * math.sin(c) / 2
l = a + b + pow((pow(a, 2) + pow(b, 2) - 2 * a * b * math.cos(c)), 0.5)
h = b * math.sin(c)
print('{0:.8f}'.format(s) + '\n' + '{0:.8f}'.format(l) + '\n' + '{0:.8f}'.format(h))
| import math
a,b, C = map(int, input().split())
C = math.radians(C)
S = (a * b * math.sin(C))/2
l = a + b + math.sqrt(a**2 + b**2 - 2*a*b*math.cos(C))
h = b * math.sin(C)
print(S)
print(l)
print(h) | 1 | 169,616,462,718 | null | 30 | 30 |
def cmb(n, r, p):
if r < 0 or n < r:
return 0
r = min(r, n - r)
return fact[n] * fact_inv[r] * fact_inv[n - r] % p
n, k = map(int, input().split())
a = sorted(list(map(int, input().split())), reverse=True)
p = 10 ** 9 + 7
fact = [1, 1] # fact[n] = (n! mod p)
fact_inv = [1, 1] # fact_inv[n] = ((n!)^(-1) mod p)
inv = [0, 1] # fact_invの計算用
for i in range(2, n + 1):
fact.append(fact[-1] * i % p)
inv.append((-inv[p % i] * (p // i)) % p)
fact_inv.append(fact_inv[-1] * inv[-1] % p)
i = 0
sum_max = 0
sum_min = 0
while n - i - 1 >= k - 1:
cnt = cmb(n - i - 1, k - 1, p)
sum_max += a[i] * cnt
sum_min += a[n - i - 1] * cnt
# print(i, a[i] * cnt, a[n - i - 1] * cnt)
i += 1
ans = (sum_max - sum_min) % p
print(ans)
| N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
def cmb(n, r):
if (r < 0) or (n < r):
return 0
r = min(r, n - r)
return fact[n] * factinv[r] * factinv[n - r] % mod
for i in range(2, N + 1):
fact.append((fact[-1] * i) % mod)
inv.append((-inv[mod % i] * (mod // i)) % mod)
factinv.append((factinv[-1] * inv[-1]) % mod)
A.sort()
ans = 0
for i, (n, x) in enumerate(zip(A, A[::-1])):
if i < K-1:
continue
ans += n * cmb(i, K - 1)
ans %= mod
ans -= x * cmb(i, K - 1)
ans %= mod
print(ans)
| 1 | 95,411,599,110,688 | null | 242 | 242 |
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)
X = int(input())
big_coin, amari = divmod(X, 500)
print(big_coin*1000+(amari//5)*5) | X = int(input())
a = X // 500 # 500円硬貨の枚数
X = X % 500 # 端数
b = X // 5 # 5円硬貨の枚数
print(a*1000+b*5) | 1 | 42,717,680,863,998 | null | 185 | 185 |
S,T = input().split(" ")
A,B = list(map(int,input().split(" ")))
U = input()
if S == U:
print(A-1,B)
else:
print(A,B-1) | s, t = input().split()
a, b = [int(i) for i in input().split()]
u = input()
if s == u:
a -= 1
elif t == u:
b -= 1
print(a, b) | 1 | 71,766,536,301,240 | null | 220 | 220 |
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
s = readline()
if s[2] == s[3] and s[4] == s[5]:
print('Yes')
else:
print('No') | import math
import collections
import itertools
import heapq
import sys
def main():
S = sys.stdin.readline()
if S[2] == S[3] and S[4] == S[5]:
print("Yes")
else:
print("No")
if __name__ == "__main__":
main() | 1 | 41,739,119,245,792 | null | 184 | 184 |
n = int(raw_input())
minv = int(raw_input())
maxv = -1*10**9
for j in range(n-1):
a = int(raw_input())
b = a- minv
if maxv < b:
maxv = b
if minv > a:
minv = a
print maxv | a,b,c = map(int,input().split())
K = int(input())
judge = False
for i in range(K+1):
for j in range(K+1):
for k in range(K+1):
x = a*2**i
y = b*2**j
z = c*2**k
if i+j+k <= K and x < y and y < z:
judge = True
if judge:
print("Yes")
else:
print("No") | 0 | null | 3,481,679,079,962 | 13 | 101 |
X,Y,A,B,C =map(int,input().split())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
R = list(map(int,input().split()))
P.sort()
Q.sort()
R.sort()
P_new = P[-X::]
Q_new = Q[-Y::]
agg = P_new + Q_new +R
agg.sort()
print(sum(agg[-X-Y::])) | def main():
s=input()
x=s[:int((len(s)-1)/2)]
y=s[int((len(s)+3)/2)-1:]
flg=0
if s !=s[::-1]:
flg=1
if x !=x[::-1]:
flg=1
if y !=y[::-1]:
flg=1
if flg==1:
print("No")
else:
print("Yes")
main() | 0 | null | 45,481,018,715,680 | 188 | 190 |
a=int(input())
c=a+a**2+a**3
print(c) | import math
a = int(input())
print(a+pow(a,2)+pow(a,3)) | 1 | 10,181,577,066,920 | null | 115 | 115 |
N,X,M=map(int,input().split())
modlist=[0]*M
modset=set()
ans=0
for i in range(M):
if X in modset:
startloop=modlist.index(X)
sum_outofloop=sum(modlist[:startloop])
inloop=ans-sum_outofloop
numofloop=(N-startloop)//(i-startloop)
ans=sum_outofloop+numofloop*inloop+sum(modlist[startloop:startloop+((N-startloop)%(i-startloop))])
#print(inloop,i,startloop,sum_outofloop,numofloop,sum(modlist[startloop:startloop+((N-startloop)%(i-startloop))]))
break
else:
modset.add(X)
ans+=X
modlist[i]=X
X**=2
X%=M
if i==N-1:
break
print(ans)
#print(modlist) | N, X, M = [int(x) for x in input().split()]
A = [0] * (M + 1)
firstApearAt = {i:0 for i in range(M)}
A[1] = X
firstApearAt[X] = 1
l, r = 1, 2
for i in range(2, M + 1):
A[i] = (A[i - 1] * A[i - 1]) % M
if firstApearAt[A[i]] > 0:
r = i
l = firstApearAt[A[i]]
break
firstApearAt[A[i]] = i
ans = 0
if N <= l - 1:
ans = sum(A[1:N + 1])
else:
q, p = (N - l + 1) // (r - l), (N - l + 1) % (r - l)
ans = sum(A[1:l]) + q * sum(A[l:r]) + sum(A[l:l + p])
print(ans) | 1 | 2,851,097,938,182 | null | 75 | 75 |
n = int(input())
s = input()
A = list(map(int,s.split()))
flag = 1
cnt = 0
#####################################
for i in range(0,n):
minj = i
for j in range(i,n):
if A[j] < A[minj]:
minj = j
if(A[i] != A[minj]):
tmp = A[i]
A[i] = A[minj]
A[minj] = tmp
cnt += 1
#####################################
for k in range(0,len(A)-1):
print(A[k],end=" ")
print(A[len(A)-1])
print (cnt) | N = int(input())
A = list(map(int,input().split()))
def selectSort(A, N):
sw = 0
for i in range(N):
minj = i
for j in range(i, N):
if A[j] < A[minj]:
minj = j
if A[i] != A[minj]:
A[i], A[minj] = A[minj], A[i]
sw += 1
return str(sw)
sw = selectSort(A, N)
print(" ".join(map(str, A)))
print(sw) | 1 | 21,635,776,796 | null | 15 | 15 |
import sys
sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip())) #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split()) #空白あり
def LS2(): return list(sys.stdin.readline().rstrip()) #空白なし
N = I()
for i in range(int(N**.5),0,-1):
if N % i == 0:
print(i+(N//i)-2)
exit()
| import math
def canMakeTriangle(a, b, c):
return abs(b - c) < a
N = int(input())
L = list(map(int, input().split()))
L.sort()
res = 0
for i in range(1, N):
for j in range(i + 1, N):
a = 0
b = i
c = 0
while b - a >= 1:
c = (a + b) / 2
if canMakeTriangle(L[math.floor(c)], L[i], L[j]):
b = c
else:
a = c
c = math.floor(c)
if canMakeTriangle(L[c], L[i], L[j]):
res += i - c
else:
res += i - c - 1
print(res) | 0 | null | 167,107,843,824,570 | 288 | 294 |
a,b,c = map(float,input().split())
import math
h = b * math.sin(math.radians(c))
S = ( a * h ) / 2
L = a + b + ( math.sqrt( a**2 + b**2 - 2*a*b*math.cos(math.radians(c)) ) )
print('{0:.8f}'.format(S))
print('{0:.8f}'.format(L))
print('{0:.8f}'.format(h))
| def main():
s = input()
print(s[:3])
if __name__ == '__main__':
main()
| 0 | null | 7,490,989,469,590 | 30 | 130 |
n = int(input())
root = int(n**0.5)
yakusu = 0
for i in range(root, 0, -1):
if n % i == 0:
yakusu = i
break
another = n // yakusu
print(yakusu + another - 2) | x,y,z = map(int, input().split())
x = x^y
y = x^y
x = x^y
x = z^x
z = z^x
x = z^x
print("{} {} {}".format(x,y,z)) | 0 | null | 99,594,117,791,300 | 288 | 178 |
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 11 17:23:36 2020
@author: Aruto Hosaka
"""
N = int(input())
S = input()
color = ['R','G','B']
k = 1
A = {color[0]:0,color[1]:0,color[2]:0}
for c1 in color:
for c2 in color:
for c3 in color:
if c1 != c2 and c2 != c3 and c1 != c3:
A[(c1,c2,c3)] = 0
for c1 in color:
for c2 in color:
if c1!=c2:
A[(c1,c2)] = 0
for i in range(N):
if S[i] == 'R':
A[('G','B','R')] += A[('G','B')]
A[('B','G','R')] += A[('B','G')]
A[('B','R')] += A['B']
A[('G','R')] += A['G']
if S[i] == 'G':
A[('R','B','G')] += A[('R','B')]
A[('B','R','G')] += A[('B','R')]
A[('B','G')] += A['B']
A[('R','G')] += A['R']
if S[i] == 'B':
A[('G','R','B')] += A[('G','R')]
A[('R','G','B')] += A[('R','G')]
A[('R','B')] += A['R']
A[('G','B')] += A['G']
A[S[i]] += 1
counter = 0
for c1 in color:
for c2 in color:
for c3 in color:
if c1 != c2 and c2 != c3 and c1 != c3:
counter += A[(c1,c2,c3)]
dc = 0
for i in range(N-2):
k = 1
while k*2+i < N:
if S[i+k] != S[i] and S[i] != S[i+k*2] and S[i+k] != S[i+k*2]:
dc += 1
k += 1
print(counter-dc) | N = int(input())
S = list(input())
R = []
G = []
B = [0 for _ in range(N)]
b_cnt = 0
for i in range(N):
if S[i] == 'R':
R.append(i)
elif S[i] == 'G':
G.append(i)
else:
B[i] = 1
b_cnt += 1
answer = 0
for r in R:
for g in G:
answer += b_cnt
if (g-r)%2 == 0 and B[(r+g)//2] == 1:
answer -= 1
if 0 <= 2*g-r < N and B[2*g-r] == 1:
answer -= 1
if 0 <= 2*r-g < N and B[2*r-g] == 1:
answer -= 1
print(answer) | 1 | 36,134,831,313,920 | null | 175 | 175 |
while True :
H, W = [int(temp) for temp in input().split()]
if H == W == 0 :
break
for making in range(H):
if making == 0 or making == (H - 1) : print('#' * W)
else : print('#' + ('.' * (W - 2)) + '#')
print() | s, t = input().split()
a, b = map(int, input().split())
if s == input():
a -= 1
else:
b -= 1
print("{} {}".format(a, b)) | 0 | null | 36,618,607,827,552 | 50 | 220 |
from collections import defaultdict
N = int(input())
A = list(map(int,input().split()))
ans = 0
di = defaultdict(int)
for i, a in enumerate(A):
ans += di[i+1 - a]
di[i+1 + a] += 1
print(ans) | h,w,k=map(int,input().split())
cnt=1
ss=[input() for i in range(h)]
ans=[[0 for i in range(w)]for f in range(h)]
misyori=0
for ok in range(h):
s=ss[ok]
if set(s)!=set("."):
v=[gg for gg in range(w) if s[gg]=="#"]
l=[0]*w
for k in v[:-1]:
l[k+1]=1
l[0]=cnt
for i in range(1,w):
l[i]+=l[i-1]
for tate in range(misyori,ok+1):
ans[tate]=l
misyori=ok+1
cnt+=len(v)
for t in range(misyori,h):
ans[t]=l
for i in range(h):
print(*ans[i],sep=" ") | 0 | null | 84,761,290,034,400 | 157 | 277 |
n,x,m=map(int,input().split())
def f(ai,m):
return (ai*ai)%m
if x==0:
print(0)
elif x==1:
print(n)
elif m==1:
print(0)
elif n<=m*2:
asum=x
ai=x
for i in range(1,n):
ai=f(ai,m)
asum+=ai
print(asum)
else:
chk=[-1]*m
asum00=[0]*m
ai,asum=x,0
for i in range(m):
if chk[ai]!=-1:
icnt0=chk[ai]
break
else:
chk[ai]=i
asum00[i]=asum
asum+=ai
ai=f(ai,m)
icnt=i
asum0=asum00[icnt0]
icntk=icnt-icnt0
n0=n-1-icnt0
nk=n0//icntk
nr=n0%icntk
asumk=asum-asum0
air,asumr=ai,0
for i in range(nr):
asumr+=air
air=f(air,m)
asumr+=air
ans=asum0+asumk*nk+asumr
print(ans)
| N,X,M = map(int, input().split())
A = [X]
S = set(A)
cnt = 0
i = 0
for i in range(1, N):
A.append(A[i-1] ** 2 % M)
if A[i] in S:
break
else:
S.add(A[i])
roup_cycle = i - A.index(A[i])
before_roup_sums = sum(A[:A.index(A[i])])
roup_sums = sum(A[A.index(A[i]):i])
if roup_cycle != 0:
roup_amount = (N - A.index(A[i])) // roup_cycle
roup_mod = (N - A.index(A[i])) % roup_cycle
print(before_roup_sums + roup_sums * roup_amount + sum(A[A.index(A[i]):(A.index(A[i]) + roup_mod)]))
else:
print(sum(A)) | 1 | 2,824,743,612,550 | null | 75 | 75 |
k=int(input())
a,b=map(int,input().split())
print("OK" if a//k < b//k or a%k==0 or b%k==0 else "NG") | K = int(input())
A,B = map(int, input().split())
if A % K == 0:
print('OK')
else:
if B % K == 0:
print('OK')
else:
if (A // K+1)*K <= B:
print('OK')
else:
print('NG')
| 1 | 26,461,129,328,290 | null | 158 | 158 |
t=list(map(int,input().split()))
print(max(t[0]*t[2],t[0]*t[3],t[1]*t[2],t[1]*t[3])) | a, b, c, d = map(int,input().split(" "))
Ns = [a*c, a*d, b*c, b*d]
print(sorted(Ns)[-1]) | 1 | 3,060,203,048,718 | null | 77 | 77 |
N,K=map(int, input().split())
d=list(map(int,input().split()))
ans=0
for i in range(N):
if d[i]>=K:
ans=ans+1
print(ans) | n, k = list(map(int, input().split(' ')))
print(sum([tall >= k for tall in list(map(int, input().split(' ')))])) | 1 | 179,499,833,854,160 | null | 298 | 298 |
def main():
N, K, C=map(int, input().split())
S = input()[::-1]
R=[0]*K
n, k=0, 0
while k < K:
if S[n]=='o':
R[k]=N-n
k+=1
n += C
n+=1
R=R[::-1]
S=S[::-1]
n, k=0, 0
while k < K:
if S[n]=='o':
if n+1 == R[k]:
print(n+1)
k+=1
n += C
n+=1
#print(L)
if __name__=='__main__':
main()
|
n, k, c = map(int, input().split())
s = input()
l = [0] * k
r = [0] * k
p = 0
# for i in range(n):
i = 0
while i < n:
if s[i] == "o":
l[p] = i
p += 1
if (p >= k):
break
i += c
i += 1
p = k-1
# for i in range(n - 1, -1, -1):
i = n - 1
while i >= 0:
if s[i] == "o":
r[p] = i
p -= 1
if (p < 0):
break
i -= c
i -= 1
#print(l, r)
for i in range(k):
if l[i] == r[i]:
print(l[i]+1)
| 1 | 40,854,048,918,720 | null | 182 | 182 |
import sys
## io
IS = lambda: sys.stdin.readline().rstrip()
II = lambda: int(IS())
MII = lambda: list(map(int, IS().split()))
MIIZ = lambda: list(map(lambda x: x-1, MII()))
## dp
INIT_VAL = 0
MD2 = lambda d1,d2: [[INIT_VAL]*d2 for _ in range(d1)]
MD3 = lambda d1,d2,d3: [MD2(d2,d3) for _ in range(d1)]
## math
DIVC = lambda x,y: -(-x//y)
DIVF = lambda x,y: x//y
def main():
n, p = MII()
s = list(map(int, IS()))
if 10%p == 0: # pが2または5の場合
cnt = 0
for i, si in enumerate(s):
if si%p == 0:
cnt += i+1
print(cnt)
return None
d = [0]*p
d[0] = 1 # 0(mod p)はそれ単体で割り切れるため+1しておく
ss = 0
x = 1
for si in s[::-1]:
ss += x*si
ss %= p
d[ss] += 1
x = (10*x)%p # %p無しだとTLE
cnt = 0
for di in d:
cnt += di*(di-1)//2 # calc combination(n_C_2)
print(cnt)
if __name__ == "__main__":
main() | r = int(input())
import math
ans = 2 * math.pi * r
print(ans) | 0 | null | 45,009,494,485,152 | 205 | 167 |
import math
a = int(input())
print(a+pow(a,2)+pow(a,3)) | n = int(input())
S = set(map(int, input().split()))
q = int(input())
T = tuple(map(int, input().split()))
cnt = 0
for n in T:
cnt += n in S
print(cnt) | 0 | null | 5,170,044,878,922 | 115 | 22 |
a, b = map(int,input().split())
def main(x,y):
print(x*y)
main(a,b) | n, m = map(int,input().split())
h = list(map(int, input().split()))
e = [[] for _ in range(n)]
for i in range(m):
u, v = map(int,input().split())
u -= 1
v -= 1
e[u].append(h[u]- h[v])
e[v].append(h[v]- h[u])
cnt = 0
for i in range(n):
if not e[i]:
cnt += 1
elif min(e[i]) > 0:
cnt += 1
print(cnt) | 0 | null | 20,442,008,764,608 | 133 | 155 |
from functools import reduce
MOD = 10**9 + 7
n, a, b = map(int, input().split())
def comb(n, k):
def mul(a, b):
return a*b%MOD
x = reduce(mul, range(n, n-k, -1))
y = reduce(mul, range(1, k+1))
return x*pow(y, MOD-2, MOD) % MOD
ans = (pow(2, n, MOD)-1-comb(n,a)-comb(n,b)) % MOD
print(ans) | def combination_mod(n, r, mod):
x = 1
y = 1
for i in range(r):
x *= n - i
y *= r - i
x %= mod
y %= mod
return (x * pow(y, mod - 2, mod)) % mod
n, a, b = map(int, input().split())
mod = 10 ** 9 + 7
ans = pow(2, n, mod) - 1
ans %= mod
ans -= combination_mod(n, a, mod)
ans %= mod
ans -= combination_mod(n, b, mod)
ans %= mod
print(ans)
| 1 | 66,406,228,838,258 | null | 214 | 214 |
X = int(input())
lack = X % 1000
if lack == 0:
print(0)
else:
print(1000 - lack) | N, K = map(int, input().split())
H = list(map(int, input().split()))
print(len(list(filter(lambda x: x >= K, H)))) | 0 | null | 94,095,467,103,008 | 108 | 298 |
import sys
import itertools
input = sys.stdin.readline
sys.setrecursionlimit(100000)
def read_values():
return map(int, input().split())
def read_index():
return map(lambda x: x - 1, map(int, input().split()))
def read_list():
return list(read_values())
def read_lists(N):
return [read_list() for n in range(N)]
def functional(N, mod):
F = [1] * (N + 1)
for i in range(N):
F[i + 1] = (i + 1) * F[i] % mod
return F
INV = dict()
def inv(a, mod):
if a in INV:
return INV[a]
i = pow(a, mod - 2, mod)
INV[a] = i
return i
def C(F, a, b, mod):
return F[a] * inv(F[a - b], mod) * inv(F[b], mod) % mod
def main():
N, K = read_values()
mod = 10 ** 9 + 7
F = functional(5 * 10 ** 5, mod)
res = 0
if N <= K:
res = C(F, 2 * N - 1, N - 1, mod)
else:
for k in range(K + 1):
res += C(F, N, k, mod) * C(F, N - 1 , k, mod) % mod
print(res % mod)
if __name__ == "__main__":
main() | while True:
h,w = map(int,input().split())
if h == 0 and w == 0:
break
for x in range(h):
if x == range(h)[0] or x == range(h)[-1]:
print(w*"#",end="")
else:
for y in range(w):
if y == range(w)[0] or y == range(w)[-1]:
print("#",end="")
else:
print(".",end="")
print()
print()
| 0 | null | 33,748,265,253,660 | 215 | 50 |
n = int(input())
s, t = input().split()
ans = ''
for si, ti in zip(s, t):
ans += si + ti
print(ans) | N=int(input())
S,T=input().split()
list=[]
for i in range(N):
list.append(S[i]+T[i])
print(''.join(list))
| 1 | 112,026,188,673,040 | null | 255 | 255 |
# B>G>R
R,G,B = map(int,input().split())
K = int(input())
cnt = 0
while not G>R:
G *= 2
cnt += 1
while not B>G:
B *= 2
cnt += 1
if cnt<=K:
print("Yes")
else:
print("No") |
N = int(input())
A = list(map(int, input().split()))
L = 1000000007
state = [0,0,0]
M = [0,0,0]
for a in A:
t = None
n = 0
for i,s in enumerate(state):
if s == a:
t = i
n += 1
if n == 0:
break
M[n-1] += 1
state[t] += 1
if n > 0:
x = 2**30 % L
y = 3**19 % L
k = M[1] % x
l = M[2] % y
r = 2**k * 3**l % L
print(r)
else:
print(0)
| 0 | null | 68,184,760,003,854 | 101 | 268 |
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7
def resolve():
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
P = (A1 - B1) * T1
Q = (A2 - B2) * T2
if P > 0:
P *= -1
Q *= -1
if P + Q < 0:
print(0)
elif P + Q == 0:
print("infinity")
else:
S = (-P) // (P + Q)
T = (-P) % (P + Q)
if T != 0:
print(S * 2 + 1)
else:
print(S * 2)
if __name__ == '__main__':
resolve()
| T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
d1 = T1*(A1-B1)
d2 = T2*(A2-B2)
if d1>d2:
d1 *= -1
d2 *= -1
if (d1>0 and d1+d2>0) or (d1<0 and d1+d2<0):
print(0)
elif d1+d2==0:
print("infinity")
else:
v = (abs(d1)//(d1+d2))
ans = v*2
pos = d1+v*(d1+d2)
if pos<0 and pos+d2>=0:
ans += 1
print(ans) | 1 | 131,482,480,719,212 | null | 269 | 269 |
import math
a, b, c = map(int, input().split())
c = math.radians(c)
s = a * b * math.sin(c) / 2
d = math.sqrt(a**2 + b**2 - 2*(a * b * math.cos(c)))
h = b * math.sin(c)
for i in (s, a+b+d, h):
print(i)
| #coding:utf-8
while True:
try:
a, b = map(int, raw_input(). split())
x = a * b
while True:
c = a % b
a = b
b = c
if b == 0:
break
x = x / a
print("%d %d" % (a, x))
except:
break | 0 | null | 85,202,850,722 | 30 | 5 |
s = input()
c = s.count('R')
if c == 2 and s[1] == 'S':
c = 1
print(c) | text = input()
times = int(input())
for i in range(times):
function = input().split()
word = text[int(function[1]):int(function[2])+1]
if function[0] == "print":
print(word)
elif function[0] == "reverse":
word_reverse = word[::-1]
text = text[:int(function[1])] + word_reverse + text[int(function[2])+1:]
elif function[0] == "replace":
text = text[:int(function[1])] + function[3] + text[int(function[2])+1:] | 0 | null | 3,491,483,040,508 | 90 | 68 |
#Union-Find木の実装
from collections import Counter
N,M=map(int,input().split())
#初期化
par=[i for i in range(N)]#親の要素(par[x]=xのときxは木の根)
rank=[0]*N#木の深さ
#木の根を求める
def find(x):
#xが根だった場合xをそのまま返す
if par[x] == x:
return x
else:
#根が出るまで再帰する
par[x] = find(par[x])
return par[x]
#xとyの属する集合を併合
def unite(x,y):
x=find(x)#xの根
y=find(y)#yの根
#根が同じなら何もしない
if x == y:
return
#もし根の深さがyの方が深いなら
if rank[x] < rank[y]:
par[x] = y#yを根にする
else:
par[y] = x#xを根にする
if rank[x] == rank[y]:
rank[x]+=1
#xとyが同じ集合に属するか否か
#def same(x,y):
#return find(x)==find(y)
for _ in range(M):
a,b = map(int,input().split())
a,b = a-1, b-1
unite(a,b)
for i in range(N):
find(i)
c=Counter(par)
print(len(c)-1) | 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, M = map(int, input().split())
A = list(sorted(map(int, input().split())))
S = sum(A)
acc = [0] * (N + 1)
for i in range(1, N + 1):
acc[i] = acc[i - 1] + A[i - 1]
def check(x):
total = 0
cnt = 0
for a in A:
l = -1
r = N
while r - l > 1:
m = (l + r) // 2
if A[m] + a >= x:
r = m
else:
l = m
cnt += N - r
total += acc[N] - acc[r]
total += a * (N - r)
return (total, cnt)
left = 0
right = 10 ** 6
while right - left > 1:
mid = (left + right) // 2
res = check(mid)
if res[1] >= M:
left = mid
else:
right = mid
res = check(left)
ans = res[0]
print(ans - (res[1] - M) * left)
| 0 | null | 55,010,048,380,826 | 70 | 252 |
n=int(input())
while n>0:
n-=1000
print(abs(n)) | N = int(input())
print(-(N%-1000)) | 1 | 8,432,556,819,390 | null | 108 | 108 |
inputs = input().split(' ')
inputs = list(map(int,inputs))
W = inputs[0]
H = inputs[1]
x = inputs[2]
y = inputs[3]
r = inputs[4]
if r <= x and x <= W-r and r <= y and y <= H-r :
print('Yes')
else :
print('No') | S = input()
if(S=='SUN'):
print(7)
elif(S=='MON'):
print(6)
elif(S=='TUE'):
print(5)
elif (S=='WED'):
print(4)
elif (S=='THU'):
print(3)
elif (S=='FRI'):
print(2)
else:
print(1) | 0 | null | 66,910,413,401,348 | 41 | 270 |
x1,y1,x2,y2=map(float,input().split())
d=((x1-x2)**2 + (y1-y2)**2)**(1/2)
print(d)
| #-*-coding:utf-8-*-
from decimal import Decimal
import math
def main():
x1,y1,x2,y2 = map(Decimal,input().split())
print("{:.8f}".format(math.sqrt((x2-x1)**2+(y2-y1)**2)))
if __name__=="__main__":
main()
| 1 | 152,019,636,384 | null | 29 | 29 |
n = int(input())
if n % 2: print(0); exit()
n //= 2
ans = 0
while n >= 5:
n //= 5
ans += n
print(ans) | import sys
sys.setrecursionlimit(500000)
MOD = 10**9+7
def input():
return sys.stdin.readline()[:-1]
def mi():
return map(int, input().split())
def ii():
return int(input())
def i2(n):
tmp = [list(mi()) for i in range(n)]
return [list(i) for i in zip(*tmp)]
def main():
N = ii()
if N%2:
print(0)
else:
m = N//2
ans = 0
t = 5
while m >= t:
ans += m//t
t *= 5
print(ans)
if __name__ == '__main__':
main()
| 1 | 116,015,948,538,132 | null | 258 | 258 |
def main():
s = input()
if s in ["hi", "hihi", "hihihi", "hihihihi", "hihihihihi"]:
print("Yes")
else:
print("No")
if __name__ == "__main__":
main()
| #!/usr/bin/env python3
n = list(input())
if '3' == n[-1]:
print('bon')
elif n[-1] in ['0', '1', '6', '8']:
print('pon')
else:
print('hon')
| 0 | null | 35,961,373,150,912 | 199 | 142 |
N = int(input())
def func(x):
if len(x) == N:
print("".join(x))
return
last = ord(max(x)) - ord("a") + 1 if x else 0
for i in range(min(26, last) + 1):
x.append(chr(ord("a") + i))
func(x)
x.pop()
func([])
| import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue
sys.setrecursionlimit(10000001)
INF = 10 ** 10
MOD = 10 ** 9 + 7
ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
# ===CODE===
def main():
n = ni()
char = [chr(i) for i in range(97, 97 + 26)]
d = [0 for _ in range(11)]
d[0] = 1
def printer(d):
s = ""
for num in d:
if num != 0:
s = char[num - 1] + s
if len(s) == n:
print(s)
return
while d[n] == 0:
printer(d)
d[0] += 1
for i in range(10):
if d[i] > max(d[i + 1:]) + 1:
d[i] = 1
d[i + 1] += 1
if __name__ == '__main__':
main()
| 1 | 52,092,441,592,660 | null | 198 | 198 |
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect# lower_bound etc
#import numpy as np
#from copy import deepcopy
#from collections import deque
def sum_count(arr):
mem = arr[:]
while True:
if len(mem) == 1:break
tmp = []
while True:
if len(mem) == 1:
tmp.append(mem[-1])
mem = tmp[:]
break
if len(mem) == 0:
mem = tmp[:]
break
x1, x2 = mem.pop(), mem.pop()
tmp.append(int(x1) + int(x2))
return int(mem[0])
def run():
N = int(input())
X = list(input())[::-1]
lis_bf = []
lis_af = []
count1 = sum_count(X)
sum_val_bf, sum_val_af = 0, 0
if count1 <= 1:
for i in range(N):
tmp_af = 0
if int(X[i]):
tmp_af = pow(2, i, count1 + 1)
lis_af.append(tmp_af)
sum_val_af += tmp_af
sum_val_af %= count1 + 1
for i in list(range(N))[::-1]:
ans = 0
if X[i] == '1':
print(0)
continue
else:
next_val = (sum_val_af + pow(2, i, count1 + 1)) % (count1 + 1)
# print(next_val)
ans += 1
# print(f'i : {i}, next_val : {next_val}')
while True:
if next_val == 0: break
val = next_val
count_n = sum_count(list(bin(val)[2:]))
next_val = val % count_n
ans += 1
# print(f'next_val : {next_val}')
print(ans)
return None
for i in range(N):
tmp_bf, tmp_af = 0,0
if int(X[i]):
tmp_bf = pow(2, i , count1-1)
tmp_af = pow(2, i, count1+1)
lis_bf.append(tmp_bf)
lis_af.append(tmp_af)
sum_val_bf += tmp_bf
sum_val_bf %= count1-1
sum_val_af += tmp_af
sum_val_af %= count1 + 1
for i in list(range(N))[::-1]:
ans = 0
if X[i] == '1':
next_val = (sum_val_bf - lis_bf[i]) % (count1-1)
else:
next_val = (sum_val_af + pow(2, i, count1+1)) % (count1+1)
#print(next_val)
ans += 1
#print(f'i : {i}, next_val : {next_val}')
while True:
if next_val == 0:break
val = next_val
count_n = sum_count(list(bin(val)[2:]))
next_val = val % count_n
ans += 1
#print(f'next_val : {next_val}')
print(ans)
if __name__ == "__main__":
run()
| n = int(input())
x = list(input())
def popcount(n):
bin_n = bin(n)[2:]
count = 0
for i in bin_n:
count += int(i)
return count
cnt = 0
for i in range(n):
if x[i] == '1':
cnt += 1
plus = [0 for i in range(n)] # 2^index を cnt+1 で割った時のあまり
minus = [0 for i in range(n)] # 2^index を cnt-1 で割った時のあまり
if cnt == 0:
plus[0] = 0
else:
plus[0] = 1
if cnt != 1:
minus[0] = 1
for i in range(1, n):
plus[i] = (plus[i-1]*2) % (cnt+1)
if cnt != 1:
minus[i] = (minus[i-1]*2) % (cnt-1)
origin = int(''.join(x), base=2)
amariplus = origin % (cnt+1)
if cnt != 1:
amariminus = origin % (cnt-1)
for i in range(n):
if x[i] == '0':
amari = (amariplus + plus[n-i-1]) % (cnt+1)
else:
if cnt != 1:
amari = (amariminus - minus[n-i-1]) % (cnt-1)
else:
print(0)
continue
ans = 1
while amari != 0:
ans += 1
amari = amari % popcount(amari)
print(ans) | 1 | 8,200,649,748,662 | null | 107 | 107 |
h1, m1, h2, m2, k = list(map(int, input().split()))
s = (h1*60)+m1
e = (h2*60)+m2
print(e-s-k) | h1, m1, h2, m2, k = map(int, input().split())
h = h2 - h1
m = m2 - m1
m += 60 * h
print(m - k)
| 1 | 18,057,672,491,430 | null | 139 | 139 |
def bubble_sort(num_list):
list_length = len(num_list)-1
cnt = 0
for i in range(list_length):
for j in range(list_length,i,-1):
if num_list[j] < num_list[j-1]:
num_list[j],num_list[j-1] = num_list[j-1],num_list[j]
cnt += 1
return num_list,cnt
n = int(input())
num_list = list(map(int,input().split()))
ans_list,cnt = bubble_sort(num_list)
str_ans_list = [str(i) for i in ans_list]
print(' '.join(str_ans_list))
print(cnt)
| while True:
h,w=map(int,input().split())
if h==0 and w==0:
break
for y in range(h):
for x in range(w):
if y==0 or y==h-1 or x==0 or x==w-1:
print("#",end='')
else:
print(".",end='')
print()
print() | 0 | null | 429,516,443,034 | 14 | 50 |
def main():
a, b, c, d = (int(i) for i in input().split())
print(max(a*c, b*d, a*d, b*c))
if __name__ == '__main__':
main()
| a,b,c,d = map(int, input().split())
m = a*c
n = a*d
o = b*c
p = b*d
if(m>n and m>o and m>p):
print(m)
elif (n>m and n>o and n>p):
print(n)
elif (o> m and o>n and o>p):
print(o)
else:
print(p) | 1 | 3,078,826,283,932 | null | 77 | 77 |
import sys
N, M = map(int,input().split())
S = input()
S = S[::-1]
i = 0
ans = []
while i < N:
flag = 0
for j in range(M,0,-1):
if i + j <= N and S[i + j] == '0':
i += j
flag = 1
ans.append(j)
break
if flag:
continue
print(-1)
sys.exit()
ans.reverse()
print(*ans) | n,m = map(int,input().split())
S = list(input())[::-1]
memo = [False]*n
cur = 0
i = 0
ans = []
while i < n:
for j in range(min(m,n-i),0,-1):
# print(i,j)
if S[i+j] == "0":
ans.append(str(j))
i += j
break
if memo[i+j]:
ans = -1
break
memo[i+j] = True
if ans == -1:
break
# print(S[i:])
print(*ans[::-1]) if ans != -1 else print(-1) | 1 | 139,497,860,892,488 | null | 274 | 274 |
import sys
n = int(input())
x = sorted(list(map(int, input().split())))
if len(list(set(x))) == 1:
print(0)
sys.exit()
if n == 1:
print(0)
sys.exit()
x_min = x[0]
x_max = x[n-1]
min_sum = 10000000
for p in range(x_min, x_max):
sum = 0
for e in x:
len = (e - p)**2
sum += len
# print("p", p, "sum", sum, "min_sum", min_sum)
if sum <= min_sum:
min_sum = sum
print(min_sum)
| K = int(input())
A, B = map(int, input().split())
x = 1000//K
for n in range(x+1):
if (n * K >= A) and (n * K <= B):
print('OK')
break
if n == x:
print('NG') | 0 | null | 45,935,414,411,162 | 213 | 158 |
N, M, K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
sumA = [0]
sumB = [0]
tmp = 0
answer = 0
st = 0
for i in range(N):
tmp = tmp + A[i]
sumA.append(tmp)
tmp = 0
for i in range(M):
tmp = tmp + B[i]
sumB.append(tmp)
for i in range(N, -1, -1):
booktmp = 0
for j in range(st, M + 1):
if sumA[i] + sumB[j] <= K:
booktmp = i + j
else:
st = j
break
answer = max(answer, booktmp)
if j == M:
break
print(answer)
| import sys
import math
N = int(input())
def solve(num):
d = math.floor(num**(1/2))
for i in range(1,d+1):
if num % i == 0:
yield [i,num//i]
if N == 2:
print(1)
sys.exit()
cnt = 0
for a,b in solve(N-1):
if a == b:
cnt += 1
else:
cnt += 2
cnt -= 1
for s in solve(N):
if s[0] == s[1]:
s.pop()
for a in s:
if a == 1:
continue
tmp = N
while True:
tmp,m = divmod(tmp, a)
if m == 0:
continue
elif m == 1:
cnt += 1
break
print(cnt) | 0 | null | 26,042,685,932,130 | 117 | 183 |
n = int(input())
S = []
H = []
C = []
D = []
for x in range(0,n):
L = raw_input().split()
if L[0] == "S": S.append(int(L[1]))
elif L[0] == "H": H.append(int(L[1]))
elif L[0] == "C": C.append(int(L[1]))
else: D.append(int(L[1]))
for x in range(1,14):
if not x in S: print "S {}".format(x)
for x in range(1,14):
if not x in H: print "H {}".format(x)
for x in range(1,14):
if not x in C: print "C {}".format(x)
for x in range(1,14):
if not x in D: print "D {}".format(x) | import bisect
N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for a in range(N):
for b in range(a):
ab = L[a] + L[b]
r = bisect.bisect_left(L, ab)
l = a + 1
ans += max(r - l, 0)
print(ans) | 0 | null | 86,674,860,872,838 | 54 | 294 |
from queue import deque
n, m, k = map(int, input().split())
friends = [[] for _ in range(n)]
for _ in range(m):
a, b = map(int, input().split())
a -= 1
b -= 1
friends[a].append(b)
friends[b].append(a)
checked = [False] * n
groups = []
for i in range(n):
if checked[i]:
continue
checked[i] = True
que = deque([i])
group = [i]
while que:
now = que.popleft()
friends_now = friends[now]
for friend in friends_now:
if checked[friend] == False:
checked[friend] = True
group.append(friend)
que.append(friend)
groups.append(group)
# print(groups)
D = dict()
for i in range(len(groups)):
for j in groups[i]:
D[j] = i
# print(D)
block = [0] * n
for _ in range(k):
c, d = map(int, input().split())
c -= 1
d -= 1
if D[c] == D[d]:
block[c] += 1
block[d] += 1
for i in range(n):
group_i = D[i]
print(len(groups[group_i]) - block[i] - len(friends[i]) - 1) | n,m,k=map(int,input().split())
BAD=[0 for i in range(n+1)]
E=[]
for inp in range(m):
a,b=map(int,input().split())
E.append((a,b))
BAD[a]-=1
BAD[b]-=1
B=[]
for inp in range(k):
a,b=map(int,input().split())
B.append((a,b))
uf=[i for i in range(n+1)]
def uf_find(x,uf=uf):
while uf[x]!=x:
x=uf[x]
return x
res=[]
for ele in E:
if uf_find(ele[0]) == uf_find(ele[1]):
continue
else:
res.append((ele[0],ele[1])) #write result
if uf_find(ele[0])>uf_find(ele[1]):
uf[uf_find(ele[0])]=uf_find(ele[1])
else:
uf[uf_find(ele[1])]=uf_find(ele[0])
SCO=[0 for i in range(n+1)]
for i in range(1,n+1):
SCO[uf_find(i)]+=1
for bl in B:
if uf_find(bl[0])==uf_find(bl[1]):
BAD[bl[0]]-=1
BAD[bl[1]]-=1
ANS=[str(SCO[uf_find(i)] + BAD[i] -1) for i in range(1,n+1)]
print(" ".join(ANS))
| 1 | 61,986,344,268,882 | null | 209 | 209 |
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#l = list(map(int, input().split()))
r = int(input())
print (r*2*3.14)
| N, K = map(int, input().split())
A = list(map(int, input().split()))
# 丸太がすべてxの長さ以下になる回数を出し、それがK回以下か判定する
def f(x):
now = 0
for i in range(N):
now += (A[i]-1)//x
return now <= K
l = 0
r = 10**9
while r-l > 1:
mid = (r+l)//2
if f(mid):
r = mid
else:
l = mid
print(r) | 0 | null | 18,775,136,486,018 | 167 | 99 |
n=int(input())
A=list(map(int,input().split()))
g=1000
for s1,s2 in zip(A[:-1],A[1:]):
if s1<s2:
stockNum=g//s1
g+=stockNum*(s2-s1)
print(g)
| import re
H,W,K = map(int,input().split())
grid = [[0 for i in range(W+1)]]
for i in range(H):
S = input()
grid.append([0]+[ int(S[i]) for i in range(W) ])
#累積和gridの作成
#行について
for i in range(H+1):
for j in range(W):
grid[i][j+1] += grid[i][j]
#列について
for j in range(W+1):
for i in range(H):
grid[i+1][j] += grid[i][j]
min_cnt = (H-1)*(W-1)
#横の区切りを作る
for h in range(2**(H-1)):
cnt = 0
#二進数の文字列を取得
bin_str = format(h,"0{}b".format(H-1))
#区切りのインデックスリスト
l_p =[1]+[ x.start()+2 for x in re.finditer("1",bin_str) ] +[H+1]
#cntに区切りの分だけ付け加える
cnt = cnt + len(l_p) - 2
ws = 0
for w in range(1,W+1):
for k in range(len(l_p)-1):
if grid[l_p[k+1]-1][w]-grid[l_p[k+1]-1][w-1] > K:
min_cnt=(H-1)*(W-1)+1
if grid[l_p[k+1]-1][w]- (grid[l_p[k]-1][w]+grid[l_p[k+1]-1][ws]-grid[l_p[k]-1][ws]) > K:
cnt = cnt + 1
ws = w-1
break
min_cnt = min(min_cnt,cnt)
###
print(min_cnt) | 0 | null | 28,157,272,609,312 | 103 | 193 |
import sys
from collections import deque
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def main():
K = int(readline())
queue = deque(range(1, 10))
for _ in range(K):
n = queue.popleft()
tail = n % 10
n10 = n * 10
if tail == 0:
queue.append(n10 + tail)
queue.append(n10 + tail + 1)
elif tail == 9:
queue.append(n10 + tail - 1)
queue.append(n10 + tail)
else:
queue.append(n10 + tail - 1)
queue.append(n10 + tail)
queue.append(n10 + tail + 1)
print(n)
return
if __name__ == '__main__':
main()
| ma = lambda :map(int,input().split())
n,u,v = ma()
u,v = u-1,v-1
tree = [[] for i in range(n)]
import collections
for i in range(n-1):
a,b = ma()
tree[a-1].append(b-1)
tree[b-1].append(a-1)
que = collections.deque([(v,0)])
vis = [False]*n
dist_v = [0]*n
while que:
now,c = que.popleft()
vis[now] = True
dist_v[now] = c
for node in tree[now]:
if not vis[node]:
que.append((node,c+1))
que = collections.deque([(u,0)])
vis = [False]*n
dist_u = [0]*n
while que:
now,c = que.popleft()
vis[now] = True
dist_u[now] = c
for node in tree[now]:
if not vis[node]:
que.append((node,c+1))
ans = 0
for i in range(n):
if dist_u[i] < dist_v[i]:
ans = max(ans,dist_v[i])
print(ans-1)
| 0 | null | 79,086,727,053,348 | 181 | 259 |
N, = map(int, input().split())
print("ACL"*N)
| a, b = map(int, input().split())
print(-1 if a not in range(1, 10) or b not in range(1, 10) else a*b)
| 0 | null | 79,874,890,875,190 | 69 | 286 |
a = list(map(int,input().split()))
print("bust" if sum(a) >= 22 else "win") | A,B,C = map(int,input().split())
ans = A+B+C
if ans <= 21:
print('win')
else:
print('bust') | 1 | 118,610,782,355,772 | null | 260 | 260 |
import bisect
import sys
def main():
input = sys.stdin.readline
n, d, a = map(int, input().split())
fox = [None]*n
for i in range(n):
x, h = map(int, input().split())
fox[i] = (x, h)
fox.sort()
x = [int(fox[i][0]) for i in range(n)]
h = [int(fox[i][1]) for i in range(n)]
ans = 0
bit = [0]*n
for i in range(n):
if i != 0:
bit[i] += bit[i-1]
sub = max([(h[i]-bit[i]-1)//a+1, 0])
ans += sub
bit[i] += sub*a
index = bisect.bisect_right(x, x[i]+2*d)
if index != n:
bit[index] -= sub*a
print(ans)
if __name__ == "__main__":
main() | from itertools import accumulate
n,k = map(int, input().split())
As = list(map(int, input().split()))
for i in range(k):
imos = [0] * (n+1)
for i,a in enumerate(As):
l = max(0, i-a)
r = min(n, i+a+1)
imos[l] += 1
imos[r] -= 1
acc = list(accumulate(imos))
# 指数関数的に増える
if acc[:-1] == As:
print(*As)
exit()
As = acc[:-1]
print(*As) | 0 | null | 48,957,349,984,338 | 230 | 132 |
import math
p_max = 1000000 * 1000000
n, k = map(int, input().split())
w = [int(input()) for i in range(n)]
def check(p):
i = 0
for j in range(k):
s = 0
while (s + w[i] <= p):
s += w[i]
i += 1
if i == n:
return n
return i
if __name__ == "__main__":
right = p_max
left = 0
mid = 0
while (right - left > 1):
mid = math.floor((right + left) / 2)
v = check(mid)
if v >= n:
right = mid
else:
left = mid
print (right)
| import collections
N = int(input())
A = list(map(int,input().split()))
#index,h
pp =[]
pm = []
all_set = set()
for i in range(N):
pp.append(i + A[i])
pm.append( i - A[i])
count = 0
c = collections.Counter(pp)
#print(c)
for i in range(N):
count += c[pm[i]]
#print(c[pm[i]])
#count += pp.count(pm[i])
#count += pm.count(pp[i])
print(count) | 0 | null | 13,157,135,018,170 | 24 | 157 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.