HDU 2089 不要62 (递推+暴力或者数位DP)
题意:中文题。
析:暴力先从1到1000000,然后输出就好了。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <queue> using namespace std;
const int maxn = 1000000 + 10;
int a[10], d[maxn]; bool judge(int n){
int j = 0;
while(n){
a[j] = n % 10;
n /= 10;
if(4 == a[j++]) return false;
}
for(int i = 0; i < j; ++i)
if(4 == a[i]) return false;
else if(i && 2 == a[i-1] && 6 == a[i]) return false; return true;
} int main(){
int n, m;
for(int i = 1; i < 1000000; ++i)
d[i] = judge(i) ? d[i-1] + 1 : d[i-1];
while(scanf("%d %d", &m, &n)){
if(!m && !n) break;
printf("%d\n", d[n]-d[m-1]);
}
return 0;
}
也可以用数位DP来做
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
#include <map>
#include <cctype>
#include <cmath> using namespace std;
typedef long long LL;
const int maxn = 30;
int d[10][3], a[25];
//d[i][j] 表示数字的位数,j代表状态
//d[i][0] 表示不存在不吉利的数字
//d[i][1] 表示不存在吉利数字,且最高位是2
//d[i][2] 表示存在不吉利数字
void init(){
memset(d, 0, sizeof(d));
d[0][0] = 1;
for(int i = 1; i < 8; ++i){
d[i][0] = d[i-1][0] * 9 - d[i-1][1];//最高位不含有4的9个数字,而且因为当放第i位放6的时候,
//i-1不能放2所以要减去一下i-1位不存在不吉利数字切最高位为2 的情况
d[i][1] = d[i-1][0];//最高位放了2其他位只要不是不吉利数字就可以
d[i][2] = d[i-1][2] * 10 + d[i-1][1] + d[i-1][0];//i位的含有不吉利数字的个数是i-1位的含有不吉利数字的×10,
//因为只要后面含有不吉利数字前面不管是什么都可以所以前面×10,另外如果最高位放一个4然后后面的全部不是不吉利数字就行,
//还可以是最高位放上一个6然后,倒数第二位放上2就可以的不是不吉利就行
}
} int solve(int n){
int t = n;
int len = 0;
while(n){
a[++len] = n % 10;
n /= 10;
}
a[len+1] = 0;
int ans = 0;
bool ok = false;
for(int i = len; i > 0; --i){
ans += d[i-1][2] * a[i];//后面的i-1是不吉利数字之后前面就可以填上任意的
if(ok) ans += d[i-1][0] * a[i];
if(!ok && a[i] > 4) ans += d[i-1][0];//如果a[i]>4那么就可以在i位上放上一个4
if(!ok && a[i+1] == 6 && a[i] > 2) ans += d[i][1];//后一位为6,此位大于2
if(!ok && a[i] > 6) ans += d[i-1][1];//如果a[i]>6那么就可以在i位上放上一个2
if((a[i] == 2 && a[i+1] == 6) || a[i] == 4) ok = true;//标记为不吉利
}
return t - ans;
} int main(){
init();
int n, m;
while(~scanf("%d %d", &n, &m)){
if(!m && !n) break;
printf("%d\n", solve(m+1) - solve(n));
//因为solve函数中并没有考虑n是不是不幸数的情况,所以r+1只算了1~r,而l只算了1~l-1,这两者相减才是正确答案
}
return 0;
}
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int dp[10][2], a[10]; int dfs(int pos, bool is, bool ok){
if(!pos) return 1;
int &ans = dp[pos][is];
if(!ok && ans >= 0) return ans;
int res = 0, n = ok ? a[pos] : 9;
for(int i = 0; i <= n; ++i){
if(4 == i || is && 2 == i) continue;
res += dfs(pos-1, i == 6, ok && i == n);
}
if(!ok) ans = res;
return res;
} int solve(int n){
int len = 0;
while(n){
a[++len] = n % 10;
n /= 10;
}
return dfs(len, false, true);
} int main(){
memset(dp, -1, sizeof dp);
while(scanf("%d %d", &m, &n) == 2 && (m+n)){
printf("%d\n", solve(n) - solve(m-1));
}
return 0;
}
HDU 2089 不要62 (递推+暴力或者数位DP)的更多相关文章
- HDU 2085 核反应堆 --- 简单递推
HDU 2085 核反应堆 /* HDU 2085 核反应堆 --- 简单递推 */ #include <cstdio> ; long long a[N], b[N]; //a表示高能质点 ...
- HDU 5860 Death Sequence(递推)
HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...
- Hdu 2089 不要62 (数位dp入门题目)
题目链接: Hdu 2089 不要62 题目描述: 给一个区间 [L, R] ,问区间内不含有4和62的数字有多少个? 解题思路: 以前也做过这个题目,但是空间复杂度是n.如果数据范围太大就GG了.今 ...
- HDU - 2089 不要62 (暴力或数位DP)
Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局常常会扩充一些的士车牌照.新近出来一个好消息.以后上牌照,不再含有不吉利的数字了.这样一来.就能够消除个别 ...
- 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 ...
- HDU 2089 不要62 数位DP模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 参考博客:https://www.cnblogs.com/HDUjackyan/p/914215 ...
随机推荐
- UI5-文档-4.24-Filtering
在此步骤中,我们为产品列表添加一个搜索字段,并定义一个表示搜索项的过滤器.搜索时,列表会自动更新,只显示与搜索项匹配的项. Preview A search field is displayed ab ...
- Oracle+Mybatis批量插入,更新和删除
1.插入 (1)第一种方式:利用<foreach>标签,将入参的list集合通过UNION ALL生成虚拟数据,从而实现批量插入(验证过) <insert id="inse ...
- VBox 安装 macOS 10.12
安装步骤⑴ 下载及解压 macOS 10.12 Sierra Final by TechReviews.rar ⑵ 下载及双击安装 VirtualBox-5.1.6-110634-Win.exe ,默 ...
- 【OpenPose-Windows】OpenPose+VS2015+Windows+CUDA8+cuDNN5.1 官方配置教程(转载)
[我的电脑配置] 操作系统:Windows 10 CUDA版本:cuda_8.0.61_win10 cuDNN版本:cudnn-8.0-windows10-x64-v5.1 GPU model:Nvi ...
- ssh结合tar命令把远程文件拉回来或推过去(实现数据无落地推送)
登录22后tar 压缩/var/log目录输出到标准输入通过管道传到本地22_log.tar.gz文件 ssh 192.168.0.22 "cd /var ;tar -zcvf - log& ...
- Null value was assigned to a property of primitive type setter of"原因及解决方法
在action请求数据的过程中报出"Null value was assigned to a property of primitive type setter of"错误,搜索之 ...
- 第五章 二叉树(e5)重构
- Infinity,NaN
常量 说明 Infinity 表示正无穷大的特殊值. -Infinity 表示负无穷大的特殊值. NaN Number 数据类型的一个特殊成员,用来表示“非数字”(NaN) 值. undefined ...
- 图片添加热点MAP之后连接无效的解决方法
好些接触网店的同事都会遇到这个问题:就是明明给图片添加了热点超链接,但是点击图片就是没反应. 其实这个问题就是热点冲突,也就是说这个页面中至少有2个名称相同的热点导致热点冲突无法正确加载. 谷歌浏览器 ...
- Spring_AOP动态代理详解(转)
在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们不但要知道怎么通过AOP来满足的 ...