//需要pre参数来记录前一个数来判断两数之差是否小于等于2
//需要lead参数判断前导零
#include <bits/stdc++.h>
#define endl '\n'
#define INF 0x3f3f3f3f
#define ll long long
#define Mirai ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define COUT cout << "###" << endl;
#define pb push_back
using namespace std;
typedef pair<int, int> PII;
const int N=25;
int l,r,len,a[N],dp[N][N];
int dfs(int pos,int pre,int lead,int limit)
{
if(!pos)return 1;
if(!limit&&!lead&&dp[pos][pre]!=-1)return dp[pos][pre];
int res=0,up=limit?a[pos]:9;
for(int i=0;i<=up;i++)
{
if(abs(pre-i)<2)continue;
if(lead&&!i)res+=dfs(pos-1,-2,1,limit&&i==up);
else res+=dfs(pos-1,i,0,limit&&i==up);
}
return limit?res:dp[pos][pre]=res;
}
int cal(int x)
{
memset(dp,-1,sizeof dp);
len=0;
while(x)a[++len]=x%10,x/=10;
return dfs(len,-2,1,1);/初始化为-2防止首位大于2而导致无法继续搜索
}
int main()
{
cin>>l>>r;
cout<<cal(r)-cal(l-1);
return 0;
}
windy数
最新推荐文章于 2024-08-16 22:18:58 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)