code1
stringlengths 17
427k
| code2
stringlengths 17
427k
| similar
int64 0
1
| pair_id
int64 2
181,637B
⌀ | question_pair_id
float64 3.7M
180,677B
⌀ | code1_group
int64 1
299
| code2_group
int64 1
299
|
---|---|---|---|---|---|---|
import math
a, b, C = map(float, input().split())
C = C / 180 * math.pi
print('%.06f' % (a * b * math.sin(C) / 2))
print('%.06f' % ((a**2 + b**2 -2*a*b*math.cos(C))**(1/2) + a + b))
print('%.06f' % (b*math.sin(C))) | # -*- coding: utf-8 -*-
def main():
A, B, C, K = map(int, input().split())
ans = 0
if K <= A:
ans = K
else:
if K <= A + B:
ans = A
else:
ans = A + (-1 * (K - A - B))
print(ans)
if __name__ == "__main__":
main() | 0 | null | 10,985,993,951,098 | 30 | 148 |
import sys
def input():
return sys.stdin.readline()[:-1]
n, p = map(int, input().split())
snacks = sorted([list(map(int, input().split())) for _ in range(n)], key=lambda x: x[0])
dp = [0 for _ in range(p)]
ans = snacks[0][1]
for i in range(1, n):
x, y = snacks[i-1]
z = snacks[i][1]
for j in range(p-1, 0, -1):
if j >= x:
dp[j] = max(dp[j], dp[j-x]+y)
ans = max(ans, dp[j]+z)
print(ans) | n, T = map(int, input().split())
food = []
for _ in range(n):
a, b = map(int, input().split())
food.append((a, b))
dp1 = [[0]*T for _ in range(1+n)]
dp2 = [[0]*T for _ in range(1+n)]
for i in range(n):
for j in range(T):
dp1[i+1][j] = dp1[i][j]
if j - food[i][0] >= 0:
dp1[i+1][j] = max(dp1[i+1][j], dp1[i][j-food[i][0]] + food[i][1])
for i in range(n-1, -1, -1):
for j in range(T):
dp2[i][j] = dp2[i+1][j]
if j - food[i][0] >= 0:
dp2[i][j] = max(dp2[i][j], dp2[i+1][j-food[i][0]] + food[i][1])
res = 0
for i in range(n):
for j in range(T):
res = max(res, food[i][1] + dp1[i][j] + dp2[i+1][T-1-j])
print(res) | 1 | 151,760,948,891,328 | null | 282 | 282 |
N = int(input())
A = sorted([(int(a), i) for i, a in enumerate(input().split())], key = lambda x: x[0])[::-1]
X = [0] + [-1<<100] * (N + 5)
for k, (a, i) in enumerate(A):
X = [max(X[j] + abs(N - (k - j) - 1 - i) * a, X[j-1] + abs(j - 1- i) * a if j else 0) for j in range(N + 5)]
print(max(X)) | from sys import stdin
for line in stdin:
a,b = line.split(" ")
c = int(a)+int(b)
print(len(str(c)))
| 0 | null | 16,966,717,810,462 | 171 | 3 |
n,m = map(int,input().split())
s = input()
now = n
ans_rev = []
while(now != 0):
for i in range( max(0,now-m),now+1):
if(i == now):
print(-1)
exit()
if(s[i] == '1'):
continue
ans_rev.append(now - i)
now = i
break
print(' '.join(map(str, ans_rev[::-1]))) | N, M = map(int, input().split())
S = input()
p = N
ans = []
ans_ok = True
while True:
if p <= M:
ans.append(p)
p -= p
break
check = False
for i in range(M, 0, -1):
if S[p-i] == '0':
check = True
ans.append(i)
p -= i
break
if not check:
ans_ok = False
break
if ans_ok:
print(' '.join(map(str, ans[::-1])))
else:
print(-1) | 1 | 139,538,343,550,980 | null | 274 | 274 |
from itertools import groupby
# RUN LENGTH ENCODING str -> tuple
def run_length_encode(S: str):
"""
'AAAABBBCCDAABBB' -> [('A', 4), ('B', 3), ('C', 2), ('D', 1), ('A', 2), ('B', 3)]
"""
grouped = groupby(S)
res = []
for k, v in grouped:
res.append((k, len(list(v))))
return res
S = input()
K = int(input())
if len(set(list(S))) == 1:
print((len(S) * K) // 2)
else:
rle = run_length_encode(S)
cnt = 0
for w, length in rle:
if length > 1:
cnt += length // 2
ans = cnt * K
if S[0] == S[-1]:
a = rle[0][1]
b = rle[-1][1]
ans -= (a // 2 + b // 2 - (a + b) // 2) * (K - 1)
print(ans)
| import sys, os
import collections
MOD = 10 ** 9 + 7
def solve(input_stream):
N, K = read_ints(input_stream)
numbers = [int(i) - 1 for i in input_stream.readline().strip().split()]
cumulative_sum = [0 for i in range(N + 1)]
mods = {}
ans = 0
for index, number in enumerate(numbers):
cumulative_sum[index + 1] = (cumulative_sum[index] + number) % K
queue = []
for index in range(N+1):
if cumulative_sum[index] not in mods:
mods[cumulative_sum[index]] = 0
ans += mods[cumulative_sum[index]]
mods[cumulative_sum[index]] += 1
queue.append(cumulative_sum[index])
if len(queue) == K:
target = queue.pop(0)
mods[target] -= 1
return [str(ans)]
def read_ints(input_stream):
return [i for i in map(int, input_stream.readline().strip().split())]
def read_int(input_stream):
return int(input_stream.readline().strip())
if __name__ == "__main__":
outputs = solve(sys.stdin)
for line in outputs:
print(line) | 0 | null | 156,977,688,057,488 | 296 | 273 |
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")
n = int(input())
lr = [None]*n
for i in range(n):
x,l = map(int, input().split())
lr[i] = (x-l, x+l)
lr.sort(key=lambda x: x[1])
ans = 0
c = -10**10
for l,r in lr:
if c<=l:
ans += 1
c = r
print(ans) | W = input().casefold()
count=0
while True:
line = input()
if line == "END_OF_TEXT":
break
line = line.casefold()
word = line.split()
count += word.count(W)
print(count) | 0 | null | 45,710,217,810,610 | 237 | 65 |
def print_arr(arr):
arr_str = ''
for i in range(len(arr)):
arr_str += str(arr[i])
if i != len(arr) - 1:
arr_str += ' '
print(arr_str)
n = int(input())
arr = list(map(int, input().split()))
swap_count = 0
for i in range(n):
mini = i
for j in range(i, n):
if arr[j] < arr[mini]:
mini = j
if mini != i:
temp = arr[i]
arr[i] = arr[mini]
arr[mini] = temp
swap_count += 1
print_arr(arr)
print(swap_count)
| M1, D1 = map(int, input().split())
M2, D2 = map(int, input().split())
if M1!=M2:
print('1')
else:
print('0')
| 0 | null | 62,133,064,474,400 | 15 | 264 |
N,K=map(int,input().split())
p=list(map(int, input().split()))
p.sort()
price=0
for idx in range(K):
price=price+p[idx]
print(price)
| N,K=input().split()
N=int(N)
K=int(K)
p = [int(i) for i in input().split()]
p.sort()
print(sum(p[0:K]))
| 1 | 11,561,403,020,590 | null | 120 | 120 |
N = int(input())
A = list(int(x) for x in input().split())
ans = 0
for i in range(N):
if i == N - 1:
break
if A[i] > A[i+1]:
sa = A[i] - A[i+1]
A[i+1] += sa
ans = ans + sa
print(ans)
| n=int(input())
a=[int(k) for k in input().split()]
sum=0;
max=a[0];
for aa in a:
if aa<max:
sum=sum+max-aa
else:
max=aa
print(sum) | 1 | 4,553,675,764,358 | null | 88 | 88 |
import sys
def main():
input = sys.stdin.buffer.readline
x, y, z = map(int, input().split())
print(z, x, y)
if __name__ == '__main__':
main()
| a, b = map(int, input().split())
c = list(map(int, input().split()))
d = sorted(c)
result = 0
for i in range(b):
result += d[i]
print(result)
| 0 | null | 24,764,057,094,532 | 178 | 120 |
from collections import deque
q=deque()
n=int(input())
for i in range(n):
cmd=input()
if cmd=="deleteFirst":
try:
q.popleft()
except:
pass
elif cmd=="deleteLast":
try:
q.pop()
except:
pass
else:
cmd,number=cmd.split()
if cmd=="delete":
try:
q.remove(number)
except:
pass
elif cmd=="insert":
q.appendleft(number)
print(*q)
| from sys import stdin
from collections import deque
# 入力
n = int(input())
# command = [stdin.readline()[:-1] for a in range(n)]
command = stdin
# process
output = deque([])
for a in command:
if a[0] == 'i':
output.appendleft(int(a[7:]))
elif a[0:7] == 'delete ':
try:
output.remove(int(a[7:]))
except ValueError:
pass
elif a[6] == 'F':
output.popleft()
else:
output.pop()
# 出力
print(*list(output))
| 1 | 49,436,948,298 | null | 20 | 20 |
a = input()
b = a.split(" ")
for i in range(len(b)):
if b[i] == "0":
print(i + 1)
break
| from itertools import combinations_with_replacement
CList = []
N, M, Q = map(int, input().split())
for i in range(1, M+1):
CList.append(i)
Qtotal = []
x = 0
ans = 0
CList=list(combinations_with_replacement(CList, N))
a, b, c, d = map(int, input().split())
for i in CList:
if CList[x][b-1]-CList[x][a-1] == c:
Qtotal.append(d)
else:
Qtotal.append(0)
x = x+1
x=0
for i in range(Q-1):
a, b, c, d = map(int, input().split())
for j in CList:
if CList[x][b-1]-CList[x][a-1] == c:
Qtotal[x] = Qtotal[x]+d
x = x+1
x=0
ans=max(Qtotal)
print(ans)
| 0 | null | 20,417,451,944,690 | 126 | 160 |
N = int(input())
A = [list(map(int, input().split())) for i in range(N)]
# N = 3
# A = [[1, 3], [2, 5], [1, 7]]
if N % 2 == 1:
minA = sorted([row[0] for row in A])
maxA = sorted([row[1] for row in A])
medA = minA[int(N/2)]
medB = maxA[int(N/2)]
print(medB-medA+1)
else:
minA = sorted([row[0] for row in A])
maxA = sorted([row[1] for row in A])
medA = (minA[int(N/2-1)]+minA[int(N/2)])/2
medB = (maxA[int(N/2-1)]+maxA[int(N/2)])/2
print(int((medB-medA)*2+1)) | n, k = list(map(int, input().split()))
# 先頭に0番要素を追加すれば、配列の要素番号と入力pが一致する
p = [0] + list(map(int, input().split()))
c = [0] + list(map(int, input().split()))
ans = -float('inf')
# 開始ノードをsi
for si in range(1, n+1):
# 閉ループのスコアを取り出す
x = si
s = list() # ノードの各スコアを保存
#s_len = 0
tot = 0 # ループ1周期のスコア
while 1:
x = p[x]
s.append(c[x])
#s[s_len] = c[x]
#s_len += 1
tot += c[x]
if (x == si): break # 始点に戻ったら終了
# siを開始とする閉ループのスコア(s)を全探
s_len = len(s)
t = 0
for i in range(s_len):
t += s[i] # i回移動したときのスコア
if (i+1 > k): break # 移動回数の上限を超えたら終了
# 1周期のスコアが正ならば、可能な限り周回する
if tot > 0:
e = (k-(i+1))//s_len # 周回数
now = t + tot*e # i回移動と周回スコアの合計
else:
now = t
ans = max(ans, now)
print(ans)
| 0 | null | 11,332,293,925,600 | 137 | 93 |
n = input()
l = map(int, raw_input().split())
k = 0
a = []
while n > 0:
a.append(l[n - 1])
n -= 1
print ' '.join(map(str, a)) | N,D=map(int, input().split())
A = 0
for i in range(N):
X,Y=map(int, input().split())
if (D*D) >= (X*X)+(Y*Y):
A +=1
print(A) | 0 | null | 3,472,432,176,080 | 53 | 96 |
import math
n = int(input())
if n%1000 == 0:
print(0)
else:
print(1000 - n%1000)
| input_lines = input()
data = input_lines.split()
if int(data[0]) < int(data[1]) < int(data[2]):
print("Yes")
else :
print("No")
| 0 | null | 4,397,788,177,920 | 108 | 39 |
N, K, S = map(int, input().split())
ans = []
for _ in range(K):
ans.append(S)
if S != 10**9:
for _ in range(N-K):
ans.append(S+1)
else:
for _ in range(N-K):
ans.append(S-1)
print(*ans) | [N,K,S] = list(map(int,input().split()))
cnt = 0
output = []
if S != 10**9:
for i in range(K):
output.append(S)
for i in range(N-K):
output.append(S+1)
else:
for i in range(K):
output.append(S)
for i in range(N-K):
output.append(1)
print(*output) | 1 | 91,266,711,008,060 | null | 238 | 238 |
import sys, bisect
N = int(input())
L = list(map(int, sys.stdin.readline().rsplit()))
L.sort()
res = 0
for i in reversed(range(1, N)):
for j in reversed(range(i)):
l = bisect.bisect_left(L, L[i] + L[j])
# r = bisect.bisect_right(L, abs(L[i] - L[j]))
res += l - 1 - i
print(res)
| import bisect
n = int(input())
L = list(map(int,input().split()))
L.sort()
ans = 0
for a in range(0,n):
for b in range(a+1,n):
p = bisect.bisect_left(L, L[a]+L[b])
ans += max(p-(b+1),0)
print(ans) | 1 | 171,982,085,794,204 | null | 294 | 294 |
a, b = map(int, input().split())
print('%d %d %.8f' %(a//b, a%b, float(a)/b)) | # -*- coding: utf-8 -*-
import sys
for s in sys.stdin:
print len(str(int(s.rstrip().split()[0])+int(s.rstrip().split()[1])))
| 0 | null | 295,471,347,850 | 45 | 3 |
# Forbidden List
X, N = [int(i) for i in input().split()]
pi = [int(i) for i in input().split()]
pl = []
pu = []
for i in pi:
if i <= X:
pl.append(i)
if X <= X:
pu.append(i)
pl.sort(reverse=True)
pu.sort()
nl = X
for i in pl:
if i == nl:
nl -= 1
nu = X
for i in pu:
if i == nu:
nu += 1
if X - nl <= nu - X:
print(nl)
else:
print(nu)
| X,N = map(int, input().split())
if N == 0:
print(X)
exit()
P = list(map(int, input().split()))
P.sort(reverse=True)
for i in range(105):
if X - i not in P:
print(X - i)
break
if X + i not in P:
print(X + i)
break | 1 | 14,127,058,160,348 | null | 128 | 128 |
c = 0
while True:
a,b = map(int, input().split()); c+=1
if a < b:
print(a,b)
elif a >b:
print(b,a)
elif a ==b and a !=0 and b!=0:
print(a,b)
elif a ==0 and b == 0: break
| import numpy as np
from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse import csr_matrix
INF = 10**12
N, M, L = map(int, input().split())
A = []; B = []; C = []
for i in range(M):
a, b, c = map(int, input().split())
A.append(a - 1); B.append(b - 1); C.append(c)
A = np.array(A); B = np.array(B); C = np.array(C)
graph = csr_matrix((C, (A, B)), (N, N))
d = floyd_warshall(graph, directed=False)
d[d <= L] = 1
d[d > L] = INF
d = floyd_warshall(d, directed=False)
Q = int(input())
for i in range(Q):
s, t = map(int, input().split())
if d[s - 1][t - 1] != INF:
print(int(d[s - 1][t - 1]) - 1)
else:
print(- 1) | 0 | null | 87,463,817,530,980 | 43 | 295 |
from collections import defaultdict
h, w, m = map(int, input().split())
rowsum = [0] * h
rowmax = 0
colsum = [0] * w
colmax = 0
targets = defaultdict(int)
for _ in range(m):
hi, wi = map(int, input().split())
hi -= 1
wi -= 1
rowsum[hi] += 1
rowmax = max(rowmax, rowsum[hi])
colsum[wi] += 1
colmax = max(colmax, colsum[wi])
targets[(hi, wi)] = 1
rowindices = []
for i, v in enumerate(rowsum):
if v == rowmax:
rowindices.append(i)
colindices = []
for i, v in enumerate(colsum):
if v == colmax:
colindices.append(i)
ans = rowmax + colmax - 1
for ri in rowindices:
for ci in colindices:
if targets[(ri, ci)]:
continue
print(rowmax + colmax)
exit()
print(ans) | def main():
l,r=0,10**9
while r-l>1:
m=(l+r)//2
if sum([(i-1)//m for i in a])<=k: r=m;
else: l=m;
print(r)
if __name__=='__main__':
n,k=map(int,input().split())
a=list(map(int,input().split()))
main() | 0 | null | 5,695,464,039,498 | 89 | 99 |
input = raw_input()
3
input = int(input)
ans = input**3
print ans | N = input()
if '7' in N:
print('Yes')
exit()
else:
print('No') | 0 | null | 17,202,291,842,098 | 35 | 172 |
n,k,s = map(int, raw_input().split())
r = [s] * k
if s == 10 ** 9:
for i in range(n - k): r.append((10 ** 9) - 2)
else:
for i in range(n - k): r.append(s + 1)
for a in r: print a,
| n = int(input())
i = 0
l = list(map(int, input().split()))
Max = l[0]
Min = l[0]
sum = 0
for i in range(0,n):
sum += l[i]
a = l[i]#変数の値が下の行の処理で変わってしまうのでaとかにしておく
if a > Max:
a,Max = Max,a
if a < Min:
a,Min = Min,a
print(f"{Min} {Max} {sum}")
| 0 | null | 45,777,085,924,932 | 238 | 48 |
def main():
N = int(input())
# K > 1
def divisor_set(x):
ret = set()
if x > 1:
ret.add(x)
d = 2
while d * d <= x:
if x % d == 0:
ret.add(d)
ret.add(x // d)
d += 1
return ret
# N % K == 1
# (N-1) % K == 0
# K > 1
ans = 0
ans += len(divisor_set(N - 1))
# N % K == 0
# N //= K ---> M
# (M-1) % K == 0
for k in divisor_set(N):
x = N
while x % k == 0:
x //= k
if x % k == 1:
ans += 1
print(ans)
if __name__ == '__main__':
main()
# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())
| from math import sqrt
def div(n):
s = set()
for i in range(1, int(sqrt(n))+2):
if n%i == 0:
s.add(i)
s.add(n//i)
return s
def solve(x, n):
while n % x == 0:
n //= x
if n % x == 1:
return True
return False
def main():
n = int(input())
t = div(n) | div(n-1)
ans = 0
for v in t:
if v != 1 and solve(v, n):
ans += 1
print(ans)
if __name__ == "__main__":
main() | 1 | 41,146,200,292,538 | null | 183 | 183 |
h, w, k = map(int, input().split())
s = [[int(i) for i in input()] for _ in range(h)]
ans = (h-1)+(w-1)
for i in range(2**(h-1)):
d = [(i >> j) & 1 for j in range(h-1)]
count, count_tmp = [0]*(sum(d)+1), [0]*(sum(d)+1)
ans_, res = 0, 0
flag = False
for w_ in range(w):
count[0] += s[0][w_]
count_tmp[0] += s[0][w_]
res += 1
tmp = 0
for h_ in range(1, h):
if d[h_-1]: tmp += 1
if s[h_][w_]:
count[tmp] += 1
count_tmp[tmp] += 1
for j in count:
if j > k:
if res==1:
flag = True
break
else:
for idx in range(sum(d)+1):
count[idx] = count_tmp[idx]
ans_ += 1
res = 1
break
if flag: break
count_tmp = [0]*(sum(d)+1)
if flag: continue
ans_ += sum(d)
ans = min(ans, ans_)
print(ans) | #!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
#%%code
def resolve():
H,W,K=pin()
choco=[list(input()) for _ in range(H)]
#think H<=10:bit full search
ans=H+W #default
#print(choco)
for h in range(1<<(H-1)):#uekara h banme no yoko de waru
tempans=0
see=[]
shiro=[0]
for i in range(H):
see.append(tempans)
if (1<<i)&h:
tempans+=1
shiro.append(0)
#print(see,tempans,bin(h),"sth")
#print(shiro,";")
#yokoni krenai nara donkoku de jituhatokeru
j=0
while(j<W):
f=1
for n,s in enumerate(see):
shiro[s]+=int(choco[n][j])
if shiro[s]>K:#suguhidaride kiru
if j==0:#kirenai kara nasi
f=0
break
tempans+=1
shiro=[0]*len(shiro)
j-=1
break
if f==0:
tempans=H*W
break
j+=1
#print(j,shiro,"tochu")
#print(tempans,"tempans")
ans=min(ans,tempans)
print(ans)
#%%submit!
resolve() | 1 | 48,549,142,734,222 | null | 193 | 193 |
def main():
A, B, C, K = map(int, input().split())
if K <= A + B:
print(min([A, K]))
else:
print(A - (K - A - B))
if __name__ == '__main__':
main()
| A, B, C, K = map(int, input().split())
# KをA,B,Cの順に割り振る
a = min(A, K)
K -= a
b = min(B, K)
K -= b
c = min(C, K)
print(1 * a + -1 * c)
| 1 | 21,733,141,141,232 | null | 148 | 148 |
word = input()
q = int(input())
for _ in range(q):
s = input().split(" ")
command = s[0]
a = int(s[1])
b = int(s[2])
if command == "print":
print(word[a:b+1])
elif command == "reverse":
word = word[:a] + word[a:b+1][::-1] + word[b+1:]
elif command == "replace":
word = word[:a] + s[3] + word[b+1:] | moto = input()
num = int(input())
for i in range(num):
order = input().split()
if order[0] == "print":
m,n=int(order[1]),int(order[2])
print(moto[m:n+1])
elif order[0] == "reverse":
m,n=int(order[1]),int(order[2])
moto = moto[:m]+moto[m:n+1][::-1]+moto[n+1:]
elif order[0] == "replace":
m,n,l=int(order[1]),int(order[2]),order[3]
moto = moto[:m]+order[3]+moto[n+1:] | 1 | 2,093,431,028,260 | null | 68 | 68 |
# #
# author : samars_diary #
# 13-09-2020 │ 12:41:59 #
# #
import sys, os.path
#if(os.path.exists('input.txt')):
#sys.stdin = open('input.txt',"r")
#sys.stdout = open('output.txt',"w")
sys.setrecursionlimit(10 ** 5)
def mod(): return 10**9+7
def i(): return sys.stdin.readline().strip()
def ii(): return int(sys.stdin.readline())
def li(): return list(sys.stdin.readline().strip())
def mii(): return map(int, sys.stdin.readline().split())
def lii(): return list(map(int, sys.stdin.readline().strip().split()))
#print=sys.stdout.write
def solve():
n,x,t=mii()
print(t*(n//x)+t*(bool(n%x)))
solve() | line = input()
N, X, T = (int(x) for x in line.split(" "))
print((N + X - 1) // X * T) | 1 | 4,255,457,547,296 | null | 86 | 86 |
def main():
N = int(input())
A = list(map(int,input().split()))
buka_num = [0]*(N+1)
for i in range(N-1):
buka_num[A[i]] += 1
for i in range(1,N+1,1):
print(buka_num[i])
main()
| 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) | 1 | 32,457,888,519,292 | null | 169 | 169 |
import sys
N = int(input())
if N>=4:
C=[0]*N
C[0]=0
C[1]=0
C[2]=1
n=2
#フィボナッチ数列のイメージ
while True:
n=n+1
C[n]=C[n-1]+C[n-3]
#print(n,C[n])
if n==(N-1):
break
print(C[N-1]%(10**9+7))
if N==3:
print(1)
if N<=2:
print(0) | # author: Taichicchi
# created: 20.09.2020 11:13:28
import sys
from math import factorial
from scipy.special import comb
MOD = 10 ** 9 + 7
S = int(input())
m = S // 3
cnt = 0
for n in range(1, m + 1):
cnt += int(comb(S - 3 * n + 1, n - 1, exact=True, repetition=True)) % MOD
cnt %= MOD
print(cnt)
| 1 | 3,292,895,543,040 | null | 79 | 79 |
x,y = map(int,input().split())
def point(a):
if a == 1:
return 300000
elif a == 2:
return 200000
elif a == 3:
return 100000
else:
return 0
c = point(x)
b = point(y)
if x == 1 and y == 1:
print(1000000)
else:
print(c+b) | N = int(input())
A = list(map(int, input().split()))
print(" ".join(map(str,A)))
for i in range(1,N):
tmp = A[i]
j = i-1
while j >= 0 and A[j] > tmp:
A[j+1] = A[j]
j += -1
A[j+1] = tmp
print(" ".join(map(str,A)))
| 0 | null | 70,141,780,184,730 | 275 | 10 |
s = ''
while True:
try:
s += input().lower()
except EOFError:
break
for i in range(97, 123):
print(chr(i)+' : '+str(s.count(chr(i))))
| import sys
alpha = "abcdefghijklmnopqrstuvwxyz"
dic = {c:0 for c in alpha}
t = sys.stdin.read()
for s in t:
s = s.lower()
if s in alpha :
dic[s] += 1
for key in sorted(dic):
print("{} : {}".format(key,dic[key])) | 1 | 1,642,493,182,482 | null | 63 | 63 |
A, B, C = map(int, input().split())
K = int(input())
for _ in range(K):
if A >= B:
B *= 2
elif B >= C:
C *= 2
if A < B < C:
print('Yes')
else:
print('No')
| import math
H = int(input())
print(2 ** (int(math.log(H, 2)) + 1 ) - 1) | 0 | null | 43,692,538,350,076 | 101 | 228 |
N ,*S = open(0).read().split()
s = list(set(S))
print(len(s)) | str = input()
for q in range(int(input())):
com, *num = input().strip().split()
a, b = int(num[0]), int(num[1])
if com == 'print':
print(str[a:b+1])
elif com == 'replace':
str = str[:a] + num[2] + str[b+1:]
else: # com == 'reverse'
str = str[:a] + str[a:b+1][::-1] + str[b+1:] | 0 | null | 16,227,409,498,562 | 165 | 68 |
from collections import deque
S = deque(list(input()))
Q = int(input())
q = [list(input().split()) for _ in range(Q)]
r = 0
for i in range(Q):
if q[i][0] == "1":
r += 1
else:
if (q[i][1] == "1" and r%2==0)or (q[i][1] == "2" and r%2==1 ):
S.appendleft(q[i][2])
else:
S.append(q[i][2])
S = "".join(S)
if r%2==1:
S = S[::-1]
print(S) | import sys
def main():
r = int(input())
print(r*r)
main() | 0 | null | 101,233,366,068,332 | 204 | 278 |
from itertools import accumulate
from bisect import bisect_left
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
B = [0] + list(accumulate(A))
def check(X):
cnt = 0
for i in range(N):
cnt += N - bisect_left(A, X - A[i])
return cnt >= M
def main():
ng = 10 ** 9
ok = 0
while abs(ng - ok) > 1:
mid = (ok + ng) // 2
if check(mid):
ok = mid
else:
ng = mid
ans = 0
cnt = 0
for i in range(N):
tmp = bisect_left(A, ok - A[i])
cnt += N - tmp
ans += B[N] - B[tmp]
ans += A[i] * (N - tmp)
ans -= ok * (cnt - M)
print(ans)
if __name__ == "__main__":
main() | 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())
def main():
X, Y, Z = MI()
A = [Z, X, Y]
print(' '.join(map(str, A)))
if __name__ == "__main__":
main() | 0 | null | 73,098,028,801,630 | 252 | 178 |
a = int(input())
b = input()
c = len(b)
if c <= a:
print(b)
else:
print(b[:a] + "...") | #!/usr/bin/env python3
# from numba import njit
import sys
sys.setrecursionlimit(10**8)
# input = stdin.readline
# @njit
def solve(n,k,r,s,p,t):
winHand = {"r":"p", "s":"b", "p":"s"}
bestHandMemo = [""]*(n+1) # i手目の勝ち手
memo = [-1]*(n+1)
def calcPoint(char):
if char == "r":
return p
elif char == "s":
return r
elif char == "p":
return s
else:
raise ValueError
def dp(i):
if i == 0:
return 0
elif memo[i] != -1:
return memo[i]
elif i <= k:
bestHandMemo[i-1] = winHand[t[i-1]] # 自由に出せる
return dp(i-1) + calcPoint(t[i-1])
elif winHand[t[i-1]] == bestHandMemo[i-k-1]:
bestHandMemo[i-1] = "" # あいこでも負けても同じこと
return dp(i-1)
else: # 勝てる
bestHandMemo[i-1] = winHand[t[i-1]]
return dp(i-1) + calcPoint(t[i-1])
for i in range(n+1):
memo[i] = dp(i)
return memo[n]
def main():
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = input()
print(solve(N,K,R,S,P,T))
return
if __name__ == '__main__':
main()
| 0 | null | 63,081,041,270,204 | 143 | 251 |
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) | sec_in = int(input())
hour = sec_in // 3600
minute = (sec_in % 3600) // 60
sec = sec_in % 60
print('{0}:{1}:{2}'.format(hour,minute,sec))
| 1 | 325,972,946,528 | null | 37 | 37 |
N = int(input())
D = list(map(int,input().split()))
from itertools import combinations
counter = 0
for i in combinations(range(N),2):
a,b = i
counter += D[a]*D[b]
print(counter) | # import itertools
# import math
# import sys
# sys.setrecursionlimit(500*500)
# import numpy as np
N = int(input())
S = input()
# n, *a = map(int, open(0))
# N, M = map(int, input().split())
# A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()
# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)
# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)
# def dfs(tree, s):
# for l in tree[s]:
# if depth[l[0]] == -1:
# depth[l[0]] = depth[s] + l[1]
# dfs(tree, l[0])
# dfs(tree, 1)
all = S.count("R") * S.count("G") * S.count("B")
cnt = 0
for i in range(N):
for j in range(i, N):
k = 2 * j - i
if k >= N:
continue
if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
cnt += 1
print(all - cnt) | 0 | null | 102,217,702,689,690 | 292 | 175 |
s=input()
s += "es" if s[-1] == 's' else "s"
print(s) | a=str(input())
if a[-1]=="s":
print(a+"es")
else:
print(a+"s") | 1 | 2,406,449,647,768 | null | 71 | 71 |
s=input()
ans='Yes'
a=s[0]
b=s[1]
c=s[2]
if a==b and b==c and a==c:
ans='No'
print(ans)
| # n, k = map(int, input().split())
# from collections import OrderedDict
# d = OrderedDict()
# a = list(input().split())
# b = list(map(int, input().split()))
# print(r + max(0, 100*(10-n)))
# print("Yes" if 500*n >= k else "No")
# s = input()
# a = int(input())
# b = int(input())
# c = int(input())
s = input()
print("Yes" if ('A' in s and 'B' in s) else "No") | 1 | 55,021,823,233,952 | null | 201 | 201 |
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
# d = m - k[i] - k[j]
# if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも!!
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
return list(map(int,input().split()))
def I():
return int(input())
n,m = readInts()
A = readInts()
sumA = sum(A)
div = sumA * (1/(4*m))
A = sorted(A,reverse=True)
for i in range(m):
if A[i] < div:
print('No')
exit()
print('Yes')
| n,m=map(int,input().split())
A=list(map(int,input().split()))
ans=0
for i in range(n):
if 4*m*A[i]<sum(A):
continue
else:
ans+=1
if ans<m:
print('No')
else:
print('Yes') | 1 | 38,651,402,938,088 | null | 179 | 179 |
a,b,k = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
from itertools import accumulate
A = list(accumulate(A))
B = list(accumulate(B))
A = [0] + A
B = [0] + B
ans = 0
num = b
for i in range(a+1):
if A[i] > k:
continue
while True :
if A[i] + B[num] > k:
num -=1
else:
ans = max(ans,i+num)
break
print(ans) | import itertools
a=int(input())
b=list(map(int,input().split()))
c=list(map(int,input().split()))
n=sorted(b)
m=list(itertools.permutations(n))
m=[list(i) for i in m]
print(abs(m.index(b)-m.index(c))) | 0 | null | 55,416,934,361,700 | 117 | 246 |
from collections import defaultdict
facts = defaultdict(int)
mod=10**9+7
def factorization(n):
arr = []
temp = n
for i in range(2, int(-(-n**0.5//1))+1):
if temp%i==0:
cnt=0
while temp%i==0:
cnt+=1
temp //= i
arr.append([i, cnt])
if temp!=1:
arr.append([temp, 1])
if arr==[]:
arr.append([n, 1])
return arr
n=int(input())
a=[int(i) for i in input().split()]
for i in range(n):
for b,p in factorization(a[i]):
if facts[b]<p:
facts[b]=p
ans=0
f=1
for k,v in facts.items():
f*=pow(k,v,mod)
f%=mod
for i in range(n):
ans+=f*pow(a[i],mod-2,mod)
ans%=mod
print(ans) | a,b,c = input().split()
ti = [a,b,c]
ti.sort()
print(ti[0],ti[1],ti[2])
| 0 | null | 44,042,577,180,292 | 235 | 40 |
import math
x1,y1,x2,y2 = tuple(float(n) for n in input().split())
D = math.sqrt((x2-x1)**2 + (y2-y1)**2)
print("{:.8f}".format(D))
| from math import sqrt, pow
x1, y1, x2, y2 = list(map(float, input().split()))
print(sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)))
| 1 | 153,693,884,762 | null | 29 | 29 |
from collections import deque
H, W = [int(x) for x in input().split()]
field = []
for i in range(H):
field.append(input())
conn = [[[] for _ in range(W)] for _ in range(H)]
for i in range(H):
for j in range(W):
if field[i][j] == '.':
for e in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
h, w = i + e[0], j + e[1]
if 0 <= h < H and 0 <= w < W and field[h][w] == '.':
conn[i][j].append([h, w])
d = 0
for i in range(H):
for j in range(W):
l = 0
q = deque([[i, j]])
dist = [[-1 for _ in range(W)] for _ in range(H)]
dist[i][j] = 0
while q:
v = q.popleft()
for w in conn[v[0]][v[1]]:
if dist[w[0]][w[1]] == -1:
q.append(w)
dist[w[0]][w[1]] = dist[v[0]][v[1]] + 1
l = dist[w[0]][w[1]]
d = max(d, l)
print(d) | #!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**7)
import bisect
import heapq
import itertools
import math
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from math import gcd
from operator import add, itemgetter, mul, xor
def cmb(n,r,mod):
bunshi=1
bunbo=1
for i in range(r):
bunbo = bunbo*(i+1)%mod
bunshi = bunshi*(n-i)%mod
return (bunshi*pow(bunbo,mod-2,mod))%mod
mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]
#bisect.bisect_left(list,key)はlistのなかでkey未満の数字がいくつあるかを返す
#つまりlist[i] < x となる i の個数
#bisect.bisect_right(list, key)はlistのなかでkey以下の数字がいくつあるかを返す
#つまりlist[i] <= x となる i の個数
#これを応用することで
#len(list) - bisect.bisect_left(list,key)はlistのなかでkey以上の数字がいくつあるかを返す
#len(list) - bisect.bisect_right(list,key)はlistのなかでkeyより大きい数字がいくつあるかを返す
#これらを使うときはあらかじめlistをソートしておくこと!
def maze_solve(S_1,S_2,maze_list):
d = deque()
d.append([S_1,S_2])
dx = [0,0,1,-1]
dy = [1,-1,0,0]
while d:
v = d.popleft()
x = v[0]
y = v[1]
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if nx < 0 or nx >= h or ny < 0 or ny >= w:
continue
if dist[nx][ny] == -1:
dist[nx][ny] = dist[x][y] + 1
d.append([nx,ny])
return max(list(map(lambda x: max(x), dist)))
h,w = MI()
if h==1 and w == 2:
print(1)
elif h == 2 and w == 1:
print(1)
else:
ans = 0
maze = [list(input()) for _ in range(h)]
dist = [[-1]*w for _ in range(h)]
start_list = []
for i in range(h):
for j in range(w):
if maze[i][j] == "#":
dist[i][j] = 0
else:
start_list.append([i,j])
dist_copy = deepcopy(dist)
for k in start_list:
dist = deepcopy(dist_copy)
ans = max(ans,maze_solve(k[0],k[1],maze))
print(ans+1) | 1 | 94,719,055,047,840 | null | 241 | 241 |
n, m = [int(s) for s in input().split()]
ans = "Yes" if m == n else "No"
print(ans) | import sys
H, M = map(int, next(sys.stdin.buffer).split())
print('Yes' if H == M else 'No')
| 1 | 83,683,113,971,912 | null | 231 | 231 |
def solve():
s = input()
if s == 'SUN':
print(7)
elif s == 'MON':
print(6)
elif s == 'TUE':
print(5)
elif s == 'WED':
print(4)
elif s == 'THU':
print(3)
elif s == 'FRI':
print(2)
elif s == 'SAT':
print(1)
if __name__ == '__main__':
solve()
| n=input()
lis=["SUN","MON","TUE","WED","THU","FRI","SAT"]
ans=7
for i in lis:
if n==i:
print(ans)
break
ans-=1 | 1 | 132,939,902,015,648 | null | 270 | 270 |
n,k=[int(s) for s in input().split()]
h=[int(s) for s in input().split()]
count=0
for i in h:
if i>=k:
count+=1
print(count)
# a.append(
#a.append(23)
#for
#print(*range(x-k+1,x+k)) | X = int(input())
for a in range(-10**3, 10**3):
for b in range(-10**3, 10**3):
if a**5 - b**5 == X:
print(a, b)
exit()
| 0 | null | 102,251,525,341,990 | 298 | 156 |
n=int(input())
def func(n):
fibonacci = [1,2]
for i in range(2,n):
fibonacci.append(fibonacci[i-2]+fibonacci[i-1])
return fibonacci[n-1]
print(func(n))
| # Queue
class MyQueue:
def __init__(self):
self.queue = [] # list
self.top = 0
def isEmpty(self):
if self.top==0:
return True
return False
def isFull(self):
pass
def enqueue(self, x):
self.queue.append(x)
self.top += 1
def dequeue(self):
if self.top>0:
self.top -= 1
return self.queue.pop(0)
else:
print("there aren't enough values in Queue")
def main():
n, q = map(int, input().split())
A = [
list(map(lambda x: int(x) if x.isdigit() else x, input().split()))
for i in range(n)
]
A = [{'name': name, 'time': time} for name, time in A]
queue = MyQueue()
for a in A:
queue.enqueue(a)
elapsed = 0
while not queue.isEmpty():
job = queue.dequeue()
if job['time']>q:
elapsed += q
job['time'] = job['time'] - q
queue.enqueue(job)
else:
elapsed += job['time']
print(job['name']+ ' ' + str(elapsed))
main()
| 0 | null | 21,754,294,464 | 7 | 19 |
#!/usr/bin/env python3
# Generated by https://github.com/kyuridenamida/atcoder-tools
from typing import *
import itertools
import math
import sys
INF = float('inf')
def solve(N: int, K: int, p: "List[int]"):
return sum(sorted(p)[:K])
def main():
sys.setrecursionlimit(10 ** 6)
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens)) # type: int
K = int(next(tokens)) # type: int
p = [int(next(tokens)) for _ in range(N)] # type: "List[int]"
print(solve(N, K, p))
if __name__ == '__main__':
main()
| N,K = map(int, input().split())
P = list(map(int, input().split()))
P.sort()
plist = P[0:K]
a = 0
for p in plist:
a+=p
print(a) | 1 | 11,573,088,529,210 | null | 120 | 120 |
n, m = map(int, input().split())
a = list(map(int, input().split()))
print("Yes" if len([i for i in a if i/sum(a) >= 1/(4 * m)]) >= m else "No") | N, M = map(int, input().split())
A = list(map(int,input().split()))
A.sort()
total=sum(A)
i=0
flg="Yes"
while i < M:
if A.pop() < total/(4*M):
flg="No"
break
i += 1
print(flg) | 1 | 38,657,468,989,682 | null | 179 | 179 |
N=int(input())
A=str(input())
def ans176(N:int, A:str):
A=list(map(int,A.split()))
step_count=0
max=A[0]
for i in range(1,N):
if A[i]<max:
step_count=step_count+max-A[i]
else:
max=A[i]
return(step_count)
print(ans176(N,A)) | def A():
n, x, t = map(int, input().split())
print(t * ((n+x-1)//x))
def B():
n = list(input())
s = 0
for e in n:
s += int(e)
print("Yes" if s % 9 == 0 else "No")
def C():
int(input())
a = list(map(int, input().split()))
l = 0
ans = 0
for e in a:
if e < l: ans += l - e
l = max(l, e)
print(ans)
C()
| 1 | 4,578,571,005,038 | null | 88 | 88 |
import sys
# input = sys.stdin.readline
def main():
N = int(input())
d = list(map(int,input().split()))
ans = 0
for i in range(N):
for j in range(N):
if i !=j:
ans += d[i]*d[j]
print(int(ans/2))
if __name__ == "__main__":
main() | n = int(input())
oi = list(map(int,input().split()))
total = 0
for i in range(n-1):
for j in range(i+1,n):
total += oi[i]*oi[j]
print(total) | 1 | 168,908,710,607,072 | null | 292 | 292 |
from collections import defaultdict
n, k = map(int, input().split())
m = defaultdict(int)
for _ in range(k):
input()
for x in input().split():
m[int(x)] += 1
result = 0
for i in range(1, n+1):
if m[i] == 0:
result += 1
print(result) | k = int(input())
def test_case(k):
ans,cnt = 7,1
if k % 2 == 0 or k % 5 == 0:
return -1
if k == 1 or k == 7:
return 1
while (1):
ans = (ans * 10 + 7) % k
cnt += 1
if ans == 0:
return cnt
print(test_case(k))
| 0 | null | 15,386,090,652,380 | 154 | 97 |
# 深さ優先探索
from sys import setrecursionlimit
def genid(a, b):
if b < a:
a, b = b, a
return a * 100000 + b
def paint(currentNode, usedColor, parentNode, edges, colors):
color = 1
for childNode in edges[currentNode]:
if childNode == parentNode:
continue
if color == usedColor:
color += 1
colors[genid(currentNode, childNode)] = color
paint(childNode, color, currentNode, edges, colors)
color += 1
setrecursionlimit(10 ** 6)
N = int(input())
ab = [list(map(int, input().split())) for _ in range(N - 1)]
edges = [[] for _ in range(N)]
for a, b in ab:
edges[a - 1].append(b - 1)
edges[b - 1].append(a - 1)
colors = {}
paint(0, -1, -1, edges, colors)
print(max(len(e) for e in edges))
for a, b in ab:
print(colors[genid(a - 1, b - 1)])
| n,k=map(int,input().split())
a=list(map(int,input().split()))
rui=[0]
for i in a:
rui.append(rui[-1]+i)
hantei=0
for i in range(0,len(rui)-k):
if hantei<=rui[i+k]-rui[i]:
hantei=rui[i+k]-rui[i]
l=i
r=i+k
ans=0
for i in range(l,r):
ans+=(a[i]+1)/2
print(ans) | 0 | null | 105,521,618,329,078 | 272 | 223 |
import sys
from fractions import gcd
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def merge(a, b):
g = gcd(a, b)
a //= g
b //= g
if a % 2 == 0:
return 0
if b % 2 == 0:
return 0
n = a * b * g
if n > 10 ** 9:
return 0
return n
n, m = map(int, readline().split())
a = list(map(int, readline().split()))
a = [x >> 1 for x in a]
for i in range(n-1):
a[i+1] = merge(a[i], a[i+1])
z = a[n-1]
if z == 0:
print("0")
else:
ans = (m//z) - (m//(z+z))
print(ans)
| S = input()
T = input()
ans = len(T)
for i in range(len(S)-len(T)+1):
count = 0
#print(S[i:i+len(T)], T)
for s, t in zip(S[i:i+len(T)], T):
if s != t:
count += 1
ans = min(count, ans)
print(ans) | 0 | null | 53,052,379,695,052 | 247 | 82 |
# coding: utf-8
import sys
# from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
printout = sys.stdout.write
sprint = sys.stdout.flush
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
#import math
# from itertools import product, accumulate, combinations, product
#import bisect
# import numpy as np
# from copy import deepcopy
from collections import deque
# from decimal import Decimal
# from numba import jit
INF = 1 << 50
EPS = 1e-8
mod = 998244353
def intread():
return int(sysread())
def mapline(t=int):
return map(t, sysread().split())
def mapread(t=int):
return map(t, read().split())
def run():
K = intread()
print('ACL' * K)
if __name__ == "__main__":
run()
| if __name__ == '__main__':
key_in = input()
data = [int(x) for x in key_in.split(' ')]
a = data[0]
b = data[1]
print('{0} {1} {2:.5f}'.format(a//b, a%b, a/b)) | 0 | null | 1,383,220,336,908 | 69 | 45 |
n = int(input())
al = list(map(int, input().split()))
num_cnt = {}
c_sum = 0
for a in al:
num_cnt.setdefault(a,0)
num_cnt[a] += 1
c_sum += a
ans = []
q = int(input())
for _ in range(q):
b,c = map(int, input().split())
num_cnt.setdefault(b,0)
num_cnt.setdefault(c,0)
diff = num_cnt[b]*c - num_cnt[b]*b
num_cnt[c] += num_cnt[b]
num_cnt[b] = 0
c_sum += diff
ans.append(c_sum)
for a in ans:
print(a) | def solve():
N = int(input())
A = [int(i) for i in input().split()]
counter = {}
total = 0
for num in A:
total += num
counter[num] = counter.get(num, 0) + 1
Q = int(input())
for i in range(Q):
B,C = [int(i) for i in input().split()]
if B in counter:
B_cnt = counter[B]
counter[C] = counter.get(C, 0) + B_cnt
total += (C-B) * B_cnt
counter[B] = 0
print(total)
if __name__ == "__main__":
solve() | 1 | 12,298,223,786,332 | null | 122 | 122 |
x = 1
y = 1
while x != 0 or y != 0:
n = map(int,raw_input().split())
x = n[0]
y = n[1]
if x == 0 and y == 0:
break
elif x <= y:
print x,y
elif x > y:
print y,x | import sys
for s in sys.stdin:
a,b = sorted(map(int, s.split()))
if a == 0 and b == 0:
break
print(a,b) | 1 | 528,795,347,392 | null | 43 | 43 |
def main():
N, K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N-K):
if A[i] < A[K+i]:
print("Yes")
else:
print("No")
if __name__ == "__main__":
main()
| N, K = map(int, input().split())
A = list(map(int, input().split()))
result = []
for i in range(K, N):
if A[i] > A[i - K]:
result.append('Yes')
else:
result.append('No')
print(*result, sep='\n') | 1 | 7,136,530,602,908 | null | 102 | 102 |
N=int(input())
from math import gcd
from functools import reduce
l=list(map(int,input().split()))
sw=0
c=[0]*1000001
for i in l:
c[i]+=1
if all(sum(c[i::i])<=1 for i in range(2,1000001)):
print("pairwise coprime")
elif reduce(gcd,l)==1:
print("setwise coprime")
else:
print("not coprime") | import math
N = int(input())
a = list(map(int,input().split()))
m = max(a)
c = [0] * (m+1)
for i in a:
c[i] += 1
pairwise = True
for i in range(2, m+1, 1):
cnt = 0
for j in range(i, m+1, i):
cnt += c[j]
if cnt > 1:
pairwise = False
break
if pairwise:
print("pairwise coprime")
exit()
g = 0
for i in range(N):
g = math.gcd(g, a[i])
if g == 1:
print("setwise coprime")
exit()
print("not coprime") | 1 | 4,111,084,357,628 | null | 85 | 85 |
s = set()
for _ in range(int(input())):
s.add(input())
print(len(s))
|
[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))
| 0 | null | 90,177,584,866,890 | 165 | 281 |
n = int(input())
result = []
for i in range(1,n+1):
si = str(i)
if (not i % 3) or "3" in si:
result.append(si)
print(" " + " ".join(result)) | class SegTree():
def segFunc(self, x, y):
return x+y
def searchIndexFunc(self, val):
return True
def __init__(self, ide, init_val):
n = len(init_val)
self.ide_ele = ide
self.num = 2**(n-1).bit_length()
self.seg = [self.ide_ele] * 2 * self.num
for i in range(n):
self.seg[i+self.num-1] = init_val[i]
for i in range(self.num-2,-1,-1):
self.seg[i] = self.segFunc(self.seg[2*i+1],self.seg[2*i+2])
def update(self, idx, val):
idx += self.num-1
self.seg[idx] = val
while idx:
idx = (idx-1)//2
self.seg[idx] = self.segFunc(self.seg[idx*2+1], self.seg[idx*2+2])
def addition(self, idx, val):
idx += self.num-1
self.seg[idx] += val
while idx:
idx = (idx-1)//2
self.seg[idx] = self.segFunc(self.seg[idx*2+1], self.seg[idx*2+2])
def multiplication(self, idx, val):
idx += self.num-1
self.seg[idx] *= val
while idx:
idx = (idx-1)//2
self.seg[idx] = self.segFunc(self.seg[idx*2+1], self.seg[idx*2+2])
def query(self, begin, end):
if end <= begin:
return self.ide_ele
begin += self.num-1
end += self.num-2
res = self.ide_ele
while begin + 1 < end:
if begin&1 == 0:
res = self.segFunc(res, self.seg[begin])
if end&1 == 1:
res = self.segFunc(res, self.seg[end])
end -= 1
begin = begin//2
end = (end-1)//2
if begin == end:
res = self.segFunc(res, self.seg[begin])
else:
res = self.segFunc(self.segFunc(res, self.seg[begin]), self.seg[end])
return res
def getLargestIndex(self, begin, end):
L, R = begin, end
if not self.searchIndexFunc(self.query(begin, end)):
return None
while L+1 < R:
P = (L+R)//2
if self.searchIndexFunc(self.query(P, R)):
L = P
else:
R = P
return L
def getSmallestIndex(self, begin, end):
L, R = begin, end
if not self.searchIndexFunc(self.query(begin, end)):
return None
while L+1 < R:
P = (L+R+1)//2
if self.searchIndexFunc(self.query(L, P)):
R = P
else:
L = P
return L
def main():
n = int(input())
s = input()
alp = [[0]*n for _ in range(26)]
for i, c in enumerate(s):
alp[ord(c) - ord("a")][i] = 1
seg = [SegTree(0, alp[i]) for i in range(26)]
q = int(input())
ans = []
for _ in range(q):
a, b, c = map(str, input().split())
if a == "1":
b = int(b) - 1
for i in range(26):
if alp[i][b] == 1:
alp[i][b] = 0
seg[i].update(b, 0)
alp[ord(c) - ord("a")][b] = 1
seg[ord(c) - ord("a")].update(b, 1)
else:
b, c = int(b)-1, int(c)
cnt = 0
for i in range(26):
if seg[i].query(b, c) > 0:
cnt += 1
ans.append(cnt)
for v in ans:
print(v)
if __name__ == "__main__":
main() | 0 | null | 31,844,750,135,370 | 52 | 210 |
from sys import *
N = int(stdin.readline())
a,b = [],[]
for i in range(N):
tmp = list(map(int, stdin.readline().split()))
a.append(tmp[0] + tmp[1])
b.append(tmp[0] - tmp[1])
a.sort()
b.sort()
print(max(a[-1]-a[0], b[-1]-b[0]))
| import sys
# C - Walking Takahashi
X, K, D = map(int, input().split())
if D * K <= abs(X):
print(abs(X) - D * K)
else:
distance = abs(X) % D
# 残りの移動回数
count = K - (abs(X) // D)
if count % 2 == 1:
print(abs(D - distance))
else:
print(distance) | 0 | null | 4,333,829,599,670 | 80 | 92 |
N = int(input())
L = sorted(list(map(int, input().split())))
ans = 0
for i in L:
for j in L:
for k in L:
if i < j < k:
if i + j + k > max(i,j,k) * 2:
ans += 1
else:
print(ans) | import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import accumulate
from bisect import bisect
def main():
n, m, k = map(int, input().split())
a = tuple(accumulate(map(int, input().split())))
b = tuple(accumulate(map(int, input().split())))
r = bisect(b, k)
for i1, ae in enumerate(a):
if k < ae:
break
r = max(r, bisect(b, k - ae) + i1 + 1)
print(r)
if __name__ == '__main__':
main() | 0 | null | 7,954,171,727,050 | 91 | 117 |
n = int(input())
s = input()
pw, pw1, pw2 = [0] * n, [0] * n, [0] * n
pw[0], pw1[0], pw2[0] = 1, 1, 1
v, mod1, mod2 = 0, 0, 0
for i in range(n):
if s[i] == '1':
v += 1
for i in range(1, n):
pw1[i] = int(pw1[i - 1] * 2) % (v + 1)
if v >= 2:
pw2[i] = int(pw2[i - 1] * 2) % (v - 1)
for i in range(n):
if s[i] == '1':
mod1 = (mod1 + pw1[n - i - 1]) % (v + 1)
if v >= 2:
mod2 = (mod2 + pw2[n - i - 1]) % (v - 1)
for i in range(n):
copy, x, cnt = 0, v, 1
if s[i] == '0':
x += 1
copy = (mod1 + pw1[n - i - 1]) % x
else:
x -= 1
if x >= 1:
copy = (mod2 - pw2[n - i - 1]) % x
else:
copy = -1
if copy == -1:
print(0)
else:
x = bin(copy).count('1')
while copy > 0:
copy %= x
x = bin(copy).count('1')
cnt += 1
print(cnt)
| import sys
#input = sys.stdin.buffer.readline
def main():
S = input()
print(S[:3])
if __name__ == "__main__":
main() | 0 | null | 11,419,108,783,080 | 107 | 130 |
# coding: utf-8
n = int(input())
A = list(map(int, input().split()))
mini = 1
c = 0
for i in range(n):
mini = i
for j in range(i, n):
if A[j] < A[mini]:
mini = j
A[i], A[mini] = A[mini], A[i]
if mini != i:
c += 1
print(" ".join(map(str, A)))
print(c) | N = int(input())
line = list(map(int, input().split()))
cnt = 0
for i in range(N):
minj = i
for j in range(i+1, N):
if line[j] < line[minj]:
minj = j
if minj != i:
tmp = line[i]
line[i] = line[minj]
line[minj] = tmp
cnt += 1
print(' '.join(list(map(str, line))))
print(cnt) | 1 | 21,018,415,278 | null | 15 | 15 |
A,B,K = map(int,input().split())
if A+B < K:
print(0,0)
exit()
elif A - K < 0:
B = B + A - K
A = 0
elif A - K >= 0:
A = A - K
print(A,B) | #!/usr/bin/env python3
import sys
import math
import decimal
import itertools
from itertools import product
from functools import reduce
def input():
return sys.stdin.readline()[:-1]
def gcd(*numbers):
return reduce(math.gcd, numbers)
def lcm_base(x, y):
return (x * y) // math.gcd(x, y)
def lcm(*numbers):
return reduce(lcm_base, numbers, 1)
def sort_zip(a:list, b:list):
z = zip(a, b)
z = sorted(z)
a, b = zip(*z)
a = list(a)
b = list(b)
return a, b
def ceil(x):
return math.ceil(x)
def floor(x):
return math.floor(x)
def main():
MOD = 998244353
N, K = map(int, input().split())
L = [0] * K
R = [0] * K
for i in range(K):
L[i], R[i] = map(int, input().split())
dp = [0] * (N + 1)
dpsum = [0] * (N + 1)
dp[1] = 1
dpsum[1] = 1
for i in range(2, N + 1):
for j in range(K):
li = i - R[j]
li = max(li, 1)
ri = i - L[j]
if ri < 0:
continue
dp[i] += (dpsum[ri] - dpsum[li - 1]) % MOD
dpsum[i] += (dpsum[i - 1] + dp[i]) % MOD
print(dp[N] % MOD)
if __name__ == '__main__':
main()
| 0 | null | 53,501,530,212,480 | 249 | 74 |
h = int(input())
count = 0
ans = 0
while h > 0:
h //= 2
count += 1
for i in range(count):
ans = ans*2 + 1
print(ans) | h = int(input())
cnt = 0
while h > 1:
h //= 2
cnt += 1
print(2**(cnt+1)-1) | 1 | 80,368,925,476,408 | null | 228 | 228 |
n,m = map(int,input().split())
alist = list(map(int,input().split()))
d = n - sum(alist)
if d < 0:
print(-1)
else:
print(d) | a = int(input())
b = input()
for i in range(0, len(b)):
c = ord(b[i])
d = int(c+a)
if d>90:
d -= 26
e = chr(d)
print(e, end="") | 0 | null | 83,492,372,658,848 | 168 | 271 |
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
def main():
n,p = map(int,ipt().split())
s = input()
ans = 0
if p == 2:
for i in range(n):
if int(s[i])%2 == 0:
ans += i+1
print(ans)
elif p == 5:
for i in range(n):
if int(s[i])%5 == 0:
ans += i+1
print(ans)
else:
d = defaultdict(int)
pi = 0
nk = 10%p
for i in s[::-1]:
pi = (pi+int(i)*nk)%p
d[pi] += 1
nk = (nk*10)%p
for i in d.values():
ans += i*(i-1)//2
ans += d[0]
print(ans)
return
if __name__ == '__main__':
main()
| N,P = map(int,input().split())
S = input()
if P in (2,5):
ans = 0
for i,c in enumerate(S):
c = int(c)
if c%P==0:
ans += (i+1)
print(ans)
else:
ans = 0
a = [0]
j = 1
for c in S[::-1]:
c = int(c)
a.append((a[-1] + j*c) % P)
j *= 10
j %= P
from collections import Counter
ctr = Counter(a)
for k,v in ctr.items():
ans += v*(v-1)//2
print(ans) | 1 | 58,169,182,208,058 | null | 205 | 205 |
def baseConv(n,ro,ri=10):
n = int(str(n),base=ri)
s = ""
nums = "0123456789abcdefghijklmnopqrstuvwxyz"
while n:
s += nums[n%ro]
n //= ro
return s[::-1]
n,k = map(int,input().split())
print(len(baseConv(n,k))) | def Base(X, n):
if (int(X/n)):
return Base(int(X/n), n)+str(X%n)
return str(X%n)
def main():
N, K = map(int, input().split())
ans1 = Base(N, K)
ans = len(ans1)
print(ans)
main() | 1 | 64,524,417,973,248 | null | 212 | 212 |
# AOJ ALDS1_4_C Dictionary
# Python3 2018.7.3 bal4u
import sys
from sys import stdin
input = stdin.readline
dic = {}
n = int(input())
for i in range(n):
s = input()
if s[0] == 'i': dic[s[7:]] = 1
else: print("yes" if s[5:] in dic else "no")
| N, X, T = map(int, input().split())
times = N // X
if N % X != 0:
times += 1
print(T * times)
| 0 | null | 2,135,613,966,598 | 23 | 86 |
a = list(map(int,input().split()))
if a[0] == a[1]:print("Yes")
else:print("No") | import sys
input = lambda: sys.stdin.readline().rstrip()
def solve():
N, M = map(int, input().split())
if N == M:
print('Yes')
else:
print('No')
if __name__ == '__main__':
solve()
| 1 | 82,976,596,606,260 | null | 231 | 231 |
import itertools
n = int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
Z = list(itertools.product(X,Y))
Z = Z[::len(X)+1]
def fn(p):
D = 0
for z in Z:
D += abs(z[0]-z[1])**p
else:
print(D**(1/p))
fn(1)
fn(2)
fn(3)
MA = []
for z in Z:
MA.append(abs(z[0]-z[1]))
print(max(MA))
| sec = int(input())
print(sec//3600, ':', (sec%3600)//60, ':', sec%60, sep='')
| 0 | null | 275,316,410,852 | 32 | 37 |
print('pphbhhphph'[int(input()[-1])]+'on') | """AtCoder."""
n = int(input()[-1])
s = None
if n in (2, 4, 5, 7, 9):
s = 'hon'
elif n in (0, 1, 6, 8):
s = 'pon'
elif n in (3,):
s = 'bon'
print(s)
| 1 | 19,127,820,407,202 | null | 142 | 142 |
def get_input():
lst = []
pair = [int(n) for n in input().split(" ")]
while pair != [0, 0]:
lst.append(pair)
pair = [int(n) for n in input().split(" ")]
return lst
def to_str(lst):
return [str(n) for n in lst]
lst = get_input()
for pair in lst:
print(' '.join(to_str(sorted(pair)))) | while True:
num = map(int, raw_input().split())
num.sort()
if num[0] == 0 and num[1] == 0:
break
print "%d %d" % (num[0], num[1]) | 1 | 519,890,586,112 | null | 43 | 43 |
N=int(input())
x=input()
num=0
n=0
def twice(a):
ans=0
while a:
ans+=a%2
a//=2
return ans
ma=5*10**5
dp=[0]*ma
for i in range(1,ma):
dp[i]=dp[i%twice(i)]+1
c=x.count("1")
a=int(x,2)%(c+1)
if c==1:
for i in range(N):
if x[i]=="0":
print(dp[(a+pow(2,N-i-1,2))%2]+1)
else:
print(0)
exit()
b=int(x,2)%(c-1)
for i in range(N):
if x[i]=="0":
print(dp[(a+pow(2,N-i-1,c+1))%(c+1)]+1)
else:
print(dp[(b-pow(2,N-i-1,c-1))%(c-1)]+1) | a=1
while True:
i=int(input())
if i==0:
break
else:
print("Case {0:d}: {1:d}".format(a,i))
a=a+1 | 0 | null | 4,401,220,150,072 | 107 | 42 |
mod = 1000000007
n = int(input())
ans = pow(10,n,mod) - 2 * pow(9,n,mod) + pow(8,n,mod)
ans = (ans % mod + mod) % mod
print(ans) | import math
from functools import reduce
"""[summary]
全体:10^N
0が含まれない:9^N
9が含まれない:9^N
0,9の両方が含まれない:8^N
0,9のどちらか一方が含まれない:9^N + 9^N - 8^N
"""
N = int(input())
mod = (10 ** 9) + 7
calc = (10**N - 9**N - 9**N + 8**N) % mod
print(calc) | 1 | 3,143,758,790,942 | null | 78 | 78 |
x,y,z = map(int,input().split())
temp = x
x = y
y = temp
temp = x
x = z
z = temp
print(x,y,z) | combined = input().split(" ")
a = combined[0]
b = combined[1]
c = combined[2]
temp1 = b
b = a
a = temp1
temp2 = c
c = a
a = temp2
print(a, b, c) | 1 | 38,015,023,679,500 | null | 178 | 178 |
n = int(input())
s = str(input())
r = 0
g = 0
b = 0
for i in range(n):
if s[i] == "R":
r += 1
elif s[i] == "G":
g += 1
else:
b += 1
p = r*g*b
for i in range(n-2):
for j in range(int(n/2)+1):
if i + 2*j >n-1:
break
if s[i] != s[i+j] and s[i] != s[i+2*j] and s[i+2*j] != s[i+j]:
p -= 1
print(p) | def main():
N = int(input())
S = input()
dict = {'R':0, 'B':0, 'G':0}
for i in range(N):
dict[S[i]] += 1
ans = dict['R']*dict['B']*dict['G']
for i in range(N-2):
if (N-i)%2 == 0:
tmp = int((N-i)/2)-1
else:
tmp = (N-i)//2
for j in range(1,tmp+1):
if S[i]!=S[i+j] and S[i]!=S[i+2*j] and S[i+j]!=S[i+2*j]:
ans = ans - 1
return ans
print(main())
| 1 | 36,320,693,221,820 | null | 175 | 175 |
class UnionFind:
def __init__(self, size):
self.parent = list(range(size))
self.height = [0] * size
self.size = [1] * size
self.componentCount = size
def root(self, index):
if self.parent[index] == index: # 根の場合
return index
rootIndex = self.root(self.parent[index]) # 葉の場合親の根を取得
self.parent[index] = rootIndex # 親の付け直し
return rootIndex
def union(self, index1, index2): # 結合
root1 = self.root(index1)
root2 = self.root(index2)
if root1 == root2: # 連結されている場合
return
self.componentCount -= 1 # 連結成分を減らす
if self.height[root1] < self.height[root2]:
self.parent[root1] = root2 # root2に結合
self.size[root2] += self.size[root1]
else:
self.parent[root2] = root1 # root1に結合
self.size[root1] += self.size[root2]
if self.height[root1] == self.height[root2]:
self.height[root1] += 1
return
def isSameRoot(self, index1, index2):
return self.root(index1) == self.root(index2)
def sizeOfSameRoot(self, index):
return self.size[self.root(index)]
N, M = map(int, input().split())
tree = UnionFind(N)
for _ in range(M):
a, b = map(lambda a: int(a) - 1, input().split())
tree.union(a, b)
print(tree.componentCount - 1)
| A,B,C=map(int,input().split())
if C-A-B>0 and (C-A-B)**2>4*A*B:
print('Yes')
else:
print('No') | 0 | null | 26,874,627,986,812 | 70 | 197 |
from _collections import deque
n,m,k=map(int,input().split())
fre=[[] for _ in range(n+1)]
bro=[[] for _ in range(n+1)]
for i in range(m):
a,b=map(int,input().split())
fre[a].append(b)
fre[b].append(a)
for i in range(k):
a, b = map(int, input().split())
bro[a].append(b)
bro[b].append(a)
ans=[]
x=[-1]*(n+1)
for i in range(1,n+1):
if x[i]==-1:
x[i]=i
data=deque([i])
while len(data)>0:
p=data.popleft()
for j in fre[p]:
if x[j]==-1:
x[j]=i
data.append(j)
g=[0]*(n+1)
for i in range(1,n+1):
g[x[i]]+=1
for i in range(1,n+1):
g[i]=g[x[i]]
for i in range(1,n+1):
aa=g[i]-1
aa-=len(fre[i])
for j in bro[i]:
if x[i]==x[j]:
aa-=1
ans.append(str(aa))
print(" ".join(ans))
| import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))
a,b,c=mp()
if a+b+c>=22:
print("bust")
else:
print("win") | 0 | null | 90,565,895,112,162 | 209 | 260 |
raw_input()
a = raw_input().split(" ")
a.reverse()
print " ".join(a) | N,K = map(int,input().split())
height = list(map(int,input().split()))
chibi = [1] * N
for i in range (N):
if height[i] >= K:
chibi[i] = 0
print(N-sum(chibi)) | 0 | null | 89,681,306,224,170 | 53 | 298 |
n = int(input())
s, t = input().split()
ans = ''
for si, ti in zip(s, t):
ans += si + ti
print(ans) | N = int(input())
S, T = input().split()
str_list = []
for i in range(N):
str_list += S[i], T[i]
print("".join(str_list)) | 1 | 111,929,303,139,020 | null | 255 | 255 |
import math
a, b, C = map(int, input().split())
C = math.radians(C)
S = a * b * math.sin(C) * 0.5
c = math.sqrt(a * a + b * b - 2 * a * b * math.cos(C))
L = a + b + c
h = 2 * S / a
print(S, L, h)
| N = int(input())
edge = [[0] for _ in range(N)]
for i in range(N):
A = list(map(int, input().split()))
for i,a in enumerate(A):
A[i] -= 1
edge[A[0]] = sorted(A[2:])
ans = [[0,0] for _ in range(N)]
import sys
sys.setrecursionlimit(10**8)
def dfs(v,now):
if len(edge[v])>0:
for u in edge[v]:
if visited[u]==False:
visited[u]=True
now += 1
ans[u][0] = now
now = dfs(u,now)
now += 1
ans[v][1] = now
return now
visited = [False]*N
now = 0
for i in range(N):
if visited[i]==False:
visited[i]=True
now += 1
ans[i][0] = now
now = dfs(i,now)
for i in range(N):
ans[i] = '{} {} {}'.format(i+1,ans[i][0],ans[i][1])
print(*ans,sep='\n')
| 0 | null | 93,488,560,422 | 30 | 8 |
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=input()
ans=0
A=[]
for i in range(K):
if T[i]=='r':
ans+=P
A.append('p')
elif T[i]=='s':
ans+=R
A.append('r')
else:
ans+=S
A.append('s')
for i in range(K,N):
if T[i]=='r':
if A[i-K]!='p':
ans+=P
A.append('p')
else:
A.append('x')
elif T[i]=='s':
if A[i-K]!='r':
ans+=R
A.append('r')
else:
A.append('x')
else:
if A[i-K]!='s':
ans+=S
A.append('s')
else:
A.append('x')
print(ans) | import sys
i=1
while True:
x=sys.stdin.readline().strip()
if x=="0":
break;
print("Case %d: %s"%(i,x))
i+=1 | 0 | null | 53,799,068,386,140 | 251 | 42 |
n, x, m = map(int, input().split())
ans = []
c = [0]*m
flag = False
for i in range(n):
if c[x] == 1:
flag = True
break
ans.append(x)
c[x] = 1
x = x**2 % m
if flag:
p = ans.index(x)
l = len(ans) - p
d, e = divmod(n-p, l)
print(sum(ans[:p]) + d*sum(ans[p:]) + sum(ans[p:p+e]))
else:
print(sum(ans))
| INFTY = 10 ** 10
import queue
def bfs(s):
q = queue.Queue()
q.put(s)
d = [INFTY] * n
d[s] = 0
while not q.empty():
u = q.get()
for v in range(n):
if M[u][v] == 0:
continue
if d[v] != INFTY:
continue
d[v] = d[u] + 1
q.put(v)
for i in range(n):
if d[i] == INFTY:
print(i+1, -1)
else:
print(i+1, d[i])
n = int(input())
M = [[0] * n for _ in range(n)]
for i in range(n):
nums = list(map(int, input().split()))
u = nums[0] - 1
k = nums[1]
for j in range(k):
v = nums[j+2] - 1
M[u][v] = 1
bfs(0)
| 0 | null | 1,434,095,590,594 | 75 | 9 |
x, k, d = [int(i) for i in input().split()]
if x < 0:
x = -x
l = min(k, x // d)
k -= l
x -= l * d
if k % 2 == 0:
print(x)
else:
print(d - x) | def main():
n,s = map(int,input().split())
a = list(map(int,input().split()))
mod = 998244353
dp = [[0]*(s+1) for i in range(n+1)]
dp[0][0] = 1
for i in range(n):
for j in range(s+1):
if j >= a[i]:
dp[i+1][j] += (dp[i][j]*2+dp[i][j-a[i]])%mod
else:
dp[i+1][j] += (dp[i][j]*2)%mod
print(dp[-1][-1])
if __name__ =="__main__":
main()
| 0 | null | 11,458,950,625,886 | 92 | 138 |
#6回目、2020-0612
#2重ループ +O(1)
#場合分けを近道と通常のみ(絶対値を使う)
#初期入力
N, x, y = map(int, input().split())
normal =0
short =0
ans ={i:0 for i in range(1,N)}
for i in range(1,N):
for j in range(i+1,N+1):
normal =j -i
short =abs(x-i) +1 +abs(j-y)
dist =min(normal,short)
ans[dist] +=1
#答え出力
for i in range(1,N):
print(ans[i]) | N,X,Y = map(int, input().split())
X,Y = X-1,Y-1
ans = [0] * N
for i in range(N):
for j in range(i+1,N):
dist = min(abs(i-j), abs(i-X) + 1 + abs(Y-j))
ans[dist] += 1
print(*ans[1:], sep='\n') | 1 | 44,142,314,037,692 | null | 187 | 187 |
n, x, m = map(int, input().split())
X = [-1] * m
P = []
sum_p = 0
while X[x] == -1: # preset
X[x] = len(P) # pre length
P.append(sum_p) # pre sum_p
sum_p += x # now sum_p
x = x*x % m
P.append(sum_p) # full sum_p
p_len = len(P) - 1
cyc_times, nxt_len = divmod(n - X[x], p_len - X[x])
cyc = (sum_p - P[X[x]]) * cyc_times
remain = P[X[x] + nxt_len]
print(cyc + remain)
| n,x,m=map(int,input().split())
sm=[-1 for i in range(m)]+[0]
w=[-1 for i in range(m)]
d=[m]
a=x
t=0
s=0
while True:
s+=a
if w[a]!=-1:
inter=t-w[a]
fv=w[a]
ls=d[(n-fv)%inter+fv]
lls=d[w[a]]
print(sm[lls]+(n-fv)//inter*(s-sm[a])+(sm[ls]-sm[lls]))
break
w[a]=t
d.append(a)
sm[a]=s
t+=1
a=(a*a)%m | 1 | 2,808,518,981,180 | null | 75 | 75 |
s=int(input())
p=10**9+7
if s<=2:
print(0)
exit()
n=s//3
ans=0
x=[0]*(s+1)
x[0]=1
x[1]=1
y=[0]*(s+1)
for i in range(2,s+1):
x[i]=x[i-1]*i%p
y[s]=pow(x[s],p-2,p)
for i in range(s):
y[s-1-i]=y[s-i]*(s-i)%p
for k in range(1,n+1):
ans+=x[s-2*k-1]*y[k-1]*y[s-3*k]%p
print(ans%p)
| import itertools
N = int(input())
c = []
d = 0
for i in range(N):
x1, y1 = map(int, input().split())
c.append((x1,y1))
cp = list(itertools.permutations(c))
x = len(cp)
for i in range(x):
for j in range(N-1):
d += ((cp[i][j][0]-cp[i][j+1][0])**2 + (cp[i][j][1] - cp[i][j+1][1])**2)**0.5
print(d/x) | 0 | null | 76,120,123,253,428 | 79 | 280 |
h = int(input())
w = int(input())
n = int(input())
print((n+max(w,h) - 1) // max(w,h)) | def main():
H = int( input())
W = int( input())
N = int( input())
if H < W:
H, W = W, H
ans = N//H
if N%H != 0:
ans += 1
print(ans)
if __name__ == '__main__':
main() | 1 | 88,539,838,462,792 | null | 236 | 236 |
def main():
x, n = list(map(int, input().split()))
if n > 0:
p_list = list(map(int, input().split()))
else:
return x
ans = 0
diff = abs(p_list[0]-x)
for i in range(n):
diff_new = abs(p_list[i] - x)
if diff_new < diff:
ans =i
diff=diff_new
#print(p_list[ans])
for i in range(200):
if p_list[ans] - i not in p_list:
return p_list[ans] - i
break
elif p_list[ans] + i not in p_list:
return p_list[ans] + i
break
if __name__=='__main__':
print(main()) | x,n = map(int,input().split())
if n == 0:
print(x)
else:
p = list(map(int,input().split()))
k = [i for i in range(-200,201)]
for i in range(n):
if p[i] in set(k):
k.remove(p[i])
minimum = 99999
ans = 0
for i in range(len(k)):
if abs(k[i] - x ) < minimum :
minimum = abs(k[i] - x)
ans = k[i]
print(ans) | 1 | 13,969,699,230,862 | null | 128 | 128 |
D = int(input())
C = list(map(int, input().split()))
S = [list(map(int, input().split())) for _ in range(D)]
T = [int(input())-1 for _ in range(D)]
last = [0] * 26
score = 0
for d in range(D):
v = T[d]
score += S[d][v]
for i in range(26):
if i == v:
last[i] = 0
else:
last[i] += 1
score -= C[i] * last[i]
print(score) | from operator import mul
D=int(input())
clist=list(map(int,input().split()))
slist=[]
for i in range (D):
s=list(map(int,input().split()))
slist.append(s)
tlist=[]
for i in range(D):
t=int(input())
tlist.append(t)
# print(clist,slist,tlist)
# D=5
# clist=[86, 90, 69, 51, 2, 96, 71, 47, 88, 34, 45, 46, 89, 34, 31, 38, 97, 84, 41, 80, 14, 4, 50, 83, 7, 82]
# slist=[[19771, 12979, 18912, 10432, 10544, 12928, 13403, 3047, 10527, 9740, 8100, 92, 2856, 14730, 1396, 15905, 6534, 4650, 11469, 3628, 8433, 2994, 10899, 16396, 18355, 11424], [6674, 17707, 13855, 16407, 12232, 2886, 11908, 1705, 5000, 1537, 10440, 10711, 4917, 10770, 17272, 15364, 19277, 18094, 3929, 3705, 7169, 6159, 18683, 15410, 9092, 4570], [6878, 4239, 19925, 1799, 375, 9563, 3445, 5658, 19857, 11401, 6997, 6498, 19933, 3848, 2426, 2146, 19745, 16880, 17773, 18359, 3921, 14172, 16730, 11157, 5439, 256], [8633, 15862, 15303, 10749, 18499, 7792, 10317, 5901, 9395, 11433, 3514, 3959, 5202, 19850, 19469, 9790, 5653, 784, 18500, 10552, 17975, 16615, 7852, 197, 8471, 7452], [19855, 17918, 7990, 10572, 4333, 438, 9140, 9104, 12622, 4985, 12319, 4028, 19922, 12132, 16259, 17476, 2976, 547, 19195, 19830, 16285, 4806, 4471, 9457, 2864, 2192]]
# tlist=[1, 17, 13, 14, 13]
noheldsday=[0]*26
benefit=0
for i in range (D):
_noheldsday=list(map(lambda x: x+1,noheldsday))
_noheldsday[tlist[i]-1]=0
unpleasant=list(map(mul,clist,_noheldsday))
v=slist[i][tlist[i]-1]-sum(unpleasant)
benefit+=v
noheldsday=_noheldsday
print(benefit)
| 1 | 9,886,869,138,910 | null | 114 | 114 |
n=int(input())
a=list(map(int,input().split()))
ans="APPROVED"
for i in range(n):
if a[i]%2==0:
if a[i]%3==0 or a[i]%5==0:
continue
else:
ans="DENIED"
break
print(ans) | import sys
input = sys.stdin.readline
from collections import Counter, defaultdict
def read():
H, W, M = map(int, input().strip().split())
HW = []
for i in range(M):
h, w = map(int, input().strip().split())
HW.append((h, w))
return H, W, M, HW
def solve(H, W, M, HW):
hcount = Counter()
wcount = Counter()
for h, w in HW:
hcount[h] += 1
wcount[w] += 1
hm = hcount.most_common()
wm = wcount.most_common()
hmax = hm[0][1]
wmax = wm[0][1]
hn = len([1 for k, v in hcount.items() if v == hmax])
wn = len([1 for k, v in wcount.items() if v == wmax])
m = hn * wn
for h, w in HW:
if hcount[h] == hmax and wcount[w] == wmax:
m -= 1
if m == 0:
return hmax + wmax - 1
return hmax + wmax
if __name__ == '__main__':
inputs = read()
outputs = solve(*inputs)
if outputs is not None:
print("%s" % str(outputs))
| 0 | null | 36,871,766,467,712 | 217 | 89 |
import sys
printf = sys.stdout.write
mat = []
new_map = []
r,c = map(int, raw_input().split())
for i in range(r):
mat = map(int, raw_input().split())
new_map.append(mat)
for i in range(r):
new_map[i].append(sum(new_map[i][:]))
for j in range(c):
print new_map[i][j],
printf(" " + str(new_map[i][j + 1]) + "\n")
for j in range(c + 1):
a = 0
for i in range(r):
a += new_map[i][j]
if j == c:
printf(" " + str(a) + "\n")
break
else:
print a, | r, c = map(int, input().split())
table = []
for i in range(r):
table.append(list(map(int, input().split())))
for i in table:
i.append(sum(i))
b = []
for i in range(c):
x = 0
for k in range(r):
x += table[k][i]
b.append(x)
b.append(sum(b))
for i in table:
print(" ".join(map(str, i)))
print(" ".join(map(str, b)))
| 1 | 1,365,923,968,444 | null | 59 | 59 |
import sys
from collections import defaultdict
time = 1
def dfs(g, visited, start, ans):
global time
visited[start - 1] = True
ans[start - 1][0] = time
for e in g[start]:
if not visited[e - 1]:
time += 1
dfs(g, visited, e, ans)
time += 1
ans[start - 1][1] = time
def main():
input = sys.stdin.buffer.readline
n = int(input())
g = defaultdict(list)
for i in range(n):
line = list(map(int, input().split()))
if line[1] > 0:
g[line[0]] = line[2:]
visited = [False] * n
ans = [[1, 1] for _ in range(n)]
global time
while False in visited:
start = visited.index(False) + 1
dfs(g, visited, start, ans)
time += 1
for i in range(1, n + 1):
print(i, *ans[i - 1])
if __name__ == "__main__":
main()
| n=int(input())
rootn=int(n**(0.5))+1
m=10**20
for i in range(1,rootn):
if n/i==n//i:
m=min(m,i+n//i)
print(m-2) | 0 | null | 81,026,904,966,758 | 8 | 288 |
# -*- coding: utf-8 -*-
func = lambda num_1, num_2: num_1 * num_2
for i1 in range(1,10):
for i2 in range(1,10):
print(str(i1) + "x" + str(i2) + "=" + str(func(i1, i2))) | a = 0
n,k=input().split()
p=input().split(" ")
for i in range(int(n)):
p[i] = int(p[i])
p=sorted(p)
for i in range(int(k)):
a += int(p[i])
print(a) | 0 | null | 5,763,260,161,580 | 1 | 120 |
Subsets and Splits