数位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. Mongodb使用基本之——安装

    版本是:Mongodb 3.2.1 本来是想用python做个爬虫然后爬取一些数据放到数据库上的,想着想着以为NoSQL会很流行,就用了Mongodb,结果,一折腾真是不容易. 遇到的第一个问题:官网 ...

  2. 解决SourceGrid在某些系统上无法用鼠标滚轮滚动的问题

    4.40版源码,找到SourceGrid/SourceGrid/Common/CustomScrollControl.cs 这个文件 定位 CustomScrollWheel() 方法,把if条件判断 ...

  3. c#简单自定义异常处理日志辅助类

    简单写了一个错误日志记录辅助类,记录在此. Loghelper类 using System; using System.Collections.Generic; using System.IO; us ...

  4. Bootstrap3.0学习第十七轮(JavaScript插件——模态框)

    详情请查看http://aehyok.com/Blog/Detail/24.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...

  5. WRONGTYPE Operation against a key holding the wrong kind of value

    今天改动代码,一运行就跑错了,错误原因: 因为redis中已经存在了相同的key, 而且key对应的值类型并不是Set,而是SortSet(改动前):再调用smembers时,抛出此错误. 解决方法: ...

  6. Daily Scrum – 1/4

    Meeting Minutes 大家讨论了一下作业的内容,以及用户的反馈,商量了一下长期计划(naive)的完成方式. 好像有些时候用户测试的时候会崩溃,不过我们自己用的时候一直没有出现过,分析可能是 ...

  7. OC基础--OC中类的声明与定义

    OC中设计一个类的步骤: 一.声明类: 1.用到的关键字--@interface 和 @end 2.类名 3.继承NSObject 4.属性 5.方法(行为,只需要声明) 二.实现(定义)类 1.用到 ...

  8. POJ 2153 stl

    #include<iostream> #include<map> #include<string> using namespace std; int main() ...

  9. str和repr的

    尽管str(),repr()和``运算在特性和功能方面都非常相似,事实上repr()和``做的是完全一样的事情,它们返回的是一个对象的“官方”字符串表示,也就是说绝大多数情况下可以通过求值运算(使用内 ...

  10. Blog Explanation

    有疑问或blog中说明错误的欢迎评论或联系QQ:30056882,邮箱BLADEVIL@outlook.com.本人水平不高,欢迎讨论问题. blog中所有随笔均为原创,转载请注明来源. (已退役.)