Bomb

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

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
 
采用dfs记忆化搜索暴力枚举,dp[len][0]代表长度为len,不含49,首位可以是9的数的个数,dp[len][1]代表长度为len,不含49,首位不是9的数的数的个数,
这样每次枚举当前位置,
a:如果当前位置是4,那么下一位不能是9,所以dfs(len,1,false/true);
b:如果当前位置不是4,那么下一位没有限制,所以dfs(len,0,false/true);
c:如果包含当前位置的前缀与n这个数相同位置的前缀相等的话,那么下一位的枚举就有限制,所以dfs(len,0/1,true);
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define LL long long
LL dp[][];
int digit[]; LL dfs(int len,bool state,bool fp)
{
if(!len)
return ;
if(!fp && dp[len][state] != -)
return dp[len][state];
LL ret = ;int fpmax = fp ? digit[len] : ;
for(int i=;i<=fpmax;i++)
{
if(state && i == )
continue;
ret += dfs(len-,i == ,fp && i == fpmax);
//第二个参数表明这位是否为4,如果为4,下一位就不能为9,否则没有限制
//第三个参数表明前缀是否相同,如果相同,下一位就只能枚举到最大值,否则就没有限制
}
if(!fp)
dp[len][state] = ret;
return ret; //ret代表0到n不含49的个数
} LL f(LL n)
{
int len = ;
while(n)
{
digit[++len] = n % ;
n /= ;
}
return dfs(len,false,true);
} int main()
{
//freopen("test.txt","r",stdin);
LL a,b;
memset(dp,-,sizeof(dp));
int t;
scanf("%d",&t);
while(t--)
{
cin>>b;
cout<<(b+-f(b))<<endl;
} return ;
}
 

hdu3555(数位DP dfs/递推)的更多相关文章

  1. [AHOI2009]中国象棋 DP,递推,组合数

    DP,递推,组合数 其实相当于就是一个递推推式子,然后要用到一点组合数的知识 一道很妙的题,因为不能互相攻击,所以任意行列不能有超过两个炮 首先令f[i][j][k]代表前i行,有j列为一个炮,有k列 ...

  2. UVa 926【简单dp,递推】

    UVa 926 题意:给定N*N的街道图和起始点,有些街道不能走,问从起点到终点有多少种走法. 很基础的dp.递推,但是有两个地方需要注意,在标记当前点某个方向不能走时,也要同时标记对应方向上的对应点 ...

  3. hdu3555 数位dp

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

  4. HDU 5965 三维dp 或 递推

    题意:= =中文题 思路一:比赛时队友想的...然后我赛后想了一下想了个2维dp,但是在转移的时候,貌似出了点小问题...吧?然后就按照队友的思路又写了一遍. 定义dp[i][j][k],表示第i列, ...

  5. 【BZOJ4944】【NOI2017】泳池 概率DP 常系数线性递推 特征多项式 多项式取模

    题目大意 有一个\(1001\times n\)的的网格,每个格子有\(q\)的概率是安全的,\(1-q\)的概率是危险的. 定义一个矩形是合法的当且仅当: 这个矩形中每个格子都是安全的 必须紧贴网格 ...

  6. [NOI2017]泳池——概率DP+线性递推

    [NOI2017]泳池 实在没有思路啊~~~ luogu题解 1.差分,转化成至多k的概率减去至多k-1的概率.这样就不用记录“有没有出现k”这个信息了 2.n是1e9,感觉要递推然后利用数列的加速技 ...

  7. [hdu 2089] 不要62 数位dp|dfs 入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求[n, m]区间内不含4和62的数字个数. 这题有两种思路,直接数位dp和dfs 数位d ...

  8. lightoj 1126 - Building Twin Towers(dp,递推)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1126 题解:一道基础的dp就是简单的递推可以设dp[height_left][ ...

  9. BZOJ5017 [Snoi2017]炸弹[线段树优化建边+scc缩点+DAG上DP/线性递推]

    方法一: 朴素思路:果断建图,每次二分出一个区间然后要向这个区间每个点连有向边,然后一个环的话是可以互相引爆的,缩点之后就是一个DAG,求每个点出发有多少可达点. 然后注意两个问题: 上述建边显然$n ...

随机推荐

  1. ES6__数据结构 Set

    /* 数据结构 Set */ /* *集合的基本概念:集合是由一组无序且唯一(即不能重复)的项组成的.这个数据结构使用了与有限集合相同的数学概念,应用在计算机的数据结构中. *特点:key 和 val ...

  2. boost thread 在非正常退出时 内存泄露问题

    在使用boost的thread库的时候,如果主程序退出,thread创建的线程不做任何处理,则会出现内存泄露. 解决方法: 在主线程退出时,对所有thread使用interrupt()命令,然后主程序 ...

  3. 时间戳转换成DateTime

    select DateAdd(hour,8,Dateadd(ss,时间戳,'1970-01-01'))   --1970/01/01+时间戳(秒数)+8小时 --因GMT是中央时区,北京在东8区,相差 ...

  4. Markdown中插入图片技巧收集

    在操作Markdown时图片应该是最头痛的一件事! 比如要发送一个md文件给对方,如果附带了图片时,那么就要一大堆文件包括图片发给对方等等,如果使用在线图片,那么这个服务器又是一大痛点,因为你不确定这 ...

  5. Java后端技术书单

    写博客记录技术上使用的各种问题,这个只能算是一个打游击. 如果要把一个知识学透,最有效的方式就是系统学习,而系统学习就是看书,书本上有清晰的学习路线以及相应的技术栈. 下面是我收集的Java后端的技术 ...

  6. Unable to process request: General SSLEngine problem.Unable to connect to neo4j at `localhost:7687`, because the certificate the server uses has changed.

    Exception in thread "main" org.neo4j.driver.v1.exceptions.ClientException: Unable to proce ...

  7. 对CSS尺寸单位'em'的长期误解

    一直以来认为'em'是相对于父元素的字体大小. 直到今天学习移动WEB开发,重新复习css的尺寸大小时,惊奇发现:对em深深的误解了!!! 在CSS官网对em的解释实例是: a. h1{line-he ...

  8. CentOS 更改Apache默认网站目录

    http://www.osyunwei.com/archives/789.html引言:Apache默认的网站目录是在/var/www/html, 现在要把网站目录更改到/home/wwwroot/w ...

  9. 运行mapreduce - java.lang.InterruptedException

    错误日志: 2018-11-19 05:23:51,686 WARN [main] util.NativeCodeLoader (NativeCodeLoader.java:<clinit> ...

  10. Visual Studio VS如何修改代码字体

    工具-选项-环境-字体和颜色