다이나믹 프로그래밍 문제입니다.
피보나치 수열의 규칙을 하고 있어서
DP[i] = DP[i - 1] + DP[i - 2];
로 점화식을 세워서 문제를 풀었습니다.
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
int DP[1000010];
int main(int argc, char *argv[])
{
int n;
memset(DP, 0, sizeof(DP));
DP[1] = 1;
DP[2] = 2;
cin >> n;
for (int i = 3; i <= n; ++i) {
DP[i] = (DP[i - 1] + DP[i - 2]) % 15746;
}
cout << DP[n] << endl;
return 0;
}
'알고리즘' 카테고리의 다른 글
[C++] 백준 10942번 팰린드롬? (0) | 2019.07.10 |
---|---|
[C++] 백준 2011번 암호코드 (0) | 2019.07.10 |
[C++] 백준 9252번 LCS 2 (0) | 2019.07.10 |
[C++] 백준 11066번 파일 합치기 (0) | 2019.07.09 |
[C++] 백준 2096번 내려가기 (0) | 2019.07.04 |