对于本题,需要注意不用考虑前导零而缩小位数,唯一的坑点就是题目范围给错了,特判一下吧
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define ll long long
#define re register
using namespace std;
inline ll read()
{
ll x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
ll a,b,dp[15][11][11][2][2][2][2],cnt,tmp[15];
ll dfs(int p,int a,int b,int c,int d,int _4,int _8)
{
//a表示上一位,b表示上上位
//c表示是否小于边界,d表示是否出现三个相同的数
if(_4&&_8) return 0;
if(p<=0) return d;//如果枚举完看看有没有出现三个相等的情况
if(~dp[p][a][b][c][d][_4][_8]) return dp[p][a][b][c][d][_4][_8];//记忆化啊
int lim=(c?9:tmp[p]);
ll res=0;
for(int i=0;i<=lim;++i)
res+=dfs(p-1,i,a,c||(i<lim),d||(i==a&&i==b),_4||(i==4),_8||(i==8));//注意这些取逻辑或的地方
return dp[p][a][b][c][d][_4][_8]=res;
}
ll calc(ll x)
{
if(x<1e10) return 0;//这真是个大坑,数据范围出锅了,题目保证>=10^10但出现了x<10^10
//这种情况不是手机号
memset(dp,-1,sizeof(dp));
cnt=0;
while(x)
{
tmp[++cnt]=x%10;
x/=10;
}
ll res=0;
for(re int i=1;i<=tmp[cnt];++i)//第一位不能是0,因此单独枚举第一位
res+=dfs(10,i,0,i<tmp[cnt],0,i==4,i==8);
return res;
}
int main()
{
a=read(),b=read();
printf("%lld
",calc(b)-calc(a-1));//记忆化搜索不用考虑边界了
return 0;
}