0%
数位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 30
| int a[20]; ll dp[20][state]; ll dfs(int pos, , bool lead, bool limit) { if (pos == -1) return 1; if (!limit && !lead && dp[pos][state] != -1) return dp[pos][state]; int up = limit ? a[pos] : 9; ll ans = 0; for (int i = 0; i <= up; i++) { if () else if () ans += dfs(pos - 1, , lead && i == 0, limit && i == a[pos]) } if (!limit && !lead) dp[pos][state] = ans; return ans; } ll solve(ll x) { int pos = 0; while (x) { a[pos++] = x % 10; x /= 10; } return dfs(pos - 1, , true, true); }
|