Submission #346699

#TimeUsernameProblemLanguageResultExecution timeMemory
346699illequiprogrammatPalindrome-Free Numbers (BOI13_numbers)C++14
13.75 / 100
1 ms384 KiB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;

struct SOLUTION {
    bool vis[30][11][11][2];
    // memo[index][penultimate][end_digit][tight];
    ll memo[30][11][11][2];

    vector<int> digits;

    ll dp(int index, int penultimate, int end, bool tight) {
        if (index == digits.size()) return 1;
        if (vis[index][penultimate][end][tight])
            return memo[index][penultimate][end][tight];

        ll res = 0;
        int max_next = 9;
        if (tight) max_next = digits[index];

        for (int i = 0; i <= max_next; i++) {
            if (i != penultimate && i != end) {
                res += dp(index + 1, end, i, tight && i == max_next);
            }
        }

        vis[index][penultimate][end][tight] = true;
        memo[index][penultimate][end][tight] = res;
        return res;
    }

    void get_digits(ll n) {
        while (n != 0) {
            digits.push_back(n%10);
            n /= 10;
        }
        reverse(digits.begin(), digits.end());
    }

    bool is_palindrome() {
        //cout << digits.size() << endl; 
        bool res = false;
        int i = 0;

        if (digits.size()>1)
            while (i < digits.size() - 1) {
                if (digits[i] == digits[i+1]) res = true;
                i++;
            }
        i = 0;

        if (digits.size() > 2)
            while (i < digits.size() - 2) {
                if (digits[i] == digits[i+2]) res = true;
                i++;
            }

        return res;
    }

};

int main() {
    int a, b;
    if (b > a) swap(a, b);
    SOLUTION l_bound;
    SOLUTION u_bound;
    cin >> a >> b;
    l_bound.get_digits(a);
    ll lres = 0;
    if (a != 0) lres = l_bound.dp(0, 0, 10, true);
    u_bound.get_digits(b-1);
    ll ures = u_bound.dp(0, 0, 10, true);
    cout << ures - lres + !u_bound.is_palindrome() + !l_bound.is_palindrome()<< endl;
}

Compilation message (stderr)

numbers.cpp: In member function 'll SOLUTION::dp(int, int, int, bool)':
numbers.cpp:16:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |         if (index == digits.size()) return 1;
      |             ~~~~~~^~~~~~~~~~~~~~~~
numbers.cpp: In member function 'bool SOLUTION::is_palindrome()':
numbers.cpp:49:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |             while (i < digits.size() - 1) {
      |                    ~~^~~~~~~~~~~~~~~~~~~
numbers.cpp:56:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |             while (i < digits.size() - 2) {
      |                    ~~^~~~~~~~~~~~~~~~~~~
numbers.cpp: In function 'int main()':
numbers.cpp:68:11: warning: 'b' is used uninitialized in this function [-Wuninitialized]
   68 |     if (b > a) swap(a, b);
      |         ~~^~~
numbers.cpp:68:11: warning: 'a' is used uninitialized in this function [-Wuninitialized]
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...