Bomb HDU - 3555【数位dp】
Bomb HDU - 3555
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.
题目大意:
已知所有含有49的数字是不合法的,问nnn以内有多少不合法的数字。
解题思路:
假设dp[i][j]dp[i][j]dp[i][j]代表第iii以下所有合法的数字的数目,jjj代表当前位是否是4.
因此,我们可以先将数字按位拆分放在数组中,从最高位开始,我们可以知道,如果当前位不为4,那么此时的dp[i][0]dp[i][0]dp[i][0]就会等于其下一位的所有情况的累加,而这种累加我们将其存在dp[i−1][0]dp[i-1][0]dp[i−1][0]与dp[i−1][1]dp[i-1][1]dp[i−1][1]中,只要算过一次,当下一次遇到时就可直接调用,大大降低了重复计算次数,当当前为为4时,将其存入dp[i][1]dp[i][1]dp[i][1]中,遍历下一位时仅需跳过9即可,其他情况相同。
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
ll dp[25][2];
ll num[25];
int cnt;
ll dfs(int wei,bool limit,bool is4) {if(wei==0) return 1;if(!limit&&dp[wei][is4]) return dp[wei][is4];int maxnum;if(limit) maxnum=num[wei];else maxnum=9;ll nape=0;for(int i=0;i<=maxnum;i++) {if(is4&&i==9) continue;nape+=dfs(wei-1,limit&&i==maxnum,i==4);}if(!limit) dp[wei][is4]=nape;return nape;
}
int main()
{#ifndef ONLINE_JUDGE//freopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);//ios::sync_with_stdio(0),cin.tie(0);int T;scanf("%d",&T);while(T--) {ll n,n1;cnt=0;scanf("%lld",&n);n1=n;while(n) {num[++cnt]=n%10;n=n/10;}printf("%lld\n",n1+1-dfs(cnt,true,false));}return 0;
}
Bomb HDU - 3555【数位dp】相关推荐
- hdu 2089 数位dp入门
HDU 2089 题意:中文题 思路:数位dp入门题 AC代码: #include "iostream" #include "string.h" #includ ...
- HDU 4352 数位dp + LIS(nlogn) + 状态压缩
#define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefully reading the entir ...
- hdu 6148 数位dp
题意:中文题,求小于等于n的"山谷"数的个数 思路:数位dp,注意此题中状态转移与前导0有关即可. 代码: #include<bits/stdc++.h> using ...
- HDU 3555 Bomb(数位DP模板啊两种形式)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 Problem Description The counter-terrorists found ...
- HDU 3555 Bomb (数位DP)
数位dp,主要用来解决统计满足某类特殊关系或有某些特点的区间内的数的个数,它是按位来进行计数统计的,可以保存子状态,速度较快.数位dp做多了后,套路基本上都差不多,关键把要保存的状态给抽象出来,保存下 ...
- HDU - 3555 Bomb(数位dp)
题目链接:点击查看 题目大意:给定一个整数n,求从1到n的闭区间内含有相邻"49"的数字的个数. 题目分析:裸的数位dp,这里说一下两种做法,第一种是正着求,也就是求含有49的数字 ...
- HDU 3555 Bomb (数位DP-记忆化搜索模板)
题意 求区间[1,n]内含有相邻49的数. 思路 比较简单的按位DP思路.这是第一次学习记忆化搜索式的数位DP,确实比递推形式的更好理解呐,而且也更通用~可以一般化: [数位DP模板总结] int d ...
- HDU 6156 Palindrome Function 数位DP
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6156 题目描述: 求L~R所有的数的l~r进制的f(x), f(x) = 当前进制 如果回文串, f ...
- Hdu 2089-不要62 数位dp
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2089 不要62 Time Limit: 1000/1000 MS (Java/Others) Me ...
最新文章
- java enter_Java UI.enter方法代码示例
- 神器 jupyter,终于来了!
- 跟前腾讯总监学Java实战项目
- Ant编译、FatJar编译方式
- dgi数据治理_荐书 | 5G时代组织急需数据体检
- 北大校友的算法被玩出新高度,AI自动化在线抠图
- AppCompatActivity中使用SearchView
- c++ enum 给定类型_C++ 枚举类型详解
- hdu 1588 Gauss Fibonacci
- Session 的生命周期
- 01-C语言之父:丹尼斯·里奇
- github上看到的springboot做的后台管理系统,bootdo,适合大家学习入门
- postgresql 手动启动_PostGreSql 手动安装
- vscode调试html页面status200无法访问此网站 localhost 拒绝了我们的连接请求
- s905各种型号的区别_工具柜规格型号
- 商标注册的费用是多少钱
- 力扣(226.112)补9.8
- mysql sga_MySQL王者晋级之路(一)安装、体系结构和存储引擎
- Python-自动下载抖音无水印高清视频
- 验证运放输入端之间电容作用
热门文章
- Python next 函数 - Python零基础入门教程
- jq之$(“tr:even“).css(“background-color“,“yellow“)
- java 项目 人力资源项目,基于jsp的人力资源系统-JavaEE实现人力资源系统 - java项目源码...
- Python工程笔记(1):轨迹与日志
- j2me安装_Java第一步 JDK安装
- mysql 查看运行级别_运行级别及进程
- 天津理工计算机通信工程学院,2018年天津理工大学计算机与通信工程学院811信号与系统考研仿真模拟五套题...
- mysql 5.7 数据库备份_MySQL5.7.20数据库备份与恢复
- android 双层饼图_python:给表格加上双层饼图,让同事的图表黯然失色
- linux 7 定时任务设置,centos7 配置crontab定时任务