【HDU】3555【Bomb】
Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 26679 Accepted Submission(s): 10102
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?
The input terminates by end of file marker.
3
1
50
500
0
1
15HintFrom 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.
题解
此题题意就是给你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】的更多相关文章
- 【HDU 4300 Clairewd’s message】
Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important ...
- 【HDU 4738 Caocao's Bridges】BCC 找桥
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:给定一个n个节点m条边的无向图(可能不连通.有重边),每条边有一个权值.判断其连通性,若双 ...
- 【HDU 2586 How far away?】LCA问题 Tarjan算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵n个节点的无根树,每条边有各自的权值.给出m个查询,对于每条查询返回节点u到v的最 ...
- 【hdu 3177 Crixalis's Equipment】 题解
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3177 \(describe\): 有一个山洞,山洞的容积最大为\(v\).现在你有\(n\)个物品,这 ...
- 【HDU 2594 Simpsons' Hidden Talents】
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- 【HDU 5532 Almost Sorted Array】水题,模拟
给出一个序列(长度>=2),问去掉一个元素后是否能成为单调不降序列或单调不增序列. 对任一序列,先假设其可改造为单调不降序列,若成立则输出YES,不成立再假设其可改造为单调不增序列,若成立则输出 ...
- 【HDU 3336 Count the string】
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- 【HDU 2087 剪花布条】
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
随机推荐
- 基于反射启动Spring容器
基于反射启动Spring容器 package com.maple.test; import org.springframework.context.ApplicationContext; import ...
- Day2 CSS
什么是CSS 层叠样式表(cascading style sheet) 控制页面元素的显示方式.(添加样式) CSS语法 行间样式 行内式是在标记的style属性中设定CSS样式.这种方式没有体现出C ...
- js如何将时间戳转换为标准时间
function formatDate(date,fmt){ let o = { 'M+' : date.getMonth() +1, //月份 'd+' : date.getDate(), //日 ...
- 关于HTML Button点击自动刷新页面的问题解决
原因 button,input type=button按钮在IE和w3c,firefox浏览器区别: 1.当在IE浏览器下面时,button标签按钮,input标签type属性为button的按钮是一 ...
- Jenkins+Github(Robotframework代码)
个人记录,且为Windows10系统,仅供参考. 一. 准备 1. Github:安装git.注册github.将Robotframework代码更新到github. 2. Jenkins安装,并安装 ...
- 使用Message
Message按照定义解释就是topic内容的数据类型, 也称之为topic的格式标准. 1.结构与类型 基本的msg包括bool. int8. int16. int32. int64(以及uint) ...
- Spring源码分析(十四)从bean的实例中获取对象
摘要:本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 在getBean方法中,getObjectForBeanlnstance ...
- CentOS添加并挂载新硬盘
1.查看当前硬盘使用状况: [test@master Desktop]$ df -hFilesystem Size Used Avail Use% Mounted on/dev/sda2 13G 12 ...
- 【vue知识点】1)vue生命周期
[vue知识点]2)vue登录认证
- linux文件系统初始化过程(6)---执行init程序
一.目的 内核加载完initrd文件后,为挂载磁盘文件系统做好了必要的准备工作,包括挂载了sysfs.proc文件系统,加载了磁盘驱动程序驱动程序等.接下来,内核跳转到用户空间的init程序,由ini ...