HDU 3555 数位DP

Posted 晓风微微

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 3555 数位DP相关的知识,希望对你有一定的参考价值。

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?

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

题意:
问小于等于n的数字中包含着49的数字有多少个。
做法:
数位dp板子题目,统计有多少个不包含49的,减去就好
代码:
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#include<cstdlib>
typedef long long ll;
const int state = 2;
int a[25];
ll dp[25][state];
ll dfs(int pos,int pre,bool limit){
    if(pos == -1)
        return 1;//深度
    int up;//枚举上界
    if(!limit&&dp[pos][pre]!=-1)//记忆化
        return dp[pos][pre];
    up = limit?a[pos]:9;
    ll ans = 0;
    for(int i=0;i<=up;i++){
        if(pre==1&&i==9)
            continue;
        ans+=dfs(pos-1,i==4,limit&&i==a[pos]);
    }
    if(!limit)//有选择地记忆化
    return dp[pos][pre]=ans;
    else
    return ans;
}
ll solve(ll x){
    int pos = 0;
    memset(a,0,sizeof(a));
    while(x){
        a[pos++]=x%10;
        x/=10;
    }
    return dfs(pos-1,0,true);
}
int main(){
    ll p,q;
    memset(dp,-1,sizeof(dp));
    int t;
    scanf("%d",&t);
    while(t--){
        ll s;
        scanf("%lld",&s);
        p=solve(s);
        q = s - p + 1;
        printf("%lld\n",q);
    }
    return 0;
}

 这篇博客不错,学习数位DP的童鞋可以看一下:

http://blog.csdn.net/wust_zzwh/article/details/52100392

以上是关于HDU 3555 数位DP的主要内容,如果未能解决你的问题,请参考以下文章

HDU 3555Bomb 数位DP

Bomb HDU - 3555 (数位DP)

HDU 3555 数位dp入门

数位DP入门题 hdu 2089 hdu 3555

HDU - 3555 - Bomb(数位DP)

HDU 3555 数位DP