hdu 2089 不要62 数位DP入门

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2089

题意:

统计区间 [a,b] 中不含 4 和 62 的数字有多少个。

思路:

学习了数位dp:http://blog.csdn.net/wust_zzwh/article/details/52100392

代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 #define MS(a) memset(a,0,sizeof(a))
 5 #define MP make_pair
 6 #define PB push_back
 7 const int INF = 0x3f3f3f3f;
 8 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
 9 inline ll read(){
10     ll x=0,f=1;char ch=getchar();
11     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
12     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
13     return x*f;
14 }
15 //////////////////////////////////////////////////////////////////////////
16 const int maxn = 1e5+10;
17 
18 int l,r,dp[10][2],a[10];
19 
20 int dfs(int pos,int pre,int sta,int limit){
21     if(pos == -1) return 1;
22     if(!limit && dp[pos][sta]!=-1) return dp[pos][sta];
23     int up = limit?a[pos]:9;
24     int re = 0;
25     for(int i=0; i<=up; i++){
26         if(i == 4) continue;
27         if(pre==6 && i==2) continue;
28         re += dfs(pos-1,i,i==6,limit&&i==a[pos]);
29     }
30     if(!limit) dp[pos][sta] = re;
31     return re;
32 }
33 
34 int solve(int x){
35     int pos = 0;
36     while(x){
37         a[pos++] = x%10;
38         x /= 10;
39     }
40     int re = dfs(pos-1,-1,0,true);
41     return re;
42 }
43 
44 int main(){
45     memset(dp,-1,sizeof(dp));
46     while(cin>>l>>r && (l+r)){
47         int ans = solve(r)-solve(l-1);
48         cout << ans << endl;
49     }
50 
51     return 0;
52 }

 

原文地址:https://www.cnblogs.com/yxg123123/p/6840407.html