hdoj 3555 Bomb(DFA+dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555

思路分析:该问题要求求解1—N中的数中含有49的数的个数,可以使用DFA来递推dp公式;详细解释点击链接查看;

代码如下:

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

const int MAX_N = 20 + 10;
long long dp[MAX_N][3];
int digit[MAX_N];

int NextState(int cur_state, int next_char)
{
    if (cur_state == 0)
    {
        if (next_char == 4)
            cur_state++;
    }
    else if (cur_state == 1)
    {
        if (next_char == 9)
            ++cur_state;
        else if (next_char != 4)
            --cur_state;
    }
    return cur_state;
}

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

    scanf("%d", &case_times);
    while (case_times--)
    {
        int cur_state = 0, count = 0;
        int len = 0;
        long long ans = 0, temp_value = 0;

        scanf("%I64d", &n);
        temp_value = ++n;
        while (temp_value)
        {
            digit[++len] = temp_value % 10;
            temp_value /= 10;
        }

        for (int i = len; i >= 1; -- i)
        {
            ++count;
            for (int j = 0; j < digit[i]; ++ j)
            {
                memset(dp, 0, sizeof(dp));
                dp[count][NextState(cur_state, j)] = 1;
                for (int k = count + 1; k <= len; ++ k)
                {
                    dp[k][0] = 9 * dp[k-1][0] + 8 * dp[k-1][1];
                    dp[k][1] = dp[k-1][0] + dp[k-1][1];
                    dp[k][2] = dp[k-1][1] + 10 * dp[k-1][2];
                }
                ans += dp[len][2];
            }
            cur_state = NextState(cur_state, digit[i]);
        }
        printf("%I64d\n", ans);
    }
    return 0;
}
时间: 2024-08-27 05:55:50

hdoj 3555 Bomb(DFA+dp)的相关文章

[ACM] hdu 3555 Bomb (数位DP,统计1-N中含有“49”的总数)

Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 7187 Accepted Submission(s): 2512 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorists impro

HDU 3555 Bomb (数位DP)

数位dp,主要用来解决统计满足某类特殊关系或有某些特点的区间内的数的个数,它是按位来进行计数统计的,可以保存子状态,速度较快.数位dp做多了后,套路基本上都差不多,关键把要保存的状态给抽象出来,保存下来. 简介: 顾名思义,所谓的数位DP就是按照数字的个,十,百,千--位数进行的DP.数位DP的题目有着非常明显的性质: 询问[l,r]的区间内,有多少的数字满足某个性质 做法根据前缀和的思想,求出[0,l-1]和[0,r]中满足性质的数的个数,然后相减即可. 算法核心: 关于数位DP,貌似写法还是

hud 3555 Bomb 数位dp

Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 19122    Accepted Submission(s): 7068 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorist

HDU 3555 Bomb(数位DP)

Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo', and a host of other names) in order to make arbitrary decisions such as who gets

【HDOJ 3555】Bomb

[HDOJ 3555]Bomb 基础数位dp 跟那个4 62差不多 这个能更简单点 慢慢来吧= .= 预处理好49连续的情况即可 代码如下: #include <iostream> #include <cstdio> #include <cstring> #define ll long long #define sc "%I64d" using namespace std; ll dp[20][3]; /* dp[0] 无连续49 dp[1] 无连续

hdu 3555 Bomb(数位dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题目大意:就是给你一个数n,判断从0到n有多少个数含有数字49...... 是不是觉得跟hdu2089很相似呀... 思路:跟hdu2089一样的,注意给出的数比较大,所以这儿用__int64  .... code: #include<cstdio> #include<iostream> #include<cstring> #include<algorithm&

HDU - 3555 Bomb (数位DP)

题意:求1-n里有多少人包含"49"的数字 思路:数位DP,分三种情况:到第i位没有49的情况,到第i位没有49且最高位是9的情况,到第i位有49的情况,将三种情况都考虑进去就是了 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; long long dp[30][3], n; int arr

杭电 3555 Bomb

Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 6609    Accepted Submission(s): 2303 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorists

HDU(3555),数位DP

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 15372    Accepted Submission(s): 5563 Problem Description The counter-terrorists f