Submission #869281

#TimeUsernameProblemLanguageResultExecution timeMemory
869281tvladm2009Palindrome-Free Numbers (BOI13_numbers)C++17
100 / 100
1 ms600 KiB
#include <cmath>
#include <functional>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <cassert>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>
#include <numeric>

using namespace std;

typedef long long ll;
const int DIGITS = 20;
const int LT = 0;
const int EQ = 1;
const int GT = 2;
ll dp[DIGITS][3][10][10];


bool check(string a) {
        for (int i = 0; i < (int)a.size() - 1; i++) {
                if (a[i] == a[i + 1]) {
                        return false;
                }
        }
        for (int i = 0; i < (int)a.size() - 2; i++) {
                if (a[i] == a[i + 2]) {
                        return false;
                }
        }
        return true;
}

ll get(string a) {
        if ((int)a.size() == 1) {
                return a[0] - '0' + 1;
        }
        memset(dp, 0, sizeof(dp));
        int n = (int)a.size();
        for (int d1 = 1; d1 <= 9; d1++) {
                for (int d2 = 0; d2 <= 9; d2++) {
                        if (d1 != d2) {
                                if (d1 * 10 + d2 < (a[0] - '0') * 10 + (a[1] - '0')) {
                                        dp[2][LT][d1][d2]++;
                                }
                                else if (d1 * 10 + d2 == (a[0] - '0') * 10 + (a[1] - '0')) {
                                        dp[2][EQ][d1][d2]++;
                                }
                                else {
                                        dp[2][GT][d1][d2]++;
                                }
                        }
                }
        }
        for (int i = 3; i <= n; i++) {
                for (int d = 0; d <= 9; d++) {
                        for (int d1 = 0; d1 <= 9; d1++) {
                                for (int d2 = 0; d2 <= 9; d2++) {
                                        if (d == d1 || d == d2 || d1 == d2) {
                                                continue;
                                        }
                                        if (d < a[i - 1] - '0') {
                                                dp[i][LT][d2][d] += dp[i - 1][LT][d1][d2];
                                                dp[i][LT][d2][d] += dp[i - 1][EQ][d1][d2];
                                                dp[i][GT][d2][d] += dp[i - 1][GT][d1][d2];
                                        }
                                        else if (d == a[i - 1] - '0') {
                                                dp[i][LT][d2][d] += dp[i - 1][LT][d1][d2];
                                                dp[i][EQ][d2][d] += dp[i - 1][EQ][d1][d2];
                                                dp[i][GT][d2][d] += dp[i - 1][GT][d1][d2];
                                        }
                                        else {
                                                dp[i][LT][d2][d] += dp[i - 1][LT][d1][d2];
                                                dp[i][GT][d2][d] += dp[i - 1][GT][d1][d2];
                                                dp[i][GT][d2][d] += dp[i - 1][EQ][d1][d2];
                                        }
                                }
                        }
                }
        }
        ll sol = 10;
        for (int len = 2; len <= n; len++) {
                for (int d1 = 0; d1 <= 9; d1++) {
                        for (int d2 = 0; d2 <= 9; d2++) {
                                sol += dp[len][LT][d1][d2] + dp[len][EQ][d1][d2];
                                if (len != n) {
                                        sol += dp[len][GT][d1][d2];
                                }
                        }
                }
        }
        return sol;
}

int main() {
#ifndef ONPC
        ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#else
        FILE* stream;
        freopen("input.txt", "r", stdin);
#endif
        string a, b;
        cin >> a >> b;
        cout << get(b) - get(a) + check(a);
        return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...