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
def to_36(number):
    global BASE
    
    a = number // 36
    b = number % 36
    
    if not a:
        return BASE[b]
    else:
        return to_36(a) + BASE[b]

# 36์ง„์ˆ˜
BASE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

N = int(input())

NUMBERS = []
for i in range(N):
    NUMBERS.append(input())

K = int(input())

# ๊ฐ€์ค‘์น˜ ์ดˆ๊ธฐํ™”
WEIGHT = dict()
for i in range(36):
    WEIGHT[BASE[i]] = 0
    
for i in range(len(NUMBERS)):
    number = NUMBERS[i]
    
    # ๊ฐ ์ž๋ฆฟ์ˆ˜ ๋งŒํผ ๊ฐ€์ค‘์น˜ ์ฆ๊ฐ€ (์ž‘์€ ์ˆ˜๋ฅผ Z ๋กœ ๋ฐ”๊ฟ€์ˆ˜๋ก ๊ฐ’์€ ๋” ์ปค์ง)
    length = len(number);
    for j in range(length):
        digit = number[j]
        WEIGHT[digit] += (35 - int(digit, 36)) * (36 ** (length - j - 1))
    
# ๊ฐ€์ค‘์น˜๊ฐ€ ๋†’์€ ์ˆœ์œผ๋กœ ์ •๋ ฌ ํ›„ K ๊ฐœ pick
sorted_weight = sorted(WEIGHT.items(), key= lambda x: -x[1])[:K]
picked = [item[0] for item in sorted_weight]

sum = 0
for i in range(N):
    number = NUMBERS[i]
    current = ""
    for ch in number:
        if ch in picked:
            current = current + "Z"
        else:
            current = current + ch
    sum += int(current, 36)
    
print(to_36(sum))


3. ํ•ด์„ค

๊ฐ ์ž๋ฆฟ์ˆ˜ ๋ณ„ ๊ฐ€์ค‘์น˜๋ฅผ 36์˜ ์ œ๊ณฑ์œผ๋กœ ๋”ํ•ด์คŒ (ํŒŒ์ด์ฌ์˜ biginteger ์ฒ˜๋ฆฌ ๋•๋ถ„์— ๊ฐ€๋Šฅ)

์ด ๋•Œ ์ค‘์š”ํ•œ ์ ์€ ์ž‘์€ ์ˆ˜๋ฅผ Z ๋กœ ๋ฐ”๊ฟจ์„ ๋•Œ ์ตœ๋Œ“๊ฐ’์— ๊ฐ€๊นŒ์›Œ์ง€๊ธฐ ๋•Œ๋ฌธ์—

(35 - {ํ•ด๋‹น ์ˆ˜์˜ ๊ฐ’}) * (36^{์ž๋ฆฟ์ˆ˜}) ๋ผ๋Š” ์ˆ˜์‹์œผ๋กœ ๊ฐ€์ค‘์น˜๋ฅผ ๊ตฌํ•จ

โ†’ Z ์˜ ๊ฐ€์ค‘์น˜๋Š” 0์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— Z โ†’ Z๋กœ ๋ฐ”๊พธ๋Š” ์—ฐ์‚ฐ์€ 36๊ฐœ๋ฅผ ๋ชจ๋‘ ๊ณ ๋ฅผ ๋•Œ ์ด์™ธ์—๋Š” ์กด์žฌํ•˜์ง€ ์•Š์Œ

๊ฐ€์ค‘์น˜๊ฐ€ ๊ฐ€์žฅ ํฐ ๊ฒƒ๋“ค์„ K๊ฐœ pick ํ•ด์„œ Z๋กœ ๋ฐ”๊พผํ›„ ๋ง์…ˆ

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