思路:
数位dp
代码:
#includeusing namespace std;#define LL long long #define pb push_back#define mem(a, b) memset(a, b, sizeof(a))int a[15];int dp[15][15];int dfs(int pos, int pre, bool limit, bool zero) { if (pos == -1) return 1; if(!limit && !zero && ~dp[pos][pre]) return dp[pos][pre]; int top = limit ? a[pos] : 9; int ans = 0; for (int i = 0; i <= top; i++) { if(zero){ ans += dfs(pos - 1, i, limit&&i==a[pos], zero&&i==0); } else { if(abs(pre - i) <= 1) continue; ans += dfs(pos - 1, i, limit&&i==a[pos], zero&&i==0); } } if(!limit && !zero) dp[pos][pre] = ans; return ans;}int solve(int n) { int cnt = 0; mem(a, 0); while (n) { a[cnt++] = n % 10; n /= 10; } return dfs(9, 0, 1, 1);} int main() { int a, b; scanf("%d%d", &a, &b); mem(dp, -1); printf("%d\n",solve(b) - solve(a - 1)); return 0;}