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
|
---|---|---|---|---|---|---|
from math import gcd, sqrt
from functools import reduce
n, *A = map(int, open(0).read().split())
def f(A):
sup = max(A)+1
table = [i for i in range(sup)]
for i in range(2, int(sqrt(sup))+1):
if table[i] == i:
for j in range(i**2, sup, i):
table[j] = i
D = set()
for a in A:
while a != 1:
if a not in D:
D.add(a)
a //= table[a]
else:
return False
return True
if reduce(gcd, A) == 1:
if f(A):
print('pairwise coprime')
else:
print('setwise coprime')
else:
print('not coprime') | FAST_IO = 0
if FAST_IO:
import io, sys, atexit
rr = iter(sys.stdin.read().splitlines()).next
sys.stdout = _OUTPUT_BUFFER = io.BytesIO()
@atexit.register
def write():
sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
else:
rr = raw_input
rri = lambda: int(rr())
rrm = lambda: map(int, rr().split())
rrmm = lambda n: [rrm() for _ in xrange(n)]
####
from collections import defaultdict as ddic, Counter, deque
import heapq, bisect, itertools
MOD = 10 ** 9 + 7
def solve(N, A):
from fractions import gcd
g = A[0]
for x in A:
g= gcd(g,x)
if g > 1:
return "not coprime"
seen = set()
for x in A:
saw = set()
while x % 2 == 0:
x //= 2
saw.add(2)
d = 3
while d*d <= x:
while x%d == 0:
saw.add(d)
x //= d
d += 2
if x > 1:
saw.add(x)
for p in saw:
if p in seen:
return "setwise coprime"
seen |= saw
return "pairwise coprime"
N= rri()
A = rrm()
print solve(N, A)
| 1 | 4,109,376,690,170 | null | 85 | 85 |
n=int(input());
print(10-(n//200)); | N = int(input())
A = list(map(int, input().split()))
result = [0] * N
for a in A:
result[a-1] += 1
for i in range(N):
print(result[i])
| 0 | null | 19,633,796,854,240 | 100 | 169 |
def main():
N, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
v = {0: 1}
n = [0]
r = 0
t = 0
for i, a in enumerate(A, 1):
if i >= K:
v[n[i - K]] -= 1
t += a
j = (t - i) % K
r += v.get(j, 0)
v[j] = v.get(j, 0) + 1
n.append(j)
return r
print(main())
| N = int(input())
A = sorted(list(map(int, input().split())), reverse=True)
ans = 0
cnt = 1
# 最大値は一回だけ
ans += A[0]
# N-2個は、2,2,3,3,..i,i,...のように取る
flag = False
for i in range(1, len(A)-1):
ans += A[cnt]
if flag == False:
flag = True
else:
flag = False
cnt += 1
print(ans) | 0 | null | 73,096,905,594,820 | 273 | 111 |
def main():
n = int(input())
operation = [input().split() for _ in range(n)]
dictionary = {}
for command, char in operation:
if command == "insert":
dictionary[char] = True
elif command == "find":
try:
if dictionary[char]:
print("yes")
except KeyError:
print("no")
return
if __name__ == "__main__":
main()
| ri = [int(v) for v in input().split()]
A, B, C, D = ri
f = 1
while A > 0 and C > 0:
if f:
C -= B
else:
A -= D
f ^= 1
print("Yes" if A > C else "No")
| 0 | null | 14,963,938,244,840 | 23 | 164 |
X, Y = [int(v) for v in input().split()]
ans = "No"
for c in range(X + 1):
t = X - c
if 2 * c + 4 * t == Y:
ans = "Yes"
break
# if ans == "No":
# for t in range(X + 1):
# c = X - t
# if 2 * c + 3 * t == Y:
# ans = "Yes"
# break
print(ans) | x,y = map(int,input().split())
n = int((y-2*x)/2)
m = int((4*x-y)/2)
if x == m+n and m >= 0 and n >= 0:
print('Yes')
else:
print('No')
| 1 | 13,900,752,470,176 | null | 127 | 127 |
def main():
a, b = input().split()
a = int(a)
b = round(float(b) * 100)
print(int(a * b // 100))
if __name__ == "__main__":
main()
| a, b = input().split()
b = b.replace('.', '')
print(int(a)*int(b)//100)
| 1 | 16,651,966,865,400 | null | 135 | 135 |
N, K = map(int, input().split())
p = map(int, input().split())
print(sum(sorted(p)[:K])) | N, K = map(int, input().split())
P = list(map(int, input().split()))
ans = sum(sorted(P)[:K])
print(ans) | 1 | 11,523,646,552,420 | null | 120 | 120 |
import heapq
from sys import stdin
input = stdin.readline
#入力
# s = input()
n = int(input())
# n,m = map(int, input().split())
# a = list(map(int,input().split()))
# a = [int(input()) for i in range()]
st=[]
for i in range(n):
s,t = map(str, input().split())
t = int(t)
st.append((s,t))
x = input()[0:-1]
def main():
ans = 0
flag = False
for i in st:
s,t = i
if flag:
ans+=t
if s == x:
flag = True
print(ans)
if __name__ == '__main__':
main() | # 再帰呼び出しのとき付ける
import sys
sys.setrecursionlimit(10**9)
def readInt():
return list(map(int, input().split()))
n, m = readInt()
root = [-1] * n
def r(x):
# print("root[x]=",root[x])
if root[x] < 0:
# print("r(x)=",x)
return x
else:
root[x] = r(root[x])
# print(root[x])
return root[x]
def unite(x, y):
x = r(x)
y = r(y)
if x == y:
return
root[x] += root[y]
root[y] = x
# print(root)
def size(x):
x = r(x)
# print(x, root[x])
return -root[x]
for i in range(m):
x, y = readInt()
x -= 1
y -= 1
unite(x, y)
# print('size={}'.format(size(i)))
# max_size = 0
# for i in range(n):
# max_size = max(max_size, size(i))
# print(max_size)
# print(root)
ans = -1
for i in root:
if i < 0:
ans += 1
print(ans) | 0 | null | 49,415,661,369,370 | 243 | 70 |
n, m = list(map(int, input().split()))
num = [0 for i in range(n)]
flag = False
for i in range(m):
s, c = list(map(int, input().split()))
if (s == 1) and (c == 0) and (n > 1):
flag = True
elif num[s-1] == 0:
num[s-1] = c
else:
if num[s-1] != c:
flag = True
if flag:
print(-1)
else:
if (n > 1) and (num[0] == 0):
num[0] = 1
print("".join(list(map(str, num)))) | n, m = map(int, input().split())
s = [0] * m
c = [0] * m
for i in range(m):
s[i], c[i] = map(int, input().split())
for i in range(0, 1000):
x = str(i)
if len(x) != n:
continue
for j in range(m):
if s[j] > len(x) or int(x[s[j] - 1]) != c[j]:
break
else:
print(x)
exit()
print(-1)
| 1 | 60,979,580,445,342 | null | 208 | 208 |
N, M = map(int, input().split())
A_list = list(map(int, input().split()))
for i in range(M):
N -= A_list[i]
if N < 0:
print(-1)
break
if i == M-1:
print(N) | class Dice:
def __init__(self, eyes):
self._eyes = eyes
@property
def eye(self):
return self._eyes[1]
def roll(self, direction):
if direction == 'N':
old = self._eyes
new = ['dummy'] * 7
new[1] = old[2]
new[2] = old[6]
new[3] = old[3]
new[4] = old[4]
new[5] = old[1]
new[6] = old[5]
self._eyes = new
elif direction == 'S':
old = self._eyes
new = ['dummy'] * 7
new[1] = old[5]
new[2] = old[1]
new[3] = old[3]
new[4] = old[4]
new[5] = old[6]
new[6] = old[2]
self._eyes = new
elif direction == 'W':
old = self._eyes
new = ['dummy'] * 7
new[1] = old[3]
new[2] = old[2]
new[3] = old[6]
new[4] = old[1]
new[5] = old[5]
new[6] = old[4]
self._eyes = new
elif direction == 'E':
old = self._eyes
new = ['dummy'] * 7
new[1] = old[4]
new[2] = old[2]
new[3] = old[1]
new[4] = old[6]
new[5] = old[5]
new[6] = old[3]
self._eyes = new
else:
raise ValueError('NEWS箱推し')
eyes = ['dummy'] + input().split()
dice = Dice(eyes)
direction_text = input()
for d in direction_text:
dice.roll(d)
print(dice.eye)
| 0 | null | 16,191,297,951,008 | 168 | 33 |
import sys
sys.setrecursionlimit(10**7)
readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))
n = readint()
ans = [[[0],[0]]]
i = 0
while len(ans[i][0])<n:
a = ans[i]
for x in a[1]:
ans.append([a[0]+[x],a[1]])
x = a[1][-1]+1
ans.append([a[0]+[x],a[1] + [x]])
i+=1
a_ = ord('a')
for i in range(len(ans)):
a = ans[i][0]
if len(a)<n:
continue
for x in a[:-1]:
print(chr(x+a_),end='')
print(chr(a[-1]+a_))
| # -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from collections import deque
from functools import lru_cache
import bisect
import re
import queue
import copy
import decimal
class Scanner():
@staticmethod
def int():
return int(sys.stdin.readline().rstrip())
@staticmethod
def string():
return sys.stdin.readline().rstrip()
@staticmethod
def map_int():
return [int(x) for x in Scanner.string().split()]
@staticmethod
def string_list(n):
return [Scanner.string() for i in range(n)]
@staticmethod
def int_list_list(n):
return [Scanner.map_int() for i in range(n)]
@staticmethod
def int_cols_list(n):
return [Scanner.int() for i in range(n)]
def pop_count(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 gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
def lcm(a, b):
return (a * b) // gcd(a, b)
MOD = 10**9 + 7
def solve():
N = Scanner.int()
A = Scanner.map_int()
l = 1
for a in A:
l = lcm(a, l)
ans = 0
for a in A:
ans += l // a
ans %= MOD
print(ans)
def main():
# sys.setrecursionlimit(1000000)
# sys.stdin = open("sample.txt")
# T = Scanner.int()
# for _ in range(T):
# solve()
# print('YNeos'[not solve()::2])
solve()
if __name__ == "__main__":
main()
| 0 | null | 70,008,661,643,130 | 198 | 235 |
n = int(input())
nums = []
ans = ""
while n >= 1:
n -= 1
ans += chr(ord('a') + n % 26)
n = n // 26
print(ans[::-1]) | n = int(input())
ans = ''
while n > 0:
n -= 1
ans += chr(97 + n % 26)
n //= 26
print(ans[::-1])
| 1 | 11,771,255,843,590 | null | 121 | 121 |
a,b,k=[int(i) for i in input().split()]
if(k>a):
k=k-a
a=0
elif(k<=a):
a=a-k
k=0
if(k>b):
k=k-b
b=0
elif(k<=b):
b=b-k
k=0
print(a,b) | 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 = 10**9 + 7
N = 10**6 # N は必要分だけ用意する
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)
x,y=map(int,input().split())
if ((x+y)%3!=0):
print(0)
else:
xx=(2*x-y)//3
yy=(2*y-x)//3
if (xx<0 or yy<0):
print(0)
else:
ans=cmb(xx+yy,xx,p)
print(ans) | 0 | null | 127,491,128,976,028 | 249 | 281 |
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
# d = m - k[i] - k[j]
# if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも!!
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
return list(map(int,input().split()))
def I():
return int(input())
n = input()
if n[-1] in ['2','4','5','7','9']:
print('hon')
elif n[-1] in ['0','1','6','8']:
print('pon')
else:
print('bon')
| N=input()
l=len(N)
if int(N[l-1]) in [2,4,5,7,9]:
print('hon')
elif int(N[l-1]) in [0,1,6,8]:
print('pon')
elif int(N[l-1]) in [3]:
print('bon') | 1 | 19,289,356,384,060 | null | 142 | 142 |
def main():
N, M, X = map(int,input().split())
cost = []
effect = []
for _ in range(N):
t = list(map(int,input().split()))
cost.append(t[0])
effect.append(t[1:])
res = float("inf")
for bits in range(1<<N):
f = True
total = 0
ans = [0] * M
for flag in range(N):
if (1<< flag) & (bits):
total += cost[flag]
for idx, e in enumerate(effect[flag]):
ans[idx] += e
for a in ans:
if a < X: f = False
if f:
res = min(res, total)
print(res if res != float("inf") else -1)
if __name__ == "__main__":
main()
| #!/usr/bin/env python3
import sys
from itertools import product
def input():
return sys.stdin.readline()[:-1]
def main():
N, M, X = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]
for m in range(1, M + 1):
s = 0
for n in range(N):
s += A[n][m]
if s < X:
print(-1)
exit()
# bit全探索
# repeat=2だと(0,0), (0,1), (1,0), (1,1)になる
ans = 10 ** 15
for i in product([0,1], repeat=N):
c = 0
for m in range(1, M + 1):
s = 0
for n in range(N):
if i[n] == 1:
s += A[n][m]
if s >= X:
c += 1
if c == M:
money = 0
for n in range(N):
if i[n] == 1:
money += A[n][0]
ans = min(ans, money)
print(ans)
if __name__ == '__main__':
main()
| 1 | 22,240,799,744,240 | null | 149 | 149 |
#!/usr/bin/env python3
N = int(input().split()[0])
xlt_list = []
for _ in range(N):
x, l = list(map(int, input().split()))
xlt_list.append((x, l, x + l))
xlt_list = sorted(xlt_list, key=lambda x: x[2])
count = 0
before_t = -float("inf")
for i, xlt in enumerate(xlt_list):
x, l, t = xlt
if x - l >= before_t:
count += 1
before_t = t
ans = count
print(ans)
| # 多次元リストの sort に使える
from operator import itemgetter
n = int(input())
x = [0] * n
l = [0] * n
for i in range(n):
x[i], l[i] = map(int, input().split())
st = sorted([(x[i] - l[i], x[i] + l[i]) for i in range(n)], key = itemgetter(1))
ans = 0
# 最後に選んだ仕事の終了時間
last = - 10 ** 10
for i in range(n):
if last <= st[i][0]:
ans += 1
last = st[i][1]
print(ans) | 1 | 89,697,230,019,930 | null | 237 | 237 |
import numpy as np
N, M, X = map(int,input().split())
C = []
A = []
for i in range(N):
c = 0
a = []
c, *a = map(int,input().split())
C.append(c)
A.append(a)
inf = float("inf")
res = inf
for bit in range(1 << N):
Cost = 0
Wise = [0] * M
for i in range(N):
if (bit >> i) & 1:# i冊目を買うときの処理
Cost += C[i]
for j, a in enumerate(A[i]):
Wise[j] += a
#else:# i冊目を買わないときの処理(今回は何もしないので省略)
if min(Wise) >= X and Cost <= res:
res = Cost
if res == inf:
print("-1")
else:
print(res)
| n,m,x=map(int,input().split())
ca=[]
ans=10**9
for i in range(n):
tmp=list(map(int,input().split()))
ca.append(tmp)
for p in range(2**n):
i_bin = (bin(p)[2:]).zfill(n)
fl=1
for i in range(1,m+1):
ttl=sum([int(i_bin[k])*ca[k][i] for k in range(n)])
if ttl<x: fl=0
ansc=sum([int(i_bin[k])*ca[k][0] for k in range(n)])
if fl and ansc<ans:
ans=ansc
if ans==10**9: print(-1)
else: print(ans) | 1 | 22,119,528,923,840 | null | 149 | 149 |
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
ans = [0] * N
for a_i in A:
ans[a_i-1] += 1
for a in ans:
print(a) | N = int(input())
A = list(map(int,input().split()))
buka = [0]*N
for p in range(N-1):
buka[A[p] -1] += 1
for q in range(N):
print(buka[q]) | 1 | 32,584,816,839,910 | null | 169 | 169 |
import sys
from collections import defaultdict
input = lambda: sys.stdin.readline().rstrip()
mod = 10**9 + 7
class Factorize(object):
def __init__(self, maxnum):
self.primes = self.__smallest_prime_factors(maxnum)
# 素因数分解する(リスト)
def factorize_list(self, n):
fct = []
while n != 1:
fct.append(self.primes[n])
n //= self.primes[n]
return fct
# 素因数分解する(辞書)
def factorize_dict(self, n):
fct = defaultdict(lambda: 0)
while n != 1:
fct[self.primes[n]] += 1
n //= self.primes[n]
return fct
# n以下の最小の素因数を列挙する
def __smallest_prime_factors(self, n):
prime = list(range(n + 1))
for i in range(2, int(n**0.5) + 1):
if prime[i] != i:
continue
for j in range(i * 2, n + 1, i):
prime[j] = min(prime[j], i)
return prime
def divmod(x, mod=10**9 + 7):
return pow(x, mod - 2, mod)
def solve():
N = int(input())
A = list(map(int, input().split()))
fct = Factorize(max(A))
lcm_dict = defaultdict(lambda: 0)
for i in range(N):
d = fct.factorize_dict(A[i])
for k, v in d.items():
lcm_dict[k] = max(lcm_dict[k], v)
lcm = 1
for k, v in lcm_dict.items():
lcm *= (k**v)
lcm %= mod
ans = 0
for i in range(N):
ans += divmod(A[i])
ans %= mod
ans *= lcm
ans %= mod
print(ans)
if __name__ == '__main__':
solve()
| def gcd(a, b):
if (a == 0):
return b
return gcd(b%a, a)
def lcm(a, b):
return (a*b)//gcd(a, b)
n = int(input())
numbers = list(map(int, input().split()))
l = numbers[0]
for i in range(1, n):
l = lcm(l, numbers[i])
ans = 0
for i in range(n):
ans += l//numbers[i]
MOD = 1000000007
print(ans%MOD)
| 1 | 87,701,594,268,108 | null | 235 | 235 |
def main():
H,W,M = map(int,input().split())
dict_ichi = {}
dict_H = {key:0 for key in range(1,H+1,1)}
dict_W = {key:0 for key in range(1,W+1,1)}
for i in range(M):
h,w = map(int,input().split())
dict_H[h] += 1
dict_W[w] += 1
dict_ichi[(h,w)] = 1
#print(list)
maxH = max(dict_H.values())
maxW = max(dict_W.values())
wi = [i for i in range(1,W+1,1) if dict_W[i] == maxW]
#print(retuH,gyoW,wi)
for h in [i for i in range(1,H+1,1) if dict_H[i] == maxH]:
for w in wi:
if (h,w) not in dict_ichi:
return maxH + maxW
return maxH + maxW -1
print(main())
| h,w,m = map(int,input().split())
bombX = [0]*h
bombY = [0]*w
bomb = set()
maxX = maxY = 0
for i in range(m):
i,j = map(lambda x:int(x)-1, input().split())
bomb.add((i,j))
bombX[i] += 1
bombY[j] += 1
maxX = max(maxX,bombX[i])
maxY = max(maxY,bombY[j])
maxX_index = list(i for i in range(h) if bombX[i] == maxX)
maxY_index = list(i for i in range(w) if bombY[i] == maxY)
for i in maxX_index:
for j in maxY_index:
if (i,j) in bomb: continue
print(maxX+maxY)
exit()
print(maxX+maxY-1)
| 1 | 4,750,073,195,308 | null | 89 | 89 |
N, M = map(int, input().split())
a = 1
b = N
d = set([1, N - 1])
for _ in range(M):
print(a, b)
a += 1
b -= 1
while (b - a) in d or (N - b + a) in d or (b - a) == (N - b + a): b -= 1
d.add(b - a)
d.add(N - b + a)
| #!/usr/bin/env python3
def next_line():
return input()
def next_int():
return int(input())
def next_int_array_one_line():
return list(map(int, input().split()))
def next_int_array_multi_lines(size):
return [int(input()) for _ in range(size)]
def next_str_array(size):
return [input() for _ in range(size)]
def main():
r, c, k = map(int, input().split())
dp = [[[0] * (c+1) for i in range(r+1)] for i in range(4)]
v = [[0] * (c+1) for i in range(r+1)]
for i in range(k):
row, col, val = map(int, input().split())
v[row][col] = val
for row in range(1, r + 1):
for col in range(1, c + 1):
for num in range(0, 4):
if num == 0:
dp[num][row][col] = dp[3][row-1][col] # simple down
else:
dp[num][row][col] = max(dp[num-1][row][col], # str
dp[num][row][col-1], # right x
dp[3][row-1][col] + \
v[row][col], # down o
dp[num-1][row][col-1] + \
v[row][col], # right o
)
print(dp[3][r][c])
if __name__ == '__main__':
main()
| 0 | null | 17,151,957,816,412 | 162 | 94 |
from itertools import accumulate
from collections import Counter
N,K,*A = map(int, open(0).read().split())
A = [a-1 for a in A]
B = [b%K for b in accumulate(A)]
if K>N:
Cnt = Counter(B)
ans = Cnt[0]+sum(v*(v-1)//2 for v in Cnt.values())
else:
Cnt = Counter(B[:K])
ans = Cnt[0]+sum(v*(v-1)//2 for v in Cnt.values())
if B[K-1]==0:
ans -= 1
for i in range(N-K):
Cnt[B[i]] -= 1
ans += Cnt[B[i+K]]
Cnt[B[i+K]] += 1
print(ans) | import fileinput
n, r = map(int, input().split())
if n >= 10:
print(r)
else:
rr = r + 100 * (10 - n)
print(rr)
| 0 | null | 100,444,009,461,978 | 273 | 211 |
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
N, R = map(int, readline().split())
ans = R
if (N<10):
ans += 100*(10-N)
print(ans) | N, R = list(map(int, input().split()))
if N >= 10:
print(R)
else:
ans = 100 * (10 - N) + R
print(ans) | 1 | 63,511,739,904,260 | null | 211 | 211 |
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()
| #!/usr/bin/env python3
import sys
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def main():
N = INT()
A = LI()
RGB = [0,0,0]
answer = 1
MOD = 10**9+7
for i in range(N):
if RGB.count(A[i]) == 0:
print(0)
return
answer *= RGB.count(A[i])
answer %= MOD
for j in range(3):
if RGB[j] == A[i]:
RGB[j] += 1
break
print(answer)
return
if __name__ == '__main__':
main()
| 1 | 129,716,430,948,212 | null | 268 | 268 |
d=raw_input().split(" ")
l=list(map(int,d))
if(l[2]>0 and l[3]>0):
if(l[0]>=(l[2]+l[4])):
if(l[1]>=(l[3]+l[4])):
print "Yes"
else:
print "No"
else:
print "No"
else:
print "No"
| nums = [int(e) for e in input().split()]
if (nums[2]+nums[4])<=nums[0] and (nums[3]+nums[4])<=nums[1] and (nums[2]-nums[4])>=0 and (nums[3]-nums[4])>=0:
print("Yes")
else:
print("No")
| 1 | 441,907,526,588 | null | 41 | 41 |
import sys
import copy
import math
import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy
from collections import deque
def lcm(x, y):
return (x * y) // math.gcd(x, y)
if __name__ == '__main__':
s = input()
s = s.replace("hi","")
if s =="":
print("Yes")
else:
print("No")
| from decimal import Decimal
a, b = input().split()
a = Decimal(a)
b = Decimal(b)
print(int(a*b)) | 0 | null | 34,768,878,331,872 | 199 | 135 |
import sys
sys.setrecursionlimit(10000000)
MOD = 998244353
INF = 10 ** 15
def main():
N,S = map(int,input().split())
A = list(map(int,input().split()))
dp = [[0]*(1 + S) for _ in range(N + 1)]
dp[0][0] = pow(2,N,MOD)
inv2 = pow(2,MOD - 2,MOD)
for i,a in enumerate(A):
for j in range(S + 1):
if j < a:
dp[i + 1][j] = dp[i][j]
else:
dp[i + 1][j] = (dp[i][j] + dp[i][j - a] * inv2)%MOD
print(dp[N][S])
if __name__ == '__main__':
main() | import sys
input = sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def main():
mod=998244353
N,S=MI()
A=LI()
dp=[[0]*(S+1) for _ in range(N+1)]
#dp[i][j]はi番目までで,和がjになるのが何個作れるか.
#ただし,その数を使わない場合,選択するかしないか選べるのでダブルでカウント
dp[0][0]=1
for i in range(N):
#dp[i][0]+=1
for j in range(S+1):
dp[i+1][j]+=dp[i][j]*2
dp[i+1][j]%=mod
if j+A[i]<=S:
dp[i+1][j+A[i]]+=dp[i][j]
dp[i+1][j+A[i]]%=mod
print(dp[-1][-1])
main()
| 1 | 17,794,821,200,432 | null | 138 | 138 |
import itertools
import math
s, w = map(int, input().split())
if s <= w:
print("unsafe")
else:
print("safe") | import sys
i=1
for s in sys.stdin:
n = int(s)
if n == 0:
break
print("Case ",i,": ",n,sep="")
i += 1 | 0 | null | 14,755,794,564,632 | 163 | 42 |
while True:
s = raw_input().split()
if '?' == s[1]:
break
if '+' == s[1]:
print int(s[0])+int(s[2])
if '-' == s[1]:
print int(s[0])-int(s[2])
if '*' == s[1]:
print int(s[0])*int(s[2])
if '/' == s[1]:
print int(s[0])/int(s[2]) | # coding:utf-8
# ??\???
n,m = map(int, input().split())
matrix = []
for i in range(n):
matrix.append([0] * m)
for i in range(n):
col = input().split()
for j in range(m):
matrix[i][j] = int(col[j])
vector = [0] * m
for j in range(m):
vector[j] = int(input())
for i in range(n):
row_sum = 0
for j in range(m):
row_sum += matrix[i][j] * vector[j]
print(row_sum) | 0 | null | 921,769,273,380 | 47 | 56 |
# coding: utf-8
# Your code here!
x = input()
lst = list(x)
pt = []
rst = []
k = 0
# point get
cnt = 0
tmp = ""
for s in lst:
if s == "/":
if tmp == "_" or tmp == "/":
cnt += 1
elif s == "\\":
if tmp == "\\":
cnt -= 1
else:
if tmp == "\\":
cnt -= 1
pt += [cnt]
tmp = s
#メンセキ
i,xs,m2 = 0,0,0
for s in lst:
if s == "/":
if i == xs and xs > 0:
rst += [m2]
k += 1
xs,m2 = 0,0
elif s == "_":
pass
else:
try:
x = pt[i+1:].index(pt[i])+i+1
m2 += x-i
if xs < x:
xs = x
except:
pass
i += 1
print(sum(rst))
print(k,*rst)
| # -*-coding:utf-8
import math
def main():
a, b, degree = map(int, input().split())
radian = math.radians(degree)
S = 0.5*a*b*math.sin(radian)
x = a + b + math.sqrt(pow(a,2)+pow(b,2)-2*a*b*math.cos(radian))
print('%.8f' % S)
print('%.8f' % x)
print('%.8f' % ((2*S)/a))
if __name__ == '__main__':
main() | 0 | null | 114,243,830,610 | 21 | 30 |
N,D = map(int,input().split())
A=[[int(i) for i in input().split()] for _ in range(N)]
ans=int()
for i in range(N):
if A[i][0]*A[i][0]+A[i][1]*A[i][1]<=D*D:
ans+=1
print(ans)
| n, d = [int(s) for s in input().split()]
d2 = d ** 2
ans = 0
for _ in range(n):
x, y = [int(s) for s in input().split()]
if x ** 2 + y ** 2 <= d2:
ans += 1
print(ans) | 1 | 5,911,462,484,900 | null | 96 | 96 |
n = int(input())
A = list(map(int,input().split()))
smallest = A[0]
ans = 0
for x in range(len(A)-1):
if A[x] > A[x+1]:
ans += A[x] - A[x+1]
A[x+1] = A[x]
print(ans)
| import sys
def input(): return sys.stdin.readline().rstrip()
def main():
n = int(input())
A = list(map(int,input().split()))
high = 0
ans = 0
for a in A:
if high > a:
ans += high - a
elif high < a:
high = a
print(ans)
if __name__=='__main__':
main() | 1 | 4,547,899,963,900 | null | 88 | 88 |
d, t, s = map(int, input().split())
ans = "Yes" if d / s <= t else "No"
print(ans) | while True:
try:
a, b = map(int, raw_input().split())
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a%b)
g = gcd(a,b)
l = a*b/g
print g, l
except:
break | 0 | null | 1,759,069,846,582 | 81 | 5 |
def BSort(A,n):
flag=1
while flag:
flag=0
for i in range(n-1,0,-1):
if A[i][1]<A[i-1][1]:
A[i],A[i-1]=A[i-1],A[i]
flag=1
def SSort(A,n):
for i in range(n):
minj=i
for j in range(i,n):
if A[j][1]<A[minj][1]:
minj=j
A[i],A[minj]=A[minj],A[i]
n = int(input())
A = list(input().split())
B=A[:]
BSort(A,n)
print(' '.join(A))
print('Stable')
SSort(B,n)
print(' '.join(B))
if A==B:
print('Stable')
else:
print('Not stable')
| import sys
ERROR_INPUT = 'input is invalid'
def main():
n = get_length()
arr = get_array(length=n)
bubbleLi = bubbleSort(li=arr.copy(), length=n)
selectionLi = selectionSort(li=arr.copy(), length=n)
print(*list(map(lambda c: c.card, bubbleLi)))
print(checkStable(inp=arr, out=bubbleLi))
print(*list(map(lambda c: c.card, selectionLi)))
print(checkStable(inp=arr, out=selectionLi))
return 0
def get_length():
n = int(input())
if n < 1 or n > 36:
print(ERROR_INPUT)
sys.exit(1)
else:
return n
def get_array(length):
nums = input().split(' ')
return [Card(n) for n in nums]
def bubbleSort(li, length):
for n in range(length):
for n in range(1, length - n):
if li[n].num < li[n - 1].num:
li[n], li[n - 1] = li[n - 1], li[n]
return li
def selectionSort(li, length):
for i in range(0, length - 1):
min_index = i
for j in range(i, length):
if li[j].num < li[min_index].num:
min_index = j
if i != min_index:
li[i], li[min_index] = li[min_index], li[i]
return li
def checkStable(inp, out):
small_num = out[0].num
for n in range(1, len(out)):
if out[n].num == small_num:
if inp.index(out[n]) < inp.index(out[n - 1]):
return 'Not stable'
elif small_num < out[n].num:
small_num = out[n].num
continue
return 'Stable'
class Card:
def __init__(self, disp):
li = list(disp)
self.card = disp
self.chara = li[0]
self.num = int(li[1])
return
main() | 1 | 25,738,009,638 | null | 16 | 16 |
from collections import Counter
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
result = []
for i in range(K):
result.append(T[i])
for j in range(K, N):
if result[j-K] == T[j]:
result.append("d")
else:
result.append(T[j])
result = Counter(result)
print(result["r"]*P+result["s"]*R+result["p"]*S)
|
N,K=map(int,input().split())
r,s,p=map(int,input().split())
S=input()
li=[1]*(K)
ans=[0,0,0]
for i in range(N-K):
if S[i]==S[i+K]:
li[i%K]+=1
else:
if S[i]=="r":
ans[0]+=(li[i%K]+1)//2
if S[i]=="s":
ans[1]+=(li[i%K]+1)//2
if S[i]=="p":
ans[2]+=(li[i%K]+1)//2
li[i%K]=1
for j in range(N-K,N):
if S[j]=="r":
ans[0]+=(li[j%K]+1)//2
if S[j]=="s":
ans[1]+=(li[j%K]+1)//2
if S[j]=="p":
ans[2]+=(li[j%K]+1)//2
print(p*ans[0]+r*ans[1]+s*ans[2])
| 1 | 106,859,544,883,520 | null | 251 | 251 |
N = (list(input()))
N =N[::-1]
N_int = [int(i) for i in N]
N_int.append(0)
maisu = 0
keta = False
for i in range(len(N_int)-1):
if keta ==True:
N_int[i] +=1
if N_int[i]<5:
maisu +=N_int[i]
keta =False
elif N_int[i]==5 :
if N_int[i+1]>4:
keta =True
maisu +=(10-N_int[i])
else:
keta =False
maisu += N_int[i]
else:
keta =True
maisu +=(10-N_int[i])
if keta ==True:
maisu +=1
print(maisu) | n = input()[::-1]
dp = [[0, 0] for i in range(len(n) + 1)]
dp[0][1] = 1
for i in range(len(n)):
dp[i + 1][0] = min(dp[i][0] + int(n[i]), dp[i][1] - int(n[i]) + 10)
dp[i + 1][1] = min(dp[i][0] + int(n[i]) + 1, dp[i][1] - int(n[i]) + 9)
print(dp[len(n)][0]) | 1 | 70,772,169,904,512 | null | 219 | 219 |
R = int(input())
print(int(R*R)) | def main():
r = int(input())
print(r**2)
if __name__ == "__main__":
main() | 1 | 145,739,287,587,310 | null | 278 | 278 |
def merge(targ,first,mid,last):
left = targ[first:mid] + [10 ** 9 + 1]
right = targ[mid:last] + [10 ** 9 + 1]
leftcnt = rightcnt = 0
global ans
for i in range(first,last):
ans += 1
#print(left,right,left[leftcnt],right[rightcnt],targ,ans)
if left[leftcnt] <= right[rightcnt]:
targ[i] = left[leftcnt]
leftcnt += 1
else:
targ[i] = right[rightcnt]
rightcnt += 1
def mergesort(targ,first,last):
if first +1 >= last:
pass
else:
mid = (first + last) // 2
mergesort(targ,first,mid)
mergesort(targ,mid,last)
merge(targ,first,mid,last)
ans = 0
num = int(input())
targ = [int(n) for n in input().split(' ')]
mergesort(targ,0,num)
print(" ".join([str(n) for n in targ]))
print(ans) | def merge(A, l, m, r):
L = A[l:m] + [SENTINEL]
R = A[m:r] + [SENTINEL]
i = 0
j = 0
for k in range(l, r):
if L[i] <= R[j]:
A[k] = L[i]
i += 1
else:
A[k] = R[j]
j += 1
global count
count += r - l
def merge_sort(A, l, r):
if l + 1 < r:
m = (l + r) // 2
merge_sort(A, l, m)
merge_sort(A, m, r)
merge(A, l, m, r)
SENTINEL = float('inf')
n = int(input())
A = list(map(int, input().split()))
count = 0
merge_sort(A, 0, len(A))
print(" ".join(map(str, A)))
print(count) | 1 | 111,092,693,218 | null | 26 | 26 |
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
S = input().rstrip()
A = [0]*N
cnt = 0
S = reversed(S)
for i, s in enumerate(S):
if s=="1":
cnt += 1
A[i] = cnt
else:
cnt = 0
dp = 0
res = []
while dp+M <= N-1:
t = M - A[dp+M]
if t>0:
dp += t
else:
print(-1)
exit()
res.append(t)
res.append(N-dp)
res.reverse()
print(*res) | import math
while True:
n = int(input())
if n == 0: break
d = list(map(int, input().strip().split()))
m = sum(d) / n
print(math.sqrt(sum((x - m)**2 for x in d) / n)) | 0 | null | 69,642,726,521,500 | 274 | 31 |
# import time
N,P = list(map(int,input().split()))
S = input()
if P == 2:
c = 0
for i in range(N):
u = int(S[i])
if u % 2 == 0:
c += i+1
print(c)
elif P == 5:
c = 0
for i in range(N):
u = int(S[i])
if u % 5 == 0:
c += i+1
print(c)
else:
U = [0]
a = 0
t = 1
for i in range(N):
t %= P
a += int(S[-i-1])*t
a %= P
t *= 10
U.append(a)
U.append(10001)
# print(U)
# t3 = time.time()
c = 0
"""
for i in range(P):
m = U.count(i)
c += m*(m-1)//2
print(c)
"""
U.sort()
# print(U)
idx = 0
q = -1
for i in range(N+2):
if q != U[i]:
q = U[i]
m = i - idx
idx = i
c += m*(m-1)//2
print(c)
# t4 = time.time()
# print(t4-t3)
# 10032 | # coding: utf-8
N,P = list(map(int, input().split()))
S = '0'+input()
#N=200000
#P=9893
#S= '0'+'1234567890' * 20000
if (P==2)|(P==5):
sum=0
for i in range(N, 0, -1):
if int(S[i]) % P == 0:
sum+= i
print(sum)
else:
dp1 = [0] * (N+1)
dp2 = [0] * (N+1)
dp3 = [0] * P
dp1[N] = int(S[N]) % P
dp3[int(dp1[N])] += 1
k = 1
for i in range(N - 1, 0, -1):
dp1[i] = (dp1[i + 1] + (int(S[i]) * 10 * k)) % P
dp3[int(dp1[i])] += 1
k = (k * 10) % P
sum = 0
for i in range(P):
if i == 0:
sum += (dp3[i] * (dp3[i] - 1)) // 2 + dp3[i]
continue
if dp3[i] == 0: continue
sum += (dp3[i] * (dp3[i] - 1)) // 2
print(int(sum))
| 1 | 58,339,622,564,988 | null | 205 | 205 |
l, r, d = map(int, input().split())
num = 0
for i in range(r - l +1):
if (i + l) / d == int((i + l) / d):
num += 1
print(num)
| def readInt():
return list(map(int, input().split()))
n = int(input())
a = readInt()
b = 0
ans = 0
for i in a:
b += i ** 2
# print(sum(a))
ans = ((sum(a) ** 2 - b) // 2) % (10 ** 9 + 7)
print(ans)
| 0 | null | 5,746,696,761,468 | 104 | 83 |
n = str(input())
ns = n.swapcase()
print(ns)
| if __name__ == "__main__":
input_word = input()
print(input_word.swapcase())
| 1 | 1,512,868,065,992 | null | 61 | 61 |
import sys
# 整数の入力
z = list(map(int, input().split()))
N = z[0]
R = z[1]
naibu = 0
if 10 <= N:
naibu = R
else:
naibu = R + (100 * (10 - N))
print(naibu) | #K = input()
NR = list(map(int, input().split()))
#s = input().split()
#N = int(input())
if (NR[0] >= 10):
print(NR[1])
else:
print(NR[1] + 100 * (10 - NR[0])) | 1 | 63,244,047,783,280 | null | 211 | 211 |
n = int(input())
str = ""
while 1:
n, mod = divmod(n, 26)
if mod == 0:
str = 'z' + str
n -=1
else:
str = chr(96+mod) + str
if n == 0:
break
print(str) | from bisect import bisect_right as br
import sys
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MergeSort(aa, left=0, right=-1):
def merge(aa, left2, mid2, right2):
res=0
inf = float("inf")
L = aa[left2:mid2] + [inf]
R = aa[mid2:right2] + [inf]
Li = Ri = 0
for ai in range(left2, right2):
res+=1
if L[Li] < R[Ri]:
aa[ai] = L[Li]
Li += 1
else:
aa[ai] = R[Ri]
Ri += 1
return res
res=0
if right == -1: right = len(aa)
if left + 1 < right:
mid = (left + right) // 2
res+=MergeSort(aa, left, mid)
res+=MergeSort(aa, mid, right)
res+=merge(aa, left, mid, right)
return res
def main():
n=int(input())
aa=list(map(int, input().split()))
cnt=MergeSort(aa)
print(*aa)
print(cnt)
main()
| 0 | null | 5,965,184,527,902 | 121 | 26 |
# coding: utf-8
import sys
strings = input()
for s in strings:
if s.islower():
sys.stdout.write(s.upper())
else:
sys.stdout.write(s.lower())
print() | N = int(input())
A = list(map(int, input().split()))
ans = 0
B = []
C = []
maA = max(A) + N -1
for k in range(N):
if A[k] + k <= maA:
B.append(A[k]+k)
if -A[k] + k >0:
C.append(-A[k]+k)
B.sort()
C.sort()
c = 0
b = 0
while b < len(B) and c < len(C):
if B[b] == C[c]:
s = 0
t = 0
j = float('inf')
k = float('inf')
for j in range(b, len(B)):
if B[b] == B[j]:
s += 1
else:
break
for k in range(c, len(C)):
if C[c] == C[k]:
t +=1
else:
break
ans += s*t
b = j
c = k
continue
elif B[b] > C[c]:
c += 1
else:
b += 1
#print(B)
#print(C)
print(ans)
| 0 | null | 13,775,392,485,884 | 61 | 157 |
n,m = list(map(int,input().split()))
A = [map(int,input().split()) for i in range(n)]
b = [int(input()) for i in range(m)]
for a in A:
print(sum([x*y for (x,y) in zip(a,b)])) | N = int(input())
P = list(map(int, input().split()))
min = P[0]
ans = 0
for i in range(N):
if i == 0:
ans += 1
elif min > P[i]:
ans += 1
min = P[i]
print(ans) | 0 | null | 43,561,846,950,752 | 56 | 233 |
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
# from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint
from statistics import median
INF = float("inf")
def mycol(data,col):
return [ row[col] for row in data ]
def mysort(data,col,reverse_flag):
data.sort(key=lambda x:x[col],reverse=reverse_flag)
return data
def mymax(data):
M = -1*float("inf")
for i in range(len(data)):
m = max(data[i])
M = max(M,m)
return M
def mymin(data):
m = float("inf")
for i in range(len(data)):
M = min(data[i])
m = min(m,M)
return m
def mycount(ls,x):
# lsはソート済みであること
l = bisect_left(ls,x)
r = bisect_right(ls,x)
return (r-l)
def myoutput(ls,space=True):
if space:
if len(ls)==0:
print(" ")
elif type(ls[0])==str:
print(" ".join(ls))
elif type(ls[0])==int:
print(" ".join(map(str,ls)))
else:
print("Output Error")
else:
if len(ls)==0:
print("")
elif type(ls[0])==str:
print("".join(ls))
elif type(ls[0])==int:
print("".join(map(str,ls)))
else:
print("Output Error")
def I():
return int(input())
def MI():
return map(int,input().split())
def RI():
return list(map(int,input().split()))
def CI(n):
return [ int(input()) for _ in range(n) ]
def LI(n):
return [ list(map(int,input().split())) for _ in range(n) ]
def S():
return input()
def MS():
return input().split()
def RS():
return list(input().split())
def CS(n):
return [ input() for _ in range(n) ]
def LS(n):
return [ list(input().split()) for _ in range(n) ]
n = I()
ab = LI(n)
a = mycol(ab,0)
ma = median(a)
b = mycol(ab,1)
mb = median(b)
if n%2==1:
ans = mb - ma + 1
else:
ans = (mb-ma)*2 + 1
print(int(ans)) | #import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
#import heapq
#from fractions import gcd
#input=sys.stdin.readline
import bisect
n,k=map(int,input().split())
Mod=10**9+7
M=2*10**5 +1#書き換え
fac=[0]*M
finv=[0]*M
inv=[0]*M
#finvに逆元、facに階乗のmod
def COMinit():
fac[0]=fac[1]=1
finv[0]=finv[1]=1
inv[1]=1
for i in range(2,M):
fac[i]=(fac[i-1]*i%Mod)%Mod
inv[i]=Mod-inv[Mod%i]*(Mod//i) %Mod
finv[i]=(finv[i-1]*inv[i]%Mod)%Mod
def COM(n,k):
if n<k:
return 0
if n<0 or k<0:
return 0
return (fac[n]*(finv[k]*finv[n-k]%Mod)%Mod)%Mod
COMinit()
if n>k:
ans=1
for i in range(1,k+1):
ans+=(COM(n,i)*COM(n-1,i))%Mod
ans%=Mod
else:
ans=1
for i in range(1,n):
ans+=(COM(n,i)*COM(n-1,i))%Mod
ans%=Mod
print(ans) | 0 | null | 42,129,232,728,390 | 137 | 215 |
import math
while True:
n = int(input())
if(n == 0):
exit()
else:
s = [int(x) for x in input().split()]
m = sum(s)/len(s)
for i in range(n):
s [i] = (s[i] - m)**2
print("%.5f" % (math.sqrt(sum(s)/len(s)))) | def judge99(x):
if x <= 9:
return True
else:
return False
a, b = map(int,input().split())
if judge99(a) and judge99(b):
print(a*b)
else:
print(-1)
| 0 | null | 79,527,978,456,862 | 31 | 286 |
import itertools
import numpy as np
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for i in range(Q)]
score = []
ans = 0
for A in itertools.combinations_with_replacement(range(1, M+1), N):
for i in range(Q):
a = abcd[i][0]
b = abcd[i][1]
c = abcd[i][2]
d = abcd[i][3]
if A[b-1]-A[a-1] == c:
score.append(d)
if ans <= sum(score):
ans = sum(score)
score.clear()
print(ans) | N,M,Q = map(int, input().split())
a = [0]*Q
b = [0]*Q
c = [0]*Q
d = [0]*Q
for i in range(Q):
a[i], b[i], c[i], d[i] = map(int, input().split())
def dfs(n_list, o, l, L):
if o+l==0:
num = 1
pos = 0
L_child = [0]*N
for i in range(N+M-1):
if n_list[i]==1:
num += 1
else:
L_child[pos] = num
pos += 1
L.append(L_child)
if o>=1:
n_list[N+M-o-l-1] = 0
dfs(n_list, o-1,l, L)
if l>=1:
n_list[N+M-o-l-1] = 1
dfs(n_list, o, l-1, L)
A = [0]*(N+M-1)
L =[]
dfs(A,N,M-1,L)
ans = 0
for i in L:
score = 0
for j in range(Q):
if i[b[j]-1]-i[a[j]-1]==c[j]:
score += d[j]
if score > ans:
ans = score
print(ans) | 1 | 27,610,015,862,716 | null | 160 | 160 |
N = int(input())
A = list(map(int,input().split()))
b = 1
c = 0
if A.count(0) != 0:
print(0)
else:
for i in range(N):
b = b * A[i]
if b > 10 ** 18:
c = 1
break
if c == 0:
print(b)
else:
print(-1) | N = int(input())
Alist = list(map(int,input().split()))
Answer = 1
if 0 in Alist:
Answer = 0
for i in range(N):
Answer *= Alist[i]
if Answer > 10**18:
Answer = -1
break
print(Answer) | 1 | 16,081,861,927,572 | null | 134 | 134 |
N,M=map(int,input().split())
x=[-1]*N
for _ in range(M):
s,c=map(int,input().split())
s-=1
if (s==c==0 and N>1 or
x[s]!=-1 and x[s]!=c):
print(-1)
exit()
x[s]=c
if x[0]==-1:
x[0]=0+(N>1)
for i in range(N):
if x[i]==-1: x[i]=0
print(''.join(map(str,x)))
| def main():
n, m = map(int, input().split())
s = [0] * m
c = [0] * m
for i in range(m):
s[i], c[i] = map(int, input().split())
ans = list()
for i in range(n):
ans.append(0)
for i in range(m):
if n != 1 and s[i] == 1 and c[i] == 0:
print(-1)
exit()
if ans[s[i]-1] == 0 or ans[s[i]-1] == c[i]:
ans[s[i]-1] = c[i]
elif ans[s[i]-1] != 0 and ans[s[i]-1] != c[i]:
print(-1)
exit()
if n != 1:
if ans[0] == 0:
ans[0] = 1
print(*ans, sep='')
else:
print(*ans, sep='')
if __name__ == '__main__':
main() | 1 | 60,541,410,794,478 | null | 208 | 208 |
a=int(input())
b=(a)//2+1
ans=0
for i in range(1,b,1):
x=a//i
ans+=((x**2+x)//2)*i
ans+=(a**2+a+b-b**2)//2
print(ans) | N = int(input())
tree = [[] for _ in range(N)]
for _ in range(N):
u, k, *v = map(int, input().split())
tree[u-1] = v
ans = [float('inf') for _ in range(N)]
ans[0] = 0
from collections import deque
q = deque([(0, 0)])
while q:
p, d = q.popleft()
for c in tree[p]:
if ans[c-1] > d+1:
ans[c-1] = d+1
q.append((c-1, d+1))
for i in range(N):
if ans[i] == float('inf'):
print(i+1, -1)
else:
print(i+1, ans[i])
| 0 | null | 5,562,214,317,782 | 118 | 9 |
n, k = map(int, input().split())
ps = []
MOD = 998244353
s = set()
for _ in range(k):
l,r = map(int, input().split())
ps.append([l,r])
dp = [0] * (n+1)
dp[0] = 1
acc = [0,1]
for i in range(1,n):
for l, r in ps:
dp[i] += acc[max(0,i-l+1)] - acc[max(0,i-r)]
acc.append((acc[i] + dp[i])%MOD)
print(dp[n-1]%MOD)
| #!/usr/bin/env python3
def main():
N= int(input())
ans = (N+1)//2/N
print(ans)
if __name__ == "__main__":
main()
| 0 | null | 89,832,738,869,222 | 74 | 297 |
D = int(input())
c = list(map(int, input().split()))
s = []
c_sum = sum(c)
for i in range(D):
tmp = list(map(int, input().split()))
s.append(tmp)
t = []
for i in range(D):
tmp = int(input())
t.append(tmp-1)
cnt = [-1 for j in range(26)]
now = 0
for i in range(D):
cnt[t[i]] = i
now += s[i][t[i]]
tmp = 0
for j in range(26):
if j != t[i]:
if cnt[t[i]] == -1:
tmp += c[j] * (i + 1)
else:
tmp += c[j] * (i - cnt[j])
now -= tmp
print(now) | d = int(input())
c = list(map(int,input().split()))
s = [list(map(int,input().split())) for i in range(d)]
t = [int(input()) for i in range(d)]
def calc_score(day, contest, score, last):
last[contest] = day + 1
contest_score = s[day][contest]
score += contest_score
dis = 0
for i in range(26):
dis += c[i] * ((day + 1) - last[i])
score -= dis
return score, last
score = 0
last = [0 for i in range(26)]
for day in range(d):
contest = t[day]
contest -= 1
score, last = calc_score(day, contest, score, last)
print(score) | 1 | 9,899,045,023,232 | null | 114 | 114 |
while 1:
x,y,z=raw_input().split()
x=int(x)
z=int(z)
if(y=='?'):
break
if(y=='+'):
print x+z
if(y=='-'):
print x-z
if(y=='/'):
print x/z
if(y=='%'):
print x%z
if(y=='*'):
print x*z | import sys
for x in sys.stdin:
if "?" in x:
break
print(eval(x.replace("/", "//"))) | 1 | 695,311,621,312 | null | 47 | 47 |
def main():
labels = list(map(int, input().split(' ')))
n = int(input())
for i in range(n):
dice = Dice(labels)
t, f = map(int, input().split(' '))
for j in range(8):
if j%4 == 0:
dice.toE()
if f == dice.front:
break
dice.toN()
for j in range(4):
if t == dice.top:
break
dice.toE()
print(dice.right)
class Dice:
def __init__(self, labels):
self.labels = labels
self._tb = (0, 5)
self._fb = (1, 4)
self._lr = (3, 2)
def toN(self):
tb = self._tb
self._tb = self._fb
self._fb = tuple(reversed(tb))
return self
def toS(self):
tb = self._tb
self._tb = tuple(reversed(self._fb))
self._fb = tb
return self
def toW(self):
tb = self._tb
self._tb = tuple(reversed(self._lr))
self._lr = tb
return self
def toE(self):
tb = self._tb
self._tb = self._lr
self._lr = tuple(reversed(tb))
return self
def get_top(self):
return self.labels[self._tb[0]]
def get_front(self):
return self.labels[self._fb[0]]
def get_right(self):
return self.labels[self._lr[1]]
top = property(get_top)
front = property(get_front)
right = property(get_right)
if __name__ == '__main__':
main() | #!/usr/bin/env python
# -*- coding: utf-8 -*-
POSITIONS = ["top", "south", "east", "west", "north", "bottom"]
class Dice(object):
def __init__(self, initial_faces):
self.faces = {p: initial_faces[i] for i, p in enumerate(POSITIONS)}
self.store_previous_faces()
def store_previous_faces(self):
self.previous_faces = self.faces.copy()
def change_face(self, after, before):
self.faces[after] = self.previous_faces[before]
def change_faces(self, rotation):
self.change_face(rotation[0], rotation[1])
self.change_face(rotation[1], rotation[2])
self.change_face(rotation[2], rotation[3])
self.change_face(rotation[3], rotation[0])
def roll(self, direction):
self.store_previous_faces()
if direction == "E":
self.change_faces(["top", "west", "bottom", "east"])
elif direction == "N":
self.change_faces(["top", "south", "bottom", "north"])
elif direction == "S":
self.change_faces(["top", "north", "bottom", "south"])
elif direction == "W":
self.change_faces(["top", "east", "bottom", "west"])
def rolls(self, directions):
for d in directions:
self.roll(d)
def main():
dice = Dice(input().split())
q = int(input())
for x in range(q):
[qtop, qsouth] = input().split()
if qsouth == dice.faces["west"]:
dice.roll("E")
elif qsouth == dice.faces["east"]:
dice.roll("W")
while qsouth != dice.faces["south"]:
dice.roll("N")
while qtop != dice.faces["top"]:
dice.roll("W")
print(dice.faces["east"])
if __name__ == "__main__":
main() | 1 | 254,966,327,488 | null | 34 | 34 |
a, b, c, k = map(int, input().split())
if k >= a + b + c:
print(a - c)
exit()
elif k <= a:
print(k)
elif k <= a + b:
print(a)
exit()
else:
print(a - (k - a - b))
exit() | ans = 0
s = []
for i in range(int(input())):
a,b = map(int,input().split())
if a==b:
ans += 1
s.append(ans)
else:
ans = 0
if len(s)!=0 and max(s)>=3:
print('Yes')
elif len(s)==0:
print('No')
else:
print('No') | 0 | null | 12,190,392,517,828 | 148 | 72 |
import sys
A, B, C, D = (int(x) for x in input().split())
while A>0 and C>0:
C-=B
if C<=0:
print("Yes")
sys.exit(0)
A-=D
if A<=0:
print("No") | a, b, c, d = map(int, input().split())
T = 0
A = 0
while a > 0:
a -= d
A += 1
while c > 0:
c -= b
T += 1
if A >= T:
print("Yes")
else:
print("No") | 1 | 29,576,453,778,382 | null | 164 | 164 |
u, s, e, w, n, d = input().split()
insts = input()
for inst in insts:
if inst == 'N':
u, s, n, d = s, d, u, n
elif inst == 'E':
u, e, w, d = w, u, d, e
elif inst == 'S':
u, s, n, d = n, u, d, s
elif inst == 'W':
u, e, w, d = e, d, u, w
print(u) | class Dice:
def __init__(self):
d = map(int, raw_input().split(" "))
self.c = raw_input()
self.rows = [d[0], d[4], d[5], d[1]]
self.cols = [d[0], d[2], d[5], d[3]]
def __move_next(self, x, y):
temp = y.pop(0)
y.append(temp)
x[0] = y[0]
x[2] = y[2]
def __move_prev(self, x, y):
temp = y.pop(3)
y.insert(0, temp)
x[0] = y[0]
x[2] = y[2]
def execute(self):
for i in self.c:
self.__move(i, self.rows, self.cols)
def __move(self, com, x, y):
if com == "N":
self.__move_prev(y, x)
elif com == "S":
self.__move_next(y, x)
elif com == "E":
self.__move_prev(x, y)
elif com == "W":
self.__move_next(x, y)
def print_top(self):
print self.rows[0]
dice = Dice()
dice.execute()
dice.print_top() | 1 | 240,084,107,938 | null | 33 | 33 |
from bisect import bisect_left
N = int(input())
S = input()
d = {"R": [], "G": [], "B": []}
for i in range(N):
d[S[i]].append(i)
def find(x, y, z):
total = len(z)
res = 0
for i in x:
for j in y:
if i > j:
continue
# Find k > j
k = bisect_left(z, j)
# Find k == 2 * j - i
k0 = bisect_left(z, 2 * j - i)
flag = int(k0 >= k and k0 < total and z[k0] == 2 * j - i)
res += total - k - flag
return res
ans = 0
ans += find(d["R"], d["G"], d["B"])
ans += find(d["R"], d["B"], d["G"])
ans += find(d["G"], d["R"], d["B"])
ans += find(d["G"], d["B"], d["R"])
ans += find(d["B"], d["R"], d["G"])
ans += find(d["B"], d["G"], d["R"])
print(ans)
| def main():
N = int(input())
S = input()
dict = {'R':0, 'B':0, 'G':0}
for i in range(N):
dict[S[i]] += 1
ans = dict['R']*dict['B']*dict['G']
for i in range(N-2):
if (N-i)%2 == 0:
tmp = int((N-i)/2)-1
else:
tmp = (N-i)//2
for j in range(1,tmp+1):
if S[i]!=S[i+j] and S[i]!=S[i+2*j] and S[i+j]!=S[i+2*j]:
ans = ans - 1
return ans
print(main())
| 1 | 36,295,851,048,380 | null | 175 | 175 |
import sys
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()
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 = ni()
ans = len(make_divisors(n-1))-1
ls = make_divisors(n)[1:]
for i in ls:
nn = n
while nn%i == 0:
nn = nn//i
if nn%i == 1:
ans += 1
print(ans) | import bisect, collections, copy, heapq, itertools, math, string
import sys
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 S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
from collections import defaultdict
from collections import Counter
import bisect
from functools import reduce
def main():
H, N = MI()
A = LI()
A_sum = sum(A)
if A_sum >= H:
print('Yes')
else:
print('No')
if __name__ == "__main__":
main()
| 0 | null | 59,649,266,418,428 | 183 | 226 |
def solve():
H,N = map(int,input().split())
ap = []
mp = []
for _ in range(N):
a,b = map(int,input().split())
ap.append(a)
mp.append(b)
ap_max = max(ap)
dp = [[float('inf')] * (H+ap_max+1) for _ in range(N+1)]
for i in range(N+1):
dp[i][0] = 0
for i in range(N):
for sum_h in range(H+ap_max+1):
if sum_h - ap[i] >= 0:
dp[i+1][sum_h] = min(dp[i][sum_h-ap[i]] + mp[i], dp[i][sum_h])
dp[i+1][sum_h] = min(dp[i+1][sum_h-ap[i]] + mp[i], dp[i][sum_h])
dp[i+1][sum_h] = min(dp[i+1][sum_h], dp[i][sum_h])
ans = float('inf')
for sum_h in range(H, H+ap_max+1):
ans = min(ans,dp[N][sum_h])
print(ans)
if __name__ == '__main__':
solve() | import sys
from collections import deque
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())
n = I()
A = LI()
q = I()
m = LI()
ans = 0
partial_sum = set()
for i in range(2 ** n):
bit = [i>>j&1 for j in range(n)]
partial_sum.add(sum(A[k]*bit[k] for k in range(n)))
for x in m:
print('yes' if x in partial_sum else 'no')
| 0 | null | 40,811,771,086,362 | 229 | 25 |
n = int(input())
s = []
t = []
for i in range(n):
ss,tt = map(str,input().split())
s.append(ss)
t.append(tt)
x = input()
ans = 0
for i in range(n-1):
if ans != 0:
ans += int(t[i+1])
elif s[i] == x:
ans += int(t[i+1])
print(ans) | import sys
import math
def gcd(x,y):
if(x%y==0):
return y
else:
return(gcd(y,x%y))
try:
while True:
a,b= map(int, input().split())
max1=gcd(a,b)
min1=(a*b)//gcd(a,b)
print(str(max1)+' '+str(min1))
except EOFError:
pass
| 0 | null | 48,719,410,336,572 | 243 | 5 |
X, Y = map(int, input().split())
Z = X * 4 - Y
if Z % 2 == 0 and X * 2 >= Z >= 0:
print('Yes')
else:
print('No') | X,Y=map(int,input().split())
def ans170(X:int, Y:int):
if Y<=X*4 and Y>=X*2 and Y%2==0:
return("Yes")
else:
return("No")
print(ans170(X,Y)) | 1 | 13,811,657,936,732 | null | 127 | 127 |
import math
from functools import reduce
def gcd(*numbers):
return reduce(math.gcd, numbers)
def gcd_list(numbers):
return reduce(math.gcd, numbers)
def eratosthenes(n):
D = [0]*(n+1)
for i in range(2, n+1):
if D[i] > 0:
continue
for j in range(i, n+1, i):
D[j] = i
return D
N = int(input())
A = list(map(int, input().split()))
D = eratosthenes(10**6+1)
# D = eratosthenes(max(A))
dic = {}
for i in A:
while i != 1:
if i in dic:
dic[i] += 1
else:
dic[i] = 1
i //= D[i]
isPairwise = True
for i in dic.items():
if i[1] > 1:
isPairwise = False
break
def is_pairwise():
used_primes = [False] * (10**6 + 1)
for a in A:
while a > 1:
prime = D[a]
while a % prime == 0:
a //= prime
if used_primes[prime]:
return False
used_primes[prime] = 1
return True
# if isPairwise:
if is_pairwise():
print("pairwise coprime")
elif gcd_list(A) == 1:
print("setwise coprime")
else:
print("not coprime")
| from math import gcd
from functools import reduce
def facs(n):
yield 2
for x in range(3, n, 2):
yield x
def main():
input() # N
array = [int(x) for x in input().split()]
MAX_A = 10 ** 6 + 1
histogram = [0] * MAX_A
for x in array:
histogram[int(x)] += 1
for divider in facs(MAX_A):
count = sum(histogram[divider::divider])
if count > 1:
break
else:
return 'pairwise coprime'
gcd_total = reduce(gcd, array)
if gcd_total == 1:
return 'setwise coprime'
else:
return 'not coprime'
if __name__ == '__main__':
print(main()) | 1 | 4,150,729,039,522 | null | 85 | 85 |
import itertools,math
N = int(input())
x = [0] * N
y = [0] * N
for i in range(N):
x[i], y[i] = map(int, input().split())
seq=[]
for i in range(N):
seq.append(i)
l=list(itertools.permutations(seq))
num=0
for i in l:
for u in range(1,len(i)):
num+=math.sqrt((x[i[u-1]]-x[i[u]])**2+(y[i[u-1]]-y[i[u]])**2)
print(num/len(l)) | import itertools
import math
N = int(input())
x_list = [0] * N
y_list = [0] * N
for i in range(N):
x_list[i], y_list[i] = map(int, input().split())
l_sum = 0
l = 0
for comb in itertools.combinations(range(N), 2):
l = (
(x_list[comb[0]] - x_list[comb[1]]) ** 2
+ (y_list[comb[0]] - y_list[comb[1]]) ** 2
) ** 0.5
l_sum += l
ans = 2 * l_sum / N
print(ans) | 1 | 148,457,702,132,752 | null | 280 | 280 |
#!/usr/bin/env python3
# Generated by https://github.com/kyuridenamida/atcoder-tools
from typing import *
import collections
import functools
import itertools
import math
import sys
INF = float('inf')
YES = "Yes" # type: str
NO = "No" # type: str
def solve(H: int, N: int, A: "List[int]"):
return [NO, YES][sum(A) >= H]
def main():
sys.setrecursionlimit(10 ** 6)
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
H = int(next(tokens)) # type: int
N = int(next(tokens)) # type: int
A = [int(next(tokens)) for _ in range(N)] # type: "List[int]"
print(f'{solve(H, N, A)}')
if __name__ == '__main__':
main()
| H, N = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
print("Yes") if H - sum(A) <= 0 else print("No") | 1 | 78,092,733,369,672 | null | 226 | 226 |
n, m = map(int, input().split())
*C, = map(int, input().split())
dp = [n]*(n+1)
dp[0] = 0
for c in C:
for i in range(n+1):
if i+c > n:
break
dp[i+c] = min(dp[i+c], dp[i] + 1)
print(dp[n])
| n, m = [int(i) for i in input().split()]
C = [int(i) for i in input().split()]
dp = [100000] * (n + 1)
dp[0] = 0
for k in C:
for j in range(k, n + 1):
if dp[j] > dp[j - k] + 1:
dp[j] = dp[j - k] + 1
print(dp[n]) | 1 | 137,799,117,180 | null | 28 | 28 |
def ev(n):
return (n+1) / 2
N, K = map(int, input().split())
p = list(map(int, input().split()))
prev = ev(p[0])
ans = sum([ev(i) for i in p[:K]])
total = ans
for i in range(1, N-K+1):
total -= prev
total += ev(p[i+K-1])
prev = ev(p[i])
ans = max(ans, total)
print(ans)
| N, M, K = map(int, input().split())
friend = {}
for i in range(M):
A, B = map(lambda x: x-1, map(int, input().split()))
if A not in friend:
friend[A] = []
if B not in friend:
friend[B] = []
friend[A].append(B)
friend[B].append(A)
block = {}
for i in range(K):
C, D = map(lambda x: x-1, map(int, input().split()))
if C not in block:
block[C] = []
if D not in block:
block[D] = []
block[C].append(D)
block[D].append(C)
first = {}
for i in range(N):
if i not in first:
first[i] = i
if i in friend:
queue = []
queue.extend(friend[i])
counter = 0
while counter < len(queue):
item = queue[counter]
first[item] = i
if item in friend:
for n in friend[item]:
if n not in first:
queue.append(n)
counter += 1
size = {}
for key in first:
if first[key] not in size:
size[first[key]] = 1
else:
size[first[key]] += 1
for i in range(N):
if i not in friend:
print(0)
continue
no_friend = 0
if i in block:
for b in block[i]:
if first[b] == first[i]:
no_friend += 1
print(size[first[i]] - len(friend[i]) - no_friend - 1) | 0 | null | 68,030,914,919,460 | 223 | 209 |
n = int(input())
sum = 0
for i in range(1, n+1):
if((i % 3 * i % 5 * i % 15) != 0):
sum += i
print(sum) | h,n=map(int,input().split())
a=list(map(int,input().split()))
ans=0
if sum(a)>=h:
print('Yes')
else:
print('No') | 0 | null | 56,554,908,501,322 | 173 | 226 |
cnt = int(input())
dif_max = -1000000000
v_min = 1000000000
for i in range(cnt):
buf = int(input())
if i > 0:
dif_max = dif_max if buf - v_min < dif_max else buf - v_min
v_min = buf if buf < v_min else v_min
print(dif_max) | # -*- coding: utf-8 -*-
D = int(input())
c = list(map(int, input().split()))
s = []
t = []
for i in range(D):
s.append(list(map(int, input().split())))
for i in range(D):
t.append(int(input()) - 1)
last = [0] * 26
ans = 0
for i in range(D):
ans = ans + s[i][t[i]]
for j in range(26):
if t[i] != j:
last[j] += 1
ans = ans - (c[j] * last[j])
elif t[i] == j:
last[j] = 0
print(ans) | 0 | null | 4,999,963,436,872 | 13 | 114 |
import re
N = int(input())
S = input()
result = ''
for i in range(len(S)):
if i == len(S) - 1:
result += S[i]
elif S[i] != S[i+1]:
result += S[i]
print(len(result))
| n,m=map(int,input().split())
a=list(map(int,input().split()))
c=0
for i in a:
if i>=sum(a)*(1/(4*m)):
c+=1
if c>=m:
print('Yes')
else:
print('No') | 0 | null | 104,615,612,537,850 | 293 | 179 |
import sys
readline = sys.stdin.readline
def main():
N = int(readline())
Sp = []
Sm = []
total = 0
bm = 0
for i in range(N):
l = readline().strip()
b = 0
h = 0
for j in range(len(l)):
if l[j] == '(':
h += 1
else:
h -= 1
b = min(b, h)
if h == 0 and b != 0:
bm = min(bm, b)
elif h > 0:
Sp.append([b, h])
elif h < 0:
Sm.append([b-h, -h])
total += h
Sp.append([bm, 0])
if total != 0:
print('No')
exit()
Sp.sort(key=lambda x:x[0], reverse=True)
p = check(Sp)
if p < 0:
print('No')
exit()
Sm.sort(key=lambda x:x[0], reverse=True)
m = check(Sm)
if m < 0:
print('No')
exit()
print('Yes')
def check(S):
h = 0
for i in range(len(S)):
if h + S[i][0] < 0:
return -1
else:
h += S[i][1]
return h
if __name__ == '__main__':
main() | N=int(input())
L=list(map(int, input().split()))
count=0
for i in range(N):
for j in range(i+1,N):
for k in range(j+1,N):
L2=[L[i],L[j],L[k]]
L2.sort()
if L2[0]+L2[1]>L2[2] and L2[0] != L2[1] and L2[1] != L2[2] and L2[2] != L2[0]:
count+=1
print(count)
| 0 | null | 14,428,660,314,398 | 152 | 91 |
import sys
i = 1
for line in sys.stdin.readlines():
x = line.strip()
if x != "0":
print("Case {}: {}".format(i, x))
i += 1 | X = int(input())
deposit = 100
y_later = 0
while deposit < X:
y_later += 1
deposit += deposit // 100
print(y_later) | 0 | null | 13,865,612,920,572 | 42 | 159 |
# A - November 30
def main():
M1, _, M2, _ = map(int, open(0).read().split())
print(int(M1 != M2))
if __name__ == "__main__":
main()
| n = input()
a = list(map(int, input().split()))
min = a[0]
ans = 0
for i in a:
if min > i:
ans += min - i
else:
min = i
print(ans) | 0 | null | 64,508,088,743,840 | 264 | 88 |
h = int(input())
ans = 0
m = 1
while True:
if h == 1 :
ans += 1
break
else:
h = h//2
m *= 2
ans += m
print(ans)
| h = int(input())
cnt = 1
if h == 1:
print(1)
exit()
while(True):
cnt += 1
h = int(h / 2)
if h == 1:
break
print(2 ** cnt - 1) | 1 | 79,877,249,604,000 | null | 228 | 228 |
H, W = [int(x) for x in input().split()]
if H == 1 or W == 1:
print(1)
else:
print((H * W + 1) // 2)
| H, W = map(int, input().split())
if (H == 1) or (W == 1):
print(1)
exit()
if H%2 == 1:
if W%2 == 1:
print(H*W//2 + 1)
exit()
# else:
# print(H*W//2)
# else:
# if W%2 == 1:
# print(H*W//2)
# else:
# print(H*W//2)
print(H*W//2) | 1 | 51,042,502,512,960 | null | 196 | 196 |
import sys
input=sys.stdin.readline
class list2D:
def __init__(self, H, W, num):
self.__H = H
self.__W = W
self.__dat = [num] * (H * W)
def __getitem__(self, a):
return self.__dat[a[0]*self.__W+a[1]]
def __setitem__(self, a, b):
self.__dat[a[0]*self.__W+a[1]] = b
def debug(self):
print(self.__dat)
class list3D:
def __init__(self, H, W, D, num):
self.__H = H
self.__W = W
self.__D = D
self.__X = W * D
self.__dat = [num] * (H * W * D)
def __getitem__(self, a):
return self.__dat[a[0]*self.__X+a[1]*self.__D + a[2]]
def __setitem__(self, a, b):
self.__dat[a[0]*self.__X+a[1]*self.__D + a[2]] = b
def debug(self):
print(self.__dat)
def main():
r,c,k=map(int,input().split())
v = list2D(r, c, 0)
for _ in range(k):
ri,ci,a=map(int,input().split())
v[ri-1, ci-1] = a
dp = list3D(r, c, 4, 0)
#print(dp)
if v[0, 0]>0: dp[0, 0, 1]=v[0, 0]
for i in range(r):
for j in range(c):
val = v[i, j]
if i>0:
x = max(dp[i-1, j, 0], dp[i-1, j, 1], dp[i-1, j, 2], dp[i-1, j, 3])
dp[i, j, 1]=max(dp[i, j, 1],x+val)
dp[i, j, 0]=max(dp[i, j, 0],x)
if j>0:
X = dp[i, j-1, 0]
Y = dp[i, j-1, 1]
V = dp[i, j-1, 2]
Z = dp[i, j-1, 3]
dp[i, j, 0]=max(dp[i, j, 0],X)
dp[i, j, 1]=max(dp[i, j, 1],X+val,Y)
dp[i, j, 2]=max(dp[i, j, 2],Y+val,V)
dp[i, j, 3]=max(dp[i, j, 3],V+val,Z)
#print(dp)
ans=0
for i in range(4): ans=max(dp[r-1, c-1, i],ans)
return print(ans)
if __name__=="__main__":
main()
| # B - Common Raccoon vs Monster
H,N = map(int,input().split())
A = list(map(int,input().split()))
ans = 'Yes' if H<=sum(A) else 'No'
print(ans) | 0 | null | 41,988,840,751,748 | 94 | 226 |
import queue
import numpy as np
import math
n = int(input())
A = list(map(int, input().split()))
A = np.array(A,np.int64)
ans = 0
for i in range(60 + 1):
a = (A >> i) & 1
count1 = np.count_nonzero(a)
count0 = len(A) - count1
ans += count1*count0 * pow(2, i)
ans%=1000000007
print(ans) | def main():
N = int(input())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7
ans = 0
L = 100
for l in range(L):
one = 0
zero = 0
for a in A:
if (a & (1<<(L - l - 1))):
one += 1
else:
zero += 1
ans += (one * zero) * 2 ** (L - l - 1)
ans %= MOD
print(ans)
if __name__ == "__main__":
main() | 1 | 123,292,768,585,774 | null | 263 | 263 |
n=int(input())
rng = [[] for _ in range(n)]
for i in range(n):
x,l=map(int,input().split())
rng[i] = [x-l,x+l]
rng.sort(key=lambda y:y[1])
cnt=0
t_mx=-(10**10)
for i in range(n):
if t_mx > rng[i][0]:
cnt += 1
else:
t_mx = rng[i][1]
print(n-cnt) | import numpy as np
n=int(input())
d_i = list(map(int, input().split()))
d=np.array(d_i)
out=0
for i in range(1,n):
a=d_i.pop(0)
d_i.append(a)
out+=np.dot(d,np.array(d_i))
print(int(out/2)) | 0 | null | 128,659,055,157,290 | 237 | 292 |
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
H, W, K = lr()
S = []
for i in range(H):
S.append([int(s) for s in sr()])
ans = 2000
idx = [0]*H
for div in range(1<<(H-1)):
g = 0
for i in range(H):
idx[i] = g
if div>>i&1:
g += 1
g += 1
c = [[0]*W for i in range(g)]
for i in range(H):
for j in range(W):
c[idx[i]][j] += S[i][j]
ok = True
for i in range(g):
for j in range(W):
if c[i][j] > K:
ok = False
if not ok:
continue
num = g-1
now = [0]*g
def add(j):
for i in range(g):
now[i] += c[i][j]
for i in range(g):
if now[i] > K:
return False
return True
for j in range(W):
if not add(j):
num += 1
now = [0]*g
add(j)
ans = min(ans, num)
print(ans)
resolve() | #!/usr/bin/env python3
h,w,k = map(int,input().split())
b =[]
for i in range(h):
s = input()
b.append(s)
# 縦の切り方を固定すれば、左から見て貪欲に考えることができる。
# どれかの切り方でK+1 以上1 のマスがあれば、そこで切る
if h == 1:
print(math.ceil(s//k))
exit()
# リストで現在のそれぞれのブロックの1の数を持つ
# 列の切断が入ったときに全て0に初期化
# bit 全探索は 2 ** h-1 存在する
ans = h*w #INF
for i in range(2**(h-1)):
cnt_white = [0]
# ループの中で宣言する
ans_i = 0
#前処理として、どこに切れ込みがあるかを見て、リストの構成
for bit in range(h-1):
if i >> bit & 1:
cnt_white.append(0)
ans_i += 1
#print("i = ",i,cnt_white)
c = 0
last_div = 0
while c < w:# 左から貪欲
r_idx = 0
for r in range(h):
if b[r][c] == "1":#対応するブロックのcnt+=1
cnt_white[r_idx] += 1
#print("col = ",c,cnt_white,r_idx,ans_i)
if i >> r & 1:#次にブロックが変わるのでidxを増やす
r_idx += 1
# 1行だけでmax(cnt_white) >= k+1になるときはその切り方では不可能
if max(cnt_white) >= k+1:
# 戻らないといけない
ans_i += 1
if c == last_div:
ans_i = h*w #INF
break
last_div = c
c -= 1
cnt_white = [0]*len(cnt_white)#初期化
#print("initialize",last_div)
c += 1
ans = min(ans,ans_i)
print(ans) | 1 | 48,327,817,081,972 | null | 193 | 193 |
import collections
import sys
readline = sys.stdin.readline
def main():
n = int(readline().rstrip())
A = list(map(int, readline().rstrip().split()))
ans = 0
c = collections.Counter()
for i in range(n):
ans += c[i + 1 - A[i]]
c[i + 1 + A[i]] += 1
print(ans)
if __name__ == '__main__':
main()
| import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np # cumsum
# from bisect import bisect_left, bisect_right
def solve():
N = II()
A = LI()
memo = {}
ans = 0
for i, a in enumerate(A):
i = i + 1
# print(i, a - i, i - a)
ans = ans + memo.get(i - a, 0)
memo[a + i] = memo.get(a + i, 0) + 1
print(ans)
if __name__ == '__main__':
solve()
| 1 | 26,020,417,244,632 | null | 157 | 157 |
S = input()
q = int(input())
for i in range(q):
L = input().split()
a = int(L[1])
b = int(L[2])
slice1 = S[:a]
slice2 = S[a:b+1]
slice3 = S[b+1:]
if L[0] == 'print':
print(slice2)
elif L[0] == 'reverse':
S = slice1 + slice2[::-1] + slice3
elif L[0] == 'replace':
S = slice1 + L[3] + slice3 | from collections import deque
S = input()
Q = int(input())
d = deque(S)
flg = 1 #Trueが前から,
for i in range(Q):
t, *a = input().split()
if t == "1":
flg = 1 - flg
else:
if flg:
if a[0] == "1":
d.appendleft(a[1])
else:
d.append(a[1])
else:
if a[0] == "2":
d.appendleft(a[1])
else:
d.append(a[1])
d = list(d)
if flg:
print(''.join(d))
else:
print(''.join(d[::-1]))
| 0 | null | 29,761,407,253,760 | 68 | 204 |
while 1:
a, op, b = input().split()
if op == '?':
break
a = int(a)
b = int(b)
if op == '+':
print(a+b)
elif op == '-':
print(a-b)
elif op == '*':
print(a * b)
elif op == '/':
print(a // b) | def az10():
while True:
xs = raw_input().split()
a,op,b = int(xs[0]),xs[1],int(xs[2])
if op == "?" : break
if op == "+" : print (a + b)
if op == "-" : print (a - b)
if op == "*" : print (a * b)
if op == "/" : print (a / b)
az10() | 1 | 680,297,784,680 | null | 47 | 47 |
import numpy as np
import math
import collections
if __name__ == '__main__':
n = int(input())
print(int(np.lcm(360,n)/n)) | N, K = map(int, input().split())
Hlist = list(map(int, input().split()))
Hlist = sorted(Hlist)[::-1]
#user K super attack for largest K enemy
remainHlist = Hlist[K:]
attackTimes = sum(remainHlist)
print(attackTimes) | 0 | null | 46,352,332,228,932 | 125 | 227 |
r=range(1,int(input())+1)
from math import gcd
print(sum(gcd(gcd(a,b),c) for a in r for b in r for c in r)) | from math import gcd
k=int(input())
cnt=0
for i in range(1,k+1):
for p in range(1,k+1):
for q in range(1,k+1):
gcd1=gcd(i,p)
cnt+=gcd(gcd1,q)
print(cnt) | 1 | 35,634,858,531,830 | null | 174 | 174 |
x = int(input())
y = int(input())
z = int(input())
a = max(x,y)
if z%a == 0:
print(z//a)
else:
print(z//a+1) | h = int(input())
w = int(input())
n = int(input())
m = max(w,h)
if n > n //m * m:
ans = n // m + 1
else:
ans = n // m
print(ans) | 1 | 88,483,374,645,952 | null | 236 | 236 |
n = int(input())
P = list(map(int, input().split()))
cnt = 0
m = P[0]
for p in P:
if m >= p:
cnt += 1
m = p
print(cnt)
| string = input()
height = 0
height_list = [[0, False]]
for s in string:
if s == '\\':
height += -1
elif s == '/':
height += 1
else:
pass
height_list.append([height, False])
highest = 0
for i in range(1, len(height_list)):
if height_list[i-1][0] < height_list[i][0] <= highest:
height_list[i][1] = True
highest = max(highest, height_list[i][0])
puddles = []
area = 0
surface_level = None
for i in range(len(height_list)-1, -1, -1):
if surface_level != None:
area += surface_level - height_list[i][0]
if surface_level == height_list[i][0]:
puddles += [area]
surface_level = None
area = 0
if surface_level == None and height_list[i][1]:
surface_level = height_list[i][0]
puddles = puddles[::-1]
print(sum(puddles))
print(len(puddles), *puddles)
| 0 | null | 42,959,968,505,792 | 233 | 21 |
#!/usr/bin/env python3
def main():
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
command = [''] * N
ans = 0
for i, t in enumerate(T):
if t == 'r':
point = P
command_candidate = 'p'
elif t == 's':
point = R
command_candidate = 'r'
else:
point = S
command_candidate = 's'
if i >= K and command[i - K] == command_candidate:
point = 0
command_candidate = ''
ans += point
command[i] = command_candidate
print(ans)
if __name__ == '__main__':
main()
| #!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: D
# CreatedDate: 2020-08-30 13:56:28 +0900
# LastModified: 2020-08-30 14:22:16 +0900
#
import os
import sys
# import numpy as np
# import pandas as pd
from collections import Counter
def main():
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
takahashi = ''
for t in T:
if t == 's':
takahashi += 'r'
elif t == 'p':
takahashi += 's'
else:
takahashi += 'p'
for i in range(K, N):
if takahashi[i-K] == takahashi[i]:
takahashi = takahashi[:i] + '_' + takahashi[i+1:]
ans = takahashi.count('r')*R + takahashi.count('s')*S + takahashi.count('p')*P
print(ans)
if __name__ == "__main__":
main()
| 1 | 106,812,501,345,814 | null | 251 | 251 |
def main():
n, k = map(int, input().split())
h_list = list(map(int, input().split()))
h_list.sort(reverse=True)
if n <= k:
ans = 0
else:
ans = sum(h_list[k:])
print(ans)
if __name__ == "__main__":
main()
| from collections import Counter, defaultdict
# n = int(input())
# li = list(map(int, input().split()))
# n = int(input())
a, b = map(int, input().split())
c, d = map(int, input().split())
# d = defaultdict(lambda: 0)
# s = input()
print("1" if c == a+1 else "0") | 0 | null | 101,770,441,091,812 | 227 | 264 |
a=int(input())
b=a//200
print(str(10-b)) | N,K=list(map(int,input().split()))
l=[0]*(K+1)
ans=0
mod=10**9+7
for x in range(K,0,-1):
l[x]=pow((K//x),N,mod)
for y in range(2*x,K+1,x):
l[x]-=l[y]
l[x]=pow(l[x],1,mod)
ans+=l[x]*x
ans=pow(ans,1,mod)
print(ans) | 0 | null | 21,731,274,597,578 | 100 | 176 |
f = open(0)
N, M, L = map(int, f.readline().split())
A = [list(map(int, f.readline().split())) for i in range(N)]
B = [list(map(int, f.readline().split())) for i in range(M)]
C = [[0]*L for i in range(N)]
for i in range(N):
for j in range(L):
C[i][j] = str(sum(A[i][k]*B[k][j] for k in range(M)))
for line in C:
print(*line)
| import sys
import fractions
readline = sys.stdin.buffer.readline
def main():
gcd = fractions.gcd
def lcm(a, b):
return a * b // gcd(a, b)
N, M = map(int, readline().split())
A = list(set(map(int, readline().split())))
B = A[::]
while not any(b % 2 for b in B):
B = [b // 2 for b in B]
if not all(b % 2 for b in B):
print(0)
return
semi_lcm = 1
for a in A:
semi_lcm = lcm(semi_lcm, a // 2)
if semi_lcm > M:
print(0)
return
print((M // semi_lcm + 1) // 2)
return
if __name__ == '__main__':
main()
| 0 | null | 51,558,454,605,050 | 60 | 247 |
N, M = [int(_) for _ in input().split()]
if N == M:
print('Yes')
else:
print('No') | a,b,m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = []
for i in range(m):
x.append(list(map(int, input().split())))
min_a = min(a)
min_b = min(b)
money = min_a + min_b
for i in range(m):
kingaku = a[x[i][0]-1] + b[x[i][1]-1] - x[i][2]
if kingaku < money:
money = kingaku
print(money) | 0 | null | 68,879,260,143,310 | 231 | 200 |
N,K = list(map(int, input().split(" ")))
H = sorted(list(map(int, input().split(" "))), reverse=True)
Z = 0
R = 0
for n,e in enumerate(H):
if (n == K):
Z = n
break
elif (K >= len(H)):
Z = len(H)
break
for i in H[Z:]:
R += i
print(R)
| from math import factorial
from itertools import permutations
n = int(input())
p = tuple(map(str, input().split()))
q = tuple(map(str, input().split()))
num = [str(i) for i in range(1, n+1)]
allnum = list(i for i in permutations(num, n))
print(abs(allnum.index(p) - allnum.index(q))) | 0 | null | 89,813,719,395,232 | 227 | 246 |
n = input()
dictInProb = set()
for i in range(int(n)):
cmdAndStr = list(input().split())
if cmdAndStr[0] == "insert":
dictInProb.add(cmdAndStr[1])
elif cmdAndStr[0] == "find":
if cmdAndStr[1] in dictInProb:
print("yes")
else:
print("no")
else: pass | S = str(input())
if S[-1] == "s":
print(S+"es")
else:
print(S+"s")
| 0 | null | 1,235,879,734,290 | 23 | 71 |
m,n=map(long,raw_input().split())
print m/n,
print m%n,
print '%.5f' % (float(m)/n) | import sys; input = sys.stdin.readline
n = int(input())
ac, wa, tle, re = 0, 0, 0, 0
for i in range(n):
a = input().strip()
if a == 'AC': ac+=1
elif a == 'WA': wa += 1
elif a == 'TLE': tle += 1
elif a == 'RE': re += 1
print(f"AC x {ac}")
print(f"WA x {wa}")
print(f"TLE x {tle}")
print(f"RE x {re}") | 0 | null | 4,635,976,057,870 | 45 | 109 |
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)) | S = len(set(input()))
if S == 2:
print('Yes')
else:
print('No') | 0 | null | 36,275,416,227,918 | 137 | 201 |
n,k=map(int,input().split())
l =[]
cnt =0
for i in range(k):
d = int(input())
a = list(map(int,input().split()))
for j in a:
l.append(j)
for i in range(1,n+1):
if i not in l:
cnt+=1
print(cnt) | N, K = map(int, input().split())
sunuke = [1] * N
for i in range(K):
di = int(input())
tmp = map(int, input().split())
for j in tmp:
sunuke[j-1] = 0
print(sum(sunuke)) | 1 | 24,785,927,584,720 | null | 154 | 154 |
n = int(input())
A = list(map(int, input().split()))
from collections import defaultdict
dic = defaultdict(int)
ans = 0
for i in range(n):
if i - A[i] in dic:
ans += dic[i-A[i]]
dic[i+A[i]] += 1
print(ans)
| def solve(n, k, a):
for i in range(k, n):
print("Yes" if a[i-k] < a[i] else "No")
n, k = map(int, input().split())
a = list(map(int, input().split()))
solve(n, k, a) | 0 | null | 16,508,850,481,760 | 157 | 102 |
n, m = map(int, input().split())
l = list(map(int, input().split()))
l.sort()
import bisect
def func(x):
C = 0
for p in l:
q = x -p
j = bisect.bisect_left(l, q)
C += n-j
if C >= m:
return True
else:
return False
l_ = 0
r_ = 2*10**5 +1
while l_+1 < r_:
c_ = (l_+r_)//2
if func(c_):
l_ = c_
else:
r_ = c_
ans = 0
cnt = 0
lr = sorted(l, reverse=True)
from itertools import accumulate
cum = [0] + list(accumulate(lr))
for i in lr:
j = bisect.bisect_left(l, l_-i)
ans += i*(n-j) + cum[n-j]
cnt += n -j
ans -= (cnt-m)*l_
print(ans) | k, m = list(map(int, input().split()))
if 500 * k >= m:
print('Yes')
elif 500 * k < m:
print('No')
else:
print('No') | 0 | null | 103,517,051,340,352 | 252 | 244 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.