Submission #779121

#TimeUsernameProblemLanguageResultExecution timeMemory
779121adaawfPalindrome-Free Numbers (BOI13_numbers)C++14
100 / 100
1 ms468 KiB
#include <iostream>
#include <cstring>
using namespace std;
string tostring(long long int a) {
    if (a == 0) return "0";
    string res = "";
    while (a != 0) {
        res = char(a % 10 + 48) + res;
        a /= 10;
    }
    return res;
}
long long int f[25][2][15][15][2];
long long int trya(string s, int i, int smaller, int x, int y, int flag) {
    if (i == s.size()) {
        return 1;
    }
    if (f[i][smaller][x][y][flag] != -1) return f[i][smaller][x][y][flag];
    long long int h = 9, res = 0;
    if (smaller == 0) h = s[i] - '0';
    for (int j = 0; j <= h; j++) {
        int ff = 0;
        if (j == x || j == y) continue;
        if (j == 0) ff = flag;
        else ff = 1;
        res += trya(s, i + 1, smaller | (j != h), (ff == 0) ? 10 : j, x, ff);
    }
    //cout << i << " " << smaller << " " << x << " " << y << " " << res << endl;
    f[i][smaller][x][y][flag] = res;
    return res;
}
int main() {
    long long int l, r;
    cin >> l >> r;
    l--;
    memset(f, -1, sizeof f);
    string s = tostring(r);
    long long int h = trya(s, 0, 0, 10, 10, 0), k;
    if (l < 0) {
        k = 0;
    }
    else {
        memset(f, -1, sizeof f);
        string t = tostring(l);
        k = trya(t, 0, 0, 10, 10, 0);
    }
    cout << h - k;
}

Compilation message (stderr)

numbers.cpp: In function 'long long int trya(std::string, int, int, int, int, int)':
numbers.cpp:15:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 |     if (i == s.size()) {
      |         ~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...