Logo냥냠감자기술 블로그
Skip to Content
AlgorithmDP (31)1, 2, 3 더하기 5
작성일: 2024-01-17

문제

백준 15990 - 1, 2, 3 더하기 5 

  • 난이도: 실버2
  • 알고리즘: dp

코드

#include <iostream> #include <vector> using namespace std; vector<vector<int> > cache(100001, vector<int>(4, -1)); int getCase(int n, int bef) { if(n<0) return 0; if(n==0) return 1; if(cache[n][bef]!=-1) return cache[n][bef]; int ret=0; for(int i=1; i<=3; i++) { if(bef==i) continue; ret+=getCase(n-i, i); ret%=1000000009; } return cache[n][bef]=ret; } int main() { int testCase; cin>>testCase; for(testCase; testCase>0; testCase--) { int n; cin>>n; cout<<getCase(n, 0)<<"\n"; } }

회고

수업 시간에 시연한 문제이다. 전날 푼 문제랑 비슷해서 쉽게 풀 수 있었다.
1, 2, 3 더하기 문제 중 dp문제를 하나만 풀면 나머지는 비슷하게 다 풀리는 것 같다.