Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

桁,bit

More than 1 year has passed since last update.

abc154 e
制約がクソデカくてN以下の~の条件を満たす数の個数を求めよ的な問題は桁dp

int main() {
    string N; cin >> N;
    int n = N.size();
    int K; cin >> K;
    ll dp[110][2][4];
    REP(i, 110) {
        REP(j, 2) {
            REP(k, 4) {
                dp[i][j][k] = 0;
            }
        }
    }
    dp[0][0][0] = 1;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < 2; ++j) {
            for (int k = 0; k <= K; ++k) {
                int x;
                x = j ? 9 : N[i] - '0';
                for (int d = 0; d <= x; ++d) {
                    if (d != 0) {
                        if (k < K) {
                            dp[i + 1][j || (d < x)][k + 1] += dp[i][j][k];
                        }
                    }
                    else {
                        dp[i + 1][j||(d<x)][k] += dp[i][j][k];
                    }
                }
            }
        }
    }
    cout << dp[n][0][K] + dp[n][1][K] << endl;
    return 0;
}

例題 typicaldp 数
code festival 2014予選a 壊れた電卓

逆にnがめっちゃ小さいやつはbit dpで解ける
abc 142 e

tell0120xxx
個人的日記
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away