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学者的学习方向的建议 注: ...

  2. 开源API集成测试工具 Hitchhiker v0.1.3 - 参数化请求

    Hitchhiker 是一款开源的 Restful Api 集成测试工具,你可以轻松部署到本地,和你的team成员一起管理Api. 详细介绍请看: http://www.cnblogs.com/bro ...

  3. 《MATLAB从入门到放弃》打通 “矩阵” 障碍

    目录: »   矩阵的生成与大小  >   简单矩阵的生成  >  随机矩阵的生成  >   矩阵的大小 »  矩阵的索引与访问 »  矩阵的拼接与裁剪 >  矩阵的拼接 &g ...

  4. “一切都是消息”--MSF(消息服务框架)入门简介

    “一切都是消息”--这是MSF(消息服务框架)的设计哲学. MSF的名字是 Message Service Framework 的简称,中文名称:消息服务框架,它是PDF.NET框架的一部分. 1,M ...

  5. 关于数据库中datareader的用法

    1.C#中提供的DataReader可以从数据库中每次提取一条数据. using System; using System.Collections.Generic; using System.Comp ...

  6. Linux入门之常用命令(8)上传下载

    [什么是rz/sz (lsz/lrz)]  简单说就是,可以很方便地用这两个sz/rz工具,实现Linux下和Windows之间的文件传输(发送和接收),速度大概为10KB/s,适合中小文件.rz/s ...

  7. bzoj2111 Perm 排列计数

    称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Magic的,答案可能很大,只能输 ...

  8. Ubuntu安装opencv3.x系列

    p { margin-bottom: 0.25cm; direction: ltr; color: rgb(0, 0, 0); line-height: 120% } p.western { font ...

  9. 简单Elixir游戏服务器开篇

    以前的Elixir游戏服设计系列种种原因没有完成. 后来虽然用Elixir + riak 完成了一个麻将的初始版本,可惜公司也挂了. 现在到新公司,比较空闲,想着像完成一个心愿一样,还是重启下吧(希望 ...

  10. VisualStudio快捷键大全

    Ctrl+m+Crtr+o折叠所有大纲Ctrl+M+Crtr+P: 停止大纲显示Ctrl+K+Crtr+C: 注释选定内容Ctrl+K+Crtr+U: 取消选定注释内容Ctrl+J : 列出成员 智能 ...