不要62

题意

来源:HDU - 2089

区间内不包含“62”或者“4”的整数的个数

分析

数位dp的经典入门题,推荐一篇博文

里面讲的很清楚,就不再赘述了。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// ybmj
#include <bits/stdc++.h>
using namespace std;
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
typedef long long ll;

ll dp[20][2];
int num[20];

ll dfs(int pos, bool limit, bool sta) { // sta 表示前一位是否为6
if (pos == -1) return 1;
if (!limit && dp[pos][sta] != -1) return dp[pos][sta];
int up = limit ? num[pos] : 9;
ll ans = 0;
for (int i = 0; i <= up; i++) {
if (i == 4) continue;
if (i == 2 && sta == true) continue;
if (i == 6)
ans += dfs(pos - 1, limit && i == num[pos], true);
else
ans += dfs(pos - 1, limit && i == num[pos], false);
}
if (!limit) dp[pos][sta] = ans;
return ans;
}

ll solve(ll val) {
int pos = 0;
while (val) {
num[pos++] = val % 10;
val /= 10;
}
return dfs(pos - 1, true, false);
}

int main() {
/*
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
*/
std::ios::sync_with_stdio(false);
ll l, r;
clr(dp, -1);
while (cin >> l >> r) {
if (l == 0 && r == 0) break;
cout << solve(r) - solve(l - 1) << endl;
}
}
Thank you for your support!