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 }

 

转载于:https://www.cnblogs.com/nuoyan2010/archive/2013/05/07/3064626.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值