본문 바로가기

분류 전체보기

(402)
[백준 9463번] 순열 그래프 (Python3) def update(i): while i
[백준 7578번] 공장 (Python3) def update(i): while i
[백준 3111번] 검열 (Python3) from collections import * def delete(T,stack,A,p): while T: if p: stack.append(T.pop()) else: stack.append(T.popleft()) if stack[-1]==A[-1]: if stack[-N:]==A: for _ in range(N): stack.pop() return 1 return A = [*input()]; A2 = [*reversed(A)]; N = len(A) T = deque([*input()]) front = []; back = [] while delete(T,front,A,0) and delete(T,back,A2,1): continue while delete(back,front,A,1): continue p..
[백준 21761번] 초직사각형 (Python3) import sys input = sys.stdin.readline def m(): return L[0]*L[1]*L[2]*L[3] def findmax(): idx,MAX = -1,-1 for i in range(4): if Q[i]: L[i] += Q[i][-1] mm = m() if MAX < mm: MAX = mm; idx = i L[i] -= Q[i][-1] return idx N,M = map(int,input().split()) L = [*map(int,input().split())] Q = [[] for i in range(4)] for _ in range(N): a,b = input().split() Q[ord(a)-65].append(int(b)) for i in range(4): Q[..
[백준 10217번] KCM Travel (Python3) import sys input = sys.stdin.readline from heapq import * for _ in range(int(input())): N,M,K = map(int,input().split()) graph = [[] for i in range(N)] for _ in range(K): a,b,c,d = map(int,input().split()) graph[a-1].append((b-1,c,d)) hq = [(0,0,0)]; distance = [1e9]*N; DP = [1e9]*N while hq: c,now,d = heappop(hq) if DP[now] c+c1: DP[next..
[백준 16118번] 달빛 여우 (Python3) import sys input = sys.stdin.readline from heapq import * N,M = map(int,input().split()) graph = [[] for i in range(N)] for _ in range(M): a,b,c = map(int,input().split()) graph[a-1].append((b-1,c*2)) graph[b-1].append((a-1,c*2)) fox = [1e12]*N; hq = [(0,0)] while hq: w,now = heappop(hq) if fox[now] w+w1: fox[next] = w+w1 heappush(hq,(w+w1,..
[백준 10090번] Counting Inversions (Python3) def update(i): while i
[백준 11778번] 피보나치 수와 최대공약수 (Python3) import math mod = 10**9+7 memo = {1:1,2:1,3:2,4:3,5:5} def Fibo(n): if memo.get(n): return memo[n] else: if n%2 == 1: memo[n] = (Fibo(n//2)**2+Fibo(n//2+1)**2)%mod else: memo[n] = (Fibo(n+1)-Fibo(n-1))%mod return memo[n] N,M = map(int,input().split()) print(Fibo(math.gcd(N,M))) 피보나치 수 응용문제 피보나치 수 구하는 코드는 [백준 11444번] 피보나치 수 6 (Python3) (tistory.com) 를 재탕하였다. 처음에는 그냥 Fibo(N),Fibo(M)의 gcd를 구하면 된다고 ..