HDU 3555 Bomb(数位dp 深搜版)

来源:互联网 发布:ubuntu u盘版本 编辑:程序博客网 时间:2024/05/22 08:13
BombTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 15793    Accepted Submission(s): 5754Problem DescriptionThe 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 Input3150500Sample Output0115HintFrom 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.Authorfatboy_cw@WHUSource2010 ACM-ICPC Multi-University Training Contest(12)——Host by WHURecommendzhouzeyong
之前写数位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|1typedef 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;}
0 0