码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3555 Bomb (数位dp)

时间:2019-04-09 00:12:22      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:iostream   click   else   sig   sizeof   you   思路   closed   max   

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.
 
题意:
小于所给数字的数字有多少个包含49.
思路:
数位dp基础,详见代码
 
技术图片
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 100086;
const int inf = 2.1e9;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);
int bit[20];
ll dp[20][4];
//sta表示三种状态。
//1:pos结尾处是4
//2:pos之前有49
//0:不含以上两种情况

ll dfs(int pos,int sta,bool limit){
    if(pos==-1&&sta==2){return 1ll;}
    else if(pos==-1){return 0;}
    else if(!limit&&dp[pos][sta]!=-1){
        return dp[pos][sta];
    }
    int up=limit?bit[pos]:9;
    ll ans=0;
    for(int i=0;i<=up;i++){
        if(sta==2||(sta==1&&i==9)){//之前有49或者刚刚凑齐一个
            ans+=dfs(pos-1,2,limit&&i==up);
        }
        else if(i==4){//pos结尾处是4
            ans+=dfs(pos-1,1,limit&&i==up);
        }
        else{
            ans+=dfs(pos-1,0,limit&&i==up);
        }
    }
    if(!limit){dp[pos][sta]=ans;}//没有限制才能赋值给dp。
    return ans;
}

ll solve(ll t){

    int pos=0;
    while(t){
        bit[pos++]=t%10;
        t/=10;
    }
    return dfs(pos-1,0,true);
}

int main()
{
    int T;
    scanf("%d",&T);
    memset(dp,-1,sizeof(dp));
    while(T--){
        ll n;
        scanf("%lld",&n);
        printf("%lld\n",solve(n));
    }
    return 0;
}
View Code

 

 

HDU 3555 Bomb (数位dp)

标签:iostream   click   else   sig   sizeof   you   思路   closed   max   

原文地址:https://www.cnblogs.com/ZGQblogs/p/10674349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!