HDU 3555 Bomb(数位dp )

Problem Description

The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the time bomb. The number sequence of the time bomb counts from 1 to N. If the current number sequence includes the sub-sequence “49”, the power of the blast would add one point.
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?

Input
The first line of input consists of an integer T (1 <= T <= 10000), indicating the number of test cases. For each test case, there will be an integer N (1 <= N <= 2^63-1) as the description.

The input terminates by end of file marker.

Output
For each test case, output an integer indicating the final points of the power.

Sample Input

3
1
50
500

Sample Output

0
1
15
Hint
From 1 to 500, the numbers that include the sub-sequence “49” are “49”,“149”,“249”,“349”,“449”,“490”,“491”,“492”,“493”,“494”,“495”,“496”,“497”,“498”,“499”, so the answer is 15.

题目链接
参考题解

题目:求 0 到 n 中含有 49 的数字的个数。
对此我们可以定义dp[i][j]意义如下:

  • dp[i][j]:长度为i的数的第j种状态
  • dp[i][0]:长度为i但是不包含 49 的方案数
  • dp[i][1]:长度为i且不含 49 但是以 9 开头的数字的方案数
  • dp[i][2]:长度为i且包含 49 的方案数

我们这里采用初始化的方法,就是将dp数组都先记录好。这里面包括了三个状态:

  • 0:遍历到这一位的时候,前面没出现过 49
  • 1:遍历的这一位是 9,再遍历下一位如果是 4,组合成 49。
  • 2:以前遍历的已经出现过 49

那么状态转移就是这样了:

  • dp[i][0] = dp[i-1][0] * 10 - dp[i-1][1]; // 如果不含49且,在前面可以填上 0-9 但是要减去dp[i-1][1] 因为4会和9构成49
  • dp[i][1] = dp[i-1][0]; // 这个直接在不含49的数上填个9就行了
  • dp[i][2] = dp[i-1][2] * 10 + dp[i-1][1]; // 已经含有49的数可以填0-9,或者9开头的填4

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL;
LL dp[27][3];
int digit[27];
//dp[i][j]:长度为i的数的第j种状态
//dp[i][0]:长度为i但是不包含49的方案数
//dp[i][1]:长度为i且不含49但是以9开头的数字的方案数
//dp[i][2]:长度为i且包含49的方案数

void init() {
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    // 遍历数字的位数
    for(int digitNum = 1; digitNum <= 20; digitNum++) {
        dp[digitNum][0] = dp[digitNum - 1][0] * 10 - dp[digitNum - 1][1];    //如果不含49且,在前面可以填上0-9 但是要减去dp[i-1][1] 因为4会和9构成49
        dp[digitNum][1] = dp[digitNum - 1][0];    //这个直接在不含49的数上填个9就行了
        dp[digitNum][2] = dp[digitNum - 1][1] + dp[digitNum - 1][2] * 10;    //已经含有49的数可以填0-9,或者9开头的填4
    }
    return ;
}

int Cal(LL num) {
    int len = 0;
    memset(digit, 0, sizeof(digit));
    while(num) {
        digit[++len] = num % 10;
        num /= 10;
    }
    return len;
}

void Solve(int len, int n) {
    bool flag = false;  //标记是否出现过49
    LL ans = 0;
    for(int DigitNum = len; DigitNum >= 1; DigitNum--) {
        ans += digit[DigitNum] * dp[DigitNum - 1][2];
        if(flag)
            ans += digit[DigitNum] * dp[DigitNum - 1][0];
        else if(digit[DigitNum] > 4)
            ans += dp[DigitNum - 1][1];
        if(digit[DigitNum] == 9 && digit[DigitNum + 1] == 4)    flag = true;
    }
    cout << ans << endl;
}

int main() {
    int CaseNum;
    LL n;
    init();
    cin >> CaseNum;
    while(CaseNum--) {
        scanf("%lld", &n);
        int len = Cal(n + 1);
        Solve(len, n);
    }
    return 0;
}

此外,除了可以使用打表DP数组的方法,还可以使用递归的方法,但是这个时候dp数组的含义也发生了变化:

  • 其他状态没有变化,但是状态 1 的含义变为了在当前位之前是否出现过4。

这里需要注意的是,一定不要和上一个方法混淆。初始化打表的方法,是从低位往高位计算,即假设对 12345 进行计算,则打表会从最低位5计算,直到最高位1。而递归的方式将从最高位1往最低位5进行递归计算。

如果不能理解可以这样想:

  • 对打表的动态规划,我们只有知道了低位的状态,才能推出来高位的状态。
  • 递归的方式,我们可以将低位的状态计算交给递归,等递归算出来再推当前位的状态。其实本质也是需要现有低位再推高位,只是我们把低位的工作交给了递归。

代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

#define DEBUG

long long dp[27][3];
int digit[27];
int digitLen;
//dp[i][j]:长度为i的数的第j种状态
//dp[i][0]:长度为i但是不包含49的方案数
//dp[i][1]:长度为i且不含49但是前一位为4
//dp[i][2]:长度为i且包含49的方案数

void getDigit(int x) {
    digitLen = 0;
    while(x) {
        digit[++digitLen] = x % 10;
        x /= 10;
    }

    #ifdef DEBUG
    printf("=======================================\n");
    printf("Split digit number.\n");
    printf("=======================================\n");
    for (int i = digitLen; i; i--) {
        printf(i ? " %d": "%d", digit[i]);
    }
    printf("\n");
    #endif
}

long long countNumbersWith49(int len, int status, bool limit) {
    #ifdef DEBUG
    if (len == digitLen) {
        printf("=======================================\n");
        printf("Count numbers with 49.\n");
        printf("=======================================\n");
    }
    #endif
    if (!len) return status == 2;
    if (!limit && dp[len][status] != -1) return dp[len][status];
    long long ret = 0;
    int _end = limit ? digit[len] : 9;
    for (int i = 0; i <= _end; i++) {
        // try every digit number
        if (status == 2 || (status == 1 && i == 9)) {
            ret += countNumbersWith49(len - 1, 2, limit && (i == _end));
        } else if (i == 4) {
            ret += countNumbersWith49(len - 1, 1, limit && (i == _end));
        } else {
            ret += countNumbersWith49(len - 1, 0, limit && (i == _end));
        }
    }
    if (!limit) dp[len][status] = ret;
    #ifdef DEBUG
    printf("Len: %d, Status: %d, Limit: %s, Numbers: %lld\n", len, status, limit ? "true" : "false", ret);
    #endif
    return ret;
}

int main() {
    int n;
    long long range;

    memset(dp, -1, sizeof(dp));
    scanf("%d", &n);
    while (n--) {
        scanf("%lld", &range);
        getDigit(range);
        printf("%lld\n", countNumbersWith49(digitLen, 0, true));
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值