ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [1562] 계단 수
    BOJ 2021. 10. 27. 20:24

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

     

    1562번: 계단 수

    첫째 줄에 정답을 1,000,000,000으로 나눈 나머지를 출력한다.

    www.acmicpc.net

    <문제>

    dp[N][마지막 숫자][1<<10]으로 dp를 구성해주었다.

    모든 숫자가 사용되었는지는 비트가 (1<<10)-1과 동일한지를 확인하면 된다.

     

    <소스코드>

    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
    30
    31
    32
    33
    34
    35
    36
    37
    #include <bits/stdc++.h>
    using namespace std;
    const int MOD = 1e9;
    const int BIT = (1 << 10- 1;
    int n, dp[101][10][1 << 10];
    int f(int depth, int endint bt) {
        if (depth == n) {
            if (bt == BIT)
                return 1;
            else
                return 0;
        }
        if (dp[depth][end][bt] != -1return dp[depth][end][bt];
        dp[depth][end][bt] = 0;
        if (end + 1 <= 9) {
            int nbt = bt | (1 << (end + 1));
            dp[depth][end][bt] += f(depth + 1end + 1, nbt);
            dp[depth][end][bt] %= MOD;
        }
        if (end - 1 >= 0) {
            int nbt = bt | (1 << (end - 1));
            dp[depth][end][bt] += f(depth + 1end - 1, nbt);
            dp[depth][end][bt] %= MOD;
        }
        return dp[depth][end][bt] % MOD;
    }
    int main(void) {
        cin >> n;
        memset(dp, -1sizeof(dp));
        int ans = 0;
        for (int i = 1; i <= 9; i++) {
            ans += f(1, i, (1 << i));
            ans %= MOD;
        }
        cout << ans % MOD;
        return 0;
    }
    cs

     

    'BOJ' 카테고리의 다른 글

    [23058] 뒤집기 게임  (0) 2021.10.31
    [1285] 동전 뒤집기  (0) 2021.10.28
    [1102] 발전소  (0) 2021.10.27
    [3056] 007  (0) 2021.10.27
    [1480] 보석 모으기  (0) 2021.10.25

    댓글