Submission #579895

# Submission time Handle Problem Language Result Execution time Memory
579895 2022-06-20T08:16:53 Z RealSnake Palindrome-Free Numbers (BOI13_numbers) C++14
100 / 100
1 ms 372 KB
#include "bits/stdc++.h"
using namespace std;

#define ll long long
#define mod 1000000007

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

ll n;
string s;
ll dp[20][2][11][11][2];

ll solve(ll ind, bool b, ll lst1, ll lst2, bool zero) {
    if(ind + 1 == n)
        return 1;
    if(dp[ind + 1][b][lst1][lst2][zero] != -1)
        return dp[ind + 1][b][lst1][lst2][zero];
    ll ans = 0;
    if(b == 1) {
        ll d = (s[ind + 1] - '0');
        for(ll i = 0; i <= d; i++) {
            if(!zero && (i == lst1 || i == lst2))
                continue;
            if(zero && (i == 0))
                ans += solve(ind + 1, (i == d), 10, 10, 1);
            else
                ans += solve(ind + 1, (i == d), i, lst1, (zero && (i == 0)));
        }
    }
    else {
        for(ll i = 0; i <= 9; i++) {
            if(!zero && (i == lst1 || i == lst2))
                continue;
            if(zero && (i == 0))
                ans += solve(ind + 1, 0, 10, 10, 1);
            else
                ans += solve(ind + 1, 0, i, lst1, (zero && (i == 0)));
        }
    }
    return dp[ind + 1][b][lst1][lst2][zero] = ans;
}

signed main() {

    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    string b;
    ll a;
    cin >> a >> b;
    s = b;
    n = s.size();
    memset(dp, -1, sizeof dp);
    ll ans = solve(-1, 1, 10, 10, 1);
    a--;
    if(a >= 0) {
        memset(dp, -1, sizeof dp);
        s = to_string(a);
        n = s.size();
        ans -= solve(-1, 1, 10, 10, 1);
    }
    cout << ans;

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