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
|
---|---|---|---|---|---|---|
n,k = map(int,input().split())
h = list(map(int,input().split()))
h.sort(reverse=True)
for i in range(min(n,k)):
h[i] = 0
print(sum(h))
|
r=input();p=3.141592653589;print "%.9f"%(p*r*r),r*2*p
| 0 | null | 39,676,477,753,340 | 227 | 46 |
x = input()
count = int(x.split()[0])
num = int(x.split()[1])
if count < 10:
num = num + (100 * (10 - count))
else:
num
print(num)
|
n,m=input().split()
n=int(n)
m=int(m)
f=[0]*n
wa=[0]*n
ac=0
for i in range(m):
p,s=input().split()
p=int(p)-1
if f[p]==1:
continue
if s=="WA":
wa[p]+=1
else:
ac+=1
f[p]=1
x=0
for i in range(n):
if f[i]==1:
x+=wa[i]
print(ac,x)
| 0 | null | 78,406,530,442,338 | 211 | 240 |
import math , sys
N = int( input() )
X = []
for i in range(N):
A , B = list(map(int, input().split()))
X.append([i , A-B , A , A+B])
X.sort(key=lambda x: x[3])
Y = X[0]
Ri = Y[-1]
ans = 1
j=0
for i in range(1,N):
Y = X[i]
if Y[1] >= Ri:
ans+=1
Ri = Y[-1]
print(ans)
|
n,m=map(int,input().split())
s=input()
ans=[]
now=n
while now>0:
for i in range(m,0,-1):
if now-i<0:
continue
if s[now-i]=='0':
ans.append(str(i))
now-=i
break
else:
print(-1)
exit()
print(' '.join(ans[::-1]))
| 0 | null | 114,359,759,714,200 | 237 | 274 |
N = int(input())
s = 0
i = 1
while i <= N:
if i % 3 != 0 and i % 5 != 0:
s += i
i += 1
print(s)
|
def main():
N = int(input())
total = 0
for i in range(N):
i = i + 1
if i % 3 == 0 and i % 5 == 0:
continue
elif i % 3 == 0:
continue
elif i % 5 == 0:
continue
total = total + i
print(total)
main()
| 1 | 34,925,543,605,056 | null | 173 | 173 |
s=[]
p=s.pop
for e in input().split():
s+=[e if e.isdigit() else str(eval(p(-2)+e+p()))]
print(*s)
|
import sys,queue,math,copy,itertools,bisect,collections,heapq
def main():
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
N,K = LI()
A = LI()
c = 0
d = collections.Counter()
d[0] = 1
ans = 0
r = [0] * (N+1)
for i,x in enumerate(A):
if i >= K-1:
d[r[i-(K-1)]] -= 1
c = (c + x - 1) % K
ans += d[c]
d[c] += 1
r[i+1] = c
print(ans)
if __name__ == '__main__':
main()
| 0 | null | 68,555,635,541,470 | 18 | 273 |
import math,sys
for e in sys.stdin:
a,b=list(map(int,e.split()));g=math.gcd(a,b)
print(g,a*b//g)
|
def gcd(a,b):
while True:
if a > b:
a %= b
else:
b %= a
if a == 0 or b == 0:
return max(a,b)
def lcm(a,b,g):
return int(a*b/g)
from sys import stdin
for l in stdin:
a,b = list(map(int,l.split()))
g = gcd(a,b)
print ("%s %s"%(g,lcm(a,b,g)))
| 1 | 658,861,510 | null | 5 | 5 |
S=int(input())
T=list(input())
t=S//2
p=0
for i in range(t):
if S%2==1:
p=-1
break
elif T[i]!=T[i+t]:
p=-1
break
if p==-1 or t==0:
print("No")
elif p==0:
print("Yes")
|
def main():
n = int(input())
s = input()
if n % 2 == 0:
sl = len(s) // 2
s1 = s[:sl]
s2 = s[sl:]
if s1 == s2:
print('Yes')
else:
print('No')
else:
print('No')
if __name__ == '__main__':
main()
| 1 | 147,378,348,779,938 | null | 279 | 279 |
a = input().split()
v = 2
while int(v) >= 1:
if int(a[int(v)-1]) < int(a[int(v)]):
pass
else:
b = int(a[int(v)-1])
a[int(v)-1] = int(a[int(v)])
a[int(v)] = int(b)
v = int(v) - 1
if int(a[1]) < int(a[2]):
pass
else:
b = int(a[1])
a[1] = int(a[2])
a[2] = int(b)
print('%d %d %d' % (int(a[0]),int(a[1]),int(a[2])))
|
N = list(map(int, input().split(' ')))
N.sort()
print('%d %d %d' % (N[0], N[1], N[2]))
| 1 | 425,348,898,602 | null | 40 | 40 |
H,W,K=map(int,input().split())
s=[]
for i in range(H):
S=input()
s.append(S)
a=[[0 for i in range(W)] for j in range(H)]
tmp=-1
flag=False
chk=[0 for _ in range(W)]
for w in range(W):
count=0
for h in range(H):
if not flag:
if s[h][w]=="#":
tmp=w
flag=True
if s[h][w]==".":
count+=1
if count==H:
chk[w]=1
num=0
flag=True
f=True
for w in range(W):
if w<tmp:
continue
if chk[w]==0:
num+=1
f=True
for h in range(H):
if chk[w]==0:
if s[h][w]=="#":
if flag:
num=1
flag=False
if f:
pass
f=False
else:
num+=1
a[h][w]=num
else:
a[h][w]=num
else:
a[h][w]=a[h][w-1]
if tmp!=0:
for w in range(tmp):
for h in range(H):
if (tmp-1-w)<0:
break
a[h][tmp-1-w]=a[h][tmp-w]
for i in range(H):
print(*a[i])
|
# ある行について、
# 最初のイチゴが出てから、次のイチゴの手前までを一つの番号とする
# それ以降は、次のイチゴが出る手前までを次の番号とする
# イチゴがない行があったら、となりの行の結果をコピーする
import sys
readline = sys.stdin.readline
H,W,K = map(int,readline().split())
A = [readline().rstrip() for i in range(H)]
ans = [[0] * W for i in range(H)]
exist_straw = [True] * H
num = 0 # 次の番号
for i in range(H):
# イチゴがない行はいったん後回し
if "#" not in A[i]:
exist_straw[i] = False
continue
num += 1
# イチゴが出たらnumを1増やしてから更新 ただし最初のイチゴは無視
first = True
for j in range(W):
if A[i][j] == "#":
if first:
first = False
else:
num += 1
ans[i][j] = num
# 行を上から順に見て、ある行にはイチゴが存在して、次の行には存在しない場合、行をコピーする
# それを逆順も実行すると全ての行が埋まる
for i in range(H - 1):
if exist_straw[i] and not exist_straw[i + 1]:
ans[i + 1] = ans[i]
exist_straw[i + 1] = True
for i in range(H - 1,0,-1):
if exist_straw[i] and not exist_straw[i - 1]:
ans[i - 1] = ans[i]
exist_straw[i - 1] = True
for a in ans:
print(*a)
| 1 | 144,112,611,903,850 | null | 277 | 277 |
N = int(input())
from collections import Counter
c = Counter()
for x in range(1,101):
for y in range(1,101):
for z in range(1,101):
n = x*x + y*y + z*z + x*y + y*z + z*x
c[n] += 1
for i in range(1,N+1):
print(c[i])
|
list=[0 for i in range(10**4+1)]
for x in range(1,100):
for y in range(1,100):
for z in range(1,100):
var=x**2 +y**2 +z**2 +x*y +y*z +z*x
if var<=10**4:
list[var]+=1
n=int(input())
for i in range(1,n+1):
print(list[i])
| 1 | 7,898,364,021,172 | null | 106 | 106 |
def main():
import collections
n, m = map(int, input().split())
hs = list(map(int, input().split()))
nmap = [0] * n
cnt = 0
for j in range(m):
v = input().split()
v0 = int(v[0]) - 1
v1 = int(v[1]) - 1
if hs[v0] < hs[v1]:
nmap[v0] += 1
elif hs[v0] == hs[v1]:
nmap[v0] += 1
nmap[v1] += 1
else:
nmap[v1] += 1
for i in nmap:
if i == 0:
cnt += 1
return cnt
if __name__ == '__main__':
print(main())
|
n,m=map(int,input().split())
h=list(map(int,input().split()))
l=[1]*n
for i in range(m):
a,b=map(int,input().split())
if h[a-1]==h[b-1]:
l[a-1]=0
l[b-1]=0
if h[a-1]>h[b-1]:
l[b-1]=0
if h[a-1]<h[b-1]:
l[a-1]=0
print(l.count(1))
| 1 | 25,243,348,599,612 | null | 155 | 155 |
def main():
n, k = map(int, input().split())
h_lst = list(map(int, input().split()))
ans = 0
for h in h_lst:
if h >= k:
ans += 1
print(ans)
if __name__ == "__main__":
main()
|
N,K = map(int,input().split())
h = list(map(int,input().split()))
cnt = 0
hei = 0
for i in range(N):
hei = h[i]
if hei >= K:
cnt += 1
print(cnt)
| 1 | 178,887,389,944,220 | null | 298 | 298 |
n = int(input())
a = [int(i) for i in input().split()]
print(min(a), max(a), sum(a))
|
b = input()
a = input().split()
for i in range(len(a)):
a[i] = int(a[i])
max_a = max(a)
min_a = min(a)
Sum = sum(a)
print(min_a,max_a,Sum)
| 1 | 729,137,216,444 | null | 48 | 48 |
import math
while True:
count = int(input())
if count == 0:
break
data = [int(i) for i in input().split()]
m = sum(data) / len(data)
a = sum([(x - m) ** 2 for x in data]) / count
print('{0:.5f}'.format(math.sqrt(a)))
|
from math import sin, cos, pi
def koch(px, py, qx, qy, n):
if n == 0:
return True
sx = (2 * px + qx) / 3
sy = (2 * py + qy) / 3
tx = (px + 2 * qx) / 3
ty = (py + 2 * qy) / 3
ux = sx + (tx - sx) * cos(pi / 3) - (ty - sy) * sin(pi / 3)
uy = sy + (tx - sx) * sin(pi / 3) + (ty - sy) * cos(pi / 3)
koch(px, py, sx, sy, n - 1)
print("{:.8f} {:.8f}".format(sx, sy))
koch(sx, sy, ux, uy, n - 1)
print("{:.8f} {:.8f}".format(ux, uy))
koch(ux, uy, tx, ty, n - 1)
print("{:.8f} {:.8f}".format(tx, ty))
koch(tx, ty, qx, qy, n - 1)
n = int(input())
print("{:.8f} {:.8f}".format(0, 0))
koch(0, 0, 100, 0, n)
print("{:.8f} {:.8f}".format(100, 0))
| 0 | null | 163,745,303,682 | 31 | 27 |
# -*- coding: utf-8 -*-
X = int(input())
# A**5 - (A-1)**5 = X = 10**9(Xの最大) となるAが
# Aの最大値となる(それ以上Aが大きくなると、Xは上限値を超える)
# → 最大となるAは120
for A in range(-120, 120 + 1):
for B in range(-120, 120):
if A**5 - B**5 == X:
print("{} {}".format(A, B))
exit()
|
X = int(input())
# Xの素因数分解をうまくつかう
# A^5 - B^5 =(A-B)(A^4+A^3B+A^2B^2+AB^3+B^4)
# どっちが正負かは決まらない
# AB(A^2+B^2)+(A^2+B^2)^2-A^2B^2
# AB((A-B)^2+2AB)+((A-B)^2+2AB)^2 - A^2B^2
# A-B = P
# AB = Q
# A^5-B^5 = P(Q(P^2+2Q)+(P^2+2Q)^2-Q^2) = P(P^4+5P^2Q+5Q^2) = P(5Q^2+5P^2Q+P^4)
# Qは整数解なので
# 5Q^2+5P^2Q+P^4が整数解を持つ楽な方法は(Q+P)^2の形の時、つまり2P = P^2+2,
# X = PRとする
# R-P^4は5の倍数になる
#Aの上限を求める
# X<10^9
# A-B = 1がAを最大にできる
maxA = 126
maxX = pow(10, 9)
flag = 0
for A in range(-126, 126):
for B in range(-127, A):
result = pow(A, 5) - pow(B, 5)
if result > maxX:
next
if result == X:
print(A, B)
flag = 1
break
if flag == 1:
break
| 1 | 25,596,377,489,124 | null | 156 | 156 |
N = int(input())
flag = 0
ans = "No"
for i in range(N):
A,B = input().split()
if A == B:
flag += 1
else:
flag = 0
if flag == 3:
ans = "Yes"
break
print(ans)
|
def main():
S = input()
if "RRR" in S:
print(3)
elif "RR" in S:
print(2)
elif "R" in S:
print(1)
else:
print(0)
if __name__ == '__main__':
main()
| 0 | null | 3,643,164,109,992 | 72 | 90 |
S = raw_input()
n = int(raw_input())
for i in range(n):
command = (raw_input()).split(" ")
if command[0] == "print":
a = int(command[1])
b = int(command[2])
print S[a:b+1]
elif command[0] == "reverse":
a = int(command[1])
b = int(command[2])
T = S[a:b+1]
S = S[0:a] + T[::-1] + S[b+1:]
else:
a = int(command[1])
b = int(command[2])
S = S[0:a] + command[3] + S[b+1:]
|
N = int(input())
S = list(input())
l = []
for i in S:
if ord(i)+N > 90:
l.append(chr(64+(ord(i)+N)%90))
else:
l.append(chr(ord(i)+N))
print("".join(l))
| 0 | null | 68,449,278,415,340 | 68 | 271 |
import math
A = []
N = int(input())
maxdiff = 0
for n in range(N):
A.append(int(input()))
R = tuple(A)
Dmax = R[1] -R[0]
Rmin = R[0]
for i in range(1, N):
if R[i] >= R[i-1]:
if R[i] - Rmin > Dmax:
Dmax = R[i] -Rmin
if R[i] < Rmin:
Rmin = R[i]
# print(R[i], Dmax, Rmin)
print(Dmax)
|
X = int(input())
print('Yes') if X >= 30 else print('No')
| 0 | null | 2,903,503,418,662 | 13 | 95 |
from time import time
from random import random
limit_secs = 2
start_time = time()
D = int(input())
c = list(map(int, input().split()))
s = [list(map(int, input().split())) for _ in range(D)]
def calc_score():
score = 0
S = 0
last = [-1] * 26
for d in range(D):
S += s[d][t[d]]
last[t[d]] = d
for i in range(26):
S -= c[i] * (d - last[i])
score += max(10 ** 6 + S, 0)
return score
def solution():
return [i % 26 for i in range(D)]
t = solution()
score = calc_score()
while time() - start_time + 0.14 < limit_secs:
d = int(random() * D)
q = int(random() * 26)
old = t[d]
t[d] = q
new_score = calc_score()
if new_score < score:
t[d] = old
else:
score = new_score
print(*[e + 1 for e in t])
|
import random
D=int(input())
c=list(map(int,input().split()))
s=[list(map(int,input().split())) for i in [0]*D]
for i in range(D):
print(random.randint(1,26))
| 1 | 9,720,784,404,292 | null | 113 | 113 |
s,t = input().split()
print("".join(map(str,[t,s])))
|
N=int(input())
S,T=input().split()
for i in range(N):
print(S[i],T[i],sep='',end='')
print()
| 0 | null | 107,627,781,219,264 | 248 | 255 |
X = int(input())
if X <= 2:
print(2)
exit()
S = [2]
for i in range(3, 10**6, 2):
f = True
for s in S:
if s > i**(0.5): break
if i % s == 0:
f = False
break
if f:
S.append(i)
if i >= X:
print(i)
break
|
M1, D1 = input().split()
M2, D2 = input().split()
M1 = int(M1)
M2 = int(M2)
if M1!=M2:
print('1')
else:
print('0')
| 0 | null | 115,209,327,334,218 | 250 | 264 |
import itertools,sys
def S(): return sys.stdin.readline().rstrip()
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
H,W = LI()
S = [S() for _ in range(H)]
dp = [[float('INF')]*W for _ in range(H)]
for i,j in itertools.product(range(H),range(W)):
if i==0:
if j==0:
dp[i][j] = 1 if S[i][j]=='#' else 0
else:
dp[i][j] = dp[i][j-1]+(1 if S[i][j]=='#' and S[i][j-1]=='.' else 0)
else:
if i==0:
dp[i][j] = dp[i-1][j]+(1 if S[i][j]=='#' and S[i-1][j]=='.' else 0)
else:
dp[i][j] = min(dp[i][j],dp[i][j-1]+(1 if S[i][j]=='#' and S[i][j-1]=='.' else 0))
dp[i][j] = min(dp[i][j],dp[i-1][j]+(1 if S[i][j]=='#' and S[i-1][j]=='.' else 0))
print(dp[-1][-1])
|
def main():
n = int(input())
points = [0, 0]
for i in range(n):
t, h = input().split(' ')
points = map(lambda x, y: x+y, points, judge(t, h))
print("{0[0]} {0[1]}".format(list(points)))
def judge(t, h):
if t < h:
return (0, 3)
elif t > h:
return (3, 0)
return (1, 1)
if __name__ == '__main__': main()
| 0 | null | 25,663,902,398,760 | 194 | 67 |
x = int(input())
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
def lcm(a, b):
return a*(b//gcd(a, b))
print(lcm(360, x)//x)
|
import sys
import fractions
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
H, W = lr()
dp = [[0 for _ in range(W)] for _ in range(H)]
masu = [sr() for _ in range(H)]
dp[0][0] = 1 if masu[0][0] == "#" else 0
for h, m in enumerate(masu):
for w, s in enumerate(m):
if w == 0 and h == 0:
continue
if w == 0:
if masu[h-1][w] != s and s == "#":
dp[h][w] += dp[h-1][w] + 1
else:
dp[h][w] += dp[h-1][w]
continue
if h == 0:
if masu[h][w-1] != s and s == "#":
dp[h][w] += dp[h][w-1] + 1
else:
dp[h][w] += dp[h][w-1]
continue
if masu[h-1][w] != s and s == "#":
cand1 = dp[h][w] + dp[h-1][w] + 1
else:
cand1 = dp[h][w] + dp[h-1][w]
if masu[h][w-1] != s and s == "#":
cand2 = dp[h][w] + dp[h][w-1] + 1
else:
cand2 = dp[h][w] + dp[h][w-1]
dp[h][w] = min(cand1, cand2)
print(dp[H-1][W-1])
| 0 | null | 31,082,082,718,262 | 125 | 194 |
S = input()
N = len(S)
ans = [0] * (N+1)
for i in range(N):
if S[i] == '<':
ans[i+1] = ans[i] + 1
for i in range(N-1, -1, -1):
if S[i] == '>' and ans[i] <= ans[i+1]:
ans[i] = ans[i+1]+1
print(sum(ans))
|
K=int(input())
a=7
for i in range(1,K+1):
if a%K==0:
print(i)
exit()
a=(a*10+7)%K
print(-1)
| 0 | null | 81,006,063,275,204 | 285 | 97 |
i = 1
while True :
x = input()
if (x == 0) :
exit()
print "Case %d: %d" % (i, x)
i += 1
|
def abc169c_multiplication3():
a, b = input().split()
a = int(a)
b = int(b[0] + b[2] + b[3])
print(a * b // 100)
abc169c_multiplication3()
| 0 | null | 8,550,787,698,960 | 42 | 135 |
X = int(input())
ans = 1
while (X*ans)%360 != 0:
ans += 1
print(ans)
|
n=int(input())
from math import gcd
m=gcd(360,n)
print(360//m)
| 1 | 13,105,722,830,108 | null | 125 | 125 |
Alp = input()
if Alp.isupper() == True :
print('A')
else:
print('a')
|
if input().isupper():
print('A')
else:
print('a')
| 1 | 11,364,462,545,600 | null | 119 | 119 |
# abc161_c.py
# https://atcoder.jp/contests/abc161/tasks/abc161_c
# C - Replacing Integer /
# 実行時間制限: 2 sec / メモリ制限: 1024 MB
# 配点 : 300点
# 問題文
# 青木君は任意の整数 xに対し、以下の操作を行うことができます。
# 操作: xを x と Kの差の絶対値で置き換える。
# 整数 Nの初期値が与えられます。この整数に上記の操作を 0 回以上好きな回数行った時にとりうる Nの最小値を求めてください。
# 制約
# 0≤N≤1018
# 1≤K≤1018
# 入力は全て整数
# 入力
# 入力は以下の形式で標準入力から与えられる。
# N K
# 出力
# 操作を 0回以上好きな回数行った時にとりうる Nの最小値を出力せよ。
# 入力例 1
# 7 4
# 出力例 1
# 1
# 最初、 N=7です。
# 1回操作を行うと、N は |7−4|=3となります。
# 2回操作を行うと、N は |3−4|=1となり、これが最小です。
# 入力例 2
# 2 6
# 出力例 2
# 2
# 1回も操作を行わなかった場合の N=2が最小です。
# 入力例 3
# 1000000000000000000 1
# 出力例 3
# 0
global FLAG_LOG
FLAG_LOG = False
def log(value):
# FLAG_LOG = True
# FLAG_LOG = False
if FLAG_LOG:
print(str(value))
def calculation(lines):
# S = lines[0]
# N = int(lines[0])
N, K = list(map(int, lines[0].split()))
# values = list(map(int, lines[1].split()))
# values = list(map(int, lines[2].split()))
# values = list()
# for i in range(N):
# values.append(int(lines[i]))
# valueses = list()
# for i in range(N):
# valueses.append(list(map(int, lines[i+1].split())))
tmp = N % K
result = min(tmp, K-tmp)
return [result]
# 引数を取得
def get_input_lines(lines_count):
lines = list()
for _ in range(lines_count):
lines.append(input())
return lines
# テストデータ
def get_testdata(pattern):
if pattern == 1:
lines_input = ['7 4']
lines_export = [1]
if pattern == 2:
lines_input = ['2 6']
lines_export = [2]
if pattern == 3:
lines_input = ['1000000000000000000 1']
lines_export = [0]
return lines_input, lines_export
# 動作モード判別
def get_mode():
import sys
args = sys.argv
global FLAG_LOG
if len(args) == 1:
mode = 0
FLAG_LOG = False
else:
mode = int(args[1])
FLAG_LOG = True
return mode
# 主処理
def main():
import time
started = time.time()
mode = get_mode()
if mode == 0:
lines_input = get_input_lines(1)
else:
lines_input, lines_export = get_testdata(mode)
lines_result = calculation(lines_input)
for line_result in lines_result:
print(line_result)
# if mode > 0:
# print(f'lines_input=[{lines_input}]')
# print(f'lines_export=[{lines_export}]')
# print(f'lines_result=[{lines_result}]')
# if lines_result == lines_export:
# print('OK')
# else:
# print('NG')
# finished = time.time()
# duration = finished - started
# print(f'duration=[{duration}]')
# 起動処理
if __name__ == '__main__':
main()
|
N, K = map(int, input().split(' '))
print(min(N % K, abs((N % K) - K)))
| 1 | 39,305,109,269,860 | null | 180 | 180 |
s=list(input())
t=list(input())
t.pop(-1)
if s==t:
print('Yes')
else:
print('No')
|
from sys import exit
X = int(input())
x = 100
i = 0
while True:
i += 1
x *= 101
x = x // 100
if X <= x:
print(i)
exit()
| 0 | null | 24,081,744,899,212 | 147 | 159 |
import random
class Dice:
def __init__(self, list = map(str, range(1, 7))):
self.top = list[0]
self.front = list[1]
self.right = list[2]
self.left = list[3]
self.back = list[4]
self.bottom = list[5]
def print_all(self):
print "top = " + self.top
print "front = " + self.front
print "right = " + self.right
print "left = " + self.left
print "back = " + self.back
print "bottom = " + self.bottom
def roll_N(self):
temp = self.top
self.top = self.front
self.front = self.bottom
self.bottom = self.back
self.back = temp
def roll_S(self):
temp = self.top
self.top = self.back
self.back = self.bottom
self.bottom = self.front
self.front = temp
def roll_W(self):
temp = self.top
self.top = self.right
self.right = self.bottom
self.bottom = self.left
self.left = temp
def roll_E(self):
temp = self.top
self.top = self.left
self.left = self.bottom
self.bottom = self.right
self.right = temp
def random_roll(self):
ram = random.randint(1, 4)
if ram == 1:
self.roll_E()
elif ram == 2:
self.roll_N()
elif ram == 3:
self.roll_S()
else:
self.roll_W()
my_dice = Dice(raw_input().split(" "))
q = int(raw_input())
for i in xrange(q):
input_dice = raw_input().split(" ")
while True:
if my_dice.top == input_dice[0] and my_dice.front == input_dice[1]:
break
else:
my_dice.random_roll()
print my_dice.right
|
#入力:N,M(int:整数)
def input2():
return map(int,input().split())
#入力:[n1,n2,...nk](int:整数配列)
def input_array():
return list(map(int,input().split()))
n,m,x=input2()
CA=[]
ans=10**9
for _ in range(n):
CA.append(input_array())
# ビット全探索
for i in range(2**n):
bag=[]
for j in range(n): #ビット全探索
if (i>>j) & 1:
bag.append(CA[j])
enoughs=[0]*m #スキルがx以上身に付く数
skills=[0]*m #身に付くスキルの合計
money=0 #探索中の金額
for b in bag:
money+=b[0]
for k in range(1,m+1):
skills[k-1]+=b[k]
if skills[k-1]>=x:
enoughs[k-1]+=1
if 0 not in enoughs: #スキルが充足
if money<ans:
ans=money #価格の更新
if ans==10**9:
print(-1)
else:
print(ans)
| 0 | null | 11,213,826,191,230 | 34 | 149 |
n = int(input())
s = input()
ok = [0,0,0,0,0,0,0,0,0,0]
ans = 0
for i in range(n):
if sum(ok) == 10:
break
if ok[int(s[i])] == 1:
continue
ok[int(s[i])] = 1
nd = [0,0,0,0,0,0,0,0,0,0]
for j in range(i+1,n):
if sum(nd) == 10:
break
if nd[int(s[j])] == 1:
continue
nd[int(s[j])] = 1
rd = [0,0,0,0,0,0,0,0,0,0]
for k in range(j+1, n):
if sum(rd) == 10:
break
if rd[int(s[k])] == 1:
continue
rd[int(s[k])] = 1
ans += 1
print(ans)
|
n = int(input())
s = input()
ans = 0
for pin in range(10**3):
str_pin = str(pin)
str_pin = str_pin.zfill(3)
nex = 0
for i in range(n):
searching_for = str_pin[nex]
now_s = s[i]
if searching_for == now_s:
nex += 1
if nex == 3:
ans += 1
break
print(ans)
| 1 | 128,596,417,572,480 | null | 267 | 267 |
N = int(input())
A = list(map(int, input().split()))
hight = A[0]
k = 0
for i in range(1, N):
if hight > A[i]:
k += (hight - A[i])
else:
hight = A[i]
print(k)
|
n = int(input())
a = list(map(int, input().split()))
ans = 0
tallest = 0
for i in range(n):
tallest = max(tallest, a[i])
ans += tallest - a[i]
print(int(ans))
| 1 | 4,585,877,069,646 | null | 88 | 88 |
import math
n = int(input())
class Coordinate:
def __init__(self,x,y):
self.x = x
self.y = y
def Koch(n,p1,p2):
if n == 0:
return
else:
s = Coordinate(2/3 * p1.x + 1/3 * p2.x, 2/3 * p1.y + 1/3 * p2.y)
u = Coordinate(1/3 * p1.x + 2/3 * p2.x, 1/3 * p1.y + 2/3 * p2.y)
t = Coordinate(1/2*(u.x-s.x) - math.sqrt(3)/2*(u.y-s.y) + s.x, math.sqrt(3)/2*(u.x-s.x) + 1/2*(u.y-s.y) + s.y)
Koch(n-1,p1,s)
print(str(s.x) +" "+str(s.y))
Koch(n-1,s,t)
print(str(t.x) +" "+str(t.y))
Koch(n-1,t,u)
print(str(u.x) +" "+str(u.y))
Koch(n-1,u,p2)
p1 = Coordinate(0,0)
p2 = Coordinate(100,0)
print(str(p1.x) +" "+str(p1.y))
Koch(n,p1,p2)
print(str(p2.x) +" "+str(p2.y))
|
def koch(n, p1, p2):
if n == 0:
return
s_x = p1[0] * 2/3 + p2[0] * 1/3
s_y = p1[1] * 2/3 + p2[1] * 1/3
t_x = p1[0] * 1/3 + p2[0] * 2/3
t_y = p1[1] * 1/3 + p2[1] * 2/3
u_x = (t_x - s_x) * 1/2 - (t_y - s_y) * 3 ** 0.5 / 2 + s_x
u_y = (t_x - s_x) * 3 ** 0.5 / 2 + (t_y - s_y) / 2 + s_y
s, t, u = (s_x, s_y), (t_x, t_y), (u_x, u_y)
koch(n - 1, p1, s)
print(' '.join(str(i) for i in s))
koch(n - 1, s, u)
print(' '.join(str(i) for i in u))
koch(n - 1, u, t)
print(' '.join(str(i) for i in t))
koch(n - 1, t, p2)
S, G = (0, 0), (100, 0)
print(' '.join(str(i) for i in S))
koch(int(input()), S, G)
print(' '.join(str(i) for i in G))
| 1 | 129,283,273,508 | null | 27 | 27 |
import math
A,B,H,M = map(int,input().split())
t1 = (30*H+M/2)*math.pi/180
x1 = A*math.cos(t1)
y1 = A*math.sin(t1)
t2 = M*math.pi/30
x2 = B*math.cos(t2)
y2 = B*math.sin(t2)
d = ((x1-x2)**2+(y1-y2)**2)**0.5
print(d)
|
while True:
m,f,r=map(int,raw_input().split())
if m==f==r==-1: break
if m+f<30 or m==-1 or f==-1: print 'F'
elif m+f>=80: print 'A'
elif 65<=m+f: print 'B'
elif 50<=m+f: print 'C'
elif r>=50: print 'C'
else: print 'D'
| 0 | null | 10,614,380,513,958 | 144 | 57 |
import math
def rot60(s, t):
v = t - s
a = 1/2 + complex(0,(math.sqrt(3)/2))
return v * a + s
def pr(p):
x = p.real
y = p.imag
print('%.10f %.10f'%(x, y))
def dfs(p1, p2, n):
if n == 0:
return
s = (p2 - p1) * (1/3) + p1
t = (p2 - p1) * (2/3) + p1
u = rot60(s, t)
dfs(p1, s, n-1)
pr(s)
dfs(s, u, n-1)
pr(u)
dfs(u, t, n-1)
pr(t)
dfs(t, p2, n-1)
n = int(input())
s = (0 + 0j)
t = (100 + 0j)
pr(s)
dfs(s, t, n)
pr(t)
|
import math
INFTY=100000000
n=int(input())
def kock(p1,p2,n):
s=[(p1[0]*2+p2[0])/3, (p1[1]*2+p2[1])/3]
t=[(p1[0]+p2[0]*2)/3, (p1[1]+p2[1]*2)/3]
u=[(t[0]-s[0])/2-(t[1]-s[1])/2*math.sqrt(3)+s[0], (t[0]-s[0])/2*math.sqrt(3)+(t[1]-s[1])/2+s[1]]
if n==0:
print("%.8f %.8f"%(p1[0],p1[1]))
else:
kock(p1,s,n-1)
kock(s,u,n-1)
kock(u,t,n-1)
kock(t,p2,n-1)
start=[0.0,0.0]
end=[100.0, 0.0]
if n==0:
print("%.8f %.8f"%(start[0],start[1]))
print("%.8f %.8f"%(end[0],end[1]))
else:
kock(start,end,n)
print("%.8f %.8f"%(end[0],end[1]))
| 1 | 126,108,237,550 | null | 27 | 27 |
n, p = map(int,input().split())
D = input()
out = 0
if 10 % p == 0:
for i in range(n):
if int(D[i]) % p == 0:
out += i + 1
else:
mod = 0
count = [0] * p
ten = 1
count[mod] += 1
for i in range(n):
mod = (mod + int(D[n-i-1]) * ten) % p
ten = ten * 10 % p
count[mod] += 1
for c in count:
out += (c * (c - 1)) // 2
print(out)
|
def main():
N, P = map(int, input().split())
*s, = map(int, input())
if P in {2, 5}:
ans = 0
for i, x in enumerate(reversed(s), start=0):
if x % P == 0:
ans += N - i
print(ans)
return
ans = 0
total = 0
coef = 1
ctr = [0] * P
ctr[0] = 1
for x in reversed(s):
total = (total + x * coef) % P
coef = coef * 10 % P
ans += ctr[total]
ctr[total] += 1
print(ans)
if __name__ == '__main__':
main()
| 1 | 58,126,569,619,728 | null | 205 | 205 |
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, *A = map(int, read().split())
M = 61
C = [0] * M
for a in A:
a = str(format(a, 'b'))
for i, d in enumerate(reversed(a)):
if d == '1':
C[i] += 1
ans = 0
p = 1
for c in C:
ans = (ans + c * (N - c) * p) % MOD
p = p * 2 % MOD
print(ans)
return
if __name__ == '__main__':
main()
|
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return S().split()
def S(): return sys.stdin.readline().strip()
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)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007
class BIT:
def __init__(self, size):
self.bit = [0] * size
self.size = size
self.total = 0
def add(self, i, w):
x = i + 1
self.total += w
while x <= self.size:
self.bit[x - 1] += w
x += x & -x
return
def sum(self, i):
res = 0
x = i + 1
while x:
res += self.bit[x - 1]
x -= x & -x
return res
def interval_sum(self, i, j): # i <= x < j の区間
return self.sum(j - 1) - self.sum(i - 1) if i else self.sum(j - 1)
n = I()
s = list(S())
q = I()
D = defaultdict(lambda:BIT(n))
for j in range(n):
D[s[j]].add(j, 1)
for _ in range(q):
qi, i, c = LS()
if qi == "1":
i = int(i) - 1
D[s[i]].add(i, -1)
D[c].add(i, 1)
s[i] = c
else:
l, r = int(i) - 1, int(c)
ret = 0
for k in range(97, 123):
if D[chr(k)].interval_sum(l, r):
ret += 1
print(ret)
| 0 | null | 92,279,130,625,880 | 263 | 210 |
n = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
A2 = [A[0]]
for a in A[1:]:
A2.extend([a, a])
ans = 0
for a in A2[:n-1]:
ans += a
print(ans)
|
import math
n = int(input())
a = list(map(int,input().split()))
s = sorted(a,reverse=True)
max = s[0]
for i in range(3,n+1):
max += s[math.ceil(i/2)-1]
print(max)
| 1 | 9,205,752,208,090 | null | 111 | 111 |
a,b,c,k=map(int,input().split())
if k < a:
print(k)
elif k <= a+b:
print(a)
else:
print(2*a+b-k)
|
L = int(input())
l3 = L/3
print(l3*l3*l3)
| 0 | null | 34,342,095,746,440 | 148 | 191 |
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
#import heapq
#from fractions import gcd
#input=sys.stdin.readline
import bisect
a,b,c=map(int,input().split())
if 4*a*b<(c-a-b)**2 and (c-a-b)>0:
print("Yes")
else:
print("No")
|
import sys
from collections import deque
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def main():
N, p, q, *AB = map(int, read().split())
p -= 1
q -= 1
G = [[] for _ in range(N)]
for a, b in zip(*[iter(AB)] * 2):
G[a - 1].append(b - 1)
G[b - 1].append(a - 1)
if len(G[p]) == 1 and G[p][0] == q:
print(0)
return
dist1 = [-1] * N
dist1[p] = 0
queue = deque([p])
while queue:
v = queue.popleft()
for nv in G[v]:
if dist1[nv] == -1:
dist1[nv] = dist1[v] + 1
queue.append(nv)
dist2 = [-1] * N
dist2[q] = 0
queue = deque([q])
while queue:
v = queue.popleft()
for nv in G[v]:
if dist2[nv] == -1:
dist2[nv] = dist2[v] + 1
queue.append(nv)
max_d = 0
for d1, d2 in zip(dist1, dist2):
if d1 < d2 and max_d < d2:
max_d = d2
print(max_d - 1)
return
if __name__ == '__main__':
main()
| 0 | null | 84,290,057,140,472 | 197 | 259 |
import sys
import math
import collections
import heapq
import queue
import itertools
import functools
import operator
import time
readline = sys.stdin.readline
IPS = lambda: readline().rstrip()
IP = lambda: int(readline().rstrip())
MP = lambda: map(int, readline().split())
LS = lambda: list(map(int, readline().split()))
def solve():
for _ in range(1):
n = IP()
print(n + n**2 + n**3)
if __name__ == "__main__":
solve()
|
N=int(input())
L=list(map(int,input().split()))
count=0
for a in range(N):
for b in range(N):
for c in range(N):
if L[a]<(L[b]+L[c]) and L[b]<(L[a]+L[c]) and L[c]<(L[b]+L[a]) and L[a]!=L[b] and L[a]!=L[c] and L[c]!=L[b] and a<b<c:
count=count+1
else:
continue
print(count)
| 0 | null | 7,677,600,073,638 | 115 | 91 |
N,M,K=map(int,input().split())
*A,=map(int,input().split())
*B,=map(int,input().split())
def binary_search(func, array, left=0, right=-1):
if right==-1:right=len(array)-1
y_left, y_right = func(array[left]), func(array[right])
while True:
middle = (left+right)//2
y_middle = func(array[middle])
if y_left==y_middle: left=middle
else: right=middle
if right-left==1:break
return left
cumA = [0]
for i in range(N):
cumA.append(cumA[-1]+A[i])
cumB = [0]
for i in range(M):
cumB.append(cumB[-1]+B[i])
cumB.append(10**20)
if cumA[-1]+cumB[-1]<=K:
print(N+M)
exit()
if A[0] > K and B[0] > K:
print(0)
exit()
ans = 0
for i in range(N+1):
if K-cumA[i]<0:break
idx = binary_search(lambda x:x<=K-cumA[i],cumB)
res = max(0,i+idx)
ans = max(ans, res)
print(ans)
|
s = raw_input()
p = raw_input()
slen = len(s)
plen = len(p)
flag = False
for i in xrange(len(s)):
j = 0
while(j < plen and s[(i+j)%slen] == p[j]):
j += 1
if (j == plen):
flag = True
if(flag):
print "Yes"
else:
print "No"
| 0 | null | 6,232,967,832,542 | 117 | 64 |
import numpy as np
from numba import jit
R, C, K = map(int, input().split())
G = np.zeros((R+1, C+1), np.int64)
for i in range(K):
[r, c, v] = list(map(int, input().split()))
G[r-1][c-1] = v
@jit
def main(G, R, C):
dp = np.full((R+1, C+1, 4), -1)
dp[0][0][0] = 0
if G[0][0] != None: dp[0][0][1] = G[0][0]
for r in range(R):
for c in range(C):
for k in range(4):
if dp[r][c][k] == -1: continue
if k < 3: # can go right and pick up
if dp[r][c+1][k+1] < dp[r][c][k] + G[r][c+1]:
dp[r][c+1][k+1] = dp[r][c][k] + G[r][c+1]
if dp[r][c+1][k] < dp[r][c][k]:
dp[r][c+1][k] = dp[r][c][k]
if dp[r+1][c][0] < dp[r][c][k]:
dp[r+1][c][0] = dp[r][c][k]
if dp[r+1][c][1] < dp[r][c][k] + G[r+1][c]:
dp[r+1][c][1] = dp[r][c][k] + G[r+1][c]
return(max(dp[r][c]))
ret = main(G, R, C)
print(ret)
|
import statistics
while True:
n = int(input())
if n == 0:
break
scores = list((int(x) for x in input().split()))
std = statistics.pstdev(scores)
print("{0:.8f}" . format(round(std,8)))
| 0 | null | 2,878,477,637,530 | 94 | 31 |
class Dice:
def __init__(self,labels):
self.stat = labels
def roll_E(self):
self.stat[0],self.stat[2],self.stat[3],self.stat[5] = self.stat[3],self.stat[0],self.stat[5],self.stat[2]
def roll_N(self):
self.stat[0],self.stat[1],self.stat[5],self.stat[4] = self.stat[1],self.stat[5],self.stat[4],self.stat[0]
def roll_S(self):
self.stat[0],self.stat[1],self.stat[5],self.stat[4] = self.stat[4],self.stat[0],self.stat[1],self.stat[5]
def roll_W(self):
self.stat[0],self.stat[2],self.stat[5],self.stat[3] = self.stat[2],self.stat[5],self.stat[3],self.stat[0]
def get_top(self):
return self.stat[0]
dice = Dice(input().split())
commands = input()
for command in commands:
if command == "E":
dice.roll_E()
elif command == "N":
dice.roll_N()
elif command == "S":
dice.roll_S()
elif command == "W":
dice.roll_W()
print(dice.get_top())
|
class Dice:
def __init__(self, list):
self.list = list #listのinput
def roll_n(self):
self.list = [self.list[1], self.list[5], self.list[2], self.list[3], self.list[0], self.list[4]]
def roll_s(self):
self.list = [self.list[4], self.list[0], self.list[2], self.list[3], self.list[5], self.list[1]]
def roll_e(self):
self.list = [self.list[3], self.list[1], self.list[0], self.list[5], self.list[4], self.list[2]]
def roll_w(self):
self.list = [self.list[2], self.list[1], self.list[5], self.list[0], self.list[4], self.list[3]]
def print0(self):
print(self.list[0])
list = list(map(int, input().split()))
direct = input()
instance = Dice(list)
for i in direct:
if i == 'N':
instance.roll_n()
if i == 'S':
instance.roll_s()
if i == 'E':
instance.roll_e()
if i == 'W':
instance.roll_w()
instance.print0()
| 1 | 231,212,283,068 | null | 33 | 33 |
S = input()
if S.upper() == "ABC":
print("ARC")
else:
print("ABC")
|
DE = 10**9 + 7
N, K = list(map(lambda x: int(x), input().split(' ')))
A = list(map(lambda x: int(x), input().split(' ')))
def main():
A_posi = []
A_nega = []
for a in A:
if a > 0:
A_posi.append(a)
elif a < 0:
A_nega.append(-a)
len_posi = len(A_posi)
len_nega = len(A_nega)
if len_posi + len_nega < K:
return 0
if (len_nega % 2 == 1 and K == len_posi + len_nega) or (K % 2 == 1 and len_posi == 0):
if len_posi + len_nega == N:
A_posi.sort()
A_nega.sort()
answer = 1
k = 0
for a in A_nega:
answer *= (- a) % DE
answer %= DE
k += 1
if k >= K:
break
else:
for a in A_posi:
answer *= a % DE
answer %= DE
k += 1
if k >= K:
break
return answer
else:
return 0
A_posi.sort(reverse=True)
A_nega.sort(reverse=True)
posi = 0
nega = 0
answer = 1
if K % 2 == 1:
answer = A_posi[0] % DE
posi = 1
while posi + nega + 2 <= K:
p = A_posi[posi] * A_posi[posi + 1] if posi + 1 < len_posi else 0
n = A_nega[nega] * A_nega[nega + 1] if nega + 1 < len_nega else 0
if p > n:
answer *= p % DE
answer %= DE
posi += 2
else:
answer *= n % DE
answer %= DE
nega += 2
return answer
print(main())
| 0 | null | 16,940,723,985,220 | 153 | 112 |
h,w = map(int,input().split())
grid = []
for _ in range(h):
grid.append(list(input()))
dp = [[0]*(w+1) for i in range(h+1)]
#dp[i][j] = i,jでの最小の操作回数
if grid[0][0] == '#':
dp[0][0] += 1
for i in range(1,h):
p = 0
if grid[i][0] == '#' and grid[i-1][0] == '.':
p += 1
dp[i][0] = dp[i-1][0] + p
for i in range(1,w):
p = 0
if grid[0][i] == '#' and grid[0][i-1] == '.':
p += 1
dp[0][i] = dp[0][i-1] + p
for x in range(1,h):
for y in range(1,w):
a,b = 0,0
if grid[x][y] == '#' and grid[x-1][y] == '.':
a += 1
if grid[x][y] == '#' and grid[x][y-1] == '.':
b += 1
dp[x][y] = min(dp[x-1][y] + a,dp[x][y-1] + b)
print(dp[h-1][w-1])
#print(dp)
|
#A
H,W = map(int,input().split())
G = [list(str(input())) for _ in range(H)]
inf = float("inf")
dist = [[inf for w in range(W)] for h in range(H)]
if G[0][0] == "#":
dist[0][0] = 1
else:
dist[0][0] = 0
for i in range(H):
for j in range(W):
if i == 0 and j == 0:
pass
elif j == 0:
dist[i][j] = dist[i-1][j]
if G[i][j] == "#" and G[i-1][j] != "#":
dist[i][j]+=1
elif i == 0:
dist[i][j] = dist[i][j-1]
if G[i][j] == "#" and G[i][j-1] != "#":
dist[i][j]+=1
else:
d1 = dist[i-1][j]
d2 = dist[i][j-1]
if G[i][j] == "#" and G[i-1][j] != "#":
d1+=1
if G[i][j] == "#" and G[i][j-1] != "#":
d2+=1
dist[i][j] = min(d1,d2)
print(dist[H-1][W-1])
| 1 | 49,170,472,262,790 | null | 194 | 194 |
h, m = map(int, input().split())
A = sum(list(map(int, input().split())))
if h <= A:
print("Yes")
else:
print("No")
|
H, N = [int(s) for s in input().split()]
A = [int(s) for s in input().split()]
print('Yes' if H - sum(A) <= 0 else 'No')
| 1 | 77,837,729,905,454 | null | 226 | 226 |
N = int(input())
s = list(input())
for i in range(len(s)):
if ord(s[i])+N-ord('A') < 26:
s[i] = chr(ord(s[i])+N)
else:
s[i] = chr(ord(s[i])+N-26)
print(''.join(s))
|
import os, sys, re, math, string
N = int(input())
S = input()
a = string.ascii_uppercase * 2
print(''.join([a[a.index(s) + (N % 26)] for s in S]))
| 1 | 134,823,429,561,998 | null | 271 | 271 |
import math
n = float(input())
area = "%.6f" % float(n **2 * math.pi)
circ = "%.6f" % float(n * 2 * math.pi)
print(area, circ)
|
import math
r = float(input())
print('%.5f' % (r * r * math.pi), '%.5f' % (2 * math.pi * r))
| 1 | 644,998,263,910 | null | 46 | 46 |
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7
def resolve():
n, k = map(int, input().split())
A = list(map(int, input().split()))
res = 1
if n == k:
for a in A:
res = (res * a) % mod
print(res)
exit()
P, M = [], []
for a in A:
P.append(a) if a >= 0 else M.append(a)
if len(P) == n:
A.sort(reverse=True)
for i in range(k):
res = (res * A[i]) % mod
elif len(M) == n:
A.sort(reverse=True) if k % 2 else A.sort()
for i in range(k):
res = (res * A[i]) % mod
else:
P.sort(reverse=True)
M.sort()
B = []
if k % 2:
k -= 1
b = P.pop(0)
B.append(b)
res = (res * b) % mod
for i in range(0, len(P), 2):
if i + 1 < len(P):
B.append(P[i] * P[i + 1])
for j in range(0, len(M), 2):
if j + 1 < len(M):
B.append(M[j] * M[j + 1])
B.sort(reverse=True)
for i in range(k // 2):
res = (res * B[i]) % mod
print(res)
if __name__ == '__main__':
resolve()
|
n, k = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7
AA = sorted(A, key=lambda x: -abs(x))
nega1 = []
posi1 = []
for a in AA[:k]:
if a < 0:
nega1.append(a)
else:
posi1.append(a)
nega2 = []
posi2 = []
for a in AA[k:]:
if a < 0:
nega2.append(a)
else:
posi2.append(a)
if all(a < 0 for a in A) and k % 2 == 1:
AA = sorted(A, key=lambda x: abs(x))
nega1 = []
posi1 = []
for a in AA[:k]:
nega1.append(a)
elif n == k:
pass
elif len(nega1) % 2 != 0:
if posi1 and nega2 and posi2:
if abs(nega1[-1] * nega2[0]) < abs(posi1[-1] * posi2[0]):
del nega1[-1]
nega1.append(posi2[0])
else:
del posi1[-1]
posi1.append(nega2[0])
elif not posi1 or not nega2:
del nega1[-1]
nega1.append(posi2[0])
elif not posi2:
del posi1[-1]
posi1.append(nega2[0])
a = 1
for m in posi1:
a = (a * m) % MOD
for p in nega1:
a = (a * p) % MOD
print(a)
| 1 | 9,362,698,284,524 | null | 112 | 112 |
n,a,b = map(int,input().split())
mod = 10**9+7
total = pow(2,n,mod) - 1 # 制約なしの全パターン_n**2-1※'-1'は0本の花束
# n個からr個を選択した時のパターン総数_組み合わせ重複なし
# n! / (r! * (n-r)!) → nが大き過ぎて出来ない →変形→ n*(n-1)*・・・(n-r+1)/r! ↓関数化
def nCr(n, r, mod):
numerator=1 #分子_n*(n-1)*・・・(n-r+1)
for i in range(n-r+1, n+1):
numerator = (numerator*i) % mod
denominator=1 #分母_r!
for j in range(1,r+1):
denominator = (denominator*j) % mod
# 剰余の計算において割るとよくない → 割るのではなく逆元を掛ける
# フェルマーの小定理_pが素数なら整数aのmod pにおける逆元は a**(p-2) ※aとpは互いに素
return (numerator * pow(denominator,mod-2,mod)) % mod
ans = (total - nCr(n,a,mod) - nCr(n,b,mod)) % mod
print(ans)
|
import math
N, M = list(map(int, input().split()))
n_pairs = math.factorial(N) // (2*math.factorial(N-2)) if N > 1 else 0
m_pairs = math.factorial(M) // (2*math.factorial(M-2)) if M > 1 else 0
print(n_pairs + m_pairs)
| 0 | null | 55,867,032,916,318 | 214 | 189 |
import sys
for i,ws in enumerate(sys.stdin,1):
list = sorted(map(int, ws[:-1].split()))
if list[0] == 0 and list[1] == 0:
break
print(' '.join(map(str,list)))
|
D=int(input())
C=list(map(int,input().split()))
S=list()
for i in range(D):
S.append(list(map(int,input().split())))
t=list()
for i in range(D):
t.append(int(input()))
M=0
di=[0]*26
for d in range(D):
for i in range(26):
if i==t[d]-1:
di[i]=0
else:
di[i]+=1
M+=S[d][t[d]-1]
for i in range(26):
M-=C[i]*(di[i])
print(M)
| 0 | null | 5,178,156,270,910 | 43 | 114 |
n=int(input())
a=list(map(int,input().split()))
ans=1
c=[0,0,0]
for i in range(n):
if a[i] not in c:
ans=0
else:
ans*=c.count(a[i])
ans%=1000000007
c[c.index(a[i])]+=1
print(ans)
|
N=int(input())
l=list(map(int,input().split()))
ans=1
cnt=[0]*(N+1)
mod=10**9+7
cnt[0]=3
for i in l:
ans=ans*cnt[i]%mod
cnt[i]-=1
cnt[i+1]+=1
print(ans)
| 1 | 130,274,319,230,830 | null | 268 | 268 |
R = int(input())
print(R * 2 * 3.1415)
|
import os, sys, re, math
N = int(input())
P = [int(n) for n in input().split()]
answer = 0
m = 2 * 10 ** 5
for p in P:
if p <= m:
answer += 1
m = p
print(answer)
| 0 | null | 58,393,076,992,292 | 167 | 233 |
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()
def resolve():
N = I()
A = []
xy = []
for _ in range(N):
A.append(I())
xy.append([LI() for _ in range(A[-1])])
ans = 0
for i in range(2 ** N):
# まず正直者を仮定し、正直者の証言に矛盾がないか判定
is_ok = True
num = 0
for j in range(N):
if i >> j & 1:
num += 1
for k in xy[j]:
x, y = k
x -= 1
if i >> x & 1 != y:
is_ok = False
if is_ok:
ans = max(num, ans)
print(ans)
if __name__ == '__main__':
resolve()
|
N=int(input())
Ins=[[] for _ in range(N)]
ans=0
#0-indexedにしておく
for i in range(N):
A=int(input())
for _ in range(A):
x,y=map(int,input().split())
Ins[i].append((x-1,y))
for i in range(2**N):
flag=1
honest=[]
unkind=[]
for j in range(N):
if (i>>j)&1:
honest.append(j)
else:
unkind.append(j)
#ここまでは想定の組み合わせを作る作業
for k in honest:
#honestな人の証言を見ていく
for l in Ins[k]:
if l[1]==1 and l[0] in unkind:
flag=0
break
#honestな人がある人物をhonestと答えたのにも関わらず
#その人がunkindに入るような組み合わせは駄目...ケース1
elif l[1]==0 and l[0] in honest:
flag=0
break
#honestな人がある人物をunkindと答えたのにも関わらず
#その人がhonestに入るような組み合わせは駄目...ケース2
if flag==1:
ans=max(ans,len(honest))
#ケース1にもケース2にも該当しない場合
#現在のhonestの人数をansと比較して大きい方を採用
print(ans)
| 1 | 121,963,081,188,634 | null | 262 | 262 |
N = int(input())
get = set()
for _ in range(N):
s = str(input())
get.add(s)
print(len(get))
|
# B
z = list(map(int, input().split(" ")))
maxnum = z[0]*z[2]
for i in range(2):
for j in range(2):
x = z[i]
y = z[j+2]
prod = x*y
# print(x, y, prod, maxnum, prod > maxnum)
if prod > maxnum:
maxnum = prod
print(maxnum)
| 0 | null | 16,700,120,510,632 | 165 | 77 |
import math
def combinations_count(n, r):
return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N, M = map(int, input().split())
ans = 0
if N >= 2:
ans += combinations_count(N, 2)
if M >= 2:
ans += combinations_count(M, 2)
print(ans)
|
N = int(input())
print(N ** 2)
| 0 | null | 95,666,386,391,170 | 189 | 278 |
N = int(input())
S = input()
cnt = 1
last = S[0]
for s in S:
if last == s:
continue
else:
cnt += 1
last = s
print(cnt)
|
N = int(input())
ary = [int(input()) for _ in range(N)]
def insertion_sort(ary, g):
cnt = 0
for i in range(g, N):
v = ary[i]
j = i - g
while j >= 0 and ary[j] > v:
ary[j + g] = ary[j]
j -= g
cnt += 1
ary[j + g] = v
return cnt
def shell_sort(ary):
g = [1]
while True:
if 3 * g[-1] + 1 < N:
g.append(3 * g[-1] + 1)
else:
break
m = len(g)
g = g[::-1]
cnt = 0
for i in range(0, m):
cnt += insertion_sort(ary, g[i])
print(m)
print(' '.join([str(_) for _ in g]))
print(cnt)
[print(_) for _ in ary]
shell_sort(ary)
| 0 | null | 85,204,514,303,452 | 293 | 17 |
x = input()
y = x.split(" ")
y[0] = int(y[0])
y[1] = int(y[1])
if y[0] < y[1]:
print("a < b")
elif y[0] > y[1]:
print("a > b")
else:
print("a == b")
|
x=raw_input()
list=x.split()
a=int(list[0])
b=int(list[1])
if a<b:
print 'a < b'
elif a>b:
print 'a > b'
elif a==b:
print 'a == b'
| 1 | 359,476,728,732 | null | 38 | 38 |
from copy import deepcopy
class Dice():
def __init__(self):
self.E = [4,2,1,6,5,3]
self.N = [2,6,3,4,1,5]
self.S = [5,1,3,4,6,2]
self.W = [3,2,6,1,5,4]
self.order = "NNNNWNNNWNNNENNNENNNWNNN"
def num_input(self):
self.number = list(map(int,input().split()))
def rotate(self,order):
tmp = deepcopy(self.number)
if order == "E":
for i in range(6):
self.number[i] = tmp[self.E[i]-1]
elif order == "N":
for i in range(6):
self.number[i] = tmp[self.N[i]-1]
elif order == "S":
for i in range(6):
self.number[i] = tmp[self.S[i]-1]
elif order == "W":
for i in range(6):
self.number[i] = tmp[self.W[i]-1]
def sidenum(self,x,y):
for i in self.order:
if self.number[1] == y and self.number[0] == x:
print(self.number[2])
break
else:
self.rotate(i)
if __name__ == "__main__":
dice = Dice()
dice.num_input()
q = int(input())
for i in range(q):
x,y = map(int,input().split())
dice.sidenum(x,y)
|
n = int(input())
a = sorted([int(i) for i in input().split()])
cnt = 1
for ai in a:
cnt *= ai
if cnt > 10 ** 18:
print(-1)
exit()
print(cnt)
| 0 | null | 8,256,051,910,252 | 34 | 134 |
N = int(input())
count = [0,1]
if N == 1:
print(1)
exit()
for i in range(2,N+1):
digit = len(str(i))
head = int(str(i)[0])
tail = int(str(i)[-1])
plus = 0
if digit == 1:
plus=1
elif digit == 2:
if head == tail:
plus = 3
elif head>tail and tail>0:
plus = 2
elif tail>0:
for j in range(digit-2):
plus += 10**j*2
if head == tail:
mid = str(i)[1:-1]
while len(mid)>1 and mid[0]=='0':
mid = mid[1:]
mid = int(mid)
plus += 2*mid+1
plus += 2
elif head > tail:
plus += 10**(digit-2)*2
count.append(count[-1]+plus)
print(count[N])
|
S = input()
Q = int(input())
query = [input().split() for _ in range(Q)]
switch = 1
top = []
bottom = []
for i in range(Q):
if query[i][0] == "1":
switch *= -1
else:
f = query[i][1]
c = query[i][2]
if switch == 1:
if f == "1":
top.append(c)
else:
bottom.append(c)
else:
if f == "1":
bottom.append(c)
else:
top.append(c)
if switch == 1:
ans = top[::-1] + list(S) + bottom
else:
ans = bottom[::-1] + list(S)[::-1] + top
print("".join(ans))
| 0 | null | 72,249,364,326,930 | 234 | 204 |
from collections import Counter
n = int(input())
D = list(map(int, input().split()))
c = Counter(D)
total = 1
count = 0
for i in range(n):
num = c.get(i, 0)
if num == 0:
total = 0
break
count += num
if i == 0 and (c[i] != 1 or D[0] != 0):
total = 0
if i > 0:
total = (total * (c[i - 1] ** c[i])) % 998244353
if count == n:
break
print(total)
|
t = list(input())
a = 0
b = 0
for i in range(len(t)):
if i == 0 and t[i] == '?':
t[i] = 'D'
if 0 < i < len(t) - 1 and t[i] == '?':
if t[i-1] == 'P':
t[i] = 'D'
elif t[i+1] == 'P':
t[i] = 'D'
else:
t[i] = 'P'
if i == len(t) -1 and t[i] == '?':
t[i] = 'D'
print(''.join(t))
| 0 | null | 86,830,103,358,418 | 284 | 140 |
num_list = list(map(int, input().split()))
num = num_list[0]
distance = num_list[1]
res = 0
for i in range(num):
nmb_list = list(map(int, input().split()))
if(nmb_list[0]*nmb_list[0]+nmb_list[1]*nmb_list[1] <= distance*distance):
res += 1
print(res)
|
N,D = (int(x) for x in input().split())
count = 0
for i in range(N):
x,y = (int(z) for z in input().split())
dist = x**2 + y**2
if dist <= D**2:
count += 1
print(count)
| 1 | 5,881,671,390,428 | null | 96 | 96 |
a,b=(int(x) for x in input().split())
print(a//b,a%b,"{0:.5f}".format(a/b))
|
a,b=map(int, raw_input().split())
d=a/b
e=a%b
print ('%d %d %f' % (a/b,a%b,float(a)/b))
| 1 | 600,712,077,170 | null | 45 | 45 |
#create date: 2020-06-30 14:42
import sys
stdin = sys.stdin
from itertools import combinations_with_replacement
def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))
def main():
n, m, q = na()
matrix = list()
for i in range(q):
matrix.append(na())
l = list(combinations_with_replacement(range(1, m+1), n))
ans = 0
for li in l:
res = 0
for q in matrix:
a, b, c, d = q
if li[b-1] - li[a-1] == c:
res += d
ans = max(ans, res)
print(ans)
if __name__ == "__main__":
main()
|
from itertools import combinations_with_replacement
n, m, q = map(int, input().split())
list_ABCD = [ list(map(int,input().split(" "))) for _ in range(q)]
list_N = [i for i in range(1, m+1)]
ans = 0
for l in combinations_with_replacement(list_N, n):
score = 0
for a, b, c, d in list_ABCD:
if l[b-1] - l[a-1] == c:
score += d
ans = max(ans, score)
print(ans)
| 1 | 27,836,781,855,872 | null | 160 | 160 |
X, K, D = map(int, input().split())
if X - K * D >= 0:
ans = abs(X - K * D)
elif X + K * D <= 0:
ans = abs(X + K * D)
else:
n = int(abs(X - K * D) / (2 * D))
ans = min(abs(X - K * D + n * 2 * D), abs(X - K * D + (n + 1) * 2 * D))
print(ans)
|
X, K, D = list(map(int, input().split()))
X = abs(X)
if(X >= K * D):
print(X - K * D)
else:
q = X // D
r = X % D
if((K - q) % 2 == 0):
print(r)
else:
print(abs(r - D))
| 1 | 5,231,261,020,928 | null | 92 | 92 |
from itertools import combinations
if __name__ == '__main__':
# ??????????????\???
num1 = int(input())
A = [int(x) for x in input().split(' ')]
num2 = int(input())
M = [int(x) for x in input().split(' ')]
#A = [1, 5, 7, 10, 21]
#pick = 4
#M = [2, 4, 17, 8]
# ????????????
results = ['no' for x in range(len(M))] # M?????¨??????????´?????????????????????????????????§????????????????????±???(no)??¨????????????
for p in range(1, len(A)+1): # A????????????1????????¨????????°?????????????????¢????????????
combi = combinations(A, p) # n??????????????¢??????????????´????????¨?????????????????????
for choice in combi:
total = sum(choice)
while total in M:
i = M.index(total)
results[i] = 'yes'
M[i] = 'Done'
# ???????????????
for txt in results:
print(txt)
|
N=int(input())
if N==0:
print("Yes")
exit()
P=[]
M=[]
for i in range(N):
s=input()
f=0
m=0
cnt=0
for j in range(len(s)):
if s[j]=="(":
cnt+=1
else:
cnt-=1
if cnt<m:
m=cnt
if cnt>=0:
P.append([m,cnt])
else:
M.append([m-cnt,-cnt])
P.sort(reverse=True)
M.sort(reverse=True)
#print(P)
#print(M)
SUM=0
for i,j in P:
SUM+=j
for i,j in M:
SUM-=j
if SUM!=0:
print("No")
exit()
SUMP=0
for i,j in P:
if SUMP>=(-i):
SUMP+=j
else:
print("No")
exit()
SUMM=0
for i,j in M:
if SUMM>=(-i):
SUMM+=j
else:
print("No")
exit()
print("Yes")
| 0 | null | 11,792,187,546,832 | 25 | 152 |
# coding: utf-8
def gcd(a, b):
if a < b:
a, b = b, a
while b:
a, b = b, a % b
return a
def main():
a, b = map(int, raw_input().split())
print gcd(a, b)
if __name__ == '__main__':
main()
|
input()
a = list(map(str, input().split()))
a.reverse()
print(' '.join(a))
| 0 | null | 485,217,748,078 | 11 | 53 |
from collections import defaultdict, deque
N, K = map(int, input().split())
A = list(map(int, input().split()))
cumA = [0] * (N + 1)
for i in range(1, N + 1):
cumA[i] = cumA[i - 1] + A[i - 1]
cnt = defaultdict(int)
que = deque([])
ans = 0
for i, c in enumerate(cumA):
while que and que[0][0] <= i - K:
cnt[que.popleft()[1]] -= 1
diff = (i - c) % K
ans += cnt[diff]
cnt[diff] += 1
que.append((i, diff))
print(ans)
|
import sys
input = sys.stdin.readline
from collections import deque
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = [0]
for a in A:
b = (B[-1] + (a-1)) % K
B.append(b)
ans = 0
dic = {}
for i, b in enumerate(B):
if b in dic:
dic[b].append(i)
else:
dic[b] = deque()
dic[b].append(i)
while len(dic[b]) > 0 and dic[b][0] <= i - K:
dic[b].popleft()
ans += len(dic[b])-1
print(ans)
| 1 | 138,069,092,193,680 | null | 273 | 273 |
import sys
N, M, K = [int(s) for s in sys.stdin.readline().split()]
n = 0
A = [0]
for s in sys.stdin.readline().split():
n = n + int(s)
A.append(n)
n = 0
B = [0]
for s in sys.stdin.readline().split():
n = n + int(s)
B.append(n)
ans = 0
for i in range(N + 1):
if A[i] > K:
break
for j in range(M, -1, -1):
if A[i] + B[j] <= K:
ans = max(ans, i + j)
M = j
break
print(ans)
|
from sys import stdin
n,k = map(int,stdin.readline().split())
p = list(map(int,stdin.readline().split()))
c = list(map(int,stdin.readline().split()))
ans = -float("inf")
def helper(index):
visited = {index}
looplen = 1
looptotal = c[index]
while p[index]-1 not in visited:
nextindex = p[index] - 1
looplen += 1
looptotal += c[nextindex]
visited.add(nextindex)
index = nextindex
return (looplen,looptotal)
for i in range(n):
looplen,looptotal = helper(i)
if looptotal >= 0:
cur = looptotal*((k//looplen)-1) if k >= looplen else 0
remain = looplen + (k%looplen) if k >= looplen else k
else:
cur = 0
remain = looplen
curmax = -float("inf")
curindex = i
for _ in range(remain):
nextindex = p[curindex]-1
cur += c[nextindex]
curmax = max(curmax,cur)
curindex = nextindex
ans = max(ans,curmax)
print(ans)
| 0 | null | 8,117,566,056,260 | 117 | 93 |
import math
N,D,A = map(int,input().split())
l = []
for i in range(N):
l.append(list(map(int,input().split())))
l.sort()
lx = []
lc = []
for i in l:
X,H = i[0],i[1]
lx.append(X)
lc.append(math.ceil(H/A))
migi = []
m = 0
i = 0
while i <= N-1:
if lx[m] - lx[i] > 2*D:
migi.append(m)
i += 1
elif m != N-1:
m += 1
elif m == N-1:
migi.append(-1)
i += 1
cnt = 0
dam = [0 for i in range(N)]
for i in range(N):
if dam[i] < lc[i]:
c = lc[i] - dam[i]
dam[i] += c
if migi[i] >= 0:
dam[migi[i]] -= c
cnt += c
if i <=N-2:
dam[i+1] += dam[i]
print(cnt)
|
# coding: utf-8
num_array = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
k = int(input())
print("{}".format(num_array[k-1]))
| 0 | null | 66,102,835,844,858 | 230 | 195 |
def resolve():
N, X, M = map(int, input().split(" "))
checked = [False] * M
val = X
ans = 0
val_list = []
base = M
for i in range(N):
if checked[val] != False:
loop_start_index = checked[val]
loop_vals = val_list[loop_start_index:]
loop_val = sum(loop_vals)
loop_length = len(val_list[loop_start_index:])
if loop_length != 0:
ans += ((N - i) // loop_length) * loop_val
for i in range(((N - i) % loop_length)):
ans += loop_vals[i]
break
ans += val
checked[val] = i
val_list.append(val)
val*=val
if val >= base:
val %= base
print(ans)
if __name__ == "__main__":
resolve()
|
def solve():
H,N = map(int,input().split())
ap = []
mp = []
for _ in range(N):
a,b = map(int,input().split())
ap.append(a)
mp.append(b)
ap_max = max(ap)
dp = [[float('inf')] * (H+ap_max+1) for _ in range(N+1)]
for i in range(N+1):
dp[i][0] = 0
for i in range(N):
for sum_h in range(H+ap_max+1):
dp[i+1][sum_h] = min(dp[i][sum_h-ap[i]] + mp[i], dp[i][sum_h])
dp[i+1][sum_h] = min(dp[i+1][sum_h-ap[i]] + mp[i], dp[i][sum_h])
ans = float('inf')
for sum_h in range(H, H+ap_max+1):
ans = min(ans,dp[N][sum_h])
print(ans)
if __name__ == '__main__':
solve()
| 0 | null | 42,160,352,013,630 | 75 | 229 |
class Dice:
def __init__(self, a, b, c, d, e, f):
# サイコロの現在一番上にある面
self.a = a
self.b = b
self.c = c
self.d = d
self.e = e
self.f = f
def move(self, move_str):
for i in move_str:
if i == "N":
self.move_N()
elif i == "E":
self.move_E()
elif i == "W":
self.move_W()
elif i == "S":
self.move_S()
def move_N(self):
tmp1 = self.a
tmp2 = self.e
self.a = self.b
self.b = self.f
self.e = tmp1
self.f = tmp2
def move_E(self):
tmp1 = self.a
tmp2 = self.c
self.a = self.d
self.c = tmp1
self.d = self.f
self.f = tmp2
def move_W(self):
tmp1 = self.a
tmp2 = self.d
self.a = self.c
self.c = self.f
self.d = tmp1
self.f = tmp2
def move_S(self):
tmp1 = self.a
tmp2 = self.b
self.a = self.e
self.b = tmp1
self.e = self.f
self.f = tmp2
"""
def debug(self):
print("--------")
print(f"{self.a=}")
print(f"{self.b=}")
print(f"{self.c=}")
print(f"{self.d=}")
print(f"{self.e=}")
print(f"{self.f=}")
print("--------")
"""
a, b, c, d, e, f = map(int, input().split())
dice = Dice(a, b, c, d, e, f)
li = list(input())
dice.move(li)
print(dice.a)
|
N = int(input())
print(N*N)
| 0 | null | 72,945,498,107,650 | 33 | 278 |
class Dice:
def __init__(self,num):
self.num = num.copy()
def east(self):
temp = self.num.copy()
self.num[1-1] = temp[4-1]
self.num[4-1] = temp[6-1]
self.num[6-1] = temp[3-1]
self.num[3-1] = temp[1-1]
def north(self):
temp = self.num.copy()
self.num[1-1] = temp[2-1]
self.num[2-1] = temp[6-1]
self.num[6-1] = temp[5-1]
self.num[5-1] = temp[1-1]
def south(self):
temp = self.num.copy()
self.num[1-1] = temp[5-1]
self.num[5-1] = temp[6-1]
self.num[6-1] = temp[2-1]
self.num[2-1] = temp[1-1]
def west(self):
temp = self.num.copy()
self.num[1-1] = temp[3-1]
self.num[3-1] = temp[6-1]
self.num[6-1] = temp[4-1]
self.num[4-1] = temp[1-1]
def right(self):
temp = self.num.copy()
self.num[2-1] = temp[4-1]
self.num[4-1] = temp[5-1]
self.num[5-1] = temp[3-1]
self.num[3-1] = temp[2-1]
num = list(map(int,input().split()))
dice = Dice(num)
q = int(input())
for _ in range(q):
top,front = map(int,input().split())
while not (top == dice.num[0] or front == dice.num[1]): dice.north()
while top != dice.num[0]: dice.east()
while front != dice.num[1]: dice.right()
print(dice.num[2])
|
import random
class Cube:
def __init__(self, u, s, e, w, n, d):
self.u = u
self.s = s
self.e = e
self.w = w
self.n = n
self.d = d
def rotate(self, dic):
if dic == "N":
tmp = self.u
self.u = self.s
self.s = self.d
self.d = self.n
self.n = tmp
elif dic == "E":
tmp = self.u
self.u = self.w
self.w = self.d
self.d = self.e
self.e = tmp
elif dic == "W":
tmp = self.u
self.u = self.e
self.e = self.d
self.d = self.w
self.w = tmp
else:
tmp = self.u
self.u = self.n
self.n = self.d
self.d = self.s
self.s = tmp
def main():
u, s, e, w, n, d = map(int, input().split())
cube = Cube(u, s, e, w, n, d)
q = int(input())
for i in range(q):
upper, front = map(int, input().split())
while True:
cube.rotate(random.choice("NWES"))
if upper == cube.u and front == cube.s:
print(cube.e)
break
if __name__ == '__main__':
main()
| 1 | 262,065,652,836 | null | 34 | 34 |
A, B, K = map(int, input().split())
if A < K:
K = K - A
A = 0
elif A == K:
A = 0
K = 0
else: # K < A
A = A - K
K = 0
if B < K:
K = K - B
B = 0
elif B == K:
B = 0
K = 0
else: # K < B
B = B - K
K = 0
print(A, B)
|
A, B, K = map(int, input().split())
if A > K:
print(A-K, B)
else:
print(0, max(0, A+B-K))
| 1 | 104,661,051,715,512 | null | 249 | 249 |
def main():
import sys
input=sys.stdin.buffer.readline
h,w=map(int,input().split())
b,q=b'.'*w,range(w)
for i in range(h):
s=input()
a=[]
a_add=a.append
for x,y,z,c in zip(b,b'.'+s,s,q):
if x==46>z:c+=1
if y==46>z:i+=1
if i>c:i=c
a_add(i)
b,q=s,a
print(i)
main()
|
n = int(input())
S = list(map(int, input().split()))
q = int(input())
T = map(int, input().split())
count = 0
for key in T:
U = S + [key]
i = 0
while U[i] != key:
i = i+1
if i != n:
count = count + 1
print(count)
| 0 | null | 24,827,833,168,490 | 194 | 22 |
n = int(input())
lst = [ int(i) for i in input().split() ]
visited = {}
for d in lst:
if d in visited.keys():
visited[d] += 1
else:
visited[d] = 1
count = 0
for d in visited.keys():
count += int(visited[d]*(visited[d]-1)/2)
for d in lst:
v = visited[d] - 1
ans = count - v
print(ans)
|
def main():
from math import gcd
K = int(input())
ans = 0
for a in range(1, K+1):
for b in range(1, K+1):
g = gcd(a, b)
for c in range(1, K+1):
ans += gcd(g, c)
print(ans)
if __name__ == '__main__':
main()
| 0 | null | 41,529,964,133,240 | 192 | 174 |
s = input()
if s[-1] != "s":
s = s + "s"
else:
s = s + "es"
print(s)
|
str=input()
if str.endswith("s"):
print(str+"es")
else:
print(str+"s")
| 1 | 2,391,486,808,092 | null | 71 | 71 |
R,C,K = map(int,input().split()) #行、列、個数
G = [[0]*C for _ in range(R)] #G[i][j]でi行目のj番目にある価値
for i in range(K):
a,b,v = map(int,input().split())
a-=1;b-=1 #0index
G[a][b] = v
#print(G)
dp = [[0]*4 for _ in range(C+1)]
#dp[j][k]ある行において、j個目まで見て、その行でk個拾っている
for i in range(R):
p = [[0]*4 for _ in range(C+1)] #pが前の行、dpが今の行
p,dp = dp,p
for j in range(C):
#print(j+1,dp)
for k in range(4):
if k == 0: #その行で一個も取っていなければ上の行から来るだけ
dp[j+1][k] = max(p[j+1])
elif k == 1: #一個目をとるということは横からきて取るか、上から来て取るか
if G[i][j] == 0: #そこにない場合は横から
dp[j+1][k] = dp[j][k]
else: #ある場合は横からくるか上から来て一個取るか
dp[j+1][k] = max(dp[j][k],max(p[j+1])+G[i][j])
else: #二個以上の場合には上からは来れない。
if G[i][j] == 0: #そこになければ横からくるだけ
dp[j+1][k] = dp[j][k]
else: #あればとったほうがよいか比較
dp[j+1][k] = max(dp[j][k],dp[j][k-1]+G[i][j])
#print(p,dp)
ans = max(dp[C])
print(ans)
|
from functools import reduce
n,a,b=map(int,input().split())
mod=10**9+7
def nCk(n,k):
if n<k or (n<0 or k<0):
return 0
k=min(k,n-k)
up,down=1,1
for i in range(k):
up=up*(n-i)%mod
down=down*(i+1)%mod
return up*pow(down,mod-2,mod)%mod
all=pow(2,n,mod)
nCa=nCk(n,a)
nCb=nCk(n,b)
print((all-nCa-nCb-1)%mod)
| 0 | null | 35,677,870,358,330 | 94 | 214 |
n = int(input())
print(" ".join(reversed(input().split())))
|
numbers = raw_input()
input_numbers = raw_input().split()
print(" ".join(input_numbers[::-1]))
| 1 | 987,448,968,858 | null | 53 | 53 |
from collections import deque
N = int(input())
A = deque(map(int, input().split()))
mod = 10**9 + 7
M = len(bin(max(A))) - 2
ans = 0
bitlist = [1]
anslist = [0 for _ in range(M)]
for k in range(M):
bitlist.append(bitlist[-1]*2%mod)
counter = [0 for _ in range(M)]
for k in range(N):
a = A.pop()
c = 0
while a:
b = a & 1
if b == 0:
anslist[c] += counter[-c-1]
else:
anslist[c] += k - counter[-c-1]
counter[-c-1] += 1
c += 1
a >>= 1
while c < M:
anslist[c] += counter[-c-1]
c += 1
for k in range(M):
ans += anslist[k]*bitlist[k]%mod
ans %= mod
print(ans)
|
N=int(input())
A=list(map(int,input().split()))
l=[0]*60
for i in A:
for j in range(60):
if i&(1<<j):l[j]+=1
ans=0
for i in range(60):
ans+=l[i]*(N-l[i])*(1<<i)
ans%=10**9+7
print(ans)
| 1 | 122,395,874,521,690 | null | 263 | 263 |
n = int(input())
ans = 0
if n//500>0:
ans += 1000 * (n//500)
n = n - 500 * (n//500)
if n//5>0:
ans += 5 * (n//5)
print(ans)
|
N = int(input())
S = input()
revALPH = "".join(list(reversed("ABCDEFGHIJKLMNOPQRSTUVWXYZ")))
A = ""
for s in S:
pos = revALPH.find(s)
A += revALPH[pos - N]
print(A)
| 0 | null | 88,608,280,767,930 | 185 | 271 |
import sys
def value(w,n,P):
tmp_w = 0
k = 1
for i in range(n):
if tmp_w + w[i] <= P:
tmp_w += w[i]
else:
tmp_w = w[i]
k += 1
return k
def test():
inputs = list(map(int,sys.stdin.readline().split()))
n = inputs[0]
k = inputs[1]
w = []
max = 0
sum = 0
for i in range(n):
w.append(int(sys.stdin.readline()))
if w[i] > max:
max = w[i]
sum += w[i]
while max != sum:
mid = (max + sum) // 2
if value(w,n,mid) > k:
max = mid + 1
else:
sum = mid
print(max)
if __name__ == "__main__":
test()
|
import numpy as np
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
S1 = (A1 - B1) * T1
S2 = (A2 - B2) * T2
if (S1 + S2) == 0:
print('infinity')
elif np.sign(S1) == np.sign(S2):
print(0)
elif abs(S1) > abs(S2):
print(0)
else:
z = 1 + ((abs(S1)) // abs(S1 + S2)) * 2
if (abs(S1)) % abs(S1 + S2) == 0:
z = z -1
print(z)
| 0 | null | 66,094,273,320,000 | 24 | 269 |
"""
Given N, D, A, N means no. of monsters, D means distances of bombs, A attack of the bombs
Find the minimum of bombs required to win.
Each monster out of N pairs - pair[0] = position, pair[1] = health
"""
from collections import deque
mod = 1e9+7
def add(a, b):
c = a + b
if c >= mod:
c -= mod
return c
def main():
N, D, A = [int(x) for x in raw_input().split()]
D *= 2
monsters = []
for _ in range(N):
pos, health = [int(x) for x in raw_input().split()]
monsters.append([pos, health])
monsters.sort(key = lambda x : x[0])
remain_attacks_queue = deque([])
remain_attack = 0
ans = 0
for monster in monsters:
# monster[0] - pos, monster[1] - health
# queue[0] - position, queue[1] - attack points
while len(remain_attacks_queue) and monster[0] - D > remain_attacks_queue[0][0]:
remain_attack -= remain_attacks_queue.popleft()[1]
if remain_attack < monster[1]:
remained_health = monster[1] - remain_attack
times = remained_health / A if remained_health % A == 0 else remained_health // A + 1
#print(times)
attack = times * A
remain_attacks_queue.append([monster[0], attack])
ans += times
remain_attack += attack
print(ans)
main()
|
from collections import deque
n,d,a = map(int,input().split())
c,cc = 0,0
deq = deque()
dl = [tuple(map(int,input().split())) for i in range(n)]
dl.sort()
for i in dl:
x,h = i
while deq:
if deq[0][0] < x-2*d:
cc -= deq.popleft()[1]
else:
break
h -= a*cc
if h > 0:
c += (h-1)//a + 1
cc += (h-1)//a + 1
deq.append((x,(h-1)//a + 1))
print(c)
| 1 | 82,155,993,488,320 | null | 230 | 230 |
N, M = map(int, input().split())
res = 0
if N >= 2:
res += N * (N - 1) / 2
if M >= 2:
res += M * (M - 1) / 2
print(int(res))
|
s1 = ''
try:
while True:
t = input()
if t == '':
break
s1 += t
except EOFError:
pass
n = [0] * 26
s = s1.lower()
for i in range(len(s)):
j = ord(s[i]) - 97
if j >= 0 and j < 26:
n[j] += 1
for i in range(26):
print('%s : %d' %(chr(i + 97),n[i]))
| 0 | null | 23,517,368,872,338 | 189 | 63 |
n = int(input())
ans= 0
for i in range(1,n+1):
j = n//i
ans += j*(j+1)*i/2
print(int(ans))
|
s = int(input())
a = [-1]*(s+10)
mod = 10**9 + 7
a[0] = 1
a[1] = 0
a[2] = 0
a[3] = 1
if s > 3:
for i in range (4,s+1):
a[i] = (a[i-1]+a[i-3])%mod
print(a[s])
| 0 | null | 7,242,285,683,358 | 118 | 79 |
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=0;r=10**10
while r-l>1:
x=(l+r)//2
ct=0
for i in range(n):
ct+=(a[i]-1)//x
if ct<=k:
r=x
else:
l=x
print(r)
|
n, k = map(int, input().split())
a = list(map(int, input().split()))
def f(m):
res = sum([-(-x//m)-1 for x in a])
return res <= k
l, r = 0, 10**9+10
while r-l > 1:
x = (l+r)//2
if f(x): r = x
else: l = x
print(r)
| 1 | 6,507,365,953,312 | null | 99 | 99 |
while 1:
n,x=map(int,input().split())
if n+x==0:break
print(sum([max(0,(x-a-1)//2-max(a,x-a-1-n))for a in range(1,x//3)]))
|
def f(n,k):
count = 0
for x in range(1,n+1):
for y in range(x,n+1):
for z in range(y,n+1):
if x + y + z == k:
if x != y and y != z and z != x:
count += 1
return count
while True:
n,x = map(int,input().split())
if n == 0 and x == 0:
break
print(f(n,x))
| 1 | 1,289,967,966,720 | null | 58 | 58 |
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from math import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect
import heapq
#
#
#
# pythonで無理なときは、pypyでやると正解するかも!!
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
#
# インデックス系
# int min_y = max(0, i - 2), max_y = min(h - 1, i + 2);
# int min_x = max(0, j - 2), max_x = min(w - 1, j + 2);
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
#mod = 998244353
INF = float('inf')
from sys import stdin
readline = stdin.readline
def readInts():
return list(map(int,readline().split()))
def readTuples():
return tuple(map(int,readline().split()))
def I():
return int(readline())
n,x,m = readInts()
lis = []
prv = None
dic = defaultdict(int)
for i in range(m):
if i == 0:
A = x%m
lis.append(A)
dic[A] = 1
else:
A = (A*A)%m
if dic[A]:
prv = A
break
else:
dic[A] = 1
lis.append(A)
cnt = None
for i in range(len(lis)):
if lis[i] == prv:
cnt = i
break
if cnt == None:
cnt = len(lis)
front_arr = lis[:cnt]
loop_arr = lis[cnt:]
if x == 0:
print(0)
exit()
len_loop_arr = len(loop_arr)
if n < cnt:
ans = sum(front_arr[:n])
else:
ans = sum(front_arr)
sum_loop_arr = sum(loop_arr)
n -= cnt
loop = n//len_loop_arr
rest = n - (loop*len_loop_arr)
mid = loop * sum_loop_arr
ans += mid
ans += sum(loop_arr[:rest])
print(ans)
|
n,m,l=map(int,input().split())
b=[]
for i in range(n):
x= list(map(int, input().split()))
b.append(x)
y=[]
for i in range(m):
z= list(map(int, input().split()))
y.append(z)
c = [[0] * l for i in range(n)]
for i in range(n):
for j in range(l):
for k in range(m):
c[i][j] += b[i][k] * y[k][j]
print(*c[i]) #*をつけると[]が外れて表示される
| 0 | null | 2,149,356,881,512 | 75 | 60 |
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")
a = l()
a = list(set(a))
if len(a) == 2:
print("Yes")
else:
print("No")
|
def main():
a, b, c = map(int, input().split())
if a == b == c:
print('No')
elif a != b and b != c and c != a:
print('No')
else:
print('Yes')
if __name__ == '__main__':
main()
| 1 | 67,958,579,528,268 | null | 216 | 216 |
n=int(input())
s=["AC","WA","TLE","RE"]
c=[0]*4
for _ in range(n):
c[s.index(input())]+=1
for s1,c1 in zip(s,c):
print(f'{s1} x {c1}')
|
ans = {
'AC' : 0,
'WA' : 0,
'TLE' : 0,
'RE' : 0
}
for i in range(int(input())):
ans[input()] += 1
for k, v in ans.items():
print(k, 'x', v)
| 1 | 8,727,040,372,674 | null | 109 | 109 |
N = list(input())
if '7' in N:
print('Yes')
else:
print('No')
|
print('Yes' if '7' in input() else ('No'))
| 1 | 34,484,997,763,332 | null | 172 | 172 |
n = int(input())
print("Yes" if n % 9 == 0 else "No")
|
a=int(input())
b=int(input())
v=[1,2,3]
for i in v:
if a!=i and b!=i:
print(i)
break
| 0 | null | 57,687,009,877,718 | 87 | 254 |
R, C, K = map(int, input().split())
V = [[0]*(C+1) for _ in range(R+1)]
for _ in range(K):
r, c, v = map(int, input().split())
V[r][c] = v
max_p = [0]*(C+1)
for i in range(R+1):
now0, now1, now2, now3 = [[0]*(C+1) for _ in range(4)]
for j in range(C+1):
v = V[i][j]
la = lb = 0
if j > 0:
la = now0[j-1]
lb = now1[j-1]
now2[j] = max(now2[j], now2[j-1], lb + v)
now3[j] = max(now3[j], now3[j-1], now2[j-1] + v)
if i > 0:
now0[j] = max(now0[j], max_p[j], la)
now1[j] = max(now1[j], max_p[j] + v, la + v, lb)
max_p[j] = max(now0[j], now1[j], now2[j], now3[j])
print(max(now0[C], now1[C], now2[C], now3[C]))
|
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
if __file__=="test.py":
f = open("./in_out/input.txt", "r", encoding="utf-8")
read = f.read
readline = f.readline
def main():
R, C, K = map(int, readline().split())
L_INF = int(1e17)
dp = [[[-L_INF for _ in range(C+1)] for _ in range(R+1)] for _ in range(4)]
cell = [[0 for _ in range(C+1)] for _ in range(R+1)]
for i in range(K):
x, y, c = map(int, readline().split())
cell[x-1][y-1] = c
dp[0][0][1] = dp[0][1][0] = 0
for i in range(1, R + 1):
for j in range(1, C + 1):
for k in range(4):
if k > 0:
dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j])
dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
if k > 0:
dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j-1] + cell[i-1][j-1])
if k == 1:
dp[1][i][j] = max(dp[1][i][j], dp[3][i-1][j] + cell[i-1][j-1])
print(dp[3][R][C])
if __name__ == "__main__":
main()
| 1 | 5,568,381,466,912 | null | 94 | 94 |
import math
import numpy as np
a,b,h,m = list(map(int, input().split()))
a_radian = math.radians(h * 30 + m / 2)
a_ = np.array([a * math.cos(a_radian), a * math.sin(a_radian)])
b_radian = math.radians(6 * m)
b_ = np.array([b * math.cos(b_radian), b * math.sin(b_radian)])
print(np.linalg.norm(a_ - b_))
|
import math
a, b, h, m = map(int, input().split())
angle = ((h * 60 + m) / 720 - m / 60) * math.pi * 2 # 角度(ラジアン)
ans = math.sqrt(a*a + b*b - 2*a*b*math.cos(angle)) # 余弦定理
print(ans)
| 1 | 20,204,557,544,860 | null | 144 | 144 |
h, w, k = map(int, input().split())
choco = [[0] * w for _ in range(h)]
for i in range(h):
s = input()
for j in range(w):
choco[i][j] = int(s[j])
ans = float("inf")
for bit in range(1 << (h - 1)):
idx = [0] * h
group = 0
for i in range(h):
idx[i] = group
if (bit >> i) & 1:
group += 1
group += 1
array = [[0] * w for _ in range(group)]
for j in range(w):
for i in range(h):
array[idx[i]][j] += choco[i][j]
cnt = group - 1
cum = [0] * group
canAdd = True
for i in range(w):
for j in range(group):
if cum[j] > k:
canAdd = False
if ans <= cnt:
canAdd = False
if canAdd is False:
break
for j in range(group):
tmp = cum[j] + array[j][i]
if tmp > k:
cnt += 1
for l in range(group):
cum[l] = array[l][i]
break
cum[j] += array[j][i]
if canAdd:
ans = min(ans, cnt)
print(ans)
|
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
def main():
H,W,K = map(int, readline().split())
S = [list(map(int, readline().strip())) for j in range(H)]
white = 0
for line in S:
white += sum(line)
if white <= K:
print(0)
exit()
# 横線の決め方を全探索
ans = 10**5
#yに横線Patternごとの列合計を作成
#入力例1のPattern[bin(2)]場合、y=[[1,1,1,0,0], [1,0,1,1,2]]
for pattern in range(2**(H-1)):
# 初期化
impossible = False
x = 0
ly = bin(pattern).count("1")
y = [S[0]]
line = 0
for i in range(1,H):
if (pattern >> i-1) & 1:
line += 1
y.append(S[i])
else:
y[line] = [y[line][j] + S[i][j] for j in range(W)]
# 各列の値を加算していく
count = [0]*(ly + 1)
for j in range(W):
for i in range(line+1):
if y[i][j] > K :
impossible = True
break
count[i] += y[i][j]
#print("横Pattern{} 縦列まで {} カウント数{} 縦線の数{}".format(i, j, count[i], x))
#横Patten毎にj列までのホワイトチョコ合計数をカウント、
#カウント>Kとなったら、縦線数を+1、その列値でカウント数を初期化
if count[i] > K:
x += 1
for i in range(line+1):
count[i] = y[i][j]
break
#x縦線の数 + ly横線の数がAnsより大きくなったらBreak
if x + ly > ans or impossible:
impossible = True
break
if impossible:
x = 10**6
#x縦線の数 + ly横線の数がAnsより小さくなったらAnsを更新
ans = min(ans, x + ly)
print(ans)
main()
| 1 | 48,501,621,287,140 | null | 193 | 193 |
n = int(raw_input())
ai_list = map(int, raw_input().split())
max_ai = max(ai_list)
min_ai = min(ai_list)
sum_ai = sum(ai_list)
print '%d %d %d' % (min_ai, max_ai, sum_ai)
|
n = int(input())
num = [int(i) for i in input().split()]
print("{0} {1} {2}".format(min(num),max(num),sum(num)))
| 1 | 719,351,010,246 | null | 48 | 48 |
def resolve():
data = [len(x) for x in input().split("S") if x]
print(max(data) if data else 0)
resolve()
|
import numpy as np
count = 0
n,d = map(int,input().split())
for _ in range(n):
p,q = map(int, input().split())
if np.sqrt(p**2+q**2) <= d:
count += 1
print(count)
| 0 | null | 5,403,939,731,740 | 90 | 96 |
def main():
M1, D1 = map(int, input().split())
M2, D2 = map(int, input().split())
if D2 == 1:
print(1)
else:
print(0)
if __name__ == "__main__":
main()
|
from collections import defaultdict
it = lambda: list(map(int, input().strip().split()))
def solve():
N, X, M = it()
if N == 1: return X % M
cur = 0
cnt = 0
value = defaultdict(int)
history = defaultdict(int)
for i in range(N):
if X in history: break
value[X] = cur
history[X] = i
cnt += 1
cur += X
X = X * X % M
loop = cur - value[X]
period = i - history[X]
freq, rem = divmod(N - cnt, period)
cur += freq * loop
for i in range(rem):
cur += X
X = X * X % M
return cur
if __name__ == '__main__':
print(solve())
| 0 | null | 63,895,791,454,762 | 264 | 75 |
from itertools import permutations as P
from math import sqrt as r
N = int(input())
xy = [list(map(int, input().split())) for _ in range(N)]
ans_sum = 0
town = list(P(list(range(N)), N))
Q = len(town)
for town in town:
d = 0
for i in range(N - 1):
X = xy[town[i]][0] - xy[town[i + 1]][0]
Y = xy[town[i]][1] - xy[town[i + 1]][1]
d += r(X**2 + Y**2)
#print(d)
ans_sum += d
print(ans_sum/Q)
|
N = int(input())
result = ["a"]
alph = "abcdefghijklmn"
def gen(w):
ret = []
s = len(set(w))
for ch in alph[:s+1]:
ret.append(w+ch)
return ret
i = 0
w = result[i]
while len(w) <= N:
w = result[i]
result.extend(gen(w))
i += 1
if len(w) == N:
print(w)
| 0 | null | 100,416,692,169,640 | 280 | 198 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.