HDU - 2089 不要62 (暴力或数位DP)
Description
杭州交通管理局常常会扩充一些的士车牌照。新近出来一个好消息。以后上牌照,不再含有不吉利的数字了。这样一来。就能够消除个别的士司机和乘客的心理障碍。更安全地服务大众。
不吉利的数字为全部含有4或62的号码。
比如:
62315 73418 88914
都属于不吉利号码。
可是。61152尽管含有6和2,但不是62连号,所以不属于不吉利数字之列。
你的任务是,对于每次给出的一个牌照区间号。判断出交管局今次又要实际上给多少辆新的士车上牌照了。
Input
Output
Sample Input
1 100
0 0
Sample Output
80
思路:打表 ,数据量不大
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1000000; int n, m;
int f[maxn+5]; int check(int num) {
while (num) {
if (num % 10 == 4)
return 0;
if (num % 100 == 62)
return 0;
num /= 10;
}
return 1;
} int cal(int num) {
int ans = 0;
for (int i = 1; i <= num; i++)
if (check(i))
f[i] = f[i-1] + 1;
else f[i] = f[i-1];
return ans;
} int main() {
memset(f, 0, sizeof(f));
cal(maxn);
while (scanf("%d%d", &n, &m) != EOF && n+m) {
printf("%d\n", f[m]-f[n-1]);
}
return 0;
}
思路:数位DP,属于比較基础的一道,注意最后的flag的推断。这个是为了将数字本身算进去
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1000; /*
* dp[i][0],表示长度为i。不存在不吉利数字
* dp[i][1],表示长度为i,不存在不吉利数字。且最高位为2
* dp[i][2],表示长度为i,存在不吉利数字
*/ int n, m;
int dp[maxn][3]; void init() {
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
dp[0][1] = dp[0][2] = 0;
for (int i = 1; i < maxn; i++) {
dp[i][0] = dp[i-1][0]*9 - dp[i-1][1];
dp[i][1] = dp[i-1][0];
dp[i][2] = dp[i-1][2]*10 + dp[i-1][1] + dp[i-1][0];
}
} int cal(int num) {
int len = 0;
int tmp = num;
int bit[maxn];
while (num) {
bit[++len] = num%10;
num /= 10;
}
bit[len+1] = 0;
int ans = 0;
int flag = 0;
for (int i = len; i >= 1; i--) {
ans += dp[i-1][2]*bit[i];
if (flag)
ans += dp[i-1][0]*bit[i];
if (!flag && bit[i] > 4)
ans += dp[i-1][0];
if (!flag && bit[i+1] == 6 && bit[i] > 2)
ans += dp[i][1];
if (!flag && bit[i] > 6)
ans += dp[i-1][1];
if (bit[i] == 4 || (bit[i+1] == 6 && bit[i] == 2))
flag = 1;
}
if (flag)
ans++;
return tmp - ans;
} int main() {
init();
while (scanf("%d%d", &n, &m) != EOF && n+m) {
printf("%d\n", cal(m)-cal(n-1));
}
return 0;
}
HDU - 2089 不要62 (暴力或数位DP)的更多相关文章
- hdu 2089 不要62(入门数位dp)
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 2089 不要62(初学数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给定 m,.n; 求车牌号 m~n之间 有多少数字 不含 4或62 ,8652是可以的 . ...
- HDU 2089 不要62 | 暴力(其实是个DP)
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2089 题解: 暴力水过 #include<cstdio> #include<algor ...
- Hdu 2089 不要62 (数位dp入门题目)
题目链接: Hdu 2089 不要62 题目描述: 给一个区间 [L, R] ,问区间内不含有4和62的数字有多少个? 解题思路: 以前也做过这个题目,但是空间复杂度是n.如果数据范围太大就GG了.今 ...
- HDU 2089 不要62 (递推+暴力或者数位DP)
题意:中文题. 析:暴力先从1到1000000,然后输出就好了. 代码如下: #include <iostream> #include <cstdio> #include &l ...
- HDU 2089 - 不要62 - [数位DP][入门题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 Time Limit: 1000/1000 MS (Java/Others) Memory Li ...
- HDU 2089 不要62(数位dp模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求区间内不包含4和连续62的数的个数. 思路: 简单的数位dp模板题.给大家推荐一个好的讲解博客.h ...
- HDU 2089(暴力和数位dp)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2089 不要62 Time Limit: 1000/1000 MS (Java/Others) M ...
- [hdu 2089] 不要62 数位dp|dfs 入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求[n, m]区间内不含4和62的数字个数. 这题有两种思路,直接数位dp和dfs 数位d ...
随机推荐
- MySQL PLSQL Demo - 001.创建、调用、删除过程
drop procedure if exists p_hello_world; create procedure p_hello_world() begin select sysdate(); end ...
- vue实现复制粘贴的两种形式
方式一: 1.安装clipboard:npm install clipboard 2.src/utils/clipboard.js import Vue from 'vue' import Clipb ...
- Ajax实现异步刷新验证用户名是否已存在
由于要做一个注册页面,看到许多网站上都是使用Ajax异步刷新验证用户名是否可用的,所以自己也动手做一个小实例 都是简单的实例,所以直接发代码 静态页面Ajax.html <html> &l ...
- 【转】Mysql两种存储引擎的异同【MyISAM和InnoDB】
MySQL默认采用的是MyISAM. MyISAM不支持事务,而InnoDB支持.InnoDB的AUTOCOMMIT默认是打开的,即每条SQL语句会默认被封装成一个事务,自动提交,这样会影响速度,所以 ...
- java的regex问题笔记
参考javadoc java.util.regex.Pattern 里面有一些说明,如果还有不明白的地方 yes,google it. @ “不能以0开头,1到多位数字,字符集为0到9” " ...
- LeetCode: Permutations 解题报告
Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...
- java 多线程阻塞队列 与 阻塞方法与和非阻塞方法
Queue是什么 队列,是一种数据结构.除了优先级队列和LIFO队列外,队列都是以FIFO(先进先出)的方式对各个元素进行排序的.无论使用哪种排序方式,队列的头都是调用remove()或poll()移 ...
- bootstrap内容太多表格撑破
增加样式 style="word-break:break-all; word-wrap:break-all;" 这样内容就会自动换行,表格就美观多了. <table cla ...
- 实战c++中的vector系列--构造、operator=和assign差别
vector或许是实际过程中使用最多的stl容器.看似简单,事实上有非常多技巧和陷阱. 着重看一看vector的构造,临时依照C++11: default (1) explicit vector (c ...
- 【C#】List列表的深复制,引用类型深复制
需求:深复制该列表. Student实体类: public class Student { public string Name { get; set; } public int Age { get; ...