数位DP的DFS写法。。。。

Bomb

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

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
 

Recommend
zhouzeyong
 

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long int LL;

int bit[30];
LL dp[25][3];

LL dfs(int pos,int s,bool limit)
{
    if(pos==-1)
        return s==2;
    if(limit==false&&~dp[pos][s])
        return dp[pos][s];
    int end=limit?bit[pos]:9;
    LL ans=0;
    for(int i=0;i<=end;i++)
    {
        int news=s;
        if(s==0&&i==4) news=1;
        if(s==1&&i!=9) news=0;
        if(s==1&&i==9) news=2;
        if(s==1&&i==4) news=1;
        ans+=dfs(pos-1,news,limit&&i==end);
    }
    if(!limit)
        return dp[pos][s]=ans;
    else
        return ans;
}

int main()
{
    LL n;
    int T;
    memset(dp,-1,sizeof(dp));
    scanf("%d",&T);
    while(T--)
    {
        scanf("%I64d",&n);
        int len=0;
        while(n)
        {
            bit[len++]=n%10;
            n/=10;
        }
        printf("%I64d\n",dfs(len-1,0,1));
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

HDOJ 3555 Bomb的更多相关文章

  1. hdoj 3555 BOMB(数位dp)

    //hdoj 3555 //2013-06-27-16.53 #include <stdio.h> #include <string.h> __int64 dp[21][3], ...

  2. hdoj 3555 Bomb(DFA+dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 思路分析:该问题要求求解1—N中的数中含有49的数的个数,可以使用DFA来递推dp公式:详细解释 ...

  3. HDU 3555 Bomb 数位dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Mem ...

  4. 数位DP入门之hdu 3555 Bomb

    hdu 3555 Bomb 题意: 在1~N(1<=N<=2^63-1)范围内找出含有 ‘49’的数的个数: 与hdu 2089 不要62的区别:2089是找不不含 '4'和 '62'的区 ...

  5. HDOJ 题目3555 Bomb(数位DP)

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Subm ...

  6. HDU 3555 Bomb(数位DP模板啊两种形式)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 Problem Description The counter-terrorists found ...

  7. HDU 3555 Bomb (数位DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题目大意:从0开始到给定的数字N所有的数字中遇到“49”的数字的个数. Sample Input ...

  8. HDU 3555 Bomb(数位DP)

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Subm ...

  9. hdu 3555 Bomb(不要49,数位DP)

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submi ...

随机推荐

  1. MATLAB仿真总结

    MATLAB仿真过程中,编写MATLAB代码的时候犯了很多错误,做了很多蠢事.记录下自己犯错的点点滴滴,并引以为戒.使用MATLAB版本为2014a,以下内容如有不当还请指正. 1. 仿真开始前清理工 ...

  2. Ajax基础详解2

    沐晴又来更新啦,话说我们上回讲到Ajax中open方法的第三个参数异步和同步的问题,今天呢,就来继续往下唠,先接着上回的代码 var oBtn = document.getElementById('b ...

  3. EntityFramework:值不能为 null。参数名: entitySet 异常解决方案

    昨天EF莫名其妙的,掉所有接口访问都出现如下错误:百度,Google了半天,倒是有很多人都遇到了这个问题,但都没有一个解决方案,或者解决方案无效.通过层层排除,终于找到问题的所在.记录下来,给以后再遇 ...

  4. 360wifi 在 windows server 2008 / 2003 的使用方法

    1. 安装驱动 在地址栏输入:Control Panel\System and Security\Administrative Tools , 然后找到Server Manager 打开 Server ...

  5. js中的this中使用

    请先查看:http://www.jb51.net/article/41656.htm 情况一:纯粹的函数调用 这是函数的最通常用法,属于全局性调用,因此this就代表全局对象Global. 情况二:作 ...

  6. node的实践(项目二)

    找以前看看简单的demo,看看node是怎么操作Mongo然后又是渲染前台的,与前面的项目一中的对比. 1.操作Mongo数据库的方法和方式. var mongodb = require('./db' ...

  7. (好文推荐)一篇文章看懂JavaScript作用域链

    闭包和作用域链是JavaScript中比较重要的概念,首先,看看几段简单的代码. 代码1: var name = "stephenchan"; var age = 23; func ...

  8. Xunit

    Attributes Note: This table was written back when xUnit.net 1.0 has shipped, and needs to be updated ...

  9. 替换文件最后一行中的所有e 为 E

    #root@athena5plus:~# cat b    northwest       NW     Charles Main           3.0      .98      3      ...

  10. JSP页面中 <base href="<%=basePath%>">

    base标记是一个基链接标记,是一个单标记.用以改变文件中所有连结标记的参数内定值.它只能应用于标记<head>与</head>之间.你网页上的所有相对路径在链接时都将在前面加 ...