其他分享
首页 > 其他分享> > 【题解】P4124 [CQOI2016]手机号码

【题解】P4124 [CQOI2016]手机号码

作者:互联网

\(Desciption:\)

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

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

\(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;
}

标签:if4,return,int,题解,if8,len,ifc,CQOI2016,P4124
来源: https://www.cnblogs.com/JCNL666/p/10717490.html