HDU 2089 —— 不要62

HDU 2089 —— 不要62

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

题意:给你一对整数n,m,求n~m有多少个数字不含4或62

数位dp入门题,dp状态为dp[当前第几位][上一位是否是6]

搜到底部,如果能搜到最后一位答案+1,因为不能把4和62算进去,所以遍历的时候当存在4或62 continue;

利用前缀和的思想累计答案,答案就是solve(m)-solve(n-1)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 int a[1000009];
 5 int dp[20][2];
 6 int dfs(int pos,int num,bool limit)
 7 {
 8     if(!limit&&dp[pos][num]!=-1) return dp[pos][num];///记忆化搜索
 9     if(pos==0) return 1;///枚举到头了
10     int up=limit?a[pos]:9;///判断枚举的最高位
11     int res=0;
12     for(int i=0;i<=up;i++)
13     {
14         if(i==4) continue;
15         if(i==2&&num) continue;
16         res+=dfs(pos-1,i==6,limit&&i==a[pos]);
17     }
18     if(!limit) dp[pos][num]=res;
19     return res;///累计答案
20 }
21 int solve(int x)
22 {
23     int pos=0;
24     while(x)///拆分位数
25     {
26         a[++pos]=x%10;
27         x/=10;
28     }
29     return dfs(pos,0,1);
30 }
31 int main()
32 {
33     memset(dp,-1,sizeof(dp));
34     int a,b;
35     while(scanf("%d%d",&a,&b))
36     {
37         if(a==0&&b==0) break;
38         printf("%d\n",solve(b)-solve(a-1));
39     }
40     return 0;
41 }

猜你喜欢

转载自www.cnblogs.com/YangKun-/p/12633026.html