Submission #749743

#TimeUsernameProblemLanguageResultExecution timeMemory
749743BidoTeimaPalindrome-Free Numbers (BOI13_numbers)C++17
100 / 100
1 ms388 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long; 
int n;
vector<ll>mn,mx;
ll dp[20][11][11][2][2];
ll rec(int idx, int p, int pp, bool b, bool s){
    if(idx == n){
        return 1;
    }
    if(dp[idx][p + 1][pp + 1][b][s] != -1)
        return dp[idx][p + 1][pp + 1][b][s];
    int i = b ? 0 : mn[idx];
    int j = s ? 9 : mx[idx];
    ll ret = 0;
    for(int d = i; d <= j; d++){
        if(d != p && d != pp)
            ret += rec(idx + 1, d, p, b || (d > mn[idx]), s || (d < mx[idx]));
    }
    return dp[idx][p + 1][pp + 1][b][s] = ret;
}
int len(ll x){
    int ret = 0;
    while(x){
        x/=10;
        ret++;
    }
    return ret;
}
int main()
{ 
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    ll a,b;
    cin>>a>>b;
    int len_a=len(a),len_b=len(b);
    ll ans = 0;
    for(n = len_a; n <= len_b; ++n){
        mn.clear(),mx.clear();
        memset(dp, -1, sizeof(dp));
        if(n != len_a){
            mn.push_back(1);
            for(int i = 1; i < n; i++)mn.push_back(0);
        }else{
            string st = to_string(a);
            for(auto ch : st)mn.push_back(ch-'0');
        }
        if(n != len_b){
            for(int i = 0; i < n; i++)mx.push_back(9);
        }else{
            string st = to_string(b);
            for(auto ch : st)mx.push_back(ch - '0');
        }
        ans += rec(0, -1, -1, 0, 0);
    }
    cout<<ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...