code1
stringlengths 16
24.5k
| code2
stringlengths 16
24.5k
| similar
int64 0
1
| pair_id
int64 2
181,637B
⌀ | question_pair_id
float64 3.71M
180,628B
⌀ | code1_group
int64 1
299
| code2_group
int64 1
299
|
---|---|---|---|---|---|---|
n,m = map(int,input().split())
matrix = [[0 for i in range(m)] for j in range(n)]
b = [0 for i in range(m)]
c = [0 for i in range(n)]
for i in range(0,n):
matrix[i] = list(map(int,input().split()))
for i in range(0,m):
b[i] = int(input())
for i in range(0,n):
for j in range(0,m):
c[i] += matrix[i][j]*b[j]
print(c[i])
| A = []
b = []
n, m = map(int,input().split())
for i in range(n):
A.append(list(map(int,input().split())))
for i in range(m):
b.append(int(input()))
for i in range(n):
c = 0
for s in range(m):
c += A[i][s] * b[s]
print(c) | 1 | 1,165,868,457,024 | null | 56 | 56 |
alpha = input()
print("a" if alpha.islower() else "A") | S = input()
print( 'A' if S.isupper() else 'a' ) | 1 | 11,347,455,266,642 | null | 119 | 119 |
l = [1,2,3]
A = int(input())
B = int(input())
for i in range(3):
if l[i]!=A and l[i]!=B:
print(l[i]) | n, k = map(int, input().split())
A = list(map(int, input().split()))
l = 1
r = 10**9+1
while l < r:
mid = (l+r)//2
count = 0
for i in range(n):
if A[i] > mid:
count += A[i]//mid
if count <= k:
r = mid
else:
l = mid+1
print(l) | 0 | null | 58,373,368,828,900 | 254 | 99 |
n,k = map(int,input().split())
ans = 1
while n >= k:
ans += 1
n //= k
print(ans) | import itertools
import functools
import math
from collections import Counter
from itertools import combinations
import re
N,K=map(int,input().split())
cnt = 1
while True:
if cnt > 10000:
break
if N // K**cnt == 0:
print(cnt)
exit()
cnt += 1
| 1 | 64,397,122,374,408 | null | 212 | 212 |
#!/usr/bin/env python3
import sys
def solve(H: int, A: int):
count = 0
while True:
H -= A
count += 1
if H <= 0:
break
print(count)
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
A = int(next(tokens)) # type: int
solve(H, A)
if __name__ == '__main__':
main()
| h, a = map(int, input().split())
b = 0
while h-a > 0:
h = h - a
b += 1
print(b + 1)
| 1 | 77,150,491,678,300 | null | 225 | 225 |
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(1, n):
j = i + d
k = j + d
if k >= n:
break
if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
ans -= 1
print(ans) | n=int(input())-1
print(n//2) | 0 | null | 94,587,990,425,848 | 175 | 283 |
import sys
from collections import defaultdict
def solve():
input = sys.stdin.readline
N, P = map(int, input().split())
S = input().strip("\n")
modSum = 0
modDict = defaultdict(int)
modDict[0] += 1
ans = 0
if P == 2 or P == 5:
for i in range(N):
if int(S[i]) % P == 0:
ans += i + 1
else:
for i in range(N):
modSum += int(S[N-i-1]) * pow(10, i, P)
modSum %= P
modDict[modSum] += 1
for key in modDict:
v = modDict[key]
ans += v * (v - 1) // 2
print(ans)
return 0
if __name__ == "__main__":
solve() | n=int(input())
s=list(map(str,input()))
if n%2==0:
num=int(n/2)
Sx=s[0:num]
Sy=s[num:n]
if Sx==Sy:
print("Yes")
else:
print("No")
else:
print("No") | 0 | null | 102,144,207,349,280 | 205 | 279 |
n = int(input())
p2 = (n + 1) / 1.08
p1 = n / 1.08
if p2 > int(p2) >= p1:
print(int(p2))
else:
print(":(") | n = int(input())
res = -1
for x in range(1, n+1):
if int(x * 1.08) == n:
res = x
if res == -1:
print(":(")
else:
print(res) | 1 | 126,246,519,290,432 | null | 265 | 265 |
N = int(input())
primes = {}
for num in range(2, int(N**0.5)+1):
while N%num == 0:
if num in primes:
primes[num] += 1
else:
primes[num] = 1
N //= num
ans = 0
for p in primes.values():
n = 1
while n*(n+1)//2 <= p:
n += 1
ans += n-1
if N > 1:
ans += 1
print(ans) | x = int(input())
flag = 0
if x >= 2000:
print(1)
exit()
else:
for i in range(20):
for j in range(20):
for k in range(20):
for l in range(20):
for m in range(20):
for n in range(20):
if i*100+j*101+k*102+l*103+m*104+n*105 == x:
flag = 1
print(1)
exit()
if flag != 1:
print(0) | 0 | null | 72,230,974,988,088 | 136 | 266 |
A = int(input())
B = int(input())
for i in range(1, 4):
if i in [A, B]:
continue
else:
print(i)
break | a=int(input())
b=int(input())
print(6//(a*b)) | 1 | 110,621,672,007,154 | null | 254 | 254 |
A,B=input().split()
A=int(A)
B=int(100*float(B)+0.5)
print(A*B//100)
| def main():
import sys
N = int(sys.stdin.readline())
L = [0 for i in range(45)]
L[0], L[1] = 1, 1
for i in range(2, 45):
L[i] = L[i-1] + L[i-2]
#print(L)
print(L[N])
if __name__=='__main__':
main()
| 0 | null | 8,307,059,501,822 | 135 | 7 |
x, k, d = map(int, input().split())
x = abs(x)
num = x // d
if num >= k:
ans = x - k*d
else:
if (k-num)%2 == 0:
ans = x - num*d
else:
ans = min(abs(x - num*d - d), abs(x - num*d + d))
print(ans) | N, A, B = map(int, input().split())
ans = 0
if (B-A)%2 == 0:
print((B-A)//2)
else:
if A-1 < N-B:
ans += A-1
else:
ans += N-B
ans += 1
ans += (B-A-1)//2
print(ans)
| 0 | null | 56,994,460,410,290 | 92 | 253 |
k = int(input())
a, b = list(map(int, input().split()))
p = 0
while p <= 1000:
if a <= p <= b:
print("OK")
exit()
p += k
print("NG")
| # 165 A
K = int(input())
A,B = list(map(int, input().split()))
m = B%K
print('OK') if B-m >= A else print('NG') | 1 | 26,503,021,998,816 | null | 158 | 158 |
N,M=map(int,input().split())
a=M//2
b=M-a
c=1
d=2*a+1
for i in range(a):
print(c,d)
c+=1
d-=1
e=2*a+2
f=2*M+1
for i in range(b):
print(e,f)
e+=1
f-=1 | def solve():
N = int(input())
ans = 0
for i in range(1, N+1):
if i%3 != 0 and i%5 != 0:
ans += i
print(ans)
if __name__ == "__main__":
solve() | 0 | null | 31,748,411,693,472 | 162 | 173 |
n = int(input())
ans = (n+2-1)//2
print(ans) | N = int(input())
if N%2 == 0:
print(0.5000000000)
else:
odd = N//2 + 1
print(odd/N) | 0 | null | 118,188,236,358,192 | 206 | 297 |
N = int(input())
L = []
for i in range(N):
s, t = input().split()
L.append((s, int(t)))
X = input()
ans = 0
f = False
for s, t in L:
if f:
ans += t
if s == X:
f = True
print(ans)
| import itertools
lst = list(map(int, input().split()))
k = int(input())
ans = 'No'
for tup in itertools.combinations_with_replacement([0, 1, 2], k):
temp = lst[:]
for i in tup:
temp[i] *= 2
if temp[0] < temp[1] and temp[1] < temp[2]:
ans = 'Yes'
print(ans) | 0 | null | 51,737,261,523,180 | 243 | 101 |
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
A_odd = A[::2]
print(np.sum(A_odd % 2)) | n = int(input())
z = input().split()
odd = 0
for i in range(n):
if i % 2 == 0:
if int(z[i]) % 2 == 1:
odd += 1
print(odd) | 1 | 7,789,468,621,350 | null | 105 | 105 |
S = input()
mod = 2019
array = []
for i in range(len(S)):
x = (int(S[len(S)-1-i])*pow(10,i,mod))%mod
array.append(x)
array2 = [0]
y = 0
for i in range(len(S)):
y = (y+array[i])%mod
array2.append(y)
array3 = [0] * 2019
ans = 0
for i in range(len(array2)):
z = array2[i]
ans += array3[z]
array3[z] += 1
print(ans)
#3*673
| #https://mirucacule.hatenablog.com/entry/2020/04/27/090908
#https://drken1215.hatenablog.com/entry/2020/04/29/171300
S=str(input())[::-1]#逆順で格納
N=len(S)
counter=[0]*2019
counter[0]=1
ans=0
num,d=0,1
for c in S:
num += int(c) * d
num %= 2019
d *= 10
d %= 2019
counter[num]+=1
for i in counter:
ans += i*(i-1)//2
print(ans) | 1 | 30,694,585,661,390 | null | 166 | 166 |
st = str(input())
q = int(input())
com = []
for i in range(q):
com.append(input().split())
for k in com:
if k[0] == "print":
print(st[int(k[1]):int(k[2])+1])
elif k[0] == "reverse":
s = list(st[int(k[1]):int(k[2])+1])
s.reverse()
ss = "".join(s)
st = st[:int(k[1])] + ss + st[int(k[2])+1:]
else:
st = st[:int(k[1])] + k[3] + st[int(k[2])+1:]
| import sys
line = sys.stdin.readline().strip()
n = int(sys.stdin.readline())
for i in range(n):
v = sys.stdin.readline().split()
op = v[0]
a = int(v[1])
b = int(v[2])
if op == "print":
print(line[a: b + 1])
elif op == "reverse":
line = line[:a] + line[a: b + 1][::-1] + line[b + 1:]
elif op == "replace":
p = v[3]
line = line[:a] + p + line[b + 1:] | 1 | 2,076,285,455,552 | null | 68 | 68 |
n=int(raw_input())
p=[0,0]
for i in range(n):
c=[0 for i in range(100)]
c=map(list,raw_input().split())
m=0
while 1:
try:
if c[0]==c[1]:
p[0]+=1
p[1]+=1
break
elif c[0][m]<c[1][m]:
p[1]+=3
break
elif c[1][m]<c[0][m]:
p[0]+=3
break
m+=1
except IndexError:
if len(c[0])<len(c[1]):
p[1]+=3
break
if len(c[1])<len(c[0]):
p[0]+=3
break
print p[0], p[1] | n = int(input())
t, h = 0, 0
for i in range(n):
s1, s2 = input().split()
if s1 > s2:
t += 3
elif s1 < s2:
h += 3
else:
t += 1
h += 1
print(t, h) | 1 | 2,029,563,850,652 | null | 67 | 67 |
# coding: utf-8
import itertools
from functools import reduce
from collections import deque
N, K = list(map(int, input().split(" ")))
A = list(map(int, input().split(" ")))
for i in range(N):
if i > K-1:
if A[i] > A[i-K]:
print("Yes")
else:
print("No")
| K = int(input())
id = 1
cnt = 7
while cnt < K:
cnt = cnt * 10 + 7
id += 1
visited = [0] * K
while True:
remz = (cnt % K)
if remz == 0:
break
visited[remz] += 1
if visited[remz] > 1:
id = -1
break
cnt = remz * 10 + 7
id += 1
print(id)
| 0 | null | 6,536,991,350,780 | 102 | 97 |
def answer(s: str) -> str:
return 'x' * len(s)
def main():
s = input()
print(answer(s))
if __name__ == '__main__':
main() | S = input()
S = list(S)
x = 'x'
for i in range(len(S)):
S[i] = x
print("".join(S)) | 1 | 72,621,516,610,932 | null | 221 | 221 |
# coding=utf-8
from math import floor, ceil, sqrt, factorial, log, gcd
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from heapq import heappop, heappush, heappushpop, heapify
import copy
import sys
INF = float('inf')
mod = 10**9+7
sys.setrecursionlimit(10 ** 6)
def lcm(a, b): return a * b / gcd(a, b)
# 1 2 3
# a, b, c = LI()
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
# a = I()
def I(): return int(sys.stdin.buffer.readline())
# abc def
# a, b = LS()
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
# a = S()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
# 2
# 1
# 2
# [1, 2]
def IR(n): return [I() for i in range(n)]
# 2
# 1 2 3
# 4 5 6
# [[1,2,3], [4,5,6]]
def LIR(n): return [LI() for i in range(n)]
# 2
# abc
# def
# [abc, def]
def SR(n): return [S() for i in range(n)]
# 2
# abc def
# ghi jkl
# [[abc,def], [ghi,jkl]]
def LSR(n): return [LS() for i in range(n)]
# 2
# abcd
# efgh
# [[a,b,c,d], [e,f,g,h]]
def SRL(n): return [list(S()) for i in range(n)]
# n, k = LI()
# s = set()
# for i in range(k):
# l, r = LI()
# s |= set(range(l, r + 1))
# # a = len(s)
# dp = [0] * (n + 1)
# dp[1] = 1
# for i in range(1, n+1):
# for j in range(1, i):
# if i-j in s:
# dp[i] += dp[j] % 998244353
# # dp[i] += dp[j]
# print(dp[n] % 998244353)
# 配るDP
n, k = map(int, input().split())
lr = []
for _ in range(k):
l, r = map(int, input().split())
lr.append((l, r))
mod = 998244353
dp = [0]*(2*n+1)
dp[0] = 1
dp[1] = -1
now = 1
for i in range(n-1):
for l, r in lr:
# 始点に足して終点は引く
# imos法というらしい
dp[i+l] += now
dp[i+r+1] -= now
now += dp[i+1]
now %= mod
print(now)
| # 雰囲気で書いたら通ってしまったがなんで通ったかわからん
# i<jという前提を無視しているのではと感じる
N = int(input())
A = list(map(int, input().split()))
# i<jとして、条件は j-i = A_i + A_j
# i + A_i = j - A_j
dict1 = {}
for i in range(1, N + 1):
tmp = i + A[i - 1]
if tmp not in dict1:
dict1[tmp] = 1
else:
dict1[tmp] += 1
dict2 = {}
for i in range(1, N + 1):
tmp = i - A[i - 1]
if tmp not in dict2:
dict2[tmp] = 1
else:
dict2[tmp] += 1
# print(dict1, dict2)
ans = 0
for k, v in dict1.items():
if k in dict2:
ans += v * dict2[k]
print(ans)
| 0 | null | 14,330,212,864,290 | 74 | 157 |
from collections import deque
from collections import defaultdict
n = int(input())
graph = defaultdict(list)
for i in range(n):
u, k, *v = map(int, input().split())
for v in v:
graph[u].append(v)
ans = [-1] * (n + 1)
q = deque([1])
ans[1] = 0
while q:
c = q.popleft()
for v in graph[c]:
if ans[v] == -1:
ans[v] = ans[c] + 1
q.append(v)
for i in range(1, n + 1):
print(i, ans[i])
| #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 math
import bisect
#
#
#
# pythonで無理なときは、pypyでやると正解するかも!!
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
#
# インデックス系
# int min_y = max(0, i - 2), max_y = min(h - 1, i + 2);
# int min_x = max(0, j - 2), max_x = min(w - 1, j + 2);
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
#mod = 998244353
from sys import stdin
readline = stdin.readline
def readInts():
return list(map(int,readline().split()))
def readTuples():
return tuple(map(int,readline().split()))
def I():
return int(readline())
n,a,b = readInts()
ab = abs(b-a)
if ab%2 == 0:
print(ab//2)
else:
print(min(a-1, n-b) + 1 + (b-a-1)//2)
| 0 | null | 54,480,848,193,926 | 9 | 253 |
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
def main():
H,W,K = map(int, readline().split())
S = [readline().strip() for j in range(H)]
white = []
for line in S:
white.append(line.count("1"))
white_total = sum(white)
if white_total <= K:
print(0)
exit()
ans = 10**5
for i in range(2**(H-1)):
impossible = False
x = 0
ly = bin(i).count("1")
y_sum = [0]*(ly + 1)
j = 0
while j < W:
m = 0
y = [0]*(ly + 1)
for k in range(H):
if S[k][j] == "1":
y[m] += 1
if y[m] > K:
impossible = True
break
y_sum[m] += 1
if y_sum[m] > K:
x += 1
y_sum = [0]*(ly + 1)
j -= 1
break
if (i >> k) & 1:
m += 1
j += 1
if impossible:
x = 10**6
break
ans = min(ans, x + ly)
print(ans)
if __name__ == "__main__":
main()
| while True:
i,e,r = map(int,raw_input().split())
if i == e == r == -1:
break
if i == -1 or e == -1 :
print 'F'
elif i+e >= 80 :
print 'A'
elif i+e >= 65 :
print 'B'
elif i+e >= 50 :
print 'C'
elif i+e >= 30 :
if r >= 50 :
print 'C'
else :
print 'D'
else :
print 'F' | 0 | null | 24,728,721,950,718 | 193 | 57 |
N = int(input())
i = 1
while True:
M = 1000*i
if M >= N:
break
i += 1
ans = M-N
print(ans) | N = int(input())
syo = N//1000
if N%1000 == 0:
print(0)
else:
amari = (syo+1)*1000 - N
print(amari) | 1 | 8,464,593,923,302 | null | 108 | 108 |
#!/usr/bin/python3
cmdvar_numlist=input()
cmdvar_spritd=cmdvar_numlist.split()
D,T,S=list(map(int,cmdvar_spritd))
if D/S<=T:
print("Yes")
else:
print("No") | # 問題:https://atcoder.jp/contests/abc142/tasks/abc142_a
n = int(input())
res = (n+1)//2
res /= n
print(res)
| 0 | null | 90,464,182,231,836 | 81 | 297 |
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) | print(["SAT","FRI","THU","WED","TUE","MON","SUN"].index(input())+1) | 1 | 133,259,689,821,258 | null | 270 | 270 |
x = int(input())
y = x
while y % 360:
y += x
print(y//x) | x = int(input())
j = 0
for i in range(360):
j += x
j %= 360
if j == 0:
print(i+1)
exit() | 1 | 13,216,317,514,868 | null | 125 | 125 |
S = input()
# 高々2^3=8通りなので、全て列挙すればよい
# RRR RRS SRR RSR RSS SRS SSR SSS
ans = 0
if S == 'RRR': ans = 3
elif S == 'SRR' or S == 'RRS': ans = 2
elif S == 'SSS': ans = 0
else: ans = 1
print(ans) | n = input()
ans = 0
temp = 0
for i in range(len(n)):
if n[i] == "R":
temp += 1
ans = max(temp,ans)
else:
temp = 0
print(ans) | 1 | 4,899,697,805,112 | null | 90 | 90 |
N=int(input())
A=list(map(int,input().split()))
L=[[10**10,0]]
for i in range(N):
L.append([A[i],i])
L.sort(reverse=True)
#print(L)
DP=[[0 for i in range(N+1)]for j in range(N+1)]
#j=右に押し込んだ数
for i in range(1,N+1):
for j in range(i+1):
if j==0:
DP[i][j]=DP[i-1][j]+L[i][0]*abs(L[i][1]-(i-1))
#print(DP,DP[i-1][j]+L[i][0]*abs(L[i][1]-(i-1)))
elif j==i:
DP[i][j]=DP[i-1][j-1]+L[i][0]*abs(L[i][1]-(N-j))
#print(DP,DP[i-1][j-1]+L[i][0]*abs(L[i][1]-(N-j)))
else:
DP[i][j]=max(DP[i-1][j]+L[i][0]*abs(L[i][1]-(i-1-j)),DP[i-1][j-1]+L[i][0]*abs(L[i][1]-(N-j)))
#print(DP,DP[i-1][j]+L[i][0]*abs(L[i][1]-(i-1-j)),DP[i-1][j-1]+L[i][0]*abs(L[i][1]-(N-j)))
print(max(DP[-1])) |
class Dice:
def __init__(self, label):
self.d = label
def roll(self, direction):
if direction == "N":
self.d[0], self.d[1], self.d[5], self.d[4] = self.d[1], self.d[5], self.d[4], self.d[0]
elif direction == "S":
self.d[0], self.d[1], self.d[5], self.d[4] = self.d[4], self.d[0], self.d[1], self.d[5]
elif direction == "E":
self.d[0], self.d[2], self.d[5], self.d[3] = self.d[3], self.d[0], self.d[2], self.d[5]
else:
self.d[0], self.d[2], self.d[5], self.d[3] = self.d[2], self.d[5], self.d[3], self.d[0]
label = [int(i) for i in input().split()]
cmd = input()
dice1 = Dice(label)
for i in cmd:
dice1.roll(i)
print(dice1.d[0]) | 0 | null | 16,997,728,034,272 | 171 | 33 |
input()
list = input().split()
list.reverse()
print (' '.join(list)) | A,B,C = list(map(int,input().split(" ")))
print(C,A,B) | 0 | null | 19,368,890,919,710 | 53 | 178 |
S = input()
T = input()
a = len(T)
for i in range(len(S)-len(T)+1):
c = 0
for j, t in enumerate(T):
if S[i+j] != t:
c += 1
a = min(a, c)
print(a) | n=int(input())
N=10**4+20;M=103
a=[0 for _ in range(N)]
for x in range(1,M):
for y in range(1,M):
for z in range(1,M):
res=x*x+y*y+z*z+x*y+y*z+z*x;
if res<N:a[res]+=1
for i in range(n):
print(a[i+1]) | 0 | null | 5,833,273,554,398 | 82 | 106 |
from sys import stdin
import math
s,t = stdin.readline().rstrip().split()
print(t + s) | N,T = map(int,input().split())
A = sorted([list(map(int,input().split())) for _ in range(N)],key=lambda x:x[0])
A.insert(0,[-1,-1])
dp = [[0 for _ in range(T+1)] for _ in range(N)]
for i in range(1,N):
for j in range(1,T+1):
dp[i][j] = dp[i-1][j]
if j>A[i][0]:
dp[i][j] = max(dp[i][j],dp[i-1][j-A[i][0]]+A[i][1])
dmax = 0
for i in range(1,N+1):
dmax = max(dmax,dp[i-1][T]+A[i][1])
print(dmax) | 0 | null | 127,704,267,215,528 | 248 | 282 |
a=int(input(''))
if a==0:
print('1')
else:
print('0') | def main():
import sys
input = sys.stdin.readline
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in range(n)]
ab = [(x,y) for x,y in zip(a,b)]
from operator import itemgetter
ab = sorted(ab,key=itemgetter(0),reverse=True)
dp = [[-10**14]*(n+1) for i in range(n+1)]
dp[0][0] = 0
for i in range(n):
for j in range(i+2):
if j >= 1:
dp[i+1][j] = max(dp[i][j]+ab[i][0]*abs(ab[i][1]-(n-1-(i-j))),dp[i][j-1]+ab[i][0]*abs(ab[i][1]+1-j))
if j == 0:
dp[i+1][0] = dp[i][0] + ab[i][0]*abs(ab[i][1]-(n-1-i))
print(max(dp[n]))
if __name__ == '__main__':
main() | 0 | null | 18,241,584,705,952 | 76 | 171 |
N,K = map(int,input().split())
m = N%K
print(min(m,K-m)) | n = input()
s, t = map(str, input().split())
for i in range(int(n)): print(s[i]+t[i], end = '') | 0 | null | 75,712,003,349,242 | 180 | 255 |
def main():
import math
import collections
N = int(input())
mod = 10**9+7
AB = [tuple(map(int, input().split())) for i in range(N)]
d = collections.defaultdict(set)
i = 0
ans2 = 0
for a, b in AB:
if a == 0 and b == 0:
N -= 1
ans2 += 1
continue
g = math.gcd(a, b)
if g != 0:
a = a // g
b = b // g
d[(a, b)].add(i)
i += 1
ans = 0
r = set()
dd = [1]
for i in range(1, N+1):
dd.append(dd[i-1]*2 % mod)
r = set()
ans = 1
for a, b in d:
if (a, b) in r:
continue
s1 = d[(a, b)]
if (-a, -b) in d:
s1 |= d[(-a, -b)]
l1 = len(s1)
r.add((-a, -b))
if l1:
s2 = set()
if (-b, a) in d:
s2 |= d[(-b, a)]
if (b, -a) in d:
s2 |= d[(b, -a)]
s2 -= s1
l2 = len(s2)
if l2:
ans *= dd[l1] + dd[l2] - 1
N -= l1 + l2
ans %= mod
r.add((-b, a))
r.add((b, -a))
else:
continue
if N:
ans *= dd[N]
ans += ans2
ans -= 1
print(ans % mod)
main()
| n,k,c=map(int,input().split())
s=input()
x,y=[0]*n,[0]*n
work1,work2=[],[]
last=-10**9;cnt=0
for i in range(n):
if s[i]=="o" and i-last>c:
cnt+=1
last=i
work1.append(i)
if cnt==k: break
nextw=10**9;cnt=0
for i in range(n-1,-1,-1):
if s[i]=="o" and nextw-i>c:
work2.append(i)
cnt+=1
nextw=i
if cnt==k: break
work2.reverse()
for i in range(k):
if work1[i]==work2[i]: print(work1[i]+1) | 0 | null | 30,806,667,518,128 | 146 | 182 |
n = int(input())
a = [0] + list(map(int, input().split()))
'''
ans = 1000
for i in range(n - 1):
x = a[i]
y = a[i + 1]
if x < y:
k = ans // x
ans %= x
ans += k * y
print(ans)
'''
dp = [0] * (n + 10)
dp[1] = 1000
for i in range(2, n + 1):
dp[i] = dp[i - 1]
for j in range(1, i):
x = dp[j] // a[j]
y = dp[j] + (a[i] - a[j]) * x
dp[i] = max(dp[i], y)
ans = dp[n]
print(ans)
| import sys
def input(): return sys.stdin.readline().rstrip()
class UnionFind():
def __init__(self, n):
self.n=n
self.parents=[-1]*n # 親(uf.find()で経路圧縮して根)の番号。根の場合は-(そのグループの要素数)
def find(self,x):
#グループの根を返す
if self.parents[x]<0:return x
else:
self.parents[x]=self.find(self.parents[x])
return self.parents[x]
def unite(self,x,y):
#要素x,yのグループを併合
x,y=self.find(x),self.find(y)
if x==y:return
if self.parents[x]>self.parents[y]:#要素数の大きい方をxに
x,y=y,x
self.parents[x]+=self.parents[y]
self.parents[y]=x #要素数が大きい方に併合
def size(self,x):
#xが属するグループの要素数
return -self.parents[self.find(x)]
def same(self,x,y):
#xとyが同じグループ?
return self.find(x)==self.find(y)
def members(self,x):
#xと同じグループに属する要素のリスト
root=self.find(x)
return [i for i in range(self.n) if self.find(i)==root]
def main():
n,m,k=map(int,input().split())
AB=[tuple(map(int,input().split())) for i in range(m)]
CD=[tuple(map(int,input().split())) for i in range(k)]
un=UnionFind(n)
friend=[[] for _ in range(n)]
for a,b in AB:
un.unite(a-1,b-1)
friend[a-1].append(b-1)
friend[b-1].append(a-1)
blockc=[0]*n
for c,d in CD:
if un.same(c-1,d-1):
blockc[c-1]+=1
blockc[d-1]+=1
for i in range(n):
print(un.size(i)-1-blockc[i]-len(friend[i]))
if __name__=='__main__':
main() | 0 | null | 34,422,723,947,904 | 103 | 209 |
S, W = map(int,input().split())
ans = 'unsafe' if S <= W else 'safe'
print(ans) | s,w = [int(x) for x in input().split()]
if s > w:
print("safe")
else:
print("unsafe") | 1 | 29,053,610,358,400 | null | 163 | 163 |
n, m, l = [int(_) for _ in input().split()]
matrix1 = []
for i in range(n):
matrix1.append([int(_) for _ in input().split()])
matrix2 = []
for i in range(m):
matrix2.append([int(_) for _ in input().split()])
for i in range(n):
for j in range(l):
x = 0
for k in range(m):
x += matrix1[i][k] * matrix2[k][j]
print(x, end=" " if j < l - 1 else "\n")
| n, m, l = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(n)]
B = [list(map(int, input().split())) for _ in range(m)]
C = []
B_T = list(zip(*B))
for a_row in A:
C.append([sum(a * b for a, b, in zip(a_row, b_column)) for b_column in B_T])
for row in C:
print(' '.join(map(str, row))) | 1 | 1,456,803,831,830 | null | 60 | 60 |
N, S = int(input()), input()
print("Yes" if N%2 == 0 and S[:(N//2)] == S[(N//2):] else "No") | import os, sys, re, math
N = int(input())
S = input()
if S[:N // 2] == S[N // 2:]:
print('Yes')
else:
print('No')
| 1 | 147,320,470,125,732 | null | 279 | 279 |
n=int(input())
a=list(map(int,input().split()))
res=0
for i in range(n):
res^=a[i]
ans=[]
for i in range(n):
ans.append(res^a[i])
print(*ans) | N=int(input())
*A,=map(int,input().split())
total=0
for i in range(N):
total ^= A[i]
print(*[total^A[i] for i in range(N)]) | 1 | 12,566,114,709,280 | null | 123 | 123 |
a2=list(input())
if a2[0]!=a2[1] and a2[2]==a2[3] and a2[4]==a2[5]:
print('Yes')
else:
print('No') | import statistics
n = int(input())
a = []
b = []
for i in range(n):
ai,bi = map(int, input().split())
a.append(ai)
b.append(bi)
ma = statistics.median(a)
mb = statistics.median(b)
if n % 2 == 1:
ans = mb - ma + 1
else:
ma = int(ma * 2)
mb = int(mb * 2)
ans = mb - ma + 1
print(ans)
| 0 | null | 29,619,217,303,732 | 184 | 137 |
def gcd(a, b):
if a < b:
a, b = b, a
while b != 0:
a, b = b, a % b
return a
print(gcd(*map(int, input().split())))
| import sys
input=sys.stdin.readline
from itertools import accumulate
def main():
n,k=map(int,input().split())
a=list(map(int,input().split()))
k=k if k<50 else 50
for _ in range(k):
b=[0]*(n+1)
for i in range(n):
ai=a[i]
l,r=i-ai,i+ai+1
l=0 if l<0 else l
r=n if r>n else r
b[l]+=1
b[r]-=1
b=tuple(accumulate(b))[:-1]
a=b
if min(set(b))==n:
break
print(*a)
if __name__ == '__main__':
main() | 0 | null | 7,783,771,064,740 | 11 | 132 |
from collections import defaultdict
d = defaultdict(int)
N = int(input())
arr = list(map(int, input().split()))
ans = 0
for i in range(N):
ans += d[i-arr[i]]
d[i+arr[i]] += 1
print(ans) | ri = lambda S: [int(v) for v in S.split()]
def rii(): return ri(input())
N, K = rii()
count = 0
while N:
N //= K
count += 1
print(count)
| 0 | null | 45,300,263,980,712 | 157 | 212 |
h1,m1,h2,m2,k=input().split()
h1 = int(h1)
m1 = int(m1)
h2 = int(h2)
m2 = int(m2)
k = int(k)
if h1 > h2:
h2 += 24
h = h2 - h1
hm = h * 60
print(hm - m1 + m2 - k) | N=int(input())
b=0
for x in range(1,10):
for y in range(1,10):
if x*y==N:
b+=1
break
if b==1:
break
if b==1:
print("Yes")
else:
print("No") | 0 | null | 88,930,661,074,976 | 139 | 287 |
H, W = map(int, input().split())
stage = []
scores = []
for _ in range(H):
stage.append(list(input()))
scores.append([float('inf')] * W)
if stage[0][0] == '#':
scores[0][0] = 1
else:
scores[0][0] = 0
move = [[0, 1], [1, 0]]
for y in range(H):
for x in range(W):
for dx, dy in move:
nx, ny = x + dx, y + dy
if H <= ny or W <= nx:
continue
if stage[ny][nx] == '#' and stage[y][x] == '.':
scores[ny][nx] = min(scores[ny][nx], scores[y][x] + 1)
else:
scores[ny][nx] = min(scores[ny][nx], scores[y][x])
# print(*scores, sep='\n')
print(scores[-1][-1]) | #!/usr/bin/env python3
import sys
from itertools import chain
# form bisect import bisect_left, bisect_right, insort_left, insort_right
# import numpy as np
# 7.n = 7 * 1.n = 101
7777
def solve(K: int):
n = 7
for answer in range(1, K + 1):
if n % K == 0:
return answer
n = 10 * (n % K) + 7
return -1
def main():
tokens = chain(*(line.split() for line in sys.stdin))
# K = map(int, line.split())
K = int(next(tokens)) # type: int
answer = solve(K)
print(answer)
if __name__ == "__main__":
main()
| 0 | null | 27,702,618,263,132 | 194 | 97 |
N=int(input())
se=set(input() for _ in range(N))
print(len(se)) | if __name__ == "__main__":
N = int(input())
hash = {}
for i in range(N):
hash[input()] = ""
print(len(hash.keys())) | 1 | 30,345,750,743,630 | null | 165 | 165 |
import math
inf = math.inf
def merge(A, left, mid, right):
global cnt
L = []
R = []
for i in A[left:mid]:
L.append(i)
for i in A[mid:right]:
R.append(i)
L.append(inf)
R.append(inf)
i = 0
j = 0
for k in range(left, right):
cnt += 1
if L[i] <= R[j]:
A[k] = L[i]
i += 1
else:
A[k] = R[j]
j += 1
def mergeSort(A, left, right):
if right - left > 1:
mid = (left + right) // 2
mergeSort(A, left, mid)
mergeSort(A, mid, right)
merge(A, left, mid, right)
_ =input()
A = list(map(int, input().rstrip().split(" ")))
cnt = 0
mergeSort(A, 0, len(A))
print(" ".join(list(map(str, A))))
print(cnt)
| n_turn = int(input())
t_point = h_point = 0
for i in range(n_turn):
t_card, h_card = input().split()
if t_card > h_card:
t_point += 3
elif t_card == h_card:
t_point += 1
h_point += 1
else:
h_point += 3
print("{} {}".format(t_point, h_point)) | 0 | null | 1,039,687,378,928 | 26 | 67 |
n=int(input())
s,t = map(str, input().split())
print(''.join([s[i] + t[i] for i in range(0,n)])) | from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from itertools import combinations # (string,3) 3回
from collections import deque
from collections import defaultdict
from fractions import gcd
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
def readInts():
return list(map(int,input().split()))
def main():
a,b,m = readInts()
A = readInts()
B = readInts()
ans = float('inf')
for i in range(m):
x,y,c = readInts()
x -=1
y -=1
ans = min(ans,A[x] + B[y] - c)
# Aの商品の中で1番最小のやつを買うだけでもいいかもしれない
# Bの商品の中で1番最小のやつを買うだけでもいいかもしれない
ans = min(ans,min(A) + min(B))
print(ans)
if __name__ == '__main__':
main()
| 0 | null | 82,900,496,383,040 | 255 | 200 |
n = int(input())
if n%2 == 0:
ans = n//2-1
else:
ans = n//2
print(ans) | def solution(N):
ans = 0
for i in range(N-1, int(N/2), -1):
j = N-i
ans+=1
return ans
test_input = input()
test_input = int(test_input)
print(solution(test_input)) | 1 | 153,066,479,515,450 | null | 283 | 283 |
# coding: utf-8
x,y=map(int,input().split())
while not(x==0 and y==0):
if x>y:
x,y=y,x
print(str(x)+" "+str(y))
x,y=map(int,input().split()) | n = int(input())
ans = 0
for a in range (1,n):
for b in range(a, n, a):
ans += 1
print(ans) | 0 | null | 1,542,744,060,928 | 43 | 73 |
class Factorial():
def __init__(self, mod=10**9 + 7):
self.mod = mod
self._factorial = [1]
self._size = 1
self._factorial_inv = [1]
self._size_inv = 1
def __call__(self, n):
return self.fact(n)
def fact(self, n):
''' n! % mod '''
if n >= self.mod:
return 0
self._make(n)
return self._factorial[n]
def _make(self, n):
if n >= self.mod:
n = self.mod
if self._size < n+1:
for i in range(self._size, n+1):
self._factorial.append(self._factorial[i-1]*i % self.mod)
self._size = n+1
def fact_inv(self, n):
''' n!^-1 % mod '''
if n >= self.mod:
raise ValueError('Modinv is not exist! arg={}'.format(n))
if self._size_inv < n+1:
self._factorial_inv += [-1] * (n+1-self._size_inv)
self._size_inv = n+1
if self._factorial_inv[n] == -1:
self._factorial_inv[n] = self.modinv(self.fact(n))
return self._factorial_inv[n]
def _make_inv(self, n, r=2):
if n >= self.mod:
n = self.mod - 1
if self._size_inv < n+1:
self._factorial_inv += [-1] * (n+1-self._size_inv)
self._size_inv = n+1
self._factorial_inv[n] = self.modinv(self.fact(n))
for i in range(n, r, -1):
self._factorial_inv[i-1] = self._factorial_inv[i]*i % self.mod
@staticmethod
def xgcd(a, b):
'''
Return (gcd(a, b), x, y) such that a*x + b*y = gcd(a, b)
'''
x0, x1, y0, y1 = 0, 1, 1, 0
while a != 0:
(q, a), b = divmod(b, a), a
y0, y1 = y1, y0 - q * y1
x0, x1 = x1, x0 - q * x1
return b, x0, y0
def modinv(self, n):
g, x, _ = self.xgcd(n, self.mod)
if g != 1:
raise ValueError('Modinv is not exist! arg={}'.format(n))
return x % self.mod
def comb(self, n, r):
''' nCr % mod '''
if r > n:
return 0
t = self(n)*self.fact_inv(n-r) % self.mod
return t*self.fact_inv(r) % self.mod
def comb_(self, n, r):
'''
nCr % mod
when r is not large and n is too large
'''
c = 1
for i in range(1, r+1):
c *= (n-i+1) * self.fact_inv(i)
c %= self.mod
return c
def comb_with_repetition(self, n, r):
''' nHr % mod '''
t = self(n+r-1)*self.fact_inv(n-1) % self.mod
return t*self.fact_inv(r) % self.mod
def perm(self, n, r):
''' nPr % mod '''
if r > n:
return 0
return self(n)*self.fact_inv(n-r) % self.mod
n, m, k = map(int, input().split())
mod = 998244353
f = Factorial(mod)
f._make_inv(n-1)
comb = f.comb
s = 0
for i in range(k+1, n):
t = comb(n-1, i)*m % mod
t = t*pow(m-1, n-1-i, mod) % mod
s = (s+t) % mod
ans = (pow(m, n, mod)-s) % mod
print(ans) | def xgcd(a, b):
x0, y0, x1, y1 = 1, 0, 0, 1
while b != 0:
q, a, b = a // b, b, a % b
x0, x1 = x1, x0 - q * x1
y0, y1 = y1, y0 - q * y1
return a, x0, y0
def invmod(a, m):
g, x, y = xgcd(a, m)
if g != 1:
raise Exception('modular inverse does not exist')
else:
return x % m
n, m, k = map(int, input().split())
ans = 0
c = 998244353
ans = m * pow(m-1, n-1, mod=c)
temp = 1
for i in range(1, k+1):
temp = temp*(n-i) % c
temp = temp*invmod(i, c) % c
ans += temp * m * pow(m-1, n-1-i, mod=c)
ans = ans % c
print(ans%c) | 1 | 23,246,339,234,552 | null | 151 | 151 |
n, k, c = map(int, input().split())
s = input()
l = []
r = []
res = []
def fun(string, pos):
counter = 0
consec_counter = c + 1
res = []
while pos < n:
# print(pos)
if counter == k:
break
if consec_counter >= c and string[pos] == 'o':
counter += 1
consec_counter = 0
res.append(pos)
else:
consec_counter += 1
pos += 1;
# print(res)
return res
def invfun(string, pos):
counter = 0
consec_counter = c + 1
res = []
while pos >= 0:
if counter == k:
break
# print(pos)
if consec_counter >= c and string[pos] == 'o':
counter += 1
consec_counter = 0
res.append(pos)
else:
consec_counter += 1
pos -= 1;
# print(res)
return res
little_res = fun(s, 0)
lres = []
if len(little_res) == k:
lres = little_res
else:
exit()
little_res = invfun(s, n - 1)
rres = []
if len(little_res) == k:
rres = little_res
else:
exit()
# print(lres)
# print(rres)
for i in range(k):
if lres[i] == rres[k - 1 - i]:
print(lres[i] + 1)
|
def resolve():
N,K,C = map(int,input().split())
S=input()
dpt1=[0]*K
dpt2=[0]*K
pnt = 0
for i in range(K):
while S[pnt]=="x":
pnt+=1
dpt1[i]=pnt
pnt += C+1
pnt = N-1
for i in range(K-1,-1,-1):
while S[pnt] == "x":
pnt -=1
dpt2[i]=pnt
pnt-=C+1
for a,b in zip(dpt1,dpt2):
if a == b:
print(a+1)
if __name__ == "__main__":
resolve() | 1 | 40,485,394,424,118 | null | 182 | 182 |
s,t=map(str,input().split());print(t+s) | if __name__ == "__main__":
n = int(input())
S = list(map(int, input().split()))
q = int(input())
T = list(map(int, input().split()))
ans = 0
for i in T:
if i in S:
ans += 1
print(ans) | 0 | null | 51,819,409,336,896 | 248 | 22 |
import collections
def resolve():
n = int(input())
s = [input() for _ in range(n)]
c = collections.Counter(s)
max_count = c.most_common()[0][1]
ans = [i[0] for i in c.items() if i[1]==max_count]
for i in sorted(ans): print(i)
resolve() | import collections
N,*S = open(0).read().split()
C = collections.Counter(S)
i = C.most_common()[0][1]
print(*sorted([k for k,v in C.most_common() if v == i]), sep='\n') | 1 | 69,825,759,810,858 | null | 218 | 218 |
def factorize(n):
b = 2
fct = []
while b * b <= n:
while n % b == 0:
n //= b
fct.append(b)
b = b + 1
if n > 1:
fct.append(n)
return fct
def getpattern(x):
pattern = 0
d = 0
for i in range(1, 1000000):
d += i
if x < d:
break
pattern += 1
return pattern
n = int(input())
f = factorize(n)
fu = set(factorize(n))
r = 0
for fui in fu:
r += getpattern(f.count(fui))
print(r) | N, K = map(int, input().split())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
def cmb(n, r):
if (r < 0) or (n < r):
return 0
r = min(r, n - r)
return fact[n] * factinv[r] * factinv[n - r] % mod
for i in range(2, N + 1):
fact.append((fact[-1] * i) % mod)
inv.append((-inv[mod % i] * (mod // i)) % mod)
factinv.append((factinv[-1] * inv[-1]) % mod)
A.sort()
ans = 0
for i, (n, x) in enumerate(zip(A, A[::-1])):
if i < K-1:
continue
ans += n * cmb(i, K - 1)
ans %= mod
ans -= x * cmb(i, K - 1)
ans %= mod
print(ans)
| 0 | null | 56,326,745,169,760 | 136 | 242 |
n, m = map(int, raw_input().split())
l = map(int, raw_input().split())
INF = 1<<31
dp = [INF]*(n+1)
dp[0] = 0
for c in l:
for i in range(n-c+1):
if dp[i] != INF:
dp[i + c] = min(dp[i] + 1, dp[i + c])
print dp[n] | n,m = map(int,input().split())
c = [int(i) for i in input().split()]
dp = [[float("INF")]*(n+1) for _ in range(m+1)]
dp[0][0] = 0
for i in range(m):
for yen in range(n+1):
if yen - c[i] < 0: dp[i+1][yen] = dp[i][yen]
else: dp[i+1][yen] = min(dp[i][yen],dp[i+1][yen-c[i]]+1)
print(dp[m][n])
| 1 | 145,253,253,152 | null | 28 | 28 |
n = int(input())
minv = int(input())
maxv = -1000000000
for i in range(1,n):
r = int(input())
m = r-minv
if maxv < m < 0:
maxv = m
minv = r
elif maxv < m:
maxv = m
elif m < 0:
minv = r
print(maxv) | def main():
N = int(input())
price = [int(input()) for i in range(N)]
minv = float("inf")
maxv = -float("inf")
for p in price:
maxv = max(maxv, p-minv)
minv = min(minv, p)
print(maxv)
if __name__ == "__main__":
import os
import sys
if len(sys.argv) > 1:
if sys.argv[1] == "-d":
fd = os.open("input.txt", os.O_RDONLY)
os.dup2(fd, sys.stdin.fileno())
main()
else:
main() | 1 | 13,426,244,260 | null | 13 | 13 |
while True:
H, W = [int(i) for i in input().split()]
if H == W == 0:
break
for i in range(H):
if i % 2 == 0:
for j in range(W):
if j % 2 == 0:
print('#', end='')
else:
print('.', end='')
else:
print('')
else:
for j in range(W):
if j % 2 == 0:
print('.', end='')
else:
print('#', end='')
else:
print('')
print('') | '''
ITP-1_5-C
?????§?????????????????????
??\????????????????????????H cm ?????? W cm ????????§????????????????????¢?????????????????°?????????????????????????????????
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#
?????????????????? 6 cm ?????? 10 cm ???????????¢?????¨??????????????????
????????¢???????????? "#" ??¨????????????????????????????????????
???Input
??\???????????°????????????????????????????§???????????????????????????????????????????????????¢????????\????????¨????????§??????
H W
H, W ?????¨?????? 0 ?????¨????????\?????????????????¨????????????
???Output
?????????????????????????????????????????? H cm ?????? W cm ?????????????????????????????????
?????????????????????????????????????????????????????\??????????????????
'''
# import
import sys
for line in sys.stdin:
# ?????°??????????????????
H, W = map(int, line.split())
if H == 0 and W == 0:
break
# ?????¢??????
for a in range(H):
# ?\???°?????????
if a%2 == 1:
for b in range(W):
if b%2 == 1:
# ??????????????§'#'??????
print('#', end='')
else:
# ??????????????§'.'??????
print('.', end='')
# ???????????????????????????
print('')
# ??¶??°?????????
else:
for c in range(W):
if c%2 == 1:
# ??????????????§'.'??????
print('.', end='')
else:
# ??????????????§'#'??????
print('#', end='')
# ???????????????????????????
print('')
# ????????????????????????
print('') | 1 | 885,648,229,088 | null | 51 | 51 |
a,b=map(int,input().split());print(['safe','unsafe'][a<=b]) | #!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる
mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000
d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))
S, W = LI()
if S <= W:
print("unsafe")
else:
print("safe") | 1 | 29,037,512,430,832 | null | 163 | 163 |
def find_divisors(n):
divisors = set()
div = 1
while div ** 2 <= n:
if n % div == 0:
divisors.add(div)
divisors.add(n // div)
div += 1
return divisors
def main():
n = int(input())
res = 0
for k in find_divisors(n) - {1}:
t = n
while t % k == 0:
t //= k
if t % k == 1:
res += 1
res += len(find_divisors(n - 1)) - 1
print(res)
main()
| import math
N = int(input())
N_d = []
N1_d = []
answer = set()
for i in range(1,math.ceil(N**0.5)+1):
if N%i == 0:
N_d.append(i)
N_d.append(N//i)
for i in range(len(N_d)):
temp = N
while temp % N_d[i] == 0:
temp = temp//N_d[i]
if N_d[i] == 1:
break
if temp % N_d[i] == 1:
answer.add(N_d[i])
for i in range(1,math.ceil((N-1)**0.5)+1):
if (N-1) %i == 0:
answer.add(i)
answer.add((N-1)//i)
answer.remove(1)
print(len(answer)) | 1 | 41,378,591,901,280 | null | 183 | 183 |
def is_p(s):
return s == s[::-1]
s = input()
n = len(s)
l = (n - 1)//2
r = l + 1
print('Yes' if is_p(s) and is_p(s[:l]) and is_p(s[r:]) else 'No') | def is_palindrome(s):
return s == s[::-1]
def main():
s = "-" + input()
n = len(s) - 1
if (
is_palindrome(s[1:])
and is_palindrome(s[1 : (n - 1) // 2 + 1])
and is_palindrome(s[(n + 3) // 2 : n + 1])
):
print("Yes")
else:
print("No")
if __name__ == "__main__":
main()
| 1 | 46,239,809,770,320 | null | 190 | 190 |
# ALDS1_3_B
# coding: utf-8
from collections import deque
n, q = [int(i) for i in raw_input().split()]
d = deque()
for i in range(n):
name, time = raw_input().split()
time = int(time)
d.append([name, time])
total = 0
while len(d) > 0:
p = d.popleft()
if p[1] <= q: # 終わり
total += p[1]
print "{} {}".format(p[0], total)
else:
total += q
p[1] -= q
d.append(p)
| from collections import deque
H, W, K = map(int, input().split())
mp = [input() for _ in range(H)]
rlt = 1010
h = deque([[1]])
while h:
t = h.pop()
if len(t) == H:
t.append(1)
tmp = sum(t) - 2
bs = tmp
lst = [0]*H
j = -1
for i in range(len(t)-1):
if t[i] == 1:
j += 1
lst[i] = j
cnt = [0]*(lst[-1]+1)
for j in range(W):
tcnt = [0]*(lst[-1]+1)
f = 0
for i in range(H):
tcnt[lst[i]] += int(mp[i][j])
for k in range(lst[-1]+1):
if tcnt[k] > K:
f = 2
break
cnt[k] += tcnt[k]
if cnt[k] > K:
f = 1
break
if f == 2:
break
if f == 1:
tmp += 1
for k in range(lst[-1]+1):
cnt[k] = tcnt[k]
if f < 2:
rlt = min(rlt, tmp)
else:
for i in range(2):
h.append(t+[i])
print(rlt) | 0 | null | 24,420,462,658,218 | 19 | 193 |
import numpy as np
from numba import njit
@njit('i8[:](i8,i8,i8[:])')
def solve(N, K, A):
for _ in range(K):
B = np.zeros(N + 1, dtype=np.int64)
for i in range(N):
a = A[i]
B[max(0, i - a)] += 1
B[min(N, i + a + 1)] -= 1
A = np.cumsum(B)[:-1]
if np.all(A == N):
return A
return A
N, K = map(int, input().split())
A = np.array(input().split(), dtype=int)
print(' '.join(map(str, solve(N, K, A)))) | N, K = map(int, input().split())
A = [int(i) for i in input().split()]
for _ in range(K):
flg = True
imos = [0]*(N+1)
for i in range(N):
imos[max(0, i-A[i])] += 1
imos[min(N, i+A[i]+1)] -= 1
A = [0]*N
c = 0
for i in range(N):
c += imos[i]
A[i] = c
if c != N:
flg = False
if flg is True:
print(*([N]*N), sep=' ')
exit()
print(*A)
| 1 | 15,482,496,560,832 | null | 132 | 132 |
f=input
n,s,q=int(f()),list(f()),int(f())
d={chr(97+i):[] for i in range(26)}
for i in range(n):
d[s[i]]+=[i]
from bisect import *
for i in range(q):
a,b,c=f().split()
b=int(b)-1
if a=='1':
if s[b]==c: continue
d[s[b]].pop(bisect(d[s[b]],b)-1)
s[b]=c
insort(d[c],b)
else:
t=0
for l in d.values():
m=bisect(l,b-1)
if m<len(l) and l[m]<int(c): t+=1
print(t) | # -*- coding: utf-8 -*-
import sys
N=input()
S=["None"]+list(raw_input())
Q=input()
query=[ sys.stdin.readline().split() for _ in range(Q) ]
#BIT
bit=[ [ 0 for _ in range(N+1) ] for _ in range(27) ] #1-indexed bit[1]:aのbit, bit[2]:bのbit ...
def add(idx,a,w):
while a<=N:
bit[idx][a]+=w
a+=a&-a
def sum(idx,a):
ret=0
while 0<a:
ret+=bit[idx][a]
a-=a&-a
return ret
for i,x in enumerate(S):
if i==0: continue
add(ord(x)-96,i,1) #a~zのどのbit配列か,特定文字のbit配列の何番目,bitに代入する値
for q1,q2,q3 in query:
q1=int(q1)
q2=int(q2)
if q1==1:
before_q3=S[q2] #文字列Sの変更前の文字をbefore_q3に代入
S[q2]=q3 #文字列Sの変更部分1文字を更新
q3=ord(q3)-96 #変更後の文字をアスキーコード10進数で表現
before_q3=ord(before_q3)-96 #変更前の文字をアスキーコード10進数で表現
add(q3,q2,1) #a~zのどのbit配列か,特定文字のbit配列の何番目,bitに代入する値
add(before_q3,q2,-1) #置き代えた文字は-1しなきゃいけない
elif q1==2:
q3=int(q3)
cnt=0
for i in range(1,27):
if 0<sum(i,q3)-sum(i,q2-1):
cnt+=1
print cnt | 1 | 62,654,811,235,680 | null | 210 | 210 |
s = input()
n = len(s) + 1
A = [0] * n
for i in range(1, n):
if s[i - 1] == "<":
A[i] = A[i - 1] + 1
for i in range(n - 1, 0, -1):
if s[i - 1] == ">":
A[i - 1] = max(A[i - 1], A[i] + 1)
print(sum(A))
| S = input()
N = len(S)
A = [-1 for i in range(N+1)]
if S[0] == "<":
A[0] = 0
if S[-1] == ">":
A[-1] = 0
for i in range(N):
if S[i:i+2] == "><":
A[i+1] = 0
#print(A)
for i in range(N):
if S[i] == "<":
A[i+1] = max(A[i]+1, A[i+1])
for i in range(N):
if S[N-i-1] == ">":
A[N-i-1] = max(A[N-i]+1, A[N-i-1])
#print(A)
print(sum(A)) | 1 | 156,092,273,480,220 | null | 285 | 285 |
import sys
N,K = map(int,input().split())
array = [ a for a in range(1,N+1) ]
for I in range(K):
_ = input()
sweets = list(map(int,input().split()))
for K in sweets:
if K in array:
array.remove(K)
print(len(array)) | N = int(input())
S = [str(input()) for _ in range(N)]
S = set(S)
print(len(S)) | 0 | null | 27,357,672,057,520 | 154 | 165 |
a = input('')
S = []
num = 0
for i in a:
S.append(i)
for j in range(3):
if S[j] == 'R':
num += 1
if num == 2 and S[1] == 'S':
print(1)
else:
print(num) | S = input()[0:3]
i=S.count('RRR')
j=S.count('RR')
k=S.count('R')
l=S.count('RSR')
print(max(i,j,k-l)) | 1 | 4,885,392,942,680 | null | 90 | 90 |
def resolve():
N = int(input())
Z = []
W = []
for i in range(N):
x, y = map(int, input().split())
Z.append(x + y)
W.append(x - y)
a = max(Z) - min(Z)
b = max(W) - min(W)
print(max(a, b))
if __name__ == "__main__":
resolve() | N = int(input())
p = [list(map(int,input().split())) for _ in range(N)]
z = []
w = []
for i in range(N):
z.append(p[i][0] + p[i][1])
w.append(p[i][0] - p[i][1])
print(max(max(z)-min(z),max(w)-min(w))) | 1 | 3,435,941,438,420 | null | 80 | 80 |
int(input())
s = list(input())
ans = 0
for i in range(10):
if str(i) in s:
ss = s[s.index(str(i))+1:]
for j in range(10):
if str(j) in ss:
sss = ss[ss.index(str(j))+1:]
for k in range(10):
if str(k) in sss:
ans += 1
print(ans) | N = int(input())
A = list(map(int, input().split()))
for a in A:
if a%2==0 and not (a%3==0 or a%5==0):
print("DENIED")
exit()
print("APPROVED")
| 0 | null | 98,845,134,057,120 | 267 | 217 |
#%%time
from itertools import accumulate
import numpy as np
from numba import njit
n, k = map(int, input().split())
a = np.array(list(map(int, input().split())), dtype=np.int64)
@njit
def loop1(a):
#b = [0]*(n+1)
b = np.zeros(n+1, dtype=np.int64)
for i in range(n):
l = max(0, i-a[i])
r = min(i+a[i]+1, n)
b[l] += 1
if r <= n-1: b[r] -= 1
b = np.cumsum(b)[:-1]
return b
#a = list(accumulate(b))
#a = a[:-1]
#if np.all(a == n):
# break
for q in range(min(42, k)):
a = loop1(a)
print(*a) | N, x, M = [int(a) for a in input().split()]
# Let g(i) = x ^ (2^i) mod M
g = [x] + [0] * M
ginv = [-1] * (M + 1)
ginv[x] = 0
partSum = [x] + [0] * (M + 1) # partSum[-1] = 0 となるようにダミーを追加
for i in range(1, M + 1):
g[i] = g[i - 1] * g[i - 1] % M
if ginv[g[i]] >= 0: # detected cycle
iStart = ginv[g[i]]
cycleSum = partSum[i - 1] - partSum[iStart - 1]
cycleLen = i - iStart
numCycle = (N - 1 - (i - 1)) // cycleLen
remCycle = (N - 1 - (i - 1)) % cycleLen
ans = partSum[i - 1] + cycleSum * numCycle + partSum[iStart + remCycle - 1] - partSum[iStart - 1]
break
else:
ginv[g[i]] = i
partSum[i] = partSum[i - 1] + g[i]
print(ans) | 0 | null | 9,161,159,611,620 | 132 | 75 |
while True:
h,w = [int(s) for s in input().split(' ')]
if h==0 and w==0:
break
else:
top_bottom = '#'*w + '\n'
low = '#' + '.'*(w-2) + '#\n'
rectangle = top_bottom + low*(h-2) + top_bottom
print(rectangle) | #coding:utf-8
#1_5_B 2015.4.1
while True:
length,width = map(int,input().split())
if (length,width) == (0,0):
break
for i in range(length):
for j in range(width):
if j == width - 1:
print('#')
elif i == 0 or i == length - 1 or j == 0:
print('#', end='')
else:
print('.', end='')
print() | 1 | 834,209,472,072 | null | 50 | 50 |
A, B, C, K = [int(_) for _ in input().split()]
print(K if K <= A else A if K <= A + B else A - (K - A - B))
| a,b,c,k=map(int,input().split())
ans=a
if a>k: ans=k
if a+b<k: ans-=(k-(a+b))
print(ans)
| 1 | 21,926,571,725,220 | null | 148 | 148 |
s = input()
if len(list(set(s))) == 1:
print('No')
else:
print('Yes') | S = str(input())
if S != 'AAA' and S != 'BBB':
print('Yes')
else:
print('No') | 1 | 54,871,902,585,642 | null | 201 | 201 |
n, k = (int(x) for x in input().split())
A = list(int(x) for x in input().split())
MOD = 10**9 + 7
A.sort()
l = 0
r = n - 1
sign = 1 # 1 or -1
ans = 1
if k % 2 == 1:
ans = A[r]
r -= 1
k -= 1
if ans < 0:
sign = -1
while k:
x = A[l] * A[l + 1]
y = A[r] * A[r - 1]
if x * sign > y * sign:
ans *= x % MOD
ans %= MOD
l += 2
else:
ans *= y % MOD
ans %= MOD
r -= 2
k -= 2
print(ans) | import sys
input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def resolve():
N = I()
fact = []
for i in range(1, int(N ** 0.5) + 1):
if N % i == 0:
fact.append((i, N // i))
print(sum(fact[-1]) - 2)
if __name__ == '__main__':
resolve()
| 0 | null | 85,399,752,310,420 | 112 | 288 |
# ALDS1_5_B Merge Sort
import sys
count = 0
def merge(A, left, mid, right):
global count
l = A[left: mid]
r = A[mid: right]
l.append(float('inf'))
r.append(float('inf'))
i = 0
j = 0
for k in range(left, right, 1):
count += 1
if l[i] <= r[j]:
A[k] = l[i]
i += 1
else:
A[k] = r[j]
j += 1
def merge_sort(A, left, right):
if left + 1 < right:
mid = (left + right) // 2
merge_sort(A, left, mid)
merge_sort(A, mid, right)
merge(A, left, mid, right)
n = int(input())
S = [int(i) for i in sys.stdin.readline().strip().split()]
merge_sort(S, 0, n)
print(' '.join(map(str, S)))
print(count)
| #!/usr/bin/env python
class PrimeFactor():
def __init__(self, n):
'''
Note:
make the table of Eratosthenes' sieve
and minFactor[] for fast_factorization.
'''
self.n = n
self.table = [True for _ in range(n+1)]
self.table[0] = self.table[1] = False
self.minFactor = [-1 for _ in range(n+1)]
for i in range(2, n+1):
if not self.table[i]: continue
self.minFactor[i] = i
for j in range(i*i, n+1, i):
self.table[j] = False
self.minFactor[j] = i
def fast_factorization(self, n):
'''
Note:
factorization
return the form of {factor: num}.
'''
data = {}
while n > 1:
if self.minFactor[n] not in data:
data[self.minFactor[n]] = 1
else:
data[self.minFactor[n]] += 1
n //= self.minFactor[n]
return data
def count_divisors(self, n):
'''
Note:
return the number of divisors of n.
'''
ret = 1
for v in self.fast_factorization(n).values():
ret *= (v+1)
return ret
n = int(input())
ans = 0
a = PrimeFactor(n)
for i in range(1, n):
ans += a.count_divisors(i)
print(ans)
| 0 | null | 1,324,792,094,970 | 26 | 73 |
ret = []
while True:
n, x = map(int, raw_input().split())
num_arr = [i for i in range(1, n+1)]
if (n, x) == (0, 0):
break
cnt = 0
for i in range(n, 0, -1):
for j in range(i -1, 0, -1):
if 0 < x - i - j < j:
cnt += 1
ret += [cnt]
for i in ret:
print i | while True:
n,x = list(map(int, input().split()))
if n == x == 0:
break
num_set = []
for i in range(1,n+1):
for j in range(1,n+1):
for k in range(1,n+1):
if i < j < k and i+j+k ==x:
li = [i,j,k]
if not li in num_set:
num_set.append(li)
print(len(num_set)) | 1 | 1,305,093,287,828 | null | 58 | 58 |
a,b,c=map(int,input().split())
k=int(input())
f=0
for i in range(1,k+1):
d=c*(2**i)
e=b*(2**(k-i))
if a<e<d:
f=1
print('Yes' if f else 'No') | a, b, c = map(int, input().split())
k = int(input())
p = "No"
for i in range(k):
if not(a < b and b < c):
if b <= a:
b = b * 2
else:
c = c * 2
if a < b and b < c:
p = "Yes"
print(p) | 1 | 6,946,396,635,072 | null | 101 | 101 |
N,K=list(map(int, input().split()))
A=[0]*(N+1)
for _ in range(K):
k=int(input())
a=list(map(int, input().split()))
for i in a:
A[i]+=1
c=0
for i in range(1,N+1):
c+=not A[i]
print(c) | mod = 1000000007
fact = []
fact_inv = []
pow_ = []
pow25_ = []
pow26_ = []
def pow_ini(nn):
global pow_
pow_.append(nn)
for j in range(62):
nxt = pow_[j] * pow_[j]
nxt %= mod
pow_.append(nxt)
return
def pow_ini25(nn):
global pow25_
pow25_.append(nn)
for j in range(62):
nxt = pow25_[j] * pow25_[j]
nxt %= mod
pow25_.append(nxt)
return
def pow_ini26(nn):
global pow26_
pow26_.append(nn)
for j in range(62):
nxt = pow26_[j] * pow26_[j]
nxt %= mod
pow26_.append(nxt)
return
def pow1(k):
ansk = 1
for ntmp in range(62):
if((k >> ntmp) % 2 == 1):
ansk *= pow_[ntmp]
ansk %= mod
return ansk
def pow25(k):
ansk = 1
for ntmp in range(31):
if((k >> ntmp) % 2 == 1):
ansk *= pow25_[ntmp]
ansk %= mod
return ansk
def pow26(k):
ansk = 1
for ntmp in range(31):
if((k >> ntmp) % 2 == 1):
ansk *= pow26_[ntmp]
ansk %= mod
return ansk
def fact_ini(n):
global fact
global fact_inv
global pow_
for i in range(n + 1):
fact.append(0)
fact_inv.append(0)
fact[0] = 1
for i in range(1, n + 1, 1):
fact_tmp = fact[i - 1] * i
fact_tmp %= mod
fact[i] = fact_tmp
pow_ini(fact[n])
fact_inv[n] = pow1(mod - 2)
for i in range(n - 1, -1, -1):
fact_inv[i] = fact_inv[i + 1] * (i + 1)
fact_inv[i] %= mod
return
def nCm(n, m):
assert(m >= 0)
assert(n >= m)
ans = fact[n] * fact_inv[m]
ans %= mod
ans *= fact_inv[n - m]
ans %= mod
return ans;
fact_ini(2200000)
pow_ini25(25)
pow_ini26(26)
K = int(input())
S = input()
N = len(S)
ans = 0
for k in range(0, K+1, 1):
n = N + K - k
anstmp = 1
anstmp *= pow26(k)
anstmp %= mod
anstmp *= nCm(n - 1, N - 1)
anstmp %= mod
anstmp *= pow25(n - N)
anstmp %= mod
ans += anstmp
ans %= mod
print(ans) | 0 | null | 18,677,322,891,072 | 154 | 124 |
import math
r = input()
l = 2 * math.pi * float(r)
s = math.pi * float(r) * float(r)
print("%f %f" % (s, l)) | s = str(input())
count = 0
for i, j in zip(s, s[::-1]):
if i != j:
count += 1
else:
pass
print(count//2) | 0 | null | 60,528,210,954,790 | 46 | 261 |
import sys
BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001
NUM = 20005
table = [""]*NUM
while True:
T = str(input())
if T == '-':
break
left = 0
right = 0
#長い配列にパターン文字列を転記
for i in range(len(T)):
# print(T[right])
table[left+right] = T[right]
right += 1
num_suffle = int(input())
for loop in range(num_suffle):
pick_num = int(input())
#先頭からpick_num文字を末尾に転記し、論理削除
for i in range(pick_num):
table[right+i] = table[left+i]
left += pick_num
right += pick_num
for i in range(left,right):
print("%s"%(table[i]),end = "")
print()
| ss = str(input())
while (ss != '-'):
slist = []
for s in ss:
slist.append(s)
m = int(input())
for i in range(m):
n = int(input())
for k in range(n):
slist.append(slist[k])
for k in range(n):
del slist[0]
for i in range(len(slist)):
print(slist[i], end = '')
print()
ss = str(input())
| 1 | 1,898,431,518,670 | null | 66 | 66 |
A = int(input())
B = int(input())
if A == 1 and B == 2:
print(3)
elif A == 2 and B == 1:
print(3)
elif A == 1 and B == 3:
print(2)
elif A == 3 and B == 1:
print(2)
elif A == 2 and A == 3:
print(1)
else:
print(1) | n, k = map(int, input().split())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7
negs = [-x for x in A if x < 0]
non_negs = [x for x in A if x >= 0]
def inv(a):
return pow(a, mod - 2, mod)
# 負の数を選ばざるを得ない数を考える
# 選べる負の数の個数の最小
n_min = k - (max(n - len(negs), 0))
# 選べる負の数の個数の最大
n_max = min(n, k)
# この2数が一致していて、更に奇数のときにのみ負になる。(はず)
if n_max == n_min and n_max % 2 == 1:
negs.sort()
non_negs.sort()
cnt = 0
ans = 1
n_idx, nn_idx = 0, 0
while cnt < k:
if n_idx == len(negs):
ans *= non_negs[nn_idx]
nn_idx += 1
elif nn_idx == len(non_negs):
ans *= -negs[n_idx]
n_idx += 1
else:
if non_negs[nn_idx] < negs[n_idx]:
ans *= non_negs[nn_idx]
nn_idx += 1
else:
ans *= -negs[n_idx]
n_idx += 1
ans %= mod
cnt += 1
print(ans % mod)
exit()
negs.sort(reverse=True)
non_negs.sort(reverse=True)
cnt = 0
ans = 1
neg_cnt = 0
n_idx, nn_idx = 0, 0
# idx番号と配列の長さを比較して、その配列から取得可能かをみる
while cnt < k:
if n_idx == len(negs):
ans *= non_negs[nn_idx]
nn_idx += 1
elif nn_idx == len(non_negs):
ans *= -negs[n_idx]
n_idx += 1
neg_cnt += 1
else:
if non_negs[nn_idx] > negs[n_idx]:
ans *= non_negs[nn_idx]
nn_idx += 1
else:
ans *= -negs[n_idx]
n_idx += 1
neg_cnt += 1
ans %= mod
cnt += 1
# 動かす余地がない場合→negが偶数個or動かす余裕がない
if neg_cnt % 2 == 0 or n == k:
# REからここがおかしそう…
print(ans % mod)
exit()
else:
# 次にかけるのはnegs[n_idx] or non_negs[nn_idx]
# 一つ戻すならnegs[n_idx-1] or non_negs[nn_idx-1]
# negsが右にずらせるかどうか
if n_idx == len(negs) or nn_idx == 0:
ans *= inv(-negs[n_idx - 1])
ans *= non_negs[nn_idx]
ans %= mod
# non_negsが右にずらせるか
elif nn_idx == len(non_negs) or n_idx == 0:
ans *= inv(non_negs[nn_idx - 1])
ans *= -negs[n_idx]
ans %= mod
else:
# 両方ともずらせないのはすでに除外済み(のはず)
if non_negs[nn_idx] * non_negs[nn_idx - 1] > negs[n_idx] * negs[n_idx - 1]:
ans *= non_negs[nn_idx]
ans *= inv(-negs[n_idx - 1])
ans %= mod
else:
ans *= -negs[n_idx]
ans *= inv(non_negs[nn_idx - 1])
ans %= mod
print(ans % mod)
exit()
| 0 | null | 60,096,967,870,038 | 254 | 112 |
n = int(input())
xy = [list(map(int, input().split())) for _i in range(n)]
x = [i+j for i, j in xy]
y = [i-j for i, j in xy]
x.sort()
y.sort()
print(max(abs(x[0]-x[-1]), abs(y[0]-y[-1]))) | N = int(input())
S = []
for _ in range(N):
S.append(input())
ans = set(S)
print(len(ans)) | 0 | null | 16,723,895,474,638 | 80 | 165 |
import collections
n = int(input())
graph = [tuple(map(int, input().split())) for _ in range(n - 1)]
tree = [[] for _ in range(n)]
deg = [0] * n
color = {}
for a, b in graph:
a, b = min(a - 1, b - 1), max(a - 1, b - 1)
deg[a] += 1
deg[b] += 1
tree[a].append(b)
tree[b].append(a)
color[(a, b)] = 0
color_max = max(deg)
print(color_max)
c = [0] * n
c[0] = -1
que = collections.deque([0])
while len(que) != 0:
i = que.popleft()
tmp = 1
for j in tree[i]:
if c[j] != 0: continue
a, b = min(i, j), max(i, j)
if tmp == c[i]: tmp += 1
color[(a, b)] = tmp
c[j] = tmp
que.append(j)
tmp += 1
for a, b in graph:
a, b = min(a - 1, b - 1), max(a - 1, b - 1)
print(color[(a, b)])
| n = int(input())
AB = [[] * 2 for _ in range(n - 1)]
graph = [[] for _ in range(n + 1)]
for i in range(n - 1):
a, b = map(int, input().split())
AB[i].append(a)
AB[i].append(b)
graph[a].append(b)
graph[b].append(a)
root = 1
parent = [0] * (n + 1)
order = []
stack = [root] # 根を stack に入れる
while stack:
x = stack.pop()
order.append(x)
for y in graph[x]:
if y == parent[x]:
continue
parent[y] = x
stack.append(y)
color = [-1] * (n + 1)
for x in order:
ng = color[x]
c = 1
for y in graph[x]:
if y == parent[x]:
continue
if c == ng:
c += 1
color[y] = c
c += 1
res = []
for a, b in AB:
if parent[a] == b:
res.append(color[a])
else:
res.append(color[b])
print(max(res)) # root
print('\n'.join(map(str, res))) | 1 | 135,664,942,892,902 | null | 272 | 272 |
def BFS(x):
cnt=1
v=deque([x])
k=0
zu[x]=k
lis=[x]
roots[x]=x
while len(v)>0:
a=v.popleft()
roots[a]=x
for i in fr[a]:
if zu[i]==-1:
zu[i]=zu[a]+1
v.append(i)
cnt += 1
lis.append(i)
return lis
##############################
from collections import deque
N,M,K=map(int,input().split())
A=[0]*M
B=[0]*M
C=[0]*K
D=[0]*K
fr=[ [] for _ in range(N)]
bl=[ set() for _ in range(N)]
zu=[ -1 for _ in range(N)] #訪問済みリスト
roots= [ -1 for _ in range(N)]
for i in range(M):
A[i],B[i]= map(int,input().split())
fr[A[i]-1].append(B[i]-1)
fr[B[i]-1].append(A[i]-1)
for k in range(K):
C[k],D[k]= map(int,input().split())
bl[C[k]-1].add(D[k]-1)
bl[D[k]-1].add(C[k]-1)
fr_set= [set(fr[i]) for i in range(N)]
size=[0]*N
pena=[0]*N
for i in range(N):
if zu[i]==-1:
kumi=BFS(i) #リスト
for k in range(len(kumi)):
size[kumi[k]]=len(kumi)
ans=[0 for _ in range(N)]
for i in range(N):
ans[i]= size[i]-1 - len(fr[i])
for j in bl[i]:
if roots[j]==roots[i]:
ans[i] -= 1
print(*ans)
| import sys,collections
input = sys.stdin.readline
def main():
N,M,K = list(map(int,input().split()))
fr = [[] for i in range(N+1)]
bl = [[] for i in range(N+1)]
for _ in range(M):
a,b = map(int,input().split())
fr[a].append(b)
fr[b].append(a)
for _ in range(K):
c,d = map(int,input().split())
bl[c].append(d)
bl[d].append(c)
fl = [-1 for i in range(N+1)]
fl[0] = 0
fl[1] = 1
nxt = 1
dic = {}
s = set(range(1,N+1))
while nxt != 0:
q = collections.deque([nxt])
fl[nxt]=nxt
cnt = 0
while q:
now = q.popleft()
s.discard(now)
for f in fr[now]:
if fl[f] == -1:
fl[f] = nxt
cnt +=1
q.append(f)
dic[nxt] = cnt
#try:
#nxt = fl.index(-1)
#except:
#nxt = 0
if len(s) == 0:
nxt = 0
else:
nxt = s.pop()
mbf = collections.deque()
for i in range(1,N+1):
ff = 0
for j in bl[i]:
if fl[j] == fl[i]:
ff -= 1
ff += dic[fl[i]]-len(fr[i])
mbf.append(ff)
print(*mbf)
if __name__ == '__main__':
main()
| 1 | 61,446,280,106,160 | null | 209 | 209 |
n = int(input())
R = []
for i in range(n):
R.append(int(input()))
# R???i???????????§???????°???????i?????????????´???¨???????????????list
mins = [R[0]]
for i in range(1, n):
mins.append(min(R[i], mins[i-1]))
# ???????????????j????????????????????\??????????°??????¨??????????±?????????????????????§???????±????????????????
mx = - 10 ** 12
for j in range(1, n):
mx = max(mx, R[j] - mins[j-1])
print(mx) | a,b = map(int,input().split())
if a<=b:
[print(a,end="") for _ in range(b)]
else:
[print(b,end="") for _ in range(a)]
| 0 | null | 42,088,894,019,330 | 13 | 232 |
import math
from collections import defaultdict
ml=lambda:map(float,input().split())
ll=lambda:list(map(int,input().split()))
ii=lambda:int(input())
ip=lambda:list(input())
"""========main code==============="""
a,b,c=ml()
if(a+b+c>=22):
print("bust")
else:
print("win") | s=input()
if len(s)%2==0:
for i in range(len(s)//2):
if s[2*i:2*i+2]=="hi":
continue
print("No")
break
else:
print("Yes")
else:
print("No") | 0 | null | 86,036,478,046,430 | 260 | 199 |
def gcd(a,b):
if a == 0:
return b
return gcd(b % a, a)
def lcm(a,b):
return (a*b) / gcd(a,b)
if __name__ == "__main__":
n1,n2=map(int,input().split(' '))
print(int(lcm(n1,n2))) | def gcd(a, b):
for i in range(min(a, b), 0, -1):
if a%i == 0 and b%i == 0:
return i
A, B = map(int, input().split())
print((A*B) // gcd(A, B)) | 1 | 113,357,378,215,550 | null | 256 | 256 |
A, B, C, D = map(int, input().split())
while True:
# 高橋
C -= B
if C <= 0:
print('Yes')
break
# 青木
A -= D
if A <= 0:
print('No')
break | def main():
a, b, c, d = map(int, input().split())
if c % b == 0:
e = c // b
else:
e = c // b + 1
if a % d == 0:
f = a // d
else:
f = a // d + 1
if e > f:
ans = "No"
else:
ans = "Yes"
print(ans)
if __name__ == "__main__":
main()
| 1 | 29,716,260,666,176 | null | 164 | 164 |
x=float(input())
p=2*3.14*x
A="dlfnsdfjsdjfsdfjsdfnksdkjnfjksndfkjnskjfkjsdkjfnskjdfknsdkjfskdnfkjsdn"
print(p) | print(int(input())*3.1415*2) | 1 | 31,562,296,734,078 | null | 167 | 167 |
from sys import exit
x, n = map(int, input().split())
p = list(map(int, input().split()))
for d in range(x+1): #0に達した時点で0を出力して終了するので、大きい側は気にしなくてよい
for s in [-1, 1]:
a = x + d*s
if p.count(a) == 0:
print(a)
exit() | import math
a,b,c,d=map(float,input().split())
x=math.sqrt((a-c)**2 + (b-d)**2)
print(round(x,8))
#round(f,6)でfを小数点以下6桁にまとめる。
| 0 | null | 7,127,136,955,118 | 128 | 29 |
from collections import deque
n, m = map(int, input().split())
way = {}
for i in range(m):
a, b = map(int, input().split())
if a not in way:
way[a] = []
if b not in way:
way[b] = []
way[a].append(b)
way[b].append(a)
queue = deque([1])
ok = set([1])
ans = [0] * (n+1)
while queue:
now = queue.popleft()
for i in way[now]:
if i in ok:
continue
ans[i] = now
queue.append(i)
ok.add(i)
print("Yes")
for i in range(2, n+1):
print(ans[i])
| def bingo():
# 初期処理
A = list()
b = list()
comb_list = list()
is_bingo = False
# 入力
for _ in range(3):
dummy = list(map(int, input().split()))
A.append(dummy)
N = int(input())
for _ in range(N):
dummy = int(input())
b.append(dummy)
# ビンゴになる組み合わせ
for i in range(3):
# 横
comb_list.append([A[i][0], A[i][1], A[i][2]])
# 縦
comb_list.append([A[0][i], A[1][i], A[2][i]])
# 斜め
comb_list.append([A[0][0], A[1][1], A[2][2]])
comb_list.append([A[0][2], A[1][1], A[2][0]])
# 集計(出現した数字をnullに置き換え)
for i in b:
for j in range(8):
for k in range(3):
if i == comb_list[j][k]:
comb_list[j][k] = 'null'
# すべてnullのリストの存否
for i in comb_list:
for j in i:
if 'null' != j:
is_bingo = False
break
else:
is_bingo = True
# 一組でもbingoがあれば即座に関数を抜ける
if is_bingo == True:
return 'Yes'
# すべてのリストを確認後
if is_bingo == False:
return 'No'
result = bingo()
print(result)
| 0 | null | 39,991,338,322,772 | 145 | 207 |
S = input()
n = int(input())
for i in range(n):
q = input().split()
q[1] = int(q[1])
q[2] = int(q[2])
if q[0] == "print":
print(S[q[1]:q[2] + 1])
elif q[0] == "reverse":
if q[1] == 0:
S = S[:q[1]] + S[q[2]::-1] + S[q[2] + 1:]
else:
S = S[:q[1]] + S[q[2]:q[1] - 1:-1] + S[q[2] + 1:]
elif q[0] == "replace":
S = S[:q[1]] + q[3] + S[q[2] + 1:] | #coding: utf-8
#itp1_9d
def rev(s,a,b):
b=b+1
t=s[a:b]
u=t[::-1]
x=s[:a]
y=s[b:]
return x+u+y
def rep(s,a,b,w):
b=b+1
x=s[:a]
y=s[b:]
return x+w+y
s=raw_input()
n=int(raw_input())
for i in xrange(n):
d=raw_input().split()
a=int(d[1])
b=int(d[2])
if d[0]=="print":
print s[a:b+1]
elif d[0]=="reverse":
s=rev(s,a,b)
elif d[0]=="replace":
w=d[3]
s=rep(s,a,b,w) | 1 | 2,104,493,611,780 | null | 68 | 68 |
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)
def solve():
N, X, M = map(int, input().split())
if N == 1:
print(X)
return
A = X
steps = [0] * M
sms = [0] * M
steps[A] = 1
sms[A] = A
sm = A
ans = A
rest = 0
for i in range(2, N+1):
A = (A*A) % M
sm += A
ans += A
if steps[A] != 0:
P = i - steps[A]
v = sm - sms[A]
rest = N - i
ans += v * (rest//P)
rest %= P
break
steps[A] = i
sms[A] = sm
for i in range(rest):
A = (A*A) % M
ans += A
print(ans)
solve()
| n, x, m = map(int, input().split())
amr = []
for i in range(n):
if i == 0:
x %= m
amr.append(x)
continue
x = pow(x, 2, m)
if x in amr:
break
if x == 0:
break
amr.append(x)
if x == 0:
print(sum(amr[:n]))
else:
idx = amr.index(x)
loop = sum(amr[idx:])
l = len(amr[idx:])
if n >= idx:
cnt = (n-idx)//l
print(cnt*loop + sum(amr[:idx]) + sum(amr[idx:idx+n-idx-cnt*l:]))
else:
print(sum(amr[:n]))
| 1 | 2,768,291,571,100 | null | 75 | 75 |
import sys
input = lambda: sys.stdin.readline().rstrip()
def solve():
n = len(input())
ans = 'x' * n
print(ans)
if __name__ == '__main__':
solve()
| #!/usr/bin/env python3
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def main():
S = readline().rstrip().decode()
print('x' * len(S))
if __name__ == '__main__':
main()
| 1 | 72,641,358,799,842 | null | 221 | 221 |
s, t = map(str, input().split())
print('{}{}'.format(t, s)) |
S, T = input().split()
print(T, S, sep='')
| 1 | 102,937,270,685,330 | null | 248 | 248 |
from collections import deque
N,M=map(int,input().split())
G=[[] for _ in range(N)]
for _ in range(M):
a,b=map(int,input().split())
a-=1
b-=1
G[a].append(b)
G[b].append(a)
group=[]
seen=[0]*N
ans=0
for i in range(N):
if seen[i]: continue
que=deque()
seen[i]=1
cnt=0
que.append(i)
while que:
cnt+=1
v=que.popleft()
for nv in G[v]:
if seen[nv]: continue
seen[nv]=1
que.append(nv)
ans=max(ans,cnt)
print(ans)
| #
# ⋀_⋀
# (・ω・)
# ./ U ∽ U\
# │* 合 *│
# │* 格 *│
# │* 祈 *│
# │* 願 *│
# │* *│
#  ̄
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないyp
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A):
for a in A: print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
# UnionFind
class UnionFind():
def __init__(self, n):
self.nodes=[-1] * n # nodes[x]: 負なら、絶対値が木の要素数
def get_root(self, x):
# nodes[x]が負ならxが根
if self.nodes[x] < 0:
return x
# 根に直接つなぎ直しつつ、親を再帰的に探す
else:
self.nodes[x]=self.get_root(self.nodes[x])
return self.nodes[x]
def unite(self, x, y):
root_x=self.get_root(x)
root_y=self.get_root(y)
# 根が同じなら変わらない
# if root_x == root_y:
# pass
if root_x != root_y:
# 大きい木の方につないだほうが計算量が減る
if self.nodes[root_x] < self.nodes[root_y]:
big_root=root_x
small_root=root_y
else:
small_root=root_x
big_root=root_y
self.nodes[big_root] += self.nodes[small_root]
self.nodes[small_root]=big_root
def main():
N,M = MI()
uf=UnionFind(N)
get_root, unite, nodes=uf.get_root, uf.unite, uf.nodes
for _ in range(M):
A,B = MI()
unite(A-1,B-1)
ans = 0
for i in range(N):
ans = max(ans, -nodes[get_root(i)])
print(ans)
if __name__ == '__main__':
main() | 1 | 3,939,209,313,910 | null | 84 | 84 |
n = int(input())
d = {}
for i in range(n):
a = input()
if a not in d:
d[a] = 1
else:
d[a] += 1
dsorted = sorted(d.items(), key=lambda x:x[1], reverse=True)
name = []
cnt = dsorted[0][1]
for i in dsorted:
if i[1] == cnt:
name.append(i[0])
else:
break
name.sort()
for i in name:
print(i) | import sys
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 19
MOD = 10 ** 9 + 7
N = INT()
A = LIST()
if N == 0:
if A[0] == 1:
print(1)
else:
print(-1)
exit()
cond1 = [0] * (N+1)
cond1[N] = A[N]
for i in range(N-1, -1, -1):
cond1[i] = cond1[i+1] + A[i]
if min(cond1) <= 0:
print(-1)
exit()
cond2 = [0] * (N+1)
cond2[0] = 1
for i in range(1, N+1):
cond2[i] = min(cond1[i], (cond2[i-1]-A[i-1]) * 2)
if min(cond2) <= 0:
print(-1)
exit()
if cond2[N] < A[N]:
print(-1)
exit()
ans = sum(cond2)
print(ans)
| 0 | null | 44,339,694,227,492 | 218 | 141 |
N = int(input())
while True:
N -= 1000
if N <= 0:
break
print(abs(N)) | import math
N =int(input())
s = N / 1000
if s == 0 :
print(0)
else :
a = math.ceil(s) * 1000 - N
print(int(a)) | 1 | 8,550,390,654,240 | null | 108 | 108 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.