# BZOJ1026: [SCOI2009]windy数 ( 数位dp )

 1 #include<cmath>
2 #include<cstdio>
3 #include<cstring>
4 #include<iostream>
5 #include<algorithm>
6 #include<queue>
7 using namespace std;
8 long long a,b;
9 int shu[20]={};
10 int f[20][20]={};
11 int abv(int x){
12     if(x>0){
13         return x;
14     }
15     return -x;
16 }
17 int dfs(int k,int num,bool shang){
18     if(k<=0){
19         return 1;
20     }
21     if((!shang)&&num>=0&&f[k][num]!=-1){
22         return f[k][num];
23     }
24     int ans=0,p,maxn=shang?shu[k]:9;
25     for(int i=0;i<=maxn;i++){
26         if(abv(i-num)<2){
27             continue;
28         }
29         p=i;
30         if(i==0&&num==-5){
31             p=num;
32         }
33         ans+=dfs(k-1,p,shang&&i==maxn);
34     }
35     if(!shang){
36         f[k][num]=ans;
37     }
38     return ans;
39 }
40 int solve(long long x){
41     memset(shu,0,sizeof(shu));
42     int k=0;
43     while(x){
44         shu[++k]=x%10;
45         x/=10;
46     }
47     return dfs(k,-5,1);
48 }
49 int main(){
50     scanf("%lld%lld",&a,&b);
51     memset(f,-1,sizeof(f));
52     printf("%d\n",solve(b)-solve(a-1));
53     return 0;
54 }
View Code

posted @ 2017-11-04 17:08  鲸头鹳  阅读(90)  评论(0编辑  收藏  举报