Submission #584166

# Submission time Handle Problem Language Result Execution time Memory
584166 2022-06-27T00:59:20 Z Olympia Palindrome-Free Numbers (BOI13_numbers) C++17
0 / 100
1000 ms 340 KB
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome (int64_t i) {
    string s = to_string(i);
    bool fine = true;
    for (int64_t j = 0; j < s.length(); j++) {
        if (j >= 1 && s[j] == s[j - 1]) {
            fine = false;
        }
        if (j >= 2 && s[j] == s[j - 2]) {
            fine = false;
        }
    }
    return fine;
}
int64_t res (int64_t a, int64_t b) {
    int64_t ans = 0;
    for (int64_t i = a; i <= b; i++) {
        ans += isPalindrome(i);
    }
    return ans;
}
int64_t removeAllBut2 (int64_t x) {
    string s = to_string(x);
    for (int64_t i = 2; i < s.length(); i++) {
        s[i] = '0';
    }
    assert(to_string(stoll(s)) == s);
    return stoll(s);
}
vector<int64_t> pos;
vector<int64_t> powr;
int64_t query (int64_t a, int64_t b) {
    if (b - a <= 100000) {
        return res(a, b);
    }
    if (a != 0) {
        return query(0, b) - query(0, a - 1);
    }
    if (a == b) {
        return a;
    }
    string s = to_string(b);
    int64_t tot = 0;
    int64_t c = 0;
    for (int64_t i = 1; i <= 9; i++) {
        for (int64_t j = 0; j <= 9; j++) {
            for (int64_t k = 0; k <= 9; k++) {
                if (i == j || j == k || i == k) continue;
                tot += (100 * i + 10 * j + k < 100 * (s[0] - '0') + 10 * (s[1] - '0') + (s[2] - '0')) * powr[s.size() - 3];
                if (i == s[0] - '0' && j == s[1] - '0' && k == s[2] - '0') {
                    if (s[1] != '0') {
                        string orig = s;
                        reverse(s.begin(), s.end());
                        s.pop_back();
                        reverse(s.begin(), s.end());
                        tot += query(removeAllBut2(stoll(s)), stoll(s));
                        s = orig;
                    } else {
                        for (int m = 0; m <= 9; m++) {
                            if (k == m || j == m) continue;
                            if (m == s[3] - '0') {
                                string orig = s;
                                reverse(s.begin(), s.end());
                                s.pop_back(), s.pop_back();
                                reverse(s.begin(), s.end());
                                tot += query(removeAllBut2(stoll(s)), stoll(s));
                                s = orig;
                            } else if (m < s[3] - '0') {
                                tot += powr[s.size() - 4];
                            }
                        }
                    }
                }
            }
        }
    }
    return tot + pos[s.length() - 1];
}
int main() {
    int64_t a, b;
    cin >> a >> b;
    pos.resize(20);
    pos[1] = 10;
    for (int i = 2; i < 19; i++) {
        pos[i] = pos[i - 1];
        int64_t res = 81;
        int t = i - 2;
        while (t--) {
            res *= 8;
        }
        pos[i] += res;
    }
    powr.push_back(1);
    for (int i = 0; i < 19; i++) {
        powr.push_back(powr.back() * 8);
    }
    cout << query(a, b) << " " << res(a, b) << '\n';
}

Compilation message

