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 ...
随机推荐
- 典藏版Web功能测试用例库
界面显示 初始界面元素:title.内容,默认值.必填项(红*) 样式美观 排版规范 字体统一 编辑页面有光标,定位在第一个可编辑文本框 内容过多时,滚动条 loading ...
- asp.net core 从 3.1 到 5.0
asp.net core 从 3.1 到 5.0 Intro 就在前几天,微软宣布了 .NET5 发布了 RC1 版本,这也意味着 .NET5 的开发基本稳定了,正式发布之前,不会再新增新的 Feat ...
- [Spring Cloud实战 | 第六篇:Spring Cloud Gateway+Spring Security OAuth2+JWT实现微服务统一认证授权
一. 前言 本篇实战案例基于 youlai-mall 项目.项目使用的是当前主流和最新版本的技术和解决方案,自己不会太多华丽的言辞去描述,只希望能勾起大家对编程的一点喜欢.所以有兴趣的朋友可以进入 g ...
- Python3基础数据类型(数字、字符串、元组、列表、集合、字典)
笔记参考了菜鸟教程 Python 中的变量不需要声明,赋值才创建.赋值给变量的是什么类型变量就是什么类型 多个变量赋值 1 a, b, c = 1, 2, "runoob" 标准数 ...
- 测试工具-XPath使用
XML有两种MIME类型,即application/xml和text/xml,在HTTP中,MIME Type类型被定义在Content-Type header中.我们经常也会看到接口返回数据类型为X ...
- ubuntu下一键安装pptpd
1 #!/bin/bash 2 # Quick and dirty pptp VPN install script 3 # Ubuntu 12+ or Debain 7+ 4 # Reference ...
- 【小白学PyTorch】15 TF2实现一个简单的服装分类任务
[新闻]:机器学习炼丹术的粉丝的人工智能交流群已经建立,目前有目标检测.医学图像.时间序列等多个目标为技术学习的分群和水群唠嗑的总群,欢迎大家加炼丹兄为好友,加入炼丹协会.微信:cyx64501661 ...
- 一加云耳2和一加云耳z区别
[解码方式]:云耳Z采用的音源解码方式是AAC,云耳2使用的是APTX: [发声单元]:云耳Z采用的是动圈,云耳2采用的是动圈+动铁组合 [颜色]:云耳Z有4款颜色(宝蓝.薄荷绿.黑色.米白)云耳2有 ...
- STM32与CH455g通信测试(仅键盘)
1.概述 CH455是数码管显示驱动和键盘扫描控制芯片.CH455内置时钟振荡电路,可以动态驱动4位数码管或者32只LED:同时还可以进行28键的键盘扫描:CH455通过SCL和SDA组成的2线串行接 ...
- 099 01 Android 零基础入门 02 Java面向对象 03 综合案例(学生信息管理) 02 案例分析及实现 03 编写并测试Student类
099 01 Android 零基础入门 02 Java面向对象 03 综合案例(学生信息管理) 02 案例分析及实现 03 编写并测试Student类 本文知识点:编写并测试Subject类 说明: ...