思路:
直接数位DP
c o d e code code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int l, r;
int lim[20], tot;
int f[100][20];
int dfs(int x, int flag, int limit, int last) {
if(x == 0) return flag == 0;
if(!limit && !flag && f[x][last] != -1) return f[x][last];
int maxx = limit ? lim[x] : 9;
int ans = 0;
for(int i = 0; i <= maxx; i ++)
if(abs(i - last) >= 2) ans += dfs(x - 1, flag && (i == 0), limit && (i == maxx), (flag && i == 0) ? -2 : i);
if(!limit && !flag) f[x][last] = ans;
return ans;
}
int solve(int x) {
tot = 0;
while(x) {
lim[++ tot] = x % 10;
x /= 10;
}
return dfs(tot, 1, 1, -2);
}
int main() {
memset(f, -1, sizeof(f));
scanf("%d%d", &l, &r);
printf("%d", solve(r) - solve(l - 1));
return 0;
}