Submission #547252

# Submission time Handle Problem Language Result Execution time Memory
547252 2022-04-10T05:06:53 Z Jomnoi Palindrome-Free Numbers (BOI13_numbers) C++17
100 / 100
2 ms 392 KB
#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;

long long dp[20][2][2][11][11];

long long solve2(string s, int idx = 0, bool f = false, bool lead0 = true, int p1 = 10, int p2 = 10) {
    if(dp[idx][f][lead0][p1][p2] != -1) {
        return dp[idx][f][lead0][p1][p2];
    }
    if(idx == s.size()) {
        return 1;
    }

    int len = 9;
    if(f == false) {
        len = s[idx] - '0';
    }

    long long res = 0;
    for(int i = 0; i <= len; i++) {
        if(i == p1 or i == p2) {
            continue;
        }

        bool nf = f, nlead0 = lead0;
        int np1 = i, np2 = p1;
        if(i < len) {
            nf = true;
        }
        if(i != 0) {
            nlead0 = false;
        }
        if(nlead0 == true) {
            np1 = np2 = 10;
        }
        res += solve2(s, idx + 1, nf, nlead0, np1, np2);
    }
    return dp[idx][f][lead0][p1][p2] = res;
}

long long solve(long long a) {
    for(int i = 0; i < 20; i++) {
        for(int j = 0; j <= 10; j++) {
            for(int k = 0; k <= 10; k++) {
                dp[i][0][0][j][k] = dp[i][1][0][j][k] = -1;
                dp[i][0][1][j][k] = dp[i][1][1][j][k] = -1;
            }
        }
    }

    string t;
    while(a > 0) {
        t.push_back(a % 10 + '0');
        a /= 10;
    }
    reverse(t.begin(), t.end());

    return solve2(t);
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    long long a, b, ans;
    cin >> a >> b;
    ans = solve(b);
    if(a > 0) {
        ans -= solve(a - 1);
    }
    cout << ans;
    return 0;
}

Compilation message

numbers.cpp: In function 'long long int solve2(std::string, int, bool, bool, int, int)':
numbers.cpp:11:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 |     if(idx == s.size()) {
      |        ~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 288 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 392 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct