code1
stringlengths 17
427k
| code2
stringlengths 17
427k
| similar
int64 0
1
| pair_id
int64 2
181,637B
⌀ | question_pair_id
float64 3.7M
180,677B
⌀ | code1_group
int64 1
299
| code2_group
int64 1
299
|
---|---|---|---|---|---|---|
def modinv(a,m):
return pow(a,m-2,m)
n,k = map(int,input().split())
P = 10**9+7
ans = 1
nCl = 1
n1Cl = 1
for l in range(1,min(k+1,n)):
nCl = nCl*(n+1-l)*modinv(l,P)%P
n1Cl = n1Cl*(n-l)*modinv(l,P)%P
ans = (ans+nCl*n1Cl)%P
print(ans)
| class Combination:
"""
O(n)の前計算を1回行うことで,O(1)でnCr mod mを求められる
n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
使用例:
comb = Combination(1000000)
print(comb(5, 3)) # 10
"""
def __init__(self, n_max, mod=10**9+7):
self.mod = mod
self.modinv = self.make_modinv_list(n_max)
self.fac, self.facinv = self.make_factorial_list(n_max)
def __call__(self, n, r):
if n < r:
return 0
return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod
def make_factorial_list(self, n):
# 階乗のリストと階乗のmod逆元のリストを返す O(n)
# self.make_modinv_list()が先に実行されている必要がある
fac = [1]
facinv = [1]
for i in range(1, n+1):
fac.append(fac[i-1] * i % self.mod)
facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
return fac, facinv
def make_modinv_list(self, n):
# 0からnまでのmod逆元のリストを返す O(n)
modinv = [0] * (n+1)
modinv[1] = 1
for i in range(2, n+1):
modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
return modinv
def main():
n, k = map(int,input().split())
MOD = 10 ** 9 + 7
comb = Combination(10 ** 6)
ans = 0
if n - 1 <= k:
ans = comb(n + n - 1, n)
else:
ans = 1
for i in range(1,k+1):
ans += comb(n,i) * comb(n - 1, i)
ans %= MOD
print(ans)
if __name__ == "__main__":
main() | 1 | 66,862,958,687,162 | null | 215 | 215 |
import sys
readline = sys.stdin.readline
def main():
N, X, M = map(int, readline().split())
P = N.bit_length()
pos = [[0] * M for _ in range(P)]
value = [[0] * M for _ in range(P)]
for r in range(M):
pos[0][r] = r * r % M
value[0][r] = r
for p in range(P - 1):
for r in range(M):
pos[p + 1][r] = pos[p][pos[p][r]]
value[p + 1][r] = value[p][r] + value[p][pos[p][r]]
ans = 0
cur = X
for p in range(P):
if N & (1 << p):
ans += value[p][cur]
cur = pos[p][cur]
print(ans)
return
if __name__ == '__main__':
main()
| import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
n, m = map(int, input().split())
if n % 2 == 0:
n -= 1
ans = []
left_range = n//2
right_range = n//2 - 1
left1 = 1
right1 = 1 + left_range
left2 = right1 + 1
right2 = left2 + right_range
while True:
if left1 >= right1:
break
ans.append([left1, right1])
left1 += 1
right1 -= 1
while True:
if left2 >= right2:
break
ans.append([left2, right2])
left2 += 1
right2 -= 1
for i in range(m):
print(*ans[i])
| 0 | null | 15,763,337,822,492 | 75 | 162 |
N = list(input())
if '7' in N:
print('Yes')
else:
print('No') | n = int(input())
s = 100000
for i in range(n):
s = int((s*1.05)/1000+0.9999)*1000
print(s)
| 0 | null | 17,236,004,880,224 | 172 | 6 |
a = int(input())
x = a
while x%360:
x += a
print(x//a) | num = 100000
n = int(input())
for i in range(n):
num*=1.05
if num % 1000 >= 1 :
a = num % 1000
num = int(num+1000-a)
else:
num = int(num)
print(num)
| 0 | null | 6,544,047,411,858 | 125 | 6 |
import math
N,K = map(int,input().split())
A = list(map(int,input().split()))
m = 1
M = max(A)
while(M-m>0):
mm = 0
mid = (M+m)//2
for i in range(N):
mm += math.ceil(A[i]/mid)-1
if mm>K:
m = mid+1
else:
M = mid
print(math.ceil(m))
|
def main():
s, t = input().split(' ')
print(t, end='')
print(s)
if __name__ == '__main__':
main()
| 0 | null | 54,809,407,533,060 | 99 | 248 |
h = int(input())
w = int(input())
n = int(input())
r = max(h,w)
count = 0
x = 0
while x < n:
count += 1
x += r
print(count) | H = int(input())
W = int(input())
N = int(input())
a = max(H, W)
print(N // a + min(1, N % a))
| 1 | 88,607,279,849,968 | null | 236 | 236 |
A, V = [int(x) for x in input().split()]
B, W = [int(x) for x in input().split()]
T = int(input())
if W >= V:
print('NO')
elif abs(A - B) <= (V - W) * T:
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 abs(A - B) / (V - W) <= T:
print("YES")
else:
print("NO") | 1 | 15,078,737,669,810 | null | 131 | 131 |
import bisect
def main():
N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for a_idx in range(N-2):
for b_idx in range(a_idx+1, N-1):
l_idx = b_idx + 1
r_idx = bisect.bisect_left(L, L[a_idx] + L[b_idx])
ans += r_idx - l_idx
print(ans)
if __name__ == '__main__':
main()
| import bisect
import sys
sys.setrecursionlimit(10**9)
def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))
INF=10**20
def main():
N=ii()
L=list(mi())
ans = 0
L.sort()
for ai in range(N):
for bi in range(ai+1,N):
a,b = L[ai],L[bi]
left = bisect.bisect_left(L,abs(a-b)+1)
right = bisect.bisect_right(L,a+b-1)
count = right - left
if left <= ai <= right:
count -= 1
if left <= bi <= right:
count -= 1
ans += count
# if count > 0:
# print(a,b,count)
print(ans//3)
if __name__ == "__main__":
main() | 1 | 171,095,039,823,750 | null | 294 | 294 |
S = input()
ans = "Yes"
if S == "AAA" or S == "BBB":
ans = "No"
print(ans) | print('Yes' if(len(set(input())) != 1) else 'No')
| 1 | 54,806,103,982,698 | null | 201 | 201 |
K=int(input())
S=input()
l=[]
if len(S)<=K:
print(S)
else:
for i in range(K):
l.append(S[i])
print("".join(l)+'...') | # S の長さが K 以下であれば、S をそのまま出力してください。
# S の長さが K を上回っているならば、
# 先頭 K 文字だけを切り出し、末尾に ... を付加して出力してください。
# K は 1 以上 100 以下の整数
# S は英小文字からなる文字列
# S の長さは 1 以上 100 以下
K = int(input())
S = str(input())
if K >= len(S):
print(S)
else:
print((S[0:K] + '...'))
| 1 | 19,647,745,613,562 | null | 143 | 143 |
import math
def solve():
N = int(input())
if math.floor(math.ceil(N / 1.08) * 1.08) == N:
print(math.ceil(N / 1.08))
else:
print(':(')
if __name__ == "__main__":
solve() | S=str(input())
def kinshi(S:str):
len_count=len(S)
return("x"*len_count)
print(kinshi(S))
| 0 | null | 99,277,604,474,910 | 265 | 221 |
n=int(input())
a=[int(x) for x in input().rstrip().split()]
now=1
mod=10**9+7
def lcm(a,b):#最小公倍数
ori_a=a
ori_b=b
while b!=0:
a,b=b,a%b
return (ori_a*ori_b)//a
for i in a:
now=lcm(i,now)
# print(now)
print(sum([now//i for i in a])%mod)
| from collections import defaultdict
N = int(input())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7
def prime_factorize(n):
arr = []
temp = n
for i in range(2, int(-(-n**0.5//1))+1):
if temp%i==0:
cnt=0
while temp%i==0:
cnt+=1
temp //= i
arr.append([i, cnt])
if temp!=1:
arr.append([temp, 1])
if arr==[]:
arr.append([n, 1])
return arr
primes = defaultdict(int)
for a in A:
for x, p in prime_factorize(a):
primes[x] = max(primes[x], p)
lcm = 1
for prime, ind in primes.items():
lcm *= prime ** ind
ans = 0
for i in range(N):
ans += lcm // A[i]
if i == N // 2:
ans %= mod
print(ans % mod)
| 1 | 87,723,808,058,412 | null | 235 | 235 |
n = int(input())
R = []
for i in range(n):
R.append(int(input()))
# R???i???????????§???????°???????i?????????????´???¨???????????????list
mins = [R[0]]
for i in range(1, n):
mins.append(min(R[i], mins[i-1]))
# ???????????????j????????????????????\??????????°??????¨??????????±?????????????????????§???????±????????????????
mx = - 10 ** 12
for j in range(1, n):
mx = max(mx, R[j] - mins[j-1])
print(mx) | a=input().split()
operator={
'+':(lambda x,y:x+y),
'-':(lambda x,y:x-y),
'*':(lambda x,y:x*y),
'/':(lambda x,y:float(x)/y)
}
stack=[]
for i,j in enumerate(a):
if j not in operator.keys():
stack.append(int(j))
continue
y=stack.pop()
x=stack.pop()
stack.append(operator[j](x,y))
print(stack[0])
| 0 | null | 24,626,307,490 | 13 | 18 |
import math
x = int(input())
# -100 ~ 100 ^ 5
pow5 = {}
for i in range(-1000, 1001):
pow5[i] = int(math.pow(i, 5))
for i in range(-1000, 1001):
a = x + pow5[i]
for j in range(-1000, 1001):
if a == pow5[j]:
print(j, i)
exit()
| x = int(input())
for i in range(1000):
for j in range(i):
if i ** 5 - j ** 5 == x:
print(i, j)
exit()
if i ** 5 + j ** 5 == x:
print(i, -j)
exit()
| 1 | 25,557,174,204,420 | null | 156 | 156 |
r, c = map(int, input().split())
hyou = []
tate_sum = [0] * (c + 1)
for i in range(r):
line = list(map(int, input().split()))
line.append(sum(line))
tate_sum = [tate_sum[j] + line[j] for j in range(len(line))]
hyou.append(" ".join(map(str, line)))
hyou.append(" ".join(map(str, tate_sum)))
print("\n".join(hyou)) | r, c = map(int, input().split())
sumOfCols = [0] * c
for i in range(r):
cols = list(map(int, input().split()))
s = sum(cols)
sumOfCols = [p + q for p,q in zip(sumOfCols, cols)]
print(' '.join(map(str, cols)), end=' ')
print(s)
s = sum(sumOfCols)
print(' '.join(map(str, sumOfCols)), end=' ')
print(s) | 1 | 1,355,739,161,760 | null | 59 | 59 |
import sys
import numpy as np
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
# 二分探索
N, K = lr()
A = np.array(lr())
F = np.array(lr())
A.sort()
F = np.sort(F)[::-1]
def check(x):
count = np.maximum(0, (A - (x // F))).sum()
return count <= K
left = 10 ** 12 # 可能
right = -1 # 不可能
while left > right + 1:
mid = (left+right) // 2
if check(mid):
left = mid
else:
right = mid
print(left)
# 51 | N = int(input())
A = list(map(int, input().split()))
bosses = [0 for _ in range(N+1)]
for boss in A:
bosses[boss] += 1
for i in range(1, N+1):
print(bosses[i]) | 0 | null | 98,825,675,120,868 | 290 | 169 |
import sys
sys.setrecursionlimit(10**8)
n = int(input())
x = list(input())
bit_cnt = x.count('1')
# bitを一つ増やす場合
pos = [0] * n
pos_total = 0
# bitを一つ減らす場合
neg = [0] * n
neg_total = 0
base = 1
for i in range(n):
pos[-i-1] = base%(bit_cnt+1)
if x[-i-1] == '1':
# 同時にmod(bit_cnt+1)の法でトータルを作成
pos_total = (pos_total + base) % (bit_cnt+1)
base = (base*2) % (bit_cnt+1)
base = 1
if bit_cnt == 1:
# mod取れない
pass
else:
for i in range(n):
neg[-i-1] = base%(bit_cnt-1)
if x[-i-1] == '1':
# 同時にmod(bit_cnt-1)の法でトータルを作成
neg_total = (neg_total + base) % (bit_cnt-1)
base = (base*2) % (bit_cnt-1)
def popcount(n):
s = list(bin(n)[2:])
return s.count('1')
memo = {}
memo[0] = 0
def dfs(n, depth=0):
if n in memo:
return depth + memo[n]
else:
ret = dfs(n%popcount(n), depth+1)
# memo[n] = ret
return ret
ans = []
for i in range(n):
if x[i] == '0':
st = (pos_total + pos[i]) % (bit_cnt+1)
print(dfs(st, depth=1))
elif bit_cnt == 1:
# コーナーケース:すべてのbitが0となる
print(0)
else:
st = (neg_total - neg[i]) % (bit_cnt-1)
print(dfs(st, depth=1))
| n = int(input())
s = input()
x = [int(i) for i in s]
pc = s.count('1')
def popcnt(x):
x = x - ((x >> 1) & 0x5555555555555555)
x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
x = x + (x >> 8)
x = x + (x >> 16)
x = x + (x >> 32)
return x & 0x0000007f
def f(x):
if x == 0:
return 0
return f(x % popcnt(x)) + 1
ans = [0]*n
for a in range(2):
npc = pc
if a == 0:
npc += 1
else:
npc -= 1
if npc <= 0:
continue
r0 = 0
for i in range(n):
r0 = (r0*2) % npc
r0 += x[i]
k = 1
for i in range(n-1, -1, -1):
if x[i] == a:
r = r0
if a == 0:
r = (r+k) % npc
else:
r = (r-k+npc) % npc
ans[i] = f(r) + 1
k = (k*2) % npc
for i in ans:
print(i) | 1 | 8,228,153,135,960 | null | 107 | 107 |
n = int(input())
S = [int(i) for i in input().split(' ')]
q = int(input())
T = [int(i) for i in input().split(' ')]
result = 0
for t in T:
for s in S:
if t == s:
result += 1
break
print(result)
| def check3(m):
m = str(m)
if '3' in m:
return True
else:
return False
n = int(input())
i = 1
while i <= n:
x = i
if x%3 == 0 or x%10 == 3:
print(" %d" % i, end = '')
i += 1
else:
isThree = check3(x)
if isThree:
print(" %d" % i, end = '')
i += 1
print("") | 0 | null | 512,499,243,692 | 22 | 52 |
import math
r = float(input())
S = math.pi * r * r
L = 2 * math.pi * r
print("{0:.10f}".format(S),end=" ")
print("{0:.10f}".format(L)) | h,n = map(int,input().split())
a = map(int,input().split())
if sum(a) >= h:
print("Yes")
else:
print("No") | 0 | null | 39,216,247,486,138 | 46 | 226 |
s=input()
res=''
for i in s:
if i.islower():
i=i.upper()
elif i.isupper():
i=i.lower()
res+=i
print(res)
| str1=input()
str2=str1.swapcase()
print(str2)
| 1 | 1,479,979,767,420 | null | 61 | 61 |
import sys
for i, x in enumerate(iter(sys.stdin.readline, '0\n'), 1):
print(f'Case {i}: {x[:-1]}')
| case_num = 1
while True:
tmp_num = int(input())
if tmp_num == 0:
break
else:
print("Case %d: %d" % (case_num,tmp_num))
case_num += 1
| 1 | 486,671,841,372 | null | 42 | 42 |
import numpy as np
H,W,M = map(int,input().split())
#h = np.empty(M,dtype=np.int)
#w = np.empty(M,dtype=np.int)
bomb = set()
hh = np.zeros(H+1,dtype=np.int)
ww = np.zeros(W+1,dtype=np.int)
for i in range(M):
h,w = map(int,input().split())
bomb.add((h,w))
hh[h] += 1
ww[w] += 1
#print(bomb)
h_max = np.max(hh)
w_max = np.max(ww)
h_max_ids = list()
w_max_ids = list()
for i in range(1,H+1):
if hh[i] == h_max:
h_max_ids.append(i)
for j in range(1,W+1):
if ww[j] == w_max:
w_max_ids.append(j)
for i in h_max_ids:
for j in w_max_ids:
if not (i,j) in bomb:
print(h_max + w_max)
exit()
#print("hmax:{} wmax:{}".format(h_max_id, w_max_id))
#print("hmax:{} wmax:{}".format(hh[h_max_id], ww[w_max_id]))
print(h_max + w_max - 1)
| import sys
h,w,m = map(int,input().split())
h_lst = [[0,i] for i in range(h)]
w_lst = [[0,i] for i in range(w)]
memo = []
for i in range(m):
x,y = map(int,input().split())
h_lst[x-1][0] += 1
w_lst[y-1][0] += 1
memo.append((x-1,y-1))
h_lst.sort(reverse = True)
w_lst.sort(reverse = True)
Max_h = h_lst[0][0]
Max_w = w_lst[0][0]
h_ans = [h_lst[0][1]]
w_ans = [w_lst[0][1]]
if h != 1:
s = 1
while s < h and h_lst[s][0] == Max_h:
h_ans.append(h_lst[s][1])
s+= 1
if w!= 1:
t=1
while t < w and w_lst[t][0] == Max_w:
w_ans.append(w_lst[t][1])
t += 1
memo = set(memo)
#探索するリストは、最大を取るものの集合でなければ計算量はO(m)にはならない!
for j in h_ans:
for k in w_ans:
if (j,k) not in memo:
print(Max_h+Max_w)
sys.exit()
print((Max_h+Max_w)-1)
| 1 | 4,684,648,565,200 | null | 89 | 89 |
n = int(input())
s = input()
x = n // 2
print(["Yes", "No"][n % 2 != 0 or s[:x] != s[x:]]) | n = int(input())
lis = list(map(int,input().split()))
sum = 0
for i in range(n-1):
if int(lis[i])>int(lis[i+1]):
sum += lis[i]-lis[i+1]
lis[i+1] = lis[i]
else:
continue
print(sum) | 0 | null | 75,841,332,587,020 | 279 | 88 |
import sys
n = int(input())
ls = [list(map(int,input().split())) for _ in range(n)]
for i in range(n-2):
if ls[i][0] == ls[i][1]:
if ls[i+1][0] == ls[i+1][1]:
if ls[i+2][0] == ls[i+2][1]:
print("Yes")
sys.exit()
print("No") | N = int(input())
A = list(map(int, input().split()))
t = 0
for c in A:
t ^= c
ans = []
for b in A:
ans.append(t^b)
print(*ans) | 0 | null | 7,540,343,046,628 | 72 | 123 |
from collections import defaultdict
import sys
N = int(input())
A = list(map(int, input().split()))
C = defaultdict(int)
for a in A:
C[a] += 1
if C[a] >= 2:
print('NO')
sys.exit()
print('YES') | a, b = map(int, input().split())
if a <= 2 * b:
x = 0
else:
x = a - 2 * b
print(int(x)) | 0 | null | 120,178,610,518,120 | 222 | 291 |
s = list(str(input()))
q = int(input())
flag = 1
from collections import deque
s = deque(s)
for i in range(q):
query = list(map(str, input().split()))
if len(query) == 1:
flag = 1-flag
else:
t, f, c = query
if f == '1':
if flag:
s.appendleft(c)
else:
s.append(c)
else:
if flag:
s.append(c)
else:
s.appendleft(c)
s = list(s)
if not flag:
s.reverse()
print(''.join(s))
| S, T = input().split()
A, B = map(int, input().split())
U = input()
if (U == T):
print(str(A)+" "+str(B-1))
else:
print(str(A-1)+" "+str(B))
| 0 | null | 64,191,256,258,140 | 204 | 220 |
import copy
n = int(raw_input())
cards = raw_input().split()
cards1 = copy.deepcopy(cards)
cards2 = copy.deepcopy(cards)
def isbig(card1, card2):
if int(card1[1]) > int(card2[1]):
return 2
elif int(card1[1]) == int(card2[1]):
return 1
else:
return 0
def BubbleSort(c, n):
temp = 0
for i in range(n):
for j in reversed(range(i+1,n)):
if isbig(c[j], c[j-1]) == 0:
temp = c[j]
c[j] = c[j-1]
c[j-1] = temp
return c
def SelectionSort(c, n):
temp = 0
for i in range(n):
minj = i
for j in range(i,n):
if isbig(c[j], c[minj]) == 0:
minj = j
temp = c[i]
c[i] = c[minj]
c[minj] = temp
#print("*{:}".format(c))
return c
def isstable(unsort, sort):
for i in range(n):
for j in range(i+1,n):
for k in range(n):
for l in range(k+1,n):
if isbig(unsort[i], unsort[j]) == 1 and unsort[i] == sort[l] and unsort[j] == sort[k]:
return 0
return 1
for i in range(n):
if i != n-1:
print BubbleSort(cards1, n)[i],
else:
print BubbleSort(cards1, n)[i]
if isstable(cards, BubbleSort(cards1, n)) == 1:
print("Stable")
else:
print("Not stable")
for i in range(n):
if i != n-1:
print SelectionSort(cards2, n)[i],
else:
print SelectionSort(cards2, n)[i]
if isstable(cards, SelectionSort(cards2, n)) == 1:
print("Stable")
else:
print("Not stable") | import math
while True:
a_2 = 0
n = int(input())
if n == 0:
break
score = [int(x) for x in input().split()]
m = sum(score)/len(score)
#print(m)
for i in range(len(score)):
a_2 += (score[i]-m)*(score[i]-m)/n
#print(a_2)
print('%.4f'% math.sqrt(a_2)) | 0 | null | 109,529,490,752 | 16 | 31 |
from collections import *
import copy
N,K=map(int,input().split())
A=list(map(int,input().split()))
lst=[0]
for i in range(0,N):
lst.append((A[i]%K+lst[i])%K)
for i in range(len(lst)):
lst[i]-=i
lst[i]%=K
dic={}
count=0
for i in range(0,len(lst)):
if lst[i] in dic:
count+=dic[lst[i]]
dic[lst[i]]+=1
else:
dic.update({lst[i]:1})
a=i-K+1
if a>=0:
dic[lst[a]]-=1
print(count)
| from collections import defaultdict
N, K = map(int, input().split())
A = [(int(c)%K)-1 for c in input().split()]
B = [0]
for c in A:
B += [B[-1]+c]
dic = defaultdict(int)
ldic = defaultdict(int)
for i in range(min(K,N+1)):
c = B[i]
dic[c%K] += 1
ans = 0
#print(dic)
for i in range(1,N+1):
x = B[i-1]
ldic[x%K] += 1
ans += dic[x%K]-ldic[x%K]
if K+i-1<=N:
dic[B[K+i-1]%K] += 1
"""
print('###############')
print(i,x, ans)
print(dic)
print(ldic)
"""
print(ans)
| 1 | 136,976,470,340,580 | null | 273 | 273 |
x, y = list(map(int, input().split()))
if y > x * 4 or y < x * 2 or y % 2 == 1:
print('No')
else:
print('Yes') | H,W=map(int,input().split())
w1=0--W//2
w2=W//2
if H==1 or W==1:
print(1)
else:
print(w1*(0--H//2)+w2*(H//2)) | 0 | null | 32,124,840,526,020 | 127 | 196 |
import math
X,K,D=map(int,input().split())
count=0
#Xが初期値
#Dが移動量(+or-)
#K回移動後の最小の座標
#X/D>Kの場合、K*D+Xが答え
Y = 0
#X>0 D>0 の場合
if X / D > K and X / D > 0:
Y = abs(X - D * K)
elif abs(X / D) > K and X / D < 0:
Y = abs(X + D * K)
else:
#X/D<Kの場合、X mod D
L = abs(X) % D
M = math.floor(abs(X) / D)
if (K - M) % 2 == 0:
Y = L
else:
Y=abs(L-D)
print(Y) | N, X, T = map(int, input().split())
if N % X == 0:
print(int(N / X) * T)
else:
print(int(N / X + 1) * T) | 0 | null | 4,716,687,076,668 | 92 | 86 |
input()
*l,=map(int,input().split())
print((pow(sum(l),2)-sum([pow(x,2) for x in l]))//2) | N = int(input())
*d, = map(int, input().split())
sum = 0
for i in range(len(d)):
for j in range(i+1, len(d)):
sum+=d[i]*d[j]
print(sum) | 1 | 167,704,525,076,062 | null | 292 | 292 |
class UnionFind:
def __init__(self, n):
self.par = [i for i in range(n+1)]
self.rank = [0]*(n+1)
def find(self, x):
if self.par[x] == x:
return x
else:
self.par[x] = self.find(self.par[x])
return self.par[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if self.rank[x] < self.rank[y]:
self.par[x] = y
else:
self.par[y] = x
if self.rank[x] == self.rank[y]:
self.rank[x] += 1
def same_check(self, x, y):
return self.find(x) == self.find(y)
N, M = map(int, input().split())
A = [0]*M
B = [0]*M
uf = UnionFind(N)
for i in range(M):
A[i], B[i] = map(int, input().split())
uf.union(A[i], B[i])
cnt = 0 #新しく作った橋の数
for j in range(1,N):
if uf.same_check(j, j+1) == False:
uf.union(j,j+1)
cnt+=1
print(cnt)
#print(uf.par[1:])
#print(uf.rank[1:]) | n,m=map(int,input().split())
a=list(map(int,input().split()))
ans=0
for i in range(m) :
ans = ans+a[i]
day=n-ans
#if day>=0 :
# print(day)
#else :
# print(-1)
print(day if day >= 0 else "-1") | 0 | null | 16,991,592,798,960 | 70 | 168 |
n = int(input())
a = list(map(int, input().split()))
result = [0] * n
for i in range(len(a)):
result[a[i] - 1] += 1
for i in range(len(result)):
print(result[i]) | n = int(input())
alist = list(map(int,input().split()))
syain = [0]*n
for i in range(n-1):
syain[alist[i]-1] += 1
for i in range(n):
print(syain[i]) | 1 | 32,594,745,587,480 | null | 169 | 169 |
n = int(input())
d = 'abcdefghijklm'
def conv(s):
s = list(map(lambda x: d[x], s))
return ''.join(s)
def dfs(s, k):
if len(s) == n:
print(s)
else:
for i in range(k):
dfs(s+d[i], k)
dfs(s+d[k], k+1)
dfs('a', 1)
| from collections import deque
import copy
H,W=map(int,input().split())
S=[list(input()) for _ in range(H)]
S1=copy.deepcopy(S)
ans=[]
sy=0
sx=0
for i in range(H):
for j in range(W):
c=0
route=deque([(i,j,0)])
S=copy.deepcopy(S1)
while route:
a,b,n=route.popleft()
c=n
if 0<=a<=H-1 and 0<=b<=W-1:
if S[a][b]=='.':
S[a][b]='#'
route.append((a+1,b,n+1))
route.append((a-1,b,n+1))
route.append((a,b+1,n+1))
route.append((a,b-1,n+1))
ans.append(c-1)
print(max(ans))
| 0 | null | 73,775,023,197,290 | 198 | 241 |
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)
T1, T2 = mapint()
A1, A2 = mapint()
B1, B2 = mapint()
if not (A1-B1)*(A2-B2)<0:
print(0)
else:
if A1<B1:
A1, B1 = B1, A1
A2, B2 = B2, A2
if A1*T1+A2*T2==B1*T1+B2*T2:
print('infinity')
exit(0)
rest = (A1-B1)*T1
come = (B2-A2)*T2
if come<rest:
print(0)
else:
ans = come//(come-rest)
last = 1 if come%(come-rest)==0 else 0
print(ans*2-1-last) | t1,t2 = map(int,input().split())
a1,a2 = map(int,input().split())
b1,b2 = map(int,input().split())
import math
f = (a1-b1)*t1
s = (a2-b2)*t2
if f == -s:
print("infinity")
elif (f>0 and f+s>0) or (f<0 and f+s<0):
print(0)
elif f//(-(f+s)) == math.ceil(f/(-(f+s))):
print(f//(-(f+s))*2)
else:
print(f//(-(f+s))*2+1) | 1 | 131,673,030,286,340 | null | 269 | 269 |
print((int(input()))**2)
| a = int(input())
print(int(a**2)) | 1 | 144,913,989,205,002 | null | 278 | 278 |
s = input()
n = int(input())
for i in range(n):
c = input().split()
a = int(c[1])
b = int(c[2])
if "replace" in c:
s = s[:a] + c[3] + s[b+1:]
if "reverse" in c:
u = s[a:b+1]
s = s[:a] + u[:: -1] +s[b + 1:]
if "print" in c:
print(s[a:b+1], sep = '')
| N, K = map(int, input().split())
A_list = list(map(int, input().split()))
F_list = list(map(int, input().split()))
A_list_min = sorted(A_list)
F_list_max = sorted(F_list, reverse=True)
first_OK = 10**12
def is_ok(arg):
check_cnt = 0
return_flag = 0
for i in range(N):
temp = A_list_min[i] - arg//F_list_max[i]
if temp > 0:
check_cnt += A_list_min[i] - arg//F_list_max[i]
if check_cnt <= K:
return_flag = 1
else:
return_flag = 0
return return_flag
def bisect(ng, ok):
while (abs(ok - ng) > 1):
mid = (ok + ng) // 2
if is_ok(mid):
ok = mid
else:
ng = mid
return ok
print(bisect(-1, first_OK)) | 0 | null | 83,854,663,013,826 | 68 | 290 |
N=int(input())
A=[]
B=[]
for i in range(N):
a,b=map(int,input().split())
A.append(a)
B.append(b)
A.sort()
B.sort()
if N%2!=0:
n=(N+1)//2
ans=B[n-1]-A[n-1]+1
else:
n=N//2
ans1=(A[n-1]+A[n])/2
ans2=(B[n-1]+B[n])/2
ans=(ans2-ans1)*2+1
print(int(ans)) | A, B, M = map(int, input().split())
price_A = list(map(int, input().split()))
price_B = list(map(int, input().split()))
min_A = min(price_A)
min_B = min(price_B)
ans = min_A + min_B
for i in range(M):
x, y, c = map(int, input().split())
ans = min(price_A[x-1]+price_B[y-1]-c,ans)
print(ans)
| 0 | null | 35,631,175,584,768 | 137 | 200 |
k=int(input())
flag=0
s=0
for i in range(1,9*k):
s+=7
s%=k
if s==0:
flag=i
break
s*=10
if flag:
print(flag)
else:
print(-1)
| K = int(input())
if K%2==0:
print(-1)
elif K==1:
print(1)
elif K==7:
print(1)
else:
if K%7==0:
K = K//7
a = 1
cnt = 1
ind = -1
for i in range(K):
a = (a*10)%K
cnt += a
if cnt%K==0:
ind = i
break
if ind<0:
print(-1)
else:
print(ind+2) | 1 | 6,190,448,960,578 | null | 97 | 97 |
#!/usr/bin/env python
def main():
N = int(input())
D = list(map(int, input().split()))
ans = 0
for i in range(N-1):
d1 = D[i]
for d2 in D[i+1:]:
ans += d1 * d2
print(ans)
if __name__ == '__main__':
main()
| import math
x1,y1,x2,y2=map(float,input().split())
n=(x1-x2)**2+(y1-y2)**2
n1=math.sqrt(n)
print(n1)
| 0 | null | 84,213,056,891,590 | 292 | 29 |
icase=0
if icase==0:
r=int(input())
print(r*r)
| S=input()
K=int(input())
if list(S).count(S[0])==len(S):
print(len(S)*K//2)
else:
S_count=0
i=0
while i<len(S)-1:
if S[i]==S[i+1]:
S_count+=1
i+=1
i+=1
if S[0]==S[-1]:
a=1
i=0
while i<len(S)-1:
if S[i]==S[i+1]:
a+=1
i+=1
else:
break
b=1
i=len(S)-1
while i>0:
if S[i]==S[i-1]:
b+=1
i+=-1
else:
break
S_count*=K
S_count-=(K-1)*(a//2 + b//2 - (a+b)//2)
else:
S_count*=K
print(S_count)
| 0 | null | 159,951,765,154,912 | 278 | 296 |
S = input()
ans=""
for i in range(len(S)):
if S[i] == "?":
ans += "D"
else:
ans += S[i]
print(ans) | h, w = map(int, input().split())
if h == 1 or w == 1:
print(1)
else:
print(int((h*w+1)/2)) | 0 | null | 34,615,644,981,440 | 140 | 196 |
import sys
input = sys.stdin.readline
from collections import deque
import math
n, d, a = map(int, input().split())
XH = []
for _ in range(n):
x, h = map(int, input().split())
XH.append((x, h))
XH.sort()
answer = 0
damage = 0
QUEUE = deque()
for x, h in XH:
if QUEUE:
while x > QUEUE[0][0]:
damage -= QUEUE[0][1]
QUEUE.popleft()
if not QUEUE:
break
h -= damage
if h <= 0:
continue
bomb = math.ceil(h / a)
QUEUE.append((x + 2 * d, a * bomb))
damage += a * bomb
answer += bomb
print(answer)
|
N = int(input())
X = []
Y = []
for _ in range(N):
a = int(input())
tmp = []
for _ in range(a):
tmp.append(list(map(int, input().split())))
X.append(a)
Y.append(tmp)
ans = 0
for bit in range(2 ** N):
flag = True
for i in range(N):
if bit >> i & 1:
for x, y in Y[i]:
flag &= (bit >> (x - 1) & 1) == y
if flag:
ans = max(ans, bin(bit).count("1"))
print(ans)
| 0 | null | 101,944,418,557,760 | 230 | 262 |
from collections import deque
n = int(input())
data = deque(list(input() for _ in range(n)))
ans = deque([])
co = 0
for i in data:
if i[6] == ' ':
if i[0] == 'i':
ans.insert(0, i[7:])
co += 1
else:
try:
ans.remove(i[7:])
co -= 1
except:
pass
else:
if i[6] == 'F':
del ans[0]
co -= 1
else:
del ans[-1]
co -= 1
for i in range(0, co-1):
print(ans[i], end=' ')
print(ans[co-1])
| # -*- coding: utf-8 -*-
from collections import deque
N = int(input())
q = deque()
for i in range(N):
lst = input().split()
command = lst[0]
if command == 'insert':
q.appendleft(lst[1])
elif command == 'delete':
try:
q.remove(lst[1])
except Exception:
pass
elif command == 'deleteFirst':
q.popleft()
elif command == 'deleteLast':
q.pop()
print(*q) | 1 | 50,568,212,764 | null | 20 | 20 |
n = int(input())
c = 0
for i in range(1,n+1):
if i%3 or i%5 == 0:
c += 0
if i%3 != 0 and i%5 != 0 :
c += i
print(c) | cases = []
n = 1
while n != 0:
n = int(raw_input())
if n == 0:
continue
cases.append(n)
for i in range(len(cases)):
print "Case %d: %d" %(i + 1, cases[i]) | 0 | null | 17,716,539,731,348 | 173 | 42 |
S = input()
length = len(S)
print('x' * length)
| #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())
H,W,M = readInts()
dic1 = Counter()
dic2 = Counter()
s = set()
for i in range(M):
h,w = map(lambda x:int(x)-1, input().split())
dic1[h] += 1
dic2[w] += 1
s.add((h,w))
# print(dic)
ans = 0
# 重なっているもので最大値がある時もあれば
# 行、列の交差点でボムなしが一番大きいものがある
for h,w in s:
ans = max(ans, dic1[h] + dic2[w] - 1)
dic1 = dic1.most_common()
dic2 = dic2.most_common()
max1 = dic1[0][1]
max2 = dic2[0][1]
for k1,v1 in dic1:
if v1 < max1:
break # continueする必要がない most_commonで大きい方から集めてるので
for k2,v2 in dic2:
if v2 < max2:
break # 同じく
if (k1,k2) in s: # 一度計算したもの
continue
# 両方とも最大であればok
ans = max(ans, v1 + v2)
break
print(ans)
| 0 | null | 38,569,109,435,120 | 221 | 89 |
s= input()
x = "x" * len(s)
print(x) | #coding:utf-8
#3????????°???????????????
n = input()
print "",
for i in xrange(1, n+1):
if i % 3 == 0:
print i,
elif "3" in str(i):
print i, | 0 | null | 37,154,393,375,040 | 221 | 52 |
def stringconcant(S,T):
a=S
b=T
c=a+b
return c
S,T = input().split()
a = stringconcant(T,S)
print(a) | A, B = input().split()
C = B + A
print(C) | 1 | 102,696,406,567,494 | null | 248 | 248 |
import sys
input=lambda: sys.stdin.readline().rstrip()
n=int(input())
A=[int(i) for i in input().split()]
inf=float("inf")
DP=[[-inf]*4 for _ in range(n+1)]
DP[0][2]=0
for i,a in enumerate(A):
if (i+1)%2==0:
DP[i+1][0]=DP[i][3]+a
if i>=1:
DP[i+1][0]=max(DP[i+1][0],DP[i-1][2]+a)
DP[i+1][2]=DP[i][0]
DP[i+1][3]=max(DP[i][1],DP[i][3])
if i>=1:
DP[i+1][3]=max(DP[i+1][3],DP[i-1][2])
else:
DP[i+1][0]=DP[i][2]+a
DP[i+1][1]=DP[i][3]+a
DP[i+1][3]=max(DP[i][0],DP[i][2])
if n%2==0:
print(max(DP[n][0],DP[n][2]))
else:
print(max(DP[n][1],DP[n][3]))
| N = int(input())
A = [int(x) for x in input().split()]
B = [0]*(N+1)
if N == 0:
if A[0] != 1:
print(-1)
exit()
else:
print(1)
exit()
if A[0] != 0:
print(-1)
exit()
else:
B[0] = 1
C = [0]*(N+1)
C[0] = 1
for i in range(1,N+1):
C[i] = 2*(C[i-1] - A[i-1])
if C[i] < A[i]:
print(-1)
exit()
#print(C)
for i in range(N, 0, -1):
if i == N:
B[i] = A[i]
else:
B[i] = min(A[i]+B[i+1], C[i])
print(sum(B))
| 0 | null | 28,359,064,428,270 | 177 | 141 |
n=int(input())
l=["a","b","c","d","e","f","g","h","i","j"]
a="a"
ans=["a"]
for i in range(1,n):
tt=[]
for j in ans:
s=len(set(j))
for k in range(s+1):
tt.append(j+l[k])
ans=tt
ans.sort()
for i in ans:
print(i) | N = int(input())
S = str(input())
count = 0
for n in range(N-1):
if S[n:n+3] == "ABC":
count += 1
print(count) | 0 | null | 75,577,179,666,896 | 198 | 245 |
D = int(input())
C = list(map(int,input().split())) #C[i]AiCを実施しないと下がる満足度
S = [] #S[i][j] i日目にAjcというコンテストを実施した場合に上がる満足度
for i in range(D): #コンテストは0index
temp = list(map(int,input().split()))
S.append(temp)
T = []
for i in range(D):
temp = int(input())
T.append(temp)
since = [0 for _ in range(26)] #最後に開催されてから何日経ったか。
manzoku = 0
for i in range(26):
since[i] += C[i]
for i in range(D):
contest = T[i]-1 #0index
manzoku += S[i][contest]
for j in range(26):
if j != contest:
manzoku -= since[j]
print(manzoku)
for j in range(26):
if j != contest:
since[j] += C[j]
else:
since[j] = C[j] | D = int(input())
C = [int(T) for T in input().split()]
S = [[] for TD in range(0,D)]
for TD in range(0,D):
S[TD] = [int(T) for T in input().split()]
Type = 26
Last = [0]*Type
Sats = 0
for TD in range(0,D):
Test = int(input())-1
Last[Test] = TD+1
Sats += S[TD][Test]
for TC in range(0,Type):
Sats -= C[TC]*(TD+1-Last[TC])
print(Sats) | 1 | 10,014,550,608,608 | null | 114 | 114 |
H,W = (int(x) for x in input().split())
lines = [input() for i in range(H)]
dp = [[0] * W for _ in range(H)]
if lines[0][0] == "#":
dp[0][0] = 1
q = r = dp[0][0]
for i in range(H):
for j in range(W):
if i == j == 0: continue
if j > 0:
q = dp[i][j-1]
if lines[i][j] == "#" and lines[i][j-1] == ".":
q = dp[i][j-1] + 1
if i > 0:
r = dp[i-1][j]
if lines[i][j] == "#" and lines[i-1][j] == ".":
r = dp[i-1][j] + 1
if j == 0:
dp[i][j] = r
elif i == 0:
dp[i][j] = q
else:
dp[i][j] = min(q,r)
print(dp[H-1][W-1]) | h = int(input())
w = int(input())
n = int(input())
v = max(h,w)
cnt = (n-1)//v + 1
print(cnt) | 0 | null | 69,073,058,130,440 | 194 | 236 |
while True:
H,W = list(map(int, input().split()))
if (H == 0 and W == 0):
break
else:
for n in range(H):
s = ""
for m in range(W):
s += "#"
print(s)
print("") | H, W = map(int, input().split())
while H or W:
for i in range(H):
for j in range(W):
print('#', end='')
print()
print()
H, W = map(int, input().split()) | 1 | 778,629,915,580 | null | 49 | 49 |
N, K = [int(i) for i in input().split()]
R, S, P = [int(i) for i in input().split()]
d = {'r': P, 's': R, 'p': S}
T = input()
checked = [False for i in range(N)]
# 勝てるだけ勝てばいい
for i in range(N-K):
if T[i] == T[i+K]:
if checked[i] == False:
checked[i+K] = True
result = 0
for i in range(N):
if checked[i] == False:
result += d[T[i]]
print(result) | H=int(input())
W=int(input())
N=int(input())
K=H
if K<W: K=W
sum = 0
ans= 0
for i in range(1,K+1):
if sum < N:
sum += K
ans = i
#print(ans, K)
print(ans) | 0 | null | 98,256,575,616,088 | 251 | 236 |
x = input()
x = int(x)
x **= 3
print(x) | print(str(int(input())**3)) | 1 | 274,459,913,740 | null | 35 | 35 |
a,b = input().split()
A = (a*int(b))
B = (b*int(a))
if A < B:
print(A)
else:
print(B) | from collections import Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = [list(map(int, input().split())) for _ in range(q)]
cou = Counter(a)
s = sum(a)
for b, c in bc:
s = s + cou[b]*(c-b)
cou[c] += cou[b]
cou[b] = 0
print(s) | 0 | null | 48,086,047,037,472 | 232 | 122 |
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
N, M = lr()
bl = N == M
print('Yes' if bl else 'No')
| s,j= input().split()
if s==j:
print("Yes")
else:
print("No")
| 1 | 82,807,572,629,870 | null | 231 | 231 |
N = int(input())
if N % 2 == 1:
print(0)
else:
i = 1
ans = 0
while 2*(5**i) <= N:
div = 2 * (5 ** i)
ans += int(N//div)
i += 1
print(ans) | N = int(input())
s = [input() for i in range(N)]
for v in ['AC', 'WA', 'TLE', 'RE']:
print('{0} x {1}'.format(v, s.count(v))) | 0 | null | 62,518,930,339,140 | 258 | 109 |
def read_int():
return int(input().strip())
def read_ints():
return list(map(int, input().strip().split(' ')))
def solve():
N = read_int()
D = read_ints()
S = sum(D)
answer = 0
for d in D:
S -= d
answer += d*S
return answer
if __name__ == '__main__':
print(solve())
| n = int(input())
num = list(map(int,input().split()))
a = 0
b = 0
for i in range(n):
for j in range(n):
a = a + num[i] * num[j]
for i in range(n):
b = b + num[i] * num[i]
print((a - b) // 2)
| 1 | 168,631,819,853,600 | null | 292 | 292 |
from itertools import groupby
n= input()
s = input()
G = groupby(s)
print(len(list(G))) | # coding: utf-8
while 1:
a, op, b = input().split()
if op == "+":
ans = int(a) + int(b)
elif op == "-":
ans = int(a) - int(b)
elif op == "*":
ans = int(a) * int(b)
elif op == "/":
ans = int(a) // int(b)
else:
break
print(ans) | 0 | null | 85,220,724,034,590 | 293 | 47 |
n = int(input())
a,b = map(int, input().split())
F = True
while a<=b:
if a%n == 0:
print('OK')
F = False
break
a += 1
if F:
print('NG') | N = int(input())
# 26進数に変換する問題
ans = ""
alphabet = "Xabcdefghijklmnopqrstuvwxyz"
while N > 0:
mod = N % 26
if mod == 0:
mod = 26
ans = alphabet[mod] + ans
N -= mod
N //= 26
print(ans)
| 0 | null | 19,190,196,312,598 | 158 | 121 |
from collections import defaultdict
n = int(input())
dic = defaultdict(lambda: 0)
for _ in range(n):
com = input().split(' ')
if com[0]=='insert':
dic[com[1]] = 1
else:
if com[1] in dic:
print('yes')
else:
print('no') | d = set()
n = int(input())
for i in range(n):
raw=input().split()
if raw[0] == 'insert':
d.add(raw[1])
else:
if raw[1] in d:
print('yes')
else:
print('no')
| 1 | 76,035,634,150 | null | 23 | 23 |
for a in range(1,10):
for b in range(1,10): print "%dx%d=%d" %(a,b,a*b) | n, k = map(int, input().split())
s = [0]
for i in range(1,n+1):
s.append(s[i-1] + i)
ans = 0
for i in range(k,n+1):
ans += ((s[n]-s[n-i]) - s[i-1])+ 1
print((ans+1) % (10**9 + 7))
| 0 | null | 16,482,087,390,980 | 1 | 170 |
def gcd(x,y):
if x%y!=0:
return gcd(y,x%y)
else:
return y
def GCD(x,y,z):
return gcd(gcd(x,y),z)
ans=0
K=int(input())
for a in range(1,K+1):
for b in range(1,K+1):
for c in range(1,K+1):
ans+=GCD(a,b,c)
print(ans)
| k = int(input())
def gcd1 (a, b):
while True:
if (a < b):
a, b = b, a
c = a%b
if (c == 0):
return (b)
else:
a = b
b = c
def gcd2 (a, b, c):
tmp = gcd1(a, b)
ans = gcd1(tmp, c)
return (ans)
count = 0
for i in range(k):
for j in range(i, k):
for l in range(j, k):
tmp = gcd2(i + 1, j + 1, l + 1)
if (i == j == l):
count = count + tmp
elif (i == j or j == l):
count = count + tmp*3
else:
count = count + tmp*6
print(count)
| 1 | 35,491,844,116,164 | null | 174 | 174 |
import math
def main():
x1, y1, x2, y2 = [float(n) for n in input().split()]
d = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
print("{:.5f}".format(d));
if __name__ == '__main__':
main()
| S = list(input())
K = int(input())
S.extend(S)
prev = ''
cnt = 0
for s in S:
if s == prev:
cnt += 1
prev = ''
else:
prev = s
b = 0
if K % 2 == 0:
b += 1
if K > 2 and S[0] == S[-1]:
mae = 1
while mae < len(S) and S[mae] == S[0]:
mae += 1
if mae % 2 == 1 and mae != len(S):
usiro = 1
i = len(S) - 2
while S[i] == S[-1]:
usiro += 1
i -= 1
if usiro % 2 == 1:
b = K // 2
if K % 2 == 0:
res = cnt * (K // 2) + (b - 1)
else:
res = cnt * (K // 2) + cnt // 2 + b
print(res)
| 0 | null | 88,094,856,364,970 | 29 | 296 |
# from collections import defaultdict
import math
def comb(n, r):
return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N = int(input())
K = int(input())
keta = len(str(N))
dp = [[0]*(keta) for i in range(4)]
ans = 0
for k in range(1, 4):
for i in range(1, keta):
if i<k:
continue
else:
dp[k][i] = comb(i-1, i-k)*(9**k)
#print(dp)
# dp[k][i]: i桁で、k個の0でない数
ans += sum(dp[K]) # (N の桁)-1 までの累積和
count = 0
for j in range(keta):
t = int(str(N)[j])
if j==0:
count+=1
ans += sum(dp[K-count])*(t-1)
if count == K: # K==1
ans+=t
break
continue
elif j==keta-1:
if t!=0:
count+=1
if count==K:
ans+=t
break
if t !=0:
count+=1
if count==K:
ans+=sum(dp[K-count+1][:keta-j]) #0のとき
ans+=t
break
ans += sum(dp[K-count][:keta-j])*(t-1) #0より大きいとき
ans += sum(dp[K-count+1][:keta-j]) #0のとき
print(ans) |
def findnumberofTriangles(arr):
n = len(arr)
arr.sort()
count = 0
for i in range(0, n-2):
k = i + 2
for j in range(i + 1, n):
while (k < n and arr[i] + arr[j] > arr[k]):
k += 1
if(k>j):
count += k - j - 1
return count
n = int(input())
arr=[int(x) for x in input().split()]
print(findnumberofTriangles(arr))
| 0 | null | 124,444,406,384,160 | 224 | 294 |
list =[]
i=0
index=1
while(index != 0):
index=int(input())
list.append(index)
i +=1
for i in range(0,i):
if list[i] == 0:
break
print("Case",i+1,end='')
print(":",list[i])
| x1,y1,x2,y2=list(map(float,input().split()))
d=((x1-x2)**2+(y1-y2)**2)**0.5
print(d) | 0 | null | 324,953,830,890 | 42 | 29 |
from collections import deque
class SegmentTree():
def __init__(self,n,ide_ele,merge_func,init_val):
self.n=n
self.ide_ele=ide_ele
self.merge_func=merge_func
self.val=[0 for i in range(1<<n)]
self.merge=[0 for i in range(1<<n)]
self.parent=[-1 for i in range(1<<n)]
deq=deque([1<<(n-1)])
res=[]
while deq:
v=deq.popleft()
res.append(v)
if not v&1:
gap=(v&-v)//2
self.parent[v-gap]=v
deq.append(v-gap)
self.parent[v+gap]=v
deq.append(v+gap)
for v in res[::-1]:
if v-1<len(init_val):
self.val[v-1]=init_val[v-1]
self.merge[v-1]=self.val[v-1]
if not v&1:
gap=(v&-v)//2
self.merge[v-1]=self.merge_func(self.merge[v-1],self.merge[v-gap-1],self.merge[v+gap-1])
def update(self,id,x):
self.val[id]=x
pos=id+1
while pos!=-1:
if pos&1:
self.merge[pos-1]=self.val[pos-1]
else:
gap=(pos&-pos)//2
self.merge[pos-1]=self.merge_func(self.val[pos-1],self.merge[pos+gap-1],self.merge[pos-gap-1])
pos=self.parent[pos]
def cnt(self,k):
lsb=(k)&(-k)
return (lsb<<1)-1
def lower_kth_merge(self,nd,k):
res=self.ide_ele
id=nd
if k==-1:
return res
while True:
if not id%2:
gap=((id)&(-id))//2
l=id-gap
r=id+gap
cnt=self.cnt(l)
if cnt<k:
k-=cnt+1
res=self.merge_func(res,self.val[id-1],self.merge[l-1])
id=r
elif cnt==k:
res=self.merge_func(res,self.val[id-1],self.merge[l-1])
return res
else:
id=l
else:
res=self.merge_func(res,self.val[id-1])
return res
def upper_kth_merge(self,nd,k):
res=self.ide_ele
id=nd
if k==-1:
return res
while True:
if not id%2:
gap=((id)&(-id))//2
l=id-gap
r=id+gap
cnt=self.cnt(r)
if cnt<k:
k-=cnt+1
res=self.merge_func(res,self.val[id-1],self.merge[r-1])
id=l
elif cnt==k:
res=self.merge_func(res,self.val[id-1],self.merge[r-1])
return res
else:
id=r
else:
res=self.merge_func(res,self.val[id-1])
return res
def query(self,l,r):
id=1<<(self.n-1)
while True:
if id-1<l:
id+=((id)&(-id))//2
elif id-1>r:
id-=((id)&(-id))//2
else:
res=self.val[id-1]
if id%2:
return res
gap=((id)&(-id))//2
L,R=id-gap,id+gap
#print(l,r,id,L,R)
left=self.upper_kth_merge(L,id-1-l-1)
right=self.lower_kth_merge(R,r-id)
return self.merge_func(res,left,right)
ide_ele=0
def seg_func(*args):
res=ide_ele
for val in args:
res|=val
return res
def popcount(x):
x = x - ((x >> 1) & 0x55555555)
x = (x & 0x33333333) + ((x >> 2) & 0x33333333)
x = (x + (x >> 4)) & 0x0f0f0f0f
x = x + (x >> 8)
x = x + (x >> 16)
return x & 0x0000007f
import sys
input=sys.stdin.readline
N=int(input())
S=input().rstrip()
init_val=[1<<(ord(S[i])-97) for i in range(N)]
test=SegmentTree(19,ide_ele,seg_func,init_val)
for _ in range(int(input())):
t,l,r=input().split()
t,l=int(t),int(l)
if t==1:
val=ord(r)-97
test.update(l-1,1<<val)
else:
r=int(r)
res=test.query(l-1,r-1)
print(popcount(res)) | #!/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)))
| 1 | 62,675,790,256,640 | null | 210 | 210 |
t=str(input())
print(t.replace("?","D")) | import itertools
import math
N = int(input())
XY = [list(map(int, input().split())) for i in range(N)]
n = [i for i in range(N)]
dist = 0
cnt = 0
for target_list in list(itertools.permutations(n)):
for i in range(len(target_list)):
if i == 0:
continue
dist += math.sqrt((XY[target_list[i]][0] - XY[target_list[i-1]][0]) ** 2 + (XY[target_list[i]][1] - XY[target_list[i-1]][1]) ** 2)
cnt += 1
print(dist / cnt) | 0 | null | 83,336,381,475,604 | 140 | 280 |
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
asum=[0]
bsum=[0]
for i in range(n):
asum.append(asum[-1]+a[i])
for i in range(m):
bsum.append(bsum[-1]+b[i])
ans=0
for i in range(n+1):
if asum[i]>k:
break
while asum[i]+bsum[m]>k and m>0:
m-=1
ans=max(ans,i+m)
print(ans) | def main(A,B,C,K):
ans=False
while K >= 0:
if A < B:
if B < C:
ans=True
return ans
else:
C = C * 2
else:
B = B * 2
K=K-1
return ans
A,B,C=map(int, input().split())
K=int(input())
ans=main(A,B,C,K)
print('Yes' if ans else 'No') | 0 | null | 8,774,319,889,298 | 117 | 101 |
moji = input()
if moji[len(moji)-1] == 's':
moji += 'es'
else :
moji += 's'
print(moji) | while True:
x = input().split()
a = int(x[0])
b = int(x[1])
if a == 0 and b == 0:
break
elif a > b:
print('{0} {1}'.format(b, a))
else:
print('{0} {1}'.format(a, b)) | 0 | null | 1,466,011,541,680 | 71 | 43 |
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
n = I()
lst = [None, ]
for _ in range(n):
x, y = LI()
lst.append((x,y))
per = itertools.permutations(range(1,n+1))
sm = 0
oc = math.factorial(n)
for p in per:
dist = 0
before = p[0]
for place in p[1:]:
dist += math.sqrt((lst[place][0]-lst[before][0])**2 + (lst[place][1]-lst[before][1])**2)
before = place
sm += dist
ans = sm/oc
print(ans)
main()
| from itertools import repeat
print(sum((lambda a, b, c: map(lambda r, n: n % r == 0,
range(a, b+1), repeat(c))
)(*map(int, input().split())))) | 0 | null | 74,649,455,560,800 | 280 | 44 |
k = int(input())
s = input()
sen = ''
if len(s) <= k:
print(s)
else:
for i in range(k):
sen = sen + s[i]
print(sen+'...')
| K = int(input())
S = str(input())
def result(K: int, S: str) -> str:
if len(S) <= K:
return S
else:
answer = S[:K]
return answer + '...'
print(result(K, S))
| 1 | 19,686,020,986,694 | null | 143 | 143 |
import math
member = []
score = []
while True:
num = int(raw_input())
if num == 0: break
member.append(num)
score.append(map(int, raw_input().split()))
alpha = []
for m, s in zip(member, score):
average = sum(s)/float(m)
sigma = 0
for t in s:
sigma += (t - average)**2
else:
alpha += [sigma/m]
for a in alpha:
#print '%.8f' % math.sqrt(a)
print math.sqrt(a) | 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))) | 1 | 194,611,329,990 | null | 31 | 31 |
S = input()
K = int(input())
#ランレングス圧縮
rle = []
pre = 'A'
chain = 1
for c in S:
if c == pre:
chain += 1
else:
if pre != 'A':
rle.append([pre,chain])
pre = c
chain = 1
rle.append([pre,chain])
ans = 0
#1種類の文字列の場合
if len(rle) == 1:
ans = (rle[0][1] * K) // 2
#2種類以上の文字列の場合
else:
#先頭と末尾が同じ文字
if rle[0][0] == rle[-1][0]:
#先頭、末尾、つなぎ目の対応
ans += rle[0][1]//2
ans += rle[-1][1]//2
rle[0][1] += rle[-1][1]
rle[-1][1] = 0
ans += rle[0][1]//2 * (K-1)
#Sの中での連続文字の対応
mid_chains = 0
for i in range(1,len(rle)):
mid_chains += rle[i][1]//2
ans += mid_chains * K
#先頭と末尾が異なる文字
else:
mid_chains = 0
for i in range(len(rle)):
mid_chains += rle[i][1]//2
ans += mid_chains * K
#print(rle)
print(ans) | text = ''
while True:
try:
s = input().lower()
except EOFError:
break
if s == '':
break
text += s
for i in 'abcdefghijklmnopqrstuvwxyz':
print(i+' : '+str(text.count(i)))
#print("{0} : {1}".format(i, text.count(i))) | 0 | null | 88,656,994,904,968 | 296 | 63 |
n,m,l = map(int,input().split())
A,B,C = [],[],[]
for i in range(n):
A.append(list(map(int,input().split())))
for ii in range(m):
B.append(list(map(int,input().split())))
for k in range(n):
ans = []
for s in range(l):
cul = 0
for kk in range(m):
cul += A[k][kk] * B[kk][s]
ans.append(cul)
ans = list(map(str,ans))
print(" ".join(ans))
| m1, d1 = map(int, input().split())
m2, d2 = map(int, input().split())
ans = 1
if m1 == m2:
ans = 0
print(ans) | 0 | null | 62,627,703,617,638 | 60 | 264 |
def func(l):
i = 0
for num in range(l[0], l[1]+1):
#print(num)
if l[2] % num == 0:
#print(num)
i = i + 1
return i
if __name__ == '__main__':
N = input().split(" ")
l = []
for i in N:
l.append(int(i))
result = func(l)
print(result) | inp = input()
a = inp.split()
a[0] = int(a[0])
a[1] = int(a[1])
a[2] = int(a[2])
c = 0
for i in range(a[0],a[1]+1):
if a[2] % i == 0:
c += 1
print(c)
| 1 | 570,302,287,008 | null | 44 | 44 |
import math
n=int(input())
m=100000
k=100000
for i in range(n):
m=1.05*k
m=m/1000
k=math.ceil(m)*1000
print(k)
| import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
from functools import lru_cache
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7
def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]
def main():
X = int(input())
print(X * 360 // math.gcd(360, X) // X)
if __name__ == "__main__":
main()
| 0 | null | 6,494,678,549,138 | 6 | 125 |
n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
if a[m-1] < (sum(a) / (4*m)):
print('No')
else:
print('Yes') | X, N = map(int, input().split())
if N>0:
p = [int(i) for i in input().split()]
else:
p = [-1]
p = sorted(p)
#print(p)
try:
idx=p.index(X)
flag=False
for i in range(X+1):
for o in [-1,+1]:
if idx+(i*o) < 0 or idx + (i*o) >= N:
print(X+(i*o))
flag=True
break
elif X+(i*o)!=p[idx+(i*o)]:
print(X+(i*o))
flag=True
break
if flag:
break
except:
print(X) | 0 | null | 26,257,801,047,010 | 179 | 128 |
n,k= map(int,input().split())
temp = 1
cnt = 0
while n >= temp:
temp *= k
cnt += 1
print(max(1,cnt)) | import sys
N,K=map(int,input().split())
if N<K:
print(1)
sys.exit()
M=K
R=1
while M<=N:
M*=K
R+=1
print(R) | 1 | 64,205,780,790,912 | null | 212 | 212 |
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
MOD = 10**9+7
def main():
N,*A = map(int, read().split())
cnt = [0] * N
ans = 1
for a in A:
if a == 0:
cnt[0] += 1
continue
ans *= cnt[a - 1] - cnt[a]
ans %= MOD
cnt[a] += 1
if cnt[0] == 1:
ans *= 3
elif cnt[0] <= 3:
ans *= 6
else:
ans = 0
ans %= MOD
print(ans)
if __name__ == "__main__":
main()
| n=int(input())
a=list(map(int,input().split()))
ans=1
d={}
ch=0
mod=10**9+7
for i in range(n):
if a[i]==0:
if 0 not in d:
d[0]=1
ans*=3
else:
if d[0]==1:
d[0]=2
ans*=2
elif d[0]==2:
d[0]=3
else:
ch+=1
break
else:
if a[i] not in d:
d[a[i]]=1
if a[i]-1 not in d:
ch+=1
break
else:
ans*=d[a[i]-1]
else:
if d[a[i]]==1:
d[a[i]]=2
if a[i]-1 not in d:
ch+=1
break
else:
if d[a[i]-1]>=2:
ans*=d[a[i]-1]-1
else:
ch+=1
break
elif d[a[i]]==2:
d[a[i]]=3
if a[i]-1 not in d:
ch+=1
break
else:
if d[a[i]-1]>=3:
ans*=1
else:
ch+=1
break
else:
ch+=1
break
ans=(ans%mod)
if ch==0:
print(ans)
else:
print(0)
| 1 | 130,080,465,293,012 | null | 268 | 268 |
import sys,queue,math,copy,itertools,bisect,collections,heapq
def main():
sys.setrecursionlimit(10**7)
INF = 10**18
MOD = 10**9 + 7
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
N = NI()
N0 = 2**(N.bit_length())
st = [0] * (N0*2)
def gindex(l, r):
L = l + N0; R = r + N0
lm = (L // (L & -L)) // 2
rm = (R // (R & -R)) // 2
while L < R:
if R <= rm:
yield R - 1
if L <= lm:
yield L - 1
L //= 2; R //= 2
while L > 0:
yield L - 1
L //= 2
def update(i,s):
x = 2 ** (ord(s) - ord('a'))
i += N0-1
st[i] = x
while i > 0:
i = (i-1) // 2
st[i] = st[i*2+1] | st[i*2+2]
def query(l,r):
l += N0
r += N0
ret = 0
while l < r:
if l % 2:
ret |= st[l-1]
l += 1
if r % 2:
r -= 1
ret |= st[r-1]
l //= 2 ; r //= 2
return ret
for i,s in enumerate(sys.stdin.readline().rstrip()):
update(i+1,s)
Q = NI()
for _ in range(Q):
c,a,b = sys.stdin.readline().split()
if c == '1':
update(int(a),b)
else:
ret = query(int(a),int(b)+1)
cnt = 0
b = 1
for i in range(26):
cnt += (b & ret) > 0
b <<= 1
print(cnt)
if __name__ == '__main__':
main() | class SegmentTree():
def segfunc(self, x, y):
return x|y #関数
def __init__(self, a): #a:リスト
n = len(a)
self.ide_ele = 0 #単位元
self.num = 2**((n-1).bit_length()) #num:n以上の最小の2のべき乗
self.seg = [self.ide_ele]*2*self.num
#set_val
for i in range(n):
self.seg[i+self.num-1] = a[i]
#built
for i in range(self.num-2,-1,-1):
self.seg[i] = self.segfunc(self.seg[2*i+1], self.seg[2*i+2])
def update(self, k, s):
x = 1<<(ord(s)-97)
k += self.num-1
self.seg[k] = x
while k:
k = (k-1)//2
self.seg[k] = self.segfunc(self.seg[k*2+1], self.seg[k*2+2])
def query(self, p, q):
if q <= p:
return self.ide_ele
p += self.num-1
q += self.num-2
res = self.ide_ele
while q - p > 1:
if p&1 == 0:
res = self.segfunc(res, self.seg[p])
if q&1 == 1:
res = self.segfunc(res, self.seg[q])
q -= 1
p = p//2
q = (q-1)//2
if p == q:
res = self.segfunc(res,self.seg[p])
else:
res = self.segfunc(self.segfunc(res,self.seg[p]),self.seg[q])
return res
def popcount(x):
'''xの立っているビット数をカウントする関数
(xは64bit整数)'''
# 2bitごとの組に分け、立っているビット数を2bitで表現する
x = x - ((x >> 1) & 0x5555555555555555)
# 4bit整数に 上位2bit + 下位2bit を計算した値を入れる
x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f # 8bitごと
x = x + (x >> 8) # 16bitごと
x = x + (x >> 16) # 32bitごと
x = x + (x >> 32) # 64bitごと = 全部の合計
return x & 0x0000007f
N = int(input())
S = list(input())
for i in range(N):
S[i] = 1<<(ord(S[i])-97)
Seg = SegmentTree(S)
Q = int(input())
for i in range(Q):
x, y, z = input().split()
if int(x) == 1:
Seg.update(int(y)-1, z)
else:
print(popcount(Seg.query(int(y)-1, int(z)))) | 1 | 62,729,321,782,592 | null | 210 | 210 |
import sys
input = sys.stdin.readline
I=lambda:int(input())
MI=lambda:map(int,input().split())
LI=lambda:list(map(int,input().split()))
N,T=MI()
dp=[[0]*(T+1) for _ in [0]*(N+1)]
food=[tuple(MI()) for _ in [0]*N]
food.sort()
for n in range(N):
a,b=food[n]
for t in range(T+1):
if t<T:
# たべる
dp[n+1][min(t+a,T)]=max(dp[n][t]+b,dp[n+1][min(t+a,T)])
# たべない
dp[n+1][t]=max(dp[n][t],dp[n+1][t])
print(dp[N][T]) | A = input()
A = list(A.split())
sum = 0
for i in A:
sum += int(i)
if sum >= 22:
print('bust')
else:
print('win') | 0 | null | 134,938,055,547,460 | 282 | 260 |
s = int(input())
print(s**2)
| while True:
number_of_input = int(raw_input())
if number_of_input == 0:
break
values = [float(x) for x in raw_input().split()]
second_moment = sum([x * x for x in values]) / number_of_input
first_moment = sum([x for x in values]) / number_of_input
print(second_moment - first_moment ** 2.0) ** 0.5 | 0 | null | 72,361,756,928,088 | 278 | 31 |
import sys
n=int(input())
if n%2:
print(0)
sys.exit()
# 10->50->2500の個数
cnt=0
v=10
while 1:
cnt+=n//v
if n//v==0:
break
v*=5
print(cnt) | #!/usr/bin/env python
n = int(input())
if n%2 == 1:
print(0)
exit()
mp = tmp = 0
while True:
if 5**tmp > n:
break
mp = tmp
tmp += 1
ans = 0
for i in range(1, mp+1):
ans += n//(2*(5**i))
print(ans)
| 1 | 115,871,952,370,382 | null | 258 | 258 |
h,n = map(int,input().split())
ab = []
for i in range(n):
a,b = map(int,input().split())
ab.append([a,b])
#ab.sort(key=lambda x: x[2], reverse=True)
if h==9999 and n==10:
print(139815)
exit()
#bubble sort
for i in range(n-1,-1,-1):
for j in range(0,i):
if ab[j][0]*ab[j+1][1]<ab[j+1][0]*ab[j][1]:
tmp = ab[j]
ab[j] = ab[j+1]
ab[j+1] = tmp
ans = 0
anslist = []
def indexH(h,arr):
li = []
for i in range(len(arr)):
if arr[i][0]>=h:
li.append(i)
return li[::-1]
while 1:
if len(ab)==0:
break
if max(ab, key=lambda x:x[0])[0]<h:
h-=ab[0][0]
ans+=ab[0][1]
#print(h,ans)
else:
c = 0
index = indexH(h,ab)
#print(h,index,ab,ab)
for i in range(len(index)):
anslist.append(ans+ab[index[i]][1])
ab.pop(index[i])
print(min(anslist)) |
def resolve():
n, t = map(int, input().split())
foods = []
limit = 0
tt = []
for i in range(n):
a, b = map(int, input().split())
foods.append((a,b))
limit += a
tt.append(a)
c = max(tt) * 2
foods.sort(key=lambda x:(x[0], -x[1]))
limit = min(limit * 2, t * 2)
time = [0 for _ in range(limit+c)]
used = [0] * (limit + c)
used[0] = 1
for a,b in foods:
for i in range(limit+c)[::-1]:
if used[i] and i+a*2 < limit + c and time[i+a*2] < time[i] + b:
time[i+a*2] = time[i] + b
if i+a*2 < limit:
used[i+a*2] = 1
print(max(time))
if __name__ == "__main__":
resolve()
| 0 | null | 116,373,949,184,948 | 229 | 282 |
from itertools import accumulate
N, K = map(int, input().split())
P = list(map(int, input().split()))
P = [((x+1) * x / 2) / x for x in P]
A = list(accumulate(P))
ans = A[K-1]
for i in range(K, N):
ans = max(ans, A[i] - A[i-K])
print(ans)
| N, K = map(int, input().split())
nums = list(map(int, input().split()))
expect = 0
answer = 0
for i in range(N):
if i < K:
expect += (nums[i]+1)/2
else:
expect += (nums[i]+1)/2 - (nums[i-K]+1)/2
answer = max(answer, expect)
print(answer) | 1 | 74,663,426,059,362 | null | 223 | 223 |
x, k, d = map(int, input().split())
x = abs(x)
if x//d >= k:
print(x-k*d)
else:
k -= x//d
x -= d*(x//d)
if k%2==0:
print(abs(x))
else:
print(abs(x-d)) | x, k, d = map(int, input().split())
# k回 dだけ変化
n=abs(x)//d
if k>=n:
k=k-n
else:
n=k
k=0
if k%2 == 0:
x=abs(x)-d*n
else:
x=abs(x)-d*(n+1)
print(abs(x)) | 1 | 5,237,909,772,272 | null | 92 | 92 |
def get_dice(pips):
return [[0, pips[4], 0, 0],
[pips[3], pips[0], pips[2], pips[5]],
[0, pips[1], 0, 0]]
def move_e(dice):
dice[1] = dice[1][3:] + dice[1][:3]
return dice
def move_n(dice):
return [[0, dice[1][1], 0, 0],
[dice[1][0], dice[2][1], dice[1][2], dice[0][1]],
[0, dice[1][3], 0, 0]]
def move_s(dice):
return [[0, dice[1][3], 0, 0],
[dice[1][0], dice[0][1], dice[1][2], dice[2][1]],
[0, dice[1][1], 0, 0]]
def move_w(dice):
dice[1] = dice[1][1:] + dice[1][:1]
return dice
func = {'E':move_e,'N':move_n,'S':move_s,'W':move_w}
dice = get_dice(list(map(int, input().split())))
for c in input():
dice = func[c](dice)
print(dice[1][1]) | class dice:
def __init__(self, numlist):
self._men = {"T": numlist[0],
"B": numlist[5],
"N": numlist[4],
"S": numlist[1],
"W": numlist[3],
"E": numlist[2]}
def roll(self, direction):
t = dict(self._men)
if direction == "N":
self._men["T"] = t["S"]
self._men["B"] = t["N"]
self._men["N"] = t["T"]
self._men["S"] = t["B"]
elif direction == "S":
self._men["T"] = t["N"]
self._men["B"] = t["S"]
self._men["S"] = t["T"]
self._men["N"] = t["B"]
elif direction == "W":
self._men["T"] = t["E"]
self._men["B"] = t["W"]
self._men["W"] = t["T"]
self._men["E"] = t["B"]
elif direction == "E":
self._men["T"] = t["W"]
self._men["B"] = t["E"]
self._men["W"] = t["B"]
self._men["E"] = t["T"]
def gettop(self):
return self._men["T"]
def decode():
numlist = [int(x) for x in input().split()]
dlist = input()
return numlist, dlist
if __name__ == '__main__':
numlist, dlist = decode()
d1 = dice(numlist)
for c in dlist:
d1.roll(c)
print(d1.gettop())
| 1 | 235,486,825,730 | null | 33 | 33 |
import sys
s = sys.stdin.readlines
A, V = [int(x) for x in input().split()]
B, W= [int(x) for x in input().split()]
T = int(input())
d = abs(A - B)
d2 = (V - W) * T
s = 'YES' if d <= d2 else 'NO'
print(s)
| N = int(input())
A = []
for i in range(N):
A.append(int(input()))
def insertionSort(N,A,g):
global cnt
for i in range(g,N):
v = A[i]
j = i-g
while A[j] > v and j >= 0:
A[j+g] = A[j]
j -= g
cnt += 1
A[j+g] = v
return A
def shellSort(N,A):
global cnt
cnt = 0
m = 1
G = [1]
x = 1
for i in range(N):
x = 3*x + 1
if x > N:
break
G.insert(0,x)
m += 1
for i in range(m):
A = insertionSort(N,A,G[i])
return cnt,A,m,G
cnt,A,m,G = shellSort(N,A)
print(m)
print(" ".join([str(g) for g in G]))
print(cnt)
for i in range(len(A)):
print(A[i])
| 0 | null | 7,601,069,981,788 | 131 | 17 |
from collections import defaultdict
roots = defaultdict(list)
for k in range(2, 10 ** 6 + 1):
ki = k * k
while ki <= 10 ** 12:
roots[ki].append(k)
ki *= k
def factors(n):
fhead = []
ftail = []
for x in range(1, int(n ** 0.5) + 1):
if n % x == 0:
fhead.append(x)
if x * x != n:
ftail.append(n // x)
return fhead + ftail[::-1]
def solve(N):
works = set()
# Answer seems to be anything of the form: N = (K ** i) * (j * K + 1)
# Try every divisor of N as K ** i
for d in factors(N):
# If i == 1, then K == d
k = d
if (N // d) % k == 1:
assert k <= N
works.add(k)
if d == 1:
# If i == 0, then K ** 0 == 1, so K = (N - 1) // j, so anything that is a factor of N - 1 works
works.update(factors(N - 1))
elif d in roots:
# For i >= 2, see if it's in the list of precomputed roots
for k in roots[d]:
if k > N:
break
if (N // d) % k == 1:
works.add(k)
works.discard(1)
return len(works)
(N,) = [int(x) for x in input().split()]
print(solve(N))
if False:
def brute(N, K):
while N >= K:
if N % K == 0:
N = N // K
else:
N = N - K
return N
for N in range(2, 10000):
count = 0
for K in range(2, N + 1):
temp = brute(N, K)
if temp == 1:
print(N, K, temp)
count += 1
print("##########", N, count, solve(N))
assert count == solve(N)
| def make_divisors(n):
divisors = []
for i in range(1, int(n**0.5)+1):
if n % i == 0:
divisors.append(i)
if i != n // i:
divisors.append(n//i)
return divisors
N = int(input())
d1 = make_divisors(N)
d2 = make_divisors(N - 1)
ans = len(d2) - 1
for n in d1:
if n == 1:
continue
n2 = N
while n2 % n == 0:
n2 //= n
if n2 % n == 1:
if n not in d2:
ans += 1
print(ans)
| 1 | 41,349,271,750,364 | null | 183 | 183 |
N = int(input())
S = str(input())
i = 0
ans = len(S)
for i in range(len(S)-1):
if S[i] == S[i+1]:
ans -= 1
print(ans) | N = int(input())
S = list(input())
cnt = 1
tmp = S[0]
for k in range(1, N):
if not S[k] == tmp:
cnt += 1
tmp = S[k]
print(cnt) | 1 | 169,875,092,772,210 | null | 293 | 293 |
N = int(input())
G = [[0] * N for _ in range(N)]
time = 0
times = [{'s': 0, 'f': 0} for _ in range(N)]
for _ in range(N):
u, n, *K = map(int, input().split())
for k in K:
G[u - 1][k - 1] = 1
def dfs(u):
global time
time += 1
times[u]['s'] = time
for i in range(N):
if G[u][i] == 1 and times[i]['s'] == 0:
dfs(i)
else:
time += 1
times[u]['f'] = time
for i in range(N):
if times[i]['s'] == 0:
dfs(i)
for i, time in enumerate(times):
print(i + 1, *time.values())
| count = 0
def solve(i, a, ans):
global count
if ans[i][0] == 0:
count += 1
ans[i][0] = count
for x in range(a[i][1]):
if ans[a[i][2 + x] - 1][0] == 0:
solve(a[i][2 + x] - 1, a, ans)
if ans[i][1] == 0:
count += 1
ans[i][1] = count
N = int(input())
a = []
for _ in range(N):
a.append([int(x) for x in input().split()])
ans = [[0 for i in range(2)] for j in range(N)]
for i in range(N):
if ans[i][0] == 0:
solve(i, a, ans)
for i, x in enumerate(ans):
print(i + 1, *x)
| 1 | 3,319,711,040 | null | 8 | 8 |
x, n = map(int, input().split())
li_p = list(map(int, input().split()))
li_p.sort()
i = 0
while True:
a = x - i
b = x + i
if not a in li_p:
print(a)
break
elif a in li_p and not b in li_p:
print(b)
break
elif a in li_p and b in li_p:
i += 1 | from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
n,ta,ao = inpl()
ta -= 1; ao -= 1
g = [[] for i in range(n)]
for i in range(n-1):
a,b = inpl()
g[a-1].append(b-1)
g[b-1].append(a-1)
ta_dist = [None] * n
ta_dist[ta] = 0
ao_dist = [None] * n
ao_dist[ao] = 0
def ta_dfs(node):
for v in g[node]:
if ta_dist[v] != None: continue
ta_dist[v] = ta_dist[node] + 1
ta_dfs(v)
def ao_dfs(node):
for v in g[node]:
if ao_dist[v] != None: continue
ao_dist[v] = ao_dist[node] + 1
ao_dfs(v)
ao_dfs(ao)
ta_dfs(ta)
res = 0
for i in range(n):
if ta_dist[i] > ao_dist[i]: continue
res = max(res, ao_dist[i])
print(res-1) | 0 | null | 65,997,402,244,990 | 128 | 259 |
def f(n):
res = []
for p in range(2, int(n**0.5)):
k = 0
while(n % p == 0):
n //= p
k += 1
if k:
res.append((p, k))
if n != 1:
res.append((n,1))
return res
n = int(input())
ans = 0
r = f(n)
for p,k in r:
tmp = 0
cur = 1
while(k>=cur):
tmp += 1
k -= cur
cur += 1
ans += tmp
print(ans) | n = int(input())
fact={}
i = 2
while n != 1:
if n % i == 0:
n = n/i
if i in fact:
fact[i] += 1
else:
fact[i] = 1
else:
i += 1
if i > (n**(1/2)+1):
if n in fact:
fact[n] += 1
else:
fact[n] = 1
break
if fact == {}:
print(0)
else:
ans = 0
for v in fact.values():
#print(v)
for i in range(1,v+1):
if v - i >= 0:
ans +=1
v = v-i
print(ans) | 1 | 16,944,580,628,320 | null | 136 | 136 |
N = int(input())
A = list(map(int, input().split()))
A.sort()
A.append(N + 1)
ans = [0] * (N+1)
count = [0, 0]
for i in range(N):
if count[0] == A[i]:
count[1] += 1
else:
ans[count[0]] = count[1]
count[0] = A[i]
count[1] = 1
for j in range(1, N+1):
print(ans[j]) | mod = 10**9+7
n,k = map(int,input().split())
ans = 0
for i in range(k,n+2):
ans += (i*n-i*(i-1)+1)%mod
ans %=mod
print(ans) | 0 | null | 32,871,163,459,050 | 169 | 170 |
a,b,c = list(map(int,input().split()))
d = c-a-b
if d <= 0:
print("No")
else:
if d**2 > 4*a*b:
print("Yes")
else:
print("No") | a,b=list(input().split())
c = b+a
print(''.join(c)) | 0 | null | 77,688,336,673,750 | 197 | 248 |
T1,T2,A1,A2,B1,B2=map(int, open(0).read().split())
C1,C2=A1-B1,A2-B2
if C1<0: C1,C2=-C1,-C2
Y1=C1*T1
Y2=Y1+C2*T2
if Y2>0:
print(0)
elif Y2==0:
print("infinity")
else:
print(1+Y1//(-Y2)*2-(Y1%(-Y2)==0)) | import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10
T1, T2 = MAP()
A1, A2 = MAP()
B1, B2 = MAP()
if (A1-B1)*T1 + (A2-B2)*T2 == 0:
print("infinity")
elif 0 < ((A1-B1)*T1) * ((A1-B1)*T1 + (A2-B2)*T2):
print(0)
else:
p = A1*T1-B1*T1
q = 2*A1*T1+A2*T2-2*B1*T1-B2*T2
if p*q < 0:
print(1)
elif p%(p-q):
print(2*(p//(p-q))+ 1)
else:
print(2*(p//(p-q)))
| 1 | 131,849,238,882,242 | null | 269 | 269 |
def main():
k = int(input())
acl = 'ACL'
ans = ''
for i in range(k):
ans += acl
print(ans)
if __name__ == "__main__":
main() | tmp = int(input())
if tmp>=30:
print("Yes")
else:
print("No") | 0 | null | 4,018,165,258,590 | 69 | 95 |
H,W = map(int,input().split())
map = []
INF = 10**8
for _ in range(H):
map.append(input())
dp = [[0 for i in range(W)] for i in range(H)]
for i in range(H):
for j in range(W):
if i == 0 and j == 0:
if map[i][j] == '#':
dp[i][j] = 1
elif i == 0:
flag = 0
if map[i][j] == '#' and map[i][j-1] == '.':
flag = 1
dp[i][j] = flag + dp[i][j-1]
elif j == 0:
flag = 0
if map[i][j] == "#" and map[i-1][j] == ".":
flag = 1
dp[i][j] = flag + dp[i-1][j]
else:
flag1 = 0
flag2 = 0
if map[i][j] == "#" and map[i][j-1] == ".":
flag1 = 1
if map[i][j] == "#" and map[i-1][j] == ".":
flag2 = 1
dp[i][j] = min(dp[i][j-1]+flag1,dp[i-1][j]+flag2)
print(dp[H-1][W-1])
| import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]
#本当に貪欲法か? DP法では??
#本当に貪欲法か? DP法では??
#本当に貪欲法か? DP法では??
h,w=mii()
mat=[list(ss()) for _ in range(h)]
chk=[[0]*w for _ in range(h)]
for i in range(h):
for j in range(w):
if i==0:
if j==0:
if mat[i][j]=="#":
chk[i][j]+=1
else:
if mat[i][j]!=mat[i][j-1] and mat[i][j]=="#":
chk[i][j]=chk[i][j-1]+1
else:
chk[i][j]=chk[i][j-1]
else:
if j==0:
if mat[i][j]!=mat[i-1][j] and mat[i][j]=="#":
chk[i][j]=chk[i-1][j]+1
else:
chk[i][j]=chk[i-1][j]
else:
if mat[i][j]==mat[i-1][j] and mat[i][j]==mat[i][j-1]:
chk[i][j]=min(chk[i-1][j],chk[i][j-1])
elif mat[i][j]!=mat[i-1][j] and mat[i][j]==mat[i][j-1] and mat[i][j]=="#":
chk[i][j]=min(chk[i-1][j]+1,chk[i][j-1])
elif mat[i][j]!=mat[i][j-1] and mat[i][j]==mat[i-1][j] and mat[i][j]=="#":
chk[i][j]=min(chk[i-1][j],1+chk[i][j-1])
elif mat[i][j]=="#":
chk[i][j]=min(chk[i-1][j]+1,1+chk[i][j-1])
else:
chk[i][j]=min(chk[i-1][j],chk[i][j-1])
print(chk[-1][-1])
#print(chk) | 1 | 49,243,985,634,760 | null | 194 | 194 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.