AcWing 1085. 不要62(数位DP模板题)
原题链接
中等
作者:
_empty
,
2020-08-24 21:31:14
,
所有人可见
,
阅读 639
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 12;
int f[N][10];
void init()
{
for (int i = 0; i < 10; i++)
if (i != 4) f[1][i] = 1;
for (int i = 2; i < N; i++)
for (int j = 0; j < 10; j++)
{
if (j == 4) continue;
for (int k = 0; k < 10; k++)
if (j == 6 && k == 2) continue;
else f[i][j] += f[i - 1][k];
}
}
int dp(int n)
{
if (n < 0) return 0;
if (!n) return 1;
vector<int> nums;
while (n) nums.push_back(n % 10), n /= 10;
int res = 0;
int last = 0;
for (int i = nums.size() - 1; i >= 0; i--)
{
int x = nums[i];
for (int j = 0; j < x; j++)
if (j == 4) continue;
else if (j == 2 && last == 6) continue;
else res += f[i + 1][j];
if (x == 4) break;
else if (last == 6 && x == 2) break;
else last = x;
if (!i) res++;
}
return res;
}
int main()
{
int a, b;
init();
while (cin >> a >> b)
{
if (a == 0 && b == 0) break;
cout << dp(b) - dp(a - 1) << endl;
}
return 0;
}