Submission #977125

#TimeUsernameProblemLanguageResultExecution timeMemory
977125VMaksimoski008Palindrome-Free Numbers (BOI13_numbers)C++17
100 / 100
3 ms1800 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

string s;
ll dp[19][11][11][2][2][19];

ll f(int pos, int last1, int last2, bool under, bool started, int len) {
    if(pos == s.size()) return 1;
    if(dp[pos][last1][last2][under][started][len] != -1) return dp[pos][last1][last2][under][started][len];
    
    ll ans = 0;
    for(int i=0; i<=9; i++) {
        if(len >= 1 && i == last1) continue;
        if(len >= 2 && i == last2) continue;
        if(!under && (i > (s[pos] - '0'))) continue;

        bool ns = started | (i > 0);
        ans += f(pos+1, i, last1, under | (i < (s[pos] - '0')), ns, len + ns);
    }

    return dp[pos][last1][last2][under][started][len] = ans;
}

ll cnt(ll x) {
    s = to_string(x);
    memset(dp, -1, sizeof(dp));
    return  f(0, 10, 10, 0, 0, 0);
}

int main() {
    ll l, r;
    cin >> l >> r;
    cout << cnt(r) - cnt(l-1) << '\n';
    return 0;
}

Compilation message (stderr)

numbers.cpp: In function 'll f(int, int, int, bool, bool, int)':
numbers.cpp:9:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    9 |     if(pos == s.size()) return 1;
      |        ~~~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...