์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Python

[BOJ/Step15] 9461 : ํŒŒ๋„๋ฐ˜ ์ˆ˜์—ด (Python)

NaNaRin๐Ÿ™ƒ 2021. 2. 24. 21:22

www.acmicpc.net/problem/9461

 

9461๋ฒˆ: ํŒŒ๋„๋ฐ˜ ์ˆ˜์—ด

์˜ค๋ฅธ์ชฝ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์‚ผ๊ฐํ˜•์ด ๋‚˜์„  ๋ชจ์–‘์œผ๋กœ ๋†“์—ฌ์ ธ ์žˆ๋‹ค. ์ฒซ ์‚ผ๊ฐํ˜•์€ ์ •์‚ผ๊ฐํ˜•์œผ๋กœ ๋ณ€์˜ ๊ธธ์ด๋Š” 1์ด๋‹ค. ๊ทธ ๋‹ค์Œ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •์œผ๋กœ ์ •์‚ผ๊ฐํ˜•์„ ๊ณ„์† ์ถ”๊ฐ€ํ•œ๋‹ค. ๋‚˜์„ ์—์„œ ๊ฐ€์žฅ ๊ธด ๋ณ€์˜

www.acmicpc.net


๊ทธ๋ฆผ์„ ๋ณด๋ฉด ์ ํ™”์‹์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค

 

๋ณด๋ฉด ์ˆœ์„œ๋Œ€๋กœ

1 1 1 2 2 3 4 5 7 9 12 … ์ธ๋ฐ

3๋ถ€ํ„ฐ

3 = 1 + 2 = w(1) + w(5) = w(6)

4 = 1 + 3 = w(2) + w(6) = w(7)

5 = 1 + 4 = w(3) + w(7) = w(8)

7 = 2 + 7 = w(4) + w(8) = w(9)

9 = 2 + 7 = w(5) + w(9) = w(10)

12 = 3 + 9 = w(6) + w(10) = w(11)

์ธ ๊ฑธ ์•Œ ์ˆ˜ ์žˆ๋‹ค

๊ฒฐ๊ตญ w(n) = w(n-1) + w(n-5)

 

๊ทผ๋ฐ ๋˜ t๋ฅผ ๋ฐ›๊ณ  t๋ฒˆ ๋ฐ˜๋ณตํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—

์ž…๋ ฅ๊ฐ’ n์ด ๋ฏธ๋ฆฌ ๋ฐฐ์—ด์— ๋„ฃ์–ด๋‘” ์ˆ˜๋ฅผ ๋„˜์–ด๊ฐ€๋ฉด ๋งค๋ฒˆ ์ƒˆ๋กœ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—

์ด์ „์— ์ž…๋ ฅ๋ฐ›์€ n๋ณด๋‹ค ์ž‘์€ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ๋ฐ”๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก ์กฐ๊ฑด๋ฌธ์„ ์ถ”๊ฐ€ํ•œ๋‹ค

 

1. n์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค

2. n์ด wave์˜ ๊ธธ์ด -1๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด wave[n]์„ ๋ฐ”๋กœ ์ถœ๋ ฅํ•œ๋‹ค

3. for๋ฌธ์„ ํ†ตํ•ด wave[len(wave) = l]๋ถ€ํ„ฐ wave[n]๊นŒ์ง€ ์ถ”๊ฐ€ํ•œ๋‹ค

4. wave[n]์„ ์ถœ๋ ฅํ•œ๋‹ค

 

# 9461.py

t = int(input())
wave = [0, 1, 1, 1, 2, 2, 3, 4, 5, 7, 9]

for i in range(t):
    n = int(input())
    if n <= len(wave) - 1:
        print(wave[n])
    else:
        for l in range(len(wave), n+1):
            wave.append(wave[l-1] + wave[l-5])
        print(wave[n])