본문 바로가기

BOJ

[BOJ][Python][C++] 백준 24417번 - 알고리즘 수업 - 피보나치 수 2

728x90

문제 링크: https://www.acmicpc.net/problem/24417

 

24417번: 알고리즘 수업 - 피보나치 수 2

코드1 코드2 실행 횟수를 1,000,000,007로 나눈 나머지를 한 줄에 출력한다.

www.acmicpc.net


문제 풀이

dp를 이용해서 피보나치 수를 구하면 된다. 너무 웰노운인 문제다.

 

코드

mod = int(1e9)+7
n = int(input())
ans = n-2
x, y = 1, 1
for _ in range(n-2):
y, x = (x+y)%mod, y
print(y, ans)
view raw 24417.py hosted with ❤ by GitHub

파이썬 코드

 

 

#include <iostream>
using namespace std;
static const int MOD = 1000000007;
int main()
{
int n, z;
int x = 1;
int y = 1;
cin >> n;
for (int i = 3; i <= n; i++){
z = y;
y = (x + y) % MOD;
x = z;
}
cout << y << ' ' << n-2;
return 0;
}
view raw 24417.cpp hosted with ❤ by GitHub

C++ 코드

728x90