数位dp模板题
入门:http://blog.csdn.net/wust_zzwh/article/details/52100392
题意 left到right这个区间中有多少个合法的数(数字中含有62或4为不合法)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int a[20];
int dp[20][2];
int dfs(int pos, int pre, int sta, bool limit) {
if (pos == -1) return 1;
if (!limit && dp[pos][sta] != -1) return dp[pos][sta];
int up = limit ? a[pos] : 9;
int tmp = 0;
for (int i = 0; i <= up; i++) {
if (pre == 6 && i == 2) continue;
if (i == 4) continue;
tmp += dfs(pos - 1, i, i == 6, limit && i == a[pos]);
}
if (!limit) dp[pos][sta] = tmp;
return tmp;
}
int solve(int x) {
int pos = 0;
while (x) {
a[pos++] += x % 10;
x /= 10;
}
return dfs(pos - 1, -1, 0, true);
}
int main()
{
int left right;
while (~scanf("%d%d", &left, &right) && left + right) {
memset(dp, -1, sizeof(dp));
printf("%d\n", solve(right) - solve(left - 1));
}
}