Submission #102420

#TimeUsernameProblemLanguageResultExecution timeMemory
102420MoNsTeR_CuBePalindrome-Free Numbers (BOI13_numbers)C++17
100 / 100
4 ms640 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define INF 9223372036854775807

string toString(int a){
    string s = "";
    if(a == 0) return "0";
    while(a){
        s = (char)(a%10+'0') + s;
        a /= 10;
    }
    return s;
}

string a, b;

int memo[20][11][11][2][2][2];

int dp(int curr, int last, int lastlast, bool ok, bool isPal, bool first){

    if(curr == b.size()){
        if(isPal && !first) return 0;
        else{
            return 1;
        }
    }

    if(memo[curr][last][lastlast][ok][isPal][first] != -1) return memo[curr][last][lastlast][ok][isPal][first];

    memo[curr][last][lastlast][ok][isPal][first] = 0;

    if(ok){
        for(int i = 0; i <= 10; i++){
            if(first && i == 1) continue;
            if(!first && i == 0) continue;
            if((i == last || i == lastlast) && !first){
                memo[curr][last][lastlast][ok][isPal][first] += dp(curr+1,i,last,ok,1,0);
            }else{
                bool cond = first;
                if(cond && i != 0) cond = false;
                memo[curr][last][lastlast][ok][isPal][first] += dp(curr+1,i,last,ok,isPal, cond);
            }
        }
    }else{
        int u = b[curr]-'0'+1;
        for(int i = 0; i <= u; i++){
            if(first && i == 1) continue;
            if(!first && i == 0) continue;
            if((i == last || i == lastlast) && !first){
                memo[curr][last][lastlast][ok][isPal][first] += dp(curr+1,i,last,i < u,1,0);
            }else{
                bool cond = first;
                if(cond && i != 0) cond = false;
                memo[curr][last][lastlast][ok][isPal][first] += dp(curr+1,i,last,i < u,isPal, cond);
            }
        }
    }
    return memo[curr][last][lastlast][ok][isPal][first];
}

void reset(){
    for(int i = 0; i <= 19; i++){
        for(int j = 0; j < 11; j++){
            for(int k = 0; k < 11; k++){
                for(int l = 0; l < 2; l++){
                    for(int m = 0; m < 2; m++){
                        for(int n = 0; n < 2; n++){
                            memo[i][j][k][l][m][n] = -1;
                        }
                    }
                }
            }
        }
    }
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int c, d;
    cin >> c >> d;

    a = toString(c-1);
    b = toString(d);

    reset();

    if(c == 0){
        cout << dp(0,0,0,0,0,1) << endl;
    }else{
        int t = dp(0,0,0,0,0,1);
        b = a;
        reset();
        int x = dp(0,0,0,0,0,1);
        t -= x;
        cout << t << endl;
    }
}

Compilation message (stderr)

numbers.cpp: In function 'long long int dp(long long int, long long int, long long int, bool, bool, bool)':
numbers.cpp:23:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(curr == b.size()){
        ~~~~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...