【数位DP-板子题目】HDU-3555-Bomb- [只要49]

Bomb

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


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.

 

 

 

题解:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include<set>
using namespace std;
#define inf 0x3f3f3f3f
const double pi=acos(-1.0);    ///数位DP,只要49
#define ll long long
#define lson root<<1
#define rson root<<1|1
const ll mod = 1000000;
#define ull unsigned long long
ll digit[30];
ll dp[20][2];
    ///limit表示是否受限,最初进行的时候必定受限!
ll Cul(int len,bool if4,bool limit){

    if(len<1)return 1;
    if(!limit&&dp[len][if4]!=-1)return dp[len][if4];
    int up_bound=(limit==1)?digit[len]:9;
    ll ans=0;
    for(int i=0;i<=up_bound;i++)
    {
        if(if4&&i==9)
            continue;
        ans+=Cul(len-1,i==4,limit&&i==digit[len]);
    }
    dp[len][if4]=ans;
    return ans;
}

ll solve(ll n){
    int k=0;
    while(n){
        digit[++k]=n%10;
        n/=10;
    }
    memset(dp,-1,sizeof(dp));
    return Cul(k,false,true);
}

int main(){

    ll n,T;
    cin>>T;
    while(T--){
        scanf("%lld",&n);
        printf("%lld\n",n-solve(n)+1);

    }

    return 0;
}

 

posted @ 2018-09-23 14:57  山枫叶纷飞  阅读(260)  评论(0编辑  收藏  举报