Link Title: http://acm.hdu.edu.cn/showproblem.php?pid=2089
Title effect: demand interval \ ([l, r] \ ) does not contain the number "4" does not include the range of consecutive numbers the number of the number of "62".
Problem-solving ideas:
Digital DP entry title.
We open a function dfs(int pos, int stat, bool limit)
, in which:
- pos indicates the position of the current number to be accessed;
- pre represents before a value is not 6, 62 involved here because even numbers (4 and a state before the individual need not be stored, STAT \ (. 1 = \) described before is a number 6, \ (= 0 \) before a survival number is not 6), so we are in a time prior to the visit to consider before one, here it is used to store a pre;
- limit represents position is not at the boundary (limit \ (. 1 = \) represents the current is within limits; \ (= 0 \) represents the current is not within limits).
Codes are as follows:
#include <bits/stdc++.h>
using namespace std;
int a[20], f[20][2];
int dfs(int pos, int stat, bool limit) {
if (pos < 0) return 1; // 说明找到了1种情况
if (!limit && f[pos][stat] != -1) return f[pos][stat];
int up = limit ? a[pos] : 9;
int tmp = 0;
for (int i = 0; i <= up; i ++) {
if (i == 4 || stat && i == 2) continue;
tmp += dfs(pos-1, i==6, limit && i==up);
}
if (!limit) f[pos][stat] = tmp;
return tmp;
}
int get_num(int x) {
int pos = 0;
while (x) {
a[pos++] = x % 10;
x /= 10;
}
return dfs(pos-1, 0, true);
}
int main() {
int l, r;
memset(f, -1, sizeof(f));
while (~scanf("%d%d", &l, &r) && l) {
printf("%d\n", get_num(r) - get_num(l-1));
}
return 0;
}