我是靠谱客的博主 温暖大门,最近开发中收集的这篇文章主要介绍【数位DP】HDU3555-Bomb,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3555

题目基本同HDU2089,不过要注意,这里是long long 型

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<iostream>
#include<cstring>
#include<cstdio>
#define LL __int64
using namespace std;
const int maxn=20;
LL dp[maxn][2];
int bit[maxn]={0};
LL dfs(int pos,bool is4,bool ismax)
{
    if(pos==0) return 1;
    if(!ismax&&dp[pos][is4]>=0) return dp[pos][is4];
    LL cnt=0;
    int maxnum=ismax?bit[pos]:9;
    for(int i=0;i<=maxnum;i++){
        if(is4&&i==9) continue;
        cnt+=dfs(pos-1,i==4,ismax&&i==maxnum);
    }
    if(!ismax) dp[pos][is4]=cnt;
    return cnt;
}
LL solve(LL x)
{
    int pos=0;
    while(x){
        bit[++pos]=x%10;
        x/=10;
    }
    return dfs(pos,false,true);
}
int main()
{
    LL t,n;
    scanf("%I64d",&t);
    memset(dp,-1,sizeof(dp));
    while(t--){
        scanf("%I64d",&n);
        printf("%I64dn",n-solve(n)+1);
    }
    return 0;
}


最后

以上就是温暖大门为你收集整理的【数位DP】HDU3555-Bomb的全部内容,希望文章能够帮你解决【数位DP】HDU3555-Bomb所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部