HDOJ 2089 数位DP
链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2089
题意:
给你一个区间,问你这个区间有多少个数字不包含4 和 62
代码:
31 int a[20]; 32 int dp[20][2]; 33 34 int dfs(int pos, int pre, int sta, bool limit) { 35 if (pos == -1) return 1; 36 if (!limit && dp[pos][sta] != -1) return dp[pos][sta]; 37 int up = limit ? a[pos] : 9; 38 int res = 0; 39 rep(i, 0, up + 1) { 40 if (i == 2 && pre == 6) continue; 41 if (i == 4) continue; 42 res += dfs(pos - 1, i, i == 6, limit && i == a[pos]); 43 } 44 if (!limit) dp[pos][sta] = res; 45 return res; 46 } 47 48 int solve(int x) { 49 int pos = 0; 50 while (x) { 51 a[pos++] = x % 10; 52 x /= 10; 53 } 54 return dfs(pos - 1, -1, 0, true); 55 } 56 57 int main() { 58 ios::sync_with_stdio(false), cin.tie(0); 59 int l, r; 60 while (cin >> l >> r, l + r) { 61 memset(dp, -1, sizeof(dp)); 62 cout << solve(r) - solve(l - 1) << endl; 63 } 64 return 0; 65 }