python-algorithm

백준 10826 피보나치 수 4

무적김두칠 2021. 12. 22. 14:04

1
2
3
4
5
6
7
8
from functools import lru_cache
 
@lru_cache()
def nth_fibo(n):
    if n==0return 0
    elif n==1 or n==2 : return  1
    else : return nth_fibo(n-1)+nth_fibo(n-2)
print(nth_fibo(int(input())))
cs

 

1
2
3
4
5
6
7
8
9
from functools import lru_cache
import sys
sys.setrecursionlimit(10**6)
@lru_cache()
def nth_fibo(n):
    if n==0return 0
    elif n==1 or n==2 : return  1
    else : return nth_fibo(n-1)+nth_fibo(n-2)
print(nth_fibo(int(input())))
cs

 

Line 4는 함수의 결과를 캐시에 가지고 있게 해주는 함수입니다

1번처럼했다가 recursion에러가 나와서 백준 문서(https://help.acmicpc.net/judge/rte/RecursionError)를 읽어보니
백준 채점서버에는 재귀깊이가 1000으로 돼있어서 그 limit를 Line3 과 같이 키워줘서 해결했습니다 

반응형