You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

AcWing 1085. 不要62

一、题目描述

杭州人称那些傻乎乎粘嗒嗒的人为 62(音:laoer)。

杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。

不吉利的数字为所有含有 462 的号码。例如:62315,73418,88914 都属于不吉利号码。但是,61152 虽然含有 62,但不是 连号,所以不属于不吉利数字之列。

你的任务是,对于每次给出的一个牌照号区间 [n,m],推断出交管局今后又要实际上给多少辆新的士车上牌照了。

输入格式 输入包含多组测试数据,每组数据占一行。

每组数据包含一个整数对 nm

当输入一行为0 0时,表示输入结束。

输出格式 对于每个整数对,输出一个不含有不吉利数字的统计个数,该数值占一行位置。

数据范围 1≤n≤m≤10^9

输入样例

1 100
0 0

输出样例

80

二、实现思路

  • 给出了[n,m]的区间
  • 不要数字4,不要连续的62,问可以有多少个符合条件的数字

以上两个条件,满足数位DP的情况,可以直接套用模板解决:

### 三、实现代码

#include <bits/stdc++.h>

using namespace std;
const int N = 32;

int a[N], al;
int f[N][10];

int dfs(int u, int st, bool op) {
    if (u == 0) return 1;
    if (!op && ~f[u][st]) return f[u][st];
    int up = op ? a[u] : 9;
    int ans = 0;
    for (int i = 0; i <= up; i++) {
        if (st == 6 && i == 2) continue;
        if (i == 4) continue;
        ans += dfs(u - 1, i, op && i == a[u]);
    }

    if (!op) f[u][st] = ans;
    return ans;
}

int calc(int x) {
    al = 0;
    while (x) a[++al] = x % 10, x /= 10;
    return dfs(al, -1, true);
}
int main() {
    memset(f, -1, sizeof f);
    int l, r;
    while (~scanf("%d%d", &l, &r) && l + r)
        printf("%d\n", calc(r) - calc(l - 1));
    return 0;
}