题目链接

Bomb

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


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
 

Statistic | Submit | Discuss | Note

题解

此题题意就是给你T个n,找出n以内的包含49的数的个数。

很裸的一题数位dp。

直接dp包含49的数的个数有点麻烦,所以我先算出不包含49的数的个数,然后用n+1来减(因为计算不包含49的数中是包括0的,所以减了之后还要加1)。

在搜索的过程中记录三个数,l表示当前是第几位,mach表示上一位是否为4,upp表示之前的位数是否都是取最大值,如果之前的数都是取最大值,当前的位只能取0到n的当前位,而不是0到9.

然后开始搜索,出现连续的49就跳过。

dp[j][i]记录第j位时上一位是否为4(i表示)不包含49的数的个数,用记忆化,如果搜过就直接加上。

因为上一位是否为4决定了当前位能否为9,对后面数的个数有影响,所以要加一维i。

上代码

#include<bits/stdc++.h>
using namespace std;
int t;
long long n;
int l,a[109];
long long dp[109][3];
long long dfs(int l,bool mach,bool upp) {
if(l<=0) return 1;
if(upp==0 && dp[l][mach]!=-1) return dp[l][mach];
int up;
if(upp==1) up=a[l];
else up=9;
long long sum=0;
for(int j=0; j<=up; j++) {
if(mach==1 && j==9) continue;
sum+=dfs(l-1,j==4,upp==1 && j==up);
}
if(upp==0) dp[l][mach]=sum;
return sum;
}
long long ans(long long x) {
while(x>0) {
l++;
a[l]=x%10;
x/=10;
}
return dfs(l,0,1);
}
int main() {
scanf("%d",&t);
memset(dp,-1,sizeof(dp));
while(t--) {
scanf("%lld",&n);
l=0;
printf("%lld\n",n+1-ans(n));
}
return 0;
}

【HDU】3555【Bomb】的更多相关文章

  1. 【HDU 4300 Clairewd’s message】

    Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important ...

  2. 【HDU 4738 Caocao's Bridges】BCC 找桥

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:给定一个n个节点m条边的无向图(可能不连通.有重边),每条边有一个权值.判断其连通性,若双 ...

  3. 【HDU 2586 How far away?】LCA问题 Tarjan算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵n个节点的无根树,每条边有各自的权值.给出m个查询,对于每条查询返回节点u到v的最 ...

  4. 【hdu 3177 Crixalis's Equipment】 题解

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3177 \(describe\): 有一个山洞,山洞的容积最大为\(v\).现在你有\(n\)个物品,这 ...

  5. 【HDU 2594 Simpsons' Hidden Talents】

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  6. 【HDU 5532 Almost Sorted Array】水题,模拟

    给出一个序列(长度>=2),问去掉一个元素后是否能成为单调不降序列或单调不增序列. 对任一序列,先假设其可改造为单调不降序列,若成立则输出YES,不成立再假设其可改造为单调不增序列,若成立则输出 ...

  7. 【HDU 3336 Count the string】

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  8. 【HDU 2087 剪花布条】

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  9. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

随机推荐

  1. U-Mail详解邮件营销优势及应用领域

    最近频频有营销人员向U-Mail小编咨询:邮件营销到底有什么好处呢?与此同时,还有不少人对邮件营销存在一定的误解:邮件营销是不是只给潜在消费者发送邮件推广商品呢?其实邮件群发的应用面非常广泛,可不仅仅 ...

  2. HBase TableExistsException: hbase:namespace

    这个报错一般存在于独立安装Zookeeper集群中. 报这个错的操作时这样的, 先停掉了了Hbase formatZK后重启Hbase 启动hbase shell 后HMaster挂掉,看log里就有 ...

  3. python第七课——循环结构 while

    while循环: 2.1.有四要素组成: ①.初始化条件(执行一次)一个起始数据/起点,一般使用变量来进行存储 ②.循环条件(可能执行多次)循环合适结束全靠它,执行结果为True,那么循环继续,反之, ...

  4. SharePoint 改动passwordWeb Part部署方案

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u012025054/article/details/31773231 SharePoint 改动pa ...

  5. [Violet]天使玩偶/SJY摆棋子

    题目 \(KD-tree\)做最近点对的复杂度好像是假的吧,怎么看也看不出来是\(O(\sqrt{n})\)啊 首先\(KD-tree\)长得和平衡树还是很像的,每个节点都存储了一个\(k\)维空间上 ...

  6. 【转】 iOS播放视频时候,忽略设备静音按钮

    用户有时会在静音模式下观看视频,如果不主动设置的话,视频是没有声音的,通过AVAudioSession可开启以视频为主导的播放模式, 首先需要导入,AVFoundtion.framework,在控制播 ...

  7. kukubeadm 1.6.1 + docker1.2.6 安装问题

    kubeadm init --apiserver-advertise-address=192.168.20.229 --pod-network-cidr=10.244.0.0/16 kubelet: ...

  8. 【MongoDB】MongoDB与项目搭配启动进程

    项目启动/数据连接命令  (20180701成功且不用再找正确关闭mongoDB的方式) 如上图在mongoDB的bin目录的同级新建mongo.config.mongostart.bat.mongo ...

  9. 参加360前端星计划总结(二)--HTML&CSS

    HTML学习手册(英文版)html:the living standard 重要知识点 文档声明的作用a. 指定html的文档标准和版本b. 告诉浏览器渲染模式,有怪异模式(较为古老的模式,不写文档声 ...

  10. Linux磁盘与文件系统管理(一)

    fdisk 常用的磁盘分区工具,受mbr分区表的限制,只能给小于2TB的磁盘划分分区,如果使用fdisk对大于2TB的磁盘进行分区,虽然可以分区,但只能识别2T的空间,一般使用parted分区工具 - ...