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 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.

Author

[email protected]

Source

2010 ACM-ICPC Multi-University Training Contest(12)——Host by WHU

数位dp的比较好的题目把。。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<cmath>
#define ls (u<<1)
#define rs (u<<1|1)
#define maxn 30
#define ll long long
#define INF 1e18+7
using namespace std;
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
int digit[maxn];
ll dp[maxn][3];
ll dfs(int pos,int flag,int limit){
    if(pos == -1){
        return flag == 2;//flag等于2的时候代表这个数满足条件
    }
    if(!limit && dp[pos][flag]!=-1){//达到极限且此时dp有值
        return dp[pos][flag];
    }
    ll sum = 0;
    int e = limit?digit[pos]:9;
    for(int i=0;i<=e;i++){
        int have = flag;
        if(flag == 1 && i == 9){
            have = 2;
        }
        if(flag == 0 && i == 4){
            have = 1;//此时为将要完成的状态
        }
        if(flag == 1 && i!=4 && i!=9){
            have = 0;
        }
        sum += dfs(pos-1,have,limit&&i==e);
    }
    if(!limit){//达到极限的情况
        dp[pos][flag] = sum;
    }
    return sum;
}
ll solve(ll n){
    int pos = 0;
    while(n){
        digit[pos++] = n%10;
        n /= 10;
    }
    return dfs(pos-1,0,1);
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        ll n;
        scanf("%lld",&n);
        memset(dp,-1,sizeof(dp));
        printf("%lld\n",solve(n));
    }
    return 0;
}
时间: 2024-12-15 07:01:22

hud 3555 Bomb 数位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,貌似写法还是

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

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

hdu 3555(数位dp 入门)

题目链接:http://acm.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): 7716    Accepted Submission(s): 2702 Problem Description The counter-terrorists found a

hdu---(3555)Bomb(数位dp(入门))

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

hdu3555 Bomb(数位dp)

Bomb                                                                                     Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)                                                                             

HDU 3555 Bomb [数位]

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

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