HDU 3555 Bomb(数位dp 深搜版)

28 篇文章 0 订阅
13 篇文章 0 订阅
Bomb

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


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.

Author
fatboy_cw@WHU

Source
2010 ACM-ICPC Multi-University Training Contest(12)——Host by WHU

Recommend
zhouzeyong
之前写数位dp一直自己推出状态转移方程,推的过程中也会遇到了一些小麻烦。
然后网上说数位dp的dfs版本比较常用,基本模板化,不过有些题目还是要自己去推敲。
确实,dfs版本写起来也比较简单。
安利一篇文章,个人也觉得讲得比较详细。

传送门: 数位dp总结 之 从入门到模板

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define inf 0x3f3f3f3f

#define Pi 4.0*atan(1.0)
#define Sqrt(x) (x)*(x)

#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-12;
const int maxn = 3000;
const int maxm = 1500;
using namespace std;
inline int read(){
    int x(0),f(1);
    char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
ull f[20][3];
int bit[20];
inline ull dfs(int pos,int st,bool flag)
{
    if(pos==0){
        return st==2;
    }
    if(!flag&&~f[pos][st]){
        return f[pos][st];
    }
    ull ret=0;
    int x=flag?bit[pos]:9;
    for(int i=0;i<=x;++i){
        if((st==2)||(st==1&&i==9)){
            ret+=dfs(pos-1,2,flag&&i==bit[pos]);
        }
        else if(i==4){
            ret+=dfs(pos-1,1,flag&&i==bit[pos]);
        }
        else{
            ret+=dfs(pos-1,0,flag&&i==bit[pos]);
        }
    }
    if(!flag){
        f[pos][st]=ret;
    }
    return ret;
}
ull calc(ull n)
{
    int k=0;
    while(n)
    {
        bit[++k]=n%10;
        n/=10;
    }
    return dfs(k,0,true);
}
int main()
{
    int t=read();
    ull n;
    mes(f,-1);
    while(t--){
        scanf("%llud",&n);
        cout<<calc(n)<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值