传送门
思路:
比较基础的数位dp ,维护的条件也很明显,前导零也很好处理
代码:
(数组开小了,调了一个世纪。。)
#include<bits/stdc++.h>
#define pf printf
#define sf scanf
#define gc getchar()
using namespace std;
#define int long long
int f[15][15][10][2][2][2],top,bit[15],p=0;
// location lastingamount prenumber has8 has4 already3
// 位置 上一个连续的个数 上一个数 是否有8 是否有4 是否已经有连续的三个
inline int dfs(int loc,int maxnum,int las,int pre,int hs8,int hs4,int own3){
++p;
if(hs4&&hs8)return 0;
if(loc==0)return (own3==1);
if(maxnum==10&&f[loc][las][pre][hs8][hs4][own3]!=-1){
return f[loc][las][pre][hs8][hs4][own3];
}
int ret=0;
for(int i=(loc==top)? 1:0 ;i<maxnum;++i){
if(i==4&&hs8)continue;
if(i==8&&hs4)continue;
int ll=(pre==i) ? las+1 : 1 ;
ret+=dfs(loc-1,10,ll,i,hs8||(i==8),hs4||(i==4),own3||(ll>=3));
}
int i=maxnum,j=bit[loc-1];
int ll=(pre==i)? las+1 : 1 ;
if(maxnum==10)return f[loc][las][pre][hs8][hs4][own3]=ret;
else return ret+dfs(loc-1,j,ll,i,hs8||(i==8),hs4||(i==4),own3||(ll>=3));
}
inline void get(int k){
int num=0;char c=gc;
for(;!isdigit(c);c=gc);
for(;isdigit(c);c=gc){
num=(num<<1)+(num<<3)+(c^48);
}
num-=k;top=0;
while(num)bit[++top]=num%10,num/=10;
}
signed main (){
int l,r;
memset(f,-1,sizeof f);
get(1);
l= top==11? dfs(11,bit[11],0,-1,0,0,0) : 0;
get(0);p=0;
r=top==11 ? dfs(11,bit[11],0,-1,0,0,0) : 0;
pf("%lld",r-l);
return 0;
}