Submission #899630

#TimeUsernameProblemLanguageResultExecution timeMemory
899630wkspPalindrome-Free Numbers (BOI13_numbers)C++17
28.75 / 100
1 ms600 KiB
#include<bits/stdc++.h>
using namespace std;


int dp[25][11]; // position, letter (0-9)
int dp_static[25][11];

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    long long a, b;
    cin >> a >> b;
    vector<int> a_ve;
    vector<int> b_ve;
    while(a > 0){
        a_ve.push_back(a%10);
        a /= 10;
    }
    while(b > 0){
        b_ve.push_back(b%10);
        b /= 10;
    }
    reverse(a_ve.begin(), a_ve.end());
    reverse(b_ve.begin(), b_ve.end());
    for(int i = 0; i < 10; i++){
        dp[1][i] = i;
    }
    for(int i = 2; i < 20; i++){
        dp[i][0] = dp[i-1][9];
        for(int j = 1; j < 10; j++){
            dp_static[i][j] = dp[i][0] - dp_static[i-1][j] - dp_static[i-2][j];
            dp[i][j] = dp[i][j-1] + dp_static[i][j]; 
        }
    }
    int last = -1;
    int last_second = -1;
    int nr = a_ve.size() + 1;
    int ans_a = 0;
    for(auto u: a_ve){
        nr--;
        if(last == u or last_second == u){
            break;
        }
        if(u == 0){
            last_second = last;
            last = u;
            continue;
        }
        ans_a += dp[nr][u-1];
        if(last != -1){
            ans_a -= dp_static[nr-1][last];
        }
        if(last != -1 and last < u){
            ans_a -= dp_static[nr][last];
        }
        if(last_second != -1 and last_second < u){
            ans_a -= dp_static[nr][last_second];
        }

        last_second = last;
        last = u;
    }   
    last = -1;
    last_second = -1;
    nr = b_ve.size() + 1;
    int ans_b = 0;
    for(auto u: b_ve){
        nr--;
        if(last == u or last_second == u){
            break;
        }
        if(u == 0){
            last_second = last;
            last = u;
            continue;
        }
        ans_b += dp[nr][u-1];
        if(last != -1){
            ans_b -= dp_static[nr-1][last];
        }
        if(last != -1 and last < u){
            ans_b -= dp_static[nr][last];
        }
        if(last_second != -1 and last_second < u){
            ans_b -= dp_static[nr][last_second];
        }

        last_second = last;
        last = u;
    }
    int ans = ans_b - ans_a;
    cout << ans << '\n';





    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...