Description
求 ([l,r]) 中至少有 (3) 个连续相同数字,不同时有 (4,8) 的数字个数。
Solution
正常数位 dp 即可,注意前导零特判
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1000005;
vector <int> a;
int f[20][5][10][4];
int solve(int pos,int con,int last,int flag,int full)
{
if(pos<0) return con==3;
if(!full && ~f[pos][con][last][flag]) return f[pos][con][last][flag];
int lim=full?a[pos]:9;
int ans=0;
for(int now=(con==0?1:0);now<=lim;now++)
{
int newpos=pos-1;
int newcon=(now==last?con:0)+1;
if(con>=3) newcon=3;
int newlast=now;
int newflag=flag|(2*(now==8))|(now==4);
if(newflag==3) continue;
int newfull=full&&now==lim;
ans+=solve(newpos,newcon,newlast,newflag,newfull);
}
if(!full) f[pos][con][last][flag]=ans;
return ans;
}
int solve(int x)
{
a.clear();
memset(f,-1,sizeof f);
while(x)
{
a.push_back(x%10);
x/=10;
}
return solve(a.size()-1,0,0,0,1);
}
signed main()
{
ios::sync_with_stdio(false);
int l,r;
cin>>l>>r;
--l;
if(l>=(int)(1e10+0.5)) cout<<solve(r)-solve(l)<<endl;
else cout<<solve(r)<<endl;
return 0;
}