码迷,mamicode.com
首页 > 其他好文 > 详细

【数位DP】HDU 2089 不要62

时间:2017-08-18 23:43:24      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:continue   lan   hid   color   blank   clu   lap   targe   print   

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

【AC】

技术分享
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn=1e6+2;
 5 int dp[10][10];
 6 int digit[10];
 7 int cnt;
 8 int ans;
 9 int n,m;
10 bool check()
11 {
12     for(int i=0;i<cnt;i++)
13     {
14         if(digit[i]==4) return false;
15         if(digit[i]==2&&digit[i+1]==6) return false;
16     }
17     return true;
18     
19 }
20 void init()
21 {
22     memset(dp,0,sizeof(dp));
23     dp[0][0]=1;
24     for(int i=1;i<=7;i++)
25     {
26         for(int j=0;j<=9;j++)
27         {
28             if(j==4) continue;
29             for(int k=0;k<=9;k++)
30             {
31                 if(j==6&&k==2) continue;
32                 dp[i][j]+=dp[i-1][k];
33             }
34         }
35     } 
36 }
37 bool judge(int pos)
38 {
39     for(int i=cnt;i>=pos-1;i--)
40     {
41         if(digit[i]==4) return false;
42         if(i-1>=pos-1&&digit[i]==6&&digit[i-1]==2) return false; 
43     } 
44     return true;
45 }
46 void calc(int x)
47 {
48     cnt=0;
49     while(x)
50     {
51         digit[cnt++]=x%10;
52         x/=10;
53     }
54     digit[cnt]=0;
55 }
56 void solve(int pos)
57 {
58     if(pos==0) return;
59     int t=digit[pos-1];
60     for(int i=0;i<t;i++)
61     {
62         if(i==4) continue;
63         if(i==2&&digit[pos]==6) continue;
64         ans+=dp[pos][i];
65      } 
66     if(judge(pos))
67     {
68         solve(pos-1);
69     }
70     return;
71 }
72 
73 int query(int x)
74 {
75     calc(x);
76     ans=0;
77     solve(cnt);
78     if(check())
79     {
80         ans++;
81     }
82     return ans;
83 }
84 int main()
85 {
86     init();
87     while(~scanf("%d%d",&n,&m))
88     {
89         if(n+m==0) break;
90         int res=query(m)-query(n-1);
91         printf("%d\n",res);
92     }
93     return 0;
94 }
View Code

 

【数位DP】HDU 2089 不要62

标签:continue   lan   hid   color   blank   clu   lap   targe   print   

原文地址:http://www.cnblogs.com/itcsl/p/7392503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!