code1
stringlengths 17
427k
| code2
stringlengths 17
427k
| similar
int64 0
1
| pair_id
int64 2
181,637B
⌀ | question_pair_id
float64 3.7M
180,677B
⌀ | code1_group
int64 1
299
| code2_group
int64 1
299
|
---|---|---|---|---|---|---|
while True:
n = int(input())
s = []
if n == 0:
break
s = list(map(int, input().split()))
m = sum(s) / n
v = 0
for i in s:
v += (i - m) ** 2
ans = (v / n) ** (1/2)
print('{:10f}'.format(ans))
| a = int(input())
s = 0
b = 1
while 15*b <= a:
s += 8*b
b += 1
while 5*b <= a:
s -= 7*b
b += 1
while 3*b <= a:
s -= 2*b
b += 1
while b <= a:
s += b
b += 1
print(s) | 0 | null | 17,512,157,583,492 | 31 | 173 |
from collections import Counter
D = int(input())
c = list(map(int, input().split()))
s = []
for i in range(D):
temp_s = list(map(int, input().split()))
s.append(temp_s)
my_count = Counter()
t = list(int(input()) for i in range(D))
c_sum = sum(c)
my_sum = 0
my_minus = 0
for d in range(D):
my_count [t[d]] = c[t[d] - 1] * (d + 1)
my_sum += s[d][t[d] - 1]
my_minus += sum(dict(my_count).values()) - c_sum * (d + 1)
print(my_sum + my_minus) | from itertools import chain
def main():
inputs = []
r, _ = map(int, input().split())
for i in range(r):
inputs.append(tuple(map(int, input().split())))
row_sums = map(lambda row: sum(row), inputs)
column_sums = map(lambda column: sum(column), map(lambda *x: x, *inputs))
all_sum = sum(chain.from_iterable(inputs))
for i, s in zip(inputs, row_sums):
print(" ".join(map(lambda n: str(n), i)) + " {}".format(s))
else:
print(" ".join(map(lambda n: str(n), column_sums)) + " {}".format(all_sum))
if __name__ == "__main__":
main() | 0 | null | 5,607,146,972,700 | 114 | 59 |
import sys
import math
from collections import deque
sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()
def main():
N, X, Y = NMI()
counts = [0] * N
for i in range(1, N):
for j in range(i+1, N+1):
dist = min(abs(j-i), abs(i-X) + 1 + abs(Y-j))
counts[dist] += 1
for d in counts[1:]:
print(d)
if __name__ == "__main__":
main() | def solve():
m1, d1 = map(int, input().split())
m2, d2 = map(int, input().split())
if m1 != m2:
print(1)
else:
print(0)
if __name__ == '__main__':
solve()
| 0 | null | 84,413,999,348,682 | 187 | 264 |
from math import sqrt
def sd(nums):
n = len(nums)
ave = sum(nums)/n
return abs(sqrt(sum([(s - ave)**2 for s in nums])/n))
def main():
while True:
n = input()
if n == '0':
break
nums = [int(x) for x in input().split()]
print("{:f}".format(sd(nums)))
if __name__ == '__main__':
main()
| from statistics import mean
import math
while True:
ans = 0
n = int(input())
if n == 0:
break
scores = list(map(int, input().split()))
m = mean(scores)
for s in scores:
ans += (s - m)**2
print(math.sqrt(ans/n))
| 1 | 199,348,765,572 | null | 31 | 31 |
# 問題:https://atcoder.jp/contests/abc142/tasks/abc142_b
n, k = map(int, input().strip().split())
h = list(map(int, input().strip().split()))
res = 0
for i in range(n):
if h[i] < k:
continue
res += 1
print(res)
| n,k = (int(x) for x in input().split())
h = [int(x) for x in input().split()]
count = 0
for i in range(len(h)):
if k <= h[i]:
count += 1
print(count) | 1 | 178,186,054,324,412 | null | 298 | 298 |
N = int(input())
A = [int(x) for x in input().split()]
L = dict(); R = dict()
for i in range(N):
l = i + A[i]
if l in L:
L[l] += 1
else:
L[l] = 1
r = i - A[i]
if r in R:
R[r] += 1
else:
R[r] = 1
ans = 0
for x in R:
if x in L:
ans += L[x] * R[x]
print(ans) | n = int(input())
a = list(map(int, input().split()))
b = [0 for i in range(n)]
cnt = 0
for i in range(n):
if i - a[i] >= 0:
cnt += b[i - a[i]]
if i + a[i] < n:
b[i + a[i]] += 1
print(cnt) | 1 | 25,997,203,564,200 | null | 157 | 157 |
n = int(input())
st, sh = 0, 0
for i in range(n):
t, h = map(str, input().split())
if t > h:
st += 3
elif t < h:
sh += 3
else:
st += 1
sh += 1
print(st, sh) | import sys
def solve():
input = sys.stdin.readline
R, C, K = map(int, input().split())
item = [[0 for _ in range(C)] for r in range(R)]
for _ in range(K):
r, c, k = map(int, input().split())
item[r-1][c-1] = k
DP = [[[-1 for _ in range(C)] for r in range(R)] for i in range(4)]
DP[0][0][0] = 0
for r in range(R):
for c in range(C):
for i in range(4):
if DP[i][r][c] > -1:
if r + 1 < R:
DP[0][r+1][c] = max(DP[0][r+1][c], DP[i][r][c])
if c + 1 < C:
DP[i][r][c+1] = max(DP[i][r][c+1], DP[i][r][c])
if i < 3 and item[r][c] > 0:
if r + 1 < R: DP[0][r+1][c] = max(DP[0][r+1][c], DP[i][r][c] + item[r][c])
if c + 1 < C: DP[i+1][r][c+1] = max(DP[i+1][r][c+1], DP[i][r][c] + item[r][c])
ans = DP[3][R-1][C-1]
for i in range(3): ans = max(ans, DP[i][R-1][C-1] + item[R-1][C-1])
print(ans)
return 0
if __name__ == "__main__":
solve()
| 0 | null | 3,757,417,528,270 | 67 | 94 |
'''
def main():
S = input()
cnt = 0
ans = 0
f = 1
for i in range(3):
if S[i] == 'R':
cnt += 1
else:
cnt = 0
ans = max(ans, cnt)
print(ans)
'''
def main():
S = input()
p = S[0] == 'R'
q = S[1] == 'R'
r = S[2] == 'R'
if p and q and r :
print(3)
elif (p and q) or (q and r):
print(2)
elif p or q or r:
print(1)
else:
print(0)
if __name__ == "__main__":
main() | n,m = map(int,input().split())
c = list(map(int,input().split()))
c.sort(reverse = True)
dp = [[float('inf') for i in range(n+1)] for j in range(m+1)]
for j in range(m+1):
dp[j][0] = 0
for j in range(1,m+1):
for i in range(c[j-1],n+1):
dp[j][i] = min(dp[j-1][i], dp[j][i-c[j-1]] + 1)
print(dp[m][n]) | 0 | null | 2,515,637,430,200 | 90 | 28 |
class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def same(self, x, y):
return self.find(x) == self.find(y)
N,M = map(int,input().split())
q = UnionFind(N)
for _ in range(M):
x,y = map(int,input().split())
q.union(x-1,y-1)
set_ = set()
cnt = 0
for i in range(N):
if q.find(i)<0:
cnt += 1
else:
set_.add(q.find(i))
print(cnt + len(set_)-1)
| class UnionFind:
def __init__(self, n):
self.par = [i for i in range(n+1)]
self.rank = [0] * (n+1)
# 検索
def find(self, x):
if self.par[x] == x:
return x
else:
self.par[x] = self.find(self.par[x])
return self.par[x]
# 併合
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if self.rank[x] < self.rank[y]:
self.par[x] = y
else:
self.par[y] = x
if self.rank[x] == self.rank[y]:
self.rank[x] += 1
# 同じ集合に属するか判定
def same_check(self, x, y):
return self.find(x) == self.find(y)
n,m = map(int,input().split())
u = UnionFind(n)
for _ in range(m):
ai,bi = map(int,input().split())
u.union(ai,bi)
s = [0 for _ in range(n+1)]
for i in range(1,n+1):
s[u.find(i)] = 1
print(sum(s)-1) | 1 | 2,295,274,550,800 | null | 70 | 70 |
a, b = map(int, input().split())
print(a // b, a % b, "{:.10f}".format(a / b))
| #coding: UTF-8
l = raw_input().split()
a = int(l[0])
b = int(l[1])
if 1 <= a and b <= 10**9:
d = a / b
r = a % b
f = float (a )/ b
print "%d %d %f" %(d, r, f) | 1 | 608,477,103,070 | null | 45 | 45 |
HW = input().split()
H = int(HW[0])
W = int(HW[1])
while H != 0 or W != 0:
for i in range(H):
for j in range(W):
print("#", end = "")
print("")
HW = input().split()
H = int(HW[0])
W = int(HW[1])
print("") | import sys
input=sys.stdin.readline
n,u,v = map(int, input().split())
link = [[] for _ in range(n)]
for i in range(n-1):
tmp = list(map(int,input().split()))
link[tmp[0]-1].append(tmp[1]-1)
link[tmp[1]-1].append(tmp[0]-1)
from collections import deque
Q = deque()
Q.append([v-1,0])
visited=[-1]*n
visited[v-1]=0
while Q:
now,cnt = Q.popleft()
for nxt in link[now]:
if visited[nxt]!=-1:
continue
visited[nxt]=cnt+1
Q.append([nxt,cnt+1])
Q = deque()
Q.append([u-1,0])
v_taka=[-1]*n
v_taka[u-1]=0
while Q:
now,cnt = Q.popleft()
for nxt in link[now]:
if v_taka[nxt]!=-1:
continue
if visited[nxt] <= cnt+1:
continue
v_taka[nxt]=cnt+1
Q.append([nxt,cnt+1])
ans=-1
for i in range(n):
if v_taka[i] == -1:
continue
if ans < visited[i]:
ans=visited[i]
print(ans-1) | 0 | null | 59,306,042,266,262 | 49 | 259 |
S = input()
mx = 0
c = 0
for si in S:
if si == 'R':
c += 1
mx = max(mx, c)
else:
c = 0
print(mx) | import sys
input = sys.stdin.readline
n,m=map(int,input().split())
M=set(tuple(map(int,input().split())) for _ in range(m))
d={i:set() for i in range(1,n+1)}
for i,j in M:
d[i].add(j)
d[j].add(i)
vis=set()
res = 0
for i in range(1,n+1):
if i not in vis:
stack = [i]
ans = 1
vis.add(i)
while stack:
curr = stack.pop()
for j in d[curr]:
if j not in vis:
stack.append(j)
ans+=1
vis.add(j)
res = max(ans,res)
print(res)
| 0 | null | 4,412,177,547,008 | 90 | 84 |
# ==================================================-
# 二分探索
# functionを満たす,search_listの最大の要素を出力
# 【注意点】searchリストの初めの方はfunctionを満たし、後ろに行くにつれて満たさなくなるべき
import math
import sys
sys.setrecursionlimit(10 ** 9)
def binary_research(start, end,function):
if start == end:
return start
middle = math.ceil((start + end) / 2)
if function(middle, k, a_sum, b_sum):
start = middle
else:
end = middle - 1
return binary_research(start, end, function)
# ==================================================-
n, m, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
a_sum = [0]
b_sum = [0]
for book in a:
a_sum.append(a_sum[-1] + book)
for book in b:
b_sum.append(b_sum[-1] + book)
def can_read(num, k, a_sum, b_sum):
min_read_time = 1000000000000
for i in range(max(0, num - m), min(num+1,n+1)):
a_num = i
b_num = num - i
min_read_time = min(min_read_time, a_sum[a_num] + b_sum[b_num])
if min_read_time <= k:
return True
return False
start = 0
end = n + m
print(binary_research(start, end, can_read)) | from bisect import bisect_left
n, k = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
l, r = 0, 10000000000
while r - l > 1:
m = (l + r) // 2
res = 0
for x in a:
res += n - bisect_left(a, m - x)
if res >= k:
l = m
else:
r = m
b = [0] * (n + 1)
for i in range(1, n + 1):
b[i] = b[i - 1] + a[n - i]
cnt = 0
ans = 0
for x in a:
t = n - bisect_left(a, l - x)
ans += b[t] + x * t
cnt += t
print(ans - (cnt - k) * l)
| 0 | null | 59,408,622,119,174 | 117 | 252 |
Station = []
Station = input()
if Station == "AAA":
print("No")
elif Station == "BBB":
print("No")
else:
print("Yes") | import sys
k, x = map(int, sys.stdin.readline().split())
def main():
ans = 'Yes' if 500*k >= x else 'No'
print(ans)
if __name__ == '__main__':
main() | 0 | null | 76,441,651,646,178 | 201 | 244 |
x, y = map(int, input().split())
prise_dict = {3: 100000, 2: 200000, 1: 300000}
ans = 0
if x in prise_dict.keys():
ans += prise_dict[x]
if y in prise_dict.keys():
ans += prise_dict[y]
if x == y == 1:
ans += 400000
print(ans)
| N,R=map(int,input().split())
num=0
while N>=R:
N=N/R
num+=1
print(num if N==0 else num+1) | 0 | null | 102,279,474,682,098 | 275 | 212 |
def multi():
for i in range(1, 10):
for j in range(1, 10):
print(str(i) + "x" + str(j) + "=" + str(i * j))
if __name__ == '__main__':
multi()
| kuk = range(1, 10)
for i in kuk:
for j in kuk:
print '%dx%d=%d' % (i, j, i*j) | 1 | 2,307,580 | null | 1 | 1 |
def solve(x, y):
# |x| = |p + q | = | 1 1 | |p|
# |y| |2p + 4q| | 2 4 | |q|
# <=>
# |p| = 1/2 | 4 -1| |x| = (4x - y)/2
# |q| |-2 1| |y| (-2x +y)/2
p2 = 4 * x - y
q2 = -2 * x + y
if p2 % 2 == 0 and q2 % 2 == 0 and p2 >= 0 and q2 >= 0:
return "Yes"
return "No"
def main(istr, ostr):
x, y = list(map(int, istr.readline().strip().split()))
result = solve(x, y)
print(result, file=ostr)
if __name__ == "__main__":
import sys
main(sys.stdin, sys.stdout)
| N = int(input())
count = {}
max_count = 0
for _ in range(N):
s = input()
if s not in count:
count[s] = 0
count[s] += 1
max_count = max(max_count, count[s])
longest = []
for s, c in count.items():
if c == max_count:
longest.append(s)
longest.sort()
for s in longest:
print(s) | 0 | null | 41,817,121,928,122 | 127 | 218 |
ans = list(map(int, input().split(' '))).index(0) + 1
print(ans) | print(input().split().index('0')+1) | 1 | 13,407,538,073,258 | null | 126 | 126 |
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([])
| n = int(input())
d = 'abcdefghijklm'
def conv(s):
s = list(map(lambda x: d[x], s))
return ''.join(s)
def dfs(s):
if len(s) == n:
print(conv(s))
else:
mx = max(s)+1
for i in range(mx+1):
dfs(s+[i])
dfs([0]) | 1 | 52,124,113,490,260 | null | 198 | 198 |
# 写経AC
from collections import defaultdict
N = int(input())
A = [int(i) for i in input().split()]
# dp[(i, x, flag)]:= i番目まででx個選んでいる時の最大値
# flag: i番目をとるフラグ
dp = defaultdict(lambda: -float("inf"))
# 初期条件
dp[(0, 0, 0)] = 0
# 貰うDP
for i, a in enumerate(A, 1):
# i番目までで選ぶ個数
for x in range((i // 2) - 1, (i + 1) // 2 + 1):
dp[(i, x, 0)] = max(dp[(i - 1, x, 0)], dp[(i - 1, x, 1)])
dp[(i, x, 1)] = dp[(i - 1, x - 1, 0)] + a
print(max(dp[(N, N // 2, 0)], dp[(N, N // 2, 1)])) | def main():
S = input()
if S == "SUN":
print(7)
exit(0)
elif S == "MON":
print(6)
exit(0)
elif S == "TUE":
print(5)
exit(0)
elif S == "WED":
print(4)
exit(0)
elif S == "THU":
print(3)
exit(0)
elif S == "FRI":
print(2)
exit(0)
else:
print(1)
exit(0)
if __name__ == "__main__":
main()
| 0 | null | 85,642,766,956,640 | 177 | 270 |
def main2():
N = int(input())
mod = 10**9 + 7
ans = 10**N - 9**N - 9**N + 8**N
print(ans % mod)
if __name__ == "__main__":
main2() | #!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる
mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000
d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))
N = int(input())
all = (10 **N) % mod
no_zero = (9**N) % mod
no_nine = (9**N)
no_zero_nine = 8 ** N
A = 1
no_zero = 1
no_nine = 1
no_zero_nine = 1
for i in range(N):
A = A * 10 % mod
no_zero = no_zero * 9 % mod
no_zero_nine = no_zero_nine * 8 % mod
ans = (A - (no_zero * 2 - no_zero_nine)) % mod
print(ans)
| 1 | 3,158,365,237,820 | null | 78 | 78 |
import sys
import itertools
def main():
N = int(input())
songs = []
time = []
for _ in range(N):
s, t = input().split()
t = int(t)
songs.append(s)
time.append(t)
X = input()
time = list(itertools.accumulate(time))
ans = time[-1] - time[songs.index(X)]
print(ans)
if __name__ == '__main__':
main()
| n = int(input())
s, t = [], []
for i in range(n):
si, ti = input().split()
ti = int(ti)
s.append(si)
t.append(ti)
x = input()
bit = 0
ans = 0
for i in range(n):
if s[i] == x:
bit = 1
else:
if bit == 1:
ans += t[i]
print(ans) | 1 | 97,054,567,143,550 | null | 243 | 243 |
a, b, c, d = map(int,input().split())
x = (a+d-1)//d
y = (c+b-1)//b
if x >= y:
print('Yes')
else:
print('No') | A, B, C, D = map(int, input().split())
takahashi = 0
aoki = 0
while C > 0:
C -= B
takahashi += 1
while A > 0:
A -= D
aoki += 1
if takahashi <= aoki:
print("Yes")
else:
print("No") | 1 | 29,666,932,274,990 | null | 164 | 164 |
from itertools import product
from collections import deque
class ZeroOneBFS:
def __init__(self, N):
self.N = N # #vertices
self.E = [[] for _ in range(N)]
def add_edge(self, init, end, weight, undirected=False):
assert weight in [0, 1]
self.E[init].append((end, weight))
if undirected: self.E[end].append((init, weight))
def distance(self, s):
INF = float('inf')
E, N = self.E, self.N
dist = [INF] * N # the distance of each vertex from s
prev = [-1] * N # the previous vertex of each vertex on a shortest path from s
dist[s] = 0
dq = deque([(0, s)]) # (dist, vertex)
n_visited = 0 # #(visited vertices)
while dq:
d, v = dq.popleft()
if dist[v] < d: continue # (s,v)-shortest path is already calculated
for u, c in E[v]:
temp = d + c
if dist[u] > temp:
dist[u] = temp; prev[u] = v
if c == 0: dq.appendleft((temp, u))
else: dq.append((temp, u))
n_visited += 1
if n_visited == N: break
self.dist, self.prev = dist, prev
return dist
def shortest_path(self, t):
P = []
prev = self.prev
while True:
P.append(t)
t = prev[t]
if t == -1: break
return P[::-1]
H, W = map(int, input().split())
zobfs = ZeroOneBFS(H * W)
def vtx(i, j): return i*W + j
def coord(n): return divmod(n, W)
grid = [input() for _ in range(H)] # |string| = W
E = [[] for _ in range(H * W)]
ans = 0 if grid[0][0] == '.' else 1
for i, j in product(range(H), range(W)):
v = vtx(i, j)
check = [vtx(i+dx, j+dy) for dx, dy in [(1, 0), (0, 1)] if i+dx <= H-1 and j+dy <= W-1]
for u in check:
x, y = coord(u)
if grid[i][j] == '.' and grid[x][y] == '#':
zobfs.add_edge(v, u, 1)
else:
zobfs.add_edge(v, u, 0)
dist = zobfs.distance(0)
ans += dist[vtx(H-1, W-1)]
print(ans) | print(' ' + ' '.join(str(i) for i in range(1, int(input()) + 1) if not i % 3 or '3' in str(i))) | 0 | null | 25,067,756,669,654 | 194 | 52 |
import sys
for line in sys.stdin:
a,b = [int(i) for i in line.split()]
print(len(str(a+b)))
| def solve():
from sys import stdin
input_lines = stdin
from math import log10
for line in input_lines:
a, b = map(int, line.split())
if (a == 0 and b == 0):
break
print(int(log10(a + b)) + 1)
solve()
| 1 | 94,226,012 | null | 3 | 3 |
x, n = map(int,input().split())
p = list(map(int,input().split()))
d =100
num = 0
if n == 0:
num = x
else:
for i in range(102):
if i not in p:
d_temp = abs(x-i)
if d_temp < d:
d = d_temp
num = i
print(num) | # coding: utf-8
def main():
n = int(input())
goods = []
for i in range(n):
s = input()
goods.append(s)
print(len(set(goods)))
main()
| 0 | null | 22,111,135,774,378 | 128 | 165 |
class UnionFind:
def __init__(self, n):
self.nodes = n
self.parents = [i for i in range(n)]
self.sizes = [1] * n
self.rank = [0] * n
def find(self, i): # どの集合に属しているか(根ノードの番号)
if self.parents[i] == i:
return i
else:
self.parents[i] = self.find(self.parents[i]) # 経路圧縮
return self.parents[i]
def unite(self, i, j): # 二つの集合を併合
pi = self.find(i)
pj = self.find(j)
if pi != pj:
if self.rank[pi] < self.rank[pj]:
self.sizes[pj] += self.sizes[pi]
self.parents[pi] = pj
else:
self.sizes[pi] += self.sizes[pj]
self.parents[pj] = pi
if self.rank[pi] == self.rank[pj]:
self.rank[pi] += 1
def same(self, i, j): # 同じ集合に属するかを判定
return self.find(i)==self.find(j)
def get_parents(self): # 根ノードの一覧を取得
for n in range(self.nodes): # findで経路圧縮する
self.find(n)
return self.parents
adj = []
N, M = map(int,input().split())
for m in range(M):
a,b = map(int,input().split())
adj.append([a-1,b-1])
uf = UnionFind(N)
for i in range(M): # 取り除く辺番号
uf.unite(*adj[i])
ans=len(set(uf.get_parents())) # 複数の集合にわかれているか確認
print (ans-1) | #http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_A&lang=jp
#??????????????????
#?????????????´?????¢????????????£????????????????????????????????¨???????¢?????????¨
#?????°??¢??°???????¨???????????¢????????????????
def bubble_sort(target_list):
list_length = len(target_list)
flag = True
change_count = 0
top_index = 1
while flag:
flag = False
for i in range(top_index, list_length)[::-1]:
if target_list[i] < target_list[i - 1]:
tmp = target_list[i]
target_list[i] = target_list[i - 1]
target_list[i - 1] = tmp
change_count += 1
flag = True
top_index += 1
return change_count
def main():
n_list = int(input())
target_list = [int(n) for n in input().split()]
cc = bubble_sort(target_list)
print(*target_list)
print(cc)
if __name__ == "__main__":
main() | 0 | null | 1,163,852,648,860 | 70 | 14 |
import math
import itertools
n = int(input())
al = [0] * n
bl = [0] * n
for i in range(n):
al[i], bl[i] = map(int, input().split())
bunbo = math.factorial(n)
ans = 0
for p in itertools.permutations(list(range(n))):
for i in range(n-1):
ans += ((al[p[i+1]]-al[p[i]])**2+(bl[p[i+1]]-bl[p[i]])**2)**0.5
print(ans/bunbo)
| from itertools import combinations
n = int(input())
xy = [list(map(int, input().split())) for _ in range(n)]
ans = 0
for i, j in combinations(xy, 2):
ans += ((i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2) ** 0.5
print(ans * 2 / n) | 1 | 148,006,740,992,000 | null | 280 | 280 |
N, M = map(int, input().split())
# 左パート
l, r = 1, N // 2
while M and l < r :
print(l, r)
l += 1
r -= 1
M -= 1
# 右パート
l, r = N // 2 + 2 - N % 2, N
while M :
print(l, r)
l += 1
r -= 1
M -= 1 | '''
N人を円周上に配置し、M本の線分で結ぶ。
このとき、線分を回転させていっても同じ人のペアが生まれないようにする。
基本的には、上から順に結べばよい。
'''
from collections import deque
def main():
N, M = map(int, input().split())
if N&1:
'''
・Nが奇数の場合
上から順に結ぶだけ。
dequeに 1,2,3,...,M*2 を突っ込んで、両端から取り出してペアにする。
'''
q = deque(range(M*2))
while q:
print(q.popleft()+1, q.pop()+1)
else:
'''
[Nが偶数の場合]
上から順に結ぶと、上下の線対称になり、同じ間隔のペアが生まれるためNG。
→ 半分以降は、1つずらしてペアを作っていく
簡単に解決するため、
・上から順に奇数間隔ペアを作る
・下から順に偶数間隔ペアを作る
この2つから、M個を順に、交互に取り出す。
'''
q1 = deque(range(N))
q2 = deque(range(N-1))
p1 = []
while q1:
p1.append((q1.popleft()+1, q1.pop()+1))
p2 = []
while len(q2)>=2:
p2.append((q2.popleft()+1, q2.pop()+1))
p2.reverse()
for _ in range(M):
print(*p1.pop())
p1, p2 = p2, p1
main()
| 1 | 28,572,223,369,898 | null | 162 | 162 |
s=input()
s_list=list(s)
x=len(s_list)
if s_list[x-1]=="s":
s_list.append("es")
else:
s_list.append("s")
print("".join(s_list)) | string = input()
if string[-1] == 's':
out = string + "es"
else:
out = string + "s"
print(out) | 1 | 2,356,536,341,828 | null | 71 | 71 |
# -*- coding: utf-8 -*-
from decimal import Decimal
import math
a, b = input().split()
a = int(a)
b = Decimal(b)
x = a * b
print(math.floor(int(a) * Decimal(b)))
| n = int(input())
from copy import copy
L = ["a"]
if n == 1:
print(L[0])
exit()
for i in range(n-1):
li = []
for s in L:
max1 = max(s)
S = ""
for j in range(ord(max1)-95):
S = s + chr(97+j)
#print(ord(max1)-95,chr(ord(max1)+j))
li.append(S)
L = copy(li)
print("\n".join(L))
| 0 | null | 34,512,811,992,702 | 135 | 198 |
dice = map(int, raw_input().split())
inst = raw_input()
def rolling(inst, dice):
if inst == 'E':
dice[5], dice[2], dice[0], dice[3] = dice[2], dice[0], dice[3], dice[5]
elif inst == 'W':
dice[5], dice[3], dice[0], dice[2] = dice[3], dice[0], dice[2], dice[5]
elif inst == 'N':
dice[5], dice[4], dice[0], dice[1] = dice[4], dice[0], dice[1], dice[5]
elif inst == 'S':
dice[5], dice[1], dice[0], dice[4] = dice[1], dice[0], dice[4], dice[5]
for i in range(len(inst)):
rolling(inst[i], dice)
print dice[0] | def north(d):
return [d[1], d[5], d[2], d[3], d[0], d[4]]
def east(d):
return [d[3], d[1], d[0], d[5], d[4], d[2]]
def south(d):
return [d[4], d[0], d[2], d[3], d[5], d[1]]
def west(d):
return [d[2], d[1], d[5], d[0], d[4], d[3]]
numbers = list(map(int, input().split()))
directions = list(input())
for d in directions:
if d == "N":
numbers = north(numbers)
elif d == "E":
numbers = east(numbers)
elif d == "S":
numbers = south(numbers)
elif d == "W":
numbers = west(numbers)
print(numbers[0]) | 1 | 226,269,868,046 | null | 33 | 33 |
from collections import defaultdict
money = defaultdict(int)
for i, m in enumerate([300000, 200000, 100000]):
money[i] = m
X, Y = map(int, input().split())
X -= 1; Y -= 1
ans = money[X] + money[Y]
if X + Y:
print(ans)
else:
print(ans + 400000) | import sys
input = sys.stdin.readline
x, y = [int(x) for x in input().split()]
ans = 0
for i in [x, y]:
if i == 1:
ans += 300000
elif i == 2:
ans += 200000
elif i == 3:
ans += 100000
if x == 1 and y == 1:
ans += 400000
print(ans)
| 1 | 140,144,251,541,422 | null | 275 | 275 |
mod = 998244353
n, s = map(int, input().split())
arr = list(map(int, input().split()))
dp = [[0] * (s + 1) for _ in range(n + 1)]
dp[0][0] = 1
for i in range(1, n + 1):
var = arr[i - 1]
for j in range(0, s + 1):
dp[i][j] = 2 * dp[i - 1][j]
dp[i][j] %= mod
if j - var >= 0 and dp[i - 1][j - var] != 0:
dp[i][j] += dp[i - 1][j - var]
dp[i][j] %= mod
print(int(dp[n][s]))
| def main():
n, s = map(int, input().split())
a = list(map(int, input().split()))
mod = 998244353
dp = [[0]*(3001) for _ in range(n+1)]
dp[0][0] = 1
for i in range(1, n+1):
ai = a[i-1]
for j in range(ai):
dp[i][j] += dp[i-1][j]*2
dp[i][j] %= mod
for j in range(ai, 3001):
dp[i][j] += dp[i-1][j]*2 + dp[i-1][j-ai]
dp[i][j] %= mod
print(dp[n][s])
if __name__ == "__main__":
main() | 1 | 17,595,973,886,920 | null | 138 | 138 |
def factorization(n):
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
cnt = 0
while n % i == 0:
cnt += 1
n //= i
e[i] = max(e[i], cnt)
if n != 1:
e[n] = max(e[n], 1)
def mod_pow(a, n):
res = 1
while n > 0:
if n & 1:
res = res * a % mod
a = a * a % mod
n >>= 1
return res
n = int(input())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
e = [0] * 10 ** 6
for v in a:
factorization(v)
lcm = 1
for i, c in enumerate(e):
if c > 0:
lcm = lcm * mod_pow(i, c) % mod
res = 0
for v in a:
res = (res + lcm * mod_pow(v, mod - 2)) % mod
print(res) | n = int(input())
h = {}
for i in range(n):
op, st = input().split()
if op == 'insert':
h[st] = 'yes'
else:
print (h.get(st, 'no')) | 0 | null | 43,866,780,765,280 | 235 | 23 |
n, k = map(int, input().split())
a = list(map(int, input().split()))
low = 0
top = max(a) + 1
while top - low > 1:
mid = (top + low) // 2
cnt = 0
for i in range(n):
if a[i] % mid == 0:
cnt += (a[i] // mid) - 1
else:
cnt += a[i] // mid
if cnt <= k:
top = mid
else:
low = mid
print(top)
| N, K = map(int, input().split())
*A, = map(int, input().split())
def f(t):
return sum(i//t if i!=t else 0 for i in A)<=K if t else all(i<=t for i in A)
l, r = -1, 10**10
while r-l>1:
m = (r+l)//2
if f(m):
r = m
else:
l = m
print(r)
| 1 | 6,523,304,844,640 | null | 99 | 99 |
from collections import deque
n,p=map(int ,input().split())
que=deque([])
for i in range(n):
name,time=input().split()
time=int(time)
que.append([name,time])
t=0
while len(que)>0:
atop=que.popleft()
spend=min(atop[1],p)
atop[1]-=spend
t+=spend
if(atop[1]==0):
print(atop[0],t)
else:
que.append(atop) | # import sys
# sys.setrecursionlimit(10 ** 6)
# import bisect
# from collections import deque
# from decorator import stop_watch
#
#
# @stop_watch
def solve(H, W, K, Si):
Si = [[int(i) for i in S] for S in Si]
ans = H + W
for i in range(2 ** (H - 1)):
h_border = bin(i).count('1')
w_border = 0
white_counts = [0] * (h_border + 1)
choco_w = 0
for w in range(W):
choco_w += 1
wc_num = 0
tmp_count = [0] * (h_border + 1)
for h in range(H):
white_counts[wc_num] += Si[h][w]
tmp_count[wc_num] += Si[h][w]
if i >> h & 1:
wc_num += 1
if max(white_counts) > K:
if choco_w == 1:
break # 1列の時点で > K の場合は条件を達成できない
w_border += 1
white_counts = tmp_count
choco_w = 1
else:
ans = min(ans, h_border + w_border)
print(ans)
if __name__ == '__main__':
H, W, K = map(int, input().split())
Si = [input() for _ in range(H)]
solve(H, W, K, Si)
# # test
# from random import randint
# from func import random_str
#
# H, W, K = 10, 1000, randint(1, 100)
# Si = [random_str(W, '01') for _ in range(H)]
# print(H, W, K)
# for S in Si:
# print(S)
# solve(H, W, K, Si)
| 0 | null | 24,438,598,170,012 | 19 | 193 |
n,k = map(int,input().split())
h = list(map(int,input().split()))
h.sort(reverse = True)
m = 0
for i in range(k,n):
m += h[i]
print(m) | N=int(input())
c=-(-N//2)-1
print(c) | 0 | null | 116,085,699,883,150 | 227 | 283 |
import numpy as np
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
from numba import njit
def getInputs():
D = int(readline())
CS = np.array(read().split(), np.int32)
C = CS[:26]
S = CS[26:].reshape((-1, 26))
return D, C, S
def _compute_score(output, i, d, last):
mask = np.ones((26, ), np.int32)
mask[i] = 0
score = S[d][i] - np.sum(C * (d + 1 - last) * mask)
return score
def _evaluate(output, i, d, last, k):
score = _compute_score(output, i, d, last)
score -= np.sum(C * (d + k + 1 - last))
return score
def solve(k):
output = np.array([], np.int32)
last = np.zeros((26, ), np.int32)
SCORE = 0
for d in range(D):
max_score = float('-inf')
best_i = 0
for i in range(26):
output = np.append(output, i)
#score = _compute_score(output, i, d, last)
score = _evaluate(output, i, d, last, k)
if max_score < score:
max_score = score
best_i = i + 1
output = output[:-1]
output = np.append(output, best_i)
last[best_i - 1] = d + 1
SCORE += max_score
return output, SCORE
D, C, S = getInputs()
max_score = float('-inf')
for k in range(7, 14):
ans, score = solve(k)
if max_score < score:
max_score = score
ANS = ans
print('\n'.join(ANS.astype(str).tolist())) | import sys
def resolve(in_):
N, M = map(int, next(in_).split())
PS = tuple(line.strip().split() for line in in_)
ac = set()
wa = {}
for p, s in PS:
if s == 'AC':
ac.add(p)
if s == 'WA' and p not in ac:
wa[p] = wa.setdefault(p, 0) + 1
penalties = 0
for k, v in wa.items():
if k in ac:
penalties += v
return '{} {}'.format(len(ac), penalties)
def main():
answer = resolve(sys.stdin)
print(answer)
if __name__ == '__main__':
main()
| 0 | null | 51,850,146,336,040 | 113 | 240 |
N, K = map(int, input().split())
A = [-1] * (N+1)
for i in range(K):
d_list = int(input())
B = input().split()
B = [int(x) for x in B]
for i in range(1, N+1):
if i in B:
A[i] = 1
count = 0
for i in range(1, N+1):
if A[i] == -1:
count += 1
print(count) | N,K = map(int,input().split())
list1 ={i for i in range(1,N+1)}
list2 = set()
for i in range(K):
NN = int(input())
MM = map(int,input().split())
for j in MM:
list2.add(j)
ans = list1 -list2
print(len(ans)) | 1 | 24,819,643,977,568 | null | 154 | 154 |
from math import gcd
from functools import reduce
def lcm_base(x, y):
return (x * y) // gcd(x, y)
def lcm(*numbers):
return reduce(lcm_base, numbers, 1)
def evencount(n):
cnt=0
while n%2==0:
cnt+=1
n//=2
return cnt
n,m=map(int,input().split())
a=list(map(int,input().split()))
for i in range(n):
a[i]//=2
if all(evencount(i) == evencount(a[0]) for i in a):
d=lcm(*a)
print((m+d)//2//d)
else:print(0) | 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,M = i_map()
a = i_list()
cd = []
new_a = []
for i in a:
cd.append(i//2)
new_a.append((i//2))
def pow2(x):
ret = 0
while x%2 == 0:
ret += 1
x//=2
return ret
P = []
for a in new_a:
P.append(pow2(a))
if len(set(P)) != 1:
print(0)
else:
m = 1
for a in new_a:
m = (m*a)//gcd(m,a)
print(-(-(M//m)//2))
if __name__=="__main__":
main()
| 1 | 101,631,322,329,822 | null | 247 | 247 |
from itertools import *
from collections import *
from functools import *
def isqrt(n):
if n > 0:
x = 1 << (n.bit_length() + 1 >> 1)
while True:
y = (x + n // x) >> 1
if y >= x:
return x
x = y
elif n == 0:
return 0
else:
raise ValueError
def qrime():
yield from [2, 3, 5, 7]
q = [i for i in range(1, 210, 2) if 0 not in (i%3, i%5, i%7)]
yield from q[1:]
for i in count(210, 210):
for j in q:
yield i + j
def factor(n):
p = Counter()
limit = isqrt(n)
for q in qrime():
if q > limit:
break
while n % q ==0:
p[q] += 1
n //= q
if q in p:
limit = isqrt(n)
if n != 1:
p[n] += 1
return p
def divisor(n):
p, m = zip(*factor(n).items())
for c in product(*map(lambda x:range(x+1), m)):
yield reduce(int.__mul__, (x**y for x, y in zip(p, c) if y), 1)
N = int(input())
ans = reduce(int.__mul__, (c+1 for c in factor(N-1).values()), 1) - 1
for d in divisor(N):
if d == 1:
continue
n = N
while n % d == 0:
n //= d
if n % d == 1:
ans += 1
print(ans)
| N = int(input())
if N == 2:
print(1)
exit()
S = set([N-1])
lst = [N]
i = 2
while i*i <= N-1:
if (N-1) % i == 0:
S.add(i)
S.add((N-1)//i)
i += 1
i = 2
while i*i < N:
if N % i == 0:
lst.append(i)
lst.append(N//i)
i += 1
if i*i == N:
lst.append(i)
lst.sort()
for l in lst:
if l in S:
continue
_N = N
while _N % l == 0:
_N //= l
if _N % l == 1:
S.add(l)
print(len(S)) | 1 | 41,143,983,604,920 | null | 183 | 183 |
# 169 B
N = int(input())
A = list(map(int, input().split()))
A.sort()
ans = 1
for i in range(N):
ans *= A[i]
if ans > 10**18:
print(-1)
break
else:
print(ans) | A, B = map(int, input().split())
print(A-B*2) if A>B*2 else print(0) | 0 | null | 91,670,409,140,358 | 134 | 291 |
S=input()
K=int(input())
if list(S).count(S[0])==len(S):
print(len(S)*K//2)
else:
S_count=0
i=0
while i<len(S)-1:
if S[i]==S[i+1]:
S_count+=1
i+=1
i+=1
if S[0]==S[-1]:
a=1
i=0
while i<len(S)-1:
if S[i]==S[i+1]:
a+=1
i+=1
else:
break
b=1
i=len(S)-1
while i>0:
if S[i]==S[i-1]:
b+=1
i+=-1
else:
break
S_count*=K
S_count-=(K-1)*(a//2 + b//2 - (a+b)//2)
else:
S_count*=K
print(S_count)
| from itertools import groupby
def solve():
S = input()
K = int(input())
a = [len(tuple(s)) for _, s in groupby(S)]
if S[0] == S[-1] and K > 1:
if len(a) == 1:
return a[0] * K // 2
else:
ans = a[0] // 2 + a[-1] // 2 + (a[0]+a[-1]) // 2 * (K-1)
ans += sum(n // 2 for n in a[1:-1]) * K
return ans
else:
return sum(n // 2 for n in a) * K
print(solve()) | 1 | 175,507,162,881,920 | null | 296 | 296 |
import sys
print('Yes' if len(set(sys.stdin.read().strip())) >= 2 else 'No') | mount = []
for i in range(0, 10):
n = input()
mount.append(n)
mount.sort(reverse = True)
for i in range(0, 3):
print mount[i] | 0 | null | 27,281,786,762,198 | 201 | 2 |
s,t = input().split()
a,b = map(int,input().split())
A = input()
if A == s:print(a-1,b)
else:print(a,b-1) | import math
from functools import reduce
K = int(input())
def gcd(*numbers):
return reduce(math.gcd, numbers)
ans = 0
for a in range(1,K+1):
if (a==1):
ans += K*K
continue
for b in range(1,K+1):
t = gcd(a,b)
if(t==1):
ans += K
continue
for c in range(1,K+1):
ans += gcd(t,c)
print(ans) | 0 | null | 53,594,573,645,410 | 220 | 174 |
a, b, m = map(int, input().split())
pa = list(map(int, input().split()))
pb = list(map(int, input().split()))
ans = min(pa) + min(pb)
for _ in range(m):
x, y, c = map(int, input().split())
x -= 1
y -= 1
ans = min(ans, pa[x] + pb[y] - c)
print(ans)
| import numpy as np
import sys
input = sys.stdin.readline
A,B,M = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
res = np.min(a) + np.min(b)
for i in range(M):
x,y,c = map(int,input().split())
x-=1
y-=1
res = min(res,(a[x]+b[y]-c))
print(res) | 1 | 53,865,170,800,100 | null | 200 | 200 |
N = int(input())
a_list = list(map(int, input().split()))
c = 0
for i in range(N-1):
minj = i
for j in range(i+1, N):
if a_list[j] < a_list[minj]:
minj = j
if a_list[i] != a_list[minj]:
a_list[i], a_list[minj] = a_list[minj], a_list[i]
c += 1
print(' '.join(map(str, a_list)))
print(c)
| import math
#入力:N,M(int:整数)
def input2():
return map(int,input().split())
#入力:[n1,n2,...nk](int:整数配列)
def input_array():
return list(map(int,input().split()))
n,x,t=input2()
ans=(math.ceil(n/x))*t
print(ans) | 0 | null | 2,113,660,285,710 | 15 | 86 |
def gcd(s,t):
if t==0:
return s
else:
return gcd(t, s%t)
s, t = [int(x) for x in input().split()]
GCD = gcd(s,t)
LCM = (s*t)//GCD
print(LCM) | def gcd(a,b):
while a!=0 and b!=0:
if a>b:
c = a
a = b
b = c
b %= a
return max(a,b)
a,b = map(int,input().split())
print(a*b//gcd(a,b)) | 1 | 113,211,331,223,648 | null | 256 | 256 |
n = int(input())
a = list(map(int, input().split()))
x = "APPROVED"
for i in range(n):
if a[i] == (2 * int(a[i] / 2)):
if a[i] != (3 * int(a[i] / 3)):
if a[i] != (5 * int(a[i] / 5)):
x = "DENIED"
print(str(x)) | import sys
N = int(input())
array = list(map(int,input().split()))
if not ( 1 <= N <= 100 ): sys.exit()
if not ( 1 <= min(array) and max(array) <= 1000 ): sys.exit()
for I in array:
if I % 2 == 0 and not ( I % 3 == 0 or I % 5 == 0 ):
print('DENIED')
sys.exit()
print('APPROVED') | 1 | 68,767,241,847,140 | null | 217 | 217 |
n, x, m = map(int, input().split())
lst, num, flag = set(), [], False
for i in range(1, n + 1):
lst.add(x), num.append(x)
x = x ** 2 % m
if x in lst:
flag = True
break
ans = sum(num)
if flag:
cnt, idx = i, num.index(x)
div, mod = divmod(n - cnt, len(num) - idx)
ans += sum(num[idx:idx + mod])
ans += sum(num[idx:]) * div
print(ans)
| n,x,m=map(int,input().split())
logn = (10**10).bit_length()
doubling = []
sumd = []
for _ in range(logn):
tmpd = [-1] * m
doubling.append(tmpd)
tmps = [0] * m
sumd.append(tmps)
for i in range(m):
doubling[0][i] = i * i % m
sumd[0][i] = i
for i in range(1,logn):
for j in range(m):
doubling[i][j] = doubling[i-1][doubling[i-1][j]]
sumd[i][j] = sumd[i-1][j] + sumd[i-1][doubling[i-1][j]]
now = x
pos = 0
result = 0
while n:
if n & 1:
result += sumd[pos][now]
now = doubling[pos][now]
n = n >> 1
pos += 1
print(result)
| 1 | 2,829,470,394,228 | null | 75 | 75 |
#!/usr/bin/env python3
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = [0]
b = [0]
for i in range(N):
a.append(a[i] + A[i])
for i in range(M):
b.append(b[i] + B[i])
count = 0
j = M
for i in range(N+1):
if a[i] > K:
break
while b[j] > K - a[i]:
j -= 1
count = max(count, i+j)
print(count)
| S=str(input())
if 'SUN'in S:
print(7)
elif 'MON' in S:
print(6)
elif 'TUE' in S:
print(5)
elif 'WED' in S:
print(4)
elif 'THU' in S:
print(3)
elif 'FRI' in S:
print(2)
elif 'SAT' in S:
print(1) | 0 | null | 71,758,218,405,620 | 117 | 270 |
import sys
A, B, M = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
t = sys.maxsize
for _ in range(M):
x, y, c = [int(i) for i in input().split()]
t = min(t, a[x - 1] + b[y - 1] - c)
print(min(t, sorted(a)[0] + sorted(b)[0])) | L = int(input())
print(L **3 / 27) | 0 | null | 50,253,604,843,352 | 200 | 191 |
from collections import deque
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
score = dict(r=P, s=R, p=S)
ans = 0
q = deque()
for i in range(K):
s = T[i]
ans += score[s]
q.append(s)
for i in range(K, N):
s = T[i]
s_pre_k = q.popleft()
if s == s_pre_k:
q.append('n')
else:
ans += score[s]
q.append(s)
print(ans)
| from itertools import combinations
n = int(input())
d = list(map(int, input().split()))
ans = 0
d = combinations(d, 2)
for i, j in d:
ans += i*j
print(ans)
| 0 | null | 137,287,083,039,792 | 251 | 292 |
[X,Y] = list(map(int,input().split()))
n = (-1*X+2*Y)//3
m = (2*X-Y)//3
if (X+Y)%3 !=0:
print(0)
elif n<0 or m<0:
print(0)
else:
MAXN = (10**6)+10
MOD = 10**9 + 7
f = [1]
for i in range(MAXN):
f.append(f[-1] * (i+1) % MOD)
def nCr(n, r, mod=MOD):
return f[n] * pow(f[r], mod-2, mod) * pow(f[n-r], mod-2, mod) % mod
print(nCr(n+m,n,10**9 + 7))
| import sys
import itertools
# import numpy as np
import time
import math
sys.setrecursionlimit(10 ** 7)
from collections import defaultdict
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
X, Y = map(int, input().split())
if (X + Y) % 3 != 0:
print(0)
exit()
MAX = 2 * 10 ** 6 + 2
MOD = 10 ** 9 + 7
fac = [0 for i in range(MAX)]
finv = [0 for i in range(MAX)]
inv = [0 for i in range(MAX)]
def comInit(mod):
fac[0], fac[1] = 1, 1
finv[0], finv[1] = 1, 1
inv[1] = 1
for i in range(2, MAX):
fac[i] = fac[i - 1] * i % mod
inv[i] = mod - inv[mod % i] * (mod // i) % mod
finv[i] = finv[i - 1] * inv[i] % mod
def com(n, r, mod):
if n < r:
return 0
if n < 0 or r < 0:
return 0
return fac[n] * (finv[r] * finv[n - r] % mod) % mod
comInit(MOD)
if X < Y:
X, Y = Y, X
b = (2 * X - Y) // 3
a = X - 2 * b
print(com(a + b, b, MOD))
| 1 | 150,185,464,434,642 | null | 281 | 281 |
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)
def main():
n = I()
ab = [LI() for _ in range(n)]
aa = sorted(map(lambda x: x[0], ab))
ba = sorted(map(lambda x: x[1], ab))
if n % 2 == 1:
a = aa[n//2]
b = ba[n//2]
return b - a + 1
a1 = aa[n//2-1]
a2 = aa[n//2]
b1 = ba[n//2-1]
b2 = ba[n//2]
r = b2 - a2
r += b1 - a1
return r + 1
print(main())
| def main():
n = int(input())
A = [0]*n
B = [0]*n
for i in range(n):
a,b = map(int,input().split())
A[i] = a
B[i] = b
A.sort()
B.sort()
if n%2 == 0:
m1 = (A[n//2-1]+A[n//2])/2
m2 = (B[n//2-1]+B[n//2])/2
print(int(2*(m2-m1))+1)
else:
print(B[(n+1)//2-1]-A[(n+1)//2-1]+1)
main() | 1 | 17,245,525,699,060 | null | 137 | 137 |
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
ans = [0] * N
for a_i in A:
ans[a_i-1] += 1
for a in ans:
print(a) | S = input()
s_rev = S[::-1]
r_list = [0] * 2019
r_list[0] = 1
num, d = 0, 1
for i in range(len(S)):
num += d*int(s_rev[i])
num %= 2019
r_list[num] += 1
d *= 10
d %= 2019
ans = 0
for i in range(2019):
ans += r_list[i]*(r_list[i]-1)//2
print(ans)
| 0 | null | 31,827,520,996,480 | 169 | 166 |
print('Yes' if sum(map(int, input()))%9 == 0 else 'No')
|
def resolve():
# 十進数表記で1~9までの数字がK個入るN桁の数字の数を答える問題
S = input()
K = int(input())
n = len(S)
# dp[i][k][smaller]:
# i:桁数
# K:0以外の数字を使った回数
# smaller:iまでの桁で値以上になっていないかのフラグ
dp = [[[0] * 2 for _ in range(4)] for _ in range(105)]
dp[0][0][0] = 1
for i in range(n):
for j in range(4):
for k in range(2):
nd = int(S[i])
for d in range(10):
ni = i+1
nj = j
nk = k
if d != 0:
nj += 1
if nj > K:
continue
if k == 0:
if d > nd: # 値を超えている
continue
if d < nd:
nk += 1
dp[ni][nj][nk] += dp[i][j][k]
ans = dp[n][K][0] + dp[n][K][1]
print(ans)
if __name__ == "__main__":
resolve() | 0 | null | 40,299,463,591,860 | 87 | 224 |
def F(x, y):
if x % y == 0:
return y
return F(y, x % y)
while True:
try:
x, y = map(int,input().split())
print('{0} {1}'.format(F(x, y), int(x * y / F(x, y))))
except EOFError:
break
| n = int(input())
A = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
u, k, *v = list(map(int, input().split()))
for j in v:
A[int(u)-1][int(j)-1] = 1
# A[int(j)-1][int(u)-1] = 1
# for row in A:
# msg = ' '.join(map(str, row))
# # print(msg)
d = [-1] * n
f = [-1] * n
t = 0
# recursive
def dfs(u):
global t
# print('visit:', str(u), str(t))
t += 1
d[u] = t
for i in range(len(A[u])):
if A[u][i] == 1 and d[i] == -1:
dfs(i)
t += 1
f[u] = t
for i in range(n):
if d[i] == -1:
# print('start from:', str(i))
dfs(i)
for i in range(n):
print(str(i+1), str(d[i]), str(f[i]))
| 0 | null | 2,062,466,432 | 5 | 8 |
#coding:utf-8
n = int(input())
S = list(map(int, input().split()))
q = int(input())
T = list(map(int, input().split()))
def search_banpei(array, target, cnt):
tmp = array[len(array)-1]
array[len(array)-1] = target
n = 0
while array[n] != target:
n += 1
array[len(array)-1] = tmp
if n < len(array) - 1 or target == tmp:
cnt += 1
return cnt
def linear_search():
cnt = 0
for t in T:
for s in S:
if t == s:
cnt += 1
break
def linear_banpei_search():
cnt = 0
for target in T:
cnt = search_banpei(S, target, cnt)
return cnt
cnt = linear_banpei_search()
print(cnt)
| n=int(input())
S=input().split(' ')
q=int(input())
T=input().split(' ')
def equal_element(S,T):
m=0
for i in range(len(T)):
for j in range(len(S)):
if S[j]==T[i]:
m+=1
break
return(m)
print(equal_element(S,T))
| 1 | 67,491,929,050 | null | 22 | 22 |
n=int(input())
l=list(map(int,input().split()))
ans=[0]*n
for i in range(len(l)):
ans[l[i]-1]=i+1
print(*ans) | r,c = map(int,raw_input().split())
matrix = []
for i in range(r):
matrix.append(map(int,raw_input().split()))
a = [0 for j in range(c)]
matrix.append(a)
for i in range(r):
sum = 0
for j in range(c):
sum += matrix[i][j]
matrix[i].append(sum)
for j in range(c):
sum = 0
for i in range(r):
sum += matrix[i][j]
matrix[r][j] = sum
sum = 0
for j in range(c):
sum += matrix[r][j]
matrix[r].append(sum)
for i in range(r):
for j in range(c):
print matrix[i][j],
print matrix[i][c]
for j in range(c + 1):
print matrix[r][j], | 0 | null | 91,243,214,110,088 | 299 | 59 |
def solve(x):
if len(x) <= 1:
return 0
else:
return (1 if x[0] != x[-1] else 0) + solve(x[1:-1])
s = input()
print(solve(s)) | from operator import itemgetter
import bisect
N, D, A = map(int, input().split())
enemies = sorted([list(map(int, input().split())) for i in range(N)], key=itemgetter(0))
d_enemy = [enemy[0] for enemy in enemies]
b_left = bisect.bisect_left
logs = []
logs_S = [0, ]
ans = 0
for i, enemy in enumerate(enemies):
X, hp = enemy
start_i = b_left(logs, X-2*D)
count = logs_S[-1] - logs_S[start_i]
hp -= count * A
if hp > 0:
attack_num = (hp + A-1) // A
logs.append(X)
logs_S.append(logs_S[-1]+attack_num)
ans += attack_num
print(ans)
| 0 | null | 101,655,932,873,628 | 261 | 230 |
n, x, t = [int(n) for n in input().split(' ')]
a = n // x * t
if n % x != 0:
a += t
print(a)
| n=int(input())
m=100
y=0
while m < n:
m += m//100
y += 1
print(y) | 0 | null | 15,821,397,351,372 | 86 | 159 |
downs, ponds = [], []
for i, s in enumerate(input()):
if s == "\\":
downs.append(i)
elif s == "/" and downs:
i_down = downs.pop()
area = i - i_down
while ponds and ponds[-1][0] > i_down:
area += ponds.pop()[1]
ponds.append([i_down, area])
print(sum(p[1] for p in ponds))
print(len(ponds), *(p[1] for p in ponds))
| # -*- coding: utf-8 -*-
l = input()
S1, S2 = [], []
sum = 0
n = len(l)
for i in range(n):
if l[i] == "\\":
S1.append(i)
elif l[i] == "/" and S1:
j = S1.pop()
a = i - j
sum += a
while S2 and S2[-1][0] > j:
a += S2.pop()[1]
S2.append([j, a])
print(sum)
print(len(S2), *(a for j, a in S2)) | 1 | 60,074,025,488 | null | 21 | 21 |
h = int(input())
w = int(input())
n = int(input())
if h >= w and n%h != 0:
print(n//h+1)
elif h >= w and n%h == 0:
print(n//h)
elif w >= h and n%w != 0:
print(n//w+1)
else:
print(n//w) | import itertools
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
R = [i+1 for i in range(N)]
RPS = sorted(list(itertools.permutations(R)))
print(abs(RPS.index(P)-RPS.index(Q))) | 0 | null | 94,636,741,947,228 | 236 | 246 |
def main():
N = int(input())
a = [int(i) for i in input().split()]
x = 0
for i in range(N):
x ^= a[i]
x ^= a[0]
xors = [x]
for i in range(1, N):
x ^= a[i-1]
x ^= a[i]
xors.append(x)
print(' '.join(str(x) for x in xors))
main()
| # -*- coding:utf-8
def main():
N = int(input())
A = list(map(int, input().split()))
insertionSort(A, N)
print(' '.join(map(str, A)))
def insertionSort(A, N):
for i in range(1, N):
print(' '.join(map(str, A)))
v = A[i]
j = i-1
while(j>=0 and A[j]>v):
A[j+1] = A[j]
j -= 1
A[j+1] = v
if __name__ == '__main__':
main() | 0 | null | 6,243,243,288,342 | 123 | 10 |
n = input()
Max = -4300000000
Min = input()
for i in range(n - 1):
IN_NOW = input()
Max = max(Max, (IN_NOW - Min))
Min = min(Min, IN_NOW)
print Max | n = int(input())
a = list(map(int, input().split()))
ans = [0]*n
for i, a in enumerate(a):
ans[a-1] += 1
print(*ans, sep="\n") | 0 | null | 16,133,794,586,208 | 13 | 169 |
S = input()
if S >= 0 and S <= 86400:
a = S // 3600
b = S % 3600
c = b // 60
d = b % 60
print "%d:%d:%d" % (a, c, d) | s = int(input())
sec = s % 60
minbuff = int(s / 60)
m = minbuff % 60
h = int(minbuff / 60)
print("{}:{}:{}".format(h, m, sec))
| 1 | 322,357,435,968 | null | 37 | 37 |
from sys import stdin
rooms = [[[0] * 10 for _ in range(3)] for _ in range(4)]
stdin.readline().rstrip()
while True:
try:
b, f, r, v = [int(x) for x in stdin.readline().rstrip().split()]
rooms[b-1][f-1][r-1] += v
except:
break
for b in range(4):
for f in range(3):
print(" ", end = "")
print(*rooms[b][f])
else:
if b != 3:
print("#"*20)
| a,b,c = map(int,input().split())
if a>= c:
print(a-c,b)
elif a+b<=c:
print("0 0")
elif a < c and c < a+b:
print("0",a+b-c) | 0 | null | 52,711,843,979,732 | 55 | 249 |
i=input;i();x=1;l=i().split()
if '0' in l:
print(0)
quit()
for j in l:
x*=int(j)
if x > 1e18:
print(-1);quit()
print(x)
| N=int(input())
A=list(map(int,input().split()))
ans=A[0]
if 0 in A:
print(0)
exit()
for cnt in range(N-1):
ans = ans * A[cnt+1]
if ans > 1000000000000000000:
break
elif ans == 0:
break
if ans > 1000000000000000000:
print(-1)
else:
print(ans) | 1 | 16,267,881,416,150 | null | 134 | 134 |
S = int(input())
mod = 10**9+7
dp = [1]+[0]*S
for i in range(1, S+1):
for j in range(0, i-2):
dp[i] += dp[j]
print(dp[S]%mod)
| def perm(n, k, p):
ret = 1
for i in range(n, n-k, -1):
ret = (ret * i)%p
return ret
def comb(n, k, p):
a = perm(n, k, p)
b = perm(k, k, p)
return (a*pow(b, -1, p))%p
S = int(input())
ans = 0
S -= 3
t = 0
while S >= 0:
ans += comb(S+t,t, 10**9+7)
S -= 3
t += 1
print(ans%(10**9+7)) | 1 | 3,293,852,367,610 | null | 79 | 79 |
s = input()
t = input()
cnt = 0
ans = 0
while cnt < len(s) - len(t) + 1:
chk = 0
for i, j in zip(range(cnt,cnt+len(t)),range(len(t))):
if s[i] == t[j]:
chk += 1
ans = max(ans,chk)
cnt += 1
print(len(t) - ans) | data = []
da = []
a = input().split()
a[0] = int(a[0])
a[1] = int(a[1])
for i in range(a[0]):
b = input().split()
for j in range(a[1]):
b[j] = int(b[j])
data.append(b)
for i in range(len(data)):
data[i].append(sum(data[i]))
for i in range(a[1]+1):
da.append(0)
for i in range(a[0]):
for j in range(a[1]+1):
da[j] += data[i][j]
data.append(da)
for i in data:
for j in range(len(i)):
i[j] = str(i[j])
for i in data:
print(" ".join(i))
| 0 | null | 2,482,973,303,598 | 82 | 59 |
x = int(input()) - 400
ans = x // 200
print(8 - ans) | import math as mt
import sys, string
from collections import Counter, defaultdict
input = sys.stdin.readline
MOD = 1000000007
# input functions
I = lambda : int(input())
M = lambda : map(int, input().split())
Ms = lambda : map(str, input().split())
ARR = lambda: list(map(int, input().split()))
def main():
n = I()
n -= 400
print(8 - n//200)
# testcases
tc = 1
for _ in range(tc):
main() | 1 | 6,755,408,762,080 | null | 100 | 100 |
def getN():
return int(input())
def getNM():
return map(int, input().split())
def getList():
return list(map(int, input().split()))
def getArray(intn):
return [int(input()) for i in range(intn)]
def input():
return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
ns = []
while len(ns) < rantime:
n = random.randint(ran1, ran2)
if not n in ns:
ns.append(n)
return sorted(ns)
def rand_query(ran1, ran2, rantime):
r_query = []
while len(r_query) < rantime:
n_q = rand_ints_nodup(ran1, ran2, 2)
if not n_q in r_query:
r_query.append(n_q)
return sorted(r_query)
from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
import math
import random
import string
from copy import deepcopy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right
import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7
#############
# Main Code #
#############
"""
N日間のうちK日選んで働く
働いたらそれからC日間は働かない
Sにxがついていたら働かない
oの付いてる日のみ働く
全ての通りを求めてみよう
N, K, C = 11, 3, 2
S = ooxxxoxxxoo の時
働けるのは[1, 2, 6, 10, 11]日目
ここから3つ以上間が空く条件で3つ選ぶと条件を満たす
全ての通りでi日目に働く必要がある
A = [1, 2, 5, 7, 11, 13, 16]
M = 4
C = 3
for bit in range(1 << len(A)):
cnt = []
for i in range(len(A)):
if bit & (1 << i):
cnt.append(A[i])
if len(cnt) == 4:
for i in range(1, len(cnt)):
if cnt[i] - cnt[i - 1] <= C:
break
else:
print(cnt)
# python index.py
[1, 5, 11, 16]
[1, 7, 11, 16]
[2, 7, 11, 16]
A = [1, 2, 5, 10, 24]
M = 3
C = 2
[1, 5, 10]
[2, 5, 10]
[1, 5, 24]
[2, 5, 24]
[1, 10, 24]
[2, 10, 24]
[5, 10, 24]
[1, 5, 10, 24] M + 1回以上ジャンプできる場合には答えが[]になる
A[0]から頑張ってもM回しかジャンプできない
間がC + 1以上離れたM個の集合はどのように求める?
必ず働く日の特性は?
動かすと?
場所は固定?
"""
# N, K, C = getNM()
# S = getN()
N, K, C = getNM()
S = input()
# i回目に選べる要素の上限と下限を比べる
place = []
for i in range(N):
if S[i] == 'o':
place.append(i)
# 下限 出来るだけ前の仕事を選べるように
fore = [place[0]]
for i in place[1:]:
if i - fore[-1] > C:
fore.append(i)
# 上限 出来るだけ後ろの仕事を選ぶように
back = deque([])
back.append(place[-1])
for i in place[::-1]:
if back[0] - i > C:
back.appendleft(i)
back = list(back)
if len(fore) != K:
print()
exit()
ans = []
for a, b in zip(fore, back):
if a == b:
ans.append(a + 1)
for i in ans:
print(i)
"""
fore = [0] * N
if S[0] == 'o':
fore[0] = 1
for i in range(1, N):
fore[i] = fore[i - 1]
if S[i] == 'o' and i - C - 1 >= 0:
fore[i] = max(fore[i], fore[i - C - 1] + 1)
back = [float('inf')] * N
ma = max(fore)
if S[N - 1] == 'o':
back[N - 1] = ma
for i in range(N - 2, -1, -1):
back[i] = min(ma, back[i + 1])
if S[i] == 'o' and i + C + 1 < N:
back[i] = min(back[i], back[i + C + 1] - 1)
print(back)
""" | # 解説を参考に作成
def solve():
N, K, C = map(int, input().split())
S = input()
left = []
rest = 0
work = 0
for i in range(N):
if S[i] == 'o' and rest == 0:
left.append(i)
rest = C + 1
work += 1
if rest > 0:
rest -= 1
if work == K:
break
right = []
rest = 0
work = 0
for i in reversed(range(N)):
if S[i] == 'o' and rest == 0:
right.append(i)
rest = C + 1
work += 1
if rest > 0:
rest -= 1
if work == K:
break
right = list(reversed(right))
# print(left)
# print(right)
for i in range(len(left)):
if left[i] == right[i]:
print(left[i] + 1)
if __name__ == '__main__':
solve()
| 1 | 40,706,451,425,910 | null | 182 | 182 |
a = input()
a = a.split()
x=int(a[0])
y=int(a[1])
if x>y:
print("safe")
else:
print("unsafe")
| m1, _ = list(map(int, input().split()))
m2, _ = list(map(int, input().split()))
if m1 == m2:
print(0)
else:
print(1) | 0 | null | 76,578,591,235,548 | 163 | 264 |
N,K = map(int,input().split())
P = list(map(int,input().split()))
a = 0
for i in range(K):
a += min(P)
del P[P.index(min(P))]
print(a) | n, k = input().strip().split()
fruits = list(map(int, input().strip().split()))
fruits.sort()
print(sum(fruits[0:int(k)])) | 1 | 11,542,954,382,750 | null | 120 | 120 |
a, b = map(int, input().split())
print(a // b, a % b, "{:f}".format(a / b)) | line = input()
words = line.split()
nums = list(map(int, words))
a = nums[0]
b = nums[1]
d = a // b
r = a % b
f = a / b
f = round(f,5)
print ("{0} {1} {2}".format(d,r,f)) | 1 | 594,296,072,092 | null | 45 | 45 |
n = int(input())
S = [1]*13
H = [1]*13
C = [1]*13
D = [1]*13
mark = [""]*n
num = [""]*n
for i in range(n):
mark[i], num[i] = input().split()
for i in range(n):
if mark[i] == "S":
S[int(num[i])-1] = 0
if mark[i] == "H":
H[int(num[i])-1] = 0
if mark[i] == "C":
C[int(num[i])-1] = 0
if mark[i] == "D":
D[int(num[i])-1] = 0
for i in range(13):
if S[i]:
print("S " + str(i+1))
for i in range(13):
if H[i]:
print("H " + str(i+1))
for i in range(13):
if C[i]:
print("C " + str(i+1))
for i in range(13):
if D[i]:
print("D " + str(i+1)) | n = input()
a = [raw_input() for _ in range(n)]
b = []
for i in ['S ','H ','C ','D ']:
for j in range(1,14):
b.append(i+str(j))
for i in a:
b.remove(i)
for i in b:
print i | 1 | 1,042,486,734,180 | null | 54 | 54 |
R=range
l=[[[0 for i in R(10)]for j in R(3)]for s in R(4)]
for i in R(int(input())):
b,f,r,v=map(int,input().split())
l[b-1][f-1][r-1]+=v
for i in R(4):
for k in l[i]:print("",*k)
if i!=3:print("#"*20) | import sys
a=[]
for t in range(4):
a.append([])
for f in range(3):
a[t].append([])
for r in range(10):
a[t][f].append(0)
lines = [line for line in sys.stdin]
n = lines[0]
for l in lines[1:]:
b, f, r, v = map(int,l.split())
b -= 1
f -= 1
r -= 1
a[b][f][r] += v
for i,t in enumerate(a):
for f in t:
print (" " +" ".join(map(str,f)))
if len(a) != i +1:
print ('#'*20) | 1 | 1,095,012,803,752 | null | 55 | 55 |
#!/usr/bin/env python
# encoding: utf-8
from __future__ import print_function
class Solution:
"""
@param prices: Given an integer array
@return: Maximum profit
"""
@staticmethod
def insertion_sort():
# write your code here
array_length = int(input())
unsorted_array = [int(x) for x in input().split()]
for i in range(array_length):
v = unsorted_array[i]
j = i - 1
while j >= 0 and unsorted_array[j] > v:
unsorted_array[j + 1] = unsorted_array[j]
j -= 1
unsorted_array[j + 1] = v
print(" ".join(map(str, unsorted_array)))
if __name__ == '__main__':
solution = Solution()
solution.insertion_sort() | n = int(input())
A = list(map(int, input().split()))
print(' '.join(list(map(str, A))))
for i in range(1,n):
key = A[i]
j = i - 1
while j >= 0 and A[j] > key:
A[j+1] = A[j]
j -= 1
A[j+1] = key
print(' '.join(list(map(str, A)))) | 1 | 5,484,846,652 | null | 10 | 10 |
x = int(input())
flag = 0
for a in range(x):
b5 = a**5 - x
for b in range(120):
if abs(b5) == int(b**5):
if b5 < 0:
b = -b
flag = 1
break
if flag:
break
print(a, int(b))
| n=int(input())
a=list(map(int,input().split()))
dp=[0]*(n+1)
for i in range(n-1):
dp[a[i]]+=1
for i in range(n):
print(dp[i+1]) | 0 | null | 28,804,773,538,620 | 156 | 169 |
#!/usr/bin/python
#-coding:utf8-
import sys
for s in sys.stdin:
data = map(int,s.split())
print len(str(data[0]+data[1])) | x = []
try:
while True:
a, b = map(int, raw_input().split())
x.append(len(list(str(a + b))))
except EOFError:
for i in x:
print(i) | 1 | 140,117,080 | null | 3 | 3 |
N = input()
Sum = 0
for n in list(N):
Sum = Sum + int(n)
ANS = 'Yes' if Sum % 9 == 0 else 'No'
print(ANS) | n = list(input())
tmp = 0
for i in n:
tmp += int(i) % 9
ans = "Yes" if tmp % 9 == 0 else "No"
print(ans)
| 1 | 4,415,919,625,176 | null | 87 | 87 |
class SegTree:
def __init__(self, init_val, ide_ele, segfunc):
self.n = len(init_val)
self.num =2**(self.n-1).bit_length()
self.ide_ele = ide_ele
self.seg = [self.ide_ele]*2*self.num
self.segfunc = segfunc
#set_val
for i in range(self.n):
self.seg[i+self.num-1] = init_val[i]
#built
for i in range(self.num-2,-1,-1) :
self.seg[i] = segfunc(self.seg[2*i+1], self.seg[2*i+2])
def update(self, k, x):
k += self.num-1
self.seg[k] = x
while k+1:
k = (k-1)//2
self.seg[k] = self.segfunc(self.seg[k*2+1], self.seg[k*2+2])
def query(self, p, q):
if q<=p:
return self.ide_ele
p += self.num-1
q += self.num-2
res = self.ide_ele
while q-p>1:
if p&1 == 0:
res = self.segfunc(res, self.seg[p])
if q&1 == 1:
res = self.segfunc(res, self.seg[q])
q -= 1
p = p//2
q = (q-1)//2
if p == q:
res = self.segfunc(res, self.seg[p])
else:
res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q])
return res
import sys
input = sys.stdin.readline
N = int(input())
S = input()
L = [-1]*N
for i in range(N):
L[i] = 2**(ord(S[i]) - ord('a'))
def segfunc(a,b):
return a | b
Seg = SegTree(L,0,segfunc)
Q = int(input())
for _ in range(Q):
q,a,b = input().split()
if q == '1':
i = int(a)-1
c = 2**(ord(b) - ord('a'))
Seg.update(i,c)
elif q=='2':
l = int(a)-1
r = int(b)-1
X = Seg.query(l,r+1)
tmp = 0
for j in range(30):
if X%2==1:
tmp += 1
X//=2
print(tmp) | import sys
# import bisect
# from collections import Counter, deque, defaultdict
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math
# from numba import jit
# from scipy import
# import numpy as np
# import networkx as nx
# import matplotlib.pyplot as plt
readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)
class SegmentTree:
def __init__(self, array, operator, identity):
self.identity = identity
self.operator = operator
self.array_size = len(array)
self.tree_height = (self.array_size - 1).bit_length()
self.tree_size = 2 ** (self.tree_height + 1)
self.leaf_start_index = 2 ** self.tree_height
self.tree = [self.identity] * self.tree_size
for i in range(self.array_size):
x = 1 << (ord(array[i]) - 96)
self.tree[self.leaf_start_index + i] = x
for i in range(self.leaf_start_index - 1, 0, -1):
self.tree[i] = self.operator(self.tree[i << 1], self.tree[i << 1 | 1])
def update(self, index, val):
x = 1 << (ord(val) - 96)
cur_node = self.leaf_start_index + index
self.tree[cur_node] = x
while cur_node > 1:
self.tree[cur_node >> 1] = self.operator(self.tree[cur_node], self.tree[cur_node ^ 1])
cur_node >>= 1
def query(self, begin, end):
if begin < 0:
begin = 0
elif begin > self.array_size:
return self.identity
if end > self.array_size:
end = self.array_size
elif end < 1:
return self.identity
res = self.identity
left = begin + self.leaf_start_index
right = end + self.leaf_start_index
while left < right:
if left & 1:
res = self.operator(res, self.tree[left])
left += 1
if right & 1:
right -= 1
res = self.operator(res, self.tree[right])
left >>= 1
right >>= 1
return res
def main():
from operator import or_
n = int(input())
s = input()
q = int(input())
seg = SegmentTree(s, or_, 0)
for i in range(q):
q1, q2, q3 = readline().split()
q1, q2 = int(q1), int(q2)
if q1 == 1:
seg.update(q2-1, q3.rstrip("\n"))
else:
q3 = int(q3)
print(bin(seg.query(q2-1, q3)).count("1"))
if __name__ == '__main__':
main()
| 1 | 62,443,485,558,248 | null | 210 | 210 |
A,B=map(int,input().split())
N = list(map(int,input().split()))
c=0
for i in range(A):
if N[i] >= B:
c+=1
else:
pass
i+=1
print(c)
| n,k=map(int,input().split())
l=list(map(int,input().split()))
ans=sum(x>=k for x in l)
print(ans) | 1 | 178,461,769,033,180 | null | 298 | 298 |
import sys
def merge(A, left, mid, right):
n1 = mid - left
n2 = right - mid
L = []
R = []
for i in range(n1):
L.append(A[left+i])
for i in range(n2):
R.append(A[mid+i])
L.append("INFTY")
R.append("INFTY")
i = 0
j = 0
for k in range(left, right):
if L[i] <= R[j]:
A[k] = L[i]
i += 1
else:
A[k] = R[j]
j += 1
global c
c += 1
def mergeSort(A, left, right):
if left+1 < right:
mid = (left + right) / 2
mergeSort(A, left, mid)
mergeSort(A, mid, right)
merge(A, left, mid, right)
if __name__ == "__main__":
lines = sys.stdin.readlines()
N = int(lines[0])
nums = [int(n) for n in lines[1].split(" ")]
c = 0
mergeSort(nums, 0, N)
print " ".join(map(str, nums))
print c | import sys
def merge(A, left, mid, right):
L = A[left: mid] + [sys.maxsize]
R = A[mid: right] + [sys.maxsize]
i = j = count = 0
k = left
while k < right:
count += 1
if L[i] <= R[j]:
A[k] = L[i]
i += 1
else:
A[k] = R[j]
j += 1
k += 1
return count
def merge_sort(A, left, right):
if right - left > 1:
mid = (left + right) // 2
lcost = merge_sort(A, left, mid)
rcost = merge_sort(A, mid, right)
tot = merge(A, left, mid, right)
return tot + lcost + rcost
else:
return 0
if __name__ == '__main__':
n = int(input())
A = [int(i) for i in input().split()]
count = merge_sort(A, 0, n)
print(str(A).replace(',', '').replace('[', '').replace(']', ''))
print(count)
| 1 | 113,258,403,840 | null | 26 | 26 |
l = 2*10**9
p = -2*10**9
n = int(input())
for _ in range(n):
x = int(input())
p = max(x-l,p)
l = min(x,l)
print(p) | # -*- coding: utf-8 -*-
s, t = map(str, input().split())
a, b = map(int, input().split())
u = str(input())
dic = {s:a, t:b}
dic[u] = dic[u] - 1
print(dic[s], dic[t]) | 0 | null | 36,153,516,536,998 | 13 | 220 |
"""bfs"""
import sys
sys.setrecursionlimit(10**6)
n = int(input())
to = [[] for _ in range(n)]
eid = [[] for _ in range(n)]
ans = [0]*(n-1)
for i in range(n-1):
a,b = map(int, input().split())
a -= 1; b -= 1
to[a].append(b); eid[a].append(i) # to:nodeの繋がり、eid:edgeの色
to[b].append(a); eid[b].append(i)
import queue
q = queue.Queue()
used = [0]*n
q.put(0)
used[0] = 1
while not q.empty():
v = q.get()
c = -1
for i in range(len(to[v])):
u = to[v][i]; ei = eid[v][i]
if used[u]: c = ans[ei]
k = 1
for i in range(len(to[v])):
u = to[v][i]; ei = eid[v][i]
if used[u]: continue
if k == c: k += 1
ans[ei] = k
k += 1
q.put(u)
used[u] = 1
mx = 0
for i in range(n):
mx = max(mx, len(to[i]))
print(mx)
for i in range(n-1):
print(ans[i]) | import sys
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7
S, T = rs().split()
print(T+S)
| 0 | null | 119,188,141,540,038 | 272 | 248 |
H = int(input())
count = 0
answer = 0
while(H > 1):
H //= 2
answer += 2**count
count += 1
answer += 2**count
print(answer) | # D - Caracal vs Monster
H = int(input())
def rec(x):
if x==1:
return 1
else:
return 2*rec(x//2)+1
print(rec(H)) | 1 | 79,719,490,455,912 | null | 228 | 228 |
a, b, c, d = map(int, input().split())
for i in range(max(a, b, c, d)):
if b >= c:
print('Yes')
exit()
else:
c -= b
if a <= d:
print('No')
exit()
else:
a -= d
continue | t_HP, t_A, a_HP, a_A = map(int,input().split())
ans = False
while True:
a_HP -= t_A
if a_HP <= 0:
ans = True
break
t_HP -= a_A
if t_HP <= 0:
break
if ans == True:
print("Yes")
else:
print("No") | 1 | 29,729,160,500,200 | null | 164 | 164 |
x = int(input())
ans = 0
div_500, x = divmod(x, 500)
div_5, x = divmod(x, 5)
ans = div_500 * 1000 + div_5 * 5
print(ans) | x = int(input())
n_500 = int(x / 500)
h = 1000 * n_500
x -= 500 * n_500
n_5 = int(x / 5)
h += 5 * n_5
print(int(h))
| 1 | 43,018,623,423,488 | null | 185 | 185 |
N = int(input())
As = list(map(int,input().split()))
array = []
B = 0
for i in range(N):
B ^= As[i]
ans_array = []
for i in range(N):
ans_array.append(B^As[i])
print(*ans_array) | import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
n,m,L=map(int,input().split())
E=[[] for _ in range(n)]
for _ in range(m):
a,b,c=map(int,input().split())
if(c>L): continue
a-=1; b-=1
E[a].append((b,c))
E[b].append((a,c))
A=[0]*n
# pure Dijkstraを全ての始点で行う
for u in range(n):
dist=[(INF,INF)]*n # (補給回数,使用燃料)
dist[u]=(0,0)
calculated=[False]*n
for _ in range(n-1):
v=min((dist[v],v) for v in range(n) if(not calculated[v]))[1]
calculated[v]=True
now=dist[v]
for nv,w in E[v]:
if(now[1]+w<=L):
next=(now[0],now[1]+w)
else:
next=(now[0]+1,w)
if(dist[nv]>next):
dist[nv]=next
A[u]=dist
# output
Q=int(input())
for _ in range(Q):
s,t=map(int,input().split())
s-=1; t-=1
ans=A[s][t][0]
print(ans if ans!=INF else -1)
resolve() | 0 | null | 93,323,184,345,272 | 123 | 295 |
N=int(input())
D=[]
TF=[]
for i in range(N):
Input=list(map(int,input().split()))
D.append(Input)
if D[i][0]==D[i][1]:
TF.append(1)
else:
TF.append(0)
TF.append(0)
TF.append(0)
Q=[]
for i in range(N):
temp=TF[i]+TF[i+1]+TF[i+2]
Q.append(temp)
if max(Q)==3:
print("Yes")
else:
print("No") | def main():
h,w,m=map(int,input().split())
target=set()
for i in range(m):
th,tw=map(int,input().split())
target.add((th-1,tw-1))
lines=[0]*w
rows=[0]*h
for th,tw in target:
lines[tw]+=1
rows[th]+=1
max_lines=max(lines)
max_rows=max(rows)
max_wi=[i for i,wi in enumerate(lines) if wi==max_lines]
max_hi=[i for i,hi in enumerate(rows) if hi==max_rows]
# print('rows\t',max_rows,max_hi,'\nlines\t',max_lines,max_wi)
ans=max_lines+max_rows-1
isLoop=True
for wi in max_wi:
if isLoop:
for hi in max_hi:
if not (hi,wi) in target:
ans+=1
isLoop=False
break
return ans
if __name__=='__main__':
ans=main()
print(ans)
| 0 | null | 3,564,525,261,792 | 72 | 89 |
n, x, m = map(int, input().split())
P = [] # value of pre & cycle
sum_p = 0 # sum of pre + cycle
X = [-1] * m # for cycle check
for i in range(n):
if X[x] > -1:
cyc_len = len(P) - X[x]
nxt_len = (n - X[x]) % cyc_len
pre = sum(P[:X[x]])
cyc = (sum_p - pre) * ((n - X[x]) // cyc_len)
nxt = sum(P[X[x]: X[x] + nxt_len])
print(pre + cyc + nxt)
exit()
X[x] = i
P.append(x)
sum_p += x
x = x*x % m
print(sum_p)
| n, x, m = map(int, input().split())
def solve(n, x, m):
if n == 1:
return x
arr = [x]
for i in range(1, n):
x = x*x % m
if x in arr:
rem = n-i
break
else:
arr.append(x)
else:
rem = 0
sa = sum(arr)
argi = arr.index(x)
roop = arr[argi:]
nn, r = divmod(rem, len(roop))
return sa + nn*sum(roop) + sum(roop[:r])
print(solve(n, x, m)) | 1 | 2,799,333,905,984 | null | 75 | 75 |
S = int(input())
m = (10**9)+7
lists=[1]+[0]*(S)
for i in range(S):
for j in range(3,S+1):
try:
lists[i+j]+=lists[i]
except IndexError:
break
print ((lists[S])%m) | S = int(input())
MOD = 10 ** 9 + 7
DP = [0] * (2000+1)
DP[0]
DP[3]=1
for i in range(4,2001):
DP[i]=DP[i-1]+DP[i-3]
print(DP[S]%(10**9+7)) | 1 | 3,294,753,162,260 | null | 79 | 79 |
a, b = map(int, raw_input().split())
if a < b:
print('a < b')
elif b < a:
print('a > b')
else:
print('a == b') | input_line1 = raw_input()
work = input_line1.split(' ')
ret = 'a == b'
if int(work[0]) < int(work[1]):
ret = 'a < b'
if int(work[0]) > int(work[1]):
ret = 'a > b'
print(ret) | 1 | 357,488,904,092 | null | 38 | 38 |
N = int(input())
ans = 0
for i in range(1, 1+N):
if not (i%3 == 0 or i%5 == 0):
ans += i
print(ans) | import sys
from itertools import product
sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7
def resolve():
n = int(input())
def dfs(s, num):
if len(s) == n:
print(s)
return
for i in range(num + 1):
t = s + chr(97 + i)
dfs(t, max(num, i + 1))
dfs("", 0)
if __name__ == '__main__':
resolve()
| 0 | null | 43,984,096,122,510 | 173 | 198 |
import sys
# \n
def input():
return sys.stdin.readline().rstrip()
def train(X, Y, T): # O(N) ans: 回数
ans = 0
for i in range(len(X)):
ans += max(0, X[i] - T // Y[i])
return ans
def main():
N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))
A.sort(reverse=True) # 0(NlogN)
F.sort() # O(NlogN)
t =A[0]*(10**6)
if K>=t:
print(0)
exit()
ok = t+1 #時間
ng = -1
# O(Nlog(maxK)?)
while ok - ng > 1:
mid = (ok + ng) // 2 #時間
ans = train(A,F,mid) #kaisuu
if ans >K:
ng =mid
else:
ok =mid
print(ok)
if __name__ == "__main__":
main()
| #!/usr/bin/env python3
import sys
def cube(x):
if not isinstance(x,int):
x = int(x)
return x*x*x
if __name__ == '__main__':
i = sys.stdin.readline()
print('{}'.format( cube( int(i) ) )) | 0 | null | 82,294,717,474,322 | 290 | 35 |
a, b, c, d = map(int, input().split())
if -(-c//b) > -(-a//d):
print('No')
exit()
print('Yes')
| score = list(map(int,input().split()))
taka = score[0]
aoki = score[2]
while taka > 0:
aoki -= score[1]
if aoki <= 0:
print('Yes')
break
taka -= score[3]
if aoki > 0:
print('No') | 1 | 29,563,805,636,960 | null | 164 | 164 |
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
| def dist(X, Y, p):
return sum(abs(x - y) ** p for x, y in zip(X,Y)) ** (1.0 / p)
n = input()
X = map(int, raw_input().split())
Y = map(int, raw_input().split())
for i in [1, 2, 3]:
print dist(X, Y, i)
print max(map(lambda xy: abs(xy[0] - xy[1]), zip(X, Y))) | 0 | null | 106,863,900,220 | 5 | 32 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.