Upload files to "백준"

This commit is contained in:
2026-04-11 11:46:51 +09:00
parent b903155309
commit 66464134ce
5 changed files with 200 additions and 0 deletions

24
백준/1016_re.py Normal file
View File

@@ -0,0 +1,24 @@
import math
import sys
input = sys.stdin.readline
min, max = map(int, input().split())
arr = [False] * (max - min + 1)
for i in range(2, int(math.sqrt(max))+1):
pow = i * i
start_index = int(min/pow)
if min % pow != 0:
start_index+=1
for j in range(start_index, int(max/pow)+1):
arr[int(j*pow-min)]=True
count = 0
for i in range(0, max-min+1):
if not arr[i]:
count += 1
print(count)

32
백준/1021.py Normal file
View File

@@ -0,0 +1,32 @@
import sys
from collections import deque
input = sys.stdin.readline
n, m = map(int, input().split())
answer_q = deque(map(int ,input().split()))
q = deque()
for i in range(1, n+1):
q.append(i)
chance = 0
while answer_q:
k = answer_q.popleft()
if (k == q[0]):
q.popleft()
elif(q.index(k)<=len(q)//2):
while(k!=q[0]):
temp = q.popleft()
q.append(temp)
chance += 1
q.popleft()
else:
while(k!=q[0]):
temp = q.pop()
q.appendleft(temp)
chance += 1
q.popleft()
print(chance)

43
백준/1033.py Normal file
View File

@@ -0,0 +1,43 @@
import sys
import math
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
N = int(input())
A = [[] for i in range(N)]
visited = [False] * (N)
D = [0] * (N)
lcm = 1
def gcd(a,b):
if b==0:
return a
else:
return gcd(b, a%b)
def DFS(v):
visited[v] = True
for i in A[v]:
next = i[0]
if not visited[next]:
D[next] = D[v] * i[2] // i[1]
DFS(next)
for i in range(N-1):
a, b, p, q = map(int, input().split())
A[a].append((b, p, q))
A[b].append((a, q, p))
lcm *= (p*q // gcd(p, q))
D[0] = lcm
DFS(0)
mgcd = D[0]
for i in range(1, N):
mgcd = gcd(mgcd, D[i])
for i in range(N):
print(int(D[i]//mgcd), end=' ')

59
백준/1043.py Normal file
View File

@@ -0,0 +1,59 @@
import sys
import math
input = sys.stdin.readline
N, M = map(int, input().split())
isNumber = 0
trueman = list(map(int, input().split()))
human = [0] * (51)
def find(a):
if (a==human[a]):
return a
else:
human[a]=find(human[a])
return human[a]
def union(a,b):
a=find(a)
b=find(b)
if a != b:
human[a] = b
for i in range(51):
human[i] = i
trueman_set = []
if trueman[0]: # trueman 숫자 정상이라면,
trueman_set = trueman[1:]
find_trueman_set=[]
for i in trueman_set:
find_trueman_set.append(find(i))
#각 한 줄에 대해서 루트를 설정해주면 된다! 유니온을 통해서
lst = []
for i in range(M):
temp = list(map(int, input().split()))
lst.append(temp)
if (temp[0]>1):
for i in range(2, temp[0]+1):
union(temp[1], temp[i])
find_trueman_set = {find(x) for x in trueman_set}
# 각 파티에서 대표 노드가 진실을 아는 그룹과 연결되지 않았는지 확인
for party in lst:
if find(party[1]) not in find_trueman_set:
isNumber += 1
print(isNumber)

42
백준/1074.py Normal file
View File

@@ -0,0 +1,42 @@
import sys
# 표준 입력을 빠르게 받기 위한 함수 재정의
input = sys.stdin.readline
# 재귀 깊이 제한을 크게 설정하여 재귀 함수가 깊게 호출되어도 문제 없도록 함
sys.setrecursionlimit(10**6)
# N: 2^N x 2^N 크기의 배열, r: 행 번호, c: 열 번호
N, r, c = map(int, input().split())
def squared(n, r, c, tot):
# 현재 정사각형의 크기를 계산 (2^n x 2^n)
next_right, next_height = 2**n, 2**n
# 기저 사례: 정사각형의 크기가 2x2가 되었을 때 각 칸에 대해 순서를 결정
if (n == 1 and r == 0 and c == 0):
return tot # 왼쪽 위: 현재까지 누적된 tot 반환
elif (n == 1 and r == 0 and c == 1):
return tot + 1 # 오른쪽 위: tot에 1을 더해 반환
elif (n == 1 and r == 1 and c == 0):
return tot + 2 # 왼쪽 아래: tot에 2를 더해 반환
elif (n == 1 and r == 1 and c == 1):
return tot + 3 # 오른쪽 아래: tot에 3을 더해 반환
# 재귀 호출을 통해 현재 정사각형을 4개 분할 (사분면)하여 타겟 위치가 있는 사분면 선택
# 1사분면: 왼쪽 위
if (r < next_right // 2 and c < next_height // 2):
return squared(n - 1, r, c, tot)
# 2사분면: 오른쪽 위
elif (r < next_right // 2):
# 타겟이 오른쪽 위에 있으므로 왼쪽 위 사분면의 칸 수만큼 tot에 더해줌
return squared(n - 1, r, c - (2**(n - 1)), tot + 2 ** (2*n - 2))
# 3사분면: 왼쪽 아래
elif (c < next_height // 2):
# 타겟이 왼쪽 아래에 있으므로 위쪽 두 사분면의 칸 수만큼 tot에 더해줌
return squared(n - 1, r - (2**(n - 1)), c, tot + 2 ** (2*n - 1))
# 4사분면: 오른쪽 아래
else:
# 타겟이 오른쪽 아래에 있으므로 왼쪽 위, 오른쪽 위, 왼쪽 아래 사분면의 칸 수 합만큼 tot에 더해줌
return squared(n - 1, r - (2**(n - 1)), c - (2**(n - 1)), tot + (2 ** (2*n - 2) + 2 ** (2*n - 1)))
# 재귀 함수를 호출하여 결과를 출력
print(squared(N, r, c, 0))