0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

桁,bit

0
Last updated at Posted at 2020-02-13

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


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?