numbers.cpp: In function 'bool isPalindrome(int64_t)':
numbers.cpp:6:27: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    6 |     for (int64_t j = 0; j < s.length(); j++) {
      |                         ~~^~~~~~~~~~~~
numbers.cpp: In function 'int64_t removeAllBut2(int64_t)':
numbers.cpp:25:27: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |     for (int64_t i = 2; i < s.length(); i++) {
      |                         ~~^~~~~~~~~~~~
numbers.cpp: In function 'int64_t query(int64_t, int64_t)':
numbers.cpp:45:13: warning: unused variable 'c' [-Wunused-variable]
   45 |     int64_t c = 0;
      |             ^
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Incorrect 0 ms 212 KB Output isn't correct
3 Incorrect 14 ms 212 KB Output isn't correct
4 Incorrect 4 ms 212 KB Output isn't correct
5 Incorrect 0 ms 212 KB Output isn't correct
6 Incorrect 0 ms 212 KB Output isn't correct
7 Incorrect 0 ms 212 KB Output isn't correct
8 Incorrect 1 ms 212 KB Output isn't correct
9 Incorrect 0 ms 212 KB Output isn't correct
10 Incorrect 0 ms 212 KB Output isn't correct
11 Incorrect 0 ms 212 KB Output isn't correct
12 Incorrect 0 ms 212 KB Output isn't correct
13 Incorrect 0 ms 212 KB Output isn't correct
14 Incorrect 4 ms 212 KB Output isn't correct
15 Incorrect 4 ms 300 KB Output isn't correct
16 Incorrect 0 ms 212 KB Output isn't correct
17 Incorrect 2 ms 212 KB Output isn't correct
18 Incorrect 0 ms 212 KB Output isn't correct
19 Incorrect 12 ms 300 KB Output isn't correct
20 Incorrect 4 ms 212 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 210 ms 276 KB Output isn't correct
2 Execution timed out 1088 ms 212 KB Time limit exceeded
3 Execution timed out 1084 ms 212 KB Time limit exceeded
4 Execution timed out 1083 ms 212 KB Time limit exceeded
5 Execution timed out 1090 ms 212 KB Time limit exceeded
6 Execution timed out 1086 ms 212 KB Time limit exceeded
7 Incorrect 83 ms 296 KB Output isn't correct
8 Incorrect 62 ms 280 KB Output isn't correct
9 Incorrect 103 ms 284 KB Output isn't correct
10 Incorrect 149 ms 276 KB Output isn't correct
11 Incorrect 222 ms 280 KB Output isn't correct
12 Execution timed out 1051 ms 212 KB Time limit exceeded
13 Incorrect 226 ms 212 KB Output isn't correct
14 Execution timed out 1093 ms 212 KB Time limit exceeded
15 Execution timed out 1080 ms 212 KB Time limit exceeded
16 Execution timed out 1075 ms 212 KB Time limit exceeded
17 Execution timed out 1082 ms 212 KB Time limit exceeded
18 Execution timed out 1093 ms 212 KB Time limit exceeded
19 Execution timed out 1074 ms 212 KB Time limit exceeded
20 Execution timed out 1082 ms 212 KB Time limit exceeded
21 Execution timed out 1082 ms 212 KB Time limit exceeded
22 Execution timed out 1088 ms 212 KB Time limit exceeded
23 Execution timed out 1093 ms 212 KB Time limit exceeded
24 Execution timed out 1076 ms 212 KB Time limit exceeded
25 Execution timed out 1085 ms 292 KB Time limit exceeded
26 Execution timed out 1080 ms 300 KB Time limit exceeded
27 Execution timed out 1078 ms 212 KB Time limit exceeded
28 Execution timed out 1081 ms 212 KB Time limit exceeded
29 Execution timed out 1089 ms 212 KB Time limit exceeded
30 Execution timed out 1095 ms 212 KB Time limit exceeded
31 Execution timed out 1090 ms 212 KB Time limit exceeded
32 Execution timed out 1087 ms 212 KB Time limit exceeded
33 Execution timed out 1089 ms 292 KB Time limit exceeded
34 Execution timed out 1089 ms 212 KB Time limit exceeded
35 Execution timed out 1094 ms 212 KB Time limit exceeded
36 Execution timed out 1069 ms 212 KB Time limit exceeded
37 Execution timed out 1032 ms 212 KB Time limit exceeded
38 Execution timed out 1071 ms 340 KB Time limit exceeded
39 Execution timed out 1095 ms 212 KB Time limit exceeded
40 Execution timed out 1097 ms 212 KB Time limit exceeded
41 Execution timed out 1084 ms 212 KB Time limit exceeded
42 Execution timed out 1089 ms 212 KB Time limit exceeded
43 Execution timed out 1090 ms 212 KB Time limit exceeded
44 Execution timed out 1090 ms 212 KB Time limit exceeded
45 Execution timed out 1084 ms 212 KB Time limit exceeded