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
|
---|---|---|---|---|---|---|
import sys
def input(): return sys.stdin.readline().strip()
def resolve():
n=int(input())
l=[list(input().split()) for i in range(n)]
x=input()
cnt=0
for s,t in l:
if s!=x:
cnt+=int(t)
elif s==x:
cnt+=int(t)
break
suml=0
for i in range(n):
suml+=int(l[i][1])
print(suml-cnt)
resolve() | n,k=[int(s) for s in input().split()]
h=[int(j) for j in input().split()]
r=0
for i in range(n):
if k<=h[i]:
r=r+1
print(r) | 0 | null | 138,229,981,229,410 | 243 | 298 |
k = int(input())
if k % 7 == 0:
l = 9*k // 7
else:
l = 9*k
if l % 2 == 0 or l % 5 == 0:
print(-1)
else:
pmo = 1
for i in range(1, l + 1):
mo = (pmo * 10) % l
if mo == 1:
print(i)
break
else:
pmo = mo | ans = -1
a = 0
K = int(input())
if K % 2 == 0:
ans = -1
elif K % 5 == 0:
ans = -1
else:
for i in range(0, K):
a = (10 * a + 7) % K
if a == 0:
ans = i + 1
break
print(ans) | 1 | 6,076,463,843,914 | null | 97 | 97 |
X = int(input())
for a in range(-118,120):
for b in range(-119,119):
if a**5 - b**5 == X:
ans = (a,b)
break
print(ans[0],ans[1])
| import sys
sys.setrecursionlimit(200001)
N, M = [int(n) for n in input().split()]
S = input()
p1 = False
x1 = 0
m1 = N
c1 = 0
for n in S:
if n == "1":
if p1:
c1 += 1
else:
c1 = 1
p1 = True
else:
p1 = False
if c1 > 0:
if c1 > x1:
x1 = c1
if c1 < m1:
m1 = c1
def rest(l, ans):
s = M
while s > 0:
if s >= l:
ans.append(l)
return ans
if S[l-s] == "1":
if s == 1:
return -1
s -= 1
continue
l -= s
if rest(l, ans) == -1:
s -= 1
else:
ans.append(s)
return ans
return -1
if x1 > M - 1:
ans = -1
else:
ans = rest(N, [])
if ans == -1:
print(-1)
else:
print(" ".join([str(n) for n in ans])) | 0 | null | 82,180,289,316,512 | 156 | 274 |
n, m, k = map(int, input().split())
mod = 998244353
def _fac_inv(_n, _mod):
_fac = [1] * (_n+1) # 階乗
_inv = [1] * (_n+1) # 逆元
for i in range(_n):
_fac[i+1] = _fac[i] * (i+1) % _mod
_inv[n] = pow(_fac[n], mod-2, mod)
for i in range(_n, 0, -1):
_inv[i-1] = _inv[i] * i % _mod
return _fac, _inv
fac, inv = _fac_inv(n, mod)
ans = 0
mm = (m * pow(m-1, n-1-k, mod)) % mod
for ii in range(k+1):
i = k-ii
ans = (ans + mm * fac[n-1] * inv[i] * inv[n-1-i]) % mod
mm = mm * (m-1) % mod
print(ans)
| N,M,K=map(int,input().split())
P=998244353
class FactInv:
def __init__(self,N,P):
fact=[];ifact=[];fact=[1]*(N+1);ifact=[0]*(N+1)
for i in range(1,N):
fact[i+1]=(fact[i]*(i+1))%P
ifact[-1]=pow(fact[-1],P-2,P)
for i in range(N,0,-1):
ifact[i-1]=(ifact[i]*i)%P
self.fact=fact;self.ifact=ifact;self.P=P
def comb(self,n,k):
return (self.fact[n]*self.ifact[k]*self.ifact[n-k])%self.P
FI=FactInv(N+10,P)
ans=0
for k in range(0,K+1):
ans+=(M*FI.comb(N-1,k)*pow(M-1,N-1-k,P))%P
ans%=P
print(ans) | 1 | 23,250,698,544,278 | null | 151 | 151 |
n = int(input())
a = [int(i) for i in input().split()]
b = []
sum_a = sum(a)
cum = 0
for i in a:
cum += i
b.append(abs(sum_a - cum * 2))
print(min(b)) | from itertools import permutations
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
r = 0
for i, x in enumerate(permutations([j for j in range(1,N+1)])):
if x == P:
r = abs(r-i)
if x == Q:
r = abs(r-i)
print(r)
| 0 | null | 121,497,666,050,410 | 276 | 246 |
# from math import sqrt
# from heapq import heappush, heappop
# from collections import deque
from functools import reduce
# a, b = [int(v) for v in input().split()]
def main():
mod = 1000000007
n, k = map(int, input().split())
fact1 = [0] * (n + 1) # 1/P!
fact1[0] = 1
for i in range(1, n + 1):
fact1[i] = (fact1[i - 1] * i) % mod
fact2 = [0] * (n + 1) # P!
fact2[n] = pow(fact1[n], mod-2, mod)
for i in range(n, 0, -1):
fact2[i - 1] = (fact2[i] * i) % mod
def cmb2(n, r, mod):
if r < 0 or r > n:
return 0
return fact1[n] * fact2[n - r] * fact2[r] % mod
ans = 0
for i in range(min(n - 1, k) + 1):
ans += cmb2(n, i, mod) * cmb2(n - 1, i, mod)
ans %= mod
print(ans)
main()
| N, K = [int(_) for _ in input().split()]
MOD = 10 ** 9 + 7
ans = 0
class ModFactorial:
"""
階乗, 組み合わせ, 順列の計算
"""
def __init__(self, n, MOD=10 ** 9 + 7):
"""
:param n: 最大の要素数.
:param MOD:
"""
kaijo = [0] * (n + 10)
gyaku = [0] * (n + 10)
kaijo[0] = 1
kaijo[1] = 1
for i in range(2, len(kaijo)):
kaijo[i] = (i * kaijo[i - 1]) % MOD
gyaku[0] = 1
gyaku[1] = 1
for i in range(2, len(gyaku)):
gyaku[i] = pow(kaijo[i], MOD - 2, MOD)
self.kaijo = kaijo
self.gyaku = gyaku
self.MOD = MOD
def nCm(self, n, m):
return (self.kaijo[n] * self.gyaku[n - m] * self.gyaku[m]) % self.MOD
def nPm(self, n, m):
return (self.kaijo[n] * self.gyaku[n - m]) % self.MOD
def factorial(self, n):
return self.kaijo[n]
mf = ModFactorial(N, MOD)
for i in range(1, N + 1):
if N - i > K: continue
p = N - i
# print(i, p, mf.nCm(p + i - 1, i - 1))
ans += mf.nCm(p + i - 1, i - 1) * mf.nCm(N, i)
ans %= MOD
print(ans)
| 1 | 67,052,883,805,060 | null | 215 | 215 |
N, K = map(int, input().split(' '))
H_ls = list(map(int, input().split(' ')))
H_ls.sort(reverse=True)
print(sum(H_ls[K:])) | from collections import deque
s = deque(input())
q = int(input())
flag = 0
for _ in range(q):
query = list(map(str, input().split()))
if query[0] == "1": flag = 1 - flag
else:
if query[1] == "1":
if flag == 0: s.appendleft(query[2])
else: s.append(query[2])
else:
if flag == 0: s.append(query[2])
else: s.appendleft(query[2])
if flag == 1: s.reverse()
print(*s, sep="") | 0 | null | 67,893,864,642,580 | 227 | 204 |
N = int(input())
alph = ['z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',\
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',\
'x', 'y']
seq = []
while N > 0:
s = N % 26
N = (N - 1) // 26
seq.append(s)
L = len(seq)
name = ''
for i in range(L):
name = alph[seq[i]] + name
print(name) | N=int(input())
A=0
for i in range(1,N+1):
if i%3!=0 and i%5!=0:
A=A+i
print(int(A)) | 0 | null | 23,351,504,979,370 | 121 | 173 |
import math as ma
r=int(input())
print(2*r*ma.pi) | from decimal import Decimal
A,B=input().split()
A=int(A)
B=Decimal(B)
Bint=int(B)
B1=int((B-Bint)*100)
result=A*Bint+A*B1//100
print(result) | 0 | null | 24,024,167,868,458 | 167 | 135 |
from collections import Counter
N = int(input())
X = list(map(int, input().split()))
MOD = 998244353
ctr = Counter(X)
if X[0] == 0 and ctr[0] == 1:
ans = 1
for i in range(1, max(X) + 1):
ans *= pow(ctr[i - 1], ctr[i], MOD)
ans %= MOD
print(ans)
else:
print(0)
| import math
a,b,h,m=map(float,input().split())
#時針の角度をA
c=abs(30*h-11/2*m)
ct=math.radians(c)
cos=math.cos(ct)
ans=a**2+b**2-2*a*b*cos
print(math.sqrt(ans)) | 0 | null | 87,122,453,166,702 | 284 | 144 |
def resolve():
N = int(input())
def prime_factorize(n):
a = []
while n % 2 == 0:
a.append(2)
n //= 2
f = 3
while f * f <= n:
if n % f == 0:
a.append(f)
n //= f
else:
f += 2
if n != 1:
a.append(n)
return a
a = prime_factorize(N)
count_dict = {}
total_key = 0
for i in range(1024):
total_key += i
for j in range(total_key-i, total_key):
count_dict[j] = i-1
a_element_counter = {}
for i in a:
if i not in a_element_counter:
a_element_counter[i] = 0
a_element_counter[i] += 1
ans = 0
for e in a_element_counter.values():
ans += count_dict[e]
print(ans)
if __name__ == "__main__":
resolve() | N = int(input())
def sb(n):
if n == 1:
return {1: 0}
else:
dic = {}
tmp = n
for i in range(2, int(n ** 0.5) + 1):
if tmp % i == 0:
dic[i] = 0
while tmp % i == 0:
dic[i] += 1
tmp //= i
if tmp != 1:
dic[tmp] = 1
if dic == {}:
dic[n] = 1
return dic
ans = 0
for key in sb(N):
v = sb(N)[key]
dv = 1
while v - dv >= 0:
v -= dv
dv += 1
ans += 1
print(ans)
| 1 | 17,085,673,789,730 | null | 136 | 136 |
N = int(input())
a = list(map(int, input().split()))
i = 1
ans = 0
for aa in a:
if aa != i:
ans += 1
else:
i += 1
if N == ans:
print(-1)
else:
print(ans) | n,d = map(int, input().split())
xy = [map(int, input().split()) for _ in range(n)]
x, y = [list(i) for i in zip(*xy)]
ans=0
for i in range(n):
if x[i]**2+y[i]**2<=d**2:
ans+=1
print(ans) | 0 | null | 60,138,825,037,060 | 257 | 96 |
def one_int():
return int(input())
N=one_int()
num=int(N/100)
amari = N % 100
if num*5 < amari:
print(0)
else:
print(1) | x=int(input())
if x<100:
print(0)
exit()
if x>=2000:
print(1)
exit()
else:
t=x//100
a=x%100
if 0<=a<=t*5:
print(1)
else:
print(0) | 1 | 127,115,017,499,010 | null | 266 | 266 |
while True:
deck = input()
if deck == '-':
break
m = int(input())
h = [int(input()) for i in range(m)]
for i in h:
deck = deck[i:] + deck[:i]
print(deck)
| string = []
while 1:
s = input()
if s == "-":
break
try:
n = int(s)
for i in range(n):
m = int(input())
string = string[m:] + string[:m]
output = "".join(string)
print(output)
except ValueError:
string = list(s)
continue | 1 | 1,916,885,409,768 | null | 66 | 66 |
import sys
sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip())) #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split()) #空白あり
def LS2(): return list(sys.stdin.readline().rstrip()) #空白なし
N,S = MI()
A = [0] + LI()
mod = 998244353
dp = [[0]*(S+1) for _ in range(N+1)]
dp[0][0] = 1
for i in range(1,N+1):
for j in range(S+1):
if j >= A[i]:
dp[i][j] = 2*dp[i-1][j] + dp[i-1][j-A[i]]
dp[i][j] %= mod
else:
dp[i][j] = 2*dp[i-1][j]
dp[i][j] %= mod
print(dp[-1][-1])
| k = 2 *10**5
mod = 10**9+7
n, a, b = map(int,input().split())
modinv_table = [-1]*(k+1)
for i in range(1,k+1):
modinv_table[i] = pow(i,-1,mod)
def binomial_coefficients(n,k):
ans = 1
for i in range(k):
ans *= n-i
ans *= modinv_table[i+1]
ans %= mod
return ans
print((pow(2,n,mod)-binomial_coefficients(n,a)-binomial_coefficients(n,b)-1)%mod) | 0 | null | 41,901,213,740,960 | 138 | 214 |
n,k = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))
g = [[0]*(n) for _ in range(n)]
A = [n]*n
# for i in range(n):
# tmp = 0
# idx = i
# cnt = 0
# set_ =set()
# while cnt<n:
# if C[idx] not in set_:
# tmp += C[idx]
# set_.add(C[idx])
# g[i][cnt] = tmp
# idx = P[idx]-1
# cnt += 1
# else:
# p = len(set_)
# A[i] = p
# break
ans = -float('inf')
for i in range(n):
S = []
idx = P[i]-1
S.append(C[idx])
while idx != i:
idx = P[idx]-1
S.append(S[-1] +C[idx])
v,w = k//len(S),k%len(S)
if k<=len(S):
val = max(S[:k])
elif S[-1]<=0:
val = max(S)
else:
val1 = S[-1] *(v-1)
val1 += max(S)
val2 = S[-1]*v
if w!=0:
val2 += max(0,max(S[:w]))
val = max(val1,val2)
ans = max(ans,val)
# for i in range(n):
# v,w = k//A[i],k%A[i]
# if A[i]<k:
# if g[i][A[i]-1]<=0:
# val = max(g[i][:A[i]])
# else:
# val1 = (v-1)*g[i][A[i]-1]
# val1 += max(g[i][:A[i]])
# val2 = v*g[i][A[i]-1]
# if w!=0:
# val2 += max(0,max(g[i][:w]))
# val = max(val1,val2)
# else:
# val = max(g[i][:k])
# ans = max(ans,val)
print(ans) | A,B,N=map(int,input().split())
f=lambda x:(A*x)//B-A*(x//B)
print(f(B-1 if B-1<=N else N)) | 0 | null | 16,708,776,814,000 | 93 | 161 |
a, b, c = map(int, raw_input().split(" "))
cnt = 0
for i in range(a, b + 1) :
if (c % i == 0) :
cnt += 1
print cnt | a,b,h,m = map(int,input().split())
import math
C = abs(360 / 60 * m - 360 / 12 * h - 360 / 12 / 60 * m)
if C > 180:
C = 360 - C
#print(C)
cosC = math.cos(math.radians(C))
#print(cosC)
print(math.sqrt(a ** 2 + b ** 2 - 2 * a * b * cosC)) | 0 | null | 10,240,088,910,622 | 44 | 144 |
n,m=map(int,raw_input().split())
if m ==n:
print "Yes"
else:
print "No" | import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**6)
n,u,v=map(int,input().split())
node=[[]for _ in range(n)]
for _ in range(n-1):
a,b=map(int,input().split())
node[a-1].append(b-1)
node[b-1].append(a-1)
def dfs(i):
visited[i]=1
for x in node[i]:
if visited[x]==0:
dis[x]=dis[i]+1
dfs(x)
inf=10**9
dis=[inf]*n;dis[u-1]=0;visited=[0]*n
dfs(u-1)
dis2=[]
from copy import copy
dis_dash=copy(dis)
dis2.append(dis_dash)
dis[v-1]=0;visited=[0]*n
dfs(v-1)
dis2.append(dis)
cnt=0
for i in range(n):
if dis2[0][i]<dis2[1][i]:
cnt=max(cnt,dis2[1][i])
print(cnt-1) | 0 | null | 100,408,860,691,220 | 231 | 259 |
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
from operator import or_
class SegmentTree:
def __init__(self, orig, func, unit):
_len = len(orig)
self.func = func
self.size = 1 << (_len - 1).bit_length()
self.tree = [unit] * self.size + orig + [unit] * (self.size - _len)
self.unit = unit
for i in range(self.size - 1, 0, -1):
self.tree[i] = func(self.tree[i * 2], self.tree[i * 2 + 1])
def update(self, i, v):
i += self.size
self.tree[i] = v
while i:
i //= 2
self.tree[i] = self.func(self.tree[i * 2], self.tree[i * 2 + 1])
def find(self, l, r):
l += self.size
r += self.size
ret = self.unit
while l < r:
if l & 1:
ret = self.func(ret, self.tree[l])
l += 1
if r & 1:
r -= 1
ret = self.func(ret, self.tree[r])
l //= 2
r //= 2
return ret
n = readline()
s = readline()
q = int(readline())
li = [1 << ord(x) - 97 for x in s[:-1]]
seg = SegmentTree(li, or_, 0)
ans = []
for _ in range(q):
f, x, y = readline().split()
x = int(x)
if f == '1':
seg.update(x - 1, 1 << ord(y) - 97)
else:
y = int(y)
z = seg.find(x - 1, y)
cnt = bin(z).count('1')
ans.append(cnt)
print(*ans)
| s = input()
t = input()
ans = 'No'
if len(s) + 1 == len(t):
for i in range(len(s)):
if s[i] != t[i]:
break
if i == len(s) - 1:
ans = 'Yes'
print(ans)
| 0 | null | 41,837,782,196,720 | 210 | 147 |
import math
A, B, H, M = map(int, input().split(' '))
deg = abs(30 * H + 0.5 * M - 6 * M)
if deg >180:
deg = 360 - deg
deg = math.radians(deg)
print(math.sqrt((B - A * math.cos(deg)) ** 2 + (A * math.sin(deg)) ** 2)) | import numpy as np
a,b,h,m = map(int,input().split())
pos1 = [b*np.sin(np.radians(6*m)),b*np.cos(np.radians(6*m))]
pos2 = [a*np.sin(np.radians(30*h+m*(360/(12*60)))),a*np.cos((np.radians(30*h+m*(360/(12*60)))))]
d = ((pos1[0]-pos2[0])**2+(pos1[1]-pos2[1])**2)**0.5
print(d)
#print(pos1)
#print(pos2) | 1 | 20,167,991,820,198 | null | 144 | 144 |
# coding: utf-8
def main():
A, B = map(int, input().split())
ans = -1
for i in range(10001):
if i * 8 // 100 == A and i // 10 == B:
ans = i
break
print(ans)
if __name__ == "__main__":
main()
| n = int(input())
ans = int(n/2) + n%2
print(ans) | 0 | null | 57,913,280,705,940 | 203 | 206 |
marks=[]
while True:
s=input().split()
marks.append(s)
if ["-1","-1","-1"] in marks:
break
marks=marks[:-1]
for i in marks:
if int(i[0])==-1 or int(i[1])==-1:
print("F")
elif int(i[0])+int(i[1])>=80:
print("A")
elif 65<=int(i[0])+int(i[1])<80:
print("B")
elif 50<=int(i[0])+int(i[1])<65:
print("C")
elif 30<=int(i[0])+int(i[1])<50:
if int(i[2])>=50:
print("C")
else:
print("D")
elif int(i[0])+int(i[1])<30:
print("F")
| num = []
while True:
m,f,r = map(int, raw_input().split())
ans = m + f
if m == f == r == -1:
break
if (m == -1) or (f == -1):
num.append("F")
elif ans >= 80:
num.append("A")
elif (65 <= ans) and (ans < 80):
num.append("B")
elif (50 <= ans) and (ans < 65):
num.append("C")
elif (30 <= ans) and (ans < 50):
if r >= 50:
num.append("C")
else:
num.append("D")
else:
num.append("F")
for i in range(len(num)):
print num[i] | 1 | 1,195,384,848,838 | null | 57 | 57 |
def f(num,N,S):
alphabet = ["a","b","c","d","e","f","g","h","i","j"]
if N == num:
for i in range(len(S)):
print(S[i])
return
else:
tmp = []
length = len(S)
for i in S:
ttmp = sorted(list(i))
for j in range(alphabet.index(ttmp[-1])+2):
tmp.append(i+alphabet[j])
f(num+1,N,tmp)
N = int(input())
f(1,N,["a"]) | h,w,kk=list(map(int,input().split()))
s=[list(map(int,list(input()))) for i in range(h)]
l=[]
for i in range(2**(h-1)):
ll=[0]
for j in range(h-1):
if i & 2**j:
ll.append(ll[-1]+1)
else:
ll.append(ll[-1])
l.append(ll)
mc=w+h
for i in l:
c=[0 for i in range(h)]
cc=0
f=1
#print(i,0)
for j in range(w):
#print(c)
ff=0
for k in range(h):
c[i[k]]+=s[k][j]
for k in range(h):
if c[k]>kk:
ff=1
break
if ff:
cc+=1
c=[0 for i in range(h)]
for k in range(h):
c[i[k]]+=s[k][j]
for k in range(h):
if c[k]>kk:
f=1
if f:
cc=w+h
#print(c)
break
f=0
#print(cc)
if mc>cc+len(set(i))-1:
mc=cc+len(set(i))-1
print(mc) | 0 | null | 50,673,819,246,080 | 198 | 193 |
h, n = map(int, input().split())
A = [int(i) for i in input().split()]
A.sort(reverse=True)
max_point = sum(A[:n])
ans = 'Yes'
if max_point < h:
ans = 'No'
print(ans) | import math
a,b,C=map(int,input().split())
C2=math.radians(C)
sin=math.sin(C2)
cos=math.cos(C2)
S = 0.5*a*b*sin
L = a+b+(math.sqrt((a**2+b**2)-(2*a*b*cos)))
H = (a*b*sin/2)/(a/2)
print("{0:.8f}".format(S))
print("{0:.8f}".format(L))
print("{0:.8f}".format(H)) | 0 | null | 38,874,597,125,092 | 226 | 30 |
a, b, c, d = [int(i) for i in input().split()]
res = float("-inf")
for i in [a,b]:
for j in [c,d]:
res = max(i * j, res)
print(res) | import math
x = int(input())
ans = x
flag = True
while True:
flag = True
for i in range(2, math.floor(math.sqrt(ans))):
if ans % i ==0:
ans += 1
flag = False
break
if flag:
print(ans)
break | 0 | null | 54,285,067,196,712 | 77 | 250 |
N, M = map(int, input().split())
x = list(map(int, input().split()))
#print(N)
#print(M)
#print(x)
def hantei(number_1, list_1):
for a in range(101):
a_plus = number_1 + a
a_minus = number_1 - a
if a_minus not in list_1:
return a_minus
break
elif a_plus not in list_1:
return a_plus
break
print(hantei(N,x))
| a = raw_input()
a = a.split()
for i in range(0,len(a)):
a[i] = int(a[i])
b = sorted(a)
for i in range(0,len(a)):
if(a[i] != b[i] or (i != (len(a) - 1) and a[i] == a[i+1])):
print 'No'
break
if(i == len(a)-1):
print 'Yes'
| 0 | null | 7,309,857,502,090 | 128 | 39 |
h1, m1, h2, m2, k = list(map(int, input().split()))
s = (h1*60)+m1
e = (h2*60)+m2
print(e-s-k) | x = list(map(int, input().split(" ")))
h1 = x[0]
m1 = x[1]
h2 = x[2]
m2 = x[3]
s = x[4]
h = (h2-h1)*60
m = m2-m1
print((h+m)-s) | 1 | 18,027,715,466,840 | null | 139 | 139 |
n,m,q=map(int,input().split())
e=[list(map(int,input().split())) for _ in range(q)]
ans=0
for i in range(1<<(m+n)):
a=[1]
for j in range(m+n):
if (i>>j)&1:a[-1]+=1
else:a+=[a[-1]]
if a[-1]>m:
flag=0
break
if len(a)>n:
flag=1
break
else:flag=1
if flag==0 or len(a)<n:continue
cnt=0
for s,t,u,v in e:
if a[t-1]-a[s-1]==u:cnt+=v
ans=max(ans,cnt)
print(ans) | N, M, Q = map(int, input().split())
a = []
b = []
c = []
d = []
for _ in range(Q):
ad, bd, cd, dd = map(int, input().split())
a.append(ad-1)
b.append(bd-1)
c.append(cd)
d.append(dd)
def score(A):
tmp = 0
for ai, bi, ci, di in zip(a, b, c, d):
if A[bi] - A[ai] == ci:
tmp += di
return tmp
def dfs(A):
if len(A) == N:
return score(A)
res = 0
prev_last = A[-1] if len(A) > 0 else 0
for v in range(prev_last, M):
A.append(v)
res = max(res, dfs(A))
A.pop()
return res
print(dfs([])) | 1 | 27,584,129,183,062 | null | 160 | 160 |
n, k = map(int, input().split())
W = []
for _ in range(n):
w = int(input())
W.append(w)
Baggages = W[:]
def _check(tmp_P):
num_track = 1
tmp_sum = 0
for b in Baggages:
if tmp_sum+b <= tmp_P:
tmp_sum += b
else:
num_track += 1
tmp_sum = b
if num_track > k:
return False
return True
def check(P):
i = 0
for _ in range(k):
s = 0
while s+Baggages[i] <= P:
s += Baggages[i]
i += 1
if i >= n:
return True
if i == n:
return True
else:
return False
def binary_search(max_r):
left = 0
right = max_r
while right-left > 1:
mid = (left+right)//2
if check(mid) == True:
right = mid
else:
left = mid
return right
max_p = sum(Baggages)+1
ans = binary_search(max_p)
print(ans)
#print(check(4))
#print(check(5))
#print(check(6))
#print(check(7))
| a,b=map(int,input().split())
x=str(a)*b
y=str(b)*a
if x<y:
print(x)
else:
print(y) | 0 | null | 42,190,590,107,172 | 24 | 232 |
#!/usr/bin/env python3
class Bit:
# Binary Indexed Tree
def __init__(self, n):
self.size = n
self.tree = [0] * (n + 1)
def __iter__(self):
psum = 0
for i in range(self.size):
csum = self.sum(i + 1)
yield csum - psum
psum = csum
raise StopIteration()
def __str__(self): # O(nlogn)
return str(list(self))
def sum(self, i):
# [0, i) の要素の総和を返す
if not (0 <= i <= self.size): raise ValueError("error!")
s = 0
while i>0:
s += self.tree[i]
i -= i & -i
return s
def add(self, i, x):
if not (0 <= i < self.size): raise ValueError("error!")
i += 1
while i <= self.size:
self.tree[i] += x
i += i & -i
def __getitem__(self, key):
if not (0 <= key < self.size): raise IndexError("error!")
return self.sum(key+1) - self.sum(key)
def __setitem__(self, key, value):
# 足し算と引き算にはaddを使うべき
if not (0 <= key < self.size): raise IndexError("error!")
self.add(key, value - self[key])
(n,), (s,), _, *q = map(str.split, open(0))
*s, = s
BIT = [Bit(int(n)) for _ in [0] * 26]
for e, t in enumerate(s):
BIT[ord(t) - 97].add(e, 1)
for a, b, c in q:
if a == "1":
i = int(b)
BIT[ord(s[i - 1]) - 97].add(i - 1, -1)
s[i - 1] = c
BIT[ord(c) - 97].add(i - 1, 1)
else:
l, r = int(b), int(c)
print(sum(BIT[i].sum(r) - BIT[i].sum(l - 1) > 0 for i in range(26)))
| from bisect import bisect_left, bisect_right, insort_left
n = int(input())
s = list(input())
q = int(input())
d = {}
flag = {}
for i in list('abcdefghijklmnopqrstuvwxyz'):
d.setdefault(i, []).append(-1)
flag.setdefault(i, []).append(-1)
for i in range(n):
d.setdefault(s[i], []).append(i)
for i in range(q):
q1,q2,q3 = map(str,input().split())
if q1 == '1':
q2 = int(q2) - 1
if s[q2] != q3:
insort_left(flag[s[q2]],q2)
insort_left(d[q3],q2)
s[q2] = q3
else:
ans = 0
q2 = int(q2) - 1
q3 = int(q3) - 1
if q2 == q3:
print(1)
continue
for string,l in d.items():
res = 0
if d[string] != [-1]:
left = bisect_left(l,q2)
right = bisect_right(l,q3)
else:
left = 0
right = 0
if string in flag:
left2 = bisect_left(flag[string],q2)
right2 = bisect_right(flag[string],q3)
else:
left2 = 0
right2 = 0
if left != right:
if right - left > right2 - left2:
res = 1
ans += res
#print(string,l,res)
print(ans)
| 1 | 62,869,641,652,560 | null | 210 | 210 |
N,M=map(int,input().split())
H=list(map(int,input().split()))
G={i+1 for i in range(N)}
for _ in range(M):
a,b=map(int,input().split())
if H[a-1]>H[b-1]:
G-={b}
elif H[b-1]>H[a-1]:
G-={a}
else:
G-={a,b}
print(len(G)) | #!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: C_fix_2
# CreatedDate: 2020-08-08 01:59:29 +0900
# LastModified: 2020-08-08 02:11:36 +0900
#
import os
import sys
# import numpy as np
# import pandas as pd
def main():
n, m = map(int, input().split())
h = [0] + list(map(int, input().split()))
path = [[] for _ in range(n+1)]
for _ in range(m):
a, b = map(int, input().split())
path[a].append(h[b])
path[b].append(h[a])
ans = 0
for index in range(1, n+1):
if path[index] and h[index] > max(path[index]):
ans += 1
elif not path[index]:
ans += 1
print(ans)
if __name__ == "__main__":
main()
| 1 | 24,887,243,855,122 | null | 155 | 155 |
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, d=DVSR): return pow(x, d - 2, d)
def DIV(x, y, d=DVSR): return (x * INV(y, d)) % d
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
N=II()
LSTP=[]
LSTN=[]
for i in range(N):
cnt = 0
cntMin = 0
s = input()
for c in s:
if c == "(":
cnt += 1
else:
cnt -= 1
cntMin = min(cntMin, cnt)
# print(cntMin)
if cnt >= 0:
LSTP.append((cntMin, cnt))
else:
LSTN.append((-(cnt-cntMin), -cnt))
LSTP.sort(reverse=True)
LSTN.sort(reverse=True)
res = 0
for cntMin, cnt in LSTP:
if res + cntMin < 0:
print("No")
exit(0)
res += cnt
res2 = 0
for cntMin, cnt in LSTN:
if res2 + cntMin < 0:
print("No")
exit(0)
res2 += cnt
if res - res2 == 0:
print("Yes")
else:
print("No")
| n = int(input())
xy = []
for index in range(n):
x, y = map(int, input().split())
xy.append([x, y])
pos_max = xy[0][0]+xy[0][1]
pos_min = xy[0][0]+xy[0][1]
neg_max = xy[0][0]-xy[0][1]
neg_min = xy[0][0]-xy[0][1]
for item in xy:
pos_v = item[0] + item[1]
neg_v = item[0] - item[1]
pos_max = pos_v if pos_v > pos_max else pos_max
pos_min = pos_v if pos_v < pos_min else pos_min
neg_max = neg_v if neg_v > neg_max else neg_max
neg_min = neg_v if neg_v < neg_min else neg_min
pos_result = pos_max - pos_min
neg_result = neg_max - neg_min
print(pos_result if pos_result >= neg_result else neg_result) | 0 | null | 13,571,853,590,970 | 152 | 80 |
x, N = map(int, input().split())
p = list(map(int, input().split()))
min =10000
temp = 0
num = 10000
for i in range(-1000, 1000):
temp = abs(x- i)
if temp < min and i not in p:
min = temp
num = i
print(num)
| def main():
x, n = map(int, input().split())
arr = set(list(map(int, input().split())))
for d in range(0, 1000):
for m in range(-1, 2, 2):
y = x + m * d
if y not in arr:
print(y)
return
main() | 1 | 14,179,056,890,292 | null | 128 | 128 |
n = int(input())
lis = list(map(int, input().split()))
cnt = 0
a = 1
for i in range(n):
if lis[i] == a:
cnt += 1
a += 1
if cnt == 0:
print(-1)
else:
print(n-cnt) | import math
from math import gcd,pi,sqrt
INF = float("inf")
MOD = 10**9 + 7
import sys
sys.setrecursionlimit(10**6)
import itertools
import bisect
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def main():
n = i_input()
a = i_list()
cnt = 1
for i in a:
if i == cnt:
cnt += 1
if cnt == 1:
print(-1)
else:
print(n - cnt + 1)
if __name__=="__main__":
main()
| 1 | 114,124,375,518,510 | null | 257 | 257 |
def insertion_sort(array, size, gap=1):
""" AOJ??¬??¶?????¬???
http://judge.u-aizu.ac.jp/onlinejudge/commentary.jsp?id=ALDS1_1_A
:param array: ?????????????±??????????
:return: ?????????????????????
"""
global Count
for i in range(gap, len(array)): # i????¬??????????????????????????????????
v = array[i] # ?????¨?????\???????????¨????????????????????????????????´???
j = i - gap
while j >= 0 and array[j] > v:
array[j + gap] = array[j]
j = j - gap
Count += 1
array[j + gap] = v
return array
def shell_sort(array, n):
Count = 0
G = calc_gap(n)
m = len(G)
print(m)
print(' '.join(map(str, G)))
for i in range(0, m):
insertion_sort(array, n, G[i])
def calc_gap(n):
results = [1]
while results[-1] < n:
results.append(3 * results[-1] + 1)
if len(results) > 1:
results = results[:-1]
results.sort(reverse=True)
return results
Count = 0
if __name__ == '__main__':
# ??????????????\???
# data = [5, 1, 4, 3, 2]
data = []
num = int(input())
for i in range(num):
data.append(int(input()))
# ???????????????
shell_sort(data, len(data))
# ???????????¨???
print(Count)
for i in data:
print(i) | import itertools
N = int(input())
P=tuple(int(c) for c in input().split())
Q=tuple(int(c) for c in input().split())
l=list(c for c in itertools.permutations(range(1,N+1),N))
print(abs(l.index(P)-l.index(Q))) | 0 | null | 50,463,552,414,450 | 17 | 246 |
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 itertools
a = []
while True:
n = map(int, raw_input().split())
if n == [0, 0]:
break
a.append(n)
for n in range(len(a)):
x = 0
b = []
for m in range(a[n][0]):
if m == range(a[n][0]): break
b.append(m+1)
c = list(itertools.combinations(b, 3))
for l in range(len(c)):
sum = c[l][0] + c[l][1] + c[l][2]
if sum == a[n][1]: x += 1
print x | 0 | null | 10,973,491,525,454 | 145 | 58 |
k, x = map(int, input().split())
if x <= k * 500: print('Yes')
else: print('No') | import numpy as np
H, W, K = map(int, input().split())
s = np.zeros((H, W), dtype=int)
num = 1
for i in range(H):
_s = input()
for j in range(W):
if _s[j] == '#':
s[i,j] = num
num += 1
else:
s[i,j] = 0
flg = False
for i in range(H):
if all(s[i,:] == 0):
if flg:
s[i,:] = s[i-1,:]
else:
flg = True
prej = 0
for j in range(W):
if s[i, j] != 0:
num = s[i, j]
s[i, prej:j] = num
prej = j+1
else:
s[i, prej:] = num
for i in range(H-1, -1, -1):
if all(s[i,:] == 0):
s[i,:] = s[i+1,:]
for i in range(H):
ans = ''
for j in range(W):
ans += str(s[i, j]) + ' '
print(ans)
| 0 | null | 120,981,370,827,340 | 244 | 277 |
n=int(input())
a="zabcdefghijklmnopqrstuvwxy"
b=""
while n/26>0:
b=a[n%26]+b
n=(n-1)//26
print(b) | H1, M1, H2, M2, K = map(int, input().split())
r = (H2*60+M2)-(H1*60+M1)-K
print(r)
| 0 | null | 14,846,033,050,280 | 121 | 139 |
s=[i for i in input().split()]
k=[]
for i in range(len(s)):
if s[i]=="+":
b=k.pop()
a=k.pop()
k.append(int(a)+int(b))
elif s[i]=="-":
b=k.pop()
a=k.pop()
k.append(int(a)-int(b))
elif s[i]=="*":
b=k.pop()
a=k.pop()
k.append(int(a)*int(b))
else:
k.append(s[i])
print(k[0])
| def readinput():
k,n=map(int,input().split())
a=list(map(int,input().split()))
return k,n,a
def main(k,n,a):
dmax=0
for i in range(n-1):
dmax=max(dmax,a[i+1]-a[i])
dmax=max(dmax,k+a[0]-a[n-1])
return k-dmax
if __name__=='__main__':
k,n,a=readinput()
ans=main(k,n,a)
print(ans)
| 0 | null | 21,789,152,762,528 | 18 | 186 |
c = input()
print(chr(ord(c[0]) + 1)) | alpha = "abcdefghijklmnopqrstuvwxyz"
s = input()
for i in range(len(alpha)):
if alpha[i] == s:
print(alpha[i+1])
exit()
| 1 | 91,788,496,384,132 | null | 239 | 239 |
'''
'''
INF = float('inf')
def main():
H, W, K = map(int, input().split())
S = [input() for _ in range(H)]
S = [''.join(S[row][col] for row in range(H)) for col in range(W)]
ans = INF
for b in range(0, 2**H, 2):
sections = [(0, H)]
for shift in range(1, H):
if (1<<shift) & b:
sections.append((sections.pop()[0], shift))
sections.append((shift, H))
t_ans = len(sections) - 1
count = {(l, r): 0 for l, r in sections}
for col in range(W):
if any(count[(l, r)] + S[col][l:r].count('1') > K for l, r in sections):
for key in count.keys():
count[key] = 0
t_ans += 1
for l, r in sections:
count[(l, r)] += S[col][l:r].count('1')
if max(count.values()) > K:
t_ans = INF
break
ans = min(ans, t_ans)
print(ans)
main()
| # D - Bouquet
n,a,b = map(int,input().split())
MOD = 10**9+7
ans = pow(2,n,MOD)-1
a,b = min(a,b),max(a,b)
tmp = 1
for i in range(1,a+1):
tmp = tmp*(n+1-i)*pow(i,-1,MOD)
tmp %= MOD
ans = (ans-tmp)%MOD
for i in range(a+1,b+1):
tmp = tmp*(n+1-i)*pow(i,-1,MOD)
tmp %= MOD
ans = (ans-tmp)%MOD
print(ans) | 0 | null | 57,523,278,461,878 | 193 | 214 |
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
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)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
a,b,c,d = I()
while a > 0 and c > 0:
c -= b
a -= d
if c <= 0:
print("Yes")
else:
print("No")
| import math
a, b = map(int, input().split())
print(int((a*b)/math.gcd(a, b))) | 0 | null | 71,547,834,065,568 | 164 | 256 |
s,t,a,b,x,y=map(int,open(0).read().split())
if a*s+b*t==x*s+y*t:
print('infinity')
exit()
if a<x:a,b,x,y=x,y,a,b
if a*s+b*t>x*s+y*t:
print(0)
exit()
n=(a*s-x*s)/(x*s+y*t-(a*s+b*t))
m=int(n)
print(m*2+(n>m)) | T1,T2=[int(i) for i in input().split()]
A1,A2=[int(i) for i in input().split()]
B1,B2=[int(i) for i in input().split()]
if((B1-A1)*((B1-A1)*T1+(B2-A2)*T2)>0):
print(0);
exit();
if((B1-A1)*T1+(B2-A2)*T2==0):
print("infinity")
exit();
ans=(abs((B1-A1)*T1)//abs((B1-A1)*T1+(B2-A2)*T2))*2+1
if(abs((B1-A1)*T1)%abs((B1-A1)*T1+(B2-A2)*T2)==0):
ans-=1
print(ans)
| 1 | 131,546,080,326,880 | null | 269 | 269 |
import sys
for i in sys.stdin:
a, b = map(int, i.split())
p, q = max(a, b), min(a, b)
while p % q != 0:
p, q = q, p % q
print("{} {}".format(q, int(a * b / q)))
| import math
while True:
try:
a, b = list(map(int, input().split()))
print('{0} {1}'.format(math.gcd(a, b), int(a * b / math.gcd(a, b))))
except EOFError:
break
| 1 | 545,192,208 | null | 5 | 5 |
import collections
N = int(input())
A = list(map(int,input().split()))
Cn_A = collections.Counter(A)
ans_list = [0]*len(Cn_A)
ans = 0
for i,j in enumerate(Cn_A.values()):
ans+=j*(j-1)//2
for i in A:
j = Cn_A[i]
print(ans-(j*(j-1)//2)+((j-1)*(j-2)//2))
| str = input()
n = int(input())
for i in range(n):
args = input().split()
command = args[0]
s = int(args[1])
e = int(args[2])
if command == 'print':
print(str[s:e + 1])
if command == 'reverse':
str = str[0:s] + str[s:e + 1][::-1] + str[e + 1:]
if command == 'replace':
str = str[0:s] + args[3] + str[e + 1:] | 0 | null | 25,063,810,223,450 | 192 | 68 |
from math import sqrt
from collections import namedtuple
Point = namedtuple('Point', 'x y')
def rot60(p):
return Point(C60 * p.x - S60 * p.y, S60 * p.x + C60 * p.y)
def koch(n, p1, p2):
if n == 0: return
s = Point((2*p1.x + p2.x)/3, (2*p1.y + p2.y)/3)
t = Point((p1.x + 2*p2.x)/3, (p1.y + 2*p2.y)/3)
q = rot60(Point((p2.x - p1.x)/3, (p2.y - p1.y)/3))
u = Point(s.x + q.x, s.y + q.y)
koch(n - 1, p1, s)
print(f.format(s.x, s.y))
koch(n - 1, s, u)
print(f.format(u.x, u.y))
koch(n - 1, u, t)
print(f.format(t.x, t.y))
koch(n - 1, t, p2)
C60 = 1 / 2
S60 = sqrt(3) / 2
f = '{:8f} {:8f}'
n = int(input())
print(f.format(0, 0))
koch(n, Point(0, 0), Point(100, 0))
print(f.format(100, 0)) | import cmath
def f():
global d
p=[]
for i in range(len(d)-1):
a,b=d[i],d[i+1]
r=(b-a)/3
p+=[a,a+r,a+r+r*cmath.rect(1,cmath.pi/3),b-r]
d=p+[d[-1]]
d=[0j,100+0j]
for _ in[0]*int(input()):f()
for e in d:print(e.real,e.imag)
| 1 | 125,097,426,992 | null | 27 | 27 |
s = input()
n = len(s)+1
ls = [0]*n
rs = [0]*n
for i in range(n-1):
if s[i] == "<":
ls[i+1] = ls[i]+1
for i in range(n-1):
if s[-1-i] == ">":
rs[-2-i] = rs[-1-i]+1
ans = 0
for i,j in zip(ls,rs):
ans += max(i,j)
print(ans) | A, B, H, M = map(int, input().split())
import math
H_angle =(M * math.pi) / 30
M_angle = (60 * H + M) * math.pi / 360
H_x, H_y = (A * math.cos(H_angle), A * math.sin(H_angle))
M_x, M_y = (B * math.cos(M_angle), B * math.sin(M_angle))
d = math.sqrt((H_x - M_x) ** 2 + (H_y - M_y) ** 2)
print(d) | 0 | null | 87,853,275,512,860 | 285 | 144 |
import sys
x = sys.stdin.readline()
a, b = x.split(" ")
a = int(a)
b = int(b)
S = a * b
l = 2 * (a + b)
print '%d %d' % (S,l) | # coding: utf-8
a, b = map(int, input().split(' '))
menseki = a * b
print(menseki, a*2 + b*2) | 1 | 304,229,669,572 | null | 36 | 36 |
from decimal import *
getcontext().prec = 1000
a, b, c = map(lambda x: Decimal(x), input().split())
if a.sqrt() + b.sqrt() + Decimal('0.00000000000000000000000000000000000000000000000000000000000000000001') < c.sqrt():
print('Yes')
else:
print('No')
| a, b, c = map(int, input().split())
sahen = 4*a*b
uhen = (c-a-b)**2
if c-a-b < 0:
ans = "No"
elif sahen < uhen:
ans = "Yes"
else:
ans = "No"
print(ans)
| 1 | 51,810,918,293,442 | null | 197 | 197 |
from sys import stdin
def score(mid, fin, re):
if mid == -1 or fin == -1:
return "F"
elif (mid+fin) >= 80:
return "A"
elif (mid+fin) >= 65:
return "B"
elif (mid+fin) >= 50:
return "C"
elif (mid+fin) >= 30:
if re >= 50:
return "C"
else:
return "D"
else:
return "F"
while True:
m, f, r = [int(x) for x in stdin.readline().rstrip().split()]
if m == -1 and f == -1 and r == -1:
break
else:
print(score(m, f, r))
| import math
x1, y1, x2, y2 = map(float, raw_input().split())
x, y = x2 - x1, y2 - y1
print math.sqrt(x**2 + y**2) | 0 | null | 679,586,877,442 | 57 | 29 |
'''
Rjの最小値を保持することで最大利益の更新判定をn回で終わらせる
'''
r = []
n = int(input())
for i in range(n):
i = int(input())
r.append(i)
minv = r[0]
maxv = r[1] - r[0]
for j in range(1,n):
maxv = max(maxv, r[j]-minv)
minv = min(minv, r[j])
print(maxv)
| #-*- coding:utf-8 -*-
def main():
n , data = input_data()
vMax = float('-inf')
vMin = data.pop(0)
for Rj in data:
if vMax < Rj - vMin:
vMax = Rj - vMin
if vMin > Rj:
vMin = Rj
print(vMax)
def input_data():
n = int(input())
lst = [int(input()) for i in range(n)]
return (n , lst)
if __name__ == '__main__':
main() | 1 | 12,451,509,278 | null | 13 | 13 |
import sys
def input(): return sys.stdin.readline().rstrip()
def dfs(s,mxs):
if len(s)==n:
print(s)
else:
for i in range(ord('a'),ord(mxs)+1):
dfs(s+chr(i),chr(i+1) if i ==ord(mxs) else mxs)
def main():
global n
n=int(input())
dfs('','a')
if __name__=='__main__':
main() | c=1
while 1:
n=input()
if n:print'Case %d: %d'%(c,n)
else:break
c+=1 | 0 | null | 26,490,403,756,740 | 198 | 42 |
h,w = list(map(int, input().split()))
board = []
dp = []
INF=10**18
for _ in range(h):
board.append(input())
dp.append([INF]*w)
dxy=[(0,1), (0,-1), (1,0), (-1,0)]
dp[0][0]=1 if board[0][0] == '#' else 0
for y in range(h):
for x in range(w):
dp_list=[dp[y][x]]
if x>0:
dp_left = dp[y][x-1]
if board[y][x-1] != board[y][x] and board[y][x] == '#':
dp_left += 1
dp_list.append(dp_left)
if y>0:
dp_up=dp[y-1][x]
if board[y-1][x] != board[y][x] and board[y][x] == '#':
dp_up += 1
dp_list.append(dp_up)
dp[y][x]=min(dp_list)
print(dp[h-1][w-1]) | H,W = map(int, input().split())
S = [input() for _ in range(H)]
inf = 10**6
L = [[inf]*W for _ in range(H)]
if S[0][0] == ".":
L[0][0] = 0
else:
L[0][0] = 1
for i in range(H):
for j in range(W):
n1 = inf
n2 = inf
if i > 0:
if S[i-1][j] == "." and S[i][j] == "#":
n1 = L[i-1][j] + 1
else:
n1 = L[i-1][j]
if j > 0:
if S[i][j-1] == "." and S[i][j] == "#":
n2 = L[i][j-1] + 1
else:
n2 = L[i][j-1]
L[i][j] = min(L[i][j], n1, n2)
print(L[-1][-1]) | 1 | 49,122,111,856,792 | null | 194 | 194 |
n = int(input())
res = 0
a = n // 500
n %= 500
b = n // 5
print(1000*a + 5*b) | user_input = input()
ascci_value = ord(user_input)
if ascci_value in range(97, 123):
print('a')
else:
print('A') | 0 | null | 27,060,233,097,280 | 185 | 119 |
r,c,k = map(int,input().split())
v = [[0]*(c+1) for i in range(r+1)]
for i in range(k):
x,y,z = map(int,input().split())
v[x][y] = z
dp = [[0]*4 for i in range(r+1)]
for i in range(1,c+1):
for j in range(1,r+1):
if v[j][i]>0:
dp[j][3] = max(dp[j][2]+v[j][i],dp[j][3])
dp[j][2] = max(dp[j][1]+v[j][i],dp[j][2])
dp[j][1] = max(dp[j][0]+v[j][i],dp[j][1],max(dp[j-1])+v[j][i])
dp[j][0] = max(dp[j][0],max(dp[j-1]))
print(max(dp[r])) | R,C,K=map(int,input().split())
point=[[0]*C for _ in range(R)]
for _ in range(K):
r,c,v=map(int,input().split())
point[r-1][c-1]=v
dp=[[[0]*C for _ in range(R)] for _ in range(4)]
dp[1][0][0]=point[0][0]
for i in range(R):
for j in range(C-1):
for k in range(4):
dp[k][i][j+1]=max(dp[k][i][j+1],dp[k][i][j])
if k!=0:
dp[k][i][j+1]=max(dp[k][i][j+1],dp[k-1][i][j]+point[i][j+1])
if i==R-1:
break
for j in range(C):
mx=0
for k in range(4):
mx=max(dp[k][i][j],mx)
dp[0][i+1][j]=mx
dp[1][i+1][j]=mx+point[i+1][j]
ans=0
for i in range(4):
ans=max(ans,dp[i][R-1][C-1])
print(ans) | 1 | 5,557,382,521,148 | null | 94 | 94 |
import sys
from io import StringIO
import unittest
import os
# 実装を行う関数
def resolve():
# 数値取得サンプル
# 1行N項目 x, y = map(int, input().split())
# N行N項目 x = [list(map(int, input().split())) for i in range(n)]
n = int(input())
bricks = list(map(int, input().split()))
# 1が無ければ-1
if 1 not in bricks:
print(-1)
return
num = 1
ban = 0
for brick in bricks:
if brick == num:
num += 1
continue
ban += 1
# dev
1 == 1
print(ban)
# 文字取得サンプル
# 1行1項目 x = input()
pass
# テストクラス
class TestClass(unittest.TestCase):
def assertIO(self, assert_input, output):
stdout, sat_in = sys.stdout, sys.stdin
sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
resolve()
sys.stdout.seek(0)
out = sys.stdout.read()[:-1]
sys.stdout, sys.stdin = stdout, sat_in
self.assertEqual(out, output)
def test_input_1(self):
test_input = """3
2 1 2"""
output = """1"""
self.assertIO(test_input, output)
def test_input_2(self):
test_input = """3
2 2 2"""
output = """-1"""
self.assertIO(test_input, output)
def test_input_3(self):
test_input = """10
3 1 4 1 5 9 2 6 5 3"""
output = """7"""
self.assertIO(test_input, output)
def test_input_4(self):
test_input = """1
1"""
output = """0"""
self.assertIO(test_input, output)
# 自作テストパターンのひな形(利用時は「tes_t」のアンダーバーを削除すること
def tes_t_1original_1(self):
test_input = """データ"""
output = """データ"""
self.assertIO(test_input, output)
# 実装orテストの呼び出し
if __name__ == "__main__":
if os.environ.get("USERNAME") is None:
# AtCoder提出時の場合
resolve()
else:
# 自PCの場合
unittest.main()
| s=list(input())
t=list(input())
t.pop(-1)
if s==t:
print('Yes')
else:
print('No') | 0 | null | 67,853,845,204,768 | 257 | 147 |
import math
def dis(x, y):
return math.sqrt(x * x + y * y)
N, D = map(int, input().split())
result = 0
for i in range(N):
x, y = map(int, input().split())
if dis(x, y) <= D:
result += 1
print(result)
| import time
start=time.time()
k=int(input())
ans=0
x=k
for i in range(10**9):
while x%10!=7:
x+=k
if time.time()-start>=1.9:exit(print(-1))
x//=10
if x==0:exit(print(i+1))
| 0 | null | 6,001,474,230,388 | 96 | 97 |
a, b, k = map(int, input().split())
print(a - min(a, k), b - min(b, k - min(a, k))) | a, b, k = map(int, input().split())
if a >= k:
c = a-k
e = "{0} {1}".format(c, b)
print(e)
elif a < k <= a + b:
c = b + a - k
e = "{0} {1}".format(0, c)
print(e)
else:
print('0' +' ' + '0')
| 1 | 104,472,612,067,122 | null | 249 | 249 |
i = 1
while True:
n = input()
if n != 0:
print 'Case %s: %s' % (str(i), str(n))
i = i + 1
else:
break | while(1):
try:
i = raw_input()
a, b = map(int, i.split())
x, y = a, b
while(b != 0):
tmp = b
b = a % b
a = tmp
print a,
print x*y/a
except:
break | 0 | null | 235,221,890,004 | 42 | 5 |
N=int(input())
A = list(map(int,input().split()))
val = 0
for i in range(N):
val = val ^ A[i]
ans = []
for i in range(N):
s = val ^ A[i]
ans.append(s)
print(*ans) | a = int(input())
res = 0
for i in range(a+1):
if i % 3 ==0 or i % 5 ==0:
pass
else:
res += i
print(res) | 0 | null | 23,518,873,288,280 | 123 | 173 |
S = input()
s_rev = S[::-1]
r_list = [0] * 2019
r_list[0] = 1
num, d = 0, 1
for i in range(len(S)):
num += d*int(s_rev[i])
num %= 2019
r_list[num] += 1
d *= 10
d %= 2019
ans = 0
for i in range(2019):
ans += r_list[i]*(r_list[i]-1)//2
print(ans)
| import numpy as np
n = int(input())
mod = 10**9 + 7
a = np.array(list(map(int, input().split())))
ans = 0
for i in range(len(bin(max(a)))):
num_1 = np.count_nonzero((a>>i)&1)
num_0 = n - num_1
ans += (2**i)*(num_1*num_0) % mod
ans %= mod
print(ans) | 0 | null | 76,816,525,526,180 | 166 | 263 |
n = int(input())
lis = list(map(int, input().split()))
cnt = 0
a = 1
for i in range(n):
if lis[i] == a:
cnt += 1
a += 1
if cnt == 0:
print(-1)
else:
print(n-cnt) | import math
import fractions
#import sys
#input = sys.stdin.readline
def make_divisors(n):
divisors = []
for i in range(1, int(n**0.5)+1):
if n % i == 0:
divisors.append(i)
if i != n // i:
divisors.append(n//i)
# divisors.sort()
return divisors
def ValueToBits(x,digit):
res = [0 for i in range(digit)]
now = x
for i in range(digit):
res[i]=now%2
now = now >> 1
return res
def BitsToValue(arr):
n = len(arr)
ans = 0
for i in range(n):
ans+= arr[i] * 2**i
return ans
def ZipArray(a):
aa = [[a[i],i]for i in range(n)]
aa.sort(key = lambda x : x[0])
for i in range(n):
aa[i][0]=i+1
aa.sort(key = lambda x : x[1])
b=[aa[i][0] for i in range(len(a))]
return b
def ValueToArray10(x, digit):
ans = [0 for i in range(digit)]
now = x
for i in range(digit):
ans[digit-i-1] = now%10
now = now //10
return ans
def Zeros(a,b):
if(b<=-1):
return [0 for i in range(a)]
else:
return [[0 for i in range(b)] for i in range(a)]
class Bit:
def __init__(self, n):
self.size = n
self.tree = [0] * (n + 1)
def sum(self, i):
s = 0
while i > 0:
s += self.tree[i]
i -= i & -i
return s
def add(self, i, x):
while i <= self.size:
self.tree[i] += x
i += i & -i
'''
def cmb(n, r, p):
if (r < 0) or (n < r):
return 0
r = min(r, n - r)
return fact[n] * factinv[r] * factinv[n-r] % p
p = 2
N = 10 ** 6 + 2
fact = [1, 1] # fact[n] = (n! mod p)
factinv = [1, 1] # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1] # factinv 計算用
for i in range(2, N + 1):
fact.append((fact[-1] * i) % p)
inv.append((-inv[p % i] * (p // i)) % p)
factinv.append((factinv[-1] * inv[-1]) % p)
'''
#a = list(map(int, input().split()))
#################################################
#################################################
#################################################
#################################################
n = int(input())
lr = []
for i in range(n):
s = input()
m = len(s)
#exam left
count = 0
left = 0
for j in range(m):
if(s[j]=='('):
count += 1
else:
if(count == 0):
left+=1
else:
count -=1
#exam right
count = 0
right = 0
for j in range(m):
if(s[m-j-1]==')'):
count += 1
else:
if(count == 0):
right+=1
else:
count -=1
#print(left,right)
lr.append([left,right])
existL = 0
existR = 0
sumL = 0
sumR = 0
for i in range(n):
sumL += lr[i][0]
sumR += lr[i][1]
if(lr[i][0]==0 and lr[i][1]!=0):
existL=1
#print(lr[i])
if(lr[i][1]==0 and lr[i][0]!=0):
existR=1
#print(lr[i])
lr2 = []
for i in range(n):
if(lr[i][0]==0): lr2.append(lr[i])
lr3 = []
for i in range(n):
if(lr[i][0]*lr[i][1]!=0): lr3.append(lr[i])
lr3.sort(key = lambda x : x[0]-x[1])
for i in lr3:
lr2.append(i)
for i in range(n):
if(lr[i][1]==0): lr2.append(lr[i])
#print(lr2)
ok = 1
now = 0
for i in range(n):
now -= lr2[i][0]
if(now<0):
ok = 0
#print('a',lr2[i])
now += lr2[i][1]
if(existL*existR == 1 and sumL == sumR and ok==1):
print("Yes")
else:
if(sumL==0 and sumR==0):
print("Yes")
else:
print("No")
| 0 | null | 68,855,816,718,150 | 257 | 152 |
n = int(input())
ans = 0
for a in range(1,n):
b = n//a
if b*a == n:
b -= 1
ans += b
print(ans) | import sys
input = sys.stdin.buffer.readline
N = int(input())
ans = 0
for a in range(1, N+1):
maxb = (N-1)//a
ans += maxb
print(ans) | 1 | 2,599,709,287,708 | null | 73 | 73 |
#C
n,k,s = map(int,input().split())
ans = [1 for _ in range(n)]
for i in range(k):
ans[i] = s
for j in range(n-k):
if s != 1:
ans[k+j] = s//2 + 1
else:
ans[k+j] = s//2 + 2
print(*ans) | N, K, S = map(int, input().split())
if S == 1000000000:
fill = 1000000000 - 1
else:
fill = S+1
print(' '.join(map(str, ([S]*K + [fill]*(N-K)))))
| 1 | 90,681,609,445,220 | null | 238 | 238 |
x = "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"
r = x.split(", ")
z = int(input())
print(r[z-1]) | BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001
while True:
N,X = map(int,input().split())
if N == 0 and X == 0:
break
ans = 0
for a in range(1,N+1):
for b in range(1,N+1):
if b <= a:
continue
c = X-(a+b)
if c > b and c <= N:
ans += 1
print("%d"%(ans))
| 0 | null | 25,504,607,698,438 | 195 | 58 |
n, k = map(int, input().split())
W = [int(input()) for i in range(n)]
left = max(W)-1; right = sum(W)
while left+1 < right:
mid = (left + right) // 2
cnt = 1; cur = 0
for w in W:
if mid < cur + w:
cur = w
cnt += 1
else:
cur += w
if cnt <= k:
right = mid
else:
left = mid
print(right) |
def check(P,k,staffList):
i = 0
for _ in range(k):
staffNum = 0
while(staffNum + staffList[i]<= P):
staffNum += staffList[i]
i += 1
if i == n:
return n
return i
def getMaxP(n,k,staffList):
# Pが増えれば,入れられる個数は単純増加するので,二部探索によりPの最小値を求められる
left = 0
right = 100000*10000
while right-left>1:
mid = (left+right)//2
staffNum = check(mid,k,staffList)
if staffNum >= n :
right = mid
else:
left = mid
return right
if __name__ == "__main__":
n,k = map(int,input().split())
staffList = [int(input()) for _ in range(n)]
# 特定の最大積載量でどれだけ積めるのかを計算する
P = getMaxP(n,k,staffList)
print(P)
| 1 | 90,160,360,540 | null | 24 | 24 |
W,H,x,y,r=map(int,input().split())
print('Yes'if(False if x<r or W-x<r else False if y<r or H-y<r else True)else'No')
| from decimal import *
import math
a, b, c = list(map(int, input().split()))
print('Yes' if c - a - b> 0 and 4*a*b < (c - a - b)**2 else 'No') | 0 | null | 25,919,744,537,152 | 41 | 197 |
select = list(range(1, 4))
a = int(input())
b = int(input())
ans = next(s for s in select if s != a and s != b)
print(ans) | def main_1_11_A():
values = list(map(int, input().split()))
query = input()
dice = Dice(values)
for d in query:
dice.roll(d)
def main_1_11_B():
values = list(map(int, input().split()))
n = int(input())
for _ in range(n):
t_value, s_value = map(int, input().split())
print(_B(values, t_value, s_value))
def _B(values, t_value, s_value):
"""
????????¢?????????????????¨??????
??´??¢???????????¢?????????????????§???????????????????????¨?????????
??????????????????????????¢???????¢??????????
"""
def rot_n(dice, n):
for _ in range(n):
dice.roll("N").roll("E").roll("S")
return dice
def make_dices():
# ?????¢???TOP?????????
return [
Dice(values),
Dice(values).roll("N"),
Dice(values).roll("N").roll("N"),
Dice(values).roll("N").roll("N").roll("N"),
Dice(values).roll("E"),
Dice(values).roll("W"),
]
dices = (
make_dices() +
list(map(lambda dice: rot_n(dice, 1), make_dices())) +
list(map(lambda dice: rot_n(dice, 2), make_dices())) +
list(map(lambda dice: rot_n(dice, 3), make_dices()))
)
for dice in dices:
# print(dice)
for d in ("E" * 4 + "W" * 4 + "S" * 4 + "N" * 4):
dice.roll(d)
#print(d, (dice.top_value, dice.south_value), (t_value, s_value))
if (dice.top_value, dice.south_value) == (t_value, s_value):
return dice.east_value
class Dice:
def __init__(self, values):
self.values = values
# top, bottom, direction 4
self.t = 1
self.b = 6
self.w = 4
self.e = 3
self.n = 5
self.s = 2
def __repr__(self):
labels = {
"t": self.t,
"b": self.b,
"w": self.w,
"e": self.e,
"n": self.n,
"s": self.s,
}
return "<%s (%s, %s)>" % (self.__class__, self.values, labels)
def roll(self, direction):
if direction == "E":
after_lables = self.t, self.b, self.e, self.w
self.e, self.w, self.b, self.t = after_lables
elif direction == "W":
after_lables = self.t, self.b, self.e, self.w
self.w, self.e, self.t, self.b = after_lables
elif direction == "S":
after_lables = self.t, self.b, self.n, self.s
self.s, self.n, self.t, self.b = after_lables
elif direction == "N":
after_lables = self.t, self.b, self.n, self.s
self.n, self.s, self.b, self.t = after_lables
return self
@property
def top_value(self):
return self.values[self.t - 1]
@property
def south_value(self):
return self.values[self.s - 1]
@property
def east_value(self):
return self.values[self.e - 1]
if __name__ == "__main__":
# main_1_11_A()
main_1_11_B() | 0 | null | 55,401,463,793,970 | 254 | 34 |
name=input()
len_n=len(name)
name2=input()
len_n2=len(name2)
if name==name2[:-1] and len_n+1==len_n2:
print("Yes")
else:
print("No") | A , B, M = map(int,input().split(" "))
a = list(map(int,input().split(" ")))
b = list(map(int,input().split(" ")))
res = min(a) + min(b)
for _ in range(M):
x,y, c = list(map(int,input().split(" ")))
x-=1
y-=1
res = min(res,a[x]+b[y]-c)
print(res)
| 0 | null | 37,938,788,150,172 | 147 | 200 |
#coding:utf-8
H,W=map(int,input().split())
while not(H==0 and W==0):
for i in range(0,H):
for j in range(0,W):
if (i+j)%2==0:
print("#",end="")
else:
print(".",end="")
print("")
print("")
H,W=map(int,input().split()) | from itertools import product
n,m,x = map(int, input().split())
al = []
cl = []
for _ in range(n):
row = list(map(int, input().split()))
cl.append(row[0])
al.append(row[1:])
ans = 10**9
bit = 2
ite = list(product(range(bit),repeat=n))
for pattern in ite:
skills = [0]*m
cost = 0
for i, v in enumerate(pattern):
if v == 1:
curr_al = al[i]
cost += cl[i]
for j, a in enumerate(curr_al):
skills[j] += a
if min(skills) >= x:
ans = min(ans,cost)
if ans == 10**9: ans = -1
print(ans) | 0 | null | 11,507,898,399,360 | 51 | 149 |
# ??¨????????°r??¨?????°c???r ?? c ???????´????????????¨?????????????????§???????????¨??????????¨?????????\????????°????????¨???????????????????????°??????
r, c = map(int, input().split())
spreadSheet = [[0 for i in range(c + 1)] for j in range(r + 1)]
# ??¨??\????????????
for i in range(0, r):
spreadSheet[i][:c] = map(int, input().split())
# print(spreadSheet)
# ?????????????¨??????????
for i in range(0, r):
for j in range(0, c):
spreadSheet[i][c] += spreadSheet[i][j]
# ?????????????¨??????????
for i in range(0, c + 1):
for j in range(0, r):
spreadSheet[r][i] += spreadSheet[j][i]
# print(spreadSheet)
for i in range(0, r + 1):
for j in range(0, c + 1):
print("{0}".format(spreadSheet[i][j]), end="")
if j != c:
print(" ",end="")
else:
print("") | r,c = map(int,input().split())
matrix = [list(map(int,input().split())) for i in range(r)]
for i in range(r):
matrix[i].append(sum(matrix[i]))
row = []
for i in range (c+1):
row_sum = 0
for j in range(r):
row_sum += matrix[j][i]
row.append(row_sum)
matrix.append(row)
for i in range(r+1):
for j in range(c+1):
print(matrix[i][j],end="")
if j != c:
print(" ",end="")
if j == c :
print("") | 1 | 1,360,055,365,478 | null | 59 | 59 |
while 1:
(a,b,c) = map(int,raw_input().split())
if a == -1 and b == -1 and c == -1:
break
if a == -1 or b == -1:
print 'F'
elif a+b >= 80:
print 'A'
elif a+b >= 65:
print 'B'
elif a+b >= 50:
print 'C'
elif a+b >= 30:
if c >= 50:
print 'C'
else:
print 'D'
else:
print 'F' |
import sys
input = sys.stdin.readline
def inps():
return str(input())
s = inps().rsplit()[0]
t = inps().rsplit()[0]
cnt = 0
for i in range(len(s)):
if s[i]!=t[i]:
cnt+=1
print(cnt)
| 0 | null | 5,834,683,877,330 | 57 | 116 |
n = int(input())
s = [list(input()) for i in range(n)]
r = []
l = []
ans = 0
for i in s:
now = 0
c = 0
for j in i:
if j =="(":
now += 1
else:
now -= 1
c = min(c,now)
if c == 0:
ans += now
elif now >= 0:
l.append([c,now])
else:
r.append([c-now,now])
l.sort(reverse =True)
r.sort()
for i,j in l:
if ans + i < 0:
print("No")
exit()
else:
ans += j
for i,j in r:
if ans +i+j < 0:
print("No")
exit()
else:
ans += j
if ans == 0:
print("Yes")
else:
print("No")
| # -*- coding: utf-8 -*-
N = int(input().strip())
S_list = [input().strip() for _ in range(N)]
#-----
def count_brackets(brackets):
L = R = 0
L_rev = R_rev = 0
max_R = -float("inf")
max_L = -float("inf")
for i in range(len(brackets)):
if brackets[i] == "(":
L += 1
else: # brackets[i] == ")"
R += 1
max_R = max(max_R, R - L)
if brackets[len(brackets)-1-i] == "(":
L_rev += 1
max_L = max(max_L, L_rev - R_rev)
else: # brackets[len(brackets)-1-i] == ")"
R_rev += 1
if max_L < 0: max_L = 0
if max_R < 0: max_R = 0
return max_L, max_R
tmp_cnt1 = [] # the number of "(" >= the number of ")"
tmp_cnt2 = [] # the number of "(" < the number of ")"
for S in S_list:
cnt_L,cnt_R = count_brackets(S)
if cnt_L == cnt_R == 0:
continue
if cnt_L >= cnt_R:
tmp_cnt1.append( (cnt_L,cnt_R) )
else:
tmp_cnt2.append( (cnt_L,cnt_R) )
tmp_cnt1.sort(key=lambda x: x[1])
tmp_cnt2.sort(key=lambda x: -x[0])
sum_bracket = 0
flag = True
for cnt_L,cnt_R in (tmp_cnt1 + tmp_cnt2):
sum_bracket -= cnt_R
if sum_bracket < 0: flag = False
sum_bracket += cnt_L
if sum_bracket < 0: flag = False
if flag == False:
break
if (sum_bracket == 0) and flag:
print("Yes")
else:
print("No")
| 1 | 23,809,403,059,328 | null | 152 | 152 |
X = int(input())
if 360 % X == 0:
print(360 // X)
exit()
else:
mod = 0
cnt = 0
while (360 + mod) % X != 0:
cnt += (360 + mod) // X
mod = (360 + mod) % X
cnt += (360 + mod) // X
print(cnt)
| def gcd(a,b):
# assume a,b are +ve integers.
if a>b:
a,b = b,a
if b%a==0:
return a
return gcd(a,b%a)
print(360//(gcd(360,int(input())))) | 1 | 13,109,075,019,908 | null | 125 | 125 |
n=int(input())
inp=list(map(int,input().split()))
from collections import Counter
z=0
k=1
for i in range(len(inp)):
if inp[i]==k:
z+=1
k+=1
if z==0:
print (-1)
else:
print (len(inp)-z)
| n = int(input())
a = list(map(int, input().split()))
r, cnt = 1, 0
for i in a:
if r == i:
r += 1
else:
cnt += 1
print(cnt) if a.count(1) else print(-1)
| 1 | 114,712,712,273,340 | null | 257 | 257 |
from math import gcd
mod = 1000000007
n = int(input())
counter = {}
zeros = 0
for i in range(n):
a, b = map(int, input().split())
if (a == 0 and b == 0):
zeros += 1
continue
g = gcd(a, b)
a //= g
b //= g
if (b < 0):
a = -a
b = -b
if (b == 0 and a < 0):
a = -a
counter[(a, b)] = counter.get((a, b), 0)+1
counted = set()
ans = 1
for s, s_count in counter.items():
if s not in counted:
if s[0] > 0 and s[1] >= 0:
t = (-s[1], s[0])
else:
t = (s[1], -s[0])
t_count = counter.get(t, 0)
now = pow(2, s_count, mod)-1
now += pow(2, t_count, mod)-1
now += 1
ans *= now
ans %= mod
counted.add(s)
counted.add(t)
print((ans - 1 + zeros) % mod)
| 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") | 0 | null | 20,055,976,297,280 | 146 | 142 |
n,x, m = map(int, input().split())
mod = [None for _ in range(m)]
count = x
loop = []
rem = [x]
for i in range(1, n):
value = (x * x) % m
rem.append(value)
x = (x * x) % m
if mod[value] != None:
# print(mod)
s_index = mod[value]
loop = rem[s_index : -1]
num = (n - s_index) // len(loop)
amari = (n - s_index) % len(loop)
if amari == 0:
print(sum(rem[:s_index]) + sum(loop) * num)
else:
print(sum(rem[:s_index]) + sum(loop) * num + sum(loop[:amari]))
exit()
else:
mod[value] = i
count += value
print(count)
| N, X, M = map(int, input().split())
visited = [False] * M
tmp = X
total_sum = X
visited[tmp] = True
total_len = 1
while True:
tmp = (tmp ** 2) % M
if visited[tmp]:
loop_start_num = tmp
break
total_sum += tmp
visited[tmp] = True
total_len += 1
path_len = 0
tmp = X
path_sum = 0
while True:
if tmp == loop_start_num:
break
path_len += 1
path_sum += tmp
tmp = (tmp ** 2) % M
loop_len = total_len - path_len
loop_sum = total_sum - path_sum
result = 0
if N <= path_len:
tmp = X
for i in range(N):
result += X
tmp = (tmp ** 2) % M
print(result)
else:
result = path_sum
N -= path_len
loops_count = N // loop_len
loop_rem = N % loop_len
result += loop_sum * loops_count
tmp = loop_start_num
for i in range(loop_rem):
result += tmp
tmp = (tmp ** 2) % M
print(result)
# print("loop_start_num", loop_start_num)
# print("path_sum", path_sum)
# print("loop_sum", loop_sum)
| 1 | 2,807,988,968,010 | null | 75 | 75 |
A, B = map(int, input().split())
if A <= 2 * B:
print('0')
else:
print(A - 2 * B) | n, m = map(int, input().split())
if 2*m>=n:
print(0)
else:
print(n-2*m) | 1 | 166,769,714,579,792 | null | 291 | 291 |
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())), reverse=True)
a_rev = a[::-1]
ok, ng = 1, 200001
while ng-ok > 1:
x = (ok+ng)//2
num = 0
cur = 0
for i in range(n-1, -1, -1):
while cur < n and a[i] + a[cur] >= x:
cur += 1
num += cur
if num < m:
ng = x
else:
ok = x
just = ok
ans = 0
larger_cnt = 0
cur = 0
a_cum = [0]
for i in range(n):
a_cum.append(a_cum[-1] + a[i])
for i in range(n):
while cur < n and a[i] + a_rev[cur] <= just:
cur += 1
larger_cnt += n - cur
ans += (n - cur) * a[i] + a_cum[n - cur]
ans += just * (m - larger_cnt)
print(ans) | import sys
from itertools import accumulate
from bisect import bisect_left
n, m, *a = map(int, sys.stdin.read().split())
a.sort()
def f(x):
# 幸福度がx未満となる組み合わせの個数を数える
cnt = 0
for ai in a:
cnt += bisect_left(a, x-ai)
# 幸福度がx以上の組み合わせの個数がm未満かどうか
return n * n - cnt < m
left = a[0] * 2
right = a[-1] * 2
while right - left > 1:
mid = (right + left) // 2
if f(mid):
right = mid
else:
left = mid
ans = 0
cumsum = [0] + list(accumulate(a))
j = 0
for ai in a:
i = bisect_left(a, left-ai)
j += n - i
ans += ai * (n - i) + cumsum[-1] - cumsum[i]
ans += (m - j) * left
print(ans)
| 1 | 107,870,422,908,710 | null | 252 | 252 |
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
if (a1*t1-b1*t1)*(a1*t1+a2*t2-b1*t1-b2*t2)>0:print(0)
elif (a1*t1-b1*t1)*(a1*t1+a2*t2-b1*t1-b2*t2)==0:print('infinity')
else:print(-(a1*t1-b1*t1)//(a1*t1+a2*t2-b1*t1-b2*t2)*2+((a1*t1-b1*t1)%(a1*t1+a2*t2-b1*t1-b2*t2)!=0)) | def main():
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
if t1*a1+t2*a2==t1*b1+t2*b2:
return 'infinity'
if a1<b1:
a1,b1=b1,a1
a2,b2=b2,a2
if a2>b2:
return 0
d1=(a1-b1)*t1
d2=(b2-a2)*t2
if d1>d2:
return 0
d=d2-d1
if d1%d==0:
return d1//d*2
return d1//d*2+1
if __name__=='__main__':
print(main()) | 1 | 131,296,709,597,362 | null | 269 | 269 |
st = []
for c in input().split():
if c in "+-*":
v2 = st.pop()
v1 = st.pop()
st.append(str(eval(v1 + c + v2)))
else:
st.append(c)
print(st[0]) | s = list(input())
n = len(s)
flag = 0
for i in range(n//2):
if s[i] != s[n-i-1]:
flag = 1
if flag == 0:
s2 = s[:(n-1)//2]
n2 = len(s2)
for i in range(n2//2):
if s2[i] != s2[n2-i-1]:
flag = 1
#print(n, n2, s, s2)
if flag == 0:
if flag == 0:
s2 = s[(n+3)//2-1:]
n2 = len(s2)
for i in range(n2//2):
if s2[i] != s2[n2-i-1]:
flag = 1
#print(s2)
if flag == 0:
print("Yes")
else:
print("No") | 0 | null | 23,077,586,766,080 | 18 | 190 |
N=int(input())
a,b,c,d=0,1e10,-1e10,1e10
for i in range(N):
A,B=input().split()
A=int(A)
B=int(B)
a=max(a,A+B)
b=min(b,A+B)
c=max(c,A-B)
d=min(d,A-B)
print(max(abs(a-b),abs(c-d))) | base = input().split()
print(base[1],end="")
print(base[0]) | 0 | null | 53,427,881,756,790 | 80 | 248 |
import bisect
import sys,io
import math
from collections import deque
from heapq import heappush, heappop
input = sys.stdin.buffer.readline
def sRaw():
return input().rstrip("\r")
def iRaw():
return int(input())
def ssRaw():
return input().split()
def isRaw():
return list(map(int, ssRaw()))
INF = 1 << 29
DIV = (10**9) + 7
#DIV = 998244353
def mod_inv_prime(a, mod=DIV):
return pow(a, mod-2, mod)
def mod_inv(a, b):
r = a
w = b
u = 1
v = 0
while w != 0:
t = r//w
r -= t*w
r, w = w, r
u -= t*v
u, v = v, u
return (u % b+b) % b
def CONV_TBL(max, mod=DIV):
fac, finv, inv = [0]*max, [0]*max, [0]*max
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
inv[1] = 1
for i in range(2, max):
fac[i] = fac[i-1]*i % mod
inv[i] = mod - inv[mod % i] * (mod//i) % mod
finv[i] = finv[i-1]*inv[i] % mod
class CONV:
def __init__(self):
self.fac = fac
self.finv = finv
pass
def ncr(self, n, k):
if(n < k):
return 0
if(n < 0 or k < 0):
return 0
return fac[n]*(finv[k]*finv[n-k] % DIV) % DIV
return CONV()
def cumsum(As):
s = 0
for a in As:
s += a
yield s
sys.setrecursionlimit(200005)
def dijkstra(G,start=0):
heap = []
cost = [INF]*len(G)
heappush(heap,(0,start))
while len(heap)!=0:
c,n = heappop(heap)
if cost[n] !=INF:
continue
cost[n]=c
for e in G[n]:
ec,v = e
if cost[v]!=INF:
continue
heappush(heap,(ec+c,v))
return cost
def main():
R,C,K = isRaw()
mas = [[0 for _ in range(C)]for _ in range(R)]
for k in range(K):
r,c,v = isRaw()
mas[r-1][c-1]=v
dp = [[[0 for _ in range(C)] for _ in range(R)] for _ in range(3)]
dp0 = dp[0]
dp1 = dp[1]
dp2 = dp[2]
dp0[0][0]=mas[0][0]
dp1[0][0]=mas[0][0]
dp2[0][0]=mas[0][0]
for r in range(R):
for c in range(C):
mrc = mas[r][c]
dp0[r][c] = mrc
dp1[r][c] = mrc
dp2[r][c] = mrc
if r>0:
dp0[r][c] = max(dp0[r][c], dp2[r-1][c]+mrc)
dp1[r][c] = max(dp1[r][c], dp2[r-1][c]+mrc)
dp2[r][c] = max(dp2[r][c], dp2[r-1][c]+mrc)
if c>0:
dp0[r][c] = max(dp0[r][c], dp0[r][c-1], mrc)
dp1[r][c] = max(dp1[r][c], dp1[r][c-1], dp0[r][c-1]+mrc)
dp2[r][c] = max(dp2[r][c], dp2[r][c-1], dp1[r][c-1]+mrc)
print(max([dp[ni][-1][-1] for ni in range(3)]))
main()
| import math
a, b, h, m = map(int, input().split())
t = math.pi * (60 * h - 11 * m) / 360
l = math.sqrt(a ** 2 + b ** 2 - 2 * a * b * math.cos(t))
print(l) | 0 | null | 12,875,957,187,158 | 94 | 144 |
a = list(map(int,input().split()))
b = list(map(int,input().split()))
if a[0]<= sum(b):
print("Yes")
else:
print("No") | x=list(map(int, input().split()))
x.sort()
print(x[0],x[1],x[2]) | 0 | null | 39,120,490,035,198 | 226 | 40 |
n = int(input())
s = input()
[print(chr((ord(s[i])+n-65)%26+65),end="") for i in range(len(s))]
| N,X,M = map(int,input().split())
i = X%M
L = [i]
S = set([i])
i = i**2 % M
while not i in S:
L.append(i)
S.add(i)
i = i**2 % M
index = L.index(i)
k = (N-index)//len(L[index:])
l = (N-index)%len(L[index:])
# これでうまくいくのたまたまだよ。本当はN<indexの場合分けがひつよう。なんで場合分けするよ
if N<index:
ans = sum(L[:N])
else:
ans = sum(L[:index]) + (k*sum(L[index:])) + sum(L[index:index+l])
print(ans) | 0 | null | 68,710,023,844,420 | 271 | 75 |
A,B,C = map(int,input().split())
print(("bust","win")[A+B+C <22]) | import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')
def main():
H, W, K = in_nn()
a = [list(in_s()) for _ in range(H)]
ans = [[0 for _ in range(W)] for _ in range(H)]
stb = []
for y in range(H):
for x in range(W):
if a[y][x] == '#':
stb.append((x, y))
for i, (x, y) in enumerate(stb):
ans[y][x] = i + 1
for i, (x, y) in enumerate(stb):
l = x
while l != 0:
if ans[y][l - 1] == 0:
l -= 1
else:
break
r = x
while r != W - 1:
if ans[y][r + 1] == 0:
r += 1
else:
break
u = y
while u != 0:
if sum(ans[u - 1][l:r + 1]) == 0:
u -= 1
else:
break
d = y
while d != H - 1:
if sum(ans[d + 1][l:r + 1]) == 0:
d += 1
else:
break
for yy in range(u, d + 1):
for xx in range(l, r + 1):
ans[yy][xx] = i + 1
for i in range(H):
print(' '.join(map(str, ans[i])))
if __name__ == '__main__':
main()
| 0 | null | 130,670,829,924,600 | 260 | 277 |
r = int(input())
print(int(r*r)) | N=int(input())
S=input()
count=0
for i in range(N):
if S[i]=='A' and i!=N-1:
if S[i+1]=='B' and i!=N-2:
if S[i+2]=='C':
count+=1
print(count) | 0 | null | 122,663,516,655,176 | 278 | 245 |
import sys
sys.setrecursionlimit(300000)
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI0(): return map(lambda s: int(s) - 1, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
def LMI0(): return list(map(lambda s: int(s) - 1, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')
A, V = MI()
B, W = MI()
T = I()
d = max(0, V - W)
if abs(A - B) - d * T <= 0:
print('YES')
else:
print('NO') | a,v=map(int,input().split())
b,w=map(int,input().split())
t=int(input())
if v<w:
print("NO")
elif v==w:
if a==b:
print("YES")
else:
print("NO")
elif v>w:
t0 = abs((a-b)/(v-w))
if t0<=t:
print("YES")
else:
print("NO")
| 1 | 15,122,630,116,320 | null | 131 | 131 |
def ABC_150_A():
K,X = map(int, input().split())
if 500*K>=X:
print('Yes')
else:
print('No')
if __name__ == '__main__':
ABC_150_A() | n = int(input())
a = list(map(int, input().split()))
print(sum(sorted(a * 2)[::-1][1:n])) | 0 | null | 53,542,502,959,008 | 244 | 111 |
N,K=map(int,input().split())
A=[int(x)-1 for x in input().split()]
seen={0}
town=0
while K>0:
town=A[town]
K-=1
if town in seen:
break
seen.add(town)
start=town
i=0
while K>0:
town=A[town]
i+=1
K-=1
if town is start:
break
K=K%i if i>0 else 0
while K>0:
town=A[town]
i+=1
K-=1
print(town+1)
| N, K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N):
A[i] -= 1
judge = N*[0]
pos = 0
freq = 0
while judge[pos]==0 and K > 0:
judge[pos] = 1
pos = A[pos]
K -= 1
freq+=1
tmp = pos
pos = 0
while pos!=tmp:
pos = A[pos]
freq -= 1
#print(freq)
if K==0:
print(pos+1)
else:
K %= freq
pos = tmp
for i in range(K):
pos = A[pos]
print(pos+1)
| 1 | 22,697,823,577,510 | null | 150 | 150 |
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() | N = int(input())
S = input()
count = 0
for p in range(10):
s = S.find(str(p))
if s == -1:
continue
for q in range(10):
t = S.find(str(q), s + 1)
if t == -1:
continue
for r in range(10):
u = S.find(str(r), t + 1)
if u != -1:
count += 1
print(count) | 0 | null | 88,969,689,767,280 | 193 | 267 |
a= input()
c=""
for i in range(0, len(a)):
if a[i]=="?":
c=c+"D"
else:
c=c+a[i]
print(c) | from sys import stdin
T = stdin.readline().rstrip()
print(T.replace('?', 'D')) | 1 | 18,586,998,101,630 | null | 140 | 140 |
N = int(input())
XY = []
for _ in range(N):
A = int(input())
xy = []
for _ in range(A):
x, y = map(int, input().split())
x -= 1
xy.append((x, y))
XY.append(xy)
ans = 0
for s in range(1<<N):
for i in range(N):
if s>>i&1:
for x, y in XY[i]:
if s>>x&1 != y:
break
else:
continue
break
else:
ans = max(ans, bin(s).count("1"))
print(ans)
| K, N = map(int, input().split())
A = [int(x) for x in input().split()]
mx = A[0] + K - A[-1]
for i in range(len(A) - 1):
dis = A[i + 1] - A[i]
if dis > mx:
mx = dis
print(K - mx)
| 0 | null | 82,285,550,782,750 | 262 | 186 |
N=int(input())
for num in range(1,10):
if (N/num).is_integer() and 1<=(N/num)<=9:
print("Yes")
break
else:
print("No") | N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
if N >= 3 :
for i in range(0, N - 1) :
for j in range(1, N - 1 - i) :
for k in range(2, N - i):
if L[i] < L[i+j] < L[i+k] and L[i] + L[i+j] > L[i+k]:
ans += 1
print(ans) | 0 | null | 82,266,212,882,688 | 287 | 91 |
N=int(input())
L= list(map(int,input().split()))
L_sorted=sorted(L,reverse=False)#昇順
count=0
import bisect
for i in range(N):
for j in range(i+1,N):
a=L_sorted[i]
b=L_sorted[j]
bisect.bisect_left(L_sorted,a+b)
count+=bisect.bisect_left(L_sorted,a+b)-j-1
print(count) | i=0
while 1:
x=input()
if x==0:break
i+=1;print"Case "+str(i)+": "+str(x) | 0 | null | 85,915,950,870,332 | 294 | 42 |
#シェルソート
def insertionSort(A, n, g):
global cnt
for i in range(g, n):
#その間隔で比較できる最も左から始める.vが基準値
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
n = int(input())
A = [int(input()) for i in range(n)]
cnt = 0
G = [1]
for i in range(100):
if n < 1 + 3*G[-1]:
break
G.append(1+3*G[-1])
m = len(G)
G.reverse()
for i in range(m):
insertionSort(A, n, G[i])
print(m)
print(*G)
print(cnt)
print(*A, sep='\n')
| import math
ans = 0
num = int(input())
for i in range(1, num+1):
for j in range(1, num+1):
for k in range(1, num+1):
ans += math.gcd(math.gcd(i, j),k)
print(ans) | 0 | null | 17,880,547,814,592 | 17 | 174 |
import math
if __name__ == '__main__':
x1, y1, x2, y2 = [float(i) for i in input().split()]
d = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
print("{0:.5f}".format(d)) | n = int(input())
a = [int(x) for x in input().split()]
a.sort()
ans = 1
for i in a:
ans *= i
if ans > 10 ** 18:
print(-1)
break
else:
print(ans) | 0 | null | 8,166,607,242,420 | 29 | 134 |
from collections import deque
n,q = tuple([int(i) for i in input().split()])
lines = [input().split() for i in range(n)]
pro = deque([[i[0], int(i[1])] for i in lines])
time = 0
while True:
left = pro.popleft()
if left[1] - q > 0:
time += q
pro.append([left[0], left[1] - q])
else:
time += left[1]
print(' '.join([left[0], str(time)]))
if len(pro) == 0:
break; | N, K = map(int, input().split())
kMod = 10**9+7
ans = 0
for k in range(K, N+2):
lb = (k-1) * k // 2
ub = (N+1-k + N) * k // 2
ans += (ub-lb+1)
ans %= kMod
print(ans) | 0 | null | 16,615,046,902,940 | 19 | 170 |
s = list(input())
t = list(input())
if s == t:
print(0)
else:
changes = 0
for i in range(0, len(s)):
if s[i] != t[i]:
changes += 1
print(changes) | S = input()
list_S = list(S)
T = input()
list_T = list(T)
count = 0
for i in range(len(list_S)):
if S[i] != T[i]:
count +=1
print(count) | 1 | 10,496,135,161,472 | null | 116 | 116 |
x = input()
s = 0
for i in range(len(x)):
s = s + int(x[i])
if s % 9 == 0:
print("Yes")
else:
print("No") | a,b,c,d = map(int,input().split())
val = [None]*4
val = [a*c,a*d,b*c,b*d]
top = a*c
for i in range(1,4):
if top < val[i]:
top = val[i]
print(top) | 0 | null | 3,724,869,402,212 | 87 | 77 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.