\(Desciption:\)

给出区间 \([L,R]\) ,求区间内满足没有 \(4\)\(8\) 同事出现并且一定要有三位连续的相同。

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

并且一定是十一位的电话号码。

\(Sample\) \(Input:\)

12121284000 12121285550

\(Sample\) \(Output:\)

5

\(Solution:\)

考虑数位dp,一眼就是啊。。。

不然数据范围不会这么大。。。

那么传入好多个参数:

位数(\(len\)),是否有 \(4\) (\(if4\)), 是否有 \(8\) (\(if8\)),是否有连续的三个数(\(ifc\)),上一个的上一个是啥子(\(prepre\)),上一个是啥子(\(pre\)),取得上限(\(limit\))。

代码就非常显然了,但要注意如果 \(l==1e10\) 那么不能直接算 \(l-1\) 的合法个数,只能减去 \(0\)

个人认为这题除了传入参数要大胆,其他也没啥子了。。。

#include<bits/stdc++.h>
#define int long long
using namespace std;
int l,r,cnt;
const int N=12;
int f[N][2][2][2][N][N],digit[N];
inline int dfs(int len,bool if4,bool if8,bool ifc,int prepre,int pre,bool limit){
    if(if4 && if8) return 0;
    if(len==0){
        if(ifc) return 1;
        return 0;
    }
    if(!limit && f[len][if4][if8][ifc][prepre][pre]!=-1) return f[len][if4][if8][ifc][prepre][pre];
    
    int ret=0,up_bound=(limit)?digit[len]:9;
    for(int i=(len==cnt);i<=up_bound;++i){
        ret+=dfs(len-1,if4||(i==4),if8||(i==8),ifc||((i==prepre) && (i==pre)),pre,i,limit&&(i==up_bound)); 
    }
    return f[len][if4][if8][ifc][prepre][pre]=ret;
}
inline int solve(int x){
    if(x<(int)1e10) return 0;
    cnt=0;
    memset(f,-1,sizeof(f));
    while(x) digit[++cnt]=x%10,x/=10;
    return dfs(cnt,false,false,false,-10,-10,true);
}
signed main(){
    scanf("%lld%lld",&l,&r);
    printf("%lld\n",solve(r)-solve(l-1));
    return 0;
}
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