def find_brackt():
    for ind,command in enumerate(commands):
        if command == '[':
            bracket.append((ind,1))
        elif command == ']':
            bracket.append((ind,-1))
    brind = 0
    temp = []
    while brind < len(bracket): 
        if bracket[brind][1] == 1:
            temp.append(bracket[brind][0])
        else:
            open_bracket = temp.pop()
            bracket_couple[bracket[brind][0]] = open_bracket
            bracket_couple[open_bracket] = bracket[brind][0]
        brind += 1

def go():
    global command_index,string_index,pointer
    if commands[command_index] == '-':
        arr[pointer] = arr[pointer]-1
        if arr[pointer]<0:
            arr[pointer] = 255
    elif commands[command_index] == '+':
        arr[pointer] = arr[pointer] + 1
        if arr[pointer] > 255:
            arr[pointer] = 0
    elif commands[command_index] == '<':
        pointer -= 1
        if pointer < 0:
            pointer = memorysize -1
    elif commands[command_index] == '>':
        pointer += 1
        if pointer == memorysize:
            pointer = 0
    elif commands[command_index] == '[':
        if arr[pointer] == 0:
            command_index = bracket_couple[command_index]
    elif commands[command_index] == ']':
        if arr[pointer] != 0:
            command_index = bracket_couple[command_index]
    elif commands[command_index] == '.':
        pass
    else:
        if string_index == inputsize:
            arr[pointer] = 255
        else:
            arr[pointer] = ord(strings[string_index])
            string_index += 1
    command_index += 1

T = int(input())
MAX_NUMBER = 50000000
for tc in range(T):
    memorysize,codesize,inputsize = map(int,input().split())
    pointer = 0 # 포인터가 가리키는 index
    arr = [0]*memorysize
    command_cnt = 0  # 명령 횟수
    commands = list(input())
    strings = list(input())
    command_index = 0  # 명령어의 위치
    string_index = 0
    result = 'Terminates'
    bracket = []
    bracket_couple = {}
    find_brackt()
    loop_flag = False
    loop_idx = float('inf')
    while command_cnt <= MAX_NUMBER and command_index < codesize:
        command_cnt += 1
        go()
    if command_index == codesize:
        print('Terminates')
    else:
        while command_cnt >=0:
            command_cnt -= 1
            go()
            loop_idx = min(command_index,loop_idx)
        print(f'Loops {loop_idx-1} {bracket_couple[loop_idx-1]}')

 

 

def find_brackt():
    for ind,command in enumerate(commands):
        if command == '[':
            bracket.append((ind,1))
        elif command == ']':
            bracket.append((ind,-1))
    brind = 0
    temp = []
    while brind < len(bracket): 
        if bracket[brind][1] == 1:
            temp.append(bracket[brind][0])
        else:
            open_bracket = temp.pop()
            bracket_couple[bracket[brind][0]] = open_bracket
            bracket_couple[open_bracket] = bracket[brind][0]
        brind += 1



T = int(input())
MAX_NUMBER = 50000000
for tc in range(T):
    memorysize,codesize,inputsize = map(int,input().split())
    pointer = 0 # 포인터가 가리키는 index
    arr = [0]*memorysize
    command_cnt = 0  # 명령 횟수
    commands = list(input())
    strings = list(input())
    command_index = 0  # 명령어의 위치
    string_index = 0
    result = 'Terminates'
    bracket = []
    bracket_couple = {}
    find_brackt()
    loop_flag = False
    loop_idx = float('inf')
    while command_index < codesize:
        if commands[command_index] == '-':
            arr[pointer] = (arr[pointer]-1)%(2**8)
        elif commands[command_index] == '+':
            arr[pointer] = (arr[pointer]+1)%(2**8)
        elif commands[command_index] == '<':
            pointer -= 1
            if pointer < 0:
                pointer = memorysize -1
        elif commands[command_index] == '>':
            pointer += 1
            if pointer == memorysize:
                pointer = 0
        elif commands[command_index] == '[':
            if arr[pointer] == 0:
                command_index = bracket_couple[command_index]
        elif commands[command_index] == ']':
            if arr[pointer] != 0:
                command_index = bracket_couple[command_index]
        elif commands[command_index] == '.':
            pass
        else:
            if string_index == inputsize:
                arr[pointer] = 255
            else:
                arr[pointer] = ord(strings[string_index])
                string_index += 1
        command_index += 1
        command_cnt += 1
        if command_cnt > MAX_NUMBER:
            loop_idx = min(loop_idx,command_index)
        if command_cnt > MAX_NUMBER*2:
            loop_flag = True
            break
    
    if loop_flag:
        print(f'Loops {loop_idx-1} {bracket_couple[loop_idx-1]}')
    else:
        print('Terminates')
import sys

input = sys.stdin.readline

T = int(input())

def find_parents(node):
    parent_list = [node]

    while parent_num[node] != -1:
        parent = parent_num[node]
        parent_list.append(parent)
        node = parent

    return parent_list



