728x90
문제 링크: https://www.acmicpc.net/problem/24417
24417번: 알고리즘 수업 - 피보나치 수 2
코드1 코드2 실행 횟수를 1,000,000,007로 나눈 나머지를 한 줄에 출력한다.
www.acmicpc.net
문제 풀이
dp를 이용해서 피보나치 수를 구하면 된다. 너무 웰노운인 문제다.
코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
파이썬 코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
C++ 코드
728x90
'BOJ' 카테고리의 다른 글
[BOJ][Python] 백준 25083번 - 새싹 (0) | 2022.05.01 |
---|---|
[BOJ][Python] 백준 9501번 - 꿍의 우주여행 (0) | 2022.04.24 |
[BOJ][Python] 백준 1312번 - 소수 (0) | 2022.04.05 |
[BOJ][Python][C++] 백준 16208번 - 귀찮음 (0) | 2022.04.05 |
[BOJ][Text] 백준 24905번 - 24905번 문제 (0) | 2022.04.03 |