Submission #869276

# Submission time Handle Problem Language Result Execution time Memory
869276 2023-11-03T23:45:11 Z tvladm2009 Palindrome-Free Numbers (BOI13_numbers) C++17
91.25 / 100
1 ms 756 KB
#include <cmath>
#include <functional>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <cassert>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>
#include <numeric>

using namespace std;

typedef long long ll;
const int DIGITS = 20;
const int LT = 0;
const int EQ = 1;
const int GT = 2;
ll dp[DIGITS][3][10][10];


bool check(string a) {
        for (int i = 0; i < (int)a.size() - 1; i++) {
                if (a[i] == a[i + 1]) {
                        return false;
                }
        }
        for (int i = 0; i < (int)a.size() - 2; i++) {
                if (a[i] == a[i + 2]) {
                        return false;
                }
        }
        return true;
}

ll get(string a) {
        if ((int)a.size() == 1) {
                return a.size() - 1;
        }
        memset(dp, 0, sizeof(dp));
        int n = (int)a.size();
        for (int d1 = 1; d1 <= 9; d1++) {
                for (int d2 = 0; d2 <= 9; d2++) {
                        if (d1 != d2) {
                                if (d1 * 10 + d2 < (a[0] - '0') * 10 + (a[1] - '0')) {
                                        dp[2][LT][d1][d2]++;
                                }
                                else if (d1 * 10 + d2 == (a[0] - '0') * 10 + (a[1] - '0')) {
                                        dp[2][EQ][d1][d2]++;
                                }
                                else {
                                        dp[2][GT][d1][d2]++;
                                }
                        }
                }
        }
        for (int i = 3; i <= n; i++) {
                for (int d = 0; d <= 9; d++) {
                        for (int d1 = 0; d1 <= 9; d1++) {
                                for (int d2 = 0; d2 <= 9; d2++) {
                                        if (d == d1 || d == d2 || d1 == d2) {
                                                continue;
                                        }
                                        if (d < a[i - 1] - '0') {
                                                dp[i][LT][d2][d] += dp[i - 1][LT][d1][d2];
                                                dp[i][LT][d2][d] += dp[i - 1][EQ][d1][d2];
                                                dp[i][GT][d2][d] += dp[i - 1][GT][d1][d2];
                                        }
                                        else if (d == a[i - 1] - '0') {
                                                dp[i][LT][d2][d] += dp[i - 1][LT][d1][d2];
                                                dp[i][EQ][d2][d] += dp[i - 1][EQ][d1][d2];
                                                dp[i][GT][d2][d] += dp[i - 1][GT][d1][d2];
                                        }
                                        else {
                                                dp[i][LT][d2][d] += dp[i - 1][LT][d1][d2];
                                                dp[i][GT][d2][d] += dp[i - 1][GT][d1][d2];
                                                dp[i][GT][d2][d] += dp[i - 1][EQ][d1][d2];
                                        }
                                }
                        }
                }
        }
        ll sol = 9;
        for (int len = 2; len <= n; len++) {
                for (int d1 = 0; d1 <= 9; d1++) {
                        for (int d2 = 0; d2 <= 9; d2++) {
                                sol += dp[len][LT][d1][d2] + dp[len][EQ][d1][d2];
                                if (len != n) {
                                        sol += dp[len][GT][d1][d2];
                                }
                        }
                }
        }
        return sol;
}

int main() {
#ifndef ONPC
        ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#else
        FILE* stream;
        freopen("input.txt", "r", stdin);
#endif
        string a, b;
        cin >> a >> b;
        cout << get(b) - get(a) + check(a);
        return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Incorrect 0 ms 348 KB Output isn't correct
6 Incorrect 0 ms 592 KB Output isn't correct
7 Incorrect 0 ms 348 KB Output isn't correct
8 Incorrect 0 ms 348 KB Output isn't correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Incorrect 1 ms 460 KB Output isn't correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 460 KB Output is correct
18 Correct 0 ms 352 KB Output is correct
19 Correct 0 ms 352 KB Output is correct
20 Correct 0 ms 352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 352 KB Output is correct
2 Correct 0 ms 464 KB Output is correct
3 Correct 0 ms 352 KB Output is correct
4 Correct 0 ms 356 KB Output is correct
5 Correct 0 ms 360 KB Output is correct
6 Correct 0 ms 360 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 1 ms 352 KB Output is correct
10 Correct 0 ms 352 KB Output is correct
11 Correct 0 ms 352 KB Output is correct
12 Correct 0 ms 352 KB Output is correct
13 Correct 0 ms 352 KB Output is correct
14 Correct 0 ms 464 KB Output is correct
15 Correct 0 ms 352 KB Output is correct
16 Correct 0 ms 352 KB Output is correct
17 Correct 1 ms 352 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 756 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 0 ms 460 KB Output is correct
43 Correct 1 ms 344 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct