11444 swift - 피보나치 수 6
2023. 6. 11. 02:16ㆍ🐣/BOJ
import Foundation
let DIV: UInt = 1000000007
var f = [UInt: UInt]()
func fibo(_ n: UInt) -> UInt {
if n == 0 { return 0 }
if n == 1 { return 1 }
if n == 2 { return 1 }
if let val = f[n] {
return val
}
if n % 2 == 0 {
let m = n / 2
let temp1 = fibo(m - 1)
let temp2 = fibo(m)
f[n] = ((2 * temp1 + temp2) * temp2) % DIV
} else {
let m = (n + 1) / 2
let temp1 = fibo(m)
let temp2 = fibo(m - 1)
f[n] = (temp1 * temp1 + temp2 * temp2) % DIV
}
return f[n]!
}f
var n = UInt(readLine()!)!
print(fibo(n))
'🐣 > BOJ' 카테고리의 다른 글
9935 swift - 문자열 폭발 (0) | 2024.02.02 |
---|---|
9663 swift - N-Queen (0) | 2023.06.12 |
11521 swift - Boggle (0) | 2023.04.05 |
9019 swift - DSLR (0) | 2023.03.03 |
6186 swift - Best Grass (1) | 2023.02.27 |