[파이썬] 10986 나머지 합 n, m = map(int, input().split()) li = list(map(int, input().split())) dp = [0 for i in range(m)] dp[0] = 1 s = 0 for i in range(n): s += li[i] dp[s % m] += 1 ans = 0 for i in dp: ans += i * (i - 1) // 2 print(ans) dp, 구간 합 Algorithm/boj 2022.07.03
[파이썬] 3518 공백왕 빈-칸 li, m = [], [] i = 0 while True: try: line = input().split() li.append(line) for i, l in enumerate(line): if i < len(m): m[i] = max(m[i], len(l)) else: m.append(len(l)) except: break for i, l in enumerate(li): for j, w in enumerate(l): li[i][j] = w + (m[j] - len(w)) * ' ' for l in li: print(' '.join(l).rstrip()) Algorithm/boj 2022.06.30
[파이썬] 20301 반전 요세푸스 from collections import deque n, k, m = map(int, input().split()) q = deque(range(1, n + 1)) ans = [] i = d = 1 count = 0 while q: if d == 1: cur = q.popleft() if i != k: q.append(cur) else: ans.append(str(cur)) i = 0 count += 1 else: cur = q.pop() if i != k: q.appendleft(cur) else: ans.append(str(cur)) i = 0 count += 1 if count == m: d *= -1 count = 0 i += 1 for a in ans: print(a) 큐, pypy로 제출 Algorithm/boj 2022.06.30
[파이썬] 9536 여우는 어떻게 울지? t = int(input()) for _ in range(t): li = input().split() while True: cur = input() if cur == 'what does the fox say?': break li = [l for l in li if l != cur.split()[2]] print(' '.join(li)) Algorithm/boj 2022.06.30
[파이썬] 2870 수학숙제 import re n = int(input()) li = [] for _ in range(n): line = input() li += re.compile('[0-9]+').findall(line) for l in sorted(list(map(int, li))): print(l) 정규식 Algorithm/boj 2022.06.30
[파이썬] 9417 최대 GCD from itertools import combinations def gcd(a, b): if a < b: a, b = b, a if a % b == 0: return b else: return gcd(b, a % b) n = int(input()) for _ in range(n): li = list(map(int, input().split())) com = list(combinations(li, 2)) m = 1 for c in com: m = max(m, gcd(c[0], c[1])) print(m) 완탐, 유클리드 호제법 Algorithm/boj 2022.06.30
[파이썬] 1069 집으로 x, y, d, t = map(int, input().split()) ans = distance = (x ** 2 + y ** 2) ** 0.5 q = distance // d if distance >= d: ans = min(ans, q * t + distance % d, (q + 1) * t) else: ans = min(ans, t + (d - distance), 2 * t) print(ans) 기하 Algorithm/boj 2022.06.29
[파이썬] 17386 선분 교차 1 li = [list(map(int, input().split())) for _ in range(2)] a, b, c, d = li[0][:2], li[0][2:], li[1][:2], li[1][2:] def ccw(x1, y1, x2, y2, x3, y3): c = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1) return 0 if c == 0 else c // abs(c) l1 = ccw(*a, *b, *c) * ccw(*a, *b, *d) l2 = ccw(*c, *d, *a) * ccw(*c, *d, *b) if not l1 and not l2: if a > b: a, b = b, a if c > d: c, d = d, c print(1 if a Algorithm/boj 2022.06.29
[파이썬] 1717 집합의 표현 import sys input = sys.stdin.readline n, m = map(int, input().split()) s = list(range(n + 1)) def find_parent(parent, x): if parent[x] != x: return find_parent(parent, parent[x]) return x def find_num(parent, x, y): if x == y or find_parent(parent, x) == find_parent(parent, y): print("YES") else: print("NO") def union_parent(parent, a, b): a = find_parent(parent, a) b = find_parent(parent, b) if.. Algorithm/boj 2022.06.29
[파이썬] 1956 운동 import sys input = sys.stdin.readline n, m = map(int, input().split()) INF = int(1e9) graph = [[INF] * (n + 1) for _ in range(n + 1)] for _ in range(m): a, b, c = map(int, input().split()) graph[a][b] = c for k in range(1, n + 1): for a in range(1, n + 1): for b in range(1, n + 1): graph[a][b] = min(graph[a][b], graph[a][k] + graph[k][b]) ans = INF for i in range(1, n + 1): ans = min(ans, graph[.. Algorithm/boj 2022.06.28