code1
stringlengths 16
427k
| code2
stringlengths 16
427k
| similar
int64 0
1
| pair_id
int64 6.82M
181,637B
⌀ | question_pair_id
float64 101M
180,471B
⌀ | code1_group
int64 2
299
| code2_group
int64 2
299
|
---|---|---|---|---|---|---|
x1,y1,x2,y2 = map(float,input().split())
distance = ( (x1 - x2)**2 + (y1 - y2)**2 )**(1/2)
print('{}'.format(distance))
|
x1, y1, x2, y2 = map(float, raw_input().split())
print ((x1-x2)**2 + (y1-y2)**2)**0.5
| 1 | 154,238,619,130 | null | 29 | 29 |
N=int(input())
C=input()
W=0
R=C.count("R")
ans=R
for i in range(N):
if C[i]=="R":
R-=1
else:
W+=1
if ans>max(W,R):
ans=max(W,R)
print(ans)
|
n = int(input())
c = list(input())
rcnt = 0
allrcnt = 0
for i in range(len(c)):
if c[i]=='R':
allrcnt += 1
for i in range(len(c)):
if i+1<=allrcnt and c[i]=='R':
rcnt += 1
ans = allrcnt-rcnt
print(ans)
| 1 | 6,322,286,367,840 | null | 98 | 98 |
import sys
def input():
return sys.stdin.readline().strip()
def main():
K = int(input())
S = input()
mod = 10 ** 9 + 7
n = len(S)
ans = 0
fact =[1]*(K+n)
factinv = [1]*(K+n)
inv = [0,1]
for i in range(2,K+n):
fact[i] = (fact[i-1] * i) % mod
inv.append((-inv[mod % i] * (mod // i)) % mod)
factinv[i] = (factinv[i-1] * inv[i]) % mod
def nCr(n,r):
r = min(r,n-r)
return (fact[n] * factinv[r] * factinv[n-r]) % mod
for i in range(K+1):
tmp = pow(25,i,mod)
tmp *= nCr(i+n-1,n-1)
tmp *= pow(26,K-i,mod)
ans += tmp
ans %= mod
print(ans)
if __name__ == "__main__":
main()
|
def main():
K = int(input())
S = input()
N = len(S)
mod = 10**9 + 7
r = 0
t = pow(26, K, mod)
s = 1
inv26 = pow(26, mod - 2, mod)
inv = [0] * (K + 2)
inv[1] = 1
for i in range(2, K + 2):
inv[i] = -inv[mod % i] * (mod // i) % mod
for i in range(K + 1):
r = (r + t * s) % mod
t = (t * 25 * inv26) % mod
s = (s * (N + i) * inv[i + 1]) % mod
return r
print(main())
| 1 | 12,744,592,911,820 | null | 124 | 124 |
height = []
for i in range(10):
height.append(input())
height.sort(reverse=True)
for i in range(3):
print(height[i])
|
# coding: utf-8
# Here your code !
ret = []
for i in range(10):
ret.append(int(input()))
ret.sort()
print(ret[-1])
print(ret[-2])
print(ret[-3])
| 1 | 27,891,210 | null | 2 | 2 |
n = int(input())
x = int(input(),2)
def popcount(l):
return bin(l).count("1")
def count(l):
res = 0
while l != 0:
p = popcount(l)
l %= p
res += 1
return res
m = popcount(x)
x_plus = x%(m+1)
if m != 1:
x_minus = x%(m-1)
else:
x_minus = 1
lis_plus = [0]*n
lis_minus = [0]*n
for i in range(n):
if i == 0:
lis_plus[i] = 1%(m+1)
if m != 1:
lis_minus[i] = 1%(m-1)
else:
lis_plus[i] = lis_plus[i-1]*2%(m+1)
if m != 1:
lis_minus[i] = lis_minus[i-1]*2%(m-1)
for i in range(n):
if (x >> (n-i-1)) & 1:
if m == 1:
print(0)
else:
print(count((x_minus - lis_minus[-i-1])%(m-1)) + 1)
else:
print(count((x_plus + lis_plus[-i-1])%(m+1)) + 1)
|
import math
from collections import defaultdict,deque
ml=lambda:map(int,input().split())
ll=lambda:list(map(int,input().split()))
ii=lambda:int(input())
ip=lambda:list(input())
ips=lambda:input().split()
"""========main code==============="""
def fast_expo(x,y,m):
res=1
while(y):
if(y&1):
res*=x
res%=m
x*=x
x%=m
y>>=1
return res
dp=defaultdict(int)
dp[1]=1
dp[2]=1
for i in range(3,200001):
k=i
cnt=0
while(k):
if(k%2): cnt+=1
k>>=1
dp[i]=dp[i%cnt]+1
n=ii()
a=ip()
a=a[::-1]
c=a.count('1')
min_1=0
max_1=0
for i in range(n):
if(a[i]=='1'):
if(c-1!=0):
min_1+=fast_expo(2,i,c-1)
min_1%=(c-1)
max_1+=fast_expo(2,i,c+1)
max_1%=(c+1)
ans=[]
for i in range(n):
if(a[i]=='1'):
if(c-1==0):
ans.append(0)
else:
ans.append(dp[(min_1-fast_expo(2,i,c-1)+(c-1))%(c-1)]+1)
else:
ans.append(dp[(max_1+fast_expo(2,i,c+1))%(c+1)]+1)
ans=ans[::-1]
for i in ans:
print(i)
| 1 | 8,190,649,972,380 | null | 107 | 107 |
str0 = raw_input()
q = int(raw_input())
for i in xrange(q):
# print "###########################"
# print "str0 = " + str0
line = raw_input().split(" ")
temp = str0[int(line[1]):int(line[2])+1]
if line[0] == "print":
print temp
elif line[0] == "reverse":
temp2 = []
# print "temp = " + temp
for j in xrange(len(temp)):
temp2.append(temp[len(temp) - j - 1])
str0 = str0[:int(line[1])] + "".join(temp2) + str0[int(line[2]) + 1:]
# print "str0 = " + str0
elif line[0] == "replace":
str0 = str0[:int(line[1])] + line[3] + str0[int(line[2]) + 1:]
# print "str0 = " + str0
|
s=input()
times=int(input())
for _ in range(times):
order=input().split()
a=int(order[1])
b=int(order[2])
if order[0]=="print":
print(s[a:b+1])
elif order[0]=="reverse":
s=s[:a]+s[a:b+1][::-1]+s[b+1:]
else :
s=s[:a]+order[3]+s[b+1:]
| 1 | 2,091,888,773,232 | null | 68 | 68 |
import numpy as np
N = int(input())
N_List = list(map(int,input().split()))
ans = (100**2)*100
for i in range(1,101):
ca = sum(map(lambda x:(x-i)**2,N_List))
if ca < ans:
ans = ca
print(ans)
|
import bisect
def is_ok(a, target, m):
num = 0
for val in a:
index = bisect.bisect_left(a, target - val)
num += len(a) - index
if num <= m:
return True
else:
return False
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
ok, ng = 10 ** 10, -1
while ok - ng > 1:
mid = (ok + ng) // 2
if is_ok(a, mid, m):
ok = mid
else:
ng = mid
rui = [0] * (n + 1)
for i in range(n):
rui[i+1] = rui[i] + a[i]
cnt = 0
ret = 0
for val in a:
index = bisect.bisect_left(a, ok - val)
num = len(a) - index
cnt += num
ret += (num * val) + (rui[-1] - rui[index])
ret += (m - cnt) * ng
print(ret)
| 0 | null | 86,502,577,821,842 | 213 | 252 |
import itertools
n = int(input())
p = [[0 for i in range(2)] for j in range(n)]
for i in range(n):
p[i][0],p[i][1] = map(int,input().split())
#print(p)
def dis(a,b):
dis = (a[0]-b[0]) ** 2 + (a[1]-b[1]) **2
return dis ** (1/2)
perm = [i for i in range(n)]
total = 0
num = 0
for v in itertools.permutations(perm,n):
path = 0
for i in range(n-1):
path += dis(p[v[i]],p[v[i+1]])
num += 1
total += path
print(total/num)
|
#n<=10**5まで
def cmb(n, k, mod, fac, ifac):
k = min(k, n-k)
return fac[n] * ifac[k] * ifac[n-k] % mod
def make_tables(mod, n):
fac = [1, 1] # 階乗テーブル・・・(1)
ifac = [1, 1] #逆元の階乗テーブル・・・(2)
inverse = [0, 1] #逆元テーブル・・・(3)
for i in range(2, n+1):
fac.append((fac[-1] * i) % mod)
inverse.append((-inverse[mod % i] * (mod//i)) % mod)
ifac.append((ifac[-1] * inverse[-1]) % mod)
return fac, ifac
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
mod = 1000000007
fac, ifac = make_tables(mod, n)
ans = 0
for i in range(n-k+1):
c = cmb(n-1-i, k-1, mod, fac, ifac)
ans += ((a[n-1-i] - a[i]) * c) % mod
print(ans%mod)
| 0 | null | 121,876,211,052,398 | 280 | 242 |
from decimal import Decimal
n,m = map(int, input().split())
A = list(map(int, input().split()))
line = Decimal(sum(A) / (4 * m))
cnt = 0
for a in A:
if a >= line:
cnt += 1
if cnt >= m:
print('Yes')
break
else:
print('No')
|
N, M = (int(x) for x in input().split())
A = list(map(int, input().split()))
sum=0
cnt=0
for i in range(N):
sum+=A[i]
sum/=4*M
for i in range(N):
if A[i]>=sum:
cnt+=1
if cnt>=M:
print("Yes")
else:
print("No")
| 1 | 38,830,509,734,820 | null | 179 | 179 |
n=int(input())
a=list(map(int,input().split()))
mod=10**9+7
c=[0]*60
for aa in a:
for i in range(60):
if aa&(1<<i):c[i]+=1
ans=0
for i,cc in enumerate(c):
ans+=cc*(n-cc)*2**i
ans%=mod
print(ans)
|
#!/usr/bin/env python3
n, *a = map(int, open(0).read().split())
b = [0] * 60
c = [0] * 60
ans = 0
for i in range(n):
for j in range(60):
c[j] += i - b[j] if a[i] >> j & 1 else b[j]
b[j] += a[i] >> j & 1
for j in range(60):
ans += c[j] << j
print(ans % (10**9 + 7))
| 1 | 122,946,655,845,936 | null | 263 | 263 |
num = int(input())
result = num + num**2 + num**3
print(result)
|
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
cA = Counter(A)
allS = 0
for key in cA:
allS += cA[key] * (cA[key] - 1) / 2
for i in range(N):
if A[i] in cA.keys():
print(int(allS - (cA[A[i]] - 1)))
else:
print(int(allS))
| 0 | null | 29,041,042,656,068 | 115 | 192 |
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
ans=a[0]
ch=1
for i in range(1,n):
if ch==n-1:
break
else:
ans+=a[i]
ch+=1
if ch==n-1:
break
else:
ans+=a[i]
ch+=1
print(ans)
|
import math
A, B, N = map(int,input().split(' '))
def f(x):
return math.floor(A*x/B)-A*math.floor(x/B)
def main():
if N >= B:
print(f(B-1))
else:
print(f(N))
if __name__ == "__main__":
main()
| 0 | null | 18,821,278,255,432 | 111 | 161 |
def main():
A, B, C = map(int, input().split())
K = int(input())
while B <= A:
B *= 2
K -= 1
while C <= B:
C *= 2
K -= 1
if K >= 0:
print("Yes")
else:
print("No")
if __name__ == "__main__":
main()
|
import math
while True:
try:
a,b=map(int,input().split())
print(*[math.gcd(a,b),a*b//math.gcd(a,b)])
except:break
| 0 | null | 3,420,022,217,664 | 101 | 5 |
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
def gcd(*numbers): reduce(math.gcd, numbers)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
count = 0
ans = 0
inf = float("inf")
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
def gcd(*numbers): reduce(math.gcd, numbers)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
count = 0
ans = 0
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
def gcd(*numbers): reduce(math.gcd, numbers)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
count = 0
ans = 0
inf = float("inf")
n,m = I()
g = [[] for _ in range(n)]
for i in range(m):
a,b = I()
g[a-1].append(b-1)
g[b-1].append(a-1)
q = deque([0])
d = [-1]*n
d[0] = 0
while q:
v = q.popleft()
for i in g[v]:
if d[i] != -1:
continue
d[i] = v
q.append(i)
if -1 in d:
print("No")
else:
print("Yes")
for i in d[1:]:
print(i+1)
|
import sys
n = int(sys.stdin.readline()[:-1])
S = sorted(list(map(int, (sys.stdin.readline()[:-1]).split())))
q = int(sys.stdin.readline()[:-1])
T = sorted(list(map(int, (sys.stdin.readline()[:-1]).split())))
count = 0
for i in T:
x = 0
while x < len(S):
if i == S[x]:
count += 1
S = S[x:]
break
x += 1
print(count)
| 0 | null | 10,360,539,207,982 | 145 | 22 |
from sys import stdin, setrecursionlimit
def main():
input = stdin.buffer.readline
l = int(input())
print((l / 3) ** 3)
if __name__ == "__main__":
setrecursionlimit(10000)
main()
|
#!/usr/bin/env python3
def pow(N,R,mod):
ans = 1
i = 0
while((R>>i)>0):
if (R>>i)&1:
ans *= N
ans %= mod
N = (N*N)%mod
i += 1
return ans%mod
def main():
N,K = map(int,input().split())
li = [0 for _ in range(K)]
mod = 10**9+7
ans = 0
for i in range(K,0,-1):
mm = K//i
mm = pow(mm,N,mod)
for j in range(2,(K//i)+1):
mm -= li[i*j-1]
li[i-1] = mm
ans += (i*mm)%mod
ans %= mod
print(ans)
if __name__ == '__main__':
main()
| 0 | null | 41,701,573,371,622 | 191 | 176 |
import sys
from collections import defaultdict
from functools import lru_cache
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
MOD = 10**9 + 7
P = 2 ** 61 - 1
N = int(readline())
m = map(int, read().split())
A, B = zip(*zip(m, m))
pow2 = [1] * (N + 10)
for n in range(1, N + 10):
pow2[n] = pow2[n - 1] * 2 % MOD
@lru_cache(None)
def inv_mod(a):
b = P
u, v = 1, 0
while a:
t = b // a
a, b = b - t * a, a
u, v = v - t * u, u
if b < 0:
v = -v
return v % P
def to_key(a, b):
if a == 0:
return -1
x = inv_mod(a) * b % P
return x
def pair_key(key):
if key == -1:
return 0
if key == 0:
return -1
return P - inv_mod(key)
counter = defaultdict(int)
origin = 0
for a, b in zip(A, B):
if a == b == 0:
origin += 1
continue
key = to_key(a, b)
counter[key] += 1
answer = origin
k = 1
for key, cnt in counter.items():
key1 = pair_key(key)
if key1 not in counter:
k *= pow(2, cnt, MOD)
elif key < key1:
x, y = cnt, counter[key1]
k *= pow(2, x, MOD) + pow(2, y, MOD) - 1
k %= MOD
answer += k - 1
answer %= MOD
print(answer)
|
x = int(input())
cnt = x//100
if x < 100 :
print("0")
else :
for i in range(cnt) :
if 100*cnt <= x <= 105*cnt :
print("1")
break
else :
print("0")
| 0 | null | 74,320,758,603,872 | 146 | 266 |
A,B,M=map(int,input().split())
alist=list(map(int,input().split()))
blist=list(map(int,input().split()))
answer=min(alist)+min(blist)
for _ in range(M):
x,y,c=map(int,input().split())
disc=alist[x-1]+blist[y-1]-c
answer=min(answer,disc)
print(answer)
|
import sys
input = sys.stdin.readline
import math
big = 10 ** 9 + 7
N = int(input())
A = list(map(int, input().split()))
num = A[0]
for i in range(1, N):
num = num * A[i] // math.gcd(num, A[i])
res = 0
for a in A:
res += num // a
print(res % big)
| 0 | null | 70,752,911,256,508 | 200 | 235 |
cnt = 0
def insertionsort(A,n,g):
global cnt
for i in range(g,n):
v = A[i]
j = i-g
while j >= 0 and A[j] > v:
A[j+g] = A[j]
j -= g
cnt += 1
A[j+g] = v
def shellsort(A,n):
h = 1
G = []
while h <= len(A):
G.append(h)
h = h*3+1
G.reverse()
m = len(G)
for i in range(m):
insertionsort(A,n,G[i])
return m,G
def main():
n = int(input())
A = []
for _ in range(n):
A.append(int(input()))
m,G = shellsort(A,n)
##print("-----------")
print(m)
print(*G)
print(cnt)
for i in A:
print(i)
if __name__ == "__main__":
main()
|
a,b,c = map(int,input().split())
if a > b:
b,a = a,b
if b > c:
b,c = c,b
if a > b:
b,a = a,b
else:
if b > c:
b,c = c,b
if a > b:
b,a = a,b
print(f"{a} {b} {c}")
| 0 | null | 232,868,169,040 | 17 | 40 |
N = int(input())
P = list(map(int,input().split()))
num = float("inf")
ans = 0
for i in range(N):
if num >= P[i]:
ans += 1
num = P[i]
print(ans)
|
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]
n = int(input())
p = list(map(int, input().split()))
ans = 1
minVal = p[0]
for i in range(1, n):
if minVal > p[i]:
ans += 1
minVal = p[i]
print(ans)
| 1 | 85,156,037,083,142 | null | 233 | 233 |
n = int(input())
alphabet = [chr(i) for i in range(97, 97+26)]
ans = ""
i = 0
# 次26**iを引いたらマイナスになるよ、というところで抜ける
while n - 26**i >= 0:
n -= 26**i
i += 1
# ここを抜けた時,iは桁数、nは「i桁の中でn番目」を表す
# したからk番目は、26**kで割った余り(に対応するアルファベット)
# 「上の桁で表現しきれなかった数」=「その桁での表現」
# これ、10進法をn進法に変換するアルゴリズムの正当性の説明を適用するとしっかりわかるわ!
for _ in range(i):
q,r = divmod(n,26)
n = q
ans += alphabet[r]
print(ans[::-1])
|
from scipy.sparse.csgraph import csgraph_from_dense
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall
import numpy as np
N,M,L=map(int,input().split())
A=[0]*M
B=[0]*M
C=[0]*M
for i in range(M):
A[i],B[i],C[i] = map(int,input().split())
A[i] -=1
B[i] -=1
Q=int(input())
ST=[list(map(int,input().split())) for _ in range(Q)]
G = csr_matrix((C, (A, B)), shape=(N, N))
d = floyd_warshall(G, directed=False)
G=np.full((N,N), np.inf)
G[d<=L]=1
G = csr_matrix(G)
E = floyd_warshall(G, directed=False)
for s,t in ST:
if E[s-1][t-1]==float('inf'):
print(-1)
else:
print(int(E[s-1][t-1]-1))
| 0 | null | 92,740,572,660,030 | 121 | 295 |
# -*- coding: utf-8 -*-
from scipy.sparse.csgraph import floyd_warshall
N, M, L = map(int,input().split())
d = [[0 for i in range(N)] for j in range(N)]
for i in range(M):
x,y,z = map(int,input().split())
d[x-1][y-1] = z
d[y-1][x-1] = z
for i in range(N):
d[i][i] = 0
d = floyd_warshall(d, directed = False)
LN = [[0 for i in range(N)] for j in range(N)]
for i in range(N):
for j in range(N):
if d[i][j] <= L:
LN[i][j] = 1
LN[j][i] = 1
LN = floyd_warshall(LN, directed = False)
s = []
t = []
Q = int(input())
for i in range(Q):
s1, t1 = map(int,input().split())
s.append(s1)
t.append(t1)
for i in range(Q):
if LN[s[i]-1][t[i]-1] == float("inf"):
print(-1)
else:
print(int(LN[s[i]-1][t[i]-1]-1))
|
arr = map(int, raw_input().split())
arr.sort()
str_arr = map(str,arr)
print(" ".join(str_arr))
| 0 | null | 86,600,045,899,990 | 295 | 40 |
import itertools
n, m, q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(q)]
l = list(itertools.combinations_with_replacement(range(1, m+1), n))
ans = 0
for i in l:
w = 0
for j in abcd:
if i[j[1]-1] - i[j[0]-1] == j[2]:
w += j[3]
ans = max(ans, w)
print(ans)
|
# coding: utf-8
import itertools
def main():
n, m, q = map(int, input().split())
matrix = []
for _ in range(q):
row = list(map(int, input().split()))
matrix.append(row)
# print(matrix)
A = list(range(1, m+1))
ans = 0
for tmp_A in itertools.combinations_with_replacement(A, n):
tmp_A = sorted(list(tmp_A))
tmp_ans = 0
for row in matrix:
# print("row", row)
# print("tmp_A", tmp_A)
tmp = tmp_A[row[1] - 1] - tmp_A[row[0]- 1]
if row[2] == tmp:
tmp_ans += row[3]
if tmp_ans >= ans:
ans = tmp_ans
print(ans)
main()
| 1 | 27,525,618,565,088 | null | 160 | 160 |
n=int(input())
a=list(map(int,input().strip().split()))[:n]
counter=0
for i in range (0,n,2):
if(a[i]%2==1):
counter+=1
print(counter)
|
n = int(input())
st = []
for _ in range(n):
si, ti = input().split()
st.append((si, int(ti)))
name = input()
count = 0
flag = False
for i in range(n):
if flag:
count += st[i][1]
if st[i][0] == name:
flag = True
print(count)
| 0 | null | 52,073,672,419,892 | 105 | 243 |
s,t = map(str,input().split())
print("".join(t+s))
|
st = input().split(' ')
print(st[1]+st[0])
| 1 | 103,066,477,120,420 | null | 248 | 248 |
X,Y = map(int,input().split())
if X == Y == 1:
print(1000000)
else:
if X <= 3 and Y <= 3:
print(((4-X)+(4-Y))*10**5)
elif X >= 4 and Y >= 4:
print(0)
else:
print((4-min(X,Y))*10**5)
|
A,V = map(int,input().split())
B,W= map(int,input().split())
T = int(input())
dist = abs(A-B)
vec = V-W
print('YES' if vec*T >= dist else 'NO')
| 0 | null | 78,033,932,287,932 | 275 | 131 |
from collections import deque
n=int(input())
que=deque()
for i in range(n):
command=input()
if command=="deleteFirst":
que.popleft()
elif command=="deleteLast":
que.pop()
else:
command,num=command.split()
num=int(num)
if command=="insert":
que.appendleft(num)
else:
if num in que:
que.remove(num)
print(*que,sep=" ")
|
import sys
res = dict()
dll = [None] * 2000000
left = 0
right = 0
n = int(input())
for inpt in sys.stdin.read().splitlines():
i = inpt.split()
if i[0] == "insert":
x = int(i[1])
dll[left] = x
if(x in res):
res[x].add(left)
else:
res[x] = set([left])
left += 1
if i[0] == "delete":
x = int(i[1])
if(x in res):
ind = max(res[x])
dll[ind] = None
res[x].remove(ind)
if(len(res[x]) == 0):
del res[x]
if(ind == (left - 1)):
left = ind
if(ind == right):
right += 1
if i[0] == "deleteFirst":
left -= 1
x = dll[left]
res[x].remove(left)
if(len(res[x]) == 0):
del res[x]
dll[left] = None
if i[0] == "deleteLast":
right += 1
# print(dll[right:left])
ret = []
for x in dll[right:left]:
if(x is not None):
ret.append(str(x))
ret.reverse()
print(" ".join(ret))
| 1 | 47,380,446,640 | null | 20 | 20 |
n,m,k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a_c_sum = [0]*(n + 1)
for i in range(n):
a_c_sum[i + 1] = a_c_sum[i] + a[i]
b_c_sum = [0]*(m + 1)
for i in range(m):
b_c_sum[i + 1] = b_c_sum[i] + b[i]
ans, best_b = 0, m
for i in range(n + 1):
if a_c_sum[i] > k:
break
else:
while 1:
if b_c_sum[best_b] + a_c_sum[i] <= k:
ans = max(ans, i + best_b)
break
else:
best_b -= 1
if best_b == 0:
break
print(ans)
|
# Common Raccoon vs Monster
H, N = map(int,input().split())
A = list(map(int, input().split()))
ans = ['No', 'Yes'][sum(A) >= H]
print(ans)
| 0 | null | 44,124,607,717,402 | 117 | 226 |
n=int(input())
s,t = map(str, input().split())
print(''.join([s[i] + t[i] for i in range(0,n)]))
|
#!/usr/bin/env python
# encoding: utf-8
import sys
class Solution:
def __init__(self):
self.count = 0
def shell_sort(self):
array_length = int(input())
# array = []
# array_length = 10
# array = [15, 12, 8, 9, 3, 2, 7, 2, 11, 1]
array = list(map(int, sys.stdin.readlines()))
G = [1]
while True:
g = G[0] * 3 + 1
if g > array_length:
break
G.insert(0, g)
g_length = len(G)
result = []
for j in range(g_length):
result = self.insertion_sort(array=array, array_length=array_length, g=G[j])
# print(result, 'r', G[j])
print(g_length)
print(" ".join(map(str, G)))
print(self.count)
for k in range(array_length):
print(result[k])
def insertion_sort(self, array, array_length, g):
# write your code here
for i in range(g, array_length):
v = array[i]
j = i - g
while j >= 0 and array[j] > v:
array[j + g] = array[j]
j -= g
self.count += 1
array[j + g] = v
# print(" ".join(map(str, array)))
return array
if __name__ == '__main__':
solution = Solution()
solution.shell_sort()
| 0 | null | 55,766,866,491,508 | 255 | 17 |
n = int(input())
nums = []
ans = ""
while n >= 1:
n -= 1
ans += chr(ord('a') + n % 26)
n = n // 26
print(ans[::-1])
|
N = int(input()) - 1
keta = 1
cumsum = 0
oldcumsum = 0
while True:
cumsum += 26 ** keta
if cumsum > N:
break
else:
oldcumsum = cumsum
keta += 1
N -= oldcumsum
#N -= 1
#print(keta)
name = ''
for k in range(1,keta+1):
ch_n = N // (26 ** (keta - k))
ch = chr(ord('a') + ch_n)
name += ch
N -= ch_n * (26 ** (keta - k))
print(name)
| 1 | 11,862,765,313,432 | null | 121 | 121 |
import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
from collections import Counter
def resolve():
n = int(input())
sieve = list(range(n + 1))
primes = []
for i in range(2, n + 1):
if sieve[i] == i:
primes.append(i)
for p in primes:
if p * i > n or sieve[i] < p:
break
sieve[p * i] = p
ans = 0
for i in range(1, n):
C = Counter()
while i > 1:
C[sieve[i]] += 1
i //= sieve[i]
score = 1
for val in C.values():
score *= val + 1
ans += score
print(ans)
resolve()
|
from math import sqrt
N = int(input())
ans = 0
for a in range(1, N+1):
ans += (N-1)//a
print(ans)
| 1 | 2,597,939,083,428 | null | 73 | 73 |
a,b = input().split()
c = [a*int(b), b*int(a)]
print(sorted(c)[0])
|
import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)
input = sys.stdin.readline
def acinput():
return list(map(int, input().split(" ")))
def II():
return int(input())
directions=np.array([[1,0],[0,1],[-1,0],[0,-1]])
directions = list(map(np.array, directions))
mod = 10**9+7
def factorial(n):
fact = 1
for integer in range(1, n + 1):
fact *= integer
return fact
def serch(x, count):
#print("top", x, count)
for d in directions:
nx = d+x
#print(nx)
if np.all(0 <= nx) and np.all(nx < (H, W)):
if field[nx[0]][nx[1]] == "E":
count += 1
field[nx[0]][nx[1]] = "V"
count = serch(nx, count)
continue
if field[nx[0]][nx[1]] == "#":
field[nx[0]][nx[1]] = "V"
count = serch(nx, count)
return count
K=int(input())
s = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14,
1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
print(s[K-1])
| 0 | null | 67,021,148,164,950 | 232 | 195 |
s = input()
p = input()
s += s
ans = "No"
for i in range(len(s)//2):
if s[i] == p[0]:
for j in range(len(p)):
if s[i+j] != p[j]:
break
if j == len(p)-1:
ans = "Yes"
print(ans)
|
import sys
s1=raw_input()
s2=raw_input()
for idx, s in enumerate(s1):
if s2[0] != s:
continue
s1_idx = idx
s2_idx = 0
while True:
s1_now = s1[s1_idx:]
s2_now = s2[s2_idx:]
if len(s1_now) >= len(s2_now):
if s1_now.startswith(s2_now):
print "Yes"
sys.exit(0)
else:
break
else:
if s2_now.startswith(s1_now):
s1_idx = 0
s2_idx += len(s1_now)
else:
break
print "No"
| 1 | 1,704,483,504,394 | null | 64 | 64 |
n=int(input())
t=0
flag=False
for i in range(1,10):
for j in range(1,10):
if n==i*j:
print('Yes')
t+=1
flag=True
break
if flag:
break
if t==0:
print('No')
|
import sys
import decimal # 10進法に変換,正確な計算
def input():
return sys.stdin.readline().strip()
def main():
r = int(input())
print(r**2)
main()
| 0 | null | 151,875,800,955,648 | 287 | 278 |
import math
def Koch(Px, Py, Qx, Qy, n) :
if n == 0 :
return
else :
Ax = (2 * Px + Qx) / 3
Ay = (2 * Py + Qy) / 3
Bx = (Px + 2 * Qx) / 3
By = (Py + 2 * Qy) / 3
Cx = Ax + (Bx - Ax) * math.cos(math.pi/3) - (By - Ay) * math.sin(math.pi/3)
Cy = Ay + (Bx - Ax) * math.sin(math.pi/3) + (By - Ay) * math.cos(math.pi/3)
Koch(Px, Py, Ax, Ay, n-1)
print(Ax, Ay)
Koch(Ax, Ay, Cx, Cy, n-1)
print(Cx, Cy)
Koch(Cx, Cy, Bx, By, n-1)
print(Bx, By)
Koch(Bx, By, Qx, Qy, n-1)
n = int(input())
print(0, 0.00000000)
Koch(0.00000000, 0.00000000, 100.00000000, 0.00000000, n)
print(100.00000000, 0.00000000)
|
import heapq
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop
#------------------------------------------#
BIG_NUM = 2000000000
HUGE_NUM = 9999999999999999
MOD = 1000000007
EPS = 0.000000001
def MIN(A,B):
if A <= B:
return A
else:
return B
def MAX(A,B):
if A >= B:
return A
else:
return B
#------------------------------------------#
class Point:
def __init__(self,arg_x,arg_y):
self.x = arg_x
self.y = arg_y
def roll(div1,div2):
tmp_start = Point(div2.x-div1.x,div2.y-div1.y)
tmp_end = Point(tmp_start.x*math.cos(math.pi/3) -tmp_start.y*math.sin(math.pi/3),
tmp_start.x*math.sin(math.pi/3) + tmp_start.y*math.cos(math.pi/3))
ret = Point(tmp_end.x + div1.x,tmp_end.y + div1.y)
return ret;
def outPut(point):
print("%.8f %.8f"%(point.x,point.y))
def calc(left,right,count):
div1 = Point((2*left.x + right.x)/3,(2*left.y + right.y)/3)
div2 = Point((2*right.x + left.x)/3,(2*right.y + left.y)/3)
peek = roll(div1,div2)
if count > 1:
calc(left,div1,count-1)
calc(div1,peek,count-1)
calc(peek,div2,count-1)
calc(div2,right,count-1)
else:
outPut(left);
outPut(div1);
outPut(peek);
outPut(div2);
N = int(input())
left = Point(0,0)
right = Point(100,0)
if N == 0:
outPut(left)
outPut(right)
else:
calc(left,right,N)
outPut(right)
| 1 | 130,521,929,604 | null | 27 | 27 |
n, k = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))
A.sort()
F.sort(reverse=True)
l = -1
r = 10 ** 13
while r - l > 1:
m = (l + r) // 2
if sum([(a - m // f) if a * f > m else 0 for a, f in zip(A, F)]) <= k:
r = m
else:
l = m
print(r)
|
if __name__ == '__main__':
N = int(input())
l = input().split()
num = [int(i) for i in l]
print(min(num), max(num), sum(num))
| 0 | null | 82,749,462,452,218 | 290 | 48 |
from bisect import bisect_left
N=int(input())
A=sorted(list(map(int,input().split())))
cnt=0
for i in range(N-1):
for j in range(i+1,N):
a=bisect_left(A,A[i]+A[j])
if j<a:
cnt+=a-j-1
print(cnt)
|
from bisect import bisect_left
n = int(input())
L = sorted(list(map(int, input().split())))
# 二分探索
cnt = 0
for i in range(n):
for j in range(i+1, n):
a, b = L[i], L[j]
# c < a + b
r = bisect_left(L, a+b)
l = j+1
cnt += max(0, r-l)
print(cnt)
| 1 | 171,707,542,046,940 | null | 294 | 294 |
n = int(input())
furui = [i for i in range(10**6+2)]
ans = 9999999999999
yakusuu = []
for i in range(1,int(n**0.5)+1+1):
if n%i == 0:
yakusuu.append(i)
for i in yakusuu:
ans = min(i+n//i,ans)
# print(i,ans)
print(ans-2)
|
N, A, B = map(int, input().split())
A, B = min(A, B), max(A, B)
d = B - A
if d % 2 == 0:
print(d // 2)
else:
if (A - 1 <= N - B):
A, B = A - 1, B - 1
else:
A, B = N - B, N - A
ans = min(B, A + 1 + (B - A - 1) // 2)
print(ans)
| 0 | null | 135,323,151,428,608 | 288 | 253 |
s = input()
n = int(input())
for i in range(n):
order, a, b, *c = input().split()
a = int(a)
b = int(b)
if order == 'replace':
s = s[:a] + c[0] + s[b+1:]
elif order[0] == 'r':
s = s[:a] + s[::-1][len(s)-b-1:len(s)-a]+ s[b+1:]
else:
print(s[a:b+1])
|
tgtstr = list(raw_input())
n = int(raw_input())
for i in range(n):
cmd_list = raw_input().split(" ")
if cmd_list[0] == "replace":
for idx in range(int(cmd_list[1]), int(cmd_list[2])+1):
tgtstr[idx] = cmd_list[3][idx - int(cmd_list[1])]
elif cmd_list[0] == "reverse":
start = int(cmd_list[1])
end = int(cmd_list[2])
while True:
tmp = str(tgtstr[start])
tgtstr[start] = str(tgtstr[end])
tgtstr[end] = tmp
start += 1
end -= 1
if start >= end:
break
elif cmd_list[0] == "print":
print "".join(tgtstr[int(cmd_list[1]):int(cmd_list[2])+1])
| 1 | 2,128,039,200,330 | null | 68 | 68 |
from math import *
from collections import *
N, D, A = list(map(int, input().split()))
ans = 0
t = 0
q = deque()
XH = sorted([list(map(int, input().split())) for i in range(N)])
for x, h in XH:
if q:
while q and q[0][0] < (x-D):
_, c = q.popleft()
t -= c
h = h - t * A
if h <= 0: continue
x += D
c = ceil(h/A)
t += c
ans += c
q.append((x, c))
print(ans)
|
n = int(input())
a = list(map(int, input().split(" ")))
for lil_a in a[-1:0:-1]:
print("%d "%lil_a, end="")
print("%d"%a[0])
| 0 | null | 41,414,591,380,932 | 230 | 53 |
n=int(input())
E=[0]*n
for _ in range(n):
u,k,*V=map(int,input().split())
E[u-1]=sorted(V)[::-1]
todo=[1]
seen=[0]*n
d=[0]*n
f=[0]*n
t=0
while 0 in f:
if not todo:
todo.append(seen.index(0)+1)
if seen[todo[-1]-1]==0:
seen[todo[-1]-1]=1
t+=1
d[todo[-1]-1]=t
for v in E[todo[-1]-1]:
if d[v-1]==0:
todo.append(v)
elif f[todo[-1]-1]==0:
t+=1
f[todo.pop()-1]=t
else:
todo.pop()
for i in range(n):
print(i+1,d[i],f[i])
|
s = int(input())
a = []
a.append(1)
a.append(0)
a.append(0)
for i in range(3,s+1):
a.append(sum(a)-a[i-1]-a[i-2])
ans = a[-1] % (10**9 + 7)
print(ans)
| 0 | null | 1,664,506,164,870 | 8 | 79 |
def solve(n):
l = []
for i in range(int(n**(1/2))):
if n % (i+1) == 0:
a = i + 1
b = n // a
l.append(a+b-2)
return min(l)
print(solve(int(input())))
|
from itertools import product
n = int(input())
ans = n-1
for a in range(1, int(n**0.5)+1):
if n%a==0:
b = n//a
ans = min(ans, b+a-2)
print(ans)
| 1 | 161,966,451,462,420 | null | 288 | 288 |
import collections
def U1(a,b):
if b > a:
b, a = a, b
while a%b != 0:
r = a%b
a = b
b = r
return b
K = int(input())
c = 0
arr = []
for i in range(1,K+1):
for j in range(1,K+1):
arr.append(U1(i,j))
arr=collections.Counter(arr)
for key,value in arr.items():
for k in range(1,K+1):
c += U1(k,key)*value
print(c)
|
import itertools
import functools
import math
def OK():
N = int(input())
sum = 0
cnt = 0
for i in range(1,N+1):
for j in range(1,N+1):
chk = math.gcd(i,j)
for k in range(1,N+1):
sum += math.gcd(chk,k)
print(sum)
OK()
| 1 | 35,679,901,127,362 | null | 174 | 174 |
x1,y1,x2,y2=map(float,input().split())
if x1<=x2:
x=x2-x1
else:
x=x1-x2
if y1<=y2:
y=y2-y1
else:
y=y1-y2
t=x**2+y**2
t1=t**(1/2)
print(f'{t1:10.8f}')
|
from __future__ import division, print_function
from sys import stdin
from math import hypot, sqrt
x1, y1, x2, y2 = (float(s) for s in stdin.readline().split())
print('{:.4f}'.format(hypot(x1-x2, y1-y2)))
| 1 | 160,545,450,550 | null | 29 | 29 |
#!/usr/bin/env python3
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
n, a, b = map(int, input().split())
MOD = 10**9 + 7
# 二項展開より 2**n - 1 - nCa - nCb を MOD 10**9 + 7 で求めればよい
def combination(n, a):
# nCk = n! / k! (n-k)! のうち、 n!/(n-k)! を計算する
res = 1
div = 1
for i in range(a):
res *= n-i
res %= MOD
div *= a - i
div %= MOD
# print(f'n {n}, a {a}, res {res}, div {div}')
res = (res * pow(div, MOD-2, MOD)) % MOD
return res
count = (pow(2, n, MOD) - 1 - combination(n, a) - combination(n, b)) % MOD
print(count)
|
I=input;s=I()*2;print(['No','Yes'][I()in s])
| 0 | null | 33,865,760,796,732 | 214 | 64 |
n = int(input())
tp = 0
hp = 0
for i in range(n):
tarou, hanako = map(str,input().split())
if(tarou > hanako):
tp += 3
elif(tarou < hanako):
hp += 3
else:
tp += 1
hp += 1
print("%d %d" %(tp,hp))
|
N = map( int , raw_input().split() )
N.sort()
print "%d %d %d" %( N[0] , N[1] , N[2] )
| 0 | null | 1,203,047,983,140 | 67 | 40 |
n,x,t = map(int,input().split())
ans = (((n+x-1)//x)*t)
print(ans)
|
n, x, t = map(int, input().split())
if n % x == 0:
res = int((n / x) * t)
else:
res = int(((n // x) + 1) * t)
print(res)
| 1 | 4,247,427,763,920 | null | 86 | 86 |
from time import time
from random import randint
from numba import njit
from numpy import int64
@njit('i8(i8[:],i8[:])')
def func(s,x):
last=[0]*26
score=0
for i,v in enumerate(x,1):
last[v]=i
c=0
for j in range(26):
c+=s[j]*(i-last[j])
score=score+s[i*26+v]-c
return score
def main():
start=time()
d,*s=map(int,open(0).read().split())
s=int64(s)
x=int64(([*range(26)]*15)[:d])
M=func(s,x)
while time()-start<1.3:
y=x.copy()
if randint(0,1):
y[randint(0,d-1)]=randint(0,25)
else:
i=randint(0,d-15)
j=randint(i+1,i+7)
k=randint(j+1,j+7)
if randint(0,1):
y[i],y[j],y[k]=y[j],y[k],y[i]
else:
y[i],y[j],y[k]=y[k],y[i],y[j]
t=func(s,y)
if t>M:
M=t
x=y
print(*x+1,sep='\n')
main()
|
import random
import time
import copy
def down_score(d, c, last_d, score):
sum = 0
for i in range(26):
sum = sum + c[i]*(d-last_d[i])
return int(score - sum)
def main():
D = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for i in range(D)]
start = time.time()
last_d = [0 for i in range(26)]
ans = []
score1 = 0
for i in range(D):
max = 0
idx = 0
for j in range(26):
if max < (s[i][j] + c[j] * (i-last_d[j])*(i-last_d[j]+1)/2) and c[j] != 0:
max = s[i][j] + c[j] * (i-last_d[j])*(i-last_d[j]+1)/2
idx = j
elif max == (s[i][j] + c[j] * (i-last_d[j])*(i-last_d[j]+1)/2) and c[j] * (i-last_d[j])*(i-last_d[j]+1)/2 > c[idx]* (i-last_d[idx])*(i-last_d[idx]+1)/2 and c[j] != 0:
idx = j
last_d[idx] = i+1
score1 += s[i][idx]
score1 = down_score(i+1,c,last_d,score1)
ans.append(idx)
while time.time() - start < 1.9:
cp = ans.copy()
last_d = [0 for i in range(26)]
score2 = 0
idx1 = random.randint(0,25)
idx2 = random.randint(0,25)
idx3 = random.randint(0,25)
if random.randint(0,1):
d1 = random.randint(0,D-1)
d2 = random.randint(0,D-1)
d3 = random.randint(0,D-1)
if idx1 == idx2 or idx1 == idx3 or idx2 == idx3:
continue
if random.randint(0,1):
ans[d1] = idx1
elif random.randint(0,1):
ans[d1] = idx1
ans[d2] = idx2
else:
ans[d1] = idx1
ans[d2] = idx2
ans[d3] = idx3
#2値入れ替え
elif random.randint(0,1):
d1 = random.randint(0,D-8)
d2 = random.randint(d1+1,d1+7)
tmp1 = ans[d1]
tmp2 = ans[d2]
if random.randint(0,1):
ans[d1] = tmp2
ans[d2] = tmp1
#3値入れ替え
else:
d1 = random.randint(0,D-11)
d2 = random.randint(d1+1,d1+5)
d3 = random.randint(d2+1,d2+5)
tmp1 = ans[d1]
tmp2 = ans[d2]
tmp3 = ans[d3]
if random.randint(0,1):
ans[d1] = tmp2
ans[d2] = tmp3
ans[d3] = tmp1
else:
ans[d1] = tmp3
ans[d2] = tmp1
ans[d3] = tmp2
for i in range(D):
score2 += s[i][ans[i]]
last_d[ans[i]] = i+1
score2 = down_score(i+1, c, last_d, score2)
if score1 > score2:
ans = cp.copy()
else:
score1 = score2
for i in range(D):
print(ans[i]+1)
if __name__ == "__main__":
main()
| 1 | 9,670,805,359,820 | null | 113 | 113 |
# coding: utf-8
N = 10
h = []
for n in range(N):
h.append(int(input()))
h = sorted(h, reverse = True)
for i in range(3):
print(h[i])
|
mountains = [int(input()) for _ in range(10)]
for height in sorted(mountains)[-3:][::-1]:
print(height)
| 1 | 26,902,578 | null | 2 | 2 |
import sys
n,m = map(int, input().split())
num = [0]*n
al = [-1]*n
Ans = 0
for i in range(m):
s,c = map(int, input().split())
if num[s-1] != 0 and num[s-1] != c:
print(-1)
sys.exit()
else:
num[s-1] = c
al[s-1] = c
if al.count(0)==n and n >= 2:
print(-1)
sys.exit()
if al[0] == 0and n >= 2:
print(-1)
sys.exit()
if num[0] == 0 and n >= 2:
num[0] = 1
for i in range(len(num)):
Ans = Ans + num[i] * (10 ** (n-i-1))
if Ans == 0 and n >= 2:
print(-1)
else:
print(Ans)
|
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from math import *
sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7
def mint():
return map(int,input().split())
def lint():
return list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
print(l[0] if x else l[1])
def lprint(l, sep='\n'):
for x in l:
print(x, end=sep)
N,M = mint()
C = []
for _ in range(M):
s,c = mint()
C.append([s,c])
for x in range(10**(N-1)-(N==1),10**N):
x = str(x)
if all([x[s-1]==str(c) for s,c in C]):
print(x)
exit()
print(-1)
| 1 | 60,649,649,649,440 | null | 208 | 208 |
letters = input()
num = int(input())
for i in range(num):
order = input().split()
a = int(order[1])
b = int(order[2])
if order[0] == 'print':
print(letters[a:b+1])
if order[0] == 'reverse':
p = letters[a:b+1]
p = p[::-1]
letters = letters[:a] + p + letters[b+1:]
if order[0] == 'replace':
letters = letters[:a] + order[3] + letters[b+1:]
|
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np # cumsum
# from bisect import bisect_left, bisect_right
def solve():
N, M, Q = MI()
D = [[] for _ in range(Q)]
for i in range(Q):
a, b, c, d = MI()
a -= 1
b -= 1
D[i] = [a, b, c, d]
ans = 0
for K in comb_w(range(1, M+1), N):
score = 0
for q in range(Q):
a, b, c, d = D[q]
if K[b] - K[a] == c:
score = score + d
ans = max(ans, score)
print(ans)
if __name__ == '__main__':
solve()
| 0 | null | 14,966,863,773,510 | 68 | 160 |
n,m,l=map(int,raw_input().split())
A=[]
B=[]
for i in range(n):
A.append(map(int,raw_input().split()))
for i in range(m):
B.append(map(int,raw_input().split()))
for i in range(n):
print(' '.join(map(str,[sum([A[i][j]*B[j][k] for j in range(m)]) for k in range(l)])))
|
N, A, B = map(int, input().split())
if A == 0:
print(0)
exit()
quotient = N // (A + B) * A
remainder = N % (A + B)
quotient += min((remainder, A))
print(quotient)
| 0 | null | 28,632,891,627,692 | 60 | 202 |
n = int(input())
ans = 0
for i in range(1,n+1):
m = n // i
ans += m*(m+1)*i//2
print(ans)
|
N=int(input())
out=0
for X in range(1,N+1):
Y=N//X
out+=Y*(Y+1)*X/2
print(int(out))
| 1 | 11,111,173,987,238 | null | 118 | 118 |
a=input()
b=list(a)
if b[-1]=="s":
print(a+"es")
else:
print(a+"s")
|
def resolve():
import math
n, m = list(map(int, input().split()))
ans1 = math.factorial(n) // (math.factorial(n-2) * 2) if n > 1 else 0
ans2 = math.factorial(m) // (math.factorial(m-2) * 2) if m > 1 else 0
print(ans1 + ans2)
resolve()
| 0 | null | 24,079,454,452,230 | 71 | 189 |
n=int(input())
s=[int(x) for x in list(input())]
ans=0
for a in range(10):
for i in range(n):
if s[i]==a:
a_dx=i
break
else:
continue
for b in range(10):
for i in range(a_dx+1,n):
if s[i]==b:
b_dx=i
break
else:
continue
for c in range(10):
for i in range(b_dx+1,n):
if s[i]==c:
ans+=1
break
else:
continue
print(ans)
|
import sys
readline = sys.stdin.readline
n = int(readline())
s = readline().rstrip()
from collections import deque
q = deque([])
ss = set(s)
for i in ss:
q.append(["",0,str(i)])
ans = 0
while q:
num, ind, target = q.popleft()
#print(num, ind, target)
while ind < len(s):
if s[ind] == target: #s[ind]が0から9の中のどれかを見てる
break
ind += 1
else:
continue
num += target
if len(num) == 3:
ans += 1
continue
sss = set(s[ind+1:])
for i in sss:
q.append([num, ind +1, str(i)]) #出現した数字に関するキューを追加(例:一回目は024)
print(ans)
| 1 | 128,774,489,190,218 | null | 267 | 267 |
# coding: utf-8
from collections import deque
n, quantum = [int(i) for i in input().split()]
dq = deque()
for i in range(n):
p, time = [str(i) for i in input().split()]
dq.append([p, int(time)])
time = 0
while(len(dq) != 0):
#引き算処理
#処理が終了しない場合
if dq[0][1] > quantum:
dq[0][1] -= quantum
dq.rotate(-1)
time += quantum
#処理が終了する場合
else:
time += dq[0][1]
print("{} {}".format(dq[0][0], time))
dq.popleft()
|
class Queue():
def __init__(self):
self.el = []
def add(self, el):
self.el.append(el)
def delete(self):
return self.el.pop(0)
def action(self):
self.el[0]['time'] -= q
tmp = self.delete()
self.el.append(tmp)
n, q = map(int, raw_input().split())
qu = Queue()
for i in range(n):
name, time = raw_input().split()
time = int(time)
qu.add({'name':name, 'time':time})
time = 0
while qu.el:
#print qu.el
if qu.el[0]['time'] > q:
qu.action()
time += q
else:
time += qu.el[0]['time']
print qu.el[0]['name'], time
qu.delete()
| 1 | 44,696,615,740 | null | 19 | 19 |
import sys
nyuukyosha = []
for tou in range(4):
nyuukyosha.append([])
for kai in range(3):
nyuukyosha[tou].append([])
for heya in range(10):
nyuukyosha[tou][kai].append(0)
n = int(raw_input())
for i in range(n):
data = map(int, raw_input().split())
a = data[0] - 1
b = data[1] - 1
c = data[2] - 1
nyuukyosha[a][b][c] += data[3]
for tou in range(4):
for kai in range(3):
for heya in range(10):
sys.stdout.write(" {:}".format(nyuukyosha[tou][kai][heya]))
print("")
if tou < 3:
print("####################")
|
l = [[[0 for r in range(10)] for f in range(3)] for b in range(4)]
n = int(input())
for num in range(n):
b,f,r,v = [int(x) for x in input().split()]
l[b-1][f-1][r-1] += v
for b in range(4):
for f in range(3):
for r in range(10):
print('', l[b][f][r], end='')
print("")
if b != 3:
print("#" * 20)
| 1 | 1,101,734,228,580 | null | 55 | 55 |
import numpy as np
#n = int(input())
a = list(map(int, input().rstrip().split()))
out=0
for i in a:
if i > 1:
out += i*(i-1)//2
print(out)
|
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 = map(int, readline().split())
ans = (N + M) * (N + M - 1) // 2 - N * M
print(ans)
return
if __name__ == '__main__':
main()
| 1 | 45,344,802,473,212 | null | 189 | 189 |
h,w,K=map(int,input().split())
s=[list(input()) for i in range(h)]
from itertools import groupby
ans=10**18
for i in range(1<<h):
pre=-1
c=[]
t_cnt=0
op=[-1]*h
for j in range(h):
if i&(1<<j):op[j]=1
else:op[j]=0
gr=groupby(op)
l=[]
for key,g in gr:
l.append(len(list(g)))
c=[[0]*w for _ in range(len(l))]
idx=0
jdx=0
for idx,li in enumerate(l):
for k in range(li):
for a in range(w):
c[idx][a]+=int(s[jdx][a])
jdx+=1
# print(c)
t_cnt=len(c)-1
t_cnt1=0
c_mx=[0]*len(c)
ok=True
for a in range(w):
for b in range(len(c)):
if c[b][a]>K:
ok=False
c_mx[b]+=c[b][a]
if c_mx[b]>K:
t_cnt1+=1
c_mx=[0]*len(c)
for b in range(len(c)):
c_mx[b]+=c[b][a]
break
if ok:
ans=min(ans,t_cnt+t_cnt1)
if ans==10**18:
print(0)
else:
print(ans)
|
import itertools
H, W, K = map(int, input().split())
grid = []
for _ in range(H):
line = list(input())
grid.append(line)
# 横に割る方法は2**(H-1)、高々2**9=512通り
# これを決め打ちして、縦の割り方はGreedyに
ans = 10**9
# 割る場合を1, 割らない場合を0で表すことにする
for ptn in itertools.product([0, 1], repeat=H - 1):
# 不可能な場合、そのパターンはスキップする############################
able = True
for w in range(W):
# それぞれのブロックにおける白の数
lines = [0] * (sum(ptn) + 1)
# 今、上から何ブロック目か
line_num = 0
for h in range(H):
# 白の場合
if grid[h][w] == "1":
lines[line_num] += 1
# 境界を引く場合
# h=H-1の時、indexが範囲外になるのでminを使って調整
if ptn[min(h, H - 2)] == 1:
line_num += 1
# 1列でK個を超えるブロックがある場合、その割り方は不適。
if max(lines) > K:
able = False
break
if able == False:
continue
# 以下、実現が可能なものとして考える################################
# それぞれのブロックにおける白の数
lines = [0] * (sum(ptn) + 1)
# 横に割った回数
cnt = sum(ptn)
# ダメだったら1個前に戻る、という処理をしたいのでwhileで書く
# for内でデクリメントしてもループにならない(自戒)
w = 0
while w < W:
line_num = 0
for h in range(H):
# 白の場合
if grid[h][w] == "1":
lines[line_num] += 1
# 境界を引く場合
# h=H-1の時、indexが範囲外になるのでminを使って調整
if ptn[min(h, H - 2)] == 1:
line_num += 1
# Kを超えるブロックができてしまう場合
# 一つ前の列で切って、今の列からlinesを数え直す
if max(lines) > K:
cnt += 1
lines = [0] * (sum(ptn) + 1)
w -= 1
w += 1
ans = min(ans, cnt)
print(ans)
| 1 | 48,418,165,858,678 | null | 193 | 193 |
from fractions import gcd
a,b=map(int,input().split())
print(a*b//gcd(a,b))
|
a=input()
list1=a.split()
list2=list(map(int,list1))
K=list2[0]
X=list2[1]
if 500*K>=X:
print("Yes")
else:
print("No")
| 0 | null | 105,497,494,859,172 | 256 | 244 |
# 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()))
S = str(input())
if S[2] == S[3] and S[4] == S[5]:
ans = "Yes"
else:
ans = "No"
print(ans)
|
import sys
s = input().strip()
if s[2] == s[3] and s[4] == s[5]:
print("Yes")
else : print("No")
| 1 | 42,022,199,725,632 | null | 184 | 184 |
#!/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()
print((N/3)**3)
main()
|
h,w,k = map(int,input().split())
ls = [str(input()) for _ in range(h)]
a = [[] for _ in range(2**h)]
b = [[] for _ in range(2**w)]
for i in range(2**h):
for j in range(h):
if (i>>j)&1:
a[i].append(j)
for s in range(2**w):
for l in range(w):
if (s>>l)&1:
b[s].append(l)
p = 0
for e in range(2**h):
for f in range(2**w):
cnt = 0
for g in range(len(ls)):
if g not in a[e]:
di = list(ls[g])
for t in range(len(di)):
if t not in b[f]:
if di[t] == "#":
cnt += 1
if cnt == k:
p += 1
print(p)
| 0 | null | 27,925,110,256,292 | 191 | 110 |
N, M = (int(x) for x in input().split())
if N==M:
print("Yes")
else:
print("No")
|
#!/usr/bin/env python3
def main():
n = int(input())
S, T = input().split()
print(*[s + t for s, t in zip(S, T)], sep="")
if __name__ == "__main__":
main()
| 0 | null | 97,761,420,155,168 | 231 | 255 |
print(sum(i**4%15%2*i for i in range(int(input())+1)))
|
import sys
def LI2(): return list(map(int,sys.stdin.readline().rstrip())) #空白なし
def S(): return sys.stdin.readline().rstrip()
N,P = map(int,S().split())
S = LI2()
if P == 2 or P == 5: #10と素でない場合一番右の桁で決まる
ans = 0
for i in range(N):
if S[i] % P == 0:
ans += i+1
print(ans)
exit()
A = [0] #A[i] = 下i桁をPで割った余り
for i in range(1,N+1):
A.append((A[i-1]+pow(10,i,P)*S[N-i]) % P)
from collections import Counter
c = Counter(A)
B = c.values()
ans = 0
for i in B:
ans += (i*(i-1)//2)
print(ans)
| 0 | null | 46,314,785,365,280 | 173 | 205 |
a,b=[int(i) for i in input().split()]
c=500*a
if(c>=b):
print('Yes')
else:
print('No')
|
from collections import Counter
ABC = list(map(int, input().split()))
ABC = Counter(ABC)
if len(ABC) == 2:
ans = 'Yes'
else:
ans = 'No'
print(ans)
| 0 | null | 82,637,093,193,200 | 244 | 216 |
s = input()
print(s.swapcase())
|
def main():
S = input()
print(S.swapcase())
main()
| 1 | 1,514,481,361,520 | null | 61 | 61 |
H, N = map(int, input().split())
A = map(int, input().split())
print("Yes" if H <= sum(A) else "No")
|
#!python3
LI = lambda: list(map(int, input().split()))
# input
N, S = LI()
A = LI()
MOD = 998244353
def main():
dp = [0] * (S + 1)
dp[0] = 1
for i in range(N):
dp, l = [0] * (S + 1), dp
for j in range(S + 1):
dp[j] = (dp[j] + 2 * l[j]) % MOD
if A[i] + j <= S:
dp[A[i] + j] = (dp[A[i] + j] + l[j]) % MOD
ans = dp[-1]
print(ans)
if __name__ == "__main__":
main()
| 0 | null | 48,018,943,907,332 | 226 | 138 |
# -*-coding:utf-8
#import fileinput
import math
def main():
n = int(input())
for i in range(1, n+1):
x = i
if (x%3 == 0):
print(' %d' % i, end='')
else:
while(x):
if (x%10 == 3):
print(' %d' % i, end='')
break
else:
x = int(x/10)
print('')
if __name__ == '__main__':
main()
|
n = int(raw_input())
ls = []
for i in range(3,n+1):
if i%3==0 or "3" in str(i) :
ls.append(i)
print " " + " ".join(map(str,ls))
| 1 | 917,960,525,354 | null | 52 | 52 |
money = [0, 300000, 200000, 100000]
a,b = map(int, input().split())
st = money[a if a < len(money) else 0] \
+ money[b if b < len(money) else 0]
if a == 1 and b == 1:
st += 400000
print(st)
|
while True:
L = map(int,raw_input().split())
H = (L[0])
W = (L[1])
if H == 0 and W == 0:break
if W % 2 == 1:
for x in range(0,H):
if x % 2 == 0:
print "{}#".format("#."* (W / 2))
else:
print "{}.".format(".#"* (W / 2))
else:
for x in range(0,H):
if x % 2 == 0:
print "#." * (W / 2)
else:
print ".#" * (W / 2)
print ""
| 0 | null | 71,019,094,703,788 | 275 | 51 |
n=int(input())
count=0
countmax=0
for i in range(n):
a,b = map(int,input().split())
if a==b:
count+=1
else:
countmax=max(countmax,count)
count=0
countmax=max(countmax,count)
if countmax>2:
print("Yes")
else:
print("No")
|
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")
#solve
def solve():
n = II()
d = LIR(n)
i = 0
for a, b in d:
if a == b:
i += 1
else:
i = 0
if i == 3:
print("Yes")
return
print("No")
return
#main
if __name__ == '__main__':
solve()
| 1 | 2,501,414,749,792 | null | 72 | 72 |
from collections import deque
import numpy as np
H, W = map(int, input().split())
grid = [input() for _ in range(H)]
ini = -1
q = deque()
dy = (-1, 0, 1, 0)
dx = (0, 1, 0, -1)
ans = 0
for y in range(H):
for x in range(W):
dist = [[ini for _ in range(W)] for _ in range(H)]
if grid[y][x] == "#": continue
dist[y][x] = 0
q.append((y, x))
while(len(q) > 0):
oy, ox = q.popleft()
for di in range(4):
ny = oy + dy[di]
nx = ox + dx[di]
if (ny < 0 or ny >= H or nx < 0 or nx >= W): continue
if grid[ny][nx] == "#": continue
if dist[ny][nx] != ini: continue
dist[ny][nx] = dist[oy][ox] + 1
q.append((ny, nx))
ans = max(np.max(dist), ans)
print(ans)
|
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *
H, W = MAP()
S = [input() for _ in range(H)]
dy = (1, 0, -1, 0)
dx = (0, -1, 0, 1)
def diff_max(start_y, start_x):
check = [[-1]*W for _ in range(H)]
check[start_y][start_x] = 0
q = deque([(start_y, start_x)])
while q:
y, x = q.popleft()
for i in range(4):
ny = y + dy[i]
nx = x + dx[i]
if 0 <= ny < H and 0 <= nx < W:
if check[ny][nx] == -1 and S[ny][nx] == ".":
check[ny][nx] = check[y][x] + 1
q.append((ny, nx))
return max([max(x) for x in check])
ans = -INF
for i in range(H):
for j in range(W):
if S[i][j] == ".":
ans = max(ans, diff_max(i, j))
print(ans)
| 1 | 94,393,136,948,504 | null | 241 | 241 |
n=int(input())
a=list(map(int,input().split()))
tot=sum(a)
b=0
c=10**10
d=0
for i in range(n):
b+=a[i]
d=abs(tot/2 - b)
if d<c:
c=d
print(int(2*c))
|
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)
| 0 | null | 71,517,197,145,900 | 276 | 38 |
from bisect import bisect_left
n, m = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
A_r = sorted(A, reverse = True)
cumsumA = [0]
total = 0
for i in range(n):
total += A_r[i]
cumsumA.append(total)
def func(x):
ans = 0
for i in range(n):
kazu = x - A[i]
idx = bisect_left(A, kazu)
cou = n - idx
ans += cou
if ans >= m:
return True
else:
return False
mi = 0
ma = 2 * 10 ** 5 + 1
while ma - mi > 1:
mid = (mi + ma) // 2
if func(mid):
mi = mid
else:
ma = mid
ans = 0
count = 0
for ai in A_r:
idx = bisect_left(A, mi - ai)
ans += ai * (n - idx) + cumsumA[n - idx]
count += n - idx
print(ans - (count-m) * mi)
|
import sys
from collections import defaultdict
import bisect
import itertools
readline = sys.stdin.buffer.readline
# sys.setrecursionlimit(10**5)
def geta(fn=lambda s: s.decode()):
return map(fn, readline().split())
def gete(fn=lambda s: s.decode()):
return fn(readline().rstrip())
def main():
N, M = geta(int)
A = list(geta(int))
A.sort()
def c(x):
"""
@return # of handshake where hapiness >= x
"""
ret = 0
for a in A:
ret += bisect.bisect_left(A, x - a)
return N * N - ret
left, right = 0, 2 * A[-1] + 1
while left + 1 < right:
middle = (left + right) // 2
if c(middle) >= M:
left = middle
else:
right = middle
ans = 0
Acum = list(itertools.accumulate(A[::-1]))[::-1]
for a in A:
idx = bisect.bisect_left(A, right - a)
if idx < N:
ans += Acum[idx] + a * (N - idx)
ans += left * (M - c(right))
print(ans)
if __name__ == "__main__":
main()
| 1 | 108,264,820,490,940 | null | 252 | 252 |
from functools import lru_cache
N = int(input())
# A * B <= N - 1はいくつありますか?
count = 0
# A ∈ {1, 2, ..., N-1}
for A in range(1, N):
# B <= (N - 1) // A
count += (N - 1) // A
print(count)
|
a,b=map(int,input().split())
print((a//b)+1 if a%b!=0 else a//b)
| 0 | null | 39,626,459,843,488 | 73 | 225 |
n, m = map(int, input().split())
s = input()
x = n
roulette = []
while x > m:
for i in range(m, 0, -1):
if s[x-i] == '0':
x -= i
roulette.append(i)
break
else:
print(-1)
exit()
roulette.append(x)
roulette.reverse()
print(' '.join(map(str, roulette)))
|
s = 0
a = 0
for i in map(int, input().split()):
s += max(0, (4 - i) * 100000)
if i == 1:
a += 1
if a == 2:
s += 400000
print(s)
| 0 | null | 140,048,124,759,380 | 274 | 275 |
while True:
l = input()
if l == '-':
break
deck = list(l)
for i in range(int(input())):
n = int(input())
deck = deck[n:] + deck[:n]
print(''.join(deck))
|
import math
N, K = map(int, input().split())
A = list(map(float, input().split()))
min_A = 0
max_A = 10**10
while( max_A - min_A > 1):
now = (min_A + max_A) // 2
temp = 0
for i in A:
if i > now:
temp += (i // now)
if temp > K:
min_A = now
else:
max_A = now
print(int(min_A) + 1)
| 0 | null | 4,231,734,238,190 | 66 | 99 |
import math
N = int(input())
A = []
for i in range(2,int(math.sqrt(N))+1):
if N%i == 0:
A.append(i + (N//i) - 2)
if not A:
print(N-1)
else:
print(min(A))
|
from math import sqrt
from math import floor
n = int(input())
ans = 10 ** 12
m = floor(sqrt(n))
for i in range(1,m+1):
if n % i == 0:
j = n // i
ans = min(ans,i+j-2)
print(ans)
| 1 | 161,893,585,566,772 | null | 288 | 288 |
ST = list(map(str, input().split()))
word = ST[::-1]
print("".join(word))
|
x, y, z = input().split()
x, y = y, x
x, z = z, x
print(x, y, z)
| 0 | null | 70,702,148,555,218 | 248 | 178 |
import math
import sys
from functools import reduce
N, M = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N):
A[i] = A[i] // 2
def lcm_base(x, y):
return (x * y) // math.gcd(x, y)
def lcm_list(numbers):
return reduce(lcm_base, numbers, 1)
C = lcm_list(A)
B = [0 for _ in range(N)]
for i in range(N):
B[i] = C // A[i]
if B[i] % 2 == 0:
print(0)
sys.exit()
print( (M // C + 1) // 2)
|
from collections import deque
n,m=map(int,input().split())
node=[[] for _ in range(n)]
for i in range(m):
a,b=map(int,input().split())
node[b-1].append(a-1)
node[a-1].append(b-1)
distance=[-1]*n
distance[0]=0
d=deque()
d.append(0)
while d:
v=d.popleft()
for i in node[v]:
if distance[i]!=-1:
continue
distance[i]=v+1
d.append(i)
print('Yes')
for i in range(1,n):
print(distance[i])
| 0 | null | 60,939,134,734,858 | 247 | 145 |
#!/usr/bin/env python3
val1 = input()
if int(val1) >= 30:
print("Yes")
else:
print("No")
|
def CP(a,b):
x = a[1]*b[2]-a[2]*b[1]
y = a[2]*b[0]-a[0]*b[2]
z = a[0]*b[1]-a[1]*b[0]
return (x,y,z)
minus = lambda d:tuple(-i for i in d)
class dice:
def __init__(self,data):
names = [(1,0,0),(0,0,1),(0,1,0),(0,-1,0),(0,0,-1),(-1,0,0)]
self.faces = {n:d for n,d in zip(names,data)}
self.top = (1,0,0)
self.north = (0,0,-1)
def turn(self,direction):
if direction == 'N':
self.top,self.north = minus(self.north),self.top
elif direction == 'S':
[self.turn('N') for i in range(3)]
elif direction == 'W':
self.top = CP(self.top,self.north)
elif direction == 'E':
[self.turn('W') for i in range(3)]
if __name__ == '__main__':
data = [int(i) for i in input().split()]
operations = input()
d = dice(data)
[d.turn(o) for o in operations]
print(d.faces[d.top])
| 0 | null | 2,960,226,365,966 | 95 | 33 |
N = int(input())
N = N % 10
if N == 2 or N == 4 or N == 5 or N == 7 or N == 9:
print('hon')
elif N == 3:
print('bon')
else:
print('pon')
|
N = int(input())
if N % 10 in [2, 4, 5, 7, 9]:
print("hon")
elif N % 10 in [0, 1, 6, 8]:
print("pon")
else:
print("bon")
| 1 | 19,169,749,775,072 | null | 142 | 142 |
n = int(input())
a = list(map(int,input().split()))
a_cumsum = [0]*(n+1)
for i in range(n):
a_cumsum[i+1] = a_cumsum[i] + a[i]
ans = 10**18
for i in range(n):
ans = min(ans, abs(a_cumsum[-1] - a_cumsum[i]*2))
print(ans)
|
n = int(input())
l = list(map(int,input().split()))
from itertools import accumulate
cum=list(accumulate(l))
tot=sum(l)
ans=2020202020*100000
for i in cum:
ans=min(ans, abs(tot-i*2))
print(ans)
| 1 | 141,875,078,912,360 | null | 276 | 276 |
A=1
B=1
C=0 #2つ前の項
D=0 #1
N=int(input())
if N==0 or N==1:
print(1)
elif N==2:
print(2)
else:
C=B #1
D=A+B #2
for i in range (N-3):
B=C
C=D
D=B+C
print(C+D)
|
def main():
n = int(input())
dp = [0] * 100
dp[0] = 1
dp[1] = 1
for i in range(2, len(dp)):
dp[i] = dp[i - 1] + dp[i - 2]
print(dp[n])
if __name__ == '__main__':
main()
| 1 | 2,006,734,730 | null | 7 | 7 |
#!python3
# import numpy as np
# input
K = int(input())
def main():
l = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
ans = l[K-1]
print(ans)
if __name__ == "__main__":
main()
|
C = input()
alphabet = 'abcdefghijklmnopqrstuvwxyz'
print(alphabet[(alphabet.index(C) + 1) % len(alphabet)])
| 0 | null | 71,371,203,601,656 | 195 | 239 |
# coding: utf-8
# Your code here!
n = int(input())
M = []
for i in range(n):
adj = list(map(int,input().split()))
if adj[1] == 0:
M += [[]]
else:
M += [adj[2:]]
time = 1
ans = [[j+1,0,0] for j in range(n)] # id d f
while True:
for i in range(n):
if ans[i][1] == 0:
stack = [i]
ans[i][1] = time
time += 1
break
elif i == n-1:
for k in range(n):
print(*ans[k])
exit()
while stack != []:
u = stack[-1]
if M[u] != []:
for i in range(len(M[u])):
v = M[u][0]
M[u].remove(v)
if ans[v-1][1] == 0:
ans[v-1][1] = time
stack.append(v-1)
time += 1
break
else:
stack.pop()
ans[u][2] = time
time += 1
|
#coding: utf-8
while True:
m, f, r = (int(i) for i in input().split())
if m == f == r == -1:
break
if m == -1 or f == -1:
print("F")
elif m + f >= 80:
print("A")
elif m + f >= 65 and m + f < 80:
print("B")
elif (m + f >= 50 and m + f < 65) or r >= 50:
print("C")
elif m + f >= 30 and m + f < 50:
print("D")
elif m + f < 30:
print("F")
| 0 | null | 628,519,141,536 | 8 | 57 |
from itertools import*
n=input()
l = map(int,input().split())
ans = 0
for a, b, c in combinations(l, 3):
if a != b != c != a and a + b > c and b + c > a and c + a > b:
ans += 1
print(ans)
|
from fractions import gcd
from functools import reduce
def lcm(x,y):
return (x*y)//gcd(x,y)
def lcm_list(nums):
return reduce(lcm,nums,1)
mod=10**9+7
n=int(input())
A=list(map(int,input().split()))
num=lcm_list(A)
cnt=0
for i in A:
cnt +=num//i
print(cnt%mod)
| 0 | null | 46,475,527,985,426 | 91 | 235 |
a, b = map(int, input().split())
n = max([a - 2 * b, 0])
print(n)
|
a, b = map(int, input().split())
if b * 2 >= a:
print('0')
else:
print(a - b * 2)
| 1 | 166,780,899,319,452 | null | 291 | 291 |
s=input()
s=s.replace('?','D')
print(s)
|
N, M = map(int, input().split())
A = list(map(int, input().split()))
if N - sum(A) <= 0:
result = "Yes"
else:
result = "No"
print(result)
| 0 | null | 48,191,130,101,722 | 140 | 226 |
A,B,M=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
xyc=[list(map(int,input().split()))for i in range(M)]
ans=min(a)+min(b)
for i in range(M):
tmp = a[xyc[i][0]-1]+b[xyc[i][1]-1]-xyc[i][2]
if ans > tmp:
ans=tmp
print(ans)
|
a,b,m = map(int,input().split())
r = tuple(map(int,input().split()))
d = tuple(map(int,input().split()))
ans = min(r)+min(d)
for i in range(m):
x,y,c = map(int,input().split())
ans = min(ans,r[x-1]+d[y-1]-c)
print(ans)
| 1 | 54,229,826,465,210 | null | 200 | 200 |
n,k = map(int, input().split())
al = list(map(int, input().split()))
for _ in range(min(100,k)):
imos = [0]*(n+1)
for i,a in enumerate(al):
l = max(0,i-a)
r = min(n,i+a+1)
imos[l] += 1
imos[r] -= 1
new_al = []
curr_val = 0
for im in imos[:-1]:
curr_val += im
new_al.append(curr_val)
al = new_al[:]
print(*al)
|
#!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()
A = LI()
for _ in range(K):
B = [0]*(N+1)
for i in range(N):
l = max(i - A[i], 0)
r = min(A[i] + i + 1, N)
B[l] += 1
B[r] -= 1
for i in range(N):
B[i+1] += B[i]
if A == B:
break
A = B
print(*A[:-1], sep=" ")
main()
| 1 | 15,434,701,157,282 | null | 132 | 132 |
n = int(input())
a = list(map(int, input().split()))
a.sort()
def main(a):
if 0 in a:
ans = 0
else:
ans = 1
for item in a:
ans *= item
if ans > 1000000000000000000:
ans = -1
break
return ans
ans = main(a)
print(ans)
|
#10^18こえたら-1
n = int(input())
Arr = list(map(int, input().split( )))
Arr = sorted(Arr)
if Arr[0] == 0:
print(0)
exit()
s = 1
for i in range(n):
s *= Arr[i]
if s > 10**18:
print(-1)
exit()
print(s)
| 1 | 16,170,104,279,702 | null | 134 | 134 |
s = input()
p = input()
if len(p) <= len(s) and (s+s).find(p) > -1:
print("Yes")
else:
print("No")
|
n, m = map(int, input().split())
A = list(map(int, input().split()))
s = 0
for i in range(m):
s += A[i]
if s > n:
print(-1)
elif s <= n:
print(n - s)
| 0 | null | 16,822,305,491,868 | 64 | 168 |
n = int(input())
if n//100*5 >= n%100:
print(1)
else:
print(0)
|
#初期定義
global result
global s_list
result = 0
#アルゴリズム:ソート
def merge(left, mid, right):
global result
n1 = mid - left
n2 = right - mid
inf = 10**9
L_list = s_list[left: mid] + [inf]
R_list = s_list[mid: right] + [inf]
i = 0
j = 0
for k in range(left, right):
result += 1
if L_list[i] <= R_list[j]:
s_list[k] = L_list[i]
i += 1
else:
s_list[k] = R_list[j]
j += 1
#アルゴリズム:マージソート
def mergeSort(left, right):
if (left + 1) < right:
mid = (left + right) // 2
mergeSort(left, mid)
mergeSort(mid, right)
merge(left, mid, right)
#初期値
n = int(input())
s_list = list(map(int, input().split()))
#処理の実行
mergeSort(0, n)
#結果の表示
print(" ".join(map(str, s_list)))
print(result)
| 0 | null | 63,882,140,922,698 | 266 | 26 |
import sys
input = sys.stdin.buffer.readline
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict
from itertools import combinations, permutations
from itertools import accumulate
from math import ceil, sqrt, pi
MOD = 10 ** 9 + 7
INF = 10 ** 18
def divisors(n) -> list:
res = []
for i in range(1, int(n ** 0.5) + 1):
if n % i == 0:
res.append(i)
if i != n // i:
res.append(n // i)
return res
N, K = map(int, input().split())
MOD = 10 ** 9 + 7
cnt = defaultdict(int)
for k in range(1, K + 1):
for d in divisors(k):
cnt[d] += 1
#print(cnt)
tmp = defaultdict(int)
for k in range(K, 0, -1):
tmp[k] = pow(cnt[k], N, MOD)
for i in range(2, K // k + 1):
tmp[k] -= tmp[i * k]
#print(tmp)
ans = 0
for k in range(1, K + 1):
ans = (ans + k * tmp[k]) % MOD
print(ans)
|
import math
import bisect
MOD = 1000000007
N = int(input())
ABs = []
count00 = 0
for i in range(N):
A, B = map(int, input().split())
if A == 0:
if B == 0:
count00 += 1
else:
ABs.append([0, -1])
elif B == 0:
ABs.append([1, 0])
else:
mygcd = math.gcd(A, B)
A, B = A//mygcd, B//mygcd
if A < 0:
ABs.append([-A, -B])
else:
ABs.append([A, B])
N2 = N - count00
if N2 == 0:
print(count00)
exit()
ABs.sort(key = lambda x:x[1])
Bs = [i[1] for i in ABs]
index0 = bisect.bisect_left(Bs, 0)
if (index0==0) or (index0==N2):
print((pow(2, N2, MOD) +count00 -1) %MOD)
exit()
btm = ABs[:index0]
top = ABs[index0:]
lenbtm = index0
lentop = N2 - index0
for i in range(lenbtm):
btm[i] = [-btm[i][1], btm[i][0]]
btmdict = {}
topdict = {}
for i in btm:
mykey = str(i[0]) + '-' + str(i[1])
btmdict[mykey] = 0
topdict[mykey] = 0
for j in top:
mykey = str(j[0]) + '-' + str(j[1])
btmdict[mykey] = 0
topdict[mykey] = 0
for i in btm:
mykey = str(i[0]) + '-' + str(i[1])
btmdict[mykey] += 1
for j in top:
mykey = str(j[0]) + '-' + str(j[1])
topdict[mykey] += 1
ans = 1
for i in btmdict:
btmi = btmdict[i]
topi = topdict[i]
if btmi*topi >= 1:
ans = ans * (pow(2, btmi, MOD) +pow(2, topi, MOD) -1) %MOD
elif btmi == 0:
ans = ans * pow(2, topi, MOD) %MOD
else:
ans = ans * pow(2, btmi, MOD) %MOD
ans = (ans +count00 -1) %MOD
print(ans)
| 0 | null | 28,929,223,176,868 | 176 | 146 |
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15
Lunlun = []
K = int(input())
def dfs(i,before):
if i == 10:
return
Lunlun.append(before)
x = before%10
for d in (-1,0,1):
y = x + d
if y < 0 or y >= 10:
continue
dfs(i + 1,before*10 + y)
for i in range(1,10):
dfs(0,i)
Lunlun.sort()
ans = Lunlun[K - 1]
print(ans)
|
from collections import deque
K = int(input())
lunlun = deque([i for i in range(1,10)])
for i in range(K):
x = lunlun.popleft()
if x % 10 != 0:
lunlun.append(x * 10 + (x % 10) - 1)
lunlun.append(x * 10 + (x % 10))
if x % 10 != 9:
lunlun.append(x * 10 + (x % 10) + 1)
print(x)
| 1 | 39,831,654,675,460 | null | 181 | 181 |
def reverse(l):
"""
l: a list
returns a reversed list
>>> reverse([1,2,3,4,5])
[5, 4, 3, 2, 1]
>>> reverse([3,3,4,4,5,8,7,9])
[9, 7, 8, 5, 4, 4, 3, 3]
>>> reverse([])
[]
"""
length = len(l)
result = []
for i in range(length):
result.append(l[length-i-1])
return result
if __name__ == '__main__':
#import doctest
#doctest.testmod()
num = int(input())
ilist = [int(i) for i in input().split(' ')]
print(' '.join([str(i) for i in reverse(ilist)]))
|
#coding:utf-8
#1_9_A 2015.4.12
w = input().lower()
c = 0
while True:
t = input().split()
if 'END_OF_TEXT' in t:
break
for word in t:
if w == word.lower():
c += 1
print(c)
| 0 | null | 1,424,627,864,670 | 53 | 65 |
import random
import numpy as np
D=int(input())
C=np.array(list(map(int,input().split())))
S=[]
for _ in range(D):
S.append(list(map(int,input().split())))
l=np.zeros(26,int)
def decrease(d):
return np.dot(C,(d-l))
def pick(i,l):
diff=[]
ll=l.copy()
for j in range(26):
ll[j]=i
diff.append(S[i][j]-decrease(i))
return np.argmax(diff)+1
for i in range(D):
p=pick(i,l)
print(p)
l[p-1]=i+1
|
import time
import random
d = int(input())
dd = d * (d + 1) // 2
*C, = map(int, input().split())
S = [list(map(int, input().split())) for i in range(d)]
T = []
L = [-1 for j in range(26)]
for i in range(d):
max_diff = -10**7
arg_max = 0
for j in range(26):
memo = L[j]
L[j] = i
diff = S[i][j] - sum([C[k] * (i - L[k]) for k in range(26)])
if diff > max_diff:
max_diff = diff
arg_max = j
L[j] = memo
T.append(arg_max)
L[arg_max] = i
def calc_score(T):
L = [-1 for j in range(26)]
X = [0 for j in range(26)]
score = 0
for i in range(d):
score += S[i][T[i]]
X[T[i]] += (d - i) * (i - L[T[i]])
L[T[i]] = i
for j in range(26):
score -= C[j] * (dd - X[j])
return score
score = calc_score(T)
start = time.time()
cnt = 0
while True:
now = time.time()
if now - start > 1.8:
break
p = (cnt // 26) % d
q = cnt % 26
memo = T[p]
T[p] = q
new_score = calc_score(T)
temp = 20000 * (1 - (now - start) / 2)
prob = 2**((new_score - score) / temp)
if random.random() < prob:
score = new_score
else:
T[p] = memo
cnt = (cnt + 1) % (10**9 + 7)
for t in T:
print(t + 1)
| 1 | 9,634,934,169,530 | null | 113 | 113 |
N, A, B = map(int, input().split())
q, r = divmod(N, A + B)
ans = 0
ans += q * A
ans += min(r, A)
print(ans)
|
import sys
n,a,b = map(int,input().split())
if a == 0:
print(0)
sys.exit()
if b == 0:
print(n)
sys.exit()
if n%(a+b) <= a:
print(n//(a+b)*a + n%(a+b))
else:
print(n//(a+b)*a + a)
| 1 | 55,641,764,418,658 | null | 202 | 202 |
w = input()
count = 0 # ????????????????????´?????°.
while True:
line = input()
if line == "END_OF_TEXT":
break
for word in line.split():
if word.lower() == w.lower():
count += 1
print(count)
|
key = raw_input().upper()
c = ''
while True:
s = raw_input()
if s == 'END_OF_TEXT':
break
c = c + ' ' + s.upper()
cs = c.split()
total = 0
for w in cs:
if w == key:
total = total + 1
print total
| 1 | 1,805,697,529,820 | null | 65 | 65 |
import sys
input = sys.stdin.readline
def main():
N, K, S = map(int, input().split())
if S == 10**9:
ans = [S] * K + [1] * (N-K)
else:
ans = [S] * K + [10**9] * (N-K)
print(*ans)
if __name__ == "__main__":
main()
|
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")
n,k,s = map(int, input().split())
if s==10**9:
a = [10**9]*k + [1]*(n-k)
else:
a = [s]*k + [s+1]*(n-k)
print(" ".join(map(str, a)))
| 1 | 90,941,247,407,760 | null | 238 | 238 |
n=int(input())
stri=[]
time=[]
for _ in range(n):
s,t=input().split()
stri.append(s)
time.append(t)
x=input()
ind=stri.index(x)
ans=0
for i in range(ind+1,n):
ans+=int(time[i])
print(ans)
|
a = [int(i) for i in input().split()]
a[0],a[1] = a[1],a[0]
a[0],a[2] = a[2],a[0]
print(" ".join(map(str,a)))
| 0 | null | 67,347,404,618,588 | 243 | 178 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.