[hdu 5898 odd-even number] 数位DP

[hdu 5898 odd-even number] 数位DP

题目链接[hdu 5898 odd-even number]
题意描述:求区间 [L,R] 中有多少个数字 x ,满足十进制展开形式的连续个奇数的长度为偶数,连续个偶数的长度为奇数。(1LR91018).
解题思路:裸的数位DP。 只要记录好当前位的奇偶性以及当前连续的长度,是否前导零。

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef unsigned __int64 ULL;

const int BITS = 25;
const int DIGS = 2;
const int STATUS = 2;

int T, cas;
LL L, R;

LL dp[BITS][DIGS][STATUS];
int dig[BITS];

LL dfs(int pos, bool pre, bool status, bool limit, bool zero) {
    if(pos < 1) return zero ? 0 : status;
    if(!zero && !limit && dp[pos][pre][status] != -1) return dp[pos][pre][status];
    int last = limit ? dig[pos] : 9;
    bool nstat, now;
    LL ret = 0;
    for(int i = 0; i <= last; i++) {
        now = (i & 1);
        if(zero) {
            nstat = !now;
        } else {
            if(pre ^ now) {
                if(status == false) continue;
                nstat = !now;
            } else {
                nstat = !status;
            }
        }
        ret += dfs(pos - 1, now, nstat, limit && (i == last), zero && (i == 0));
    }
    if(!zero && !limit) dp[pos][pre][status] = ret;
    return ret;
}

LL solve(LL x) {
    int len = 0;
    while(x) {
        dig[++ len] = x % 10;
        x /= 10;
    }
    return dfs(len, false, true, true, true);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif // ONLINE_JUDGE
    cas = 0;
    scanf("%d", &T);
    memset(dp, -1, sizeof(dp));
    while(T --) {
        scanf("%I64d%I64d", &L, &R);
        printf("Case #%d: %I64d\n", ++cas, solve(R) - solve(L - 1));
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值