for _ in range(T):
    N = int(input())
    parent_num = [-1]*(N+1) # 해당 index의 부모가 안에 들어가 있다.
    for _ in range(N-1):
        parent,child = map(int,input().split())
        parent_num[child] = parent


    num1,num2 = map(int,input().split())

    parents1 = find_parents(num1)
    parents2 = find_parents(num2)

    if len(parents1) < len(parents2):
        parents1,parents2 = parents2, parents1
    result = -1
    for num in parents1:
        if num in parents2:
            result = num
            break
    print(result)
def check(number):
    remain_person = M

    for k in time_list:
        remain_person = remain_person - number//k
        if remain_person <=0:
            return -1
    return 1


N,M = map(int,input().split())
time_list = [int(input()) for _ in range(N)]

start = 1
end = ((M//N)+1)*max(time_list)
while start<end:
    mid = (start+end)//2
    remain_person = check(mid)

    if remain_person > 0:
        start = mid + 1
    else:
        end = mid -1
print(end)
import sys

input = sys.stdin.readline

N = int(input())


bit_cnt_list = [0]*1024
for _ in range(N):
    num = input().strip()
    temp = 0
    for bit_num in num:
        temp = temp | 2**(int(bit_num))
    bit_cnt_list[temp] += 1
result = 0
for k in range(1,1024):
    for t in range(k,1024):
        if k == t:
            if bit_cnt_list[k] >= 2:
                result = result + (bit_cnt_list[k]*(bit_cnt_list[k]-1))//2
        else:
            if bit_cnt_list[k] != 0 or bit_cnt_list[t] != 0:
                if k&t >0:
                    result = result + bit_cnt_list[k]*bit_cnt_list[t]

print(result)
T = int(input())
dp = [[0]*10 if i !=0 else [1]*10 for i in range(65)]
visited = [0]*64
visited[0] = 1
last_n = 0
for ind in range(T):
    n = int(input())
    if visited[n-1]:
        print(sum(dp[n-1]))
    else:
        for ind in range(last_n+1,n):
            for num in range(9,-1,-1):
                if num == 9:
                    dp[ind][num] = sum(dp[ind-1])
                else:
                    dp[ind][num] = dp[ind][num+1] - dp[ind-1][num+1]
        last_n = max(last_n,n-1)

        print(sum(dp[n-1]))
T = int(input())
dp = [[1]*10 if ind == 0 else [0]*10 for ind in range(64)]
result = [0]*64
result[0] = 10
for ind in range(1,64):
    for num in range(9,-1,-1):
        if num == 9:
            dp[ind][num] = result[ind-1]
        else:
            dp[ind][num] = dp[ind][num+1] - dp[ind-1][num+1]
    result[ind] = sum(dp[ind])

for _ in range(T):
    n = int(input())
    print(result[n-1])
def checkminNumber(index,open1,open2):
    if index > M-1:
        return 0 
    result = dp[index][open1][open2]
    if result != float('inf'):
        return result
    next_open = output_list[index]
    dp[index][open1][open2] = min(abs(open1 - next_open) + checkminNumber(index+1,next_open,open2), abs(open2 - next_open) + checkminNumber(index+1,open1,next_open))
    return dp[index][open1][open2]



N = int(input())

open1,open2 = list(map(int,input().split()))

M = int(input())
dp = [[[float('inf')]*(N+1) for _ in range(N+1)] for _ in range(M)]
output_list = []

for _ in range(M):
    output_list.append(int(input()))
print(checkminNumber(0,open1,open2))

 

import sys
import collections
input = sys.stdin.readline

def bfs1(x):
    que=collections.deque()
    que.append(x)
    temp=set()
    temp.add(x)
    while que:
        node=que.popleft()
        for i in graph1[node]:
            if i not in temp:
                temp.add(i)
                visit[x]+=1       
                que.append(i)
    return
def bfs2(x):
    que=collections.deque()
    que.append(x)
    temp=set()
    temp.add(x)
    while que:
        node=que.popleft()
        for i in graph2[node]:            
            if i not in temp:
                temp.add(i)
                visit[x]+=1       
                que.append(i)
    return
 

N,M = map(int,input().split())
arr=[list(map(int,input().split())) for i in range(M)]
graph1=[[] for i in range(N+1)]
graph2=[[] for i in range(N+1)]
for i in arr:
    graph1[i[0]].append(i[1])
    graph2[i[1]].append(i[0])
visit=[0]*(N+1)
for i in range(1,N+1):        
    bfs1(i)
    bfs2(i)

cnt=visit.count(N-1)
print(cnt)
def gcd(A,B):
    if not A%B:
        return B
    return gcd(B,A%B)


N, M = map(int,input().split())
ab = M//N
list_a = []
last_N = int(ab**(1/2))
for number_a in range(last_N,0,-1):
    if not ab%number_a:
        number_b = ab//number_a
        if gcd(number_a,number_b) == 1:
            list_a.extend([number_a,number_b])
            break
print(*[ i*N for i in list_a])

 

'알고리즘 > 백준_복기_미완료' 카테고리의 다른 글

[BOJ/백준] 2666 벽장문의 이동  (0) 2021.05.03
[BOJ/백준] 2458 키순서  (0) 2021.05.03
[BOJ/백준] 2122 센서  (0) 2021.05.02
[BOJ/백준] 2156 포도주 시식  (0) 2021.05.02
[BOJ/백준] 2141 우체국  (0) 2021.05.02

+ Recent posts