int f[20][10], bits[20]; int dfs(int pos, int pre, bool lmt, bool first) { if (pos == -1) return 1; if (!lmt && ~f[pos][pre]) return f[pos][pre]; int u = lmt ? bits[pos] : 9, ret = 0; for (int i = 0; i <= u; i++) { if (first || (!first && abs(pre - i) >= 2)) ret += dfs(pos - 1, i, lmt && i == u, first && !i); } return lmt || first ? ret : f[pos][pre] = ret; } int calc(int n) { CLR(f, -1); int len = 0; while (n) { bits[len++] = n % 10; n /= 10; } return dfs(len - 1, 0, true, true); } int main() { //freopen("0.txt", "r", stdin); int a, b; while (~RII(a, b)) { cout << calc(b) - calc(a - 1) << endl; } return 0; }
原文:http://blog.csdn.net/wty__/article/details/38023079