Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- XR Interaction Toolkit
- 정렬
- 문자열
- 누적 합
- 다익스트라
- VR
- DFS
- 브루트포스
- 구현
- 백트래킹
- 그리디 알고리즘
- 투 포인터
- 유니티
- 우선순위 큐
- 수학
- 자료구조
- 알고리즘
- BFS
- 그래프
- 다이나믹 프로그래밍
- 시뮬레이션
- 유니온 파인드
- 백준
- 재귀
- 스택
- 트리
- Unreal Engine 5
- ue5
- c++
- Team Fortress 2
Archives
- Today
- Total
1일1알
백준 15990번 1, 2, 3 더하기 5 C++ 본문
https://www.acmicpc.net/problem/15990
15990번: 1, 2, 3 더하기 5
각 테스트 케이스마다, n을 1, 2, 3의 합으로 나타내는 방법의 수를 1,000,000,009로 나눈 나머지를 출력한다.
www.acmicpc.net
다이나믹 프로그래밍
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
const int64 mod = 1000000009;
vector<vector<int64>> cache;
int64 dp(int n, int lastN) {
if (n <= 0) return 0;
int64& val = cache[n][lastN];
if (val != -1) return val;
val = 0;
for (int i = 1; i <= 3; i++) {
if (lastN == i) continue;
val += dp(n - lastN, i);
}
val %= mod;
return val;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
cache = vector<vector<int64>>(100001, vector<int64>(4, -1));
cache[1][1] = 1;
cache[2][2] = 1;
cache[3][1] = 1;
cache[3][2] = 1;
cache[3][3] = 1;
while (t--) {
int n;
cin >> n;
int64 ans = dp(n, 1) + dp(n, 2) + dp(n, 3);
ans %= mod;
cout << ans << "\n";
}
}
'알고리즘' 카테고리의 다른 글
백준 14395번 4연산 C++ (0) | 2022.11.21 |
---|---|
백준 9011번 순서 C++ (0) | 2022.11.20 |
백준 25516번 거리가 k이하인 트리 노드에서 사과 수확하기 C++ (0) | 2022.11.18 |
백준 16719번 ZOAC C++ (0) | 2022.11.17 |
백준 8911번 거북이 C++ (0) | 2022.11.16 |