From 7841cb80bff1c47ca9edaff24092f4148f12b91e Mon Sep 17 00:00:00 2001 From: yenru0 Date: Mon, 13 Jan 2025 17:37:34 +0900 Subject: [PATCH] complete 2252.py 2623.py 9527.py 31403.py 32822.py --- zeta_python/completed/2252.py | 38 ++++++++++++++++++++++++++++++ zeta_python/completed/2623.py | 43 ++++++++++++++++++++++++++++++++++ zeta_python/completed/31403.py | 3 +++ zeta_python/completed/32822.py | 27 +++++++++++++++++++++ zeta_python/completed/9527.py | 17 ++++++++++++++ 5 files changed, 128 insertions(+) create mode 100644 zeta_python/completed/2252.py create mode 100644 zeta_python/completed/2623.py create mode 100644 zeta_python/completed/31403.py create mode 100644 zeta_python/completed/32822.py create mode 100644 zeta_python/completed/9527.py diff --git a/zeta_python/completed/2252.py b/zeta_python/completed/2252.py new file mode 100644 index 0000000..89edcde --- /dev/null +++ b/zeta_python/completed/2252.py @@ -0,0 +1,38 @@ +import sys + +input = sys.stdin.readline + + +class TopoSort: + def __init__(self, N, E): + self.__N = N + self.__edges = E + self.__targets = [[] for _ in range(self.__N)] + self.__indeg = [0] * self.__N + for u, v in self.__edges: + self.__indeg[v] += 1 + self.__targets[u].append(v) + + def sort(self) -> list[int]: + indeg = self.__indeg.copy() + Q = [] + for i, d in enumerate(indeg): + if d == 0: + Q.append(i) + line = [] + while Q: + u = Q.pop() + line.append(u) + for v in self.__targets[u]: + indeg[v] -= 1 + if indeg[v] == 0: + Q.append(v) + + return line + + +if __name__ == "__main__": + N, M = map(int, input().split()) + E = [tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(M)] + solver = TopoSort(N, E) + print(" ".join(map(lambda x: str(int(x) + 1), solver.sort()))) diff --git a/zeta_python/completed/2623.py b/zeta_python/completed/2623.py new file mode 100644 index 0000000..0d1b0e7 --- /dev/null +++ b/zeta_python/completed/2623.py @@ -0,0 +1,43 @@ +import sys + +input = sys.stdin.readline + + +class TopoSort: + def __init__(self, N, E): + self.__N = N + self.__edges = E + self.__targets = [[] for _ in range(self.__N)] + self.__indeg = [0] * self.__N + for u, v in self.__edges: + self.__indeg[v] += 1 + self.__targets[u].append(v) + + def sort(self) -> list[int]: + indeg = self.__indeg.copy() + Q = [] + for i, d in enumerate(indeg): + if d == 0: + Q.append(i) + line = [] + while Q: + u = Q.pop() + line.append(u) + for v in self.__targets[u]: + indeg[v] -= 1 + if indeg[v] == 0: + Q.append(v) + if len(line) != N: + return [-1] + return line + + +if __name__ == "__main__": + N, M = map(int, input().split()) + E = [] + for _ in range(M): + order = list(map(lambda x: int(x) - 1, input().split())) + for u, v in zip(order[1:-1], order[2:]): + E.append((u, v)) + solver = TopoSort(N, E) + print("\n".join(map(lambda x: str(int(x) + 1), solver.sort()))) diff --git a/zeta_python/completed/31403.py b/zeta_python/completed/31403.py new file mode 100644 index 0000000..8d449b0 --- /dev/null +++ b/zeta_python/completed/31403.py @@ -0,0 +1,3 @@ +a, b, c = input(), input(), int(input()) +print(int(a) + int(b) - int(c)) +print(int(a + b) - int(c)) diff --git a/zeta_python/completed/32822.py b/zeta_python/completed/32822.py new file mode 100644 index 0000000..673ae9b --- /dev/null +++ b/zeta_python/completed/32822.py @@ -0,0 +1,27 @@ +import sys + +input = sys.stdin.readline + + +class DiffGameSolver: + def __init__(self, N, A: list[int], B: list[int], betas: list[int]): + self.__diff_max_by_columns = [] + for i in range(N): + self.__diff_max_by_columns.append( + max(abs(A[j][i] - B[j][i]) for j in range(N)) + ) + + self.__betas = betas + + def solve(self) -> int: + return sum([self.__diff_max_by_columns[beta] for beta in self.__betas]) + + +if __name__ == "__main__": + N, M = map(int, input().split()) + A = [list(map(int, input().split())) for _ in range(N)] + B = [list(map(int, input().split())) for _ in range(N)] + betas = list(map(lambda x: int(x) - 1, input().split())) + + solver = DiffGameSolver(N, A, B, betas) + print(solver.solve()) diff --git a/zeta_python/completed/9527.py b/zeta_python/completed/9527.py new file mode 100644 index 0000000..6992f4d --- /dev/null +++ b/zeta_python/completed/9527.py @@ -0,0 +1,17 @@ +def S(n: int): + s = n & 1 + for i in range(60 - 1, 0, -1): + if n & (1 << i): + s += S.points[i - 1] + (n - (1 << i) + 1) + n -= 1 << i + return s + + +S.points = [0] * 60 +S.points[0] = 1 +for i in range(1, 60): + S.points[i] = 2 * S.points[i - 1] + (1 << i) + +if __name__ == "__main__": + A, B = map(int, input().split()) + print(S(B) - S(A - 1))