MYF

HDU 2089 不要62

题目链接

HDU 2089

题目类型:数位DP

题目分析

题目大意

统计区间[l, r]内,数字中不含462的数字个数

解析

参考HDU 3555的题解

代码

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
55
56
57
58
59
60
61
62
63
64
65
66
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <iomanip>
#include <bitset>
#include <cstring>
#include <iostream>
#include <iosfwd>
#include <deque>
#include <algorithm>
#define Memset(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)
#define PB push_back
#define MP make_pair
#define rt(n) (i == n ? '\n' : ' ')
#define hi printf("Hi----------\n")
#define debug(x) cout<<"Debug : ---"<<x<<"---"<<endl;
#define debug2(x,y) cout<<"Debug : ---"<<x<<" , "<<y<<"---"<<endl;
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
const int maxn=500+5;
const int mod=1000000007;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
ll dp[25][10][2];
ll num[25];
ll dfs(int pos,int pre,int status, bool limit){
if (pos < 1) {
return status;
}
if (!limit&&dp[pos][pre][status]!=-1) {
return dp[pos][pre][status];
}
int mx = limit ? num[pos] : 9;
ll ret = 0;
for (int i=0; i<=mx; i++) {
ret += dfs(pos - 1, i, status||(i==4)||(pre==6&&i==2), limit&&i==mx);
}
if (!limit) {
dp[pos][pre][status] = ret;
}
return ret;
}
ll solve(ll n){
Memset(dp, -1);
int pos = 0;
ll tmp = n;
while (tmp) {
num[++pos] = tmp % 10;
tmp /= 10;
}
return n - dfs(pos, 0, 0, 1);
}
int main(){
ll n,m;
while (scanf("%lld%lld",&n,&m),n+m) {
cout<<solve(m) - solve(n-1)<<endl;
}
}