不要62

LibreOJ-10167-不要62open in new window

实现

#include <iostream>
#include <vector>
using namespace std;

const int N = 12;
int f[N][N];

void initialize () {
    for (int i = 0; i <= 9; ++ i) f[1][i] = i != 4;
    for (int i = 2; i < N; ++ i) {
        for (int j = 0; j <= 9; ++ j) {
            if (j == 4) continue;
            for (int k = 0; k <= 9; ++ k) {
                if (k == 4 || (j == 6 && k == 2)) continue;
                f[i][j] += f[i - 1][k];
            }
        }
    }
}
int count (int n) {
    if (n == 0) return 1;

    vector<int> a;
    while (n > 0) {
        a.push_back(n % 10);
        n /= 10;
    }

    int res = 0, last = 0;
    for (int i = a.size() - 1; i >= 0; -- i) {
        int val = a[i];
        for (int j = 0; j < val; ++ j) {
            if (j == 4 || (last == 6 && j == 2)) continue;
            res += f[i + 1][j];
        }

        if (val == 4 || (last == 6 && val == 2)) break;
        last = val;

        if (i == 0) ++ res;
    }
    return res;
}

int main () {
    initialize();
    int l, r;
    while (cin >> l >> r, l || r)
        cout << count(r) - count(l - 1) << endl;
    return 0;
}
最后修改于: