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 sys
nums = sorted( [ int( val ) for val in sys.stdin.readline().split( " " ) ] )
print( "{:d} {:d} {:d}".format( nums[0], nums[1], nums[2] ) ) | a,b,c = map(int,input().split())
if a > b:
tmp = a
a = b
b = tmp
if b > c:
tmp = b
b = c
c = tmp
if a > b:
tmp = a
a = b
b = tmp
print(str(a) + " " + str(b) + " " + str(c))
| 1 | 424,458,035,870 | null | 40 | 40 |
n, k = map(int,input().split())
count = 0
while True:
n = n//k
count += 1
if n <1:
break
print(count) | import sys
N,K=map(int,input().split())
if N<K:
print(1)
sys.exit()
M=K
R=1
while M<=N:
M*=K
R+=1
print(R) | 1 | 64,157,378,548,672 | null | 212 | 212 |
n,k = map(int, input().split())
H = list(map(int, input().split()))
H.sort()
H.reverse()
if n <= k:
print(0)
else:
print(sum(H[k:n]))
| def p_has_value_x(p:list, x:int):
for item in p:
if int(item) == x:
return True
return False
def solve(p: list, x:int) -> int:
if not p_has_value_x(p, x):
return x
pos = 1
while True:
aim_value = x - pos
if aim_value <= 0:
return aim_value
if not p_has_value_x(p, aim_value):
return aim_value
aim_value = x + pos
if not p_has_value_x(p, aim_value):
return aim_value
pos += 1
x, n = map(int, input().split())
if n == 0:
print(x)
else:
p = input().split()
print(solve(p, x))
| 0 | null | 46,571,306,608,320 | 227 | 128 |
N=int(input())
st= [list(map(str, input().split())) for _ in range(N)]
X=input()
ans=0
check=0
for i in range(N):
if check==1:
ans+=int(st[i][1])
if st[i][0]==X:
check=1
print(ans) | # 147 B
s = input()
k = s[::-1]
n = 0
for i in range(len(s)):
if s[i] != k[i]:
n += 1
# print(s[i])
print(n // 2)
| 0 | null | 108,907,390,576,542 | 243 | 261 |
import sys
for i,ws in enumerate(sys.stdin,1):
list = sorted(map(int, ws[:-1].split()))
if list[0] == 0 and list[1] == 0:
break
print(' '.join(map(str,list))) | #coding:utf-8
#1_3_C 2015.3.24
while True:
x,y = map(int,input().split())
if (x,y) == (0,0):
break
elif x > y:
x,y = y,x
print('{} {}'.format(x,y)) | 1 | 513,815,014,050 | null | 43 | 43 |
n, m = input().split()
s1 = n*int(m)
s2 = m*int(n)
print(min(s1, s2)) | a, b = map(int, input().split())
alpha = str(b)
beta = str(a)
if a >= b:
for k in range(a - 1):
alpha = alpha + str(b)
print(int(alpha))
else:
for k in range(b - 1):
beta = beta + str(a)
print(beta) | 1 | 84,721,473,812,092 | null | 232 | 232 |
s=list(input())
if s[1]=="A" and s[0]=="A" and s[2]=="A":
print("No")
elif s[1]=="B" and s[0]=="B" and s[2]=="B":
print("No")
else:
print("Yes") | s=input()
l=set(s)
if(len(l)==1):
print("No")
else:
print("Yes")
| 1 | 54,596,189,574,560 | null | 201 | 201 |
D = 100000
N = int(raw_input())
for i in xrange(N):
D += D/20
if D % 1000 > 0:
D += 1000-D%1000
print D | v = 100000
for _ in [0]*int(input()):
v *= 1.05
v += 1000 * (v%1000 > 0)
v -= v%1000
print(int(v)) | 1 | 1,117,196,334 | null | 6 | 6 |
import sys
input = lambda: sys.stdin.readline().rstrip()
def resolve():
S = input()
cnt = [0]*2019
cnt[0] = 1
# Sの右側から1ずつ左に伸ばして、cntの対応するところに加算
rmd = 0
num_rate = 1
L = len(S)
for i in range(L):
rmd = (int(S[L-i-1])*num_rate + rmd) % 2019
num_rate = num_rate*10%2019
cnt[rmd] += 1
ans = sum([i*(i-1)//2 for i in cnt])
print(ans)
if __name__ == '__main__':
resolve()
| import itertools
N = int(input())
cities = [list(map(int, input().split())) for _ in range(N)]
patterns = itertools.permutations(cities, N)
result = 0
count = 0
for ptn in patterns:
count += 1
dis = 0
for i in range(N-1):
dis += ((ptn[i][0]-ptn[i+1][0])**2 + (ptn[i][1]-ptn[i+1][1])**2)**0.5
result += dis
print(result/count)
| 0 | null | 89,277,190,808,850 | 166 | 280 |
n, m, L = map(int, input().split())
abc = [list(map(int, input().split())) for _ in range(m)]
q = int(input())
st = [list(map(int, input().split())) for _ in range(q)]
d = [[float('inf') for _ in range(n)] for _ in range(n)]
for a, b, c in abc:
if c > L:
continue
d[a-1][b-1] = c
d[b-1][a-1] = c
def warshall_floyd(d):
for k in range(n):
for i in range(n):
if i == k or d[i][k] > L:
continue
for j in range(n):
if i == j:
continue
d[i][j] = min(d[i][j],d[i][k] + d[k][j])
return d
warshall_floyd(d)
for i in range(n):
for j in range(n):
if i == j:
continue
elif d[i][j] <= L:
d[i][j] = 1
else:
d[i][j] = float('inf')
warshall_floyd(d)
for s, t in st:
if d[s-1][t-1] == float('inf'):
print(-1)
else:
print(d[s-1][t-1] - 1)
| A = list(map(int, input().split()))
print('bust') if sum(A) >= 22 else print('win') | 0 | null | 146,551,128,309,312 | 295 | 260 |
k = int(input())
a = 7%k
if a == 0:
print(1)
exit()
for i in range(1,k+1):
a = (10*a + 7)%k
if a == 0:
print(i+1)
exit()
print(-1) | N=int(input())
num = 7
i=1
a=[]
prir1=1
while i<=N:
xx=num%N
if xx==0:
print(i)
prir1=0
break
num=10*xx+7
i+=1
if prir1:
print(-1) | 1 | 6,099,278,230,552 | null | 97 | 97 |
a,b=input().split()
c=int(a);d=int(b)
if c>=-1000 and d<=1000:
if c>d:
print("a > b")
elif c<d:
print("a < b")
else:
print("a == b") | #!/usr/bin/env python3
class UnionFind():
def __init__(self, n):
self.parents = list(range(n))
self.size = [1] * n
def find(self, x):
if self.parents[x] == x:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x != y:
if self.size[x] < self.size[y]:
x, y = y,x
self.parents[y] = x
self.size[x] += self.size[y]
def main():
N, M, K = map(int, input().split())
friend_adj = [[] for _ in range(N)]
block_adj = [[] for _ in range(N)]
uf = UnionFind(N)
for _ in range(M):
a, b = map(lambda x: int(x)-1, input().split())
friend_adj[a].append(b)
friend_adj[b].append(a)
uf.union(a,b)
for _ in range(K):
c, d = map(lambda x: int(x)-1, input().split())
if uf.find(c) == uf.find(d):
friend_adj[c].append(d)
friend_adj[d].append(c)
ans = [uf.size[uf.find(i)]-1 for i in range(N)]
for i in range(N):
ans[i] -= len(friend_adj[i])
# for b in block_adj[i]:
# if uf.find(b) == uf.find(i):
# ans[i] -= 1
print(*ans)
if __name__ == "__main__":
main()
| 0 | null | 31,064,390,247,374 | 38 | 209 |
# usr/bin/python
# coding: utf-8
################################################################################
#Write a program which prints multiplication tables in the following format:
#
#1x1=1
#1x2=2
#.
#.
#9x8=72
#9x9=81
#
################################################################################
if __name__ == "__main__":
for i in range(1, 10):
for j in range(1, 10):
print("{0}x{1}={2}".format(i,j,i*j))
exit(0) | from itertools import groupby
s = input()
A = [(key, sum(1 for _ in group)) for key, group in groupby(s)]
tmp = 0
ans = 0
for key, count in A:
if key == '<':
ans += count*(count+1)//2
tmp = count
else:
if tmp < count:
ans -= tmp
ans += count
ans += (count-1)*count//2
tmp = 0
print(ans)
| 0 | null | 78,144,747,616,188 | 1 | 285 |
import sys,queue,math,copy
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = 10**18
MOD = 10**9 + 7
LI = lambda : [int(x) for x in input().split()]
_LI = lambda : [int(x)-1 for x in input().split()]
N = int(input())
A = LI()
c =[0,0,0]
cnt = [0 for _ in range(N)]
for i in range(N):
cnt[i] = c.count(A[i])
if cnt[i] == 0:
print (0)
exit (0)
for j in range(3):
if c[j] == A[i]:
c[j] += 1
break
ans = 1
for i in range(N):
ans = (ans * cnt[i]) % MOD
print (ans) | n = int(input())
hats = list(map(int, input().split()))
mod = 1000000007
cnt = [0] * 3
ans = 1
for i in range(n):
count = 0
minj = 4
for j in range(3):
if hats[i] == cnt[j]:
count += 1
minj = min(minj, j)
if count == 0:
ans = 0
break
cnt[minj] += 1
ans = ans * count % mod
print(ans) | 1 | 130,541,588,009,670 | null | 268 | 268 |
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
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
def make_divisors(n):
divisors = []
for i in range(1, int(n**0.5)+1):
if n % i == 0:
divisors.append(i)
if i != n // i:
divisors.append(n//i)
divisors.sort()
return divisors
#maincode-------------------------------------------------
k = ini()
a,b = inm()
ans = 'NG'
for i in range(1000):
if a <= i * k <= b:
ans = 'OK'
print(ans) | K = int(input())
A,B = map(int,input().split())
if (B//K *K)>=A:
print("OK")
else:
print("NG") | 1 | 26,479,504,037,162 | null | 158 | 158 |
num = int(input())
val = 100000
for i in range(num):
val = val * 1.05
rem = val%1000
if rem != 0:
val = val - rem + 1000
print(int(val))
| n=int(input())
s=100000
for i in range(n):
s*=1.05
p=s%1000
if p!=0:
s+=1000-p
print(int(s))
| 1 | 995,564,530 | null | 6 | 6 |
n=int(input())
P=list(map(int,input().split()))
m = n + 1
cnt = 0
for i in range(n):
if m >= P[i]:
m = P[i]
cnt += 1
print(cnt) | N = int(input())
P = list(map(int, input().split()))
c = 10 ** 20
con = 0
for i in range(N):
if c > P[i]:
con += 1
c = P[i]
print(con)
| 1 | 85,812,490,296,258 | null | 233 | 233 |
import sys
from collections import deque
data = list(sys.stdin.readline()[:-1])
ponds = deque()
result = deque()
index = 0
amount = 0
lefs = deque()
for i in data:
if i == "\\" or i == "_":
ponds.append([i, index])
else:
count = 0
while 1 <= len(ponds):
pond, ind = ponds.pop()
if pond == "\\":
count += (index - ind)
amount += count
pre_pond = 0
pre_left = 0
while 0 < len(lefs) and ind < lefs[-1]:
pre_left = lefs.pop()
pre_pond += result.pop()
lefs.append(ind)
result.append(count + pre_pond)
break
index += 1
print(amount)
if 0 == len(result):
print(0)
else:
print(len(result), ' '.join(map(str, result))) | # coding: utf-8
s = list(input().rstrip())
ht = []
ponds=[]
for i, ch in enumerate(s):
if ch == "\\":
ht.append(i)
elif ch == "/":
if ht:
b = ht.pop()
ap = i - b
if not ponds:
ponds.append([b, ap])
else:
while ponds:
p = ponds.pop()
if p[0] > b:
ap += p[1]
else:
ponds.append([p[0],p[1]])
break
ponds.append([b,ap])
else:
pass
ans = ""
area = 0
for point, ar in ponds:
area += ar
ans += " "
ans += str(ar)
print(area)
print(str(len(ponds)) + ans)
| 1 | 56,285,950,336 | null | 21 | 21 |
A, B, C, K = (int(x) for x in input().split())
AB = A + B
if K < A:
print(K)
elif AB < K:
print(A - (K-AB))
else:
print(A)
| a,b,c,k=map(int,input().split())
if a>=k:
print(k)
elif (k>a) and (a+b>=k):
print(a)
elif (a+b<k):
print(a-(k-(a+b)))
| 1 | 21,927,969,670,910 | null | 148 | 148 |
H = int(input())
W = int(input())
N = int(input())
ans = 0
num = 0
big = 0
if H < W:
big = W
else:
big = H
while num < N:
num += big
ans += 1
print(ans) | #!/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) | 0 | null | 91,908,719,257,632 | 236 | 241 |
N=int(input())
def make_divisors(n):
divisors = []
for i in range(1, int(n**0.5)+1):
if n % i == 0:
divisors.append(i)
if i != n // i:
divisors.append(n//i)
return(divisors)
l1=make_divisors(N)
ans=len(make_divisors(N-1))-1
for i in l1:
m=N
while m%i==0 and m!=1 and m!=0 and i!=1:
m//=i
if m%i==1:
ans+=1
print(ans) |
class Dice:
def __init__(self,a,b,c,d,e,f):
self.face1 = a
self.face2 = b
self.face3 = c
self.face4 = d
self.face5 = e
self.face6 = f
def above_face(self):
return self.face1
def roll(self, order):
if order == 'N':
tmp = self.face1
self.face1 = self.face2
self.face2 = self.face6
self.face6 = self.face5
self.face5 = tmp
elif order == 'S':
tmp = self.face1
self.face1 = self.face5
self.face5 = self.face6
self.face6 = self.face2
self.face2 = tmp
elif order == 'W':
tmp = self.face1
self.face1 = self.face3
self.face3 = self.face6
self.face6 = self.face4
self.face4 = tmp
else:
tmp = self.face1
self.face1 = self.face4
self.face4 = self.face6
self.face6 = self.face3
self.face3 = tmp
a,b,c,d,e,f = map(int,input().split())
dice1 = Dice(a,b,c,d,e,f)
l_order = list(input())
for i in range(len(l_order)):
dice1.roll(l_order[i])
print(dice1.above_face())
| 0 | null | 20,898,646,824,550 | 183 | 33 |
n = int(input())
a = [int(i) for i in input().split()]
counter = 0
for i in range(n):
minj = i
for j in range(i, n):
if a[j] < a[minj]:
minj = j
if minj != i:
a[i], a[minj] = a[minj], a[i]
counter += 1
print(*a)
print(counter)
| input()
print ' '.join(raw_input().split()[::-1]) | 0 | null | 494,516,343,178 | 15 | 53 |
h,n= map(int, input().split())
p = [list(map(int, input().split())) for _ in range(n)]
m = 10**4
dp = [0]*(h+m+1)
for i in range(m+1,h+m+1):
dp[i] = min(dp[i-a] + b for a,b in p)
print(dp[h+m]) | from operator import itemgetter
faces = list(map(int, input().split()))
commands = input()
for command in commands:
if command == 'N':
faces = itemgetter(1,5,2,3,0,4)(faces)
elif command == 'E':
faces = itemgetter(3,1,0,5,4,2)(faces)
elif command == 'S':
faces = itemgetter(4,0,2,3,5,1)(faces)
elif command == 'W':
faces = itemgetter(2,1,5,0,4,3)(faces)
print(faces[0])
| 0 | null | 40,485,411,668,832 | 229 | 33 |
S = input()
if S == 'AAA' or S== 'BBB':
print("No")
else:
print("Yes") | N = (list(input()))
N =N[::-1]
N_int = [int(i) for i in N]
N_int.append(0)
maisu = 0
keta = False
for i in range(len(N_int)-1):
if keta ==True:
N_int[i] +=1
if N_int[i]<5:
maisu +=N_int[i]
keta =False
elif N_int[i]==5 :
if N_int[i+1]>4:
keta =True
maisu +=(10-N_int[i])
else:
keta =False
maisu += N_int[i]
else:
keta =True
maisu +=(10-N_int[i])
if keta ==True:
maisu +=1
print(maisu) | 0 | null | 62,926,088,048,670 | 201 | 219 |
N = int(input())
get = set()
for _ in range(N):
s = str(input())
get.add(s)
print(len(get)) | n=int(input())
s=[]
for i in range(n):
s.append(str(input()))
s=set(s)
print(len(s)) | 1 | 30,432,372,565,560 | null | 165 | 165 |
a = int(input())
for i in range(1,a+1):
x = i
if i % 3 == 0 or i % 10 == 3:
print(" {}".format(i), end = "")
else:
while int(x) > 0:
x /= 10
if int(x) % 10 == 3:
print(" {}".format(i), end = "")
break
print()
| T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
P = (A1-B1)*T1
Q = (A2-B2)*T2
if(P > 0):
P *= -1
Q *= -1
if(P+Q<0):
print(0)
elif(P+Q==0):
print('infinity')
else:
if((-P)%(P+Q)==0):
print((-P)//(P+Q)*2)
else:
print((-P)//(P+Q)*2+1) | 0 | null | 66,611,876,906,180 | 52 | 269 |
n=int(input())
a,b,c,d=-10**9,10**9,-10**9,10**9
for _ in range(n):
x,y=map(int,input().split())
a=max(a,x+y)
b=min(b,x+y)
c=max(c,x-y)
d=min(d,x-y)
print(max(a-b,c-d)) | import fractions
import math
def divide_two(x):
ans = 0
while x > 0:
if x % 2 == 0:
ans += 1
x = x // 2
else:
break
return ans
n, m = map(int, input().split())
a = list(map(int, input().split()))
flag = [divide_two(i) for i in a]
if sum(flag) != flag[0] * n:
ans = 0
else:
# 最小公倍数求める
lcm = a[0]
for i in range(1, n):
lcm = lcm * a[i] // fractions.gcd(lcm, a[i])
ans = math.floor(m / lcm + 0.5)
print(ans) | 0 | null | 52,765,518,394,182 | 80 | 247 |
# ABC153
# B Common Raccoon VS Monster
h, n = map(int, input().split())
a = list(map(int, input().split()))
s = sum(a)
if h <= s:
print("Yes")
else:
print("No")
| X, Y, Z = map(int, input().split())
print("%d %d %d"%(Z, X, Y)) | 0 | null | 57,954,747,083,350 | 226 | 178 |
N, K = map(int, input().split())
p = sorted(list(map(int, input().split())))
print(sum(p[:K])) | tmp = input().split(" ")
N = int(tmp[0])
K = int(tmp[1])
ary = list(map(lambda x: int(x), input().split(" ")))
ary.sort()
print(sum(ary[0:K])) | 1 | 11,599,965,683,862 | null | 120 | 120 |
n = int(input())
memo = [-1]*(n+1)
memo[0] = 1
memo[1] = 1
def fib(n):
if memo[n] != -1:
return memo[n]
else:
ans = fib(n-1)+fib(n-2)
memo[n] = ans
return ans
print(fib(n))
| s,t=list(input().split())
a,b=list(map(int,input().split()))
u=input()
if u==s:
print(str(a-1)+" "+str(b))
else:
print(str(a)+" "+str(b-1)) | 0 | null | 36,157,237,976,860 | 7 | 220 |
from math import gcd
from math import factorial as f
from math import ceil, floor, sqrt
import math
import bisect
import re
import heapq
from copy import deepcopy
import itertools
from itertools import permutations
from sys import exit
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(map(int, input().split()))
yes = "Yes"
no = "No"
def main():
n, x, t = mi()
tmp = 0
ans = 0
for i in range(10000):
ans += t
tmp += x
if n <= tmp:
break
print(ans)
main()
| '''
Problem Link - https://atcoder.jp/contests/abc176/tasks/abc176_a
'''
from sys import stdin,stdout
from math import *
from collections import *
mod = 1000000007
def gcd(a,b):
while b:
a,b = b,a%b
return a
def lcm(a,b): return (a*b) // gcd(a,b)
def set_bits(X):
c = 0
while X:
X &= (X-1);c += 1
return c
def compute_MOD(N,M): return (N%M + M) % M
def get_array(): return list(map(int, stdin.readline().split()))
def get_ints(): return map(int, stdin.readline().split())
def get_int(): return int(stdin.readline())
def get_input(): return stdin.readline().strip()
def main():
N,X,T = get_ints()
stdout.write(str(ceil(N/X)*T)+'\n')
if __name__ == "__main__":
main() | 1 | 4,238,259,847,050 | null | 86 | 86 |
def count_rainy(weather):
if weather == 'RSR':
return 1
return len([r for r in weather if r == 'R'])
weather = input()
print(count_rainy(weather)) | s=input()
num=s.count("R")
if num==2:
if s=="RSR":
ans=1
else:
ans=2
else:
ans=num
print(ans) | 1 | 4,892,478,521,620 | null | 90 | 90 |
x = raw_input()
x = int(x)
print x * x * x | 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 # (string,3) 3回
from collections import deque
from collections import defaultdict
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
def readInts():
return list(map(int,input().split()))
def main():
n,k,s = readInts()
ans = []
for i in range(k):
ans.append(s)
for i in range(k,n):
if s >= 100:
ans.append(s-1)
else:
ans.append(s+1)
print(*ans)
if __name__ == '__main__':
main()
| 0 | null | 45,667,603,350,292 | 35 | 238 |
import math
from math import cos, sin
a,b,C=map(float,input().split())
C = math.radians(C)
S = 0.5*a*b*sin(C)
L = math.sqrt(b**2+a**2-2*a*b*cos(C))+a+b
h = 2*S / a
print(S)
print(L)
print(h) | import math
def toRad(theta):
return theta * math.pi / 180
def calc_S(_a, _b, _c):
return (_a * _b * math.sin(toRad(_c))) / 2
def calc_L(_a, _b, _c):
return _a + _b + math.sqrt(_a**2 + _b**2 - 2*a*b*math.cos(toRad(_c)))
def calc_H(_a, _b, _c):
return _b * math.sin(toRad(_c))
a, b, c = map(int, input().split())
print(calc_S(a, b, c))
print(calc_L(a, b, c))
print(calc_H(a, b, c)) | 1 | 176,961,350,800 | null | 30 | 30 |
n, k = map(int, input().split())
okashi = set()
for _ in range(k):
d = int(input())
lst = [int(i) for i in input().split()]
for i in range(d):
if lst[i] not in okashi:
okashi.add(lst[i])
count = 0
for i in range(n):
if i + 1 not in okashi:
count += 1
print(count) | N,K = map(int,input().split())
S = set()
for i in range(K) :
D=int(input())
L=list(map(int, input().split()))
for A in L :
S.add(A)
print (N-len(S))
| 1 | 24,485,078,316,930 | null | 154 | 154 |
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]))
| a,b=map(int,input().split())
c=list(map(int,input().split()))
c.sort()
# print(c)
sum=0
for i in range(b):
sum+=c[i]
print(sum) | 1 | 11,634,865,300,410 | null | 120 | 120 |
X,N = map(int, input().split())
P = list(map(int, input().split()))
table = [0] * 102
ans = X
dist = 102
for p in P:
table[p] = 1
for i in range(102):
if table[i] != 0:
continue
if abs(i - X) < dist:
ans = i
dist = abs(i - X)
elif abs(i - X) == dist:
ans = min(i, ans)
print(str(ans)) | from sys import stdin
input = stdin.readline
from time import time
from random import randint
from copy import deepcopy
start_time = time()
def calcScore(t, s, c):
scores = [0]*26
lasts = [0]*26
for i in range(1, len(t)):
scores[t[i]] += s[i][t[i]]
dif = i - lasts[t[i]]
scores[t[i]] -= c[t[i]] * dif * (dif-1) // 2
lasts[t[i]] = i
for i in range(26):
dif = len(t) - lasts[i]
scores[i] -= c[i] * dif * (dif-1) // 2
return scores
def greedy(c, s):
day_lim = len(s)
socres = [0]*26
t = [0]*day_lim
lasts = [0]*26
for i in range(1, day_lim):
pls = [v for v in socres]
mns = [v for v in socres]
for j in range(26):
pls[j] += s[i][j]
mns[j] -= c[j] * (i - lasts[j])
sum_mns = sum(mns)
pt = sum_mns - mns[0] + pls[0]
idx = 0
for j in range(1, 26):
tmp = sum_mns - mns[j] + pls[j]
if pt < tmp:
pt = tmp
idx = j
t[i] = idx
lasts[idx] = i
for j in range(26):
if j == idx:
socres[j] = pls[j]
else:
socres[j] = mns[j]
return socres, t
def subGreedy(c, s, t, day):
day_lim = len(s)
socres = [0]*26
t = [0]*day_lim
lasts = [0]*26
for i in range(1, day_lim):
if day <= i:
pls = [v for v in socres]
mns = [v for v in socres]
for j in range(26):
pls[j] += s[i][j]
mns[j] -= c[j] * (i - lasts[j])
sum_mns = sum(mns)
pt = sum_mns - mns[0] + pls[0]
idx = 0
for j in range(1, 26):
tmp = sum_mns - mns[j] + pls[j]
if pt < tmp:
pt = tmp
idx = j
t[i] = idx
lasts[idx] = i
for j in range(26):
if j == idx:
socres[j] = pls[j]
else:
socres[j] = mns[j]
else:
scores[t[i]] += s[i][t[i]]
lasts[t[i]] = i
for j in range(26):
dif = i - lasts[j]
scores[j] -= c[j] * dif
return socres, t
D = int(input())
c = list(map(int, input().split()))
s = [[0]*26 for _ in range(D+1)]
for i in range(1, D+1):
s[i] = list(map(int, input().split()))
scores, t = greedy(c, s)
sum_score = sum(scores)
tm = time() - start_time
while tm < 1.86:
typ = randint(1, 100)
if typ <= 40:
for _ in range(500):
tmp_t = deepcopy(t)
tmp_t[randint(1, D)] = randint(0, 25)
tmp_scores = calcScore(tmp_t, s, c)
sum_tmp_score = sum(tmp_scores)
if sum_score < sum_tmp_score or (tm < 1.0 and randint(1, 1000) <= 1):
sum_score = sum_tmp_score
t = deepcopy(tmp_t)
scores = deepcopy(tmp_scores)
elif typ <= 99:
for _ in range(100):
tmp_t = deepcopy(t)
dist = randint(1, 15)
p = randint(1, D-dist)
q = p + dist
tmp_t[p], tmp_t[q] = tmp_t[q], tmp_t[p]
tmp_scores = calcScore(tmp_t, s, c)
sum_tmp_score = sum(tmp_scores)
if sum_score < sum_tmp_score or (tm < 1.0 and randint(1, 200) <= 1):
sum_score = sum_tmp_score
t = deepcopy(tmp_t)
scores = deepcopy(tmp_scores)
elif typ <= 100:
tmp_t = deepcopy(t)
day = randint(D//4*3, D)
tmp_scores, tmp_t = subGreedy(c, s, tmp_t, day)
sum_tmp_score = sum(tmp_scores)
if sum_score < sum_tmp_score:
sum_score = sum_tmp_score
t = deepcopy(tmp_t)
scores = deepcopy(tmp_scores)
tm = time() - start_time
for v in t[1:]:
print(v+1)
| 0 | null | 11,914,991,652,476 | 128 | 113 |
def resolve():
S = input()
print(S[:3])
resolve()
| def divisor(N:int):
res = set()
i = 1
while i * i <= N:
if N % i == 0:
res.add(i)
if i != N // i:
res.add(N // i)
i += 1
return res
def check(N:int, K:int):
while N % K == 0:
N //= K
return True if N % K == 1 else False
def main():
N = int(input())
ans = set()
for K in divisor(N):
if K == 1: continue
if check(N, K):
ans.add(K)
ans |= divisor(N - 1)
ans.remove(1)
print(len(ans))
if __name__ == "__main__":
main() | 0 | null | 28,028,880,744,898 | 130 | 183 |
import sys
input = sys.stdin.readline
def print_ans(X):
"""Test Case
>>> print_ans(30)
Yes
>>> print_ans(25)
No
>>> print_ans(35)
Yes
>>> print_ans(-10)
No
"""
if X >= 30:
print('Yes')
else:
print('No')
if __name__ == '__main__':
X = int(input().rstrip())
print_ans(X)
| a = int(input())
print('YNeos'[a<30::2])
| 1 | 5,684,389,031,330 | null | 95 | 95 |
X = int(input())
a = X**2
print(a) | from sys import stdin
a = stdin.readline().rstrip()
a = int(a)
print(a*a) | 1 | 145,739,286,083,644 | null | 278 | 278 |
N_dict = {i:0 for i in list(map(str,input().split()))}
N_List = list(map(int,input().split()))
ct = 0
for i in N_dict.keys():
N_dict[i] = N_List[ct]
ct += 1
N_dict[str(input())] -= 1
print(" ".join(list(map(str,N_dict.values()))))
| s, t = input().split()
a, b = map(int, input().split())
u = input()
if s == u:
a -= 1
else:
b -= 1
print('{} {}'.format(a, b)) | 1 | 71,835,104,296,800 | null | 220 | 220 |
def main():
a,b,c = map(str,input().split())
print(c + ' ' + a + ' ' + b)
main() | import sys
from collections import deque
def input():
return sys.stdin.readline().strip()
def main():
H,W = map(int,input().split())
S = [list(input()) for _ in range(H)]
dp = [[float('inf') for _ in range(W)] for _ in range(H)]
def bfs(x,y):
que = deque()
que.append((x,y))
if S[y][x] == '.':
dp[y][x] = 0
else:
dp[y][x] = 1
while que.__len__():
x,y = que.popleft()
for dx,dy in ((1,0),(0,1)):
sx = x + dx
sy = y + dy
if sx == W or sy == H:
continue
if S[y][x] == '.' and S[sy][sx] == '#':
tmp = 1
else:
tmp = 0
dp[sy][sx] = min(dp[y][x]+tmp,dp[sy][sx])
if (sx,sy) not in que:
que.append((sx,sy))
bfs(0,0)
print(dp[H-1][W-1])
if __name__ == "__main__":
main() | 0 | null | 43,571,871,759,548 | 178 | 194 |
import collections
H, W, K = [int(x) for x in input().split()]
S = [input().strip() for _ in range(H)]
ans = float("inf")
for i in range(2 ** (H - 1)):
bundan = {}
bundan[0] = 0
prev = 0
tmp = 0
for j in range(H - 1):
if i >> j & 1 == 1:
prev += 1
bundan[j + 1] = prev
tmp += 1
else:
bundan[j + 1] = prev
c = collections.Counter()
for kk in range(W):
f = False
for k in range(H):
if f:
break
if S[k][kk] == '1':
c[bundan[k]] += 1
if c[bundan[k]] > K:
tmp += 1
f = True
c = collections.Counter()
for kkk in range(H):
if S[kkk][kk] == '1':
c[bundan[kkk]] += 1
break
for k in c.keys():
if c[k] > K:
tmp = float("inf")
break
ans = min(ans, tmp)
print(ans)
| import sys
input = sys.stdin.readline
h, w, kk = map(int, input().split())
ss = [[0 for _ in range(h)] for _ in range(w)]
for i in range(h):
for j, c in enumerate(list(input().strip())):
if c == '1':
ss[j][i] = 1
ans = 100000
for i in range(2 ** (h - 1)):
end = []
for j in range(h - 1):
if i & 2 ** j:
end.append(j + 1)
end.append(h)
start = [0]
for j in end:
start.append(j)
start.pop()
sums = [0] * len(start)
tans = len(start) - 1
imp = False
for j in range(w):
reset = False
for k, (s, e) in enumerate(zip(start, end)):
sums[k] += sum(ss[j][s:e])
if sums[k] > kk:
reset = True
break
if reset:
tans += 1
for k, (s, e) in enumerate(zip(start, end)):
sums[k] = sum(ss[j][s:e])
if sums[k] > kk:
imp = True
if imp:
break
if not imp:
ans = min(ans, tans)
print(ans)
| 1 | 48,526,551,861,600 | null | 193 | 193 |
d = int(input())
cList = input().split()
sList = []
for idx in range(0, d):
tempList = input().split()
sList.append(tempList)
tList = []
for idx in range(0, d):
tList.append(int(input()))
countList = [0] * 26
val = 0
for idx in range(0, d):
val += int(sList[idx][tList[idx] -1])
countList = list(map(lambda x: x+1, countList))
countList[int(tList[idx]) -1] = 0
for wkChar in range(0,26):
val -= int(countList[wkChar]) * int(cList[wkChar])
print(val)
| list1=[]
for i in range(10):
list1.append(int(input()))
list1=sorted(list1)
list1.reverse()
for i in range(3):
print(list1[i]) | 0 | null | 4,917,361,446,180 | 114 | 2 |
import sys
def main():
N=int(sys.stdin.readline())
A=tuple(map(int,sys.stdin.readline().split()))
R=[0 for _ in range(N)]
for a in A:R[a-1]+=1
for r in R:print(r)
if __name__=='__main__':main() | N = int(input())
As = list(map(int, input().split()))
dp = [[0] * (N-k+1) for k in range(N+1)]
s = 0
for i, A in sorted(enumerate(As), key = lambda t:t[1], reverse=True):
s += 1
for x in range(s+1):
y = s - x
if x > 0 and y > 0:
dp[x][y] = max(dp[x-1][y] + A * (i - x + 1), dp[x][y-1] + A * ((N-y) - i))
elif x == 0:
dp[x][y] = dp[x][y-1] + A * ((N-y) - i)
else:
dp[x][y] = dp[x-1][y] + A * (i - x + 1)
ans = max((dp[x][N-x] for x in range(N+1)))
print(ans) | 0 | null | 33,337,342,592,960 | 169 | 171 |
def main():
import sys
from collections import deque
from operator import itemgetter
M=10**10
b=sys.stdin.buffer
n,d,a=map(int,b.readline().split())
m=map(int,b.read().split())
q=deque()
popleft,append=q.popleft,q.append
s=b=0
for x,h in sorted(zip(m,m),key=itemgetter(0)):
while q and q[0]//M<x:b-=popleft()%M
if h>b:
t=(b-h)//a
s-=t
t*=a
b-=t
append((x+d+d)*M-t)
print(s)
main() | from math import ceil
def binary(N,LIST,num): # 二分探索 # N:探索要素数
l, r = -1, N
while r - l > 1:
if LIST[(l + r) // 2] > num: # 条件式を代入
r = (l + r) // 2
else:
l = (l + r) // 2
return r + 1
n, d, a = map(int, input().split())
xh = sorted(list(map(int, input().split())) for _ in range(n))
x = [i for i, j in xh]
h = [j for i, j in xh]
bomb, bsum, ans = [0] * (n + 1), [0] * (n + 1), 0
for i, xi in enumerate(x):
j = binary(n, x, xi + 2 * d) - 1
bsum[i] += bsum[i - 1] + bomb[i]
bnum = max(ceil(h[i] / a - bsum[i]), 0)
bomb[i] += bnum
bomb[j] -= bnum
bsum[i] += bnum
ans += bnum
print(ans)
| 1 | 82,309,556,394,570 | null | 230 | 230 |
import math
a,b,x = map(int,input().split())
if x <= b*a*a*1/2:
dum = 2*x/(a*b)
ans = 90-(math.degrees(math.atan(dum/b)))
else:
x -= b*a*a*1/2
dum = b-(x*2/(a*a))
ans = math.degrees(math.atan(dum / a))
print(ans) | N, K = input().split()
N = int(N)
K = int(K)
scores = [int(s) for s in input().split()]
sums = []
k = K-1
for i in range(k,N-1):
if(scores[i-k] < scores[i+1]):
print("Yes")
else:
print("No") | 0 | null | 85,303,453,652,512 | 289 | 102 |
import sys
input = sys.stdin.readline
def main():
S = input().rstrip()
N = len(S)
if N % 2 == 0:
is_h = all(s == "h" for s in S[0::2])
is_i = all(s == "i" for s in S[1::2])
ans = "Yes" if is_h and is_i else "No"
else:
ans = "No"
print(ans)
if __name__ == "__main__":
main()
| H = int(input())
count = 0
ans = 0
while H>1:
H = H//2
ans += 2**count
count += 1
ans += 2**count
print(ans) | 0 | null | 66,390,819,948,232 | 199 | 228 |
n = int(input())
print(n*6.28318) | h,w,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
H=[0]*h
W=[0]*w
for i in range(m):
H[l[i][0]-1]+=1
W[l[i][1]-1]+=1
hmax=max(H)
wmax=max(W)
cthmax=0
ctwmax=0
for i in range(h):
if H[i]==hmax:
cthmax+=1
for i in range(w):
if W[i]==wmax:
ctwmax+=1
ct=0
for i in range(m):
if H[l[i][0]-1]==hmax and W[l[i][1]-1]==wmax:
ct+=1
if cthmax*ctwmax>ct:
print(hmax+wmax)
else:
print(hmax+wmax-1) | 0 | null | 17,967,650,479,978 | 167 | 89 |
n, x, m = map(int, input().split())
lis = set()
loop_lis = []
cnt = 0
while x not in lis:
cnt += 1
lis.add(x)
loop_lis.append(x)
x = pow(x, 2) % m
if cnt == n:
print(sum(lis))
exit()
start = loop_lis.index(x)
length = len(loop_lis) - start
loop_sum = sum(loop_lis[start:])
before = sum(loop_lis[:start])
loop = loop_sum * ((n - start) // length)
remain = sum(loop_lis[start : (start + (n - start) % length)])
print(before + loop + remain)
| import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()
def resolve():
N, X, M = LI()
A = []
seen = set()
tmp = X
head_idx = -1
while True:
A.append(tmp)
seen.add(tmp)
tmp = pow(tmp, 2, M)
if tmp in seen:
head_idx = A.index(tmp)
break
# print(A, head_idx)
cnt = [0] * len(A)
for i in range(len(A)):
if i < head_idx:
if i <= N:
cnt[i] = 1
else:
l = len(A) - head_idx
cnt[i] = (N - head_idx) // l
for i in range(head_idx, head_idx + (N - head_idx) % l):
cnt[i] += 1
# print(cnt)
ans = sum([a * c for a, c in zip(A, cnt)])
print(ans)
if __name__ == '__main__':
resolve()
| 1 | 2,799,409,814,180 | null | 75 | 75 |
#E
N,T=map(int,input().split())
A=[0 for i in range(N+1)]
B=[0 for i in range(N+1)]
for i in range(1,N+1):
A[i],B[i]=map(int,input().split())
dp1=[[0 for i in range(6001)] for j in range(N+1)]
dp2=[[0 for i in range(6001)] for j in range(N+2)]
for i in range(1,N+1):
for j in range(T+1):
if j>=A[i]:
dp1[i][j]=max(dp1[i-1][j],dp1[i-1][j-A[i]]+B[i])
else:
dp1[i][j]=dp1[i-1][j]
for i in range(N,0,-1):
for j in range(T+1):
if j>=A[i]:
dp2[i][j]=max(dp2[i+1][j],dp2[i+1][j-A[i]]+B[i])
else:
dp2[i][j]=dp2[i+1][j]
ans=0
for i in range(1,N+1):
for j in range(T):
ans=max(ans,dp1[i-1][j]+dp2[i+1][T-1-j]+B[i])
print(ans) | count = int(input())
for i in range(count):
a = map(int,raw_input().split())
a.sort()
if a[0]**2+a[1]**2 == a[2]**2: print "YES"
else: print "NO" | 0 | null | 75,472,650,330,840 | 282 | 4 |
#!/usr/bin/env python3
import sys
# from scipy.special import comb
sys.setrecursionlimit(10**4)
from math import factorial
def comb(n, r):
'''
>>> comb(2, 1)
2
>>> comb(3, 2)
3
'''
if r == 0:
return 1
if n == 0 or r > n:
return 0
return factorial(n) // factorial(n - r) // factorial(r)
def f(S, i, k):
# ここに来るのは繰り下がりが発生していない状態...
if k == 0:
# 以降は0を選び続けるしかない
return 1
if len(S) <= i:
# 走破したのにK個使いきれていない
return 0
n = int(S[i])
if n == 0:
# 注目する桁が0の場合、0を選ぶしかない
return f(S, i+1, k)
# 注目する桁の数値nより小さい値を選べば、以降は繰り下げで選び放題
ret = (n - 1) * comb(len(S)-i-1, k-1) * pow(9, k-1)
ret += comb(len(S)-i-1, k) * pow(9, k)
# 注目する桁の数値nを選んだ場合、繰り下げができない状態が続行
ret += f(S, i+1, k-1)
return ret
def solve(S: str, K: int):
return f(S, 0, K)
# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = next(tokens) # type: str
K = int(next(tokens)) # type: int
print(solve(N, K))
def test():
import doctest
doctest.testmod()
if __name__ == '__main__':
test()
main()
| #!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000
d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))
A, B = LI()
print(A * B) | 0 | null | 45,671,500,962,570 | 224 | 133 |
n,k=map(int,input().split())
lst_1=list(map(int,input().split()))
# print(lst_1)
for i in range(n-k):
if(lst_1[i+k]>lst_1[i]):
print("Yes")
else:print("No") | def main():
N, K = map(int, input().split())
*A, = map(int, input().split())
ans = []
for i in range(K, N):
cond = A[i] > A[i - K]
ans.append('Yes' if cond else 'No')
print(*ans, sep='\n')
if __name__ == '__main__':
main()
| 1 | 7,177,943,263,998 | null | 102 | 102 |
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF = float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353
N = int(input())
if N % 2 == 1:
print(0)
else:
i = 1
ans = 0
while N > 0:
N = N // 5
ans += N // 2
print(ans) | def showList(A, N):
for i in range(N-1):
print(A[i],end=' ')
print(A[N-1])
def selectionSort(A, N):
"""
選択ソート
O(n^2)のアルゴリズム
"""
count = 0
for i in range(N):
minj = i
for j in range(i,N):
if A[j] < A[minj]:
minj = j
if minj != i:
t = A[minj]
A[minj] = A[i]
A[i] = t
count += 1
showList(A, N)
print(count)
N = int(input())
A = [int(x) for x in input().split(' ')]
selectionSort(A, N)
| 0 | null | 58,102,269,291,948 | 258 | 15 |
import numpy as np
N,K=map(int,input().split())
a = np.array([int(x) for x in input().split()])
val=sum(a[:K])
max_val=val
for i in range(N-K):
val+=a[K+i]
val-=a[i]
max_val=max(val,max_val)
print(max_val/2+K*0.5) | n, k = [int(i) for i in input().split()]
cnt = 0
if n == 0:
print(1)
exit()
while n != 0:
n = n // k
cnt += 1
print(cnt) | 0 | null | 69,715,496,931,190 | 223 | 212 |
import bisect
n = int(input())
lines = list(int(i) for i in input().split())
lines.sort()
counter = 0
for i in range(n-2):
for j in range(i+1, n-1):
counter += bisect.bisect_left(lines, lines[i] + lines[j]) - (j + 1)
print(counter) | def solve():
A, B, C, K = map(int, input().split())
ans = min(A,K)-max(0,K-A-B)
return ans
print(solve()) | 0 | null | 96,408,487,145,022 | 294 | 148 |
n = int(input())
a = input()
s=list(map(int,a.split()))
q = int(input())
a = input()
t=list(map(int,a.split()))
i = 0
for it in t:
if it in s:
i = i+1
print(i) | N,M,X=[int(s) for s in input().split()]
Book=[[int(s) for s in input().split()] for _ in range(N)]
INF=10**7
ans=set()
ans.add(INF)
#深さ優先探索
def DFS(n,cost,Xls):
if n==N:
if min(Xls)>=X:
ans.add(cost)
else:
Xnext=[Xls[i]+Book[n][i+1] for i in range(M)]
DFS(n+1,cost+Book[n][0],Xnext)
DFS(n+1,cost,Xls)
DFS(0,0,[0 for _ in range(M)])
if min(ans)==INF:
print(-1)
else:
print(min(ans)) | 0 | null | 11,165,898,685,020 | 22 | 149 |
n,*a=map(int,open(0).read().split())
mod=10**9+7
ans=0
for j in range(60):
cnt=0
for i in range(n):
cnt+=a[i]&1
a[i]=(a[i]>>1)
ans+=(cnt*(n-cnt)%mod)*pow(2,j,mod)
ans%=mod
print(ans) | input = list(map(int, input().split()))
if input[0] > input[1]:
print("safe")
else:
print("unsafe") | 0 | null | 75,767,442,246,758 | 263 | 163 |
#
# ⋀_⋀
# (・ω・)
# ./ U ∽ U\
# │* 合 *│
# │* 格 *│
# │* 祈 *│
# │* 願 *│
# │* *│
#  ̄
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないyp
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A):
for a in A: print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
# ワーシャルフロイド
def warshall_floyd(d, N):
for k in range(N):
for i in range(N):
for j in range(N):
d[i][j]=min(d[i][j], d[i][k] + d[k][j])
N,M,L=MI()
ABC=LLIN(M)
d=[[float("inf")] * N for i in range(N)]
for i in range(N):
d[i][i] = 0
for a,b,c in ABC:
a-=1
b-=1
d[a][b]=c
d[b][a]=c
warshall_floyd(d,N)
D=[[float("inf")] * N for i in range(N)]
for i in range(N):
for j in range(N):
if d[i][j]<=L:
D[i][j] = 1
warshall_floyd(D,N)
Q=I()
ST=LLIN_(Q)
for s,t in ST:
if D[s][t]==inf:
print(-1)
else:
print(D[s][t]-1)
if __name__ == '__main__':
main() | import sys
import numpy as np
from scipy.sparse.csgraph import floyd_warshall
input = sys.stdin.buffer.readline
INF = 10**12
N, M, L = map(int, input().split())
G = np.zeros((N, N), dtype=np.int64)
for _ in range(M):
a, b, c = map(int, input().split())
a -= 1
b -= 1
G[a][b] = c
G[b][a] = c
Q = int(input())
ST = [list(map(int, input().split())) for _ in range(Q)]
fw = floyd_warshall(G)
path = np.full((N, N), INF, dtype=np.int64)
path[fw <= L] = 1
path_fw = (floyd_warshall(path) + 0.5).astype(np.int64)
ans = []
for s, t in ST:
s -= 1
t -= 1
if path_fw[s][t] >= INF:
ans.append(-1)
continue
ans.append(path_fw[s][t]-1)
print(*ans, sep="\n")
| 1 | 173,929,614,742,508 | null | 295 | 295 |
n = int(input())
ans = 0
for a in range(1, n+1):
cnt = (n-1)//a
ans += cnt
print(ans) | import sys
def main():
N = int(input())
P = list(map(int, input().split()))
min_so_far = sys.maxsize
ans = 0
for p in P:
if p <= min_so_far:
ans += 1
min_so_far = p
print(ans)
if __name__ == '__main__':
main()
| 0 | null | 44,137,097,940,508 | 73 | 233 |
N=int(input())
A=list(map(int,input().split()))
if A[0]:
if N==0 and A[0]==1:
print(1)
else:
print(-1)
exit()
B=[0 for _ in range(N)]
B.append(A[-1])
for i in range(N)[::-1]:
B[i]=B[i+1]+A[i]
r=1
ans=1
for i in range(1,N+1):
t=min(r*2,B[i])
ans+=t
r=t-A[i]
if r<0:
print(-1)
exit()
print(ans) | x = int(input())
a = 360
for _ in range(360):
if a%x == 0:
print(a//x)
break
else:
a += 360 | 0 | null | 16,016,938,169,258 | 141 | 125 |
a,b=1,0
for c in map(int,input()):a,b=min(a+10-c-1,b+c+1),min(a+10-c,b+c)
print(b) | import sys
input = sys.stdin.readline
def solve(N):
res = 0
flg = 0
for n in reversed(N):
if flg == 2:
if n >= 5:
flg = 1
else:
flg = 0
m = flg + n
if m == 5:
res += 5
flg = 2
elif m < 5:
res += m
flg = 0
else:
res += (10 - m)
flg = 1
return res + flg%2
N = list(map(int, input().strip()))
print(solve(N))
# N = 1
# q = 0
# while True:
# p = solve(list(map(int, str(N))))
# if abs(p-q) > 1:
# print(N, "OK")
# exit()
# q = p
# N += 1 | 1 | 70,668,903,450,392 | null | 219 | 219 |
S = input()
n = len(S) + 1
a = [0] * n
S = ('<' if S[0] == '>' else '>') + S \
+ ('<' if S[-1] == '>' else '>') # 最初と最後にダミー追加、長さn+1
for i in range(n):
if S[i : i + 2] == '><': # 極小値
for j in range(i, 0, -1): # 左に辿る
if S[j] == '<': break
a[j - 1] = max(a[j - 1], a[j] + 1)
for j in range(i + 1, n): # 右に辿る
if S[j] == '>': break
a[j] = max(a[j], a[j - 1] + 1)
print(sum(a)) | import math
from functools import reduce
K = int(input())
def gcd(*numbers):
return reduce(math.gcd, numbers)
ans = 0
for a in range(1,K+1):
if (a==1):
ans += K*K
continue
for b in range(1,K+1):
t = gcd(a,b)
if(t==1):
ans += K
continue
for c in range(1,K+1):
ans += gcd(t,c)
print(ans) | 0 | null | 96,273,451,469,468 | 285 | 174 |
s = list(input())
n = len(s) + 1
left = [0]*n
right = [0]*n
for i in range(n-1):
if s[i] == '<':
left[i+1] = left[i] + 1
for i in range(n-2, -1, -1):
if s[i] == '>':
right[i] = right[i+1] + 1
a = [0]*n
for i in range(n):
a[i] = max(left[i], right[i])
ans = sum(a)
print(ans) | S = input()
print(S.replace('?', 'D'))
| 0 | null | 87,474,286,970,070 | 285 | 140 |
x = int(input())
r = 100
ans = 0
while r < x:
r = int(r * 101)//100
ans += 1
print(ans)
| import math
x=int(input());money=100;year=0
while money < x:
money+=money//100
year+=1
print(year) | 1 | 26,901,417,066,172 | null | 159 | 159 |
import sys
x = int(raw_input())
print(x*x*x) | def main():
N, K = map(int, input().split())
mod = 10**9 + 7
r = 0
D = [0] * (K + 1)
for i in reversed(range(1, K + 1)):
D[i] = pow(K // i, N, mod) - sum(D[::i])
return sum(i * j for i, j in enumerate(D)) % mod
print(main())
| 0 | null | 18,525,564,463,552 | 35 | 176 |
X,N=map(int,input().split())
p=list(map(int,input().split()))
upper=float("inf")
lower=float("inf")
a=X
b=X
while True:
if not a in p:
upper=a
break
a+=1
while True:
if not b in p:
lower=b
break
b-=1
if abs(upper-X)==abs(lower-X):
print(lower)
elif abs(upper-X)>abs(lower-X):
print(lower)
else:
print(upper) | def f_knapsack_for_all_subsets_power_series(MOD=998244353, MAX=3010):
# 参考: https://maspypy.com/atcoder-参加感想-2020-05-31abc-169
import numpy as np
N, S = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
f = np.zeros(MAX + 1, np.int64) # 次数が S より大きな項には興味ないため、こうする
f[0] = 1 # f_{0}(x) = 1
for a in A:
# f_{n+1} = f_{n} * (2 + x**a) = 2 * f_{n} + x**a * f_{n}
f_next = 2 * f # 第一項
f_next[a:] += f[:-a] # 第二項
f_next %= MOD
f = f_next
return f[S]
print(f_knapsack_for_all_subsets_power_series()) | 0 | null | 15,806,742,078,652 | 128 | 138 |
ri = raw_input().split()
stack = []
for i in xrange(len(ri)):
if ri[i] == "+":
b = stack.pop()
a = stack.pop()
stack.append(str(int(a)+int(b)))
elif ri[i] == "-":
b = stack.pop()
a = stack.pop()
stack.append(str(int(a)-int(b)))
elif ri[i] == "*":
b = stack.pop()
a = stack.pop()
stack.append(str(int(a)*int(b)))
else:
stack.append(ri[i])
print stack.pop() | #親要素を示す
par = {}
sum_union = {}
len_union = {}
# 初期化 -- すべての要素が根である --
def init(n):
for i in range(n):
par[i] = i
sum_union[i] = Cs[i]
len_union[i] = 1
return
# 根を求める
def root(x):
if par[x] == x:
return x
else:
#根の直下にxを置く
par[x] = root(par[x])
return par[x]
# 同じ集合に属するかどうかは根が同じかで判定
def same(x, y):
return root(x) == root(y)
# 別々のUnion同士を融合する
def unite(x, y):
rx = root(x)
ry = root(y)
if rx == ry:
return
else:
par[rx] = ry
tmp_sum = sum_union[rx] + sum_union[ry]
tmp_len = len_union[rx] + len_union[ry]
sum_union[ry] = tmp_sum
len_union[ry] = tmp_len
return
N, K = map(int, input().split())
Ps = list(map(lambda x: int(x) - 1, input().split()))
Cs = list(map(int, input().split()))
init(N)
# 結合
for i in range(N):
unite(i, Ps[i])
max_result = -(10**9+1)
for i in range(N):
ri = root(i)
if sum_union[ri] <= 0:
partial_range = min(K, len_union[ri])
roop_sum = 0
elif K < len_union[ri]:
partial_range = K
roop_sum = 0
else:
partial_range = (K % len_union[ri]) + len_union[ri]
roop_sum = sum_union[ri] * ((K // len_union[ri])-1)
pc = i
sum_list = []
sum_c = 0
for _ in range(partial_range):
pn = Ps[pc]
sum_c += Cs[pn]
sum_list.append(sum_c)
pc = pn
max_result = max(max_result, roop_sum + max(sum_list))
print(max_result)
| 0 | null | 2,681,800,748,390 | 18 | 93 |
from collections import Counter
s=input()[::-1]
DP=[0]
num,point=0,1
for i in s:
num +=int(i)*point
num %=2019
DP.append(num)
point *=10
point %=2019
ans=0
DP=Counter(DP)
for v,m in DP.items():
if m>=2:
ans +=m*(m-1)//2
print(ans) | # https://atcoder.jp/contests/abc164/tasks/abc164_d
import sys
input = sys.stdin.readline
S = input().rstrip()
res = 0
x = 0
p = 1
L = len(S)
MOD = 2019
reminder = [0]*2019
reminder[0] = 1
for s in reversed(S):
""" 累積和 """
x = (int(s)*p + x)%MOD
p = p*10%MOD
reminder[x] += 1
for c in reminder:
res += c*(c-1)//2
print(res) | 1 | 30,785,939,695,420 | null | 166 | 166 |
from itertools import product
H, W, K = map(int, input().split())
G = [list(map(int, list(input()))) for _ in range(H)]
ans = float("inf")
for pattern in product([0, 1], repeat = H - 1):
div = [0] + [i for i, p in enumerate(pattern, 1) if p == 1] + [H]
rows = []
for i in range(len(div) - 1):
row = []
for j in range(W):
tmp = 0
for k in range(div[i], div[i + 1]):
tmp += G[k][j]
row.append(tmp)
rows.append(row)
flag = True
for row in rows:
if [r for r in row if r > K]:
flag = False
break
if not flag:
continue
tmp_ans = 0
cnt = [0] * len(rows)
w = 0
while w < W:
for r in range(len(rows)):
cnt[r] += rows[r][w]
if [c for c in cnt if c > K]:
cnt = [0] * len(rows)
tmp_ans += 1
w -= 1
w += 1
tmp_ans += pattern.count(1)
ans = min(ans, tmp_ans)
print(ans) | S = list(input())
K = int(input())
S.extend(S)
prev = ''
cnt = 0
for s in S:
if s == prev:
cnt += 1
prev = ''
else:
prev = s
b = 0
if K % 2 == 0:
b += 1
if K > 2 and S[0] == S[-1]:
mae = 1
while mae < len(S) and S[mae] == S[0]:
mae += 1
if mae % 2 == 1 and mae != len(S):
usiro = 1
i = len(S) - 2
while S[i] == S[-1]:
usiro += 1
i -= 1
if usiro % 2 == 1:
b = K // 2
if K % 2 == 0:
res = cnt * (K // 2) + (b - 1)
else:
res = cnt * (K // 2) + cnt // 2 + b
print(res)
| 0 | null | 112,345,251,832,460 | 193 | 296 |
import sys
n,m = map(int,input().split())
#input = sys.stdin.readline
sys.setrecursionlimit(10**9)
list1 = []
s = list(input())
s.reverse()
def maze(mass): #今いるマス、現状の手数
if mass+m >= n:
list1.append(n-mass)
return
for i in range(m,0,-1):
if s[mass+i] == "0":
list1.append(i)
maze(mass+i)
return
elif i == 1:
print(-1)
exit()
maze(0)
list1.reverse()
print(" ".join(map(str,list1))) | n = int(input())
S = input().split()
q = int(input())
T = input().split()
count = 0
for i in T:
if i in S:
count += 1
print(count) | 0 | null | 69,870,802,087,872 | 274 | 22 |
from sys import stdin
input = stdin.readline
from time import time
from random import randint
from copy import deepcopy
start_time = time()
def calcScore(t, s, c):
scores = [0]*26
lasts = [0]*26
for i in range(1, len(t)):
scores[t[i]] += s[i][t[i]]
dif = i - lasts[t[i]]
scores[t[i]] -= c[t[i]] * dif * (dif-1) // 2
lasts[t[i]] = i
for i in range(26):
dif = len(t) - lasts[i]
scores[i] -= c[i] * dif * (dif-1) // 2
return scores
def greedy(c, s):
day_lim = len(s)
socres = [0]*26
t = [0]*day_lim
lasts = [0]*26
for i in range(1, day_lim):
pls = [v for v in socres]
mns = [v for v in socres]
for j in range(26):
pls[j] += s[i][j]
mns[j] -= c[j] * (i - lasts[j])
sum_mns = sum(mns)
pt = sum_mns - mns[0] + pls[0]
idx = 0
for j in range(1, 26):
tmp = sum_mns - mns[j] + pls[j]
if pt < tmp:
pt = tmp
idx = j
t[i] = idx
lasts[idx] = i
for j in range(26):
if j == idx:
socres[j] = pls[j]
else:
socres[j] = mns[j]
return socres, t
def subGreedy(c, s, t, day):
day_lim = len(s)
socres = [0]*26
t = [0]*day_lim
lasts = [0]*26
for i in range(1, day_lim):
if day <= i:
pls = [v for v in socres]
mns = [v for v in socres]
for j in range(26):
pls[j] += s[i][j]
mns[j] -= c[j] * (i - lasts[j])
sum_mns = sum(mns)
pt = sum_mns - mns[0] + pls[0]
idx = 0
for j in range(1, 26):
tmp = sum_mns - mns[j] + pls[j]
if pt < tmp:
pt = tmp
idx = j
t[i] = idx
lasts[idx] = i
for j in range(26):
if j == idx:
socres[j] = pls[j]
else:
socres[j] = mns[j]
else:
scores[t[i]] += s[i][t[i]]
lasts[t[i]] = i
for j in range(26):
dif = i - lasts[j]
scores[j] -= c[j] * dif
return socres, t
D = int(input())
c = list(map(int, input().split()))
s = [[0]*26 for _ in range(D+1)]
for i in range(1, D+1):
s[i] = list(map(int, input().split()))
scores, t = greedy(c, s)
sum_score = sum(scores)
tm = time() - start_time
while tm < 1.86:
typ = randint(1, 100)
if typ <= 40:
for _ in range(500):
tmp_t = deepcopy(t)
tmp_t[randint(1, D)] = randint(0, 25)
tmp_scores = calcScore(tmp_t, s, c)
sum_tmp_score = sum(tmp_scores)
if sum_score < sum_tmp_score or (tm < 1.0 and randint(1, 1000) <= 1):
sum_score = sum_tmp_score
t = deepcopy(tmp_t)
scores = deepcopy(tmp_scores)
elif typ <= 99:
for _ in range(100):
tmp_t = deepcopy(t)
dist = randint(1, 15)
p = randint(1, D-dist)
q = p + dist
tmp_t[p], tmp_t[q] = tmp_t[q], tmp_t[p]
tmp_scores = calcScore(tmp_t, s, c)
sum_tmp_score = sum(tmp_scores)
if sum_score < sum_tmp_score or (tm < 1.0 and randint(1, 200) <= 1):
sum_score = sum_tmp_score
t = deepcopy(tmp_t)
scores = deepcopy(tmp_scores)
elif typ <= 100:
tmp_t = deepcopy(t)
day = randint(D//4*3, D)
tmp_scores, tmp_t = subGreedy(c, s, tmp_t, day)
sum_tmp_score = sum(tmp_scores)
if sum_score < sum_tmp_score:
sum_score = sum_tmp_score
t = deepcopy(tmp_t)
scores = deepcopy(tmp_scores)
tm = time() - start_time
for v in t[1:]:
print(v+1)
| #!/usr/bin python3
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline
def main():
a, b, c = map(int, input().split())
x = ((c-a-b)**2-4*a*b > 0 ) and (c > a+b)
print('NYoe s'[x::2])
if __name__ == '__main__':
main() | 0 | null | 30,537,899,444,468 | 113 | 197 |
from math import gcd
k=int(input())
cnt=0
for i in range(1,k+1):
for j in range(1,k+1):
a=gcd(i,j)
for k in range(1,k+1):
cnt+=gcd(a,k)
print(cnt)
| import math
k = int(input())
ans = 0
for h in range(1, k+1):
for i in range(1, k+1):
g = math.gcd(h, i)
for j in range(1, k+1):
ans += math.gcd(g, j)
print(ans) | 1 | 35,559,553,552,200 | null | 174 | 174 |
n,k=map(int,input().split())
s=map(int,list(input()))
ans=0
if k==2 or k==5:
for i,p in enumerate(s):
if p%k==0:
ans+=i+1
else:
inv10=pow(10,k-2,k)
inv=10
total=0
S=[0]*k
S[0]=1
for i,p in enumerate(s):
inv*=inv10
inv%=k
total+=p*inv
total%=k
ans+=S[total]
S[total]+=1
print(ans)
|
def resolve():
l, P = map(int, input().split())
S = input()
ans = 0
# 10^r: 2^r * 5^r の為、10と同じ素数
if P == 2 or P == 5:
for i, s in enumerate(S):
if int(s) % P == 0:
ans += i + 1
return print(ans)
cnts = [0] * P
cnts[0] = 1
num, d = 0, 1
for s in S[::-1]:
s = int(s)
num = (num + (s * d)) % P
d = (10 * d) % P
cnts[num] += 1
for cnt in cnts:
ans += cnt * (cnt - 1) // 2
print(ans)
if __name__ == "__main__":
resolve() | 1 | 58,450,518,069,570 | null | 205 | 205 |
mount=[]
for i in range(10):
tmp=int(input())
mount.append(tmp)
mount.sort()
mount.reverse()
for i in range(3):
print(mount[i]) | # coding: utf-8
# Here your code !
array = []
for i in range(10):
line = int(input())
array += [line]
result = sorted(array)[::-1]
print(result[0])
print(result[1])
print(result[2]) | 1 | 23,927,228 | null | 2 | 2 |
W = input()
T = ""
while True:
x = input()
if x == "END_OF_TEXT":
break
T += x + " "
num = 0
for t in T.split():
if t.lower() == W.lower():
num += 1
print(num) | def upper(word):
Word = ''
str = list(word)
for i in range(len(str)):
if str[i].islower(): Word += str[i].upper()
else: Word += str[i]
return Word
key_word = str(upper(input()))
sen = []
count = 0
while True:
a = str(input())
if a == 'END_OF_TEXT': break
a = upper(a)
sen.extend(a.split())
for i in range(len(sen)):
if sen[i] == key_word:
count+= 1
print(count)
| 1 | 1,819,145,967,482 | null | 65 | 65 |
n,k = map(int,input().split())
li = list(map(int,input().split()))
if n==k:
ans = 0
for i in li:
ans += i*0.5+0.5
print(ans)
exit()
K = sum(li[:k])
ans = K
t = 0
for i in range(n-k):
K += li[i+k] - li[i]
if K > ans:
ans = K
t = i
ans = 0
for i in range(k):
ans += li[t+i+1]*0.5 + 0.5
print(ans) | N,K = map(int,input().split())
p = list(map(int,input().split()))
math = [0]*N
ans = 0
check = 0
for i,j in enumerate(p):
math[i] += (j*(j+1)/2)/j
if(i+1<=K):
check += math[i]
else:
check += math[i]
check -= math[i-K]
ans = max(ans,check)
print(ans) | 1 | 74,926,116,654,558 | null | 223 | 223 |
import math
def make_divisors(n):
lower_divisors , upper_divisors = [], []
i = 1
while i*i <= n:
if n % i == 0:
lower_divisors.append(i)
if i != n // i:
upper_divisors.append(n//i)
i += 1
return lower_divisors + upper_divisors[::-1]
n = int(input())
yaku = make_divisors(n)
Min = float('inf')
for i in range(math.ceil(len(yaku)/2)):
Min = min(Min, yaku[i]+yaku[-(i+1)])
print(Min-2) | n=int(input())
s=input().split()
q=int(input())
t=input().split()
cnt=0
for i in range(q):
for j in range(n):
if s[j]==t[i]:
cnt+=1
break
print(cnt)
| 0 | null | 81,023,262,839,052 | 288 | 22 |
import sys
sys.setrecursionlimit(10**8)
def find(x):
if par[x]==x:
return x
else:
par[x]=find(par[x])
return par[x]
def union(a,b):
a=find(a)
b=find(b)
if a==b:
return
if rank[a]<rank[b]:
par[a]=b
rank[b]+=rank[a]
rank[a]=rank[b]
else:
par[b]=a
rank[a]+=rank[b]
rank[b]=rank[a]
return
def chk(a,b):
if par[a]==par[b]:
print('Yes')
else:
print('No')
return
N,M=map(int, input().split())
par=(list(range(N+1)))
rank=[1]*(N+1)
for _ in range(M):
A,B=map(int, input().split())
union(A,B)
print(max(rank)) | mycode = r'''
# distutils: language=c++
# cython: language_level=3, boundscheck=False, wraparound=False
# cython: cdivision=True
# False:Cython はCの型に対する除算・剰余演算子に関する仕様を、(被演算子間の符号が異なる場合の振る舞いが異なる)Pythonのintの仕様に合わせ、除算する数が0の場合にZeroDivisionErrorを送出します。この処理を行わせると、速度に 35% ぐらいのペナルティが生じます。 True:チェックを行いません。
ctypedef long long LL
from libc.stdio cimport scanf, printf
from libcpp.vector cimport vector
ctypedef vector[LL] vec
cdef class UnionFind:
cdef:
LL N,n_groups
vec parent
def __init__(self, LL N):
self.N = N # ノード数
self.n_groups = N # グループ数
# 親ノードをしめす。負は自身が親ということ。
self.parent = vec(N,-1) # idxが各ノードに対応。
cdef LL root(self, LL A):
# print(A)
# ノード番号を受け取って一番上の親ノードの番号を帰す
if (self.parent[A] < 0):
return A
self.parent[A] = self.root(self.parent[A]) # 経由したノードすべての親を上書き
return self.parent[A]
cdef LL size(self, LL A):
# ノード番号を受け取って、そのノードが含まれている集合のサイズを返す。
return -self.parent[self.root(A)]
cdef bint unite(self,LL A,LL B):
# ノード番号を2つ受け取って、そのノード同士をつなげる処理を行う。
# 引数のノードを直接つなぐ代わりに、親同士を連結する処理にする。
A = self.root(A)
B = self.root(B)
# すでにくっついている場合
if (A == B):
return False
# 大きい方に小さい方をくっつけたほうが処理が軽いので大小比較
if (self.size(A) < self.size(B)):
A, B = B, A
# くっつける
self.parent[A] += self.parent[B] # sizeの更新
self.parent[B] = A # self.rootが呼び出されればBにくっついてるノードもすべて親がAだと上書きされる
self.n_groups -= 1
return True
cdef bint is_in_same(self,LL A,LL B):
return self.root(A) == self.root(B)
cdef LL N,M,_
scanf('%lld %lld',&N, &M)
cdef UnionFind uf = UnionFind(N)
cdef LL a,b
for _ in range(M):
scanf('%lld %lld',&a, &b)
uf.unite(a-1, b-1)
print(-min(uf.parent))
'''
import sys
import os
if sys.argv[-1] == 'ONLINE_JUDGE': # コンパイル時
with open('mycode.pyx', 'w') as f:
f.write(mycode)
os.system('cythonize -i -3 -b mycode.pyx')
import mycode
| 1 | 3,903,439,399,652 | null | 84 | 84 |
S = input()
wait_day = 0
if S == "SUN":
wait_day = 7
elif S == "MON":
wait_day = 6
elif S == "TUE":
wait_day = 5
elif S == "WED":
wait_day = 4
elif S == "THU":
wait_day = 3
elif S == "FRI":
wait_day = 2
elif S == "SAT":
wait_day = 1
print(wait_day) | import sys
youbi = ["MON", "TUE", "WED", "THU", "FRI", "SAT"]
S = input()
if S == "SUN":
print(7)
sys.exit()
for i, youbi in enumerate(youbi):
if S == youbi:
print(6-i)
| 1 | 132,609,500,867,118 | null | 270 | 270 |
def selectSort(A, N):
cnt = 0
for i in range(0,N):
mina = A[i]
k = 0
for j in range(i+1,N):
if int(mina) > int(A[j]):
mina = A[j]
k = j
if int(A[i]) > int(mina):
swap = A[i]
A[i] = mina
A[k] = swap
cnt = cnt + 1
for t in range(0,N):
if t < N-1:
print(A[t], end=" ")
else:
print(A[t])
print(cnt)
if __name__ == "__main__":
N = int(input())
A = (input()).split()
selectSort(A,N) | import sys
input = lambda: sys.stdin.readline().rstrip()
N, K = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
F = sorted(list(map(int, input().split())))
def binary_search(min_n, max_n):
while max_n - min_n != 1:
tn = (min_n + max_n) // 2
if judge(tn):
max_n = tn
else:
min_n = tn
return max_n
def judge(tn):
k = 0
for i in range(N):
if A[i] * F[i] > tn:
k += A[i] - (tn // F[i])
if k > K:
return False
return True
def solve():
ans = binary_search(-1, 10**12)
print(ans)
if __name__ == '__main__':
solve()
| 0 | null | 82,056,787,647,940 | 15 | 290 |
#!/usr/bin/env python3
import sys
import collections as cl
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def main():
N = II()
graph = [[] for i in range(N)]
prev = [-1 for i in range(N)]
colors = {}
edges = []
for _ in range(N-1):
a, b = MI()
graph[a-1].append(b-1)
graph[b-1].append(a-1)
edges.append([a-1, b-1])
max_edge = 0
max_edge_idx = -1
for i in range(len(graph)):
length = len(graph[i])
if length >= max_edge:
max_edge = length
max_edge_idx = i
deque = cl.deque([max_edge_idx])
while len(deque) > 0:
target = deque.popleft()
pre = prev[target]
pre_key = (target, pre) if target < pre else (pre, target)
used_color = 0
if pre != -1:
used_color = colors[pre_key]
connecteds = graph[target]
color = 1
for connected in connecteds:
key = (target, connected) if target < connected else (
connected, target)
if key in colors:
continue
if color == used_color:
color += 1
colors[key] = color
prev[connected] = target
deque.append(connected)
color += 1
print(max_edge)
for edge in edges:
print(colors[(edge[0], edge[1])])
main()
| import sys
# import math
# import bisect
# import numpy as np
# from decimal import Decimal
# from numba import njit, i8, u1, b1 #JIT compiler
# from itertools import combinations, product
# from collections import Counter, deque, defaultdict
# sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.14159265358979323846
def read_str(): return sys.stdin.readline().strip()
def read_int(): return int(sys.stdin.readline().strip())
def read_ints(): return map(int, sys.stdin.readline().strip().split())
def read_ints2(x): return map(lambda num: int(num) - x, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def GCD(a: int, b: int) -> int: return b if a%b==0 else GCD(b, a%b)
def LCM(a: int, b: int) -> int: return (a * b) // GCD(a, b)
class Graph():
def __init__(self, v):
from heapq import heappop, heappush
self.v = v
self.graph = [[] for _ in range(v)]
self.INF = 10 ** 9
def addEdge(self, start, end, itr):
self.graph[start].append((end, itr))
self.graph[end].append((start, itr))
def BFS(self, start):
from collections import deque
dist = [-1] * self.v
ret = [0] * (self.v-1)
que = deque()
que.append((start, -1))
dist[start] = 0
while que:
now, _color = que.popleft()
color = 1
if _color == color: color += 1
for to, itr in self.graph[now]:
if dist[to] == -1:
que.append((to, color))
dist[to] = dist[now] + 1
ret[itr] = color
color += 1
if color == _color: color += 1
return ret
def color_types(self):
ret = len(self.graph[0])
for x in self.graph[1:]:
ret = max(ret, len(x))
return ret
def Main():
n = read_int()
g = Graph(n)
for i in range(n - 1):
a, b = read_ints()
g.addEdge(~-a, ~-b, i)
print(g.color_types())
print(*g.BFS(0), sep='\n')
if __name__ == '__main__':
Main() | 1 | 135,540,228,138,788 | null | 272 | 272 |
A = input()
B = input()
KAI = "123"
KAI1 = KAI.replace(A,"")
KAI2 = KAI1.replace(B,"")
print(KAI2) | a= int(input())
b= int(input())
lista=[1, 2, 3]
for x in range(3):
if lista[x] != a and lista[x] != b:
print(lista[x]) | 1 | 110,547,628,979,940 | null | 254 | 254 |
a_index, a_v = map(int, input().split())
b_index, b_v = map(int, input().split())
t = int(input())
d = abs(b_index - a_index)
d_v = a_v - b_v
if d == 0 :
print("YES")
elif d_v > 0 and d / d_v <= t :
print("YES")
else :
print("NO") | house1 = [[0 for x in range(10)], [0 for x in range(10)], [0 for x in range(10)]]
house2 = [[0 for x in range(10)], [0 for x in range(10)], [0 for x in range(10)]]
house3 = [[0 for x in range(10)], [0 for x in range(10)], [0 for x in range(10)]]
house4 = [[0 for x in range(10)], [0 for x in range(10)], [0 for x in range(10)]]
houses = [house1, house2, house3, house4]
n = int(input())
for i in range(n):
b, f, r, v = [int(x) for x in input().split()]
houses[b - 1][f - 1][r - 1] += v
cnt = 0
for house in houses:
for floor in house:
floor = [str(x) for x in floor]
print(' ' + ' '.join(floor))
if cnt != 3:
print('#' * 20)
cnt += 1 | 0 | null | 8,060,213,545,670 | 131 | 55 |
def main():
mountain=[]
for i in range(10):
num=int(input())
mountain.append(num)
mountain=sorted(mountain)
mountain=mountain[::-1]
for i in range(3):
print(mountain[i])
if __name__=='__main__':
main() | A = [int(input()) for i in range(10)]
for i in sorted(A)[:-4:-1]:
print(i)
| 1 | 18,144,448 | null | 2 | 2 |
N,T = map(int,input().split())
item=[list(map(int,input().split())) for _ in range(N)]
item = sorted(item,key=lambda x:x[0])
dp=[[0]*(T+1) for _ in range(N+1)]
for i,(a,b) in enumerate(item,1):
for t in reversed(range(T)):
dist=min(T,t+a)
dp[i][dist] = max(dp[i-1][t]+b,dp[i][dist])
dp[i][t]=max(dp[i-1][t],dp[i-1][t])
ans=0
for l in dp:
ans=max(ans,max(l))
print(ans)
| #!/usr/bin/env python
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)
INF = float("inf")
def main():
N,T = map(int,input().split())
AB = [(0,0)] + [tuple(map(int,input().split())) for _ in range(N)]
# 最後に食べるものを全探索し, それ以外でDPしたくなる
# 普通にやると TLE だが, 左から,右から で事前にDPしておくことで計算量を落とせる
# このとき, 配列添字を 1〜N にしておくと楽
# 1番目の料理を最後に食べるとき : dp1[0][] + b + dp2[2][], このときに index out of range しないのが嬉しい
# 2番目の料理を最後に食べるとき : dp1[1][] + b + dp2[3][]
# ...
# N番目の料理を最後に食べるとき : dp1[N-1][] + b + dp2[N+1][], dp2 は N+2 の長さがあると良さそう
# dp1[i][j] : 1〜i 番目の料理から選んで T 分以内に食べるときの美味しさの和の最大値
dp1 = [[0 for _ in range(T+1)] for _ in range(N+1)]
for i in range(1,N+1):
a,b = AB[i]
for j in range(T+1):
# 食べる
if j >= a:
dp1[i][j] = max(dp1[i-1][j], dp1[i-1][j-a] + b)
# 食べない
else:
dp1[i][j] = dp1[i-1][j]
# dp2[i][j] : i〜N 番目の料理から選んで T 分以内に食べるときの美味しさの和の最大値
dp2 = [[0 for _ in range(T+1)] for _ in range(N+2)]
for i in range(N,0,-1):
a,b = AB[i]
for j in range(T+1):
# 食べる
if j >= a:
dp2[i][j] = max(dp2[i+1][j], dp2[i+1][j-a] + b)
# 食べない
else:
dp2[i][j] = dp2[i+1][j]
# 最後に食べる料理で全探索
ans = 0
for i in range(1,N+1):
a,b = AB[i]
for j in range(T):
ans = max(ans, dp1[i-1][j] + b + dp2[i+1][T-1-j])
print(ans)
if __name__ == "__main__":
main() | 1 | 151,540,701,233,672 | null | 282 | 282 |
N, K = map(int, input().split())
H = list(map(int, input().split()))
if K >= N:
print(0)
else:
H = sorted(H)
count = 0
for i in range(N-K):
count += H[i]
print(count) |
N= int(input())
st = [list(map(str,input().split())) for _ in range(N)]
x = input()
ans = 0
flag = False
for s,t in st:
if flag:
ans += int(t)
else:
if s == x:
flag = True
print(ans) | 0 | null | 87,721,116,393,566 | 227 | 243 |
from decimal import Decimal
N = int(input())
M = int(N/Decimal(1.08)) + 1
if N == int(M*Decimal(1.08)) :
print( int(M))
else :
print(':(') | N=int(input())
import math
X=math.ceil(N/1.08)
if int(X*1.08)==N:
print(X)
else:
print(':(') | 1 | 125,852,490,284,950 | null | 265 | 265 |
import math
H, W = map(int, input().split(' '))
if H == 1 or W == 1:
print(1)
exit()
res = math.ceil((H*W)/2)
print(res)
| H, W = list(map(int, input().split()))
if H == 1 or W == 1:
print(1)
else:
print(H*W//2 + (H*W)%2)
| 1 | 50,876,281,886,980 | null | 196 | 196 |
a,b = map(int,input().split())
ans = 0
if a - (2 * b) > 0:
ans = a - (2 * b)
print(ans) | import math
n,m,x = map(int,input().split())
book = [list(map(int,input().split())) for _ in range(n)]
best = math.inf
for i in range(2**n):
und = [0]*m
money = 0
for j in range(n):
if (i>>j) & 1:
money += book[j][0]
for k in range(m):
und[k] += book[j][k+1]
if all(s>=x for s in und) and best>money:
best = money
if best==math.inf:
print(-1)
else:
print(best) | 0 | null | 94,299,086,565,708 | 291 | 149 |
x = int(input())
num = (x//500)*1000
num += ((x%500)//5)*5
print(num) | n = int(input())
lis = input().split()
dic = {}
for i in range(n):
if lis[i] not in dic:
dic[lis[i]] = 1
else:
dic[lis[i]] += 1
for x in dic.values():
if x != 1:
print("NO")
exit()
print("YES") | 0 | null | 58,175,314,248,282 | 185 | 222 |
a = int(input())
n = 3.141592
print((a + a) * n) | #!/usr/bin/env python3
import sys
from collections import deque
sys.setrecursionlimit(1000000)
class UnionFind:
def __init__(self, num):
self.par = list(range(1,num+1))
self.size = [1]*num
def root(self, n):
if self.par[n-1] != n:
self.par[n-1] = self.root(self.par[n-1])
return self.par[n-1]
def unite(self, a, b):
a=self.root(a)
b=self.root(b)
if a!=b:
self.par[b-1]=a
self.size[a-1] += self.size[b-1]
return
def get_size(self, n):
return self.size[self.root(n)-1]
def solve(N: int, M: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]", D: "List[int]"):
fris = [0]*N
blos = [0]*N
union = UnionFind(N)
for ai, bi in zip(A, B):
union.unite(ai,bi)
fris[ai-1]+=1
fris[bi-1]+=1
for ci, di in zip(C, D):
if union.root(ci) == union.root(di):
blos[ci-1]+=1
blos[di-1]+=1
ans = [0]*N
for i in range(1,N+1):
s = union.get_size(i)
s -= fris[i-1]
s -= blos[i-1]
s -= 1
ans[i-1] = s
print(*ans)
return
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens)) # type: int
M = int(next(tokens)) # type: int
K = int(next(tokens)) # type: int
A = [int()] * (M) # type: "List[int]"
B = [int()] * (M) # type: "List[int]"
for i in range(M):
A[i] = int(next(tokens))
B[i] = int(next(tokens))
C = [int()] * (K) # type: "List[int]"
D = [int()] * (K) # type: "List[int]"
for i in range(K):
C[i] = int(next(tokens))
D[i] = int(next(tokens))
solve(N, M, K, A, B, C, D)
if __name__ == '__main__':
main()
| 0 | null | 46,297,806,305,312 | 167 | 209 |
n = int(input())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
def cumsum(num, lst):
cumsumlst = [0] * num
for hoge in range(1, n):
cumsumlst[hoge] = (cumsumlst[hoge-1] + lst[hoge]) % mod
return cumsumlst
lst = cumsum(n, a)
ans = 0
for i in range(n-1):
ans += (a[i] * (lst[n-1] - lst[i])) % mod
ans %= mod
print(ans) | n=input()
ans=0
if n=="0":
print("Yes")
else:
for i in n:
ans+=int(i)
if ans%9==0:
print("Yes")
else:
print("No") | 0 | null | 4,098,125,309,808 | 83 | 87 |
n = int(input())
p = list(map(int, input().split()))
min_val = n + 1
count = 0
for x in p:
if x < min_val:
min_val = x
count += 1
print(count)
| # C
N = int(input())
P = list(map(int,input().split()))
min_p = 10**10
ans = 0
for i in range(N):
if min_p >= P[i]:
ans += 1
min_p = min(min_p,P[i])
print(ans) | 1 | 85,434,338,410,642 | null | 233 | 233 |
from itertools import combinations
n = int(input())
d = list(map(int, input().split()))
ans = 0
d = combinations(d, 2)
for i, j in d:
ans += i*j
print(ans)
| n = int(input())
A = [0]
A.extend(list(map(int,input().split())))
B = {}
pair = 0
for i in range(1,n+1):
if i+A[i] in B:
B[i+A[i]] += 1
else:
B[i+A[i]] = 1
for j in range(2,n+1):
if j-A[j] in B:
pair += B[j-A[j]]
print(pair) | 0 | null | 96,923,341,659,440 | 292 | 157 |
def inpl(): return list(map(int, input().split()))
print((int(input())-1)//2) | import sys
import itertools
# import numpy as np
import time
import math
sys.setrecursionlimit(10 ** 7)
from collections import defaultdict
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n, x, y = map(int, readline().split())
dists = [0 for _ in range(n)]
for i in range(1, n + 1):
for j in range(i + 1, n + 1):
d = min(abs(i - j), abs(x - i) + 1 + abs(y - j))
dists[d] += 1
for i in range(1, n):
print(dists[i])
| 0 | null | 98,211,639,198,528 | 283 | 187 |
s=input()
t=input()
scorelist=[]
for i in range(len(s)-len(t)+1):
S=s[i:i+len(t)]
score=0
for j in range(len(t)):
if S[j]!=t[j]:
score+=1
scorelist.append(str(score))
point=min(scorelist)
print(point)
| N,X,T = [int(i) for i in input().split()]
print((N+X-1)//X*T) | 0 | null | 3,932,149,166,040 | 82 | 86 |
from collections import deque
n = int(input())
G = []
for _ in range(n):
tmp = list(map(int, input().split()))
u = tmp.pop(0)
k = tmp.pop(0)
t = []
for i in range(k):
a = tmp.pop(0)
t.append(a-1)
G.append(t)
dist = [-1]*n
dist[0] = 0
que = deque()
que.append(0)
while que:
v = que.popleft()
for nv in G[v]:
if dist[nv] != -1:
continue
dist[nv] = dist[v] + 1
que.append(nv)
for i in range(n):
print(i+1, dist[i])
| from collections import deque
u = int(input())
g = [[i for i in map(int, input().split())] for _ in range(u)]
graph = [[] for _ in range(u)]
ans = [-1] * u
for i in range(u):
for j in range(g[i][1]):
graph[i].append(g[i][2 + j] - 1)
que = deque()
que.append(0)
ans[0] = 0
while que:
v = que.popleft()
for nv in graph[v]:
if ans[nv] != -1:
continue
ans[nv] = ans[v] + 1
que.append(nv)
for i in range(u):
print(i+1, ans[i])
| 1 | 4,737,260,722 | null | 9 | 9 |
X,Y=list(map(int, input().split()))
a=(2*X-Y)//3
b=(2*Y-X)//3
if not (2*a+b==X and a+2*b==Y and a>=0 and b>=0):
print(0)
exit()
C=10**9+7
A=1
B=1
D=1
for i in range(a):
A*=i+1
A=A%C
for j in range(b):
B*=j+1
B=B%C
for k in range(a+b):
D*=k+1
D=D%C
D*=pow(A,C-2,C)
D*=pow(B,C-2,C)
print(D%C) | import copy
import math
import time
import statistics
import math
import itertools
# a = get_int()
def get_int():
return int(input())
# a = get_string()
def get_string():
return input()
# a_list = get_int_list()
def get_int_list():
return [int(x) for x in input().split()]
# a_list = get_string_list():
def get_string_list():
return input().split()
# a, b = get_int_multi()
def get_int_multi():
return map(int, input().split())
# a_list = get_string_char_list()
def get_string_char_list():
return list(str(input()))
# print("{} {}".format(a, b))
# for num in range(0, a):
# a_list[idx]
# a_list = [0] * a
'''
while (idx < n) and ():
idx += 1
'''
def main():
start = time.time()
n = get_int()
p = get_int_list()
q = get_int_list()
a = []
for i in range(1, n+1):
a.append(i)
count = 0
count_p = 0
count_q = 0
for keiro in list(itertools.permutations(a)):
if list(keiro) == p:
count_p = count
if list(keiro) == q:
count_q = count
count += 1
print(abs(count_p - count_q))
if __name__ == '__main__':
main()
| 0 | null | 124,685,185,315,968 | 281 | 246 |
N,X,M = [int(a) for a in input().split()]
amari = [0]*M
i = 1
A = X
amari[A] = i
l = [X]
ichi = i
while i<=M:
i += 1
A = A**2 % M
if amari[A]: i += M
else:
amari[A] = i
l.append(A)
if i==N: i = M+1
else:
if i>M+1: ni = i-M - amari[A]
else: ni = 0
#for j in range(M):
# if amari[j]: print(j, amari[j])
#print(i,len(l))
ichi = len(l) - ni
#print(l)
if ni:
ni_times, san = divmod((N - ichi), ni)
#print(ichi, '%d*%d'%(ni,ni_times), san)
print(sum(l[:ichi]) +
sum(l[ichi:])*ni_times +
sum(l[ichi:ichi+san]))
else:
#print(ichi, '%d*%d'%(ni,ni_times), san)
print(sum(l)) | n = int(input())
x = [int(x) for x in input().split()]
y = [int(x) for x in input().split()]
abs_list = [abs(a-b) for a,b in zip(x,y)]
p1 = p2 = p3 = p4 = 0
for a in abs_list:
p1 += a
p2 += a**2
p3 += a**3
p2 = p2 **(1/2)
p3 = p3 **(1/3)
p4 = max(abs_list)
preanswer = [p1,p2,p3,p4]
answer = ['{:.6f}'.format(i) for i in preanswer]
for a in answer:
print(a)
| 0 | null | 1,522,789,290,112 | 75 | 32 |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.