Submission #1121223

# Submission time Handle Problem Language Result Execution time Memory
1121223 2024-11-28T15:02:28 Z Captain_Georgia Palindrome-Free Numbers (BOI13_numbers) C++17
100 / 100
1 ms 504 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int32_t main () {

    ios_base::sync_with_stdio(0); 
    cin.tie(0); 
    cout.tie(0); 

    ll L, R;
    cin >> L >> R;

    auto get = [&](ll eded) -> ll {
        if (eded == -1LL) return 0;

        string x = to_string(eded);
        if (x.size() == 1) {
            return x[0] - '0' + 1LL;
        }
        reverse (x.begin(), x.end());
        const int MAXN = x.size();
        const int MAXM = 10;
        vector<vector<vector<array<ll, 2>>>> dp(MAXN + 1, vector<vector<array<ll, 2>>> (MAXM, vector<array<ll, 2>>(MAXM, {0, 0})));

        for (int a = 0;a < MAXM;a ++) {
            for (int b = 0;b < MAXM;b ++) {
                if (a != b) {
                    dp[2][a][b][0] = 1;
                    if (x[1] - '0' >= a || (x[1] - '0' == a && x[0] - '0' >= b)) {
                        dp[2][a][b][1] = 1;
                    }
                }
            }
        }
        for (int i = 3;i <= MAXN;i ++) {
            for (int a = 0;a < MAXM;a ++) {
                for (int b = 0;b < MAXM;b ++) if (a != b) {
                    for (int c = 0;c < MAXM;c ++) if (a != c && b != c) {
                        if (a < x[i - 1] - '0' || (a == x[i - 1] - '0' && b < x[i - 2] - '0') || (a == x[i - 1] - '0' && b == x[i - 2] - '0' && c < x[i - 3] - '0')) {
                            dp[i][a][b][0] += dp[i - 1][b][c][0];
                            dp[i][a][b][1] += dp[i - 1][b][c][0];
                        } else if ((a == x[i - 1] - '0' && b == x[i - 2] - '0' && c == x[i - 3] - '0')) {
                            dp[i][a][b][0] += dp[i - 1][b][c][0];
                            dp[i][a][b][1] += dp[i - 1][b][c][1];
                        } else {
                            dp[i][a][b][0] += dp[i - 1][b][c][0];
                        }
                    }
                }
            }
        }
        ll ans = 10;
        for (int i = 2;i <= MAXN;i ++) {
            for (int a = 1;a < MAXM;a ++) {
                for (int b = 0;b < MAXM;b ++) if (a != b) {
                    if (i < MAXN || (a < x[i - 1] - '0' || (a == x[i - 1] - '0' && b < x[i - 2] - '0'))) {
                        ans += dp[i][a][b][0];
                    } else if ((a == x[i - 1] - '0' && b == x[i - 2] - '0')) {
                        ans += dp[i][a][b][1];
                    }
                }
            }
        }
        return ans;
    };
    cout << get (R) - get (L - 1) << "\n";
    // cout << get (R) << " " << get(L - 1) << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 504 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 336 KB Output is correct
37 Correct 1 ms 336 KB Output is correct
38 Correct 1 ms 336 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 404 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 1 ms 336 KB Output is correct
44 Correct 1 ms 336 KB Output is correct
45 Correct 1 ms 336 KB Output is correct