AcWing 1085. 不要62
原题链接
中等
作者:
wjie
,
2020-07-16 16:27:19
,
所有人可见
,
阅读 525
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int N = 11;
int f[N][10];
void init()
{
for (int i = 0; i <= 9; ++i) f[1][i] = 1;
f[1][4] = 0;
for (int i = 2; i < N; ++i)
{
for (int j = 0; j <= 9; ++j)
{
if (j == 4) continue;
for (int k = 0; k <= 9; ++k)
{
if (k == 4 || (j == 6 && k == 2)) continue;
f[i][j] += f[i-1][k];
}
}
}
}
int dp(int n)
{
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 (last == 6 && j == 2) continue;
res += f[i+1][j];
}
if ((x == 4) || (last == 6 && x == 2)) break;
last = x;
if (!i) res++;
}
return res;
}
int main()
{
int l, r;
init();
while (scanf("%d%d", &l, &r), l+r)
{
printf("%d\n", dp(r) - dp(l-1));
}
return 0;
}