Forget limit can't remember wa ...
Typical digital DP, which becomes work (R)-work (L-1), then Dfs records w current position, la last number selected, LM current bit is upper limit, OK current bit can not consider the case of difference greater than or equal to 2 (front all is 0)
Then, for LM and OK are 0 of the case of memory can be
Ah Bzoj don't know why give Cmath under the ABS award CE ... It's okay to switch to algorithm.
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;const int N=15;long long l,r,f[N][N],a[N],tot;long long dfs(int w,int la,int lm,int ok){ if(!lm&&!ok&&f[w][la]) return f[w][la]; if(!w) return 1; long long r=0; if(lm) { for(int i=0;i<=a[w];i++) if(abs(la-i)>1||ok) r+=dfs(w-1,i,i==a[w],ok&&i==0); } else { for(int i=0;i<=9;i++) if(abs(la-i)>1||ok) r+=dfs(w-1,i,0,ok&&i==0); } if(!lm&&!ok) return f[w][la]=r; else return r;}long long wk(long long x){ tot=0; while(x) a[++tot]=x%10,x/=10; return dfs(tot,13,1,1);}int main(){ scanf("%lld%lld",&l,&r); printf("%lld\n",wk(r)-wk(l-1)); return 0;}
Bzoj 1026: [Scoi2009]windy number "digital DP"