code1
stringlengths 16
427k
| code2
stringlengths 16
427k
| similar
int64 0
1
| pair_id
int64 6.82M
181,637B
⌀ | question_pair_id
float64 101M
180,471B
⌀ | code1_group
int64 2
299
| code2_group
int64 2
299
|
---|---|---|---|---|---|---|
def main():
a, b, c = [int(x) for x in input().split()]
count = int(input())
while a >= b:
b *= 2
count -= 1
while b >= c:
c *= 2
count -= 1
print('Yes' if count >= 0 else 'No')
if __name__ == '__main__':
main()
|
#coding: UTF-8
a = raw_input()
l = map(int, raw_input().split())
l.sort()
x =l[0]
l.reverse()
y = l[0]
z = sum(l)
print "%d %d %d" %(x, y, z)
| 0 | null | 3,858,834,527,680 | 101 | 48 |
import math
n = int(input())
while n:
s = list(map(float, input().split()))
m = sum(s) / n
tmp = 0
for si in s:
tmp += (si - m) ** 2
print(math.sqrt(tmp / n))
n = int(input())
|
import sys, math
while True:
n = int(input())
if n == 0:
sys.exit()
s = list(map(int, input().split()))
m = sum(s) / n
S = 0
for i in range(n):
S += (s[i] - m) ** 2
S /= n
a = math.sqrt(S)
print(a)
| 1 | 189,191,954,840 | null | 31 | 31 |
def func(N):
result = 0
for A in range(1,N):
result += (N-1)//A
return result
if __name__ == "__main__":
N = int(input())
print(func(N))
|
N = int(input())
count = 0
for i in range(N):
count += int((N-1)/(i+1))
print(count)
| 1 | 2,587,596,910,976 | null | 73 | 73 |
import math
# b=input()
# c=[]
# for i in range(b):
# c.append(a[i])
#a = list(map(int,input().split()))
#b = list(map(int,input().split()))
s=int(input())
a = math.ceil(s/2)
print(a-1)
|
S = input()
if S.isupper():
print('A')
else:
print('a')
| 0 | null | 82,320,842,503,536 | 283 | 119 |
r,c=map(int,input().split())
a = []
for i in range(r):
a.append(list(map(int, input().split())))
a[i]+=[sum(a[i])]
print(*a[i])
a=list(zip(*a[::-1]))
for i in range(c):print(sum(a[i]),end=' ')
print(sum(a[c]))
|
r, c = [int (x) for x in input().split(' ')]
a = [[0 for i in range(c)] for j in range(r)]
for s in range(0,r):
a[s] = [int (x) for x in input().split(' ')]
for tr in range(0,r):
total = 0
for v in a[tr]:
total += v
a[tr].append(total)
total = [0 for i in range(c+1)]
for tr in range(0,r):
for tc in range(0,c+1):
total[tc] += a[tr][tc]
a.append(total)
for i in range(r+1):
a[i] = [str(a[i][j]) for j in range(c+1)]
print(' '.join(a[i]))
| 1 | 1,381,164,550,390 | null | 59 | 59 |
import sys
while 1:
x = sys.stdin.readline().strip()
x_list = list(map(int,x.split(" ")))
if x_list[0] == 0 and x_list[1] == 0 :
break
x_list.sort()
print(x_list[0],x_list[1])
|
#coding:UTF-8
while True:
n = map(int,raw_input().split())
if (n[0] or n[1]) == 0:
break
n.sort()
print n[0],n[1]
| 1 | 531,354,738,850 | null | 43 | 43 |
h, n = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(n)]
#(a,b):=(ダメージ,消費魔力)
dp = [float('inf')] * (h + 1)
dp[0] = 0
for i in range(h + 1):
for j in range(n):
if dp[i] == float('inf'):continue
temp_a = i + ab[j][0] if i + ab[j][0] < h else h
temp_b = dp[i] + ab[j][1]
dp[temp_a] = min(dp[temp_a], temp_b)
print(dp[-1])
#print(dp)
|
h,n=map(int, input().split())
a=[0]*n
b=[0]*n
for i in range(n):
a[i],b[i]=map(int,input().split())
max_a=max(a)
dp=[float("inf")]*(h+max_a+1)
dp[0]=0
for i in range(h):
for j in range(n):
dp[i+a[j]] = min(dp[i+a[j]], dp[i]+b[j])
print(min(dp[h:]))
| 1 | 81,118,278,518,492 | null | 229 | 229 |
S = input()
if S == 'ABC':
answer = 'ARC'
elif S == 'ARC':
answer = 'ABC'
else:
answer = '入力間違い【ABC】【ARC】を入力'
print(answer)
|
# 171 A
X = input()
print('a') if X.islower() else print('A')
| 0 | null | 17,669,114,868,570 | 153 | 119 |
if __name__ == '__main__':
key_in = input()
data = [int(x) for x in key_in.split(' ')]
a = data[0]
b = data[1]
print('{0} {1} {2:.5f}'.format(a//b, a%b, a/b))
|
a=list(map(int,input().split()))
string=str(a[0]//a[1])+" "+str(a[0]%a[1])+" "+str("%.10f"%(a[0]/a[1]))
print(string)
| 1 | 603,025,507,712 | null | 45 | 45 |
n1 = int(input())
nums1 = list(map(int, input().split()))
n2 = int(input())
nums2 = list(map(int, input().split()))
def linear_search(nums1, n1, key):
nums1[n1] = key
index = 0
while nums1[index] != key:
index += 1
return index != n1
output = 0
nums1.append(None)
for key in nums2:
if linear_search(nums1, n1, key):
output += 1
print(output)
|
n_1=input()
n_1_List=map(int,raw_input().split())
n_2=input()
n_2_List=map(int,raw_input().split())
n_1_List=set(n_1_List)
n_2_List=set(n_2_List)
print(len(n_1_List.intersection(n_2_List)))
| 1 | 67,271,931,490 | null | 22 | 22 |
def solve(N, S):
ans = 0
R, G, B = 0, 0, 0
for i in range(N):
if S[i] == "R":
R += 1
elif S[i] == "G":
G += 1
else:
B += 1
# print(R, G, B, R*G*B)
ans = R * G * B
dif = 1
while dif <= N//2+1:
idx = 0
while idx + dif + dif < N:
if S[idx] != S[idx + dif] and S[idx+dif] != S[idx+2*dif] and S[idx+2*dif] != S[idx]:
ans -= 1
idx += 1
dif += 1
print(ans)
if __name__ == '__main__':
# N = 4000
# S = 'RGBR' * 1000
N = int(input())
S = input()
solve(N, S)
|
n = int(input())
s = input()
r = [0] * (n+1)
g = [0] * (n+1)
b = [0] * (n+1)
for i, c in enumerate(reversed(s), 1):
if c == 'R':
r[i] += r[i-1] + 1
else:
r[i] += r[i-1]
if c == 'G':
g[i] += g[i-1] + 1
else:
g[i] += g[i-1]
if c == 'B':
b[i] += b[i-1] + 1
else:
b[i] += b[i-1]
r = r[:-1][::-1]
g = g[:-1][::-1]
b = b[:-1][::-1]
ans=0
for i in range(n-2):
for j in range(i+1, n-1):
if s[i] == s[j]:
continue
se = set('RGB') - set(s[i] + s[j])
c = se.pop()
if c == 'R':
ans += r[j]
if 2*j-i < n and s[2*j-i] == 'R':
ans -= 1
elif c == 'G':
# print('j=',j,'g[j]=',g[j])
ans += g[j]
if 2*j-i < n and s[2*j-i] == 'G':
ans -= 1
elif c == 'B':
ans += b[j]
if 2*j-i < n and s[2*j-i] == 'B':
ans -= 1
# print('i=',i,'j=',j,'c=',c, 'ans=',ans)
print(ans)
| 1 | 36,029,242,459,418 | null | 175 | 175 |
a,b = list(map(int, input().split()))
d = int(a / b)
r = int(a % b)
f = a / b
print(str(d) + " " + str(r) + " " + str("{0:.10f}".format(f)))
|
h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
dp = [[float("inf")] * w for i in range(h)]
dp[0][0] = 1 if s[0][0] == "#" else 0
d = [(-1, 0), (0, -1)]
for i in range(h):
for j in range(w):
if i == 0 and j == 0:
pass
else:
m = float("inf")
for k in d:
x = i + k[0]
y = j + k[1]
if 0 <= x < h and 0 <= y < w:
if s[i][j] == "#" and s[x][y] == ".":
m = dp[x][y] + 1
else:
m = dp[x][y]
dp[i][j] = min(dp[i][j], m)
print(dp[h - 1][w - 1])
| 0 | null | 25,050,460,019,760 | 45 | 194 |
n=int(input())
A=list(map(int,input().split()))
ans = list(range(n))
for i in range(n):
ans[A[i]-1] = i+1
print(" ".join(map(str,ans)))
|
n=int(input())
l=list(map(int,input().split()))
la=[0]*n
for i in range(n):
la[l[i]-1]=i+1
ans=[str(j) for j in la]
ans=" ".join(ans)
print(ans)
| 1 | 180,965,299,780,256 | null | 299 | 299 |
a=int(input())
b=int(input())
if a+b==4:
ans=2
elif a+b==3:
ans=3
else:
ans=1
print(ans)
|
a = input()
b = input()
ans = '123'
ans = ans.replace(a, '').replace(b, '')
print(ans)
| 1 | 110,887,341,560,160 | null | 254 | 254 |
MOD = 10**9+7
K = int(input())
S = input()
N = len(S)
n = N+K
fac = [1]*(n+1)
rev = [1]*(n+1)
for i in range(1,n+1):
fac[i] = i*fac[i-1]%MOD
rev[i] = pow(fac[i], MOD-2, MOD)
comb = lambda a,b:(fac[a]*rev[a-b]*rev[b])%MOD
ans = 0
for i in range(K+1):
ans += pow(26, K-i, MOD) * pow(25, i, MOD) * comb(N+i-1, i)
ans %= MOD
print(ans)
|
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
a = int(input())
print(a + a**2 + a**3)
| 0 | null | 11,493,842,640,682 | 124 | 115 |
n = int(input())
p = list(map(int,input().split()))
for x in p:
if x % 2 == 0:
if x % 3 != 0 and x % 5 != 0:
print("DENIED")
break
else :
print("APPROVED")
|
class Cmb:
def __init__(self, N, mod=998244353):
self.fact = [1,1]
self.fact_inv = [1,1]
self.inv = [0,1]
""" 階乗を保存する配列を作成 """
for i in range(2, N+1):
self.fact.append((self.fact[-1]*i) % mod)
self.inv.append((-self.inv[mod%i] * (mod//i))%mod)
self.fact_inv.append((self.fact_inv[-1]*self.inv[i])%mod)
""" 関数として使えるように、callで定義 """
def __call__(self, n, r, mod=998244353):
if (r<0) or (n<r):
return 0
r = min(r, n-r)
return self.fact[n] * self.fact_inv[r] * self.fact_inv[n-r] % mod
n,m,k = map(int,input().split())
mod = 998244353
ans = 0
c = Cmb(N=n, mod=mod)
for x in range(k+1):
if x==0:
ans += m*pow(m-1, n-1, mod)
ans %= mod
else:
ans += m*pow(m-1, n-1-x, mod)*c(n-1, x)%mod
ans %= mod
print(ans%mod)
| 0 | null | 46,244,215,052,266 | 217 | 151 |
N = int(input())
S = input()
r = S.count('R')
g = S.count('G')
b = S.count('B')
ans = r*g*b
for i in range(N):
for d in range(N):
j = i+d
k = i+2*d
if k > N-1:
break
if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
ans -= 1
print(ans)
|
num = map(int, raw_input().split())
if num[0] > num[1]:
x = num[0]
y = num[1]
else:
y = num[0]
x = num[1]
while y > 0:
z = x % y
x = y
y = z
print x
| 0 | null | 18,183,133,968,208 | 175 | 11 |
n,x,t=[int(i) for i in input().split()]
k=n//x
if n%x:
print((k+1)*t)
else:
print(k*t)
|
from heapq import heapify, heappop
from collections import deque
x, y, a, b, c = list(map(int, input().split(' ')))
p = list(map(int, input().split(' ')))
q = list(map(int, input().split(' ')))
r = list(map(lambda x: -1 * int(x), input().split(' ')))
p = sorted(p, reverse=True)
q = sorted(q, reverse=True)
s = p[:x] + q[:y]
s.sort()
heapify(r)
for i, _s in enumerate(s):
if len(r) == 0:
break
n = -1 * heappop(r)
if n > _s:
s[i] = n
print(sum(s))
| 0 | null | 24,555,220,510,572 | 86 | 188 |
a = int(input())
s = 0
b = 1
while 15*b <= a:
s += 8*b
b += 1
while 5*b <= a:
s -= 7*b
b += 1
while 3*b <= a:
s -= 2*b
b += 1
while b <= a:
s += b
b += 1
print(s)
|
n=int(input())
a=(n*(n+1))//2
z=n%3
x=n-z
x=x//3
s1=((x*(x+1))//2)*3
z=n%5
x=n-z
x=x//5
s2=((x*(x+1))//2)*5
z=n%15
x=n-z
x=x//15
s3=((x*(x+1))//2)*15
print (a-s1-s2+s3)
| 1 | 34,990,246,434,880 | null | 173 | 173 |
from collections import Counter
S = input()
L = len(list(S))
dp = [0] * L
dp[0] = 1
T = [0] * L
T[0] = int(S[-1])
for i in range(1, L):
dp[i] = dp[i - 1] * 10
dp[i] %= 2019
T[i] = int(S[-(i+1)]) * dp[i] + T[i - 1]
T[i] %= 2019
ans = 0
for k, v in Counter(T).items():
if k == 0:
ans += v
ans += v * (v - 1) // 2
else:
ans += v * (v - 1) // 2
print(ans)
|
x, n = map(int, input().split())
li_p = list(map(int, input().split()))
li_p.sort()
i = 0
while True:
a = x - i
b = x + i
if not a in li_p:
print(a)
break
elif a in li_p and not b in li_p:
print(b)
break
elif a in li_p and b in li_p:
i += 1
| 0 | null | 22,508,040,630,238 | 166 | 128 |
n=int(input())
s=input()
ans=0
for i in range(1000):
i=str(i).zfill(3)
a=0
for c in s:
if c==i[a]:
a+=1
if a==3:
ans+=1
break
print(ans)
|
def main():
n = int(input())
d = list(map(int,input().split()))
mod = 998244353
if d[0]!=0:
print(0)
return
D = {0:1}
for i in range(1,n):
if d[i]==0:
print(0)
return
if D.get(d[i]) == None:
D[d[i]] = 1
else:
D[d[i]] += 1
ans = 1
if sorted(D.keys())[-1]!=len(D.keys())-1:
print(0)
return
for k in range(1,len(D.keys())):
ans *= pow(D[k-1],D[k],mod)
ans = ans % mod
print(ans)
if __name__ == "__main__":
main()
| 0 | null | 141,683,905,179,670 | 267 | 284 |
X=int(input())
A=0
B=0
for i in range(-120,120):
for k in range(-120,120):
if i**5==X+k**5:
A=i
B=k
break
print(A,B)
|
def main():
N = input()
if "7" in N:
print("Yes")
else:
print("No")
main()
| 0 | null | 29,960,507,769,472 | 156 | 172 |
import fractions
A,B = (int(a) for a in input().split())
print(A * B // fractions.gcd(A,B))
|
N = int(input())
A = list(map(int, input().split()))
ans = 0
lst = [1000] * (N)
for i in range(N): #買うところを選択
a = A[i]
for j in range(i + 1, N):
if A[j] > a:
tmp = lst[i] // a * A[j] + lst[i] % a
lst[j] = max(max(lst[j], tmp), lst[j - 1])
else:
lst[j] = max(lst[j], lst[j - 1])
print (lst[-1])
# print (lst)
| 0 | null | 60,008,985,403,200 | 256 | 103 |
#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でやると正解するかも!!
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
return list(map(int,input().split()))
def I():
return int(input())
dx = [-1,0,1,0]
dy = [0,-1,0,1]
h,w = readInts()
S = [list(input()) for _ in range(h)]
def bfs(y,x):
dq = deque()
dq.append((y,x))
res = -1
dist = [[-1] * w for _ in range(h)]
dist[y][x] = 0
while dq:
y,x = dq.popleft()
res = max(res,dist[y][x])
for i in range(4):
ny = y + dy[i]
nx = x + dx[i]
if not(0 <= ny < h and 0 <= nx < w):
continue
if S[ny][nx] == '#' or dist[ny][nx] != -1:
continue
dist[ny][nx] = dist[y][x] + 1
dq.append((ny,nx))
return res
ans = -1
for y in range(h):
for x in range(w):
if S[y][x] != '#':
ans = max(ans,bfs(y,x))
print(ans)
|
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 3 23:43:13 2020
@author: liang
"""
N ,M ,Q = map(int, input().split())
A = list()
lis = list()
ans = 0
def make_list(n,m):
if n == N:
A.append(lis.copy())
return
for i in range(m,M+1):
lis.append(i)
make_list(n+1,i)
lis.pop()
make_list(0,1)
#print(A)
calc = [list(map(int,input().split())) for _ in range(Q)]
for a in A:
tmp = 0
for c in calc:
if a[c[1]-1] - a[c[0]-1] == c[2]:
tmp += c[3]
if tmp > ans:
ans = tmp
print(ans)
| 0 | null | 61,201,762,384,640 | 241 | 160 |
from sys import stdin
a, b, c = (int(n) for n in stdin.readline().rsplit())
answer = "Yes" if a < b < c else "No"
print(answer)
|
from math import gcd
def lcm(a, b):
return a * b // gcd(a, b)
A, B = map(int, input().split())
print(lcm(A, B))
| 0 | null | 56,903,277,400,380 | 39 | 256 |
H, W, K = map(int, input().split())
c = [input() for _ in range(H)]
r = [[0]*W for _ in range(H)]
S = 0
for i in range(2**H):
for j in range(2**W):
cnt = 0
for k in range(H):
for l in range(W):
if (i >> k) & 1:
if (j >> l) & 1:
if c[k][l] == "#":
cnt += 1
if cnt == K:
S += 1
print(S)
|
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
h, w, k = map(int, input().split())
C = []
for _ in range(h): C.append(input())
from itertools import *
ans = 0
for bit in range(1<<(h+w)):
c = 0
for i, j in product(range(w, h+w), range(w)):
if bit>>i & 1 and bit>>j & 1: c += C[i-w][j] == '#'
if c == k: ans += 1
print(ans)
| 1 | 8,922,776,600,908 | null | 110 | 110 |
import math
L = int(input())
print(math.pow(L / 3, 3))
|
N = input()
ans = "No"
for s in N:
if s == "7":
ans = "Yes"
break
print(ans)
| 0 | null | 40,519,139,336,250 | 191 | 172 |
r = int(input())
s = r * 2 * 3.14
print(s)
|
r = int(input())
pi = 3.14159265359
print(r*2*pi)
| 1 | 31,460,482,978,560 | null | 167 | 167 |
K = int(input())
S = input()
if len(S) <= K:
print(S)
else:
s = ""
for i in range(K):
s += S[i]
s += "..."
print(s)
|
K = int(input())
S = input()
if len(S) <= K:
print(S)
else:
print(f"{S[:K]}...")
| 1 | 19,869,818,679,280 | null | 143 | 143 |
if __name__ == "__main__":
K = int(input())
num_ls = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
ret = num_ls[K-1]
print(ret)
|
x = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
y = input()
K = int(y)
print(x[K-1])
| 1 | 49,999,281,160,640 | null | 195 | 195 |
from collections import deque
[N,M] = list(map(int,input().split()))
AB = []
for i in range(M):
AB.append(list(map(int,input().split())))
#そこから直で行けるところをまとめる
ikeru = [[] for i in range(N)]
for i in range(M):
ikeru[AB[i][0]-1].append(AB[i][1]-1)
ikeru[AB[i][1]-1].append(AB[i][0]-1)
# print('ikeru:',ikeru)
que = deque([])
que.append(0)
# print('que ini:',que)
out=[10**6]*N
out[0]=0
while que:
p = que.popleft()
# print('p,que:',p,que)
for next in ikeru[p]:
# print('next,out:',next,out)
if out[next]==10**6:
que.append(next)
out[next]=p+1
# print(out)
print('Yes')
for i in range(1,N):
print(out[i])
|
from collections import deque
n, m = map(int, input().split())
links = [[] for _ in range(n)]
for _ in range(m):
a, b = map(int, input().split())
a -= 1
b -= 1
links[a].append(b)
links[b].append(a)
ans = [-1] * n
ans[0] = 0
q = deque([(0, 0)])
while q:
room, prev = q.popleft()
for next_ in links[room]:
if ans[next_] < 0:
ans[next_] = room
q.append((next_, room))
print('Yes')
for i in range(1, n):
print(ans[i] + 1)
| 1 | 20,419,700,687,872 | null | 145 | 145 |
import itertools
N, M, Q = map(int, input().split())
l = [i for i in range(1, M+1)]
qus = []
As = []
for _ in range(Q):
b = list(map(int, input().split()))
qus.append(b)
for v in itertools.combinations_with_replacement(l, N):
a = list(v)
A = 0
for q in qus:
pre = a[q[1]-1] - a[q[0]-1]
if pre == q[2]:
A += q[3]
As.append(A)
print(max(As))
|
# ABC165 C
from itertools import combinations as com
N,M,Q=map(int,input().split())
query=[tuple(map(int,input().split())) for _ in [0]*Q]
res=0
for p in com(list(range(N+M-1)),M-1):
m=1
A=[]
for i in range(N+M-1):
if i in p:
m+=1
else:
A.append(m)
tmpres=0
for a,b,c,d in query:
if A[b-1]-A[a-1]==c:
tmpres+=d
# print(p,A,tmpres)
res=max(tmpres,res)
print(res)
| 1 | 27,589,519,890,940 | null | 160 | 160 |
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
n,k = map(int, input().split())
m = n % k
m = 0 if m == 0 else abs(m - k)
print(min(n, m))
|
n,k = list(map(int,input().split()))
tmp = n % k
if tmp <= abs(tmp - k):
print(tmp)
else:
print(abs(tmp - k))
| 1 | 39,280,771,917,762 | null | 180 | 180 |
while True:
n = list(map(int,input().split()))
if(n[0] == n[1] == 0):break
print(" ".join(map(str,sorted(n))))
|
import math
def main():
A, B = map(int,input().split())
print(int(A*B/math.gcd(A,B)))
if __name__ =="__main__":
main()
| 0 | null | 56,866,083,370,992 | 43 | 256 |
s=input()
n=len(s)
a=[0]*(n+1)
for i in range(n):
if s[i]=="<":
a[i+1]=a[i]+1
for i in reversed(range(n)):
if s[i]==">":
a[i]=max(a[i+1]+1,a[i])
print(sum(a))
|
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'))
| 0 | null | 110,110,462,873,328 | 285 | 210 |
import sys
RS = lambda: sys.stdin.readline()[:-1]
RI = lambda x=int: x(RS())
RA = lambda x=int: map(x, RS().split())
RSS = lambda: RS().split()
def solve():
ans = []
for i in s:
if i == "P":
ans.append("P")
else:
ans.append("D")
print("".join(ans))
return
s = RS()
solve()
|
s = input()
print("YNeos" [not (s[2]==s[3] and s[4]==s[5]) ::2])
| 0 | null | 30,344,071,408,178 | 140 | 184 |
L = input().split()
n = len(L)
A = []
top = -1
for i in range(n):
if L[i] not in {"+","-","*"}:
A.append(int(L[i]))
top += 1
else:
if(L[i] == "+"):
A[top - 1] = A[top - 1] + A[top]
elif(L[i] == "-"):
A[top - 1] = A[top -1] - A[top]
elif(L[i] == "*"):
A[top - 1] = A[top - 1] * A[top]
del A[top]
top -= 1
print(A[top])
|
str = input()
num = int(str)
if num % 2 == 0:
print('-1');
else:
i = 1
j = 7
while j % num != 0 and i <= num:
j = (j % num) * 10 + 7
i += 1
if i > num:
print('-1');
else:
print(i);
| 0 | null | 3,065,807,591,234 | 18 | 97 |
a = [list(map(int, input().split())) for _ in range(3)]
n = int(input())
for _ in range(n):
d = int(input())
for i in range(3):
if d in a[i]:
a[i][a[i].index(d)] = -1
for i in range(3):
if sum(a[i])==-3:
print('Yes')
exit()
s = 0
for j in range(3):
s += a[j][i]
if s==-3:
print('Yes')
exit()
if a[0][0]+a[1][1]+a[2][2]==-3:
print('Yes')
exit()
if a[0][2]+a[1][1]+a[2][0]==-3:
print('Yes')
exit()
print('No')
|
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 9 20:50:35 2020
@author: akros
"""
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 9 16:51:42 2020
@author: akros
"""
A1 = list(map(int, input().split()))
A2 = list(map(int, input().split()))
A3 = list(map(int, input().split()))
N = int(input())
a = []
for i in range(N):
a.append(int(input()))
if A1[0] == a[i]:
A1[0] = True
elif a[i] == A1[1]:
A1[1] = True
elif a[i] == A1[2]:
A1[2] = True
elif a[i] == A2[0]:
A2[0] = True
elif a[i] == A2[1]:
A2[1] = True
elif a[i] == A2[2]:
A2[2] = True
elif a[i] == A3[0]:
A3[0] = True
elif a[i] == A3[1]:
A3[1] = True
elif a[i] == A3[2]:
A3[2] = True
if A1[0] == True and A1[1] == True and A1[2] == True:
print("Yes")
elif A1[0] == True and A2[0] == True and A3[0] == True:
print("Yes")
elif A1[0] == True and A2[1] == True and A3[2] == True:
print("Yes")
elif A1[1] == True and A2[1] == True and A3[1] == True:
print("Yes")
elif A1[2] == True and A2[2] == True and A3[2] == True:
print("Yes")
elif A2[0] == True and A2[1] == True and A2[2] == True:
print("Yes")
elif A3[0] == True and A3[1] == True and A3[2] == True:
print("Yes")
elif A1[2] == True and A2[1] == True and A3[0] == True:
print("Yes")
else:
print("No")
| 1 | 59,567,603,009,610 | null | 207 | 207 |
t = input().lower()
cnt = 0
while True:
w = input()
if w == 'END_OF_TEXT': break
cnt += len(list(filter(lambda x: x == t, [v.lower() for v in w.split(' ')])))
print(cnt)
|
import sys
W = input().lower()
T = sys.stdin.read().lower()
print(T.split().count(W))
| 1 | 1,796,436,847,700 | null | 65 | 65 |
#169D
#Div Game
n=int(input())
def factorize(n):
fct=[]
b,e=2,0
while b**2<=n:
while n%b==0:
n/=b
e+=1
if e>0:
fct.append([b,e])
b+=1
e=0
if n>1:
fct.append([n,1])
return fct
l=factorize(n)
ans=0
for i in l:
c=1
while i[1]>=c:
ans+=1
i[1]-=c
c+=1
print(ans)
|
n=int(input())
c=input()
d=c.count("R")
e=c[:d].count("R")
print(d-e)
| 0 | null | 11,675,294,826,988 | 136 | 98 |
n = int(input())
a = []
for i in range(n):
x,l = map(int,input().split())
s = x-l
g = x+l
a.append((s,g))
a = sorted(a, reverse=False, key=lambda x: x[1])
tmp = a[0][1]
ans = 1
for i in range(1,len(a)):
if tmp <= a[i][0]:
tmp = a[i][1]
ans += 1
print(ans)
|
n = int(input())
ranges = []
for i in range(n):
x, l = map(int, input().split())
ranges.append([max(x-l, 0), x+l])
ranges.sort(key=lambda x: x[1])
start = 0
ans = 0
for range in ranges:
if range[0] >= start:
start = range[1]
ans += 1
print(ans)
| 1 | 90,308,324,254,240 | null | 237 | 237 |
import sys
import time
import math
def inpl():
return list(map(int, input().split()))
st = time.perf_counter()
# ------------------------------
N = int(input())
d = inpl()
ans = 0
for i in range(N-1):
for j in range(i+1, N):
ans += d[i] * d[j]
print(ans)
# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)
|
l = raw_input()
print l.swapcase()
| 0 | null | 85,321,070,346,022 | 292 | 61 |
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
ac = Counter(a)
if len(ac) == n:
print("YES")
else:
print("NO")
|
n = int(input())
if len(set(input().split())) == n:
print('YES')
else:
print('NO')
| 1 | 74,111,863,341,966 | null | 222 | 222 |
ri = lambda S: [int(v) for v in S.split()]
def rii(): return ri(input())
a, b = rii()
print(min(str(a) * b, str(b) * a))
|
# coding: utf-8
# Your code here!
n = int(input())
M = []
for i in range(n):
adj = list(map(int,input().split()))
if adj[1] == 0:
M += [[]]
else:
M += [adj[2:]]
time = 1
ans = [[j+1,0,0] for j in range(n)] # id d f
while True:
for i in range(n):
if ans[i][1] == 0:
stack = [i]
ans[i][1] = time
time += 1
break
elif i == n-1:
for k in range(n):
print(*ans[k])
exit()
while stack != []:
u = stack[-1]
if M[u] != []:
for i in range(len(M[u])):
v = M[u][0]
M[u].remove(v)
if ans[v-1][1] == 0:
ans[v-1][1] = time
stack.append(v-1)
time += 1
break
else:
stack.pop()
ans[u][2] = time
time += 1
| 0 | null | 42,087,605,749,186 | 232 | 8 |
n = int(input())
a = list(map(int, input().split()))
tmp = 0
sum = 0
for i in a:
if tmp > i:
dif = tmp - i
sum += dif
else:
tmp = i
print(sum)
|
import sys
import math
lines = sys.stdin.readlines()
# print(lines)
n = int(lines[0].rstrip())
heights = [int(x) for x in lines[1].rstrip().split()]
# print(heights)
tot = 0
for i in range(n-1):
if heights[i] <= heights[i+1]:
continue
else:
diff = heights[i] - heights[i+1]
heights[i+1] += diff
tot += diff
print(tot)
| 1 | 4,511,667,359,390 | null | 88 | 88 |
import math
import sys
import collections
import bisect
readline = sys.stdin.readline
def main():
n = int(readline().rstrip())
S = [readline().rstrip() for _ in range(n)]
c = collections.Counter(S)
keys = sorted(c.keys())
maxS = c.most_common()[0][1]
for key in keys:
if c[key] == maxS:
print(key)
if __name__ == '__main__':
main()
|
n = int(input())
stmp = list(input())
Q = int(input())
target = ord("a")
s = [ord(stmp[i])-target for i in range(n)]
class BIT:
"""
<Attention> 0-indexed.
query ... return the sum [0 to m]
sum ... return the sum [a to b]
sumall ... return the sum [all]
add ... 'add' number to element (be careful that it doesn't set a value.)
search ... the sum version of bisect.right
output ... return the n-th element
listout ... return the BIT list
"""
def query(self, m):
res = 0
while m > 0:
res += self.bit[m]
m -= m&(-m)
return res
def sum(self, a, b):
return self.query(b)-self.query(a)
def sumall(self):
bitlen = self.bitlen-1
return self.query(bitlen)
def add(self, m, x):
m += 1
bitlen = len(self.bit)
while m <= bitlen-1:
self.bit[m] += x
m += m&(-m)
return
def __init__(self, n):
self.bitlen = n+1
self.bit = [0]*(n+1)
b = [BIT(n) for i in range(26)]
for i in range(n):
b[s[i]].add(i, 1)
for _ in range(Q):
q,qtmpa,qtmpb = input().split()
if q[0] == "1":
qa = int(qtmpa)
t = ord(qtmpb)-target
b[t].add(qa-1, 1)
b[s[qa-1]].add(qa-1, -1)
s[qa-1] = t
else:
ans = 0
qta = int(qtmpa)
qtb = int(qtmpb)
for i in range(26):
if b[i].sum(qta-1, qtb) != 0:
ans += 1
print(ans)
| 0 | null | 66,195,843,260,832 | 218 | 210 |
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#l = list(map(int, input().split()))
n = int(input())
a = list(map(int, input().split()))
ans = "APPROVED"
for i in range(n):
if (a[i] % 2 == 0):
if (a[i] % 3 == 0 or a[i] % 5 == 0):
pass
else:
ans = "DENIED"
break
print(ans)
|
N = int(input())
A = list(map(int,input().split()))
B = len([i for i in A if i % 2==0])
count = 0
for i in A:
if i % 2 !=0:
continue
elif i % 3 ==0 or i % 5==0:
count +=1
if count == B:
print('APPROVED')
else:
print('DENIED')
| 1 | 69,021,507,747,240 | null | 217 | 217 |
l="abcdefghijklmnopqrstuvwxyz"
print(l[l.index(input())+1])
|
x = input()
alpha2num = lambda c: ord(c) - ord('a') + 1
num2alpha = lambda c: chr(c+96+1)
a = alpha2num(x)
b = num2alpha(a)
print(b)
| 1 | 91,969,464,535,166 | null | 239 | 239 |
n, m = map(int, input().split())
a = list(map(int, input().split()))
s = sum(a)
a.sort()
a = a[::-1]
print('Yes' if s <= 4 * m * a[m - 1] else 'No')
|
n,m = list(map(int,input().split()))
A = list(map(int,input().split()))
sum_A = sum(A) / 4 / m
cnt = 0
for i in A:
if i >= sum_A:
cnt += 1
if cnt >= m:
print('Yes')
else:
print('No')
| 1 | 38,778,690,341,340 | null | 179 | 179 |
def main():
import sys
from collections import Counter
input = sys.stdin.readline
input()
a = Counter(map(int, input().split()))
ans = sum(k * v for k, v in a.items())
for _ in range(int(input())):
b, c = map(int, input().split())
if b in a:
ans += (c - b) * a[b]
print(ans)
a[c] = a[c] + a[b] if c in a else a[b]
del a[b]
else:
print(ans)
if __name__ == "__main__":
main()
|
s=input()
if s<='Z':print('A')
else:print('a')
| 0 | null | 11,856,307,605,778 | 122 | 119 |
N=int(input())
def struct(s,i,n,ma):
if i==n:
print(s)
else:
for j in range(0,ma+2):
struct(s+chr(ord('a')+j),i+1,n,max(j,ma))
struct('a',1,N,0)
|
import sys
import itertools
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')
def main():
N = in_n()
ans = []
def dfs(s):
if len(s) == N:
ans.append(s)
return
maxs = max(map(ord, s))
for i in range(ord('a'), maxs + 2):
dfs(s + chr(i))
dfs('a')
print('\n'.join(sorted(ans)))
if __name__ == '__main__':
main()
| 1 | 52,565,735,550,152 | null | 198 | 198 |
def check(k, W, p):
s = 0
count = 1
for w in W:
if s + w > p:
count += 1
s = 0
s += w
return count <= k
n, k = map(int, input().split())
W = [int(input()) for _ in range(n)]
right = sum(W)
left = max(right // k, max(W)) - 1
while right - left > 1:
p = (left + right) // 2
if check(k, W, p):
right = p
else:
left = p
print(right)
|
n, k = map(int, input().split())
ww = [int(input()) for _ in range(n)]
def chk(p):
c = 1
s = 0
for w in ww:
if w > p:
return False
if s + w <= p:
s += w
else:
c += 1
s = w
if c > k:
return False
return True
def search(l, r):
if r - l < 10:
for i in range(l, r):
if chk(i):
return i
m = (l + r) // 2
if chk(m):
return search(l, m + 1)
else:
return search(m, r)
print(search(0, 2 ** 30))
| 1 | 88,174,789,020 | null | 24 | 24 |
line = input()
N, X, T = (int(x) for x in line.split(" "))
print((N + X - 1) // X * T)
|
n,q=map(int,raw_input().split())
lst = []
for idx in xrange(n):
name, time = raw_input().split()
lst.append([name, int(time)])
idx = 0
tot = 0
while True:
lst[idx][1] = lst[idx][1] - q
if lst[idx][1] <= 0:
tot += q + lst[idx][1]
lst[idx][1] = 0
print "%s %d" % (lst[idx][0], tot)
lst.pop(idx)
if len(lst)==0:
break
else:
tot += q
idx+=1
if (idx >= len(lst)):
idx = 0
| 0 | null | 2,185,636,100,636 | 86 | 19 |
n=int(input())
x=[-1]*(10**7+5)
x[0]=0
x[1]=1
i=2
x=[-1]*(10**7+5) #2以上の自然数に対して最小の素因数を表す
x[0]=0
x[1]=1
i=2
prime=[]
while i<=10**7+1:
if x[i]==-1:
x[i]=i
prime.append(i)
for j in prime:
if i*j>10**7+1 or j>x[i]:break
x[j*i]=j
i+=1
def f(k):
if k==1:
return 1
z=[]
p=x[k]
m=1
ans=1
while k>=2:
k=k//p
if p==x[k]:
m+=1
else:
z.append(m)
m=1
p=x[k]
for i in range(len(z)):
ans*=z[i]+1
return ans
s=0
for i in range(1,n+1):
s+=i*f(i)
print(s)
|
import sys
class Card:
def __init__(self, kind, num):
self.kind = kind
self.num = num
def __eq__(self, other):
return (self.kind == other.kind) and (self.num == other.num)
def __ne__(self, other):
return not ((self.kind == other.kind) and (self.num == other.num))
def __str__(self):
return self.kind + self.num
def list_to_string(array):
s = ""
for n in array:
s += str(n) + " "
return s.strip()
def swap(array, i, j):
tmp = array[i]
array[i] = array[j]
array[j] = tmp
def is_stable(array1, array2):
if reduce(lambda x, y: x and y, map(lambda x: x[0] == x[1], zip(array1, array2))):
return "Stable"
else:
return "Not stable"
def bubble_sort(array):
for i in range(0, len(array)):
for j in reversed(range(i+1, len(array))):
if array[j].num < array[j-1].num:
swap(array, j, j-1)
return array
def selection_sort(array):
for i in range(0, len(array)):
min_index = i
for j in range(i, len(array)):
if array[min_index].num > array[j].num:
min_index = j
swap(array, i, min_index)
return array
def main():
num = int(sys.stdin.readline().strip())
array = map(lambda x: Card(x[0], x[1]), sys.stdin.readline().strip().split(" "))
a = list(array)
b = list(array)
print list_to_string(bubble_sort(a))
print "Stable"
print list_to_string(selection_sort(b))
print is_stable(a, b)
if __name__ == "__main__":
main()
| 0 | null | 5,565,783,974,208 | 118 | 16 |
n = input()
S = map(int, raw_input().split())
q = input()
T = map(int, raw_input().split())
S.append(0)
cnt = 0
for i in range(q):
j = 0
S[n] = T[i]
while S[j] != S[n]:
j = j + 1
if j != n:
cnt = cnt + 1
print cnt
|
# coding: utf-8
# input
n = int(input())
s = list(map(int,input().split()))
q = int(input())
t = list(map(int,input().split()))
cnt = 0
for i in t:
if i in s:
cnt += 1
print(cnt)
| 1 | 68,512,676,370 | null | 22 | 22 |
lst = list(input())
tmp = 0
ans = 0
for i in lst:
if i == "R":
tmp = tmp + 1
ans = max(ans, tmp)
else:
tmp = 0
print(ans)
|
s = input()
ans = 0
if 'RRR' in s:
ans = 3
elif 'RR' in s:
ans = 2
elif 'R' in s:
ans = 1
print(ans)
| 1 | 4,897,286,115,358 | null | 90 | 90 |
#3 Range Flip Find Route
h,w = map(int,input().split())
s= [None for i in range(h)]
for i in range(h):
s[i] = input()
dp = [[0]*w for i in range(h)]
dp[0][0] = int(s[0][0]=="#")
for i in range(1,w):
dp[0][i]=dp[0][i-1] + (s[0][i-1]=="." and s[0][i]=="#")
for i in range(1,h):
dp[i][0]=dp[i-1][0] + (s[i-1][0]=="." and s[i][0]=="#")
for i in range(1,h):
for j in range(1,w):
cand1 = dp[i-1][j] + (s[i-1][j]=="." and s[i][j]=="#")
cand2 = dp[i][j-1] + (s[i][j-1]=="." and s[i][j]=="#")
dp[i][j]=min(cand1,cand2)
print(dp[-1][-1])
|
N,K = map(int,input().split())
A = sorted(list(map(int,input().split())))
F = sorted(list(map(int,input().split())),reverse=True)
if K>=sum(A):
ans = 0
else:
low = 0
high = 10**12
while high-low>1:
mid = (high+low)//2
B = A[:]
cnt = 0
for i in range(N):
if B[i]*F[i]>mid:
cnt += (B[i]-mid//F[i])
if cnt<=K:
high = mid
else:
low = mid
ans = high
print(ans)
| 0 | null | 107,300,330,051,552 | 194 | 290 |
a = int(input())
b = a*a*a
c = a*a
print(a+b+c)
|
a=int(input())
result=a+a**2+a**3
print(int(result))
| 1 | 10,157,539,945,730 | null | 115 | 115 |
# 解説放送の方針
# ダメージが消える右端をキューで管理
# 右端を超えた分は、累積ダメージから引いていく
from collections import deque
N,D,A=map(int,input().split())
XH = [tuple(map(int,input().split())) for _ in range(N)]
XH = sorted(XH)
que=deque()
cum = 0
ans = 0
for i in range(N):
x,h = XH[i]
if i == 0:
r,n = x+2*D,(h+A-1)//A
d = n*A
cum += d
que.append((r,d))
ans += n
continue
while que and que[0][0]<x:
r,d = que.popleft()
cum -= d
h -= cum
if h<0:
continue
r,n = x+2*D,(h+A-1)//A
d = n*A
cum += d
que.append((r,d))
ans += n
print(ans)
|
import sys
import fractions
input = sys.stdin.readline
mod = 10 ** 9 + 7
N = int(input().strip())
A = list(map(int, input().strip().split(" ")))
lcm = 1
for a in A:
lcm = a // fractions.gcd(a, lcm) * lcm
ans = 0
for a in A:
ans += lcm // a
print(ans % mod)
| 0 | null | 84,734,723,584,548 | 230 | 235 |
from collections import Counter
N, P = map(int, input().split())
S = input()
if 10 % P == 0:
ans = 0
for i, s in enumerate(S, start=1):
if int(s) % P != 0:
continue
ans += i
print(ans)
else:
X = [0]
for i, s in enumerate(S[::-1]):
X.append((X[-1] + pow(10, i, P) * int(s)) % P)
C = Counter(X)
ans = 0
for v in C.values():
ans += v * (v - 1) // 2
print(ans)
|
N = int(input())
A = list(map(int,input().split()))
number_list = [0]*1000010
for a in A:
number_list[a] += 1
flag1 = True
flag2 = True
for i in range(2,1000001):
count = 0
for j in range(1,1000001):
if i*j > 1000000:
break
tmp = i*j
count += number_list[tmp]
if count > 1:
flag1 = False
if count == N:
flag2 = False
if flag2 == False and flag1 == False:
print("not coprime")
elif flag2 == True and flag1 == False:
print("setwise coprime")
else:
print("pairwise coprime")
| 0 | null | 31,024,220,622,992 | 205 | 85 |
import math
a,b,H,M=map(int,input().split())
h=30*H+(0.5)*M
m=6*M
c=abs(h-m)
x=math.sqrt(a**2+b**2-(2*a*b*(math.cos(math.radians(c)))))
print(x)
|
def bisect_left(L,x):
lo=0
hi=N
while lo<hi:
mid=(lo+hi)//2
if L[mid]<x: lo=mid+1
else: hi=mid
return lo
N=int(input())
L=list(map(int,input().split()))
L=sorted(L)
ans=0
for i in range(N-2):
a=L[i]
for j in range(i+1,N-1):
b=L[j]
"""
lo=0
hi=N
x=a+b
while lo<hi:
mid=(lo+hi)//2
if L[mid]<x: lo=mid+1
else: hi=mid
ans+=lo-(j+1)
"""
ans+=bisect_left(L,a+b)-(j+1)
print(ans)
| 0 | null | 95,614,505,414,912 | 144 | 294 |
a = int(input())
b = input()
for i in range(0, len(b)):
c = ord(b[i])
d = int(c+a)
if d>90:
d -= 26
e = chr(d)
print(e, end="")
|
def main():
N = int(input())
x, y = map(int, input().split())
a1 = x + y
a2 = x + y
b1 = y - x
b2 = y - x
N -= 1
while N != 0:
x, y = map(int, input().split())
a1 = max(a1, x + y)
a2 = min(a2, x + y)
b1 = max(b1, y - x)
b2 = min(b2, y - x)
N = N - 1
print(max(a1 - a2, b1 - b2))
main()
| 0 | null | 69,138,829,380,112 | 271 | 80 |
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
# あるbit列に対するコストの計算
def count(zerone):
one = 0
cnt = 0
for i in range(N):
flag = True
if zerone[i] == 1:
one += 1
for j in range(len(A[i])):
if A[i][j][1] != zerone[A[i][j][0]-1]:
flag = False
break
if flag:
cnt += 1
if cnt == N:
return one
else:
return 0
# bit列の列挙
def dfs(bits):
if len(bits) == N:
return count(bits)
res = 0
for i in range(2):
bits.append(i)
res = max(res, dfs(bits))
bits.pop()
return res
# main
N = int(input())
A = [[] for _ in range(N)]
for i in range(N):
a = int(input())
for _ in range(a):
A[i].append([int(x) for x in input().split()])
ans = dfs([])
print(ans)
|
t=0
h=0
n=input()
for _ in range (n):
s = raw_input().split()
if s[0] > s[1]:
t+=3
elif s[0] < s[1]:
h+=3
else:
t+=1
h+=1
print str(t) + " " + str(h)
| 0 | null | 62,104,263,063,212 | 262 | 67 |
from collections import deque
n, m = map(int, input().split())
links = [[] for _ in range(n)]
for _ in range(m):
a, b = map(int, input().split())
a -= 1
b -= 1
links[a].append(b)
links[b].append(a)
ans = [-1] * n
ans[0] = 0
q = deque([(0, 0)])
while q:
room, prev = q.popleft()
for next_ in links[room]:
if ans[next_] < 0:
ans[next_] = room
q.append((next_, room))
print('Yes')
for i in range(1, n):
print(ans[i] + 1)
|
import sys
from sys import stdin
def I():
return stdin.readline().rstrip()
def MI():
return map(int,stdin.readline().rstrip().split())
def LI():
return list(map(int,stdin.readline().rstrip().split()))
#main part
from collections import deque
n, m =MI()
ans = [-1 for _ in range(n+1)]
ans[0] = 0
ans[1] = 0
V = [[] for _ in range(n+1)]
for _ in range(m):
x, y = MI()
V[x].append(y)
V[y].append(x)
d = deque([1])
while d:
l = d.popleft()
for v in V[l]:
if ans[v] != -1:
continue
ans[v] = l
d.append(v)
if ans.count(-1) > 0:
print('No')
else :
print('Yes')
for i in ans[2:]:
print(i)
| 1 | 20,464,893,637,760 | null | 145 | 145 |
# -*- coding: utf-8 -*-
str = raw_input()
for _ in xrange(input()):
ops = raw_input().split()
a = int(ops[1])
b = int(ops[2]) + 1
op = ops[0]
if op[0]=="p": print str[a:b]
elif op[2]=="v": str = str[:a] + str[a:b][::-1] + str[b:]
else: str = str[:a] + ops[3] + str[b:]
|
str = input()
q = int(input())
for i in range(q):
args = input().split()
order = args[0]
a = int(args[1])
b = int(args[2])
if order == "print":
print(str[a : b + 1])
elif order == "reverse":
str = str[: a] + str[a : b + 1][: : -1] + str[b + 1 :]
elif order == "replace":
str = str[: a] + args[3] + str[b + 1 :]
| 1 | 2,105,606,665,122 | null | 68 | 68 |
import collections
n=int(input())
data=[""]*n
for i in range(n):
data[i]=input()
data=sorted(data)
count=collections.Counter(data)
num=max(count.values())
for v,k in count.items():
if k==num:
print(v)
|
n, p = [int(_) for _ in input().split()]
s = input()
if p == 2 or p == 5:
ans = 0
for i in range(n):
if int(s[i]) % p == 0:
ans += i+1
print(ans)
exit()
t = [0 for i in range(n+1)]
P = [0 for i in range(p)]
P[0] += 1
d = 1
for i in range(1, n+1):
t[i] = (t[i-1]+int(s[n-i])*d) % p
P[t[i]] += 1
d = d*10 % p
ans = 0
for c in P:
ans += c*(c-1)//2
# print(t)
print(ans)
| 0 | null | 64,043,081,331,868 | 218 | 205 |
s= input().strip()
print(s.replace("?","D",len(s)))
|
def dfs(v, time_, d, f, dp):
if d[v] == 0:
d[v] = time_
for i in range(len(dp[v])):
if d[dp[v][i]] == 0:
time_ = dfs(dp[v][i], time_ + 1, d, f, dp)
f[v] = time_ + 1
return time_ + 1
def main():
N = int(input())
dp = [[] for i in range(N)]
for i in range(N):
line = [int(k) for k in input().split()]
for j in range(len(line)):
if j == 0 or j == 1:
continue
dp[line[0] - 1].append(line[j] - 1)
d = [0] * N
f = [0] * N
time_ = 1
for i in range(N):
if d[i] == 0:
time_ = dfs(i, time_, d, f, dp)
time_ += 1
for i in range(N):
print(i + 1, d[i], f[i])
if __name__ == '__main__':
main()
| 0 | null | 9,233,371,234,552 | 140 | 8 |
A = 0
W = str(input())
while True:
T = str(input())
if T == 'END_OF_TEXT':
break
T = (T.lower().split())
a = (T.count(W))
A = A + a
print(A)
|
import math
r = int(input())
print(int(r * r))
| 0 | null | 73,303,308,773,200 | 65 | 278 |
N = int(input())
a = list(map(int, input().split()))
MOD = 10**9+7
ans = 0
for j in range(60):
count = 0
for i in range(len(a)):
a[i], pre = divmod(a[i], 2)
count += pre
ans += count*(N-count)*2**j
ans %= MOD
print(ans)
|
K = int(input())
S = input()
print(S[:K] + ('...' if len(S) > K else ''))
| 0 | null | 71,586,507,788,994 | 263 | 143 |
class Combination:
"""
O(n)の前計算を1回行うことで,O(1)でnCr mod mを求められる
n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
使用例:
comb = Combination(1000000)
print(comb(5, 3)) # 10
"""
def __init__(self, n_max, mod=10**9+7):
self.mod = mod
self.modinv = self.make_modinv_list(n_max)
self.fac, self.facinv = self.make_factorial_list(n_max)
def __call__(self, n, r):
if n < r:
return 0
return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod
def make_factorial_list(self, n):
# 階乗のリストと階乗のmod逆元のリストを返す O(n)
# self.make_modinv_list()が先に実行されている必要がある
fac = [1]
facinv = [1]
for i in range(1, n+1):
fac.append(fac[i-1] * i % self.mod)
facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
return fac, facinv
def make_modinv_list(self, n):
# 0からnまでのmod逆元のリストを返す O(n)
modinv = [0] * (n+1)
modinv[1] = 1
for i in range(2, n+1):
modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
return modinv
def main():
N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7
A.sort()
ans = 0
comb = Combination(1000000)
cnt = []
for i in range(N-K+1):
cnt.append(comb(N-i-1,K-1))
ans -= comb(N-i-1,K-1) * A[i]
for i in range(N,N-len(cnt),-1):
ans += A[i-1] * (cnt[N-i])
ans %= MOD
print(ans)
if __name__ == "__main__":
main()
|
n, k = [int(x) for x in input().split()]
a_list = [int(x) for x in input().split()]
a_list.sort()
mod = 10 ** 9 + 7
temp_list = [1] * (n + 1)
for i in range(1, n + 1):
temp_list[i] = i * temp_list[i - 1] % mod
i_temp_list = [1] * (n + 1)
i_temp_list[-1] = pow(temp_list[-1], mod - 2, mod)
for i in range(n, 0, -1):
i_temp_list[i - 1] = i_temp_list[i] * i % mod
y = k - 1
ans = 0
for i in range(k - 1, n):
x = i
num = temp_list[x] * i_temp_list[y] * i_temp_list[x - y] % mod
ans += a_list[i] * num % mod
for i in range(n - k + 1):
x = n - 1 - i
num = temp_list[x] * i_temp_list[y] * i_temp_list[x - y] % mod
ans -= a_list[i] * num % mod
ans %= mod
print(ans)
| 1 | 95,969,019,163,600 | null | 242 | 242 |
import math
a, b, h, m = map(int, input().split())
h_rad = (h * 60 + m) * math.pi / 360
m_rad = m * math.pi / 30
cosine = math.cos(abs(h_rad - m_rad))
print(math.sqrt(a ** 2 + b ** 2 - 2 * a * b * cosine))
|
import numpy as np
A,B,H,M=map(int,input().split())
M_angle=M*6
h_=0
if M_angle>0:
h_=30/(360/M_angle)
H_angle=H*5*6+h_
Angle=abs(H_angle-M_angle)
Cos=np.cos(np.deg2rad(Angle))
ans_=(A**2)+(B**2)-(2*A*B*Cos)
ans=ans_**0.5
print(ans)
| 1 | 20,189,049,840,672 | null | 144 | 144 |
n, k = list(map(int, input().split()))
mod = 10**9 + 7
# xの逆元を求める。フェルマーの小定理より、 x の逆元は x ^ (mod - 2) に等しい。計算時間はO(log(mod))程度。
def modinv(x):
return pow(x, mod-2, mod)
# 二項係数の左側の数字の最大値を max_len とする。nとかだと他の変数と被りそうなので。
# factori_table = [1, 1, 2, 6, 24, 120, ...] 要は factori_table[n] = n!
# 計算時間はO(max_len * log(mod))
max_len = 2 * n - 1 #適宜変更する
factori_table = [1] * (max_len + 1)
factori_inv_table = [1] * (max_len + 1)
for i in range(1, max_len + 1):
factori_table[i] = factori_table[i-1] * (i) % mod
factori_inv_table[i] = modinv(factori_table[i])
def binomial_coefficients(n, k):
# n! / (k! * (n-k)! )
if k <= 0 or k >= n:
return 1
return (factori_table[n] * factori_inv_table[k] * factori_inv_table[n-k]) % mod
if k >= n-1:
# nHn = 2n-1 C n
print(binomial_coefficients(2 * n - 1, n))
else:
# 移動がk回←→ 人数0の部屋がk個以下
# 人数0の部屋がちょうどj個のものは
# nCj(人数0の部屋の選び方) * jH(n-j) (余剰のj人を残りの部屋に入れる)
ans = 0
for j in range(k+1):
if j == 0:
ans += 1
else:
ans += binomial_coefficients(n, j) * binomial_coefficients(n-1, j)
ans %= mod
print(ans)
|
S=list(input())
K=int(input())
cnt=cnt2=0
s=S*2
if len(set(S))==1:
print(len(S)*K//2);exit()
for i in range(len(S)-1):
if S[i+1]==S[i]:
cnt+=1
S[i+1]="#"
for i in range(len(s)-1):
if s[i+1]==s[i]:
cnt2+=1
s[i+1]="#"
print(cnt+(cnt2-cnt)*(K-1))
| 0 | null | 121,056,047,682,934 | 215 | 296 |
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import combinations_with_replacement
import numpy as np
def main():
n, m, q = map(int, input().split())
A = np.array(list(combinations_with_replacement(range(1, m + 1), n)))
numA = len(A)
score = np.zeros(numA, np.int32)
ma = map(int, read().split())
for a, b, c, d in zip(ma, ma, ma, ma):
a -= 1
b -= 1
eachA_is_equalOrNot = A[:, b] - A[:, a] == c
score += d * eachA_is_equalOrNot
# print(score)
print(score.max())
if __name__ == '__main__':
main()
|
a,b=(int(x) for x in input().split())
print(a//b,a%b,"{0:.5f}".format(a/b))
| 0 | null | 14,068,427,400,660 | 160 | 45 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
????????????
"""
inp = input().strip().split(" ")
n = int(inp[0])
m = int(inp[1])
l = int(inp[2])
# ?????????????????????????¢????
# A[n][m] B[m][l] C[n][l]
A = [[0 for i in range(m)] for j in range(n)]
B = [[0 for i in range(l)] for j in range(m)]
C = [[0 for i in range(l)] for j in range(n)]
# A???????????°??????????????????
for i in range(n):
inp = input().strip().split(" ")
for j in range(m):
A[i][j] = int(inp[j])
# B???????????°??????????????????
for i in range(m):
inp = input().strip().split(" ")
for j in range(l):
B[i][j] = int(inp[j])
# A x B
for i in range(n):
for j in range(l):
for k in range(m):
C[i][j] += A[i][k] * B[k][j]
print(" ".join(map(str,C[i])))
|
a = input().split()
x = []
y = []
z = []
a[0] = int(a[0])
a[1] = int(a[1])
a[2] = int(a[2])
for i in range(a[0]):
b = input().split()
x.append(b)
z.append([])
for i in range(a[1]):
b = input().split()
y.append(b)
for i in range(a[0]):
for j in range(a[2]):
z[i].append(0)
for i in range(a[0]):
for j in range(a[2]):
for k in range(a[1]):
z[i][j] += int(x[i][k])*int(y[k][j])
for i in range(len(z)):
for j in range(a[2]):
z[i][j] = str(z[i][j])
for i in z:
gam = " ".join(i)
print(gam)
| 1 | 1,442,359,172,060 | null | 60 | 60 |
H, N = map(int, input().split())
dp = [10 ** 9] * (H + 1)
dp[0] = 0
for _ in range(N):
a, b = map(int, input().split())
for i in range(H):
idx = min(H, i + a)
dp[idx] = min(dp[idx], dp[i] + b)
print(dp[H])
|
n=int(input())
a1=1
a=1
i=1
while i<n:
a1,a=a,a1+a
i+=1
print(a)
| 0 | null | 40,637,579,378,990 | 229 | 7 |
n, *a = map(int, open(0).read().split())
mod = 10 ** 9 + 7
ans = 0
for i in range(60):
cnt = 0
for j in range(n):
cnt += a[j] >> i & 1
ans = (ans + (1 << i) * cnt * (n - cnt) % mod) % mod
print(ans)
|
while True:
str_shuffled = input()
if str_shuffled == '-':
break
shuffle_time = int(input())
for _ in range(shuffle_time):
num_to_shuffle = int(input())
str_shuffled = str_shuffled[num_to_shuffle:] + str_shuffled[:num_to_shuffle]
print(str_shuffled)
| 0 | null | 62,222,052,168,480 | 263 | 66 |
import sys
lines = [line.split() for line in sys.stdin]
T = H = 0
n = int(lines[0][0])
for w1,w2 in lines[1:]:
if w1 > w2:
T += 3
elif w1 < w2:
H += 3
else:
T += 1
H += 1
print (str(T) + " " + str(H))
|
def factorials(n,mod=10**9+7):
# 0~n!のリスト
f = [1,1]
for i in range(2,n+1):
f.append(f[-1]*i%mod)
# 0~n!の逆元のリスト
g = [1]
for i in range(n):
g.append(pow(f[i+1],mod-2,mod))
return f,g
def combination_mod(f,g,n,r,mod=10**9+7):
# nCr mod modを求める
nCr = f[n] * g[n-r] * g[r]
nCr %= mod
return nCr
def permutation_mod(f,g,n,r,mod=10**9+7):
# nPr mod modを求める
nPr = f[n] * g[n-r]
nPr %= mod
return nPr
n,k = map(int, input().split())
a = list(map(int, input().split()))
m = 10**9+7
a.sort()
f,g = factorials(n)
ans = 0
for i in range(k-1,n):
ans += combination_mod(f,g,i,k-1)*a[i]
for i in range(n-k+1):
ans -= combination_mod(f,g,n-i-1,k-1)*a[i]
while ans < 0:
ans += m
print(ans%m)
| 0 | null | 49,074,399,686,872 | 67 | 242 |
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())
ave_a = t1 * a1 + t2 * a2
ave_b = t1 * b1 + t2 * b2
if ave_a < ave_b:
(a1, a2), (b1, b2) = (b1, b2), (a1, a2)
ave_a, ave_b = ave_b, ave_a
if ave_a == ave_b:
print('infinity')
exit()
half, all = t1 * (b1 - a1), ave_a - ave_b
ans = half // all * 2 + (half % all != 0)
print(max(0, ans))
|
T1,T2 = map(int,input().split())
A1,A2 = map(int,input().split())
B1,B2 = map(int,input().split())
ans = 0
if T1*A1+T2*A2 == T1*B1+T2*B2:
print("infinity")
exit()
if A1>B1:
jtA = True
else:
jtA = False
if jtA:
if T1*A1+T2*A2 > T1*B1+T2*B2:
print(0)
exit()
else:
jA = False
x = abs(T1*B1+T2*B2 - T1*A1-T2*A2)
else:
if T1*A1+T2*A2 < T1*B1+T2*B2:
print(0)
exit()
else:
jA = True
x = abs(T1*A1+T2*A2 - T1*B1-T2*B2)
if (abs(A1-B1)*T1)%x == 0:
ans += ((abs(A1-B1)*T1)//x)*2
else:
ans += ((abs(A1-B1)*T1)//x)*2+1
print(ans)
| 1 | 131,854,192,687,360 | null | 269 | 269 |
N = int(input())
ans = 0
for i in range(1, N+1):
if i % 3 == 0:
continue
if i % 5 == 0:
continue
ans += i
print(ans)
|
N = int(input())
ans = 0
for i in range(N):
j = i + 1
if j % 3 != 0 and j % 5 != 0:
ans += j
print(ans)
| 1 | 34,924,107,356,248 | null | 173 | 173 |
asd=input()
dfg=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
print(dfg[dfg.index(asd)+1])
|
a = [chr(i) for i in range(97, 97+26)]
b = input()
print(a[a.index(b)+1])
| 1 | 92,652,635,861,390 | null | 239 | 239 |
from collections import deque
k=int(input())
d=deque()
for i in range(1,10):
d.append(i)
for i in range(k-1):
x=d.popleft()
if x%10!=0:
d.append(10*x+(x%10)-1)
d.append(10*x+(x%10))
if x%10!=9:
d.append(10*x+(x%10)+1)
print(d.popleft())
|
r=int(input())
print((r*r))
| 0 | null | 92,507,697,925,280 | 181 | 278 |
a = 0
N = int(input())
for i in range(1, 10):
for j in range(1, 10):
if N == i*j:
print('Yes')
a = 1
if a == 1:
break
if a == 0:
print('No')
|
# B-81
# 標準入力 N
N = int(input())
num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
j = 0
for i in range(1, 9 + 1):
if (N / i) in num_list:
j += 1
if j == 0:
print('No')
else:
print('Yes')
| 1 | 159,792,465,438,560 | null | 287 | 287 |
a,b,c,k = map(int,input().split())
s = 0
if k <= a:
s = k
elif k <= a + b:
s = a
else:
s = a - (k - a - b)
print(s)
|
a,b,c,k = map(int,input().split())
if k < a:
print(str(k))
elif k < (a + b):
print(str(a))
else:
print(str(2*a + b - k))
| 1 | 21,810,371,167,492 | null | 148 | 148 |
a,b,n = map(int,input().split())
if b <= n:
k = b-1
else:
k = n
m = int(k*a/b) - a*int(k/b)
print(m)
|
import sys
a,b,c = map(int,sys.stdin.readline().split())
if a < b and b < c:
print('Yes')
else:
print('No')
| 0 | null | 14,373,021,060,572 | 161 | 39 |
alp = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','w','v','x','y','z']
c = str(input())
print(alp[alp.index(c)+1])
|
C = input()
letters = 'abcdefghijklmnopqrstuxyz'
print(letters[(letters.index(C)+1)])
| 1 | 92,182,025,780,572 | null | 239 | 239 |
a = [0]
n = 0
def main():
global n
for s in input():
if s == "<":
nproc()
a.append(a[-1]+1)
else:
n += 1
nproc()
print(sum(a))
def nproc():
global n, a
if n > 0 and n > a[-1]:
a[-1] = n
for i in list(range(n))[::-1]:
a.append(i)
n = 0
if __name__ == "__main__":
main()
|
s=input()
a=[0]*(len(s)+1)
for i in range(len(s)):
if s[i]=="<":
a[i+1]=a[i]+1
for i in range(len(s)-1,-1,-1):
if s[i]==">":
a[i]=max(a[i],a[i+1]+1)
print(sum(a))
| 1 | 156,168,961,365,088 | null | 285 | 285 |
a, b, c, d = map(int, input().split())
t_death = a // d + 1 if a % d else a // d
a_death = c // b + 1 if c % b else c // b
if t_death >= a_death:
print('Yes')
else:
print('No')
|
n = int(input())
a = list(map(int, input().split()))
result = 'APPROVED'
for i in a:
if i % 2 == 0:
if i % 3 != 0:
if i % 5 != 0:
result = 'DENIED'
print(result)
| 0 | null | 49,174,724,040,640 | 164 | 217 |
import bisect, collections, copy, heapq, itertools, math, string
from functools import reduce
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())
def gcd(*numbers):
return reduce(math.gcd, numbers)
K = I()
ans = 0
for i in range(1, K + 1):
for j in range(1, K + 1):
for k in range(1, K + 1):
ans += gcd(i, j, k)
print(ans)
|
#!/usr/bin/env python3
from functools import reduce
mod = 10**9 + 7
n, k, *a = map(int, open(0).read().split())
a.sort(key=lambda x: abs(x))
ans = reduce(lambda a, b: (a * b) % mod, a[-k:])
c = a[-k:]
j = sum(i < 0 for i in c) % 2
if j:
c = a[-k:]
neg = [i for i in c if i < 0]
pos = [i for i in c if i > 0]
b = sorted(a[:n - k])
if b == []:
print(ans)
exit()
if neg == []:
if pos[0] * b[0] < 0:
ans = ans * pow(pos[0], mod - 2, mod) * b[0] % mod
else:
ans = reduce(lambda a, b: (a * b) % mod, a[:k])
elif pos == []:
if neg[0] * b[-1] < 0:
ans = ans * pow(neg[0], mod - 2, mod) * b[-1] % mod
else:
ans = reduce(lambda a, b: (a * b) % mod, a[:k])
elif pos[0] * b[-1] < neg[0] * b[0] and pos[0] * b[0] < 0:
ans = ans * pow(pos[0], mod - 2, mod) * b[0] % mod
elif 0 > b[-1] * neg[0]:
ans = ans * pow(neg[0], mod - 2, mod) * b[-1] % mod
else:
ans = reduce(lambda a, b: (a * b) % mod, a[:k])
print(ans)
| 0 | null | 22,389,411,087,218 | 174 | 112 |
import math
import collections
import copy
import sys
n = int(input())
a = list(map(int,input().split()))
limit = 10 ** 6
sCheck = math.gcd(a[0],a[1])
if n == 2:
if sCheck == 1:
print("pairwise coprime")
else:
print("not coprime")
sys.exit()
else:
for i in range(2,n):
sCheck = math.gcd(sCheck,a[i])
beforeQ = collections.deque([int(i) for i in range(3,(limit + 1))])
D = dict()
p = 2
D[2] = 2
D[1] = 1
while p < math.sqrt(limit):
afterQ = collections.deque()
while len(beforeQ) > 0:
k = beforeQ.popleft()
if k % p != 0:
afterQ.append(k)
else:
D[k] = p
beforeQ = copy.copy(afterQ)
p = beforeQ.popleft()
D[p] = p
while len(beforeQ) > 0:
k = beforeQ.popleft()
D[k] = k
ansSet = set()
count = 0
for i in a:
k = i
kSet = set()
while k != 1:
if D[k] in ansSet:
if not(D[k] in kSet):
count = 1
break
else:
ansSet.add(D[k])
kSet.add(D[k])
k = int(k // D[k])
if count == 1:
break
if count == 0:
print("pairwise coprime")
elif sCheck == 1:
print("setwise coprime")
else:
print("not coprime")
|
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")
| 1 | 4,145,353,032,000 | null | 85 | 85 |
class BalancingTree:
"""平衡二分木のクラス
Pivotを使った実装.
0以上2^n - 2以下の整数を扱う
"""
def __init__(self, n):
"""
2の指数nで初期化
"""
self.N = n
self.root = self.node(1<<n, 1<<n)
def debug(self):
"""デバッグ用の関数"""
def debug_info(nd_):
return (nd_.value - 1, nd_.pivot - 1, nd_.left.value - 1 if nd_.left else -1, nd_.right.value - 1 if nd_.right else -1)
def debug_node(nd):
re = []
if nd.left:
re += debug_node(nd.left)
if nd.value: re.append(debug_info(nd))
if nd.right:
re += debug_node(nd.right)
return re
print("Debug - root =", self.root.value - 1, debug_node(self.root)[:50])
def append(self, v):
"""
v を追加(その時点で v はない前提)
"""
v += 1
nd = self.root
while True:
if v == nd.value:
# v がすでに存在する場合に何か処理が必要ならここに書く
return 0
else:
mi, ma = min(v, nd.value), max(v, nd.value)
if mi < nd.pivot:
nd.value = ma
if nd.left:
nd = nd.left
v = mi
else:
p = nd.pivot
nd.left = self.node(mi, p - (p&-p)//2)
break
else:
nd.value = mi
if nd.right:
nd = nd.right
v = ma
else:
p = nd.pivot
nd.right = self.node(ma, p + (p&-p)//2)
break
def leftmost(self, nd):
if nd.left: return self.leftmost(nd.left)
return nd
def rightmost(self, nd):
if nd.right: return self.rightmost(nd.right)
return nd
def find_l(self, v):
"""
vより真に小さいやつの中での最大値(なければ-1)
"""
v += 1
nd = self.root
prev = 0
if nd.value < v: prev = nd.value
while True:
if v <= nd.value:
if nd.left:
nd = nd.left
else:
return prev - 1
else:
prev = nd.value
if nd.right:
nd = nd.right
else:
return prev - 1
def find_r(self, v):
"""
vより真に大きいやつの中での最小値(なければRoot)
"""
v += 1
nd = self.root
prev = 0
if nd.value > v: prev = nd.value
while True:
if v < nd.value:
prev = nd.value
if nd.left:
nd = nd.left
else:
return prev - 1
else:
if nd.right:
nd = nd.right
else:
return prev - 1
@property
def max(self):
"""最大値の属性"""
return self.find_l((1<<self.N)-1)
@property
def min(self):
"""最小値の属性"""
return self.find_r(-1)
def delete(self, v, nd = None, prev = None):
"""
値がvのノードがあれば削除(なければ何もしない)
"""
v += 1
if not nd: nd = self.root
if not prev: prev = nd
while v != nd.value:
prev = nd
if v <= nd.value:
if nd.left:
nd = nd.left
else:
return
else:
if nd.right:
nd = nd.right
else:
return
if (not nd.left) and (not nd.right):
if nd.value < prev.value:
prev.left = None
else:
prev.right = None
elif not nd.left:
if nd.value < prev.value:
prev.left = nd.right
else:
prev.right = nd.right
elif not nd.right:
if nd.value < prev.value:
prev.left = nd.left
else:
prev.right = nd.left
else:
nd.value = self.leftmost(nd.right).value
self.delete(nd.value - 1, nd.right, nd)
def __contains__(self, v: int) -> bool:
return self.find_r(v - 1) == v
class node:
"""ノードをあらわすクラス
v: 値
p: ピボット値
で初期化
"""
def __init__(self, v, p):
self.value = v
self.pivot = p
self.left = None
self.right = None
Trees = [BalancingTree(50) for _ in range(26)]
N = int(input())
S = input()
Q = int(input())
alphabets = list("abcdefghijklmnopqrstuvwxyz")
c2n = {c: i for i, c in enumerate(alphabets)}
for i in range(N):
Trees[c2n[S[i]]].append(i+1)
S = list(S)
for _ in range(Q):
tmp = list(input().split())
if tmp[0] == "1":
_, i, c = tmp
i = int(i)
bef = S[i-1]
if bef == c:
continue
Trees[c2n[bef]].delete(i)
Trees[c2n[c]].append(i)
S[i-1] = c
else:
_, l, r = tmp
l = int(l)
r = int(r)
ans = 0
for char in range(26):
res = Trees[char].find_r(l-1)
if l <= res <= r:
ans += 1
print(ans)
|
# -*- coding:utf-8 -*-
def solve():
from collections import deque
N = int(input())
Us = [[] for _ in range(N)]
for i in range(N):
_in = list(map(int, input().split()))
if len(_in) == 2:
continue
u, k, Vs = _in[0], _in[1], _in[2:]
Vs.sort()
for v in Vs:
Us[u-1].append(v-1)
Us[u-1] = deque(Us[u-1])
find = [0 for _ in range(N)] # 発見時刻
terminated = [0 for _ in range(N)] # 終了時刻
tim = [0] # 経過時間
def dfs(now):
while Us[now]:
nxt = Us[now].popleft()
if find[nxt] != 0:
continue
tim[0] += 1
find[nxt] = tim[0]
dfs(nxt)
if terminated[now] == 0:
tim[0] += 1
terminated[now] = tim[0]
for i in range(N):
if find[i] == 0:
tim[0] += 1
find[i] = tim[0]
dfs(i)
for i in range(N):
print(i+1, find[i], terminated[i])
if __name__ == "__main__":
solve()
| 0 | null | 31,424,406,360,590 | 210 | 8 |
a, b, c, d = map(int, input().split())
taka = 0
aoki = 0
while a > 0:
a = a - d
taka += 1
while c > 0:
c = c - b
aoki += 1
if taka < aoki:
print("No")
else:
print("Yes")
|
import math
A,B,C,D = map(int, input().split())
Taka=A/D
Aoki=C/B
if math.ceil(Taka)>=math.ceil(Aoki):
print("Yes")
else:
print("No")
| 1 | 29,759,388,350,120 | null | 164 | 164 |
from math import *
a, b, C = map(float, input().split())
c = sqrt(a ** 2 + b ** 2 - 2 * a * b * cos(radians(C)))
s = (a + b + c) / 2
S = sqrt(s * (s - a) * (s - b) * (s - c))
L = a + b + c
h = b * sin(radians(C))
print(S)
print(L)
print(h)
|
n=int(input())
a=[int(k) for k in input().split()]
sum=0;
max=a[0];
for aa in a:
if aa<max:
sum=sum+max-aa
else:
max=aa
print(sum)
| 0 | null | 2,384,983,582,780 | 30 | 88 |
def gcd(a,b):
if b == 0:return a
return gcd(b,a%b)
a = int(input())
print(360//gcd(360,a))
|
X=int(input())
i=1
a=0
while True:
a=a+X
if a%360==0:
print(i)
break
i+=1
| 1 | 13,050,276,524,572 | null | 125 | 125 |
n = int(input())
x = list(map(int, input().split()))
p = [0] * 100
for i in range(1, 101):
for j in x:
p[i-1] += (i - j)**2
print(min(p))
|
import sys
def main():
N = int(sys.stdin.readline().rstrip())
X = [int(x) for x in sys.stdin.readline().rstrip().split()]
m = 100**2 * 100
for p in range(101):
tmp = 0
for x in X:
tmp += (x - p)**2
m = min(tmp,m)
print(m)
main()
| 1 | 65,376,904,566,972 | null | 213 | 213 |
import math
import itertools
from collections import deque
import bisect
import heapq
def IN(): return int(input())
def sIN(): return input()
def lIN(): return list(input())
def MAP(): return map(int,input().split())
def LMAP(): return list(map(int,input().split()))
def TATE(n): return [input() for i in range(n)]
ans = 0
def bfs(sx,sy):
d = [[-1] * w for i in range(h)]
MAX = 0
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
que = deque([])
que.append((sx, sy))#スタート座標の記録
d[sx][sy] = 0#スタートからスタートへの最短距離は0
while que:#中身がなくなるまで
p = que.popleft()
for m in range(4):#現在地から4方向の移動を考える
nx = p[0] + dx[m]
ny = p[1] + dy[m]
if 0 <= nx < h and 0 <= ny < w:
if maze[nx][ny] != "#" and d[nx][ny] == -1:
que.append((nx, ny))#↑格子点からでない&壁でない&まだ通ってない
d[nx][ny] = d[p[0]][p[1]] + 1
for k in range(h):
MAX = max(max(d[k]),MAX)
return MAX
h, w = map(int, input().split())
maze = [lIN() for i in range(h)]
for i in range(h):#sx座標指定0~h-1
for j in range(w):#sy座標指定0~w-1
if maze[i][j] == '.':
ans = max(bfs(i,j),ans)
print(ans)
|
from sys import stdin
from math import ceil
def is_stackable(k,p,w):
if max(w) > p:
return False
s = w[0]
count = len(w)
for i in range(1, len(w)):
if s + w[i] <= p:
s += w[i]
count -= 1
else:
s = w[i]
return k >= count
def main():
n,k = map(int, stdin.readline().split())
w = [int(line) for line in stdin.readlines()]
left = max(max(w), ceil(sum(w)/k))
right = (max(w) * ceil(n/k)) + 1
while True:
mid = int((left + right) / 2)
if is_stackable(k, mid, w):
if not is_stackable(k, mid - 1, w):
print(mid)
break
right = mid
else:
if is_stackable(k, mid + 1, w):
print(mid + 1)
break
left = mid + 1
main()
| 0 | null | 47,386,641,588,700 | 241 | 24 |
import sys
from operator import itemgetter
input = sys.stdin.readline
def nibun_right(a, x):
lo, hi = 0, len(a)
while lo < hi:
mid = (lo+hi)//2
if x < a[mid][0]: hi = mid
else: lo = mid+1
return lo
N,D,A=map(int,input().split())
lst=[0]*N
for i in range(N):
lst[i]=list(map(int,input().split()))
lst[i][1]=int((lst[i][1]-1)/A)+1
lst.sort(key=itemgetter(0))
DMG=[0]*(N+1)
ans=0
for i in range(N):
renji=nibun_right(lst,lst[i][0]+2*D)
Z=max(lst[i][1]-DMG[i],0)
DMG[i]+=Z
DMG[renji]-=Z
ans+=Z
DMG[i+1]+=DMG[i]
print(ans)
|
import sys
# 最大公約数(greatest common divisor)を求める関数
def gcd(a, b):
while b:
a, b = b, a % b
return a
# 最小公倍数(least common multiple)を求める関数
# //は切り捨て除算
def lcm(a, b):
return a * b // gcd(a, b)
lines = sys.stdin.readlines()
for line in lines:
a = list(map(int, line.split(" ")))
print(gcd(a[0], a[1]), lcm(a[0], a[1]))
| 0 | null | 41,128,325,907,920 | 230 | 5 |
# E - Bullet
from collections import Counter
from math import gcd
n = int(input())
counter = Counter() # 各ベクトルの個数
for i in range(n):
a, b = map(int, input().split())
if b < 0:
a, b = -a, -b # 180度回転して第1〜2象限に
elif b == 0:
a = abs(a)
if not a == b == 0:
# 既約化
g = gcd(a, b)
a //= g
b //= g
counter[(a, b)] += 1
modulus = 1000000007
vs = set(counter)
# 第2象限のベクトルと直交するベクトルを追加
vs.update((b, -a) for a, b in counter if a <= 0)
# 第1象限のベクトルのみ抽出
vs = [(a, b) for a, b in vs if a > 0]
ncomb = 1 # イワシの選び方の個数
for a, b in vs:
# 互いに仲が悪いイワシ群
n1 = counter[(a, b)]
n2 = counter[(-b, a)]
# それぞれの群から好きな数だけイワシを選ぶ (0匹選ぶことも含む)
m = pow(2, n1, modulus) + pow(2, n2, modulus) - 1
ncomb = ncomb * m % modulus
ncomb -= 1 # 1匹も選ばない場合を除く
# (0, 0)のイワシを1匹だけ選ぶ
ncomb += counter[(0, 0)]
print(ncomb % modulus)
|
N = int(input())
zoro = 0
ans = 'No'
for i in range(N):
D1, D2 = map(int, input().split())
if D1 == D2:
zoro += 1
else:
zoro = 0
if zoro == 3:
ans = 'Yes'
break
print(ans)
| 0 | null | 11,658,728,209,920 | 146 | 72 |
import sys
input = sys.stdin.readline
a, b = map(int, input().split())
print(abs(a * b) // __import__('math').gcd(a, b))
|
n = int(input())
alp = [chr(i) for i in range(97, 97+26)]
def dfs(i):
if i == 1:
return [("a", 1)]
else:
ret = []
for i in dfs(i-1):
nowstr, nowmax = i
for j in range(nowmax+1):
ret.append((nowstr+alp[j], max(j+1,nowmax)))
return ret
ans = dfs(n)
#print(ans)
for i in range(len(ans)):
print(ans[i][0])
| 0 | null | 82,902,448,088,508 | 256 | 198 |
N = int(input())
S = str(input())
#print(N,S)
stack = []
while len(S) > 0:
if len(stack) == 0:
stack.append(S[0])
S = S[1:]
elif S[0] == stack[-1]:
S = S[1:]
elif S[0] != stack[-1]:
stack.append(S[0])
S = S[1:]
#print(S,stack)
print(len(stack))
|
from collections import deque
k = int(input())
num_deq = deque([i for i in range(1, 10)])
for i in range(k-1):
x = num_deq.popleft()
if x % 10 != 0:
num_deq.append(10 * x + x % 10 - 1)
num_deq.append(10 * x + x % 10)
if x % 10 != 9:
num_deq.append(10 * x + x % 10 + 1)
x = num_deq.popleft()
print(x)
| 0 | null | 104,754,459,469,282 | 293 | 181 |
n,m,k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a_c_sum = [0]*(n + 1)
for i in range(n):
a_c_sum[i + 1] = a_c_sum[i] + a[i]
b_c_sum = [0]*(m + 1)
for i in range(m):
b_c_sum[i + 1] = b_c_sum[i] + b[i]
ans, best_b = 0, m
for i in range(n + 1):
if a_c_sum[i] > k:
break
else:
while 1:
if b_c_sum[best_b] + a_c_sum[i] <= k:
ans = max(ans, i + best_b)
break
else:
best_b -= 1
if best_b == 0:
break
print(ans)
|
from bisect import bisect
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
SA = [0] * (N + 1)
SB = [0] * (M + 1)
for i in range(N):
SA[i+1] = SA[i] + A[i]
for i in range(M):
SB[i+1] = SB[i] + B[i]
result = 0
y = M
for x in range(N+1):
if SA[x] > K:
break
while y >= 0 and SA[x] + SB[y] > K:
y -= 1
result = max(result, x + y)
print(result)
| 1 | 10,772,240,423,322 | null | 117 | 117 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.