P2657 [SCOI2009]windy数

P2657 [SCOI2009]windy数

题目描述
windy定义了一种windy数。不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。 windy想知道,

在A和B之间,包括A和B,总共有多少个windy数?

Solution

有先导 \(0\) 的数位\(dp\)
把此位前有无前导 \(0\) 作为搜索的一个状态即可
注意有前导 \(0\) 时不能直接返回, 因为有前导 \(0\) 就代表着无法到达 \(10^{len} - 1\)

Code

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<climits>
#define LL long long
#define REP(i, x, y) for(LL i = (x);i <= (y);i++)
using namespace std;
LL RD(){
    LL out = 0,flag = 1;char c = getchar();
    while(c < '0' || c >'9'){if(c == '-')flag = -1;c = getchar();}
    while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();}
    return flag * out;
    }
const LL maxn = 19;
LL num[maxn];
LL dp[maxn][maxn];
LL DP(LL Index, LL state, LL zero, bool limit){
    if(Index == 0)return 1;
    if(!zero && !limit && dp[Index][state] != -1)return dp[Index][state];
    LL ans = 0, up = limit ? num[Index] : 9;
    REP(i, 0, up){
        if(zero)ans += DP(Index - 1, i, i == 0, limit && (i == num[Index]));
        else{
            if(abs(i - state) < 2)continue;
            ans += DP(Index - 1, i, 0, limit && (i == num[Index]));
            }
        }
    if(!zero && !limit)dp[Index][state] = ans;
    return ans;
    }
LL solve(LL x){
    LL len = 0;
    while(x){
        num[++len] = x % 10;
        x /= 10;
        }
    return DP(len, 0, 1, 1);
    }
int main(){
    memset(dp, -1, sizeof(dp));
    LL l = RD(), r = RD();
    printf("%lld\n", solve(r) - solve(l - 1));
    return 0;
    }

转载于:https://www.cnblogs.com/Tony-Double-Sky/p/9768641.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值