Bomb

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

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

/*
这个是自己的代码反着求的,正如“不要49”,跑了65ms
*/
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string.h>
#include<cstdio>
#define N 22
using namespace std;
long long t,n;
int g[N];//用来存放数字的位数
long long dp[N][N];//dp[i][j][k]表示当前剩余位数是i上一位数字是j且当前位上是否为4的状态的个数
long long dfs(int len,bool s,bool f)//s表示是不是当前位上是不是4
{
if(len<)
return ;
if(!f&&dp[len][s]!=-)
return dp[len][s];
int fmax=f?g[len]:;
long long cur=;
//cout<<"fmax="<<fmax<<endl;
for(int i=;i<=fmax;i++)
{
if(s&&i==) continue;//不要有49
cur+=dfs(len-,i==,f&&(i==fmax));
//cout<<"cur="<<cur<<endl;
}
//cout<<cur<<endl;
if(!f)
dp[len][s]=cur;
return cur;
}
long long solve(long long x)
{
int len=;
while(x!=)
{
g[len++]=x%;
x/=;
}
//for(int i=1;i<=len;i++)
// cout<<g[i];
//cout<<endl;
memset(dp,-,sizeof dp);
return dfs(len-,false,true);
}
int main()
{
//freopen("in.txt","r",stdin);
scanf("%lld",&t);
while(t--)
{
scanf("%I64d",&n);
printf("%I64d\n",n-solve(n)+);
}
return ;
}
/*
一开始真的按照“不要49”这么来求的。写博客的时候看看别人博客吸收精华,下面是正着求得,z[i]数组真是神来之笔
本来我也行正着求但是不知道怎么表示找到49之后应该加什么,一个z[i]完美解决了找到49之后应该加多少
*/
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string.h>
#include<cstdio>
#define N 30
using namespace std;
long long t,n;
int g[N];//用来存放数字的位数
long long dp[N][];//dp[i][j][k]表示当前剩余位数是i上一位数字是j且当前位上是否为4的状态的个数
long long z[N]={};
long long dfs(int len,bool s,bool f)//s表示是不是当前位上是不是4
{
if(len==)
return ;
if(!f&&dp[len][s]>=)
return dp[len][s];
int fmax=f?g[len]:;
long long cur=;
//cout<<"fmax="<<fmax<<endl;
for(int i=;i<=fmax;i++)
{
if(s&&i==)
{
cur+=f?n%z[len-]+:z[len-];//当前位找到了,剩下的不用搜了,直接加上就行了,这里加的是剩下的所有位
}
else
cur+=dfs(len-,i==,f&&g[len]==i);
//cout<<"cur="<<cur<<endl;
}
//cout<<cur<<endl;
return f?cur:dp[len][s]=cur;
}
long long solve(long long x)
{
int len=;
while(x)
{
g[++len]=x%;
x/=;
}
//for(int i=1;i<=len;i++)
// cout<<g[i];
//cout<<endl;
g[len+]=;
return dfs(len,false,true);
}
int main()
{
//freopen("in.txt","r",stdin);
for (int i=;i<N;i++)
{
z[i]=z[i-]*;
}
scanf("%lld",&t);
memset(dp,-,sizeof dp);
while(t--)
{
scanf("%lld",&n);
printf("%lld\n",solve(n));
}
return ;
}
 

hdu 3555 Bomb(不要49,数位DP)的更多相关文章

  1. hdu 3555 Bomb 炸弹(数位DP,入门)

    题意: 给一个数字n,求从1~n中有多少个数是含有49的,比如49,149,1490等都是含49的. 思路: 2^64也顶多是十进制的20多位,那么按十进制位来分析更简单.如果能计算k位十进制数中分别 ...

  2. Bomb(要49)--数位dp

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

  3. 数位DP入门之hdu 3555 Bomb

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

  4. HDU 3555 Bomb 数位dp

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

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

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

  6. HDU 3555 Bomb(数位DP)

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

  7. HDU 3555 Bomb 数位DP 入门

    给出n,问所有[0,n]区间内的数中,不含有49的数的个数 数位dp,记忆化搜索 dfs(int pos,bool pre,bool flag,bool e) pos:当前要枚举的位置 pre:当前要 ...

  8. 动态规划晋级——HDU 3555 Bomb【数位DP详解】

    转载请注明出处:http://blog.csdn.net/a1dark 分析:初学数位DP完全搞不懂.很多时候都是自己花大量时间去找规律.记得上次网络赛有道数位DP.硬是找规律给A了.那时候完全不知数 ...

  9. 数位dp整理 && 例题HDU - 2089 不要62 && 例题 HDU - 3555 Bomb

    数位dp: 数位dp是一种计数用的dp,一般就是要统计一个区间[li,ri]内满足一些条件数的个数.所谓数位dp,字面意思就是在数位上进行dp.数位的含义:一个数有个位.十位.百位.千位......数 ...

随机推荐

  1. 【京东详情页】——原生js爬坑之二级菜单

    一.引言 做京东详情页仿写的时候,要用原生js实现顶部菜单的二级菜单显示与隐藏事件的触发. 过程中遇到了一个坑,在这里与大家分享.要实现的效果如下: 二.坑 谁触发事件?显示.隐藏二级菜单       ...

  2. [js高手之路] html5 canvas系列教程 - 图片操作(drawImage,clip,createPattern)

    接着上文[js高手之路] html5 canvas系列教程 - 文本样式(strokeText,fillText,measureText,textAlign,textBaseline)继续,本文介绍的 ...

  3. Java Byte取值范围

    Java Byte 的取值范围大家都知道(-128 ~ 127),那么-128 和 127 这两个数是怎么计算的呢? #大学知识回顾: 概念:负数的补码是该 数 绝 对 值 的 原 码 按 位 取 反 ...

  4. ThinkPHP中:检查Session是否过期

    1.创建Session public function index(){ $sess_time=time(); session('name','andy'); session('time_stamp' ...

  5. ElasticSearch入门(3) —— head插件

    #### 安装ES head插件 具体请参考github地址:https://github.com/mobz/elasticsearch-head 使用 安装Install # 在线安装head插件 ...

  6. 快速搭建应用服务日志收集系统(Filebeat + ElasticSearch + kibana)

    快速搭建应用服务日志收集系统(Filebeat + ElasticSearch + kibana) 概要说明 需求场景,系统环境是CentOS,多个应用部署在多台服务器上,平时查看应用日志及排查问题十 ...

  7. 再起航,我的学习笔记之JavaScript设计模式30(简单模板模式)

    简单模板模式 概念介绍 简单模板模式(Simple template): 通过格式化字符串拼凑出视图避免创建视图时大量节点操作,优化内存开销. 创建模板 在实际的业务中如果我们需要进行前后台交互,或多 ...

  8. 考了3年,工作四年,零基础在职终于拿到CFA证书

    大家都知道CFA Charterholder是独有的全球公认的投资管理从业人员高职业水平和道德水准的有力证明,是金融界卓越专业成就的象征:CFA资格强调和遵循极其严格的职业操守和道德准则,世界各主要发 ...

  9. Linux 独立安装subversion-1.8.18

    一.所需软件包 1.apr-1.4.6.tar.gz 下载地址:http://apr.apache.org/   2.apr-util-1.4.1.tar.gz 下载地址:http://apr.apa ...

  10. jvm的垃圾回收算法

    一.对象存活判断判断对象是否存活一般有两种方式:1.引用计数:每个对象有一个引用计数属性,新增一个引用时计数加1,引用释放时计数减1,计数为0时可以回收.此方法简单,无法解决对象相互循环引用的问题.2 ...