digital dp
topic | knowledge points | difficulty |
---|---|---|
2376. Count Special Integers | digital dp template | difficulty |
600. Non-negative integers without consecutive 1s | digital dp | difficulty |
233. Number of 1s | Digital dp template questions | difficulty |
902. Number combinations up to N | Digital dp template topic | difficulty |
Number of 1's | Mask records the number of numbers x | |
The number of acwing 1081 degrees | Use base thinking + each bit is 1 or 0. The number of mask records 1 | |
acwing 1082、1083 ,1085 | What is the number of the first digit of the Mask record | |
acwing 1084 | The mask records the sum of the previous numbers | |
Ideas for doing digital dp questions
First change the [a, b] interval to a single calculation from [0, a], [0, b]. then make a difference
Second, use the standard template code
Standard template code, according to the conditions defined in the topic, just modify the pre state variable.
Common state variables are
- For the previous one, there is a divisive number between adjacent digits in this topic. Such as acwing's 1082, 1083, 1085
- The sum of digits, refer to the case of acwing 1084
- mask, numbers from 1-9 cannot be repeated. The mask record selects that number
- c n t x cnt_x cntx, the number of occurrences of x is used to count the number of occurrences. Number of reference numerals 1
private static int dfs(int cur, int pre, boolean isNum, boolean isLimit) {
if (cur == s.length) {
return isNum ? 1 : 0;
}
// 记忆化
int res = dp[cur][pre];
if (isNum && !isLimit && res >= 0)
return res;
res = 0;
// 不是数字,可以跳过
if (!isNum) res += dfs(cur + 1, pre, false, false);
// 枚举是数字的情况
int up = isLimit ? s[cur] - '0' : 9;
for (int i = isNum ? 0 : 1; i <= up; i++) {
if (i == 4 || (pre == 6 && i == 2)) continue;
res += dfs(cur + 1, i, true, isLimit && i == up);
}
// 记录
if (!isLimit && isNum) dp[cur][pre] = res;
return res;
}