code1
stringlengths 16
427k
| code2
stringlengths 16
427k
| similar
int64 0
1
| pair_id
int64 2
178,025B
⌀ | question_pair_id
float64 27.1M
177,113B
⌀ | code1_group
int64 1
297
| code2_group
int64 1
297
|
---|---|---|---|---|---|---|
N = int(input())
for i in range(N):
l = list(map(int,input().split()))
l.sort()
if l[0]*l[0]+l[1]*l[1] == l[2]*l[2]: print("YES")
else: print("NO")
|
N, S = map(int, input().split())
A = list(map(int, input().split()))
MOD = 998244353
dp = [[0] * (S + 1) for _ in range(N + 1)]
dp[0][0] = 1
for i, a in enumerate(A):
for j in range(S + 1):
dp[i + 1][j] += 2 * dp[i][j]
dp[i + 1][j] %= MOD
if j + a <= S:
dp[i + 1][j + a] += dp[i][j]
dp[i + 1][j + a] %= MOD
print(dp[-1][-1] % MOD)
| 0 | null | 8,917,511,030,568 | 4 | 138 |
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
S1 = Counter()
S2 = Counter()
for i,a in enumerate(A):
S1[i+1+a] += 1
S2[(i+1)-a] += 1
res = 0
for k,v in S1.items():
if S2[k] > 0:
res += v*S2[k]
print(res)
|
MOD=10**9+7
N=int(input())
ans=pow(10,N,MOD)
ans-=2*pow(9,N,MOD)
ans+=pow(8,N,MOD)
ans%=MOD
print(ans)
| 0 | null | 14,599,178,939,128 | 157 | 78 |
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def main():
N, M, L = map(int, readline().split())
G = [[INF] * N for _ in range(N)]
for _ in range(M):
a, b, c = map(int, readline().split())
G[a - 1][b - 1] = G[b - 1][a - 1] = c
for i in range(N):
G[i][i] = 0
Q, *ST = map(int, read().split())
for k in range(N):
for i in range(N):
for j in range(N):
if G[i][j] > G[i][k] + G[k][j]:
G[i][j] = G[i][k] + G[k][j]
H = [[INF] * N for _ in range(N)]
for i in range(N - 1):
for j in range(i + 1, N):
if G[i][j] <= L:
H[i][j] = H[j][i] = 1
for i in range(N):
H[i][i] = 0
for k in range(N):
for i in range(N):
for j in range(N):
if H[i][j] > H[i][k] + H[k][j]:
H[i][j] = H[i][k] + H[k][j]
ans = [0] * Q
for i, (s, t) in enumerate(zip(*[iter(ST)] * 2)):
s -= 1
t -= 1
if H[s][t] == INF:
ans[i] = -1
else:
ans[i] = H[s][t] - 1
print(*ans, sep='\n')
return
if __name__ == '__main__':
main()
|
def main():
n = int(input())
d_lst = list(map(int, input().split()))
ans = 0
for i in range(n - 1):
for j in range(i + 1, n):
ans += d_lst[i] * d_lst[j]
print(ans)
if __name__ == "__main__":
main()
| 0 | null | 171,097,117,740,228 | 295 | 292 |
N, K = map(int, input().split())
A = list(map(int, input().split()))
visited = [-1 for _ in range(N)]
visited[0] = 0
k = 0
k = A[k] - 1
count = 0
while visited[k] == -1:
count += 1
visited[k] = count
k = A[k]-1
roop = count - visited[k] + 1
beforeroop = visited[k]
if K < beforeroop:
k = 0
for _ in range(K):
k = A[k] - 1
print(k+1)
else:
K = K - beforeroop
K = K % roop
for _ in range(K):
k = A[k]-1
print(k+1)
|
N, K = map(int,input().split())
tele = list(map(int, input().split()))
now = 0
goal,loop,visit = [],[],[0]*N
while visit[now]!=2:
if visit[now]==0:
goal.append(now)
else:
loop.append(now)
visit[now] += 1
now = tele[now]-1
if len(goal)>K:
print(goal[K]+1)
else:
res= len(goal)-len(loop)
print(loop[(K-res)%len(loop)]+1)
| 1 | 22,782,878,484,938 | null | 150 | 150 |
N = int(input())
ans = [0] * (N+1)
for i in range(N+1):
if i % 3 != 0 and i % 5 != 0 and i % 15 != 0:
ans[i] = ans[i-1] + i
else:
ans[i] = ans[i-1]
print(ans[-1])
|
N = int(input())
A = []
for i in range(N+1):
if i % 3 == 0:
pass
elif i % 5 == 0:
pass
else:
A.append(i)
print(sum(A))
| 1 | 34,961,834,531,172 | null | 173 | 173 |
X = int(input())
flag = 0
for i in range(-150,150):
for j in range(-150,150):
if(i**5 - j**5 == X):
print(i,j)
flag = 1
break
if(flag==1):
break
|
from collections import defaultdict as dd
from collections import deque
import bisect
import heapq
def ri():
return int(input())
def rl():
return list(map(int, input().split()))
def solve():
x = ri()
fifths = {i**5: i for i in range(10000)}
a = 0
while 1:
if x - a**5 in fifths:
print (a, -fifths[x - a**5])
return
elif a**5 - x in fifths:
print (a, fifths[a**5 - x])
return
a += 1
mode = 's'
if mode == 'T':
t = ri()
for i in range(t):
solve()
else:
solve()
| 1 | 25,588,122,469,430 | null | 156 | 156 |
#ABC167A
s = input()
t = input()
print("Yes" if s==t[:-1] else "No")
|
s = input()
t = input()
if s == t[:len(t) - 1]:
print('Yes')
else:
print('No')
| 1 | 21,262,627,072,020 | null | 147 | 147 |
s=input()
p=input()
s2=s*2
ren=s2.count(p)
if ren==0:
print('No')
else:
print('Yes')
|
s,p=input()*2,input()
print(['No','Yes'][p in s])
| 1 | 1,774,192,037,530 | null | 64 | 64 |
#coding:utf-8
import sys,os
from collections import defaultdict, deque
from fractions import gcd
from math import ceil, floor
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
input = lambda: given().rstrip()
LMIIS = lambda: list(map(int,input().split()))
II = lambda: int(input())
XLMIIS = lambda x: [LMIIS() for _ in range(x)]
YN = lambda c : print('Yes') if c else print('No')
MOD = 10**9+7
list_N = list(map(int,list(input())))
len_N = len(list_N)
list_N = [0] + list_N
K = II()
dp0 = []
dp1 = []
for i in range(len_N+1):
dp0.append([0]*(K+1))
dp1.append([0]*(K+1))
for i in range(1,len_N+1):
dp0[i][0] = 1
dp0[1][1] = max(list_N[1]-1,0)
dp1[1][1] = 1
for i in range(2,len_N+1):
dp1[i][0] = dp1[i-1][0]
for j in range(1,K+1):
num = list_N[i]
if num > 0:
dp0[i][j] = dp0[i-1][j-1] * 9 + dp0[i-1][j] + dp1[i-1][j-1] * (num-1) + dp1[i-1][j]
dp1[i][j] = dp1[i-1][j-1]
else:
dp0[i][j] = dp0[i-1][j-1] * 9 + dp0[i-1][j]
dp1[i][j] = dp1[i-1][j]
print(dp0[len_N][K]+dp1[len_N][K])
dbg(dp0[len_N][K],dp1[len_N][K])
if __name__ == '__main__':
main()
|
n, *AB = map(int, open(0).read().split())
a = sorted(AB[::2])
b = sorted(AB[1::2])
if n % 2 == 0:
print(b[n // 2 - 1] + b[n // 2] - a[n // 2 - 1] - a[n // 2] + 1)
else:
print(b[(n + 1) // 2 - 1] - a[(n + 1) // 2 - 1] + 1)
| 0 | null | 46,821,689,566,940 | 224 | 137 |
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
l ,r, d = map(int,input().split())
ans=0
for i in range(l,r+1):
if i % d ==0:
ans+=1
print(ans)
if __name__=='__main__':
main()
|
l,r,d = map(int, input().split())
var=l//d
var1=r//d
ans=var1-var
if l%d==0:
ans+=1
print(ans)
| 1 | 7,486,050,551,232 | null | 104 | 104 |
n=int(input())
a=list(map(int,input().split()))
q=int(input())
b=[0]*q
c=[0]*q
for i in range(q):
b[i],c[i]=map(int,input().split())
N=[0]*(10**5)
s=0
for i in range(n):
N[a[i]-1]+=1
s+=a[i]
for i in range(q):
s+=(c[i]-b[i])*N[b[i]-1]
N[c[i]-1]+=N[b[i]-1]
N[b[i]-1]=0
print(s)
|
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
S = [list(map(int, input().split())) for l in range(Q)]
numList = {}
sum = 0
for n in range(N):
if A[n] not in numList:
numList[A[n]] = 0
numList[A[n]] += 1
sum += A[n]
for q in range(Q):
if S[q][0] in numList:
if S[q][1] not in numList:
numList[S[q][1]] = 0
sum += (S[q][1] - S[q][0]) * numList[S[q][0]]
numList[S[q][1]] += numList[S[q][0]]
numList[S[q][0]] = 0
print(sum)
| 1 | 12,263,145,368,988 | null | 122 | 122 |
def resolve():
A, B, N = map(int, input().split())
if B <= N+1:
print(A*(B-1)//B)
else:
print(A*N//B)
resolve()
|
import sys
input = sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def main():
mod=10**9+7
class UnionFind:
def __init__(self, N: int):
"""
N:要素数
root:各要素の親要素の番号を格納するリスト.
ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数.
rank:ランク
"""
self.N = N
self.root = [-1] * N
self.rank = [0] * N
def __repr__(self):
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
def find(self, x: int):
"""頂点xの根を見つける"""
if self.root[x] < 0:
return x
else:
while self.root[x] >= 0:
x = self.root[x]
return x
def union(self, x: int, y: int):
"""x,yが属する木をunion"""
# 根を比較する
# すでに同じ木に属していた場合は何もしない.
# 違う木に属していた場合はrankを見てくっつける方を決める.
# rankが同じ時はrankを1増やす
x = self.find(x)
y = self.find(y)
if x == y:
return
elif self.rank[x] > self.rank[y]:
self.root[x] += self.root[y]
self.root[y] = x
else:
self.root[y] += self.root[x]
self.root[x] = y
if self.rank[x] == self.rank[y]:
self.rank[y] += 1
def same(self, x: int, y: int):
"""xとyが同じグループに属するかどうか"""
return self.find(x) == self.find(y)
def count(self, x):
"""頂点xが属する木のサイズを返す"""
return - self.root[self.find(x)]
def members(self, x):
"""xが属する木の要素を列挙"""
_root = self.find(x)
return [i for i in range(self.N) if self.find == _root]
def roots(self):
"""森の根を列挙"""
return [i for i, x in enumerate(self.root) if x < 0]
def group_count(self):
"""連結成分の数"""
return len(self.roots())
def all_group_members(self):
"""{ルート要素: [そのグループに含まれる要素のリスト], ...}の辞書を返す"""
return {r: self.members(r) for r in self.roots()}
N,M=MI()
uf=UnionFind(N)
for _ in range(M):
a,b=MI()
a-=1
b-=1
uf.union(a,b)
cnt=uf.group_count()
print(cnt-1)
main()
| 0 | null | 15,137,199,365,990 | 161 | 70 |
s = input()
for i in range(int(input())):
cmd = input().split()
a, b = int(cmd[1]), int(cmd[2])
if cmd[0] == 'print':
print(s[a:b+1])
elif cmd[0] == 'reverse':
s = s[:a] + s[a:b+1][::-1] + s[b+1:]
elif cmd[0] == 'replace':
s = s[:a] + cmd[3] + s[b+1:]
|
N = [int(_) for _ in list(input())]
a, b = 0, 1
for n in N:
a, b = min(a+n, b+10-n), min(a+(n+1), b+10-(n+1))
print(a)
| 0 | null | 36,653,662,724,938 | 68 | 219 |
cnt = 0
n = int(input())
lst = list(map(int, input().split()))
for i in range(n):
for j in range(n-1,i,-1):
if lst[j] < lst[j-1]:
lst[j-1:j+1] = lst[j], lst[j-1]
cnt += 1
print(*lst)
print(cnt)
|
s = input()
hitachi = ""
for i in range(5):
hitachi += "hi"
if s==hitachi:
print("Yes")
exit(0)
print("No")
| 0 | null | 26,563,170,974,858 | 14 | 199 |
x=int(input())
for a in range(-120,121):
for b in range(-120,121):
if x==pow(a,5)-pow(b,5):
print(a,b)
exit()
|
#import numpy as np
#import math
#from decimal import *
#from numba import njit
#@njit
def main():
X = int(input())
p = []
def pow5(n):
nonlocal p
if len(p) > n:
return p[n]
ans = pow(n,5)
p += ans,
return ans
a = 0
b = 0
while True:
if abs(X) + pow5(b) == pow5(a) or abs(X) - pow5(b) == pow5(a) :
a5 = pow5(a)
b5 = pow5(b)
if X == a5 - b5:
print(a,b)
elif X == a5 + b5:
print(a, -b)
elif X == -a5 - b5:
print(-a, b)
else:
print(-a, -b)
return
while abs(X) + pow5(b) > pow5(a):
a += 1
if abs(X) + pow5(b) == pow5(a) or abs(X) - pow5(b) == pow5(a) :
a5 = pow5(a)
b5 = pow5(b)
if X == a5 - b5:
print(a,b)
elif X == a5 + b5:
print(a, -b)
elif X == -a5 - b5:
print(-a, b)
else:
print(-a, -b)
return
a = 0
b += 1
main()
| 1 | 25,698,306,981,088 | null | 156 | 156 |
a, b = input().split()
a, b = int(a), int(b)
print('%d %d' % (a * b, 2 * (a + b)))
|
length,breadth =(int(x) for x in input().split())
area = length * breadth
perimeter = (length * 2) + (breadth*2)
print(str(area) + ' ' + str(perimeter))
| 1 | 309,166,898,512 | null | 36 | 36 |
a,b=raw_input().split()
print str(int(a)*int(b)) +" "+str(int(a)*2+int(b)*2)
|
a,b = [int(i) for i in input().split()]
print(a*b,2*(a+b))
| 1 | 306,622,855,632 | null | 36 | 36 |
import sys
# C - Walking Takahashi
X, K, D = map(int, input().split())
if D * K <= abs(X):
print(abs(X) - D * K)
else:
distance = abs(X) % D
# 残りの移動回数
count = K - (abs(X) // D)
if count % 2 == 1:
print(abs(D - distance))
else:
print(distance)
|
import sys
X,K,D= map(int,input().split())
temp = X
if abs(X) > K*D:
print(abs(X)-K*D)
sys.exit()
for t in range(K):
# 絶対値の小さい方に移動
if abs(temp - D) < abs(temp + D):
temp = temp-D
else:
temp = temp+D
# 移動幅より小さくなったとき
if abs(temp) < D:
if (K-t-1) % 2 == 0:
print(abs(temp))
sys.exit()
else:
if abs(temp - D) < abs(temp + D):
temp = temp-D
print(abs(temp))
sys.exit()
else:
temp = temp+D
print(abs(temp))
sys.exit()
print(abs(temp))
| 1 | 5,220,267,680,598 | null | 92 | 92 |
import sys
S = sys.stdin.readline().strip()
N = 2019
L = len(S)
curr = 0
seen = {}
INV = 210
seen[curr] = 1
for i in range(L):
curr = (curr * 10 + int(S[i])) % N
t = (curr * pow(10, L-i, N)) %N
if t not in seen: seen[t] = 0
seen[t] += 1
res = 0
for i in range(N):
if i not in seen: continue
t = seen[i]
res += t * (t-1)//2
print(res)
|
S = input()
from collections import defaultdict
d = defaultdict(int)
before = 0
for i in range(1,len(S)+1):
now = (int(S[-i])*pow(10,i,2019)+before) % 2019
d[now] += 1
before = now
d[0] += 1
ans = 0
for i in d.values():
ans += i*(i-1)//2
print(ans)
| 1 | 30,844,934,070,836 | null | 166 | 166 |
a = []
for _ in range(3):
a += list(map(int, input().split()))
n = int(input())
for _ in range(n):
b = int(input())
if b in a:
i = a.index(b)
a[i] = 0
if sum(a[0:3]) == 0 or sum(a[3:6]) == 0 or sum(a[6:]) == 0 \
or sum(a[0::3]) == 0 or sum(a[1::3]) == 0 or sum(a[2::3]) == 0 \
or sum(a[0::4]) == 0 or sum(a[2:7:2]) == 0:
print("Yes")
else:
print("No")
|
while True:
(H, W) = [int(i) for i in input().split()]
if H == W == 0:
break
for a in range(H):
for b in range(W):
if (a + b) % 2 == 0:
print('#', end='')
else:
print('.', end='')
print('')
print('')
| 0 | null | 30,516,575,317,668 | 207 | 51 |
import math
a,b,agree=map(float,input().split())
agree=math.radians(agree)
c=(a**2+b**2-2*a*b*math.cos(agree))**0.5
s=0.5*a*b*math.sin(agree)
l=a+b+c
h=b*math.sin(agree)
print("{0:.5f}".format(s))
print("{0:.5f}".format(l))
print("{0:.5f}".format(h))
|
import math
a, b, C = map(int, input().split())
S = a * b * math.sin(math.radians(C)) / 2
L = a + b + (a ** 2 + b ** 2 - 2 * a * b * math.cos(math.radians(C))) ** 0.5
h = b * math.sin(math.radians(C))
print('%.7f\n%.7f\n%.7f\n' % (S, L, h))
| 1 | 171,800,041,592 | null | 30 | 30 |
a,b=[int(x) for x in input().split()]
op = '>' if a>b else '<' if a<b else '=='
print('a',op,'b')
|
def compare(a, b):
if a > b:
print('a > b')
elif a < b:
print('a < b')
else:
print('a == b')
a, b = [int(x) for x in input().split()]
compare(a, b)
| 1 | 352,562,923,062 | null | 38 | 38 |
dic = {}
line = []
for i in range(int(input())):
line.append(input().split())
for i in line:
if i[0][0] == "i":
dic[i[1]] = None
else:
print("yes" if i[1] in dic else "no")
|
dic = {}
for s in range(int(input())):
i = input().split()
if i[0] == "insert":
dic[i[1]] = None
else:
print("yes" if i[1] in dic else "no")
| 1 | 76,730,202,680 | null | 23 | 23 |
n = int(input())
pl = list(map(int, input().split()))
min_num = 1001001001
ans = 0
for p in pl:
if p <= min_num:
ans += 1
min_num = p
print(ans)
|
n = int(input())
p = list(map(int, input().split()))
ans = 1
p_min = p[0]
for i in range(1,n):
if p_min >= p[i]:
ans += 1
p_min = min(p_min, p[i])
print(ans)
| 1 | 85,309,866,537,440 | null | 233 | 233 |
n = int(input())
a = list(map(int,input().split()))
mod = 10**9 + 7
c = [n]*61
for i in range(n):
b = str(bin(a[i]))[2:]
b = b[::-1]
for j, x in enumerate(b):
if x == "1":
c[j] -= 1
ans = 0
for i in range(60):
ans += c[i]*(n-c[i])*pow(2,i,mod)
ans %= mod
print(ans)
|
import sys
input = sys.stdin.readline
def main():
N = int(input())
A = list(map(int, input().split()))
p = 10**9 + 7
total = 0
for i in range(60):
cnt_1, cnt_0 = 0, 0
for j in range(N):
if (A[j]>>i)&1:
cnt_1 += 1
else:
cnt_0 += 1
total += pow(2, i, p) * cnt_0 * cnt_1 %p
print(total%p)
main()
| 1 | 122,857,704,238,852 | null | 263 | 263 |
def main():
N,K = map(int,input().split())
A = [0] + list(map(int,input().split()))
i = 1
pas = [i]
pas_set = set(pas)
k = 1
while k <= K:
if A[i] in pas_set:
rps = pas.index(A[i])
ans = A[i]
break
pas.append(A[i])
pas_set.add(A[i])
ans = A[i]
i = A[i]
k += 1
if k >= K:
print(ans)
else:
rpnum = (K-rps)%(k-rps)
print(pas[rps+rpnum])
main()
|
# -*- coding: utf-8 -*-
def main():
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
a = list(map(int, input().split()))
# See:
# https://www.youtube.com/watch?v=ENSOy8u9K9I&feature=youtu.be
# KeyInsight:
# 移動: 周期的ではない + 周期的に繰り返すの合計
# 実装上のポイント
# 経路と順番を別々に管理する
path = list()
order = [-1 for _ in range(n + 1)] # -1: 到達していない
place = 1
# 到達していない点がある限り、ループを繰り返す
# = 同じ点を2回通るまで繰り返す
while order[place] == -1:
order[place] = len(path) # ある場所を通った順番を管理
path.append(place) # 経路を更新
place = a[place - 1] # 次の行き先
# 周期: 同じ点を2回通るまでに要した移動回数 - 周期に入るまでの移動回数
cycle = len(path) - order[place]
before_cycle_count = order[place]
if (k < before_cycle_count):
print(path[k])
else:
k -= before_cycle_count
k %= cycle # 周期: 途中の繰り返し部分を省いて、途中の部分だけ計算するようにする
print(path[before_cycle_count + k])
if __name__ == '__main__':
main()
| 1 | 22,725,282,191,920 | null | 150 | 150 |
import math
n = int(input())
debt = 100000
for _ in range(n):
debt = debt * 1.05
debt = math.ceil(debt / 1000) * 1000
print(debt)
|
text = ""
ans = [0 for i in range(26)]
while True:
try:
text += raw_input().lower()
except:
for s in text:
if s.isalpha():
ans[ord(s)-97] += 1
for j in range(len(ans)):
print "{} : {}".format(chr(j+97),ans[j])
break
| 0 | null | 812,401,921,498 | 6 | 63 |
a,b=open(0);c=1;
for i in sorted(b.split()):
c*=int(i)
if c>10**18:print(-1);exit()
print(c)
|
#import numpy as np
#import math
#from decimal import *
#from numba import njit
#@njit
def main():
X = int(input())
p = []
def pow5(n):
nonlocal p
if len(p) > n:
return p[n]
ans = pow(n,5)
p += ans,
return ans
a = 0
b = 0
while True:
if abs(X) + pow5(b) == pow5(a) or abs(X) - pow5(b) == pow5(a) :
a5 = pow5(a)
b5 = pow5(b)
if X == a5 - b5:
print(a,b)
elif X == a5 + b5:
print(a, -b)
elif X == -a5 - b5:
print(-a, b)
else:
print(-a, -b)
return
while abs(X) + pow5(b) > pow5(a):
a += 1
if abs(X) + pow5(b) == pow5(a) or abs(X) - pow5(b) == pow5(a) :
a5 = pow5(a)
b5 = pow5(b)
if X == a5 - b5:
print(a,b)
elif X == a5 + b5:
print(a, -b)
elif X == -a5 - b5:
print(-a, b)
else:
print(-a, -b)
return
a = 0
b += 1
main()
| 0 | null | 20,945,537,507,740 | 134 | 156 |
while True:
x=input()
if x=="0":
break
cnt = 0
for i in x:
num = int(i)
cnt += num
print(cnt)
|
N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7
plus = []
zero = []
minus = []
ans = 1
for k in range(N):
if A[k] > 0:
plus.append(A[k])
elif A[k] < 0:
minus.append(-1 * A[k])
else:
zero.append(A[k])
plus.sort(reverse = True)
minus.sort(reverse = True)
lp = len(plus)
lm = len(minus)
lz = len(zero)
if lz + K > N:
print(0)
exit()
elif lp == 0:
if K % 2 == 1:
if lz > 0:
print(0)
exit()
else:
for k in range(K):
ans = ans * (-1) * minus[- k - 1] % mod
print(ans)
exit()
else:
for k in range(K):
ans = ans * (-1) * minus[k] % mod
print(ans)
exit()
elif lm == 0:
for k in range(K):
ans = ans * plus[k] % mod
print(ans)
exit()
p = 0
m = 0
for k in range(K):
if plus[p] >= minus[m]:
p += 1
if p == lp:
m = K - p
break
else:
m += 1
if m == lm:
p = K - m
break
def main(mp, mm, plus, minus):
mod = 10 ** 9 + 7
ans = 1
for k in range(mp):
ans = ans * plus[k] % mod
for k in range(mm):
ans = ans * (-1) * minus[k] % mod
print(ans)
exit()
if m % 2 == 0:
main(p, m, plus, minus)
else:
if p == lp:
if m != lm:
main(p - 1, m + 1, plus, minus)
else:
if lz > 0:
print(0)
exit()
else:
main(p, m, plus, minus)
elif m == lm:
main(p + 1, m - 1, plus, minus)
elif p == 0:
main(p + 1, m - 1, plus, minus)
else:
if plus[p] * plus[p - 1] > minus[m - 1] * minus[m]:
main(p + 1, m - 1, plus, minus)
else:
main(p - 1, m + 1, plus, minus)
| 0 | null | 5,539,072,920,832 | 62 | 112 |
n,k=map(int,input().split())
A=list(map(int,input().split()))
mod=10**9+7
flag=0
A.sort()
tmp1=1
tmp2=1
l=0
r=n-1
if k%2==1:
tmp1=A[-1]%mod
r-=1
if A[-1]<0:
flag=1
for i in range(k//2):
vl=A[l]*A[l+1]
vr=A[r]*A[r-1]
if max(vl,vr)<0:
flag=1
if vl>vr:
l+=2
tmp1*=vl
else:
r-=2
tmp1*=vr
tmp1%=mod
from bisect import bisect_right
idx=bisect_right(A,0)
if idx==n:
idx-=1
l=max(0,idx-1)
r=idx
for i in range(k):
vl=A[l] if l>=0 else mod
vr=A[r] if r<n else mod
if abs(vl)<abs(vr) or r==n:
l-=1
tmp2*=vl
else:
r+=1
tmp2*=vr
tmp2%=mod
if flag==0:
print(tmp1)
else:
print(tmp2)
|
while True:
[H,W]=[int(x) for x in input().split()]
if [H,W]==[0,0]:
break
unit="#."
for i in range(0,H):
print(unit*(W//2)+unit[0]*(W%2))
unit=unit[1]+unit[0]
print("")
| 0 | null | 5,125,861,319,680 | 112 | 51 |
def comb(n,r,m):
if 2*r > n:
r = n - r
nume,deno = 1,1
for i in range(1,r+1):
nume *= (n-i+1)
nume %= m
deno *= i
deno %= m
return (nume * pow(deno,m-2,m)) % m
def main():
N,M,K = map(int,input().split())
mod = 998244353
ans,comb_r = pow(M-1,N-1,mod),1
for r in range(1,K+1):
comb_r = (comb_r * (N-r) * pow(r,mod-2,mod)) % mod
ans = (ans + comb_r * pow(M-1,N-r-1,mod)) % mod
ans = (ans * M) % mod
print(ans)
if __name__ == "__main__":
main()
|
import math
import sys
while True:
try:
a,b = map(int, input().split())
print(len(str(a+b)))
except EOFError:
break
| 0 | null | 11,624,645,880,068 | 151 | 3 |
import itertools
# H, W = [int(_) for _ in input().split()]
N = int(input())
comb = itertools.combinations_with_replacement([str(i) for i in range(1, 10)], 2)
count = 0
for c in comb:
A = c[0]
B = c[1]
count_1 = 0
count_2 = 0
for i in range(1, N+1):
if str(i)[0] == A and str(i)[-1] == B:
count_1 += 1
elif str(i)[0] == B and str(i)[-1] == A:
count_2 += 1
if A == B:
count += count_1 ** 2
else:
count += count_1 * count_2 * 2
print(count)
|
S = input()
K = int(input())
ss = []
seq = 1
for a,b in zip(S,S[1:]):
if a==b:
seq += 1
else:
ss.append(seq)
seq = 1
ss.append(seq)
if len(ss)==1:
print(len(S)*K//2)
exit()
if S[0] != S[-1]:
ans = sum([v//2 for v in ss]) * K
print(ans)
else:
ans = sum([v//2 for v in ss[1:-1]]) * K
ans += (ss[0]+ss[-1])//2 * (K-1)
ans += ss[0]//2 + ss[-1]//2
print(ans)
| 0 | null | 130,814,870,678,854 | 234 | 296 |
N = int(input())
a = list(map(int, input().split()))
ans = 0
h = a[0]
for i in range(1,N,1):
if h < a[i]:
h = a[i]
else:
ans += (h - a[i])
print(ans)
|
N = int(input())
A = list(map(int,input().split()))
ans = 0
for i in range(N-1):
next = i + 1
if A[i] > A[next]:
a =A[i] - A[next]
ans += a
A[next] += a
else:
pass
print(ans)
| 1 | 4,599,949,411,200 | null | 88 | 88 |
def insertion_sort(seq):
print(' '.join(map(str, seq)))
for i in range(1, len(seq)):
key = seq[i]
j = i - 1
while j >= 0 and seq[j] > key:
seq[j+1] = seq[j]
j -= 1
seq[j+1] = key
print(' '.join(map(str, seq)))
return seq
n = int(input())
seq = list(map(int, input().split()))
insertion_sort(seq)
|
from __future__ import division, print_function, unicode_literals
from future_builtins import *
N = int(raw_input())
A = list(map(int, raw_input().split()))
print(" ".join(map(str, A)))
for i in xrange(1,N):
j, p = i-1, A[i]
while j >= 0 and A[j] > p:
A[j+1] = A[j]
j -= 1
A[j+1] = p
print(" ".join(map(str, A)))
| 1 | 5,309,729,178 | null | 10 | 10 |
a = int(input())
num = a //1000
b = num*1000 - a
if b < 0:
c = (num+1)*1000-a
print(c)
else:
print(b)
|
N,K=map(int,input().split())
A=[int(x)-1 for x in input().split()]
seen={0}
town=0
while K>0:
town=A[town]
K-=1
if town in seen:
break
seen.add(town)
start=town
i=0
while K>0:
town=A[town]
i+=1
K-=1
if town is start:
break
K=K%i if i>0 else 0
while K>0:
town=A[town]
i+=1
K-=1
print(town+1)
| 0 | null | 15,540,308,777,800 | 108 | 150 |
alpha = "abcdefghijklmnopqrstuvwxyz"
A = [0]*26
while True :
try :
n = input()
for i in range(len(n)) :
if n[i] in alpha or n[i].lower() in alpha :
A[alpha.index(n[i].lower())] += 1
except :
break
for j in range(len(A)) :
print(alpha[j], ":", A[j])
|
text = ''
while True:
try:
s = input().lower()
except EOFError:
break
if s == '':
break
text += s
for i in 'abcdefghijklmnopqrstuvwxyz':
print(i+' : '+str(text.count(i)))
#print("{0} : {1}".format(i, text.count(i)))
| 1 | 1,670,117,822,536 | null | 63 | 63 |
a = [int(i) for i in input().split()]
if(max(a) > 9):
print("-1")
else:
print(a[0] * a[1])
|
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LS2(): return list(sys.stdin.readline().rstrip()) #空白なし
N = I()
S = LS2()
ans = 0
for i in range(N-2):
if S[i] == 'A' and S[i+1] == 'B' and S[i+2] == 'C':
ans += 1
print(ans)
| 0 | null | 128,835,088,229,648 | 286 | 245 |
while True:
n, x = map(int, input().split())
if not n and not x:
break
li = []
for i in range(1,x//3):
y = x - i
li.append((y-1)//2 - max(i, y-n-1))
print(sum([comb for comb in li if comb > 0]))
|
m = 0
while m == 0:
n, x = map(int,raw_input().split())
count = 0
if n == 0 and x == 0:
break
for i in xrange(1,n+1):
for j in xrange(1,n+1):
for k in xrange(1,n+1):
ans = i + j + k
if ans == x and i != j and j != k and k != i:
count = count + 1
print count / 6
| 1 | 1,280,602,642,332 | null | 58 | 58 |
n=int(input())
al='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ans=[]
s=input()
for i in range(len(s)):
ans.append(al[(ord(s[i])-65+n)%26])
print(''.join(ans))
|
n = int(input())
a = input()
ans = ""
for i in a:
ans += chr(65 + (ord(i) - 65 + n )%26)
print(ans)
| 1 | 133,935,878,848,132 | null | 271 | 271 |
s=input()
t=input()
count_min = 1000
for i in range(len(s)-len(t)+1):
count = 0
for j in range(len(t)):
if s[i+j] != t[j]:
count+=1
if count < count_min:
count_min = count
print(count_min)
|
s=input()
ans='Yes'
a=s[0]
b=s[1]
c=s[2]
if a==b and b==c and a==c:
ans='No'
print(ans)
| 0 | null | 29,222,033,757,182 | 82 | 201 |
from functools import reduce
x,y=list(map(int,input().split()))
mod = 10 ** 9 + 7
if (2 * y - x) % 3 != 0 or (2 * x - y) % 3 != 0:
print("0")
exit()
a,b = (2 * y - x) // 3, (2 * x - y) // 3
r = max(a,b)
if min(a,b) < 0:
print("0")
else:
numerator = reduce(lambda x, y: x * y % mod, range(a + b - r + 1, a + b + 1))
denominator = reduce(lambda x, y: x * y % mod, range(1 , r + 1))
print(numerator * pow(denominator, mod - 2, mod) % mod)
|
class Node(object):
def __init__(self, num, prv = None, nxt = None):
self.num = num
self.prv = prv
self.nxt = nxt
class DoublyLinkedList(object):
def __init__(self):
self.start = self.last = None
def insert(self, num):
new_elem = Node(num)
if self.start is None:
self.start = self.last = new_elem
else:
new_elem.nxt = self.start
self.start.prv = new_elem
self.start = new_elem
def delete_num(self, target):
it = self.start
while it is not None:
if it.num == target:
if it.prv is None and it.nxt is None:
self.start = self.last = None
else:
if it.prv is not None:
it.prv.nxt = it.nxt
else:
self.start = self.start.nxt
if it.nxt is not None:
it.nxt.prv = it.prv
else:
self.last = self.last.prv
break
it = it.nxt
def delete_start(self):
if self.start is self.last:
self.start = self.last = None
else:
self.start.nxt.prv = None
self.start = self.start.nxt
def delete_last(self):
if self.start is self.last:
self.start = self.last = None
else:
self.last.prv.nxt = None
self.last = self.last.prv
def get_content(self):
ret = []
it = self.start
while it is not None:
ret.append(it.num)
it = it.nxt
return ' '.join(ret)
def _main():
from sys import stdin
n = int(input())
lst = DoublyLinkedList()
for _ in range(n):
cmd = stdin.readline().strip().split()
if cmd[0] == 'insert':
lst.insert(cmd[1])
elif cmd[0] == 'delete':
lst.delete_num(cmd[1])
elif cmd[0] == 'deleteFirst':
lst.delete_start()
elif cmd[0] == 'deleteLast':
lst.delete_last()
print(lst.get_content())
if __name__ == '__main__':
_main()
| 0 | null | 74,659,826,055,770 | 281 | 20 |
K = int(input())
S = input()
if len(S) <= K:
print(S)
else:
print(S[0:K] + '...')
|
s = input()
print("Yes" if s[2] == s[3] and s[4] == s[5] else "No")
| 0 | null | 30,874,399,006,824 | 143 | 184 |
import sys
from collections import Counter
N = int(sys.stdin.readline().rstrip())
D = list(map(int, sys.stdin.readline().rstrip().split()))
mod = 998244353
if D[0] != 0 or 0 in D[1:]:
print(0)
exit()
d_cnt = sorted(Counter(D).items())
ans = 1
par = 1
prev = 0
for k, v in d_cnt:
if k > 0:
if prev + 1 == k:
ans *= pow(par, v, mod)
ans %= mod
par = v
prev = k
else:
print(0)
exit()
print(ans)
|
import sys
read = lambda: sys.stdin.readline().rstrip()
def counting_tree(N, D):
tot = 1
# cnt = collections.Counter(D)
cnt = [0]*N
for i in D:
cnt[i]+=1
for i in D[1:]:
tot = tot * cnt[i-1]%998244353
return tot if D[0]==0 else 0
def main():
N0 = int(read())
D0 = tuple(map(int, read().split()))
res = counting_tree(N0, D0)
print(res)
if __name__ == "__main__":
main()
| 1 | 155,343,010,383,662 | null | 284 | 284 |
r=int(input())
print(r**2//1)
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
r = int(input())
print(r*r)
| 1 | 145,301,126,889,652 | null | 278 | 278 |
n=int(input())
a=list()
for i in range(n):
s,t=input().split()
a.append([s,int(t)])
x=input()
flag=False
ans=0
for i in a:
if flag:
ans+=i[1]
if i[0]==x:
flag=True
print(ans)
|
from collections import deque
K = int(input())
que = deque([1, 2, 3, 4, 5, 6, 7, 8, 9])
for i in range(K):
num = que.popleft()
if i == K-1:
print(num)
break
if num%10 == 0:
que.append(num*10 + num%10)
que.append(num*10 + num%10 + 1)
elif num%10 == 9:
que.append(num*10 + num%10 - 1)
que.append(num*10 + num%10)
else:
que.append(num*10 + num%10 - 1)
que.append(num*10 + num%10)
que.append(num*10 + num%10 + 1)
| 0 | null | 68,571,734,748,416 | 243 | 181 |
taka, aoki, num = map(int, input().split())
if taka <= num:
num -= taka
taka = 0
if aoki <= num:
aoki = 0
print(taka, aoki)
elif aoki > num:
aoki -= num
print(taka, aoki)
if taka > num:
taka -= num
print(taka, aoki)
|
N = int(input())
if N ==2:
print(1)
exit()
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==[]: #1の場合はここ
arr.append([n, 1])
return arr
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]
A = make_divisors(N)
ans = 0 #自分自身の一個を足しておく
SET = set([N])
#print(A)
for i in range(1,len(A)):
M = N*1
while M%A[i]==0:
M = M//A[i]
if M%A[i] == 1:
ans += 1
SET.add(A[i])
#ans = len(SET)
#print(SET)
#print(ans)
N -= 1
B = factorization(N)
yakusu = 1
#print(B)
for i in range(len(B)):
yakusu *= (B[i][1]+1)
#print(yakusu)
ans += yakusu-1
print(ans)
| 0 | null | 72,791,871,424,508 | 249 | 183 |
X,N = map(int, input().split())
if N == 0:
print(X)
exit()
P = list(map(int, input().split()))
P.sort(reverse=True)
for i in range(105):
if X - i not in P:
print(X - i)
break
if X + i not in P:
print(X + i)
break
|
MOD=10**9+7
N=int(input())
dp=[0]*(N+1)
dp[0]=1
for i in range(N+1):
for j in range(i-2):
dp[i]+=dp[j]
dp[i]%=MOD
print(dp[N])
| 0 | null | 8,731,957,844,416 | 128 | 79 |
def sumitrust2019_d():
n = int(input())
s = input()
ans = 0
for i in range(1000):
# 確認するpinの数字の組み合わせと桁の初期化
# pin = list(str(i).zfill(3))
current_digit = 0
pin = f'{i:0>3}'
search_num = pin[current_digit]
for num in s:
# 見つけると桁を右に一つずらす
# 毎回リストにアクセスするより変数に取り出しておくほうがいいかも。
# if num == pin[current_digit]:
if num == search_num:
current_digit += 1
# indexが3(桁が3→2→1→0)になったときはそのPINは作れる
if current_digit == 3:
ans += 1
break
# 見つけ終わるまではチェックする数字を置き換え。
elif current_digit < 3:
search_num = pin[current_digit]
print(ans)
if __name__ == '__main__':
sumitrust2019_d()
|
from collections import defaultdict
N=int(input())
S=input()
d = defaultdict(list)
for i in range(N):
d[int(S[i])].append(i)
ans = 0
for n1 in range(10):
if not d[n1]:continue
for n2 in range(10):
if not d[n2]:continue
if d[n1][0]>d[n2][-1]: continue
for n3 in range(10):
if not d[n3]:continue
if d[n1][0]>d[n3][-1] or d[n2][0]>d[n3][-1]:continue
for idx in d[n2]:
if idx>d[n1][0] and idx<d[n3][-1]:
ans += 1
break
print(ans)
| 1 | 128,379,685,712,892 | null | 267 | 267 |
n = list(map(int,input().split()))
ans = n[1] * n[3]
ans = max(n[0]*n[2],ans)
ans = max(n[0]*n[3],ans)
ans = max(n[1]*n[2],ans)
ans = max(n[1]*n[3],ans)
print(ans)
|
a,b,c,d=map(int,input().split())
A=a*c
B=a*d
C=b*c
D=b*d
N=[A,B,C,D]
print(max(N))
| 1 | 3,023,192,926,866 | null | 77 | 77 |
# -*- coding: utf-8 -*-
def check_numbers(cargo_weight_list, number_of_all_cargo, number_of_tracks, maximum_weight):
"""check the numbers that are loadable in tracks
Args:
cargo_weight_list: cargo weight list
number_of_tracks: the number of tracks
Returns:
the number of cargos that are loadable in tracks
"""
counter = 0
number_of_loaded_cargo = 0
while counter < number_of_tracks:
current_track_weight = 0
while current_track_weight + cargo_weight_list[number_of_loaded_cargo] <= maximum_weight:
current_track_weight += cargo_weight_list[number_of_loaded_cargo]
number_of_loaded_cargo += 1
if number_of_loaded_cargo == number_of_all_cargo:
return number_of_all_cargo
counter += 1
return number_of_loaded_cargo
def find_the_minimum_of_maximum_weihgt(cargo_weight_list, number_of_all_cargo, number_of_tracks):
"""find the minimum of maximum weight s.t all of the cargos can be loaded into tracks.
(binary search is used. the number of loadable cargos monotonicaly increases as the maximum weight increases)
Args:
cargo_weight_list: cargo weight list
numbef of all cargo: the number of all cargos
number of tracks: the number of tracks
Returns:
minumim number of maximum track weight that are needed to load all of the cargos
"""
left = max(cargo_weight_list)
right = sum(cargo_weight_list)
while (right - left) > 0:
middle = (right + left) / 2
the_number_of_loadable_cagos = check_numbers(cargo_weight_list, number_of_all_cargo, number_of_tracks, middle)
if the_number_of_loadable_cagos >= number_of_all_cargo:
right = middle
else:
left = middle + 1
return right
def main():
number_of_all_cargo, number_of_tracks = [int(x) for x in raw_input().split(' ')]
cargo_weight_list = [int(raw_input()) for _ in xrange(number_of_all_cargo)]
print find_the_minimum_of_maximum_weihgt(cargo_weight_list, number_of_all_cargo, number_of_tracks)
if __name__ == '__main__':
main()
|
n, k = map(int, raw_input().split())
w = []
max = 0
sum = 0
for i in xrange(n):
w_i = int(raw_input())
if w_i > max:
max = w_i
sum += w_i
w.append(w_i)
def can_put(w, p, k):
weight = 0
cnt = 1
for w_i in w:
if weight + w_i <= p:
weight += w_i
else:
cnt += 1
if cnt > k:
return 0
else:
weight = w_i
return 1
low = max
high = sum
while low < high:
mid = (low + high) / 2
#print high, mid, low
if can_put(w, mid, k) == 1:
#print("*1")
high = mid
else:
#print("*0")
low = mid + 1
#print high, mid, low
print high
| 1 | 83,599,986,052 | null | 24 | 24 |
n = int(input())
playList = []
sumTerm = 0
for i in range(n) :
song = input().split()
song[1] = int(song[1])
playList.append(song)
sumTerm += song[1]
x = input()
tmpSumTerm = 0
for i in range(n) :
tmpSumTerm += playList[i][1]
if playList[i][0] == x :
print(sumTerm - tmpSumTerm)
break
|
X, Y = map(int, input().split())
if (X+Y) % 3 != 0:
print(0)
else:
# 経路
def C(n, r, mod):
num = 1
den = 1
for i in range(r):
num *= n-i
num %= mod
den *= i+1
den %= mod
return (num * pow(den, mod-2, mod)) % mod
mod = 10**9 + 7
minimun = (X+Y)/3
if X >= minimun and Y >= minimun:
X -= minimun
Y -= minimun
print(C(int(X+Y), int(X), mod))
else:
print(0)
| 0 | null | 123,374,748,795,988 | 243 | 281 |
mod = 10**9 + 7
n, a, b = map(int, input().split())
res = pow(2, n, mod)-1
c1 = 1
for i in range(n-a+1, n+1):
c1 *= i
c1 %= mod
for i in range(1, a+1):
c1 *= pow(i, mod-2, mod)
c1 %= mod
c2 = 1
for i in range(n-b+1, n+1):
c2 *= i
c2 %= mod
for i in range(1, b+1):
c2 *= pow(i, mod-2, mod)
c2 %= mod
res -= (c1+c2)
res %= mod
print(res)
|
# -*- coding: utf-8 -*-
from collections import Counter, defaultdict, deque
import math
import sys
# sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline
INF = 2**62-1
def read_int():
return int(buff_readline())
def read_int_n():
return list(map(int, buff_readline().split()))
def read_float():
return float(buff_readline())
def read_float_n():
return list(map(float, buff_readline().split()))
def read_str():
return readline().strip()
def read_str_n():
return readline().strip().split()
def error_print(*args):
print(*args, file=sys.stderr)
def mt(f):
import time
def wrap(*args, **kwargs):
s = time.time()
ret = f(*args, **kwargs)
e = time.time()
error_print(e - s, 'sec')
return ret
return wrap
from fractions import Fraction
def f(a, b):
if a== 0 and b == 0:
return (1, 0, 0)
if b == 0:
return (1, 1, 0)
f = Fraction(a, b)
a = f.numerator
b = f.denominator
if a < 0:
s = -1
a *= -1
else:
s = 1
return s, a, b
class Mod:
def __init__(self, m):
self.m = m
def add(self, a, b):
return (a + b) % self.m
def sub(self, a, b):
return (a - b) % self.m
def mul(self, a, b):
return ((a % self.m) * (b % self.m)) % self.m
def div(self, a, b):
return self.mul(a, pow(b, self.m-2, self.m))
def pow(self, a, b):
return pow(a, b, self.m)
@mt
def slv(N, AB):
adb = Counter()
for a, b in AB:
s, a, b = f(a, b)
adb[(s*a, b)] += 1
M = Mod(1000000007)
ans = 1
done = set()
for k, v in adb.items():
if k in done:
continue
if k == (0, 0):
continue
a, b = k
if a < 0:
s = 1
a = abs(a)
elif a == 0:
s = 1
else:
s = -1
n = adb[(s*b, a)]
done.add(k)
done.add((s*b, a))
t = M.sub(M.pow(2, v) + M.pow(2, n), 1)
ans = M.mul(ans, t)
return M.add(M.sub(ans, 1), adb[(0, 0)])
def main():
N = read_int()
AB = [read_int_n() for _ in range(N)]
print(slv(N, AB))
if __name__ == '__main__':
main()
| 0 | null | 43,670,369,426,310 | 214 | 146 |
import numpy as np
import sys
input = sys.stdin.readline
def main():
n, s = map(int, input().split())
A = np.array([int(i) for i in input().split()])
MOD = 998244353
dp = np.zeros(s + 1, dtype="int32")
dp[0] = 1
for i in range(n):
p = (dp * 2) % MOD
p %= MOD
p[A[i]:] += dp[:-A[i]]
dp = p % MOD
print(dp[s])
if __name__ == '__main__':
main()
|
# -*- coding:utf-8 -*-
from collections import deque
result = deque()
def operation(command):
if command[0] == "insert":
result.appendleft(command[1])
elif command[0] == "delete":
if command[1] in result:
result.remove(command[1])
elif command[0] == "deleteFirst":
result.popleft()
elif command[0] == "deleteLast":
result.pop()
n = int(input())
for i in range(n):
command = input().split()
operation(command)
print(*result)
| 0 | null | 8,952,535,446,500 | 138 | 20 |
T=str(input())
T2=list(T)
if T2[0]=="?":
T2[0]="D"
if T2[len(T)-1]=="?":
T2[len(T)-1]="D"
for i in range(1,len(T2)-1):
if T2[i]=="?":
if T2[i-1]=="D" and T2[i+1]!="P":
T2[i]="P"
else:
T2[i]="D"
ans="".join(T2)
print(ans)
|
import itertools
import decimal
import math
import collections
import sys
input = sys.stdin.readline
T=input()
T=T.replace('?','D')
print(T)
| 1 | 18,370,285,102,630 | null | 140 | 140 |
import sys
x,n = map(int,input().split())
p = list(map(int,input().split()))
if p.count(x) == 0:
print(x)
else:
for i in range(1,110):
if p.count(x-i) == 0:
print(x-i)
sys.exit()
elif p.count(x+i) == 0:
print(x+i)
sys.exit()
|
f = lambda x: x if p[x]<0 else f(p[x])
N,M = map(int,input().split())
p = [-1]*N
for _ in range(M):
A,B = map(lambda x:f(int(x)-1),input().split())
if A==B: continue
elif A<B: A,B=B,A
p[A] += p[B]
p[B] = A
print(sum(i<0 for i in p)-1)
| 0 | null | 8,201,859,642,340 | 128 | 70 |
import math
s=input()
count=0
for i in range(0,math.floor(len(s)/2)):
if s[i]!=s[len(s)-i-1]:
count=count+1
print(count)
|
a,b,c,d = map(int, input().split())
if a*b <= 0 and c*d <= 0:
print(max([a*c, b*d]))
elif a*b <= 0 and c >= 0:
print(b*d)
elif a*b <=0 and d <=0:
print(a*c)
elif a>=0 and c*d<=0:
print(b*d)
elif a>=0 and c>=0:
print(b*d)
elif a>=0 and d<=0:
print(a*d)
elif b<=0 and c*d<=0:
print(a*c)
elif b<=0 and c>=0:
print(b*c)
elif b<=0 and d<=0:
print(a*c)
| 0 | null | 61,693,807,454,158 | 261 | 77 |
n=int(input())
ans=0
d=5
for i in range(36):
ans+=n//d//2
d*=5
print(0 if n%2 else ans)
|
import sys, math
from functools import lru_cache
from collections import deque
sys.setrecursionlimit(500000)
MOD = 10**9+7
def input():
return sys.stdin.readline()[:-1]
def mi():
return map(int, input().split())
def ii():
return int(input())
def i2(n):
tmp = [list(mi()) for i in range(n)]
return [list(i) for i in zip(*tmp)]
def main():
S = input()
for i in range(1, 6):
if S == 'hi'*i:
print('Yes')
return
print('No')
if __name__ == '__main__':
main()
| 0 | null | 84,758,211,752,222 | 258 | 199 |
n,m=map(int,input().split())
ac_cnt = set()
wa_cnt = 0
penalty = [0]*n
for i in range(m):
p,s = input().split()
num = int(p) - 1
if num not in ac_cnt:
if s == "AC":
ac_cnt.add(num)
wa_cnt += penalty[num]
else:
penalty[num] += 1
print(len(set(ac_cnt)),wa_cnt)
|
n, m = map(int, input().split())
ans = [0, 0]
ac = [0 for _ in range(n)]
for _ in range(m):
p, s = input().split()
p = int(p) - 1
if ac[p] == -1:
continue
elif s == "AC":
ans[0] += 1
ans[1] += ac[p]
ac[p] = -1
else:
ac[p] += 1
print(*ans)
| 1 | 93,378,151,054,540 | null | 240 | 240 |
def dfs(S, mx):
if len(S) == N:
print(S)
else:
for i in range(a, mx+2):
if i == mx+1:
dfs(S+chr(i), mx+1)
else:
dfs(S+chr(i), mx)
return
N = int(input())
a = ord('a')
dfs('a', a)
|
n = int(input())
def dfs(i,s):
if i == n:
print(s)
return
ma = 0
for j in s:
ma = max(ma,ord(j))
for j in range(ma - 95):
dfs(i+1,s+chr(97+j))
return
dfs(1,"a")
| 1 | 52,367,103,457,262 | null | 198 | 198 |
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
S = input()
if not S.islower():sys.exit()
if not ( 3 <= len(S) <= 99 and len(S) % 2 == 1 ): sys.exit()
# whole check
first = int((len(S)-1)/2)
F = S[0:first]
last = int((len(S)+3)/2)
L = S[last-1:]
condition = 0
if S == S[::-1]:
condition += 1
if F == F[::-1]:
condition += 1
if L == L[::-1]:
condition += 1
print('Yes') if condition == 3 else print('No')
| 1 | 46,393,348,729,632 | null | 190 | 190 |
n = int(input())
d = set()
for _ in range(n):
command = input()
if 'i' == command[0]:
_, word = command.split()
d.add(word)
else:
_, word = command.split()
print('yes' if word in d else 'no')
|
n=int(input())
A=set()
for i in range(n):
x,y=input().split()
if x == 'insert':
A.add(y)
else:
print('yes'*(y in A)or'no')
| 1 | 79,103,542,022 | null | 23 | 23 |
# import itertools
# import math
# import sys
# sys.setrecursionlimit(500*500)
# import numpy as np
# import heapq
# from collections import deque
N = int(input())
# S = input()
# n, *a = map(int, open(0))
# N, M = map(int, input().split())
A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()
# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)
# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)
# def dfs(tree, s):
# for l in tree[s]:
# if depth[l[0]] == -1:
# depth[l[0]] = depth[s] + l[1]
# dfs(tree, l[0])
# dfs(tree, 1)
# 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
A.insert(0, 0)
cnt = [0] * (N + 1)
ans = 0
for i in range(1, N + 1):
if i - A[i] >= 0:
ans += cnt[i - A[i]]
if i + A[i] <= N:
cnt[i + A[i]] += 1
print(ans)
|
n = int(input())
A = [0]+list(map(int,input().split()))
d = {}
ans = 0
for i in range(1,n+1):
if i+A[i] not in d:
d[i+A[i]] = 1
else:
d[i+A[i]] += 1
if i-A[i] in d:
ans += d[i-A[i]]
print(ans)
| 1 | 26,115,123,697,432 | null | 157 | 157 |
N,S=map(int,input().split())
A=list(map(int,input().split()))
mod=998244353
DP=[0]*(S+1)
DP[0]=pow(2,N,mod)
INV=pow(2,mod-2,mod)
for a in A:
for i in range(S-a,-1,-1):
DP[i+a]=(DP[i]*INV+DP[i+a])%mod
print(DP[S])
|
#!/usr/bin/env python3
import sys
MOD = 998244353 # type: int
class ModOperator:
def __init__(self, MOD: int = 10 ** 9 + 7):
self.MOD = MOD
self.factorials = [1, 1]
# x^n % MOD
def mod_pow(self, x: int, n: int):
bi = str(format(n, "b"))
res = 1
a = x
for i in range(len(bi)):
if n >> i & 1:
res = (res * a) % self.MOD
a = (a * a) % self.MOD
return res
# x! % MOD
def mod_fact(self, x: int):
if x < len(self.factorials):
return self.factorials[x]
else:
res = self.factorials[-1]
for i in range(len(self.factorials), x + 1):
res = res * i % self.MOD
self.factorials.append(res)
return res % self.MOD
# inverse of a number
def mod_inv(self, x: int):
return self.mod_pow(x, self.MOD - 2)
# conmination nCk
def mod_comb(self, n: int, k: int):
numerator = self.mod_fact(n)
denomintator1 = self.mod_fact(k)
denomintator2 = self.mod_fact(n - k)
denomintator = denomintator1 * denomintator2 % self.MOD
return numerator * self.mod_inv(denomintator) % self.MOD
def main():
N, M, K = map(int, input().split())
op = ModOperator(MOD)
sum = 0
for i in range(K + 1):
sum += M * op.mod_pow(M - 1, N - 1 - i) * op.mod_comb(N - 1, i)
print(sum % MOD)
if __name__ == '__main__':
main()
| 0 | null | 20,382,119,711,240 | 138 | 151 |
import os, sys, re, math
(K, X) = (int(n) for n in input().split())
if 500 * K >= X:
print('Yes')
else:
print('No')
|
a, b = list(map(int, input().split()))
if a * 500 >= b:
my_result = 'Yes'
else:
my_result = 'No'
print(my_result)
| 1 | 97,859,048,533,948 | null | 244 | 244 |
A,B=map(int,input().split())
if A>=10 or B>=10:
print('-1')
else:
print(A*B)
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
a,b = map(int, input().split())
if a >= 1 and a <= 9 and b >= 1 and b <= 9:
print(a*b)
else:
print(-1)
| 1 | 158,432,346,063,730 | null | 286 | 286 |
line = input()
q = int(input())
for _ in range(q):
cmd = input().split()
a = int(cmd[1])
b = int(cmd[2])
if cmd[0] == "print":
print(line[a:b + 1])
elif cmd[0] == "replace":
line = line[:a] + cmd[3] + line[b + 1:]
else:
line = line[:a] + line[a:b + 1][::-1] + line[b + 1:]
|
#70 C - 100 to 105 WA
X = int(input())
dp = [0]*(100010)
lst = [100,101,102,103,104,105]
for i in range(100,106):
dp[i] = 1
# dp[v] が存在するなら 1, しないなら 0
for v in range(100,X+1):
for w in range(6):
n = lst[w]
if dp[v-n] == 1:
dp[v] = 1
print(dp[X])
| 0 | null | 64,996,706,929,750 | 68 | 266 |
while True:
H, W = [int(x) for x in input().split() if x.isdigit()]
if H ==0 and W == 0:
break
else:
for i in range(H):
print("#", end='')
if i == 0 or i == H-1:
for j in range(W-2):
print("#", end='')
else:
for j in range(W-2):
print(".", end='')
print("#")
print("")
|
a, b = input().split()
print(['No','Yes'][a==b])
| 0 | null | 41,860,589,651,452 | 50 | 231 |
from collections import Counter
N, P = map(int, input().split())
Ss = input()
if P == 2 or P == 5:
ans = 0
for i, S in enumerate(Ss):
if int(S)%P == 0:
ans += i+1
else:
As = [0]
A = 0
D = 1
for S in Ss[::-1]:
S = int(S)
A = (A + S*D) % P
As.append(A)
D = D*10 % P
cnt = Counter()
ans = 0
for A in As:
ans += cnt[A]
cnt[A] += 1
print(ans)
|
n,p = tuple(int(x) for x in input().split())
s = [int(s) for s in input()][::-1]
modlist = [0]*p
modlist[0] = 1
if p!= 2 and p != 5:
mod = 0
pwrmod = 1
for i in s:
mod = (mod + pwrmod * i) % p
pwrmod = (pwrmod * 10) % p
modlist[mod] += 1
ans = 0
for i in modlist:
ans += (i * (i-1)) // 2
print(ans)
else:
ans = 0
for i in range(n):
if s[i] % p == 0:
ans += n-i
print(ans)
| 1 | 58,010,509,108,850 | null | 205 | 205 |
n, x, m = map(int, input().split())
a = []
check = [-1] * m
i = 0
while check[x] == -1:
a.append(x)
check[x] = i
x = x * x % m
i += 1
if n <= i:
print(sum(a[:n]))
else:
print(
sum(a[:check[x]])
+ (n - check[x]) // (i - check[x]) * sum(a[check[x]:])
+ sum(a[check[x] : check[x] + (n - check[x]) % (i - check[x])]))
|
n=int(input())
s=input()
if n%2==1:
print("No")
exit()
half=n//2
if s[:half]==s[half:]:
print("Yes")
else:
print("No")
| 0 | null | 75,088,943,976,188 | 75 | 279 |
def selection(a, n):
cnt = 0
for i in range(n):
flag = 0
minj = i
for j in range(i+1,n):
if a[j] < a[minj]:
minj = j
flag = 1
if flag == 1:
temp = a[minj]
a[minj] = a[i]
a[i] = temp
cnt += 1
return cnt
n = int(input())
a = list(map(int, input().split()))
cnt = selection(a, n)
ans = str(a[0])
for i in range(1,n):
ans += ' '+str(a[i])
print(ans)
print(cnt)
|
def selection_sort(seq):
l = len(seq)
cnt = 0
for i in range(l):
mi = i
for j in range(i+1, l):
if seq[j] < seq[mi]:
mi = j
if i is not mi:
seq[i], seq[mi] = seq[mi], seq[i]
cnt += 1
return seq, cnt
n = int(input())
a = list(map(int, input().split()))
sorted_a, num_swap = selection_sort(a)
print(' '.join(map(str, sorted_a)))
print(num_swap)
| 1 | 19,018,155,830 | null | 15 | 15 |
import sys
for i in sys.stdin:
n,x = map(int,i.split())
s = 0
if n==0 and x==0:
break
if x <= 3*n-3 and x >= 6:
for i in range(1,min(n-1,x//3)):
s += len(range(max(i+1,x-i-n),min(n,(x-i+1)//2)))
print(s)
|
from itertools import combinations as comb
def get_ans(m, n):
ans = 0
for nums in comb(list(range(1, min(m+1,n-2))), 3):
if sum(nums) == n:
ans += 1
print(ans)
while True:
m, n = (int(x) for x in input().split())
if m==0 and n==0:
quit()
get_ans(m, n)
| 1 | 1,309,585,380,180 | null | 58 | 58 |
# 逆元を利用した組み合わせの計算
#############################################################
def cmb(n, r, mod):
if (r < 0 or r > n):
return 0
r = min(r, n - r)
return g1[n] * g2[r] * g2[n - r] % mod
mod = 10**9 + 7
NN = 10**6
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]
for i in range(2, NN + 1):
g1.append((g1[-1] * i) % mod)
inverse.append((-inverse[mod % i] * (mod // i)) % mod)
g2.append((g2[-1] * inverse[-1]) % mod)
#############################################################
N, K = map(int, input().split())
# M = 空き部屋数の最大値
M = min(N - 1, K)
"""
m = 0,1,2,...,M
N部屋から、空き部屋をm個選ぶ
-> N_C_m
残りの(N-m)部屋に、N人を配置する(空き部屋が出来てはいけない)
まず、(N-m)部屋に1人ずつ配置し、残ったm人を(N-m)部屋に配置する
これは、m個のボールと(N-m-1)本の仕切りの並び替えに相当する
-> (m+N-m-1)_C_m = N-1_C_m
"""
ans = 0
for m in range(0, M + 1):
ans += cmb(N, m, mod) * cmb(N - 1, m, mod)
ans %= mod
print(ans)
|
while True:
table=str(input())
if table[0] == '0':
break
print(sum(int(num) for num in(table)))
| 0 | null | 34,537,066,820,640 | 215 | 62 |
# -*- coding:utf-8 -*-
def bubble_sort(num_list):
swap_count = 0
for i in range(len(num_list)):
for j in range(len(num_list)-1, i , -1):
if num_list[j] < num_list[j-1]:
num_list[j], num_list[j-1] = num_list[j-1], num_list[j]
swap_count = swap_count + 1
return num_list, swap_count
def show_list(list):
i = 0;
while(i < len(list)-1):
print(list[i], end=" ")
i = i + 1
print(list[i])
num = int(input())
num_list = [int(x) for i, x in enumerate(input().split()) if i < num]
num_list, swap_count = bubble_sort(num_list)
show_list(num_list)
print(swap_count)
|
n = int(input())
print('{:.10f}'.format(1-(n//2 / n)))
| 0 | null | 88,439,482,183,302 | 14 | 297 |
n, m = map(int, input().split())
c = sorted([int(i) for i in input().split()])
dp = [[0 for j in range(n+1)] for i in range(m+1)]
dp[1] = [int(i) for i in range(n+1)]
for i in range(2, m+1):
for j in range(n+1):
if j - c[i-1] > -1:
dp[i][j] = min(dp[i-1][j], dp[i][j-c[i-1]]+1)
else:
dp[i][j] = dp[i-1][j]
print(dp[m][n])
|
import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect
MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(10 ** 8)
n, m = map(int, input().split())
c = list(map(int, input().split()))
dp = [[INF] * (n + 10) for _ in range(m + 10)]
for i in range(m+10):
dp[i][0] = 0
for i in range(1, n + 1):
if i % c[0] == 0:
dp[1][i] = i // c[0]
for i in range(2, m + 1):
for j in range(1, n + 1):
if j - c[i - 1] >= 0:
dp[i][j] = min(dp[i-1][j], dp[i][j - c[i - 1]] + 1)
else:
dp[i][j] = dp[i-1][j]
print(dp[m][n])
| 1 | 136,290,468,678 | null | 28 | 28 |
import sys
num = int(sys.stdin.readline())
print(num**3)
|
n = int(input())
print(n ** 3)
| 1 | 276,388,785,638 | null | 35 | 35 |
A, B, C, K = map(int, input().split())
a = min(A, K)
K -= a
b = min(B, K)
K -= b
c = min(C, K)
print(a-c)
|
def resolve():
A, B, C, K = map(int, input().split())
if K < A:
ans = K
elif (A + B) >= K:
ans = A
else:
ans = A - (K - A - B)
print(ans)
if __name__ == "__main__":
resolve()
| 1 | 21,702,234,236,462 | null | 148 | 148 |
N,K=map(int,input().split())
A=list(map(int,input().split()))
m,p,z=[],[],0
for a in A:
if a<0:
m.append(a)
elif a>0:
p.append(a)
else:
z+=1
m.sort()
p.sort()
mod=10**9+7
from functools import reduce
if (len(p) or K%2==0) and K<len(m)+len(p) or K==len(m)+len(p) and len(m)%2==0:
r=1
i,j=0,0
while i+j<K:
if i<len(m)-1 and i+j<K-1 and (j>=len(p)-1 or m[i]*m[i+1]>p[-j-1]*p[-j-2]):
r=r*m[i]*m[i+1]%mod
i+=2
else:
r=r*p[-j-1]%mod
j+=1
print(r)
elif z:
print(0)
else:
print(reduce(lambda x,y:x*y%mod,(m+p)[-K:]))
|
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
mia = sorted([i for i in a if i < 0], reverse=True)
pla = sorted([i for i in a if i >= 0])
ans, num = 1, []
if len(pla) == 0 and k % 2 == 1:
for i in mia[:k]:
ans = ans * i % mod
print(ans)
exit()
for _ in range(k):
if len(mia) > 0 and len(pla) > 0:
if abs(mia[-1]) <= pla[-1]:
tmp = pla.pop()
elif abs(mia[-1]) > pla[-1]:
tmp = mia.pop()
elif len(mia) == 0:
tmp = pla.pop()
elif len(pla) == 0:
tmp = mia.pop()
num.append(tmp)
cnt = sum(i < 0 for i in num)
for i in num:
ans = ans * i % mod
if cnt % 2 == 1:
p, q, r, s = 1, 0, -1, 0
if len(mia) > 0 and cnt != k:
p,q = min(i for i in num if i >= 0), mia[-1]
if len(pla) > 0:
for i in num[::-1]:
if i < 0:
r, s = i, pla[-1]
break
if len(mia) > 0 or (len(pla) > 0 and cnt != k):
if q * r >= p * s:
ans *= q * pow(p, mod - 2, mod)
if q * r < p * s:
ans *= s * pow(r, mod - 2, mod)
ans %= mod
print(ans)
| 1 | 9,436,770,848,380 | null | 112 | 112 |
a, b, c = eval(input().replace(' ', ','))
print("Yes" if a < b < c else "No")
|
from sys import stdin
import sys
import math
from functools import reduce
import itertools
n = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
if len(set(a)) == n:
print('YES')
else:
print('NO')
| 0 | null | 37,018,766,824,522 | 39 | 222 |
class UnionFind:
def __init__(self,n):
self.par = [-1] * n # 負数は友達の数(自分を含む)初期値は自分-1、0と正数は根を示す
def root(self,x): # xの根を返す
if self.par[x] < 0: # xが根の時は、xを返す
return x
self.par[x] = self.root(self.par[x]) # 根でないときは、par[x]に根の根を代入
return self.par[x] # 根の根を返す
def union(self,x,y):
x = self.root(x)
y = self.root(y)
if x == y:
return
if self.par[x] > self.par[y]: # par[x]が小さく(友達が多く)なるように交換
x, y = y, x
self.par[x] += self.par[y] # 友達が多い方に少ない方を加える
self.par[y] = x # yの根をxにする
def size(self):
return -1 * min(self.par)
n, m = map(int, input().split())
uf = UnionFind(n)
for _ in range(m):
a, b = map(int, input().split())
uf.union(a-1,b-1) # 1が0番目、2が1番目・・・
print(uf.size())
|
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
for i in range(1, 4):
nums = [(abs(a - b)) ** i for a, b in zip(A,B)]
print(sum(nums) ** (1/i))
if i == 1:
ans = max(nums)
print(ans)
| 0 | null | 2,082,885,649,920 | 84 | 32 |
def main():
from collections import deque
import sys
input = sys.stdin.readline
N,M = map(int,input().split())
to = [[] for _ in range(N)]
for _ in range(M):
a,b = map(int,input().split())
a -= 1
b -= 1
to[a].append(b)
to[b].append(a)
# print(to)
guide = [0] * N
guide[0] = 1
q = deque()
q.append(0)
while len(q) > 0:
u = q.popleft()
for v in to[u]:
# print(guide)
if guide[v] != 0:
continue
guide[v] = u+1
q.append(v)
print("Yes")
for g in guide[1:]:
print(g)
if __name__ == '__main__':
main()
|
from collections import deque
n, m = map(int, input().split())
eg = [[] for _ in range(n + 1)]
for _ in range(m):
a, b = map(int, input().split())
eg[a].append(b)
eg[b].append(a)
xs = [0] * (n + 1)
q = deque()
q.append(1)
seen = {1}
while len(q) > 0:
v = q.popleft()
for t in eg[v]:
if t in seen:
continue
q.append(t)
seen.add(t)
xs[t] = v
print("Yes")
print(*xs[2:], sep="\n")
| 1 | 20,493,273,035,760 | null | 145 | 145 |
while True:
[h, w] = map(int, (input().split()))
if h==0 and w==0:
break
else:
print(("#"*w+"\n")*h)
|
while 1:
H, W = map(int, raw_input().split())
if H == W == 0:
break
for i in range(H):
print "#" * W
print ""
| 1 | 771,565,881,318 | null | 49 | 49 |
A,V=map(int,input().split())
B,W=map(int,input().split())
T=int(input())
if V<=W or (abs(A-B)/(V-W))>T:
print("NO")
else:
print("YES")
|
A, V = map(int, input().split())
B, W = map(int, input().split())
T = int(input())
a = abs(B-A)
if T * (V-W) >= a:
print("YES")
else:print("NO")
| 1 | 15,104,070,113,050 | null | 131 | 131 |
from math import sqrt
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
p_1 = 0
p_2 = 0
p_3 = 0
p_inf = 0
for i, j in zip(x, y):
p_1 += abs(i - j)
p_2 += (i - j) ** 2
p_3 += abs((i - j)) ** 3
p_inf = max(p_inf, abs(i - j))
print(p_1)
print(sqrt(p_2))
print(p_3 ** (1 / 3))
print(p_inf)
|
input_line = set([])
input_line.add(int(input()))
input_line.add(int(input()))
for i in range(1,4):
if i not in input_line:
print(i)
| 0 | null | 55,320,423,514,488 | 32 | 254 |
a, b, h, m = map(int,input().split())
angle = 30*h - 5.5*m
import math
ans = (a*a + b*b - 2*a*b*math.cos(math.radians(angle)))**0.5
print(ans)
|
import math
a, b, h, m = map(int,input().split())
ms = m//60
ma = m%60
h = h + ms
ha =h%12 + ma/60
rh = ha*math.pi/6
rm = ma*math.pi/30
x = math.cos(rh - rm)
ans = math.sqrt(a**2 + b**2 - 2*a*b*x)
print(ans)
| 1 | 20,131,770,109,682 | null | 144 | 144 |
N = int(input())
print(1 - N)
|
n,m=list(map(int,input().split()))
s=input()
a=[0]*(n+1)
a[0]=1
for i in range(1,n+1):
if s[i]=='1':
continue
for j in range(max(0,i-m),i):
if s[j]=='1':
continue
if a[j]>0:
break
a[i]=i-j
ans=''
cur=n
while cur>0:
d=a[cur]
cur-=d
if d==0:
print(-1)
exit(0)
ans=str(d)+' '+ans
print(ans[:-1])
| 0 | null | 70,681,525,734,870 | 76 | 274 |
# Belongs to : midandfeed aka asilentvoice
n, m, l = [int(x) for x in input().split()]
a = [[int(x) for x in input().split()] for i in range(n)]
b = [[int(x) for x in input().split()] for i in range(m)]
ans = []
for i in range(n):
rtemp = []
for j in range(l):
temp = 0
for k in range(m):
temp += (a[i][k] * b[k][j])
rtemp.append(temp)
ans.append(rtemp)
for row in ans:
print(" ".join(map(str, row)))
|
while True:
H, W = map(int, input().split())
if (H, W) == (0, 0):
break
[print("#"*W+"\n") if i == H-1 else print("#"*W) for i in range(H)]
| 0 | null | 1,117,661,119,352 | 60 | 49 |
m, n = map(int, input().split())
print((m + n - 1)//n)
|
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, M = map(int, readline().split())
if N == M:
print('Yes')
else:
print('No')
| 0 | null | 80,266,843,954,352 | 225 | 231 |
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from decimal import *
input = sys.stdin.readline
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]
inf = float('inf')
mod = 1000000007
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
def main():
D = inputInt()
C = inputList()
S = []
for i in range(D):
s = inputList()
S.append(s)
ans1 = []
ans2 = []
ans3 = []
ans4 = []
ans5 = []
for i in range(D):
bestSco1 = 0
bestSco2 = 0
bestSco3 = 0
bestSco4 = 0
bestSco5 = 0
bestI1 = 1
bestI2 = 1
bestI3 = 1
bestI4 = 1
bestI5 = 1
for j,val in enumerate(S[i]):
if j == 0:
tmpAns = ans1 + [j+1]
tmpSco = findScore(tmpAns, S, C)
if bestSco1 < tmpSco:
bestSco5 = bestSco4
bestI5 = bestI4
bestSco4 = bestSco3
bestI4 = bestI3
bestSco3 = bestSco2
bestI3 = bestI2
bestSco2 = bestSco1
bestI2 = bestI1
bestSco1 = tmpSco
bestI1 = j+1
else:
tmpAns1 = ans1 + [j+1]
tmpAns2 = ans2 + [j+1]
tmpAns3 = ans3 + [j+1]
tmpAns4 = ans4 + [j+1]
tmpAns5 = ans5 + [j+1]
tmpSco1 = findScore(tmpAns1, S, C)
tmpSco2 = findScore(tmpAns2, S, C)
tmpSco3 = findScore(tmpAns3, S, C)
tmpSco4 = findScore(tmpAns4, S, C)
tmpSco5 = findScore(tmpAns5, S, C)
if bestSco1 < tmpSco1:
bestSco5 = bestSco4
bestI5 = bestI4
bestSco4 = bestSco3
bestI4 = bestI3
bestSco3 = bestSco2
bestI3 = bestI2
bestSco2 = bestSco1
bestI2 = bestI1
bestSco1 = tmpSco1
bestI1 = j+1
if bestSco1 < tmpSco2:
bestSco5 = bestSco4
bestI5 = bestI4
bestSco4 = bestSco3
bestI4 = bestI3
bestSco3 = bestSco2
bestI3 = bestI2
bestSco2 = bestSco1
bestI2 = bestI1
bestSco1 = tmpSco1
bestI1 = j+1
if bestSco1 < tmpSco3:
bestSco5 = bestSco4
bestI5 = bestI4
bestSco4 = bestSco3
bestI4 = bestI3
bestSco3 = bestSco2
bestI3 = bestI2
bestSco2 = bestSco1
bestI2 = bestI1
bestSco1 = tmpSco1
bestI1 = j+1
if bestSco1 < tmpSco4:
bestSco5 = bestSco4
bestI5 = bestI4
bestSco4 = bestSco3
bestI4 = bestI3
bestSco3 = bestSco2
bestI3 = bestI2
bestSco2 = bestSco1
bestI2 = bestI1
bestSco1 = tmpSco1
bestI1 = j+1
if bestSco1 < tmpSco5:
bestSco5 = bestSco4
bestI5 = bestI4
bestSco4 = bestSco3
bestI4 = bestI3
bestSco3 = bestSco2
bestI3 = bestI2
bestSco2 = bestSco1
bestI2 = bestI1
bestSco1 = tmpSco1
bestI1 = j+1
ans1.append(bestI1)
ans2.append(bestI2)
ans3.append(bestI3)
ans4.append(bestI4)
ans5.append(bestI5)
for i in ans1:
print(i)
def findScore(ans, S, C):
scezhu = [inf for i in range(26)]
sco = 0
for i,val in enumerate(ans):
tmp = S[i][val-1]
scezhu[val-1] = i
mins = 0
for j,vol in enumerate(C):
if scezhu[j] == inf:
mins = mins + (vol * (i+1))
else:
mins = mins + (vol * ((i+1)-(scezhu[j]+1)))
tmp -= mins
sco += tmp
return sco
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
main()
|
from random import choice
from time import time
start_time = time()
d = int(input())
*C, = map(int, input().split())
S = [list(map(int, input().split())) for i in range(d)]
X = []
L = [-1 for j in range(26)]
for i in range(d):
max_diff = -10**10
best_j = 0
for j in range(26):
memo = L[j]
L[j] = i
diff = S[i][j] - sum([C[jj] * (i - L[jj]) for jj in range(26)])
if diff > max_diff:
max_diff = diff
best_j = j
L[j] = memo
L[best_j] = i
X.append(best_j)
def f(X):
score = 0
L = [-1 for j in range(26)]
A = [0 for j in range(26)]
for i in range(d):
score += S[i][X[i]]
A[X[i]] += (i - L[X[i]]) * (i - L[X[i]] - 1) // 2
L[X[i]] = i
for j in range(26):
A[j] += (d - L[j]) * (d - L[j] - 1) // 2
score -= C[j] * A[j]
return score
max_score = f(X)
while time() - start_time < 1.8:
i = choice(range(d))
j = choice(range(26))
memo = X[i]
X[i] = j
score = f(X)
if score > max_score:
max_score = score
else:
X[i] = memo
a = choice(range(7))
i0 = choice(range(d - a))
i1 = i0 + a
memo = X[i0], X[i1]
X[i0], X[i1] = X[i1], X[i0]
score = f(X)
if score > max_score:
max_score = score
else:
X[i0], X[i1] = memo
for x in X:
print(x + 1)
| 1 | 9,697,621,210,408 | null | 113 | 113 |
class BalancingTree:
def __init__(self, n):
self.N = n
self.root = self.node(1<<n, 1<<n)
def append(self, v):# v を追加(その時点で v はない前提)
v += 1
nd = self.root
while True:
if v == nd.value:
# v がすでに存在する場合に何か処理が必要ならここに書く
return 0
else:
mi, ma = min(v, nd.value), max(v, nd.value)
if mi < nd.pivot:
nd.value = ma
if nd.left:
nd = nd.left
v = mi
else:
p = nd.pivot
nd.left = self.node(mi, p - (p&-p)//2)
break
else:
nd.value = mi
if nd.right:
nd = nd.right
v = ma
else:
p = nd.pivot
nd.right = self.node(ma, p + (p&-p)//2)
break
def leftmost(self, nd):
if nd.left: return self.leftmost(nd.left)
return nd
def rightmost(self, nd):
if nd.right: return self.rightmost(nd.right)
return nd
def find_l(self, v): # vより真に小さいやつの中での最大値(なければ-1)
v += 1
nd = self.root
prev = 0
if nd.value < v: v = nd.value
while True:
if v <= nd.value:
if nd.left:
nd = nd.left
else:
return prev - 1
else:
prev = nd.value
if nd.right:
nd = nd.right
else:
return prev - 1
def find_r(self, v): # vより真に大きいやつの中での最小値(なければRoot)
v += 1
nd = self.root
prev = 0
while True:
if v < nd.value:
prev = nd.value
if nd.left:
nd = nd.left
else:
return prev - 1
else:
if nd.right:
nd = nd.right
else:
return prev - 1
@property
def max(self):
return self.find_l((1<<self.N)-1)
@property
def min(self):
return self.find_r(-1)
def delete(self, v, nd = None, prev = None): # 値がvのノードがあれば削除(なければ何もしない)
v += 1
if not nd: nd = self.root
if not prev: prev = nd
while v != nd.value:
prev = nd
if v <= nd.value:
if nd.left:
nd = nd.left
else:
return
else:
if nd.right:
nd = nd.right
else:
return
if (not nd.left) and (not nd.right):
if nd.value < prev.value:
prev.left = None
else:
prev.right = None
elif not nd.left:
if nd.value < prev.value:
prev.left = nd.right
else:
prev.right = nd.right
elif not nd.right:
if nd.value < prev.value:
prev.left = nd.left
else:
prev.right = nd.left
else:
nd.value = self.leftmost(nd.right).value
self.delete(nd.value - 1, nd.right, nd)
def __contains__(self, v: int) -> bool:
return self.find_r(v - 1) == v
class node:
def __init__(self, v, p):
self.value = v
self.pivot = p
self.left = None
self.right = None
k = (500000+1).bit_length()
dic = {}
for i in range(97, 123):
dic[chr(i)] = BalancingTree(k)
n = int(input())
s = input()
lst = []
for i in range(n):
lst.append(s[i])
dic[s[i]].append(i)
ans = []
q = int(input())
for _ in range(q):
que = input()
if que[0] == '1':
kind, i, c = que.split()
i = int(i) - 1
dic[lst[i]].delete(i)
dic[c].append(i)
lst[i] = c
else:
kind, l, r = map(int, que.split())
l -= 2; r -= 1
temp = 0
for key in dic.keys():
if dic[key].find_r(l) <= r:
temp += 1
ans.append(temp)
for i in ans:
print(i)
|
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub, itemgetter, attrgetter
import sys
# sys.setrecursionlimit(10**6)
# readline = sys.stdin.buffer.readline
readline = sys.stdin.readline
INF = 2**62-1
def read_int():
return int(readline())
def read_int_n():
return list(map(int, readline().split()))
def read_float():
return float(readline())
def read_float_n():
return list(map(float, readline().split()))
def read_str():
return readline().strip()
def read_str_n():
return readline().strip().split()
def ep(*args):
print(*args, file=sys.stderr)
def mt(f):
import time
def wrap(*args, **kwargs):
s = time.perf_counter()
ret = f(*args, **kwargs)
e = time.perf_counter()
ep(e - s, 'sec')
return ret
return wrap
@mt
def slv(N):
c = Counter()
for i in range(1, N+1):
s = str(i)
c[int(s[0])*10+int(s[-1])] += 1
ans = 0
for i, j in product(range(1, 10), repeat=2):
ans += c[10*i+j] * c[10*j+i]
return ans
def main():
N = read_int()
print(slv(N))
if __name__ == '__main__':
main()
| 0 | null | 74,140,492,916,000 | 210 | 234 |
def print_list(ele_list):
print(" ".join(map(str, ele_list)))
def insertion_sort(ele_list):
len_ele_list = len(ele_list)
print_list(ele_list)
for i in range(1, len_ele_list):
key = ele_list[i]
j = i - 1
while j >= 0 and ele_list[j] > key:
ele_list[j+1] = ele_list[j]
j -= 1
ele_list[j+1] = key
print_list(ele_list)
N = int(input())
ele_list = list(map(int, input().split()))
insertion_sort(ele_list)
|
a,b=map(int,input().split())
print(a*b if a<10 and b<10 else '-1')
| 0 | null | 79,443,709,570,910 | 10 | 286 |
N, M, X = map(int, input().split())
C = []
A = []
for _ in range(N):
c, *a = map(int, input().split())
C.append(c)
A.append(a)
ans = 10**18
for s in range(1<<N):
an = 0
L = [0] * M
for i, (c, a) in enumerate(zip(C, A)):
if s>>i&1:
an += c
for j, a_ in enumerate(a):
L[j] += a_
if all(l >= X for l in L):
if an < ans:
ans = an
if ans == 10**18:
print(-1)
else:
print(ans)
|
N, M, X = map(int, input().split())
C = []
A = []
for i in range(N):
tmp = list(map(int, input().split()))
C.append(tmp[0])
A.append(tmp[1:])
ans = -1
for i in range(1, 1 << N):
price = 0
skill = [0] * M
b = len(bin(i)) - 2
for j in range(b):
price += C[j] * (i >> j & 1)
for k in range(M):
skill[k] += A[j][k] * (i >> j & 1)
if min(skill) >= X and (price < ans or ans < 0):
ans = price
print(ans)
| 1 | 22,250,823,044,200 | null | 149 | 149 |
#!usr/bin/env pypy3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
res = list(sys.stdin.readline())
if res[-1] == "\n":
return res[:-1]
return res
def IR(n):
return [I() for i in range(n)]
def LIR(n):
return [LI() for i in range(n)]
def SR(n):
return [S() for i in range(n)]
def LSR(n):
return [LS() for i in range(n)]
sys.setrecursionlimit(1000000)
def main():
N, K = LI()
x = N % K
y = abs(x - K)
print(min(x, y))
main()
|
x, k, d = map(int, input().split())
t = min(abs(x)//d, k)
u = abs(x)-d*t
print(abs(u-d*((k-t)%2)))
| 0 | null | 22,200,815,571,418 | 180 | 92 |
L,R,d = list(map(int, input().split()))
count = 0
for i in range(L,R+1):
count += (i%d == 0)
print(count)
|
n, k = map(int, input().split())
a = list(map(int, input().split()))
f = list(map(int, input().split()))
a.sort()
f.sort()
high = max(a)*max(f)
low = 0
while high > low:
mid = (high+low)//2
b = 0
for i in range(n):
b += max(a[i]-mid//f[n-i-1],0)
if b > k:
low = mid + (low==mid)
else:
high = mid - (high==mid)
mid = (high+low)//2
print(mid)
| 0 | null | 86,264,068,996,960 | 104 | 290 |
string = input()
q = int(input())
for _ in range(q):
line = input().split()
a = int(line[1])
b = int(line[2])
if line[0] == "print":
print(string[a:b+1])
elif line[0] == "reverse":
tmp = string[a:b+1]
tmp = tmp[::-1]
new_s = string[:a] + tmp + string[b+1:]
string = new_s
else:
new_s = string[:a] + line[3] + string[b+1:]
string = new_s
|
def dp_solver(tmp):
l=len(tmp)
rev=tmp[::-1]+"0"
dp=[[float("inf") for _ in range(2)] for _ in range(l+2)]
dp[0][0]=0
for i in range(l+1):
for j in range(2):
num=int(rev[i])
num+=j
if num<10:
dp[i+1][0]=min(dp[i+1][0],dp[i][j]+num)
if num>0:
dp[i+1][1]=min(dp[i+1][1],dp[i][j]+(10-num))
#import numpy as np
#dp=np.array(dp)
return(dp[l+1])
s=input()
ans=dp_solver(s)
print(ans[0])
| 0 | null | 36,445,674,230,030 | 68 | 219 |
x = input().split()
for i in range(5):
if x[i] == '0':
print(i+1)
|
def resolve():
'''
code here
'''
N = input()
res = 0
if int(str(N)[-1]) % 2 == 0:
i = 0
N=int(N)
while int(N) >= 2*5**i:
i += 1
res += N//(2*5**i)
print(res)
if __name__ == "__main__":
resolve()
| 0 | null | 64,632,342,105,870 | 126 | 258 |
import sys
import math
N, K = map(int, sys.stdin.readline().rstrip().split())
A = list(map(int, sys.stdin.readline().rstrip().split()))
F = list(map(int, sys.stdin.readline().rstrip().split()))
A.sort()
A = A[::-1]
F.sort()
def test(x):
res = 0
for i in range(N):
t = A[i] * F[i]
if t > x:
res += math.ceil((t - x) / F[i])
return res <= K
ng = -1
ok = 10**18
while ng + 1 < ok:
mid = (ng + ok) // 2
if test(mid):
ok = mid
else:
ng = mid
print(ok)
|
n,k = map(int,input().split())
a = list(map(int,input().split()))
f = list(map(int,input().split()))
lim = sum(a) -k
if(lim <= 0):
print(0)
exit()
min_ = 0
max_ = 10**12+1
a.sort()
a = tuple(a)
f.sort(reverse=True)
f = tuple(f)
for i in range(100):
tmp = (max_ + min_)//2
tmp_sum = 0
for j in range(n):
tmp_sum += max(0, a[j] - tmp//f[j])
if(tmp_sum <= k):
max_ = tmp
else:
min_ = tmp
if(max_ - min_ == 1):
break
print(max_)
| 1 | 164,455,710,858,660 | null | 290 | 290 |
#!/usr/bin/env python3
n = int(input())
print(int((n - 1) / 2) if n % 2 == 1 else int(n / 2) - 1)
|
N,S=map(int,input().split())
*a,=map(int,input().split())
mod=998244353
dp=[[0]*(S+1) for _ in range(N+1)]
dp[0][0]=1
for i in range(N):
for j in range(S+1):
dp[i+1][j]=2*dp[i][j]
if j>=a[i]:
dp[i+1][j]+=dp[i][j-a[i]]
dp[i+1][j]%=mod
print(dp[N][S])
| 0 | null | 85,548,242,939,100 | 283 | 138 |
s = input()
res = 0
cur = 0
for i in s:
if i =='S':
cur = 0
else:
cur += 1
res = max(cur, res)
print(res)
|
N = int(input())
S = input()
abc = S.count('R')*S.count('G')*S.count('B')
L = (N-1)//2
for i in range(1,L+1):
for j in range(N-2*i):
if S[j]!=S[j+i] and S[j+i]!=S[j+2*i] and S[j]!=S[j+2*i]:
abc -= 1
print(abc)
| 0 | null | 20,425,319,379,036 | 90 | 175 |
N=int(input())
ok=0
for i in range(1,10):
j = N/i
if j==int(j) and 1<=j<=9:
ok=1
print('Yes')
break
if ok==0:print('No')
|
a = input()
if ("AB" in a) or ("BA" in a):
print("Yes")
else:
print("No")
| 0 | null | 107,568,798,500,732 | 287 | 201 |
alp = input()
if alp==alp.upper() :
print("A")
else :
print("a")
|
import sys, math
max_int = 1000000001 # 10^9+1
min_int = -max_int
n = int(sys.stdin.readline())
positive_counter = {}
negative_counter = {}
to_add = 0
a_zero_counter = b_zero_counter = 0
for _n in range(n):
s = sys.stdin.readline()
a, b = map(int, s.split())
if a == 0 and b == 0:
to_add += 1
continue
elif a == 0:
a_zero_counter += 1
continue
elif b == 0:
b_zero_counter += 1
continue
elif (a > 0) == (b > 0):
a = abs(a)
b = abs(b)
gcd = math.gcd(a, b)
definer = (a // gcd, b // gcd)
if definer not in positive_counter:
positive_counter[definer] = 1
else:
positive_counter[definer] += 1
else:
a = abs(a)
b = abs(b)
gcd = math.gcd(a, b)
definer = (b // gcd, a // gcd)
if definer not in negative_counter:
negative_counter[definer] = 1
else:
negative_counter[definer] += 1
prod = 2 ** a_zero_counter + 2 ** b_zero_counter - 1
#print(positive_counter, negative_counter)
for k in positive_counter:
if k in negative_counter:
prod *= 2 ** positive_counter[k] + 2 ** negative_counter[k] - 1
negative_counter.pop(k)
else:
prod *= 2 ** positive_counter[k]
prod %= 1000000007
for k in negative_counter:
prod *= 2 ** negative_counter[k]
prod %= 1000000007
prod += to_add - 1
print(prod % 1000000007)
| 0 | null | 16,049,017,742,248 | 119 | 146 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.