hdu3555 Bomb (数位dp入门题)
Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 19698 Accepted Submission(s): 7311
Problem Description
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?
Input
The input terminates by end of file marker.
Output
Sample Input
1
50
500
Sample Output
1
15
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.
代码(dfs解法):
#include<iostream>
#include<cstring>
#define LL long long
using namespace std; LL bit[25], dp[25][2]; LL dfs(int pos, int is4, int lim)//pos当前位, is4上一位是否为4, lim上一位是否取到最大值
{
if(pos<0) return 1;
if(!lim && dp[pos][is4]!=-1) return dp[pos][is4];
int las=lim?bit[pos]:9;//若上以一位没有取最大值位则可以0~9循环
LL res=0;
for(int i=0; i<=las; ++i)
if(!(is4 && i==9))//搜索不含49的,结果取反
res+=dfs(pos-1, i==4, lim&&i==las);
if(!lim) dp[pos][is4]=res;
return res;
} int main()
{
int T;
memset(dp, -1, sizeof(dp));
cin>>T;
while(T--)
{
LL n;
cin>>n;
LL len=0, m=n;
while(n)
{
bit[len++]=n%10;
n/=10;
}
LL ans=m-dfs(len-1, 0, 1)+1;//结果取反, 且搜索过程中会包含0, 故+1
cout<<ans<<endl;
}
return 0;
}
代码(传统dp):
#include<iostream>
#include<cstring>
#include<cstdio>
#define LL long long
using namespace std; LL num[25], dp[25][3]; int main()
{
int T;
memset(dp, 0, sizeof(dp));
dp[0][0]=1;
for(int i=1; i<21; ++i)
{
dp[i][0]=dp[i-1][0]*10-dp[i-1][1];//dp[i][0]表示i位数不包含49且最高位不是9的数目
dp[i][1]=dp[i-1][0];//dp[i][1]表示i位数不包含49且最高位是9的数目
dp[i][2]=dp[i-1][2]*10+dp[i-1][1];//dp[i][2]表示i位数包含49的数目
}
cin>>T;
while(T--)
{
LL n;
cin>>n;
int len=0;
memset(num, 0, sizeof(num));
while(n)
{
num[++len]=n%10;
n/=10;
}
int las=0;
bool flag=false;
LL ans=0;
for(int i=len; i>=1; --i)
{
ans+=(dp[i-1][2]*num[i]);//若n=789, 则i=3时此处计算700以内的结果
if(flag) ans+=dp[i-1][0]*num[i];//若之前已包含49
if(!flag && num[i]>4) ans+=dp[i-1][1];//若之前未包含49
if(las==4 && num[i]==9) flag=true;
las=num[i];
}
if(flag) ans++;
cout<<ans<<endl;
}
return 0;
}
hdu3555 Bomb (数位dp入门题)的更多相关文章
- hdu3555 Bomb 数位DP入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 简单的数位DP入门题目 思路和hdu2089基本一样 直接贴代码了,代码里有详细的注释 代码: ...
- HDU3555 Bomb 数位DP第一题
The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the ti ...
- hdu3555 Bomb(数位dp)
题目传送门 Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total ...
- HDU 2089 不要62【数位DP入门题】
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu---(3555)Bomb(数位dp(入门))
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submi ...
- HDU3555 Bomb —— 数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) M ...
- HDU 2089 - 不要62 - [数位DP][入门题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 Time Limit: 1000/1000 MS (Java/Others) Memory Li ...
- HDU 3555 Bomb 数位DP 入门
给出n,问所有[0,n]区间内的数中,不含有49的数的个数 数位dp,记忆化搜索 dfs(int pos,bool pre,bool flag,bool e) pos:当前要枚举的位置 pre:当前要 ...
- 数位DP入门题——[hdu2089]不要62
数位DP是我的噩梦. 现在初三了,却没AC过数位DP的题目. 感觉数位DP都是毒瘤-- 题目 hdu不用登录也可以进去,所以就不把题目copy到这里来了. 题目大意 求区间[n,m][n,m][n,m ...
随机推荐
- SpringBoot-01-快速入门
SpringBoot 1. 快速入门 什么是Spring 轻量级的Java开源开发框架 为了解决企业级应用开发的复杂性而创建的,简化开发 Spring是如何简化Java开发的 为了降低Java开发 ...
- vue 中后台 列表的增删改查同一解决方案
查看 & 查询 常⻅业务列表⻚都是由 搜索栏 和 数据列表 组成. 其中: 搜索栏包含 搜索条件 . 新增 . 批量xx . 导出 等对 数据列表 全局操作功能项. 数据列表包含 分⻚ 和每条 ...
- jenkins在windows系统及linux环境安装
一.下载 jenkins是当前持续集成(Continuous integration,简称 CI)的主流工具,在项目中得到了广泛的推广和应用. 下载地址: https://jenkins.io/dow ...
- 定时任务与feign超时的纠葛,该咋优化?
1 背景 业务定时器应用半夜经常会触发熔断异常的告警邮件 根据邮件提示的类找到归纳以下表格 编号 报错方法 接口所属应用 所属定时任务类 A VipTradeReportFeignService#ge ...
- Flutter音频播放--chewie_player的基本使用(二)——样式修改
先贴修改图,只改了部分布局与样式 官方的demo并不十分适合我的需求,从组件进入chewie_player并没有查看到相应的布局,那么直接从chewie的依赖包进入 可以看到以下的目录结构: 我主要修 ...
- 一道无限极类 PHP 试题
记某次笔试碰到的一道无限极类试题,当时时间比较紧(满满六页试题),还是手写代码,所以最终写的有点错误.记不住原题了,但是要求都知道,特此记录下来! 试题 有下面一个数组: $arr = [ '小红' ...
- 进程管理、PS命令、nohup命令
1. Windows 下,扩展名为exe的文件,鼠标双击,运行,把这个程序正在运行的实例,称之为进程 Windows进程的信息可以通过 任务管理器看到 查看到:正在运行的计算器程序 Calculato ...
- Spring Boot(二) :Redis 使用
Redis 介绍 Redis 是目前业界使用最广泛的内存数据存储.相比 Memcached,Redis 支持更丰富的数据结构,例如 hashes, lists, sets 等,同时支持数据持久化.除此 ...
- git的详细使用,项目创建到同步远程仓库,版本回退,忽略文件,分支创建,分支合并,分支名称修改,冲突解决,项目迁移
注意:此处省略git的安装 1..git的工作流程示意图: 2.本地仓库的初始化: 2.1 创建一个文件夹,如我创建的是:D:\gitdemo\shop 2.2 进入shop目录,鼠标右键,打开git ...
- p.array 的shape (2,)与(2,1)的分别是什么意思
numpy.ndarray.shap是返回一个数组维度的元组. (2,)与(2,1)的区别如下: ndarray.shape:数组的维度.为一个表示数组在每个维度上大小的整数元组.例如二维数组中, ...