[CQOI2016]手机号码
[CQOI2016]手机号码
BZOJ
luogu
t1:第p-2位的数字
t2:第p-1位的数字
sa:是否出现三连
et:是否出现8
fr:是否出现4
lm:最高位限制
#define ll long long
#include<bits/stdc++.h>
using namespace std;
int len,w[12],c[12];
ll a,b,f[12][11][11][2][2][2];
ll dfs(int p,int t1,int t2,bool sa,bool et,bool fr,bool lm){
if(et&&fr)return 0;
if(!p)return sa;
if(!lm&&f[p][t1][t2][sa][et][fr]!=-1)
return f[p][t1][t2][sa][et][fr];
int up=lm?w[p]:9;ll res=0;
for(int i=0;i<=up;i++)
res+=dfs(p-1,t2,i,sa||(i==t1&&i==t2),et||(i==8),fr||(i==4),lm&&(i==up));
return lm?res:f[p][t1][t2][sa][et][fr]=res;
}
ll solve(ll x){
if(x<1e10)return 0;
len=0;
while(x){w[++len]=x%10;x/=10;}
ll res=0;
for(int i=1;i<=w[len];i++)//省掉判前导零
res+=dfs(len-1,10,i,0,i==8,i==4,i==w[len]);
return res;
}
int main(){
memset(f,-1,sizeof(f));
cin>>a>>b;
cout<<solve(b)-solve(a-1)<<endl;
return 0;
}

浙公网安备 33010602011771号