1 条题解

  • 0
    @ 2025-10-8 17:10:45

    by qkw:

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    LL f[20][15][15][2][2][2],a[20];
    //从前往后依次为:当前长度,最低位数值,次低位数值,含4,含8,是否有连续三个数 
    LL dfs(LL x,LL b,LL l,bool f1,bool f2,bool f3,bool lim)
    {
        if(x==0)return f3;//一定要判断它符不符合条件 
        if(!lim&&f[x][ b ][l][f1][f2][f3]+1)return f[x][ b ][l][f1][f2][f3];
        LL up=lim?a[x]:9,ans=0;
        for(int i=0;i<=up;i++)
        {
            if(i==4&&f2)continue;if(i==8&&f1)continue;//遇到4和8重复时跳过 
            ans+=dfs(x-1,i,b,f1||(i==4),f2||(i==8),f3||(i==b&&b==l),lim&&i==up);
        }
        if(!lim)f[x][ b ][l][f1][f2][f3]=ans;
        return ans;
    }
    LL calc(LL x)
    {
        int len=0;
        while(x)a[++len]=x%10,x/=10;
        return dfs(len,0,0,0,0,0,1); 
    }
    int main()
    {
        LL a,b;scanf("%lld%lld",&a,&b);
    	memset(f,-1,sizeof(f));
        LL ans=0;
    	if(a==1e10)a++,ans++;//特判1e10(注意:1e10也是一个符合要求的电话号码) 
        ans+=calc(b)-calc(a-1);
        printf("%lld\n",ans);
        return 0;
    }
    
    • 1

    信息

    ID
    6186
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    递交数
    130
    已通过
    16
    上传者