Submission #584168

# Submission time Handle Problem Language Result Execution time Memory
584168 2022-06-27T01:00:58 Z Olympia Palindrome-Free Numbers (BOI13_numbers) C++17
100 / 100
2 ms 240 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 <= 10000) {
        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;
    powr.push_back(1);
    for (int i = 0; i < 19; i++) {
        powr.push_back(powr.back() * 8);
    }
    for (int i = 2; i < 19; i++) {
        pos[i] = pos[i - 1];
        int64_t res = 81;
        pos[i] += res * powr[i - 2];
    }
    cout << query(a, b);
}

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 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 240 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 2 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 2 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 224 KB Output is correct
38 Correct 2 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 0 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 0 ms 212 KB Output is correct