create dev channel

This commit is contained in:
2025-05-07 04:44:30 +09:00
parent 603fca2b20
commit 16a8e59450
426 changed files with 643 additions and 36 deletions

17
zeta/py/completed/1002.py Normal file
View File

@@ -0,0 +1,17 @@
T = int(input())
for i in range(T):
x1, y1, r1, x2, y2, r2 = map(int, input().split())
dist:int = (x1-x2)**2 + (y1- y2)**2
distR:int = (r1+r2)**2
if dist == 0 and r1 == r2:
print(-1)
elif dist < (r1-r2)**2:
print(0)
elif dist == (r1-r2)**2:
print(1)
elif dist > distR:
print(0)
elif dist < distR:
print(2)
elif dist == distR:
print(1)

37
zeta/py/completed/1005.py Normal file
View File

@@ -0,0 +1,37 @@
import sys
import os
import io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
sys.setrecursionlimit(1000000)
Mem: list
D: list
E: list
def get_time(now):
if Mem[now] != -1:
return Mem[now]
elif not E[now]:
Mem[now] = D[now]
return D[now]
else:
s = max([get_time(target) for target in E[now]]) + D[now]
Mem[now] = s
return s
if __name__ == "__main__":
T = int(input())
for _ in range(T):
N, K = map(int, input().split())
D = [0] + list(map(int, input().split()))
E = [[] for _ in range(N + 1)]
for _ in range(K):
u, v = map(int, input().split())
E[v].append(u)
W = int(input())
Mem = [-1] * (N + 1)
print(get_time(W))

20
zeta/py/completed/1010.py Normal file
View File

@@ -0,0 +1,20 @@
def case(N, M):
"""
정의역의 원소가 N개이고 치역의 공역의 원소가 M개인 일대일 '증가' 함수의 경우의 수
(M)C(N)
"""
ans = 1
for i in range(M - N + 1, M + 1):
ans *= i
for i in range(1, N + 1):
ans //= i
return ans
def solve(T, I):
return "\n".join([str(case(*I[i])) for i in range(T)])
if __name__ == '__main__':
T = int(input())
print(solve(T, [list(map(int, input().split())) for _ in range(T)]))

16
zeta/py/completed/1011.py Normal file
View File

@@ -0,0 +1,16 @@
import math
T = int(input())
for i in range(T):
x, y = map(int, input().split())
distance = y - x
disted = distance ** (1 / 2)
upper = math.ceil(disted)
lowersq = (upper - 1)**2
uppersq = upper**2
if (lowersq + uppersq)/2 <= distance:
print(upper * 2 -1)
else:
print(upper * 2 - 2)

50
zeta/py/completed/1012.py Normal file
View File

@@ -0,0 +1,50 @@
def find_group(Map, M, N, i, j):
ret = []
temp = []
temp.append((i, j))
while temp:
t = temp.pop()
if t in ret:
continue
if 1 <= t[0]:
if Map[t[0] - 1][t[1]]:
temp.append((t[0] - 1, t[1]))
if t[0] < M - 1:
if Map[t[0] + 1][t[1]]:
temp.append((t[0] + 1, t[1]))
if 1 <= t[1]:
if Map[t[0]][t[1] - 1]:
temp.append((t[0], t[1] - 1))
if t[1] < N - 1:
if Map[t[0]][t[1] + 1]:
temp.append((t[0], t[1] + 1))
ret.append(t)
return ret
def solve(M, N, Ks):
Map = [[0 for _ in range(N)] for _ in range(M)]
count = 0
found = {}
for k in Ks:
Map[k[0]][k[1]] = 1
for i in range(M):
for j in range(N):
if Map[i][j]:
if (i, j) in found:
continue
else:
r = find_group(Map, M, N, i, j)
count += 1
for s in r:
found[s] = count
return count
if __name__ == '__main__':
T = int(input())
for _ in range(T):
M, N, K = map(int, input().split())
Ks = [tuple(map(int, input().split())) for _ in range(K)]
print(solve(M, N, Ks))

View File

@@ -0,0 +1,4 @@
print("""\ /\\
) ( ')
( / )
\(__)|""")

32
zeta/py/completed/1018.py Normal file
View File

@@ -0,0 +1,32 @@
N, M = map(int, input().split())
T = [input() for i in range(N)]
Min = 10000000000000
for i in range(N-7):
for j in range(M-7):
temp = [t[j:j+8] for t in T[i:i+8]]
start = temp[0][0]
C = 0
ifC = 0
for ki, k in enumerate(range(8)):
for l in range(k+1):
if ki%2 == 0:
C += temp[k-l][l].count("B")
ifC += temp[k-l][l].count("W")
else:
C += temp[k-l][l].count("W")
ifC += temp[k-l][l].count("B")
for ki, k in enumerate(range(7)):
for l in range(k+1):
if ki%2 == 0:
C += temp[l-k+7][7-l] .count("B")
ifC += temp[l-k+7][7-l] .count("W")
else:
C += temp[l-k+7][7-l] .count("W")
ifC += temp[l-k+7][7-l] .count("B")
mine = min(C, ifC)
if mine < Min:
Min = mine
print(Min)

35
zeta/py/completed/1025.py Normal file
View File

