P4124 [CQOI2016]手机号码

链接:https://www.luogu.org/problemnew/show/P4124

题目描述

人们选择手机号码时都希望号码好记、吉利。比如号码中含有几位相邻的相同数字、不含谐音不吉利的数字等。手机运营商在发行新号码时也会考虑这些因素,从号段中选取含有某些特征的号码单独出售。为了便于前期规划,运营商希望开发一个工具来自动统计号段中满足特征的号码数量。

工具需要检测的号码特征有两个:号码中要出现至少 33 个相邻的相同数字;号码中不能同时出现 88 和 44 。号码必须同时包含两个特征才满足条件。满足条件的号码例如:13000988721、23333333333、14444101000。而不满足条件的号码例如:1015400080、10010012022。

手机号码一定是 11 位数,前不含前导的 00 。工具接收两个数 LL 和 RR ,自动统计出 [L,R][L,R] 区间内所有满足条件的号码数量。 LL 和 RR 也是 1111 位的手机号码。

输入输出格式

输入格式:

输入文件内容只有一行,为空格分隔的 22 个正整数 L,RL,R 。

输出格式:

输出文件内容只有一行,为 11 个整数,表示满足条件的手机号数量。

输入输出样例

输入样例#1:  复制
12121284000 12121285550
输出样例#1:  复制
5

说明

样例解释:满足条件的号码: 12121285000、 12121285111、 12121285222、 12121285333、 12121285550。

数据范围: 10^10LR<10^1

题解:数位dp, 还是挺容易yy出来的,但要去掉前导0的情况,<10^10是不合法的

// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rt register
const int M = 2530;
ll dp[20][10][2][2][2][2];
int digit[20], cnt, tot, bg[M];

ll dfs(int dep, bool f, bool two, bool ok, int t, bool a4, bool a8){
    if(!dep) return (( !(a4 & a8) ) & ok);
    if(!f && dp[dep][t][two][ok][a4][a8] != -1) return dp[dep][t][two][ok][a4][a8];
    rt int i = f ? digit[dep] : 9;
    ll tmp = 0;
    for(; i >= 0; i--){
        if(dep==tot&&i==0) continue;
        bool ntwo, nok = ok;
        if(two && (t == i)) ntwo = 0, nok = 1;
        else if(i == t) ntwo = 1;
        else ntwo = 0;
        tmp += dfs(dep - 1, f & (i == digit[dep]), ntwo, nok, i, a4 | (i == 4), a8 | (i == 8));
    }
    if(!f)dp[dep][t][two][ok][a4][a8] = tmp;
    return tmp;
}


ll get(ll a){
    memset(dp, -1, sizeof(dp));    
    tot = 0;    
    while(a){
        digit[++tot] = a%10;
        a /= 10;
    }
    if(tot<11) return 0;
    ll ans = dfs(tot, 1, 0, 0, 0, 0, 0);
    return ans;
}
ll read(){
    ll x = 0; ll f = 1; char c = getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c<='9'&&c>='0'){x=x*10+c-'0';c=getchar();}
    return x*f;
}

int main(){
    ///freopen("1.in","r",stdin);
    //freopen("my.out","w",stdout);
    int T;
    memset(dp, -1, sizeof(dp));
    ll L = read(), R = read();
    ll ans2 = get(L - 1);
    ll ans1 = get(R);
    //cout<<ans1<<" "<<ans2;
    cout<<ans1-ans2<<endl;
}
//10000000000 10000012345
View Code

猜你喜欢

转载自www.cnblogs.com/EdSheeran/p/9483273.html