uestc 1307 windy数 数位DP

主要注意下0的情况就可以!!!

链接:http://acm.uestc.edu.cn/problem.php?pid=1307

代码如下:

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define ll __int64
 5 using namespace std;
 6 int bit[10],dp[20][20];
 7 int dfs(int pos,int pre,bool h,bool f)
 8 {
 9     if(pos==-1) return h==0;
10     if(!f&&dp[pos][pre]!=-1&&!h) return dp[pos][pre];
11     int ans=0;
12     int e=f?bit[pos]:9;
13     for(int i=0;i<=e;i++){
14         if(h) ans+=dfs(pos-1,i,h&&i==0,f&&(i==bit[pos]));
15         else if(abs(pre-i)>=2)ans+=dfs(pos-1,i,h,f&&(i==bit[pos]));
16     }
17     if(!f&&!h) dp[pos][pre]=ans;
18     return ans;
19 }
20 int cal(int n)
21 {
22     int m=0;
23     while(n){
24         bit[m++]=n%10;
25         n/=10;
26     }
27     return dfs(m-1,0,1,1);
28 }
29 int main()
30 {
31     int n,m;
32     memset(dp,-1,sizeof(dp));
33     while(scanf("%d%d",&n,&m)!=EOF){
34         printf("%d\n",cal(m)-cal(n-1));
35     }
36 }
View Code

 

 

posted @ 2013-09-05 21:30  _随心所欲_  阅读(229)  评论(0编辑  收藏  举报