首页 > 其他 > 详细

HDU3555 Bomb 数位DP第一题

时间:2017-11-01 10:57:04      阅读:219      评论:0      收藏:0      [点我收藏+]
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

技术分享
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define LL long long
const int N=30;
LL dp[N][2][2][2],n,ans;
int a[N],cnt;
void _divide(LL v){
    cnt=0;
    while(v){
        a[++cnt]=v%10;
        v/=10;
    }return ;
}
LL _dfs(int pos,bool limit,bool pre,bool stat)
{
     if(pos==0) return stat;
     LL tmp=0;
     if(!limit&&dp[pos][limit][pre][stat]) return dp[pos][limit][pre][stat];
     int Up=limit?a[pos]:9;
     for(int i=0;i<=Up;i++)
         tmp+=_dfs(pos-1,limit&&i==Up,i==4,stat||(pre&&i==9));
     dp[pos][limit][pre][stat]=tmp;
     if(tmp>ans) ans=tmp;
     return tmp;
}
int main()
{
    int i,T;
    scanf("%d",&T);
    while(T--){
        memset(dp,0,sizeof(dp));
        scanf("%lld",&n);
        _divide(n);
        printf("lld\n",_dfs(cnt,true,false,false));
    }
    return 0;
}
View Code

 

HDU3555 Bomb 数位DP第一题

原文:http://www.cnblogs.com/hua-dong/p/7765361.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!