传送门
题解:
数位dp,注意高位0不需要满足”相邻两位之差不小于2“的条件(最高位没有之前的相邻一位)
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
int dp[11][10],b[11];
int x,y;
int dfs(int pos,int now,bool t,bool lim) {
if (!pos) return 1;
if (t&&~dp[pos][now]) return dp[pos][now];
int end=lim?b[pos]:9,ans=0;
if (!t)
for (int i=0;i<=end;++i)
ans+=dfs(pos-1,i,i,lim&&(i==end));
else
for (int i=0;i<=end;++i)
if (abs(i-now)>1)
ans+=dfs(pos-1,i,true,lim&&(i=end));
if (t&&!lim) dp[now][pos]=ans;
return ans;
}
int work(int v) {
int len=0;
while (v) {
b[++len]=v%10;
v/=10;
}
return dfs(len,0,0,1);
}
int main() {
memset(dp,-1,sizeof(dp));
scanf("%d%d",&x,&y);
printf("%d\n",work(y)-work(x-1));
return 0;
}