数位dp其实就是一种用来求区间[l, r]满足条件的数的个数。数位是指:个十百千万,而在这里的dp其实相当于暴力枚举每一位数。

  我们通过把l, r的每位数分解出来,然后分别求r里满足条件的数有多少,l-1里满足条件的数有多少,然后用r的减去(l-1)的就是所求。

  数位分解:

int deal(int x) {
int pos=;
while(x) {
a[pos++]=x%;
x/=;
}
return dfs(pos-, , , );
}

  我们每一次枚举其实是有上界的,我们要控制我们枚举的这个数不能超过这个上界,此时我们就用limit来限制他。

  以HDU-2089为例,他不要4和连续的6和2,此时我们的操作如下:

int dfs(int pos, int pre, int sta, int limit) {
if (pos==-) return ;
if (!limit && dp[pos][sta]!=-) return dp[pos][sta];
int up=limit?a[pos]:;
int tmp=;
for (int i=; i<=up; i++) {
if (i==) continue;
if (pre==&&i==) continue;
tmp+=dfs(pos-, i, i==, limit&&i==a[pos]);
}
if (!limit) dp[pos][sta]=tmp;
return tmp;
}

  我们用pos来表示我们当前枚举的这个数的数位(个十百千万...), pre表示前一位数(有些地方会与上一位数有关),sta表示是否满足我们所求的条件。

    if (pos==-)  return ;

  这里是搜到最底层了,其实也不一定是直接返回-1,也是要满足我们题目所给的条件才行。

    if (!limit && dp[pos][sta]!=-)  return dp[pos][sta];

  其实这里才是比较难理解的,我们为什么在这里要返回呢?其实就是我们可能在前面已经搜索到了这个值,我们可以不再对他进行下一步的搜索,所以可以直接返回。

int up=limit?a[pos]:;
int tmp=;
for (int i=; i<=up; i++) {
if (i==) continue;
if (pre==&&i==) continue;
tmp+=dfs(pos-, i, i==, limit&&i==a[pos]);
}

  up表示我这次对这个数位能进行枚举的上届,limit表示上一位是否处于该位的最大值(如233,枚举到十位时,如果上一次枚举的是2,那么我们这次枚举的数最大只能为3,如果上一次枚举的是1,那么对这位就没有影响。limit就我而言它的作用就是限制枚举的数的上界),在不要62那道题里的限制是不能有连续的62和4,所以枚举的时候特判一下就好了。

  代码

/*  gyt
Live up to every day */
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#include <time.h>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 1e3+;
const int maxm=+;
const ll mod = 1e9+;
const int INF = 0x3f3f3f;
const db eps = 1e-;
int n, m;
int dp[maxn][];
int a[maxn]; int dfs(int pos, int pre, int sta, int limit) {
if (pos==-) return ;
if (!limit && dp[pos][sta]!=-) return dp[pos][sta];
int up=limit?a[pos]:;
int tmp=;
for (int i=; i<=up; i++) {
if (i==) continue;
if (pre==&&i==) continue;
tmp+=dfs(pos-, i, i==, limit&&i==a[pos]);
}
if (!limit) dp[pos][sta]=tmp;
return tmp;
}
int deal(int x) {
int pos=;
while(x) {
a[pos++]=x%;
x/=;
}
return dfs(pos-, -, , );
}
void solve() {
while(scanf("%d%d", &n, &m)!=EOF) {
if (!n&&!m) break;
memset(dp, -, sizeof(dp));
printf("%d\n", deal(m)-deal(n-));
}
}
int main() {
int t = ;
//freopen("in.txt", "r", stdin);
//scanf("%d", &t);
while(t--)
solve();
return ;
}

   