@@ -0,0 +1,35 @@
import sys
input = sys.stdin.readline
# 제곱수
def check_square(x):
return (x ** (1 / 2)).is_integer()
def solve(N: int, M: int, I: list[list[str]]) -> int: # length
maxed = -1
# 전탐색
for n in range(N):
for m in range(M):
for dn in range(-N, N):
for dm in range(-M, M):
if dn == 0 and dm == 0:
continue
s = ''
start = [n, m]
while 0 <= start[0] < N and 0 <= start[1] < M:
s += I[start[0]][start[1]]
if check_square(int(s)):
maxed = max(maxed, int(s))
start[0] += dn
start[1] += dm
return maxed
if __name__ == '__main__':
N, M = map(int, input().split())
I = [list(map(str, input().rstrip())) for i in range(N)]
print(solve(N, M, I))

View File

@@ -0,0 +1,4 @@
T = int(input())
for i in range(T):
H, W, N = map(int, input().split())
print("%d%02d"%((N-1)%H+1,(N-1)//H+1))

13
zeta/py/completed/1032.py Normal file
View File

@@ -0,0 +1,13 @@
N = int(input())
K = [input() for i in range(N)]
shared = K[0]
for k in K:
for i, c in enumerate(k):
if c == "?":
pass
elif shared[i] == c:
pass
else:
shared = shared[:i] + "?" + shared[i + 1:]
print(shared)

36
zeta/py/completed/1036.py Normal file
View File

@@ -0,0 +1,36 @@
import sys
N = int(input())
S = [input() for i in range(N)]
K = int(input())
def change(s):
return int(s, 36)
def nofet(i):
if i <= 9:
return chr(i + 48)
else:
return chr(i + 55)
def rechange(i):
s = ""
if i == 0:
return "0"
while i > 0:
s += nofet(i % 36)
i //= 36
return s[::-1]
mult = [[0, i] for i in range(36)]
for s in S:
for i, c in enumerate(s[::-1]):
mult[change(c)][0] += 36 ** i
mult.sort(key=lambda M: M[0] * (35 - M[1]))
print(rechange(sum([mult[i][0] * mult[i][1] for i in range(36 - K)] + [mult[i][0] * 35 for i in range(36 - K, 36)])))

View File

@@ -0,0 +1,3 @@
input()
I = sorted(map(int, input().split()))
print(I[0] * I[-1])

37
zeta/py/completed/1043.py Normal file
View File

@@ -0,0 +1,37 @@
import sys
input = sys.stdin.readline
def solve(N, M, know: set[int], parties):
valid_party = [1 for _ in parties]
flag = True
while flag:
flag = False
for i, p in enumerate(parties):
cond = [m in know for m in p]
if all(cond):
valid_party[i] = 0
elif any(cond):
flag = True
valid_party[i] = 0
for m in p:
know.add(m)
return sum(valid_party)
if __name__ == "__main__":
N, M = map(int, input().split())
know = set()
pre_know = list(map(int, input().split()))
L = pre_know[0]
for i in pre_know[1:]:
know.add(i)
parties = [list(map(int, input().split()))[1:] for _ in range(M)]
print(solve(N, M, know, parties))

View File

@@ -0,0 +1,28 @@
import sys
input = sys.stdin.readline
def solve(A: list[int]):
A.reverse()
B = []
cnt = 0
while A:
a = A.pop()
l = len(B)
for i, b in enumerate(B):
if b > a:
B.insert(i, a)
cnt += l - i
break
else:
B.append(a)
return cnt
if __name__ == "__main__":
T = int(input())
for _ in range(T):
t, *A = map(int, input().split())
print(t, solve(A))

View File

@@ -0,0 +1,8 @@
a = int(input())
r = 0
k = []
for i in range(1, a+1):
k = tuple(map(int, str(i)))
if sum(k) == len(k)*(k[0] + k[-1]) / 2:
r += 1
print(r)

View File

@@ -0,0 +1,3 @@
import datetime
print(datetime.date.today().isoformat())

41
zeta/py/completed/1071.py Normal file
View File

@@ -0,0 +1,41 @@
def continuous(a, b):
return a + 1 == b
def solve(N: int, S: list[int]):
x = []
spares = []
nums = list(S)
nums.sort()
x.append(nums[0])
for n in nums[1:]:
if continuous(x[-1], n):
spares.append(n)
else:
x.append(n)
while spares:
if not continuous(x[-1], spares[0]):
x.append(spares.pop(0))
else:
break
if spares:
k = -1
l = len(x)
while spares:
if -l == k:
x.insert(0, spares.pop(0))
continue
if continuous(spares[0], x[k]) or continuous(x[k - 1], spares[0]):
k -= 1
else:
x.insert(k, spares.pop(0))
return x
if __name__ == '__main__':
N = int(input())
S = list(map(int, input().split()))
print(" ".join(map(str, solve(N, S))))

10
zeta/py/completed/1074.py Normal file
View File

@@ -0,0 +1,10 @@
N, r, c = map(int, input().split())
Z = lambda r, c: r * 2 + c
S = 0
for i in range(N - 1, -1, -1):
S += (4 ** i) * Z(r // (2 ** i), c // (2 ** i))
r, c = r % (2 ** i), c % (2 ** i)
print(S)

13
zeta/py/completed/1076.py Normal file
View File

@@ -0,0 +1,13 @@
VALUE = {
"black": 0,
"brown": 1,
"red": 2,
"orange":3,
"yellow":4,
"green": 5,
"blue": 6,
"violet": 7,
"grey": 8,
"white": 9}
print((10*VALUE[input()] + VALUE[input()])*10**VALUE[input()])

View File

@@ -0,0 +1,13 @@
def solve(K, I):
stack = list()
for i in I:
if i:
stack.append(i)
else:
stack.pop()
return sum(stack)
if __name__ == '__main__':
K = int(input())
print(solve(K, [int(input()) for _ in range(K)]))

View File

@@ -0,0 +1,8 @@
N = int(input())
L = list(map(int, input().split()))
v = int(input())
c = 0
for x in L:
if x == v:
c += 1
print(c)

View File

@@ -0,0 +1,9 @@
from sys import *
c=0
D=[]
I=[ord(i)-97 for i in stdin.readline().rstrip()]
for i in range(26):
try:D.append(I.index(i))
except:D.append(-1)
D=list(map(str,D))
print(" ".join(D))

View File

@@ -0,0 +1,8 @@
N, M = map(int, input().split())
L = [0 for _ in range(N)]
for _ in range(M):
i, j, k = map(int, input().split())
for m in range(i - 1, j):
L[m] = k
print(" ".join(map(str, L)))

View File

@@ -0,0 +1,7 @@
N, M = map(int, input().split())
L = [i + 1 for i in range(N)]
for _ in range(M):
i, j = map(int, input().split())
L[i - 1:j] = reversed(L[i - 1:j])
print(" ".join(map(str, L)))

View File

@@ -0,0 +1,14 @@
N, M = map(int, input().split())
L = [i for i in range(1, N + 1)]
def swap(i, j):
global L
L[i - 1], L[j - 1] = L[j - 1], L[i - 1]
for _ in range(M):
i, j = map(int, input().split())
swap(i, j)
print(" ".join(map(str, L)))

View File

@@ -0,0 +1,2 @@
N = int(input())
for s in sorted([tuple(map(str, input().split())) for i in range(N)], key=lambda v: int(v[0])): print(s[0], s[1])

View File

@@ -0,0 +1,9 @@
from collections import Counter
if __name__ == "__main__":
N = int(input())
S = list(map(int, input().split()))
counter = Counter(S)
M = int(input())
I = list(map(int, input().split()))
print(" ".join(str(counter[i]) for i in I))

View File

@@ -0,0 +1,3 @@
input()
A = list(map(int, input().split() ) )
print(min(A), max(A))

View File

@@ -0,0 +1,32 @@
import sys
class Stack(list):
def push(self, *args, **kwargs):
self.append(args[0])
def size(self, *args, **kwargs):
return self.__len__()
def empty(self, *args, **kwargs):
return 1 if self.__len__() == 0 else 0
def top(self, *args, **kwargs):
return self.__getitem__(-1)
def dispatch(self, exp):
t = exp.strip().split()
try:
return self.__getattribute__(t[0])(*map(int, t[1:]))
except:
return -1
if __name__ == '__main__':
s = Stack()
N = int(sys.stdin.readline())
for i in range(N):
exp = sys.stdin.readline()
ret = s.dispatch(exp)
if ret is not None:
print(ret)

View File

@@ -0,0 +1,20 @@
N = int(input())
Mem = [[0] * 10 for i in range(100)]
def I(l, k):
if 0 <= k <= 9:
if l == 1:
return 1
elif Mem[l-1][k] != 0:
return Mem[l-1][k]
else:
temp = (I(l - 1, k - 1) + I(l - 1, k + 1)) % 1000000000
Mem[l-1][k] = temp
return temp
else:
return 0
print(sum(I(N, i) for i in range(1, 10)) % 1000000000)

View File

@@ -0,0 +1,36 @@
import sys
from collections import deque
queue = deque()
def dispatch(command: str, *args):
if command == "push":
queue.append(int(args[0]))
elif command == "pop":
if queue:
return queue.popleft()
else:
return -1
elif command == "size":
return len(queue)
elif command == "empty":
return 0 if queue else 1
elif command == "front":
if queue:
return queue[0]
else:
return -1
elif command == "back":
if queue:
return queue[-1]
else:
return -1
if __name__ == "__main__":
N = int(sys.stdin.readline())
for _ in range(N):
d = dispatch(*sys.stdin.readline().strip().split())
if d is not None:
print(d)

View File

@@ -0,0 +1,9 @@
def fib(n):
if not n:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
print(fib(int(input())))

View File

@@ -0,0 +1,2 @@
a, k = map(int, input().split())
print(1 if a == 1 else (a if k == 0 else (a if a % 2 else 1)))

View File

@@ -0,0 +1,15 @@
if __name__ == "__main__":
P, N = map(int, input().split())
S = list(map(int, input().split()))
S.sort(reverse=True)
I = []
P -= 1
for _ in range(N):
s = S.pop()
if P - s < 0:
break
else:
I.append(P)
P -= s
print(len(I), sum(I))

View File

@@ -0,0 +1 @@
print(input()+"??!")

View File

@@ -0,0 +1,44 @@
import sys
input = sys.stdin.readline
class Palindrome:
def __init__(self, N: int, arr: list[int]):
self.N = N
self.arr = arr
self.P = [[0 for _ in range(N)] for _ in range(N)]
for i in range(self.N):
s = i
e = i
while 0 <= s and e < self.N:
if self.arr[s] == self.arr[e]:
self.P[s][e] = 1
else:
break
s -= 1
e += 1
for i in range(self.N - 1):
s = i
e = i + 1
while 0 <= s and e < self.N:
if self.arr[s] == self.arr[e]:
self.P[s][e] = 1
else:
break
s -= 1
e += 1
def is_partial_valid(self, s: int, e: int) -> int:
return self.P[s][e]
if __name__ == "__main__":
N = int(input())
arr = list(map(int, input().split()))
solver = Palindrome(N, arr)
M = int(input())
for s, e in [tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(M)]:
print(solver.is_partial_valid(s, e))

View File

@@ -0,0 +1,10 @@
N = range(int(input()))
[print(
sum(
map(
int,input().split()
)
)
)
for i in N
]

View File

@@ -0,0 +1,5 @@
while 1:
try:
print(sum(map(int,input().split())))
except EOFError:
break

View File

@@ -0,0 +1,5 @@
while True:
s = sum(map(int, input().split()))
if s == 0:
break
print(s)

View File

@@ -0,0 +1,17 @@
import sys
import math
input = sys.stdin.readline
if __name__ == "__main__":
N, M = map(int, input().split())
A = list(map(int, input().split()))
S = [(0, 0)]
C = [0] * 1001
C[0] = 1
for i in range(N):
s = ((S[-1][0] + A[i]) % M, i + 1)
S.append(s)
C[s[0]] += 1
print(sum([math.comb(c, 2) for c in C]))

View File

@@ -0,0 +1 @@
I=input();print(1 if I == I[::-1] else 0)

View File

@@ -0,0 +1,8 @@
import sys
N = int(sys.stdin.readline())
K = [0]*10000
for i in range(N):
K[int(sys.stdin.readline())-1] += 1
for i in range(1,10001):
for k in range(K[i-1]):
print(i)

View File

@@ -0,0 +1,14 @@
N = int(input())
for i in range(N):
for j in range(N):
if j % 2 == 0:
print("*", end='')
else:
print(" ", end='')
print()
for j in range(N):
if j % 2 == 1:
print("*", end='')
else:
print(" ", end='')
print()

View File

@@ -0,0 +1,23 @@
import sys
import heapq
input = sys.stdin.readline
def solve(N, Cl):
Cl.sort()
pq = []
heapq.heappush(pq, Cl[0][1])
for i in range(1, N):
if pq[0] <= Cl[i][0]:
heapq.heappop(pq)
heapq.heappush(pq, Cl[i][1])
return len(pq)
if __name__ == "__main__":
N = int(input())
Cl = [tuple(map(int, input().split())) for _ in range(N)]
print(solve(N, Cl))

View File

@@ -0,0 +1,3 @@
N = int(input())
for i in range(N):
print("Case #%s:"%(i+1), sum(map(int, input().split())))

View File

@@ -0,0 +1,17 @@
N, K = map(int, input().split())
A = [int(input())for i in range(N)]
r = 0
back = 1
while K != 0:
for i in A:
if i > K:
r += K // back
K %= back
break
back = i
else:
r += K // back
K %= back
print(r)

View File

@@ -0,0 +1,13 @@
N, K = map(int, input().split())
def C(n, k):
if n == 1:
return 1
elif k == 0 or k == n:
return 1
else:
return C(n - 1, k) + C(n - 1, k - 1)
print(C(N, K))

View File

@@ -0,0 +1,17 @@
import sys
sys.setrecursionlimit(12000)
N, K = map(int, input().split())
Mem = [[0]*1001for i in range(1001)]
def C(n, k):
if n == 1:
return 1
elif k == 0 or k == n:
return 1
if Mem[n][k] != 0:
return Mem[n][k]
t = (C(n - 1, k) + C(n - 1, k - 1)) % 10007
Mem[n][k] = t
return t
print(C(N, K))

View File

@@ -0,0 +1,15 @@
N = int(input())
A = list(map(int, input().split()))
T = [0 for _ in range(N)]
T[0] = 1
for i in range(1, N):
t = []
for j in range(i):
if A[j] < A[i]:
t.append(T[j] + 1)
else:
t.append(1)
T[i] = max(t)
print(max(T))

View File

@@ -0,0 +1,27 @@
def solve(N: int, A: list):
T = [[0, 0] for _ in range(N)]
T[0][0] = 1 # 상승부
T[0][1] = 1 # 하강부
for i in range(1, N): # mainloop
t = []
for j in range(i):
if A[j] < A[i]:
t.append(T[j][0] + 1)
else:
t.append(1)
T[i][0] = max(t)
t = []
for j in range(i):
if A[j] > A[i]:
t.append(T[j][0] + 1)
t.append(T[j][1] + 1)
else:
t.append(1)
T[i][1] = max(t)
return max(max(T, key=lambda x: max(x)))
if __name__ == '__main__':
print(solve(int(input()), list(map(int, input().split()))))

32
zeta/py/completed/1107.py Normal file
View File

@@ -0,0 +1,32 @@
class RemoteController:
"""
start at number 100
action:
press number not broken
press ++
press --
example(5457 with broken 6 7 8):
5 4 5 5 + +
"""
def __init__(self, broken: list[int]):
self.broken = broken
def solve(self, target: int) -> int:
"""return: number of press buttons to reach target number"""
mins = abs(target - 100)
for i in range(0, 1000000 + 1):
nums = list(map(int, str(i)))
if all(False if s in self.broken else True for s in nums):
mins = min(abs(target - i) + len(nums), mins)
return mins
if __name__ == "__main__":
N = int(input())
M = int(input())
broken = list(map(int, input().split())) if M != 0 else []
solver = RemoteController(broken)
print(solver.solve(N))

12
zeta/py/completed/1120.py Normal file
View File

@@ -0,0 +1,12 @@
A, B = input().split()
def get(a, b):
cnt = 0
for f, s in zip(a, b):
if f != s:
cnt += 1
return cnt
print(min(get(A, B[i:i+len(A)+1]) for i in range(len(B) - len(A)+1)))

View File

@@ -0,0 +1,6 @@
while True:
i = input()
if i == 'END':
break
i = i[::-1]
print(i)

View File

@@ -0,0 +1 @@
print(sum(map(int,input().split())))

View File

@@ -0,0 +1,12 @@
def solve(N, P):
P.sort()
S = P[0]
before = P[0]
for p in P[1:]:
S += before + p
before += p
return S
if __name__ == '__main__':
print(solve(int(input()), list(map(int, input().split()))))

12
zeta/py/completed/1149.py Normal file
View File

@@ -0,0 +1,12 @@
N = int(input())
C = [list(map(int, input().split())) for _ in range(N)]
T = [[0, 0, 0] for i in range(N)]
T[0] = C[0][:]
for i in range(1, N):
T[i][0] = min((T[i - 1][1], T[i - 1][2])) + C[i][0]
T[i][1] = min((T[i - 1][0], T[i - 1][2])) + C[i][1]
T[i][2] = min((T[i - 1][0], T[i - 1][1])) + C[i][2]
print(min((T[N-1][0], T[N-1][1], T[N-1][2])))

View File

@@ -0,0 +1,29 @@
def solve(N, P):
s = 0
r, a = partition(P)
s += a
while (r):
r, a = partition(r)
s += a
return s
def partition(part: list):
max_p = max(part)
m_idx = 0
for i in range(len(part)):
if part[i] == max_p:
m_idx = i
remain = part[m_idx + 1:]
after = part[:m_idx]
amount = sum([max_p - v for v in after])
return remain, amount
if __name__ == '__main__':
T = int(input())
for _ in range(T):
N = int(input())
P = list(map(int, input().split()))
print(solve(N, P))

View File

@@ -0,0 +1 @@
print("<EFBFBD>")

View File

@@ -0,0 +1,17 @@
def solve(N, H):
lasts = []
for i, h in enumerate(H):
for j, l in enumerate(lasts):
if h == l - 1:
lasts[j] -= 1
break
else:
lasts.append(h)
return len(lasts)
if __name__ == '__main__':
N = int(input())
H = list(map(int, input().split()))
print(solve(N, H))

View File

@@ -0,0 +1,13 @@
def solve(N, H):
arrow = [0] * 1000001
for h in H:
if arrow[h]:
arrow[h] -= 1
arrow[h - 1] += 1
return sum(arrow)
if __name__ == '__main__':
N = int(input())
H = list(map(int, input().split()))
print(solve(N, H))

View File

@@ -0,0 +1,11 @@
if __name__ == "__main__":
N = int(input())
I = [input() for _ in range(N)]
I_a = sorted(I)
I_d = I_a.copy()[::-1]
if I == I_a:
print("INCREASING")
elif I == I_d:
print("DECREASING")
else:
print("NEITHER")

View File

@@ -0,0 +1,3 @@
N = int(input())
for i in sorted([list(map(int,input().split()))for i in range(N)]):
print(' '.join(map(str, i)))

View File

@@ -0,0 +1,3 @@
T = int(input())
S = [tuple(map(int, input().split()))for _ in range(T)]; S.sort(key=lambda v: (v[1], v[0]))
[print(i, j)for i, j in S]

View File

@@ -0,0 +1,12 @@
N = int(input())
p = 2
while N % p == 0:
N //= p
print(p)
p += 1
while N != 1:
if N % p == 0:
N //= p
print(p)
else:
p += 2

View File

@@ -0,0 +1,6 @@
I = input()
T = set()
for i in range(len(I)):
T.add(I[i:])
for t in sorted(T):
print(t)

View File

@@ -0,0 +1,13 @@
import sys
input = sys.stdin.readline
if __name__ == "__main__":
N, M = map(int, input().split())
A = list(map(int, input().split()))
S = [0]
for i in range(N):
S.append(S[-1] + A[i])
for _ in range(M):
i, j = map(int, input().split())
print(S[j] - S[i - 1])

View File

@@ -0,0 +1,16 @@
import sys
input = sys.stdin.readline
if __name__ == "__main__":
N, M = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N)]
S = [[0 for _ in range(N + 1)] for _ in range(N + 1)]
for i in range(N):
for j in range(N):
S[i + 1][j + 1] = S[i][j + 1] + S[i + 1][j] - S[i][j] + A[i][j]
for _ in range(M):
x1, y1, x2, y2 = map(int, input().split())
print(S[x2][y2] - S[x2][y1 - 1] - S[x1 - 1][y2] + S[x1 - 1][y1 - 1])

47
zeta/py/completed/1167.py Normal file
View File

@@ -0,0 +1,47 @@
import sys
input = sys.stdin.readline
def longest(E, start) -> tuple[int, int]:
D = []
M = 0
V_M = 0
D.append((start, [start], 0)) # now, visited, accumulated
while D:
now, visited, accumulated = D.pop(0)
flag = True
for e in E[now]:
target, cost = e[0], e[1]
if target not in visited:
flag = False
D.append((target, visited + [target], cost + accumulated))
if flag:
if accumulated > M:
M = accumulated
V_M = now
return V_M, M
def solve(V, E):
first = longest(E, 1)[0]
_, diameter = longest(E, first)
return diameter
if __name__ == "__main__":
V = int(input())
E = {}
for _ in range(V):
_i = list(map(int, input().split()))
E[_i[0]] = []
for i, v in zip(_i[1:-1:2], _i[2:-1:2]):
E[_i[0]].append((i, v))
print(solve(V, E))

View File

@@ -0,0 +1,14 @@
N = int(input())
def move(f, t):
print(f, t)
def hanoi(n, f, b, t):
if n == 1:
move(f, t)
else:
hanoi(n-1, f, t, b)
move(f, t)
hanoi(n-1, b, f, t)
print(2**N-1)
hanoi(N, 1,2,3)

View File

@@ -0,0 +1,6 @@
N = int(input())
S = [input() for i in range(N)]
S = list(set(S))
S.sort(key=lambda v: (len(v), v))
for s in S:
print(s)

View File

@@ -0,0 +1,9 @@
S = lambda n: n*(n+1)//2
N = int(input())
nor = round((2*N)**.5)
t = N - S(nor-1)
k = S(nor) - S(nor-1)
if nor % 2:
print("%d/%d"%(k-t+1, t))
else:
print("%d/%d"%(t, k-t+1))

View File

@@ -0,0 +1 @@
for i in range(int(input().split()[0])):print(input()[::-1])

View File

@@ -0,0 +1,5 @@
d=1;n=int(input())
while n>=d:
if(n==d):print(1);break
d*=2
else:print(0)

48
zeta/py/completed/1197.py Normal file
View File

@@ -0,0 +1,48 @@
import sys
input = sys.stdin.readline
class MinimumSpanningTree:
def __init__(self, V: int, E: int, edges: list[tuple[int]]):
self.V = V
self.E = E
self.edges: list[tuple[int]] = edges
self.edges.sort(key=lambda x: x[2])
self.__parents = [i for i in range(self.V + 1)]
def __root(self, x: int) -> int:
node = x
while node != self.__parents[node]:
node = self.__parents[node]
return node
def __union(self, x: int, y: int):
rx = self.__root(x)
ry = self.__root(y)
if rx > ry:
self.__parents[ry] = rx
else:
self.__parents[rx] = ry
def solve(self):
mst = []
for e in self.edges:
a = e[0]
b = e[1]
if self.__root(a) == self.__root(b):
continue
mst.append(e)
self.__union(a, b)
if len(mst) == self.V - 1:
break
return sum(map(lambda x: x[2], mst))
if __name__ == "__main__":
V, E = map(int, input().split())
edges = [tuple(map(int, input().split())) for _ in range(E)]
solver = MinimumSpanningTree(V, E, edges)
print(solver.solve())

40
zeta/py/completed/1238.py Normal file
View File

@@ -0,0 +1,40 @@
import heapq
import sys
input = sys.stdin.readline
def dijkstra(N, E, start) -> list[int]:
heap = []
distance = [10 ** 9 + 1 for _ in range(N + 1)]
distance[start] = 0
heapq.heappush(heap, (0, start,))
while heap:
cost, now, = heapq.heappop(heap)
if distance[now] < cost:
continue
for new, weight in E[now]:
new_cost = cost + weight
if new_cost < distance[new]:
distance[new] = new_cost
heapq.heappush(heap, (new_cost, new))
return distance
def solve(N, M, E, iE, X):
come = dijkstra(N, E, X)
gone = dijkstra(N, iE, X)
return max([come[i] + gone[i] for i in range(1, N + 1)])
if __name__ == '__main__':
N, M, X = map(int, input().split())
E = {i: [] for i in range(1, N + 1)}
iE = {i: [] for i in range(1, N + 1)}
for _ in range(M):
A, B, T = map(int, input().split())
E[A].append((B, T))
iE[B].append((A, T))
print(solve(N, M, E, iE, X))

13
zeta/py/completed/1247.py Normal file
View File

@@ -0,0 +1,13 @@
import sys
for _ in range(3):
N = int(sys.stdin.readline())
t = 0
for i in range(N):
t += int(sys.stdin.readline())
if t > 0:
print("+")
elif t < 0:
print("-")
else:
print("0")

View File

@@ -0,0 +1,8 @@
S = input()
MAX = '~'*len(S)
for i in range(1,len(S)-1):
for j in range(i+1, len(S)):
first, second, last = S[:i][::-1], S[i:j][::-1], S[j:][::-1]
if first + second + last < MAX:
MAX = first + second + last
print(MAX)

View File

@@ -0,0 +1,9 @@
t = input()
while t != "0":
t_r = int(t[::-1])
t_i = int(t)
if t_r == t_i:
print("yes")
else:
print("no")
t = input()

45
zeta/py/completed/1260.py Normal file
View File

@@ -0,0 +1,45 @@
def dfs(N, M, E, V):
D = []
D.append((V))
visited = []
while D:
now = D.pop()
if now in visited:
continue
else:
visited.append(now)
for i in E[now][::-1]:
D.append(i)
return visited
def bfs(N, M, E, V):
D = []
D.append((V))
visited = []
while D:
now = D.pop(0)
if now in visited:
continue
else:
visited.append(now)
for i in E[now]:
D.append(i)
return visited
if __name__ == '__main__':
N, M, V = map(int, input().split())
E = {}
for i in range(1, N + 1):
E[i] = []
for i in range(1, M + 1):
a, b = map(int, input().split())
E[a].append(b)
E[b].append(a)
for i in range(1, N + 1):
E[i].sort()
print(*dfs(N, M, E, V))
print(*bfs(N, M, E, V))

View File

@@ -0,0 +1,4 @@
a, b = map(int, input().split())
print(a // b)
print(a % b)

View File

@@ -0,0 +1,18 @@
N, K = map(int, input().split())
Ws = [tuple(map(int, input().split())) for i in range(N)] # weight, value
D = [[0] * (K + 1) for _ in range(N + 1)]
def solve(k):
global D
for n in range(1, N + 1):
for k in range(1, K + 1):
if Ws[n - 1][0] <= k:
D[n][k] = max(Ws[n - 1][1] + D[n - 1][k - Ws[n - 1][0]], D[n - 1][k])
else:
D[n][k] = D[n - 1][k]
return D[N][K]
print(solve(K))

View File

@@ -0,0 +1,4 @@
if __name__ == "__main__":
s = input()
t = input()
print(1 if t * len(s) == s * len(t) else 0)

View File

@@ -0,0 +1,40 @@
import sys
input = sys.stdin.readline
class HappyCow:
def __init__(self, N: int, H: list[int]):
self.__N: int = N
self.__happiness: list[int] = H
self.__total: list[int] = [[0] * (self.__N + 2) for _ in range(self.__N + 2)]
for i in range(1, self.__N + 1):
day = i
self.__total[i][self.__N] = (
self.__total[i - 1][self.__N] + day * self.__happiness[i - 1]
)
self.__total[0][self.__N - i] = (
self.__total[0][self.__N - i + 1] + day * self.__happiness[self.__N - i]
)
for i in range(1, self.__N):
for j in range(self.__N - 1, i - 1, -1):
self.__total[i][j] = max(
[
self.__total[i - 1][j]
+ self.__happiness[i - 1] * (self.__N - j + i),
self.__total[i][j + 1]
+ self.__happiness[j] * (self.__N - j + i),
]
)
def solve(self) -> int:
return max([self.__total[i][i] for i in range(self.__N)])
if __name__ == "__main__":
N = int(input())
H = list(map(int, input().split()))
solver = HappyCow(N, H)
print(solver.solve())

19
zeta/py/completed/1316.py Normal file
View File

@@ -0,0 +1,19 @@
N = int(input())
c = 0
for i in range(N):
s = input()
beforehead = s[0]
befores = set()
befores.add(beforehead)
for t in s:
if t == beforehead:
continue
elif t in befores:
break
else:
beforehead = t
befores.add(t)
else:
c+=1
print(c)

View File

@@ -0,0 +1,2 @@
for i in range(int(input())):print("god"+"".join(input().split()[1:]))

37
zeta/py/completed/1325.py Normal file
View File

@@ -0,0 +1,37 @@
from collections import deque
import sys
input = sys.stdin.readline
def bfs(N, H, start):
Vsx = [0] * (N + 1)
count = 0
D = deque()
D.append(start)
Vsx[start] = 1
while D:
now = D.pop()
count += 1
for target in H[now]:
if not Vsx[target]:
D.append(target)
Vsx[target] = 1
return count
def solve(N, M, H):
S = [bfs(N, H, i) for i in range(1, N + 1)]
m = max(S)
return [i + 1 for i, v in enumerate(S) if v == m]
if __name__ == "__main__":
N, M = map(int, input().split())
H = [[] for _ in range(N + 1)]
for _ in range(M):
s, e = map(int, input().split())
H[e].append(s)
print(*solve(N, M, H))

38
zeta/py/completed/1326.py Normal file
View File

@@ -0,0 +1,38 @@
def solve(N, P, a, b):
a -= 1
b -= 1
D = []
D.append((a, 0))
visited = [2147483647] * N
visited[a] = 0
if b == a:
return 0
while D:
now, cost = D.pop(0)
if now == b:
return cost
from_now_on = now - P[now]
# - dir
while from_now_on >= 0:
if visited[from_now_on] > cost + 1:
visited[from_now_on] = cost + 1
D.append((from_now_on, cost + 1))
from_now_on -= P[now]
from_now_on = now + P[now]
# + dir
while from_now_on < N:
if visited[from_now_on] > cost + 1:
visited[from_now_on] = cost + 1
D.append((from_now_on, cost + 1))
from_now_on += P[now]
return -1
if __name__ == "__main__":
N = int(input())
P = list(map(int, input().split()))
a, b = map(int, input().split())
print(solve(N, P, a, b))

View File

@@ -0,0 +1,7 @@
a,b = map(int, input().split())
if a > b:
print(">")
elif a < b:
print("<")
else:
print("==")

View File

@@ -0,0 +1,19 @@
import sys
def solve(N, E, C):
S = 0
m = C[0]
for i in range(0, len(E)):
if C[i] <= m:
m = C[i]
S += m * E[i]
return S
if __name__ == '__main__':
print(solve(int(sys.stdin.readline()),
list(map(int, sys.stdin.readline().split())),
list(map(int, sys.stdin.readline().split()))))

16
zeta/py/completed/1339.py Normal file
View File

@@ -0,0 +1,16 @@
def solve(N: int, W: list):
V = {}
for w in W:
for i, a in enumerate(w[::-1]):
if a not in V:
V[a] = 0
V[a] += 10 ** i
S = list(sorted(V.items(), key=lambda x: x[1], reverse=True))
return sum([(9 - i) * S[i][1] for i in range(len(S))])
if __name__ == '__main__':
N = int(input())
W = [input() for _ in range(N)]
print(solve(N, W))

View File

@@ -0,0 +1,53 @@
import sys
input = sys.stdin.readline
def get_max_xor(N, S):
trie = [[0, 0] for _ in range(3000000)]
cnt = 0
for s in S:
idx = 0
for b in s:
b = int(b)
if trie[idx][b]:
idx = trie[idx][b]
else:
trie[idx][b] = cnt + 1
idx = cnt + 1
cnt += 1
n = []
D = [(0, 0, 0, 0)]
while D:
depth, idx1, idx2, xor = D.pop()
if depth == 30:
n.append(xor)
flag = False
if trie[idx1][0] and trie[idx2][1]:
D.append(
(depth + 1, trie[idx1][0], trie[idx2][1], xor + (1 << (29 - depth)))
)
flag = True
if trie[idx1][1] and trie[idx2][0]:
D.append(
(depth + 1, trie[idx1][1], trie[idx2][0], xor + (1 << (29 - depth)))
)
flag = True
if flag:
continue
if trie[idx1][0] and trie[idx2][0]:
D.append((depth + 1, trie[idx1][0], trie[idx2][0], xor))
if trie[idx1][1] and trie[idx2][1]:
D.append((depth + 1, trie[idx1][1], trie[idx2][1], xor))
return max(n)
if __name__ == "__main__":
N = int(input())
S = list(map(lambda x: format(int(x), "b").zfill(30), input().split()))
print(get_max_xor(N, S))

28
zeta/py/completed/1389.py Normal file
View File

@@ -0,0 +1,28 @@
def get_kevin(N, p, E):
D = []
D.append((p, 0))
kevins = [-1 for _ in range(N + 1)]
while D:
now, cost = D.pop(0)
if kevins[now] == -1:
kevins[now] = cost
else:
continue
for arrow in E[now]:
D.append((arrow, cost + 1))
return sum(kevins)
def get_lowest_kevin(N, E):
return min([(get_kevin(N, i, E), i) for i in range(1, N + 1)])[1]
if __name__ == '__main__':
N, M = map(int, input().split())
E = [[] for _ in range(N + 1)]
for _ in range(M):
i, j = map(int, input().split())
E[i].append(j)
E[j].append(i)
print(get_lowest_kevin(N, E))

View File

@@ -0,0 +1,10 @@
if __name__ == "__main__":
N = int(input())
A = list(map(int, input().split()))
AS = sum(A)
S = 0
for _ in range(N - 1):
r = A.pop()
AS -= r
S += r * AS
print(S)

19
zeta/py/completed/1421.py Normal file
View File

@@ -0,0 +1,19 @@
import sys
input = sys.stdin.readline
relu = lambda x: x if x >= 0 else 0
if __name__ == "__main__":
N, C, W = map(int, input().split())
A = [int(input()) for _ in range(N)]
costs = [
sum(
[
relu(-((a // i - 1) * C if a % i == 0 else a // i * C) + a // i * W * i)
for a in A
]
)
for i in range(1, max(A) + 1)
]
print(max(costs))

View File

@@ -0,0 +1 @@
for i in sorted(map(int, input()))[::-1]:print(i, end='')

View File

@@ -0,0 +1,8 @@
N = int(input())
def c(v):
return sum([int(i) for i in v if i.isdigit()])
for c in sorted([input() for i in range(N)], key=lambda v: (len(v), c(v), v)):print(c)

21
zeta/py/completed/1436.py Normal file
View File

@@ -0,0 +1,21 @@
N = int(input())
count = 0
now = 666
def isEnd(n):
k = 100
while k != 666:
if n < 100:
break
k = n % 1000
n //= 10
else:
return True
while count != N:
if isEnd(now):
count += 1
now += 1
print(now - 1) # Simple is the best

View File

@@ -0,0 +1,8 @@
import sys
input = sys.stdin.readline
if __name__ == "__main__":
N, M = map(int, input().split())
S = {input().rstrip() for _ in range(N)}
print(sum(1 for _ in range(M) if input().rstrip() in S))

View File

@@ -0,0 +1,44 @@
import sys
input = sys.stdin.readline
trie = [[0 for _ in range(26)] for _ in range(5050505)]
fins = [0 for _ in range(5050505)]
cnt = 1
def convert(c: str) -> int:
return ord(c) - ord("a")
# trie
def insert(s: str):
global cnt
cur = 0
for c in s:
nxt = convert(c)
if not trie[cur][nxt]:
trie[cur][nxt] = cnt
cnt += 1
cur = trie[cur][nxt]
fins[cur] = 1
def query(s):
cur = 0
for c in s:
nxt = convert(c)
if not trie[cur][nxt]:
if s == "sundaycoding":
print(c)
return 0
cur = trie[cur][nxt]
return fins[cur]
if __name__ == "__main__":
N, M = map(int, input().split())
for _ in range(N):
insert(input().strip())
print(sum(query(input().strip()) for _ in range(M)))

View File

@@ -0,0 +1,21 @@
N = int(input())
D = [tuple(map(int, input().split())) for i in range(N)]
T = []
def progress(day, cost):
if day == N:
T.append(cost)
return
elif day > N:
return
if D[day][0] == 1:
progress(day + 1, cost + D[day][1])
else:
progress(day + D[day][0], cost + D[day][1])
progress(day + 1, cost)
progress(0, 0)
print(max(T))

Some files were not shown because too many files have changed in this diff Show More