UESTC-1307-windy数

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

Input

包含两个整数,A B。
满足 1 <= A <= B <= 2000000000 。

Output

包含一个整数:闭区间[A,B]上windy数的个数。

Sample Input

1 10

Sample Output

9


状态分析比较简单,需要加入前导0的状态 如01 02 03



#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;

typedef long long ll;
ll dp[20][10];
vector<int> digit;

int a,b;
ll dfs(int pos,int statu,int done,int first){
    if(pos==-1) return 1;
    if(!done && ~dp[pos][statu]&&!first) return dp[pos][statu];
    ll res = 0;
    int end = done? digit[pos]:9;
    if(first){
        for(int i = 0; i <= end; i++)
            res += dfs(pos-1,i,done&&i==end,i==0);
    }
    else{
        for(int i = 0; i <= end; i++){
            if(abs(i-statu)>=2)
                res += dfs(pos-1,i,done&&i==end,0);
        }
    }
    if(!done&&!first)   dp[pos][statu] = res;
    return res;
}

ll solve(int num){
    memset(dp,-1,sizeof dp);
    digit.clear();
    while(num){
        digit.push_back(num%10);
        num /= 10;
    }
    return dfs(digit.size()-1,0,1,1);
}

int main(){
    while(cin >> a >> b){
        cout<<solve(b)-solve(a-1)<<endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值