Submission #786819

#TimeUsernameProblemLanguageResultExecution timeMemory
786819serifefedartarPalindrome-Free Numbers (BOI13_numbers)C++17
100 / 100
2 ms340 KiB
#include <bits/stdc++.h>
using namespace std;

#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define MAXN 100005
#define int long long

ll count(string x) {
    int len = x.length();

    vector<vector<vector<vector<int>>>> dp(11, vector<vector<vector<int>>>(11, vector<vector<int>>(2, vector<int>(2, 0))));
    dp[10][10][0][0] = 1;
    for (int i = 0; i < len; i++) {
        vector<vector<vector<vector<int>>>> new_dp(11, vector<vector<vector<int>>>(11, vector<vector<int>>(2, vector<int>(2, 0))));
        for (auto not_leading : {false, true}) {
            for (auto smaller : {false, true}) {
                for (int second_last = 0; second_last <= 10; second_last++) {
                    for (int last_digit = 0; last_digit <= 10; last_digit++) {
                        for (int digit = 0; digit < 10; digit++) {
                            if (!smaller && digit > x[i]-'0')
                                break;
                            if (not_leading && (digit == last_digit || digit == second_last))
                                continue;
                            new_dp[((!not_leading && digit == 0) ? 10 : digit)][last_digit][smaller || digit < x[i]-'0'][not_leading || digit != 0] += dp[last_digit][second_last][smaller][not_leading];
                        }
                    }
                }
            }
        }
        dp = new_dp;
    }
    
    ll ans = 0;
    for (int second_last = 0; second_last <= 10; second_last++)
        for (int last_digit = 0; last_digit <= 10; last_digit++)
            for (auto smaller : {false, true})
                for (auto not_leading : {false, true})
                    ans += dp[last_digit][second_last][smaller][not_leading];

    return ans;
}
signed main() {
    fast
    ll a, b;
    cin >> a >> b;
    if (a == 0) {
        string strB = to_string(b);
        cout << count(strB) << endl;
    } else {
        string strA = to_string(a-1), strB = to_string(b);
        ll ans = count(strB) - count(strA);
        cout << ans << endl;
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...