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. JavaScript随机数类型

    1.Math.random(); 结果为0-1间的一个随机数(包括0,不包括1) 2.Math.floor(num); 参数num为一个数值,函数结果为num的整数部分. 3.Math.round(n ...

  2. ES6 Promise 对象

    Promise 的含义 Promise 是异步编程的一种解决方案,比传统的解决方案--回调函数和事件--更合理和更强大.它由社区最早提出和实现,ES6 将其写进了语言标准,统一了用法,原生提供了Pro ...

  3. vue.js项目构建

    这里构建的vue.js项目依赖node服务器运行. 项目搭建完整步骤: 安装node.js ,转至nodeJs网站http://nodejs.cn/ 下载nodeJs进行安装. 安装完毕检查nodeJ ...

  4. swift 开发学习问题

    1 UITableViewController 问题: [UITableView dequeueReusableCellWithIdentifier:forIndexPath:], unable to ...

  5. mysql用户权限配置

    创建管理员: mysqladmin -u root password 123456 登录 mysql -u root -p 建库及授权 > create database bdp charact ...

  6. NOIP2017SummerTraining0712

    个人感受:打了三个小时不到的第一题,然后也就没有多少时间去搞第二题了,特别是第二题还看到了期望这样的东西,这个难以理解,第三题的树分治,myx大佬说50分好拿,但是我觉得也挺难拿的. 单词检索 时间限 ...

  7. Elixir游戏服设计三

    玩家进程用gen_server来建模,我不直接使用 use GenServer, 而是使用exactor,该库可以去掉反锁的接口定义. 我们新建一个 player_server_manager app ...

  8. 在项目中创建单元测试时junit的配置和使用

    首先配置项目中AndroidMainfest.xml文件,加入 <instrumentation android:name="android.test.InstrumentationT ...

  9. less使用ch1--认识语法

    @charset "utf-8"; //注释------------------------------ /*我是可以被编译出来的*/ //不能被编译出来 //变量-------- ...

  10. 云计算---openstack各服务端口使用情况

    端口占用情况 端口情况可以使用ss -tanp命令进行查看 监听的所有端口ss -tanp | grep LISTEN 基础服务 22 --SSH 3306 --MariaDB(MySQL) 2701 ...