hdu 2089 不要62 数位DP

思路:

dp[i][0]:位数<=i且不含不吉利数字的个数;

dp[i][1]:位数<=i且含6的个数。

代码如下:

 1 #include<cstdio>
 2 #include<cstring>
 3 int bit[6],dp[6][3];
 4 int dfs(int pos,int h,bool f)
 5 {
 6     if(pos==-1) return 1;
 7     if(!f&&dp[pos][h]!=-1) return dp[pos][h];
 8     int ans=0;
 9     int e=f?bit[pos]:9;
10     for(int i=0;i<=e;i++){
11         if(i==4||(h==1&&i==2)) continue;
12         ans+=dfs(pos-1,i==6,f&&(i==bit[pos]));
13     }
14     if(!f) dp[pos][h]=ans;
15     return ans;
16 }
17 int cal(int n)
18 {
19     int m=0;
20     while(n){
21         bit[m++]=n%10;
22         n/=10;
23     }
24     return dfs(m-1,0,1);
25 }
26 int main()
27 {
28     int n,m;
29     memset(dp,-1,sizeof(dp));
30     while(scanf("%d%d",&n,&m)&&(n+m)){
31         printf("%d\n",cal(m)-cal(n-1));
32     }
33 }
View Code

 

 

 

posted @ 2013-09-05 18:58  _随心所欲_  阅读(186)  评论(0编辑  收藏  举报