Code

```#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#define maxb 15
using namespace std;
typedef long long ll;

ll l,r;
ll dp[maxb][15][15][2][2][2];
ll b[maxb],len;

ll dfs(ll pos,ll lst,ll llst,bool rep,bool four,bool eight,bool limit)
{
if(!pos)
return ((!eight)||(!four))&&rep;
if(~dp[pos][lst][llst][rep][four][eight] && !limit)
return dp[pos][lst][llst][rep][four][eight];
ll re=0;
for(register ll i=0;i<=(limit?b[pos]:9);++i)
re+=dfs(pos-1,i,lst,rep||((i==lst)&&(lst==llst)),four||(i==4),eight||(i==8),limit&&(i>=b[pos]));
if(!limit)
dp[pos][lst][llst][rep][four][eight]=re;
return re;
}

ll Work(ll num)
{
if(num<1e10)
return 0;
memset(dp,-1,sizeof(dp));
memset(b,0,sizeof(b));
len=0;
while(num)
{
b[++len]=num%10;
num/=10;
}
ll re=0;
for(register ll i=1;i<=b[len];++i)
re+=dfs(len-1,i,10,0,i==4,i==8,i==b[len]);
return re;
}

int main()
{
scanf("%lld%lld",&l,&r);
printf("%lld",Work(r)-Work(l-1));
return 0;
}```

【题解】Luogu P4121 [CQOI2016]手机号码 数位DP

(0)
(0)

0条