bzoj 4720: [Noip2016]换教室 (期望dp)

来源:互联网 发布:剔除异常数据的方法 编辑:程序博客网 时间:2024/05/16 10:06

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

存个板子。

代码

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;long long l, r, len, a[20], dp[20][20][20][2][2][2]; long long search(int pos, int pre, int ppre, int s1, int s2, int status, int fd) {    if(! fd && dp[pos][pre][ppre][s1][s2][status] != -1)        return dp[pos][pre][ppre][s1][s2][status];    if(! pos)        return status;    long long ans = 0;    int fdmax = fd ? a[pos] : 9;    for(int i = (pos == len) ? 1 : 0; i <= fdmax; i ++) {        if(i == 4 && ! s2)            ans += search(pos - 1, i, pre, 1, 0, status | (pre == ppre && pre == i), fd && (i == fdmax));        else if(i == 8 && ! s1)            ans += search(pos - 1, i, pre, 0, 1, status | (pre == ppre && pre == i), fd && (i == fdmax));        else if(i != 4 && i != 8)            ans += search(pos - 1, i, pre, s1, s2, status | (pre == ppre && pre == i), fd && (i == fdmax));    }    if(! fd) dp[pos][pre][ppre][s1][s2][status] = ans;    return ans;}long long work(long long x) {    len = 0;    while(x) {        a[++ len] = x % 10;        x /= 10;    }    return search(len, -1, -1, 0, 0, 0, 1);}int main() {    memset(dp, -1, sizeof(dp));    scanf("%lld %lld", &l, &r);    if(l == 10000000000) printf("%lld", work(r));    else printf("%lld", work(r) - work(l - 1));    return 0;}