22251-g5 성공

This commit is contained in:
2026-03-09 11:04:21 +09:00
parent 4d7b40c0fd
commit 93ea0d98a9

View File

@@ -0,0 +1,74 @@
# 빌런 호석
import sys
input = sys.stdin.readline
def check_change_table(change_table, now_floor, target_floor):
count = 0
for i in range(len(now_floor)):
count += change_table[int(now_floor[i])][int(target_floor[i])]
return count
def count_diff(led1, led2):
count = 0
for i in range(len(led1)):
if led1[i] != led2[i]:
count += 1
return count
def fill_change_table(change_table, led):
for i in range(10):
for j in range(10):
if i == j:
continue
if change_table[i][j] != 0:
continue
change_table[i][j] = count_diff(led[i], led[j])
return
def solution():
led = [[0]*7 for _ in range(10)]
led[0] = [1, 1, 1, 0, 1, 1, 1]
led[1] = [0, 0, 1, 0, 0, 1, 0]
led[2] = [1, 0, 1, 1, 1, 0, 1]
led[3] = [1, 0, 1, 1, 0, 1, 1]
led[4] = [0, 1, 1, 1, 0, 1, 0]
led[5] = [1, 1, 0, 1, 0, 1, 1]
led[6] = [1, 1, 0, 1, 1, 1, 1]
led[7] = [1, 0, 1, 0, 0, 1, 0]
led[8] = [1, 1, 1, 1, 1, 1, 1]
led[9] = [1, 1, 1, 1, 0, 1, 1]
change_table = [[0]*10 for _ in range(10)]
fill_change_table(change_table, led)
n, k, p, x = map(int, input().rstrip().split())
result = 0
now_floor = "0"*(k-len(str(x))) + str(x)
for i in range(1, n+1):
if i == x:
continue
target_floor = "0"*(k-len(str(i))) + str(i)
if check_change_table(change_table, now_floor, target_floor) <= p:
result += 1
print(result)
return
solution()
"""
걸린 시간: 46분
시간 복잡도: 필요한 반전 횟수를 기록한 테이블을 만드는데 상수시간이다.
모든 층에 대해 반전횟수 테이블을 참조하여 계산해보기 때문에 O(n)이다.
해설: led가 켜진 곳은 1, 꺼진 곳은 0으로 해서 두 수를 비교했을 때 다른 숫자인 곳이 필요한 반전 횟수이다.
반전횟수 테이블을 가지고 현재 층에서 모든 층을 타겟으로 p보다 반전 횟수가 적은 것의 개수가 답이다.
"""