code1
stringlengths 17
427k
| code2
stringlengths 17
427k
| similar
int64 0
1
| pair_id
int64 2
181,637B
⌀ | question_pair_id
float64 3.7M
180,677B
⌀ | code1_group
int64 1
299
| code2_group
int64 1
299
|
---|---|---|---|---|---|---|
n=int(input())
s=input()
ans=""
for i in s:
ans+=chr(ord("A")+(ord(i)-ord("A")+n)%26)
print(ans)
|
N = int(input())
S = input()
T = ""
char = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
char2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i in range(len(S)):
for j in range(26):
if S[i] == char[j]:
T += char2[j + N]
break
print(T)
| 1 | 133,930,954,113,014 | null | 271 | 271 |
honmyou=str(input())
print(honmyou[0:3])
|
def depth_search(u, t):
t += 1
s.append(u)
d[u] = t
for v in range(N):
if adjacency_list[u][v] == 0:
continue
if d[v] == 0 and v not in s:
t = depth_search(v, t)
t += 1
s.pop()
f[u] = t
return t
N = int(input())
adjacency_list = [[0 for j in range(N)] for i in range(N)]
for _ in range(N):
u, k, *v = input().split()
for i in v:
adjacency_list[int(u)-1][int(i)-1] = 1
s = []
d = [0 for _ in range(N)]
f = [0 for _ in range(N)]
t = 0
for u in range(N):
if d[u] == 0:
t = depth_search(u, t)
for i in range(N):
print("{} {} {}".format(i+1, d[i], f[i]))
| 0 | null | 7,451,509,286,652 | 130 | 8 |
MOD = int(1e9+7)
N = int(input())
# all posible list
Na = pow(10, N, MOD)
# number of list without 0
N0 = pow(9, N, MOD)
# number of list without both 0 and 9
N09 = pow(8, N, MOD)
M = Na - 2*N0 + N09
print(M%MOD)
|
import sys
input = sys.stdin.readline
ins = lambda: input().rstrip()
ini = lambda: int(input().rstrip())
inm = lambda: map(int, input().split())
inl = lambda: list(map(int, input().split()))
out = lambda x: print('\n'.join(map(str, x)))
x, y, z = inm()
print(z, x, y)
| 0 | null | 20,665,973,232,982 | 78 | 178 |
from math import pi
r = float(input())
print("{0:.6f} {1:.6f}".format(float(r*r*pi), float(2*r*pi)))
|
# -*- coding: utf-8 -*-
import sys
import os
import math
PI = math.pi
r = float(input())
s = r * r * PI
l = 2 * PI * r
print(s, l)
| 1 | 640,063,957,680 | null | 46 | 46 |
n,m = map(int, input().split())
d = list(map(int, input().split()))
inf = float('inf')
dp = [inf for i in range(n+1)]
dp[0] = 0
for i in range(m):
for j in range(d[i], n+1):
dp[j] = min(dp[j], dp[j-d[i]]+1)
print(dp[-1])
|
n, m = map(int, input().split())
c = list(map(int, input().split()))
dp = [0] * (n+1)
for i in range(1, n+1):
dpi = 10 ** 9
for j in range(m):
if(i-c[j]>=0):
tmp = dp[i-c[j]] + 1
if(tmp < dpi):
dpi = tmp
dp[i] = dpi
print(dp[n])
| 1 | 140,021,005,514 | null | 28 | 28 |
n = input()
num = map(int, raw_input().split())
for i in range(n):
if i == n-1:
print num[n-i-1]
break
print num[n-i-1],
|
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np
from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse import csr_matrix
INF = 10**12
N, M, L = map(int, readline().split())
ABCQST = np.array(read().split(), dtype=np.int64)
ABC = ABCQST[:3*M]
A = ABC[::3]; B = ABC[1::3]; C = ABC[2::3]
Q = ABCQST[3*M]
ST = ABCQST[3*M + 1:]
S = ST[::2]; T = ST[1::2]
graph = csr_matrix((C, (A, B)), (N + 1, N + 1))
d = floyd_warshall(graph, directed=False)
d[d <= L] = 1
d[d > L] = INF
d = floyd_warshall(d, directed=False).astype(int)
d[d == INF] = 0
d -= 1
print('\n'.join(d.astype(str)[S, T]))
| 0 | null | 86,894,442,724,498 | 53 | 295 |
l = [int(i) for i in input().split()]
a = l[0]
b = l[1]
c = l[2]
yaku = 0
for i in range(b-a+1):
if c % (i+a) == 0:
yaku = yaku + 1
print(yaku)
|
N, K = map(int, input().split())
A = [-1] * (N+1)
for i in range(K):
d_list = int(input())
B = input().split()
B = [int(x) for x in B]
for i in range(1, N+1):
if i in B:
A[i] = 1
count = 0
for i in range(1, N+1):
if A[i] == -1:
count += 1
print(count)
| 0 | null | 12,685,691,085,060 | 44 | 154 |
S, T = input().split(' ')
print(T + S)
|
n, x, m = map(int, input().split())
def solve(n, x, m):
if n == 1:
return x
arr = [x]
for i in range(1, n):
x = x*x % m
if x in arr:
rem = n-i
break
else:
arr.append(x)
else:
rem = 0
sa = sum(arr)
argi = arr.index(x)
roop = arr[argi:]
nn, r = divmod(rem, len(roop))
return sa + nn*sum(roop) + sum(roop[:r])
print(solve(n, x, m))
| 0 | null | 53,159,045,002,492 | 248 | 75 |
S = int(input())
dp = [0 for _ in range(S+1)]
dp[0] = 1
MOD = 10**9+7
for i in range(3,S+1):
dp[i] = dp[i-1]+dp[i-3]
print(dp[S]%MOD)
|
S = int(input())
if S < 3:
print(0)
import sys
sys.exit()
DP = [0]*(S+1)
DP[0] = 0
DP[1] = 0
DP[2] = 0
for i in range(3, S + 1):
DP[i] += 1
for j in range(i - 3 + 1):
DP[i] += DP[j]
print(DP[S] % (10 ** 9 + 7))
| 1 | 3,234,125,256,150 | null | 79 | 79 |
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
INF = 10**10
def main(N, K, P, C):
ans = max(C)
for i in range(N):
used = [False] * N
now = 0
mx = -INF
cnt = 0
while True:
if used[i] or cnt >= K:
break
used[i] = True
i = P[i] - 1
now += C[i]
mx = max(mx, now)
cnt += 1
tmp = max(mx, now)
if now > 0:
cycle, mod = divmod(K, cnt)
if cycle > 1:
tmp = max(tmp, now * (cycle - 1) + mx)
t = now * cycle
tmp = max(tmp, t)
for j in range(mod):
i = P[i] - 1
t += C[i]
tmp = max(tmp, t)
ans = max(ans, tmp)
print(ans)
if __name__ == '__main__':
input = sys.stdin.readline
N, K = map(int, input().split())
*P, = map(int, input().split())
*C, = map(int, input().split())
main(N, K, P, C)
|
a = []
for i in range(10000):
a.append(int(input()))
if a[i] == 0:
break
print("Case {}: {}".format(i+1,a[i]))
| 0 | null | 2,926,497,246,090 | 93 | 42 |
import sys
import resource
sys.setrecursionlimit(10000)
n,k=map(int,input().rstrip().split())
p=list(map(int,input().rstrip().split()))
c=list(map(int,input().rstrip().split()))
def find(start,now,up,max,sum,count,flag):
if start==now:
flag+=1
if start==now and flag==2:
return [max,sum,count]
elif count==up:
return [max,sum,count]
else:
count+=1
sum+=c[p[now-1]-1]
if max<sum:
max=sum
return find(start,p[now-1],up,max,sum,count,flag)
kara=[-10000000000]
for i in range(1,n+1):
m=find(i,i,n,c[p[i-1]-1],0,0,0)
if m[2]>=k:
m=find(i,i,k,c[p[i-1]-1],0,0,0)
if m[0]>kara[0]:
kara[0]=m[0]
result=kara[0]
elif m[1]<=0:
if m[0]>kara[0]:
kara[0]=m[0]
result=kara[0]
else:
w=k%m[2]
if w==0:
w=m[2]
spe=find(i,i,w,c[p[i-1]-1],0,0,0)[0]
if m[1]+spe>m[0] and m[1]*(k-w)//m[2]+spe>kara[0]:
kara[0]=m[1]*(k-w)//m[2]+spe
elif m[1]+spe<=m[0] and m[1]*((k-w)//m[2]-1)+m[0]>kara[0]:
kara[0]=m[1]*((k-w)//m[2]-1)+m[0]
result=kara[0]
print(result)
|
(n,k),p,c=[[*map(int,t.split())]for t in open(0)]
a=-9e9
for i in range(n):
s=f=m=0;x,*l=i,
while~f:x=p[x]-1;s+=c[x];l+=s,;m+=1;f-=x==i
for j in range(min(m,k)):a=max(a,l[j]+(~j+k)//m*s*(s>0))
print(a)
| 1 | 5,445,203,108,760 | null | 93 | 93 |
from math import sqrt
def sd(nums):
n = len(nums)
ave = sum(nums)/n
return abs(sqrt(sum([(s - ave)**2 for s in nums])/n))
def main():
while True:
n = input()
if n == '0':
break
nums = [int(x) for x in input().split()]
print("{:f}".format(sd(nums)))
if __name__ == '__main__':
main()
|
import statistics
while True:
n = int(input())
if n == 0:
break
s = map(int, input().split())
print('{:.5f}'.format(statistics.pstdev(s)))
| 1 | 197,799,298,980 | null | 31 | 31 |
def selectionSort():
l = int(input())
A = list(map(int, input().split(" ")))
count = 0
for i in range(l-1):
mini = i
for j in range(i, l-1):
if (A[mini]>A[j+1]):
mini = j+1
if (i != mini):
temp = A[mini]
A[mini] = A[i]
A[i] = temp
count += 1
print(" ".join(map(str, A)))
print(count)
selectionSort()
|
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
def main():
N,*ab = map(int, read().split())
A, B = [], []
for a, b in zip(*[iter(ab)]*2):
A.append(a)
B.append(b)
A.sort()
B.sort()
if N % 2 == 0:
a1, a2 = A[N // 2 - 1], A[N // 2]
b1, b2 = B[N // 2 - 1], B[N // 2]
ans = b1 + b2 - (a1 + a2) + 1
else:
a = A[N // 2]
b = B[N // 2]
ans = b - a + 1
print(ans)
if __name__ == "__main__":
main()
| 0 | null | 8,655,007,394,748 | 15 | 137 |
from collections import Counter
N = int(input())
S = input()
ctr = Counter(S)
res = 0
for i in range(N - 1):
for j in range(i + 1, N):
k = 2 * j - i
if k < N and (S[i] != S[j] and S[i] != S[k] and S[j] != S[k]):
res += 1
print(ctr["R"] * ctr["G"] * ctr["B"] - res)
|
"""
import numpy as np
from collections import Counter
n = map(int,input().split())
a = list(map(int,input().split()))
Q = int(input())
a_count = dict(Counter(a))
def change_dict_key(d, old_key, new_key, default_value=None):
d[new_key] = d.pop(old_key, default_value)
#change_dict_key(a_count, 1, 5)
#print(a_count)
for q in range(Q):
num = list(map(int,input().split()))
if(num[0] not in a_count):
print(np.dot( np.array(list(a_count.keys())) , np.array(list(a_count.values()) ) ) )
continue
if(num[1] not in a_count):
change_dict_key(a_count,num[0],num[1])
else :
a_count[num[1]] += a_count[num[0]]
del a_count[num[0]]
#print(a_count.values())
print(np.dot( np.array(list(a_count.keys())) , np.array(list(a_count.values()) ) ) )
#print((sum(list_a)) )
"""
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B, C = [0]*Q, [0]*Q
for i in range(Q):
B[i], C[i] = list(map(int, input().split()))
bucket = [0]*100001
for i in A:
bucket[i] += 1
sum = sum(A)
for i in range(Q):
sum += (C[i] - B[i]) * bucket[B[i]]
bucket[C[i]] += bucket[B[i]]
bucket[B[i]] = 0
print(sum)
| 0 | null | 24,044,429,651,648 | 175 | 122 |
N, K = map(int, input().split())
start = sum(range(K))
end = sum(range(N-K+1, N+1))
count = 0
#print(start, end)
for k in range(K, N + 2):
count += end - start + 1
count %= 1000000007
start += k
end += (N-k)
print(count)
|
n,k=map(int,input().split())
mod=10**9+7
c=0
for i in range(k,n+2):
min=(1/2)*i*(i-1)
max=(1/2)*i*(2*n+1-i)
c+=max-min+1
print(int(c%mod))
| 1 | 33,009,003,477,658 | null | 170 | 170 |
n=int(input())
ans=10**12
d=2
while d<=n**(1/2):
if n%d==0:
ans=min(ans,d+n//d-2)
d+=1
if ans==10**12:
print(n-1)
exit()
print(ans)
|
import math
n= int(input())
x=n+1
for i in range(2,int(math.sqrt(n)+1)):
if n%i==0:
x= i+ (n/i)
print(int(x)-2)
| 1 | 161,316,700,402,802 | null | 288 | 288 |
a = int(input())
res = 0
if a%2 ==0:
res = int(a/2)
else:
a +=1
res = int(a/2)
print(res)
|
K = int(input())
for i in range(0, K):
print("ACL", end="")
| 0 | null | 30,546,751,870,612 | 206 | 69 |
n, m = map(int, input().split())
a = list(map(int, input().split()))
for time in a:
n -= time
if n<0:
n = -1
break
print(n)
|
MOD = 1000000007#い つ も の
n,k = map(int,input().split())
nCk = 1#nC0
ans = 1
if k > n-1:
k = n-1
for i in range(1,k+1,1):
nCk = (((nCk*(n-i+1))%MOD)*pow(i,1000000005,1000000007))%MOD
ans = (ans%MOD + ((((nCk*nCk)%MOD)*(n-i)%MOD)*pow(n,1000000005,1000000007)%MOD)%MOD)%MOD
print(ans)
| 0 | null | 49,724,665,045,088 | 168 | 215 |
n = list(map(int, input()[::-1])) + [0]
s = 0
res = 0
for i, ni in enumerate(n[:-1]):
k = ni + s
if k < 5 or (k == 5 and int(n[i + 1]) < 5):
res += k
s = 0
else:
res += 10 - k
s = 1
res += s
print(res)
|
N = int(input())
arr = list(map(int, input().split()))
s = 0
for i in range(N):
s ^= arr[i]
for i in range(N):
arr[i] ^= s
print(' '.join(map(str, arr)))
| 0 | null | 41,848,623,572,640 | 219 | 123 |
l = int(input())
x = l/3
print(x**3)
|
L = int(input())
k = L/3
print(k**3)
| 1 | 46,861,022,701,540 | null | 191 | 191 |
N = int(input())
P = list(map(int, input().split()))
mini = N + 1
ans = 0
for i in range(N):
if P[i] < mini:
ans += 1
mini = P[i]
print(ans)
|
a, b, c, k = map(int, input().split())
ans = 0
if a >= k:
print(k)
exit()
ans += a
k -= a
if b >= k:
print(ans)
exit()
k -= b
ans -= k
print(ans)
| 0 | null | 53,448,736,191,310 | 233 | 148 |
cnt = 0
n = int(input())
l = list(map(int, input().split()))
if len(l) >= 3:
for i in range(len(l)):
for j in range(i, len(l)):
for k in range(j, len(l)):
if l[i] == l[j] or l[i] == l[k] or l[j] == l[k]:
continue
elif l[i] + l[j] > l[k] and l[i] + l[k] > l[j] and l[j] + l[k] > l[i]:
cnt += 1
else:
continue
print(cnt)
|
from itertools import combinations as comb
N = int(input())
L = list(map(int,input().split()))
count = 0
for a, b, c in comb(L, 3):
if a != b and b != c and c != a and a + b > c and b + c > a and c + a > b:
count += 1
print(count)
| 1 | 5,091,128,530,336 | null | 91 | 91 |
import math
N,K=map(int,input().split())
sum=0
for s in range(K,N+1):
sum+=(s*(2*N-s+1))/2-(s*(s-1))/2+1
print(int(sum+1)%1000000007)
|
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
n,k = map(int, input().split())
mod = 10**9+7
ans = 0
for i in range(k, n + 2):
mi = (i - 1) * i // 2
ma = (n + n - i + 1) * i // 2
ans += ma - mi + 1
print(ans % mod)
| 1 | 33,322,994,780,498 | null | 170 | 170 |
n,m,x = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
ans = 10**9
for i in range(2 ** n):
skill = [0] * m
overTen = False
money = 0
for j in range(n):
if ((int(bin(i)[2:]) >> j) & 1):
money += a[j][0]
for h in range(m):
skill[h] += a[j][h+1]
for k in skill:
if k >= x:
overTen = True
else:
overTen = False
break
if overTen == True:
if money < ans:
ans = money
if ans == 10**9:
print(-1)
else:
print(ans)
|
#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, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
# d = m - k[i] - k[j]
# if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも!!
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
return list(map(int,input().split()))
def I():
return int(input())
n,m,x = readInts()
C = [readInts() for _ in range(n)]
INF = float('inf')
ans = INF
for bit in range(1 << n):
all = [0] * m
money = 0
for i in range(n):
if bit & (1 << i):
money += C[i][0]
for j in range(m):
all[j] += C[i][j+1]
flag = True
for k in range(m):
if all[k] >= x:
pass
else:
flag = False
break
if flag:
ans = min(ans, money)
print(ans if ans != INF else '-1')
| 1 | 22,322,921,655,448 | null | 149 | 149 |
def main():
n = int(input())
s = input()
if n % 2 == 0:
sl = len(s) // 2
s1 = s[:sl]
s2 = s[sl:]
if s1 == s2:
print('Yes')
else:
print('No')
else:
print('No')
if __name__ == '__main__':
main()
|
from sys import exit
def main():
N = int(input())
S = input()
middle = int(N / 2)
if middle == 0 or N % 2 != 0:
print('No')
exit()
for i in range(middle):
if S[i] != S[i + middle]:
print('No')
exit()
print('Yes')
main()
| 1 | 147,452,179,460,456 | null | 279 | 279 |
import os
import sys
import numpy as np
def solve(inp):
def bitree_sum(bit, t, i):
s = 0
while i > 0:
s += bit[t, i]
i ^= i & -i
return s
def bitree_add(bit, n, t, i, x):
while i <= n:
bit[t, i] += x
i += i & -i
def bitree_lower_bound(bit, n, d, t, x):
sum_ = 0
pos = 0
for i in range(d, -1, -1):
k = pos + (1 << i)
if k <= n and sum_ + bit[t, k] < x:
sum_ += bit[t, k]
pos += 1 << i
return pos + 1
def initial_score(d, ccc, sss):
bit_n = d + 3
bit = np.zeros((26, bit_n), dtype=np.int64)
INF = 10 ** 18
for t in range(26):
bitree_add(bit, bit_n, t, bit_n - 1, INF)
ttt = np.zeros(d, dtype=np.int64)
last = np.full(26, -1, dtype=np.int64)
score = 0
for i in range(d):
best_t = 0
best_diff = -INF
costs = ccc * (i - last)
costs_sum = costs.sum()
for t in range(26):
tmp_diff = sss[i, t] - costs_sum + costs[t]
if best_diff < tmp_diff:
best_t = t
best_diff = tmp_diff
ttt[i] = best_t
last[best_t] = i
score += best_diff
bitree_add(bit, bit_n, best_t, i + 2, 1)
return bit, score, ttt
def calculate_score(d, ccc, sss, ttt):
last = np.full(26, -1, dtype=np.int64)
score = 0
for i in range(d):
t = ttt[i]
last[t] = i
score += sss[i, t] - (ccc * (i - last)).sum()
return score
def pinpoint_change(bit, bit_n, bit_d, d, ccc, sss, ttt, permissible):
cd = np.random.randint(0, d)
ct = np.random.randint(0, 26)
while ttt[cd] == ct:
ct = np.random.randint(0, 26)
diff = 0
t = ttt[cd]
k = bitree_sum(bit, t, cd + 2)
c = bitree_lower_bound(bit, bit_n, bit_d, t, k - 1) - 2
e = bitree_lower_bound(bit, bit_n, bit_d, t, k + 1) - 2
b = ccc[t]
diff -= b * (cd - c) * (e - cd)
diff -= sss[cd, t]
k = bitree_sum(bit, ct, cd + 2)
c = bitree_lower_bound(bit, bit_n, bit_d, ct, k) - 2
e = bitree_lower_bound(bit, bit_n, bit_d, ct, k + 1) - 2
b = ccc[ct]
diff += b * (cd - c) * (e - cd)
diff += sss[cd, ct]
if diff > permissible:
bitree_add(bit, bit_n, t, cd + 2, -1)
bitree_add(bit, bit_n, ct, cd + 2, 1)
ttt[cd] = ct
else:
diff = 0
return diff
def swap_change(bit, bit_n, bit_d, d, ccc, sss, ttt, permissible):
dd = np.random.randint(1, 14)
cd1 = np.random.randint(0, d - dd)
cd2 = cd1 + dd
ct1 = ttt[cd1]
ct2 = ttt[cd2]
if ct1 == ct2:
return 0
diff = 0
k = bitree_sum(bit, ct1, cd1 + 2)
c = bitree_lower_bound(bit, bit_n, bit_d, ct1, k - 1) - 2
e = bitree_lower_bound(bit, bit_n, bit_d, ct1, k + 1) - 2
diff += ccc[ct1] * (e + c - cd1 - cd2)
k = bitree_sum(bit, ct2, cd2 + 2)
c = bitree_lower_bound(bit, bit_n, bit_d, ct2, k - 1) - 2
e = bitree_lower_bound(bit, bit_n, bit_d, ct2, k + 1) - 2
diff -= ccc[ct2] * (e + c - cd1 - cd2)
diff -= sss[cd1, ct1] + sss[cd2, ct2]
diff += sss[cd1, ct2] + sss[cd2, ct1]
if diff > permissible:
bitree_add(bit, bit_n, ct1, cd1 + 2, -1)
bitree_add(bit, bit_n, ct1, cd2 + 2, 1)
bitree_add(bit, bit_n, ct2, cd1 + 2, 1)
bitree_add(bit, bit_n, ct2, cd2 + 2, -1)
ttt[cd1] = ct2
ttt[cd2] = ct1
else:
diff = 0
return diff
d = inp[0]
ccc = inp[1:27]
sss = np.zeros((d, 26), dtype=np.int64)
for r in range(d):
sss[r] = inp[27 + r * 26:27 + (r + 1) * 26]
bit, score, ttt = initial_score(d, ccc, sss)
bit_n = d + 3
bit_d = int(np.log2(bit_n))
loop = 5 * 10 ** 6
permissible_min = -2000.0
method_border = 0.8
best_score = score
best_ttt = ttt.copy()
for lp in range(loop):
permissible = (1 - lp / loop) * permissible_min
if np.random.random() < method_border:
diff = pinpoint_change(bit, bit_n, bit_d, d, ccc, sss, ttt, permissible)
else:
diff = swap_change(bit, bit_n, bit_d, d, ccc, sss, ttt, permissible)
score += diff
# print(lp, score, calculate_score(d, ccc, sss, ttt))
if score > best_score:
best_score = score
best_ttt = ttt.copy()
return best_ttt + 1
if sys.argv[-1] == 'ONLINE_JUDGE':
from numba.pycc import CC
cc = CC('my_module')
cc.export('solve', '(i8[:],)')(solve)
cc.compile()
exit()
if os.name == 'posix':
# noinspection PyUnresolvedReferences
from my_module import solve
else:
from numba import njit
solve = njit('(i8[:],)', cache=True)(solve)
print('compiled', file=sys.stderr)
inp = np.fromstring(sys.stdin.read(), dtype=np.int64, sep=' ')
ans = solve(inp)
print('\n'.join(map(str, ans)))
|
from sys import stdin
input = stdin.readline
def Judge(S, T):
ls = len(S)
lt = len(T)
m = 0
for i in range(ls-lt+1):
tmp = 0
for j in range(lt):
if S[i+j] == T[j]:
tmp += 1
if m < tmp:
m = tmp
return lt - m
S = input().strip()
T = input().strip()
print(Judge(S, T))
| 0 | null | 6,710,997,385,470 | 113 | 82 |
n=int(input())
a=list(map(int,input().split()))
p=[]
m=[]
for i in range(n):
p.append(i+a[i])
m.append(i-a[i])
x=[0]*(2*n)
y=[0]*(2*n)
ans=0
for i in range(n):
if 0<=p[i]<=(2*n)-1:
x[p[i]]+=1
if 0<=m[i]<=(2*n)-1:
y[m[i]]+=1
for i in range(n):
ans+=x[i]*y[i]
print(ans)
|
n=int(input())
mod=10**9+7
ans=10**n
ans-=2*(9**n)
ans+=8**n
ans%=mod
print(ans)
| 0 | null | 14,549,974,541,060 | 157 | 78 |
h,w,m=map(int,input().split())
HW=[list(map(int,input().split())) for _ in range(m)]
H,W=[0 for _ in range(h)],[0 for _ in range(w)]
for i,j in HW:
i,j=i-1,j-1
H[i] +=1
W[j] +=1
max_h,max_w=max(H),max(W)
ans=max_h+max_w
cnt=H.count(max_h)*W.count(max_w)
for i,j in HW:
if H[i-1]==max_h and W[j-1]==max_w:
cnt -=1
print(ans if cnt!=0 else ans-1)
|
h,w,m = map(int,input().split())
row = [0]*(h+1)
col = [0]*(w+1)
bombs = set([])
for i in range(m):
a,b = map(int,input().split())
row[a] += 1
col[b] += 1
bombs.add((a,b))
r,c = max(row),max(col)
rcnt,ccnt = 0,0
for v in row:
if v==r:
rcnt += 1
for v in col:
if v==c:
ccnt += 1
doubled = 0
for i,j in bombs:
if row[i]==r and col[j]==c:
doubled += 1
if doubled==rcnt*ccnt:
print(r+c-1)
else:
print(r+c)
| 1 | 4,710,889,588,380 | null | 89 | 89 |
a,b=sorted(list(map(int, input().split())))
m=b%a
while m>=1:
b=a
a=m
m=b%a
print(a)
|
r = int(input())
pi= 3
ans = r*r*pi / pi
print(int(ans))
| 0 | null | 72,803,166,234,950 | 11 | 278 |
#!/usr/bin/env python3
import sys
def solve(H: int, W: int, s: "List[str]"):
dp = [[0 for w in range(W)] for h in range(H)] # dp[h][w]はh,wに達するための回数
if s[0][0] == '#':
dp[0][0] = 1
for w in range(W-1): # 0行目について
if s[0][w+1] == '.': # 移動先が白だったら特に変わりなし
dp[0][w+1] = dp[0][w]
elif s[0][w] == '.' and s[0][w+1] == '#': # 移動元が白で先が黒ならば、新しく施行1回追加
dp[0][w+1] = dp[0][w] + 1
elif s[0][w] == '#' and s[0][w+1] == '#': # 移動元も先も黒だったとしたら、試行回数は変わらない
dp[0][w+1] = dp[0][w]
for h in range(H-1): # 1列目について
if s[h+1][0] == '.':
dp[h+1][0] = dp[h][0]
elif s[h][0] == '.' and s[h+1][0] == '#':
dp[h+1][0] = dp[h][0] + 1
elif s[h][0] == '#' and s[h+1][0] == '#':
dp[h+1][0] = dp[h][0]
for h in range(1, H):
for w in range(W-1):
if s[h][w+1] == '.':
dp[h][w+1] = min(dp[h][w], dp[h-1][w+1])
elif s[h][w] == '.' and s[h][w+1] == '#':
if s[h-1][w+1] == '.':
dp[h][w+1] = min(dp[h][w]+1, dp[h-1][w+1]+1)
elif s[h-1][w+1] == '#':
dp[h][w+1] = min(dp[h][w]+1, dp[h-1][w+1])
elif s[h][w] == '#' and s[h][w+1] == '#':
if s[h-1][w+1] == '.':
dp[h][w+1] = min(dp[h][w], dp[h-1][w+1]+1)
elif s[h-1][w+1] == '#':
dp[h][w+1] = min(dp[h][w], dp[h-1][w+1])
print(dp[H-1][W-1])
return
# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
H = int(next(tokens)) # type: int
W = int(next(tokens)) # type: int
s = [next(tokens) for _ in range(H)] # type: "List[str]"
solve(H, W, s)
if __name__ == '__main__':
main()
|
import sys
input = sys.stdin.readline
a, b = map(int, input().split())
print(abs(a * b) // __import__('math').gcd(a, b))
| 0 | null | 81,276,581,373,508 | 194 | 256 |
s,t = input().split()
a,b = map(int, input().split())
u = input()
if(u == s):
a = a-1
else:
b = b-1
print(str(a) + " " + str(b))
|
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10 ** 9)
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
INF=float('inf')
N,M=MAP()
C=LIST()
dp=[INF]*(N+1)
dp[0]=0
for i in range(N):
for j in range(M):
if i+C[j]<=N:
dp[i+C[j]]=min(dp[i+C[j]], dp[i]+1)
print(dp[N])
| 0 | null | 36,168,484,671,826 | 220 | 28 |
X = int(input())
ans="No"
if X>=30:
ans='Yes'
print(ans)
|
n = int(input())
a = 0
b = 0
d1 = 0
d2 = 0
for i in range(n):
d1,d2 = map(int,input().split())
if(a==0 and d1 ==d2):
a+=1
elif(a==1 and d1 ==d2):
a+=1
elif(a==2 and d1 ==d2):
b +=1
break
else:
a =0
if(b>=1):
print("Yes")
else:
print("No")
| 0 | null | 4,064,465,260,870 | 95 | 72 |
import sys
sys.setrecursionlimit(10**9)
f=lambda:map(int,sys.stdin.readline().split())
n,st,sa=f()
g=[set() for _ in range(n)]
for _ in range(n-1):
a,b=f()
g[a-1].add(b-1)
g[b-1].add(a-1)
def dfs(l,v,p=-1,d=0):
l[v]=d
for c in g[v]:
if c!=p: dfs(l,c,v,d+1)
lt=[0]*n
dfs(lt,st-1)
la=[0]*n
dfs(la,sa-1)
print(max(la[i] for i in range(n) if lt[i]<la[i])-1)
|
from collections import defaultdict, deque
N, u, v = map(int, input().split())
G = defaultdict(lambda: [])
for _ in range(N-1):
a,b=map(int,input().split())
G[a].append(b)
G[b].append(a)
if G[u] == [v]:
print(0)
exit()
def bfs(v):
seen = [0]*(N+1)
queue = deque()
dist = [-1]*(N+1)
dist[v] = 0
seen[v] = 1
queue.append(v)
while queue:
q = queue.popleft()
for nx in G[q]:
if seen[nx]:
continue
dist[nx] = dist[q] + 1
seen[nx] = 1
queue.append(nx)
return dist
d_u = bfs(u)
d_v = bfs(v)
ans = 0
for node in range(1, N+1):
if d_u[node] < d_v[node] and len(G[node]) == 1:
ans = max(ans, d_v[node]-1)
print(ans)
| 1 | 117,827,666,431,270 | null | 259 | 259 |
import heapq
INFTY = 10**4
N,X,Y = map(int,input().split())
G = {i:[] for i in range(1,N+1)}
for i in range(1,N):
G[i].append(i+1)
G[i+1].append(i)
G[X].append(Y)
G[Y].append(X)
dist = [[INFTY for _ in range(N+1)] for _ in range(N+1)]
for i in range(N+1):
dist[i][i] = 0
for i in range(1,N+1):
hist = [0 for _ in range(N+1)]
heap = [(0,i)]
hist[i] = 1
while heap:
d,x = heapq.heappop(heap)
if d>dist[i][x]:continue
hist[x] = 1
for y in G[x]:
if hist[y]==0:
if dist[i][y]>d+1:
dist[i][y] = d+1
heapq.heappush(heap,(d+1,y))
C = {k:0 for k in range(1,N)}
for i in range(1,N):
for j in range(i+1,N+1):
C[dist[i][j]] += 1
for k in range(1,N):
print(C[k])
|
import sys
import os
import math
import bisect
import collections
import itertools
import heapq
import re
import queue
from decimal import Decimal
# import fractions
sys.setrecursionlimit(10000000)
ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]
iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]
lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)
MOD = 10 ** 9 + 7
MAX = float('inf')
def main():
if os.getenv("LOCAL"):
sys.stdin = open("input.txt", "r")
N, X, Y = il()
k = {n:0 for n in range(N-1)}
X -= 1
Y -= 1
for i in range(N-1):
for j in range(i+1, N):
m = min(abs(i-j), abs(X-i) + abs(Y-j) + 1, abs(X-j) + abs(Y-i) + 1)
k[m-1] += 1
for i in range(len(k)):
print(k[i])
if __name__ == '__main__':
main()
| 1 | 43,967,635,569,568 | null | 187 | 187 |
from collections import Counter
N = int(input())
C = Counter([input() for _ in range(N)])
# print(f'{list(C)=}')
max_value = max([value for value in C.values()])
# print(f'{max_value=}')
S = [key for key, value in zip(C.keys(), C.values()) if value == max_value]
# print(f'{S=}')
for s in sorted(S):
print(s)
|
N = int(input())
sum_num = 0
for i in range(1, N+1):
if (i%3) and (i%5):
sum_num += i
print(sum_num)
| 0 | null | 52,193,829,730,560 | 218 | 173 |
n = int(input())
ans = 0
for i in range(1, n + 1):
if i % 3 != 0 and i % 5 != 0:
ans += i
else:
continue
print(ans)
|
n = int(input())
ans = 0
for i in range(1,n+1):
if (i % 3 == 0) or (i % 5 == 0):
pass
else:
ans += i
print(ans)
| 1 | 34,897,434,484,778 | null | 173 | 173 |
X = int(input())
a = 100
year = 0
import math
while True:
a = a*101//100
year += 1
if a >= X:
break
print(year)
|
T = str(input())
print(T.replace('?', 'D'))
| 0 | null | 22,770,424,165,200 | 159 | 140 |
N, K = map(int, input().split())
X = list(map(int, input().split()))
ub = 10 ** 9 + 7
lb = 0
while ub - lb > 1:
mid = (ub + lb) // 2
cnt = 0
for v in X:
cnt += -(-v // mid) - 1
if cnt <= K:
ub = mid
else:
lb = mid
print(ub)
|
N,K = map(int,input().split())
A = list(map(int,input().split()))
def f(n):
now = 0
for i in range(N):
now += (A[i]-1)//x
if now <= K:
return True
else:
return False
l = 0; r = 10**10
while r-l > 1:
x = (l+r)//2
if f(x):
r = x
else:
l = x
print(r)
| 1 | 6,520,118,460,390 | null | 99 | 99 |
def insertionSort(A, n, g):
local_cnt = 0
for i in range(g, n):
# print(i, local_cnt)
v = A[i]
j = i - g
while j >= 0 and A[j] > v:
# print(i, j, local_cnt)
A[j + g] = A[j]
j -= g
local_cnt += 1
A[j + g] = v
return A, local_cnt
def shellSort(A, n):
cnt = 0
m = 1
G = []
h = 1
while (h < n // 3):
h = h * 3 + 1
m += 1
# print(f'm: {m}')
while True:
G.append(h)
if h == 1:
break
h //= 3
# print(f'G: {G}')
for i in range(m):
ans = insertionSort(A, n, G[i])
# print(ans)
A = ans[0]
cnt += ans[1]
return m, G, A, cnt
n = int(input())
A = [int(input()) for _ in range(n)]
m, G, A, cnt = shellSort(A, n)
print(m)
print(' '.join(str(g) for g in G))
print(cnt)
for a in A:
print(a)
|
import math
def insert_sort(A, n, g):
cnt = 0
for i in range(g, n):
j = i - g
v = A[i]
while j >= 0 and A[j] > v:
A[j+g] = A[j]
j -= g
cnt += 1
A[j+g] = v
return cnt
def shell_sort(A, n):
cnt = 0
G = []
h = 1
while h <= n:
G.append(h)
h = h*3+1
G.reverse()
print(len(G))
print(*G)
for g in G:
cnt += insert_sort(A, n, g)
return cnt
N = int(input())
A = [int(input()) for _ in range(N)]
cnt = shell_sort(A, N)
print(cnt)
for a in A:
print(a)
| 1 | 31,297,148,178 | null | 17 | 17 |
N = int(input())
S = input()
count = 0
RGBlist = [[S.count('R')],[S.count('G')],[S.count('B')]]
for i in range(1,N):
RGBlist[0].append(RGBlist[0][-1] - (S[i-1] == 'R'))
RGBlist[1].append(RGBlist[1][-1] - (S[i-1] == 'G'))
RGBlist[2].append(RGBlist[2][-1] - (S[i-1] == 'B'))
for i in range(N-2):
si = S[i]
for j in range(i+1,N-1):
sj = S[j]
if si==sj:
continue
else:
RGB = set(['R','G','B'])
RGB -= set([si,sj])
k=j+1
kji=j-i
if RGB == {'R'}:
rgb = 0
elif RGB =={'G'}:
rgb = 1
else:
rgb = 2
if k+kji-1 < len(S):
count += RGBlist[rgb][k] - ({S[k+kji-1]} == RGB)
else:
count += RGBlist[rgb][k]
print(count)
|
d,t,s=map(int, input().split());print(('Yes','No')[d/s>t])
| 0 | null | 19,822,095,416,368 | 175 | 81 |
#C - Count Order
#DFS
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
a = 0
b = 0
cnt = 0
def dfs(A):
global cnt
global a
global b
if len(A) == N:
cnt += 1
if A == P:
a = cnt
if A == Q:
b = cnt
return
for v in range(1,N+1):
if v in A:
continue
A.append(v)
dfs(A)
A.pop()
dfs([])
print(abs(a-b))
|
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
fact = [1]
for i in range(0,N):
fact.append(fact[i] * (i+2))
p = 0
q = 0
def number(a):
res = 0
global fact
l = len(a)
for i in range(l-2):
res += (a[i] - 1) * fact[l-2-i]
for j in range(i+1, l-2):
if a[j] > a[i]:
a[j] -= 1
if a[l-1] < a[l-2]:
res += 2
else:
res += 1
return res
p, q = number(P), number(Q)
print(abs(q-p))
| 1 | 100,842,810,293,148 | null | 246 | 246 |
first=0
second=0
third=0
for i in range(10):
mountain=int(input())
if mountain>first:
third=second
second=first
first=mountain
elif mountain>second:
third=second
second=mountain
elif mountain>third:
third=mountain
print(first)
print(second)
print(third)
|
mount_list = []
for i in range(10):
mount_list.append(int(input()))
mount_list.sort(reverse = True)
for i in range(3):
print(mount_list[i])
| 1 | 19,183,880 | null | 2 | 2 |
#coding: UTF-8
import statistics as st
while True:
N = int(input())
if N == 0:
break
buf = list(map(float, input().split()))
sd = st.pstdev(buf)
print("%.8f"%sd)
|
import statistics
while input() != "0":
print("{0:.6f}".format(statistics.pstdev(map(float, input().split()))))
| 1 | 194,444,711,648 | null | 31 | 31 |
#!/usr/bin/env python
# encoding: utf-8
class Solution:
@staticmethod
def selection_sort():
# write your code here
array_length = int(input())
unsorted_array = [int(x) for x in input().split()]
count = 0
for i in range(array_length):
min_j = i
for j in range(i, array_length):
if unsorted_array[j] < unsorted_array[min_j]:
min_j = j
unsorted_array[i], unsorted_array[min_j] = unsorted_array[min_j], unsorted_array[i]
if i != min_j:
count += 1
print(" ".join(map(str, unsorted_array)))
print(str(count))
if __name__ == '__main__':
solution = Solution()
solution.selection_sort()
|
N,K = map(int,input().split())
mod = 10**9+7
ans = [0]*(K+1)
result = 0
for i in range(K,0,-1):
c = K//i
ans[i] += pow(c,N,mod)
for j in range(i,K+1,i):
if i != j:
ans[i] -= ans[j]
result += i*ans[i] % mod
print(result%mod)
| 0 | null | 18,366,811,514,538 | 15 | 176 |
import numpy as np
S=input()
N=len(S)
mod=[0 for i in range(2019)]
mod2=0
ten=1
for i in range(N-1,-1,-1):
s=int(S[i])*ten
mod2+=np.mod(s,2019)
mod2=np.mod(mod2,2019)
mod[mod2]+=1
ten=(ten*10)%2019
ans=0
for i in range(2019):
k=mod[i]
if i==0:
if k>=2:
ans+=k*(k-1)//2+k
else:
ans+=k
else:
if k>=2:
ans+=k*(k-1)//2
print(ans)
|
import copy
s = list(input())
s.reverse()
n = len(s)
MOD = 2019
m = [0] * n
msum = [0] * (n+1)
cnt = [0] * (MOD)
cnt[0] = 1
t = 1
for i in range(n):
m[i] = int(s[i]) * t % MOD
msum[i+1] = (msum[i] + m[i]) % MOD
cnt[msum[i+1]] += 1
t = t * 10 % MOD
ans = 0
for i in range(MOD):
ans += cnt[i] * (cnt[i] - 1) // 2
print(ans)
| 1 | 30,822,629,235,488 | null | 166 | 166 |
from collections import defaultdict
n,k=map(int,input().split())
a=list(map(int,input().split()))
a[0]=(a[0]-1)%k
for i in range(n-1):a[i+1]=(a[i+1]+a[i]-1)%k
d=defaultdict(int)
ans=0
d[0]=1
for i in range(n):
if i==k-1:d[0]-=1
if i>=k:d[a[i-k]]-=1
ans+=d[a[i]]
d[a[i]]+=1
print(ans)
|
from itertools import accumulate
from collections import defaultdict
n, k = map(int, input().split())
a = list(map(int, input().split()))
acc = [0] + list(accumulate(a))
sm = [(e - i) % k for i, e in enumerate(acc)]
d = defaultdict(int)
ans = 0
for r in range(1, n + 1):
if r - k >= 0:
e = sm[r - k]
d[e] -= 1
e = sm[r - 1]
d[e] += 1
e = sm[r]
ans += d[e]
print(ans)
| 1 | 137,665,898,002,360 | null | 273 | 273 |
S, T = input().split()
A, B = map(int, input().split())
U = input()
if (U == T):
print(str(A)+" "+str(B-1))
else:
print(str(A-1)+" "+str(B))
|
s,t = input().split()
a,b = map(int,input().split())
A = input()
if A == s:print(a-1,b)
else:print(a,b-1)
| 1 | 71,645,696,719,090 | null | 220 | 220 |
import sys
def input():
return sys.stdin.readline().strip()
MOD = 10 ** 9 + 7
sys.setrecursionlimit(20000000)
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())
answer = 0
if K >= N - 1:
cmb = Combination(2 * N + 1)
answer = cmb(N * 2 - 1, N - 1)
else:
cmb = Combination(N)
for i in range(K + 1):
answer += cmb(N, i) * cmb(N - 1, N - 1 - i)
print(answer % MOD)
if __name__ == "__main__":
main()
|
import sys
sys.setrecursionlimit(2147483647)
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
input = lambda:sys.stdin.readline().rstrip()
class modfact(object):
def __init__(self, n):
fact, invfact = [1] * (n + 1), [1] * (n + 1)
for i in range(1, n + 1): fact[i] = i * fact[i - 1] % MOD
invfact[n] = pow(fact[n], MOD - 2, MOD)
for i in range(n - 1, -1, -1): invfact[i] = invfact[i + 1] * (i + 1) % MOD
self._fact, self._invfact = fact, invfact
def inv(self, n):
return self._fact[n - 1] * self._invfact[n] % MOD
def fact(self, n):
return self._fact[n]
def invfact(self, n):
return self._invfact[n]
def comb(self, n, k):
if k < 0 or n < k: return 0
return self._fact[n] * self._invfact[k] % MOD * self._invfact[n - k] % MOD
def perm(self, n, k):
if k < 0 or n < k: return 0
return self._fact[n] * self._invfact[n - k] % MOD
def resolve():
n, k = map(int, input().split())
mf = modfact(n)
res = 0
for i in range(min(k + 1, n)):
res += mf.comb(n, i) * mf.comb(n - 1, i) % MOD
res %= MOD
print(res)
resolve()
| 1 | 67,025,427,223,948 | null | 215 | 215 |
#!/usr/bin/env python3
from networkx.utils import UnionFind
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
n,m=map(int, input().split())
uf = UnionFind()
for i in range(1,n+1):
_=uf[i]
for _ in range(m):
a,b=map(int, input().split())
uf.union(a, b) # aとbをマージ
print(len(list(uf.to_sets()))-1)
#for group in uf.to_sets(): # すべてのグループのリストを返す
#print(group)
if __name__ == '__main__':
main()
|
S=str(input())
if 'SUN'in S:
print(7)
elif 'MON' in S:
print(6)
elif 'TUE' in S:
print(5)
elif 'WED' in S:
print(4)
elif 'THU' in S:
print(3)
elif 'FRI' in S:
print(2)
elif 'SAT' in S:
print(1)
| 0 | null | 67,541,851,074,500 | 70 | 270 |
from math import sqrt
n = int(input())
minSoFar = 10**12+1
for a in range(1, int(sqrt(n))+1):
if n % a == 0:
b = n // a
minSoFar = min(minSoFar, a+b-2)
print(minSoFar)
|
import sys,queue,math,copy,itertools,bisect,collections,heapq
def main():
sys.setrecursionlimit(10**7)
INF = 10**18
MOD = 10**9 + 7
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
N = NI()
R = 2**(N.bit_length())
st = [0] * (R*2)
def update(i,s):
x = 2 ** (ord(s) - ord('a'))
i += R-1
st[i] = x
while i > 0:
i = (i-1) // 2
st[i] = st[i*2+1] | st[i*2+2]
def query(l,r):
l += R-1
r += R-2
ret = 0
while l+1 < r:
if l % 2 == 0:
ret |= st[l]
if r % 2 == 1:
ret |= st[r]
r -= 1
l = l // 2
r = (r-1) // 2
if l == r:
ret |= st[l]
else:
ret |= st[l] | st[r]
return ret
for i,s in enumerate(sys.stdin.readline().rstrip()):
update(i+1,s)
Q = NI()
for _ in range(Q):
c,a,b = sys.stdin.readline().split()
if c == '1':
update(int(a),b)
else:
ret = query(int(a),int(b)+1)
cnt = 0
b = 1
for i in range(26):
cnt += (b & ret) > 0
b <<= 1
print(cnt)
if __name__ == '__main__':
main()
| 0 | null | 112,589,662,649,552 | 288 | 210 |
# E - Rem of Sum is Num
import queue
N, K = map(int, input().split())
A = list(map(int, input().split()))
S = [0] * (N + 1)
for i in range(N):
S[i + 1] = (S[i] + A[i] - 1) % K
v_set = set(S)
mp = {v: 0 for v in v_set}
ans = 0
q = queue.Queue()
for i in range(N + 1):
ans += mp[S[i]]
mp[S[i]] += 1
q.put(S[i])
if q.qsize() == K:
mp[q.get()] -= 1
print(ans)
|
cnt = 0
def insertionSort(A, n, g):
global cnt
for i in range(g,n):
v = A[i]
j = i - g
while (j >= 0 and A[j] > v ):
A[j+g] = A[j]
j = j - g
cnt += 1
A[j+g] = v
def shellSort(A, n):
global cnt
cnt = 0
m = 0
b=n
while b > 0:
b = b // 4
m += 1
G = []
base = 1
for i in range(0,m):
G.insert(0,base)
base = base * 3 + 1
print(m)
strG = [str(i) for i in G]
print(" ".join(strG))
for i in range(m):
insertionSort(A, n, G[i])
N = int(input())
A = list()
for i in range(N):
A.append(int(input()))
shellSort(A,N)
print(cnt)
for a in A:
print(a)
| 0 | null | 68,422,188,502,600 | 273 | 17 |
def solve():
n = int(input())
if n % 2 == 0:
print(0.5)
else:
print(((n+1)//2) / n)
if __name__ == '__main__':
solve()
|
# coding: utf-8
# Your code here!
import bisect
N,D,A=map(int,input().split())
log=[]
loc=[]
ene=[]
for _ in range(N):
X,H=map(int,input().split())
log.append([X,H])
log.sort(key=lambda x:x[0])
for X,H in log:
loc.append(X)
ene.append(H)
syokan=[0]*N
power=0
for i in range(N):
temp=max(-((-ene[i]+power)//A),0)*A
power+=temp
rng=bisect.bisect_right(loc,loc[i]+2*D)
syokan[min(rng-1,N-1)]+=temp
power-=syokan[i]
#print(syokan)
#print(syokan)
print(sum(syokan)//A)
| 0 | null | 129,209,688,066,692 | 297 | 230 |
import math
def order(N,P):
value=0
N_ls=list(range(1,N+1))
score=0
for i in P:
score+=N_ls.index(i)*math.factorial(len(N_ls)-1)
N_ls.remove(i)
return score
N=int(input())
P=[int(x) for x in input().split()]
Q=[int(x) for x in input().split()]
print(abs(order(N,P)-order(N,Q)))
|
number = int(input())
ans = 0
i: int
for i in range(1, number + 1):
if i % 3 == 0 or i % 5 == 0:
continue
ans += i
print(ans)
| 0 | null | 67,544,402,812,238 | 246 | 173 |
#!/usr/bin python3
# -*- coding: utf-8 -*-
n, k = map(int, input().split())
p = [int(i)-1 for i in input().split()]
c = list(map(int, input().split()))
rc = [0] * n
rp = [0] * n
for i in range(n):
if rc[i]!=0:
continue
else:
nw = set([i])
np = c[i]
nx = p[i]
while not nx in nw:
nw.add(nx)
np += c[nx]
nx = p[nx]
cnt = len(nw)
for x in nw:
rc[x] = cnt
rp[x] = np
ret = - 10 ** 18
for i in range(n):
if rc[i] == 1:
if rp[i]>=0:
ret = max(ret, k*rp[i])
else:
ret = max(ret, rp[i])
continue
pseq = [0]
nx = p[i]
for j in range(1,rc[i]+1):
pseq.append(pseq[-1]+c[nx])
nx = p[nx]
pseq = pseq[1:]
if k <= rc[i]:
ret = max(ret, max(pseq[:k]))
elif rp[i] < 0:
ret = max(ret, max(pseq))
else:
tmp = max(pseq)
if k%rc[i]!=0:
tmp = max(tmp, rp[i] + max(pseq[:k%rc[i]]))
tmp += rp[i]*(k//rc[i]-1)
ret = max(ret, tmp)
print(ret)
|
import sys
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline
class UnionFind:
def __init__(self, n: int):
self.n = n
self.parents = [-1] * n
def find(self, x: int):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x: int, y: int):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[y] < self.parents[x]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x: int):
return -self.parents[self.find(x)]
def same(self, x: int, y: int):
return self.find(x) == self.find(y)
def members(self, x: int):
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def all_group_members(self):
return {r: self.members(r) for r in self.roots()}
def __str__(self):
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
def solve():
N, K = map(int, rl().split())
P = list(map(lambda n: int(n) - 1, rl().split()))
C = list(map(int, rl().split()))
uf = UnionFind(N)
for i in range(N):
uf.union(i, P[i])
roop_scores = defaultdict(int)
for i in range(N):
roop_scores[uf.find(i)] += C[i]
ans = -(10 ** 18)
K -= 1
for s in range(N):
cur = P[s]
tmp = C[cur]
ans = max(ans, tmp)
roop_size = uf.size(s)
roop_score = roop_scores[uf.find(s)]
roop_cnt = K // roop_size
if roop_cnt and 0 < roop_score:
tmp += roop_score * roop_cnt
rem = K % roop_size
ans = max(ans, tmp)
while rem:
cur = P[cur]
tmp += C[cur]
ans = max(ans, tmp)
rem -= 1
continue
cnt = min(roop_size, K)
while cnt:
cur = P[cur]
tmp += C[cur]
ans = max(ans, tmp)
cnt -= 1
print(ans)
if __name__ == '__main__':
solve()
| 1 | 5,344,052,460,450 | null | 93 | 93 |
from sys import stdin
import sys
import math
from functools import reduce
import itertools
n = int(stdin.readline().rstrip())
p = [int(x) for x in stdin.readline().rstrip().split()]
m = n
count = 0
for i in range(n):
m = min([m, p[i]])
if m == p[i]: count = count + 1
print(count)
|
n = int(input())
p = list(map(int,input().split()))
m = n
q = 2 * (10 ** 5)
for i in range(n):
q = min(p[i],q)
if not p[i] <= q:
m -= 1
print(m)
| 1 | 84,976,914,313,488 | null | 233 | 233 |
a, b, c = map(int, input().split())
k = int(input())
r = 0
while b <= a :
b = 2 * b
r += 1
while c <= b:
c = 2 * c
r += 1
if r <= k:
print("Yes")
else:
print("No")
|
N=int(input())
X=[0 for _ in range(N)]
Y=[0 for _ in range(N)]
INF=1e10
Zmax=-INF
Zmin=INF
Wmax=-INF
Wmin=INF
for n in range(N):
X[n], Y[n] = map(int, input().split())
z = X[n] + Y[n]
w = X[n] - Y[n]
Zmax = max( Zmax, z )
Zmin = min( Zmin, z )
Wmax = max( Wmax, w )
Wmin = min( Wmin, w )
ans = max( abs(Zmax - Zmin), abs(Wmax - Wmin))
print(ans)
| 0 | null | 5,116,909,643,968 | 101 | 80 |
import sys
input = sys.stdin.readline
inf = 1000
inf5 = 10**10
n, m, l = map(int, input().split())
graph = [[inf5] * (n+1) for _ in range(n+1)]
for _ in range(m):
a, b, c = map(int, input().split())
graph[a][b] = c
graph[b][a] = c
inf = 1000
def warshall_floyd(d):
for k in range(1, n+1):
for i in range(1, n+1):
for j in range(1, n+1):
d[i][j] = min(d[i][j], d[i][k] + d[k][j])
warshall_floyd(graph)
ans = [[inf] * (n+1) for _ in range(n+1)]
for i in range(1, n+1):
for j in range(1, n+1):
if graph[i][j] <= l:
ans[i][j] = 1
warshall_floyd(ans)
q = int(input())
for _ in range(q):
s, t = map(int, input().split())
if ans[s][t] == inf:
print(-1)
continue
print(ans[s][t] - 1)
|
inf = 10**10
n,m,l = map(int,input().split())
def warshall_floyd(d):
for i in range(n):
for j in range(n):
for k in range(n):
d[j][k] = min(d[j][k],d[j][i]+d[i][k])
return d
G = [[inf] * n for _ in range(n)] #重み付きグラフ
for i in range(n):
G[i][i] = 0
for _ in range(m):
a,b,c = map(int,input().split())
G[a-1][b-1] = c
G[b-1][a-1] = c
G = warshall_floyd(G)
P = [[0] * n for _ in range(n)]
for i in range(n):
for j in range(n):
if i == j:
P[i][j] = 0
elif G[i][j] <= l:
P[i][j] = 1
else:
P[i][j] = inf
p = warshall_floyd(P)
q = int(input())
for _ in range(q):
s,t = map(int,input().split())
ans = p[s-1][t-1]-1
print(ans if ans <= 10**9 else -1)
| 1 | 173,435,953,419,200 | null | 295 | 295 |
s = raw_input()
r = ''
for c in s:
d = c
if c >= 'a' and c <= 'z':
d = chr(ord(c) - ord('a') + ord('A'))
if c >= 'A' and c <= 'Z':
d = chr(ord(c) - ord('A') + ord('a'))
r = r + d
print r
|
from string import ascii_lowercase as LOW
from string import ascii_uppercase as UP
a = {l:u for l,u in zip(list(LOW),list(UP))}
b = {u:l for u,l in zip(list(UP),list(LOW))}
a.update(b)
data = input()
data = [a[s] if s in a else s for s in data]
data = ''.join(data)
print(data)
| 1 | 1,513,161,408,880 | null | 61 | 61 |
N = int(input())
S,T = map(str,input().split())
slist = list(S)
tlist = list(T)
new = ''
for i in range(N):
new += slist[i]
new += tlist[i]
print(new)
|
while True:
x = [int(z) for z in input().split(" ")]
if x[0] == 0 and x[1] == 0: break
for h in range(0,x[0]):
for w in range(0,x[1]):
print("#", end="")
print("\n", end="")
print("")
| 0 | null | 56,317,598,914,148 | 255 | 49 |
n = int(input())
min = 2000000
max = -2000000
nums = list(map(int,input().split()))
sum = 0
for i in range(n):
sum += nums[i]
if nums[i] < min:
min = nums[i]
if nums[i] > max:
max = nums[i]
print(min,max,sum)
|
minute = int(input())
print('{}:{}:{}'.format(minute // 3600, (minute % 3600) // 60, (minute % 3600) % 60))
| 0 | null | 542,737,591,730 | 48 | 37 |
def resolve():
import numpy as np
d = int(input())
C = list(map(int, input().split()))
S = []
for _ in range(d):
_S = list(map(int, input().split()))
S.append(_S)
S = np.array(S)
T = []
for _ in range(d):
T.append(int(input()))
last = [-1 for i in range(26)]
score = 0
for i in range(d):
# max_idx = np.argmax(S[i])
# print(max_idx + 1)
score += S[i][T[i] - 1]
last[T[i] - 1] = i
for j in range(26):
score -= C[j] * (i - last[j])
print(score)
resolve()
|
D = int(input())
c = list(map(int, input().split()))
s = []
for i in range(D):
s.append(list(map(int, input().split())))
t = []
for i in range(D):
t.append(int(input())-1)
v = 0
last = [-1 for _ in range(26)]
for d in range(D):
v += s[d][t[d]]
last[t[d]]=d
for i in range(26):
v -= c[i] * (d - last[i])
print(v)
| 1 | 9,958,806,940,704 | null | 114 | 114 |
import pprint
N=int(input())
A=list(map(int,input().split()))
if N%2==0:#偶数なら1回まで2こ飛ばしで無茶できる
dp=[[-float("inf") for _ in range(2)] for _ in range(N+10)]#dpはj=0の時は無茶をしていない系列j=1の時は1回無茶をした系列である
if N==2:#2の時は個別
print(max(A[0],A[1]))
else:#それ以外の時は
for i in range(N):
if i==0: #i=0をとる時の任意の系列は無茶を確実にしていないのでdp[i][0]のみA[0]
dp[0][0]=A[0]
elif i==1: #i=1をとる時の任意の系列は一回無茶をして取っているはずである、よってdp[i][1]のみA[1]
dp[1][1]=A[1]
elif i==2: #i=2をとる時の任意の系列は一回も無茶せずに先頭からとっているのでdp[i][0]=A[0]+A[2]
dp[2][0]=A[0]+A[2]
else: #そのほかは再帰的に考える
for j in range(2):
if j==0: #無茶しない系列を生成する時
dp[i][0]=max(dp[i][0],dp[i-2][0]+A[i]) #そこまでの無茶しなかった系列に自らを足していけば良い
elif j==1: #一回無茶する系列を生成する時
dp[i][1]=max(dp[i][1],dp[i-2][1]+A[i],dp[i-3][0]+A[i]) #すでにある無茶した時の値(いらない?)と、前までの系列で無茶していて今回は無茶できないパターン、今回で無茶をするパターンのうち最大を用いて系列生成すれば良い
print(max(dp[N-1][1],dp[N-2][0])) #1回も飛ばさない時は後ろから二番目のdp値が相当することに注意
else:#奇数なら計2回まで無茶できる(2飛ばし×2、3飛ばし×1まで許容)
#print(A[0],A[1])
dp=[[-float("inf") for _ in range(3)] for _ in range(N+10)]#dpはj=0の時は無茶をしない系列j=1は1回無茶をした系列、j=2は2回無茶をしている系列
if N==3:#N=3の時は個別
print(max(A[0],A[1],A[2]))
else:#それ以外の時は
for i in range(N):
if i<4:
if i==0: #i=0をとる任意の系列は無茶を確実にしていないのでdp[i][0]のみA[0]
dp[0][0]=A[0]
if i==1: #i=1をとる任意の系列は確実に1回無茶をしているのでdp[i][1]のみA[1]
dp[1][1]=A[1]
if i==2: #i=2をとる時は2回分の無茶をしてA[2]を得る時(dp[i][2]=A[2])および1かいも無茶せずに撮っている時(dp[i][0]=A[2]+A[0])
dp[2][2]=A[2]
dp[2][0]=A[0]+A[2]
if i==3: #i=3をとる時は1回目で無茶をしてそのあと無茶していないパターン(dp[1][1]+A[3])といきなり1回無茶をしたパターン(dp[0][0]+A[3])があるのでその最大を
dp[3][1]=max(dp[1][1]+A[3],dp[0][0]+A[3])
else: #そのほかは再帰的に
for j in range(3):
if j==0: #無茶してない系列を生成する時、
dp[i][0]=max(dp[i][0],dp[i-2][0]+A[i]) #そこまでの無茶しなかった系列に自らを足していけば良い
elif j==1: #1回だけ無茶した系列を生成する時
dp[i][1]=max(dp[i][1],dp[i-2][1]+A[i],dp[i-3][0]+A[i]) #すでにある1回無茶した時の値(いらない?)と、前までの系列で1回無茶していて今回は無茶しないパターン、今回で初めて無茶をするパターンのうち最大を用いて系列生成すれば良い
else: #2回無茶した系列を生成する時
dp[i][2]=max(dp[i][2],dp[i-2][2]+A[i],dp[i-3][1]+A[i],dp[i-4][0]+A[i]) #すでにある2回無茶した時の値(いらない?)と、もう二回無茶していて無茶できないパターン、前までの系列で1回無茶していて今回も1回無茶するしないパターン、今回でいきなり2回無茶をするパターンのうち最大を用いて系列生成すれば良い
print(max(dp[N-1][2],dp[N-2][1],dp[N-3][0])) #1回も飛ばさない時は後ろから3番目が、1回だけ飛ばした時は後ろから2番目のdp値が相当することに注意
|
n=int(input())
if 30<=n :
print('Yes')
else:
print('No')
| 0 | null | 21,417,205,256,700 | 177 | 95 |
import math
a, b, x = map(int, input().split())
if x/(a*a*b) <= 0.5: #水が水筒の半分以下の場合
h = x/a/b*2
hy2 = (h*h + b*b)
cosa = (hy2 + b*b - h*h )/(2*(hy2**0.5)*b)
d = 180 - math.degrees(math.acos(cosa)) - 90
else:
rest = (a*a*b - x)/a
tan = rest*2/(a*a) #空の部分の面積は、a*atanθ/2 = a*a*b - x
d = math.degrees(math.atan(tan))
print(d)
|
D, T, S = map(int, input().split())
print("Yes" if(((T*S) - D) >= 0) else "No")
| 0 | null | 83,294,083,825,030 | 289 | 81 |
import sys
input = sys.stdin.readline
def main():
N = int(input())
a = list(map(int, input().split()))
s = 0
for i in a:
s ^= i
for i in a:
ans = s ^ i
print(ans, sep=' ', end=' ')
main()
|
n = int(input())
a = [int(i) for i in input().split(" ")]
t = a[0]
for i in a[1:]:
t = t ^ i
ans = [t^i for i in a]
print(" ".join(map(str, ans)))
| 1 | 12,463,191,437,088 | null | 123 | 123 |
n,m=map(int,input().split())
k=m//2
for i in range (0,k):
a=i+1
b=2*k+1-i
print (a,b)
c=2*k+2
k=(m+1)//2
j=1
#print(c,k)
for i in range (c,c+k):
a=i
b=c+2*k-j
j+=1
print(a,b)
|
def main():
n, m = map(int, input().split())
left = 1
right = n
is_shift = False
ans = []
while left < right:
if right - left <= n // 2 and not is_shift and n % 2 == 0:
left += 1
is_shift = True
ans.append([left, right])
left += 1
right -= 1
for l in range(m):
print(*ans[l])
if __name__ == "__main__":
main()
| 1 | 28,699,687,945,208 | null | 162 | 162 |
moji = str(input())
print(("No","Yes")[("B" in moji) and ("A" in moji)])
|
a = input("")
if "A" in a and "B" in a:
print("Yes")
else:
print("No")
| 1 | 55,172,084,805,292 | null | 201 | 201 |
a, b = [int(x) for x in raw_input().split(" ")]
if a > b:
print("a > b")
elif a < b:
print("a < b")
else:
print("a == b")
|
count = 1
while True:
x = int(input())
if x == 0:
break
print('Case %d: %d' % (count, x))
count += 1
| 0 | null | 431,386,636,460 | 38 | 42 |
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])
|
inputs = input().split(" ")
stack = []
for str in inputs:
if str.isdigit():
stack.append(int(str))
else:
b = stack.pop()
a = stack.pop()
if str == '+':
stack.append(a + b)
elif str == '-':
stack.append(a - b)
elif str == '*':
stack.append(a * b)
print(stack.pop())
| 1 | 39,497,436,132 | null | 18 | 18 |
a, b = map(int, input().split())
print("unsafe" if (a <= b) else "safe")
|
x, y = map(int, input().split())
if y >= x:
print("unsafe")
else:
print("safe")
| 1 | 29,336,815,544,036 | null | 163 | 163 |
from collections import deque
import copy
H,W=map(int,input().split())
S=[list(input()) for _ in range(H)]
S1=copy.deepcopy(S)
ans=[]
sy=0
sx=0
for i in range(H):
for j in range(W):
c=0
route=deque([(i,j,0)])
S=copy.deepcopy(S1)
while route:
a,b,n=route.popleft()
c=n
if 0<=a<=H-1 and 0<=b<=W-1:
if S[a][b]=='.':
S[a][b]='#'
route.append((a+1,b,n+1))
route.append((a-1,b,n+1))
route.append((a,b+1,n+1))
route.append((a,b-1,n+1))
ans.append(c-1)
print(max(ans))
|
n,k,s=map(int,input().split())
sMax=10**9
if k==0:
if s==sMax:
print("1 "*n)
else:
print((str(sMax)+" ")*n)
else:
if s==sMax:
print((str(s)+" ")*k+"1 "*(n-k))
else:
print((str(s)+" ")*k+(str(sMax)+" ")*(n-k))
| 0 | null | 92,884,170,848,590 | 241 | 238 |
N = int(input())
X = [list(map(int,input().split())) for _ in range(N)]
A = []
for i in range(N):
x,l = X[i]
A.append((x-l,x+l))
B1 = sorted(A,key=lambda x:x[1])
B1 = [(B1[i][0],B1[i][1],i) for i in range(N)]
B2 = sorted(B1,key=lambda x:x[0])
hist = [0 for _ in range(N)]
cnt = 0
i = 0
for k in range(N):
if hist[k]==0:
r = B1[k][1]
hist[k] = 1
cnt += 1
while i<N:
l,j = B2[i][0],B2[i][2]
if hist[j]==1:
i += 1
continue
if l<r:
hist[j] = 1
i += 1
else:
break
print(cnt)
|
def search(S, n, t):
i=0
S[n]=t
while (S[i] != t):
i+=1
return i!=n
sum=0
n=int(input())
S=list(map(int, input().split()))
S.append([0]*10000)
q=int(input())
T=list(map(int, input().split()))
for i in range(0,q):
if search(S,n,T[i]):
sum+=1
print(sum)
| 0 | null | 45,106,734,581,310 | 237 | 22 |
r,c = map(int,input().split())
rc = list([int(x) for x in input().split()] for _ in range(r))
[rc[i].append(sum(rc[i])) for i in range(r)]
rc.append([sum(i) for i in zip(*rc)])
for col in rc: print(*col)
|
n, q = map(int, input().split())
processes = [[li[0], int(li[1])] for li in [input().split() for _ in range(n)]]
elapsed_time = 0
while len(processes):
process = processes.pop(0)
if process[1] > q:
process[1] -= q
processes.append(process)
elapsed_time += q
else:
elapsed_time += process[1]
print(process[0], elapsed_time)
| 0 | null | 686,640,682,800 | 59 | 19 |
A, B = list(map(int, input().split()))
ans = A - B*2
if ans <= 0:
print(0)
else:
print(A-B*2)
|
import sys
from math import pi
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def main():
R = int(readline())
print(2 * R * pi)
return
if __name__ == '__main__':
main()
| 0 | null | 99,386,078,107,680 | 291 | 167 |
#ABC 171 E - Red Scarf
n = int(input())
A = list(map(int, input().split()))
S = 0
for i in A:
S ^= i
B = [S^i for i in A]
print(*B)
|
from functools import reduce
n = input()
a = list(map(int, input().split()))
s = int(reduce(lambda i,j: i^j, a))
print(' '.join(list(map(lambda x: str(x^s), a))))
| 1 | 12,466,215,243,168 | null | 123 | 123 |
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')
H, W = map(int, readline().split())
masu = []
for _ in range(H):
masu.append(readline().rstrip().decode('utf-8'))
# print(masu)
dp = [[INF]*W for _ in range(H)]
dp[0][0] = int(masu[0][0] == "#")
dd = [(1, 0), (0, 1)]
# 配るDP
for i in range(H):
for j in range(W):
for dx, dy in dd:
ni = i + dy
nj = j + dx
# はみ出す場合
if (ni >= H or nj >= W):
continue
add = 0
if masu[ni][nj] == "#" and masu[i][j] == ".":
add = 1
dp[ni][nj] = min(dp[ni][nj], dp[i][j] + add)
# print(dp)
ans = dp[H-1][W-1]
print(ans)
|
import math
n=int(input())
print( (pow(10,n)-pow(9,n)*2+pow(8,n))%1000000007)
| 0 | null | 26,371,381,119,860 | 194 | 78 |
n = int(input())
s = [[] for _ in range(n)]
for i in range(n):
s[i] = input()
s = set(s)
print(len(s))
|
if __name__ == '__main__':
N, K = map(int, input().split())
H = list(map(int, input().split()))
H = sorted(H)[::-1]
cnt = 0
for h in H:
if K>0:
K -= 1
else:
cnt += h
print(cnt)
| 0 | null | 54,594,530,630,430 | 165 | 227 |
while True:
x,y = map(int,input().split())
if (x,y) ==(0,0): break
if x < y:
print(x,y)
else:
print(y,x)
|
n = input()
A = [i for i in input().split()]
A.reverse()
print(' '.join(A))
| 0 | null | 743,552,843,572 | 43 | 53 |
N = int(input())
A = []
for i in range(N):
a = int(input())
xy = [list(map(int, input().split())) for _ in range(a)]
A.append([a, xy])
# print(A)
# 下からi桁目のbitが1->人iは正直
# 下からi桁目のbitが0->人iは不親切
# 不親切な人の証言は無視して良い
ans = 0
for i in range(2**N):
for j in range(N):
flag = 0
if (i >> j) & 1:
for k in range(A[j][0]):
l = A[j][1][k][0]
m=A[j][1][k][1]
# print(i,j,k,(l,m),(i >> (l-1)))
if not (i >> (l-1)) & 1 == A[j][1][k][1]:
flag = 1
break
if flag == 1:
break
else:
# print(bin(i))
ct = 0
for l in range(N):
ct += ((i >> l) & 1)
ans = max(ans, ct)
print(ans)
|
S = str(input())
name = S[0:3]
print(name)
| 0 | null | 68,094,349,067,862 | 262 | 130 |
a=int(input())
if 400<=a and a<=599:
print(8)
if 600<=a and a<=799:
print(7)
if 800<=a and a<=999:
print(6)
if 1000<=a and a<=1199:
print(5)
if 1200<=a and a<=1399:
print(4)
if 1400<=a and a<=1599:
print(3)
if 1600<=a and a<=1799:
print(2)
if 1800<=a and a<=1999:
print(1)
|
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1
X=int(input())
if X>=1800:
print(1)
elif X>=1600:
print(2)
elif X>=1400:
print(3)
elif X>=1200:
print(4)
elif X>=1000:
print(5)
elif X>=800:
print(6)
elif X>=600:
print(7)
elif X>=400:
print(8)
| 1 | 6,692,377,968,572 | null | 100 | 100 |
from collections import deque
N = int(input())
# mtx = np.zeros([N, N], dtype=np.int32)
tree = [[] for i in range(N)]
key_order = [0] * (N-1)
for i in range(N-1):
in1, in2 = map(int, input().split())
in1 -= 1
in2 -= 1
tree[in1].append(in2)
tree[in2].append(in1)
key_order[i] = (in1, in2)
# [print(i, t) for i, t in enumerate(tree)]
def bfs(tree, p):
seen = [False] * len(tree)
queue = deque((p,))
edge_colors = dict()
node_colors = [0] * len(tree)
while len(queue) > 0:
q = queue.popleft()
seen[q] = True
parent_color = node_colors[q]
cnt = 1
for v in tree[q]:
if not seen[v]:
if cnt == parent_color:
cnt += 1
edge_colors[(q, v)] = cnt
node_colors[v] = cnt
queue.append(v)
cnt += 1
"""
node_colors = [set() for _ in tree]
edge_colors = dict()
all_colors = set()
color = 0
while len(queue) > 0:
q = queue.popleft()
seen[q] = True
for v in tree[q]:
if not seen[v]:
residual = all_colors - node_colors[q]
if len(residual) > 0:
this_color = residual.pop()
else:
color += 1
this_color = color
edge_colors[(q, v)] = this_color
node_colors[q].add(this_color)
node_colors[v].add(this_color)
all_colors.add(this_color)
queue.append(v)
"""
return edge_colors
edge_colors = bfs(tree, 0)
print(max([c for key, c in edge_colors.items()]))
[print(edge_colors[t]) for t in key_order]
# print(edge_colors)
# show_tree(tree)
|
import collections
h,w,m = map(int,input().split())
gyou = [0 for i in range(h)]
retu = [0 for i in range(w)]
l = []
for i in range(m):
y,x = map(int,input().split())
gyou[y-1] += 1
retu[x-1] += 1
l.append((y-1,x-1))
ymax = max(gyou)
xmax = max(retu)
c_ymax = gyou.count(ymax)
c_xmax = retu.count(xmax)
ans = ymax + xmax
c = 0
for y,x in l:
if gyou[y] == ymax and retu[x] == xmax:
c += 1
if c == c_ymax*c_xmax:
print(ans-1)
else:
print(ans)
| 0 | null | 70,129,773,290,470 | 272 | 89 |
a, b=map(int, input().split())
print(-1 if a>9 or b>9 else a*b)
|
n = list(map(int,input().split()))
if max(n)>9:
print(-1)
else:
print(n[0]*n[1])
| 1 | 158,183,466,424,900 | null | 286 | 286 |
N,S = map(int,input().split())
A = list(map(int,input().split()))
mod = 998244353
dp = [[0]*(S+1) for _ in range(N)]
dp[0][0] = 2
if A[0] <= S:
dp[0][A[0]] = 1
for i in range(1,N):
for j in range(S+1):
if j-A[i] >= 0:
dp[i][j] = dp[i-1][j-A[i]] + 2*dp[i-1][j]
else:
dp[i][j] = 2*dp[i-1][j]
dp[i][j] %= mod
#print(dp)
print(dp[N-1][S])
|
def sep():
return map(int,input().strip().split(" "))
def lis():
return list(sep())
a,b=sep()
print(a*b)
| 0 | null | 16,768,560,056,420 | 138 | 133 |
while True:
try:
(h,w)=map(int,raw_input().split())
if h==0 and w==0: break
for i in xrange(h):
print ''.join(['#' for j in xrange(w)])
print
except EOFError: break
|
# coding: utf-8
# Your code here!
# ITP1_5_A
while(True):
x,y=map(int,input().split())
if x==0 and y==0:
break
else:
for r in range(0,x):
print("#"*y)
print("")
| 1 | 776,657,053,910 | null | 49 | 49 |
import heapq
def main():
_ = int(input())
A = sorted(list(map(int, input().split())), reverse=True)
q = []
heapq.heapify(q)
ans = A[0]
tmp_score = min(A[0], A[1])
heapq.heappush(q, (-tmp_score, A[0], A[1]))
heapq.heappush(q, (-tmp_score, A[1], A[0]))
for a in A[2:]:
g = heapq.heappop(q)
ans += -g[0]
tmp_score1 = min(a, g[1])
tmp_push1 = (-tmp_score1, a, g[1])
tmp_score2 = min(a, g[2])
tmp_push2 = (-tmp_score2, a, g[2])
heapq.heappush(q, tmp_push1)
heapq.heappush(q, tmp_push2)
print(ans)
if __name__ == '__main__':
main()
|
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
ans=-10**10
for i in range(n):
loop=[0]
now=i
while True:
now=p[now]-1
loop.append(loop[-1]+c[now])
if now==i:
break
L=len(loop)-1
if k<=L:
score=max(loop[1:k+1])
ans=max(score,ans)
else:
score=max(loop[-1],0)*(k//L-1)
ans=max(ans,score)
for j in range(k%L+L):
score+=loop[j%L+1]-loop[j%L]
ans=max(score,ans)
print(ans)
| 0 | null | 7,323,591,421,800 | 111 | 93 |
N=int(input())
A=map(int, input().split())
P=1000000007
ans = 1
cnt = [3 if i == 0 else 0 for i in range(N + 1)]
for a in A:
ans=ans*cnt[a]%P
if ans==0:
break
cnt[a]-=1
cnt[a+1]+=1
print(ans)
|
MOD = 10**9 + 7
def main():
N = int(input())
A = [int(i) for i in input().split()]
B = [0]*N
ans = 3
for a in A:
if a == 0:
if B[a] == 1:
ans *= 2
else:
ans *= B[a-1] - B[a]
B[a] += 1
if 3 < B[a] or (a != 0 and B[a-1] < B[a]):
ans = 0
break
ans %= MOD
print(ans)
if __name__ == '__main__':
main()
| 1 | 130,128,591,797,962 | null | 268 | 268 |
from collections import deque
n = int(input())
d = deque()
dic = {}
dic_l = {}
for i in range(n-1):
a,b = map(int,input().split())
if(a in dic):
dic[a].append(b)
else:
dic[a] = [b]
if(b in dic):
dic[b].append(a)
else:
dic[b] = [a]
l = max(a,b)
s = min(a,b)
dic_l[s,l] = i
#print(dic_l)
mx = 0
for k in dic:
mx = max(mx,len(dic[k]))
#print("mx",mx)
col = [i+1 for i in range(mx)]
ans = [0 for _ in range(n-1)]
seen = {1}
cnt = 0
tmp = deque()
for i in dic[1]:
if not(i in seen):
d.append(i)
seen.add(i)
tmp.append(1)
tmp.append(i)
load_key = (1,i)
l_num = dic_l[load_key]
ans[l_num] = col[cnt]
cnt += 1
while d:
nxt = d.popleft()
tmp_1 = tmp.popleft()
tmp_2 = tmp.popleft()
ng = (tmp_1,tmp_2)
#print(ng)
ng_col = ans[dic_l[ng]]
cnt = 0
for i in dic[nxt]:
if not(i in seen):
d.append(i)
seen.add(i)
tmp.append(nxt)
tmp.append(i)
l = max(nxt,i)
s = min(nxt,i)
load_key = (s,l)
l_num = dic_l[load_key]
while cnt+1 == ng_col:
cnt += 1
ans[l_num] = col[cnt]
cnt += 1
print(mx)
for s in ans:
print(s)
|
kazu = input()
kazu = int(kazu)
for i in range( kazu ):
print("ACL",end = '')
| 0 | null | 68,963,224,157,662 | 272 | 69 |
import sys
readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)
def main():
from itertools import permutations
from math import factorial
N = int(readline())
mat = []
s = 0
for _ in range(N):
x, y = map(int, readline().split())
mat.append([x, y])
for per in permutations(range(N)):
for i in range(N - 1):
x1, y1 = mat[per[i]]
x2, y2 = mat[per[i + 1]]
s += ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
print(s / factorial(N))
if __name__ == '__main__':
main()
|
import math
N = int(input())
xy = [list(map(int,input().split())) for i in range(N)]
ave = 0
cnt = 0
import itertools
for v in itertools.permutations([int(x) for x in range(N)],N) :
for i in range(1,len(v)) :
ave += math.sqrt((xy[v[i-1]][0]-xy[v[i]][0])**2+(xy[v[i-1]][1]-xy[v[i]][1])**2)
cnt += 1
ave = ave / cnt
print(ave)
| 1 | 147,922,193,683,440 | null | 280 | 280 |
counter=0
while True:
counter+=1
x=int(input())
if x==0:
break
print("Case "+str(counter)+": "+str(x))
|
i = 1
while True:
n = input()
if n != 0:
print 'Case %s: %s' % (str(i), str(n))
i = i + 1
else:
break
| 1 | 469,542,010,012 | null | 42 | 42 |
A, B = [x for x in input().split(" ")]
print(int(A) * int(float(B) * 100 + 0.5) // 100)
|
n=int(input())
a=list(map(int,input().split()))
count=0
total=0
for i in range(n):
if a[i]%2==0:
count+=1
if a[i]%3==0 or a[i]%5==0:
total+=1
if count==total:
print("APPROVED")
else:
print("DENIED")
| 0 | null | 42,525,844,257,796 | 135 | 217 |
n = int(input())
a = list(map(int, input().split()))
# a = np.array(a)
# ans = []
# while a.min() != 9999999:
# min_index = a.argmin()
# ans.append(min_index+1)
# a[min_index] = 9999999
# print(' '.join([str(_) for _ in ans]))
l = []
for i in range(n):
l.append([i+1, a[i]])
sl = sorted(l, key=lambda x: x[1])
sl0 = [r[0] for r in sl]
print(' '.join([str(_) for _ in sl0]))
|
import sys
input = lambda: sys.stdin.readline().rstrip()
def main():
k, x = map(int, input().split())
if x <= k * 500:
print('Yes')
else:
print('No')
if __name__ == '__main__':
main()
| 0 | null | 139,674,042,908,078 | 299 | 244 |
x = list(input().split())
for i in range(5):
# logging.debug("i = {},x[i] = {}".format(i, x[i])) #
if x[i] == "0":
print(i + 1)
exit()
# logging.debug("n = {},f = {},f**b = {}".format(n, f, f**b)) #
#logging.debug("デバッグ終了")#
|
N = int(input())
A = [int(a) for a in input().split(" ")]
swop = 0
for i, a in enumerate(A):
j = N - 1
while j >= i + 1:
if A[j] < A[j - 1]:
tmp = A[j]
A[j] = A[j - 1]
A[j - 1] = tmp
swop += 1
j -= 1
print(" ".join([str(a) for a in A]))
print(swop)
| 0 | null | 6,663,987,739,512 | 126 | 14 |
n = int(input())
R = []
for i in range(n):
R.append(int(input()))
minv = R[0]
maxv = R[1] - R[0]
for r in R[1:]:
maxv = max(maxv, r - minv)
minv = min(minv, r)
print(maxv)
|
n = int(raw_input())
a = [int(x) for x in raw_input().split()]
q = int(raw_input())
op = []
for _ in xrange(q):
v = raw_input().split()
op.append([int(x) for x in v])
d = {}
total = 0
for v in a:
d[v] = d.get(v, 0) + 1
total += v
for src, dest in op:
c = d.pop(src, 0)
d[dest] = d.get(dest, 0) + c
total += c * (dest - src)
print total
| 0 | null | 6,041,124,102,772 | 13 | 122 |
n,m=map(int,input().split())
lst=list(map(int,input().split()))
if n<sum(lst) : print(-1)
else : print(n-sum(lst))
|
n,m = map(int,input().split())
alist = list(map(int,input().split()))
d = n - sum(alist)
if d < 0:
print(-1)
else:
print(d)
| 1 | 32,061,328,134,418 | null | 168 | 168 |
from fractions import gcd
def lcm(a, b):
return (a * b) // gcd(a, b)
def f(x):
res = 0
while x % 2 == 0:
x //= 2
res += 1
return res
n, m = map(int, input().split())
a = [int(i) // 2 for i in input().split()]
t = f(a[0])
flag = True
for i in range(n):
if f(a[i]) != t:
flag = False
else:
a[i] >>= t
m >>= t
l = 1
for i in range(n):
l = lcm(l, a[i])
if l > m:
flag = False
m //= l
print(-(-m // 2) if flag else 0)
|
from fractions import gcd
def lcm(a,b):
return a*b//gcd(a,b)
N,M = map(int,input().split())
A = [int(i) for i in input().split()]
a = 0
b = A[0]
while b % 2 == 0:
a += 1
b = b//2
for i in range(1,N):
if A[i] % (2**a) != 0 or A[i] % (2**(a+1)) == 0:
print(0)
exit()
c = A[0]
for i in range(N-1):
c = lcm(c,A[i+1])
print((M+c//2)//c)
| 1 | 101,825,391,968,092 | null | 247 | 247 |
while True:
H, W = map(int, input().split())
if H == 0 and W == 0 :
break
for j in range(H):
for i in range(W):
print('#',end='')
print()
print()
if H == 0 and W == 0 :
break
|
H=1
W=1
while H!=0 or W!=0:
H,W = [int(i) for i in input().split()]
if H!=0 or W!=0:
for i in range(H):
for j in range(W):
print('#',end='')
print()
print()
| 1 | 770,714,317,892 | null | 49 | 49 |
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
def main():
x = int(ipt())
if x < 600:
print(8)
elif x < 800:
print(7)
elif x < 1000:
print(6)
elif x < 1200:
print(5)
elif x < 1400:
print(4)
elif x < 1600:
print(3)
elif x < 1800:
print(2)
elif x < 2000:
print(1)
else:
pass
return None
if __name__ == '__main__':
main()
|
X = int(input())
rank = -1
if X <= 599:
rank = 8
elif X <= 799:
rank = 7
elif X <= 999:
rank = 6
elif X <= 1199:
rank = 5
elif X <= 1399:
rank = 4
elif X <= 1599:
rank = 3
elif X <= 1799:
rank = 2
else:
rank = 1
print(rank)
| 1 | 6,697,530,248,028 | null | 100 | 100 |
n = input()
n = n[::-1] + '0'
inf = 10 ** 9
dp = [[inf for _ in range(2)] for _ in range(len(n) + 1)]
dp[0][0] = 0
dp[0][1] = 2
for i in range(len(n)):
n_i = int(n[i])
dp[i + 1][0] = min(dp[i][0] + n_i, dp[i][1] + n_i)
dp[i + 1][1] = min(dp[i][0] + 11 - n_i, dp[i][1] + 9 - n_i)
print(min(dp[-1][0], dp[-1][1]))
|
N=input()
dp0=[0]*(len(N)+1)
dp1=[0]*(len(N)+1)
dp1[0]=1
for i in range(len(N)):
n=int(N[i])
dp0[i+1]=min(dp0[i]+n, dp1[i]+(10-n))
n+=1
dp1[i+1]=min(dp0[i]+n, dp1[i]+(10-n))
#print(dp0)
#print(dp1)
print(dp0[-1])
| 1 | 71,076,774,007,370 | null | 219 | 219 |
n = int(input())
A = list(map(int,input().split()))
ANS=[0]*(n)
for i in range(n-1):
a=A[i]
ANS[a-1]+=1
for i in range(n):
print(ANS[i])
|
N = int(input())
A = [0]*(N)
for i in input().split():
A[int(i)-1] = A[int(i)-1]+1
for i in A:
print(i)
| 1 | 32,505,833,651,148 | null | 169 | 169 |
X = int(input())
if 1800 <= X:
print(1)
elif 1600 <= X:
print(2)
elif 1400 <= X:
print(3)
elif 1200 <= X:
print(4)
elif 1000 <= X:
print(5)
elif 800 <= X:
print(6)
elif 600 <= X:
print(7)
else:
print(8)
|
X, Y = map(int, input().split())
if Y % 2 == 1:
print("No")
exit(0)
if 0 <= 2 * X - Y / 2 <= X:
print("Yes")
else:
print("No")
| 0 | null | 10,186,340,866,310 | 100 | 127 |
(a, b, c) = (int(i) for i in input().split())
count = 0
for i in range(a, (b + 1)):
if c % i == 0:
count = count + 1
print(count)
|
N,M = map(int, input().split())
AC = [0] * (N+1)
pl = [0] * (N+1)
cole = 0
pena = 0
for target_list in range(M):
ans = input().split()
problem = int(ans[0])
result = ans[1]
if AC[problem] == 0 and result == 'WA':
pl[problem] += 1
elif AC[problem] == 0 and result == 'AC':
pena += pl[problem]
cole += 1
AC[problem] = 1
print(cole, pena)
| 0 | null | 46,930,528,193,560 | 44 | 240 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.