Submission #681864

# Submission time Handle Problem Language Result Execution time Memory
681864 2023-01-14T17:55:03 Z vjudge1 Palindrome-Free Numbers (BOI13_numbers) C++17
72.5 / 100
1 ms 340 KB
// header file
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// pragma
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
// macros
#define endl "\n"
#define ll long long
#define mp make_pair
#define ins insert
#define lb lower_bound
#define pb push_back
#define ub upper_bound
#define lll __int128
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
ll solve(ll a) {
    if(a < 0)
        return 0;
    // palindrome free -> yang penting 2 bilangan terakhir
    string s = to_string(a);
    // fi -> idx
    // se -> second last digit
    // ti -> last digit
    // fo -> follow s or not
    ll dp[s.size()][11][10][2];
    memset(dp, 0, sizeof(dp));
    // 10 -> case leading zero
    // handle case leading zero
    for(int i = 0; i < s.size(); ++i) {
        if(i == 0) {
            for(int j = 0; j < s[i] - '0'; ++j)
                dp[i][0][j][0] = 1;
            dp[i][0][s[i] - '0'][1] = 1;
        }
        else if(i == 1) {
            for(int j = 0; j < 10; ++j)
                dp[i][10][j][0] = 1;
            for(int j = 0; j < 10; ++j) {
                // ambil dari non leadingnya terserah
                // tidak boleh sama dengan pr
                for(int k = 0; k <= 10; ++k) {
                    if(k == j)
                        continue;
                    // j last digit
                    // k second last
                    dp[i][k][j][0] += dp[i - 1][0][k][0];
                    if(j == s[i] - '0' && k == s[i - 1] - '0')
                        dp[i][k][j][1] += dp[i - 1][0][k][1];
                    else if(j < s[i] - '0')
                        dp[i][k][j][0] += dp[i - 1][0][k][1];
                }
            }
        }
        else {
            for(int j = 0; j < 10; ++j)
                dp[i][10][j][0] = 1;
            for(int j = 0; j < 10; ++j) {
                for(int k = 0; k < 10; ++k) {
                    if(j == k)
                        continue;
                    for(int l = 0; l <= 10; ++l) {
                        if(j == l || k == l)
                            continue;
                        // j last digit
                        // k second last
                        // l third last
                        dp[i][k][j][0] += dp[i - 1][l][k][0];
                        if(j == s[i] - '0' && k == s[i - 1] - '0' && l == s[i - 2] - '0')
                            dp[i][k][j][1] += dp[i - 1][l][k][1];
                        else if(j < s[i] - '0')
                            dp[i][k][j][0] += dp[i - 1][l][k][1];
                    }
                }
            }
        }
    }
    ll ret = 0;
    for(int i = 0; i <= 10; ++i)
        for(int j = 0; j < 10; ++j)
            ret += dp[s.size() - 1][i][j][0] + dp[s.size() - 1][i][j][1];
    return ret;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(NULL);
    ll a, b;
    cin >> a >> b;
    // find res for b and find res for a
    cout << solve(b) - solve(a - 1) << endl;
    return 0;
}

Compilation message

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