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. java基础解析系列(六)---深入注解原理及使用

    java基础解析系列(六)---注解原理及使用 java基础解析系列(一)---String.StringBuffer.StringBuilder java基础解析系列(二)---Integer ja ...

  2. 杂谈--DML触发器学习

    触发器按类型分为三类: 1. DML 触发器,在数据变更时触发: 2. DDL 触发器,在修改数据库级别或实例级别对象时触发: 3. Login 触发器,在用户登录时触发: 最常见的是DML触发器,D ...

  3. Maven实战1

    屏上得来终觉浅,绝知此事要躬行 总结: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px ".PingFang SC"; c ...

  4. M方法

    ThinkPHP函数详解:M方法 M方法用于实例化一个基础模型类,和D方法的区别在于:1.不需要自定义模型类,减少IO加载,性能较好:2.实例化后只能调用基础模型类(默认是Model类)中的方法:3. ...

  5. Linux系统管理命令(1)accton的使用

    安装: apt install acct accton accton命令是Linux系统进程管理命令之一,它的作用是打开进程统计,如果不带任何参数,即关闭进程统计.         具体用法为:acc ...

  6. 批处理之 for/f 详解

    含有/F的for格式:FOR /F ["options"] %%i IN (file) DO command FOR /F ["options"] %%i IN ...

  7. JSP入门 Listener

    实现HttpSessionListener 编写一个OnlineUserListener类 package anni; import java.util.List; import javax.serv ...

  8. web版的tty

      1.wetty Wetty是使用Node.js和websockets开发的一个开源`Web-based SSH` 2.环境配置 2.1.配置epel源 [epel] name=epel baseu ...

  9. 搭建dubbo+zookeeper+dubboadmin分布式服务框架(windows平台下)

    1.zookeeper注册中心的配置安装 1.1 下载zookeeper包(zookeeper-3.4.6.tar.gz),ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Goo ...

  10. CSS3D模型

    html部分 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> < ...