๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][Baekjoon][DP] 1309๋ฒˆ ๋™๋ฌผ์›

ibelieveinme 2021. 8. 22. 16:51
728x90

๋ฌธ์ œ

https://www.acmicpc.net/problem/1309

 

1309๋ฒˆ: ๋™๋ฌผ์›

์ฒซ์งธ ์ค„์— ์šฐ๋ฆฌ์˜ ํฌ๊ธฐ N(1≤N≤100,000)์ด ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

๋ฐฑ์ค€์— ์Šคํ‹ฐ์ปค๋ž‘ 2*n ํƒ€์ผ๋ง ๋ฌธ์ œ์™€ ๋งค์šฐ ๋น„์Šท...

 

์ฐธ๊ณ ์‚ฌ์ง„

 

์ฝ”๋“œ

#include <iostream>
using namespace std;

void DP_function(int);

const int MAX_SIZE = 100000;
const int MOD = 9901;
int dp[MAX_SIZE][3];

int main() {
    int column = 0;
    cin >> column;
    DP_function(column);
}

void DP_function(int column) {
    dp[1][0] = 1;
    dp[1][1] = 1;
    dp[1][2] = 1;

    for (int i = 2; i <= column; i++) {
        //i-1๋ฒˆ์งธ ์ค„์— ์‚ฌ์ž๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ
        dp[i][0] = (dp[i - 1][0] + dp[i - 1][1] + dp[i-1][2]) % MOD;
        //i-1๋ฒˆ์งธ ์ค„ ์™ผ์ชฝ์— ์‚ฌ์ž๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ
        dp[i][1] = (dp[i - 1][0] + dp[i - 1][2]) % MOD;
        //i-1๋ฒˆ์งธ ์ค„ ์˜ค๋ฅธ์ชฝ์— ์‚ฌ์ž๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ
        dp[i][2] = (dp[i - 1][0] + dp[i - 1][1]) % MOD;
    }
    cout << (dp[column][0] + dp[column][1] + dp[column][2]) % MOD;
}
728x90