题目链接

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. Java使用HTTP编程模拟多参数多文件表单信息的请求与处理

    本文目的是提供Java环境下模拟浏览器页面提交多参数多文件表单请求以及解析请求的Demo代码.这里用Java提供的HttpURLConnection类做HTTP请求,再原始点可以直接使用socket. ...

  2. Django之模板配置(template)

    Django模板系统 官方文档 jinja2模块中文 jinja2模块官方 常用语法 只需要记两种特殊符号: {{  }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 变量 在Djan ...

  3. Git rebase日志

    Git日志重写 为了方便管理,最近公司git接了jira,然后开发任务需要在jira上面先建立task,然后再task上面建立分支,后面该分支就和这个task进行了绑定. 因为之前一直使用传统的svn ...

  4. JavaScript中操作有些DOM时关于文本节点和元素节点的问题。

    初学JavaScript时候,经常遇到需要操作的内容因为文本节点而使操作更麻烦或者不能达到效果. 例: <ul id="ull">    <li>111&l ...

  5. [USACO19FEB]Moorio Kart

    题目 我们的神仙教练在考试里放了这道题,当时我非常惊讶啊 背包是\(O(n^3)\)的吧明明是带根号的好吧,那既然要优化的话 NTT!什么时候我们教练会在考试里放多项式了 模数\(1e9+7\)? 任 ...

  6. Java并发编程--7.Java内存操作总结

    主内存和工作内存 工作规则 Java内存模型, 定义变量的访问规则, 即将共享变量存储到内存和取出内存的底层细节  所有的变量都存储在主内存中,每条线程有自己的工作内存,工作内存中用到的变量, 是从主 ...

  7. SVN 客户端使用

    一.TortoiseSVN基本设置 1.1 客户端设置     1.1  语言设置       二.基本操作 2.1 浏览服务器           用户名跟密码,跟公司配置管理员人员获取,没有专门的 ...

  8. openstack的网络模式(转)

    单节点上虚拟机和虚拟机之间通信 直接同过linuxbridge转发数据,即可通信 虚拟机跨物理节点通信 利用交换机的包转发机制,在大二层网络中转发数据包 虚拟机跟外网通信 同样利用交换机转发,将数据包 ...

  9. springboot mybatis 后台框架平台 shiro 权限 集成代码生成器

    1.代码生成器: [正反双向](单表.主表.明细表.树形表,快速开发利器)+快速表单构建器freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本.处理类.servic ...

  10. java crm 进销存 springmvc SSM 项目 源码 系统

    系统介绍: 1.系统采用主流的 SSM 框架 jsp JSTL bootstrap html5 (PC浏览器使用) 2.springmvc +spring4.3.7+ mybaits3.3  SSM ...