Post

๐Ÿข ์นด๋“œ ์„ž๊ธฐ

1. ๋ฌธ์ œ ๋งํฌ

21315๋ฒˆ: ์นด๋“œ ์„ž๊ธฐ


2. ์ฝ”๋“œ

PyPy3 110404KB 112ms

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
"""
[21315] ์นด๋“œ ์„ž๊ธฐ

๐Ÿ’› ๋ฌธ์ œ
๋งˆ์ˆ ์‚ฌ ์˜์žฌ๋Š” ์นด๋“œ ๋”๋ฏธ๋ฅผ ์ด์šฉํ•œ ๋งˆ์ˆ ์„ ๊ฐœ๋ฐœํ•˜์˜€๋‹ค.
์นด๋“œ๋“ค์—๋Š” 1๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ์ˆซ์ž๊ฐ€ ์ ํ˜€์žˆ์œผ๋ฉฐ ์ดˆ๊ธฐ ์ƒํƒœ์—๋Š” 1์ด ๋งจ ์œ„์— ์žˆ์œผ๋ฉฐ N๊ฐœ์˜ ์นด๋“œ๊ฐ€ ๋ฒˆํ˜ธ ์ˆœ์„œ๋Œ€๋กœ ์Œ“์—ฌ์žˆ๋‹ค.

์˜์žฌ๋Š” ๋งˆ์ˆ ์„ ์œ„ํ•ด (2, K) - ์„ž๊ธฐ๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.
(2, K) - ์„ž๊ธฐ๋Š” ์ด K + 1๊ฐœ์˜ ๋‹จ๊ณ„๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ๋‹ค.
์ฒซ ๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” ์นด๋“œ ๋”๋ฏธ ์ค‘ ๋ฐ‘์—์„œ 2K๊ฐœ์˜ ์นด๋“œ๋ฅผ ๋”๋ฏธ์˜ ๋งจ ์œ„๋กœ ์˜ฌ๋ฆฐ๋‹ค.
์ดํ›„ i(2 โ‰ค i โ‰ค K + 1)๋ฒˆ์งธ ๋‹จ๊ณ„๋Š” ์ง์ „์— ๋งจ ์œ„๋กœ ์˜ฌ๋ฆฐ ์นด๋“œ ์ค‘ ๋ฐ‘์—์„œ 2K - i + 1๊ฐœ์˜ ์นด๋“œ๋ฅผ ๋”๋ฏธ์˜ ๋งจ ์œ„๋กœ ์˜ฌ๋ฆฐ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์นด๋“œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 5๊ฐœ ์ผ ๋•Œ ์ดˆ๊ธฐ ์ƒํƒœ์—์„œ (2, 2) - ์„ž๊ธฐ๋ฅผ ํ•˜๋Š” ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
(๊ด„ํ˜ธ ๋‚ด์—์„œ ์™ผ์ชฝ์— ์žˆ์„์ˆ˜๋ก ์œ„์— ์žˆ๋Š” ์นด๋“œ์ด๋‹ค.)
(1, 2, 3, 4, 5) โ†’ (2, 3, 4, 5, 1) โ†’ (4, 5, 2, 3, 1) โ†’ (5, 4, 2, 3, 1)
์˜์žฌ์˜ ๋งˆ์ˆ ์€ ์ƒ๋Œ€๋ฐฉ์ด ์ดˆ๊ธฐ ์ƒํƒœ์—์„œ (2, K) - ์„ž๊ธฐ๋ฅผ 2๋ฒˆ ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ณ  2๋ฒˆ์˜ (2, K) - ์„ž๊ธฐ์—์„œ K๊ฐ€ ๊ฐ๊ฐ ๋ฌด์—‡์ธ์ง€ ๋งž์ถ”๋Š” ๋งˆ์ˆ ์ด๋‹ค.

๋งˆ์ˆ  ์•„์ด๋””์–ด๋Š” ์ƒ๊ฐํ–ˆ์ง€๋งŒ, K๋ฅผ ์•Œ์•„๋‚ด๋Š” ๋ฐฉ๋ฒ•์„ ๋ชจ๋ฅด๋Š” ์˜์žฌ๋ฅผ ์œ„ํ•ด K๋ฅผ ์•Œ์•„๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์ž.
2๋ฒˆ์˜ (2, K) - ์„ž๊ธฐ ํ›„์˜ ์นด๋“œ ๋”๋ฏธ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฐ๊ฐ์˜ K๋Š” ์œ ์ผํ•จ์ด ๋ณด์žฅ๋œ๋‹ค.

๐Ÿ’š ์ž…๋ ฅ
์ฒซ ์ค„์— N์ด ์ฃผ์–ด์ง„๋‹ค.
๋‘˜์งธ ์ค„์— 2๋ฒˆ์˜ (2, K) - ์„ž๊ธฐ ํ›„์˜ ์นด๋“œ ๋”๋ฏธ๊ฐ€ ์œ„์— ์žˆ๋Š” ์นด๋“œ๋ถ€ํ„ฐ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ฃผ์–ด์ง„๋‹ค.

๐Ÿ’™ ์ถœ๋ ฅ
์ฒซ ๋ฒˆ์งธ K์™€ ๋‘ ๋ฒˆ์งธ K๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
"""

# n๊ฐœ์˜ ์นด๋“œ,
# ์Œ์„ ์ˆœ์„œ๋Œ€๋กœ ๋ชจ๋‘ ๊ฒ€์‚ฌ(์นด๋“œ ์„ž๊ธฐ)ํ•˜์—ฌ ์ž…๋ ฅ ๊ฐ’๊ณผ ๊ฐ™์€ ์Œ์„ ์ฐพ๋Š” ๊ฒƒ

import math
n = int(input())
array = list(map(int, input().split()))

def card_shuffle(cardlist, index):
    if index == 0:
        return cardlist
    divided = cardlist[len(cardlist)-index:]
    return card_shuffle(divided, index//2) + cardlist[:len(cardlist)-index]


result = []
# 2^(k-i+1)๊ฐœ์˜ ์นด๋“œ๋ฅผ ๋”๋ฏธ์˜ ๋งจ ์œ„๋กœ ์˜ฌ๋ฆฐ๋‹ค
M = int(math.log2(n))
for k1 in range(1, M+1):
    for k2 in range(1, M+1):
        cards = [i for i in range(1, n+1)]
        if card_shuffle(card_shuffle(cards, 2**k1), 2**k2) == array:
            result.append((k1, k2))

print(*result[0])
  • ํ•ด์„ค
This post is licensed under CC BY 4.0 by the author.