code1
stringlengths 16
24.5k
| code2
stringlengths 16
24.5k
| similar
int64 0
1
| pair_id
int64 2
181,637B
⌀ | question_pair_id
float64 3.71M
180,628B
⌀ | code1_group
int64 1
299
| code2_group
int64 1
299
|
---|---|---|---|---|---|---|
n=int(input())
d = {}
for _ in range(n):
op,s = input().split()
if op == 'insert': d[s] = 'yes'
elif op == 'find' : print(d.get(s,'no'))
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: dic
# CreatedDate: 2020-06-30 15:07:10 +0900
# LastModified: 2020-06-30 15:09:49 +0900
#
import os
import sys
# import numpy as np
# import pandas as pd
def main():
n = int(input())
dic = {}
for _ in range(n):
command, s = input().split()
if command == 'insert':
dic[s] = 1
# print(dic)
else:
print("yes" if s in dic.keys() else "no")
if __name__ == "__main__":
main()
| 1 | 76,073,764,900 | null | 23 | 23 |
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())
c1 = t1*(a1-b1)
c2 = t2*(a2-b2)
d = c1 + c2
if d == 0:
print("infinity")
elif c1*c2 > 0 or c1*d > 0:
print(0)
else:
if abs(c1)%abs(d)==0:
print((abs(c1)//abs(d))*2)
else:
print((abs(c1)//abs(d))*2+1)
|
import sys
T1, T2, A1, A2, B1, B2 = map(int, sys.stdin.read().split())
a1, b1 = T1*A1, T1*B1
a2, b2 = T2*A2, T2*B2
sa, sb = a1+a2, b1+b2
if sa==sb:
print("infinity")
exit()
if sa < sb:
a1, a2, b1, b2, sa, sb = b1, b2, a1, a2, sb, sa
# 2*n-1 回以上出会うか
ok = 0
ng = 10**1800
a = 0
while ok+1 < ng:
c = (ok+ng)//2
if (c-1)*sa+a1 <= (c-1)*sb+b1:
if (c - 1) * sa + a1 == (c - 1) * sb + b1:
a = -1
ok = c
else:
ng = c
if ok > 10**1799:
print("a")
print("infinity")
else:
print(max(ok*2-1+a, 0))
| 1 | 131,487,903,531,280 | null | 269 | 269 |
# -*- coding: utf-8 -*-
from sys import stdin
input = stdin.readline
MOD = 10**9+7
def main():
n = int(input().strip())
buf = 9**n
ans = 10**n - buf - buf + 8**n
print(ans%MOD)
if __name__ == "__main__":
main()
|
n = int(input())
nums = list(map(int, input().split()))
def insertion_sort(lis, num):
print(' '.join(map(str, lis)))
for i in range(1, num):
v = lis[i]
j = i - 1 # j は iの前の数
while j >= 0 and lis[j] > v: # jが0以上で配列のj番目の要素が現在の要素より大きい場合
lis[j + 1] = lis[j] # 配列j+1番目の要素に配列j番目の要素を格納
# print(' '.join(lis))
j -= 1 # jを-1
lis[j + 1] = v # 配列j+1番目の要素にvを格納
print(' '.join(map(str, lis)))
return lis
insertion_sort(nums, n)
| 0 | null | 1,600,166,563,700 | 78 | 10 |
N = int(input())
A = [[[0] * 10, [0] * 10, [0] * 10], [[0] * 10, [0] * 10, [0] * 10], [[0] * 10, [0] * 10, [0] * 10], [[0] * 10, [0] * 10, [0] * 10]]
for i in range(N):
b, f, r, v = [int(j) for j in input().split()]
A[b-1][f-1][r-1] += v
for a, b in enumerate(A):
for f in b:
print(' ' + ' '.join([str(i) for i in f]))
else:
if a != len(A) - 1:
print('#' * 20)
|
N = int(input())
ans = [0]*10050
for i in range(N+1):
ans.append(0)
for x in range(1,105):
for y in range(1,105):
for z in range(1,105):
tmp = x**2 + y**2 + z**2 + x*y + y*z + z*x
if tmp < 10050:
ans[tmp] += 1
for i in range(N):
print(ans[i+1])
| 0 | null | 4,491,357,912,172 | 55 | 106 |
N, K = map(int, input().split())
mod = 10**9+7
gcd_l = [0]*K
for i in range(K, 0, -1):
n = pow((K//i), N, mod)
for x in range(i, K+1, i):
n -= gcd_l[K-x]
gcd_l[K-i] = n%mod
res = 0
for i in range(K):
res += (gcd_l[i] * (K-i))%mod
print(res%mod)
|
mod=10**9+7
n,k=map(int,input().split())
l=[0]*(k+1)
for i in range(k):
num=(k-i)
if k//num==1:
l[num]=1
else:
ret=pow(k//num,n,mod)
for j in range(2,k//num+1):
ret-=l[num*j]
ret%=mod
l[num]=(ret)%mod
ans=0
for i in range(k+1):
ans+=(l[i]*i)%mod
print(ans%mod)
| 1 | 36,962,848,830,180 | null | 176 | 176 |
from collections import deque
H, W, K = map(int, input().split())
S = []
for _ in range(H):
S.append(list(input()))
def cut(bit):
"""
横の切れ目の場所をビットで管理する。
"00...0" <= bit <= "11...1"(H - 1桁)の時に、縦に入れる切れ目の最小回数を返す。
"""
end = [] # 板チョコを横区切りにした結果、上から何段目で各板チョコのピースが終わるか
for i in range(H - 1):
if bit[i] == '1':
end.append(i)
end.append(H - 1)
chocolates = [0] * (len(end))
#print("end={}".format(end))
"""
ここまでで板チョコを横に切ったわけだが、こうして分割した横長の部分板チョコを
上からsection = 0, 1, 2,..., len(end)-1と呼び分けることにする。
"""
cut = 0
whites_sum = [0] * len(end) # w列目までの白チョコの累積数をsectionごとに管理。cutが入れば初期化。
for w in range(W):
whites = [0] * len(end) # whites[section] = (w列目にある白チョコをsectionごとに保存)
section = 0 # 現在のsection
coarse = False # 横切りが荒過ぎて1列に白チョコがK個より多く並んだ場合に探索を打ち切るフラグ
for h in range(H + 1):
# sectionを跨いだ場合の処理
if h > end[section]:
if whites[section] > K:
coarse = True
break
elif whites_sum[section] + whites[section] > K:
cut += 1
whites_sum = whites
whites = [0] * len(end)
section += 1
else:
whites_sum[section] += whites[section]
section += 1
# 白チョコカウント
if h < H and S[h][w] == '1':
whites[section] += 1
# coarseフラグが立っていたら-1を返す。
if coarse:
return -1
return cut
ans = 10 ** 10
for h in range(2 ** (H - 1)):
bit = bin(h)[2:] # "0b..."を除くため
l = len(bit)
bit = '0' * (H - 1 - l) + bit # bitの桁数をH - 1にする。
#print("bit={}".format(bit))
vertical_cuts = cut(bit)
if vertical_cuts != -1:
preans = vertical_cuts + bit.count('1')
ans = min(ans, preans)
#print("vertical_cut={}, cut={} -> ans={}".format(vertical_cuts, preans, ans))
print(ans)
|
from collections import defaultdict
from itertools import product
H,W,K = map(int,input().split())
S = [input() for i in range(H)]
C = [[int(S[i][k]) for i in range(H)] for k in range(W)]
answer = H*W #出力がこれを超えることはない
for X in product([False,True],repeat = H-1):
M = [[0]]
ans = sum(X)
if ans > answer:#初期値がanswerよりも大きかったら考える必要がない
continue
for i,x in enumerate(X):#配列Mの作成
if x:
M.append([])
M[-1].append(i+1)
D = [0]*len(M)
for c in C:
for k,m in enumerate(M):
D[k] += sum(c[i] for i in m)#k番目の要素に足していく
#Dにどんどん代入することによってどの列まで足すことができるか算出することができる
if any(d>K for d in D):#一つでもKを超えていたら
ans += 1
if ans >answer:
break
D = [sum(c[i] for i in m) for m in M]#超えた行について再び調査
if any(d>K for d in D):
ans = answer + 1
#デフォルトanswerの結果より高い値にしてこの結果が出力にならないように設定する
break
answer = min(answer,ans)#あるXに対してanswerを算出(更新)
print(answer)
| 1 | 48,641,891,122,748 | null | 193 | 193 |
import numpy as np
from numpy.fft import rfft,irfft
import sys
sys.setrecursionlimit(10**9)
def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))
INF=10**20
class Polynomial:
def __init__(self,coefficient=None,dim=0,const=1):
if coefficient == None:
self.coefficient = np.zeros(dim+1, np.int64)
self.coefficient[dim] = const
else:
self.coefficient = coefficient
def __add__(self,other): # +
f,g = self.coefficient, other.coefficient
if len(f) > len(g): f,g = g,f
h = Polynomial(dim=len(g)-1,const=0)
h.coefficient[len(f):] += g[len(f):]
h.coefficient[:len(f)] += f + g[:len(f)]
return h
def __iadd__(self,other): # +=
h = self.__add__(other)
self.coefficient = h.coefficient
return self
def __mul__(self,other): # *
f = self.coefficient
g = other.coefficient
h = Polynomial()
h.coefficient = self.fft(f,g)[:len(f)+len(g)-1]
return h
def __len__(self):
return len(self.coefficient)
def __getitem__(self,key):
return self.coefficient[key]
def get_coefficient(self,x):
return self.coefficient[x]
def fft(self,A,B,fft_len=1<<18):
x = irfft(rfft(A,fft_len) * rfft(B,fft_len))
return np.rint(x).astype(np.int64)
def main():
N,M=mi()
A=list(mi())
coefficient = [0] * (2*10**5+10)
for a in A:
coefficient[a] += 1
f = Polynomial(coefficient=coefficient)
g = f * f
ans = 0
handshake = M
for x in range(len(g)-1,1,-1):
count = g[x]
if count <= 0: continue
if count > handshake:
ans += handshake * x
else:
ans += count * x
handshake = max(handshake-count,0)
if handshake == 0:
break
print(ans)
if __name__ == "__main__":
main()
|
N, M = (int(i) for i in input().split())
A = [int(i) for i in input().split()]
A.sort(reverse=True)
As = [0]*(N+1)
for i in range(N):
As[i+1] = As[i] + A[i]
A.reverse()
import bisect
def flag(x):
ans = 0
for i in range(N):
a = x - A[i]
res = bisect.bisect_left(A, a)
ans += (N-res)
return bool(ans >= M)
def an(x):
ans = 0
m = 0
for i in range(N):
a = x - A[i]
res = bisect.bisect_left(A, a)
m += (N-res)
ans += As[N-res]
ans += A[i] * (N-res)
ans -= (m - M) * x
return ans
low = 0
high = 10**6
while low <= high:
mid = (low + high) // 2
if flag(mid):
if not flag(mid+1):
ans = mid
break
else:
low = mid + 1
else:
high = mid - 1
print(an(ans))
| 1 | 108,024,592,069,820 | null | 252 | 252 |
import numpy as np
n = int(input())
A = list(map(int, input().strip().split()))
arr = np.array(A)
print(' '.join(map(str, np.argsort(arr) + 1)))
|
N = int(input())
A = [int(x) for x in input().split()]
B = []
for i, a in enumerate(A):
B.append([a, i + 1])
B.sort()
ans = []
for b in B:
ans.append(b[1])
print(*ans)
| 1 | 180,530,142,949,508 | null | 299 | 299 |
import collections
import math
def c2(x):
c = math.factorial(x) // (math.factorial(x-2)*2)
return c
n = int(input())
a = list(map(int, input().split()))
adic = collections.Counter(a)
cnt = 0
for i in adic:
num = adic[i]
if num > 1:
cnt += c2(num)
for i in a:
num = adic[i]
if num == 1:
print(cnt)
else:
print(cnt - (num-1))
|
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *
#階乗#
lim = 10**6 #必要そうな階乗の限界を入力
fact = [1] * (lim+1)
for n in range(1, lim+1):
fact[n] = n * fact[n-1] % mod
#階乗の逆元#
fact_inv = [1]*(lim+1)
fact_inv[lim] = pow(fact[lim], mod-2, mod)
for n in range(lim, 0, -1):
fact_inv[n-1] = n*fact_inv[n]%mod
def C(n, r):
return (fact[n]*fact_inv[r]%mod)*fact_inv[n-r]%mod
X, Y = MAP()
a = (2*X - Y)/3
b = (2*Y - X)/3
if a%1 == b%1 == 0 and 0 <= a and 0 <= b:
print(C(int(a+b), int(a)))
else:
print(0)
| 0 | null | 98,908,010,486,582 | 192 | 281 |
import numpy as np
import sys
read = sys.stdin.read
def solve(stdin):
h, w, m = stdin[:3]
row = np.zeros(h, np.int)
col = np.zeros(w, np.int)
for i in range(3, m * 2 + 3, 2):
y, x = stdin[i: i + 2] - 1
row[y] += 1
col[x] += 1
max_cnt_y = max(row)
max_cnt_x = max(col)
max_pair = np.sum(row == max_cnt_y) * np.sum(col == max_cnt_x)
for i in range(3, m * 2 + 3, 2):
y, x = stdin[i: i + 2] - 1
if row[y] == max_cnt_y and col[x] == max_cnt_x:
max_pair -= 1
return max_cnt_y + max_cnt_x - (max_pair == 0)
print(solve(np.fromstring(read(), dtype=np.int, sep=' ')))
|
n=input()
fn_first=1
fn_second=1
if n >= 2:
for x in xrange(n-1):
tmp = fn_first
fn_first = fn_first + fn_second
fn_second = tmp
print fn_first
| 0 | null | 2,332,347,713,032 | 89 | 7 |
def solve():
N = int(input())
lfs = list(map(int, input().strip().split()))
ans = 0
L = 1 << N
lo = lfs[-1]
hi = lfs[-1]
memo = [0] * (N + 1)
for i in range(N - 1, -1, -1):
if lo > L:
return -1
memo[i + 1] = hi
lo = (lo + 1) // 2 + lfs[i]
hi = hi + lfs[i]
L = L >> 1
if lo > L:
return -1
memo[0] = hi
curr = 1
for i in range(N + 1):
ans += min(memo[i], curr)
curr = (curr - lfs[i]) << 1
return ans
print(solve())
|
n_max, m_max, l_max = (int(x) for x in input().split())
a = []
b = []
c = []
for n in range(n_max):
a.append([int(x) for x in input().split()])
for m in range(m_max):
b.append([int(x) for x in input().split()])
for n in range(n_max):
c.append( [sum(a[n][m] * b[m][l] for m in range(m_max)) for l in range(l_max)])
for n in range(n_max):
print(" ".join(str(c[n][l]) for l in range(l_max)))
| 0 | null | 10,140,552,424,980 | 141 | 60 |
import sys
input = sys.stdin.readline
N = [int(x) for x in input().rstrip()][::-1] + [0]
L = len(N)
dp = [[10 ** 18, 10 ** 18] for _ in range(L)]
dp[0] = [N[0], 10 - N[0]]
for i in range(1, L):
n = N[i]
dp[i][0] = min(dp[i - 1][0] + n, dp[i - 1][1] + (n + 1))
dp[i][1] = min(dp[i - 1][0] + (10 - n), dp[i - 1][1] + (10 - (n + 1)))
print(dp[-1][0])
|
s = '0' + input()
m = len(s)
up = 0
cnt = 0
y = []
for i in range(-1, -m-1, -1):
cs = int(s[i]) + up
if cs < 5 or (cs == 5 and int(s[i-1]) < 5):
cnt += cs
y.append([cs, s[i]])
up = 0
else:
cnt += 10 - cs
y.append([10 - cs, s[i]])
up = 1
print(cnt)
| 1 | 70,720,625,317,210 | null | 219 | 219 |
while True:
s = input()
if s == '-':
break
for n in range(int(input())):
h = int(input())
s = s[h:]+s[:h]
print(s)
|
# AOJ ITP1_9_B
def main():
while True:
string = input()
if string == "-": break
n = int(input()) # シャッフル回数
for i in range(n):
h = int(input())
front = string[0 : h] # 0番からh-1番まで
back = string[h : len(string)] # h番からlen-1番まで
string = back + front
print(string)
if __name__ == "__main__":
main()
| 1 | 1,931,796,409,020 | null | 66 | 66 |
a = []
for i in range(10):
a.append(int(raw_input()))
a.sort()
a.reverse()
for i in range(3):
print a[i]
|
import sys
heights = []
for height in sys.stdin:
heights.append(int(height.rstrip()))
heights.sort()
heights.reverse()
for i in range(3):
print heights[i]
| 1 | 21,848,060 | null | 2 | 2 |
def main():
s = input()
n = len(s)
rule1 = True
rule2 = True
rule3 = True
for i in range(n):
if s[i] != s[n - 1 - i]:
rule1 = False
break
for i in range(n // 2):
if s[i] != s[n // 2 - 1 - i]:
rule2 = False
break
for i in range(n // 2):
if s[n // 2 + 1 + i] != s[n - 1 - i]:
rule3 = False
break
if rule1 and rule2 and rule3:
ans = "Yes"
else:
ans = "No"
print(ans)
if __name__ == "__main__":
main()
|
import sys
read = sys.stdin.buffer.read
s = read().decode("utf-8")
H = s[:(len(s) - 1) // 2]
B = s[(len(s) - 1) // 2 + 1:-1]
if s[:-1] == s[:-1][::-1] and H == H[::-1] and B == B[::-1]:
print("Yes")
else:
print("No")
| 1 | 46,508,233,931,350 | null | 190 | 190 |
A,B,C,K=map(int,input().split())
print(min(A,K)-max(0,K-A-B))
|
import sys
readline = sys.stdin.buffer.readline
a,b,c,k =list(map(int,readline().rstrip().split()))
if k <= a:
print(k)
elif k > a and k <= a + b:
print(a)
else:
print(a -(k-(a+b)))
| 1 | 21,665,496,585,150 | null | 148 | 148 |
# Card Game
cardAmount = int(input())
taro = 0
hanako = 0
for i in range(cardAmount):
cards = input().rstrip().split()
sortedCards = sorted(cards)
if cards[0] == cards[1]:
taro += 1
hanako += 1
elif cards == sortedCards:
hanako += 3
else:
taro += 3
print(taro, hanako)
|
s = list(input())
n = len(s)
if (
s[: (n - 1) // 2] == s[: (n - 1) // 2][::-1]
and s[(n + 1) // 2 :] == s[(n + 1) // 2 :][::-1]
and s == s[::-1]
):
print("Yes")
else:
print("No")
| 0 | null | 24,275,889,325,910 | 67 | 190 |
#template
def inputlist(): return [int(j) for j in input().split()]
#template
N = int(input())
lis = ['0']*N
time = [0]*N
for i in range(N):
lis[i],time[i] = input().split()
sing = input()
index = -1
for i in range(N):
if lis[i] == sing:
index = i
break
ans = 0
for i in range(index+1,N):
ans += int(time[i])
print(ans)
|
n, k = map(int, input().split())
ww = [int(input()) for _ in range(n)]
def chk(p):
c = 1
s = 0
for w in ww:
if w > p:
return False
if s + w <= p:
s += w
else:
c += 1
s = w
if c > k:
return False
return True
def search(l, r):
if r - l < 10:
for i in range(l, r):
if chk(i):
return i
m = (l + r) // 2
if chk(m):
return search(l, m + 1)
else:
return search(m, r)
print(search(0, 2 ** 30))
| 0 | null | 48,293,463,187,200 | 243 | 24 |
n, k = list(map(int, input().split()))
p = list(map(int, input().split()))
r = [0]*(n + 1)
for i in range(1,n + 1):
r[i] += r[i - 1] + (p[i - 1] + 1) / 2
ans = 0
for j in range(n - k + 1):
ans = max(ans, r[j+k] - r[j])
print(ans)
|
import queue
N = int(input())
tree = [[]for _ in range(N+1)]
for i in range(1,N):
a,b = map(int,input().split())
tree[a].append( [b,i])
tree[b].append([a,i])
que = queue.Queue()
node = [-1]*(N+1)
edge = [-1]*N
que.put(1)
node[1] = 1
maxi = 1
start = 0
color = [[] for _ in range(N+1)]
node[1] = 0
while(True):
if que.empty():
break
before = start
start = que.get()
col =1
for i in tree[start]:
if node[i[0]] == -1 :
que.put(i[0])
if edge[i[1]] == -1:
if col == node[start]:
col += 1
edge[i[1]] = col
node[i[0]] = col
if col > maxi:
maxi = col
col +=1
print(maxi)
for i in edge[1:]:
print(i)
| 0 | null | 105,348,573,613,622 | 223 | 272 |
A,B=list(map(int,input().split()))
def gcd(A, B):
if B==0: return(A)
else: return(gcd(B, A%B))
print(gcd(A,B))
|
def gcd(a,b):
if a>b:
x=a
y=b
else:
x=b
y=a
if y==0:
return x
else:
return gcd(y,x%y)
p,q=[int(i) for i in input().split(" ")]
print(gcd(p,q))
| 1 | 6,982,851,122 | null | 11 | 11 |
x, y = (int(a) for a in input().split())
print(x*y)
|
a = input()
a = [int(n) for n in a.split()]
print(a[0] * a[1])
| 1 | 15,808,774,665,120 | null | 133 | 133 |
A,B,C = map(int,input().split())
K = int(input())
count = 0
while(A >= B):
B *= 2
count += 1
while(B >= C):
C *= 2
count += 1
if(count <= K):
print("Yes")
else:
print("No")
|
from math import log2,ceil
def main():
A,B,C=map(int,input().split())
K=int(input())
if A<B<C:
print("Yes")
else:
op=0
if B<=A:
val=ceil(log2(A/B))
op+=val
B<<=val
if B==A:
B*=2
op+=1
if C<=B:
val=ceil(log2(B/C))
op+=val
C<<=val
if C==B:
C*=2
op+=1
if op<=K:
print("Yes")
else:
print("No")
if __name__=='__main__':
main()
| 1 | 6,871,016,517,770 | null | 101 | 101 |
def top(n):
return int(str(n)[0])
def bottom(n):
return int(str(n)[-1])
n = int(input())
c = [[0 for _ in range(10)] for _ in range(10)]
for i in range(1,n+1):
c[top(i)][bottom(i)] += 1
ans = 0
for i in range(10):
for j in range(10):
ans += c[i][j]*c[j][i]
print(ans)
|
A,B,C=map(int,input().split())
K=int(input())
count=0
if B<=A:
while B<=A:
B*=2
count+=1
if count>K:
print('No')
else:
C*=2**(K-count)
if C>B:
print('Yes')
else:
print('No')
| 0 | null | 46,967,618,406,240 | 234 | 101 |
a,b,n=map(int,input().split())
import math
if n/b >= 1:
print(math.floor(a*(b-1)/b))
else:
print(math.floor(a*n/b))
|
a, b, n = map(int, input().split())
x = (n//b)*b - 1
if x == -1:
x = n
ans = a*x//b - a*(x//b)
print(ans)
| 1 | 28,197,957,462,908 | null | 161 | 161 |
n = input()
if int(n[-1]) in [2,4,5,7,9]: ans = "hon"
elif int(n[-1]) in [0,1,6,8]: ans = "pon"
elif int(n[-1])==3: ans ="bon"
print(ans)
|
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
A = list(map(int,readline().split()))
A = [(A[i], i) for i in range(N)]
A_sort = sorted(A,reverse = True)
dp = [-float('inf')]*(N+1)
dp[0] = 0
for i in range(N):
dp_new = [-float('inf')]*(N+1)
pi = A_sort[i][1] #もともといた場所
for l in range(i+1):
r = i - l #右を選んだ回数
dp_new[l+1] = max(dp_new[l+1],dp[l] + A_sort[i][0] * (pi-l))
dp_new[l] = max(dp_new[l],dp[l] + A_sort[i][0] * ((N-r-1)-pi))
dp = dp_new
print(max(dp))
| 0 | null | 26,283,962,333,232 | 142 | 171 |
while True:
try:
# a >=b
a, b = sorted(map(int, input().strip("\n").split(" ")), reverse=True)
d = a * b
# calc. gcm by ユークリッド互除法
while True:
c = a % b
# print("{0} {1} {2}".format(a, b, c))
if c == 0:
break
else:
a, b = sorted([b, a % b], reverse=True)
gcm = b
lcm = d / gcm
# print("gcm is {}".format(b))
print("{0:d} {1:d}".format(int(gcm), int(lcm)))
#print("{0} {1}".format(a, b))
#a, b = sorted([b, a % b], reverse=True)
#print("{0} {1}".format(a, b))
except EOFError:
break # escape from while loop
|
X = int(input())
A = 0
B = 0
if X >= 500:
A = X // 500
X = X % 500
if X >= 5:
B = X // 5
X = X % 5
print(A * 1000 + B * 5)
| 0 | null | 21,208,164,889,330 | 5 | 185 |
n = int(input())
d = {}
for _ in range(n):
s = input()
d.setdefault(s,0)
d[s] -= 1
t = []
for k,v in d.items():
t.append((v,k))
t.sort()
num = t[0][0]
for v,s in t:
if v == num:
print(s)
|
N = int(input())
S = [input() for _ in range(N)]
res = {}
for s in S:
if s in res:
res[s] += 1
else:
res[s] = 1
max_ = max(res.values())
ans = []
for k in res:
if res[k] == max_:
ans.append(k)
ans.sort()
for a in ans:
print(a)
| 1 | 69,999,070,775,652 | null | 218 | 218 |
import sys
for line in sys.stdin:
print(len(str(sum(list(map(int, line.split()))))))
|
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
n,t = inpl()
wv = [None] * n
for i in range(n):
a,b = inpl()
wv[i] = (a,b)
rwv = wv[::-1]
udp = [[0] * t for _ in range(n)]
ddp = [[0] * t for _ in range(n)]
for i in range(n-1):
w,v = wv[i]
for j in range(t):
if j < w:
udp[i+1][j] = udp[i][j]
else:
udp[i+1][j] = max(udp[i][j], udp[i][j-w] + v)
res = udp[n-1][t-1] + wv[n-1][1]
for i in range(n-1):
w,v = rwv[i]
for j in range(t):
if j < w:
ddp[i+1][j] = ddp[i][j]
else:
ddp[i+1][j] = max(ddp[i][j], ddp[i][j-w] + v)
res = max(res,ddp[n-1][t-1] + wv[0][1])
# print(res)
for i in range(1,n-1):
u = i; d = n-i-1
mx = 0
for j in range(t):
tmp = udp[u][j] + ddp[d][t-1-j]
mx = max(mx, tmp)
res = max(res, mx + wv[i][1])
print(res)
| 0 | null | 75,531,479,599,202 | 3 | 282 |
n,k = map(int,input().split())
a = []
for i in range(k):
d = input()
a = a+input().split()
a = set(a)
print(n-len(a))
|
n, k= map(int, input().split())
x = [0]*n
for i in range(k):
z = int(input())
if z == 1:
x[int(input())-1] +=1
else:
a = list(map(int, input().split()))
for j in a:
x[j-1] +=1
print(x.count(0))
| 1 | 24,668,880,455,210 | null | 154 | 154 |
s = list(input())
q = int(input())
rvs = []
i = 0
j = 0
p = []
order = []
for i in range(q):
order.append(input().split())
if order[i][0] == "replace":
p = []
p = list(order[i][3])
for j in range(int(order[i][1]), int(order[i][2]) + 1):
s[j] = p[j - int(order[i][1])]
elif order[i][0] == "reverse":
ss = "".join(s[int(order[i][1]):int(order[i][2]) + 1])
rvs = list(ss)
rvs.reverse()
for j in range(int(order[i][1]), int(order[i][2]) + 1):
s[j] = rvs[j - int(order[i][1])]
# temp = s[int(order[i][1])]
# s[int(order[i][1])] = s[int(order[i][2])]
# s[int(order[i][2])] = temp
elif order[i][0] == "print":
ss = "".join(s)
print("%s" % ss[int(order[i][1]):int(order[i][2]) + 1])
|
S = list(input())
q = int(input())
for i in range(q):
cmd, a, b, *c = input().split()
a = int(a)
b = int(b)
if cmd == "replace":
S[a:b+1] = c[0]
elif cmd == "reverse":
S[a:b+1] = reversed(S[a:b+1])
else:
print(*S[a:b+1], sep='')
| 1 | 2,075,144,671,608 | null | 68 | 68 |
# -*- coding: utf-8 -*-
def main():
A, B = map(int, input().split())
ans = A - 2 * B
if ans < 0:
ans = 0
print(ans)
if __name__ == "__main__":
main()
|
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
a, b = map(int, readline().split())
ans = a - b * 2
if ans < 0:
print(0)
else:
print(ans)
| 1 | 166,706,483,668,240 | null | 291 | 291 |
import sys
while True:
line = sys.stdin.readline().rstrip()
t = sum( [ int( line[i] ) for i in range( len( line ) ) ] )
if 0 == t:
break
else:
print( t )
|
while True:
n = list(map(int,input()))
if n[0] ==0:
break
else:
print(sum(n))
| 1 | 1,584,083,344,968 | null | 62 | 62 |
import sys
N = int(input())
if N % 2 != 0:
print(0)
sys.exit()
N //= 2
ans = 0
while N != 0:
ans += N//5
N //= 5
print(ans)
|
n = int(input())
a = list(map(int, input().split()))
b = a[0]
for v in a[1:]:
b ^= v
print(*map(lambda x: x ^ b, a))
| 0 | null | 64,281,328,883,250 | 258 | 123 |
r=float(input())
a=r*r*(3.141592653589)
b=(r+r)*(3.141592653589)
print(f"{a:.6f} {b:.6f}")
|
import math
r = float(input())
S = math.pi * r**2
L = 2 * math.pi * r
print(S, L)
| 1 | 641,966,618,576 | null | 46 | 46 |
import math
a,b,c,d = (int(x) for x in input().split())
if math.ceil (a/d) < math.ceil (c/b):
print ('No')
else:
print ('Yes')
|
x = int(input())
h500 = x // 500
h5 = (x%500) //5
print(h500 * 1000 + h5* 5)
| 0 | null | 36,310,792,518,786 | 164 | 185 |
# WA
N = int(input())
A = [int(_) for _ in input().split()]
dp = [1000]
K = [0]
C = [0]
for i in range(N):
k, c = divmod(dp[-1], A[i])
v = dp[-1]
for j in range(i+1):
u = K[j] * A[i] + C[j]
v = max(u, v)
K.append(k)
C.append(c)
dp.append(v)
print(dp[-1])
|
S = str(input())
N = len(S)
ans = ""
for i in range(N):
ans += "x"
print(ans)
| 0 | null | 39,960,792,650,558 | 103 | 221 |
P = [1 for _ in range(10**6)]
P[0]=0
P[1]=0
for i in range(2,10**3):
for j in range(i*i,10**6,i):
P[j] = 0
Q = []
for i in range(10**6):
if P[i]==1:
Q.append(i)
N = int(input())
C = {}
for q in Q:
if N%q==0:
cnt = 0
while N%q==0:
N = N//q
cnt += 1
C[q] = cnt
if N>1:
C[N] = 1
cnt = 0
for q in C:
n = C[q]
k = 1
while n>=(k*(k+1))//2:
k += 1
cnt += k-1
print(cnt)
|
import collections
import bisect
def prime_factorize(n):
a = []
while n % 2 == 0:
a.append(2)
n //= 2
f = 3
while f * f <= n:
if n % f == 0:
a.append(f)
n //= f
else:
f += 2
if n != 1:
a.append(n)
return a
N = int(input())
c = collections.Counter(prime_factorize(N))
sums = []
s=1
ans = 0
for v in c.values():
count = 0
for i in range(1,v+1):
if count+i > v:
break
else:
count += i
ans += 1
print(ans)
| 1 | 16,805,149,795,040 | null | 136 | 136 |
'''
Created on 2020/09/04
@author: harurun
'''
def main():
import sys
pin=sys.stdin.readline
pout=sys.stdout.write
perr=sys.stderr.write
N=int(pin())
S=pin()[:-1]
cnt=0
l=["0","1","2","3","4","5","6","7","8","9"]
for i in l:
for j in l:
for k in l:
c=[i,j,k]
n=0
for m in S:
if m==c[n]:
if n==2:
cnt+=1
break
else:
n+=1
print(cnt)
return
main()
#解説AC
|
def is_passcode_exist(S,passcode):
passcode_idx = 0
passcode_is_exist = False
for c in S:
if passcode_idx == 3:
passcode_is_exist = True
break
if passcode[passcode_idx] == c:
passcode_idx += 1
if passcode_idx == 3:
passcode_is_exist = True
return passcode_is_exist
if __name__ == "__main__":
N = int(input())
S = input()
count = 0
for i in range(0,1000):
if is_passcode_exist(S,str(i).zfill(3)):
count += 1
print(count)
| 1 | 129,121,718,410,502 | null | 267 | 267 |
from math import ceil
print(ceil(int(input())/2))
|
n = int(input())
print(int(n/2)) if n%2 == 0 else print(int(n/2+1))
| 1 | 58,976,367,809,450 | null | 206 | 206 |
a, b = map(float, input().split())
print(int(a)*int(b*100+0.5)//100)
|
import bisect,collections,copy,heapq,itertools,math,string,decimal
import numpy as np
import sys
sys.setrecursionlimit(10**7)
def _S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
# N = I()
_A,_B = LS()
A = decimal.Decimal(_A)
B = decimal.Decimal(_B)
print((A*B).quantize(decimal.Decimal('0'), rounding=decimal.ROUND_DOWN))
#AB = [LI() for _ in range(N)]
#A,B = zip(*AB)
#Ap = np.array(A)
#C = np.zeros(N + 1)
# if ans:
# print('Yes')
# else:
# print('No')
| 1 | 16,612,453,620,128 | null | 135 | 135 |
from sys import stdin
input = stdin.buffer.readline
a, b, c = map(int, input().split())
k = int(input())
cnt = 0
while b <= a:
cnt += 1
b *= 2
while c <= b:
cnt += 1
c *= 2
if cnt <= k:
print('Yes')
else:
print('No')
|
#!usr/bin/env python3
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def S():
res = list(sys.stdin.readline())
if res[-1] == "\n":
return res[:-1]
return res
def IR(n):
return [I() for i in range(n)]
def LIR(n):
return [LI() for i in range(n)]
def SR(n):
return [S() for i in range(n)]
def LSR(n):
return [LS() for i in range(n)]
sys.setrecursionlimit(1000000)
mod = 1000000007
def solve():
a, b, c = LI()
k = I()
for i in range(k):
if a >= b:
b *= 2
elif b >= c:
c *= 2
if a < b < c:
print('Yes')
else:
print('No')
return
# Solve
if __name__ == "__main__":
solve()
| 1 | 6,839,515,786,500 | null | 101 | 101 |
import numpy as np
def main():
H, W, K = map(int, input().split())
S = []
for _ in range(H):
S.append(list(map(int, list(input()))))
S = np.array(S)
if S.sum() <= K:
print(0)
exit(0)
answer = float('INF')
for i in range(0, 2 ** (H - 1)):
horizons = list(map(int, list(bin(i)[2:].zfill(H - 1))))
result = greedy(W, S, K, horizons, answer)
answer = min(answer, result)
print(answer)
# ex. horizons = [0, 0, 1, 0, 0, 1]
def greedy(W, S, K, horizons, current_answer):
answer = sum(horizons)
# ex.
# S = [[1, 1, 1, 0, 0],
# [1, 0, 0, 0, 1],
# [0, 0, 1, 1, 1]]
# horizons = [0, 1]のとき,
# S2 = [[2, 1, 1, 0, 0],
# [0, 0, 1, 1, 1]]
# となる
top = 0
bottom = 0
S2 = []
for h in horizons:
if h == 1:
S2.append(S[:][top:(bottom + 1)].sum(axis=0).tolist())
top = bottom + 1
bottom += 1
S2.append(S[:][top:].sum(axis=0).tolist())
# ブロック毎の累積和を計算する
h = len(S2)
partial_sums = [0] * h
for right in range(W):
current = [0] * h
for idx in range(h):
current[idx] = S2[idx][right]
partial_sums[idx] += S2[idx][right]
# 1列に含むホワイトチョコの数がkより多い場合
if max(current) > K:
return float('INF')
# 無理な(ブロックの中のホワイトチョコの数をk以下にできない)場合
if max(partial_sums) > K:
answer += 1
if answer >= current_answer:
return float('INF')
partial_sums = current
return answer
if __name__ == '__main__':
main()
|
import string
n = int(input())
s = ''
lowCase = string.ascii_lowercase
while n > 0:
n -= 1
s += lowCase[int(n % 26)]
n //= 26
print(s[::-1])
| 0 | null | 30,179,153,801,148 | 193 | 121 |
n = int(input())
print((10**n-2*9**n+8**n)%1000000007)
|
n=int(input())
s=input()
tmp=s[0]
ans=1
for i in range(1,n):
if s[i]!=tmp:
tmp=s[i]
ans+=1
print(ans)
| 0 | null | 86,253,921,533,802 | 78 | 293 |
a=[int(i) for i in input().split()]
if a[0]<=a[1]:
print("unsafe")
else:
print("safe")
|
s,w=map(int, input().split())
if(s<=w) :
print('unsafe')
else :
print('safe')
| 1 | 29,231,123,042,098 | null | 163 | 163 |
import math
def main():
n=int(input())
print(math.ceil(n/2)/n)
if __name__ == '__main__':
main()
|
import math
N = float(input())
if(N%2==0):
print(1/2)
else:
print((math.floor(N/2)+1)/N)
| 1 | 176,998,567,773,240 | null | 297 | 297 |
import bisect
n, m, k = map(int, input().split())
alist = list(map(int, input().split()))
blist = list(map(int, input().split()))
for i in range(len(alist)-1):
alist[i+1] += alist[i]
for i in range(len(blist)-1):
blist[i+1] += blist[i]
x = bisect.bisect_right(alist, k)
ans = bisect.bisect_right(blist, k)
for i in range(x):
d = k - alist[i]
y = bisect.bisect_right(blist, d) + i + 1
if ans < y:
ans = y
print(ans)
|
import bisect
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
time = 0
cnt = 0
C = []
SA =[0]
SB =[0]
for a in A:
SA.append(SA[-1] + a)
for b in B:
SB.append(SB[-1] + b)
for x in range(N+1):
L = K - SA[x]
if L < 0:
break
y_max = bisect.bisect_right(SB, L) - 1
C.append(y_max + x)
print(max(C))
| 1 | 10,816,561,792,410 | null | 117 | 117 |
import sys
def solve():
readline = sys.stdin.buffer.readline
mod = 10 ** 9 + 7
h, w, k = list(map(int, readline().split()))
a = [list(str(readline().rstrip().decode('utf-8'))) for _ in range(h)]
no = 0
is_f = True
for i in range(h):
if a[i].count("#") != 0:
row_f = True
no += 1
for j in range(w):
if a[i][j] == "#":
if not row_f:
no += 1
else:
row_f = False
a[i][j] = no
if is_f:
is_f = False
if i != 0:
for j in range(i - 1, -1, -1):
for k in range(w):
a[j][k] = a[i][k]
else:
for j in range(w):
a[i][j] = a[i-1][j]
for i in range(h):
print(*a[i])
if __name__ == '__main__':
solve()
|
H,W,s=list(map(int,input().split()))
l=[list(input()) for i in range(H)]
l_h=[0]*H
for i in range(H):
if "#" in set(l[i]):
l_h[i]=1
cnt=0
bor=sum(l_h)-1
for i in range(H):
if cnt==bor:
break
if l_h[i]==1:
l_h[i]=-1
cnt+=1
cnt=1
import numpy as np
tmp=[]
st=0
ans=np.zeros((H,W))
for i in range(H):
tmp.append(l[i])
if l_h[i]==-1:
n_tmp=np.array(tmp)
s_count=np.count_nonzero(n_tmp=="#")-1
for j in range(W):
ans[st:i+1,j]=cnt
if "#" in n_tmp[:,j] and s_count>0:
cnt+=1
s_count-=1
st=i+1
cnt+=1
tmp=[]
n_tmp=np.array(tmp)
s_count=np.count_nonzero(n_tmp=="#")-1
for j in range(W):
ans[st:i+1,j]=cnt
if "#" in n_tmp[:,j] and s_count>0:
cnt+=1
s_count-=1
for i in ans:
print(*list(map(int,i)))
| 1 | 143,987,788,880,062 | null | 277 | 277 |
mod=998244353
n=int(input())
from collections import Counter as co
j=list(map(int,input().split()))
l=co(j)
ll=len(l)
for i in range(ll):
if i not in l:print(0);exit()
if l[0]>=2 or j[0]!=0:print(0);exit()
ans=1
pre=1
for k,v in sorted(l.items()):
ans*=pow(pre,v,mod)
ans%=mod
pre=v
print(ans%mod)
|
n=int(input())
d=list(map(int,input().split()))
mx=max(d)
l=[0]*(10**5)
mx=0
for i in range(n):
if (i==0 and d[i]!=0) or (i!=0 and d[i]==0):
print(0)
exit()
l[d[i]]+=1
mx=max(mx,d[i])
t=1
ans=1
for i in range(1,mx+1):
ans *= t**l[i]
t=l[i]
print(ans%998244353)
| 1 | 154,966,887,285,200 | null | 284 | 284 |
def chebyshev_0(a, b):
return a - b
def chebyshev_1(a, b):
return a + b
N = int(input())
X = [0] * N
Y = [0] * N
for i in range(N):
X[i], Y[i] = map(int, input().split())
max_0 = - (10 ** 9) - 1
min_0 = 2 * (10 ** 9)
for x, y in zip(X, Y):
if chebyshev_0(x, y) > max_0:
max_0 = chebyshev_0(x, y)
if chebyshev_0(x, y) < min_0:
min_0 = chebyshev_0(x, y)
l0 = abs(max_0 - min_0)
max_1 = - (10 ** 9) - 1
min_1 = 2 * (10 ** 9)
for x, y in zip(X, Y):
if chebyshev_1(x, y) > max_1:
max_1 = chebyshev_1(x, y)
if chebyshev_1(x, y) < min_1:
min_1 = chebyshev_1(x, y)
l1 = abs(max_1 - min_1)
print(max([l0, l1]))
|
N = int(input())
lst = []
for i in range(N):
lst.append(input().split())
X = input()
index = 0
for i in range(N):
if lst[i][0] == X:
index = i
break
ans = 0
for i in range(index + 1, N):
ans += int(lst[i][1])
print(ans)
| 0 | null | 50,025,534,756,320 | 80 | 243 |
while 1:
H, W = map(int, raw_input().split())
if H == W == 0:
break
print ('#'*W + '\n')*H
|
nums = []
while True:
in_line = raw_input().split()
h = int(in_line[0])
w = int(in_line[1])
if h == 0 and w == 0:
break
else:
nums.append([h,w])
for num in nums:
for i in range(0,num[0]):
print "#"*num[1]
print ""
| 1 | 770,991,711,490 | null | 49 | 49 |
while 1:
s = input()
if s == "-": break
for _ in range(int(input())):
h = int(input())
s = s[h:] + s[:h]
print(s)
|
n, m = map(int, input().split())
class UnionFind():
def __init__(self, n):
self.n = n
self.parent = [int(x) for x in range(n)]
self.tree_size = [1 for _ in range(n)]
def unite(self, x, y):
x_root = self.find(x)
y_root = self.find(y)
if self.same(x_root, y_root):
return
if self.size(x_root) >= self.size(y_root):
self.parent[y_root] = x_root
self.tree_size[x_root] += self.tree_size[y_root]
else:
self.parent[x_root] = y_root
self.tree_size[y_root] += self.tree_size[x_root]
def find(self, x):
if self.parent[x] == x:
return x
else:
next = self.find(self.parent[x])
self.parent[x] = next
return next
def size(self, x):
return self.tree_size[self.find(x)]
def same(self, x, y):
if self.find(x) == self.find(y):
return True
else:
return False
uf = UnionFind(n)
for _ in range(m):
a, b = map(int, input().split())
uf.unite(a-1, b-1)
# print([uf.size(i) for i in range(n)])
print(max([uf.size(i) for i in range(n)]))
| 0 | null | 2,981,561,523,268 | 66 | 84 |
x, y = map(int, input(). split())
while True:
if x % y == 0:
break
tmp = x % y
x = y
y = tmp
print(y)
|
inp = input()
a = inp.split()
a[0] = int(a[0])
a[1] = int(a[1])
a[2] = int(a[2])
a.sort()
print(a[0],a[1],a[2])
| 0 | null | 215,888,382,010 | 11 | 40 |
from collections import deque
def bfs(start, g, visited):
q = deque([start])
visited[start] = 0
while q:
curr_node = q.popleft()
for next_node in g[curr_node]:
if visited[next_node] >= 0: continue
visited[next_node] = visited[curr_node] + 1
q.append(next_node)
def main():
n,u,v = map(int, input().split())
gl = [ [] for _ in range(n+1)]
visited_u = [-1] * (n+1)
visited_v = [-1] * (n+1)
for i in range(n-1):
a, b = map(int, input().split())
gl[a].append(b)
gl[b].append(a)
bfs(u, gl, visited_u)
bfs(v, gl, visited_v)
# print(visited_u)
# print(visited_v)
# target_i = 0
# max_diff_v = 0
ans = 0
for i in range(1,n+1):
if visited_u[i] < visited_v[i] and visited_v[i]:
# if (visited_v[i]-visited_u[i])%2 == 0:
val = visited_v[i]-1
ans = max(ans,val)
# else:
# val = visited_v[i]
# ans = max(ans,val)
print(ans)
if __name__ == "__main__":
main()
|
import sys
input()
d = set()
for l in sys.stdin:
c,s = l.split()
if c[0] == 'i':
d.add(s)
else:
print('yes' if s in d else 'no')
| 0 | null | 58,676,865,882,792 | 259 | 23 |
N,A,B = map(int, input().split())
X = N//(A+B)
Y = N%(A+B)
if Y < A:
print(A*X+Y)
else:
print(A*(X+1))
|
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time, copy
sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip() # ignore trailing spaces
n, a, b = na()
if a == 0:
print(0)
else:
print((n//(a+b))*a + min(n%(a+b), a))
| 1 | 55,639,764,721,982 | null | 202 | 202 |
import math
n = int(input())
x_lst = map(int, input().split())
y_lst = map(int, input().split())
xy_lst = list(zip(x_lst, y_lst))
p1 = 0
p2 = 0
p3 = 0
p4 = []
for x, y in xy_lst:
# マンハッタン距離
p1 += abs(x - y)
# ユークリッド距離
p2 += (abs(x - y)) ** 2
p3 += (abs(x - y)) ** 3
# チェビシェフ距離
p4.append(abs(x - y))
print(p1)
print(math.sqrt(p2))
print(math.pow(p3, 1 / 3))
print(max(p4))
|
N = int(input())
X = [int(_) for _ in input().split()]
Y = [int(_) for _ in input().split()]
def calc_dist_mink(list_x, list_y, p):
list_xy = [abs(list_x[i] - list_y[i]) for i in range(len(list_x))]
list_xy_p = [i ** p for i in list_xy]
ans = sum(list_xy_p) ** (1 / p)
return(ans)
list_ans = []
ans_1 = calc_dist_mink(X, Y, 1)
list_ans.append(ans_1)
ans_2 = calc_dist_mink(X, Y, 2)
list_ans.append(ans_2)
ans_3 = calc_dist_mink(X, Y, 3)
list_ans.append(ans_3)
ans_4 = max([abs(X[i] - Y[i]) for i in range(N)])
list_ans.append(ans_4)
for ans in list_ans:
print(f"{ans:.5f}")
| 1 | 216,721,625,240 | null | 32 | 32 |
numbers = raw_input()
input_numbers = raw_input().split()
print(" ".join(input_numbers[::-1]))
|
r, c = map(int, input().split())
a, b, ans = [], [], []
for i in range(r):
a.append([int(x) for x in input().split()])
for i in range(c):
b.append(int(input()))
for i in range(r):
tmp = 0
for j in range(c):
tmp += a[i][j] * b[j]
ans.append(tmp)
for x in ans:
print(x)
| 0 | null | 1,091,178,013,820 | 53 | 56 |
def INT():
return int(input())
def MI():
return map(int, input().split())
def LI():
return list(map(int, input().split()))
N = INT()
cnt = 0
for _ in range(N):
D1, D2 = MI()
if D1 == D2:
cnt += 1
else:
cnt = 0
if cnt == 3:
print("Yes")
exit()
print("No")
|
n=int(input())
print(1-(n//2)/n)
| 0 | null | 89,421,602,545,080 | 72 | 297 |
from collections import defaultdict as d
from collections import deque
def bfs(adj, start, n):
visited = [0] * (n + 1)
tab = [0] * n
q = deque([start])
visited[start] = 1
while q:
s = q.popleft()
for i in adj[s]:
if visited[i] == 0:
q.append(i)
visited[i] = 1
tab[i - 1] = tab[s - 1] + 1
return tab
n, u, v = map(int, input().split())
adj = d(list)
for i in range(n - 1):
a, b = map(int, input().split())
adj[a].append(b)
adj[b].append(a)
takahashi = bfs(adj, u, n)
aoki = bfs(adj, v, n)
res = -1
for i in range(n):
if takahashi[i] <= aoki[i]:
res = max(res, aoki[i] - 1)
print(res)
|
n,k = map(int,input().split())
ans = 1
while n >= k:
ans += 1
n //= k
print(ans)
| 0 | null | 90,934,920,644,288 | 259 | 212 |
import math
def main():
n = input()
p1 = [0., 0.]
p2 = [100., 0.]
a = [p1,p2]
a = rec(a,n)
for item in a:
print item[0], item[1]
def rec(a,n):
if n == 0:
return a
else:
n -= 1
b = []
for i in range(len(a)-1):
p1 = a[i]
p2 = a[i+1]
b += koch(p1,p2)
b.append(a[-1])
return rec(b,n)
def koch(p1,p2):
s = [p1[0]+(p2[0]-p1[0])/3., p1[1]+(p2[1]-p1[1])/3.]
t = [p1[0]+2.*(p2[0]-p1[0])/3., p1[1]+2.*(p2[1]-p1[1])/3.]
st = [t[0]-s[0],t[1]-s[1]]
u = [s[0]+(st[0]-math.sqrt(3)*st[1])/2., s[1]+(math.sqrt(3)*st[0]+st[1])/2.]
return [p1,s,u,t]
if __name__ == "__main__":
main()
|
def sub(strings, listseq):
for index in listseq:
strings = strings[index:] + strings[:index]
return strings
def main():
while True:
strings = input()
if strings == "-": break
times = int(input())
list1 = [int(input()) for i in range(times)]
print(sub(strings, list1))
main()
| 0 | null | 998,067,476,588 | 27 | 66 |
import numpy as np
h,w,m=map(int, input().split())
hw=[tuple(map(int,input().split())) for i in range(m)]
H=np.zeros(h)
W=np.zeros(w)
for i in range(m):
hi,wi=hw[i]
H[hi-1]+=1
W[wi-1]+=1
mh=max(H)
mw=max(W)
hmax=[i for i, x in enumerate(H) if x == mh]
wmax=[i for i, x in enumerate(W) if x == mw]
f=0
for i in range(m):
hi,wi=hw[i]
if H[hi-1]==mh and W[wi-1]==mw:
f+=1
if len(hmax)*len(wmax)-f<1:
print(int(mh+mw-1))
else:
print(int(mh+mw))
|
H, W, M, *HW = map(int, open(0).read().split())
HW = list(zip(*[iter(HW)] * 2))
A = [0] * (H + 1)
B = [0] * (W + 1)
for h, w in HW:
A[h] += 1
B[w] += 1
a = max(A)
b = max(B)
cnt = A.count(a) * B.count(b) - len([0 for h, w in HW if A[h] == a and B[w] == b])
print(a + b - (cnt == 0))
| 1 | 4,742,009,822,890 | null | 89 | 89 |
def main():
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
target = 1
for i in A:
if i == target:
target += 1
if target == 1:
print(-1)
return
print(N - target + 1)
main()
|
s = input()
t = input()
count = 0
for i in range(len(s)):
if s[i] != t[i]:
count = count + 1
print(count)
| 0 | null | 62,612,692,473,760 | 257 | 116 |
import sys
k1 = [[0 for i in xrange(10)] for j in xrange(3)]
k2 = [[0 for i in xrange(10)] for j in xrange(3)]
k3 = [[0 for i in xrange(10)] for j in xrange(3)]
k4 = [[0 for i in xrange(10)] for j in xrange(3)]
n = input()
for i in xrange(n):
m = map(int,raw_input().split())
if m[0] == 1:
k1[m[1]-1][m[2]-1] = k1[m[1]-1][m[2]-1] + m[3]
elif m[0] == 2:
k2[m[1]-1][m[2]-1] = k2[m[1]-1][m[2]-1] + m[3]
elif m[0] == 3:
k3[m[1]-1][m[2]-1] = k3[m[1]-1][m[2]-1] + m[3]
elif m[0] == 4:
k4[m[1]-1][m[2]-1] = k4[m[1]-1][m[2]-1] + m[3]
for i in xrange(3):
for j in xrange(10):
x = ' ' + str(k1[i][j])
sys.stdout.write(x)
if j == 9:
print ""
print"#"*20
for i in xrange(3):
for j in xrange(10):
y = ' ' + str(k2[i][j])
sys.stdout.write(y)
if j == 9:
print ""
print"#"*20
for i in xrange(3):
for j in xrange(10):
z = ' ' + str(k3[i][j])
sys.stdout.write(z)
if j == 9:
print ""
print"#"*20
for i in xrange(3):
for j in xrange(10):
zz = ' ' + str(k4[i][j])
sys.stdout.write(zz)
if j == 9:
print ""
|
bldgs = []
for k in range(4):
bldgs.append([[0 for i in range(10)] for j in range(3)])
n = int(input())
for i in range(n):
b,f,r,v = map(int, input().split(' '))
bldgs[b-1][f-1][r-1] += v
for i, bldg in enumerate(bldgs):
if i > 0:
print('#'*20)
for floor in bldg:
print(' '+' '.join([str(f) for f in floor]))
| 1 | 1,104,553,982,008 | null | 55 | 55 |
#atcoder template
def main():
import sys
imput = sys.stdin.readline
#文字列入力の時は上記はerrorとなる。
#ここにコード
#input
N = int(input())
A, B = [0] * N, [0] * N
for i in range(N):
A[i], B[i] = map(int, input().split())
#output
import statistics as st
import math
p = st.median(A)
q = st.median(B)
# %%
if N % 2 == 0:
print(int((q-p)/0.5)+1)
else:
print(int(q-p)+1)
#N = 1のときなどcorner caseを確認!
if __name__ == "__main__":
main()
|
N = int(input())
A = [0 for _ in range(N)]
B = [0 for _ in range(N)]
for i in range(N):
a,b = map(int,input().split())
A[i] = a
B[i] = b
A_sort = sorted(A)
B_sort = sorted(B)
if N % 2 == 1:
print(B_sort[N//2] - A_sort[N//2] + 1)
else:
med_A = (A_sort[N//2] + A_sort[N//2 - 1])
med_B = (B_sort[N//2] + B_sort[N//2 - 1])
print(med_B - med_A + 1)
| 1 | 17,265,858,740,052 | null | 137 | 137 |
n = int(input())
x = list(map(float, input().split()))
y = list(map(float, input().split()))
p_1 = sum([abs(x[i]-y[i]) for i in range(n)])
p_2 = (sum([(x[i]-y[i])**2 for i in range(n)]))**0.5
p_3 = (sum([(abs(x[i]-y[i]))**3 for i in range(n)]))**(1/3)
p_inf = max([abs(x[i]-y[i]) for i in range(n)])
print("{:.5f}".format(p_1))
print("{:.5f}".format(p_2))
print("{:.5f}".format(p_3))
print("{:.5f}".format(p_inf))
|
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = min(a) + min(b)
for i in range(M):
x, y, c = map(int, input().split())
p = a[x-1] + b[y-1] - c
if p < ans:
ans = p
print(ans)
| 0 | null | 27,150,259,635,852 | 32 | 200 |
import sys
import time
import math
import itertools as it
def inpl():
return list(map(int, input().split()))
st = time.perf_counter()
# ------------------------------
N = int(input())
mn = 1001001001001
a = 1
while a*a <= N:
if N % a == 0:
mn = min(mn, a + N//a - 2)
a += 1
print(mn)
# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)
|
import math
def is_prime(n):
if n == 1: return False
for k in range(2, int(math.sqrt(n)) + 1):
if n % k == 0:
return False
return True
n = int(input())
if is_prime(n):
print(n -1)
exit()
ans = n
for k in range(2, int(math.sqrt(n)) + 1):
if n % k == 0:
temp_1 = n/k
temp_2 = k
ans = min(ans,temp_1 + temp_2)
print(int(ans-2))
| 1 | 162,269,875,089,336 | null | 288 | 288 |
N=int(input())
alst=list(map(int,input().split()))
i=1
for a in alst:
if a==i:
i+=1
i-=1
if i>0:
print(N-i)
else:
print(-1)
|
import sys
input = lambda: sys.stdin.readline().rstrip()
def main():
n = int(input())
a = [int(i) for i in input().split()]
mark = 1
remain = 0
for i in range(n):
if a[i] == mark:
remain += 1
mark += 1
if remain == 0:
print(-1)
else:
print(n-remain)
if __name__ == '__main__':
main()
| 1 | 115,172,506,724,132 | null | 257 | 257 |
N=int(input())
K=int(input())
import sys
sys.setrecursionlimit(10**6)
def solve(N, K):
if K==1:
l=len(str(N))
ans=0
for i in range(l):
if i!=l-1:
ans+=9
else:
ans+=int(str(N)[0])
#print(ans)
return ans
else:
nextK=K-1
l=len(str(N))
ini=int(str(N)[0])
res=ini*(10**(l-1))-1
#print(res)
if l>=K:
ans=solve(res, K)
else:
ans=0
if l-1>=nextK:
nextN=int(str(N)[1:])
ans+=solve(nextN, nextK)
return ans
return ans
ans=solve(N,K)
print(ans)
|
def main(M_1: int, D_1: int, M_2: int, D_2: int):
print(M_2 - M_1)
if __name__ == "__main__":
M_1, D_1 = map(int, input().split())
M_2, D_2 = map(int, input().split())
main(M_1, D_1, M_2, D_2)
| 0 | null | 100,105,865,931,840 | 224 | 264 |
n = int(input())
result = 100000
for i in range(0,n):
result = result * 1.05
if result % 1000 == 0:
pass
else:
result = result-(result % 1000)+ 1000
result = int(result)
print(result)
|
import math
a,b,C=map(int,input().split())
radC=math.radians(C)
S=a*b*math.sin(radC)*(1/2)
c=a**2+b**2-2*a*b*math.cos(radC)
L=a+b+math.sqrt(c)
h=2*S/a
list=[S,L,h]
for i in list:
print('{:.08f}'.format(i))
| 0 | null | 86,412,764,228 | 6 | 30 |
#ALL you can eat
#0-1 なっぷサック問題をとく* N
#要領T-1のバックと 大きさがA,価値がBの荷物がNこずつある。 この時これらを使って価値をT-1をこえない範囲で最大にする方法を考える。
#dpl[N][V]=1番目からN番目までの荷物を用いて、重さをVにする上で実現可能な価値の最大値
N,T=map(int,input().split())
d=[]
for i in range(N):
a,b=map(int,input().split())
d.append((a,b))
dpl=[[0 for i in range(T)] for j in range(N+1)]
for i in range(N):
weight,value=d[i]
for j in range(T):
if j>=weight:
dpl[i+1][j]=max(dpl[i][j],dpl[i][j-weight]+value)
else:
dpl[i+1][j]=dpl[i][j]
#dpr[K][V]=K番目からN番目までの荷物を用いて、重さをVにする上で実現可能な価値の最大値
dpr=[[0 for i in range(T)] for j in range(N+2)]
for i in range(1,N+1):
weight,value=d[-i]
for j in range(T):
if j>=weight:
dpr[N+1-i][j]=max(dpr[N+2-i][j],dpr[N+2-i][j-weight]+value)
else:
dpr[N+1-i][j]=dpr[N+2-i][j]
#dpr[K][V]=K番目からN番目までの荷物を用いて重さをV以下にする上で実現可能な価値の最大値にする
for i in range(1,N+1):
for j in range(1,T):
dpr[i][j]=max(dpr[i][j],dpr[i][j-1])
ans=0
for i in range(N):
sub=d[i][1]
#i+1番目の物を使わない
add=0
for j in range(T):
add=max(add,dpl[i][j]+dpr[i+2][T-1-j])
sub+=add
ans=max(sub,ans)
print(ans)
|
import math
n = int(input())
xv = [int(xi) for xi in input().split()]
yv = [int(yi) for yi in input().split()]
print(sum([abs(xi-yi) for (xi, yi) in zip(xv, yv)]))
print(math.sqrt(sum([pow(abs(xi-yi), 2) for (xi, yi) in zip(xv, yv)])))
print(math.pow(sum([pow(abs(xi-yi), 3) for (xi, yi) in zip(xv, yv)]), 1/3))
print(max([abs(xi-yi) for (xi, yi) in zip(xv, yv)]))
| 0 | null | 75,551,736,965,458 | 282 | 32 |
n = int(input())
lst = [0] *4
for i in range(n):
a = input()
if a == 'AC':
lst[0] += 1
if a == 'WA':
lst[1] += 1
if a == 'TLE':
lst[2] += 1
if a == 'RE':
lst[3] += 1
print('AC x',lst[0])
print('WA x',lst[1])
print('TLE x',lst[2])
print('RE x',lst[3])
|
s=input()
t=input()
for i in range(len(s)):
if s[i]!=t[i]:
print("No")
exit()
if len(t)==len(s)+1:
print("Yes")
| 0 | null | 15,041,059,309,754 | 109 | 147 |
# coding:utf-8
import sys
n = input()
array = []
for i in range(n):
array.append(map(int, raw_input().split()))
array.sort()
result = [[0 for i in range(4)] for j in range(n)]
index = 0
result[index] = array[0][0:4]
for i in range(1,n):
if result[index][0:3] == array[i][0:3]:
result[index][3] += array[i][3]
elif result[index][0:3] != array[i][0:3]:
index += 1
result[index] = array[i][0:]
index = 0
for i in range(4):
for j in range(3):
for k in range(10):
sys.stdout.write(' ')
if(i == result[index][0] - 1 and j == result[index][1] - 1 and k == result[index][2] - 1):
print result[index][3],
if index != len(result) - 1:
index += 1
else:
print 0,
else:
print
else:
if(i != 3):
print "####################"
|
import sys
n = int(raw_input())
building_a = [[0 for j in range(11)] for i in range(4)]
building_b = [[0 for j in range(11)] for i in range(4)]
building_c = [[0 for j in range(11)] for i in range(4)]
building_d = [[0 for j in range(11)] for i in range(4)]
for i in range(n):
b, f, r, v = map(int, raw_input().split())
if b == 1:
building_a[f][r] += v
elif b == 2:
building_b[f][r] += v
elif b == 3:
building_c[f][r] += v
elif b == 4:
building_d[f][r] += v
for i in range(1, 4):
print "",
for j in range(1, 11):
print building_a[i][j],
print
for i in range(20):
sys.stdout.write("#")
print
for i in range(1, 4):
print "",
for j in range(1, 11):
print building_b[i][j],
print
for i in range(20):
sys.stdout.write ("#")
print
for i in range(1, 4):
print "",
for j in range(1, 11):
print building_c[i][j],
print
for i in range(20):
sys.stdout.write("#")
print
for i in range(1, 4):
print "",
for j in range(1, 11):
print building_d[i][j],
print
| 1 | 1,118,978,618,240 | null | 55 | 55 |
n=int(input())
m=1000000007
print(((pow(10,n,m)-2*pow(9,n,m)+pow(8,n,m))+m)%m)
|
N = int(input())
MOD = 10**9 + 7
val1 = pow(9,N,MOD)
val2 = pow(8,N,MOD)
ans = pow(10,N,MOD) - 2*val1 + val2
print(ans%MOD)
| 1 | 3,167,662,733,940 | null | 78 | 78 |
N, K = map(int, input().split())
H = tuple(map(int, input().split()))
assert len(H) == N
print(len([h for h in H if h >= K]))
|
s = raw_input()
p = raw_input()
s += s
if p in s:
print "Yes"
else:
print "No"
| 0 | null | 90,556,180,870,008 | 298 | 64 |
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
aa = [0]
bb = [0]
for i in range(n):
aa.append(a[i]+aa[-1])
for i in range(m):
bb.append(b[i]+bb[-1])
ans = 0
j = m
for i in range(n+1):
r = k - aa[i]
while bb[j] > r and j >= 0:
j -= 1
if j >= 0:
ans = max(ans, i+j)
print(ans)
|
def roll(l, command):
'''
return rolled list
l : string list
command: string
'''
res = []
i = -1
if command =='N':
res = [l[i+2], l[i+6], l[i+3], l[i+4], l[i+1], l[i+5]]
if command =='S':
res = [l[i+5], l[i+1], l[i+3], l[i+4], l[i+6], l[i+2]]
if command =='E':
res = [l[i+4], l[i+2], l[i+1], l[i+6], l[i+5], l[i+3]]
if command =='W':
res = [l[i+3], l[i+2], l[i+6], l[i+1], l[i+5], l[i+4]]
return res
faces = input().split()
commands = list(input())
for command in commands:
faces = roll(faces, command)
print(faces[0])
| 0 | null | 5,562,134,905,980 | 117 | 33 |
def rec_z():
for i in range(N):
for j in range(Hmax+1):
if j >= a[i]:
dp[j] = min(dp[j], dp[j-a[i]] + b[i])
return min(dp[H:Hmax+1])
H, N = map(int,input().split())
a = []
b = []
for i in range(N):
aa, bb = map(int, input().split())
a.append(aa)
b.append(bb)
Hmax = H + max(a)
INF = 10**9
dp = [INF for _ in range(Hmax+1)]
dp[0] = 0
print(rec_z())
|
h,n=map(int, input().split())
a=[0]*n
b=[0]*n
for i in range(n):
a[i],b[i]=map(int,input().split())
max_a=max(a)
dp=[float("inf")]*(h+max_a+1)
dp[0]=0
for i in range(h):
for j in range(n):
dp[i+a[j]] = min(dp[i+a[j]], dp[i]+b[j])
print(min(dp[h:]))
| 1 | 81,298,444,243,364 | null | 229 | 229 |
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def f(x):
cnt = 0
while x%2 == 0:
cnt += 1
x //= 2
return cnt
def lcm(x,y):
return x*y//math.gcd(x,y)
n,m = inpl()
a = inpl()
a = list(set(a))
c = list(set([f(x) for x in a]))
if len(c) != 1:
print(0)
quit()
c = c[0]
x = 1
for i in range(len(a)):
x = lcm(a[i]//2, x)
# print(x)
print((m+x)//(2*x))
|
n, m = map( int, input().split() )
a = list( map( int, input().split() ) )
def f( a_k ): # 2で割り切れる回数
count = 0
while a_k % 2 == 0:
count += 1
a_k = a_k // 2
return count
b = []
f_0 = f( a[ 0 ] )
for a_k in a:
f_k = f( a_k )
if f_k != f_0:
print( 0 )
exit()
b.append( a_k // pow( 2, f_k ) )
import math
def lcm( x, y ):
return x * y // math.gcd( x, y )
b_lcm = 1
for b_k in b:
b_lcm = lcm( b_lcm, b_k )
a_lcm = b_lcm * pow( 2, f_0 )
# print( b_lcm, f_0, b )
print( ( m + a_lcm // 2 ) // a_lcm )
| 1 | 101,684,915,055,068 | null | 247 | 247 |
from sys import stdin
readline = stdin.readline
def i_input(): return int(readline().rstrip())
def i_map(): return map(int, readline().rstrip().split())
def i_list(): return list(i_map())
def main():
X, Y = i_map()
ans = False
z = Y - (X * 2)
if z < 0:
pass
elif z == 0 or (z % 2 == 0 and (z // 2) <= X):
ans = True
print("Yes" if ans else "No")
if __name__ == "__main__":
main()
|
x, y = map(int, input().split())
for i in range(1, x+1):
if (4*i + 2*(x - i) == y) or (2*i + 4*(x - i) == y):
print("Yes")
exit()
print("No")
| 1 | 13,757,041,417,252 | null | 127 | 127 |
def main():
n,x,m = map(int,input().split())
a = [0] * m
b = [x]
ans = x
i = 1
while i < n:
x = (x*x) % m
if x == 0:
print(ans)
exit()
if a[x] == 0:
a[x] = i
b.append(x)
ans += x
i += 1
else :
q = len(b) - a[x]
qq = (n-len(b)) // q
qqq = (n-len(b)) % q
ans += sum(b[a[x]:]) * qq
ans += sum(b[a[x]:a[x]+qqq])
#print(b + b[a:] * qq + b[a:a+qqq])
print(ans)
exit()
print(ans)
if __name__ == '__main__':
main()
|
import sys
n,x,m = map(int,input().split())
lst_sum = [0,x]
loop = [0]*(10**5)
loop[x] = 1
s = 1
for i in range(2,n+1):
y = ((lst_sum[-1] - lst_sum[-2])**2)%m
if loop[y] != 0:
l = i-loop[y]
d = (n-loop[y]+1) // l
q = (n-loop[y]+1) % l
sum_loop = lst_sum[-1]-lst_sum[loop[y]-1]
sum_first = lst_sum[loop[y]-1]
if q == 0:
sum_rest = 0
else:
sum_rest = lst_sum[loop[y] + q-1] - lst_sum[loop[y]-1]
print(sum_first+d*sum_loop+sum_rest)
sys.exit()
else:
s += 1
loop[y] = s
lst_sum.append(lst_sum[-1] + y)
print(lst_sum[-1])
| 1 | 2,793,723,565,760 | null | 75 | 75 |
def resolve():
N, K = list(map(int, input().split()))
A = list(map(lambda x: int(x)-1, input().split()))
routes = [0]
current = 0
visited = [False for _ in range(N)]
visited[0] = True
while visited[A[current]] is False:
current = A[current]
routes.append(current)
visited[current] = True
leftlen = routes.index(A[current])
repeat = routes[leftlen:]
# print(leftlen)
# print(routes)
# print(repeat)
print(repeat[(K-leftlen)%len(repeat)]+1 if K > leftlen else routes[K]+1)
if '__main__' == __name__:
resolve()
|
N, K = map(int, input().split())
A = list(map(int, input().split()))
A = [-1] + A
loop_pos = None
loop_start_time = None
loop_end_time = None
visited = [-1]*(N+1)
pos = 1
i = 0
while visited[pos] == -1:
visited[pos] = i
pos = A[pos]
i += 1
if visited[pos] != -1:
loop_pos = pos
loop_start_time = visited[pos]
loop_end_time = i
# print(loop_start_time, loop_end_time)
w = loop_end_time - loop_start_time
K = min(K, loop_start_time) + max(0, K-loop_start_time)%w
# print(f"K: {K}")
pos = 1
for i in range(K):
# print(pos)
pos = A[pos]
print(pos)
| 1 | 22,867,979,893,878 | null | 150 | 150 |
X, N = map(int, input().split())
p_list = list(map(int, input().split()))
if N == 0:
print(X)
else:
for i in range(0,100):
if not X-i in p_list:
print(X-i)
break
elif not X+i in p_list:
print(X+i)
break
|
n = int(input())
d = []
for i in range(n):
d.append(list(map(int,input().split())))
cnt = 0
flag = 0
for i in range(n):
if d[i][0] == d[i][1]:
cnt = cnt + 1
else:
cnt = 0
if cnt == 3:
flag = 1
if flag == 1:
print("Yes")
else:
print("No")
| 0 | null | 8,266,592,743,178 | 128 | 72 |
# coding:utf-8
# ??\???
n,m = map(int, input().split())
matrix = []
for i in range(n):
matrix.append([0] * m)
for i in range(n):
col = input().split()
for j in range(m):
matrix[i][j] = int(col[j])
vector = [0] * m
for j in range(m):
vector[j] = int(input())
for i in range(n):
row_sum = 0
for j in range(m):
row_sum += matrix[i][j] * vector[j]
print(row_sum)
|
n,m = [int(i) for i in input().split()]
a = [[0 for i in range(m)] for j in range(n)]
for i in range(n):
a[i] = [int(j) for j in input().split()]
b = [0 for i in range(m)]
for i in range(m):
b[i] = int(input())
for i in range(n):
sum = 0
for j in range(m):
sum += a[i][j] * b[j]
print(sum)
| 1 | 1,176,341,131,888 | null | 56 | 56 |
n,m,k = map(int,input().split())
a = [0] + list(map(int,input().split()))
b = [0] + list(map(int,input().split()))
for i in range(1,n+1):
a[i] += a[i-1]
for i in range(1,m+1):
b[i] += b[i-1]
ans,ai,bi = 0,0,0
for i in range(n+1):
if a[i]<=k:
ai = i
for j in range(m+1):
if a[ai]+b[j]<=k:
bi = j
ans = ai+bi
for i in range(ai):
for j in range(bi,m+1):
if a[ai-i-1]+b[j]>k:
bi = j
break
else:
ans = max(ans,ai-i-1+j)
print(ans)
|
N=int(input())
if N&0b1:
ans=N//2+1
else:
ans=N//2
print(ans)
| 0 | null | 35,161,565,883,742 | 117 | 206 |
N = int(input())
S = input()
'''
Editorialのヒントを参照しました。
000 ~ 999の実現可能性を考えます。
'''
L = []
s = ''
Comb = 0
key_k = False
for i in range(0, 10):
for j in range(0, 10):
if key_k is not True:
s += str(j)
else:
s += str(i)
s += str(j)
key_k = False
for k in range(0, 10):
s += str(k)
L.append(s)
s = ''
s += str(i)
s += str(j)
s = ''
key_k = True
L.remove(L[0])
L.append('000') #生成、もう少し短くしたい
S_copy = S
A = []
for el in L:
c0 = el[0]
c1 = el[1]
c2 = el[2]
S = S_copy
N0 = S.find(c0)
if N0 == -1:
continue
else:
S = S[N0+1::]
N1 = S.find(c1)
if N1 == -1:
continue
else:
S = S[N1+1::]
N2 = S.find(c2)
if N2 == -1:
continue
else:
Comb += 1
print(Comb)
|
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
xyc= [list(map(int, input().split())) for _ in range(M)]
minmoney = min(a) + min(b)
for obj in xyc:
summoney = a[obj[0] - 1] + b[obj[1] - 1] - obj[2]
minmoney = min(minmoney, summoney)
print(minmoney)
| 0 | null | 91,396,686,923,104 | 267 | 200 |
n, k, c = map(int, input().split())
s = input()
s2 = s[::-1]
dp1 = [0] * (n + 2)
dp2 = [0] * (n + 2)
for (dp, ss) in zip([dp1, dp2], [s, s2]):
for i in range(n):
if ss[i] == 'x':
dp[i+1] = dp[i]
elif i <= c:
dp[i+1] = min(1, dp[i] + 1)
else:
dp[i+1] = max(dp[i-c] + 1, dp[i])
dp2 = dp2[::-1]
for i in range(1, n+1):
if s[i-1] == 'o' and dp1[i-1] + dp2[i+1] < k:
print(i)
|
import sys, bisect, math, itertools, heapq, collections
from operator import itemgetter
# a.sort(key=itemgetter(i)) # i番目要素でsort
from functools import lru_cache
# @lru_cache(maxsize=None)
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp():
'''
一つの整数
'''
return int(input())
def inpl():
'''
一行に複数の整数
'''
return list(map(int, input().split()))
n, k, c = inpl()
s = list(input())[:-1]
q = collections.deque()
cool = 0
left = [0] * n
right = [0] * n
val=-1
for i in range(n):
if cool<=0 and s[i]=="o" and val<k:
val += 1
cool = c
else:
cool-=1
left[i] = val
cool = 0
val+=1
for i in range(n-1,-1,-1):
if cool<=0 and s[i]=="o" and val>0:
val -= 1
cool = c
else:
cool-=1
right[i]=val
# print(left)
# print(right)
for i in range(n):
if left[i]==right[i] and (i == 0 or left[i] - left[i - 1] == 1) and (i == n - 1 or right[i + 1] - right[i] == 1):
print(i + 1)
| 1 | 40,777,879,863,772 | null | 182 | 182 |
import math
n = int(input())
address = list(map(int, input().split()))
min_hp = math.inf
for i in range(1, 101):
hp = sum(map(lambda x: (x - i) ** 2, address))
if hp < min_hp:
min_hp = hp
print(min_hp)
|
N = int(input())
Xs = list(map(int, input().split()))
ans = 10**10
for P in range(0, 102):
cost = 0
for X in Xs:
cost += (X-P)**2
ans = min(ans, cost)
print(ans)
| 1 | 65,444,221,786,460 | null | 213 | 213 |
Hs,Ms,He,Me,K=map(int,input().split())
print(str((He-Hs)*60+Me-Ms-K))
|
import sys
tscore = hscore = 0
n = int( sys.stdin.readline() )
for i in range( n ):
tcard, hcard = sys.stdin.readline().rstrip().split( " " )
if tcard < hcard:
hscore += 3
elif hcard < tcard:
tscore += 3
else:
hscore += 1
tscore += 1
print( "{:d} {:d}".format( tscore, hscore ) )
| 0 | null | 10,144,200,364,772 | 139 | 67 |
n = int(input())
cnt = [[[0 for _ in range(10)] for _ in range(3)] for _ in range(4)]
for _ in range(n):
b, f, r, v = map(int, input().split())
cnt[b-1][f-1][r-1] += v
for i in range(4):
for j in range(3):
print(' '+' '.join(map(str, cnt[i][j])))
if i != 3:
print('#'*20)
|
arr = [[[0 for i in range(10)] for j in range(3)] for k in range(4)]
for i in range(int(input())):
b,f,r,v = map(int, input().split())
arr[b-1][f-1][r-1] += v
for i in range(4):
for j in range(3):
for k in range(10):
if k == 0:
print ("", end=" ")
if k == 9:
print(arr[i][j][k])
else:
print(arr[i][j][k], end=" ")
if i != 3:
print("#"*20)
| 1 | 1,121,816,285,412 | null | 55 | 55 |
# coding: utf-8
while True:
h, w = map(int, input().split())
if h == 0 and w == 0:
exit()
else:
print('#' * w)
for _ in range(h - 2):
print('#' + '.' * (w - 2) + '#')
print('#' * w)
print()
|
while True:
x = [int(z) for z in input().split(" ")]
if x[0] == 0 and x[1] == 0: break
for h in range(0,x[0]):
for w in range(0,x[1]):
if h == 0 or h == x[0]-1:
print("#", end="")
elif w == 0 or w == x[1]-1:
print("#", end="")
else:
print(".", end="")
print("\n", end="")
print("")
| 1 | 833,522,819,632 | null | 50 | 50 |
from collections import defaultdict
N, K, *A = map(int, open(0).read().split())
S = [0] * (N + 1)
for i in range(N):
S[i + 1] = S[i] + A[i]
d = defaultdict(int)
ans = 0
for j in range(N + 1):
v = (S[j] - j) % K
ans += d[v]
d[v] += 1
if j >= K - 1:
d[(S[j - K + 1] - (j - K + 1)) % K] -= 1
print(ans)
|
import sys
import decimal # 10進法に変換,正確な計算
def input():
return sys.stdin.readline().strip()
def main():
s, t = input().split()
print(t, end="")
print(s)
main()
| 0 | null | 120,524,871,029,024 | 273 | 248 |
x, y = map(int,input().split())
while True:
if x >= y:
x %= y
else:
y %= x
if x == 0 or y == 0:
print(x+y)
break
|
def make_divisors(n):
cnt = 0
i = 1
while i*i <= n:
if n % i == 0:
cnt += 1
if i != n // i:
cnt += 1
i += 1
return cnt
numbers = [1,10, 100, 1000, 10000, 100000, 150000,200000, 250000,300000,350000, 400000,450000, 500000,550000,600000,630000,675000,700000,720000,750000,800000,825000,850000,875000,900000,912500,925000,950000,975000,987500,1000000]
datas = [0,23,473,7053,93643,1166714, 1810893, 2472071, 3145923, 3829761, 4522005,5221424,5927120,6638407,7354651,8075420,8509899,9164377,9529244,9821778,10261678,10997400,11366472,11736253,12106789,12478098,12664043,12850013,13222660,13595947,13782871,13969985]
N = int(input())
ttl = 0
numPin = 0
for i in range(len(numbers)):
if N == 1000000:
numPin = len(numbers) - 1
break
if numbers[i] <= N < numbers[i+1]:
numPin = i
break
if numPin != len(numbers)-1:
if N - numbers[numPin] < numbers[numPin + 1] - N:
for i in range(numbers[numPin], N):
ttl += make_divisors(i)
print(ttl + datas[numPin])
else:
for i in range(N,numbers[numPin+1]):
ttl -= make_divisors(i)
print(ttl + datas[numPin + 1])
else:
print(datas[numPin])
| 0 | null | 1,304,514,823,462 | 11 | 73 |
a = list(map(int,input().split()))
if a[0]/a[2] <= a[1]:
print("Yes")
else:
print("No")
|
class Dice:
def __init__(self, eyes):
self._eyes = eyes
@property
def eye(self):
return self._eyes[1]
def roll(self, direction):
if direction == 'N':
old = self._eyes
new = ['dummy'] * 7
new[1] = old[2]
new[2] = old[6]
new[3] = old[3]
new[4] = old[4]
new[5] = old[1]
new[6] = old[5]
self._eyes = new
elif direction == 'S':
old = self._eyes
new = ['dummy'] * 7
new[1] = old[5]
new[2] = old[1]
new[3] = old[3]
new[4] = old[4]
new[5] = old[6]
new[6] = old[2]
self._eyes = new
elif direction == 'W':
old = self._eyes
new = ['dummy'] * 7
new[1] = old[3]
new[2] = old[2]
new[3] = old[6]
new[4] = old[1]
new[5] = old[5]
new[6] = old[4]
self._eyes = new
elif direction == 'E':
old = self._eyes
new = ['dummy'] * 7
new[1] = old[4]
new[2] = old[2]
new[3] = old[1]
new[4] = old[6]
new[5] = old[5]
new[6] = old[3]
self._eyes = new
else:
raise ValueError('NEWS箱推し')
eyes = ['dummy'] + input().split()
dice = Dice(eyes)
direction_text = input()
for d in direction_text:
dice.roll(d)
print(dice.eye)
| 0 | null | 1,869,835,972,558 | 81 | 33 |
def main():
a, b = map(int, input().split())
import math
print(int(a*b/math.gcd(a,b)))
if __name__ == "__main__":
main()
|
a,b = map(int, input().split())
child = a*b
if a < b:
a,b = b,a
r = a%b
while r > 0:
a = b
b = r
r = a%b
print(child//b)
| 1 | 113,210,914,473,464 | null | 256 | 256 |
import sys
input = sys.stdin.readline
K = int(input())
S = list(input())[: -1]
N = len(S)
mod = 10 ** 9 + 7
class Factorial:
def __init__(self, n, mod):
self.f = [1]
for i in range(1, n + 1):
self.f.append(self.f[-1] * i % mod)
self.i = [pow(self.f[-1], mod - 2, mod)]
for i in range(1, n + 1)[: : -1]:
self.i.append(self.i[-1] * i % mod)
self.i.reverse()
def factorial(self, i):
return self.f[i]
def ifactorial(self, i):
return self.i[i]
def combi(self, n, k):
return self.f[n] * self.i[n - k] % mod * self.i[k] % mod
f = Factorial(N + K + 1, mod)
res = 0
for l in range(K + 1):
r = K - l
res += f.combi(l + N - 1, l) * pow(25, l, mod) % mod * pow(26, r, mod) % mod
res %= mod
print(res)
|
t=0
h=0
n=input()
for _ in range (n):
s = raw_input().split()
if s[0] > s[1]:
t+=3
elif s[0] < s[1]:
h+=3
else:
t+=1
h+=1
print str(t) + " " + str(h)
| 0 | null | 7,475,135,109,712 | 124 | 67 |
l=[]
for mark in ["S","H","C","D"]:
for i in range(1,14):
l.append(mark+" "+str(i))
n=input()
for i in range(int(n)):
l.remove(input())
for i in l:
print(i)
|
import sys
n = int(input()) # ?????£?????????????????????????????°
pic_list = ['S', 'H', 'C', 'D']
card_dict = {'S':[], 'H':[], 'C':[], 'D':[]} # ?????£?????????????????\?????????????????°
lost_card_dict = {'S':[], 'H':[], 'C':[], 'D':[]} # ?¶??????????????????\?????????????????°
# ?????????????¨??????\???????????????
for i in range(n):
pic, num = input().split()
card_dict[pic].append(int(num))
# ?¶???????????????????????¨??????\????¨????
for pic in card_dict.keys():
for j in range(1, 14):
if not j in card_dict[pic]:
lost_card_dict[pic].append(j)
# ?????????????????????
for pic in card_dict.keys():
lost_card_dict[pic] = sorted(lost_card_dict[pic])
# ?¶????????????????????????????
for pic in pic_list:
for k in range(len(lost_card_dict[pic])):
print(pic, lost_card_dict[pic][k])
| 1 | 1,055,037,742,240 | null | 54 | 54 |
def main():
ary = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14,
1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
K = int(input())
print(ary[K - 1])
main()
|
N = int(input())
import math
print(int(math.ceil(N / 1000) * 1000 - N))
| 0 | null | 29,263,954,751,710 | 195 | 108 |
n, k = map(int, input().split())
ans = 0
while n != 0:
n //= k
ans += 1
print(ans)
|
N, K = map(int, input().split())
for i in range(1000000):
if K ** (i - 1) <= N < K ** i:
print(i)
break
| 1 | 64,682,668,283,618 | null | 212 | 212 |
X=int(input())
i=1
a=0
while True:
a=a+X
if a%360==0:
print(i)
break
i+=1
|
x=int(input())
next=[]
now=[1,2,3,4,5,6,7,8,9]
for _ in range(11):
if x<=len(now):
print(now[x-1])
exit()
else:
x-=len(now)
next=now
now=[]
for i in next:
i=str(i)
if i[-1]=='0':
j=i+'0'
k=i+'1'
now+=[j]
now+=[k]
elif i[-1]=='9':
j=i+'8'
k=i+'9'
now+=[j]
now+=[k]
else:
j=i+str(int(i[-1])-1)
k=i+str(int(i[-1]))
l=i+str(int(i[-1])+1)
now+=[j]
now+=[k]
now+=[l]
| 0 | null | 26,524,159,385,152 | 125 | 181 |
A=int(input())
B=int(input())
print(6-(A+B))
|
if __name__ == '__main__':
W = input().lower()
words = []
while True:
line = input()
[words.append(i) for i in line.lower().split()]
if 'END_OF_TEXT' in line: break
print(words.count(W))
| 0 | null | 55,994,600,855,930 | 254 | 65 |
r = input()
print(r[0:3])
|
while True:
a, b = map(int, input().split())
if a == 0 and b == 0:
break;
for i in range(0, a):
if i % 2 == 0:
print(("#." * int((b + 1) / 2))[:b])
else:
print((".#" * int((b + 1) / 2))[:b])
print("")
| 0 | null | 7,836,806,037,998 | 130 | 51 |
def Qb():
n, k = map(int, input().split())
ans = set()
for v in range(k):
d = int(input())
ns = [int(v) for v in input().split()]
for N in ns:
ans.add(N)
print(n - len(ans))
if __name__ == '__main__':
Qb()
|
from fractions import gcd
n, m = map(int, input().split())
a = list(map(int, input().split()))
def lcm(a, b):
return a*b // gcd(a, b)
"""
akが偶数だから、bk=2akとして、
bk × (2p + 1)を満たすpが存在すれば良い
2p+1は奇数のため、Xとbkの2の素因数は一致しなければならない
akに含まれる最大の2^kを探す。
"""
if len(a) == 1: lcm_all = a[0]//2
else: lcm_all = lcm(a[0]//2, a[1]//2)
amax = 0
for i in range(1, n):
lcm_all = lcm(lcm_all, a[i]//2)
amax = max(amax, a[i]//2)
f = True
if lcm_all > m:
f = False
for i in range(n):
if n == 1: break
if (lcm_all//(a[i]//2))%2 == 0:
# akの2の約数の数がそろわないので、どんな奇数を掛けても共通のXを作れない
f = False
if f:
# amaxの奇数倍でmを超えないもの
ans = (m // lcm_all + 1)//2
print(ans)
else:
print(0)
| 0 | null | 63,299,698,903,310 | 154 | 247 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.