当前位置: 移动技术网 > IT编程>开发语言>C/C++ > BZOJ1026: [SCOI2009]windy数(数位dp)

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

2018年09月10日  | 移动技术网IT编程  | 我要评论

郭嘉新传txt下载,正雄饮水机,赛尔号奥利贡

题意

题目链接

 

sol

很zz的数位dp

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

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

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

/*
 
*/
#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
*/

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网