前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >BZOJ1026: [SCOI2009]windy数(数位dp)

BZOJ1026: [SCOI2009]windy数(数位dp)

作者头像
attack
发布2018-09-17 15:39:41
5050
发布2018-09-17 15:39:41
举报
文章被收录于专栏:数据结构与算法

题意

题目链接

Sol

很zz的数位dp

$f[i][j]$表示第$i$位,前一位是$j$的方案数

转移的时候枚举一下是否相同即可

注意当lim达到上界的时候是不能记忆化的!

代码语言:javascript
复制
/*
 
*/
#include<cstdio>
#include<map>
#include<cmath>
#include<algorithm>
#define int long long
using namespace std;
const int MAXN = 1e6 + 10;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int A, B;
int num[MAXN], tot = 0, f[21][11];
int dfs(int x, bool lim, int pre) {
    if(x == 0) return pre != -1;
    if(!lim && f[x][pre]) return f[x][pre];
    int ans = 0;
    for(int i = 0; i <= (lim ? num[x] : 9); i++) {
        if(i == 0 && pre == -1) ans += dfs(x - 1, lim && i == num[x], -1);
        else if(abs(pre - i)>= 2) ans += dfs(x - 1, lim && i == num[x], i);
    }
    if(!lim) f[x][pre] = ans;
    return ans;
}
int solve(int x) {
    tot = 0;
    while(x) num[++tot] = x % 10, x /= 10;
    return dfs(tot, 1, -1);
}
 main() {
    A = read(); B = read();
    printf("%lld", solve(B) - solve(A - 1));
    return 0;
}
/*
25 50
*/
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-09-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • Sol
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档