algorithm/BOJ

BOJ 15988번 1, 2, 3 더하기 3

_JunHo 2020. 2. 18. 00:14

BOJ : https://www.acmicpc.net/problem/15988

github : https://github.com/junho0956/Algorithm/blob/master/15988/15988/%EC%86%8C%EC%8A%A4.cpp

 

9095번 문제에서 dp의 범위를 늘려준 문제입니다.

이외는 동일합니다.

 

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
29
#include <iostream>
using namespace std;
#define MOD 1000000009
typedef long long ll;
 
ll dp[1000002];
 
ll dfs(int num) {
    if (num < 0return 0;
    if (num == 0return 1;
 
    ll& res = dp[num];
    if (res) return res;
 
    for (int i = 1; i <= 3; i++)
        res += dfs(num - i) % MOD;
 
    return res;
}
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int T; cin >> T;
    while (T--) {
        int n; cin >> n;
        cout << dfs(n) % MOD << "\n";
    }
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter