code1
stringlengths 16
427k
| code2
stringlengths 16
427k
| similar
int64 0
1
| pair_id
int64 2
178,025B
⌀ | question_pair_id
float64 27.1M
177,113B
⌀ | code1_group
int64 1
297
| code2_group
int64 1
297
|
---|---|---|---|---|---|---|
L,R,d = map(int,input().split())
A = 0
for _ in range(L,R+1):
if _ %d == 0:
A += 1
print(A)
|
a,b,n=map(int,input().split())
if a%n == 0 and b%n == 0:
print((b//n) - (a//n) + 1)
else:
print((b//n) - (a//n))
| 1 | 7,524,735,905,818 | null | 104 | 104 |
n,m = list(map(int, input().split()))
if n % 2 == 1:
for i in range(m):
print("{} {}".format(2 + i, n - i))
else:
for i in range(m):
if i % 2 == 0:
print("{} {}".format(int(n / 2 - i / 2), int(n / 2 + 1 + i / 2)))
else:
print("{} {}".format(int(2 + i // 2), int(n - i // 2)))
|
# happendはsetで保持しておき,roopはlistで保持しておく
N, X, M = map(int, input().split()) # modMは10**5以下が与えられる
happend = set()
A = [X]
MOD = M
# 第1~N項まで計N項の和をもとめる
start = 0
for _ in range(N-1):
if A[-1]**2 % MOD not in happend:
happend.add(A[-1]**2 % MOD)
A.append(A[-1]**2 % MOD)
else:
start = A.index(A[-1]**2 % MOD)
break
roop_count = (N-start)//len(A[start:])
ans = sum(A[:start]) + sum(A[start:])*roop_count + sum(A[start:start+(N-start) % len(A[start:])])
# print(A[:start], A[start:], A[start:start+(N-start) % len(A[start:])])
print(ans)
| 0 | null | 15,776,367,501,792 | 162 | 75 |
x = int(input())
price = [100, 101, 102, 103, 104, 105]
dp = [False for _ in range(x + 1)]
dp[0] = True
for i in range(1, x + 1):
for j in range(6):
if i - price[j] >= 0:
if dp[i - price[j]]:
dp[i] = True
break
else:
dp[i] = False
if dp[x]:
print(1)
else:
print(0)
|
n=int(input())
a=list(int(x) for x in input().split())
count=0
z=True
j=0
if 1 not in a:
print(-1)
else:
for k in range(j,n):
if a[k]==count+1:
j=k
count=count+1
print(n-count)
| 0 | null | 120,752,596,375,028 | 266 | 257 |
import math
pi=math.pi
r=float(input())
menseki=r*r*pi
ensyu=2*r*pi
print(str(menseki)+" "+str(ensyu))
|
import sys, math, itertools
input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**7)
INF = 10**20
MOD = 998244353
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def factorialMod(n, p):
fact = [0] * (n+1)
fact[0] = fact[1] = 1
factinv = [0] * (n+1)
factinv[0] = factinv[1] = 1
inv = [0] * (n+1)
inv[1] = 1
for i in range(2, n + 1):
fact[i] = (fact[i-1] * i) % p
inv[i] = (-inv[p % i] * (p // i)) % p
factinv[i] = (factinv[i-1] * inv[i]) % p
return fact, factinv
def combMod(n, r, fact, factinv, p):
if (r < 0) or (n < r):
return 0
r = min(r, n - r)
return fact[n] * factinv[r] * factinv[n-r] % p
def resolve():
N, M, K = LI()
ans = 0
fact, factinv = factorialMod(N, MOD)
for i in range(K + 1):
ans += combMod(N - 1, i, fact, factinv, MOD) * M * pow(M - 1, N - 1 - i, MOD)
ans %= MOD
print(ans)
if __name__ == '__main__':
resolve()
| 0 | null | 11,818,547,000,468 | 46 | 151 |
def check(List,mid):
tmp=0
for l in List:
tmp+=(-(-l//mid)-1)
return tmp
n,k=map(int,input().split())
a=list(map(int,input().split()))
lo=0
hi=max(a)+1
while hi - lo > 1:
mid=(lo+hi)//2
if check(a,mid)<=k:
hi=mid
else:
lo=mid
print(hi)
|
n,k = map(int,input().split())
a = list(map(int,input().split()))
l=0
r=10**9
def check(x):
now = 0
for i in range(n):
now += (a[i] - 1 )// x
return now <= k
while r-l > 1:
x = (l+r) //2
if check(x):
r = x
else:
l = x
print(r)
| 1 | 6,496,736,733,380 | null | 99 | 99 |
import itertools
from typing import List
def main():
h, w, k = map(int, input().split())
c = []
for _ in range(h):
c.append(list(input()))
print(hv(c, h, w, k))
def hv(c: List[List[str]], h: int, w: int, k: int) -> int:
ret = 0
for comb_h in itertools.product((False, True), repeat=h):
for comb_w in itertools.product((False, True), repeat=w):
cnt = 0
for i in range(h):
for j in range(w):
if comb_h[i] and comb_w[j] and c[i][j] == '#':
cnt += 1
if cnt == k:
ret += 1
return ret
if __name__ == '__main__':
main()
|
import sys
input = lambda: sys.stdin.readline().rstrip()
N, K = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)
F = sorted(list(map(int, input().split())))
def binary_search(min_n, max_n):
while max_n - min_n != 1:
tn = (min_n + max_n) // 2
if judge(tn):
max_n = tn
else:
min_n = tn
return max_n
def judge(tn):
k = 0
for i in range(N):
if A[i] * F[i] > tn:
k += A[i] - (tn // F[i])
if k > K:
return False
return True
def solve():
ans = binary_search(-1, 10**12)
print(ans)
if __name__ == '__main__':
solve()
| 0 | null | 86,479,931,215,310 | 110 | 290 |
import sys
N, M, K = [int(s) for s in sys.stdin.readline().split()]
n = 0
A = [0]
for s in sys.stdin.readline().split():
n = n + int(s)
A.append(n)
n = 0
B = [0]
for s in sys.stdin.readline().split():
n = n + int(s)
B.append(n)
ans = 0
for i in range(N + 1):
if A[i] > K:
break
for j in range(M, -1, -1):
if A[i] + B[j] <= K:
ans = max(ans, i + j)
M = j
break
print(ans)
|
print(sum([i for i in range(1,int(input())+1) if not ((i%3==0 and i%5==0) or i%3==0 or i%5==0)]))
| 0 | null | 22,694,044,931,428 | 117 | 173 |
h,w,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
H=[0]*h
W=[0]*w
for i in range(m):
H[l[i][0]-1]+=1
W[l[i][1]-1]+=1
hmax=max(H)
wmax=max(W)
cthmax=0
ctwmax=0
for i in range(h):
if H[i]==hmax:
cthmax+=1
for i in range(w):
if W[i]==wmax:
ctwmax+=1
ct=0
for i in range(m):
if H[l[i][0]-1]==hmax and W[l[i][1]-1]==wmax:
ct+=1
if cthmax*ctwmax>ct:
print(hmax+wmax)
else:
print(hmax+wmax-1)
|
R, C, T = map(int, input().split())
RC = []
for _ in range(T):
RC.append(list(map(int, input().split())))
TR = [0] * (R + 1)
TC = [0] * (C + 1)
mxr=0
mxc=0
for r, c in RC:
TR[r] += 1
TC[c] += 1
if(TR[r]>mxr):
mxr=TR[r]
if(TC[c]>mxc):
mxc=TC[c]
ct = TR.count(mxr) * TC.count(mxc) - len([0 for r, c in RC if TR[r] == mxr and TC[c] == mxc])
print(mxc + mxr - (ct == 0))
| 1 | 4,748,422,959,562 | null | 89 | 89 |
S = int(input())
s = S%60
m = (S-s)/60%60
h = S/3600%24
answer = str(int(h))+":"+str(int(m))+":"+str(s)
print(answer)
|
time = int(input())
hour = int(time / 3600)
minute = int(time % 3600/60)
second = int(time % 60 % 60)
print("%d:%d:%d" % (hour, minute, second))
| 1 | 325,364,370,140 | null | 37 | 37 |
# n, k = map(int, input().split())
# from collections import OrderedDict
# d = OrderedDict()
# a = list(input().split())
b = list(map(int, input().split()))
# print(r + max(0, 100*(10-n)))
# print("Yes" if 500*n >= k else "No")
# s = input()
# a = int(input())
# b = int(input())
# c = int(input())
# s = input()
print(b[-1], *b[:2])
|
K = int(input())
A,B = map(int,input().split())
li = []
for i in range(A,B+1):
li.append(i)
ans = False
for j in li:
if j%K == 0:
ans = True
break
else:
ans = False
if ans == True:print("OK")
else:print("NG")
| 0 | null | 32,400,035,834,260 | 178 | 158 |
import sys
input = sys.stdin.readline
n,p=map(int,input().split())
s=input()
ans=0
if p == 2 or p == 5:
for i in range(n):
if int(s[i]) % p == 0:
ans += i + 1
print(ans)
exit()
total=[0]*p#pで割った余りで分類
total[0]=1#t[0]=0の分をあらかじめカウント
t=[0]*(n+1)#s[i:n]を格納する用の配列
for i in range(n-1,-1,-1):
t[i]=t[i+1]+int(s[i])*pow(10,n-1-i,p)#s[i:n]を漸化式で付け加えていく
total[t[i]%p]+=1
for i in range(p):
ans+=total[i]*(total[i]-1)//2
print(ans)
|
n, p = map(int, input().split())
s = input()
res = 0
if p == 2 or p == 5:
for i in range(n):
if int(s[i]) % p == 0:
res += i + 1
else:
s = s[::-1]
cnt = {0: 1}
now = 0
d = 1
for i in range(n):
now = (now + int(s[i]) * d) % p
res += cnt.get(now, 0)
d = (d * 10) % p
cnt[now] = cnt.get(now, 0) + 1
print(res)
| 1 | 58,439,075,618,092 | null | 205 | 205 |
a, b, c = map(int, input().split())
p = a**2 + b**2 + c**2 - 2*a*b - 2*b*c - 2*a*c
if p > 0 and c > a + b:
print('Yes')
else:
print('No')
|
def main():
mod = 10**9+7
n, k = map(int, input().split())
a = list(map(int, input().split()))
a.sort(key=lambda x: -abs(x))
kouho = []
minus = 0
last_plus = None
last_minus = None
if k == 1:
print(max(a))
return
for i in range(k):
kouho.append(a[i])
if a[i] >= 0:
last_plus = a[i]
if a[i] < 0:
minus += 1
last_minus = a[i]
if minus % 2 == 0:
ans = 1
for i in kouho:
ans = ans*i % mod
print(ans)
return
change_plus = None
change_minus = None
if last_plus != None:
# マイナスを追加する
for i in range(k, n):
if a[i] <= 0:
change_plus = a[i]
break
if last_minus != None:
# マイナスを追加する
for i in range(k, n):
if a[i] >= 0:
change_minus = a[i]
break
# print(kouho)
#print(last_plus, change_plus)
#print(last_minus, change_minus)
if change_plus == None and change_minus == None:
ans = 1
for i in range(k):
ans = ans*a[-i-1] % mod
elif change_plus == None:
ans = change_minus
for i in kouho:
ans = ans*i % mod
ans = ans*pow(last_minus, mod-2, mod) % mod
elif change_minus == None:
ans = change_plus
for i in kouho:
ans = ans*i % mod
ans = ans*pow(last_plus, mod-2, mod) % mod
else:
if abs(change_plus*last_minus) < abs(change_minus*last_plus):
ans = change_minus
for i in kouho:
ans = ans*i % mod
ans = ans*pow(last_minus, mod-2, mod) % mod
else:
ans = change_plus
for i in kouho:
ans = ans*i % mod
ans = ans*pow(last_plus, mod-2, mod) % mod
print(ans)
main()
| 0 | null | 30,307,930,442,692 | 197 | 112 |
n = int(input())
def make_divisors(n):
lower_divisors , upper_divisors = [], []
i = 1
while i*i <= n:
if n % i == 0:
lower_divisors.append(i)
if i != n // i:
upper_divisors.append(n//i)
i += 1
return lower_divisors + upper_divisors[::-1]
# その数自身
ans = 1
# 1以外のn-1の約数
ans += len(make_divisors(n-1)) - 1
d = make_divisors(n)
for k in range(1,(len(d)+1)//2):
i = d[k]
j = n//i
while j % i == 0:
j //= i
if j % i == 1:
ans += 1
print(ans)
|
X = int(input())
a = X // 100
b = X % 100
m = 5*a
if b > m:
print(0)
else:
print(1)
| 0 | null | 84,577,848,362,652 | 183 | 266 |
S = input()
T = input()
ans = 0
for i in range(len(S)):
if S[i] == T[i]:
continue
ans += 1
print(ans)
|
s=list(input())
count=0
if 'R' in s:
count+=1
for i in range(len(s)-1):
if s[i]==s[i+1] and s[i]=='R':
count+=1
print(count)
| 0 | null | 7,622,153,037,312 | 116 | 90 |
#
# abc165 c
#
import sys
from io import StringIO
import unittest
sys.setrecursionlimit(100000)
class TestClass(unittest.TestCase):
def assertIO(self, input, output):
stdout, stdin = sys.stdout, sys.stdin
sys.stdout, sys.stdin = StringIO(), StringIO(input)
resolve()
sys.stdout.seek(0)
out = sys.stdout.read()[:-1]
sys.stdout, sys.stdin = stdout, stdin
self.assertEqual(out, output)
def test_入力例_1(self):
input = """3 4 3
1 3 3 100
1 2 2 10
2 3 2 10"""
output = """110"""
self.assertIO(input, output)
def test_入力例_2(self):
input = """4 6 10
2 4 1 86568
1 4 0 90629
2 3 0 90310
3 4 1 29211
3 4 3 78537
3 4 2 8580
1 2 1 96263
1 4 2 2156
1 2 0 94325
1 4 3 94328"""
output = """357500"""
self.assertIO(input, output)
def test_入力例_3(self):
input = """10 10 1
1 10 9 1"""
output = """1"""
self.assertIO(input, output)
def resolve():
global N, M
N, M, Q = map(int, input().split())
ABCD = [list(map(int, input().split())) for _ in range(Q)]
T = []
func([1], T)
ans = 0
for t in T:
score = 0
for abcd in ABCD:
a, b, c, d = abcd
if t[b-1] - t[a-1] == c:
score += d
ans = max(ans, score)
print(ans)
def func(L, T):
if len(L) == N:
T.append(L)
return
for i in range(L[-1], M+1):
NL = L + [i]
func(NL, T)
if __name__ == "__main__":
# unittest.main()
resolve()
|
def backtrack(nums, N, cur_state, result):
if len(cur_state) == N:
result.append(cur_state[:])
else:
for i in range(len(nums)):
cur_state.append(nums[i])
backtrack(nums[i:], N, cur_state, result)
cur_state.pop()
def solve():
N,M,Q = [int(i) for i in input().split()]
quads = []
for i in range(Q):
quads.append([int(i) for i in input().split()])
candidates = []
backtrack(list(range(1, M+1)), N, [], candidates)
ans = 0
for candidate in candidates:
score = 0
for a,b,c,d in quads:
if candidate[b-1] - candidate[a-1] == c:
score += d
ans = max(score, ans)
print(ans)
if __name__ == "__main__":
solve()
| 1 | 27,403,350,528,202 | null | 160 | 160 |
n,k = map(int,input().split())
l = list(map(int,input().split()))
l.sort()
cnt = 0
s = 0
while(cnt < k):
s = s+l[cnt]
cnt = cnt+1
print(s)
|
cnt=0
def merge(a,left,mid,right):
n1=mid-left
n2=right-mid
l=a[left:mid]
r=a[mid:right]
l.append(float("inf"))
r.append(float("inf"))
i=0
j=0
for k in range(left,right):
global cnt
cnt+=1
if(l[i] <= r[j]):
a[k] = l[i]
i+=1
else:
a[k]=r[j]
j+=1
def merge_sort(a,left,right):
if(left+1<right):
mid=(left+right)//2
merge_sort(a,left,mid)
merge_sort(a,mid,right)
merge(a,left,mid,right)
n=int(input())
s=input()
a=list(map(int,s.split()))
merge_sort(a,0,n)
a=map(str,a)
print(" ".join(list(a)))
print(cnt)
| 0 | null | 5,823,284,286,912 | 120 | 26 |
n, k = map(int, input().split())
MOD = 10 ** 9 + 7
cnt = [0] * (k + 1)
for num in range(1, k + 1):
cnt[num] = pow((k // num), n, MOD)
for num in range(1, k + 1)[::-1]:
for i in range(2, k // num + 1):
cnt[num] -= cnt[num * i]
cnt[num] % MOD
ans = 0
for num in range(1, k + 1):
ans += cnt[num] * num
ans %= MOD
print(ans)
|
def testfunc(start,end,target):
cnt=0
for num in range(start,end+1):
if target % num ==0:
cnt+=1
print(cnt)
tmp=list(map(int,input().split()))
testfunc(tmp[0],tmp[1],tmp[2])
| 0 | null | 18,665,952,123,268 | 176 | 44 |
# -*- coding: utf-8 -*-
"""
Created on Thu May 7 23:19:34 2020
@author: Kanaru Sato
"""
def dfs(v):
global t
d[v] = t
t += 1
if conlist[v]:
for connectedv in conlist[v]:
if d[connectedv] == -1:
dfs(connectedv)
f[v] = t
t += 1
n = int(input())
conlist = [[] for i in range(n)]
for i in range(n):
_ = list(map(int,input().split()))
[conlist[i].append(_[k]-1) for k in range(2,_[1]+2)]
d = [-1 for i in range(n)]
f = [-1 for i in range(n)]
t = 1
for i in range(n):
if d[i] == -1:
dfs(i)
for i in range(n):
print(i+1,d[i],f[i])
|
s, t = input().split()
ans = '{}{}'.format(t, s)
print(ans)
| 0 | null | 51,363,956,165,142 | 8 | 248 |
n = int(input())
s = input()
t = ""
for i in range(n // 2):
t += s[i]
if s == (t + t):
print('Yes')
else:
print('No')
|
N = int(input())
S = str(input())
flag = False
half = (N+1) // 2
if S[:half] == S[half:N]:
flag = True
if flag:
print("Yes")
else:
print("No")
| 1 | 146,914,853,090,108 | null | 279 | 279 |
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")
|
n = int(input())
a = list(map(int,input().split()))
q = int(input())
table = {}
sum = 0
for i in range(n):
sum += a[i]
if a[i] not in table.keys():
table[a[i]] = 1
else:
table[a[i]] += 1
for i in range(q):
b,c = map(int,input().split())
if b in table.keys():
sum -= b * table[b]
sum += c * table[b]
if c not in table.keys():
table[c] = table[b]
else:
table[c] += table[b]
table[b] = 0
print(sum)
| 0 | null | 32,686,159,278,288 | 199 | 122 |
s=input()
stack=[]
tmpans=[]
ans=[]
for k,v in enumerate(s):
if v=="\\":
stack.append(k)
elif v=="/" and len(stack)>0:
p=stack.pop()
a=k-p
while len(tmpans)>0 and tmpans[-1][-1]>p:
a+=tmpans[-1][0]
tmpans.pop()
tmpans.append([a,p])
x=[i[0] for i in tmpans]
print(sum(x))
print(len(x),*x)
|
def main():
sectionalview = input()
stack = []
a_surface = 0
surfaces = []
for cindex in range(len(sectionalview)):
if sectionalview[cindex] == "\\":
stack.append(cindex)
elif sectionalview[cindex] == "/" and 0 < len(stack):
if 0 < len(stack):
left_index = stack.pop()
a = cindex - left_index
a_surface += a
while 0 < len(surfaces):
if left_index < surfaces[-1][0]:
a += surfaces.pop()[1]
else:
break
surfaces.append((cindex, a))
t = [i[1] for i in surfaces]
print(sum(t))
print(" ".join(map(str, [len(t)] + t)))
if __name__ == "__main__":
import os
import sys
if len(sys.argv) > 1:
if sys.argv[1] == "-d":
fd = os.open("input.txt", os.O_RDONLY)
os.dup2(fd, sys.stdin.fileno())
main()
else:
main()
| 1 | 57,785,771,068 | null | 21 | 21 |
while True:
n, x = map(int, raw_input().split())
if(n == 0 and x == 0):
break
count = 0
for i in range(1, n - 1):
for j in range(i + 1, n):
for k in range(j + 1, n + 1):
if(i + j + k == x):
count += 1
print(count)
|
import math
while True:
n,x=list(map(int,input().split()))
if n==x==0: break
c=0
for i in range(1,x//3):
if i>n: break
for j in range(i+1,(x-i)//2+1):
if j>n: break
k=x-i-j
if not (n<k or k<=j):
c+=1
print(c)
| 1 | 1,272,123,627,200 | null | 58 | 58 |
x = input()
if len(x) < 3:
print(0)
exit()
k = int(x[0:-2])
x = int(x[-2:])
k -= x / 5
if x % 5 == 0 and k >= 0:
print(1)
elif k > 0:
print(1)
else:
print(0)
|
X = int(input())
a = X // 100
b = X % 100
m = 5*a
if b > m:
print(0)
else:
print(1)
| 1 | 127,420,109,788,982 | null | 266 | 266 |
k = int(input())
s = input()
a = len(s) - k
if a <= 0:
print(s)
else:
#a >0
print(s[:k] + "...")
|
a = int(input())
res = 0
for i in range(a+1):
if i % 3 ==0 or i % 5 ==0:
pass
else:
res += i
print(res)
| 0 | null | 27,053,783,590,538 | 143 | 173 |
N = int(input())
print(N // 2 - int(N % 2 == 0))
|
import math
a = math.ceil(int(input())/2)
print(int(a-1))
| 1 | 153,082,039,924,828 | null | 283 | 283 |
def main():
input()
in_a = list(map(int, input().split()))
leaves = [0] * len(in_a)
leaves[-1] = in_a[-1]
if in_a[0] > 1:
print(-1)
return
#for i in range(len(in_a)-2, -1, -1):
# leaves[i] = leaves[i+1] + in_a[i]
for i in range(len(in_a) - 1, 1, -1):
leaves[i-1] = leaves[i] + in_a[i-1]
node = 1
total = 1
for i in range(1, len(in_a)):
n = (node - in_a[i-1]) * 2
if n < in_a[i]:
print(-1)
return
node = min(n, leaves[i])
total += node
print(total)
if __name__ == '__main__':
main()
|
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))
| 1 | 18,938,107,016,544 | null | 141 | 141 |
import math
n = int(input())
s = 0
for i in range(1,n+1):
for j in range(1,n+1):
for k in range(1,n+1):
s += math.gcd(math.gcd(i,j),k)
print(s)
|
N = int(input())
kotae = []
for i in range(1,N+1):
for j in range(1,N+1):
for k in range(1,N+1):
s = i
t = j
u = k
x = 1
while x != 0:
v = t%s
if v != 0:
t = s
s = v
else:
t = s
x = u%t
if x != 0:
u = t
t = x
else:
kotae.append(t)
print(sum(kotae))
| 1 | 35,535,035,539,356 | null | 174 | 174 |
import sys
import numpy as np
def Ii():return int(sys.stdin.buffer.readline())
def Mi():return map(int,sys.stdin.buffer.readline().split())
def Li():return list(map(int,sys.stdin.buffer.readline().split()))
n = Ii()
a = Li()
b = [0]*(n+1)
bm = [0]*(n+1)
b[0] = 1
if a[0] > 1:
print(-1)
exit(0)
bm[-1] = a[-1]
for i in range(1,n+1):
bm[-1-i] = bm[-i]+a[-i-1]
b[i] = (b[i-1]-a[i-1])*2
b = np.array(b)
bm = np.array(bm)
b = np.amin([[b],[bm]], axis=0)
if a[-1] != b[-1,-1]:
print(-1)
exit(0)
if np.any(b <= 0):
print(-1)
exit(0)
print(np.sum(b))
|
n, m = map(int, input().split())
if m%2 == 0:
x = m//2
y = m//2
else:
x = m//2
y = m//2+1
for i in range(x):
print(i+1, 2*x+1-i)
for i in range(y):
print(i+2*x+2, 2*y+2*x+1-i)
| 0 | null | 23,911,524,422,340 | 141 | 162 |
n = int(input())
tbl = [[0]*10 for _ in [0]*10]
k = 10
for i in range(1, n+1):
if i // k: k *= 10
l = i*10//k
r = i%10
tbl[l][r] += 1
ans = 0
for l, _t in enumerate(tbl):
for r, t in enumerate(_t):
if l == r:
ans += t*t
else:
ans += t*tbl[r][l]
print(ans)
|
N=int(input())
import copy
raw=[0]*10
a=[]
for i in range(10):
a.append(copy.deepcopy(raw))
ans=0
#print(a)
for i in range(1,N+1):
temp=str(i)
#print(temp)
a[int(temp[0])][int(temp[-1])]+=1
#print(a)
for i in range(10):
for j in range(10):
ans+=a[i][j]*a[j][i]
print(ans)
| 1 | 86,447,222,838,368 | null | 234 | 234 |
import sys
s = sys.stdin.readline()
print(s.swapcase(),end='')
|
K, N = [int(a) for a in input().split()]
A = [int(a) for a in input().split()]
max_diff = 0
dsum = 0
for i in range(len(A)):
if i < len(A)-1:
diff = A[i+1] - A[i]
else:
diff = K - A[i] + A[0]
dsum += diff
if diff > max_diff:
max_diff = diff
ans = dsum - max_diff
print(ans)
| 0 | null | 22,471,272,271,460 | 61 | 186 |
import math
def modpow(a, n, p):
if n == 0:
return 1
if n == 1:
return a % p
elif n % 2 == 1:
return a * modpow(a, n-1, p) % p
else:
return modpow(a, n//2, p) **2 % p
MOD = 1000000007
n = int(input())
#初期値が0の辞書
from collections import defaultdict
pp = defaultdict(lambda: 0)
pm = defaultdict(lambda: 0)
def seiki(x,y):
tmp=math.gcd(x,y)
return (x//tmp,y//tmp)
a0b0 = 0
for i in range(n):
a, b = map(int, input().split())
if a == 0 and b == 0:
a0b0 += 1
elif a == 0:
pp[(0,1)] += 1
elif b == 0:
pm[(0,1)] += 1
elif a*b>0:
tmp = seiki(abs(a),abs(b))
pp[tmp] += 1
else:
tmp = seiki(abs(b),abs(a))
pm[tmp] += 1
ans = 1
for key in set(pp.keys()) | set(pm.keys()):
pp_v=pp[key]
pm_v=pm[key]
ans*=(modpow(2,pm_v,MOD)+modpow(2,pp_v,MOD)-1)
ans%=MOD
ans = (ans+a0b0-1) % MOD
print(ans)
|
from collections import defaultdict
MOD = (10 ** 9) + 7
assert MOD == 1000000007
def gcd(a, b):
if a % b == 0:
return b
return gcd(b, a % b)
n = int(raw_input())
ns = []
d = defaultdict(int)
zero = 0
for i in xrange(n):
a, b = map(int, raw_input().split())
ns.append((a, b))
if a and b:
s = 1 if a * b >= 0 else -1
g = gcd(abs(a), abs(b))
m1 = (s * abs(a) / g, abs(b) / g)
m2 = (-s * abs(b) / g, abs(a) / g)
elif a == 0 and b == 0:
zero += 1
continue
elif a == 0:
m1 = (1, 0)
m2 = (0, 1)
elif b == 0:
m1 = (0, 1)
m2 = (1, 0)
d[m1] += 1
d[m2] += 0
'''
res = 0
for i in xrange(1 << n):
fs = []
flag = True
for j in xrange(n):
if i & (1 << j):
fs.append(ns[j])
for i, f1 in enumerate(fs):
for f2 in fs[i + 1:]:
(a1, b1) = f1
(a2, b2) = f2
if a1 * a2 + b1 * b2 == 0:
flag = False
break
if not flag:
break
if flag:
res += 1
print res - 1
'''
pre = 1
for k in d.keys():
if k[0] <= 0:
assert (k[1], -k[0]) in d
continue
else:
k1 = k
k2 = (-k[1], k[0])
tot = pow(2, d[k1], MOD) + pow(2, d[k2], MOD) - 1
pre = pre * tot % MOD
print (pre - 1 + zero + MOD) % MOD
| 1 | 21,019,890,710,310 | null | 146 | 146 |
from math import gcd
from functools import reduce
from collections import defaultdict
n=int(input())
a=list(map(int,input().split()))
if reduce(gcd,a)!=1:
print("not coprime")
exit()
def primes(x):
ret=[]
for i in range(1,int(x**0.5)+1):
if x%i==0:
ret.append(i)
if x//i!=i:
ret.append(x//i)
ret.sort()
return ret[1:]
s=set()
for q in a:
p=primes(q)
for j in p:
if j in s:
print("setwise coprime")
exit()
s.add(j)
print("pairwise coprime")
|
from sys import stdin
import math
import re
import queue
input = stdin.readline
MOD = 1000000007
INF = 122337203685477580
def solve():
str = (input().rstrip())
str = str.replace("hi","")
if(len(str) == 0):
print("Yes")
else:
print("No")
if __name__ == '__main__':
solve()
| 0 | null | 28,808,940,109,742 | 85 | 199 |
n=int(input())
vec=[0 for _ in range(10050)]
for x in range(1,105):
for y in range (1,105):
for z in range (1 , 105):
sum= x*x + y*y + z*z + x*y +y*z + z*x
if sum<10050:
vec[sum]+=1
for i in range(1,n+1):
print(vec[i])
|
N = int(input())
ans = [0]*(1+N)
for x in range(1, 10**2+1):
for y in range(1, 10**2+1):
for z in range(1, 10**2+1):
v = x*x+y*y+z*z+x*y+y*z+z*x
if v<=N:
ans[v] += 1
for i in range(1, N+1):
print(ans[i])
| 1 | 7,908,128,011,602 | null | 106 | 106 |
n = int(input())
a = list(map(int, input().split()))
a = [(a[i], i) for i in range(n)]
a.sort(key=lambda x: -x[0])
dp = [[0 for _ in range(n+1)] for _ in range(n+1)]
for x in range(n+1):
for y in range(n+1):
if x + y > n:
break
active, idx = a[x+y-1]
if x > 0:
dp[x][y] = max(dp[x][y],
dp[x-1][y] + active * abs(idx - (x-1)))
if y > 0:
dp[x][y] = max(dp[x][y],
dp[x][y-1] + active * abs(idx - (n-y)))
ans = 0
for x in range(n+1):
ans = max(ans, dp[x][n-x])
print(ans)
|
A,B,C = map(int,input().split())
L = [i for i in range(A,B + 1)]
_ = 0
for l in L:
if l % C == 0:
_ += 1
print(_)
| 0 | null | 20,670,574,304,690 | 171 | 104 |
s=int(input())
MOD=10**9+7
fact=[0]*s
invfact=[0]*s
fact[0]=1
for i in range(1,s):
fact[i]=fact[i-1]*i % MOD
invfact[s-1]=pow(fact[s-1],MOD-2,MOD)
for i in range(s-1,0,-1):
invfact[i-1]=invfact[i]*i%MOD
def nCk(n, k):
return fact[n]*invfact[k]*invfact[n-k]
ans = 0
for n in range(1,s):
if 3*n>s:
break
ans+=nCk(s-2*n-1,n-1)
ans%=MOD
print(ans)
|
def make_factorial_table(n):
result = [0] * (n + 1)
result[0] = 1
for i in range(1, n + 1):
result[i] = result[i - 1] * i % m
return result
def mcomb(n, k):
if n == 0 and k == 0:
return 1
if n < k or k < 0:
return 0
return fac[n] * pow(fac[n - k], m - 2, m) * pow(fac[k], m - 2, m) % m
m = 1000000007
S = int(input())
fac = make_factorial_table(S + 10)
#print(fac)
#print(len(fac))
result = 0
for i in range(1, S // 3 + 1):
result += mcomb(S - i * 3 + i - 1, i - 1)
result %= m
print(result)
| 1 | 3,335,605,335,822 | null | 79 | 79 |
M1, D1 = [int(x) for x in input().split()]
M2, D2 = [int(x) for x in input().split()]
print(0 if M1 == M2 else 1)
|
class Dice:
def __init__(self,d1,d2,d3,d4,d5,d6):
self.dice = [d1,d2,d3,d4,d5,d6]
def turn(self,dir):
if dir == 'S':
self.dice = [self.dice[4],self.dice[0],self.dice[2],self.dice[3],self.dice[5],self.dice[1]]
if dir == 'N':
self.dice = [self.dice[1],self.dice[5],self.dice[2],self.dice[3],self.dice[0],self.dice[4]]
if dir == 'W':
self.dice = [self.dice[2],self.dice[1],self.dice[5],self.dice[0],self.dice[4],self.dice[3]]
if dir == 'E':
self.dice = [self.dice[3],self.dice[1],self.dice[0],self.dice[5],self.dice[4],self.dice[2]]
def check(m):
for b in range(4):
m.turn('N')
if m.dice[0] == a[0] and m.dice[1] == a[1]:
return(m.dice[2])
dice_num = input().split()
q = int(input())
conditions = []
for a in range(q):
temp = input().split()
conditions.append(temp)
init_Dice = Dice(dice_num[0],dice_num[1],dice_num[2],dice_num[3],dice_num[4],dice_num[5])
kaitou = []
for a in conditions:
if check(init_Dice):
print(check(init_Dice))
continue
init_Dice.turn('E')
if check(init_Dice):
print(check(init_Dice))
continue
init_Dice.turn('E')
if check(init_Dice):
print(check(init_Dice))
continue
init_Dice.turn('E')
if check(init_Dice):
print(check(init_Dice))
continue
init_Dice.turn('W')
init_Dice.turn('N')
init_Dice.turn('E')
if check(init_Dice):
print(check(init_Dice))
continue
init_Dice.turn('E')
if check(init_Dice):
print(check(init_Dice))
continue
init_Dice.turn('E')
if check(init_Dice):
print(check(init_Dice))
continue
init_Dice.turn('E')
if check(init_Dice):
print(check(init_Dice))
continue
| 0 | null | 62,226,848,731,168 | 264 | 34 |
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
from copy import deepcopy
def make_divisors(n):
divisors = []
for i in range(1, int(n**0.5)+1):
if n % i == 0:
divisors.append(i)
if i != n // i:
divisors.append(n//i)
# divisors.sort()
return divisors
n = int(readline())
ans = len(make_divisors(n - 1))
for check in make_divisors(n):
if check == 1:
continue
nn = deepcopy(n)
while nn % check == 0:
nn //= check
if nn % check == 1:
ans += 1
print(ans - 1)
|
from sys import stdin
def make_divisors(n):
mod0 = set()
mod1 = set()
for i in range(2, int(n**0.5)+1):
if n%i==0:
mod0.add(i)
mod0.add(n/i)
if (n-1)%i==0:
mod1.add(i)
mod1.add((n-1)/i)
return mod0,mod1
N=list(map(int,(stdin.readline().strip().split())))
num=N[0]
if num==2:print(1)
else:
K=0
mod0,mod1=(make_divisors(num))
# mod0.remove(1)
# mod1.remove(1)
for i in mod0:
num = N[0]
while(num % i == 0):
num/=i
if num%i==1:
K+=1
K+=len(mod1)+2
print(K)
| 1 | 41,349,233,710,208 | null | 183 | 183 |
N=int(input())
A=list(map(int,input().split()))
s=sum(A)
ans=1
eda=1
if N==0:
if A[0]==1:
print(1)
else:
print(-1)
exit()
if A[0]!=0:
print(-1)
exit()
for i in range(1,N+1):
if eda*2<=s:
eda*=2
else:
eda=s
ans+=eda
eda-=A[i]
s-=A[i]
if eda<=0 and i != N:
print(-1)
exit()
if eda!=0:
print(-1)
exit()
print(ans)
|
n = int(input())
a = list(map(int, input().split()))
max_node = [0 for _ in range(n+1)]
for i in range(n-1, -1, -1):
max_node[i] = max_node[i+1] + a[i+1]
ans = 1
node = 1
for i in range(n+1):
node -= a[i]
if (i < n and node <= 0) or node < 0:
print(-1)
exit(0)
node = min(node * 2, max_node[i])
ans += node
print(ans)
| 1 | 18,803,950,006,720 | null | 141 | 141 |
N=int(input())
cnt=0
ans="No"
for i in range(N):
x,y=input().split()
if x==y:
cnt=cnt+1
else:
cnt=0
if cnt>=3:
ans="Yes"
print(ans)
|
def e_handshake():
import numpy as np
N, M = [int(i) for i in input().split()]
A = np.array(input().split(), np.int64)
A.sort()
def shake_cnt(x):
# 幸福度が x 以上となるような握手を全て行うときの握手の回数
# 全体から行わない握手回数を引く
return N**2 - np.searchsorted(A, x - A).sum()
# 幸福度の上昇が right 以上となるような握手はすべて行い、
# left となるような握手はいくつか行って握手回数が M 回になるようにする
left = 0
right = 10 ** 6
while right - left > 1:
mid = (left + right) // 2
if shake_cnt(mid) >= M:
left = mid
else:
right = mid
# 幸福度が right 以上上がるような握手をすべて行ったとして、回数と総和を計算
X = np.searchsorted(A, right - A) # 行わない人数
Acum = np.zeros(N + 1, np.int64) # 累積和で管理する
Acum[1:] = np.cumsum(A)
happiness = (Acum[-1] - Acum[X]).sum() + (A * (N - X)).sum()
# 幸福度が right 未満の上昇となる握手を、握手回数が M になるまで追加で行う
shake = N * N - X.sum()
happiness += (M - shake) * left
return happiness
print(e_handshake())
| 0 | null | 55,544,666,077,208 | 72 | 252 |
answer = [True] * 3
a = int(input())
b = int(input())
answer[a - 1] = False
answer[b - 1] = False
for i, ans in enumerate(answer):
if ans:
print(i + 1)
|
a = int(input())
b = int(input())
if a == 1 and b == 2:
print(3)
elif a == 1 and b == 3:
print(2)
elif a == 2 and b == 1:
print(3)
elif a == 2 and b == 3:
print(1)
elif a == 3 and b == 1:
print(2)
elif a == 3 and b == 2:
print(1)
| 1 | 110,791,598,430,172 | null | 254 | 254 |
#!/usr/bin/env python3
from collections import deque
def main():
N = int(input())
adj = [[] for _ in range(N)]
for i in range(N-1):
a, b = map(lambda x: int(x)-1, input().split())
adj[a].append((b,i))
adj[b].append((a,i))
p = max([len(a) for a in adj])
ans = [-1] * (N-1)
visited = [-1] * N
visited[0] = 0
q = deque([0])
while q:
now = q.popleft()
tmp = 0
for v in adj[now]:
if visited[v[0]] == 0:
tmp = ans[v[1]]
break
for v in adj[now]:
if visited[v[0]] < 0:
q.append(v[0])
ans[v[1]] = (tmp+1) % p
tmp += 1
visited[v[0]] = 0
if ans[v[1]] == 0:
ans[v[1]] += p
print(p)
[print(a) for a in ans]
if __name__ == "__main__":
main()
|
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**7)
import bisect
import heapq
import itertools
import math
import numpy as np
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from math import gcd
from operator import add, itemgetter, mul, xor
def cmb(n,r,mod):
bunshi=1
bunbo=1
for i in range(r):
bunbo = bunbo*(i+1)%mod
bunshi = bunshi*(n-i)%mod
return (bunshi*pow(bunbo,mod-2,mod))%mod
mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]
n = I()
#graph[i]には頂点iと繋がっている頂点を格納する
graph = [[] for _ in range(n+1)]
prev_col = [0]*(n+1)
a_b = []
k=0
for i in range(n-1):
a,b = MI()
a_b.append(str(a)+" "+str(b))
graph[a].append(b)
graph[b].append(a)
#check[i] == -1ならば未探索
check = [-1]*(n+1)
check[0] = 0
check[1] = 0
for i in range(n+1):
k = max(len(graph[i]),k)
d = deque()
d.append(1)
ans = dict()
while d:
v = d.popleft()
check[v] = 1
cnt = 0
for i in graph[v]:
if check[i] != -1:
continue
cnt = cnt+1
if prev_col[v] == cnt:
cnt = cnt + 1
prev_col[i] = cnt
ans[str(min(i,v))+" "+str(max(i,v))]=cnt
d.append(i)
print(k)
for key in a_b:
print(ans[key])
| 1 | 135,525,291,272,906 | null | 272 | 272 |
import bisect
N = int(input())
L = sorted([int(i) for i in input().split()])
ans = 0
for j in range(N-2):
for k in range(j+1,N-1):
S = L[j] + L[k]
a = bisect.bisect_left(L, S)
ans += a - 1 - k
print(ans)
|
import bisect
def LI():
return list(map(int, input().split()))
N = int(input())
L = LI()
L.sort()
ans = 0
for i in range(N-1, 1, -1):
for j in range(i-1, 0, -1):
ind = bisect.bisect_right(L, L[i]-L[j])
if ind < j:
ans += j-ind
print(ans)
| 1 | 171,351,572,227,540 | null | 294 | 294 |
X, Y = map(int, input().split())
print(400000*(X==1)*(Y==1) + max(0, 400000-100000*X) + max(0, 400000-100000*Y))
|
n, x, m = map(int, input().split())
flag = [False]*m
a = []
while not flag[x]:
flag[x] = True
a.append(x)
x = pow(x, 2, m)
loop_start_idx = a.index(x)
loop_len = len(a) - loop_start_idx
loop_count = (n - loop_start_idx)//loop_len
loop_amari = (n-loop_start_idx)%loop_len
ans = sum(a[:loop_start_idx])
ans += sum(a[loop_start_idx:])*loop_count
ans += sum(a[loop_start_idx: loop_start_idx + loop_amari])
print(ans)
| 0 | null | 71,507,260,962,368 | 275 | 75 |
n, k = map(int, input().split())
arr = list(map(int, input().split()))
expected = []
cumsum = []
for x in arr:
sum = (x * (x + 1)) // 2
ex = sum / x
expected.append(ex)
sum = 0
maxi = 0
taken = 0
bad = 0
for x in expected:
sum += x
taken += 1
if taken == k:
maxi = max(maxi, sum)
elif taken > k:
sum -= expected[bad]
bad += 1
maxi = max(maxi, sum)
print(maxi)
|
def main():
height, width, strawberries = map(int, input().split())
cake = [list(input()) for _ in range(height)]
answer = [[0 for _ in range(width)] for _ in range(height)]
strawberry_num = 0
for i in range(height):
is_exist_strawberry = False
for j in range(width):
if cake[i][j] == "#":
is_exist_strawberry = True
strawberry_num += 1
answer[i][j] = strawberry_num
elif is_exist_strawberry:
answer[i][j] = strawberry_num
row_with_strawberry = -1
for i in range(height):
before_index = 0
for j in range(-1, -width - 1, -1):
if answer[i][j] == 0:
answer[i][j] = before_index
else:
before_index = answer[i][j]
if row_with_strawberry == -1:
row_with_strawberry = i
for i in range(row_with_strawberry):
answer[i] = answer[row_with_strawberry]
for i in range(row_with_strawberry, height):
if answer[i][0] == 0:
answer[i] = answer[i - 1]
for ans in answer:
print(" ".join(map(str, ans)))
if __name__ == '__main__':
main()
| 0 | null | 109,294,972,649,512 | 223 | 277 |
n = int(input())
a = list(map(int, input().split()))
def judge(x, y):
if x > y:
return(x-y)
else:
return(0)
ans = 0
for i in range(n - 1):
xy = judge(a[i], a[i+1])
a[i+1] += xy
ans += xy
print(ans)
|
# -*- coding: utf-8 -*-
N=int(input())
As=list(map(int, input().split()))
k=0
k1=As[0]
g=0
for i in range(2,N+1):
if i%2==1:
k=max(k+As[i-1],g)
k1=k1+As[i-1]
if i%2==0:
g=max(k1,g+As[i-1])
#print(k,k1,g)
ans=g if N%2==0 else k
print(ans)
| 0 | null | 20,998,153,962,522 | 88 | 177 |
n = raw_input()
A = map(int, raw_input().split())
for i, key in enumerate(A):
j = i - 1
while(j >= 0 and A[j] > key):
A[j+1] = A[j]
j -= 1
A[j+1] = key
for x in A:
print x,
print
|
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10**9 + 7
a.sort()
fac = [1] * (n + 1)
inv = [1] * (n + 1)
def COMinit():
for j in range(1, n + 1):
fac[j] = fac[j - 1] * j % mod
inv[n] = pow(fac[n], mod - 2, mod)
for j in range(n - 1, -1, -1):
inv[j] = inv[j + 1] * (j + 1) % mod
def comb2(n, r):
if r > n or n < 0 or r < 0:
return 0
return fac[n] * inv[n - r] * inv[r] % mod
COMinit()
ans = 0
for i in range(n):
x = comb2(n-1-i, k-1)
ans = (ans - a[i]*x % mod) % mod
ans = (ans + a[-1-i]*x % mod) % mod
print(ans%mod)
| 0 | null | 47,922,103,789,570 | 10 | 242 |
# Skill Up
n, m, x = map(int, input().split())
ca = [list(map(int, input().split())) for _ in range(n)]
costs = []
for i in range(1 << n): #全パターン
cost = 0
total = [0] * m #値段とm冊の本についてのリスト
for j in range(n):
if (i>>j) & 1 == 1 : #iをjだけ右にシフト。j冊目を買う場合
cost += ca[j][0]
for k in range(m):
total[k] += ca[j][k+1]
if all(total[l] >= x for l in range(m)):
costs.append(cost)
if len(costs) == 0:
print(-1)
else:
print(min(costs))
|
from collections import deque
def bfs(G,dist,s):
q = deque([])
dist[s] = 0
q.append(s)
while len(q) > 0:
v = q.popleft()
for nv in G[v]:
if dist[nv] != -1:
continue
dist[nv] = dist[v] + 1
q.append(nv)
n,m = map(int,input().split())
G = [[] for _ in range(n)]
for i in range(m):
a,b = map(int,input().split())
G[a-1].append(b-1)
G[b-1].append(a-1)
dist = [-1]*n
cnt = 0
for v in range(n):
if dist[v] != -1:
continue
bfs(G,dist,v)
cnt += 1
print(cnt-1)
| 0 | null | 12,197,546,160,672 | 149 | 70 |
from heapq import heapify,heappush,heappop
N, M = map(int, input().split())
I = [[] for _ in range(N)]
for i in range(M):
A, B = map(int, input().split())
A -= 1
B -= 1
I[A].append(B)
I[B].append(A)
task = []
used = [0 for _ in range(N)]
min_len = [0 for _ in range(N)]
length = [10**20 for _ in range(N)]
prev_points = [0 for _ in range(N)]
heappush(task, (0, 0, -1))
print("Yes")
while task:
while task:
l, p, prev = heappop(task)
if used[p] == 0:
break
#print(task)
used[p] = 1
min_len[p] = l
prev_points[p] = prev +1 #番号を1ずらす
for j in I[p]:
if used[j] == 1: continue
#print(p,j)
if length[j] > l+1:
length[j] = l+1
heappush(task, (l+1, j, p))
for i in range(1, N):
print(prev_points[i])
|
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
N,M = map(int,input().split())
AB = [tuple(map(int,input().split())) for i in range(M)]
es = [[] for _ in range(N)]
for a,b in AB:
a,b = a-1,b-1
es[a].append(b)
es[b].append(a)
prev = [-1] * N
visited = [0] * N
visited[0] = 1
from collections import deque
q = deque([0])
while q:
v = q.popleft()
for to in es[v]:
if visited[to]: continue
visited[to] = 1
prev[to] = v
q.append(to)
print('Yes')
for p in prev[1:]:
print(p+1)
| 1 | 20,603,779,440,420 | null | 145 | 145 |
N, K = map(int, input().split())
MOD = 10**9+7
mx = [0]*(K+1)
ans = 0
for i in range(K, 0, -1):
mx[i] = pow(K//i, N, MOD)
for j in range(2*i, K+1, i):
mx[i] -= mx[j]
ans += i*mx[i]
print(ans%MOD)
|
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
L = [a+i for a, i in enumerate(A, 1)]
R = [a-i for a, i in enumerate(A, 1)]
ans = 0
R_c = Counter(R)
for l in L:
ans += R_c[l]
print(ans)
| 0 | null | 31,267,061,650,160 | 176 | 157 |
N,T = map(int,input().split())
X = []
for i in range(N):
a,b = map(int,input().split())
X.append((a,b))
X.sort()
#print(X)
A = [];B = []
for i in range(N):
A.append(X[i][0])
B.append(X[i][1])
dp = [[0 for _ in range(T)] for _ in range(N+1)]
#dp[i][j] i番目までの料理を頼んだ時j分後の最大値
ans = 0
for i in range(N):
time, aji = X[i]
for j in range(T):
if j-time >= 0:
dp[i+1][j] = max(dp[i][j-time] + aji,dp[i][j])
else:
dp[i+1][j] = dp[i][j]
if i < N-1:
last = max(B[i+1:])
else:
last = 0
#print(ans,last,dp[i+1][T-1])
ans = max(ans,dp[i+1][T-1]+last)
print(ans)
|
import sys
read = sys.stdin.buffer.read
def main():
N, T, *AB = map(int, read().split())
D = [(a, b) for a, b in zip(*[iter(AB)] * 2)]
D.sort()
dp = [[0] * T for _ in range(N + 1)]
for i, (a, b) in enumerate(D):
for t in range(T):
if 0 <= t - a:
dp[i + 1][t] = dp[i][t - a] + b
if dp[i + 1][t] < dp[i][t]:
dp[i + 1][t] = dp[i][t]
ans = 0
for i in range(N - 1):
if ans < dp[i + 1][T - 1] + D[i + 1][1]:
ans = dp[i + 1][T - 1] + D[i + 1][1]
print(ans)
return
if __name__ == '__main__':
main()
| 1 | 151,449,112,947,388 | null | 282 | 282 |
def resolve():
from collections import defaultdict
N = int(input())
A = [int(i) for i in input().split()]
L = defaultdict(int)
ans = 0
for i in range(N):
L[A[i] + i] += 1
ans += L[i - A[i]]
print(ans)
resolve()
|
def is_prime(n):
# ?´???°??????
if n == 2:
return True
elif n < 2 or n & 1 == 0:
return False
for i in range(3, int(n**0.5) + 1, 2):
if n % i == 0:
return False
return True
N = int(input())
cnt = 0
for i in range(N):
e = int(input())
if is_prime(e):
cnt += 1
print(cnt)
| 0 | null | 13,131,726,636,260 | 157 | 12 |
n,k=map(int,input().split())
a=list(map(int,input().split()))
f=list(map(int,input().split()))
a.sort()
f.sort(reverse=True)
hi=10**12
lo=0
def c(mi):
tmp=0
for i,j in zip(a,f):
if mi//j<i:
tmp+=i-mi//j
if tmp<=k:
return True
return False
while hi>=lo:
mi=(hi+lo)//2
if c(mi):
hi=mi-1
else:
lo=mi+1
print(lo)
|
import sys
sys.setrecursionlimit(10 ** 8)
def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]
def main():
N, K = ZZ()
A = sorted(ZZ(), reverse=True)
F = sorted(ZZ())
if sum(A) <= K:
print(0)
return
cost = list()
for a, f in zip(A, F): cost.append(a*f)
ok = 10 ** 12
ng = -1
while ok - ng > 1:
mid = (ok+ng)//2
cc = 0
for i in range(N): cc += max(0, (cost[i]-mid+F[i]-1)//F[i])
if cc <= K: ok = mid
else: ng = mid
print(ok)
return
if __name__ == '__main__':
main()
| 1 | 164,990,063,104,928 | null | 290 | 290 |
a, b, c = list(map(int, input().split()))
if a < b and b < c:
print("Yes")
else:
print("No")
|
x,y,z = input().split()
if x<y<z:
print('Yes')
else:
print('No')
| 1 | 376,367,343,482 | null | 39 | 39 |
a,b,k = map(int,input().split())
if a == k :
print(0,b)
elif a > k :
print(a-k,b)
elif a < k :
c = b - (k-a)
if c >= 0 :
print(0,c)
else :
print(0,0)
|
n = input()
a = list(map(int, input().split()))
b = sum(a)
sum = 0
for i in range(len(a)):
b -= a[i]
sum += a[i]*b
print(sum%(10**9+7))
| 0 | null | 53,842,674,413,792 | 249 | 83 |
s = input()
if s == 'AAA':
print('No')
elif s == 'BBB':
print('No')
else:
print('Yes')
|
def main():
H, W, M = map(int, input().split())
h_dic = [0] * H
w_dic = [0] * W
boms = set()
for _ in range(M):
h, w = map(int, input().split())
h, w = h-1, w-1
h_dic[h] += 1
w_dic[w] += 1
boms.add((h, w))
hmax = max(h_dic)
wmax = max(w_dic)
h_maxs = [idx for idx, v in enumerate(h_dic) if v==hmax]
w_maxs = [idx for idx, v in enumerate(w_dic) if v==wmax]
# 爆弾数はたかだか3*10~5なので、総当りで解ける
for h in h_maxs:
for w in w_maxs:
if (h, w) not in boms:
print(hmax + wmax)
return
print(hmax + wmax - 1)
return
if __name__ == "__main__":
main()
| 0 | null | 29,839,930,525,302 | 201 | 89 |
N = int(input())
print(N // 2 - 1 if N % 2 == 0 else (N-1) // 2)
|
N = int(input())
if N % 2 == 0:
ans = (N // 2) - 1
ans = (N // 2) - 1
else:
ans = (N - 1) // 2
print(ans)
| 1 | 153,943,443,670,048 | null | 283 | 283 |
import numpy as np
inp = input()
A, B, K = int(inp.split(' ')[0]), int(inp.split(' ')[1]), int(inp.split(' ')[2])
if K>A:
B = np.max([0, B-(K-A)])
A = np.max([0, A-K])
print(f'{A} {B}')
|
import math
N = int(input())
ans = math.floor(N / 2)
if N % 2 == 0:
print(ans - 1)
else:
print(ans)
| 0 | null | 129,191,163,443,434 | 249 | 283 |
n = int(input())
d = 'abcdefghijklm'
def conv(s):
s = list(map(lambda x: d[x], s))
return ''.join(s)
def dfs(s):
if len(s) == n:
print(conv(s))
else:
mx = max(s)+1
for i in range(mx+1):
dfs(s+[i])
dfs([0])
|
n=int(input())
d = {}
for _ in range(n):
op,s = input().split()
if op == 'insert': d[s] = 'yes'
elif op == 'find' : print(d.get(s,'no'))
| 0 | null | 26,147,404,427,708 | 198 | 23 |
x=int(input())
a=0
while True:
for b in range(10**3):
if a**5-b**5==x:
print(a,b)
exit()
elif a**5+b**5==x:
print(a,-b)
exit()
elif -a**5+b**5==x:
print(-a,-b)
exit()
a+=1
|
from collections import defaultdict
n = int(input())
a = list(map(int,input().split()))
infants = []
for i in range(n):
infants.append((a[i],i))
infants.sort()
dp = [defaultdict(int) for _ in range(n+1)]
dp[0][0] = 0
for i in range(n):
score,initial = infants.pop()
for right in dp[i].keys():
dp[i+1][right] = max(dp[i][right]+abs(initial-(i-right))*score,dp[i+1][right])
dp[i+1][right+1] = max(dp[i][right]+abs((n-1-right)-initial)*score,dp[i+1][right+1])
print(max(dp[n].values()))
| 0 | null | 29,768,598,013,220 | 156 | 171 |
N = int(input())
A = list(map(int, input().split()))
if len(A) > len(set(A)):
print("NO")
else:
print("YES")
|
import math
a, b, C = map(int, input().split())
C = C / 180 * math.pi
S = a * b * math.sin(C) / 2
print(S)
print(a + b + math.sqrt(a**2 + b**2 - 2*a*b*math.cos(C)))
print(2 * S / a)
| 0 | null | 37,100,534,493,500 | 222 | 30 |
N = int(input())
li = list(map(int, input().split()))
an = 1
mi = li[0]
for i in range(1,N):
if mi >= li[i]:
an += 1
mi = li[i]
print(an)
|
import math
n = int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
D = [abs(X[i]-Y[i]) for i in range(n)]
for p in [1, 2, 3, -1]:
if p == -1:
print(max(D))
else:
print(math.pow(sum([math.pow(D[i],p) for i in range(n)]),1/p))
| 0 | null | 42,644,862,677,960 | 233 | 32 |
mod = 998244353
N,M,K = map(int,input().split())
ans = 0
def pow(x,n):
if n == 0:
return 1
if n % 2 == 0:
return pow((x ** 2)%mod, n // 2)%mod
else:
return x * pow((x ** 2)%mod, (n - 1) // 2)%mod
A = 1
for i in range(K+1):
p = N - i
x = M*pow(M-1,p-1)*A % mod
ans += x
A = A*(N-i-1)%mod
y = pow(i+1,mod-2)
A = A*y % mod
print(ans%mod)
|
def resolve():
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
for i in range(K, N):
if A[i-K] < A[i]:
print("Yes")
else:
print("No")
if '__main__' == __name__:
resolve()
| 0 | null | 15,051,748,405,090 | 151 | 102 |
def inverse(n, mod):
return pow(n, mod - 2, mod)
def cmb(n, r, mod):
p, q = 1, 1
for i in range(r):
p = p * (n - i) % mod
q = q * (i + 1) % mod
return p * inverse(q, mod) % mod
n, a, b = map(int, input().split())
mod = 10 ** 9 + 7
ans = pow(2, n, mod) - 1
ans -= cmb(n, a, mod)
ans -= cmb(n, b, mod)
print(ans % mod)
|
from collections import defaultdict
N, P = map(int, input().split())
S = input()
"""
S = "s0 s1 ... s(N-1)"に対して"sl s(l+1) ... s(r-1)"が素数Pで割り切れるかを考える。
これはa_i := int("si s(i+1) ... s(N-1)")と定めることで、
(a_l - a_r) / 10^{N-r} ¥cong 0 (mod P)
と定式化できる。
(整数関連の区間の問題は後ろからの累積和が相性良さそう。頭からの累積を考えたのが敗因な気がする)
Pが2でも5でもない場合は、上式は
a_l - a_r ¥cong 0 (mod P)
に同値であるから、各a_iのmod Pでの値を辞書にまとめておけば良さそう。
Pが2, 5のいずれかの場合は偶然チェックが簡単なので解ける。
"""
ans = 0
if P == 2:
for i in range(N):
a = int(S[i])
if a % 2 == 0:
ans += i + 1
elif P == 5:
for i in range(N):
a = int(S[i])
if a == 0 or a == 5:
ans += i + 1
else:
S += "0" # 上の定式化の都合上、r = Nのケースが必要なので
d = defaultdict(int)
r = 0
d[0] += 1
for i in range(N - 1, -1, -1):
"""
ここでS[i:]を毎回Pで割るとTLEする(桁の大きい演算はやはり負担が大きいのか?)
(a_i % P)をi = N, N-1,...の降順に求めていくことを考えれば、
前の計算結果が使える、特に繰り返し二乗法が使えるのでだいぶ早くなるみたい。
"""
r += int(S[i]) * pow(10, N - i - 1, P)
r %= P
d[r] += 1
for num in d.values():
ans += num * (num - 1) // 2
print(ans)
| 0 | null | 62,087,334,566,868 | 214 | 205 |
import numpy as np
s=input()
k=int(input())
cnt=1
a=[]
ss=s+"."
for i in range(len(s)):
if ss[i]==ss[i+1]:
cnt+=1
else:
a.append(cnt)
cnt=1
pp=0
if s[0]==s[len(s)-1]:
pp=a[0]+a[-1]
b=[]
if len(a)>1:
b.append(pp)
b+=a[1:len(a)-1]
else:
a=[len(s)*k]
b=[]
else:
b=a
arr=np.array(a)
arr2=np.array(b)
a=arr//2
b=arr2//2
if len(s)>=2:
print(np.sum(a)+int((k-1)*np.sum(b)))
else:
print(k//2)
|
n = input()
sum_of_digits = 0
for d in n:
sum_of_digits += int(d)
print('Yes' if sum_of_digits%9 == 0 else 'No')
| 0 | null | 90,264,212,849,188 | 296 | 87 |
count={}
for i in range(97,97+26):
count[chr(i)] = 0
text=[]
while True:
try:
text.append(input())
except EOFError:
break
text_out="".join(text)
text_lower=text_out.lower()
for letter in text_lower:
if letter in count:
count[letter] += 1
for x,y in sorted(count.items()):
print("{0} : {1}".format(x,y))
|
import sys
a = sys.stdin.read()
alphabet = "abcdefghijklmnopqrstuvwxyz"
for i in alphabet:
print(i + " : " + str(a.lower().count(i)) )
| 1 | 1,668,085,956,962 | null | 63 | 63 |
import math
a, b, x = map(int, input().split())
theta = math.atan((-2) * x / (a ** 3) + 2 * b / a)
if a * math.tan(theta) > b:
theta = math.atan2(a * b * b, 2 * x)
print(math.degrees(theta))
|
import sys
import math
ct= 0
def insertion_sort(a, n, g):
global ct
for j in range(0,n-g):
v = a[j+g]
while j >= 0 and a[j] > v:
a[j+g] = a[j]
j = j-g
ct += 1
a[j+g] = v
n = int(input())
a = list(map(int, sys.stdin.readlines()))
b = 701
g = [x for x in [1,4,10,23,57,132,301,701] if x <= n]
while True:
b = math.floor(2.25*b)
if b > n:
break
g.append(b)
g = g[::-1]
for i in g:
insertion_sort(a, n, i)
print(len(g))
print(*g, sep=" ")
print(ct)
print(*a, sep="\n")
| 0 | null | 81,379,015,438,180 | 289 | 17 |
nums = input().split()
a = int(nums[0])
b = int(nums[1].replace('.', ''))
result = str(a * b)
if len(result) < 3:
print(0)
else:
print(result[:-2])
|
#input
X, N = map(int, input().split())
p = list(map(int, input().split()))
if len(p) == 0 or X < min(p) or X > max(p):
print(X)
exit()
min = X
max = X
while True:
if min not in p:
print(min)
break
elif max not in p:
print(max)
break
else:
min -= 1
max += 1
| 0 | null | 15,207,469,994,078 | 135 | 128 |
x = input()
if x == x.upper():
print("A")
else:
print("a")
|
alpha = input()
if alpha.isupper():
print('A')
elif alpha.islower():
print('a')
| 1 | 11,201,741,747,462 | null | 119 | 119 |
import math
def sd(n, students, average):
rlt = 0
for i in students:
rlt += (i - average) ** 2
rlt = rlt / n
return math.sqrt(rlt)
if __name__ == '__main__':
while True:
n = int(input())
if not n:
break
students = list(map(int, input().split()))
average = sum(students) / n
ans = sd(n, students, average)
print(round(ans, 8))
|
H, W, K = map(int, input().split())
blacks = [[None] * W for _ in range(H)]
black_sums = [[0] * W for _ in range(H)]
black_col_sums = [[0] * W for _ in range(H)]
for i in range(H):
for j, item in enumerate(input()):
if item == "#":
blacks[i][j] = 1
else:
blacks[i][j] = 0
ok_count = 0
for i in range(1<<H):
for j in range(1<<W):
# print(f"{i:b}, {j:b}")
black_count = 0
for m in range(H):
for n in range(W):
if (1 & i >> m and 1 & j >> n) == False:
continue
black_count += blacks[m][n]
if black_count == K:
ok_count += 1
print(ok_count)
| 0 | null | 4,506,940,798,140 | 31 | 110 |
n=int(input())
s=list(input())
fa=[n+1]*10
la=[-1]*10
for i in range(n):
s[i]=int(s[i])
if fa[s[i]]>n:
fa[s[i]]=i
la[s[i]]=i
ans=0
for i in range(10):
for j in range(10):
if fa[i]<la[j]:
num=[0]*10
for k in range(fa[i]+1,la[j]):
num[s[k]]=1
ans+=num.count(1)
print(ans)
|
d,t,s=map(int,input().split())
print("Yes" if t>=d/s else "No")
| 0 | null | 65,991,736,754,672 | 267 | 81 |
dp = [False] * 100200
dp[0] = True
for x in range(100000):
if dp[x]:
for i in range(100, 106):
dp[x+i] = True
print(int(dp[int(input())]))
|
str = input()
nums = str.split()
if nums[0] == "0" or nums[1] == "0":
print("error")
else:
turn = float(nums[0]) / float(nums[1]) + 0.9
if turn < 1:
turn = 1
print(int(turn))
| 0 | null | 101,761,890,070,038 | 266 | 225 |
class BIT:
def __init__(self, n):
self.node = [ 0 for _ in range(n+1) ]
def add(self, idx, w):
i = idx
while i < len(self.node) - 1:
self.node[i] += w
i |= (i + 1)
def sum_(self, idx):
ret, i = 0, idx-1
while i >= 0:
ret += self.node[i]
i = (i & (i + 1)) - 1
return ret
def sum(self, l, r):
return self.sum_(r) - self.sum_(l)
n = int(input())
s = list(input())
q = int(input())
tree = [ BIT(n) for _ in range(26) ]
for i in range(n):
tree_id = ord(s[i]) - ord("a")
tree[tree_id].add(i, 1)
for _ in range(q):
query = input().split()
com = int(query[0])
if com == 1:
idx, new_char = int(query[1]), query[2]
idx -= 1
old_char = s[idx]
new_id = ord(new_char) - ord("a")
old_id = ord(old_char) - ord("a")
tree[old_id].add(idx, -1)
tree[new_id].add(idx, 1)
s[idx] = new_char
if com == 2:
l, r = int(query[1]), int(query[2])
ret = 0
for c in range(26):
if tree[c].sum(l-1, r) > 0:
ret += 1
print(ret)
|
class SegmentTree():
'''
非再帰
segment tree
'''
def __init__(self, n, func, init=float('inf')):
'''
n->配列の長さ
func:func(a,b)->val, func=minだとRMQになる
木の高さhとすると,
n:h-1までのノード数。h段目のノードにアクセスするために使う。
data:ノード。
parent:k->child k*2+1とk*2+2
'''
self.n = 2**(n-1).bit_length()
self.init = init
self.data = [init]*(2*self.n)
self.func = func
def set(self, k, v):
'''
あたいの初期化
'''
self.data[k+self.n-1] = v
def build(self):
'''
setの後に一斉更新
'''
for k in reversed(range(self.n-1)):
self.data[k] = self.func(self.data[k*2+1], self.data[k*2+2])
def update(self, k, a):
'''
list[k]=aに更新する。
更新ぶんをrootまで更新
'''
k += self.n-1
self.data[k] = a
while k > 0:
k = (k-1)//2
self.data[k] = self.func(self.data[k*2+1], self.data[k*2+2])
def query(self, l, r):
'''
[l,r)のfuncを求める
'''
L = l+self.n
R = r+self.n
ret = self.init
while L < R:
if R & 1:
R -= 1
ret = self.func(ret, self.data[R-1])
if L & 1:
ret = self.func(ret, self.data[L-1])
L += 1
L >>= 1
R >>= 1
return ret
N = int(input())
S = input()
Q = int(input())
queries = [list(input().split()) for _ in range(Q)]
def a2n(a):
return ord(a)-ord("a")
def createInp(a):
return 1 << a2n(a)
Seg = SegmentTree(len(S), lambda x, y: x | y, init=0)
for i, s in enumerate(S):
Seg.set(i, createInp(s))
Seg.build()
for q, l, r in queries:
if q == "1":
Seg.update(int(l)-1, createInp(r))
else:
print(bin(Seg.query(int(l)-1, int(r))).count('1'))
| 1 | 62,726,594,328,288 | null | 210 | 210 |
a, b, c = map(int, input().split())
if a == b and a != c:
print('Yes')
elif b == c and b != a:
print('Yes')
elif c == a and c != b:
print('Yes')
else:
print('No')
|
import math
a,b,c = [float(s) for s in input().split()]
r = c * math.pi / 180
h = math.sin(r) * b
s = a * h / 2
x1 = a
y1 = 0
if c == 90:
x2 = 0
else:
x2 = math.cos(r) * b
y2 = h
d = math.sqrt((math.fabs(x1 - x2) ** 2) + (math.fabs(y1 - y2) ** 2))
l = a + b + d
print(s)
print(l)
print(h)
| 0 | null | 34,270,452,458,100 | 216 | 30 |
A = int(input())
B = int(input())
if A+B == 3:
print(3)
elif A+B == 4:
print(2)
else:
print(1)
|
u={1,2,3}
AB=[int(input()) for i in range(2)]
print(list(u-set(AB))[0])
| 1 | 110,199,505,286,270 | null | 254 | 254 |
def main():
N,K = map(int,input().split())
syo = N//K
amari = N%K
N = abs(amari-K)
ans = min(N,amari)
return ans
print(main())
|
while 1:
s=input();l=len(s);n=0
if'-'==s:break
for _ in range(int(input())):n+=int(input())
print((s+s)[n%l:][:l])
| 0 | null | 20,679,594,273,800 | 180 | 66 |
data = input().split()
n = data[0]
m = data[1]
if n == m:
print("Yes")
else:
print("No")
|
N, M, *PS = open(0).read().split()
N, M = [int(_) for _ in [N, M]]
cor = [0] * (N + 1)
pen = [0] * (N + 1)
for p, s in zip(PS[::2], PS[1::2]):
p = int(p)
if cor[p] == 1:
continue
if s == 'AC':
cor[p] = 1
else:
pen[p] += 1
ans = [0, 0]
for i in range(1, N + 1):
if cor[i] == 0:
continue
ans[0] += 1
ans[1] += pen[i]
print(*ans)
| 0 | null | 88,334,344,261,184 | 231 | 240 |
n= int(input())
ans=(n+1)//2
print(ans)
|
def main():
a,b = input().split()
b = b[0] + b[2:]
print(int(a)*int(b)//100)
if __name__ == "__main__":
main()
| 0 | null | 37,520,760,711,412 | 206 | 135 |
def main():
n, k = map(int, input().split())
d_lst = [0 for _ in range(k)]
a_lst = [0 for _ in range(k)]
for i in range(k):
d_lst[i] = int(input())
a_lst[i] = list(map(int, input().split()))
snuke_lst = [0 for _ in range(n)]
for i in range(k):
for a in a_lst[i]:
snuke_lst[a - 1] = 1
ans = n - sum(snuke_lst)
print(ans)
if __name__ == "__main__":
main()
|
import sys
MOD = 10**9+7
s = int(input())
dp = [0, 0, 0, 1]
ruisekiwa = [0, 0, 0, 0]
if s < 3:
print(0)
sys.exit()
for i in range(4, s+1):
dp.append(1 + ruisekiwa[i-1])
ruisekiwa.append(dp[i-2] + ruisekiwa[i-1])
# print(dp)
# print(ruisekiwa)
print(dp[-1] % MOD)
| 0 | null | 13,856,905,084,828 | 154 | 79 |
H,A = map(int,input().split())
cnt = 0
while(True) :
H = H - A
cnt += 1
if H <= 0 :
break
print(cnt)
|
import sys
H, A = map(int, next(sys.stdin.buffer).split())
print(-(-H // A))
| 1 | 77,150,142,575,518 | null | 225 | 225 |
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)
N = int(input())
Ls = list(mapint())
Ls.sort()
from bisect import bisect_left
ans = 0
for i in range(N-2):
for j in range(i+1, N-1):
idx = bisect_left(Ls, Ls[i]+Ls[j])
ans += idx-1-j
print(ans)
|
"""
n = int(input())
ab = [list(map(int,input().split())) for _ in range(n)]
mod = 1000000007
ab1 = []
ab2 = []
ab3 = []
ab4 = []
count00 = 0
count01 = 0
count10 = 0
for i in range(n):
if ab[i][0] != 0 and ab[i][1] != 0:
ab1.append(ab[i][0]/ab[i][1])
ab2.append(-ab[i][1]/ab[i][0])
if ab[i][0]/ab[i][1] > 0:
ab3.append((ab[i][0]/ab[i][1],-ab[i][1]/ab[i][0]))
else:
ab4.append((ab[i][0]/ab[i][1],-ab[i][1]/ab[i][0]))
elif ab[i][0] == 0 and ab[i][1] == 0:
count00 += 1
elif ab[i][0] == 0 and ab[i][1] == 1:
count01 += 1
else:
count10 += 1
dict1 = {}
dict2 = {}
ab3.sort()
ab4.sort(reverse = True)
print(ab3)
print(ab4)
for i in ab1:
if i in dict1:
dict1[i] += 1
else:
dict1[i] = 1
for i in ab2:
if i in dict2:
dict2[i] += 1
else:
dict2[i] = 1
sorted1 = sorted(dict1.items(), key = lambda x: x[0])
sorted2 = sorted(dict2.items(), key = lambda x: x[0])
print(sorted1)
print(sorted2)
cnt = 0
num1 = n - count00 - count01 - count10
ans = 0
for i in range(len(ab3)):
a,b = ab3[i]
num1 -= 1
if cnt < len(sorted2):
while cnt < len(sorted2):
if sorted2[cnt][0] == a:
ans += pow(2, num1+count01+count10-sorted2[cnt][1], mod)
ans %= mod
break
elif sorted2[cnt][0] < a:
cnt += 1
else:
ans += pow(2, num1+count01+count10, mod)
ans %= mod
break
else:
ans += pow(2, num1+count01+count10, mod) - pow(2, )
ans %= mod
print(ans)
for i in range(len(ab4)):
num1 -= 1
ans += pow(2, num1+count01+count10, mod)
ans %= mod
print(ans)
ans += pow(2, count01, mod) -1
print(ans)
ans += pow(2, count10, mod) -1
print(ans)
ans += count00
print(ans)
print(ans % mod)
"""
from math import gcd
n = int(input())
dict1 = {}
mod = 1000000007
cnt00 = 0
cnt01 = 0
cnt10 = 0
for _ in range(n):
a,b = map(int,input().split())
if a == 0 and b == 0:
cnt00 += 1
elif a == 0:
cnt01 += 1
elif b == 0:
cnt10 += 1
else:
c = gcd(a,b)
if b < 0:
a *= -1
b *= -1
set1 = (a//c, b//c)
if set1 in dict1:
dict1[set1] += 1
else:
dict1[set1] = 1
ans = 1
for k,v in dict1.items():
a,b = k
if dict1[(a,b)] == -1:
continue
if a > 0:
if (-b,a) in dict1:
ans *= 2**v + 2**dict1[(-b,a)] - 1
dict1[(-b,a)] = -1
else:
ans *= 2**v
else:
if (b,-a) in dict1:
ans *= 2**v + 2**dict1[(b,-a)] - 1
dict1[(b,-a)] = -1
else:
ans *= 2**v
ans %= mod
ans *= 2 ** cnt01 + 2 ** cnt10 - 1
ans += cnt00 - 1
print(ans%mod)
| 0 | null | 96,741,048,679,220 | 294 | 146 |
num_length = int(input())
num_array = [int(x) for x in input().split()]
def bubbleSort(A, N):
flag = 1
exchange_counter = 0
while flag:
flag = 0
for i in range(1, N):
if A[i] < A[i-1]:
A[i], A[i-1] = A[i-1], A[i]
flag = 1
exchange_counter += 1
print(" ".join(map(str, A)))
print(exchange_counter)
bubbleSort(num_array, num_length)
|
N = int(raw_input())
nums = map(int, raw_input().split(" "))
count = 0
for i in range(0, len(nums)):
for j in range(len(nums)-1, i, -1):
if nums[j-1] > nums[j]:
temp = nums[j-1]
nums[j-1] = nums[j]
nums[j] = temp
count += 1
nums = map(str, nums)
print(" ".join(nums))
print(count)
| 1 | 16,243,604,762 | null | 14 | 14 |
import sys
H,W,M=map(int,input().split())
hlist=[0]*H
wlist=[0]*W
hwset=set()
for _ in range(M):
h,w=map(int,input().split())
hlist[h-1]+=1
wlist[w-1]+=1
hwset.add((h-1,w-1))
hmax=max(hlist)
hlist2=[]
for i in range(H):
if hlist[i]==hmax:
hlist2.append(i)
wmax=max(wlist)
wlist2=[]
for i in range(W):
if wlist[i]==wmax:
wlist2.append(i)
#print(hlist2,wlist2)
H2=len(hlist2)
W2=len(wlist2)
if H2*W2>M:
print(hmax+wmax)
else:
for i in range(H2):
ii=hlist2[i]
for j in range(W2):
jj=wlist2[j]
if not (ii,jj) in hwset:
print(hmax+wmax)
sys.exit(0)
else:
print(hmax+wmax-1)
|
n,m,l = map(int,input().split())
g = [[1<<30]*n for _ in range(n)]
for _ in range(m):
a,b,c = map(int,input().split())
g[a-1][b-1] = c
g[b-1][a-1] = c
for i in range(n):
g[i][i] = 0
def bell(g,n):
for k in range(n):
for i in range(n-1):
for j in range(i+1,n):
if g[i][j] > g[i][k] + g[k][j]:
g[i][j] = g[i][k] + g[k][j]
g[j][i] = g[i][j]
return g
g = bell(g,n)
for i in range(n):
for j in range(n):
if i == j:
g[i][j] = 0
elif g[i][j] <= l:
g[i][j] = 1
else:
g[i][j] = 1<<30
g = bell(g,n)
q = int(input())
for _ in range(q):
s,t = map(int,input().split())
if g[s-1][t-1]>>30:
print(-1)
else:
print(g[s-1][t-1]-1)
| 0 | null | 89,502,965,771,608 | 89 | 295 |
K=input()
r='ACL'
for i in range(1,int(K)):
r='ACL'+ r
print(r)
|
K = int ( input().strip() ) ;
print ( "ACL" * K ) ;
| 1 | 2,185,417,328,630 | null | 69 | 69 |
n,k = map(int,input().split())
p = map(int,input().split())
a = sorted(p)
print(sum([a[i] for i in range(k)]))
|
import sys
read = sys.stdin.buffer.read
n, k, *p = map(int, read().split())
p.sort()
print(sum(p[:k]))
| 1 | 11,602,887,981,894 | null | 120 | 120 |
import sys
n = int(input())
l = sys.stdin.readlines()
s = ""
for i in l:
x, y, z = sorted(map(lambda x:x*x,map(int, i.split())))
if x + y == z:
s += "YES\n"
else:
s += "NO\n"
print(s,end="")
|
x = int(input())
for a in range(x):
y = map(int,raw_input().split())
y.sort()
if y[2]**2-y[1]**2 == y[0]**2:
print "YES"
else:
print "NO"
| 1 | 342,033,682 | null | 4 | 4 |
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
"""
sum[Al,,,Ar] = Sの部分和がちょうどK個。
"""
n, k, s = map(int, input().split())
if n == k:
r = [s] * k
else:
if s == 1000000000:
r = [1000000000] * k + [1] * (n - k)
else:
r = [s] * k + [1000000000] * (n - k)
print(*r, sep=' ')
if __name__ == '__main__':
main()
|
from itertools import combinations
n, a, q, ms, cache = int(input()), list(map(int, input().split())), input(), map(int, input().split()), {}
l = set(sum(t) for i in range(n) for t in combinations(a, i + 1))
for m in ms:
print('yes' if m in l else 'no')
| 0 | null | 45,613,632,728,160 | 238 | 25 |
x,y,a,b,c=map(int,input().split())
a_list=list(map(int,input().split()))
a_list=sorted(a_list)
b_list=list(map(int,input().split()))
b_list=sorted(b_list)
a_list=a_list[-x:]
b_list=b_list[-y:]
c_list=list(map(int,input().split()))
for i in range(c):
c_list[i]=-c_list[i]
import heapq
heapq.heapify(a_list)
heapq.heapify(b_list)
heapq.heapify(c_list)
flag=True
while flag and c_list:
x=heapq.heappop(c_list)
x=x*(-1)
#c_listの中でのもっとも大きい値
min_a=heapq.heappop(a_list)
min_b=heapq.heappop(b_list)
if min(min_a,min_b)>=x:
flag=False
heapq.heappush(a_list,min_a)
heapq.heappush(b_list,min_b)
else:
if min_a>=min_b:
heapq.heappush(a_list,min_a)
heapq.heappush(b_list,x)
elif min_a<min_b:
heapq.heappush(b_list,min_b)
heapq.heappush(a_list,x)
sum_a=sum(list(a_list))
sum_b=sum(list(b_list))
print(sum_a+sum_b)
|
string = input()
string=list(string)
count = len(string)
summary = 0
for i in range(count):
if string[i]=="?":
string[i]="D"
#print(string)
for i in string:
print(i, end="")
| 0 | null | 31,703,781,414,460 | 188 | 140 |
import math
class MergeSort:
cnt = 0
def merge(self, a, n, left, mid, right):
n1 = mid - left
n2 = right - mid
l = a[left:(left+n1)]
r = a[mid:(mid+n2)]
l.append(2000000000)
r.append(2000000000)
i = 0
j = 0
for k in range(left, right):
self.cnt += 1
if l[i] <= r[j]:
a[k] = l[i]
i += 1
else:
a[k] = r[j]
j += 1
return a
def mergeSort(self, a, n, left, right):
if left + 1 < right:
mid = (left + right) // 2
self.mergeSort(a, n, left, mid)
self.mergeSort(a, n, mid, right)
self.merge(a, n, left, mid, right)
return a
if __name__ == '__main__':
n = int(input().rstrip())
s = [int(i) for i in input().rstrip().split(" ")]
x = MergeSort()
print(" ".join(map(str, x.mergeSort(s, n, 0, n))))
print(x.cnt)
|
import sys
input = sys.stdin.readline
n = int(input())
L = [int(x) for x in input().split()]
cnt = 0
def Merge(S, l, m, r):
global cnt
cnt += r - l
L = S[l:m] + [float('inf')]
R = S[m:r] + [float('inf')]
i, j = 0, 0
for k in range(l, r):
if L[i] < R[j]:
S[k] = L[i]
i += 1
else:
S[k] = R[j]
j += 1
return
def MergeSort(S, l, r):
if r - l > 1:
m = (l + r) // 2
MergeSort(S, l, m)
MergeSort(S, m, r)
Merge(S, l, m, r)
return
MergeSort(L, 0, n)
print(*L)
print(cnt)
| 1 | 110,218,049,980 | null | 26 | 26 |
H,W,K = map(int,input().split())
array = [ list(input()) for k in range(H)]
ans = 0
for bit_row in range(2**H):
for bit_line in range(2**W):
count = 0
for i in range(H):
for j in range(W):
if ( bit_row >> i ) & 1 == 0 and ( bit_line >> j ) & 1 == 0:
if array[i][j] == '#':
count += 1
if count == K:
ans += 1
print( ans )
|
f,l,n=map(int,input().split())
ctr=0
for i in range(f,l+1):
if i%n==0:
ctr+=1
print(ctr)
| 0 | null | 8,206,017,788,852 | 110 | 104 |
S = input()
T = input()
if T.startswith(S):
if len(T)-len(S)==1:
print("Yes")
else:
print("No")
else:
print("No")
|
s = input()
t = input()
if s == t[:-1]: print('Yes')
else: print('No')
| 1 | 21,344,237,621,080 | null | 147 | 147 |
import sys
input = lambda: sys.stdin.readline().rstrip()
def solve():
N, M = map(int, input().split())
if N == M:
print('Yes')
else:
print('No')
if __name__ == '__main__':
solve()
|
# coding : utf-8
lst = []
while True:
h, w = map(int, input().split())
lst.append([h, w])
if h == 0 and w == 0:
break
for l in lst:
H = l[0]
W = l[1]
for i in range(H):
for j in range(W):
if (i + j) % 2 == 0:
print ('#', end = '')
else:
print ('.', end = '')
print ('')
if i == H-1:
print ('')
| 0 | null | 42,026,629,341,620 | 231 | 51 |
s = input()
data = []
cnt = 0
for i in range(len(s)-1):
cnt += 1
if s[i] != s[i+1]:
if s[i] == "<" and s[i+1] == ">":
data.append(cnt)
data.append(-1)
else:
data.append(cnt)
cnt = 0
data.append(cnt+1)
for i in range(len(data)-1):
if data[i] == -1:
if data[i-1] < data[i+1]:
data[i-1] -= 1
else:
data[i-1] > data[i+1]
data[i+1] -= 1
ans = 0
for i in range(len(data)):
if data[i] != -1:
for j in range(1, data[i]+1):
ans += j
# print(data)
print(ans)
|
MOD = 998244353
n, s = map(int, input().split())
nums = list(map(int, input().split()))
dp = [[0] * (s + 1) for _ in range(n + 1)]
dp[0][0] = 1
for i in range(n):
for j in range(s + 1):
dp[i + 1][j] += dp[i][j] * 2 % MOD
if j >= nums[i]:
dp[i + 1][j] += dp[i][j - nums[i]]
dp[i + 1][j] %= MOD
print(dp[n][s])
| 0 | null | 87,163,848,572,572 | 285 | 138 |
h, w, n= [int(input()) for i in range(3)]
k = max(h, w)
ans = (n+k-1)//k
print(ans)
|
a,k,d = map(int, input().split())
if a < 0:
x = -a
else:
x = a
y = x % d
l = x // d
m = k - l
if m < 0:
ans = x - (k * d)
elif m % 2 ==0:
ans = y
else :
ans = y - d
print(abs(ans))
| 0 | null | 47,083,795,393,182 | 236 | 92 |
l='#.'*999
while 1:
h,w=map(int,raw_input().split());s=""
if h==0:break
for i in range(h):s+=l[i%2:w+i%2]+"\n"
print s
|
# -*- coding: utf-8 -*-
def merge(A, left, mid, right):
global cnt
n1 = mid - left
n2 = right - mid
L = [None]*(n1+1)
R = [None]*(n2+1)
L[:n1] = A[left:left+n1]
R[:n2] = A[mid:mid+n2]
L[n1] = float('inf')
R[n2] = float('inf')
i = 0
j = 0
for k in range(left, right):
cnt += 1
if L[i] <= R[j]:
A[k] = L[i]
i += 1
else:
A[k] = R[j]
j += 1
def mergeSort(A, left, right):
if left + 1 < right:
mid = (left + right) // 2
mergeSort(A, left, mid)
mergeSort(A, mid, right)
merge(A, left, mid, right)
if __name__ == '__main__':
n = int(input())
S = [int(s) for s in input().split(" ")]
cnt = 0
mergeSort(S, 0, n)
print(" ".join(map(str, S)))
print(cnt)
| 0 | null | 486,406,481,120 | 51 | 26 |
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
H = list(map(int, input().split()))
H = sorted(H, reverse=True)
total = 0
for i in range(K, N):
total += H[i]
print(total)
|
n=input()
s1= input()
count=0
for i in range(len(s1)-2):
if(s1[i]=='A' and s1[i+1]=='B' and s1[i+2]=='C'):
count=count+1
print(count)
| 0 | null | 89,414,641,145,918 | 227 | 245 |
from collections import deque
s = deque(list(input()))
q = int(input())
# -1がnotReverse, 1がReverser
isR = -1
for i in range(q):
line = list(input().split())
t = int(line[0])
if t == 1:
isR *= -1
else:
f = int(line[1])
if isR + f == 0 or isR + f == 3:
s.appendleft(line[2])
else:
s.append(line[2])
if isR == 1:
s.reverse()
print("".join(s))
|
import bisect
N, M, K = map(int, input().split())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
Asums = [0] * (N + 1)
for i, a in enumerate(A):
Asums[i+1] = Asums[i] + a
Bsums = [0] * (M + 1)
for i, b in enumerate(B):
Bsums[i+1] = Bsums[i] + b
ans = 0
for anum in range(N + 1):
t = Asums[anum]
if K < t:
break
bnum = bisect.bisect_right(Bsums, K - t) - 1
ans = max(ans, anum + bnum)
print(ans)
| 0 | null | 34,017,092,721,632 | 204 | 117 |
N, A, B = map(int, input().split())
d = B-A
if d%2 == 1:
d = min((B + (N-B)*2 + 1) - A, B - (A - (A-1)*2 - 1))
ans = d//2
print(ans)
|
import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
from bisect import bisect, bisect_left, bisect_right
MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(10 ** 8)
N, A, B = map(int, input().split())
dis = B - A - 1
if dis % 2 == 0:
print(min((N - max(A, B) + 1) + (N - min(A, B) - (N - max(A, B) + 1)) // 2,
min(A, B) + ((max(A, B) - min(A, B) - 1) // 2)))
else:
print((dis + 1) // 2)
| 1 | 109,791,689,130,708 | null | 253 | 253 |
import math as m
EPS = 0.0000000001
def f(a, b, theta):
if theta > m.pi/2 - EPS:
return 0
if a * m.tan(theta) <= b:
ret = a * a * b - a * a * a * m.tan(theta) / 2
else:
ret = b * b / m.tan(theta) * a / 2
return ret
def solve():
a, b, x = map(int, input().split())
ok = m.pi / 2
ng = 0
for _ in range(100):
mid = (ok + ng) / 2
if f(a, b, mid) < x:
ok = mid
else:
ng = mid
print(ok * 180 / m.pi)
if __name__ == "__main__":
solve()
|
from math import atan,pi
a,b,x=map(int,input().split())
if b-x/a**2 <= x/a**2:print(atan((b-x/a**2)/(a/2))*(180/pi))
else:
y = x/a*2/b
print(atan(b/y)*(180/pi))
| 1 | 163,657,042,031,688 | null | 289 | 289 |
#!/usr/bin/env python
n, k = map(int, input().split())
a = list(map(int ,input().split()))
def check(x):
now = 0
for i in range(n):
now += (a[i]-1)//x
return now <= k
l = 0
r = int(1e9)
while r-l > 1:
mid = (l+r)//2
if check(mid):
r = mid
else:
l = mid
print(r)
|
import math
n=int(input())
while n != 0 :
sums = 0.0
s = list(map(float,input().split()))
m = sum(s)/n
for i in range(n):
sums += ((s[i]-m)**2)/n
print(math.sqrt(sums))
n = int(input())
| 0 | null | 3,325,376,150,152 | 99 | 31 |
n, m, l = map(int, raw_input().split())
matrix_a = []
matrix_b = []
for i in range(n):
line = map(int, raw_input().split())
matrix_a.append(line)
for i in range(m):
line = map(int, raw_input().split())
matrix_b.append(line)
for i in range(n):
result = []
for j in range(l):
tmp = 0
for k in range(m):
tmp += matrix_a[i][k] * matrix_b[k][j]
result.append(tmp)
print " ".join(map(str, result))
|
n,m,l=map(int,input().split())
A=[list(map(int,input().split())) for i in range(n)]
B=[list(map(int,input().split())) for i in range(m)]
C=[[0]*l for i in range(n)]
for x in range(n):
for y in range(l):
for z in range(m):
C[x][y] += A[x][z]*B[z][y]
for row in range(n):
print("%d"%(C[row][0]),end="")
for col in range(1,l):
print(" %d"%(C[row][col]),end="")
print()
| 1 | 1,429,854,936,300 | null | 60 | 60 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.