BZOJ 1026: [SCOI2009]windy数

二次联通门 : BZOJ 1026: [SCOI2009]windy数

 

 

 

 

/*
    水题开心
*/
#include <cstdio>
#include <iostream>
#include <cstring>
#define rg register

int f[20][20], c[20], cur;
inline int abs (int x) { 
    return x < 0 ? -x : x;
}
int dfs (int n, int l, bool led, bool lim) { 
    if (n == 0) return 1;
    if (!lim && !led && f[n][l] >= 0) return f[n][l];
    int up = lim ? c[n] : 9, res = 0;
    for (int i = 0; i <= up; ++ i) { 
        if (!led && abs (i - l) < 2 && n != cur) continue;
        if (led && i == 0) res += dfs (n - 1, i, true, lim && i == up);
        else res += dfs (n - 1, i, led && i == 0, lim && i == up);
    }    
    return (!lim && !led) ? f[n][l] = res : res;
}

int sv (int key) {
    for (cur = 0; key; c[++ cur] = key % 10, key /= 10);
    return dfs (cur, 0, true, true);
}
int main (int argc, char *argv[]) { 
    int A, B; scanf ("%d%d", &A, &B);
    memset (f, -1, sizeof f);
    printf ("%d", sv (B) - sv (A - 1));
    return 0;
}

 

posted @ 2017-12-27 21:43  ZlycerQan  阅读(...)  评论(... 编辑 收藏