[SCOI 2009]windy数
#include<bits/stdc++.h> #define re return #define reg register #define ll long long #define inc(i,l,r) for(int i=l;i<=r;++i) #define dec(i,l,r) for(int i=l;i>=r;--i) using namespace std; ll l,r,f[40][15],num[20]; ll dfs(int pos,int limit,int lead,int last) { if(!lead)last=11; if(!pos)re 1; if(!limit&&(~f[pos][last])) re f[pos][last]; ll up=limit?num[pos]:9,res=0; inc(i,0,up) if(abs(last-i)>1)res+=dfs(pos-1,limit&&(i==up),lead||i,i); if(!limit)f[pos][last]=res; re res; } ll cala(int x) { int len=0; while(x){ num[++len]=x%10; x/=10; } re dfs(len,1,0,0); } int main() { memset(f,-1,sizeof f); scanf("%lld%lld",&l,&r); printf("%lld\n",cala(r)-cala(l-1)); re 0; }
一堆dfs乱搞+记忆化