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
|
---|---|---|---|---|---|---|
import math
import collections
N = int(input())
Point = collections.namedtuple('Point', ['x', 'y'])
def koch(d, p1, p2):
if d == 0:
return
th = math.pi * 60 / 180
s = Point(x=(2 * p1.x + p2.x) / 3, y=(2 * p1.y + p2.y) / 3)
t = Point(x=(p1.x + 2 * p2.x) / 3, y=(p1.y + 2 * p2.y) / 3)
u = Point(x=(t.x - s.x) * math.cos(th) - (t.y - s.y) * math.sin(th) + s.x,
y=(t.x - s.x) * math.sin(th) + (t.y - s.y) * math.cos(th) + s.y)
koch(d - 1, p1, s)
print('{:.8f} {:.8f}'.format(s.x, s.y))
koch(d - 1, s, u)
print('{:.8f} {:.8f}'.format(u.x, u.y))
koch(d - 1, u, t)
print('{:.8f} {:.8f}'.format(t.x, t.y))
koch(d - 1, t, p2)
p1 = Point(x=0, y=0)
p2 = Point(x=100, y=0)
print('{:.8f} {:.8f}'.format(p1.x, p1.y))
koch(N, p1, p2)
print('{:.8f} {:.8f}'.format(p2.x, p2.y))
| import sys
input = sys.stdin.buffer.readline
mod = 10**9+7
MAX_N = 10**7 + 10
fact = [1]
fact_inv = [0]*(MAX_N+4)
for i in range(MAX_N+3):
fact.append(fact[-1]*(i+1)%mod)
fact_inv[-1] = pow(fact[-1],mod-2,mod)
for i in range(MAX_N+2,-1,-1):
fact_inv[i] = fact_inv[i+1]*(i+1)%mod
def mod_comb_k(n,k,mod):
return fact[n] * fact_inv[k] % mod * fact_inv[n-k] %mod
k = int(input())
s = list(input())
n = len(s)-1
res = 0
for i in range(n,n+k+1):
res = (res + (mod_comb_k(i-1,n-1,mod)*pow(25,i-n,mod)%mod)*pow(26,n+k-i,mod))%mod
print(res) | 0 | null | 6,456,367,519,380 | 27 | 124 |
while 1:
x, y = map(int, raw_input().split())
if x == y == 0:
break
else:
if x < y:
print "%d %d" % (x, y)
else:
print "%d %d" % (y, x) | while True :
x, y = map(int, raw_input().split(" "))
if x==0 and y==0 :
break
if x < y :
print "%d %d" % (x, y)
else :
print "%d %d" % (y, x) | 1 | 526,996,637,340 | null | 43 | 43 |
from decimal import Decimal
import sys
A,B = map(Decimal,input().split())
ans = int(A*B)
print(ans) | import decimal
a,b=map(str,input().split())
RES = decimal.Decimal(a)*decimal.Decimal(b)
res = int(RES)
print(res) | 1 | 16,703,676,800,682 | null | 135 | 135 |
text = input()
q = int(input())
for _ in range(q):
ops = input().split()
if len(ops) == 3:
op, a, b = ops
else:
op, a, b, p = ops
a = int(a)
b = int(b)
if op == 'print':
print(text[a:b+1])
elif op == 'reverse':
text = text[:a] + ''.join(reversed(text[a:b+1])) + text[b + 1:]
else:
text = text[:a] + p + text[b + 1:] |
s = str(input())
q = int(input())
for i in range(q):
arr = list(map(str, input().split()))
c = arr[0]
n1 = int(arr[1])
n2 = int(arr[2])
if c == 'print':
print(s[n1:n2 + 1])
if c == 'replace':
s = s[0:n1] + arr[3] + s[n2 + 1:len(s)]
if c == 'reverse':
l = n2 - n1 + 1
reverse = ''
for i in range(l):
reverse += s[n2 - i]
s = s[0:n1] + reverse + s[n2 + 1:len(s)]
| 1 | 2,105,647,377,468 | null | 68 | 68 |
import math
import numpy as np
n,k=(int(x) for x in input().split())
a = [int(x) for x in input().split()]
for _ in range(k):
new_a = np.zeros(n+1,dtype=int)
np.add.at(new_a, np.maximum(0,np.arange(0, n)-a), 1)
np.add.at(new_a, np.minimum(n,np.arange(0, n)+a+1), -1)
a = new_a.cumsum()[:-1]
if np.all(a==n):
break
print(" ".join([str(x) for x in a])) | from itertools import accumulate
N, K, *A = map(int, open(0).read().split())
for _ in range(min(K, 41)):
next_A = [0] * (N + 1)
for i, a in enumerate(A):
next_A[max(0, i - a)] += 1
next_A[min(N, i + a + 1)] -= 1
A = list(accumulate(next_A))
A.pop()
print(*A) | 1 | 15,444,018,596,262 | null | 132 | 132 |
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T =input()
com = [0]*N
for i in range(N):
if i>=K and T[i]==T[i-K] and com[i-K]!=0:
continue
elif T[i] =="r":
com[i] =P
elif T[i] =="s":
com[i] =R
else:
com[i] =S
print(sum(com))
| n = int(input())
s = list(input().split())
q = int(input())
t = list(input().split())
ans = 0
for num in t:
if num in s:
ans += 1
print(ans)
| 0 | null | 53,341,912,873,382 | 251 | 22 |
n = int(input())
a = list(map(int, input().split()))
a.sort()
m = a[-1]
c = [0] * (m + 1)
for ai in a:
for i in range(ai, m + 1, ai):
c[i] += 1
ans = 0
for ai in a:
if c[ai] == 1:
ans += c[ai]
print(ans) | import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
arr = [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]
n = INT()
print(arr[n - 1]) | 0 | null | 32,118,750,698,188 | 129 | 195 |
from collections import deque
n,limit = map(int,input().split())
total_time = 0
queue = deque()
for _ in range(n):
p,t = input().split()
queue.append([p,int(t)])
while len(queue) > 0:
head = queue.popleft()
if head[1] <= limit:
total_time += head[1]
print('{} {}'.format(head[0],total_time))
else:
head[1] -= limit
total_time += limit
queue.append(head)
| n = int(input())
l = [list(map(int, input().split())) for i in range(n)]
#print(l[0][0])
#print(l[0][1])
ans = n-2
ans1 = 0
for m in range(ans):
if l[m][0] == l[m][1] and l[m+1][0] == l[m+1][1] and l[m+2][0] == l[m+2][1]:
ans1 += 1
if ans1 >= 1:
print("Yes")
else:
print("No") | 0 | null | 1,247,862,443,520 | 19 | 72 |
import math
while True:
try:
a = list(map(int,input().split()))
b = (math.gcd(a[0],a[1]))
c = ((a[0]*a[1])//math.gcd(a[0],a[1]))
print(b,c)
except EOFError:
break
| A,B=map(int,input().split())
for i in range(1,B+1):
if((A*i)%B==0):
print(A*i)
exit() | 0 | null | 56,685,290,749,348 | 5 | 256 |
def resolve():
k=int(input())
list=[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(list[k-1])
resolve() | cards = []
mark = ["S","H","C","D"]
for i in range(4):
mk = mark[i]
for j in range(1,14):
cards.append("{} {}".format(mk,j))
n = int(input())
for _ in range(n):
card = input()
cards.remove(card)
for i in cards:
print(i) | 0 | null | 25,727,156,162,022 | 195 | 54 |
n, m, l = list(map(int, input().split()))
a = [[int(j) for j in input().split()] for i in range(n)]
b = [[int(j) for j in input().split()] for i in range(m)]
c = [[0 for j in range(l)] for i in range(n)]
for i in range(n):
for j in range(l):
value = 0
for k in range(m):
value = value + a[i][k] * b[k][j]
c[i][j] = value
for i in c:
print(*i)
| (n, m, l) = [int(i) for i in input().split()]
a = []
b = []
for i in range(n):
a.append([int(i) for i in input().split()])
for j in range(m):
b.append([int(j) for j in input().split()])
for x in range(n):
c = []
for y in range(l):
d = 0
for z in range(m):
d += a[x][z] * b[z][y]
c.append(str(d))
print(' '.join(c)) | 1 | 1,439,934,752,400 | null | 60 | 60 |
def solve():
N = int(input())
X = input()
X_10 = int(X,2)
r = X.count("1")
r0 = r-1
r1 = r+1
ans = []
s0 = X_10 % r0 if r != 1 else None
s1 = X_10 % r1
for i in range(N):
if X[i] == '0':
ans.append(f((s1 + pow(2,N-1-i,r1)) % r1))
else:
if r0 == 0:
ans.append(0)
else:
ans.append(f((s0 - pow(2,N-1-i,r0)) % r0))
print(*ans, sep='\n')
def f(N):
cnt = 1
while N != 0:
N = N % popcount(N)
cnt += 1
return cnt
def popcount(N):
b = bin(N)
cnt = 0
for i in range(2,len(b)):
if b[i] == '1':
cnt += 1
return cnt
if __name__ == '__main__':
solve() | def popcount(n):
ret = 0
while n > 0:
ret += n & 1
n >>= 1
return ret
def f(n):
ret = 0
while n > 0:
n %= popcount(n)
ret += 1
return ret
def mod(x, d):
'''
x % d where x: string, d: int
'''
rem = 0
for i in range(len(x)):
divd = (rem << 1) + int(x[i], 2)
rem = divd % d
return rem
N = int(input())
X = input()
popx = X.count('1')
qp = mod(X, popx + 1) # X mod (popcount(X) + 1)
if popx - 1 > 0:
qm = mod(X, popx - 1) # X mod (popcount(X) - 1)
ans = []
pow2p = pow2m = 1
for i in range(N):
if X[N - 1 - i] == '1':
if popx == 1: # X_iが0
ans.append(0)
else:
next = (qm - pow2m) % (popx - 1)
ans.append(1 + f(next))
else: # X[N - 1 - i] == '0'
next = (qp + pow2p) % (popx + 1)
ans.append(1 + f(next))
pow2p = pow2p * 2 % (popx + 1)
if popx > 1:
pow2m = pow2m * 2 % (popx - 1)
for i in range(N - 1, -1, -1):
print(ans[i]) | 1 | 8,300,766,093,898 | null | 107 | 107 |
import math
def koch(n, p1_x, p1_y, p2_x, p2_y):
if n == 0:
print(f"{p1_x:.8f} {p1_y:.8f}")
return
# p1, p2からs, t, uを計算
# 与えられた線分(p1, p2) を3等分点s、tを求める
s_x = (2 * p1_x + 1 * p2_x) / 3
s_y = (2 * p1_y + 1 * p2_y) / 3
t_x = (p1_x + 2 * p2_x) / 3
t_y = (p1_y + 2 * p2_y) / 3
# 線分を3等分する2点 s,t を頂点とする正三角形 (s,u,t) を作成する
u_x = (
(t_x - s_x) * math.cos(math.pi / 3) - (t_y - s_y) * math.sin(math.pi / 3) + s_x
)
u_y = (
(t_x - s_x) * math.sin(math.pi / 3) + (t_y - s_y) * math.cos(math.pi / 3) + s_y
)
# 線分 (p1,s)、線分 (s,u)、線分 (u,t)、線分 (t,p2)に対して再帰的に同じ操作を繰り返す
koch(n - 1, p1_x, p1_y, s_x, s_y)
koch(n - 1, s_x, s_y, u_x, u_y)
koch(n - 1, u_x, u_y, t_x, t_y)
koch(n - 1, t_x, t_y, p2_x, p2_y)
n = int(input())
koch(n, 0, 0, 100, 0)
print("100.00000000 0.00000000")
| 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, log
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 fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
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 ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *
N = INT()
ab = [LIST() for _ in range(N-1)]
graph = [[] for _ in range(N)]
for a, b in ab:
graph[a-1].append(b-1)
graph[b-1].append(a-1)
length = [len(x) for x in graph]
ans = max(length)
color = defaultdict(int)
def coloring(n, previous, col): #頂点nにpreviousから色colで辿ってきた。
cnt = 1
for x in graph[n]:
if x == previous:
color[(n, x)] = col
color[(x, n)] = col #やってきた頂点との間における色
else:
if cnt == col:
cnt += 1
color[(x, n)] = cnt
color[(n, x)] = cnt
coloring(x, n, cnt)
cnt += 1
coloring(0, -1, 0)
print(ans)
for a, b in ab:
print(color[(a-1, b-1)]) | 0 | null | 67,893,010,332,380 | 27 | 272 |
N = int(input())
A = list(map(int, input().split()))
M = 1000
S = 0
for i in range(N-1):
if A[i] < A[i+1]:
S = M // A[i]
M = M % A[i] + S*A[i+1]
S = 0
else:
pass
print(M) | from collections import deque
N, M = map(int, input().split())
Mlist = [[] for _ in range(N+1)]
for i in range(M):
a, b = map(int, input().split())
Mlist[a].append(b)
Mlist[b].append(a)
ans = [0] * (N+1)
d = deque()
d.append(1)
count = 0
while count != N:
Q = d.popleft()
for i in Mlist[Q]:
if ans[i] == 0:
ans[i] = Q
d.append(i)
count = count+1
print('Yes')
for i in range(N-1):
print(ans[i+2])
| 0 | null | 13,930,085,097,698 | 103 | 145 |
from sys import stdin
def main():
#入力
readline=stdin.readline
A,B=map(int,readline().split())
C=max(0,A-B*2)
print(C)
if __name__=="__main__":
main() | A,B=(int(x) for x in input().split())
val = A-B*2
print(max(0,val)) | 1 | 167,101,808,024,548 | null | 291 | 291 |
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
#import bisect
#from collections import deque
import math
sys.setrecursionlimit(10**5)
def input():
return sys.stdin.readline().rstrip('\n')
N = int(input())
peers = [[] for _ in range(N)]
colors = [{} for _ in range(N)]
pairs = []
for _ in range(N-1):
a,b = list(map(int, input().split()))
a -= 1
b -= 1
pairs.append([a,b])
peers[a].append(b)
colors[a][b] = 0
def draw(node, parent_edge_color):
_peers = peers[node]
if len(_peers) == 0:
return 0
color = 1
max_nrof_color = 1
for peer in _peers:
#print(node,peer,color)
if color == parent_edge_color:
color +=1
colors[node][peer] = color
max_nrof_color = max(max_nrof_color, color, draw(peer, color))
color += 1
return max_nrof_color
print(draw(0,0))
for a,b in pairs:
print(colors[a][b])
| from collections import deque
N=int(input())
G=[{} for i in range(N+1)]
colors = []
for i in range(N-1):
a,b=map(int,input().split())
G[a][b]=[i,-1]
G[b][a]=[i,-1]
def bfs(s):
seen = [0 for i in range(N+1)]
prev = [0 for i in range(N+1)]
todo = deque([])
cmax = 0
now = s
seen[now]=1
todo.append(now)
while 1:
if len(todo)==0:break
a = todo.popleft()
if len(G[a])<50:
if prev[a] == 0:
a_color=set([])
else:
a_color=set([G[a][prev[a]][1]])
for b in G[a]:
if seen[b] == 0:
seen[b] = 1
todo.append(b)
prev[b] = a
for c in range(1,N+1):
if c not in a_color:
a_color.add(c)
colors.append((G[a][b][0],c))
G[a][b][1]=G[b][a][1]=c
if c > cmax: cmax = c
break
else:
temp = list(range(1,N))
if prev[a] != 0:
del temp[G[a][prev[a]][1]-1]
temp = deque(temp)
for i,b in enumerate(G[a]):
if seen[b] == 0:
seen[b] = 1
todo.append(b)
prev[b] = a
c = temp.popleft()
colors.append((G[a][b][0],c))
G[a][b][1]=G[b][a][1]=c
if c > cmax: cmax = c
return colors, cmax
colors,cmax = bfs(1)
colors=sorted(colors)
print(cmax)
for i in range(N-1):
print(colors[i][1]) | 1 | 135,505,300,682,532 | null | 272 | 272 |
n = int(input())
if(n % 2 !=0):
print(0)
exit()
ketasu = len(str(n))
ans = 0
for i in range(1, 100):
ans += n // (2 * 5 **i)
print(ans) | X,Y = map(int,input().split())
ans = "No"
for i in range(X+1):
for j in range (X-i+1):
if i*2 + j*4 == Y and i + j == X:
ans = "Yes"
break
print(ans)
| 0 | null | 64,684,964,764,992 | 258 | 127 |
temp=int(input())
if(temp >= 30):
print("Yes")
else:
print("No") | #144_C
import math
n = int(input())
m = math.floor(math.sqrt(n))
div = 1
for i in range(1,m+1):
if n % i == 0:
div = i
print(int(div + n/div -2)) | 0 | null | 84,035,400,393,440 | 95 | 288 |
W = input().lower()
ans = 0
while True:
s = input()
if s == "END_OF_TEXT":
break
s = s.lower().split()
ans += s.count(W)
print(ans)
| R=int(input())
N=R*2*3.141592
print(N) | 0 | null | 16,551,180,962,040 | 65 | 167 |
INF = int(1e9)
def main():
n, m = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort()
s = [0]
for i in range(n):
s.append(s[i]+a[i])
#Xより大きい要素の数を数える
def counter(x):
count = 0
for i in range(n):
l = -1
r = n
while r-l>1:
mid = (l+r)//2
if a[i] + a[mid] > x:
r = mid
else:
l = mid
count += n-r
return count
#Xより大きい要素数がm個未満であるXのうち最大のものを探す
l = 0
r = INF
while r-l>1:
mid = (l+r)//2
if counter(mid) < m:
r = mid
else:
l = mid
#lより大きい要素の総和を求める
def sigma(x):
cnt = 0
sgm = 0
for i in range(n):
l = -1
r = n
while r-l >1:
mid = (l+r)//2
if a[i]+a[mid]>x:
r = mid
else:
l = mid
cnt += n-r
sgm += a[i] * (n-r) + (s[n]-s[r])
return (sgm, cnt)
sgm, cnt = sigma(l)
ans = sgm+(m-cnt)*r
print(ans)
main() | from bisect import bisect_left
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
def count(x):
ret = 0
for a in A:
ret += N - bisect_left(A, x - a)
return ret
overEq = 0
less = 10**7
while less - overEq > 1:
mid = (less + overEq) // 2
if count(mid) >= M:
overEq = mid
else:
less = mid
ans = 0
cnt = [0] * N
for a in A:
i = (N - bisect_left(A, overEq - a))
ans += i * a
if i > 0:
cnt[-i] += 1
for i in range(1, N):
cnt[i] += cnt[i - 1]
for a, c in zip(A, cnt):
ans += a * c
ans -= overEq * (count(overEq) - M)
print(ans)
| 1 | 108,085,263,921,060 | null | 252 | 252 |
x,y=map(int,input().split())
if y%2==1:
print('No')
elif y>=2*x and y<=4*x:
print('Yes')
else:
print('No') | from itertools import product
H, W, K = map(int, input().split())
choco = [list(input()) for _ in range(H)]
def cnt(array):
count = [0] * H
split_cnt = 0
for w in range(W):
for h in range(H):
if choco[h][w] == "1":
count[array[h]] += 1
if any(i > K for i in count):
split_cnt += 1
count = [0] * H
for h in range(H):
if choco[h][w] == "1":
count[array[h]] += 1
if any(i > K for i in count):
return 10 ** 20
return split_cnt
def get_array(array):
l = len(array)
ret = [0] * l
for i in range(1, l):
ret[i] = ret[i-1] + array[i]
return ret
ans = 10 ** 20
for p in product(range(2), repeat=H-1):
p = get_array([0]+list(p))
ans = min(ans, cnt(p)+max(p))
print(ans)
| 0 | null | 31,138,588,888,448 | 127 | 193 |
a, b = list(map(int, input().split()))
my_result = a * (a - 1) + b * (b - 1)
print(int(my_result / 2)) | n = int(input())
dp = [1] * (50)
for i in range(n):
dp[i + 2] = dp[i + 1] + dp[i]
print(dp[n])
| 0 | null | 22,793,785,280,552 | 189 | 7 |
import math
a, b = map(int, input().split())
if a%b == 0:
ans = a
elif b%a == 0:
ans = b
else:
ans = int(a*b / math.gcd(a, b))
print(ans) | def lcm(x, y):
return x / gcd(x, y) * y
def gcd(a, b):
if (b == 0):
return a
return gcd(b, a % b);
a, b = [int(i) for i in input().split()]
print(int(lcm(a, b))) | 1 | 113,649,310,774,490 | null | 256 | 256 |
import math
from math import gcd,pi,sqrt
INF = float("inf")
MOD = 10**9 + 7
import sys
sys.setrecursionlimit(10**6)
import itertools
import bisect
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def main():
n = i_input()
l = [[0]*10 for i in range(10)]
for i in range(1,n+1):
s = str(i)
head = int(s[0])
tail = int(s[-1])
l[head][tail] += 1
ans = 0
for i in range(1,10):
for j in range(i,10):
if i == j:
ans += l[i][j]**2
else:
one = l[i][j]
two = l[j][i]
ans += one*two*2
print(ans)
if __name__=="__main__":
main()
| S = input()
N = len(S)
s = S[:(N-1)//2]
t = S[(N+3)//2-1:]
if s == t[::-1] and s == s[::-1] and t == t[::-1]:
ans = "Yes"
else:
ans = "No"
print(ans) | 0 | null | 66,441,917,430,550 | 234 | 190 |
n,m,l = map(int,input().split())
g = [[float('inf')]*n for _ in range(n)]
for _ in range(m):
a,b,c = map(int,input().split())
a-=1
b-=1
g[a][b] = c
g[b][a] = c
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])
h = [[float('inf')]*n for _ in range(n)]
for i in range(n):
for j in range(n):
if g[i][j]<=l:
h[i][j] = 1
for k in range(n):
for i in range(n):
for j in range(n):
h[i][j] = min(h[i][j],h[i][k] + h[k][j])
Q = int(input())
for _ in range(Q):
s,t = map(int,input().split())
s -=1
t-=1
if h[s][t] ==float('inf'):
print(-1)
else:
print(h[s][t]-1)
| import sys
import itertools
# import numpy as np
import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
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, L = map(int, input().split())
dist = [[INF] * N for _ in range(N)]
for i in range(M):
a, b, c = map(int, input().split())
if c > L:
continue
a -= 1
b -= 1
dist[a][b] = c
dist[b][a] = c
Q = int(input())
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])
fuel = [[INF] * N for _ in range(N)]
for i in range(N):
for j in range(N):
fuel[i][j] = 1 if dist[i][j] <= L else INF
for k in range(N):
for i in range(N):
for j in range(N):
fuel[i][j] = min(fuel[i][j], fuel[i][k] + fuel[k][j])
for i in range(Q):
s,t = map(int, input().split())
if fuel[s - 1][t - 1] != INF:
print(fuel[s - 1][t - 1] - 1)
else:
print(-1) | 1 | 173,347,292,862,230 | null | 295 | 295 |
import math
a, b, n = map(int, input().split())
if n < b:
c = math.floor(a * n / b)
print(c)
else:
c = math.floor(a * (b-1)/ b)
print(c) | from itertools import accumulate,chain
n,*s=open(0).read().split()
t=[2*i.count("(")-len(i) for i in s]
st=[[min(accumulate(s_,lambda a,b: a+(1 if b=="(" else -1),initial=0)),t_] for s_,t_ in zip(s,t)]
now=0
for c,d in chain(sorted([x for x in st if x[1]>=0])[::-1],sorted([x for x in st if x[1]<0],key=lambda z:z[1]-z[0])[::-1]):
if now+c<0:
print("No");break
now+=d
else:
print("No" if now else "Yes") | 0 | null | 26,037,341,684,490 | 161 | 152 |
w,h=map(int,input().split())
print("{} {}".format(w*h, (w+h)*2)) | input()
s, t = input().split()
for i,j in zip(s,t):
print(i,j,sep='', end = '') | 0 | null | 56,472,087,734,880 | 36 | 255 |
S=input()
P=input()
I=S*2
if P in I:
print('Yes')
else:
print('No')
| s = input()
p = input()
ring = s * 2
if p in ring:
print("Yes")
else:
print("No")
| 1 | 1,753,072,870,560 | null | 64 | 64 |
a, b, c, k = map(int, input().split())
ans = 0
for i, j in zip([1, 0, -1], [a, b, c]):
x = min(k, j)
ans += i*x
k -= x
print(ans)
| import sys
A, B, C, K = list(map(int, input().split()))
ans = 0
if A > K:
print(K)
sys.exit()
ans += A
K -= A
if B > K:
print(ans)
sys.exit()
K -= B
if C > K:
print(ans - K)
sys.exit()
else:
print(ans - C) | 1 | 21,730,319,664,328 | null | 148 | 148 |
N = int(input())
S = dict()
for i in range(N):
s = input()
if s in S:
S[s] += 1
else:
S[s] = 1
max_val = max(S.values())
max_key = [key for key in S if S[key] == max_val]
print("\n".join(sorted(max_key)))
| def isPrimeNum(in_num):
if in_num <= 1:
return False
elif in_num == 2:
return True
elif in_num % 2 == 0:
return False
else:
if pow(2, in_num-1, in_num) == 1:
return True
else:
return False
num_len = int(input())
cnt = 0
for i in range(num_len):
num = int(input())
if isPrimeNum(num)==True:
cnt = cnt + 1
print(str(cnt)) | 0 | null | 34,820,221,187,268 | 218 | 12 |
while 1:
a = list(map(int, input().split()))
if a[0] or a[1]:
print(min(a), max(a))
else:
break
| list = []
while True:
line = raw_input().split(" ")
if line[0] == "0" and line[1] == "0":
break
line = map(int, line)
#print line
if line[0] > line[1]:
temp = line[0]
line[0] = line[1]
line[1] = temp
print " ".join(map(str, line)) | 1 | 519,455,682,680 | null | 43 | 43 |
def main():
k = int(input())
for _ in range(k):
print("ACL",end='')
print()
if __name__ == "__main__":
main() | k=int(input())
print("ACL"*k)
| 1 | 2,166,433,351,420 | null | 69 | 69 |
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
p = [i+Ai for i, Ai in zip(range(1, N+1), A)]
q = [j-Aj for j, Aj in zip(range(1, N+1), A)]
pc = Counter(p)
qc = Counter(q)
r = sum(pc[k]*qc[k] for k in pc.keys() & qc.keys())
print(r) | import collections
hoge = collections.deque()
n = int(input())
for _ in range(n):
commands = input().split()
if len(commands) == 1:
if commands[0] == 'deleteFirst':
hoge.popleft()
else:
hoge.pop()
else:
com, x = commands
if com == 'insert':
hoge.insert(0, x)
else:
if x in hoge:
hoge.remove(x)
print (' '.join(hoge))
| 0 | null | 13,016,779,815,318 | 157 | 20 |
in_sec = int(raw_input())
sec = in_sec%60
in_sec = (in_sec-sec)/60
min = in_sec%60
h = (in_sec-min)/60
print str(h) + ":" + str(min) + ":" + str(sec) | S = int(input())
h = S//3600
m = (S - (h*3600))//60
s = S - (h*3600 + m*60)
print("%d:%d:%d" %(h,m,s))
| 1 | 332,024,810,890 | null | 37 | 37 |
#!/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, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10**9 + 7
num_zero = 0
neg_list = []
pos_list = []
for a in A:
if a == 0:
num_zero += 1
elif a < 0:
neg_list.append(a)
else:
pos_list.append(a)
# 0にしかできない
if len(pos_list) + len(neg_list) < K:
print(0)
else:
min_num_neg = K - len(pos_list)
max_num_neg = min(K, len(neg_list))
# 負にしかできない
if min_num_neg == max_num_neg and min_num_neg % 2 == 1:
if num_zero > 0:
print(0)
return
ans = 1
neg_list.sort()
pos_list.sort(reverse=True)
for i in range(K):
use_pos = False
if len(neg_list) == 0 or len(pos_list) == 0:
if len(neg_list) == 0:
use_pos = True
else:
use_pos = False
elif abs(neg_list[-1]) < pos_list[-1]:
use_pos = False
else:
use_pos = True
if use_pos:
ans *= pos_list[-1]
pos_list.pop()
else:
ans *= neg_list[-1]
neg_list.pop()
ans %= MOD
print(ans)
else:
# 正にできる
ans = 1
neg_list.sort(reverse=True)
pos_list.sort()
if K % 2 == 1:
K -= 1
ans *= pos_list[-1]
pos_list.pop()
# posもnegも偶数個ずつ使う
for _ in range(0, K, 2):
use_pos = False
if len(neg_list) <= 1 or len(pos_list) <= 1:
if len(neg_list) <= 1:
use_pos = True
else:
use_pos = False
elif abs(neg_list[-1] * neg_list[-2]) > (pos_list[-1] * pos_list[-2]):
use_pos = False
else:
use_pos = True
if use_pos:
ans *= pos_list[-1] * pos_list[-2]
pos_list.pop()
pos_list.pop()
else:
ans *= neg_list[-1] * neg_list[-2]
neg_list.pop()
neg_list.pop()
ans %= MOD
print(ans)
def main():
solve()
if __name__ == '__main__':
main()
| """
9 5
-4 -3 -2 -1 0 1 2 3
"""
N,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
Am = []
Ap = []
for a in A:
if a < 0:
Am.append(a)
else:
Ap.append(a)
mod = 10**9 + 7
ans = 1
if N == K:
for a in A:
ans *= a
ans %= mod
elif A[-1] < 0:
if K % 2 == 1:
for i in range(K):
ans *= Am[-1-i]
ans %= mod
else:
for i in range(K):
ans *= Am[i]
ans %= mod
else:
if K%2 == 1:
ans *= Ap[-1]
ans %= mod
K -= 1
del Ap[-1]
Amd = []
Apd = []
for i in range(len(Ap)//2):
Apd.append(Ap[-1-(2*i)]*Ap[-1-(2*i+1)])
for i in range(len(Am)//2):
Amd.append(Am[2*i]*Am[2*i+1])
Apd.append(-1)
Amd.append(-1)
ip = 0
im = 0
while K > 0:
if Apd[ip] >= Amd[im]:
ans *= Apd[ip]
ip += 1
else:
ans *= Amd[im]
im += 1
ans %= mod
K -= 2
ans %= mod
print(ans)
| 1 | 9,361,039,803,040 | null | 112 | 112 |
while True:
m,f,r = map(int, input().split())
if(m==-1 or f == -1):
if (m == -1 and f == -1 and r == -1):
break
else:
print('F')
else:
score = m + f
if(score >= 80):
print('A')
elif(score >= 65):
print('B')
elif(score >= 50):
print('C')
elif(score >= 30):
if(r >= 50):
print('C')
else:
print('D')
else:
print('F')
| while True:
mid_score, term_score, re_score = map(int, input().split())
if mid_score == term_score == re_score == -1:
break
test_score = mid_score + term_score
score = ''
if test_score >= 80:
score = 'A'
elif test_score >= 65:
score = 'B'
elif test_score >= 50:
score = 'C'
elif test_score >=30:
if re_score >= 50:
score = 'C'
else:
score = 'D'
else:
score = 'F'
if mid_score == -1 or term_score == -1:
score = 'F'
print(score)
| 1 | 1,202,004,391,062 | null | 57 | 57 |
n, k = map(int, input().split())
if n > k:
n %= k
print(min(k-n, n)) | k = int(input())
ans = 0
s = 7
for i in range(1, 10**6 + 1):
s %= k
if s == 0:
print(i)
exit()
s = s*10 +7
print(-1) | 0 | null | 22,665,136,734,930 | 180 | 97 |
N = int(input())
X = str(input())
X10 = int(X,2)
one_num = X.count("1")
X10_pl = X10%(one_num+1)
if one_num>1:
X10_mi = X10%(one_num-1)
else:
X10_mi = 0
for i in range(N):
target = int(X[i])
if target == 0:
ones = one_num + 1
plus = pow(2,N-i-1,ones)
Xnow = (X10_pl + plus)%ones
else:
if one_num == 1:
print(0)
continue
ones = one_num-1
plus = -pow(2,N-i-1,ones)
Xnow = (X10_mi + plus)%ones
count=1
while Xnow > 0:
Xnow2 = bin(Xnow)
ones = Xnow2.count("1")
Xnow = Xnow%ones
count+=1
print(count)
| a,b,c,d,e,f=map(int,input().split())
x=input()
for i in range(len(x)):
if x[i]=="S":
a,b,c,d,e,f=e,a,c,d,f,b
if x[i]=="E":
a,b,c,d,e,f=d,b,a,f,e,c
if x[i]=="N":
a,b,c,d,e,f=b,f,c,d,a,e
if x[i]=="W":
a,b,c,d,e,f=c,b,f,a,e,d
print(a)
| 0 | null | 4,214,218,703,558 | 107 | 33 |
N = int(input())
A = list(map(int, input().split()))
D = {}
for i in range(N):
i+=1
D.update([(A[i-1], i)])
ans = []
for i in range(1, N+1):
ans.append(D[i])
str_ans = list(map(str, ans))
print(' '.join(str_ans)) | n = int(input())
num_list = list(map(int, input().split()))
ans_list = [0]*n
for i, j in enumerate(num_list):
ans_list[j-1] = str(i+1)
ans = ' '.join(ans_list)
print(ans) | 1 | 180,602,707,688,960 | null | 299 | 299 |
def solve():
N=n=int(input())
q=int(n**0.5)
ans=0
for d in range(2,q+1) :
a=0
while n % d == 0 :
a+=1
n//=d
if a>0 :
ans+=int(((a*8+1)**0.5-1)/2)
if n > 1 :
ans+=1
return ans
print(solve())
| import math
from decimal import *
import random
def pf(n):
rn = int(n)
ans = []
while(n%2==0):
if(2 not in ans):
ans.append(2)
n//=2
for i in range(3, int(math.sqrt(rn)), 2):
while(n%i==0):
if(i not in ans):
ans.append(i)
n//=i
if(n==1):
break
if(n==1):
break
if(n>2):
ans.append(n)
return sorted(ans)
n = int(input())
d = pf(n)
if(d=={}):
print(1)
else:
ans = 0
for i in range(len(d)):
cnt = 1
while(n >= d[i]**cnt):
if(n%(d[i]**cnt)==0):
n//=(d[i]**cnt)
ans+=1
cnt+=1
print(ans)
| 1 | 17,058,117,310,212 | null | 136 | 136 |
N = int(input())
A = [int(a) for a in input().split(" ")]
swapnum = 0
for i in range(N):
mini = i
swap = False
for j in range(i, N):
if A[j] < A[mini]:
mini = j
swap = True
if swap:
tmp = A[i]
A[i] = A[mini]
A[mini] = tmp
swapnum += 1
print(" ".join([str(a) for a in A]))
print(swapnum) | def insection_sort(lst):
lens=len(lst)
ans=0
for i in xrange(lens):
mn=i
for j in xrange(i+1,lens):
if(lst[j]<lst[mn]):
mn=j
if(mn!=i):
tmp=lst[i]
lst[i]=lst[mn]
lst[mn]=tmp
ans+=1
return ans
num=int(raw_input().strip())
arr=map(int,raw_input().strip().split())
ans=insection_sort(arr)
print " ".join(str(pp) for pp in arr)
print ans
| 1 | 19,123,835,370 | null | 15 | 15 |
num = map(int,input().split())
s=sum(num)
print(15-s) | A = list(map(int, input().split()))
for i in range(len(A)):
if A[i] == 0: print(i+1) | 1 | 13,387,248,146,988 | null | 126 | 126 |
n = input()
print("Yes" if n == "hi" * (len(n)// 2) else "No") | import re
print("No" if re.fullmatch("(hi)+",input())==None else "Yes") | 1 | 53,437,799,073,290 | null | 199 | 199 |
def myAnswer(N:int,A:int,B:int) -> int:
ans = 0
BN = N - B
A1 = A - 1
while True:
if((B - A) % 2== 0):
ans += (B - A)//2
break
elif(A == 1):
ans += 1
B -= 1
elif(B == N):
ans += 1
A += 1
else:
if(BN > A1):
ans +=A1
B -= A1
A = 1
else:
ans += BN
A += BN
B = N
return ans
def modelAnswer():
tmp=1
def main():
N,A,B = map(int,input().split())
print(myAnswer(N,A,B))
if __name__ == '__main__':
main() | # 1 <= N <= 1000000
N = int(input())
total = []
# N項目までに含まれる->N項目は含まない。だからN項目は+1で外す。
for x in range(1, N+1):
if x % 15 == 0:
"FizzBuzz"
elif x % 5 == 0:
"Buzz"
elif x % 3 == 0:
"Fizz"
else:
total.append(x) #リストに加える
print(sum(total)) | 0 | null | 72,124,770,128,288 | 253 | 173 |
from collections import Counter
input()
a = list(map(int, input().split()))
c = Counter(a)
d = {i: (i * (i - 1)) // 2 for i in set(c.values())}
s = sum(d[i] for i in c.values())
for k in d:
d[k] = s - d[k] + ((k - 1) * (k - 2) // 2)
d[1] = s
for i in a:
print(d[c[i]]) | import collections
N = int(input())
A = list(map(int,input().split()))
Cn_A = collections.Counter(A)
ans_list = [0]*len(Cn_A)
ans = 0
for i,j in enumerate(Cn_A.values()):
ans+=j*(j-1)//2
for i in A:
j = Cn_A[i]
print(ans-(j*(j-1)//2)+((j-1)*(j-2)//2))
| 1 | 48,049,986,638,710 | null | 192 | 192 |
while True:
a=input()
if a=='0': break
print(sum(int(i) for i in a)) | import sys
# input = sys.stdin.readline
input = lambda: sys.stdin.readline().rstrip()
n = int(input())
s = [input() for _ in range(n)]
def bracket(x):
# f: final sum of brackets '(':+1, ')': -1
# m: min value of f
f = m = 0
for i in range(len(x)):
if x[i] == '(':
f += 1
else:
f -= 1
m = min(m, f)
# m <= 0
return f, m
def func(l):
# l = [(f, m)]
l.sort(key=lambda x: -x[1])
v = 0
for fi, mi in l:
if v + mi >= 0:
v += fi
else:
return -1
return v
l1 = []
l2 = []
for i in range(n):
fi, mi = bracket(s[i])
if fi >= 0:
l1.append((fi, mi))
else:
l2.append((-fi, mi - fi))
v1 = func(l1)
v2 = func(l2)
if v1 == -1 or v2 == -1:
ans = 'No'
else:
ans = 'Yes' if v1 == v2 else 'No'
print(ans)
| 0 | null | 12,606,376,766,078 | 62 | 152 |
N=int(input())
S={}
for n in range(N):
s=input()
S[s]=S.get(s,0)+1
maxS=max(S.values())
S=[k for k,v in S.items() if v==maxS]
print('\n'.join(sorted(S)))
| c={}
N=int(input())
for i in range(N):
s=input()
if s not in c:
c[s]=1
else:
c[s]+=1
n=max(c.values())
L=list()
for i in c:
if c[i]==n:
L.append(i)
L=sorted(L)
for i in L:
print(i) | 1 | 69,871,076,788,360 | null | 218 | 218 |
def main():
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
#from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil
#from operator import itemgetter
#inf = 10**17
#mod = 10**9 + 7
x,y = map(int, input().split())
a = 0
b = 0
if x==1:
a += 300000
if x==2:
a += 200000
if x==3:
a += 100000
if y==1:
b += 300000
if y==2:
b += 200000
if y==3:
b += 100000
if a+b == 600000:
print(1000000)
else:
print(a+b)
if __name__ == '__main__':
main() | x,y=map(int,input().split())
d={3:100000,2:200000,1:300000}
r=0
if x<=3:
r+=d[x]
if y<=3:
r+=d[y]
if x+y==2:
r+=400000
print(r) | 1 | 140,844,207,180,590 | null | 275 | 275 |
n=int(input())
x=list(map(int,input().split()))
m=10**15
for i in range(101):
t=x[:]
s=sum(list(map(lambda x:(x-i)**2,t)))
m=min(m,s)
print(m) | def nearest_int(f):
if f - int(f) < 0.5:
return int(f)
else:
return int(f) + 1
N = int(input())
X = list(map(lambda x: int(x), input().split(" ")))
ave = sum(X) / N
avei = nearest_int(ave)
print(sum([(x - avei) ** 2 for x in X])) | 1 | 65,312,045,834,058 | null | 213 | 213 |
h,w,k = map(int, input().split())
ls = [list(input()) for i in range(h)]
ansls = [[0]*w for _ in range(h)]
absent = []
idx = 1
for i in range(h):
if ls[i] == ['.']*w:
absent.append(i)
else:
count=0
for j in range(w):
if ls[i][j] == "#":
if count>0:
idx+=1
count+=1
ansls[i][j] = idx
idx+=1
for i in absent:
idx = i
while idx in absent:
idx+=1
if idx>=h:
idx = i
while idx in absent:
idx-=1
ansls[i] = ansls[idx]
for l in ansls:
print(*l) | def main():
x, y, a, b, c = list(map(int, input().split()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = list(map(int, input().split()))
P = sorted(P, reverse = True)[:x]
Q = sorted(Q, reverse = True)[:y]
ans = sorted(P + Q + R, reverse = True)
print(sum(ans[:x + y]))
if __name__ == '__main__':
main()
| 0 | null | 94,084,139,848,608 | 277 | 188 |
#16D8101023J 久保田凌弥 kubotarouxxx python3
x,y=map(int, input().split())
if x>y:
while 1:
if (x%y)==0:
print(y)
break
a=x%y
x=y
y=a
else:
while 1:
if (y%x)==0:
print(x)
break
a=y%x
y=x
x=a
| x,y = map(int, raw_input().split())
while y > 0:
x,y = y,x%y
print x | 1 | 7,809,255,840 | null | 11 | 11 |
N = input()
lenN = len(N)
sumN = 0
for x in N:
sumN += int(x)
if sumN > 9:
sumN %= 9
if sumN % 9 == 0:
print('Yes')
else:
print('No') | #176-B
N = input()
sum = 0
for i in range(1,len(N)+1):
sum += int(N[-i])
if int(sum) % 9 == 0:
print('Yes')
else:
print('No')
| 1 | 4,354,101,282,062 | null | 87 | 87 |
a= int(input())
h = a//3600
m = (a%3600)//60
s = (a%3600)%60
print('%d:%d:%d' % (h, m, s))
|
x=input()
l=(x/60)
h=(l/60)
m=(l-h*60)
s=x-m*60-h*60*60
print str(h)+":"+str(m)+":"+str(s) | 1 | 328,711,545,190 | null | 37 | 37 |
n = int(input()) % 10
if n == 3:
print('bon')
elif n == 0 or n == 1 or n == 6 or n == 8:
print('pon')
else:
print('hon') | def main():
n = int(input())
if n % 10 == 3:
print("bon")
return
elif n % 10 == 0 or n % 10 == 1 or n % 10 == 6 or n % 10 == 8:
print("pon")
return
else:
print("hon")
return
main() | 1 | 19,111,621,224,822 | null | 142 | 142 |
#!/usr/bin/env python3
import sys
from itertools import chain
def solve(A: int, B: int):
answer = A * B
return answer
def main():
tokens = chain(*(line.split() for line in sys.stdin))
# A, B = map(int, line.split())
A = int(next(tokens)) # type: int
B = int(next(tokens)) # type: int
answer = solve(A, B)
print(answer)
if __name__ == "__main__":
main()
|
def fibonacciDP(i):
if i == 0:
return 1
elif i == 1:
return 1
else:
if dp[i-1] is None and dp[i-2] is None:
dp[i-1] = fibonacciDP(i-1)
dp[i-2] = fibonacciDP(i-2)
elif dp[i-1] is None:
dp[i-1] = fibonacciDP(i-1)
elif dp[i-2] is None:
dp[i-2] = fibonacciDP(i-2)
return dp[i-1]+ dp[i-2]
def fibonacci(i):
if i == 0:
return 1
elif i == 1:
return 1
else:
return fibonacci(i-1)+fibonacci(i-2)
n = int(input())
dp = [None]*n
print(fibonacciDP(n)) | 0 | null | 7,822,334,693,782 | 133 | 7 |
string = input()
if string == "ABC":
print("ARC")
else:
print("ABC") | n,m = map(int,raw_input().split(" "))
a = []
for i in range(0,n):
b = map(int,raw_input().split(" "))
a.append(b)
for j in range(0,m):
k = input()
for i in range(0,n):
a[i][j] = a[i][j] * k
for i in range(0,n):
s = 0
for j in range(0,m):
s = s + a[i][j]
print str(s) | 0 | null | 12,555,260,515,204 | 153 | 56 |
def print_frame(h, w):
"""
h: int(3 <= h <= 300)
w: int(3 <= w <= 300)
outputs a rectangle frame h x w
>>> print_frame(3, 4)
####
#..#
####
>>> print_frame(5, 6)
######
#....#
#....#
#....#
######
>>> print_frame(3, 3)
###
#.#
###
"""
print('#' * w)
for i in range(h-2):
print('#' + '.'*(w-2) + '#')
print('#' * w)
if __name__ == '__main__':
while True:
(h, w) = [int(i) for i in input().split(' ')]
if h == 0 and w == 0:
break
print_frame(h, w)
print() | n = int(input())
G = {}
D = [-1] * n
for _ in range(n):
A = list(map(int, input().split()))
G[A[0] - 1] = [a - 1 for a in A[2:]]
# print(G)
q = [(0, 0)]
while len(q) > 0:
v, d = q[0]
del q[0]
if D[v] >= 0:
continue
D[v] = d
for nv in G[v]:
q.append((nv, d+1))
for i in range(n):
print(i+1, D[i])
| 0 | null | 411,165,216,132 | 50 | 9 |
import math
x = list(map(int,input().split()))
A = x[0]
B = x[1]
H = x[2]
M = x[3]
y = 2*math.pi*((H*60+M)/720-M/60)
z = math.sqrt(A**2+B**2-2*A*B*math.cos(y))
print(z) | #!/usr/bin/env python3
import collections as cl
import sys
def II():
return int(sys.stdin.readline())
def MI():
return map(int, sys.stdin.readline().split())
def LI():
return list(map(int, sys.stdin.readline().split()))
def main():
N = II()
counter = cl.Counter()
for i in range(N):
s = input()
counter.update({s: 1})
item, value = zip(*counter.most_common())
ans = []
max_num = value[0]
for i in range(len(value)):
if value[i] == max_num:
ans.append(item[i])
ans.sort()
print(*ans, sep="\n")
main()
| 0 | null | 45,102,171,770,140 | 144 | 218 |
import math
import collections
#2,4,5,7,9 hon
#0,1,6,8 pon
#3, bon
N = int(input())
a = N%10
B = [0,1,6,8]
if a == 3:
print('bon')
elif a in B:
print('pon')
else:
print('hon') | from fractions import gcd
import sys
sys.setrecursionlimit(10**7)
a,b = map(int,input().split())
def lcm(x, y):
return (x * y) // gcd(x, y)
if b != 0:
print(lcm(a, b))
else:
print(a)
| 0 | null | 66,073,507,750,150 | 142 | 256 |
n,m = map(int,input().split())
graph = [[] for _ in range(n)]
h = list(map(int,input().split()))
for _ in range(m):
a,b = map(int,input().split())
graph[a-1].append(b-1)
graph[b-1].append(a-1)
cnt = 0
for i in range(n):
if graph[i] == []:
cnt += 1
continue
if max([h[j] for j in graph[i]]) < h[i] : cnt += 1
print(cnt) | h,w,k=map(int, input().split())
s=[list(input()) for _ in range(h)]
ans=[[0]*w for i in range(h)]
cnt=1
for i in range(h):
for j in range(w):
if s[i][j]=="#":
ans[i][j]=cnt
cnt+=1
for i in range(h):
for j in range(w):
if ans[i][j]==0:
if j!=0:
ans[i][j]=ans[i][j-1]
for i in range(h):
for j in range(w-1, -1, -1):
if ans[i][j]==0:
if j!=(w-1):
ans[i][j]=ans[i][j+1]
# for i in range(h):
# print(*ans[i])
# print()
for i in range(h):
for j in range(w):
if ans[i][j]==0:
if i!=0:
ans[i][j]=ans[i-1][j]
for i in range(h-1, -1, -1):
for j in range(w):
if ans[i][j]==0:
if i!=(h-1):
ans[i][j]=ans[i+1][j]
for i in range(h):
print(*ans[i])
| 0 | null | 84,079,118,612,608 | 155 | 277 |
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
from collections import deque
from collections import defaultdict
def main():
N, P = MI()
S_ = S()
mod_list = defaultdict(int)
ans = 0
if P == 2 or P == 5:
for i in range(N):
num = int(S_[i])
if num % P == 0:
ans += i + 1
else:
num = 0
point = 1
for i in S_[::-1]:
num += int(i) * point
mod = int(num) % P
mod_list[mod] += 1
point *= 10
point %= P
values_ = mod_list.values()
ans += mod_list[0]
for value in values_:
ans += (value - 1) * value // 2
print(ans)
if __name__ == "__main__":
main()
| import bisect
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a_sum = []
b_sum = []
sm = 0
for a in A:
sm += a
if sm > K:
break
a_sum.append(sm)
sm = 0
for b in B:
sm += b
if sm > K:
break
b_sum.append(sm)
index_max = 0
# a_size = len(a_sum)
# for i in range(a_size):
while a_sum:
a_size = len(a_sum)
b_index = bisect.bisect_right(b_sum, K - a_sum.pop())
index_max = max(index_max, a_size + b_index)
# print(a_size, b_index)
b_index = bisect.bisect_right(b_sum, K)
index_max = max(index_max, b_index)
print(index_max) | 0 | null | 34,649,738,684,330 | 205 | 117 |
n = int(input())
index = set()
for _ in range(n):
command, string = input().split()
if command == "insert":
index.add(string)
else:
if string in index:
print("yes")
else:
print("no")
| N=int(input())
for i in range(N+1):
if int(i*1.08)==N:
print(i)
break
if all(int(i*1.08)!=N for i in range(N+1))==True:
print(":(") | 0 | null | 62,715,773,598,752 | 23 | 265 |
n = int(input())
print((n+1)//2)
| n = input()
n = int(n)
if(n==2):
print(1)
elif(n%2==0):
print(int(n/2))
else:
print(int(n/2)+1) | 1 | 59,036,815,604,060 | null | 206 | 206 |
while True:
d = input()
if d == '-':
break
m = int(input())
h = [int(input()) for _ in range(m)]
for i in h:
d = d[i:] + d[:i]
print(d)
| import itertools
N = int(input())
tbl = [0]*N
for x in range(1,N):
for y in range(1,N):
for z in range(1,N):
p = x*x + y*y + z*z + x*y + y*z + z*x
if p > N:
break
tbl[p-1] += 1
for i in range(N):
print(tbl[i])
| 0 | null | 4,964,350,819,682 | 66 | 106 |
def gcd(x, y):
if y == 0:
return x
else:
return gcd(y, x % y)
def main():
l, r = tuple(map(int, input().split(' ')))
print(gcd(l, r))
main()
| import sys
import math
def gcd(x, y):
if y == 0:
return x
return gcd(y, x%y)
x, y = map(int, raw_input().split())
print gcd(min(x, y), max(x, y)) | 1 | 8,044,484,932 | null | 11 | 11 |
a,b,c=map(int,input().split())
if a>b:
n=a
a=b
b=n
else:
pass
if b>c:
n=b
b=c
c=n
else:
pass
if a>b:
n=a
a=b
b=n
else:
pass
print(a,b,c)
| #! /usr/bin/env python
# -*- coding: utf-8 -*-
raw_list = sorted(map(int, raw_input().split()))
print("%d %d %d") % (raw_list[0], raw_list[1], raw_list[2]) | 1 | 416,323,254,816 | null | 40 | 40 |
line = input()
q = int(input())
for _ in range(q):
cmd = input().split()
a = int(cmd[1])
b = int(cmd[2])
if cmd[0] == "print":
print(line[a:b + 1])
elif cmd[0] == "replace":
line = line[:a] + cmd[3] + line[b + 1:]
else:
line = line[:a] + line[a:b + 1][::-1] + line[b + 1:] | NM = input().split()
n = int(NM[0])
m = int(NM[1])
A = []
B = []
for i in range(n):
A.append([])
a = input().split()
for j in range(m):
A[i].append(int(a[j]))
for i in range(m):
b = int(input())
B.append(b)
out = []
for i in range(n):
sum = 0
for j in range(m):
sum = sum + A[i][j]*B[j]
out.append(sum)
for i in range(n):
print(out[i]) | 0 | null | 1,596,936,815,250 | 68 | 56 |
N = int(input())
for i in range(1,int(N**(1/2)) + 1)[::-1]:
if N%i == 0:
print(i + N//i - 2)
exit() | while 1:
H, W = map(int,raw_input().split())
if H ==0 and W == 0:
break
for i in range(H):
if i%2==0:
if W%2==0:
print "#."*(W/2)
elif W%2==1:
print "#."*(W/2)+"#"
elif i%2==1:
if W%2==0:
print ".#"*(W/2)
elif W%2==1:
print ".#"*(W/2)+"."
print "" | 0 | null | 81,583,866,879,742 | 288 | 51 |
from math import sqrt
n = int(input())
minSoFar = 10**12+1
for a in range(1, int(sqrt(n))+1):
if n % a == 0:
b = n // a
minSoFar = min(minSoFar, a+b-2)
print(minSoFar) | # import itertools
import math
# import sys
# import numpy as np
# K = int(input())
# S = input()
# n, *a = map(int, open(0))
A, B, H, M = map(int, input().split())
# H = list(map(int, input().split()))
# Q = list(map(int, input().split()))
# S = input()
# d = sorted(d.items(), key=lambda x:x[0]) # keyでsort
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement([i for i in range(1, M + 1)], N))
# print(a[0][0])
# print(conditions[0])
kakudo_H = (H * 60 + M) / 720 * 360
kakudo_M = M / 60 * 360
print(math.sqrt(A ** 2 + B ** 2 - 2 * A * B * math.cos(math.radians(abs(kakudo_H - kakudo_M))))) | 0 | null | 91,091,007,447,852 | 288 | 144 |
#!/usr/bin/env python
import queue
def dfs(node, edge, vis):
vis[node] = True
count = 1
vq = []
vq.append(node)
vq = queue.LifoQueue()
vq.put(node)
while not vq.empty():
fnode = vq.get()
for b in edge[fnode]:
if not vis[b]:
count += 1
vq.put(b)
vis[b] = True
return count
n, m = [int(i) for i in input().split(' ')]
edge = [[] for i in range(n)]
for _ in range(m):
a, b = [int(i) for i in input().split(' ')]
edge[a-1].append(b-1)
edge[b-1].append(a-1)
vis = [False]*n
count = 0
for i in range(n):
if not vis[i]:
count = max(count, dfs(i, edge, vis))
print(count)
| import sys
from collections import deque
n = int(sys.stdin.readline().strip())
edges = [[] for _ in range(n)]
for _ in range(n):
tmp = list(map(int, sys.stdin.readline().strip().split(" ")))
for node in tmp[2:]:
edges[tmp[0] - 1].append(node-1)
# print(edges)
distance = [0] * n
q = deque()
q.append((0, 0))
visited = set()
while q:
node, d = q.popleft()
# print(node, d)
if node in visited:
continue
distance[node] = d
visited.add(node)
for next in edges[node]:
# print("next", next)
q.append((next, d + 1))
for i, d in enumerate(distance):
if i == 0:
print(1, 0)
else:
print(i + 1, d if d > 0 else -1)
| 0 | null | 1,981,834,829,052 | 84 | 9 |
a,b,c = map(int,raw_input().split())
count = 0
if a == b :
if c % a == 0:
count += 1
else:
for i in range(a,b+1):
if c % i == 0:
count += 1
print count | a,b,c=map(int,input().split())
d=0
while a!=b+1:
if c%a==0:d+=1
a+=1
print(d)
| 1 | 551,027,830,212 | null | 44 | 44 |
x = int(input())
sosu = [int(i) for i in range(2*x)]
sosu[1] = 0
for i in range(2,x):
if sosu[i]!=0:
j = 2
while i * j < 2*x:
sosu[i*j]=0
j += 1
#print(sosu[x])
while sosu[x]==0:
x += 1
print(x)
| import math
def isPrime(a):
mx = math.sqrt(a)
tmp = 2
while tmp <= mx:
if a % tmp == 0:
return False
tmp += 1
return True
x = int(input())
tmp = 0
while True:
if isPrime(x + tmp):
f = 1
print(x+tmp)
break
tmp += 1
| 1 | 105,211,750,520,848 | null | 250 | 250 |
#!usr/bin/env pypy3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
res = list(sys.stdin.readline())
if res[-1] == "\n":
return res[:-1]
return res
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)]
sys.setrecursionlimit(1000000)
def main():
N = I()
s = S()
R = s.count("R")
G = s.count("G")
B = s.count("B")
dup = 0
for i in range(N):
for k in range(i+1, N):
if (i + k) % 2 == 0:
j = (i + k) // 2
if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
dup += 1
print(R * G * B - dup)
main()
| import math
a = float(input())
area = a * a * math.pi
cir = (a * 2) * math.pi
print(area,cir) | 0 | null | 18,461,918,828,830 | 175 | 46 |
H, W, M = map(int, input().split())
h_list = [0 for i in range(H)]
w_list = [0 for i in range(W)]
point = {}
for _ in range(M):
h, w = map(int, input().split())
h_list[h - 1] += 1
w_list[w - 1] += 1
point[(h - 1, w - 1)] = 1
hmax = 0
wmax = 0
hmap = {}
wmap = {}
for i, h in enumerate(h_list):
if hmax <= h:
hmax = h
hmap.setdefault(h, []).append(i)
for i, w in enumerate(w_list):
if wmax <= w:
wmax = w
wmap.setdefault(w, []).append(i)
# print(hmap)
# print(wmap)
# hmax = max(h_list)
# wmax = max(w_list)
h_index = h_list.index(hmax)
w_index = w_list.index(wmax)
for h in hmap[hmax]:
for w in wmap[wmax]:
if (h, w) not in point:
print(hmax + wmax)
exit(0)
print(hmax + wmax - 1) | from collections import Counter
def solve():
H, W, M = map(int, input().split())
dh, dw = Counter(), Counter()
used = set()
for _ in range(M):
h, w = map(int, input().split())
dh[h] += 1
dw[w] += 1
used.add((h, w))
ih = dh.most_common()
iw = dw.most_common()
h, counth = ih[0]
w, countw = iw[0]
s = counth + countw
ans = counth + countw - ((h, w) in used)
for h, counth in ih:
for w, countw in iw:
if counth+countw < s or ans == s:
break
b = counth + countw - ((h, w) in used)
ans = max(ans, b)
print(ans)
solve() | 1 | 4,720,332,485,652 | null | 89 | 89 |
import math
n = int(input())
for i in range(int(math.sqrt(n)),0,-1):
if n % i == 0:
print(int(i + (n / i) - 2))
break | def main():
import math
n = int(input())
n_sq = int(math.sqrt(n)) + 1
tmp = 10 ** 13
for i in range(n_sq, 1, -1):
if n % i == 0:
j = n // i
k = (i-1) + (j-1)
if tmp > k :
tmp = k
if tmp != 10 ** 13:
ans = tmp
else:
ans = n-1
print(ans)
if __name__ == "__main__":
main()
| 1 | 161,423,233,761,568 | null | 288 | 288 |
import sys
input = sys.stdin.readline
def main():
N, K = map(int, input().split())
A = list(map(lambda n: int(n)-1, input().split()))
visited = [False] * N
visited[0] = True
index = 0
visited_index = [0]
while K:
index = A[index]
if visited[index]:
K -= 1
start = visited_index.index(index)
loop_index = visited_index[start:]
print(loop_index[K % len(loop_index)] + 1)
sys.exit()
visited_index.append(index)
visited[index] = True
K -= 1
print(index + 1)
main() | #!/usr/bin/env python3
import numpy as np
# def input():
# return sys.stdin.readline().rstrip()
def main():
n, k = map(int, input().split())
warps = list(map(int, input().split()))
warps = [0] + warps
# warps = np.array(warps, dtype=int)
# order_num = np.zeros(len(warps) + 1, dtype=int)
order_num = [0 for i in range(len(warps))]
path_history = []
node = 1
while order_num[node] == 0:
path_history.append(node)
order_num[node] = len(path_history)
node = warps[node]
# print(order_num)
# print(path_history)
begin = order_num[node]
# print(tail)
if k <= begin:
print(path_history[k])
else:
# path_history = path_history[tail:]
# print(path_history)
leng = len(path_history) - order_num[node] + 1
# print(leng)
index = (k - (begin - 1)) % leng
# print(index)
print(path_history[begin - 1 + index])
main()
| 1 | 22,654,185,901,440 | null | 150 | 150 |
import sys
from collections import Counter
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def main():
N = int(readline())
S = readline().strip()
counter = Counter(S)
ans = counter['R'] * counter['G'] * counter['B']
for i in range(N):
for j in range(i + 1, (N + i + 1) // 2):
if 2 * j - i >= N:
break
if S[i] != S[j] and S[i] != S[2 * j - i] and S[j] != S[2 * j - i]:
ans -= 1
print(ans)
return
if __name__ == '__main__':
main()
| #!/usr/bin/env python3
# -*- coding: utf-8 -*-
def update(x):
y = x * 105 // 100
r = y % 1000
y = y // 1000 * 1000
if r: y += 1000
return y
def main():
n = int(input())
x = 100000
for _ in range(n):
x = update(x)
print(x)
if __name__ == "__main__": main() | 0 | null | 18,005,913,365,102 | 175 | 6 |
from math import sqrt
a,b,c,d = map(float,input().split())
print(sqrt((c-a)**2+(d-b)**2)) | from math import sqrt
x1, y1, x2, y2 = map(float, input().split())
X = abs(x1 - x2)
Y = abs(y1 - y2)
print(sqrt(X**2 + Y**2))
| 1 | 157,796,813,450 | null | 29 | 29 |
n = int(input())
S = [int(x) for x in input().split()]
q = int(input())
T = [int(x) for x in input().split()]
res = 0
for num in T:
if num in S:
res += 1
print(res) | def main():
n=int(input())
lst=list(map(int,input().split()))
mod=10**9+7
sm=1
color=[0,0,0]
for x in lst:
if x not in color : sm=0;break
sm*=color.count(x)
sm%=mod
color[color.index(x)]+=1
print(sm)
main() | 0 | null | 65,012,615,470,542 | 22 | 268 |
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []
for q in range(Q):
b, c = map(int, input().split())
B.append(b)
C.append(c)
sam = sum(A)
baketu = [0]*(100001)
for a in A:
baketu[a] += 1
for q in range(Q):
sam = sam + baketu[B[q]] * (C[q] - B[q])
baketu[C[q]] += baketu[B[q]]
baketu[B[q]] = 0
#print(baketu)
print(sam) | N = int(input())
A = list(map(int,input().split()))
Q = int(input())
S = [list(map(int, input().split())) for l in range(Q)]
l = [0] * 10**6
tot = 0
for i in range(N) :
l[A[i]]+=1
tot += A[i]
for i in range(Q):
s0 = S[i][0]
s1 = S[i][1]
tot += s1 * (l[s0] + l[s1]) - (s0 * l[s0] + s1*l[s1])
l[s1] += l[s0]
l[s0] = 0
print(tot) | 1 | 12,139,911,609,900 | null | 122 | 122 |
i = 0
d = 0
while d == 0:
x = input()
if x != 0:
i += 1
print("Case %d: %d" %(i, x))
else:
d = 1 | import sys
cnt = 1
while 1:
x=sys.stdin.readline().strip()
if x == '0':
break;
print("Case " + str(cnt) + ": " + x)
cnt+=1 | 1 | 489,173,033,580 | null | 42 | 42 |
n=int(input())
z_max,z_min=-10**10,10**10
w_max,w_min=-10**10,10**10
for i in range(n):
a,b=map(int,input().split())
z=a+b
w=a-b
z_max=max(z_max,z)
z_min=min(z_min,z)
w_max=max(w_max,w)
w_min=min(w_min,w)
print(max(z_max-z_min,w_max-w_min)) | import numpy as np
N = int(input())
x = []
y = []
xy1 = []
xy2 = []
for i in range(N):
a, b = (int(t) for t in input().split())
x.append(a)
y.append(b)
xy1.append(a + b)
xy2.append(a - b)
max1 = max(xy1) - min(xy1)
max2 = max(xy2) - min(xy2)
print(max((max1, max2)))
| 1 | 3,441,765,072,978 | null | 80 | 80 |
def main(S, T):
N = len(S)
ans = 0
for i in range(N//2):
if S[i] != T[i]:
ans += 1
if S[-1-i] != T[-1-i]:
ans += 1
if N%2 == 1:
if S[N//2] != T[N//2]:
ans += 1
return ans
if __name__ == '__main__':
S = input()
T = input()
ans = main(S, T)
print(ans)
| n = int(input())
num = [int(x) for x in range(1,n+1)]
for i in num:
if (i%3 == 0) or (i%5 == 0):
num[i-1] = 0
print(sum(num)) | 0 | null | 22,667,783,131,610 | 116 | 173 |
N = int(input())
A = [0] * (N + 1)
B = [0] * (N + 1)
count = 0
for i in range(1, N + 1):
A[i], B[i] = map(int, input().split())
A.sort()
B.sort()
if N % 2 == 1:
centerA = A[(N + 1) // 2]
centerB = B[(N + 1) // 2]
else:
centerA = (A[N // 2] + A[N // 2 + 1]) / 2
centerB = (B[N // 2] + B[N // 2 + 1]) / 2
#print(centerA, centerB)
if N % 2 == 1:
count = centerB - centerA + 1
else:
count = (centerB - centerA) * 2 + 1
print(int(count)) | s = input()
t = input()
l = len(s)
x = 0
for i in range(l):
if s[i] != t[i]:
x += 1
print(x) | 0 | null | 13,941,926,863,492 | 137 | 116 |
a=input()
if 65<=ord(a)<=90:
print('A')
else:
print('a') | import os, sys, re, math
N = int(input())
print(((N + 1) // 2) / N)
| 0 | null | 94,175,920,113,600 | 119 | 297 |
def main():
N, K = map(int, input().split())
N = N + 1
A=[n for n in range(N)]
S=[0]*(N+1)
for a in A:
S[a+1]+=S[a]+a
ans=0
for k in range(K, N+1):
maxS, minS=S[-1], S[k]
offset=S[-k-1]
ans+=(maxS - offset) - minS + 1
print(ans%(1000000007))
if __name__=='__main__':
main() | K = int(input())
a = 7
flag = True
for i in range(K):
if a % K == 0:
print(i+1)
flag = False
break
a = (a * 10 + 7) % K
if flag:
print(-1)
| 0 | null | 19,612,449,485,762 | 170 | 97 |
n = int(input())
A = list(map(int,input().split()))
val1 = sum(A)//n
val2 = val1+1
ans1 = 0
ans2 = 0
for a in A:
ans1 += (a-val1)**2
ans2 += (a-val2)**2
print(min(ans1,ans2))
| def abc156c_rally():
n = int(input())
x = list(map(int, input().split()))
min_x, max_x = min(x), max(x)
best = float('inf')
for i in range(min_x, max_x + 1):
total = 0
for v in x:
total += (v - i) * (v - i)
if best > total:
best = total
print(best)
abc156c_rally() | 1 | 65,293,692,097,062 | null | 213 | 213 |
def kakeru(x):
for i in range(1,10):
a = x * i
print(str(x) + 'x' +str(i) + '=' + str(a))
for i in range (1,10):
kakeru(i) | def main():
for N in range(1,10):
for M in range(1,10):
print(str(N)+"x"+str(M)+"="+str(N*M))
if __name__ == '__main__':
main() | 1 | 1,512,180 | null | 1 | 1 |
# https://atcoder.jp/contests/abc154/tasks/abc154_e
# 桁DPっぽいよなぁ => O
"""
memo
dp0[i][j]
上からi桁目まで決めて,0でない桁がj個あり
Nより小さいことが確定している (less)
dp1[i][j]
上からi桁目まで決めて,0でない桁がj個あり
Nより小さいことが確定していない (i桁目まで同じ)
通常の再帰
rec
"""
S = input()
K = int(input())
N = len(S)
def com(N, R):
if R < 0 or R > N:
return 0
if R == 1:
return N
elif R == 2:
return N * (N - 1) // 2
elif R == 3:
return N * (N - 1) * (N - 2) // 6
else:
raise NotImplementedError("NIE")
# 再帰呼出し
def rec(i, k, smaller):
if i == N:
return 1 if k == 0 else 0
if k == 0:
return 1
if smaller:
# 残っている桁数からk個を選び,それぞれ9^kのパターンがある
return com(N - i, k) * pow(9, k)
else:
if S[i] == '0':
return rec(i + 1, k , False)
else:
zero = rec(i + 1, k, True)
aida = rec(i + 1, k - 1, True) * (int(S[i]) - 1)
ichi = rec(i + 1, k - 1, False)
return zero + aida + ichi
ans = rec(0, K, False)
print(ans) | def solve(n):
if n % 2 == 0:
return n // 2 - 1
else:
return (n - 1) // 2
def main():
n = int(input())
res = solve(n)
print(res)
def test():
assert solve(4) == 1
assert solve(999999) == 499999
if __name__ == "__main__":
test()
main()
| 0 | null | 114,710,610,259,908 | 224 | 283 |
X=int(input())
i=2
while i < X:
if X % i == 0:
X += 1
i = 2
else:
i += 1
print(X) | X = int(input())
if X == 2:
print(2)
exit()
count = 0
if X % 2 == 0:
X += 1
while True:
now = X + count
k = 3
while k*k <= now:
if now % k == 0:
count += 2
break
else:
k += 2
continue
if k*k > now:
break
print(now) | 1 | 105,329,602,074,300 | null | 250 | 250 |
heights = [int(input()) for i in range(0, 10)]
heights.sort(reverse=True)
for height in heights[:3]:
print(height) | n = 0
list = []
while n < 10:
x = int(input())
list.append(x)
n += 1
list.sort()
print(list[9])
print(list[8])
print(list[7]) | 1 | 30,700,684 | null | 2 | 2 |
import bisect
n = int(input())
l = list(map(int, input().split()))
l.sort()
cnt = 0
for i in range(n-2):
for j in range(i+1, n-1):
sum = l[i] + l[j]
index = bisect.bisect_left(l, sum)
cnt += index - j - 1
print(cnt) | import bisect
N=int(input())
L=list(map(int,input().split()))
L=sorted(L)
ans=0
for i in range(N-1):
for k in range(i+1,N-1):
a=L[i]+L[k]
b=bisect.bisect_left(L,a)
ans=ans+(b-k-1)
print(ans) | 1 | 172,016,974,763,920 | null | 294 | 294 |
n=int(input())
L=[int(i) for i in input().split()]
count=0
for i in range(1,n+1,2):
if(L[i-1]%2!=0):
count+=1
print(count) | N = int(input())
A = [int(i) for i in input().split()]
print(sum(a % 2 for a in A[::2]))
| 1 | 7,867,672,941,600 | null | 105 | 105 |
s = input()
l, r = 0, len(s)-1
c = 0
while l < r:
if s[l] != s[r]:
c += 1
l += 1
r -= 1
print(c)
| mod = 998244353
n = int(input())
d = list(map(int,input().split()))
if d[0]!=0 or d[1:].count(0)!=0:
print(0)
exit()
l = [0 for i in range(n)]
for i in d:
l[i] += 1
cou = 1
if 0 in l:
a = l.index(0)
if sum(l[a:])!=0:
print(0)
exit()
for i in range(n-2):
if l[i+1]!=0:
a = l[i]
b = l[i+1]
cou *= (a**b)%mod
print(cou%mod) | 0 | null | 137,354,937,119,230 | 261 | 284 |
# ABC160 B
x = int(input())
a,moda = divmod(x,500)
b,modb = divmod(moda,5)
print(a*1000+b*5) | x = int(input())
ans = 0
ans += (x // 500) * 1000
ans += ((x % 500) // 5) * 5
print(ans) | 1 | 42,805,504,295,008 | null | 185 | 185 |
n, k, *a = map(int, open(0).read().split())
for _ in range(k):
b = [0] * -~n
for i in range(len(a)):
b[max(0, i - a[i])] += 1
b[min(n, i + a[i] + 1)] -= 1
for i in range(n):
b[i + 1] += b[i]
if all(x == n for x in b[:n]):
print(*b[:n])
exit()
a = b
print(*a[:n]) | #!/usr/bin python3
# -*- coding: utf-8 -*-
def main():
mod = 10**9+7
N = int(input())
A = list(map(int, input().split()))
C = [0] * 3
ret = 1
for i in range(N):
ret *= C.count(A[i])
ret %= mod
for j in range(3):
if C[j]==A[i]:
C[j]+=1
break
print(ret)
if __name__ == '__main__':
main() | 0 | null | 72,626,312,712,162 | 132 | 268 |
import sys
H, A = map(int, next(sys.stdin.buffer).split())
print(-(-H // A)) | from math import gcd
def lcm(a, b):
return a // gcd(a, b) * b
N, M = map(int, input().split())
As = list(map(int, input().split()))
rightmostbit = As[0] & -As[0]
for A in As[1:]:
if rightmostbit != A & -A:
print(0)
exit()
lcm_of_half_As = 1
for A in As:
lcm_of_half_As = lcm(lcm_of_half_As, A // 2)
if lcm_of_half_As > M:
break
print((M // lcm_of_half_As + 1) // 2) | 0 | null | 89,575,555,627,646 | 225 | 247 |
P = 10**9+7
N, K = map(int, input().split())
S = list(map(int, input().split()))
S.sort()
ans = 0
c = 1
for i in range(N-K+1):
ans += (S[K-1+i]*c)%P
ans -= (S[N-K-i]*c)%P
ans %= P
c = c*(K+i)*pow(i+1, P-2, P)%P
print(ans) | N,K= map(int, input().split())
p = 10**9+7
f=[1]
for i in range(1,N+1):
f.append(f[-1]*i % p)
def nCk(n,k):
return f[n]*pow(f[n-k], p-2, p)*pow(f[k], p-2, p)
a = list(map(int, input().split()))
a.sort()
ans = 0
for i in range(N-K+1):
ans -= (a[i] * nCk(N-i-1, K-1)) % p
ans += (a[N-i-1] * nCk(N-i-1, K-1)) % p
print(ans % p) | 1 | 95,735,625,791,442 | null | 242 | 242 |
h, w, k = map(int, input().split())
S = []
for i in range(h):
s = str(input())
S.append(s)
Ans = [[False for _ in range(w)] for _ in range(h)]
now = 1
for i in range(h):
for j in range(w):
if S[i][j] == '#':
Ans[i][j] = now
now += 1
for i in range(h):
now = False
for j in range(w):
if now == False and Ans[i][j] == False:
continue
elif now != False and Ans[i][j] == False:
Ans[i][j] = now
elif Ans[i][j] != False:
now = Ans[i][j]
for i in range(h):
now = False
for j in range(w-1, -1, -1):
if now == False and Ans[i][j] == False:
continue
elif now != False and Ans[i][j] == False:
Ans[i][j] = now
elif Ans[i][j] != False:
now = Ans[i][j]
now = False
for i in range(h):
if now == False and Ans[i].count(False) == w:
continue
elif now != False and Ans[i].count(False) == w:
Ans[i] = now
elif Ans[i].count(False) != w:
now = Ans[i]
now = False
for i in range(h-1, -1, -1):
if now == False and Ans[i].count(False) == w:
continue
elif now != False and Ans[i].count(False) == w:
Ans[i] = now
elif Ans[i].count(False) != w:
now = Ans[i]
for i in range(h):
print(' '.join(map(str, Ans[i]))) | def LI():
return list(map(int, input().split()))
def LSH(h):
return [list(input()) for _ in range(h)]
H, W, K = LI()
S = LSH(H)
ans = [[0 for i in range(W)]for j in range(H)]
count = 1
for i in range(H):
itigo = 0
ok = 0
for j in range(W):
if S[i][j] == "#":
ok = 1
if itigo == 0:
ans[i][j] = count
itigo = 1
else:
count += 1
ans[i][j] = count
else:
ans[i][j] = count
if ok == 0:
ans[i][0] = 0
else:
count += 1
ok = -1
for i in range(H):
if ans[i][0] == 0:
if ok == -1:
for k in range(i+1, H):
if ans[k][0] != 0:
ok = k
break
for x in range(W):
print(ans[ok][x], end=" ")
print()
else:
ok = i
for j in range(W):
print(ans[i][j], end=" ")
print()
| 1 | 143,365,780,600,768 | null | 277 | 277 |
lst = []
for i in range(10):
lst.append(int(input()))
for j in range(3):
print max(lst)
lst.remove(max(lst)) | while 1:
h,w = map(int,input().split())
if h == w == 0:
break;
print("#"*w)
for i in range(h-2):
print("#" + ("."*(w-2)) + "#")
print("#"*w)
print() | 0 | null | 405,236,494,800 | 2 | 50 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.