【luogu4124】【bzoj4521】 [CQOI2016]手机号码 [数位dp]
这道题要注意卡上下界
我错了 写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 }