hdu 3555 Bomb(数位dp入门)

Bomb

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 24148    Accepted Submission(s): 9092

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


#include<bits/stdc++.h>
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<math.h>
#include<queue>
#include<set>
#include<map>
#include<iomanip>
#include<algorithm>
#include<stack>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
int bit[40];
ll f[40][3];
ll dp(int pos,int st,bool flag)
{
    if(pos==0)return st==2;
    if(flag&&f[pos][st]!=-1)return f[pos][st];
    ll ans=0;
    int x=flag?9:bit[pos];
    for(int i=0;i<=x;i++)
    {
        if((st==2)||(st==1&&i==9))
        {
            ans+=dp(pos-1,2,flag||i<x);
        }
        else if(i==4)ans+=dp(pos-1,1,flag||i<x);
        else ans+=dp(pos-1,0,flag||i<x);
    }
    if(flag)f[pos][st]=ans;
    return ans;
}
ll calc(ll x)
{
    int len=0;
    while(x)
    {
        bit[++len]=x%10;
        x/=10;
    }
    return dp(len,0,0);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(f,-1,sizeof(f));
        ll n;
        scanf("%lld",&n);
        printf("%lld
",calc(n));
    }
    return 0;
}



原文地址:https://www.cnblogs.com/linruier/p/9919160.html