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

题意

题目链接

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

 

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

 

上一篇:[SCOI2009]迷路


下一篇:2021.10.08pm