Bomb(数位DP板题)
题目链接:Bomb
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.
题意是问我们从1~N中有多少个包含了“49”的数。
做法,数位DP板子题
dp[i][j][k]表示在第i位的位置上,j(0,1)表示前面有没有出现49,k(0,1)表示前一位是不是4;
#include<algorithm>
#include<string.h>
#include<iostream>
#include<stdio.h>
#include<string>
#include<math.h>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define emplace_back push_back
#define pb push_back
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
const double eps = 1e-6;
const LL inf = 0x3f3f3f3f;
const LL N = 2e5 + 10;
LL dp[50][2][2];
LL a[50];
LL dfs(LL pos,LL sta,LL limit,LL pre)
{if(pos<0){return sta;}if(!limit&&dp[pos][sta][pre]!=-1){return dp[pos][sta][pre];}LL up = limit ? a[pos] : 9;LL res = 0;for (LL i = 0; i <= up;i++){if(i==9&&pre){res += dfs(pos - 1,sta|1, limit && a[pos] == i, i==4);}else{res+=dfs(pos - 1,sta, limit && a[pos] == i, i==4);}}if(!limit)dp[pos][sta][pre] = res;return res;
}
LL solve(LL x)
{LL tot = 0;while(x){a[tot++] = x % 10;x /= 10;}return dfs(tot-1,0,1,0);
}
int main()
{LL t;scanf("%lld", &t);while(t--){LL n;scanf("%lld", &n);memset(dp, -1, sizeof(dp));printf("%lld\n", solve(n));}return 0;
}
Bomb(数位DP板题)相关推荐
- 数位dp入门题 洛谷P2657 [SCOI2009] windy 数
题干 传送门 windy 定义了一种 windy 数. 题目描述 不含前导零且相邻两个数字之差至少为 2的正整数被称为 windy 数.windy 想知道,在 a 和 b 之间,包括 a 和 b ,总 ...
- 不要62(HDU-2089)(数位DP模板题)
杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍 ...
- 【hdu3555】Bomb 数位dp
题目描述 求 1~N 内包含数位串 "49" 的数的个数. 输入 The first line of input consists of an integer T (1 <= ...
- HDU - 3555 Bomb(数位dp)
题目链接:点击查看 题目大意:给定一个整数n,求从1到n的闭区间内含有相邻"49"的数字的个数. 题目分析:裸的数位dp,这里说一下两种做法,第一种是正着求,也就是求含有49的数字 ...
- HDU 3555 Bomb (数位DP)
数位dp,主要用来解决统计满足某类特殊关系或有某些特点的区间内的数的个数,它是按位来进行计数统计的,可以保存子状态,速度较快.数位dp做多了后,套路基本上都差不多,关键把要保存的状态给抽象出来,保存下 ...
- hdu3555 Bomb 数位dp
//3555 //题意:统计 1 到 n 中包含 49 的数的个数 1 #include "bits\stdc++.h" 2 using namespace std; 3 __in ...
- uestc 250 数位dp(水)
/* 数位dp 水题 开两维一个记录长度,一个记录上一个数 */ #include<stdio.h> #include<string.h> #define N 13 int d ...
- bzoj 1026: [SCOI2009]windy数 数位DP算法笔记
数位DP入门题之一 也是我所做的第一道数位DP题目 (其实很久以前就遇到过 感觉实现太难没写) 数位DP题目貌似多半是问从L到R内有多少个数满足某些限制条件 只要出题人不刻意去卡多一个$log$什么的 ...
- bzoj1833: [ZJOI2010]count 数字计数(数位DP+记忆化搜索)
1833: [ZJOI2010]count 数字计数 题目:传送门 题解: 今天是躲不开各种恶心DP了??? %爆靖大佬啊!!! 据说是数位DP裸题...emmm学吧学吧 感觉记忆化搜索特别强: 定义 ...
最新文章
- 使用node来搭建简单的后台业务
- decode encode
- pexpect oracle,expect免交互脚本编程
- JS:两个json数组合并、去重,以及删除某一项元素
- [原+转]CSS hack 小技巧 让你的CSS 兼容ff ie6.0 ie7.0
- 程序显示文本框_【教程】TestComplete测试桌面应用程序教程(二)
- 如何计算java对象占用的内存
- nodejs笔记五--MongoDB基本环境配置及增删改查;
- jdk环境变量的配置与练习
- 【大数据分析】Spark SQL查询:使用SQL命令
- 麦肯锡极简工作法-读书笔记
- 关于源级串联电感提高稳定性的理由
- Spring Boot 2.1.6.RELEASE embed tomcat启动过程
- 《云计算与虚拟化分析》_2章:虚拟化技术
- PaddleRec与Milvus深度结合,手把手带你体验工业级推荐系统召回速度
- Unity-VolumeLighting组件(体积雾/光效果)使用小记
- 红帽认证工程师常见问题解答(转)
- cs1.5服务器指定ip,CS1.5 IP 服务器
- python xmind转excel 知乎_Xmind 8 Pro v3.7.7 build – 最好用的思维导图工具之
- python系统设计与实现_毕业设计5:基于MicroPython的智能火灾报警器系统的设计与实现...
热门文章
- Eclipse基础知识
- 产品卖不出去?那你该学学向上销售和交叉销售了
- 几何光学学习笔记(3)- 2.1光线经过单个折射球面的折射
- 微信公众平台开发——引言
- Spring boot 项目Kafka Error connecting to node xxx:xxx Kafka项目启动异常 Failed to construct kafka consumer
- web端与移动端高德地图接入及实例化
- Ubuntu18修改系统语言为英文
- excel解析html代码,使用excel vba解析HTML的错误
- android 页面默认不弹软键盘_Android避免进入页面自动弹出软键盘(真正好用)
- Linux搭建samba服务及使用案例