Post

🐹 36μ§„μˆ˜

1. 문제 링크

1036번: 36μ§„μˆ˜


2. μ½”λ“œ

Python3 31120KB 48ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
N = int(input())
numbers = []
reverse_numbers = []
for _ in range(N):
    numbers.append(input().replace(' ', ''))
K = int(input())

def change_36_to_demical(x):
    if 48 <= ord(x) <= 57:
        return int(x)
    else:
        return ord(x) - 55

def change_demical_to_36(num):
    if 0 <= num <= 9:
        return str(num)
    else:
        return chr(num + 55)

# μ€‘μš”λ„ 계산(μ€‘μš”λ„, μœ„μΉ˜)
importance = [[0, i] for i in range(36)]
for n in numbers:
    number = list(n)
    number.reverse()
    reverse_numbers.append(number)
    for i in range(len(number)):
        alpha = number[i]
        # 이미 ZλŠ” λ³€κ²½ν•  ν•„μš” μ—†μŒ
        if alpha == 'Z':
            continue
        dnum = change_36_to_demical(alpha)
        # μžλ¦Ώμˆ˜μ™€ 숫자의 크기 λͺ¨λ‘λ‘œ μ€‘μš”λ„ 계산(더 μž‘μ€ μˆ«μžκ°€ λ³€κ²½ μ€‘μš”λ„ λ†’λ‹€)
        importance[dnum][0] += (35-dnum) * (36 ** i)

importance.sort(key=lambda x: -x[0])
importance = importance[:K]
change_num = []
for im in importance:
    change_num.append(im[1])

# 10μ§„μˆ˜λ‘œ κ³„μ‚°ν•œ 숫자
demical_num = 0
for r_num in reverse_numbers:
    for i in range(len(r_num)):
        dnum = change_36_to_demical(r_num[i])
        # Z둜 λ³€ν™˜ν•˜λŠ” 숫자
        if dnum in change_num:
            dnum = 35
        demical_num += dnum * (36 ** i)

answer = ''
while demical_num:
    n = change_demical_to_36(demical_num % 36)
    demical_num //= 36
    answer = n + answer

# '002'와 같은 λ‹΅μΌλ•Œ μ•žμ˜ '00' 제거
answer = list(answer)
for i in range(len(answer)):
    if answer[i] != 0:
        answer = answer[i:]
        break
if answer:
    print(''.join(answer))
else:
    print('0')


3. ν•΄μ„€

각 36μ§„μˆ˜ 숫자의 μ€‘μš”λ„λ₯Ό κ³„μ‚°ν•΄μ•Όν•œλ‹€. μ€‘μš”λ„λŠ” $(35-숫자)*36^{자릿수}$둜 κ³„μ‚°ν•˜λŠ” 것이 μ •ν™•ν•˜λ‹€.

35-숫자λ₯Ό ν•΄μ€€ 것은 μˆ«μžκ°€ μž‘μ„μˆ˜λ‘ μ€‘μš”λ„κ°€ 높아지기 λ•Œλ¬Έμ— κ°€μž₯ 큰 숫자 Z(=35)μ—μ„œ λΉΌμ£ΌλŠ” 것이닀.

μ€‘μš”λ„λ‘œ 정해진 μˆ«μžλŠ” λͺ¨λ‘ 35둜 λ°”κΏ”μ€€ ν›„ 10μ§„μˆ˜ 계산을 ν•œλ‹€.

10μ§„μˆ˜λ‘œ κ³„μ‚°λœ 숫자λ₯Ό 36으둜 λ‚˜λˆ κ°€λ©° 36μ§„μˆ˜ 숫자둜 λ°”κΏ”μ€€λ‹€.

μ΄λ•Œ, μ΅œμ’… μˆ«μžκ°€ β€˜000’ ν˜Ήμ€ β€˜00203’ λ“± 0으둜 μ‹œμž‘ν•˜λŠ” 경우 β€˜0’ ν˜Ήμ€ β€˜203β€™μœΌλ‘œ λ°”κΏ”μ€€λ‹€.

This post is licensed under CC BY 4.0 by the author.