๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/๋ฐฑ์ค€(BOJ)

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 9625 - BABBA

by YWTechIT 2021. 5. 21.
728x90

๐Ÿ“Œ ๋ฐฑ์ค€ 9625 - BABBA

๋ฐฑ์ค€ 9625 - BABBA


๐Ÿ’ก ๋‚˜์˜ ํ’€์ด

์ „ํ˜•์ ์ธ DP(Dynamic Programming)๋ฌธ์ œ์˜€์œผ๋‚˜, ์ฒ˜์Œ์— ๋ฌธ์ œ ๊ทธ๋Œ€๋กœ i๊ฐ€ ์ฆ๊ฐ€ํ•˜๋ฉด ๋ฐ”๋€Œ๋Š” ๋ฌธ์ž์—ด์„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์— ๋„ฃ๊ณ  ๋งˆ์ง€๋ง‰์— ์ด ๋ช‡ ๊ฐœ์ธ์ง€ count๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ’€๋‹ค๊ฐ€ ์‹œ๊ฐ„ ์ดˆ๊ณผ ํŒ์ •์„ ๋ฐ›์•˜๋‹ค.

 

๊ทธ๋ž˜์„œ ์–ด๋–ค ๊ทœ์น™์ด ์žˆ๋Š”์ง€ ์‚ดํŽด๋ณด๋‹ˆ๊นŒ ์ „์ฒด A, B ์ด ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•  ํ•„์š”๋Š” ์—†๊ณ  A, B์˜ ๊ฐœ์ˆ˜๋ฅผ ๋”ฐ๋กœ๋”ฐ๋กœ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค. a[i] = a[i-1] + a[i-2]์ธ ์ „ํ˜•์ ์ธ ํ”ผ๋ณด๋‚˜์น˜(Fibonacci) ์ˆ˜์—ด ํ˜•ํƒœ๋ฅผ ๋ณด์˜€๋‹ค.

 

๋‚˜๋Š” DP๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ „์ฒด k์˜ ์ตœ๋Œ€๊ฐ’๋ฅผ ์„ ์–ธํ–ˆ๋Š”๋ฐ, ๊ทธ๋Ÿด ํ•„์š” ์—†์ด k+1๋งŒํผ๋งŒ ์ค˜์„œ ๊ทธ๋•Œ๊ทธ๋•Œ ๊ณ„์‚ฐํ•ด๋„ ๋œ๋‹ค. ๋˜, ์ƒˆ๋กญ๊ฒŒ ๋ฐฐ์šด ์ ์€ a[0] = 1, a[1] = 0 ๋Œ€์‹ , ์ฒ˜์Œ๋ถ€ํ„ฐ a=[1,0]์„ ์„ ์–ธํ•˜๊ณ  DP ๋ฐ˜๋ณต๋ฌธ์—์„œ ๋‚˜์˜จ๊ฐ’์„ appendํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.

 

# ๋‚˜์˜ ์ฝ”๋“œ
k = int(input())
n = 46

a = [0] * 46
a[0] = 1
a[1] = 0

b = [0] * 46
b[0] = 0
b[1] = 1

for i in range(2, n):
    a[i] = a[i - 1] + a[i - 2]
    b[i] = b[i - 1] + b[i - 2]
print(a[k], b[k])
# ๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ
k = int(input())

a = [1, 0]
b = [0, 1]

for i in range(2, k+1):
    a_num = a[i-1] + a[i-2]
    a.append(a_num)
    b_num = b[i-1] + b[i-2]
    b.append(b_num)

print(a[k], b[k])
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€