Time Limits: 1000 ms Memory Limits: 65536 KB Detailed Limits
1 #include<bits/stdc++.h> 2 using namespace std; 3 int num[11],dp[11][11]; 4 inline int read() 5 { 6 char ch=getchar();int num=0; 7 while(ch<'0'||ch>'9')ch=getchar(); 8 while(ch>='0'&&ch<='9'){ 9 num=num*10+ch-'0';ch=getchar();} 10 return num; 11 } 12 inline int Abs(int x) 13 { 14 return x>0?x:-x; 15 } 16 inline int dfs(int len,int las,bool top,bool zero) 17 { 18 if(len==0)return 1; 19 if(!top&&!zero&&dp[len][las]!=-1) 20 return dp[len][las]; 21 int cnt=0,maxx=(top?num[len]:9); 22 for(int i=0;i<=maxx;i++){ 23 if(Abs(i-las)<2)continue; 24 int p=i;if(zero&&i==0)p=-555; 25 cnt+=dfs(len-1,p,top&&(i==maxx),(p==-555)); 26 } 27 if(!zero&&!top) 28 dp[len][las]=cnt; 29 return cnt; 30 } 31 inline int work(int x) 32 { 33 int tot=0; 34 while(x){ 35 num[++tot]=x%10; 36 x/=10; 37 } 38 memset(dp,-1,sizeof(dp)); 39 return dfs(tot,-555,true,true); 40 } 41 int main() 42 { 43 int x=read(); 44 int y=read(); 45 printf("%d",work(y)-work(x-1)); 46 return 0; 47 }