码迷,mamicode.com
首页 > 移动开发 > 详细

【luogu4124】【bzoj4521】 [CQOI2016]手机号码 [数位dp]

时间:2019-06-10 21:15:30      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:ems   https   org   ref   lin   tchar   ios   har   clu   

P4124 [CQOI2016]手机号码

4521

这道题要注意卡上下界

我错了 写dfs版的更好考虑状态 写纯方程转移那个细节把我想瓜了

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<stack>
 7 #include<algorithm>
 8 using namespace std;
 9 #define ll long long
10 #define rg register
11 const int N=2000000000+5,inf=0x3f3f3f3f,P=19650827;
12 ll a,b;
13 ll base[20],f[15][10][10][2][2][2];
14 ll num[20];
15 template <class t>void rd(t &x)
16 {
17     x=0;int w=0;char ch=0;
18     while(!isdigit(ch)) w|=ch==-,ch=getchar();
19     while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
20     x=w?-x:x;
21 }
22 
23 ll dfs(int pos,int pre,int prr,int h4,int h8,int h,bool dlimi,bool ulimi){
24     if(!pos) return h?1:0;
25     if(!ulimi&&!dlimi&&pre+1&&prr+1&&f[pos][pre][prr][h4][h8][h])
26     return f[pos][pre][prr][h4][h8][h];
27     int up=ulimi?num[pos]:9,dw=dlimi?1:0;
28     ll ans=0;
29     for(int i=dw;i<=up;++i){
30         if(h4&&i==8) continue;
31         if(h8&&i==4) continue;
32         ans+=dfs(pos-1,i,pre,h4||(i==4),h8||(i==8),h||(pre==prr&&prr==i),0,ulimi&&(i==num[pos]));
33     }
34     if(!ulimi&&!dlimi&&pre+1&&prr+1) f[pos][pre][prr][h4][h8][h]=ans;
35     return ans;
36 }
37 ll solve(ll x){
38     int p=0;
39     memset(num,0,sizeof(num));
40     while(x) num[++p]=x%10,x/=10;
41     return p!=11?0:dfs(p,-1,-1,0,0,0,1,1);
42 }
43 
44 int main()
45 {
46     rd(a),rd(b);
47     //memset(f,-1,sizeof(f));
48     printf("%lld",solve(b)-solve(a-1));
49     return 0;
50 }

 

【luogu4124】【bzoj4521】 [CQOI2016]手机号码 [数位dp]

标签:ems   https   org   ref   lin   tchar   ios   har   clu   

原文地址:https://www.cnblogs.com/lxyyyy/p/11000204.html

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