Skip to content

Commit

Permalink
Merge pull request #97 from HYU-PS-STUDY/leGit-y
Browse files Browse the repository at this point in the history
[week-14] 1562, 13549
  • Loading branch information
clean2001 authored Apr 29, 2024
2 parents f5489c2 + 6e473c7 commit 6d6c83e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
Empty file removed week14/leGit-y/.gitkeep
Empty file.
32 changes: 32 additions & 0 deletions week14/leGit-y/13549.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import heapq
import sys
input = sys.stdin.readline

INF = int(1e9)

N, K = map(int, input().split())
distance = [INF] * 100001

def dijkstra(start):
distance[start] = 0
q = []
heapq.heappush(q, (0, start))

while q:
dist, now = heapq.heappop(q)
if distance[now] < dist:
continue
for n in (now+1, now-1, now*2):
if n < 0 or n > 100000:
continue

cost = dist
if n != now*2:
cost = dist + 1

if cost < distance[n]:
distance[n] = cost
q.append((cost, n))

dijkstra(N)
print(distance[K])
48 changes: 48 additions & 0 deletions week14/leGit-y/1562.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
10844. 쉬운 계단 수 문제와 유사
다른 점은 0~9까지의 모든 수를 써야한다는 조건이 추가된 부분
해당 부분을 체크하기 위한 bitmask 저장공간만 추가
dp[글자길이][마지막수][방문집합(bitmask)]
"""
import sys
input = sys.stdin.readline

MOD = 10**9
N = int(input())

"""
dp[수의길이][마지막수][방문집합]
1) 방문집합: 0~9까지의 수들 중 어느것 포함되는지 체크
크기: 1<<10
2) 마지막수: 가능한 수들 (0~9)
크기: 10
3) 수의길이: 입력값 (N)
"""
dp = [[[0 for _ in range(1<<10)] for _ in range(10)] for _ in range(N + 1)]

# 0으로 시작하든 수는 계단수 x, 따라서 범위 1부터 시작
for k in range(1, 10):
dp[1][k][1 << k] = 1

for i in range(2,N+1):
for j in range(10):
for k in range(1<<10):
bit = k | (1<<j)
# 마지막 수가 0인 경우
if j == 0:
# 그 이전수는 무조건 1이었어야
dp[i][j][bit] += dp[i-1][1][k] % MOD
# 마지막 수가 9인 경우
elif j == 9:
# 그 이전수는 무조건 8이었어야
dp[i][j][bit] += dp[i-1][8][k] % MOD
else:
dp[i][j][bit] += (dp[i-1][j-1][k] + dp[i-1][j+1][k]) % MOD


answer = 0
for j in range(10):
answer += dp[N][j][(1 << 10) - 1]
answer %= MOD

print(answer)

0 comments on commit 6d6c83e

Please sign in to comment.