定义了不含前导
0
0
0但是任意相邻两数之差至少的
2
2
2的数为windy数,求问
A
A
A,
B
B
B之前有多少这样的数。考虑
d
p
[
p
o
s
]
[
p
r
e
]
[
0
∣
1
]
[
0
∣
1
]
dp[pos][pre][0|1][0|1]
dp[pos][pre][0∣1][0∣1]是
p
o
s
pos
pos位向后,前驱为
p
r
e
pre
pre,是否被高位限制,是否有前导
0
0
0的方案数。
当搜到某一位有前导
0
0
0限制的时候,可以把
−
2
-2
−2作为前驱。这样枚举下一位时候
0
−
9
0-9
0−9恰好是合法的。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const ll INF=LONG_LONG_MAX;
ll dp[11][10][2];
int a[11];
ll dfs(int pos,int pre,bool lim,bool zero) {
if(pos==0) return 1;
if(dp[pos][pre][lim]!=-1)
return dp[pos][pre][lim];
ll ans=0;
int up=lim?a[pos]:9;
for(int i=0;i<=up;i++) {
if(abs(i-pre)<2) continue;
ans+=dfs(pos-1,(zero&&i==0)?-2:i,lim&&i==a[pos],zero&&i==0);
}
return dp[pos][pre][lim]=ans;
}
ll solve(ll x) {
memset(dp,-1,sizeof(dp));
int len=0;
while(x) {
a[++len]=x%10;
x/=10;
}
return dfs(len,-2,1,1);
}
int main() {
ll l,r;
scanf("%lld%lld",&l,&r);
printf("%lld\n",solve(r)-solve(l-1));
return 0;
}