数位dp小结的更多相关文章

  1. 数位dp小结以及模板

    这里是网址 别人的高一啊QAQ.... 嗯一般记忆化搜索是比递推好写的所以我写的都是dfs嗯......(因为我找不到规律啊摔,还是太菜.....) 显然这个东西的条件是非常的有套路..但是不管怎么样 ...

  2. 基础数位DP小结

    HDU 3555 Bomb dp[i][0] 表示含 i 位数的方案总和. sp[i][0] 表示对于位数为len 的 num 在区间[ 10^(i-1) , num/(10^(len-i)) ] 内 ...

  3. 数位DP之小小结

    资料链接:http://wenku.baidu.com/view/9de41d51168884868662d623.html http://wenku.baidu.com/view/d2414ffe0 ...

  4. 数位DP复习小结

    转载请注明原文地址http://www.cnblogs.com/LadyLex/p/8490222.html 之前学数位dp的时候底子没打扎实 虚的要死 这次正好有时间……刷了刷之前没做的题目 感觉自 ...

  5. [poj3252]Round Numbers_数位dp

    Round Numbers poj3252 题目大意:求一段区间内Round Numbers的个数. 注释:如果一个数的二进制表示中0的个数不少于1的个数,我们就说这个数是Round Number.给 ...

  6. [bzoj1026][SCOI2009]windy数_数位dp

    windy数 bzoj-1026 题目大意:求一段区间中的windy数个数. 注释:如果一个数任意相邻两位的差的绝对值都不小于2,这个数就是windy数,没有前导0.$区间边界<=2\cdot ...

  7. 【做题】CF388D. Fox and Perfect Sets——线性基&数位dp

    原文链接https://www.cnblogs.com/cly-none/p/9711279.html 题意:求有多少个非空集合\(S \subset N\)满足,\(\forall a,b \in ...

  8. [bzoj3209]花神的数论题_数位dp

    花神的数论题 bzoj-3209 题目大意:sum(i)表示i的二进制表示中1的个数,求$\prod\limits_{i=1}^n sum(i)$ 注释:$1\le n\le 10^{15}$. 想法 ...

  9. [bzoj3530][Sdoi2014]数数_AC自动机_数位dp

    数数 bzoj-3530 Sdoi-2014 题目大意:给你一个整数集合,求所有不超过n的正整数,是的它的十进制表示下不能再一段等于集合中的任意数. 注释:$1\le n \le 1200$,$1\l ...

随机推荐

  1. SpringCloud报错:Caused by: org.yaml.snakeyaml.parser.ParserException: while parsing MappingNode

    今天在配置eureka集群时,SpringCloud报错如下: Caused by: org.yaml.snakeyaml.parser.ParserException: while parsing ...

  2. vue打包后,接口请求404的完美解决方案

    在开发环境中,和后台对接为了解决跨域问题,使用了代理,也就是vue的proxyTable,但是打包放到生产环境中去时,接口请求不到,404,原因是开发环境的代理并不能用到生产环境,但是直接在请求接口是 ...

  3. Centos 7 下 LAMP 部署

    一.介绍 LAMP is a combination of operating system and open-source software stack. The acronym of LAMP i ...

  4. Django 之多对多关系

    1. 多对多关系 作者 <--> 书籍 1. 表结构设计 1. SQL版 -- 创建作者表 create table author( id int primary key auto_inc ...

  5. 100-days:nine

    Title: Boeing(波音飞机) crash isolates FAA as(伴随,随着) China leads push against Max(出事机型,即737 Max) crash n ...

  6. ajax请求跨域

    解决方式 1: 解决方式 2: 服务端: package ceshi_utils; import java.util.*; import com.xwhb.utils.encrypt.CipherUt ...

  7. macOS 升级后重装命令行工具的问题

    问题背景 最近升级个人macbook 从 10.13 到 10.14 在终端输入 git 不能用了,发现是重装操作系统后原来的 Command Line Tools 被自动卸载了, 采用 xcode- ...

  8. 数据库中where与having区别

    having 和where 都是用来筛选用的 having 是筛选组 而where是筛选记录 他们有各自的区别 1>当分组筛选的时候 用having 2>其它情况用where------- ...

  9. TabLayout+ViewPager的简单使用

    1.   build.gradle文件中加入 compile 'com.android.support:design:22.2.0' 2.写Xml文件,注意TabLayout的三个属性 app:tab ...

  10. python 数据类型 之 利用 dict 模仿 switch语句功能

    Python本身并不提供Switch的语法功能,为了能够解决类似switch分支需求的问题,我们可以使用字典代替实现. 解决思路: 利用字典取值的get方法的容错性,处理switch语句中的defau ...