[BZOJ4521] [Cqoi2016]手机号码

Description

人们选择手机号码时都希望号码好记、吉利。比如号码中含有几位相邻的相同数字、不含谐音不
吉利的数字等。手机运营商在发行新号码时也会考虑这些因素,从号段中选取含有某些特征的号
码单独出售。为了便于前期规划,运营商希望开发一个工具来自动统计号段中满足特征的号码数
量。
工具需要检测的号码特征有两个:号码中要出现至少3个相邻的相同数字,号码中不能同
时出现8和4。号码必须同时包含两个特征才满足条件。满足条件的号码例如:13000988721、
23333333333、14444101000。而不满足条件的号码例如:1015400080、10010012022。
手机号码一定是11位数,前不含前导的0。工具接收两个数L和R,自动统计出[L,R]区间
内所有满足条件的号码数量。L和R也是11位的手机号码。

Input

输入文件内容只有一行,为空格分隔的2个正整数L,R。
10^10 < =  L < =  R < 10^11

Output

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

Sample Input

12121284000 12121285550

Sample Output

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

 
又是数位DP...
还是那么水,都是老套路。
设$\large f[i][j][k][0/1][0/1][0/1][0/1]$前i位,i-1位是什么,i-2位是什么,是否出现8,是否出现4,是否已经满足连续三个相同。
然后就随便DP。
 

 
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define int long long
#define reg register

int l, r;
int wei[15], cnt;
int f[15][15][15][2][2][2];//前i位,i-1位是什么,i-2位是什么,是否出现8,是否出现4,是否已经满足连续三个相同

int dp(int pos, int lst1, int lst2, bool eight, bool four, bool lim, bool lead, bool hav)
{
    if (pos == 0) return hav;
    if (~f[pos][lst1][lst2][eight][four][hav] and !lim and !lead) return f[pos][lst1][lst2][eight][four][hav];
    int up = lim ? wei[pos] : 9;
    int down = lead ? 1 : 0;
    int ans = 0;
    for (reg int i = down ; i <= up ; i ++)
    {
        if (eight and i == 4) continue;
        if (four and i == 8) continue;
        ans += dp(pos - 1, i, lst1, eight || (i == 8), four || (i == 4), (lim && (i == wei[pos])), 0, hav || ((i == lst1) && (lst1 == lst2)));
    }
    if (!lim and !lead) f[pos][lst1][lst2][eight][four][hav] = ans;
    return ans;
}

inline int solve(int x)
{
    if (x < 1e10) return 0;
    cnt = 0;
    while(x)
    {
        wei[++cnt] = x % 10;
        x /= 10;
    }
    return dp(cnt, 11, 11, 0, 0, 1, 1, 0);
}

signed main()
{
    scanf("%lld%lld", &l, &r);
    memset(f, -1, sizeof f);
    printf("%lld\n", solve(r) - solve(l - 1));
    return 0;
}

 

posted @ 2018-08-30 10:32  zZhBr  阅读(164)  评论(0编辑  收藏  举报