uestc 1307 统计数位之间相差不小于2的数的个数

windy定义了一种windy数。
不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。
windy想知道,在A和B之间,包括A和B,总共有多少个windy数?

解题思路:

简单枚举+记忆化搜索

View Code
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cmath>
 6 using std::cout;
 7 using std::endl;
 8 typedef long long LL;
 9 int const N = 20;
10 LL A,B;
11 int bit[N],len;
12 LL dp[N][11];
13 LL getsum1(int t,int limit,int last,int limit2)
14 {
15      if(t==0)
16      {
17         if(!limit2)return (last==0?0:1);
18         return 1;
19      }
20      if(!limit&&limit2&&dp[t][last]!=-1)return dp[t][last];
21      int up=(limit?bit[t]:9);
22      LL ans=0LL;
23      for(int i=0;i<=up;i++)
24      {
25          if(abs(i-last)>=2||!limit2)
26          {
27               ans+=getsum1(t-1,limit&&(i==up),i,limit2||i);
28          }
29      }
30      if(!limit&&limit2&&dp[t][last]==-1)dp[t][last]=ans;
31      return ans;
32 }
33 LL getsum2(LL n)
34 {
35    if(n<10)return n;
36    for(len=0;n;n/=10)bit[++len]=n%10;
37    return getsum1(len,1,-2,0);
38 }
39 int main()
40 {
41     memset(dp,-1,sizeof(dp));
42     while(~scanf("%I64d %I64d",&A,&B))
43     {
44          cout<<getsum2(B)-getsum2(A-1)<<endl;
45     }
46     return 0;
47 }

 

posted @ 2013-05-07 13:55  诺小J  阅读(141)  评论(0编辑  收藏  举报