题目链接
题解
我有了一种所有数位dp都能用记忆话搜索水的错觉
代码
#include<cstdio>
#include<algorithm>
inline int read() {
int x = 0,f = 1;
char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar();
return x * f ;
}
int a,b;
int f[100][100];
int num[100];
int tot;
int dfs(int n,int lim,int pre) {
if(n == 0) return 1;
if(!lim && f[n][pre]) return f[n][pre];
int sum = 0;
for(int i = 0;i <= (lim ? num[n] : 9);++ i) {
if(i == 0 && pre == -1) sum += dfs(n - 1,lim && i == num[n],-1);
else if(abs(pre - i) >= 2) sum += dfs(n - 1,lim && i == num[n],i);
}
if(!lim) f[n][pre] = sum ;
return sum;
}
int solve(int x) {
int tot = 0;
while(x) num[++ tot] = x % 10,x /= 10;
return dfs(tot,1,-1);
}
int main() {
a = read(),b = read();
printf("%d\n", solve(b) - solve(a - 1));
return 0;
}