码迷,mamicode.com
首页 > Windows程序 > 详细

P2657 [SCOI2009]windy数

时间:2018-10-10 22:04:17      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:bool   题目   size   ring   space   include   printf   cpp   lag   

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;
    }

P2657 [SCOI2009]windy数

标签:bool   题目   size   ring   space   include   printf   cpp   lag   

原文地址:https://www.cnblogs.com/Tony-Double-Sky/p/9768641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!