Submission #755445

# Submission time Handle Problem Language Result Execution time Memory
755445 2023-06-10T06:13:57 Z The_Samurai Palindrome-Free Numbers (BOI13_numbers) C++17
100 / 100
1 ms 340 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ll atoill(string s) {
    ll ans = 0;
    for (int i = 0; i < s.size(); i++) {
        ans = ans * 10 + (s[i] - '0');
    }
    return ans;
}

bool check(ll n) {
    string s = to_string(n);
    for (int i = 0; i < s.size(); i++) {
        if (i + 1 < s.size() and s[i] == s[i + 1]) return false;
        if (i + 2 < s.size() and s[i] == s[i + 2]) return false;
    }
    return true;
}

int get(ll l, ll r) {
    int ans = 0;
    for (ll i = l; i <= r; i++) ans += check(i);
    return ans;
}

ll pw(int a, int b) {
    ll ans = 1;
    for (int i = 0; i < b; i++) ans *= a;
    return ans;
}

ll dp(string s) {
    int n = s.size(), cnt = 0;
    for (int i = 0; i < n; i++) {
        if (s[i] == '*') break;
        if (i + 1 < n and s[i] == s[i + 1]) return 0;
        if (i + 2 < n and s[i] == s[i + 2]) return 0;
        cnt++;
    }
    if (cnt == 0) {
        return pw(9, 2) * pw(8, n - 2);
    }
    if (cnt == 1) {
        return pw(9, 1) * pw(8, n - 2);
    }
    return pw(8, n - cnt);
}

ll get(ll n) {
    if (n < 0) return 0;
    if (n < 10) return n + 1;
    ll ans = 10;
    int len = to_string(n).size();
    for (int i = 2; i < len; i++) ans += dp(string(i, '*'));
    string s = to_string(n), t = "";
    for (int i = 0; i < len; i++) {
        for (char x = '0'; x < s[i]; x++) {
            if (i == 0 and x == '0') continue;
            t += x;
            ans += dp(t + string(len - i - 1, '*'));
            t.pop_back();
        }
        t += s[i];
    }
    if (check(n)) ans++;
    return ans;

}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);

    ll l, r;
    cin >> l >> r;
//#ifdef sunnitov
//    cerr << get(l, r) << endl;
//#endif
    cout << get(r) - get(l - 1);

    return 0;
}

Compilation message

numbers.cpp: In function 'll atoill(std::string)':
numbers.cpp:8:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 |     for (int i = 0; i < s.size(); i++) {
      |                     ~~^~~~~~~~~~
numbers.cpp: In function 'bool check(ll)':
numbers.cpp:16:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |     for (int i = 0; i < s.size(); i++) {
      |                     ~~^~~~~~~~~~
numbers.cpp:17:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |         if (i + 1 < s.size() and s[i] == s[i + 1]) return false;
      |             ~~~~~~^~~~~~~~~~
numbers.cpp:18:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |         if (i + 2 < s.size() and s[i] == s[i + 2]) return false;
      |             ~~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 1 ms 320 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 320 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 272 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 320 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 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 320 KB Output is correct
19 Correct 1 ms 324 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 324 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 320 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 324 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 316 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 0 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 320 KB Output is correct